diff --git a/.gitignore b/.gitignore index 931d2cd..db06042 100644 --- a/.gitignore +++ b/.gitignore @@ -47,5 +47,5 @@ webapp/models/*.meta.json # 从头重跑时旧产物挪到这里(不入库) ml/archive/ -# onnxruntime-web 本地副本(bash ml/prep/vendor_ort.sh 生成,约 50 MB,不入库) -webapp/vendor/ort/ +# onnxruntime-web 本地副本(约 33 MB):**入库**,离线/无 npm 时直接可用,不再依赖 CDN(2026-10-18 用户要求) +# 来源 onnxruntime-web@1.20.1 dist,六个文件 + VERSION;ml/prep/vendor_ort.sh 在缺失时仍可补齐 diff --git a/docs/ML运行手册审查.md b/docs/ML运行手册审查.md index 7ab7f84..2fdcf3d 100644 --- a/docs/ML运行手册审查.md +++ b/docs/ML运行手册审查.md @@ -3,7 +3,7 @@ 六轮历史(`0218d7f` → `a5e779c`):第一轮 13 条、第二轮可行性、第三~五轮处置、第六轮本机实跑。**已解决的条目按用户要求删去,完整原文在 git 历史里**。本文件只剩三部分:仍有效的实测数字 / 仍未解决的问题 / 已复核正常的项,供实现方下一轮处置。 环境:本机 CBB-003(RTX 3090、28 核、Python 3.12 + torch 2.10 + onnxruntime 1.22、Edge + onnxruntime-web 1.20.1)。 -当前结论:第 1–6 步已实测可跑;**第 7–13 步零证据**;R4 部署口径(纯推理)已判通过,`bash ml/pipeline.sh r4` 端到端 rc=0(2026-10-18 本机修复后复跑)。A 组原三处中两处已修,只剩离线部署 A-3。 +当前结论:第 1–6 步已实测可跑;**第 7–13 步零证据**;R4 部署口径(纯推理)已判通过,`bash ml/pipeline.sh r4` 端到端 rc=0(2026-10-18 本机修复后复跑)。A 组原三处中两处已修,A-3 由实现方第七轮改掉、待本机复跑验证。**第七轮本机复核与显存预算(19–20 GB)见第七节。** --- @@ -43,8 +43,8 @@ tickGapMax 全部 ≈21.5 ms(20 ms 节拍没被卡住)。背景:第五轮 - `pipeline.sh` 7 处 `json.load(open(...))` 全部补 `encoding='utf-8'`(`:90 :110 :118 :127 :136 :138 :144`)。 - 附带发现并修掉:r4 分支末行 `[ "$r" = null ] && echo` 在 `r=pass` 时整体返回 1,`set -e` + EXIT trap 把**通过的 r4 记成 rc=1**(`all` 会因此停)。已改为 `if ... then ... fi`。 -**A-3 仍未修:`&ortLocal=1` + 默认 proxy 必然失败。** -`ik-policy-glue.js` 把 `ort.env.wasm.wasmPaths` 设为相对前缀 `vendor/ort/`,ORT 对 `.mjs` 走 `import()` 需要绝对说明符 → `no available backend found. ERR: [wasm] TypeError: Failed to resolve module specifier 'vendor/ort/ort-wasm-simd-threaded.mjs'`。README 明确支持的离线部署路径在默认配置下不可用。(`ortLocal=1` + `proxy=0` 未测;`wasmPaths` 与 proxy 无关,大概率同样失败。) +**A-3 离线 ort 加载:实现方第七轮已改(绝对 URL + `vendor_ort.sh`),本机待复跑验证。** +`ik-policy-glue.js` 把 `ort.env.wasm.wasmPaths` 设为相对前缀 `vendor/ort/`,ORT 对 `.mjs` 走 `import()` 需要绝对说明符 → `no available backend found. ERR: [wasm] TypeError: Failed to resolve module specifier 'vendor/ort/ort-wasm-simd-threaded.mjs'`。README 明确支持的离线部署路径在默认配置下不可用。(`ortLocal=1` + `proxy=0` 未测;`wasmPaths` 与 proxy 无关,大概率同样失败。)**复跑判据**:断网/CDN 不可达时 `bash ml/pipeline.sh r4` 仍能出数(探针默认走本地 `webapp/vendor/ort/`),且 `&ortLocal=1` 的查看器徽标显示「策略模式就绪」。 ### B. 待实现方判断(数据已摆在上面;分歧按第五轮第〇节规则,以实现方为准) @@ -60,12 +60,13 @@ tickGapMax 全部 ≈21.5 ms(20 ms 节拍没被卡住)。背景:第五轮 2. **3 mm 间隙过滤的真实剔除比例(`gapDrop`)没测过**:只测过沙箱 30 条(剔 9/29,不具代表性);上表 p50 4.4 mm 是未过滤口径。生成后看 `stats.json` 的 `gapDrop`。 3. **R2 新判据(`label_std_norm`)的池无关性**:需在 1.7 M 样本上 `--pool 50000` 与 `--pool 200000` 两跑、差 <0.02 才成立;没做。默认 `R2_STRICT=0` 只警告,开着就是一道未校准的闸。 4. **R8 出题耗时**:沙箱 2 题 9 分钟没出完;本机未测(`policy_bench.json` 会记 `requested` / `genS`,不足额时看警告)。 -5. **Windows 侧生命周期全部未实测**:关控制台黑窗口训练是否继续(`CREATE_NO_WINDOW`)、`/proc/$$/winpid` + `taskkill /T` 能否结束整树、Git Bash 自带 `timeout` 能否把 TERM 送到 python / node。 -6. **断点续跑的沙箱自测未在本机复核**:gen 截断续写(「续写:已有 N 条」)、train STOP 存档(rc 143、`last.pt` 第 N 步)、apg `--resume`、prep 二次运行「待打包 0 个」。 +5. ~~**Windows 侧生命周期未实测**~~ → **已实测**(见 7.2):整树杀净 ✅、关黑窗口训练继续 ✅、`timeout` 的 TERM 送不到 python ❌(超时只能硬杀,靠 STOP 文件与定时存档)。 +6. ~~**断点续跑未在本机复核**~~ → **已实测**(见 7.2):gen 按条续写逐条一致、train STOP 2 s 存档续训、apg `--resume`、prep 二次运行「待打包 0 个」全部通过。 +7. **APG 的机时仍然零实测**(显存已量:`batch=48/plans=150` → 13.08 GiB)。`batch` 从 128 降到 48 后每步代价与收敛都要重算一遍才知道。 ### D. 小遗留 -`webapp/models/README.md` 过时:还写"由 `--fp16` 生成"、指向"第 8 步"(现为第 10 步、默认 fp32)。 +~~`webapp/models/README.md` 过时~~ → 实现方第七轮已改,本机复核过:现在写的是 `bash ml/pipeline.sh export`(默认 fp32)、第 10 步、新输入签名 `feat[1 或 K,83]` 与 `feat_dyn`,并说明离线要先跑 `ml/prep/vendor_ort.sh`。 --- @@ -81,7 +82,7 @@ tickGapMax 全部 ≈21.5 ms(20 ms 节拍没被卡住)。背景:第五轮 1. ~~A 组修复并重跑 `r4`~~:已完成(本机 2026-10-18,rc=0、P99 11.25)。 2. 接着 `FROM=gen TO=prep` 跑一次(数小时),拿 `gapDrop` 与 R2 两个口径的数——C-2 / C-3 随之消掉,B-2 的 Node 侧安全层数字可顺手从 m5 前的 bench 拿。 -3. B 组等上面的数字齐了再定;C-1 / C-5 / C-6 随第一次真跑逐项核对;A-3(离线 ort 加载)交给实现方。 +3. B 组等上面的数字齐了再定;C-1/C-7 随第一次真跑逐项核对;A-3 由实现方已改,本机复跑 `r4` 时顺带验(会弹有界面窗口)。 --- @@ -179,3 +180,106 @@ V1 通过后再跑,分三段,每段跑完把回传文件发我: | 后训练与评测 | `FROM=apg TO=accept bash ml/pipeline.sh all` | 约 10–20 小时 | `ml/runs/acceptance.json`、`policy_bench.json`、`r9_browser.json` | 任何一段中途出错:把出错信息发我,我修完后用 `FROM=<出错阶段>` 接着跑,前面的不重做。 + +--- + +## 七、本机复核第六/七轮处置(2026-10-18,含显存预算) + +**先说约束(用户定的,之前所有估算都按整卡 24 GB 写了)**:本机 Windows + 4K 桌面**空闲就占 4.4 GB**(`nvidia-smi` 空载 23604→4394 MiB 实测),所以 **ML 只能用 19–20 GB**。 + +### 7.1 APG 会直接把第 8 步打崩(实测,非推算) + +用 d=192(与正式训练同结构)、`--steps 1`(峰值在第一次反向就到位)、每进程硬帽 19.2 GB,逐档实测 `max_memory_allocated`(GiB): + +| batch | plans=60 | 90 | 120 | 150 | +|---|---|---|---|---| +| 32 | 6.14 | | | 9.53 | +| **48(已定为新值)** | 7.67 | | | **13.08** | +| 56 | | | 13.61 | | +| 64 | 10.81 | 15.41 | | 17.66 | +| **128(原值)** | **OOM** | | | **OOM**(崩前已分配 17.85 GiB) | + +- **原 `batch=128` 无论 `plans` 取 60 还是 150 都 OOM**(d=32 的小模型也一样,崩前 17.54 GiB)→ 显存是被展开图吃掉的,与模型大小、数据量无关。 +- **已把 `apg.json` 的 batch 定为 48,`plans 60→150` 不动**:最坏 13.08 GiB,加桌面 4.4 GB ≈ 17.5 GB,在 19–20 GB 预算内。没选"降 plans"是因为降展开长度会把长任务的绕出段截掉(就是口径-3 那个问题)。`batch=64 + plans=150` 要 17.66 GiB,加桌面就到 22 GB 了,不取。 +- **加了每进程显存硬帽**:`ml/common/vram.py`,`VRAM_FRAC` 默认 0.78 ≈ 18.7 GiB,训练与 APG 启动时打印上限。没帽时它会把整卡顶到 23.6 GB、连桌面一起挤掉(就是这次报显存的原因)。 +- **显存用量现在看得见**:`train.py` / `apg_finetune.py` 每条日志带 `vram`(峰值 GiB);监控页指标卡加「显存峰值」(>14 黄、>17 红),GPU 卡的显存条改成按 **20 GB 预算**算而不是整卡 24。实测:预训练 `batch=1024` 记到 `vram: 2.71`,APG 自检档 `vram: 0.33`。 +- 对照:预训练 `batch=1024` 峰值 **2.76 GiB**(够);`check_r2` 每样本常驻 **776 B** → 1.7 M 样本 **1.32 GB** + 分块 0.05(4× 池自检 0.2)≈ **1.6 GB**,脚本里"GPU 峰值 <0.5 GB"错了约 3 倍,但不会崩。 +- 顺带:N-6 的"按空闲显存 1/4 自动定块"、出题分块等估算都要按 19 GB 重算。 +- **`ml/configs/smoke.json` 原来没有 `apg` 段**,而 `selftest.sh` 调 APG 时不带 `--config` → 自检第 6 项实际按正式 `batch=128` 跑,在本机必然 OOM、自检永远跑不完。已补 `apg` 段(`batch=8`、实测 `vram` 0.33 GiB)并让自检带 `--config`。 + +### 7.2 生命周期与断点续跑:本机整轮自检 **7/8 通过**,用时 3.5 分钟 + +| 项 | 结果 | +|---|---| +| `gen_resume` | ✅ true(判据修正后)——「续写:已有 2/6 条,从第 3 条继续」,与一口气生成**逐条一致** | +| `prep_rerun` | ✅ true——二次运行「待打包 0 个」 | +| `train_stop` | ✅ true——`--stop-at 20` → rc 143、`last.pt` 第 20 步、续训到 40 步 rc 0 | +| `train_stopfile` | ✅ true——写 `ml/runs/STOP` 后 **2 s** 退出并存档(第 351 步)。控制台「停止」走的就是这条路 | +| `train_term` | ❌ **false(Windows 的真实限制)**——`timeout` 的 TERM 送不到原生 python,rc=124 是硬杀。结论:阶段超时不能指望体面存档,靠 STOP 文件 + 每 10 分钟定时存档 | +| `apg_resume` | ✅ true(自检档 `batch=8`,`vram` 0.33 GiB) | +| `tree_killed` | ✅ true(实测)——`taskkill /T /F` 把 bash→python+node 整树杀净,没留孤儿 | +| `survive_parent_exit` | ✅ true(实测)——启动器退出后子树仍活着 = **关掉控制台黑窗口不影响训练** | + +用时 3.5 分钟(APG 用自检档;上一轮 APG 走正式 `batch=128` 时是 13.2 分钟,而且 OOM)。 +之前测不到的原因:① `monitor.find_bash()` 在本机返回 `None`(Qoder 自带 git 在 `mingw64\bin\git.exe`,"上上级 + bin/bash.exe"推出一个不存在的路径);② `selftest_proc.alive()` 用 `text=True` 读 `tasklist`,中文 Windows 的表头是 GBK → 读线程 `UnicodeDecodeError`、`stdout` 变 `None` → `TypeError`,异常又被 `wait_pids` 的 `except Exception` 吞掉。 + +### 7.3 两条判据自身的问题(会让"通过/失败"失真) + +1. **`selftest.sh:27` 用 `cmp -s` 逐字节比生成结果**,但记录里含 `plan_ms` 与 `cert.ms` 两个**墙钟耗时字段** → 两次运行不可能相同,**这项永远是 false**。我在副本里重跑基线、只把这两个字段剔掉后逐行比:**6 条完全一致** → 续写逻辑正确,是判据错。 +2. **`rec()` 用 `json.loads` 解析传入值**,而 `str(None).lower()`=`none` 不是合法 JSON → 抛异常、整项**静默不进结果文件**,`all_pass` 照样算出来。所以"第七轮说自检已修完"的两项进程测试,实际上是没跑过。 +3. **自检副本只复制 `git ls-files` 里的文件**(`selftest.sh:16`)→ 任何**未跟踪的新文件**在副本里不存在,调用它的项会以 `ModuleNotFoundError` 变成 rc=1,看起来像功能坏了。本轮新加 `ml/common/vram.py` 就踩了:`train_stop`/`train_stopfile`/`apg_resume` 三项同时 rc=1,`git add` 之后才恢复。判据建议:自检开头先 `git ls-files --others --exclude-standard` 非空就报出来(或改用 `git ls-files -o --exclude-standard -t` 一起复制)。 + +### 7.4 本机已改的(都按实测落地,不改判据含义) + +| 位置 | 改动 | 验证 | +|---|---|---| +| `ml/monitor.py` `find_bash()` | 便携版 git 的 `mingw64` 布局要再上一层;再兜 `which('bash')`(排除 System32 的 WSL bash) | 返回 `...\.qoder\bin\git\bin\bash.exe`(bash 5.2.37);网页「开始训练」在本机的拦路石解除 | +| `ml/selftest_proc.py` `alive()` / `wait_pids()` | `tasklist` 用 `text=True, errors='replace'` 并防 `None`;起不来时把最后一次异常带进结果 | `tree_killed`、`survive_parent_exit` 两项实测 true | +| `ml/selftest.sh` | `rec` 解析失败记 false 不再丢项;`gen_resume` 判据剔除 `plan_ms`/`cert.ms`;APG 两项带 `--config` | 重跑第 1 项已变 **true**(「续写:已有 2/6 条 → 逐条一致」) | +| `ml/configs/apg.json` | `batch 128 → 48`,加 `_vram` 记录实测表 | 最坏 13.08 GiB(实测),在 19–20 GB 预算内 | +| `ml/configs/smoke.json` | 补 `apg` 段(`batch=8`) | 自检 APG 档 `vram: 0.33` | +| `ml/common/vram.py`(新)+ `train.py` + `apg/apg_finetune.py` | 每进程硬帽 `VRAM_FRAC=0.78`(启动打印上限);日志每条记录加 `vram` 峰值 | `显存上限:本进程 18.7 GiB(整卡 24.0 × 0.78)`;train `vram: 2.71` | +| `ml/monitor.py` + `ml/monitor.html` | 状态带 `vram_budget_gb`(默认 20,`VRAM_BUDGET_GB` 可改);GPU 显存条按预算算并超线变色;指标卡加「显存峰值」 | headless 加载渲染出的 DOM 里有「显存峰值 / 预算 19–20 GB(整卡 24,桌面占 4–5)」 | + +### 7.5 仍需实现方处理的(我没动) + +- **APG 的 `steps` 与机时**:`batch` 从 128 降到 48,每步看到的任务少 2.7 倍。`steps=3000` 要不要加、加多少是收敛判断,我只有显存实测、没有这个依据。`R2/R4/N-6` 各处按整卡写的显存估算文案也要按 19–20 GB 重写。 +- `R4 复跑`:`feat_dyn` 要重导模型,探针现在会**弹有界面窗口**跑 `viewer_ref`(30 题×≤10 s)与 WebGPU——本机跑之前请知悉会有窗口,别动它。 +- B-2 的几何查询搬 Worker(若本机 R9 仍不过);R9 用真模型才有意义。 + +--- + +## 八、R4 复跑(2026-10-18,`feat_dyn` 新模型 + 本地 ort + WebGPU) + +`bash ml/pipeline.sh r4` 端到端 **rc=0**。这次先把 `webapp/vendor/ort/`(onnxruntime-web 1.20.1,33 MB)**入库**(`.gitignore` 同步移除),不再依赖 npm/CDN——上一轮 `vendor_ort.sh` 的 `npm pack` 失败后探针静默退回 CDN,而 jsDelivr 本机完全不通,四种配置全变成「加载 ort 失败」,一条数字都没有。**这是个真问题**:下载失败时流程不该"看起来跑完了"。 + +### 8.1 R4 判定口径:通过 + +| 配置 | P50 | P95 | P99 | 备注 | +|---|---|---|---|---| +| **wasm·16 线程·proxy(部署口径)** | 7.06 | 8.88 | **12.67** | 本地 ort,`enc_once` + `feat_dyn` 新模型 | +| wasm·4 线程·proxy | 10.25 | 12.48 | 13.22 | | +| wasm·16 线程·无 proxy | 6.83 | 8.28 | 9.53 | | +| **WebGPU** | 18.26 | 22.76 | **27.09** | 首次拿到真数:`hasGpu:true`、adapter `nvidia ampere`、有界面窗口;比 wasm 慢,不用 | + +结论:纯推理 P99 12.67 < 15,**R4 通过**;与修复前(10.22–11.25)同量级,`feat_dyn` 没让主推理变慢(主推理仍只送 1 行)。 + +### 8.2 `viewer_ref`:随机模型的整周期数字(只作参考,但暴露了三件事) + +30 道贴柱题、随机权重、真查看器(有界面窗口)、16 线程 + proxy: + +| 指标 | 值 | 说明 | +|---|---|---| +| `planP50` / `planP99` | 68.5 / **131.2 ms** | R9 线是 45;随机策略几乎每次都认证多条候选 | +| `starve_rate` | **32.7%** | 执行时钟到点队列为空 = 实际丢步 | +| `inferP99` | 58.3 ms | 纯推理探针只有 12.7 → 差额是主线程排队 | +| `lookaheadP99` | **170 ms** | 前瞻(8 行特征逐行编码 + 8 次 gapMatrix) | +| `certFailWhy` | **time 948** / box 346 / cert 19 | 3902 次规划里 70% 的失败是"墙钟到点判不过" | +| `tickGapMax` | **736 ms** | 主线程仍会被一次性占住(安全层在主线程) | + +**三条判读(给实现方,不是结论)**: +1. 前瞻是现在最贵的一环(170 ms P99)。虽然已改成只在冷启动做,但 30 题就是 30 次冷启动,每次都落在 P99 上——R9 的 `planP99` 会被冷启动那几次主导。建议 `r4_probe --viewer` 同时报 `planP99`(含冷启动)与 `planP99Warm`(剔除每题第一次规划),否则这条线测的是"题数"而不是稳态性能。 +2. **`time` 截止占失败原因 70%**:45 ms 预算在随机模型上基本等于"到点判不过 → 刹停"(1019 次刹停 / 3902 次规划)。R9 若"通过",必须同时报 `certFailWhy.time` 占比与 `brakeRate`,否则可能是靠拒绝候选换来的。 +3. `tickGapMax 736 ms` 说明安全层(几何查询)仍在主线程;B-2 的"搬进 Worker"没有做,这条是真模型跑之前要盯的。 + +这些全是随机权重的数,R9 判据本来就规定用训练好的模型测,所以**不据此判 R9 过不过**;`r4.json` 里 `viewer_ref` 已标注"仅参考"。 diff --git a/ml/apg/apg_finetune.py b/ml/apg/apg_finetune.py index 421a3b8..ae20075 100644 --- a/ml/apg/apg_finetune.py +++ b/ml/apg/apg_finetune.py @@ -17,6 +17,7 @@ from ml.model.policy import build, wta_loss from ml.model.meanflow import meanflow_loss from ml.sim.closed_loop import evaluate, tasks_to_tensors, goal_err from ml.common.ckpt import Saver, atomic_save, rng_state, set_rng_state +from ml.common import vram def load_pool(paths): @@ -38,6 +39,7 @@ def main(): cfg = json.load(open(a.config)); C = cfg['apg'] if a.steps: C['steps'] = a.steps dev = a.device; torch.manual_seed(a.seed); random.seed(a.seed) + vram.limit() os.makedirs(a.out, exist_ok=True) ck = torch.load(a.init, map_location=dev) mcfg = ck['cfg'] @@ -147,7 +149,7 @@ def main(): with torch.no_grad(): e, oe = goal_err(kin, q, T, R) rec = {'it': it, 'total': C['steps'], 't': time.time(), 'plans': n_plans, 'err_mm_mean': e.mean().item(), 'reach_frac': ((e < 1) & (oe < 0.5)).float().mean().item(), 'bc': l_bc.item(), - 'gnorm': gn.item(), **stats, 'min': (time.time() - t0) / 60} + 'gnorm': gn.item(), **stats, 'min': (time.time() - t0) / 60, 'vram': vram.peak_gib()} if it % 10 == 0: print(json.dumps(rec), flush=True) log.write(json.dumps(rec) + '\n'); log.flush() if saver.stop() or (a.stop_at and it + 1 >= a.stop_at): diff --git a/ml/common/vram.py b/ml/common/vram.py new file mode 100644 index 0000000..2009601 --- /dev/null +++ b/ml/common/vram.py @@ -0,0 +1,21 @@ +"""显存预算(2026-10-18 本机实测后加)。 +用户定的预算:这台 3090 只有 19–20 GB 能给训练用——Windows + 4K 桌面常驻就占掉 4–5 GB(空载 nvidia-smi 4.4 GB)。 + limit() 给本进程设硬帽(VRAM_FRAC,默认 0.78 ≈ 18.7 GiB)。超限时在自己的反向里早失败, + 而不是把桌面挤到没有显存(整机卡顿、别的应用崩),这也是 APG batch=128 原先 OOM 时把显存顶到 23.6 GB 的原因。 + peak_gib() 记进日志的 vram 字段:网页指标卡与 ml/selftest.sh 都按它核对预算,不用再去猜。 +实测参考(d=192 同结构、1 步即到位):预训练 batch=1024 → 2.76;APG batch=48/plans=150 → 13.08、batch=128 → OOM。""" +import os +import torch + + +def limit(): + if not torch.cuda.is_available(): return False + f = float(os.environ.get('VRAM_FRAC', '0.78')) + tot = torch.cuda.get_device_properties(0).total_memory / 2**30 + torch.cuda.set_per_process_memory_fraction(f) + print('显存上限:本进程 %.1f GiB(整卡 %.1f GiB × %.2f,其余留给桌面)' % (f * tot, tot, f), flush=True) + return True + + +def peak_gib(): + return round(torch.cuda.max_memory_allocated() / 2**30, 2) if torch.cuda.is_available() else None diff --git a/ml/configs/apg.json b/ml/configs/apg.json index 209510d..474f3e3 100644 --- a/ml/configs/apg.json +++ b/ml/configs/apg.json @@ -1,7 +1,8 @@ { "apg": { - "batch": 128, "lr": 1e-4, "steps": 3000, "grad_clip": 1.0, "amp": "bf16", + "batch": 48, "lr": 1e-4, "steps": 3000, "grad_clip": 1.0, "amp": "bf16", "plans": 60, "plans_max": 150, "seg_plans": 10, + "_vram": "batch 由 128 降到 48:2026-10-18 本机实测(d=192 同结构、每进程 19.2 GB 硬帽)batch=128 无论 plans 取 60 还是 150 都 OOM(崩前已分配 17.85 GiB);batch=48/plans=150 峰值 13.08 GiB、plans=60 为 7.67 GiB,加 Windows+4K 桌面常驻 4.4 GB 仍在 19–20 GB 预算内。batch=64/plans=150 要 17.66 GiB(不取)。展开长度 plans 没动——降 plans 会把长任务的绕出段截掉。", "margin_train_mm": 3.0, "w_tgt": 1.0, "w_rot": 0.3, "w_col": 2.0, "w_smooth": 0.02, "w_len": 0.001, "w_bc": 0.5, "cat_weights": {"A": 0.4, "B": 1.0, "C": 1.6, "D": 0.6, "Dr": 0.6}, diff --git a/ml/configs/smoke.json b/ml/configs/smoke.json index 5cc09f2..7fea377 100644 --- a/ml/configs/smoke.json +++ b/ml/configs/smoke.json @@ -31,5 +31,13 @@ "log_every": 10, "patience_evals": 5 }, - "_note": "冒烟专用(bash ml/smoke.sh):只验证流程能跑通,数值无意义" + "apg": { + "batch": 8, "lr": 0.0001, "steps": 10, "grad_clip": 1.0, "amp": "bf16", + "plans": 20, "plans_max": 30, "seg_plans": 5, + "margin_train_mm": 3.0, + "w_tgt": 1.0, "w_rot": 0.3, "w_col": 2.0, "w_smooth": 0.02, "w_len": 0.001, "w_bc": 0.5, + "cat_weights": {"A": 0.4, "B": 1.0, "C": 1.6, "D": 0.6, "Dr": 0.6}, + "ckpt_min": 10, "eval_every": 1000, "eval_tasks": 4 + }, + "_note": "冒烟与自检专用(bash ml/pipeline.sh selftest / smoke):只验证流程能跑通,数值无意义。apg 段必须有:selftest.sh 第 6 项原先不带 --config,走的是正式 apg.json 的 batch=128,本机实测第一次反向就 OOM;改成正式值 48 也要约 13 GiB,自检不必吃这么多。" } \ No newline at end of file diff --git a/ml/monitor.html b/ml/monitor.html index 250221b..04f88bd 100644 --- a/ml/monitor.html +++ b/ml/monitor.html @@ -436,6 +436,7 @@ function renderKpis() { tile('展开内到达比例', pct(r.reach_frac), '误差 < 1 mm 且姿态 < 0.5°', '', last('reach_frac'), COLORS[2]); tile('闭环到达率', pct(e.success) + dlt('success'), `最好 ${pct(best)}`, '', evs('success'), COLORS[2]); tile('碰撞率', pct(e.collision_rate, 2) + dlt('collision_rate', -1), '关安全层', e.collision_rate > 0.03 ? 'warn' : '', evs('collision_rate'), COLORS[3]); + tile('显存峰值', isNum(r.vram) ? `${r.vram.toFixed(2)}GiB` : '—', '预算 19–20 GB(整卡 24,桌面占 4–5)', r.vram > 17 ? 'bad' : r.vram > 14 ? 'warn' : '', last('vram'), COLORS[1]); } else { const sps = r.samples_per_s; tile('总损失', fmt(r.loss, 4), `MF ${fmt(r.mf)} · FK ${fmt(r.fk_cm)} cm`, '', ema(last('loss'), 0.9), COLORS[0]); @@ -443,6 +444,7 @@ function renderKpis() { tile('闭环到达率', pct(e.success) + dlt('success'), evals.length ? `最好 ${pct(best)} · @${e.x.toLocaleString()} 步` : '尚未评估', '', evs('success'), COLORS[2]); tile('碰撞率', pct(e.collision_rate, 2) + dlt('collision_rate', -1), '关安全层,部署另有护栏', e.collision_rate > 0.03 ? 'warn' : '', evs('collision_rate'), COLORS[3]); tile('梯度范数', fmt(r.gnorm), '裁剪阈值 1.0', r.gnorm > 5 ? 'warn' : '', ema(last('gnorm'), 0.9), COLORS[4]); + tile('显存峰值', isNum(r.vram) ? `${r.vram.toFixed(2)}GiB` : '—', '预算 19–20 GB(整卡 24,桌面占 4–5)', r.vram > 17 ? 'bad' : r.vram > 14 ? 'warn' : '', last('vram'), COLORS[1]); } $('kpis').innerHTML = tiles.map(t => t.html).join(''); [...$('kpis').querySelectorAll('canvas')].forEach((cv, i) => spark(cv, tiles[i].sp, tiles[i].color)); @@ -477,7 +479,7 @@ async function poll() { render(); } const P = lastStatus.pipeline, busy = (job && job.running) || (P && P.current); - if (DEMO) { setPill('demo', '示例数据'); if (!$('demobar')) $('pipe').insertAdjacentHTML('beforebegin', `
这是示例数据,不是你的训练。${location.protocol === 'file:' ? '直接双击 monitor.html 只能看示例;' : ''}看真实训练请双击 ml/console.bat(或 python -m ml.monitor --open)。
`); } + if (DEMO) { setPill('demo', '示例数据'); if (!$('demobar')) $('pipe').closest('.hero').insertAdjacentHTML('beforebegin', `
这是示例数据,不是你的训练。${location.protocol === 'file:' ? '直接双击 monitor.html 只能看示例;' : ''}看真实训练请双击 ml/console.bat(或 python -m ml.monitor --open)。
`); } else if (busy) setPill('live', `运行中 · ${P && P.current ? SM[P.current]?.n || P.current : '启动中'}`); else if (cur && Date.now() / 1000 - lastMtime < 120) setPill('live', `运行中 · ${ago(lastMtime)}更新`); else setPill('', '空闲'); @@ -531,15 +533,16 @@ function renderStatus(s) { // GPU const G = s.gpu || []; + const BUD = (s.vram_budget_gb || 24) * 1024; // 显存预算(MB):桌面常驻占 4–5 GB,不能按整卡 24 GB 看 $('gputime').textContent = G.length ? '实时' : ''; $('gpu').innerHTML = G.length ? G.map(g => { const u = g.util ?? 0, C = 2 * Math.PI * 50, col = u > 85 ? css('--ok') : u > 50 ? css('--accent') : css('--warn'); - const memP = 100 * g.mem_used / g.mem_total, tc = g.temp > 83 ? css('--bad') : g.temp > 75 ? css('--warn') : css('--c6'); + const memP = 100 * g.mem_used / BUD, tc = g.temp > 83 ? css('--bad') : g.temp > 75 ? css('--warn') : css('--c6'); const row = (k, v, p, c) => `
${k}${v}
`; return `
${u}%利用率
-
${row('显存', `${(g.mem_used / 1024).toFixed(1)} / ${(g.mem_total / 1024).toFixed(0)} GB`, memP, css('--accent2'))} +
${row('显存', `${(g.mem_used / 1024).toFixed(1)} / ${(BUD / 1024).toFixed(0)} GB 预算(整卡 ${(g.mem_total / 1024).toFixed(0)})`, memP, memP > 100 ? css('--bad') : memP > 85 ? css('--warn') : css('--accent2'))} ${row('温度', `${g.temp} °C`, g.temp, tc)}${row('功耗', g.power != null ? `${Math.round(g.power)} W` : '—', g.power ? g.power / 3.5 : 0, css('--c7'))}
${g.name}
`; }).join('
') : empty('未检测到 nvidia-smi'); diff --git a/ml/monitor.py b/ml/monitor.py index 17d543d..40f2e80 100644 --- a/ml/monitor.py +++ b/ml/monitor.py @@ -192,7 +192,8 @@ def status(): pb = jload(P(RUNS, 'policy_bench.json')) if pb: pb = {k: {kk: v.get(kk) for kk in ('n', 'ok', 'reached', 'collisionFrames', 'brakeRate', 'recoveryRate', 'livelock', 'inferMsP99', 'safetyMsP99')} for k, v in pb.items() if isinstance(v, dict) and 'n' in v} - return {'now': time.time(), 'gpu': gpu(), 'pipeline': pl, 'check': CHECK.get(), 'lock': lock_info(), + return {'now': time.time(), 'gpu': gpu(), + 'vram_budget_gb': float(os.environ.get('VRAM_BUDGET_GB', 20)), # 本机桌面常驻占 4–5 GB,给训练的预算 19–20 GB 'pipeline': pl, 'check': CHECK.get(), 'lock': lock_info(), 'stage_meta': [{'id': k, 'name': n, 'est_h': h, 'timeout_h': pstate.TIMEOUT_H[k]} for k, n, h in pstate.STAGES], 'r4': jload(P(RUNS, 'r4.json')), 'data': {'consistency': (jload(P(DATA, 'consistency.json')) or {}).get('consistentByGateRule'), @@ -218,8 +219,13 @@ def find_bash(): if p and os.path.exists(p): return p g = shutil.which('git') if g: - p = os.path.join(os.path.dirname(os.path.dirname(g)), 'bin', 'bash.exe') - if os.path.exists(p): return p + root = os.path.dirname(os.path.dirname(g)) # 便携版 git 常放在 <根>\mingw64\bin\git.exe,要再上一层才找到 bash + for base in dict.fromkeys([root, os.path.dirname(root)]): + for sub in ('bin', os.path.join('usr', 'bin')): + p = os.path.join(base, sub, 'bash.exe') + if os.path.exists(p): return p + b = shutil.which('bash') + if b and 'windows\\system32' not in b.lower(): return b return None return shutil.which('bash') diff --git a/ml/selftest.sh b/ml/selftest.sh index 9e90000..432438a 100755 --- a/ml/selftest.sh +++ b/ml/selftest.sh @@ -17,14 +17,21 @@ git ls-files -z | tar --null -T - -cf - | (cd "$D" && tar xf -) [ -d ml/node_modules ] && cp -r ml/node_modules "$D/ml/node_modules" cd "$D"; mkdir -p ml/runs ml/data R=ml/runs/selftest_items.jsonl; : > $R -rec() { $PY -c "import json,sys;print(json.dumps({'item':sys.argv[1],'pass':json.loads(sys.argv[2]),'detail':sys.argv[3]},ensure_ascii=False))" "$1" "$2" "$3" >> $R; echo "[selftest] $1 → $2 $3"; } +rec() { $PY -c "import json,sys +try: v=json.loads(sys.argv[2]) +except Exception: v=False # 值不是合法 JSON 时记 false,不能让整项静默消失(第七轮实测:'none' 抛异常 → 两项没进结果) +print(json.dumps({'item':sys.argv[1],'pass':v,'detail':sys.argv[3]},ensure_ascii=False))" "$1" "$2" "$3" >> $R; echo "[selftest] $1 → $2 $3"; } CFGS=ml/configs/smoke.json # 1 生成续写 node ml/gen/gen_dataset.cjs --n 6 --seed 11 --out ml/data/a.jsonl 2>/dev/null node ml/gen/gen_dataset.cjs --n 3 --seed 11 --out ml/data/shard_0011.jsonl.part 2>/dev/null sz=$(wc -c < ml/data/shard_0011.jsonl.part); head -c $((sz - 40)) ml/data/shard_0011.jsonl.part > t && mv t ml/data/shard_0011.jsonl.part node ml/gen/gen_dataset.cjs --n 6 --seed 11 --out ml/data/shard_0011.jsonl.part 2>ml/runs/st_gen.log; mv ml/data/shard_0011.jsonl.part ml/data/shard_0011.jsonl -cmp -s ml/data/a.jsonl ml/data/shard_0011.jsonl && rec gen_resume true "$(grep -o '续写[^,]*' ml/runs/st_gen.log | head -1)" || rec gen_resume false "续写结果与一口气生成不同" +gd=$($PY -c "import json,sys +f=lambda p:[json.dumps({**r,'plan_ms':0,'cert':{**r.get('cert',{}),'ms':0}},sort_keys=True) for r in (json.loads(l) for l in open(p,encoding='utf-8') if l.strip())] +a,b=f(sys.argv[1]),f(sys.argv[2]);print('OK' if a==b else '不同 %d/%d 条'%(sum(x!=y for x,y in zip(a,b))+abs(len(a)-len(b)),max(len(a),len(b))))" ml/data/a.jsonl ml/data/shard_0011.jsonl) +if [ "$gd" = OK ]; then rec gen_resume true "$(grep -o '续写[^,]*' ml/runs/st_gen.log | head -1)(逐条一致;不含 plan_ms/cert.ms 两个墙钟字段,第七轮实测 cmp -s 恒不等)" +else rec gen_resume false "续写与一口气生成不同:$gd"; fi rm -f ml/data/a.jsonl node ml/gen/gen_dataset.cjs --n 40 --seed 12 --out ml/data/shard_0012.jsonl 2>/dev/null # 训练自检用的小数据 # 2 打包二次运行 @@ -49,13 +56,16 @@ if command -v timeout >/dev/null; then grep -q "收到停止请求" ml/runs/st_c.log && rec train_term true "rc=$rc,TERM 送达并存档" || rec train_term false "rc=$rc,TERM 没送到 python(Windows 原生进程常见)→ 阶段超时只能硬杀,进度靠每 10 分钟存档" else rec train_term false "没有 timeout 命令"; fi # 6 APG 续跑 -$PY -m ml.apg.apg_finetune --init ml/runs/st_a/last.pt --samples ml/data/samples --out ml/runs/st_apg --steps 10 --stop-at 5 > ml/runs/st_apg.log 2>&1; rc=$? -$PY -m ml.apg.apg_finetune --init ml/runs/st_a/last.pt --samples ml/data/samples --out ml/runs/st_apg --steps 10 --resume ml/runs/st_apg/last.pt >> ml/runs/st_apg.log 2>&1; rc2=$? +$PY -m ml.apg.apg_finetune --config $CFGS --init ml/runs/st_a/last.pt --samples ml/data/samples --out ml/runs/st_apg --steps 10 --stop-at 5 > ml/runs/st_apg.log 2>&1; rc=$? +$PY -m ml.apg.apg_finetune --config $CFGS --init ml/runs/st_a/last.pt --samples ml/data/samples --out ml/runs/st_apg --steps 10 --resume ml/runs/st_apg/last.pt >> ml/runs/st_apg.log 2>&1; rc2=$? [ $rc = 143 ] && [ $rc2 = 0 ] && rec apg_resume true "" || rec apg_resume false "rc=$rc 续跑rc=$rc2(日志 $D/ml/runs/st_apg.log)" # 7 进程树 / 关窗口 pj=$($PY ml/selftest_proc.py 2>ml/runs/st_proc.err | tail -1) echo "$pj" > ml/runs/st_proc.json -k=$($PY -c "import json,sys;d=json.loads(sys.argv[1]);print(str(d.get('tree_killed')).lower(), str(d.get('survive_parent_exit')).lower())" "$pj" 2>/dev/null || echo "null null") +k=$($PY -c "import json,sys +try: d=json.loads(sys.argv[1]) +except Exception: d={} +print(json.dumps(d.get('tree_killed')), json.dumps(d.get('survive_parent_exit')))" "$pj" 2>/dev/null || echo "null null") rec tree_killed ${k% *} "$pj" ; rec survive_parent_exit ${k#* } "" mkdir -p "$SRC/ml/runs" $PY - "$R" "$SRC/ml/runs/selftest.json" "$D" <<'PY' diff --git a/ml/selftest_proc.py b/ml/selftest_proc.py index 0724988..0224fbd 100644 --- a/ml/selftest_proc.py +++ b/ml/selftest_proc.py @@ -14,8 +14,9 @@ FLAGS = {'creationflags': subprocess.CREATE_NEW_PROCESS_GROUP | 0x08000000} if o def alive(pid): if os.name == 'nt': - r = subprocess.run(['tasklist', '/FI', f'PID eq {pid}', '/NH'], capture_output=True, text=True) - return str(pid) in r.stdout + # 中文 Windows 的 tasklist 表头是 GBK,text=True 会让读线程解码崩掉(stdout 变 None)→ 按字节读再容错解码 + r = subprocess.run(['tasklist', '/FI', f'PID eq {pid}', '/NH'], capture_output=True, text=True, errors='replace') + return str(pid) in (r.stdout or '') try: os.kill(pid, 0); return True except OSError: return False @@ -26,11 +27,14 @@ def tree_cmd(d): f'node -e "require(\'fs\').writeFileSync(\'{dd}/n.pid\',String(process.pid));setInterval(()=>{{}},1000)" & wait') +WAITERR = [''] + + def wait_pids(d, t=20): for _ in range(t * 4): try: return int(open(os.path.join(d, 'p.pid')).read()), int(open(os.path.join(d, 'n.pid')).read()) - except Exception: time.sleep(0.25) - return None, None + except Exception as e: WAITERR[0] = type(e).__name__ + ': ' + str(e)[:90]; time.sleep(0.25) + return None, None # 起不来时把最后一次异常带进结果,别让 tree_started=false 变成没有原因的失败 def main(): diff --git a/ml/train.py b/ml/train.py index fcfab9a..2706eab 100644 --- a/ml/train.py +++ b/ml/train.py @@ -9,6 +9,7 @@ import torch from ml.common.kin_torch import Kin from ml.common.features import STEP, D2R from ml.common.ckpt import Saver, atomic_save, rng_state, set_rng_state +from ml.common import vram from ml.data import Samples, load_tasks from ml.model.policy import build, wta_loss from ml.model.meanflow import meanflow_loss, one_step @@ -40,6 +41,7 @@ def main(): os.makedirs(a.out, exist_ok=True) torch.manual_seed(a.seed) dev = a.device + vram.limit() kin = Kin(device=dev) data = Samples(a.samples, device=dev, split='train') val_tasks = [t for t in load_tasks(a.samples) if t['split'] == 'val'][:tc['eval_tasks']] @@ -111,7 +113,7 @@ def main(): el = time.time() - t0 rec = {'step': step, 'total': total, 't': time.time(), 'loss': loss.item(), 'mf': mf_raw.item(), 'fk_cm': l_fk.item(), 'smooth': l_sm.item(), 'value': l_val.item(), 'sub': l_sub.item(), 'gnorm': gn.item(), 'lr': lr_at(step), 'samples_per_s': seen / max(el, 1e-6), - 'eta_h': (total - step - 1) * tc['batch'] / max(seen / max(el, 1e-6), 1e-6) / 3600} + 'eta_h': (total - step - 1) * tc['batch'] / max(seen / max(el, 1e-6), 1e-6) / 3600, 'vram': vram.peak_gib()} print(json.dumps(rec), flush=True); log.write(json.dumps(rec) + '\n'); log.flush() if not a.no_eval and (step + 1) % tc['eval_every'] == 0: model.eval() diff --git a/webapp/vendor/ort/VERSION b/webapp/vendor/ort/VERSION new file mode 100644 index 0000000..0044d6c --- /dev/null +++ b/webapp/vendor/ort/VERSION @@ -0,0 +1 @@ +1.20.1 diff --git a/webapp/vendor/ort/ort-wasm-simd-threaded.jsep.mjs b/webapp/vendor/ort/ort-wasm-simd-threaded.jsep.mjs new file mode 100644 index 0000000..5c66317 --- /dev/null +++ b/webapp/vendor/ort/ort-wasm-simd-threaded.jsep.mjs @@ -0,0 +1,130 @@ + +var ortWasmThreaded = (() => { + var _scriptName = import.meta.url; + + return ( +async function(moduleArg = {}) { + var moduleRtn; + +function e(){l.buffer!=q.buffer&&t();return q}function w(){l.buffer!=q.buffer&&t();return ba}function ca(){l.buffer!=q.buffer&&t();return da}function ea(){l.buffer!=q.buffer&&t();return fa}function z(){l.buffer!=q.buffer&&t();return ha}function A(){l.buffer!=q.buffer&&t();return ia}function ja(){l.buffer!=q.buffer&&t();return ka}function la(){l.buffer!=q.buffer&&t();return ma} +var B=Object.assign({},moduleArg),na,oa,pa=new Promise((a,b)=>{na=a;oa=b}),qa="object"==typeof window,C="function"==typeof importScripts,D="object"==typeof process&&"object"==typeof process.versions&&"string"==typeof process.versions.node,E=C&&"em-pthread"==self.name;if(D){const {createRequire:a}=await import("module");var require=a(import.meta.url),ra=require("worker_threads");global.Worker=ra.Worker;E=(C=!ra.pc)&&"em-pthread"==ra.workerData}"use strict"; +B.mountExternalData=(a,b)=>{a.startsWith("./")&&(a=a.substring(2));(B.Fb||(B.Fb=new Map)).set(a,b)};B.unmountExternalData=()=>{delete B.Fb};var SharedArrayBuffer=globalThis.SharedArrayBuffer??(new WebAssembly.Memory({initial:0,maximum:0,shared:!0})).buffer.constructor;"use strict"; +let ta=()=>{const a=(c,d,f)=>(...g)=>{const k=F,m=d?.();g=c(...g);const p=d?.();m!==p&&(c=p,f(m),d=f=null);return F!=k?sa():g},b=c=>async(...d)=>{try{if(B.Eb)throw Error("Session already started");const f=B.Eb={fc:d[0],errors:[]},g=await c(...d);if(B.Eb!==f)throw Error("Session mismatch");B.Gb?.flush();const k=f.errors;if(0p);if(0B._OrtCreateSession, +c=>B._OrtCreateSession=c);B._OrtRun=b(a(B._OrtRun,()=>B._OrtRun,c=>B._OrtRun=c));B._OrtRunWithBinding=b(a(B._OrtRunWithBinding,()=>B._OrtRunWithBinding,c=>B._OrtRunWithBinding=c));B._OrtBindInput=a(B._OrtBindInput,()=>B._OrtBindInput,c=>B._OrtBindInput=c);ta=void 0}; +B.jsepInit=(a,b)=>{ta?.();if("webgpu"===a){[B.Gb,B.Ub,B.Yb,B.Nb,B.Xb,B.jb,B.Zb,B.bc,B.Vb,B.Wb,B.$b]=b;const c=B.Gb;B.jsepRegisterBuffer=(d,f,g,k)=>c.registerBuffer(d,f,g,k);B.jsepGetBuffer=d=>c.getBuffer(d);B.jsepCreateDownloader=(d,f,g)=>c.createDownloader(d,f,g);B.jsepOnReleaseSession=d=>{c.onReleaseSession(d)};B.jsepOnRunStart=d=>c.onRunStart(d);B.cc=(d,f)=>{c.upload(d,f)}}else if("webnn"===a){[B.Gb,B.ac,B.Ob,B.jsepEnsureTensor,B.dc,B.jsepDownloadTensor]=b;B.jsepReleaseTensorId=B.Ob;const c=B.Gb; +B.jsepOnRunStart=d=>c.onRunStart(d);B.jsepRegisterMLContext=(d,f)=>{c.registerMLContext(d,f)};B.jsepOnReleaseSession=d=>{c.onReleaseSession(d)};B.jsepCreateMLTensorDownloader=(d,f)=>c.createMLTensorDownloader(d,f);B.jsepRegisterMLTensor=(d,f,g)=>c.registerMLTensor(d,f,g)}};var ua=Object.assign({},B),va="./this.program",wa=(a,b)=>{throw b;},G="",xa,ya,za; +if(D){var fs=require("fs"),Aa=require("path");G=require("url").fileURLToPath(new URL("./",import.meta.url));xa=(a,b)=>{a=Ba(a)?new URL(a):Aa.normalize(a);return fs.readFileSync(a,b?void 0:"utf8")};za=a=>{a=xa(a,!0);a.buffer||(a=new Uint8Array(a));return a};ya=(a,b,c,d=!0)=>{a=Ba(a)?new URL(a):Aa.normalize(a);fs.readFile(a,d?void 0:"utf8",(f,g)=>{f?c(f):b(d?g.buffer:g)})};!B.thisProgram&&1{process.exitCode= +a;throw b;}}else if(qa||C)C?G=self.location.href:"undefined"!=typeof document&&document.currentScript&&(G=document.currentScript.src),_scriptName&&(G=_scriptName),G.startsWith("blob:")?G="":G=G.substr(0,G.replace(/[?#].*/,"").lastIndexOf("/")+1),D||(xa=a=>{var b=new XMLHttpRequest;b.open("GET",a,!1);b.send(null);return b.responseText},C&&(za=a=>{var b=new XMLHttpRequest;b.open("GET",a,!1);b.responseType="arraybuffer";b.send(null);return new Uint8Array(b.response)}),ya=(a,b,c)=>{var d=new XMLHttpRequest; +d.open("GET",a,!0);d.responseType="arraybuffer";d.onload=()=>{200==d.status||0==d.status&&d.response?b(d.response):c()};d.onerror=c;d.send(null)});D&&"undefined"==typeof performance&&(global.performance=require("perf_hooks").performance);var Ca=console.log.bind(console),Da=console.error.bind(console);D&&(Ca=(...a)=>fs.writeSync(1,a.join(" ")+"\n"),Da=(...a)=>fs.writeSync(2,a.join(" ")+"\n"));var Ea=Ca,H=Da;Object.assign(B,ua);ua=null; +if(E){var Fa;if(D){var Ga=ra.parentPort;Ga.on("message",b=>onmessage({data:b}));Object.assign(globalThis,{self:global,importScripts:()=>{},postMessage:b=>Ga.postMessage(b),performance:global.performance||{now:Date.now}})}var Ha=!1;H=function(...b){b=b.join(" ");D?fs.writeSync(2,b+"\n"):console.error(b)};self.alert=function(...b){postMessage({Mb:"alert",text:b.join(" "),qc:Ia()})};B.instantiateWasm=(b,c)=>new Promise(d=>{Fa=f=>{f=new WebAssembly.Instance(f,Ja());c(f);d()}});self.onunhandledrejection= +b=>{throw b.reason||b;};function a(b){try{var c=b.data,d=c.cmd;if("load"===d){let f=[];self.onmessage=g=>f.push(g);self.startWorker=()=>{postMessage({cmd:"loaded"});for(let g of f)a(g);self.onmessage=a};for(const g of c.handlers)if(!B[g]||B[g].proxy)B[g]=(...k)=>{postMessage({Mb:"callHandler",oc:g,args:k})},"print"==g&&(Ea=B[g]),"printErr"==g&&(H=B[g]);l=c.wasmMemory;t();Fa(c.wasmModule)}else if("run"===d){Ka(c.pthread_ptr,0,0,1,0,0);La(c.pthread_ptr);Ma();Na();Ha||(Oa(),Ha=!0);try{Pa(c.start_routine, +c.arg)}catch(f){if("unwind"!=f)throw f;}}else"cancel"===d?Ia()&&Qa(-1):"setimmediate"!==c.target&&("checkMailbox"===d?Ha&&Ra():d&&(H(`worker: received unknown command ${d}`),H(c)))}catch(f){throw Sa(),f;}}self.onmessage=a}var Ta;B.wasmBinary&&(Ta=B.wasmBinary);var l,Ua,I=!1,Va,q,ba,da,fa,ha,ia,ka,J,Wa,ma; +function t(){var a=l.buffer;B.HEAP8=q=new Int8Array(a);B.HEAP16=da=new Int16Array(a);B.HEAPU8=ba=new Uint8Array(a);B.HEAPU16=fa=new Uint16Array(a);B.HEAP32=ha=new Int32Array(a);B.HEAPU32=ia=new Uint32Array(a);B.HEAPF32=ka=new Float32Array(a);B.HEAPF64=ma=new Float64Array(a);B.HEAP64=J=new BigInt64Array(a);B.HEAPU64=Wa=new BigUint64Array(a)} +if(!E){l=new WebAssembly.Memory({initial:256,maximum:65536,shared:!0});if(!(l.buffer instanceof SharedArrayBuffer))throw H("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"),D&&H("(on node you may need: --experimental-wasm-threads --experimental-wasm-bulk-memory and/or recent version)"),Error("bad memory");t()} +var Xa=[],Ya=[],Za=[],$a=0,ab=null,bb=null;function cb(){$a--;if(0==$a&&(null!==ab&&(clearInterval(ab),ab=null),bb)){var a=bb;bb=null;a()}}function db(a){a="Aborted("+a+")";H(a);I=!0;Va=1;a=new WebAssembly.RuntimeError(a+". Build with -sASSERTIONS for more info.");oa(a);throw a;}var eb=a=>a.startsWith("data:application/octet-stream;base64,"),Ba=a=>a.startsWith("file://"),fb; +function gb(a){if(a==fb&&Ta)return new Uint8Array(Ta);if(za)return za(a);throw"both async and sync fetching of the wasm failed";}function hb(a){if(!Ta&&(qa||C)){if("function"==typeof fetch&&!Ba(a))return fetch(a,{credentials:"same-origin"}).then(b=>{if(!b.ok)throw`failed to load wasm binary file at '${a}'`;return b.arrayBuffer()}).catch(()=>gb(a));if(ya)return new Promise((b,c)=>{ya(a,d=>b(new Uint8Array(d)),c)})}return Promise.resolve().then(()=>gb(a))} +function ib(a,b,c){return hb(a).then(d=>WebAssembly.instantiate(d,b)).then(c,d=>{H(`failed to asynchronously prepare wasm: ${d}`);db(d)})}function jb(a,b){var c=fb;return Ta||"function"!=typeof WebAssembly.instantiateStreaming||eb(c)||Ba(c)||D||"function"!=typeof fetch?ib(c,a,b):fetch(c,{credentials:"same-origin"}).then(d=>WebAssembly.instantiateStreaming(d,a).then(b,function(f){H(`wasm streaming compile failed: ${f}`);H("falling back to ArrayBuffer instantiation");return ib(c,a,b)}))} +function Ja(){kb={O:lb,Aa:mb,b:nb,aa:ob,B:pb,qa:qb,Y:rb,_:sb,ra:tb,oa:ub,ha:vb,na:wb,L:xb,Z:yb,W:zb,pa:Ab,X:Bb,wa:Cb,F:Db,Q:Eb,P:Fb,E:Gb,u:Hb,q:Ib,G:Jb,A:Kb,R:Lb,ua:Mb,ka:Nb,U:Ob,ba:Pb,H:Qb,ja:La,ta:Rb,t:Sb,x:Tb,o:Ub,l:Vb,c:Wb,n:Xb,j:Yb,w:Zb,p:$b,g:ac,s:bc,m:cc,e:dc,k:ec,i:fc,h:gc,d:hc,ea:ic,fa:jc,ga:kc,ca:lc,da:mc,T:nc,f:oc,D:pc,I:qc,M:rc,y:sc,sa:tc,V:uc,v:vc,z:wc,N:xc,S:yc,za:zc,ya:Ac,la:Bc,ma:Cc,$:Dc,C:Ec,K:Fc,ia:Gc,J:Hc,a:l,xa:Ic,va:Jc,r:Kc};return{a:kb}} +var Lc={868340:(a,b,c,d,f)=>{if("undefined"==typeof B||!B.Fb)return 1;a=K(a>>>0);a.startsWith("./")&&(a=a.substring(2));a=B.Fb.get(a);if(!a)return 2;b>>>=0;c>>>=0;d>>>=0;if(b+c>a.byteLength)return 3;try{const g=a.subarray(b,b+c);switch(f){case 0:w().set(g,d>>>0);break;case 1:B.cc(d,g);break;default:return 4}return 0}catch{return 4}},869023:(a,b,c)=>{B.dc(a,w().subarray(b>>>0,b+c>>>0))},869086:()=>B.ac(),869127:a=>{B.Ob(a)},869163:()=>{B.Vb()},869194:()=>{B.Wb()},869223:()=>{B.$b()},869248:a=>B.Ub(a), +869281:a=>B.Yb(a),869313:(a,b,c)=>{B.Nb(a,b,c,!0)},869352:(a,b,c)=>{B.Nb(a,b,c)},869385:()=>"undefined"!==typeof wasmOffsetConverter,869442:a=>{B.jb("Abs",a,void 0)},869493:a=>{B.jb("Neg",a,void 0)},869544:a=>{B.jb("Floor",a,void 0)},869597:a=>{B.jb("Ceil",a,void 0)},869649:a=>{B.jb("Reciprocal",a,void 0)},869707:a=>{B.jb("Sqrt",a,void 0)},869759:a=>{B.jb("Exp",a,void 0)},869810:a=>{B.jb("Erf",a,void 0)},869861:a=>{B.jb("Sigmoid",a,void 0)},869916:(a,b,c)=>{B.jb("HardSigmoid",a,{alpha:b,beta:c})}, +869995:a=>{B.jb("Log",a,void 0)},870046:a=>{B.jb("Sin",a,void 0)},870097:a=>{B.jb("Cos",a,void 0)},870148:a=>{B.jb("Tan",a,void 0)},870199:a=>{B.jb("Asin",a,void 0)},870251:a=>{B.jb("Acos",a,void 0)},870303:a=>{B.jb("Atan",a,void 0)},870355:a=>{B.jb("Sinh",a,void 0)},870407:a=>{B.jb("Cosh",a,void 0)},870459:a=>{B.jb("Asinh",a,void 0)},870512:a=>{B.jb("Acosh",a,void 0)},870565:a=>{B.jb("Atanh",a,void 0)},870618:a=>{B.jb("Tanh",a,void 0)},870670:a=>{B.jb("Not",a,void 0)},870721:(a,b,c)=>{B.jb("Clip", +a,{min:b,max:c})},870790:a=>{B.jb("Clip",a,void 0)},870842:(a,b)=>{B.jb("Elu",a,{alpha:b})},870900:a=>{B.jb("Gelu",a,void 0)},870952:a=>{B.jb("Relu",a,void 0)},871004:(a,b)=>{B.jb("LeakyRelu",a,{alpha:b})},871068:(a,b)=>{B.jb("ThresholdedRelu",a,{alpha:b})},871138:(a,b)=>{B.jb("Cast",a,{to:b})},871196:a=>{B.jb("Add",a,void 0)},871247:a=>{B.jb("Sub",a,void 0)},871298:a=>{B.jb("Mul",a,void 0)},871349:a=>{B.jb("Div",a,void 0)},871400:a=>{B.jb("Pow",a,void 0)},871451:a=>{B.jb("Equal",a,void 0)},871504:a=> +{B.jb("Greater",a,void 0)},871559:a=>{B.jb("GreaterOrEqual",a,void 0)},871621:a=>{B.jb("Less",a,void 0)},871673:a=>{B.jb("LessOrEqual",a,void 0)},871732:(a,b,c,d,f)=>{B.jb("ReduceMean",a,{keepDims:!!b,noopWithEmptyAxes:!!c,axes:d?Array.from(z().subarray(d>>>0,f>>>0)):[]})},871891:(a,b,c,d,f)=>{B.jb("ReduceMax",a,{keepDims:!!b,noopWithEmptyAxes:!!c,axes:d?Array.from(z().subarray(d>>>0,f>>>0)):[]})},872049:(a,b,c,d,f)=>{B.jb("ReduceMin",a,{keepDims:!!b,noopWithEmptyAxes:!!c,axes:d?Array.from(z().subarray(d>>> +0,f>>>0)):[]})},872207:(a,b,c,d,f)=>{B.jb("ReduceProd",a,{keepDims:!!b,noopWithEmptyAxes:!!c,axes:d?Array.from(z().subarray(d>>>0,f>>>0)):[]})},872366:(a,b,c,d,f)=>{B.jb("ReduceSum",a,{keepDims:!!b,noopWithEmptyAxes:!!c,axes:d?Array.from(z().subarray(d>>>0,f>>>0)):[]})},872524:(a,b,c,d,f)=>{B.jb("ReduceL1",a,{keepDims:!!b,noopWithEmptyAxes:!!c,axes:d?Array.from(z().subarray(d>>>0,f>>>0)):[]})},872681:(a,b,c,d,f)=>{B.jb("ReduceL2",a,{keepDims:!!b,noopWithEmptyAxes:!!c,axes:d?Array.from(z().subarray(d>>> +0,f>>>0)):[]})},872838:(a,b,c,d,f)=>{B.jb("ReduceLogSum",a,{keepDims:!!b,noopWithEmptyAxes:!!c,axes:d?Array.from(z().subarray(d>>>0,f>>>0)):[]})},872999:(a,b,c,d,f)=>{B.jb("ReduceSumSquare",a,{keepDims:!!b,noopWithEmptyAxes:!!c,axes:d?Array.from(z().subarray(d>>>0,f>>>0)):[]})},873163:(a,b,c,d,f)=>{B.jb("ReduceLogSumExp",a,{keepDims:!!b,noopWithEmptyAxes:!!c,axes:d?Array.from(z().subarray(d>>>0,f>>>0)):[]})},873327:a=>{B.jb("Where",a,void 0)},873380:(a,b,c)=>{B.jb("Transpose",a,{perm:b?Array.from(z().subarray(b>>> +0,c>>>0)):[]})},873488:(a,b,c,d)=>{B.jb("DepthToSpace",a,{blocksize:b,mode:K(c),format:d?"NHWC":"NCHW"})},873621:(a,b,c,d)=>{B.jb("DepthToSpace",a,{blocksize:b,mode:K(c),format:d?"NHWC":"NCHW"})},873754:(a,b,c,d,f,g,k,m,p,n,r,v,x,h,u)=>{B.jb("ConvTranspose",a,{format:p?"NHWC":"NCHW",autoPad:b,dilations:[c],group:d,kernelShape:[f],pads:[g,k],strides:[m],wIsConst:()=>!!e()[n>>>0],outputPadding:r?Array.from(z().subarray(r>>>0,v>>>0)):[],outputShape:x?Array.from(z().subarray(x>>>0,h>>>0)):[],activation:K(u)})}, +874155:(a,b,c,d,f,g,k,m,p,n,r,v,x,h)=>{B.jb("ConvTranspose",a,{format:m?"NHWC":"NCHW",autoPad:b,dilations:Array.from(z().subarray(c>>>0,(c>>>0)+2>>>0)),group:d,kernelShape:Array.from(z().subarray(f>>>0,(f>>>0)+2>>>0)),pads:Array.from(z().subarray(g>>>0,(g>>>0)+4>>>0)),strides:Array.from(z().subarray(k>>>0,(k>>>0)+2>>>0)),wIsConst:()=>!!e()[p>>>0],outputPadding:n?Array.from(z().subarray(n>>>0,r>>>0)):[],outputShape:v?Array.from(z().subarray(v>>>0,x>>>0)):[],activation:K(h)})},874720:(a,b,c,d,f,g,k, +m,p,n,r,v,x,h,u)=>{B.jb("ConvTranspose",a,{format:p?"NHWC":"NCHW",autoPad:b,dilations:[c],group:d,kernelShape:[f],pads:[g,k],strides:[m],wIsConst:()=>!!e()[n>>>0],outputPadding:r?Array.from(z().subarray(r>>>0,v>>>0)):[],outputShape:x?Array.from(z().subarray(x>>>0,h>>>0)):[],activation:K(u)})},875121:(a,b,c,d,f,g,k,m,p,n,r,v,x,h)=>{B.jb("ConvTranspose",a,{format:m?"NHWC":"NCHW",autoPad:b,dilations:Array.from(z().subarray(c>>>0,(c>>>0)+2>>>0)),group:d,kernelShape:Array.from(z().subarray(f>>>0,(f>>> +0)+2>>>0)),pads:Array.from(z().subarray(g>>>0,(g>>>0)+4>>>0)),strides:Array.from(z().subarray(k>>>0,(k>>>0)+2>>>0)),wIsConst:()=>!!e()[p>>>0],outputPadding:n?Array.from(z().subarray(n>>>0,r>>>0)):[],outputShape:v?Array.from(z().subarray(v>>>0,x>>>0)):[],activation:K(h)})},875686:(a,b)=>{B.jb("GlobalAveragePool",a,{format:b?"NHWC":"NCHW"})},875777:(a,b,c,d,f,g,k,m,p,n,r,v,x,h)=>{B.jb("AveragePool",a,{format:h?"NHWC":"NCHW",auto_pad:b,ceil_mode:c,count_include_pad:d,storage_order:f,dilations:g?Array.from(z().subarray(g>>> +0,k>>>0)):[],kernel_shape:m?Array.from(z().subarray(m>>>0,p>>>0)):[],pads:n?Array.from(z().subarray(n>>>0,r>>>0)):[],strides:v?Array.from(z().subarray(v>>>0,x>>>0)):[]})},876192:(a,b)=>{B.jb("GlobalAveragePool",a,{format:b?"NHWC":"NCHW"})},876283:(a,b,c,d,f,g,k,m,p,n,r,v,x,h)=>{B.jb("AveragePool",a,{format:h?"NHWC":"NCHW",auto_pad:b,ceil_mode:c,count_include_pad:d,storage_order:f,dilations:g?Array.from(z().subarray(g>>>0,k>>>0)):[],kernel_shape:m?Array.from(z().subarray(m>>>0,p>>>0)):[],pads:n?Array.from(z().subarray(n>>> +0,r>>>0)):[],strides:v?Array.from(z().subarray(v>>>0,x>>>0)):[]})},876698:(a,b)=>{B.jb("GlobalMaxPool",a,{format:b?"NHWC":"NCHW"})},876785:(a,b,c,d,f,g,k,m,p,n,r,v,x,h)=>{B.jb("MaxPool",a,{format:h?"NHWC":"NCHW",auto_pad:b,ceil_mode:c,count_include_pad:d,storage_order:f,dilations:g?Array.from(z().subarray(g>>>0,k>>>0)):[],kernel_shape:m?Array.from(z().subarray(m>>>0,p>>>0)):[],pads:n?Array.from(z().subarray(n>>>0,r>>>0)):[],strides:v?Array.from(z().subarray(v>>>0,x>>>0)):[]})},877196:(a,b)=>{B.jb("GlobalMaxPool", +a,{format:b?"NHWC":"NCHW"})},877283:(a,b,c,d,f,g,k,m,p,n,r,v,x,h)=>{B.jb("MaxPool",a,{format:h?"NHWC":"NCHW",auto_pad:b,ceil_mode:c,count_include_pad:d,storage_order:f,dilations:g?Array.from(z().subarray(g>>>0,k>>>0)):[],kernel_shape:m?Array.from(z().subarray(m>>>0,p>>>0)):[],pads:n?Array.from(z().subarray(n>>>0,r>>>0)):[],strides:v?Array.from(z().subarray(v>>>0,x>>>0)):[]})},877694:(a,b,c,d,f)=>{B.jb("Gemm",a,{alpha:b,beta:c,transA:d,transB:f})},877798:a=>{B.jb("MatMul",a,void 0)},877852:(a,b,c, +d)=>{B.jb("ArgMax",a,{keepDims:!!b,selectLastIndex:!!c,axis:d})},877960:(a,b,c,d)=>{B.jb("ArgMin",a,{keepDims:!!b,selectLastIndex:!!c,axis:d})},878068:(a,b)=>{B.jb("Softmax",a,{axis:b})},878131:(a,b)=>{B.jb("Concat",a,{axis:b})},878191:(a,b,c,d,f)=>{B.jb("Split",a,{axis:b,numOutputs:c,splitSizes:d?Array.from(z().subarray(d>>>0,f>>>0)):[]})},878331:a=>{B.jb("Expand",a,void 0)},878385:(a,b)=>{B.jb("Gather",a,{axis:Number(b)})},878456:(a,b)=>{B.jb("GatherElements",a,{axis:Number(b)})},878535:(a,b,c, +d,f,g,k,m,p,n,r)=>{B.jb("Resize",a,{antialias:b,axes:c?Array.from(z().subarray(c>>>0,d>>>0)):[],coordinateTransformMode:K(f),cubicCoeffA:g,excludeOutside:k,extrapolationValue:m,keepAspectRatioPolicy:K(p),mode:K(n),nearestMode:K(r)})},878881:(a,b,c,d,f,g,k)=>{B.jb("Slice",a,{starts:b?Array.from(z().subarray(b>>>0,c>>>0)):[],ends:d?Array.from(z().subarray(d>>>0,f>>>0)):[],axes:g?Array.from(z().subarray(g>>>0,k>>>0)):[]})},879097:a=>{B.jb("Tile",a,void 0)},879149:(a,b,c)=>{B.jb("InstanceNormalization", +a,{epsilon:b,format:c?"NHWC":"NCHW"})},879263:(a,b,c)=>{B.jb("InstanceNormalization",a,{epsilon:b,format:c?"NHWC":"NCHW"})},879377:a=>{B.jb("Range",a,void 0)},879430:(a,b)=>{B.jb("Einsum",a,{equation:K(b)})},879511:(a,b,c,d,f)=>{B.jb("Pad",a,{mode:b,value:c,pads:d?Array.from(z().subarray(d>>>0,f>>>0)):[]})},879638:(a,b,c,d,f,g)=>{B.jb("BatchNormalization",a,{epsilon:b,momentum:c,spatial:!!f,trainingMode:!!d,format:g?"NHWC":"NCHW"})},879807:(a,b,c,d,f,g)=>{B.jb("BatchNormalization",a,{epsilon:b,momentum:c, +spatial:!!f,trainingMode:!!d,format:g?"NHWC":"NCHW"})},879976:(a,b,c)=>{B.jb("CumSum",a,{exclusive:Number(b),reverse:Number(c)})},880073:(a,b,c)=>{B.jb("DequantizeLinear",a,{axis:b,blockSize:c})},880163:(a,b,c,d,f,g,k,m,p)=>{B.jb("Attention",a,{numHeads:b,isUnidirectional:c,maskFilterValue:d,scale:f,doRotary:g,qkvHiddenSizes:k?Array.from(z().subarray(Number(m)>>>0,Number(m)+k>>>0)):[],pastPresentShareBuffer:!!p})},880435:a=>{B.jb("BiasAdd",a,void 0)},880490:a=>{B.jb("BiasSplitGelu",a,void 0)},880551:a=> +{B.jb("FastGelu",a,void 0)},880607:(a,b,c,d,f,g,k,m,p,n,r,v,x,h,u,y)=>{B.jb("Conv",a,{format:v?"NHWC":"NCHW",auto_pad:b,dilations:c?Array.from(z().subarray(c>>>0,d>>>0)):[],group:f,kernel_shape:g?Array.from(z().subarray(g>>>0,k>>>0)):[],pads:m?Array.from(z().subarray(m>>>0,p>>>0)):[],strides:n?Array.from(z().subarray(n>>>0,r>>>0)):[],w_is_const:()=>!!e()[x>>>0],activation:K(h),activation_params:u?Array.from(ja().subarray(u>>>0,y>>>0)):[]})},881103:a=>{B.jb("Gelu",a,void 0)},881155:(a,b,c,d)=>{B.jb("GroupQueryAttention", +a,{numHeads:b,kvNumHeads:c,scale:d})},881268:(a,b,c,d)=>{B.jb("LayerNormalization",a,{axis:b,epsilon:c,simplified:!!d})},881379:(a,b,c,d)=>{B.jb("LayerNormalization",a,{axis:b,epsilon:c,simplified:!!d})},881490:(a,b,c,d,f,g)=>{B.jb("MatMulNBits",a,{k:b,n:c,accuracyLevel:d,bits:f,blockSize:g})},881617:(a,b,c,d,f,g)=>{B.jb("MultiHeadAttention",a,{numHeads:b,isUnidirectional:c,maskFilterValue:d,scale:f,doRotary:g})},881776:(a,b)=>{B.jb("QuickGelu",a,{alpha:b})},881840:(a,b,c,d,f)=>{B.jb("RotaryEmbedding", +a,{interleaved:!!b,numHeads:c,rotaryEmbeddingDim:d,scale:f})},881979:(a,b,c)=>{B.jb("SkipLayerNormalization",a,{epsilon:b,simplified:!!c})},882081:(a,b,c)=>{B.jb("SkipLayerNormalization",a,{epsilon:b,simplified:!!c})},882183:(a,b,c,d)=>{B.jb("GatherBlockQuantized",a,{gatherAxis:b,quantizeAxis:c,blockSize:d})},882304:a=>{B.Zb(a)},882338:(a,b)=>B.bc(a,b,B.Eb.fc,B.Eb.errors)};function mb(a,b,c){return Mc(async()=>{await B.Xb(a,b,c)})}function lb(){return"undefined"!==typeof wasmOffsetConverter} +function Nc(a){this.name="ExitStatus";this.message=`Program terminated with exit(${a})`;this.status=a} +var Oc=a=>{a.terminate();a.onmessage=()=>{}},Rc=a=>{0==L.length&&(Pc(),Qc(L[0]));var b=L.pop();if(!b)return 6;M.push(b);N[a.Ab]=b;b.Ab=a.Ab;var c={cmd:"run",start_routine:a.hc,arg:a.Qb,pthread_ptr:a.Ab};D&&b.unref();b.postMessage(c,a.mc);return 0},O=0,P=(a,b,...c)=>{for(var d=2*c.length,f=Sc(),g=Tc(8*d),k=g>>>3,m=0;m>>0]=p)}a=Vc(a,0,d,g,b);Wc(f);return a}; +function Ic(a){if(E)return P(0,1,a);Va=a;if(!(0{Va=a;if(E)throw Xc(a),"unwind";Ic(a)},L=[],M=[],Yc=[],N={};function Zc(){for(var a=B.numThreads-1;a--;)Pc();Xa.unshift(()=>{$a++;$c(()=>cb())})}var bd=a=>{var b=a.Ab;delete N[b];L.push(a);M.splice(M.indexOf(a),1);a.Ab=0;ad(b)};function Na(){Yc.forEach(a=>a())} +var Qc=a=>new Promise(b=>{a.onmessage=g=>{g=g.data;var k=g.cmd;if(g.targetThread&&g.targetThread!=Ia()){var m=N[g.targetThread];m?m.postMessage(g,g.transferList):H(`Internal error! Worker sent a message "${k}" to target pthread ${g.targetThread}, but that thread no longer exists!`)}else if("checkMailbox"===k)Ra();else if("spawnThread"===k)Rc(g);else if("cleanupThread"===k)bd(N[g.thread]);else if("killThread"===k)g=g.thread,k=N[g],delete N[g],Oc(k),ad(g),M.splice(M.indexOf(k),1),k.Ab=0;else if("cancelThread"=== +k)N[g.thread].postMessage({cmd:"cancel"});else if("loaded"===k)a.loaded=!0,D&&!a.Ab&&a.unref(),b(a);else if("alert"===k)alert(`Thread ${g.threadId}: ${g.text}`);else if("setimmediate"===g.target)a.postMessage(g);else if("callHandler"===k)B[g.handler](...g.args);else k&&H(`worker sent an unknown command ${k}`)};a.onerror=g=>{H(`${"worker sent an error!"} ${g.filename}:${g.lineno}: ${g.message}`);throw g;};D&&(a.on("message",g=>a.onmessage({data:g})),a.on("error",g=>a.onerror(g)));var c=[],d=[],f;for(f of d)B.hasOwnProperty(f)&& +c.push(f);a.postMessage({cmd:"load",handlers:c,wasmMemory:l,wasmModule:Ua})});function $c(a){E?a():Promise.all(L.map(Qc)).then(a)}function Pc(){var a=new Worker(new URL(import.meta.url),{type:"module",workerData:"em-pthread",name:"em-pthread"});L.push(a)}var cd=a=>{for(;0{var a=Ia(),b=A()[a+52>>>2>>>0];a=A()[a+56>>>2>>>0];dd(b,b-a);Wc(b)},Pa=(a,b)=>{O=0;a=ed(a,b);0>>=0;var d=new fd(a);b>>>=0;c>>>=0;A()[d.Jb+16>>>2>>>0]=0;A()[d.Jb+4>>>2>>>0]=b;A()[d.Jb+8>>>2>>>0]=c;gd=a;hd++;throw gd;}function jd(a,b,c,d){return E?P(2,1,a,b,c,d):ob(a,b,c,d)} +function ob(a,b,c,d){a>>>=0;b>>>=0;c>>>=0;d>>>=0;if("undefined"==typeof SharedArrayBuffer)return H("Current environment does not support SharedArrayBuffer, pthreads are not available!"),6;var f=[];if(E&&0===f.length)return jd(a,b,c,d);a={hc:c,Ab:a,Qb:d,mc:f};return E?(a.Mb="spawnThread",postMessage(a,f),0):Rc(a)} +var kd="undefined"!=typeof TextDecoder?new TextDecoder("utf8"):void 0,ld=(a,b,c)=>{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},K=(a,b)=>(a>>>=0)?ld(w(),a,b):"";function pb(a,b,c){return E?P(3,1,a,b,c):0}function qb(a,b){if(E)return P(4,1,a,b)} +var md=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},nd=(a,b,c,d)=>{c>>>=0;if(!(0=k){var m=a.charCodeAt(++g);k=65536+((k&1023)<<10)|m&1023}if(127>=k){if(c>=d)break;b[c++>>>0]=k}else{if(2047>=k){if(c+1>=d)break;b[c++>>>0]=192|k>>6}else{if(65535>=k){if(c+2>=d)break;b[c++>>>0]=224|k>>12}else{if(c+3>=d)break;b[c++>>>0]=240|k>> +18;b[c++>>>0]=128|k>>12&63}b[c++>>>0]=128|k>>6&63}b[c++>>>0]=128|k&63}}b[c>>>0]=0;return c-f},od=(a,b,c)=>nd(a,w(),b,c);function rb(a,b){if(E)return P(5,1,a,b)}function sb(a,b,c){if(E)return P(6,1,a,b,c)}function tb(a,b,c){return E?P(7,1,a,b,c):0}function ub(a,b){if(E)return P(8,1,a,b)}function vb(a,b,c){if(E)return P(9,1,a,b,c)}function wb(a,b,c,d){if(E)return P(10,1,a,b,c,d)}function xb(a,b,c,d){if(E)return P(11,1,a,b,c,d)}function yb(a,b,c,d){if(E)return P(12,1,a,b,c,d)} +function zb(a){if(E)return P(13,1,a)}function Ab(a,b){if(E)return P(14,1,a,b)}function Bb(a,b,c){if(E)return P(15,1,a,b,c)}var Cb=()=>{db("")},pd,Q=a=>{for(var b="";w()[a>>>0];)b+=pd[w()[a++>>>0]];return b},qd={},rd={},sd={},R; +function td(a,b,c={}){var d=b.name;if(!a)throw new R(`type "${d}" must have a positive integer typeid pointer`);if(rd.hasOwnProperty(a)){if(c.Sb)return;throw new R(`Cannot register type '${d}' twice`);}rd[a]=b;delete sd[a];qd.hasOwnProperty(a)&&(b=qd[a],delete qd[a],b.forEach(f=>f()))}function S(a,b,c={}){if(!("argPackAdvance"in b))throw new TypeError("registerType registeredInstance requires argPackAdvance");return td(a,b,c)} +var ud=(a,b,c)=>{switch(b){case 1:return c?d=>e()[d>>>0]:d=>w()[d>>>0];case 2:return c?d=>ca()[d>>>1>>>0]:d=>ea()[d>>>1>>>0];case 4:return c?d=>z()[d>>>2>>>0]:d=>A()[d>>>2>>>0];case 8:return c?d=>J[d>>>3]:d=>Wa[d>>>3];default:throw new TypeError(`invalid integer width (${b}): ${a}`);}}; +function Db(a,b,c){a>>>=0;c>>>=0;b=Q(b>>>0);S(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},argPackAdvance:T,readValueFromPointer:ud(b,c,-1==b.indexOf("u")),Db:null})}var T=8; +function Eb(a,b,c,d){a>>>=0;b=Q(b>>>0);S(a,{name:b,fromWireType:function(f){return!!f},toWireType:function(f,g){return g?c:d},argPackAdvance:T,readValueFromPointer:function(f){return this.fromWireType(w()[f>>>0])},Db:null})}var vd=[],U=[];function Wb(a){a>>>=0;9{if(!a)throw new R("Cannot use deleted val. handle = "+a);return U[a]},W=a=>{switch(a){case void 0:return 2;case null:return 4;case !0:return 6;case !1:return 8;default:const b=vd.pop()||U.length;U[b]=a;U[b+1]=1;return b}};function wd(a){return this.fromWireType(A()[a>>>2>>>0])}var xd={name:"emscripten::val",fromWireType:a=>{var b=V(a);Wb(a);return b},toWireType:(a,b)=>W(b),argPackAdvance:T,readValueFromPointer:wd,Db:null};function Fb(a){return S(a>>>0,xd)} +var yd=(a,b)=>{switch(b){case 4:return function(c){return this.fromWireType(ja()[c>>>2>>>0])};case 8:return function(c){return this.fromWireType(la()[c>>>3>>>0])};default:throw new TypeError(`invalid float width (${b}): ${a}`);}};function Gb(a,b,c){a>>>=0;c>>>=0;b=Q(b>>>0);S(a,{name:b,fromWireType:d=>d,toWireType:(d,f)=>f,argPackAdvance:T,readValueFromPointer:yd(b,c),Db:null})} +function Hb(a,b,c,d,f){a>>>=0;c>>>=0;b=Q(b>>>0);-1===f&&(f=4294967295);f=m=>m;if(0===d){var g=32-8*c;f=m=>m<>>g}var k=b.includes("unsigned")?function(m,p){return p>>>0}:function(m,p){return p};S(a,{name:b,fromWireType:f,toWireType:k,argPackAdvance:T,readValueFromPointer:ud(b,c,0!==d),Db:null})} +function Ib(a,b,c){function d(g){var k=A()[g>>>2>>>0];g=A()[g+4>>>2>>>0];return new f(e().buffer,g,k)}a>>>=0;var f=[Int8Array,Uint8Array,Int16Array,Uint16Array,Int32Array,Uint32Array,Float32Array,Float64Array,BigInt64Array,BigUint64Array][b];c=Q(c>>>0);S(a,{name:c,fromWireType:d,argPackAdvance:T,readValueFromPointer:d},{Sb:!0})} +function Jb(a,b){a>>>=0;b=Q(b>>>0);var c="std::string"===b;S(a,{name:b,fromWireType:function(d){var f=A()[d>>>2>>>0],g=d+4;if(c)for(var k=g,m=0;m<=f;++m){var p=g+m;if(m==f||0==w()[p>>>0]){k=K(k,p-k);if(void 0===n)var n=k;else n+=String.fromCharCode(0),n+=k;k=p+1}}else{n=Array(f);for(m=0;m>>0]);n=n.join("")}X(d);return n},toWireType:function(d,f){f instanceof ArrayBuffer&&(f=new Uint8Array(f));var g="string"==typeof f;if(!(g||f instanceof Uint8Array||f instanceof +Uint8ClampedArray||f instanceof Int8Array))throw new R("Cannot pass non-string to std::string");var k=c&&g?md(f):f.length;var m=zd(4+k+1),p=m+4;A()[m>>>2>>>0]=k;if(c&&g)od(f,p,k+1);else if(g)for(g=0;g>>0]=n}else for(g=0;g>>0]=f[g];null!==d&&d.push(X,m);return m},argPackAdvance:T,readValueFromPointer:wd,Db(d){X(d)}})} +var Ad="undefined"!=typeof TextDecoder?new TextDecoder("utf-16le"):void 0,Bd=(a,b)=>{var c=a>>1;for(var d=c+b/2;!(c>=d)&&ea()[c>>>0];)++c;c<<=1;if(32=b/2);++d){var f=ca()[a+2*d>>>1>>>0];if(0==f)break;c+=String.fromCharCode(f)}return c},Cd=(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}ca()[b>>>1>>>0]=0;return b-d},Dd=a=>2*a.length,Ed=(a,b)=> +{for(var c=0,d="";!(c>=b/4);){var f=z()[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},Fd=(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 k=a.charCodeAt(++f);g=65536+((g&1023)<<10)|k&1023}z()[b>>>2>>>0]=g;b+=4;if(b+4>c)break}z()[b>>>2>>>0]=0;return b-d},Gd=a=>{for(var b=0,c=0;c=d&&++c;b+=4}return b}; +function Kb(a,b,c){a>>>=0;b>>>=0;c>>>=0;c=Q(c);if(2===b){var d=Bd;var f=Cd;var g=Dd;var k=m=>ea()[m>>>1>>>0]}else 4===b&&(d=Ed,f=Fd,g=Gd,k=m=>A()[m>>>2>>>0]);S(a,{name:c,fromWireType:m=>{for(var p=A()[m>>>2>>>0],n,r=m+4,v=0;v<=p;++v){var x=m+4+v*b;if(v==p||0==k(x))r=d(r,x-r),void 0===n?n=r:(n+=String.fromCharCode(0),n+=r),r=x+b}X(m);return n},toWireType:(m,p)=>{if("string"!=typeof p)throw new R(`Cannot pass non-string to C++ string type ${c}`);var n=g(p),r=zd(4+n+b);A()[r>>>2>>>0]=n/b;f(p,r+4,n+b); +null!==m&&m.push(X,r);return r},argPackAdvance:T,readValueFromPointer:wd,Db(m){X(m)}})}function Lb(a,b){a>>>=0;b=Q(b>>>0);S(a,{Tb:!0,name:b,argPackAdvance:0,fromWireType:()=>{},toWireType:()=>{}})}var Mb=()=>1;function Nb(a){Ka(a>>>0,!C,1,!qa,131072,!1);Na()}var Hd=a=>{if(!I)try{if(a(),!(0>>=0;"function"===typeof Atomics.nc&&(Atomics.nc(z(),a>>>2,a).value.then(Ra),a+=128,Atomics.store(z(),a>>>2,1))}var Ra=()=>{var a=Ia();a&&(La(a),Hd(Id))};function Ob(a,b){a>>>=0;a==b>>>0?setTimeout(Ra):E?postMessage({targetThread:a,cmd:"checkMailbox"}):(a=N[a])&&a.postMessage({cmd:"checkMailbox"})}var Jd=[];function Pb(a,b,c,d,f){b>>>=0;d/=2;Jd.length=d;c=f>>>0>>>3;for(f=0;f>>0];return(b?Lc[b]:Kd[a])(...Jd)} +function Qb(a){a>>>=0;E?postMessage({cmd:"cleanupThread",thread:a}):bd(N[a])}function Rb(a){D&&N[a>>>0].ref()}var Md=(a,b)=>{var c=rd[a];if(void 0===c)throw a=Ld(a),c=Q(a),X(a),new R(`${b} has unknown type ${c}`);return c},Nd=(a,b,c)=>{var d=[];a=a.toWireType(d,c);d.length&&(A()[b>>>2>>>0]=W(d));return a};function Sb(a,b,c){b>>>=0;c>>>=0;a=V(a>>>0);b=Md(b,"emval::as");return Nd(b,c,a)}var Od=a=>{try{a()}catch(b){db(b)}}; +function Pd(){var a=Y,b={};for(let [c,d]of Object.entries(a))b[c]="function"==typeof d?(...f)=>{Qd.push(c);try{return d(...f)}finally{I||(Qd.pop(),F&&1===Z&&0===Qd.length&&(Z=0,O+=1,Od(Rd),"undefined"!=typeof Fibers&&Fibers.sc()))}}:d;return b}var Z=0,F=null,Sd=0,Qd=[],Td={},Ud={},Vd=0,Wd=null,Xd=[];function sa(){return new Promise((a,b)=>{Wd={resolve:a,reject:b}})} +function Yd(){var a=zd(65548),b=a+12;A()[a>>>2>>>0]=b;A()[a+4>>>2>>>0]=b+65536;b=Qd[0];var c=Td[b];void 0===c&&(c=Vd++,Td[b]=c,Ud[c]=b);b=c;z()[a+8>>>2>>>0]=b;return a}function Zd(){var a=z()[F+8>>>2>>>0];a=Y[Ud[a]];--O;return a()} +function $d(a){if(!I){if(0===Z){var b=!1,c=!1;a((d=0)=>{if(!I&&(Sd=d,b=!0,c)){Z=2;Od(()=>ae(F));"undefined"!=typeof Browser&&Browser.Kb.Rb&&Browser.Kb.resume();d=!1;try{var f=Zd()}catch(m){f=m,d=!0}var g=!1;if(!F){var k=Wd;k&&(Wd=null,(d?k.reject:k.resolve)(f),g=!0)}if(d&&!g)throw f;}});c=!0;b||(Z=1,F=Yd(),"undefined"!=typeof Browser&&Browser.Kb.Rb&&Browser.Kb.pause(),Od(()=>be(F)))}else 2===Z?(Z=0,Od(ce),X(F),F=null,Xd.forEach(Hd)):db(`invalid state: ${Z}`);return Sd}} +function Mc(a){return $d(b=>{a().then(b)})}function Tb(a){a>>>=0;return Mc(()=>{a=V(a);return a.then(W)})}var de=[];function Ub(a,b,c,d){c>>>=0;d>>>=0;a=de[a>>>0];b=V(b>>>0);return a(null,b,c,d)}var ee={},fe=a=>{var b=ee[a];return void 0===b?Q(a):b};function Vb(a,b,c,d,f){c>>>=0;d>>>=0;f>>>=0;a=de[a>>>0];b=V(b>>>0);c=fe(c);return a(b,b[c],d,f)}var ge=()=>"object"==typeof globalThis?globalThis:Function("return this")();function Xb(a){a>>>=0;if(0===a)return W(ge());a=fe(a);return W(ge()[a])} +var he=a=>{var b=de.length;de.push(a);return b},ie=(a,b)=>{for(var c=Array(a),d=0;d>>2>>>0],"parameter "+d);return c},je=(a,b)=>Object.defineProperty(b,"name",{value:a});function ke(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=je(b.name||"unknownFunctionName",function(){});c.prototype=b.prototype;c=new c;a=b.apply(c,a);return a instanceof Object?a:c} +function Yb(a,b,c){b=ie(a,b>>>0);var d=b.shift();a--;var f="return function (obj, func, destructorsRef, args) {\n",g=0,k=[];0===c&&k.push("obj");for(var m=["retType"],p=[d],n=0;nr.name).join(", ")}) => ${d.name}>`;return he(je(c,a))}function Zb(a){a=fe(a>>>0);return W(B[a])}function $b(a,b){b>>>=0;a=V(a>>>0);b=V(b);return W(a[b])}function ac(a){a>>>=0;9>>0);for(var b=Array(a.length),c=0;c>>0))}function ec(){return W({})} +function fc(a){a>>>=0;for(var b=V(a);b.length;){var c=b.pop();b.pop()(c)}Wb(a)}function gc(a,b,c){b>>>=0;c>>>=0;a=V(a>>>0);b=V(b);c=V(c);a[b]=c}function hc(a,b){b>>>=0;a=Md(a>>>0,"_emval_take_value");a=a.readValueFromPointer(b);return W(a)} +function ic(a,b){a=-9007199254740992>a||9007199254740992>>=0;a=new Date(1E3*a);z()[b>>>2>>>0]=a.getUTCSeconds();z()[b+4>>>2>>>0]=a.getUTCMinutes();z()[b+8>>>2>>>0]=a.getUTCHours();z()[b+12>>>2>>>0]=a.getUTCDate();z()[b+16>>>2>>>0]=a.getUTCMonth();z()[b+20>>>2>>>0]=a.getUTCFullYear()-1900;z()[b+24>>>2>>>0]=a.getUTCDay();a=(a.getTime()-Date.UTC(a.getUTCFullYear(),0,1,0,0,0,0))/864E5|0;z()[b+28>>>2>>>0]=a} +var le=a=>0===a%4&&(0!==a%100||0===a%400),me=[0,31,60,91,121,152,182,213,244,274,305,335],ne=[0,31,59,90,120,151,181,212,243,273,304,334]; +function jc(a,b){a=-9007199254740992>a||9007199254740992>>=0;a=new Date(1E3*a);z()[b>>>2>>>0]=a.getSeconds();z()[b+4>>>2>>>0]=a.getMinutes();z()[b+8>>>2>>>0]=a.getHours();z()[b+12>>>2>>>0]=a.getDate();z()[b+16>>>2>>>0]=a.getMonth();z()[b+20>>>2>>>0]=a.getFullYear()-1900;z()[b+24>>>2>>>0]=a.getDay();var c=(le(a.getFullYear())?me:ne)[a.getMonth()]+a.getDate()-1|0;z()[b+28>>>2>>>0]=c;z()[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;z()[b+32>>>2>>>0]=a} +function kc(a){a>>>=0;var b=new Date(z()[a+20>>>2>>>0]+1900,z()[a+16>>>2>>>0],z()[a+12>>>2>>>0],z()[a+8>>>2>>>0],z()[a+4>>>2>>>0],z()[a>>>2>>>0],0),c=z()[a+32>>>2>>>0],d=b.getTimezoneOffset(),f=(new Date(b.getFullYear(),6,1)).getTimezoneOffset(),g=(new Date(b.getFullYear(),0,1)).getTimezoneOffset(),k=Math.min(g,f);0>c?z()[a+32>>>2>>>0]=Number(f!=g&&k==d):0>>2>>>0]=b.getDay();c=(le(b.getFullYear())?me:ne)[b.getMonth()]+ +b.getDate()-1|0;z()[a+28>>>2>>>0]=c;z()[a>>>2>>>0]=b.getSeconds();z()[a+4>>>2>>>0]=b.getMinutes();z()[a+8>>>2>>>0]=b.getHours();z()[a+12>>>2>>>0]=b.getDate();z()[a+16>>>2>>>0]=b.getMonth();z()[a+20>>>2>>>0]=b.getYear();a=b.getTime();return BigInt(isNaN(a)?-1:a/1E3)}function lc(a,b,c,d,f,g,k){return E?P(16,1,a,b,c,d,f,g,k):-52}function mc(a,b,c,d,f,g){if(E)return P(17,1,a,b,c,d,f,g)} +function nc(a,b,c,d){a>>>=0;b>>>=0;c>>>=0;d>>>=0;var f=(new Date).getFullYear(),g=new Date(f,0,1),k=new Date(f,6,1);f=g.getTimezoneOffset();var m=k.getTimezoneOffset(),p=Math.max(f,m);A()[a>>>2>>>0]=60*p;z()[b>>>2>>>0]=Number(f!=m);a=n=>n.toLocaleTimeString(void 0,{hour12:!1,timeZoneName:"short"}).split(" ")[1];g=a(g);k=a(k);m{oe.length=0;for(var c;c=w()[a++>>>0];){var d=105!=c;d&=112!=c;b+=d&&b%8?4:0;oe.push(112==c?A()[b>>>2>>>0]:106==c?J[b>>>3]:105==c?z()[b>>>2>>>0]:la()[b>>>3>>>0]);b+=d?8:4}return oe};function oc(a,b,c){a>>>=0;b=pe(b>>>0,c>>>0);return Lc[a](...b)}function pc(a,b,c){a>>>=0;b=pe(b>>>0,c>>>0);return Lc[a](...b)}var qc=()=>{},rc=()=>Date.now();function sc(a,b){return H(K(a>>>0,b>>>0))}var tc=()=>{O+=1;throw"unwind";};function uc(){return 4294901760}var vc; +vc=()=>performance.timeOrigin+performance.now();var wc=()=>D?require("os").cpus().length:navigator.hardwareConcurrency;function xc(){db("Cannot use emscripten_pc_get_function without -sUSE_OFFSET_CONVERTER");return 0} +function yc(a){a>>>=0;var b=w().length;if(a<=b||4294901760=c;c*=2){var d=b*(1+.2/c);d=Math.min(d,a+100663296);var f=Math;d=Math.max(a,d);a:{f=(f.min.call(f,4294901760,d+(65536-d%65536)%65536)-l.buffer.byteLength+65535)/65536;try{l.grow(f);t();var g=1;break a}catch(k){}g=void 0}if(g)return!0}return!1}var qe=()=>{db("Cannot use convertFrameToPC (needed by __builtin_return_address) without -sUSE_OFFSET_CONVERTER");return 0},re={},se=a=>{a.forEach(b=>{var c=qe();c&&(re[c]=b)})}; +function zc(){var a=Error().stack.toString().split("\n");"Error"==a[0]&&a.shift();se(a);re.Pb=qe();re.ec=a;return re.Pb}function Ac(a,b,c){a>>>=0;b>>>=0;if(re.Pb==a)var d=re.ec;else d=Error().stack.toString().split("\n"),"Error"==d[0]&&d.shift(),se(d);for(var f=3;d[f]&&qe()!=a;)++f;for(a=0;a>>2>>>0]=qe();return a} +var te={},ve=()=>{if(!ue){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",_:va||"./this.program"},b;for(b in te)void 0===te[b]?delete a[b]:a[b]=te[b];var c=[];for(b in a)c.push(`${b}=${a[b]}`);ue=c}return ue},ue; +function Bc(a,b){if(E)return P(18,1,a,b);a>>>=0;b>>>=0;var c=0;ve().forEach((d,f)=>{var g=b+c;f=A()[a+4*f>>>2>>>0]=g;for(g=0;g>>0]=d.charCodeAt(g);e()[f>>>0]=0;c+=d.length+1});return 0}function Cc(a,b){if(E)return P(19,1,a,b);a>>>=0;b>>>=0;var c=ve();A()[a>>>2>>>0]=c.length;var d=0;c.forEach(f=>d+=f.length+1);A()[b>>>2>>>0]=d;return 0}function Ec(a){return E?P(20,1,a):52}function Fc(a,b,c,d){return E?P(21,1,a,b,c,d):52}function Gc(a,b,c,d){return E?P(22,1,a,b,c,d):70} +var we=[null,[],[]];function Hc(a,b,c,d){if(E)return P(23,1,a,b,c,d);b>>>=0;c>>>=0;d>>>=0;for(var f=0,g=0;g>>2>>>0],m=A()[b+4>>>2>>>0];b+=8;for(var p=0;p>>0],r=we[a];0===n||10===n?((1===a?Ea:H)(ld(r,0)),r.length=0):r.push(n)}f+=m}A()[d>>>2>>>0]=f;return 0}var xe=[31,29,31,30,31,30,31,31,30,31,30,31],ye=[31,28,31,30,31,30,31,31,30,31,30,31];function ze(a){var b=Array(md(a)+1);nd(a,b,0,b.length);return b}var Ae=(a,b)=>{e().set(a,b>>>0)}; +function Jc(a,b,c,d){function f(h,u,y){for(h="number"==typeof h?h.toString():h||"";h.lengthUc?-1:0aa-h.getDate())u-=aa-h.getDate()+1,h.setDate(1),11>y?h.setMonth(y+1):(h.setMonth(0),h.setFullYear(h.getFullYear()+1));else{h.setDate(h.getDate()+u);break}}y=new Date(h.getFullYear()+1,0,4);u=m(new Date(h.getFullYear(), +0,4));y=m(y);return 0>=k(u,h)?0>=k(y,h)?h.getFullYear()+1:h.getFullYear():h.getFullYear()-1}a>>>=0;b>>>=0;c>>>=0;d>>>=0;var n=A()[d+40>>>2>>>0];d={kc:z()[d>>>2>>>0],jc:z()[d+4>>>2>>>0],Hb:z()[d+8>>>2>>>0],Lb:z()[d+12>>>2>>>0],Ib:z()[d+16>>>2>>>0],Cb:z()[d+20>>>2>>>0],ub:z()[d+24>>>2>>>0],Bb:z()[d+28>>>2>>>0],rc:z()[d+32>>>2>>>0],ic:z()[d+36>>>2>>>0],lc:n?K(n):""};c=K(c);n={"%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"};for(var r in n)c=c.replace(new RegExp(r,"g"),n[r]);var v="Sunday Monday Tuesday Wednesday Thursday Friday Saturday".split(" "),x="January February March April May June July August September October November December".split(" ");n={"%a":h=>v[h.ub].substring(0,3),"%A":h=> +v[h.ub],"%b":h=>x[h.Ib].substring(0,3),"%B":h=>x[h.Ib],"%C":h=>g((h.Cb+1900)/100|0,2),"%d":h=>g(h.Lb,2),"%e":h=>f(h.Lb,2," "),"%g":h=>p(h).toString().substring(2),"%G":p,"%H":h=>g(h.Hb,2),"%I":h=>{h=h.Hb;0==h?h=12:12{for(var u=0,y=0;y<=h.Ib-1;u+=(le(h.Cb+1900)?xe:ye)[y++]);return g(h.Lb+u,3)},"%m":h=>g(h.Ib+1,2),"%M":h=>g(h.jc,2),"%n":()=>"\n","%p":h=>0<=h.Hb&&12>h.Hb?"AM":"PM","%S":h=>g(h.kc,2),"%t":()=>"\t","%u":h=>h.ub||7,"%U":h=>g(Math.floor((h.Bb+7-h.ub)/7), +2),"%V":h=>{var u=Math.floor((h.Bb+7-(h.ub+6)%7)/7);2>=(h.ub+371-h.Bb-2)%7&&u++;if(u)53==u&&(y=(h.ub+371-h.Bb)%7,4==y||3==y&&le(h.Cb)||(u=1));else{u=52;var y=(h.ub+7-h.Bb-1)%7;(4==y||5==y&&le(h.Cb%400-1))&&u++}return g(u,2)},"%w":h=>h.ub,"%W":h=>g(Math.floor((h.Bb+7-(h.ub+6)%7)/7),2),"%y":h=>(h.Cb+1900).toString().substring(2),"%Y":h=>h.Cb+1900,"%z":h=>{h=h.ic;var u=0<=h;h=Math.abs(h)/60;return(u?"+":"-")+String("0000"+(h/60*100+h%60)).slice(-4)},"%Z":h=>h.lc,"%%":()=>"%"};c=c.replace(/%%/g,"\x00\x00"); +for(r in n)c.includes(r)&&(c=c.replace(new RegExp(r,"g"),n[r](d)));c=c.replace(/\0\0/g,"%");r=ze(c);if(r.length>b)return 0;Ae(r,a);return r.length-1}function Kc(a,b,c,d){return Jc(a>>>0,b>>>0,c>>>0,d>>>0)}E||Zc();for(var Be=Array(256),Ce=0;256>Ce;++Ce)Be[Ce]=String.fromCharCode(Ce);pd=Be;R=B.BindingError=class extends Error{constructor(a){super(a);this.name="BindingError"}};B.InternalError=class extends Error{constructor(a){super(a);this.name="InternalError"}};U.push(0,1,void 0,1,null,1,!0,1,!1,1); +B.count_emval_handles=()=>U.length/2-5-vd.length; +var Kd=[Ic,Xc,jd,pb,qb,rb,sb,tb,ub,vb,wb,xb,yb,zb,Ab,Bb,lc,mc,Bc,Cc,Ec,Fc,Gc,Hc],kb,Y=function(){function a(c,d){Y=c.exports;Y=Pd();Y=De();Yc.push(Y.ib);Ya.unshift(Y.Ba);Ua=d;cb();return Y}var b=Ja();$a++;if(B.instantiateWasm)try{return B.instantiateWasm(b,a)}catch(c){H(`Module.instantiateWasm callback failed with error: ${c}`),oa(c)}fb||=B.locateFile?eb("ort-wasm-simd-threaded.jsep.wasm")?"ort-wasm-simd-threaded.jsep.wasm":B.locateFile?B.locateFile("ort-wasm-simd-threaded.jsep.wasm",G):G+"ort-wasm-simd-threaded.jsep.wasm": +(new URL("ort-wasm-simd-threaded.jsep.wasm",import.meta.url)).href;jb(b,function(c){a(c.instance,c.module)}).catch(oa);return{}}(),Ld=a=>(Ld=Y.Ca)(a),Oa=()=>(Oa=Y.Da)();B._OrtInit=(a,b)=>(B._OrtInit=Y.Ea)(a,b);B._OrtGetLastError=(a,b)=>(B._OrtGetLastError=Y.Fa)(a,b);B._OrtCreateSessionOptions=(a,b,c,d,f,g,k,m,p,n)=>(B._OrtCreateSessionOptions=Y.Ga)(a,b,c,d,f,g,k,m,p,n);B._OrtAppendExecutionProvider=(a,b)=>(B._OrtAppendExecutionProvider=Y.Ha)(a,b); +B._OrtAddFreeDimensionOverride=(a,b,c)=>(B._OrtAddFreeDimensionOverride=Y.Ia)(a,b,c);B._OrtAddSessionConfigEntry=(a,b,c)=>(B._OrtAddSessionConfigEntry=Y.Ja)(a,b,c);B._OrtReleaseSessionOptions=a=>(B._OrtReleaseSessionOptions=Y.Ka)(a);B._OrtCreateSession=(a,b,c)=>(B._OrtCreateSession=Y.La)(a,b,c);B._OrtReleaseSession=a=>(B._OrtReleaseSession=Y.Ma)(a);B._OrtGetInputOutputCount=(a,b,c)=>(B._OrtGetInputOutputCount=Y.Na)(a,b,c);B._OrtGetInputName=(a,b)=>(B._OrtGetInputName=Y.Oa)(a,b); +B._OrtGetOutputName=(a,b)=>(B._OrtGetOutputName=Y.Pa)(a,b);B._OrtFree=a=>(B._OrtFree=Y.Qa)(a);B._OrtCreateTensor=(a,b,c,d,f,g)=>(B._OrtCreateTensor=Y.Ra)(a,b,c,d,f,g);B._OrtGetTensorData=(a,b,c,d,f)=>(B._OrtGetTensorData=Y.Sa)(a,b,c,d,f);B._OrtReleaseTensor=a=>(B._OrtReleaseTensor=Y.Ta)(a);B._OrtCreateRunOptions=(a,b,c,d)=>(B._OrtCreateRunOptions=Y.Ua)(a,b,c,d);B._OrtAddRunConfigEntry=(a,b,c)=>(B._OrtAddRunConfigEntry=Y.Va)(a,b,c);B._OrtReleaseRunOptions=a=>(B._OrtReleaseRunOptions=Y.Wa)(a); +B._OrtCreateBinding=a=>(B._OrtCreateBinding=Y.Xa)(a);B._OrtBindInput=(a,b,c)=>(B._OrtBindInput=Y.Ya)(a,b,c);B._OrtBindOutput=(a,b,c,d)=>(B._OrtBindOutput=Y.Za)(a,b,c,d);B._OrtClearBoundOutputs=a=>(B._OrtClearBoundOutputs=Y._a)(a);B._OrtReleaseBinding=a=>(B._OrtReleaseBinding=Y.$a)(a);B._OrtRunWithBinding=(a,b,c,d,f)=>(B._OrtRunWithBinding=Y.ab)(a,b,c,d,f);B._OrtRun=(a,b,c,d,f,g,k,m)=>(B._OrtRun=Y.bb)(a,b,c,d,f,g,k,m);B._OrtEndProfiling=a=>(B._OrtEndProfiling=Y.cb)(a); +B._JsepOutput=(a,b,c)=>(B._JsepOutput=Y.db)(a,b,c);B._JsepGetNodeName=a=>(B._JsepGetNodeName=Y.eb)(a); +var Ia=()=>(Ia=Y.fb)(),zd=B._malloc=a=>(zd=B._malloc=Y.gb)(a),X=B._free=a=>(X=B._free=Y.hb)(a),Ka=(a,b,c,d,f,g)=>(Ka=Y.kb)(a,b,c,d,f,g),Sa=()=>(Sa=Y.lb)(),Vc=(a,b,c,d,f)=>(Vc=Y.mb)(a,b,c,d,f),ad=a=>(ad=Y.nb)(a),Qa=a=>(Qa=Y.ob)(a),Id=()=>(Id=Y.pb)(),dd=(a,b)=>(dd=Y.qb)(a,b),Wc=a=>(Wc=Y.rb)(a),Tc=a=>(Tc=Y.sb)(a),Sc=()=>(Sc=Y.tb)(),ed=B.dynCall_ii=(a,b)=>(ed=B.dynCall_ii=Y.vb)(a,b),be=a=>(be=Y.wb)(a),Rd=()=>(Rd=Y.xb)(),ae=a=>(ae=Y.yb)(a),ce=()=>(ce=Y.zb)();B.___start_em_js=882450;B.___stop_em_js=882672; +function De(){var a=Y;a=Object.assign({},a);var b=d=>f=>d(f)>>>0,c=d=>()=>d()>>>0;a.Ca=b(a.Ca);a.fb=c(a.fb);a.gb=b(a.gb);a.emscripten_main_runtime_thread_id=c(a.emscripten_main_runtime_thread_id);a.sb=b(a.sb);a.tb=c(a.tb);return a}B.stackSave=()=>Sc();B.stackRestore=a=>Wc(a);B.stackAlloc=a=>Tc(a);B.UTF8ToString=K;B.stringToUTF8=od;B.lengthBytesUTF8=md;var Ee;bb=function Fe(){Ee||Ge();Ee||(bb=Fe)}; +function Ge(){0<$a||(E?(na(B),E||cd(Ya),startWorker(B)):(cd(Xa),0<$a||Ee||(Ee=!0,B.calledRun=!0,I||(E||cd(Ya),na(B),E||cd(Za)))))}Ge();moduleRtn=pa; + + + return moduleRtn; +} +); +})(); +export default ortWasmThreaded; +var isPthread = globalThis.self?.name === 'em-pthread'; +var isNode = typeof globalThis.process?.versions?.node == 'string'; +if (isNode) isPthread = (await import('worker_threads')).workerData === 'em-pthread'; + +// When running as a pthread, construct a new instance on startup +isPthread && ortWasmThreaded(); diff --git a/webapp/vendor/ort/ort-wasm-simd-threaded.jsep.wasm b/webapp/vendor/ort/ort-wasm-simd-threaded.jsep.wasm new file mode 100644 index 0000000..6852071 Binary files /dev/null and b/webapp/vendor/ort/ort-wasm-simd-threaded.jsep.wasm differ diff --git a/webapp/vendor/ort/ort-wasm-simd-threaded.mjs b/webapp/vendor/ort/ort-wasm-simd-threaded.mjs new file mode 100644 index 0000000..07faf24 --- /dev/null +++ b/webapp/vendor/ort/ort-wasm-simd-threaded.mjs @@ -0,0 +1,81 @@ + +var ortWasmThreaded = (() => { + var _scriptName = import.meta.url; + + return ( +async function(moduleArg = {}) { + var moduleRtn; + +function aa(){g.buffer!=l.buffer&&m();return l}function n(){g.buffer!=l.buffer&&m();return ba}function r(){g.buffer!=l.buffer&&m();return ca}function u(){g.buffer!=l.buffer&&m();return da}function ea(){g.buffer!=l.buffer&&m();return fa}var w=Object.assign({},moduleArg),ha,x,ia=new Promise((a,b)=>{ha=a;x=b}),ja="object"==typeof window,A="function"==typeof importScripts,B="object"==typeof process&&"object"==typeof process.versions&&"string"==typeof process.versions.node,C=A&&"em-pthread"==self.name; +if(B){const {createRequire:a}=await import("module");var require=a(import.meta.url),D=require("worker_threads");global.Worker=D.Worker;C=(A=!D.lb)&&"em-pthread"==D.workerData}"use strict";w.mountExternalData=(a,b)=>{a.startsWith("./")&&(a=a.substring(2));(w.Ua||(w.Ua=new Map)).set(a,b)};w.unmountExternalData=()=>{delete w.Ua}; +var SharedArrayBuffer=globalThis.SharedArrayBuffer??(new WebAssembly.Memory({initial:0,maximum:0,shared:!0})).buffer.constructor,ka=Object.assign({},w),la="./this.program",E=(a,b)=>{throw b;},F="",ma,G,H; +if(B){var fs=require("fs"),na=require("path");F=require("url").fileURLToPath(new URL("./",import.meta.url));ma=(a,b)=>{a=oa(a)?new URL(a):na.normalize(a);return fs.readFileSync(a,b?void 0:"utf8")};H=a=>{a=ma(a,!0);a.buffer||(a=new Uint8Array(a));return a};G=(a,b,c,d=!0)=>{a=oa(a)?new URL(a):na.normalize(a);fs.readFile(a,d?void 0:"utf8",(f,h)=>{f?c(f):b(d?h.buffer:h)})};!w.thisProgram&&1{process.exitCode= +a;throw b;}}else if(ja||A)A?F=self.location.href:"undefined"!=typeof document&&document.currentScript&&(F=document.currentScript.src),_scriptName&&(F=_scriptName),F.startsWith("blob:")?F="":F=F.substr(0,F.replace(/[?#].*/,"").lastIndexOf("/")+1),B||(ma=a=>{var b=new XMLHttpRequest;b.open("GET",a,!1);b.send(null);return b.responseText},A&&(H=a=>{var b=new XMLHttpRequest;b.open("GET",a,!1);b.responseType="arraybuffer";b.send(null);return new Uint8Array(b.response)}),G=(a,b,c)=>{var d=new XMLHttpRequest; +d.open("GET",a,!0);d.responseType="arraybuffer";d.onload=()=>{200==d.status||0==d.status&&d.response?b(d.response):c()};d.onerror=c;d.send(null)});B&&"undefined"==typeof performance&&(global.performance=require("perf_hooks").performance);var pa=console.log.bind(console),qa=console.error.bind(console);B&&(pa=(...a)=>fs.writeSync(1,a.join(" ")+"\n"),qa=(...a)=>fs.writeSync(2,a.join(" ")+"\n"));var ra=pa,I=qa;Object.assign(w,ka);ka=null; +if(C){var sa;if(B){var ta=D.parentPort;ta.on("message",b=>onmessage({data:b}));Object.assign(globalThis,{self:global,importScripts:()=>{},postMessage:b=>ta.postMessage(b),performance:global.performance||{now:Date.now}})}var ua=!1;I=function(...b){b=b.join(" ");B?fs.writeSync(2,b+"\n"):console.error(b)};self.alert=function(...b){postMessage({Za:"alert",text:b.join(" "),nb:K()})};w.instantiateWasm=(b,c)=>new Promise(d=>{sa=f=>{f=new WebAssembly.Instance(f,va());c(f);d()}});self.onunhandledrejection= +b=>{throw b.reason||b;};function a(b){try{var c=b.data,d=c.cmd;if("load"===d){let f=[];self.onmessage=h=>f.push(h);self.startWorker=()=>{postMessage({cmd:"loaded"});for(let h of f)a(h);self.onmessage=a};for(const h of c.handlers)if(!w[h]||w[h].proxy)w[h]=(...k)=>{postMessage({Za:"callHandler",kb:h,args:k})},"print"==h&&(ra=w[h]),"printErr"==h&&(I=w[h]);g=c.wasmMemory;m();sa(c.wasmModule)}else if("run"===d){wa(c.pthread_ptr,0,0,1,0,0);xa(c.pthread_ptr);ya();za();ua||=!0;try{Aa(c.start_routine,c.arg)}catch(f){if("unwind"!= +f)throw f;}}else"cancel"===d?K()&&Ba(-1):"setimmediate"!==c.target&&("checkMailbox"===d?ua&&Ca():d&&(I(`worker: received unknown command ${d}`),I(c)))}catch(f){throw Da(),f;}}self.onmessage=a}var L;w.wasmBinary&&(L=w.wasmBinary);var g,Ea,Fa=!1,M,l,ba,ca,da,N,fa; +function m(){var a=g.buffer;w.HEAP8=l=new Int8Array(a);w.HEAP16=new Int16Array(a);w.HEAPU8=ba=new Uint8Array(a);w.HEAPU16=new Uint16Array(a);w.HEAP32=ca=new Int32Array(a);w.HEAPU32=da=new Uint32Array(a);w.HEAPF32=new Float32Array(a);w.HEAPF64=fa=new Float64Array(a);w.HEAP64=N=new BigInt64Array(a);w.HEAPU64=new BigUint64Array(a)} +if(!C){g=new WebAssembly.Memory({initial:256,maximum:65536,shared:!0});if(!(g.buffer instanceof SharedArrayBuffer))throw I("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"),B&&I("(on node you may need: --experimental-wasm-threads --experimental-wasm-bulk-memory and/or recent version)"),Error("bad memory");m()} +var Ga=[],Ha=[],Ia=[],O=0,Ja=null,P=null;function Ka(){O--;if(0==O&&(null!==Ja&&(clearInterval(Ja),Ja=null),P)){var a=P;P=null;a()}}function La(a){a="Aborted("+a+")";I(a);Fa=!0;M=1;a=new WebAssembly.RuntimeError(a+". Build with -sASSERTIONS for more info.");x(a);throw a;}var Ma=a=>a.startsWith("data:application/octet-stream;base64,"),oa=a=>a.startsWith("file://"),Na;function Oa(a){if(a==Na&&L)return new Uint8Array(L);if(H)return H(a);throw"both async and sync fetching of the wasm failed";} +function Pa(a){if(!L&&(ja||A)){if("function"==typeof fetch&&!oa(a))return fetch(a,{credentials:"same-origin"}).then(b=>{if(!b.ok)throw`failed to load wasm binary file at '${a}'`;return b.arrayBuffer()}).catch(()=>Oa(a));if(G)return new Promise((b,c)=>{G(a,d=>b(new Uint8Array(d)),c)})}return Promise.resolve().then(()=>Oa(a))}function Qa(a,b,c){return Pa(a).then(d=>WebAssembly.instantiate(d,b)).then(c,d=>{I(`failed to asynchronously prepare wasm: ${d}`);La(d)})} +function Ra(a,b){var c=Na;return L||"function"!=typeof WebAssembly.instantiateStreaming||Ma(c)||oa(c)||B||"function"!=typeof fetch?Qa(c,a,b):fetch(c,{credentials:"same-origin"}).then(d=>WebAssembly.instantiateStreaming(d,a).then(b,function(f){I(`wasm streaming compile failed: ${f}`);I("falling back to ArrayBuffer instantiation");return Qa(c,a,b)}))} +function va(){Sa={j:Ta,b:Ua,E:Va,g:Wa,V:Xa,A:Ya,C:Za,W:$a,T:ab,L:bb,S:cb,o:db,B:eb,y:fb,U:gb,z:hb,_:ib,Z:jb,P:kb,w:lb,F:mb,k:nb,O:xa,Y:ob,I:pb,J:qb,K:rb,G:sb,H:tb,v:ub,q:vb,l:wb,p:xb,e:yb,X:zb,x:Ab,d:Bb,f:Cb,i:Db,u:Eb,t:Fb,s:Gb,Q:Hb,R:Ib,D:Jb,h:Kb,n:Lb,M:Mb,m:Nb,a:g,r:Ob,N:Pb,c:Qb};return{a:Sa}} +var Rb={837620:(a,b,c,d,f)=>{if("undefined"==typeof w||!w.Ua)return 1;a=Q(a>>>0);a.startsWith("./")&&(a=a.substring(2));a=w.Ua.get(a);if(!a)return 2;b>>>=0;c>>>=0;d>>>=0;if(b+c>a.byteLength)return 3;try{const h=a.subarray(b,b+c);switch(f){case 0:n().set(h,d>>>0);break;case 1:w.mb(d,h);break;default:return 4}return 0}catch{return 4}},838303:()=>"undefined"!==typeof wasmOffsetConverter};function Ta(){return"undefined"!==typeof wasmOffsetConverter} +function Sb(a){this.name="ExitStatus";this.message=`Program terminated with exit(${a})`;this.status=a} +var Tb=a=>{a.terminate();a.onmessage=()=>{}},Wb=a=>{0==R.length&&(Ub(),Vb(R[0]));var b=R.pop();if(!b)return 6;S.push(b);T[a.Ra]=b;b.Ra=a.Ra;var c={cmd:"run",start_routine:a.cb,arg:a.ab,pthread_ptr:a.Ra};B&&b.unref();b.postMessage(c,a.ib);return 0},U=0,V=(a,b,...c)=>{for(var d=2*c.length,f=$b(),h=ac(8*d),k=h>>>3,q=0;q>>0]=y)}a=bc(a,0,d,h,b);cc(f);return a}; +function Ob(a){if(C)return V(0,1,a);M=a;if(!(0{M=a;if(C)throw dc(a),"unwind";Ob(a)},R=[],S=[],ec=[],T={};function fc(){for(var a=w.numThreads-1;a--;)Ub();Ga.unshift(()=>{O++;gc(()=>Ka())})}var ic=a=>{var b=a.Ra;delete T[b];R.push(a);S.splice(S.indexOf(a),1);a.Ra=0;hc(b)};function za(){ec.forEach(a=>a())} +var Vb=a=>new Promise(b=>{a.onmessage=h=>{h=h.data;var k=h.cmd;if(h.targetThread&&h.targetThread!=K()){var q=T[h.targetThread];q?q.postMessage(h,h.transferList):I(`Internal error! Worker sent a message "${k}" to target pthread ${h.targetThread}, but that thread no longer exists!`)}else if("checkMailbox"===k)Ca();else if("spawnThread"===k)Wb(h);else if("cleanupThread"===k)ic(T[h.thread]);else if("killThread"===k)h=h.thread,k=T[h],delete T[h],Tb(k),hc(h),S.splice(S.indexOf(k),1),k.Ra=0;else if("cancelThread"=== +k)T[h.thread].postMessage({cmd:"cancel"});else if("loaded"===k)a.loaded=!0,B&&!a.Ra&&a.unref(),b(a);else if("alert"===k)alert(`Thread ${h.threadId}: ${h.text}`);else if("setimmediate"===h.target)a.postMessage(h);else if("callHandler"===k)w[h.handler](...h.args);else k&&I(`worker sent an unknown command ${k}`)};a.onerror=h=>{I(`${"worker sent an error!"} ${h.filename}:${h.lineno}: ${h.message}`);throw h;};B&&(a.on("message",h=>a.onmessage({data:h})),a.on("error",h=>a.onerror(h)));var c=[],d=[],f;for(f of d)w.hasOwnProperty(f)&& +c.push(f);a.postMessage({cmd:"load",handlers:c,wasmMemory:g,wasmModule:Ea})});function gc(a){C?a():Promise.all(R.map(Vb)).then(a)}function Ub(){var a=new Worker(new URL(import.meta.url),{type:"module",workerData:"em-pthread",name:"em-pthread"});R.push(a)}var jc=a=>{for(;0{var a=K(),b=u()[a+52>>>2>>>0];a=u()[a+56>>>2>>>0];kc(b,b-a);cc(b)},lc=[],mc,Aa=(a,b)=>{U=0;var c=lc[a];c||(a>=lc.length&&(lc.length=a+1),lc[a]=c=mc.get(a));a=c(b);0>>=0;var d=new nc(a);b>>>=0;c>>>=0;u()[d.Xa+16>>>2>>>0]=0;u()[d.Xa+4>>>2>>>0]=b;u()[d.Xa+8>>>2>>>0]=c;oc=a;pc++;throw oc;}function qc(a,b,c,d){return C?V(2,1,a,b,c,d):Va(a,b,c,d)} +function Va(a,b,c,d){a>>>=0;b>>>=0;c>>>=0;d>>>=0;if("undefined"==typeof SharedArrayBuffer)return I("Current environment does not support SharedArrayBuffer, pthreads are not available!"),6;var f=[];if(C&&0===f.length)return qc(a,b,c,d);a={cb:c,Ra:a,ab:d,ib:f};return C?(a.Za="spawnThread",postMessage(a,f),0):Wb(a)} +var rc="undefined"!=typeof TextDecoder?new TextDecoder("utf8"):void 0,sc=(a,b,c)=>{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},Q=(a,b)=>(a>>>=0)?sc(n(),a,b):"";function Wa(a,b,c){return C?V(3,1,a,b,c):0}function Xa(a,b){if(C)return V(4,1,a,b)} +var tc=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},uc=(a,b,c,d)=>{c>>>=0;if(!(0=k){var q=a.charCodeAt(++h);k=65536+((k&1023)<<10)|q&1023}if(127>=k){if(c>=d)break;b[c++>>>0]=k}else{if(2047>=k){if(c+1>=d)break;b[c++>>>0]=192|k>>6}else{if(65535>=k){if(c+2>=d)break;b[c++>>>0]=224|k>>12}else{if(c+3>=d)break;b[c++>>>0]=240|k>> +18;b[c++>>>0]=128|k>>12&63}b[c++>>>0]=128|k>>6&63}b[c++>>>0]=128|k&63}}b[c>>>0]=0;return c-f},W=(a,b,c)=>uc(a,n(),b,c);function Ya(a,b){if(C)return V(5,1,a,b)}function Za(a,b,c){if(C)return V(6,1,a,b,c)}function $a(a,b,c){return C?V(7,1,a,b,c):0}function ab(a,b){if(C)return V(8,1,a,b)}function bb(a,b,c){if(C)return V(9,1,a,b,c)}function cb(a,b,c,d){if(C)return V(10,1,a,b,c,d)}function db(a,b,c,d){if(C)return V(11,1,a,b,c,d)}function eb(a,b,c,d){if(C)return V(12,1,a,b,c,d)} +function fb(a){if(C)return V(13,1,a)}function gb(a,b){if(C)return V(14,1,a,b)}function hb(a,b,c){if(C)return V(15,1,a,b,c)}var ib=()=>{La("")},jb=()=>1;function kb(a){wa(a>>>0,!A,1,!ja,131072,!1);za()}function xa(a){a>>>=0;"function"===typeof Atomics.jb&&(Atomics.jb(r(),a>>>2,a).value.then(Ca),a+=128,Atomics.store(r(),a>>>2,1))} +var Ca=()=>{var a=K();if(a&&(xa(a),a=vc,!Fa))try{if(a(),!(0>>=0;a==b>>>0?setTimeout(Ca):C?postMessage({targetThread:a,cmd:"checkMailbox"}):(a=T[a])&&a.postMessage({cmd:"checkMailbox"})}var wc=[];function mb(a,b,c,d,f){b>>>=0;d/=2;wc.length=d;c=f>>>0>>>3;for(f=0;f>>0];return(b?Rb[b]:xc[a])(...wc)} +function nb(a){a>>>=0;C?postMessage({cmd:"cleanupThread",thread:a}):ic(T[a])}function ob(a){B&&T[a>>>0].ref()} +function pb(a,b){a=-9007199254740992>a||9007199254740992>>=0;a=new Date(1E3*a);r()[b>>>2>>>0]=a.getUTCSeconds();r()[b+4>>>2>>>0]=a.getUTCMinutes();r()[b+8>>>2>>>0]=a.getUTCHours();r()[b+12>>>2>>>0]=a.getUTCDate();r()[b+16>>>2>>>0]=a.getUTCMonth();r()[b+20>>>2>>>0]=a.getUTCFullYear()-1900;r()[b+24>>>2>>>0]=a.getUTCDay();a=(a.getTime()-Date.UTC(a.getUTCFullYear(),0,1,0,0,0,0))/864E5|0;r()[b+28>>>2>>>0]=a} +var X=a=>0===a%4&&(0!==a%100||0===a%400),yc=[0,31,60,91,121,152,182,213,244,274,305,335],zc=[0,31,59,90,120,151,181,212,243,273,304,334]; +function qb(a,b){a=-9007199254740992>a||9007199254740992>>=0;a=new Date(1E3*a);r()[b>>>2>>>0]=a.getSeconds();r()[b+4>>>2>>>0]=a.getMinutes();r()[b+8>>>2>>>0]=a.getHours();r()[b+12>>>2>>>0]=a.getDate();r()[b+16>>>2>>>0]=a.getMonth();r()[b+20>>>2>>>0]=a.getFullYear()-1900;r()[b+24>>>2>>>0]=a.getDay();var c=(X(a.getFullYear())?yc:zc)[a.getMonth()]+a.getDate()-1|0;r()[b+28>>>2>>>0]=c;r()[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;r()[b+32>>>2>>>0]=a} +function rb(a){a>>>=0;var b=new Date(r()[a+20>>>2>>>0]+1900,r()[a+16>>>2>>>0],r()[a+12>>>2>>>0],r()[a+8>>>2>>>0],r()[a+4>>>2>>>0],r()[a>>>2>>>0],0),c=r()[a+32>>>2>>>0],d=b.getTimezoneOffset(),f=(new Date(b.getFullYear(),6,1)).getTimezoneOffset(),h=(new Date(b.getFullYear(),0,1)).getTimezoneOffset(),k=Math.min(h,f);0>c?r()[a+32>>>2>>>0]=Number(f!=h&&k==d):0>>2>>>0]=b.getDay();c=(X(b.getFullYear())?yc:zc)[b.getMonth()]+ +b.getDate()-1|0;r()[a+28>>>2>>>0]=c;r()[a>>>2>>>0]=b.getSeconds();r()[a+4>>>2>>>0]=b.getMinutes();r()[a+8>>>2>>>0]=b.getHours();r()[a+12>>>2>>>0]=b.getDate();r()[a+16>>>2>>>0]=b.getMonth();r()[a+20>>>2>>>0]=b.getYear();a=b.getTime();return BigInt(isNaN(a)?-1:a/1E3)}function sb(a,b,c,d,f,h,k){return C?V(16,1,a,b,c,d,f,h,k):-52}function tb(a,b,c,d,f,h){if(C)return V(17,1,a,b,c,d,f,h)} +function ub(a,b,c,d){a>>>=0;b>>>=0;c>>>=0;d>>>=0;var f=(new Date).getFullYear(),h=new Date(f,0,1),k=new Date(f,6,1);f=h.getTimezoneOffset();var q=k.getTimezoneOffset(),y=Math.max(f,q);u()[a>>>2>>>0]=60*y;r()[b>>>2>>>0]=Number(f!=q);a=v=>v.toLocaleTimeString(void 0,{hour12:!1,timeZoneName:"short"}).split(" ")[1];h=a(h);k=a(k);q>>=0;b>>>=0;c>>>=0;Ac.length=0;for(var d;d=n()[b++>>>0];){var f=105!=d;f&=112!=d;c+=f&&c%8?4:0;Ac.push(112==d?u()[c>>>2>>>0]:106==d?N[c>>>3]:105==d?r()[c>>>2>>>0]:ea()[c>>>3>>>0]);c+=f?8:4}return Rb[a](...Ac)}var wb=()=>{},xb=()=>Date.now();function yb(a,b){return I(Q(a>>>0,b>>>0))}var zb=()=>{U+=1;throw"unwind";};function Ab(){return 4294901760}var Bb;Bb=()=>performance.timeOrigin+performance.now();var Cb=()=>B?require("os").cpus().length:navigator.hardwareConcurrency; +function Db(){La("Cannot use emscripten_pc_get_function without -sUSE_OFFSET_CONVERTER");return 0}function Eb(a){a>>>=0;var b=n().length;if(a<=b||4294901760=c;c*=2){var d=b*(1+.2/c);d=Math.min(d,a+100663296);var f=Math;d=Math.max(a,d);a:{f=(f.min.call(f,4294901760,d+(65536-d%65536)%65536)-g.buffer.byteLength+65535)/65536;try{g.grow(f);m();var h=1;break a}catch(k){}h=void 0}if(h)return!0}return!1} +var Bc=()=>{La("Cannot use convertFrameToPC (needed by __builtin_return_address) without -sUSE_OFFSET_CONVERTER");return 0},Y={},Cc=a=>{a.forEach(b=>{var c=Bc();c&&(Y[c]=b)})};function Fb(){var a=Error().stack.toString().split("\n");"Error"==a[0]&&a.shift();Cc(a);Y.$a=Bc();Y.bb=a;return Y.$a} +function Gb(a,b,c){a>>>=0;b>>>=0;if(Y.$a==a)var d=Y.bb;else d=Error().stack.toString().split("\n"),"Error"==d[0]&&d.shift(),Cc(d);for(var f=3;d[f]&&Bc()!=a;)++f;for(a=0;a>>2>>>0]=Bc();return a} +var Dc={},Fc=()=>{if(!Ec){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 Dc)void 0===Dc[b]?delete a[b]:a[b]=Dc[b];var c=[];for(b in a)c.push(`${b}=${a[b]}`);Ec=c}return Ec},Ec; +function Hb(a,b){if(C)return V(18,1,a,b);a>>>=0;b>>>=0;var c=0;Fc().forEach((d,f)=>{var h=b+c;f=u()[a+4*f>>>2>>>0]=h;for(h=0;h>>0]=d.charCodeAt(h);aa()[f>>>0]=0;c+=d.length+1});return 0}function Ib(a,b){if(C)return V(19,1,a,b);a>>>=0;b>>>=0;var c=Fc();u()[a>>>2>>>0]=c.length;var d=0;c.forEach(f=>d+=f.length+1);u()[b>>>2>>>0]=d;return 0}function Kb(a){return C?V(20,1,a):52}function Lb(a,b,c,d){return C?V(21,1,a,b,c,d):52}function Mb(a,b,c,d){return C?V(22,1,a,b,c,d):70} +var Gc=[null,[],[]];function Nb(a,b,c,d){if(C)return V(23,1,a,b,c,d);b>>>=0;c>>>=0;d>>>=0;for(var f=0,h=0;h>>2>>>0],q=u()[b+4>>>2>>>0];b+=8;for(var y=0;y>>0],z=Gc[a];0===v||10===v?((1===a?ra:I)(sc(z,0)),z.length=0):z.push(v)}f+=q}u()[d>>>2>>>0]=f;return 0}var Hc=[31,29,31,30,31,30,31,31,30,31,30,31],Ic=[31,28,31,30,31,30,31,31,30,31,30,31];function Jc(a){var b=Array(tc(a)+1);uc(a,b,0,b.length);return b}var Kc=(a,b)=>{aa().set(a,b>>>0)}; +function Pb(a,b,c,d){function f(e,p,t){for(e="number"==typeof e?e.toString():e||"";e.lengthXb?-1:0J-e.getDate())p-=J-e.getDate()+1,e.setDate(1),11>t?e.setMonth(t+1):(e.setMonth(0),e.setFullYear(e.getFullYear()+1));else{e.setDate(e.getDate()+p);break}}t=new Date(e.getFullYear()+1,0,4);p=q(new Date(e.getFullYear(), +0,4));t=q(t);return 0>=k(p,e)?0>=k(t,e)?e.getFullYear()+1:e.getFullYear():e.getFullYear()-1}a>>>=0;b>>>=0;c>>>=0;d>>>=0;var v=u()[d+40>>>2>>>0];d={gb:r()[d>>>2>>>0],fb:r()[d+4>>>2>>>0],Va:r()[d+8>>>2>>>0],Ya:r()[d+12>>>2>>>0],Wa:r()[d+16>>>2>>>0],Ta:r()[d+20>>>2>>>0],Qa:r()[d+24>>>2>>>0],Sa:r()[d+28>>>2>>>0],ob:r()[d+32>>>2>>>0],eb:r()[d+36>>>2>>>0],hb:v?Q(v):""};c=Q(c);v={"%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"};for(var z in v)c=c.replace(new RegExp(z,"g"),v[z]);var Yb="Sunday Monday Tuesday Wednesday Thursday Friday Saturday".split(" "),Zb="January February March April May June July August September October November December".split(" ");v={"%a":e=>Yb[e.Qa].substring(0,3), +"%A":e=>Yb[e.Qa],"%b":e=>Zb[e.Wa].substring(0,3),"%B":e=>Zb[e.Wa],"%C":e=>h((e.Ta+1900)/100|0,2),"%d":e=>h(e.Ya,2),"%e":e=>f(e.Ya,2," "),"%g":e=>y(e).toString().substring(2),"%G":y,"%H":e=>h(e.Va,2),"%I":e=>{e=e.Va;0==e?e=12:12{for(var p=0,t=0;t<=e.Wa-1;p+=(X(e.Ta+1900)?Hc:Ic)[t++]);return h(e.Ya+p,3)},"%m":e=>h(e.Wa+1,2),"%M":e=>h(e.fb,2),"%n":()=>"\n","%p":e=>0<=e.Va&&12>e.Va?"AM":"PM","%S":e=>h(e.gb,2),"%t":()=>"\t","%u":e=>e.Qa||7,"%U":e=>h(Math.floor((e.Sa+ +7-e.Qa)/7),2),"%V":e=>{var p=Math.floor((e.Sa+7-(e.Qa+6)%7)/7);2>=(e.Qa+371-e.Sa-2)%7&&p++;if(p)53==p&&(t=(e.Qa+371-e.Sa)%7,4==t||3==t&&X(e.Ta)||(p=1));else{p=52;var t=(e.Qa+7-e.Sa-1)%7;(4==t||5==t&&X(e.Ta%400-1))&&p++}return h(p,2)},"%w":e=>e.Qa,"%W":e=>h(Math.floor((e.Sa+7-(e.Qa+6)%7)/7),2),"%y":e=>(e.Ta+1900).toString().substring(2),"%Y":e=>e.Ta+1900,"%z":e=>{e=e.eb;var p=0<=e;e=Math.abs(e)/60;return(p?"+":"-")+String("0000"+(e/60*100+e%60)).slice(-4)},"%Z":e=>e.hb,"%%":()=>"%"};c=c.replace(/%%/g, +"\x00\x00");for(z in v)c.includes(z)&&(c=c.replace(new RegExp(z,"g"),v[z](d)));c=c.replace(/\0\0/g,"%");z=Jc(c);if(z.length>b)return 0;Kc(z,a);return z.length-1}function Qb(a,b,c,d){return Pb(a>>>0,b>>>0,c>>>0,d>>>0)}C||fc(); +var xc=[Ob,dc,qc,Wa,Xa,Ya,Za,$a,ab,bb,cb,db,eb,fb,gb,hb,sb,tb,Hb,Ib,Kb,Lb,Mb,Nb],Sa,Z=function(){function a(c,d){Z=c.exports;Z=Lc();ec.push(Z.Ea);mc=Z.Fa;Ha.unshift(Z.$);Ea=d;Ka();return Z}var b=va();O++;if(w.instantiateWasm)try{return w.instantiateWasm(b,a)}catch(c){I(`Module.instantiateWasm callback failed with error: ${c}`),x(c)}Na||=w.locateFile?Ma("ort-wasm-simd-threaded.wasm")?"ort-wasm-simd-threaded.wasm":w.locateFile?w.locateFile("ort-wasm-simd-threaded.wasm",F):F+"ort-wasm-simd-threaded.wasm": +(new URL("ort-wasm-simd-threaded.wasm",import.meta.url)).href;Ra(b,function(c){a(c.instance,c.module)}).catch(x);return{}}();w._OrtInit=(a,b)=>(w._OrtInit=Z.aa)(a,b);w._OrtGetLastError=(a,b)=>(w._OrtGetLastError=Z.ba)(a,b);w._OrtCreateSessionOptions=(a,b,c,d,f,h,k,q,y,v)=>(w._OrtCreateSessionOptions=Z.ca)(a,b,c,d,f,h,k,q,y,v);w._OrtAppendExecutionProvider=(a,b)=>(w._OrtAppendExecutionProvider=Z.da)(a,b);w._OrtAddFreeDimensionOverride=(a,b,c)=>(w._OrtAddFreeDimensionOverride=Z.ea)(a,b,c); +w._OrtAddSessionConfigEntry=(a,b,c)=>(w._OrtAddSessionConfigEntry=Z.fa)(a,b,c);w._OrtReleaseSessionOptions=a=>(w._OrtReleaseSessionOptions=Z.ga)(a);w._OrtCreateSession=(a,b,c)=>(w._OrtCreateSession=Z.ha)(a,b,c);w._OrtReleaseSession=a=>(w._OrtReleaseSession=Z.ia)(a);w._OrtGetInputOutputCount=(a,b,c)=>(w._OrtGetInputOutputCount=Z.ja)(a,b,c);w._OrtGetInputName=(a,b)=>(w._OrtGetInputName=Z.ka)(a,b);w._OrtGetOutputName=(a,b)=>(w._OrtGetOutputName=Z.la)(a,b);w._OrtFree=a=>(w._OrtFree=Z.ma)(a); +w._OrtCreateTensor=(a,b,c,d,f,h)=>(w._OrtCreateTensor=Z.na)(a,b,c,d,f,h);w._OrtGetTensorData=(a,b,c,d,f)=>(w._OrtGetTensorData=Z.oa)(a,b,c,d,f);w._OrtReleaseTensor=a=>(w._OrtReleaseTensor=Z.pa)(a);w._OrtCreateRunOptions=(a,b,c,d)=>(w._OrtCreateRunOptions=Z.qa)(a,b,c,d);w._OrtAddRunConfigEntry=(a,b,c)=>(w._OrtAddRunConfigEntry=Z.ra)(a,b,c);w._OrtReleaseRunOptions=a=>(w._OrtReleaseRunOptions=Z.sa)(a);w._OrtCreateBinding=a=>(w._OrtCreateBinding=Z.ta)(a); +w._OrtBindInput=(a,b,c)=>(w._OrtBindInput=Z.ua)(a,b,c);w._OrtBindOutput=(a,b,c,d)=>(w._OrtBindOutput=Z.va)(a,b,c,d);w._OrtClearBoundOutputs=a=>(w._OrtClearBoundOutputs=Z.wa)(a);w._OrtReleaseBinding=a=>(w._OrtReleaseBinding=Z.xa)(a);w._OrtRunWithBinding=(a,b,c,d,f)=>(w._OrtRunWithBinding=Z.ya)(a,b,c,d,f);w._OrtRun=(a,b,c,d,f,h,k,q)=>(w._OrtRun=Z.za)(a,b,c,d,f,h,k,q);w._OrtEndProfiling=a=>(w._OrtEndProfiling=Z.Aa)(a);var K=()=>(K=Z.Ba)();w._malloc=a=>(w._malloc=Z.Ca)(a);w._free=a=>(w._free=Z.Da)(a); +var wa=(a,b,c,d,f,h)=>(wa=Z.Ga)(a,b,c,d,f,h),Da=()=>(Da=Z.Ha)(),bc=(a,b,c,d,f)=>(bc=Z.Ia)(a,b,c,d,f),hc=a=>(hc=Z.Ja)(a),Ba=a=>(Ba=Z.Ka)(a),vc=()=>(vc=Z.La)(),kc=(a,b)=>(kc=Z.Ma)(a,b),cc=a=>(cc=Z.Na)(a),ac=a=>(ac=Z.Oa)(a),$b=()=>($b=Z.Pa)();w.___start_em_js=838360;w.___stop_em_js=838421; +function Lc(){var a=Z;a=Object.assign({},a);var b=d=>()=>d()>>>0,c=d=>f=>d(f)>>>0;a.Ba=b(a.Ba);a.Ca=c(a.Ca);a.emscripten_main_runtime_thread_id=b(a.emscripten_main_runtime_thread_id);a.Oa=c(a.Oa);a.Pa=b(a.Pa);return a}w.stackSave=()=>$b();w.stackRestore=a=>cc(a);w.stackAlloc=a=>ac(a);w.UTF8ToString=Q;w.stringToUTF8=W;w.lengthBytesUTF8=tc;var Mc;P=function Nc(){Mc||Oc();Mc||(P=Nc)}; +function Oc(){0{var Ic=Object.create;var yr=Object.defineProperty;var _c=Object.getOwnPropertyDescriptor;var Sc=Object.getOwnPropertyNames;var Oc=Object.getPrototypeOf,Ac=Object.prototype.hasOwnProperty;var Bn=(i=>typeof require<"u"?require:typeof Proxy<"u"?new Proxy(i,{get:(e,o)=>(typeof require<"u"?require:e)[o]}):i)(function(i){if(typeof require<"u")return require.apply(this,arguments);throw Error('Dynamic require of "'+i+'" is not supported')});var x=(i,e)=>()=>(i&&(e=i(i=0)),e);var tt=(i,e)=>()=>(e||i((e={exports:{}}).exports,e),e.exports),We=(i,e)=>{for(var o in e)yr(i,o,{get:e[o],enumerable:!0})},oi=(i,e,o,t)=>{if(e&&typeof e=="object"||typeof e=="function")for(let r of Sc(e))!Ac.call(i,r)&&r!==o&&yr(i,r,{get:()=>e[r],enumerable:!(t=_c(e,r))||t.enumerable});return i};var Ae=(i,e,o)=>(o=i!=null?Ic(Oc(i)):{},oi(e||!i||!i.__esModule?yr(o,"default",{value:i,enumerable:!0}):o,i)),gr=i=>oi(yr({},"__esModule",{value:!0}),i);var xr,se,ue,Pc,Tr,wr=x(()=>{"use strict";xr=new Map,se=[],ue=(i,e,o)=>{if(e&&typeof e.init=="function"&&typeof e.createInferenceSessionHandler=="function"){let t=xr.get(i);if(t===void 0)xr.set(i,{backend:e,priority:o});else{if(t.priority>o)return;if(t.priority===o&&t.backend!==e)throw new Error(`cannot register backend "${i}" using priority ${o}`)}if(o>=0){let r=se.indexOf(i);r!==-1&&se.splice(r,1);for(let n=0;n{let e=xr.get(i);if(!e)return"backend not found.";if(e.initialized)return e.backend;if(e.aborted)return e.error;{let o=!!e.initPromise;try{return o||(e.initPromise=e.backend.init(i)),await e.initPromise,e.initialized=!0,e.backend}catch(t){return o||(e.error=`${t}`,e.aborted=!0),e.error}finally{delete e.initPromise}}},Tr=async i=>{let e=i.executionProviders||[],o=e.map(u=>typeof u=="string"?u:u.name),t=o.length===0?se:o,r,n=[],s=new Set;for(let u of t){let l=await Pc(u);typeof l=="string"?n.push({name:u,err:l}):(r||(r=l),r===l&&s.add(u))}if(!r)throw new Error(`no available backend found. ERR: ${n.map(u=>`[${u.name}] ${u.err}`).join(", ")}`);for(let{name:u,err:l}of n)o.includes(u)&&console.warn(`removing requested execution provider "${u}" from session options because it is not available: ${l}`);let a=e.filter(u=>s.has(typeof u=="string"?u:u.name));return[r,new Proxy(i,{get:(u,l)=>l==="executionProviders"?a:Reflect.get(u,l)})]}});var ii=x(()=>{"use strict";wr()});var ai,si=x(()=>{"use strict";ai="1.20.1"});var ui,Tt,Fn=x(()=>{"use strict";si();ui="warning",Tt={wasm:{},webgl:{},webgpu:{},versions:{common:ai},set logLevel(i){if(i!==void 0){if(typeof i!="string"||["verbose","info","warning","error","fatal"].indexOf(i)===-1)throw new Error(`Unsupported logging level: ${i}`);ui=i}},get logLevel(){return ui}};Object.defineProperty(Tt,"logLevel",{enumerable:!0})});var $,li=x(()=>{"use strict";Fn();$=Tt});var fi,ci,pi=x(()=>{"use strict";fi=(i,e)=>{let o=typeof document<"u"?document.createElement("canvas"):new OffscreenCanvas(1,1);o.width=i.dims[3],o.height=i.dims[2];let t=o.getContext("2d");if(t!=null){let r,n;e?.tensorLayout!==void 0&&e.tensorLayout==="NHWC"?(r=i.dims[2],n=i.dims[3]):(r=i.dims[3],n=i.dims[2]);let s=e?.format!==void 0?e.format:"RGB",a=e?.norm,u,l;a===void 0||a.mean===void 0?u=[255,255,255,255]:typeof a.mean=="number"?u=[a.mean,a.mean,a.mean,a.mean]:(u=[a.mean[0],a.mean[1],a.mean[2],0],a.mean[3]!==void 0&&(u[3]=a.mean[3])),a===void 0||a.bias===void 0?l=[0,0,0,0]:typeof a.bias=="number"?l=[a.bias,a.bias,a.bias,a.bias]:(l=[a.bias[0],a.bias[1],a.bias[2],0],a.bias[3]!==void 0&&(l[3]=a.bias[3]));let f=n*r,c=0,d=f,m=f*2,y=-1;s==="RGBA"?(c=0,d=f,m=f*2,y=f*3):s==="RGB"?(c=0,d=f,m=f*2):s==="RBG"&&(c=0,m=f,d=f*2);for(let g=0;g{let o=typeof document<"u"?document.createElement("canvas").getContext("2d"):new OffscreenCanvas(1,1).getContext("2d"),t;if(o!=null){let r,n,s;e?.tensorLayout!==void 0&&e.tensorLayout==="NHWC"?(r=i.dims[2],n=i.dims[1],s=i.dims[3]):(r=i.dims[3],n=i.dims[2],s=i.dims[1]);let a=e!==void 0&&e.format!==void 0?e.format:"RGB",u=e?.norm,l,f;u===void 0||u.mean===void 0?l=[255,255,255,255]:typeof u.mean=="number"?l=[u.mean,u.mean,u.mean,u.mean]:(l=[u.mean[0],u.mean[1],u.mean[2],255],u.mean[3]!==void 0&&(l[3]=u.mean[3])),u===void 0||u.bias===void 0?f=[0,0,0,0]:typeof u.bias=="number"?f=[u.bias,u.bias,u.bias,u.bias]:(f=[u.bias[0],u.bias[1],u.bias[2],0],u.bias[3]!==void 0&&(f[3]=u.bias[3]));let c=n*r;if(e!==void 0&&(e.format!==void 0&&s===4&&e.format!=="RGBA"||s===3&&e.format!=="RGB"&&e.format!=="BGR"))throw new Error("Tensor format doesn't match input tensor dims");let d=4,m=0,y=1,g=2,T=3,_=0,w=c,v=c*2,L=-1;a==="RGBA"?(_=0,w=c,v=c*2,L=c*3):a==="RGB"?(_=0,w=c,v=c*2):a==="RBG"&&(_=0,v=c,w=c*2),t=o.createImageData(r,n);for(let B=0;B{"use strict";vr();Nn=(i,e)=>{if(i===void 0)throw new Error("Image buffer must be defined");if(e.height===void 0||e.width===void 0)throw new Error("Image height and width must be defined");if(e.tensorLayout==="NHWC")throw new Error("NHWC Tensor layout is not supported yet");let{height:o,width:t}=e,r=e.norm??{mean:255,bias:0},n,s;typeof r.mean=="number"?n=[r.mean,r.mean,r.mean,r.mean]:n=[r.mean[0],r.mean[1],r.mean[2],r.mean[3]??255],typeof r.bias=="number"?s=[r.bias,r.bias,r.bias,r.bias]:s=[r.bias[0],r.bias[1],r.bias[2],r.bias[3]??0];let a=e.format!==void 0?e.format:"RGBA",u=e.tensorFormat!==void 0&&e.tensorFormat!==void 0?e.tensorFormat:"RGB",l=o*t,f=u==="RGBA"?new Float32Array(l*4):new Float32Array(l*3),c=4,d=0,m=1,y=2,g=3,T=0,_=l,w=l*2,v=-1;a==="RGB"&&(c=3,d=0,m=1,y=2,g=-1),u==="RGBA"?v=l*3:u==="RBG"?(T=0,w=l,_=l*2):u==="BGR"&&(w=0,_=l,T=l*2);for(let B=0;B{let o=typeof HTMLImageElement<"u"&&i instanceof HTMLImageElement,t=typeof ImageData<"u"&&i instanceof ImageData,r=typeof ImageBitmap<"u"&&i instanceof ImageBitmap,n=typeof i=="string",s,a=e??{},u=()=>{if(typeof document<"u")return document.createElement("canvas");if(typeof OffscreenCanvas<"u")return new OffscreenCanvas(1,1);throw new Error("Canvas is not supported")},l=f=>typeof HTMLCanvasElement<"u"&&f instanceof HTMLCanvasElement||f instanceof OffscreenCanvas?f.getContext("2d"):null;if(o){let f=u();f.width=i.width,f.height=i.height;let c=l(f);if(c!=null){let d=i.height,m=i.width;if(e!==void 0&&e.resizedHeight!==void 0&&e.resizedWidth!==void 0&&(d=e.resizedHeight,m=e.resizedWidth),e!==void 0){if(a=e,e.tensorFormat!==void 0)throw new Error("Image input config format must be RGBA for HTMLImageElement");a.tensorFormat="RGBA",a.height=d,a.width=m}else a.tensorFormat="RGBA",a.height=d,a.width=m;c.drawImage(i,0,0),s=c.getImageData(0,0,m,d).data}else throw new Error("Can not access image data")}else if(t){let f,c;if(e!==void 0&&e.resizedWidth!==void 0&&e.resizedHeight!==void 0?(f=e.resizedHeight,c=e.resizedWidth):(f=i.height,c=i.width),e!==void 0&&(a=e),a.format="RGBA",a.height=f,a.width=c,e!==void 0){let d=u();d.width=c,d.height=f;let m=l(d);if(m!=null)m.putImageData(i,0,0),s=m.getImageData(0,0,c,f).data;else throw new Error("Can not access image data")}else s=i.data}else if(r){if(e===void 0)throw new Error("Please provide image config with format for Imagebitmap");let f=u();f.width=i.width,f.height=i.height;let c=l(f);if(c!=null){let d=i.height,m=i.width;return c.drawImage(i,0,0,m,d),s=c.getImageData(0,0,m,d).data,a.height=d,a.width=m,Nn(s,a)}else throw new Error("Can not access image data")}else{if(n)return new Promise((f,c)=>{let d=u(),m=l(d);if(!i||!m)return c();let y=new Image;y.crossOrigin="Anonymous",y.src=i,y.onload=()=>{d.width=y.width,d.height=y.height,m.drawImage(y,0,0,d.width,d.height);let g=m.getImageData(0,0,d.width,d.height);a.height=d.height,a.width=d.width,f(Nn(g.data,a))}});throw new Error("Input data provided is not supported - aborted tensor creation")}if(s!==void 0)return Nn(s,a);throw new Error("Input data provided is not supported - aborted tensor creation")},hi=(i,e)=>{let{width:o,height:t,download:r,dispose:n}=e,s=[1,t,o,4];return new st({location:"texture",type:"float32",texture:i,dims:s,download:r,dispose:n})},mi=(i,e)=>{let{dataType:o,dims:t,download:r,dispose:n}=e;return new st({location:"gpu-buffer",type:o??"float32",gpuBuffer:i,dims:t,download:r,dispose:n})},bi=(i,e)=>{let{dataType:o,dims:t,download:r,dispose:n}=e;return new st({location:"ml-tensor",type:o??"float32",mlTensor:i,dims:t,download:r,dispose:n})},yi=(i,e,o)=>new st({location:"cpu-pinned",type:i,data:e,dims:o??[e.length]})});var le,He,xi,Ti,wi=x(()=>{"use strict";le=new Map([["float32",Float32Array],["uint8",Uint8Array],["int8",Int8Array],["uint16",Uint16Array],["int16",Int16Array],["int32",Int32Array],["bool",Uint8Array],["float64",Float64Array],["uint32",Uint32Array],["int4",Uint8Array],["uint4",Uint8Array]]),He=new Map([[Float32Array,"float32"],[Uint8Array,"uint8"],[Int8Array,"int8"],[Uint16Array,"uint16"],[Int16Array,"int16"],[Int32Array,"int32"],[Float64Array,"float64"],[Uint32Array,"uint32"]]),xi=!1,Ti=()=>{if(!xi){xi=!0;let i=typeof BigInt64Array<"u"&&BigInt64Array.from,e=typeof BigUint64Array<"u"&&BigUint64Array.from,o=typeof Float16Array<"u"&&Float16Array.from;i&&(le.set("int64",BigInt64Array),He.set(BigInt64Array,"int64")),e&&(le.set("uint64",BigUint64Array),He.set(BigUint64Array,"uint64")),o?(le.set("float16",Float16Array),He.set(Float16Array,"float16")):le.set("float16",Uint16Array)}}});var vi,Ii,_i=x(()=>{"use strict";vr();vi=i=>{let e=1;for(let o=0;o{switch(i.location){case"cpu":return new st(i.type,i.data,e);case"cpu-pinned":return new st({location:"cpu-pinned",data:i.data,type:i.type,dims:e});case"texture":return new st({location:"texture",texture:i.texture,type:i.type,dims:e});case"gpu-buffer":return new st({location:"gpu-buffer",gpuBuffer:i.gpuBuffer,type:i.type,dims:e});case"ml-tensor":return new st({location:"ml-tensor",mlTensor:i.mlTensor,type:i.type,dims:e});default:throw new Error(`tensorReshape: tensor location ${i.location} is not supported`)}}});var st,vr=x(()=>{"use strict";pi();gi();wi();_i();st=class{constructor(e,o,t){Ti();let r,n;if(typeof e=="object"&&"location"in e)switch(this.dataLocation=e.location,r=e.type,n=e.dims,e.location){case"cpu-pinned":{let a=le.get(r);if(!a)throw new TypeError(`unsupported type "${r}" to create tensor from pinned buffer`);if(!(e.data instanceof a))throw new TypeError(`buffer should be of type ${a.name}`);this.cpuData=e.data;break}case"texture":{if(r!=="float32")throw new TypeError(`unsupported type "${r}" to create tensor from texture`);this.gpuTextureData=e.texture,this.downloader=e.download,this.disposer=e.dispose;break}case"gpu-buffer":{if(r!=="float32"&&r!=="float16"&&r!=="int32"&&r!=="int64"&&r!=="uint32"&&r!=="uint8"&&r!=="bool"&&r!=="uint4"&&r!=="int4")throw new TypeError(`unsupported type "${r}" to create tensor from gpu buffer`);this.gpuBufferData=e.gpuBuffer,this.downloader=e.download,this.disposer=e.dispose;break}case"ml-tensor":{if(r!=="float32"&&r!=="float16"&&r!=="int32"&&r!=="int64"&&r!=="uint32"&&r!=="uint64"&&r!=="int8"&&r!=="uint8"&&r!=="bool")throw new TypeError(`unsupported type "${r}" to create tensor from MLTensor`);this.mlTensorData=e.mlTensor,this.downloader=e.download,this.disposer=e.dispose;break}default:throw new Error(`Tensor constructor: unsupported location '${this.dataLocation}'`)}else{let a,u;if(typeof e=="string")if(r=e,u=t,e==="string"){if(!Array.isArray(o))throw new TypeError("A string tensor's data must be a string array.");a=o}else{let l=le.get(e);if(l===void 0)throw new TypeError(`Unsupported tensor type: ${e}.`);if(Array.isArray(o)){if(e==="float16"&&l===Uint16Array||e==="uint4"||e==="int4")throw new TypeError(`Creating a ${e} tensor from number array is not supported. Please use ${l.name} as data.`);e==="uint64"||e==="int64"?a=l.from(o,BigInt):a=l.from(o)}else if(o instanceof l)a=o;else if(o instanceof Uint8ClampedArray)if(e==="uint8")a=Uint8Array.from(o);else throw new TypeError("A Uint8ClampedArray tensor's data must be type of uint8");else throw new TypeError(`A ${r} tensor's data must be type of ${l}`)}else if(u=o,Array.isArray(e)){if(e.length===0)throw new TypeError("Tensor type cannot be inferred from an empty array.");let l=typeof e[0];if(l==="string")r="string",a=e;else if(l==="boolean")r="bool",a=Uint8Array.from(e);else throw new TypeError(`Invalid element type of data array: ${l}.`)}else if(e instanceof Uint8ClampedArray)r="uint8",a=Uint8Array.from(e);else{let l=He.get(e.constructor);if(l===void 0)throw new TypeError(`Unsupported type for tensor data: ${e.constructor}.`);r=l,a=e}if(u===void 0)u=[a.length];else if(!Array.isArray(u))throw new TypeError("A tensor's dims must be a number array");n=u,this.cpuData=a,this.dataLocation="cpu"}let s=vi(n);if(this.cpuData&&s!==this.cpuData.length&&!((r==="uint4"||r==="int4")&&Math.ceil(s/2)===this.cpuData.length))throw new Error(`Tensor's size(${s}) does not match data length(${this.cpuData.length}).`);this.type=r,this.dims=n,this.size=s}static async fromImage(e,o){return di(e,o)}static fromTexture(e,o){return hi(e,o)}static fromGpuBuffer(e,o){return mi(e,o)}static fromMLTensor(e,o){return bi(e,o)}static fromPinnedBuffer(e,o,t){return yi(e,o,t)}toDataURL(e){return fi(this,e)}toImageData(e){return ci(this,e)}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(e){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 o=await this.downloader();return this.downloader=void 0,this.dataLocation="cpu",this.cpuData=o,e&&this.disposer&&(this.disposer(),this.disposer=void 0),o}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(e){if(this.ensureValid(),this.downloader||this.disposer)throw new Error("Cannot reshape a tensor that owns GPU resource.");return Ii(this,e)}}});var et,Ir=x(()=>{"use strict";vr();et=st});var Cn,Si,Jt,Zt,Rn=x(()=>{"use strict";Fn();Cn=(i,e)=>{(typeof Tt.trace>"u"?!Tt.wasm.trace:!Tt.trace)||console.timeStamp(`${i}::ORT::${e}`)},Si=(i,e)=>{let o=new Error().stack?.split(/\r\n|\r|\n/g)||[],t=!1;for(let r=0;r{(typeof Tt.trace>"u"?!Tt.wasm.trace:!Tt.trace)||Si("BEGIN",i)},Zt=i=>{(typeof Tt.trace>"u"?!Tt.wasm.trace:!Tt.trace)||Si("END",i)}});var _r,Oi=x(()=>{"use strict";wr();Ir();Rn();_r=class i{constructor(e){this.handler=e}async run(e,o,t){Jt();let r={},n={};if(typeof e!="object"||e===null||e instanceof et||Array.isArray(e))throw new TypeError("'feeds' must be an object that use input names as keys and OnnxValue as corresponding values.");let s=!0;if(typeof o=="object"){if(o===null)throw new TypeError("Unexpected argument[1]: cannot be null.");if(o instanceof et)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.");s=!1;for(let l of o){if(typeof l!="string")throw new TypeError("'fetches' must be a string array or an object.");if(this.outputNames.indexOf(l)===-1)throw new RangeError(`'fetches' contains invalid output name: ${l}.`);r[l]=null}if(typeof t=="object"&&t!==null)n=t;else if(typeof t<"u")throw new TypeError("'options' must be an object.")}else{let l=!1,f=Object.getOwnPropertyNames(o);for(let c of this.outputNames)if(f.indexOf(c)!==-1){let d=o[c];(d===null||d instanceof et)&&(l=!0,s=!1,r[c]=d)}if(l){if(typeof t=="object"&&t!==null)n=t;else if(typeof t<"u")throw new TypeError("'options' must be an object.")}else n=o}}else if(typeof o<"u")throw new TypeError("Unexpected argument[1]: must be 'fetches' or 'options'.");for(let l of this.inputNames)if(typeof e[l]>"u")throw new Error(`input '${l}' is missing in 'feeds'.`);if(s)for(let l of this.outputNames)r[l]=null;let a=await this.handler.run(e,r,n),u={};for(let l in a)if(Object.hasOwnProperty.call(a,l)){let f=a[l];f instanceof et?u[l]=f:u[l]=new et(f.type,f.data,f.dims)}return Zt(),u}async release(){return this.handler.dispose()}static async create(e,o,t,r){Jt();let n,s={};if(typeof e=="string"){if(n=e,typeof o=="object"&&o!==null)s=o;else if(typeof o<"u")throw new TypeError("'options' must be an object.")}else if(e instanceof Uint8Array){if(n=e,typeof o=="object"&&o!==null)s=o;else if(typeof o<"u")throw new TypeError("'options' must be an object.")}else if(e instanceof ArrayBuffer||typeof SharedArrayBuffer<"u"&&e instanceof SharedArrayBuffer){let f=e,c=0,d=e.byteLength;if(typeof o=="object"&&o!==null)s=o;else if(typeof o=="number"){if(c=o,!Number.isSafeInteger(c))throw new RangeError("'byteOffset' must be an integer.");if(c<0||c>=f.byteLength)throw new RangeError(`'byteOffset' is out of range [0, ${f.byteLength}).`);if(d=e.byteLength-c,typeof t=="number"){if(d=t,!Number.isSafeInteger(d))throw new RangeError("'byteLength' must be an integer.");if(d<=0||c+d>f.byteLength)throw new RangeError(`'byteLength' is out of range (0, ${f.byteLength-c}].`);if(typeof r=="object"&&r!==null)s=r;else if(typeof r<"u")throw new TypeError("'options' must be an object.")}else if(typeof t<"u")throw new TypeError("'byteLength' must be a number.")}else if(typeof o<"u")throw new TypeError("'options' must be an object.");n=new Uint8Array(f,c,d)}else throw new TypeError("Unexpected argument[0]: must be 'path' or 'buffer'.");let[a,u]=await Tr(s),l=await a.createInferenceSessionHandler(n,u);return Zt(),new i(l)}startProfiling(){this.handler.startProfiling()}endProfiling(){this.handler.endProfiling()}get inputNames(){return this.handler.inputNames}get outputNames(){return this.handler.outputNames}}});var Ai,Pi=x(()=>{"use strict";Oi();Ai=_r});var Ei=x(()=>{"use strict"});var Di=x(()=>{"use strict"});var Li=x(()=>{"use strict"});var $i=x(()=>{"use strict"});var Ec,Sr,ki=x(()=>{"use strict";wr();Ir();Ec="Training backend could not be resolved. Make sure you're using the correct configuration & WebAssembly files.",Sr=class i{constructor(e,o,t){this.handler=e,this.hasOptimizerModel=o,this.hasEvalModel=t}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(e,o){let t=e.evalModel||"",r=e.optimizerModel||"",n=o||{},[s,a]=await Tr(n);if(s.createTrainingSessionHandler){let u=await s.createTrainingSessionHandler(e.checkpointState,e.trainModel,t,r,a);return new i(u,!!e.optimizerModel,!!e.evalModel)}else throw new Error(Ec)}typeNarrowingForRunStep(e,o,t,r,n){let s={},a={};if(typeof t!="object"||t===null||t instanceof et||Array.isArray(t))throw new TypeError("'feeds' must be an object that use input names as keys and OnnxValue as corresponding values.");let u=!0;if(typeof r=="object"){if(r===null)throw new TypeError("Unexpected argument[1]: cannot be null.");if(r instanceof et)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.");u=!1;for(let l of r){if(typeof l!="string")throw new TypeError("'fetches' must be a string array or an object.");if(o.indexOf(l)===-1)throw new RangeError(`'fetches' contains invalid output name: ${l}.`);s[l]=null}if(typeof n=="object"&&n!==null)a=n;else if(typeof n<"u")throw new TypeError("'options' must be an object.")}else{let l=!1,f=Object.getOwnPropertyNames(r);for(let c of o)if(f.indexOf(c)!==-1){let d=r[c];(d===null||d instanceof et)&&(l=!0,u=!1,s[c]=d)}if(l){if(typeof n=="object"&&n!==null)a=n;else if(typeof n<"u")throw new TypeError("'options' must be an object.")}else a=r}}else if(typeof r<"u")throw new TypeError("Unexpected argument[1]: must be 'fetches' or 'options'.");for(let l of e)if(typeof t[l]>"u")throw new Error(`input '${l}' is missing in 'feeds'.`);if(u)for(let l of o)s[l]=null;return[s,a]}convertHandlerReturnTypeToMapOfTensors(e){let o={};for(let t in e)if(Object.hasOwnProperty.call(e,t)){let r=e[t];r instanceof et?o[t]=r:o[t]=new et(r.type,r.data,r.dims)}return o}async lazyResetGrad(){await this.handler.lazyResetGrad()}async runTrainStep(e,o,t){let[r,n]=this.typeNarrowingForRunStep(this.trainingInputNames,this.trainingOutputNames,e,o,t),s=await this.handler.runTrainStep(e,r,n);return this.convertHandlerReturnTypeToMapOfTensors(s)}async runOptimizerStep(e){if(this.hasOptimizerModel)await this.handler.runOptimizerStep(e||{});else throw new Error("This TrainingSession has no OptimizerModel loaded.")}async runEvalStep(e,o,t){if(this.hasEvalModel){let[r,n]=this.typeNarrowingForRunStep(this.evalInputNames,this.evalOutputNames,e,o,t),s=await this.handler.runEvalStep(e,r,n);return this.convertHandlerReturnTypeToMapOfTensors(s)}else throw new Error("This TrainingSession has no EvalModel loaded.")}async getParametersSize(e=!0){return this.handler.getParametersSize(e)}async loadParametersBuffer(e,o=!0){let t=await this.getParametersSize(o);if(e.length!==4*t)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(e,o)}async getContiguousParameters(e=!0){return this.handler.getContiguousParameters(e)}async release(){return this.handler.dispose()}}});var Bi,Fi=x(()=>{"use strict";ki();Bi=Sr});var Gn={};We(Gn,{InferenceSession:()=>Ai,TRACE:()=>Cn,TRACE_FUNC_BEGIN:()=>Jt,TRACE_FUNC_END:()=>Zt,Tensor:()=>et,TrainingSession:()=>Bi,env:()=>$,registerBackend:()=>ue});var kt=x(()=>{"use strict";ii();li();Pi();Ir();Ei();Di();Rn();Li();$i();Fi()});function Yt(i,e,o,t){if(e===void 0)return Lc(i);if(o===void 0)Or(i,e,1);else if(typeof o=="number"&&t===void 0)Or(i,e,o);else if(typeof o=="string"&&t===void 0)Or(i,o,1,e);else if(typeof o=="string"&&typeof t=="number")Or(i,o,t,e);else throw new TypeError("input is valid")}function Lc(i){return{verbose:Yt.verbose.bind(null,i),info:Yt.info.bind(null,i),warning:Yt.warning.bind(null,i),error:Yt.error.bind(null,i),fatal:Yt.fatal.bind(null,i)}}function Or(i,e,o,t){let r=qe[t||""]||qe[""];Ci[i]{"use strict";Mn=class{log(e,o,t){}},Vn=class{log(e,o,t){console.log(`${this.color(e)} ${t?"\x1B[35m"+t+"\x1B[0m ":""}${o}`)}color(e){switch(e){case"verbose":return"\x1B[34;40mv\x1B[0m";case"info":return"\x1B[32mi\x1B[0m";case"warning":return"\x1B[30;43mw\x1B[0m";case"error":return"\x1B[31;40me\x1B[0m";case"fatal":return"\x1B[101mf\x1B[0m";default:throw new Error(`unsupported severity: ${e}`)}}},Ci={verbose:1e3,info:2e3,warning:4e3,error:5e3,fatal:6e3},Dc={none:new Mn,console:new Vn},Ri={provider:"console",minimalSeverity:"warning",logDateTime:!0,logSourceLocation:!1},qe={"":Ri};(u=>{function i(l,f){u("verbose",l,f)}u.verbose=i;function e(l,f){u("info",l,f)}u.info=e;function o(l,f){u("warning",l,f)}u.warning=o;function t(l,f){u("error",l,f)}u.error=t;function r(l,f){u("fatal",l,f)}u.fatal=r;function n(l){qe={},s("",l||{})}u.reset=n;function s(l,f){if(l==="*")n(f);else{let c=qe[l]||Ri;qe[l]={provider:f.provider||c.provider,minimalSeverity:f.minimalSeverity||c.minimalSeverity,logDateTime:f.logDateTime===void 0?c.logDateTime:f.logDateTime,logSourceLocation:f.logSourceLocation===void 0?c.logSourceLocation:f.logSourceLocation}}}u.set=s;function a(l){let f={};l.logLevel&&(f.minimalSeverity=l.logLevel),s("",f)}u.setWithEnv=a})(Yt||={});z=Yt,Pr=class{constructor(e,o,t,r,n,s){this.category=e;this.name=o;this.startTime=t;this.endCallback=r;this.timer=n;this.ctx=s}async end(){return this.endCallback(this)}async checkTimer(){if(this.ctx===void 0||this.timer===void 0)throw new Error("No webgl timer found");return this.ctx.endTimer(),this.ctx.waitForQueryAndGetTime(this.timer)}},Er=class{constructor(e,o,t,r){this.category=e;this.name=o;this.startTime=t;this.endTime=r}},Dr=class{constructor(e,o,t){this._started=!1;this._flushPointer=0;this._started=!1,this._maxNumberEvents=e===void 0?1e4:e,this._flushBatchSize=o===void 0?10:o,this._flushIntervalInMilliseconds=t===void 0?5e3:t}static create(e){return e===void 0?new this:new this(e.maxNumberEvents,e.flushBatchSize,e.flushIntervalInMilliseconds)}start(){this._started=!0,this._timingEvents=[],this._flushTime=Ar(),this._flushPointer=0}stop(){for(this._started=!1;this._flushPointer{a.then(async f=>{n&&await n.end(),u(f)},async f=>{n&&await n.end(),l(f)})});if(!s&&n){let u=n.end();if(u&&typeof u.then=="function")return new Promise((l,f)=>{u.then(()=>{l(a)},c=>{f(c)})})}return a}begin(e,o,t){if(!this._started)throw new Error("profiler is not started yet");if(t===void 0){let r=Ar();return this.flush(r),new Pr(e,o,r,n=>this.endSync(n))}else{let r=t.beginTimer();return new Pr(e,o,0,async n=>this.end(n),r,t)}}async end(e){let o=await e.checkTimer();this._timingEvents.length=this._flushBatchSize||e-this._flushTime>=this._flushIntervalInMilliseconds){for(let o=this._flushPointer;this._flushPointerperformance.now():Date.now});function Gi(i,e,o){for(let t of o){let r=t[0],n=t[1],s=t[2],a=t[3],u=t[4];if(i.opType===r){for(let l of e)if((l.domain===n||l.domain==="ai.onnx"&&n==="")&&$c(l.version,s))return{opImpl:a,opInit:u}}}throw new TypeError(`cannot resolve operator '${i.opType}' with opsets: ${e.map(t=>`${t.domain||"ai.onnx"} v${t.version}`).join(", ")}`)}function $c(i,e){if(e.endsWith("+")){let o=Number.parseInt(e.substring(0,e.length-1),10);return!isNaN(o)&&o<=i}else if(e.split("-").length===2){let o=e.split("-"),t=Number.parseInt(o[0],10),r=Number.parseInt(o[1],10);return!isNaN(t)&&!isNaN(r)&&t<=i&&i<=r}else return Number.parseInt(e,10)===i}var Mi=x(()=>{"use strict"});var Vi=tt(Un=>{"use strict";Un.__esModule=!0;var kc=function(){function i(e){if(!e)throw new TypeError("Invalid argument; `value` has no value.");this.value=i.EMPTY,e&&i.isGuid(e)&&(this.value=e)}return i.isGuid=function(e){var o=e.toString();return e&&(e instanceof i||i.validator.test(o))},i.create=function(){return new i([i.gen(2),i.gen(1),i.gen(1),i.gen(1),i.gen(3)].join("-"))},i.createEmpty=function(){return new i("emptyguid")},i.parse=function(e){return new i(e)},i.raw=function(){return[i.gen(2),i.gen(1),i.gen(1),i.gen(1),i.gen(3)].join("-")},i.gen=function(e){for(var o="",t=0;t>>=0,(r=0<=i&&i<256)&&(t=Wi[i],t)?t:(o=V(i,0,!0),r&&(Wi[i]=o),o)):(i|=0,(r=-128<=i&&i<128)&&(t=zi[i],t)?t:(o=V(i,i<0?-1:0,!1),r&&(zi[i]=o),o))}function St(i,e){if(isNaN(i))return e?qt:Bt;if(e){if(i<0)return qt;if(i>=Xi)return Zi}else{if(i<=-qi)return yt;if(i+1>=qi)return Ji}return i<0?St(-i,e).neg():V(i%Ee|0,i/Ee|0,e)}function V(i,e,o){return new H(i,e,o)}function Wn(i,e,o){if(i.length===0)throw Error("empty string");if(typeof e=="number"?(o=e,e=!1):e=!!e,i==="NaN"||i==="Infinity"||i==="+Infinity"||i==="-Infinity")return e?qt:Bt;if(o=o||10,o<2||360)throw Error("interior hyphen");if(t===0)return Wn(i.substring(1),e,o).neg();for(var r=St(Lr(o,8)),n=Bt,s=0;s{_t=null;try{_t=new WebAssembly.Instance(new WebAssembly.Module(new Uint8Array([0,97,115,109,1,0,0,0,1,13,2,96,0,1,127,96,4,127,127,127,127,1,127,3,7,6,0,1,1,1,1,1,6,6,1,127,1,65,0,11,7,50,6,3,109,117,108,0,1,5,100,105,118,95,115,0,2,5,100,105,118,95,117,0,3,5,114,101,109,95,115,0,4,5,114,101,109,95,117,0,5,8,103,101,116,95,104,105,103,104,0,0,10,191,1,6,4,0,35,0,11,36,1,1,126,32,0,173,32,1,173,66,32,134,132,32,2,173,32,3,173,66,32,134,132,126,34,4,66,32,135,167,36,0,32,4,167,11,36,1,1,126,32,0,173,32,1,173,66,32,134,132,32,2,173,32,3,173,66,32,134,132,127,34,4,66,32,135,167,36,0,32,4,167,11,36,1,1,126,32,0,173,32,1,173,66,32,134,132,32,2,173,32,3,173,66,32,134,132,128,34,4,66,32,135,167,36,0,32,4,167,11,36,1,1,126,32,0,173,32,1,173,66,32,134,132,32,2,173,32,3,173,66,32,134,132,129,34,4,66,32,135,167,36,0,32,4,167,11,36,1,1,126,32,0,173,32,1,173,66,32,134,132,32,2,173,32,3,173,66,32,134,132,130,34,4,66,32,135,167,36,0,32,4,167,11])),{}).exports}catch{}H.prototype.__isLong__;Object.defineProperty(H.prototype,"__isLong__",{value:!0});H.isLong=ct;zi={},Wi={};H.fromInt=fe;H.fromNumber=St;H.fromBits=V;Lr=Math.pow;H.fromString=Wn;H.fromValue=Ft;Hi=65536,Bc=1<<24,Ee=Hi*Hi,Xi=Ee*Ee,qi=Xi/2,ji=fe(Bc),Bt=fe(0);H.ZERO=Bt;qt=fe(0,!0);H.UZERO=qt;Pe=fe(1);H.ONE=Pe;Ki=fe(1,!0);H.UONE=Ki;zn=fe(-1);H.NEG_ONE=zn;Ji=V(-1,2147483647,!1);H.MAX_VALUE=Ji;Zi=V(-1,-1,!0);H.MAX_UNSIGNED_VALUE=Zi;yt=V(0,-2147483648,!1);H.MIN_VALUE=yt;I=H.prototype;I.toInt=function(){return this.unsigned?this.low>>>0:this.low};I.toNumber=function(){return this.unsigned?(this.high>>>0)*Ee+(this.low>>>0):this.high*Ee+(this.low>>>0)};I.toString=function(e){if(e=e||10,e<2||36>>0,f=l.toString(e);if(s=u,s.isZero())return f+a;for(;f.length<6;)f="0"+f;a=""+f+a}};I.getHighBits=function(){return this.high};I.getHighBitsUnsigned=function(){return this.high>>>0};I.getLowBits=function(){return this.low};I.getLowBitsUnsigned=function(){return this.low>>>0};I.getNumBitsAbs=function(){if(this.isNegative())return this.eq(yt)?64:this.neg().getNumBitsAbs();for(var e=this.high!=0?this.high:this.low,o=31;o>0&&!(e&1<=0};I.isOdd=function(){return(this.low&1)===1};I.isEven=function(){return(this.low&1)===0};I.equals=function(e){return ct(e)||(e=Ft(e)),this.unsigned!==e.unsigned&&this.high>>>31===1&&e.high>>>31===1?!1:this.high===e.high&&this.low===e.low};I.eq=I.equals;I.notEquals=function(e){return!this.eq(e)};I.neq=I.notEquals;I.ne=I.notEquals;I.lessThan=function(e){return this.comp(e)<0};I.lt=I.lessThan;I.lessThanOrEqual=function(e){return this.comp(e)<=0};I.lte=I.lessThanOrEqual;I.le=I.lessThanOrEqual;I.greaterThan=function(e){return this.comp(e)>0};I.gt=I.greaterThan;I.greaterThanOrEqual=function(e){return this.comp(e)>=0};I.gte=I.greaterThanOrEqual;I.ge=I.greaterThanOrEqual;I.compare=function(e){if(ct(e)||(e=Ft(e)),this.eq(e))return 0;var o=this.isNegative(),t=e.isNegative();return o&&!t?-1:!o&&t?1:this.unsigned?e.high>>>0>this.high>>>0||e.high===this.high&&e.low>>>0>this.low>>>0?-1:1:this.sub(e).isNegative()?-1:1};I.comp=I.compare;I.negate=function(){return!this.unsigned&&this.eq(yt)?yt:this.not().add(Pe)};I.neg=I.negate;I.add=function(e){ct(e)||(e=Ft(e));var o=this.high>>>16,t=this.high&65535,r=this.low>>>16,n=this.low&65535,s=e.high>>>16,a=e.high&65535,u=e.low>>>16,l=e.low&65535,f=0,c=0,d=0,m=0;return m+=n+l,d+=m>>>16,m&=65535,d+=r+u,c+=d>>>16,d&=65535,c+=t+a,f+=c>>>16,c&=65535,f+=o+s,f&=65535,V(d<<16|m,f<<16|c,this.unsigned)};I.subtract=function(e){return ct(e)||(e=Ft(e)),this.add(e.neg())};I.sub=I.subtract;I.multiply=function(e){if(this.isZero())return this;if(ct(e)||(e=Ft(e)),_t){var o=_t.mul(this.low,this.high,e.low,e.high);return V(o,_t.get_high(),this.unsigned)}if(e.isZero())return this.unsigned?qt:Bt;if(this.eq(yt))return e.isOdd()?yt:Bt;if(e.eq(yt))return this.isOdd()?yt:Bt;if(this.isNegative())return e.isNegative()?this.neg().mul(e.neg()):this.neg().mul(e).neg();if(e.isNegative())return this.mul(e.neg()).neg();if(this.lt(ji)&&e.lt(ji))return St(this.toNumber()*e.toNumber(),this.unsigned);var t=this.high>>>16,r=this.high&65535,n=this.low>>>16,s=this.low&65535,a=e.high>>>16,u=e.high&65535,l=e.low>>>16,f=e.low&65535,c=0,d=0,m=0,y=0;return y+=s*f,m+=y>>>16,y&=65535,m+=n*f,d+=m>>>16,m&=65535,m+=s*l,d+=m>>>16,m&=65535,d+=r*f,c+=d>>>16,d&=65535,d+=n*l,c+=d>>>16,d&=65535,d+=s*u,c+=d>>>16,d&=65535,c+=t*f+r*l+n*u+s*a,c&=65535,V(m<<16|y,c<<16|d,this.unsigned)};I.mul=I.multiply;I.divide=function(e){if(ct(e)||(e=Ft(e)),e.isZero())throw Error("division by zero");if(_t){if(!this.unsigned&&this.high===-2147483648&&e.low===-1&&e.high===-1)return this;var o=(this.unsigned?_t.div_u:_t.div_s)(this.low,this.high,e.low,e.high);return V(o,_t.get_high(),this.unsigned)}if(this.isZero())return this.unsigned?qt:Bt;var t,r,n;if(this.unsigned){if(e.unsigned||(e=e.toUnsigned()),e.gt(this))return qt;if(e.gt(this.shru(1)))return Ki;n=qt}else{if(this.eq(yt)){if(e.eq(Pe)||e.eq(zn))return yt;if(e.eq(yt))return Pe;var s=this.shr(1);return t=s.div(e).shl(1),t.eq(Bt)?e.isNegative()?Pe:zn:(r=this.sub(e.mul(t)),n=t.add(r.div(e)),n)}else if(e.eq(yt))return this.unsigned?qt:Bt;if(this.isNegative())return e.isNegative()?this.neg().div(e.neg()):this.neg().div(e).neg();if(e.isNegative())return this.div(e.neg()).neg();n=Bt}for(r=this;r.gte(e);){t=Math.max(1,Math.floor(r.toNumber()/e.toNumber()));for(var a=Math.ceil(Math.log(t)/Math.LN2),u=a<=48?1:Lr(2,a-48),l=St(t),f=l.mul(e);f.isNegative()||f.gt(r);)t-=u,l=St(t,this.unsigned),f=l.mul(e);l.isZero()&&(l=Pe),n=n.add(l),r=r.sub(f)}return n};I.div=I.divide;I.modulo=function(e){if(ct(e)||(e=Ft(e)),_t){var o=(this.unsigned?_t.rem_u:_t.rem_s)(this.low,this.high,e.low,e.high);return V(o,_t.get_high(),this.unsigned)}return this.sub(this.div(e).mul(e))};I.mod=I.modulo;I.rem=I.modulo;I.not=function(){return V(~this.low,~this.high,this.unsigned)};I.countLeadingZeros=function(){return this.high?Math.clz32(this.high):Math.clz32(this.low)+32};I.clz=I.countLeadingZeros;I.countTrailingZeros=function(){return this.low?Ui(this.low):Ui(this.high)+32};I.ctz=I.countTrailingZeros;I.and=function(e){return ct(e)||(e=Ft(e)),V(this.low&e.low,this.high&e.high,this.unsigned)};I.or=function(e){return ct(e)||(e=Ft(e)),V(this.low|e.low,this.high|e.high,this.unsigned)};I.xor=function(e){return ct(e)||(e=Ft(e)),V(this.low^e.low,this.high^e.high,this.unsigned)};I.shiftLeft=function(e){return ct(e)&&(e=e.toInt()),(e&=63)===0?this:e<32?V(this.low<>>32-e,this.unsigned):V(0,this.low<>>e|this.high<<32-e,this.high>>e,this.unsigned):V(this.high>>e-32,this.high>=0?0:-1,this.unsigned)};I.shr=I.shiftRight;I.shiftRightUnsigned=function(e){return ct(e)&&(e=e.toInt()),(e&=63)===0?this:e<32?V(this.low>>>e|this.high<<32-e,this.high>>>e,this.unsigned):e===32?V(this.high,0,this.unsigned):V(this.high>>>e-32,0,this.unsigned)};I.shru=I.shiftRightUnsigned;I.shr_u=I.shiftRightUnsigned;I.rotateLeft=function(e){var o;return ct(e)&&(e=e.toInt()),(e&=63)===0?this:e===32?V(this.high,this.low,this.unsigned):e<32?(o=32-e,V(this.low<>>o,this.high<>>o,this.unsigned)):(e-=32,o=32-e,V(this.high<>>o,this.low<>>o,this.unsigned))};I.rotl=I.rotateLeft;I.rotateRight=function(e){var o;return ct(e)&&(e=e.toInt()),(e&=63)===0?this:e===32?V(this.high,this.low,this.unsigned):e<32?(o=32-e,V(this.high<>>e,this.low<>>e,this.unsigned)):(e-=32,o=32-e,V(this.low<>>e,this.high<>>e,this.unsigned))};I.rotr=I.rotateRight;I.toSigned=function(){return this.unsigned?V(this.low,this.high,!1):this};I.toUnsigned=function(){return this.unsigned?this:V(this.low,this.high,!0)};I.toBytes=function(e){return e?this.toBytesLE():this.toBytesBE()};I.toBytesLE=function(){var e=this.high,o=this.low;return[o&255,o>>>8&255,o>>>16&255,o>>>24,e&255,e>>>8&255,e>>>16&255,e>>>24]};I.toBytesBE=function(){var e=this.high,o=this.low;return[e>>>24,e>>>16&255,e>>>8&255,e&255,o>>>24,o>>>16&255,o>>>8&255,o&255]};H.fromBytes=function(e,o,t){return t?H.fromBytesLE(e,o):H.fromBytesBE(e,o)};H.fromBytesLE=function(e,o){return new H(e[0]|e[1]<<8|e[2]<<16|e[3]<<24,e[4]|e[5]<<8|e[6]<<16|e[7]<<24,o)};H.fromBytesBE=function(e,o){return new H(e[4]<<24|e[5]<<16|e[6]<<8|e[7],e[0]<<24|e[1]<<16|e[2]<<8|e[3],o)};jt=H});var b,$r=x(()=>{b={};b.Offset;b.Table;b.SIZEOF_SHORT=2;b.SIZEOF_INT=4;b.FILE_IDENTIFIER_LENGTH=4;b.SIZE_PREFIX_LENGTH=4;b.Encoding={UTF8_BYTES:1,UTF16_STRING:2};b.int32=new Int32Array(2);b.float32=new Float32Array(b.int32.buffer);b.float64=new Float64Array(b.int32.buffer);b.isLittleEndian=new Uint16Array(new Uint8Array([1,0]).buffer)[0]===1;b.Long=function(i,e){this.low=i|0,this.high=e|0};b.Long.create=function(i,e){return i==0&&e==0?b.Long.ZERO:new b.Long(i,e)};b.Long.prototype.toFloat64=function(){return(this.low>>>0)+this.high*4294967296};b.Long.prototype.equals=function(i){return this.low==i.low&&this.high==i.high};b.Long.ZERO=new b.Long(0,0);b.Builder=function(i){if(i)var e=i;else var e=1024;this.bb=b.ByteBuffer.allocate(e),this.space=e,this.minalign=1,this.vtable=null,this.vtable_in_use=0,this.isNested=!1,this.object_start=0,this.vtables=[],this.vector_num_elems=0,this.force_defaults=!1};b.Builder.prototype.clear=function(){this.bb.clear(),this.space=this.bb.capacity(),this.minalign=1,this.vtable=null,this.vtable_in_use=0,this.isNested=!1,this.object_start=0,this.vtables=[],this.vector_num_elems=0,this.force_defaults=!1};b.Builder.prototype.forceDefaults=function(i){this.force_defaults=i};b.Builder.prototype.dataBuffer=function(){return this.bb};b.Builder.prototype.asUint8Array=function(){return this.bb.bytes().subarray(this.bb.position(),this.bb.position()+this.offset())};b.Builder.prototype.prep=function(i,e){i>this.minalign&&(this.minalign=i);for(var o=~(this.bb.capacity()-this.space+e)+1&i-1;this.space=0&&this.vtable[e]==0;e--);for(var o=e+1;e>=0;e--)this.addInt16(this.vtable[e]!=0?i-this.vtable[e]:0);var t=2;this.addInt16(i-this.object_start);var r=(o+t)*b.SIZEOF_SHORT;this.addInt16(r);var n=0,s=this.space;t:for(e=0;e=0;n--)this.writeInt8(r.charCodeAt(n))}this.prep(this.minalign,b.SIZEOF_INT+t),this.addOffset(i),t&&this.addInt32(this.bb.capacity()-this.space),this.bb.setPosition(this.space)};b.Builder.prototype.finishSizePrefixed=function(i,e){this.finish(i,e,!0)};b.Builder.prototype.requiredField=function(i,e){var o=this.bb.capacity()-i,t=o-this.bb.readInt32(o),r=this.bb.readInt16(t+e)!=0;if(!r)throw new Error("FlatBuffers: field "+e+" must be set")};b.Builder.prototype.startVector=function(i,e,o){this.notNested(),this.vector_num_elems=e,this.prep(b.SIZEOF_INT,i*e),this.prep(o,i*e)};b.Builder.prototype.endVector=function(){return this.writeInt32(this.vector_num_elems),this.offset()};b.Builder.prototype.createString=function(i){if(i instanceof Uint8Array)var e=i;else for(var e=[],o=0;o=56320)t=r;else{var n=i.charCodeAt(o++);t=(r<<10)+n+(65536-56623104-56320)}t<128?e.push(t):(t<2048?e.push(t>>6&31|192):(t<65536?e.push(t>>12&15|224):e.push(t>>18&7|240,t>>12&63|128),e.push(t>>6&63|128)),e.push(t&63|128))}this.addInt8(0),this.startVector(1,e.length,1),this.bb.setPosition(this.space-=e.length);for(var o=0,s=this.space,a=this.bb.bytes();o>24};b.ByteBuffer.prototype.readUint8=function(i){return this.bytes_[i]};b.ByteBuffer.prototype.readInt16=function(i){return this.readUint16(i)<<16>>16};b.ByteBuffer.prototype.readUint16=function(i){return this.bytes_[i]|this.bytes_[i+1]<<8};b.ByteBuffer.prototype.readInt32=function(i){return this.bytes_[i]|this.bytes_[i+1]<<8|this.bytes_[i+2]<<16|this.bytes_[i+3]<<24};b.ByteBuffer.prototype.readUint32=function(i){return this.readInt32(i)>>>0};b.ByteBuffer.prototype.readInt64=function(i){return new b.Long(this.readInt32(i),this.readInt32(i+4))};b.ByteBuffer.prototype.readUint64=function(i){return new b.Long(this.readUint32(i),this.readUint32(i+4))};b.ByteBuffer.prototype.readFloat32=function(i){return b.int32[0]=this.readInt32(i),b.float32[0]};b.ByteBuffer.prototype.readFloat64=function(i){return b.int32[b.isLittleEndian?0:1]=this.readInt32(i),b.int32[b.isLittleEndian?1:0]=this.readInt32(i+4),b.float64[0]};b.ByteBuffer.prototype.writeInt8=function(i,e){this.bytes_[i]=e};b.ByteBuffer.prototype.writeUint8=function(i,e){this.bytes_[i]=e};b.ByteBuffer.prototype.writeInt16=function(i,e){this.bytes_[i]=e,this.bytes_[i+1]=e>>8};b.ByteBuffer.prototype.writeUint16=function(i,e){this.bytes_[i]=e,this.bytes_[i+1]=e>>8};b.ByteBuffer.prototype.writeInt32=function(i,e){this.bytes_[i]=e,this.bytes_[i+1]=e>>8,this.bytes_[i+2]=e>>16,this.bytes_[i+3]=e>>24};b.ByteBuffer.prototype.writeUint32=function(i,e){this.bytes_[i]=e,this.bytes_[i+1]=e>>8,this.bytes_[i+2]=e>>16,this.bytes_[i+3]=e>>24};b.ByteBuffer.prototype.writeInt64=function(i,e){this.writeInt32(i,e.low),this.writeInt32(i+4,e.high)};b.ByteBuffer.prototype.writeUint64=function(i,e){this.writeUint32(i,e.low),this.writeUint32(i+4,e.high)};b.ByteBuffer.prototype.writeFloat32=function(i,e){b.float32[0]=e,this.writeInt32(i,b.int32[0])};b.ByteBuffer.prototype.writeFloat64=function(i,e){b.float64[0]=e,this.writeInt32(i,b.int32[b.isLittleEndian?0:1]),this.writeInt32(i+4,b.int32[b.isLittleEndian?1:0])};b.ByteBuffer.prototype.getBufferIdentifier=function(){if(this.bytes_.length>10)+55296,(n&1024-1)+56320))}return t};b.ByteBuffer.prototype.__indirect=function(i){return i+this.readInt32(i)};b.ByteBuffer.prototype.__vector=function(i){return i+this.readInt32(i)+b.SIZEOF_INT};b.ByteBuffer.prototype.__vector_len=function(i){return this.readInt32(i+this.readInt32(i))};b.ByteBuffer.prototype.__has_identifier=function(i){if(i.length!=b.FILE_IDENTIFIER_LENGTH)throw new Error("FlatBuffers: file identifier must be length "+b.FILE_IDENTIFIER_LENGTH);for(var e=0;e{"use strict";$r();(e=>{let i;(t=>{let o;(n=>{let r;(v=>(v[v.UNDEFINED=0]="UNDEFINED",v[v.FLOAT=1]="FLOAT",v[v.INT=2]="INT",v[v.STRING=3]="STRING",v[v.TENSOR=4]="TENSOR",v[v.GRAPH=5]="GRAPH",v[v.FLOATS=6]="FLOATS",v[v.INTS=7]="INTS",v[v.STRINGS=8]="STRINGS",v[v.TENSORS=9]="TENSORS",v[v.GRAPHS=10]="GRAPHS",v[v.SPARSE_TENSOR=11]="SPARSE_TENSOR",v[v.SPARSE_TENSORS=12]="SPARSE_TENSORS"))(r=n.AttributeType||={})})(o=t.fbs||={})})(i=e.experimental||={})})(P||={});(e=>{let i;(t=>{let o;(n=>{let r;(l=>(l[l.UNKNOWN=0]="UNKNOWN",l[l.VALUE=1]="VALUE",l[l.PARAM=2]="PARAM"))(r=n.DimensionValueType||={})})(o=t.fbs||={})})(i=e.experimental||={})})(P||={});(e=>{let i;(t=>{let o;(n=>{let r;(k=>(k[k.UNDEFINED=0]="UNDEFINED",k[k.FLOAT=1]="FLOAT",k[k.UINT8=2]="UINT8",k[k.INT8=3]="INT8",k[k.UINT16=4]="UINT16",k[k.INT16=5]="INT16",k[k.INT32=6]="INT32",k[k.INT64=7]="INT64",k[k.STRING=8]="STRING",k[k.BOOL=9]="BOOL",k[k.FLOAT16=10]="FLOAT16",k[k.DOUBLE=11]="DOUBLE",k[k.UINT32=12]="UINT32",k[k.UINT64=13]="UINT64",k[k.COMPLEX64=14]="COMPLEX64",k[k.COMPLEX128=15]="COMPLEX128",k[k.BFLOAT16=16]="BFLOAT16",k[k.FLOAT8E4M3FN=17]="FLOAT8E4M3FN",k[k.FLOAT8E4M3FNUZ=18]="FLOAT8E4M3FNUZ",k[k.FLOAT8E5M2=19]="FLOAT8E5M2",k[k.FLOAT8E5M2FNUZ=20]="FLOAT8E5M2FNUZ"))(r=n.TensorDataType||={})})(o=t.fbs||={})})(i=e.experimental||={})})(P||={});(e=>{let i;(t=>{let o;(n=>{let r;(u=>(u[u.Primitive=0]="Primitive",u[u.Fused=1]="Fused"))(r=n.NodeType||={})})(o=t.fbs||={})})(i=e.experimental||={})})(P||={});(e=>{let i;(t=>{let o;(n=>{let r;(f=>(f[f.NONE=0]="NONE",f[f.tensor_type=1]="tensor_type",f[f.sequence_type=2]="sequence_type",f[f.map_type=3]="map_type"))(r=n.TypeInfoValue||={})})(o=t.fbs||={})})(i=e.experimental||={})})(P||={});(e=>{let i;(t=>{let o;(n=>{class r{constructor(){this.bb=null;this.bb_pos=0}__init(a,u){return this.bb_pos=a,this.bb=u,this}static getRootAsShape(a,u){return(u||new r).__init(a.readInt32(a.position())+a.position(),a)}static getSizePrefixedRootAsShape(a,u){return a.setPosition(a.position()+b.SIZE_PREFIX_LENGTH),(u||new r).__init(a.readInt32(a.position())+a.position(),a)}dim(a,u){let l=this.bb.__offset(this.bb_pos,4);return l?(u||new e.experimental.fbs.Dimension).__init(this.bb.__indirect(this.bb.__vector(this.bb_pos+l)+a*4),this.bb):null}dimLength(){let a=this.bb.__offset(this.bb_pos,4);return a?this.bb.__vector_len(this.bb_pos+a):0}static startShape(a){a.startObject(1)}static addDim(a,u){a.addFieldOffset(0,u,0)}static createDimVector(a,u){a.startVector(4,u.length,4);for(let l=u.length-1;l>=0;l--)a.addOffset(u[l]);return a.endVector()}static startDimVector(a,u){a.startVector(4,u,4)}static endShape(a){return a.endObject()}static createShape(a,u){return r.startShape(a),r.addDim(a,u),r.endShape(a)}}n.Shape=r})(o=t.fbs||={})})(i=e.experimental||={})})(P||={});(e=>{let i;(t=>{let o;(n=>{class r{constructor(){this.bb=null;this.bb_pos=0}__init(a,u){return this.bb_pos=a,this.bb=u,this}static getRootAsDimension(a,u){return(u||new r).__init(a.readInt32(a.position())+a.position(),a)}static getSizePrefixedRootAsDimension(a,u){return a.setPosition(a.position()+b.SIZE_PREFIX_LENGTH),(u||new r).__init(a.readInt32(a.position())+a.position(),a)}value(a){let u=this.bb.__offset(this.bb_pos,4);return u?(a||new e.experimental.fbs.DimensionValue).__init(this.bb.__indirect(this.bb_pos+u),this.bb):null}denotation(a){let u=this.bb.__offset(this.bb_pos,6);return u?this.bb.__string(this.bb_pos+u,a):null}static startDimension(a){a.startObject(2)}static addValue(a,u){a.addFieldOffset(0,u,0)}static addDenotation(a,u){a.addFieldOffset(1,u,0)}static endDimension(a){return a.endObject()}static createDimension(a,u,l){return r.startDimension(a),r.addValue(a,u),r.addDenotation(a,l),r.endDimension(a)}}n.Dimension=r})(o=t.fbs||={})})(i=e.experimental||={})})(P||={});(e=>{let i;(t=>{let o;(n=>{class r{constructor(){this.bb=null;this.bb_pos=0}__init(a,u){return this.bb_pos=a,this.bb=u,this}static getRootAsDimensionValue(a,u){return(u||new r).__init(a.readInt32(a.position())+a.position(),a)}static getSizePrefixedRootAsDimensionValue(a,u){return a.setPosition(a.position()+b.SIZE_PREFIX_LENGTH),(u||new r).__init(a.readInt32(a.position())+a.position(),a)}dimType(){let a=this.bb.__offset(this.bb_pos,4);return a?this.bb.readInt8(this.bb_pos+a):0}dimValue(){let a=this.bb.__offset(this.bb_pos,6);return a?this.bb.readInt64(this.bb_pos+a):this.bb.createLong(0,0)}dimParam(a){let u=this.bb.__offset(this.bb_pos,8);return u?this.bb.__string(this.bb_pos+u,a):null}static startDimensionValue(a){a.startObject(3)}static addDimType(a,u){a.addFieldInt8(0,u,0)}static addDimValue(a,u){a.addFieldInt64(1,u,a.createLong(0,0))}static addDimParam(a,u){a.addFieldOffset(2,u,0)}static endDimensionValue(a){return a.endObject()}static createDimensionValue(a,u,l,f){return r.startDimensionValue(a),r.addDimType(a,u),r.addDimValue(a,l),r.addDimParam(a,f),r.endDimensionValue(a)}}n.DimensionValue=r})(o=t.fbs||={})})(i=e.experimental||={})})(P||={});(e=>{let i;(t=>{let o;(n=>{class r{constructor(){this.bb=null;this.bb_pos=0}__init(a,u){return this.bb_pos=a,this.bb=u,this}static getRootAsTensorTypeAndShape(a,u){return(u||new r).__init(a.readInt32(a.position())+a.position(),a)}static getSizePrefixedRootAsTensorTypeAndShape(a,u){return a.setPosition(a.position()+b.SIZE_PREFIX_LENGTH),(u||new r).__init(a.readInt32(a.position())+a.position(),a)}elemType(){let a=this.bb.__offset(this.bb_pos,4);return a?this.bb.readInt32(this.bb_pos+a):0}shape(a){let u=this.bb.__offset(this.bb_pos,6);return u?(a||new e.experimental.fbs.Shape).__init(this.bb.__indirect(this.bb_pos+u),this.bb):null}static startTensorTypeAndShape(a){a.startObject(2)}static addElemType(a,u){a.addFieldInt32(0,u,0)}static addShape(a,u){a.addFieldOffset(1,u,0)}static endTensorTypeAndShape(a){return a.endObject()}static createTensorTypeAndShape(a,u,l){return r.startTensorTypeAndShape(a),r.addElemType(a,u),r.addShape(a,l),r.endTensorTypeAndShape(a)}}n.TensorTypeAndShape=r})(o=t.fbs||={})})(i=e.experimental||={})})(P||={});(e=>{let i;(t=>{let o;(n=>{class r{constructor(){this.bb=null;this.bb_pos=0}__init(a,u){return this.bb_pos=a,this.bb=u,this}static getRootAsMapType(a,u){return(u||new r).__init(a.readInt32(a.position())+a.position(),a)}static getSizePrefixedRootAsMapType(a,u){return a.setPosition(a.position()+b.SIZE_PREFIX_LENGTH),(u||new r).__init(a.readInt32(a.position())+a.position(),a)}keyType(){let a=this.bb.__offset(this.bb_pos,4);return a?this.bb.readInt32(this.bb_pos+a):0}valueType(a){let u=this.bb.__offset(this.bb_pos,6);return u?(a||new e.experimental.fbs.TypeInfo).__init(this.bb.__indirect(this.bb_pos+u),this.bb):null}static startMapType(a){a.startObject(2)}static addKeyType(a,u){a.addFieldInt32(0,u,0)}static addValueType(a,u){a.addFieldOffset(1,u,0)}static endMapType(a){return a.endObject()}static createMapType(a,u,l){return r.startMapType(a),r.addKeyType(a,u),r.addValueType(a,l),r.endMapType(a)}}n.MapType=r})(o=t.fbs||={})})(i=e.experimental||={})})(P||={});(e=>{let i;(t=>{let o;(n=>{class r{constructor(){this.bb=null;this.bb_pos=0}__init(a,u){return this.bb_pos=a,this.bb=u,this}static getRootAsSequenceType(a,u){return(u||new r).__init(a.readInt32(a.position())+a.position(),a)}static getSizePrefixedRootAsSequenceType(a,u){return a.setPosition(a.position()+b.SIZE_PREFIX_LENGTH),(u||new r).__init(a.readInt32(a.position())+a.position(),a)}elemType(a){let u=this.bb.__offset(this.bb_pos,4);return u?(a||new e.experimental.fbs.TypeInfo).__init(this.bb.__indirect(this.bb_pos+u),this.bb):null}static startSequenceType(a){a.startObject(1)}static addElemType(a,u){a.addFieldOffset(0,u,0)}static endSequenceType(a){return a.endObject()}static createSequenceType(a,u){return r.startSequenceType(a),r.addElemType(a,u),r.endSequenceType(a)}}n.SequenceType=r})(o=t.fbs||={})})(i=e.experimental||={})})(P||={});(e=>{let i;(t=>{let o;(n=>{class r{constructor(){this.bb=null;this.bb_pos=0}__init(a,u){return this.bb_pos=a,this.bb=u,this}nodeIndex(){return this.bb.readUint32(this.bb_pos)}srcArgIndex(){return this.bb.readInt32(this.bb_pos+4)}dstArgIndex(){return this.bb.readInt32(this.bb_pos+8)}static createEdgeEnd(a,u,l,f){return a.prep(4,12),a.writeInt32(f),a.writeInt32(l),a.writeInt32(u),a.offset()}}n.EdgeEnd=r})(o=t.fbs||={})})(i=e.experimental||={})})(P||={});(e=>{let i;(t=>{let o;(n=>{class r{constructor(){this.bb=null;this.bb_pos=0}__init(a,u){return this.bb_pos=a,this.bb=u,this}static getRootAsNodeEdge(a,u){return(u||new r).__init(a.readInt32(a.position())+a.position(),a)}static getSizePrefixedRootAsNodeEdge(a,u){return a.setPosition(a.position()+b.SIZE_PREFIX_LENGTH),(u||new r).__init(a.readInt32(a.position())+a.position(),a)}nodeIndex(){let a=this.bb.__offset(this.bb_pos,4);return a?this.bb.readUint32(this.bb_pos+a):0}inputEdges(a,u){let l=this.bb.__offset(this.bb_pos,6);return l?(u||new e.experimental.fbs.EdgeEnd).__init(this.bb.__vector(this.bb_pos+l)+a*12,this.bb):null}inputEdgesLength(){let a=this.bb.__offset(this.bb_pos,6);return a?this.bb.__vector_len(this.bb_pos+a):0}outputEdges(a,u){let l=this.bb.__offset(this.bb_pos,8);return l?(u||new e.experimental.fbs.EdgeEnd).__init(this.bb.__vector(this.bb_pos+l)+a*12,this.bb):null}outputEdgesLength(){let a=this.bb.__offset(this.bb_pos,8);return a?this.bb.__vector_len(this.bb_pos+a):0}static startNodeEdge(a){a.startObject(3)}static addNodeIndex(a,u){a.addFieldInt32(0,u,0)}static addInputEdges(a,u){a.addFieldOffset(1,u,0)}static startInputEdgesVector(a,u){a.startVector(12,u,4)}static addOutputEdges(a,u){a.addFieldOffset(2,u,0)}static startOutputEdgesVector(a,u){a.startVector(12,u,4)}static endNodeEdge(a){return a.endObject()}static createNodeEdge(a,u,l,f){return r.startNodeEdge(a),r.addNodeIndex(a,u),r.addInputEdges(a,l),r.addOutputEdges(a,f),r.endNodeEdge(a)}}n.NodeEdge=r})(o=t.fbs||={})})(i=e.experimental||={})})(P||={});(e=>{let i;(t=>{let o;(n=>{class r{constructor(){this.bb=null;this.bb_pos=0}__init(a,u){return this.bb_pos=a,this.bb=u,this}static getRootAsNode(a,u){return(u||new r).__init(a.readInt32(a.position())+a.position(),a)}static getSizePrefixedRootAsNode(a,u){return a.setPosition(a.position()+b.SIZE_PREFIX_LENGTH),(u||new r).__init(a.readInt32(a.position())+a.position(),a)}name(a){let u=this.bb.__offset(this.bb_pos,4);return u?this.bb.__string(this.bb_pos+u,a):null}docString(a){let u=this.bb.__offset(this.bb_pos,6);return u?this.bb.__string(this.bb_pos+u,a):null}domain(a){let u=this.bb.__offset(this.bb_pos,8);return u?this.bb.__string(this.bb_pos+u,a):null}sinceVersion(){let a=this.bb.__offset(this.bb_pos,10);return a?this.bb.readInt32(this.bb_pos+a):0}index(){let a=this.bb.__offset(this.bb_pos,12);return a?this.bb.readUint32(this.bb_pos+a):0}opType(a){let u=this.bb.__offset(this.bb_pos,14);return u?this.bb.__string(this.bb_pos+u,a):null}type(){let a=this.bb.__offset(this.bb_pos,16);return a?this.bb.readInt32(this.bb_pos+a):0}executionProviderType(a){let u=this.bb.__offset(this.bb_pos,18);return u?this.bb.__string(this.bb_pos+u,a):null}inputs(a,u){let l=this.bb.__offset(this.bb_pos,20);return l?this.bb.__string(this.bb.__vector(this.bb_pos+l)+a*4,u):null}inputsLength(){let a=this.bb.__offset(this.bb_pos,20);return a?this.bb.__vector_len(this.bb_pos+a):0}outputs(a,u){let l=this.bb.__offset(this.bb_pos,22);return l?this.bb.__string(this.bb.__vector(this.bb_pos+l)+a*4,u):null}outputsLength(){let a=this.bb.__offset(this.bb_pos,22);return a?this.bb.__vector_len(this.bb_pos+a):0}attributes(a,u){let l=this.bb.__offset(this.bb_pos,24);return l?(u||new e.experimental.fbs.Attribute).__init(this.bb.__indirect(this.bb.__vector(this.bb_pos+l)+a*4),this.bb):null}attributesLength(){let a=this.bb.__offset(this.bb_pos,24);return a?this.bb.__vector_len(this.bb_pos+a):0}inputArgCounts(a){let u=this.bb.__offset(this.bb_pos,26);return u?this.bb.readInt32(this.bb.__vector(this.bb_pos+u)+a*4):0}inputArgCountsLength(){let a=this.bb.__offset(this.bb_pos,26);return a?this.bb.__vector_len(this.bb_pos+a):0}inputArgCountsArray(){let a=this.bb.__offset(this.bb_pos,26);return a?new Int32Array(this.bb.bytes().buffer,this.bb.bytes().byteOffset+this.bb.__vector(this.bb_pos+a),this.bb.__vector_len(this.bb_pos+a)):null}implicitInputs(a,u){let l=this.bb.__offset(this.bb_pos,28);return l?this.bb.__string(this.bb.__vector(this.bb_pos+l)+a*4,u):null}implicitInputsLength(){let a=this.bb.__offset(this.bb_pos,28);return a?this.bb.__vector_len(this.bb_pos+a):0}static startNode(a){a.startObject(13)}static addName(a,u){a.addFieldOffset(0,u,0)}static addDocString(a,u){a.addFieldOffset(1,u,0)}static addDomain(a,u){a.addFieldOffset(2,u,0)}static addSinceVersion(a,u){a.addFieldInt32(3,u,0)}static addIndex(a,u){a.addFieldInt32(4,u,0)}static addOpType(a,u){a.addFieldOffset(5,u,0)}static addType(a,u){a.addFieldInt32(6,u,0)}static addExecutionProviderType(a,u){a.addFieldOffset(7,u,0)}static addInputs(a,u){a.addFieldOffset(8,u,0)}static createInputsVector(a,u){a.startVector(4,u.length,4);for(let l=u.length-1;l>=0;l--)a.addOffset(u[l]);return a.endVector()}static startInputsVector(a,u){a.startVector(4,u,4)}static addOutputs(a,u){a.addFieldOffset(9,u,0)}static createOutputsVector(a,u){a.startVector(4,u.length,4);for(let l=u.length-1;l>=0;l--)a.addOffset(u[l]);return a.endVector()}static startOutputsVector(a,u){a.startVector(4,u,4)}static addAttributes(a,u){a.addFieldOffset(10,u,0)}static createAttributesVector(a,u){a.startVector(4,u.length,4);for(let l=u.length-1;l>=0;l--)a.addOffset(u[l]);return a.endVector()}static startAttributesVector(a,u){a.startVector(4,u,4)}static addInputArgCounts(a,u){a.addFieldOffset(11,u,0)}static createInputArgCountsVector(a,u){a.startVector(4,u.length,4);for(let l=u.length-1;l>=0;l--)a.addInt32(u[l]);return a.endVector()}static startInputArgCountsVector(a,u){a.startVector(4,u,4)}static addImplicitInputs(a,u){a.addFieldOffset(12,u,0)}static createImplicitInputsVector(a,u){a.startVector(4,u.length,4);for(let l=u.length-1;l>=0;l--)a.addOffset(u[l]);return a.endVector()}static startImplicitInputsVector(a,u){a.startVector(4,u,4)}static endNode(a){return a.endObject()}static createNode(a,u,l,f,c,d,m,y,g,T,_,w,v,L){return r.startNode(a),r.addName(a,u),r.addDocString(a,l),r.addDomain(a,f),r.addSinceVersion(a,c),r.addIndex(a,d),r.addOpType(a,m),r.addType(a,y),r.addExecutionProviderType(a,g),r.addInputs(a,T),r.addOutputs(a,_),r.addAttributes(a,w),r.addInputArgCounts(a,v),r.addImplicitInputs(a,L),r.endNode(a)}}n.Node=r})(o=t.fbs||={})})(i=e.experimental||={})})(P||={});(e=>{let i;(t=>{let o;(n=>{class r{constructor(){this.bb=null;this.bb_pos=0}__init(a,u){return this.bb_pos=a,this.bb=u,this}static getRootAsValueInfo(a,u){return(u||new r).__init(a.readInt32(a.position())+a.position(),a)}static getSizePrefixedRootAsValueInfo(a,u){return a.setPosition(a.position()+b.SIZE_PREFIX_LENGTH),(u||new r).__init(a.readInt32(a.position())+a.position(),a)}name(a){let u=this.bb.__offset(this.bb_pos,4);return u?this.bb.__string(this.bb_pos+u,a):null}docString(a){let u=this.bb.__offset(this.bb_pos,6);return u?this.bb.__string(this.bb_pos+u,a):null}type(a){let u=this.bb.__offset(this.bb_pos,8);return u?(a||new e.experimental.fbs.TypeInfo).__init(this.bb.__indirect(this.bb_pos+u),this.bb):null}static startValueInfo(a){a.startObject(3)}static addName(a,u){a.addFieldOffset(0,u,0)}static addDocString(a,u){a.addFieldOffset(1,u,0)}static addType(a,u){a.addFieldOffset(2,u,0)}static endValueInfo(a){return a.endObject()}static createValueInfo(a,u,l,f){return r.startValueInfo(a),r.addName(a,u),r.addDocString(a,l),r.addType(a,f),r.endValueInfo(a)}}n.ValueInfo=r})(o=t.fbs||={})})(i=e.experimental||={})})(P||={});(e=>{let i;(t=>{let o;(n=>{class r{constructor(){this.bb=null;this.bb_pos=0}__init(a,u){return this.bb_pos=a,this.bb=u,this}static getRootAsTypeInfo(a,u){return(u||new r).__init(a.readInt32(a.position())+a.position(),a)}static getSizePrefixedRootAsTypeInfo(a,u){return a.setPosition(a.position()+b.SIZE_PREFIX_LENGTH),(u||new r).__init(a.readInt32(a.position())+a.position(),a)}denotation(a){let u=this.bb.__offset(this.bb_pos,4);return u?this.bb.__string(this.bb_pos+u,a):null}valueType(){let a=this.bb.__offset(this.bb_pos,6);return a?this.bb.readUint8(this.bb_pos+a):0}value(a){let u=this.bb.__offset(this.bb_pos,8);return u?this.bb.__union(a,this.bb_pos+u):null}static startTypeInfo(a){a.startObject(3)}static addDenotation(a,u){a.addFieldOffset(0,u,0)}static addValueType(a,u){a.addFieldInt8(1,u,0)}static addValue(a,u){a.addFieldOffset(2,u,0)}static endTypeInfo(a){return a.endObject()}static createTypeInfo(a,u,l,f){return r.startTypeInfo(a),r.addDenotation(a,u),r.addValueType(a,l),r.addValue(a,f),r.endTypeInfo(a)}}n.TypeInfo=r})(o=t.fbs||={})})(i=e.experimental||={})})(P||={});(e=>{let i;(t=>{let o;(n=>{class r{constructor(){this.bb=null;this.bb_pos=0}__init(a,u){return this.bb_pos=a,this.bb=u,this}static getRootAsOperatorSetId(a,u){return(u||new r).__init(a.readInt32(a.position())+a.position(),a)}static getSizePrefixedRootAsOperatorSetId(a,u){return a.setPosition(a.position()+b.SIZE_PREFIX_LENGTH),(u||new r).__init(a.readInt32(a.position())+a.position(),a)}domain(a){let u=this.bb.__offset(this.bb_pos,4);return u?this.bb.__string(this.bb_pos+u,a):null}version(){let a=this.bb.__offset(this.bb_pos,6);return a?this.bb.readInt64(this.bb_pos+a):this.bb.createLong(0,0)}static startOperatorSetId(a){a.startObject(2)}static addDomain(a,u){a.addFieldOffset(0,u,0)}static addVersion(a,u){a.addFieldInt64(1,u,a.createLong(0,0))}static endOperatorSetId(a){return a.endObject()}static createOperatorSetId(a,u,l){return r.startOperatorSetId(a),r.addDomain(a,u),r.addVersion(a,l),r.endOperatorSetId(a)}}n.OperatorSetId=r})(o=t.fbs||={})})(i=e.experimental||={})})(P||={});(e=>{let i;(t=>{let o;(n=>{class r{constructor(){this.bb=null;this.bb_pos=0}__init(a,u){return this.bb_pos=a,this.bb=u,this}static getRootAsTensor(a,u){return(u||new r).__init(a.readInt32(a.position())+a.position(),a)}static getSizePrefixedRootAsTensor(a,u){return a.setPosition(a.position()+b.SIZE_PREFIX_LENGTH),(u||new r).__init(a.readInt32(a.position())+a.position(),a)}name(a){let u=this.bb.__offset(this.bb_pos,4);return u?this.bb.__string(this.bb_pos+u,a):null}docString(a){let u=this.bb.__offset(this.bb_pos,6);return u?this.bb.__string(this.bb_pos+u,a):null}dims(a){let u=this.bb.__offset(this.bb_pos,8);return u?this.bb.readInt64(this.bb.__vector(this.bb_pos+u)+a*8):this.bb.createLong(0,0)}dimsLength(){let a=this.bb.__offset(this.bb_pos,8);return a?this.bb.__vector_len(this.bb_pos+a):0}dataType(){let a=this.bb.__offset(this.bb_pos,10);return a?this.bb.readInt32(this.bb_pos+a):0}rawData(a){let u=this.bb.__offset(this.bb_pos,12);return u?this.bb.readUint8(this.bb.__vector(this.bb_pos+u)+a):0}rawDataLength(){let a=this.bb.__offset(this.bb_pos,12);return a?this.bb.__vector_len(this.bb_pos+a):0}rawDataArray(){let a=this.bb.__offset(this.bb_pos,12);return a?new Uint8Array(this.bb.bytes().buffer,this.bb.bytes().byteOffset+this.bb.__vector(this.bb_pos+a),this.bb.__vector_len(this.bb_pos+a)):null}stringData(a,u){let l=this.bb.__offset(this.bb_pos,14);return l?this.bb.__string(this.bb.__vector(this.bb_pos+l)+a*4,u):null}stringDataLength(){let a=this.bb.__offset(this.bb_pos,14);return a?this.bb.__vector_len(this.bb_pos+a):0}static startTensor(a){a.startObject(6)}static addName(a,u){a.addFieldOffset(0,u,0)}static addDocString(a,u){a.addFieldOffset(1,u,0)}static addDims(a,u){a.addFieldOffset(2,u,0)}static createDimsVector(a,u){a.startVector(8,u.length,8);for(let l=u.length-1;l>=0;l--)a.addInt64(u[l]);return a.endVector()}static startDimsVector(a,u){a.startVector(8,u,8)}static addDataType(a,u){a.addFieldInt32(3,u,0)}static addRawData(a,u){a.addFieldOffset(4,u,0)}static createRawDataVector(a,u){a.startVector(1,u.length,1);for(let l=u.length-1;l>=0;l--)a.addInt8(u[l]);return a.endVector()}static startRawDataVector(a,u){a.startVector(1,u,1)}static addStringData(a,u){a.addFieldOffset(5,u,0)}static createStringDataVector(a,u){a.startVector(4,u.length,4);for(let l=u.length-1;l>=0;l--)a.addOffset(u[l]);return a.endVector()}static startStringDataVector(a,u){a.startVector(4,u,4)}static endTensor(a){return a.endObject()}static createTensor(a,u,l,f,c,d,m){return r.startTensor(a),r.addName(a,u),r.addDocString(a,l),r.addDims(a,f),r.addDataType(a,c),r.addRawData(a,d),r.addStringData(a,m),r.endTensor(a)}}n.Tensor=r})(o=t.fbs||={})})(i=e.experimental||={})})(P||={});(e=>{let i;(t=>{let o;(n=>{class r{constructor(){this.bb=null;this.bb_pos=0}__init(a,u){return this.bb_pos=a,this.bb=u,this}static getRootAsSparseTensor(a,u){return(u||new r).__init(a.readInt32(a.position())+a.position(),a)}static getSizePrefixedRootAsSparseTensor(a,u){return a.setPosition(a.position()+b.SIZE_PREFIX_LENGTH),(u||new r).__init(a.readInt32(a.position())+a.position(),a)}values(a){let u=this.bb.__offset(this.bb_pos,4);return u?(a||new e.experimental.fbs.Tensor).__init(this.bb.__indirect(this.bb_pos+u),this.bb):null}indices(a){let u=this.bb.__offset(this.bb_pos,6);return u?(a||new e.experimental.fbs.Tensor).__init(this.bb.__indirect(this.bb_pos+u),this.bb):null}dims(a){let u=this.bb.__offset(this.bb_pos,8);return u?this.bb.readInt64(this.bb.__vector(this.bb_pos+u)+a*8):this.bb.createLong(0,0)}dimsLength(){let a=this.bb.__offset(this.bb_pos,8);return a?this.bb.__vector_len(this.bb_pos+a):0}static startSparseTensor(a){a.startObject(3)}static addValues(a,u){a.addFieldOffset(0,u,0)}static addIndices(a,u){a.addFieldOffset(1,u,0)}static addDims(a,u){a.addFieldOffset(2,u,0)}static createDimsVector(a,u){a.startVector(8,u.length,8);for(let l=u.length-1;l>=0;l--)a.addInt64(u[l]);return a.endVector()}static startDimsVector(a,u){a.startVector(8,u,8)}static endSparseTensor(a){return a.endObject()}static createSparseTensor(a,u,l,f){return r.startSparseTensor(a),r.addValues(a,u),r.addIndices(a,l),r.addDims(a,f),r.endSparseTensor(a)}}n.SparseTensor=r})(o=t.fbs||={})})(i=e.experimental||={})})(P||={});(e=>{let i;(t=>{let o;(n=>{class r{constructor(){this.bb=null;this.bb_pos=0}__init(a,u){return this.bb_pos=a,this.bb=u,this}static getRootAsAttribute(a,u){return(u||new r).__init(a.readInt32(a.position())+a.position(),a)}static getSizePrefixedRootAsAttribute(a,u){return a.setPosition(a.position()+b.SIZE_PREFIX_LENGTH),(u||new r).__init(a.readInt32(a.position())+a.position(),a)}name(a){let u=this.bb.__offset(this.bb_pos,4);return u?this.bb.__string(this.bb_pos+u,a):null}docString(a){let u=this.bb.__offset(this.bb_pos,6);return u?this.bb.__string(this.bb_pos+u,a):null}type(){let a=this.bb.__offset(this.bb_pos,8);return a?this.bb.readInt32(this.bb_pos+a):0}f(){let a=this.bb.__offset(this.bb_pos,10);return a?this.bb.readFloat32(this.bb_pos+a):0}i(){let a=this.bb.__offset(this.bb_pos,12);return a?this.bb.readInt64(this.bb_pos+a):this.bb.createLong(0,0)}s(a){let u=this.bb.__offset(this.bb_pos,14);return u?this.bb.__string(this.bb_pos+u,a):null}t(a){let u=this.bb.__offset(this.bb_pos,16);return u?(a||new e.experimental.fbs.Tensor).__init(this.bb.__indirect(this.bb_pos+u),this.bb):null}g(a){let u=this.bb.__offset(this.bb_pos,18);return u?(a||new e.experimental.fbs.Graph).__init(this.bb.__indirect(this.bb_pos+u),this.bb):null}floats(a){let u=this.bb.__offset(this.bb_pos,20);return u?this.bb.readFloat32(this.bb.__vector(this.bb_pos+u)+a*4):0}floatsLength(){let a=this.bb.__offset(this.bb_pos,20);return a?this.bb.__vector_len(this.bb_pos+a):0}floatsArray(){let a=this.bb.__offset(this.bb_pos,20);return a?new Float32Array(this.bb.bytes().buffer,this.bb.bytes().byteOffset+this.bb.__vector(this.bb_pos+a),this.bb.__vector_len(this.bb_pos+a)):null}ints(a){let u=this.bb.__offset(this.bb_pos,22);return u?this.bb.readInt64(this.bb.__vector(this.bb_pos+u)+a*8):this.bb.createLong(0,0)}intsLength(){let a=this.bb.__offset(this.bb_pos,22);return a?this.bb.__vector_len(this.bb_pos+a):0}strings(a,u){let l=this.bb.__offset(this.bb_pos,24);return l?this.bb.__string(this.bb.__vector(this.bb_pos+l)+a*4,u):null}stringsLength(){let a=this.bb.__offset(this.bb_pos,24);return a?this.bb.__vector_len(this.bb_pos+a):0}tensors(a,u){let l=this.bb.__offset(this.bb_pos,26);return l?(u||new e.experimental.fbs.Tensor).__init(this.bb.__indirect(this.bb.__vector(this.bb_pos+l)+a*4),this.bb):null}tensorsLength(){let a=this.bb.__offset(this.bb_pos,26);return a?this.bb.__vector_len(this.bb_pos+a):0}graphs(a,u){let l=this.bb.__offset(this.bb_pos,28);return l?(u||new e.experimental.fbs.Graph).__init(this.bb.__indirect(this.bb.__vector(this.bb_pos+l)+a*4),this.bb):null}graphsLength(){let a=this.bb.__offset(this.bb_pos,28);return a?this.bb.__vector_len(this.bb_pos+a):0}static startAttribute(a){a.startObject(13)}static addName(a,u){a.addFieldOffset(0,u,0)}static addDocString(a,u){a.addFieldOffset(1,u,0)}static addType(a,u){a.addFieldInt32(2,u,0)}static addF(a,u){a.addFieldFloat32(3,u,0)}static addI(a,u){a.addFieldInt64(4,u,a.createLong(0,0))}static addS(a,u){a.addFieldOffset(5,u,0)}static addT(a,u){a.addFieldOffset(6,u,0)}static addG(a,u){a.addFieldOffset(7,u,0)}static addFloats(a,u){a.addFieldOffset(8,u,0)}static createFloatsVector(a,u){a.startVector(4,u.length,4);for(let l=u.length-1;l>=0;l--)a.addFloat32(u[l]);return a.endVector()}static startFloatsVector(a,u){a.startVector(4,u,4)}static addInts(a,u){a.addFieldOffset(9,u,0)}static createIntsVector(a,u){a.startVector(8,u.length,8);for(let l=u.length-1;l>=0;l--)a.addInt64(u[l]);return a.endVector()}static startIntsVector(a,u){a.startVector(8,u,8)}static addStrings(a,u){a.addFieldOffset(10,u,0)}static createStringsVector(a,u){a.startVector(4,u.length,4);for(let l=u.length-1;l>=0;l--)a.addOffset(u[l]);return a.endVector()}static startStringsVector(a,u){a.startVector(4,u,4)}static addTensors(a,u){a.addFieldOffset(11,u,0)}static createTensorsVector(a,u){a.startVector(4,u.length,4);for(let l=u.length-1;l>=0;l--)a.addOffset(u[l]);return a.endVector()}static startTensorsVector(a,u){a.startVector(4,u,4)}static addGraphs(a,u){a.addFieldOffset(12,u,0)}static createGraphsVector(a,u){a.startVector(4,u.length,4);for(let l=u.length-1;l>=0;l--)a.addOffset(u[l]);return a.endVector()}static startGraphsVector(a,u){a.startVector(4,u,4)}static endAttribute(a){return a.endObject()}static createAttribute(a,u,l,f,c,d,m,y,g,T,_,w,v,L){return r.startAttribute(a),r.addName(a,u),r.addDocString(a,l),r.addType(a,f),r.addF(a,c),r.addI(a,d),r.addS(a,m),r.addT(a,y),r.addG(a,g),r.addFloats(a,T),r.addInts(a,_),r.addStrings(a,w),r.addTensors(a,v),r.addGraphs(a,L),r.endAttribute(a)}}n.Attribute=r})(o=t.fbs||={})})(i=e.experimental||={})})(P||={});(e=>{let i;(t=>{let o;(n=>{class r{constructor(){this.bb=null;this.bb_pos=0}__init(a,u){return this.bb_pos=a,this.bb=u,this}static getRootAsGraph(a,u){return(u||new r).__init(a.readInt32(a.position())+a.position(),a)}static getSizePrefixedRootAsGraph(a,u){return a.setPosition(a.position()+b.SIZE_PREFIX_LENGTH),(u||new r).__init(a.readInt32(a.position())+a.position(),a)}initializers(a,u){let l=this.bb.__offset(this.bb_pos,4);return l?(u||new e.experimental.fbs.Tensor).__init(this.bb.__indirect(this.bb.__vector(this.bb_pos+l)+a*4),this.bb):null}initializersLength(){let a=this.bb.__offset(this.bb_pos,4);return a?this.bb.__vector_len(this.bb_pos+a):0}nodeArgs(a,u){let l=this.bb.__offset(this.bb_pos,6);return l?(u||new e.experimental.fbs.ValueInfo).__init(this.bb.__indirect(this.bb.__vector(this.bb_pos+l)+a*4),this.bb):null}nodeArgsLength(){let a=this.bb.__offset(this.bb_pos,6);return a?this.bb.__vector_len(this.bb_pos+a):0}nodes(a,u){let l=this.bb.__offset(this.bb_pos,8);return l?(u||new e.experimental.fbs.Node).__init(this.bb.__indirect(this.bb.__vector(this.bb_pos+l)+a*4),this.bb):null}nodesLength(){let a=this.bb.__offset(this.bb_pos,8);return a?this.bb.__vector_len(this.bb_pos+a):0}maxNodeIndex(){let a=this.bb.__offset(this.bb_pos,10);return a?this.bb.readUint32(this.bb_pos+a):0}nodeEdges(a,u){let l=this.bb.__offset(this.bb_pos,12);return l?(u||new e.experimental.fbs.NodeEdge).__init(this.bb.__indirect(this.bb.__vector(this.bb_pos+l)+a*4),this.bb):null}nodeEdgesLength(){let a=this.bb.__offset(this.bb_pos,12);return a?this.bb.__vector_len(this.bb_pos+a):0}inputs(a,u){let l=this.bb.__offset(this.bb_pos,14);return l?this.bb.__string(this.bb.__vector(this.bb_pos+l)+a*4,u):null}inputsLength(){let a=this.bb.__offset(this.bb_pos,14);return a?this.bb.__vector_len(this.bb_pos+a):0}outputs(a,u){let l=this.bb.__offset(this.bb_pos,16);return l?this.bb.__string(this.bb.__vector(this.bb_pos+l)+a*4,u):null}outputsLength(){let a=this.bb.__offset(this.bb_pos,16);return a?this.bb.__vector_len(this.bb_pos+a):0}sparseInitializers(a,u){let l=this.bb.__offset(this.bb_pos,18);return l?(u||new e.experimental.fbs.SparseTensor).__init(this.bb.__indirect(this.bb.__vector(this.bb_pos+l)+a*4),this.bb):null}sparseInitializersLength(){let a=this.bb.__offset(this.bb_pos,18);return a?this.bb.__vector_len(this.bb_pos+a):0}static startGraph(a){a.startObject(8)}static addInitializers(a,u){a.addFieldOffset(0,u,0)}static createInitializersVector(a,u){a.startVector(4,u.length,4);for(let l=u.length-1;l>=0;l--)a.addOffset(u[l]);return a.endVector()}static startInitializersVector(a,u){a.startVector(4,u,4)}static addNodeArgs(a,u){a.addFieldOffset(1,u,0)}static createNodeArgsVector(a,u){a.startVector(4,u.length,4);for(let l=u.length-1;l>=0;l--)a.addOffset(u[l]);return a.endVector()}static startNodeArgsVector(a,u){a.startVector(4,u,4)}static addNodes(a,u){a.addFieldOffset(2,u,0)}static createNodesVector(a,u){a.startVector(4,u.length,4);for(let l=u.length-1;l>=0;l--)a.addOffset(u[l]);return a.endVector()}static startNodesVector(a,u){a.startVector(4,u,4)}static addMaxNodeIndex(a,u){a.addFieldInt32(3,u,0)}static addNodeEdges(a,u){a.addFieldOffset(4,u,0)}static createNodeEdgesVector(a,u){a.startVector(4,u.length,4);for(let l=u.length-1;l>=0;l--)a.addOffset(u[l]);return a.endVector()}static startNodeEdgesVector(a,u){a.startVector(4,u,4)}static addInputs(a,u){a.addFieldOffset(5,u,0)}static createInputsVector(a,u){a.startVector(4,u.length,4);for(let l=u.length-1;l>=0;l--)a.addOffset(u[l]);return a.endVector()}static startInputsVector(a,u){a.startVector(4,u,4)}static addOutputs(a,u){a.addFieldOffset(6,u,0)}static createOutputsVector(a,u){a.startVector(4,u.length,4);for(let l=u.length-1;l>=0;l--)a.addOffset(u[l]);return a.endVector()}static startOutputsVector(a,u){a.startVector(4,u,4)}static addSparseInitializers(a,u){a.addFieldOffset(7,u,0)}static createSparseInitializersVector(a,u){a.startVector(4,u.length,4);for(let l=u.length-1;l>=0;l--)a.addOffset(u[l]);return a.endVector()}static startSparseInitializersVector(a,u){a.startVector(4,u,4)}static endGraph(a){return a.endObject()}static createGraph(a,u,l,f,c,d,m,y,g){return r.startGraph(a),r.addInitializers(a,u),r.addNodeArgs(a,l),r.addNodes(a,f),r.addMaxNodeIndex(a,c),r.addNodeEdges(a,d),r.addInputs(a,m),r.addOutputs(a,y),r.addSparseInitializers(a,g),r.endGraph(a)}}n.Graph=r})(o=t.fbs||={})})(i=e.experimental||={})})(P||={});(e=>{let i;(t=>{let o;(n=>{class r{constructor(){this.bb=null;this.bb_pos=0}__init(a,u){return this.bb_pos=a,this.bb=u,this}static getRootAsModel(a,u){return(u||new r).__init(a.readInt32(a.position())+a.position(),a)}static getSizePrefixedRootAsModel(a,u){return a.setPosition(a.position()+b.SIZE_PREFIX_LENGTH),(u||new r).__init(a.readInt32(a.position())+a.position(),a)}irVersion(){let a=this.bb.__offset(this.bb_pos,4);return a?this.bb.readInt64(this.bb_pos+a):this.bb.createLong(0,0)}opsetImport(a,u){let l=this.bb.__offset(this.bb_pos,6);return l?(u||new e.experimental.fbs.OperatorSetId).__init(this.bb.__indirect(this.bb.__vector(this.bb_pos+l)+a*4),this.bb):null}opsetImportLength(){let a=this.bb.__offset(this.bb_pos,6);return a?this.bb.__vector_len(this.bb_pos+a):0}producerName(a){let u=this.bb.__offset(this.bb_pos,8);return u?this.bb.__string(this.bb_pos+u,a):null}producerVersion(a){let u=this.bb.__offset(this.bb_pos,10);return u?this.bb.__string(this.bb_pos+u,a):null}domain(a){let u=this.bb.__offset(this.bb_pos,12);return u?this.bb.__string(this.bb_pos+u,a):null}modelVersion(){let a=this.bb.__offset(this.bb_pos,14);return a?this.bb.readInt64(this.bb_pos+a):this.bb.createLong(0,0)}docString(a){let u=this.bb.__offset(this.bb_pos,16);return u?this.bb.__string(this.bb_pos+u,a):null}graph(a){let u=this.bb.__offset(this.bb_pos,18);return u?(a||new e.experimental.fbs.Graph).__init(this.bb.__indirect(this.bb_pos+u),this.bb):null}graphDocString(a){let u=this.bb.__offset(this.bb_pos,20);return u?this.bb.__string(this.bb_pos+u,a):null}static startModel(a){a.startObject(9)}static addIrVersion(a,u){a.addFieldInt64(0,u,a.createLong(0,0))}static addOpsetImport(a,u){a.addFieldOffset(1,u,0)}static createOpsetImportVector(a,u){a.startVector(4,u.length,4);for(let l=u.length-1;l>=0;l--)a.addOffset(u[l]);return a.endVector()}static startOpsetImportVector(a,u){a.startVector(4,u,4)}static addProducerName(a,u){a.addFieldOffset(2,u,0)}static addProducerVersion(a,u){a.addFieldOffset(3,u,0)}static addDomain(a,u){a.addFieldOffset(4,u,0)}static addModelVersion(a,u){a.addFieldInt64(5,u,a.createLong(0,0))}static addDocString(a,u){a.addFieldOffset(6,u,0)}static addGraph(a,u){a.addFieldOffset(7,u,0)}static addGraphDocString(a,u){a.addFieldOffset(8,u,0)}static endModel(a){return a.endObject()}static createModel(a,u,l,f,c,d,m,y,g,T){return r.startModel(a),r.addIrVersion(a,u),r.addOpsetImport(a,l),r.addProducerName(a,f),r.addProducerVersion(a,c),r.addDomain(a,d),r.addModelVersion(a,m),r.addDocString(a,y),r.addGraph(a,g),r.addGraphDocString(a,T),r.endModel(a)}}n.Model=r})(o=t.fbs||={})})(i=e.experimental||={})})(P||={});(e=>{let i;(t=>{let o;(n=>{class r{constructor(){this.bb=null;this.bb_pos=0}__init(a,u){return this.bb_pos=a,this.bb=u,this}static getRootAsKernelCreateInfos(a,u){return(u||new r).__init(a.readInt32(a.position())+a.position(),a)}static getSizePrefixedRootAsKernelCreateInfos(a,u){return a.setPosition(a.position()+b.SIZE_PREFIX_LENGTH),(u||new r).__init(a.readInt32(a.position())+a.position(),a)}nodeIndices(a){let u=this.bb.__offset(this.bb_pos,4);return u?this.bb.readUint32(this.bb.__vector(this.bb_pos+u)+a*4):0}nodeIndicesLength(){let a=this.bb.__offset(this.bb_pos,4);return a?this.bb.__vector_len(this.bb_pos+a):0}nodeIndicesArray(){let a=this.bb.__offset(this.bb_pos,4);return a?new Uint32Array(this.bb.bytes().buffer,this.bb.bytes().byteOffset+this.bb.__vector(this.bb_pos+a),this.bb.__vector_len(this.bb_pos+a)):null}kernelDefHashes(a){let u=this.bb.__offset(this.bb_pos,6);return u?this.bb.readUint64(this.bb.__vector(this.bb_pos+u)+a*8):this.bb.createLong(0,0)}kernelDefHashesLength(){let a=this.bb.__offset(this.bb_pos,6);return a?this.bb.__vector_len(this.bb_pos+a):0}static startKernelCreateInfos(a){a.startObject(2)}static addNodeIndices(a,u){a.addFieldOffset(0,u,0)}static createNodeIndicesVector(a,u){a.startVector(4,u.length,4);for(let l=u.length-1;l>=0;l--)a.addInt32(u[l]);return a.endVector()}static startNodeIndicesVector(a,u){a.startVector(4,u,4)}static addKernelDefHashes(a,u){a.addFieldOffset(1,u,0)}static createKernelDefHashesVector(a,u){a.startVector(8,u.length,8);for(let l=u.length-1;l>=0;l--)a.addInt64(u[l]);return a.endVector()}static startKernelDefHashesVector(a,u){a.startVector(8,u,8)}static endKernelCreateInfos(a){return a.endObject()}static createKernelCreateInfos(a,u,l){return r.startKernelCreateInfos(a),r.addNodeIndices(a,u),r.addKernelDefHashes(a,l),r.endKernelCreateInfos(a)}}n.KernelCreateInfos=r})(o=t.fbs||={})})(i=e.experimental||={})})(P||={});(e=>{let i;(t=>{let o;(n=>{class r{constructor(){this.bb=null;this.bb_pos=0}__init(a,u){return this.bb_pos=a,this.bb=u,this}static getRootAsSubGraphSessionState(a,u){return(u||new r).__init(a.readInt32(a.position())+a.position(),a)}static getSizePrefixedRootAsSubGraphSessionState(a,u){return a.setPosition(a.position()+b.SIZE_PREFIX_LENGTH),(u||new r).__init(a.readInt32(a.position())+a.position(),a)}graphId(a){let u=this.bb.__offset(this.bb_pos,4);return u?this.bb.__string(this.bb_pos+u,a):null}sessionState(a){let u=this.bb.__offset(this.bb_pos,6);return u?(a||new e.experimental.fbs.SessionState).__init(this.bb.__indirect(this.bb_pos+u),this.bb):null}static startSubGraphSessionState(a){a.startObject(2)}static addGraphId(a,u){a.addFieldOffset(0,u,0)}static addSessionState(a,u){a.addFieldOffset(1,u,0)}static endSubGraphSessionState(a){let u=a.endObject();return a.requiredField(u,4),u}static createSubGraphSessionState(a,u,l){return r.startSubGraphSessionState(a),r.addGraphId(a,u),r.addSessionState(a,l),r.endSubGraphSessionState(a)}}n.SubGraphSessionState=r})(o=t.fbs||={})})(i=e.experimental||={})})(P||={});(e=>{let i;(t=>{let o;(n=>{class r{constructor(){this.bb=null;this.bb_pos=0}__init(a,u){return this.bb_pos=a,this.bb=u,this}static getRootAsSessionState(a,u){return(u||new r).__init(a.readInt32(a.position())+a.position(),a)}static getSizePrefixedRootAsSessionState(a,u){return a.setPosition(a.position()+b.SIZE_PREFIX_LENGTH),(u||new r).__init(a.readInt32(a.position())+a.position(),a)}kernels(a){let u=this.bb.__offset(this.bb_pos,4);return u?(a||new e.experimental.fbs.KernelCreateInfos).__init(this.bb.__indirect(this.bb_pos+u),this.bb):null}subGraphSessionStates(a,u){let l=this.bb.__offset(this.bb_pos,6);return l?(u||new e.experimental.fbs.SubGraphSessionState).__init(this.bb.__indirect(this.bb.__vector(this.bb_pos+l)+a*4),this.bb):null}subGraphSessionStatesLength(){let a=this.bb.__offset(this.bb_pos,6);return a?this.bb.__vector_len(this.bb_pos+a):0}static startSessionState(a){a.startObject(2)}static addKernels(a,u){a.addFieldOffset(0,u,0)}static addSubGraphSessionStates(a,u){a.addFieldOffset(1,u,0)}static createSubGraphSessionStatesVector(a,u){a.startVector(4,u.length,4);for(let l=u.length-1;l>=0;l--)a.addOffset(u[l]);return a.endVector()}static startSubGraphSessionStatesVector(a,u){a.startVector(4,u,4)}static endSessionState(a){return a.endObject()}static createSessionState(a,u,l){return r.startSessionState(a),r.addKernels(a,u),r.addSubGraphSessionStates(a,l),r.endSessionState(a)}}n.SessionState=r})(o=t.fbs||={})})(i=e.experimental||={})})(P||={});(e=>{let i;(t=>{let o;(n=>{class r{constructor(){this.bb=null;this.bb_pos=0}__init(a,u){return this.bb_pos=a,this.bb=u,this}static getRootAsInferenceSession(a,u){return(u||new r).__init(a.readInt32(a.position())+a.position(),a)}static getSizePrefixedRootAsInferenceSession(a,u){return a.setPosition(a.position()+b.SIZE_PREFIX_LENGTH),(u||new r).__init(a.readInt32(a.position())+a.position(),a)}static bufferHasIdentifier(a){return a.__has_identifier("ORTM")}ortVersion(a){let u=this.bb.__offset(this.bb_pos,4);return u?this.bb.__string(this.bb_pos+u,a):null}model(a){let u=this.bb.__offset(this.bb_pos,6);return u?(a||new e.experimental.fbs.Model).__init(this.bb.__indirect(this.bb_pos+u),this.bb):null}sessionState(a){let u=this.bb.__offset(this.bb_pos,8);return u?(a||new e.experimental.fbs.SessionState).__init(this.bb.__indirect(this.bb_pos+u),this.bb):null}static startInferenceSession(a){a.startObject(3)}static addOrtVersion(a,u){a.addFieldOffset(0,u,0)}static addModel(a,u){a.addFieldOffset(1,u,0)}static addSessionState(a,u){a.addFieldOffset(2,u,0)}static endInferenceSession(a){return a.endObject()}static finishInferenceSessionBuffer(a,u){a.finish(u,"ORTM")}static finishSizePrefixedInferenceSessionBuffer(a,u){a.finish(u,"ORTM",!0)}static createInferenceSession(a,u,l,f){return r.startInferenceSession(a),r.addOrtVersion(a,u),r.addModel(a,l),r.addSessionState(a,f),r.endInferenceSession(a)}}n.InferenceSession=r})(o=t.fbs||={})})(i=e.experimental||={})})(P||={})});var Qi=tt((xb,Yi)=>{"use strict";Yi.exports=Fc;function Fc(i,e){for(var o=new Array(arguments.length-1),t=0,r=2,n=!0;r{"use strict";var kr=ra;kr.length=function(e){var o=e.length;if(!o)return 0;for(var t=0;--o%4>1&&e.charAt(o)==="=";)++t;return Math.ceil(e.length*3)/4-t};var De=new Array(64),ea=new Array(123);for(Nt=0;Nt<64;)ea[De[Nt]=Nt<26?Nt+65:Nt<52?Nt+71:Nt<62?Nt-4:Nt-59|43]=Nt++;var Nt;kr.encode=function(e,o,t){for(var r=null,n=[],s=0,a=0,u;o>2],u=(l&3)<<4,a=1;break;case 1:n[s++]=De[u|l>>4],u=(l&15)<<2,a=2;break;case 2:n[s++]=De[u|l>>6],n[s++]=De[l&63],a=0;break}s>8191&&((r||(r=[])).push(String.fromCharCode.apply(String,n)),s=0)}return a&&(n[s++]=De[u],n[s++]=61,a===1&&(n[s++]=61)),r?(s&&r.push(String.fromCharCode.apply(String,n.slice(0,s))),r.join("")):String.fromCharCode.apply(String,n.slice(0,s))};var ta="invalid encoding";kr.decode=function(e,o,t){for(var r=t,n=0,s,a=0;a1)break;if((u=ea[u])===void 0)throw Error(ta);switch(n){case 0:s=u,n=1;break;case 1:o[t++]=s<<2|(u&48)>>4,s=u,n=2;break;case 2:o[t++]=(s&15)<<4|(u&60)>>2,s=u,n=3;break;case 3:o[t++]=(s&3)<<6|u,n=0;break}}if(n===1)throw Error(ta);return t-r};kr.test=function(e){return/^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/.test(e)}});var ia=tt((wb,oa)=>{"use strict";oa.exports=Br;function Br(){this._listeners={}}Br.prototype.on=function(e,o,t){return(this._listeners[e]||(this._listeners[e]=[])).push({fn:o,ctx:t||this}),this};Br.prototype.off=function(e,o){if(e===void 0)this._listeners={};else if(o===void 0)this._listeners[e]=[];else for(var t=this._listeners[e],r=0;r{"use strict";ca.exports=aa(aa);function aa(i){return typeof Float32Array<"u"?function(){var e=new Float32Array([-0]),o=new Uint8Array(e.buffer),t=o[3]===128;function r(u,l,f){e[0]=u,l[f]=o[0],l[f+1]=o[1],l[f+2]=o[2],l[f+3]=o[3]}function n(u,l,f){e[0]=u,l[f]=o[3],l[f+1]=o[2],l[f+2]=o[1],l[f+3]=o[0]}i.writeFloatLE=t?r:n,i.writeFloatBE=t?n:r;function s(u,l){return o[0]=u[l],o[1]=u[l+1],o[2]=u[l+2],o[3]=u[l+3],e[0]}function a(u,l){return o[3]=u[l],o[2]=u[l+1],o[1]=u[l+2],o[0]=u[l+3],e[0]}i.readFloatLE=t?s:a,i.readFloatBE=t?a:s}():function(){function e(t,r,n,s){var a=r<0?1:0;if(a&&(r=-r),r===0)t(1/r>0?0:2147483648,n,s);else if(isNaN(r))t(2143289344,n,s);else if(r>34028234663852886e22)t((a<<31|2139095040)>>>0,n,s);else if(r<11754943508222875e-54)t((a<<31|Math.round(r/1401298464324817e-60))>>>0,n,s);else{var u=Math.floor(Math.log(r)/Math.LN2),l=Math.round(r*Math.pow(2,-u)*8388608)&8388607;t((a<<31|u+127<<23|l)>>>0,n,s)}}i.writeFloatLE=e.bind(null,sa),i.writeFloatBE=e.bind(null,ua);function o(t,r,n){var s=t(r,n),a=(s>>31)*2+1,u=s>>>23&255,l=s&8388607;return u===255?l?NaN:a*(1/0):u===0?a*1401298464324817e-60*l:a*Math.pow(2,u-150)*(l+8388608)}i.readFloatLE=o.bind(null,la),i.readFloatBE=o.bind(null,fa)}(),typeof Float64Array<"u"?function(){var e=new Float64Array([-0]),o=new Uint8Array(e.buffer),t=o[7]===128;function r(u,l,f){e[0]=u,l[f]=o[0],l[f+1]=o[1],l[f+2]=o[2],l[f+3]=o[3],l[f+4]=o[4],l[f+5]=o[5],l[f+6]=o[6],l[f+7]=o[7]}function n(u,l,f){e[0]=u,l[f]=o[7],l[f+1]=o[6],l[f+2]=o[5],l[f+3]=o[4],l[f+4]=o[3],l[f+5]=o[2],l[f+6]=o[1],l[f+7]=o[0]}i.writeDoubleLE=t?r:n,i.writeDoubleBE=t?n:r;function s(u,l){return o[0]=u[l],o[1]=u[l+1],o[2]=u[l+2],o[3]=u[l+3],o[4]=u[l+4],o[5]=u[l+5],o[6]=u[l+6],o[7]=u[l+7],e[0]}function a(u,l){return o[7]=u[l],o[6]=u[l+1],o[5]=u[l+2],o[4]=u[l+3],o[3]=u[l+4],o[2]=u[l+5],o[1]=u[l+6],o[0]=u[l+7],e[0]}i.readDoubleLE=t?s:a,i.readDoubleBE=t?a:s}():function(){function e(t,r,n,s,a,u){var l=s<0?1:0;if(l&&(s=-s),s===0)t(0,a,u+r),t(1/s>0?0:2147483648,a,u+n);else if(isNaN(s))t(0,a,u+r),t(2146959360,a,u+n);else if(s>17976931348623157e292)t(0,a,u+r),t((l<<31|2146435072)>>>0,a,u+n);else{var f;if(s<22250738585072014e-324)f=s/5e-324,t(f>>>0,a,u+r),t((l<<31|f/4294967296)>>>0,a,u+n);else{var c=Math.floor(Math.log(s)/Math.LN2);c===1024&&(c=1023),f=s*Math.pow(2,-c),t(f*4503599627370496>>>0,a,u+r),t((l<<31|c+1023<<20|f*1048576&1048575)>>>0,a,u+n)}}}i.writeDoubleLE=e.bind(null,sa,0,4),i.writeDoubleBE=e.bind(null,ua,4,0);function o(t,r,n,s,a){var u=t(s,a+r),l=t(s,a+n),f=(l>>31)*2+1,c=l>>>20&2047,d=4294967296*(l&1048575)+u;return c===2047?d?NaN:f*(1/0):c===0?f*5e-324*d:f*Math.pow(2,c-1075)*(d+4503599627370496)}i.readDoubleLE=o.bind(null,la,0,4),i.readDoubleBE=o.bind(null,fa,4,0)}(),i}function sa(i,e,o){e[o]=i&255,e[o+1]=i>>>8&255,e[o+2]=i>>>16&255,e[o+3]=i>>>24}function ua(i,e,o){e[o]=i>>>24,e[o+1]=i>>>16&255,e[o+2]=i>>>8&255,e[o+3]=i&255}function la(i,e){return(i[e]|i[e+1]<<8|i[e+2]<<16|i[e+3]<<24)>>>0}function fa(i,e){return(i[e]<<24|i[e+1]<<16|i[e+2]<<8|i[e+3])>>>0}});var da=tt((exports,module)=>{"use strict";module.exports=inquire;function inquire(moduleName){try{var mod=eval("quire".replace(/^/,"re"))(moduleName);if(mod&&(mod.length||Object.keys(mod).length))return mod}catch(i){}return null}});var ma=tt(ha=>{"use strict";var qn=ha;qn.length=function(e){for(var o=0,t=0,r=0;r191&&u<224?s[a++]=(u&31)<<6|e[o++]&63:u>239&&u<365?(u=((u&7)<<18|(e[o++]&63)<<12|(e[o++]&63)<<6|e[o++]&63)-65536,s[a++]=55296+(u>>10),s[a++]=56320+(u&1023)):s[a++]=(u&15)<<12|(e[o++]&63)<<6|e[o++]&63,a>8191&&((n||(n=[])).push(String.fromCharCode.apply(String,s)),a=0);return n?(a&&n.push(String.fromCharCode.apply(String,s.slice(0,a))),n.join("")):String.fromCharCode.apply(String,s.slice(0,a))};qn.write=function(e,o,t){for(var r=t,n,s,a=0;a>6|192,o[t++]=n&63|128):(n&64512)===55296&&((s=e.charCodeAt(a+1))&64512)===56320?(n=65536+((n&1023)<<10)+(s&1023),++a,o[t++]=n>>18|240,o[t++]=n>>12&63|128,o[t++]=n>>6&63|128,o[t++]=n&63|128):(o[t++]=n>>12|224,o[t++]=n>>6&63|128,o[t++]=n&63|128);return t-r}});var ya=tt((_b,ba)=>{"use strict";ba.exports=Nc;function Nc(i,e,o){var t=o||8192,r=t>>>1,n=null,s=t;return function(u){if(u<1||u>r)return i(u);s+u>t&&(n=i(t),s=0);var l=e.call(n,s,s+=u);return s&7&&(s=(s|7)+1),l}}});var xa=tt((Sb,ga)=>{"use strict";ga.exports=it;var Xe=te();function it(i,e){this.lo=i>>>0,this.hi=e>>>0}var ce=it.zero=new it(0,0);ce.toNumber=function(){return 0};ce.zzEncode=ce.zzDecode=function(){return this};ce.length=function(){return 1};var Cc=it.zeroHash="\0\0\0\0\0\0\0\0";it.fromNumber=function(e){if(e===0)return ce;var o=e<0;o&&(e=-e);var t=e>>>0,r=(e-t)/4294967296>>>0;return o&&(r=~r>>>0,t=~t>>>0,++t>4294967295&&(t=0,++r>4294967295&&(r=0))),new it(t,r)};it.from=function(e){if(typeof e=="number")return it.fromNumber(e);if(Xe.isString(e))if(Xe.Long)e=Xe.Long.fromString(e);else return it.fromNumber(parseInt(e,10));return e.low||e.high?new it(e.low>>>0,e.high>>>0):ce};it.prototype.toNumber=function(e){if(!e&&this.hi>>>31){var o=~this.lo+1>>>0,t=~this.hi>>>0;return o||(t=t+1>>>0),-(o+t*4294967296)}return this.lo+this.hi*4294967296};it.prototype.toLong=function(e){return Xe.Long?new Xe.Long(this.lo|0,this.hi|0,!!e):{low:this.lo|0,high:this.hi|0,unsigned:!!e}};var Qt=String.prototype.charCodeAt;it.fromHash=function(e){return e===Cc?ce:new it((Qt.call(e,0)|Qt.call(e,1)<<8|Qt.call(e,2)<<16|Qt.call(e,3)<<24)>>>0,(Qt.call(e,4)|Qt.call(e,5)<<8|Qt.call(e,6)<<16|Qt.call(e,7)<<24)>>>0)};it.prototype.toHash=function(){return String.fromCharCode(this.lo&255,this.lo>>>8&255,this.lo>>>16&255,this.lo>>>24,this.hi&255,this.hi>>>8&255,this.hi>>>16&255,this.hi>>>24)};it.prototype.zzEncode=function(){var e=this.hi>>31;return this.hi=((this.hi<<1|this.lo>>>31)^e)>>>0,this.lo=(this.lo<<1^e)>>>0,this};it.prototype.zzDecode=function(){var e=-(this.lo&1);return this.lo=((this.lo>>>1|this.hi<<31)^e)>>>0,this.hi=(this.hi>>>1^e)>>>0,this};it.prototype.length=function(){var e=this.lo,o=(this.lo>>>28|this.hi<<4)>>>0,t=this.hi>>>24;return t===0?o===0?e<16384?e<128?1:2:e<2097152?3:4:o<16384?o<128?5:6:o<2097152?7:8:t<128?9:10}});var te=tt(jn=>{"use strict";var E=jn;E.asPromise=Qi();E.base64=na();E.EventEmitter=ia();E.float=pa();E.inquire=da();E.utf8=ma();E.pool=ya();E.LongBits=xa();E.isNode=!!(typeof global<"u"&&global&&global.process&&global.process.versions&&global.process.versions.node);E.global=E.isNode&&global||typeof window<"u"&&window||typeof self<"u"&&self||jn;E.emptyArray=Object.freeze?Object.freeze([]):[];E.emptyObject=Object.freeze?Object.freeze({}):{};E.isInteger=Number.isInteger||function(e){return typeof e=="number"&&isFinite(e)&&Math.floor(e)===e};E.isString=function(e){return typeof e=="string"||e instanceof String};E.isObject=function(e){return e&&typeof e=="object"};E.isset=E.isSet=function(e,o){var t=e[o];return t!=null&&e.hasOwnProperty(o)?typeof t!="object"||(Array.isArray(t)?t.length:Object.keys(t).length)>0:!1};E.Buffer=function(){try{var i=E.inquire("buffer").Buffer;return i.prototype.utf8Write?i:null}catch{return null}}();E._Buffer_from=null;E._Buffer_allocUnsafe=null;E.newBuffer=function(e){return typeof e=="number"?E.Buffer?E._Buffer_allocUnsafe(e):new E.Array(e):E.Buffer?E._Buffer_from(e):typeof Uint8Array>"u"?e:new Uint8Array(e)};E.Array=typeof Uint8Array<"u"?Uint8Array:Array;E.Long=E.global.dcodeIO&&E.global.dcodeIO.Long||E.global.Long||E.inquire("long");E.key2Re=/^true|false|0|1$/;E.key32Re=/^-?(?:0|[1-9][0-9]*)$/;E.key64Re=/^(?:[\\x00-\\xff]{8}|-?(?:0|[1-9][0-9]*))$/;E.longToHash=function(e){return e?E.LongBits.from(e).toHash():E.LongBits.zeroHash};E.longFromHash=function(e,o){var t=E.LongBits.fromHash(e);return E.Long?E.Long.fromBits(t.lo,t.hi,o):t.toNumber(!!o)};function Ta(i,e,o){for(var t=Object.keys(e),r=0;r-1;--n)if(o[r[n]]===1&&this[r[n]]!==void 0&&this[r[n]]!==null)return r[n]}};E.oneOfSetter=function(e){return function(o){for(var t=0;t{"use strict";Sa.exports=G;var Ot=te(),Xn,Fr=Ot.LongBits,va=Ot.base64,Ia=Ot.utf8;function Ke(i,e,o){this.fn=i,this.len=e,this.next=void 0,this.val=o}function Jn(){}function Rc(i){this.head=i.head,this.tail=i.tail,this.len=i.len,this.next=i.states}function G(){this.len=0,this.head=new Ke(Jn,0,0),this.tail=this.head,this.states=null}var _a=function(){return Ot.Buffer?function(){return(G.create=function(){return new Xn})()}:function(){return new G}};G.create=_a();G.alloc=function(e){return new Ot.Array(e)};Ot.Array!==Array&&(G.alloc=Ot.pool(G.alloc,Ot.Array.prototype.subarray));G.prototype._push=function(e,o,t){return this.tail=this.tail.next=new Ke(e,o,t),this.len+=o,this};function Zn(i,e,o){e[o]=i&255}function Gc(i,e,o){for(;i>127;)e[o++]=i&127|128,i>>>=7;e[o]=i}function Yn(i,e){this.len=i,this.next=void 0,this.val=e}Yn.prototype=Object.create(Ke.prototype);Yn.prototype.fn=Gc;G.prototype.uint32=function(e){return this.len+=(this.tail=this.tail.next=new Yn((e=e>>>0)<128?1:e<16384?2:e<2097152?3:e<268435456?4:5,e)).len,this};G.prototype.int32=function(e){return e<0?this._push(Qn,10,Fr.fromNumber(e)):this.uint32(e)};G.prototype.sint32=function(e){return this.uint32((e<<1^e>>31)>>>0)};function Qn(i,e,o){for(;i.hi;)e[o++]=i.lo&127|128,i.lo=(i.lo>>>7|i.hi<<25)>>>0,i.hi>>>=7;for(;i.lo>127;)e[o++]=i.lo&127|128,i.lo=i.lo>>>7;e[o++]=i.lo}G.prototype.uint64=function(e){var o=Fr.from(e);return this._push(Qn,o.length(),o)};G.prototype.int64=G.prototype.uint64;G.prototype.sint64=function(e){var o=Fr.from(e).zzEncode();return this._push(Qn,o.length(),o)};G.prototype.bool=function(e){return this._push(Zn,1,e?1:0)};function Kn(i,e,o){e[o]=i&255,e[o+1]=i>>>8&255,e[o+2]=i>>>16&255,e[o+3]=i>>>24}G.prototype.fixed32=function(e){return this._push(Kn,4,e>>>0)};G.prototype.sfixed32=G.prototype.fixed32;G.prototype.fixed64=function(e){var o=Fr.from(e);return this._push(Kn,4,o.lo)._push(Kn,4,o.hi)};G.prototype.sfixed64=G.prototype.fixed64;G.prototype.float=function(e){return this._push(Ot.float.writeFloatLE,4,e)};G.prototype.double=function(e){return this._push(Ot.float.writeDoubleLE,8,e)};var Mc=Ot.Array.prototype.set?function(e,o,t){o.set(e,t)}:function(e,o,t){for(var r=0;r>>0;if(!o)return this._push(Zn,1,0);if(Ot.isString(e)){var t=G.alloc(o=va.length(e));va.decode(e,t,0),e=t}return this.uint32(o)._push(Mc,o,e)};G.prototype.string=function(e){var o=Ia.length(e);return o?this.uint32(o)._push(Ia.write,o,e):this._push(Zn,1,0)};G.prototype.fork=function(){return this.states=new Rc(this),this.head=this.tail=new Ke(Jn,0,0),this.len=0,this};G.prototype.reset=function(){return this.states?(this.head=this.states.head,this.tail=this.states.tail,this.len=this.states.len,this.states=this.states.next):(this.head=this.tail=new Ke(Jn,0,0),this.len=0),this};G.prototype.ldelim=function(){var e=this.head,o=this.tail,t=this.len;return this.reset().uint32(t),t&&(this.tail.next=e.next,this.tail=o,this.len+=t),this};G.prototype.finish=function(){for(var e=this.head.next,o=this.constructor.alloc(this.len),t=0;e;)e.fn(e.val,o,t),t+=e.len,e=e.next;return o};G._configure=function(i){Xn=i,G.create=_a(),Xn._configure()}});var Pa=tt((Pb,Aa)=>{"use strict";Aa.exports=Mt;var Oa=to();(Mt.prototype=Object.create(Oa.prototype)).constructor=Mt;var ee=te();function Mt(){Oa.call(this)}Mt._configure=function(){Mt.alloc=ee._Buffer_allocUnsafe,Mt.writeBytesBuffer=ee.Buffer&&ee.Buffer.prototype instanceof Uint8Array&&ee.Buffer.prototype.set.name==="set"?function(e,o,t){o.set(e,t)}:function(e,o,t){if(e.copy)e.copy(o,t,0,e.length);else for(var r=0;r>>0;return this.uint32(o),o&&this._push(Mt.writeBytesBuffer,o,e),this};function Vc(i,e,o){i.length<40?ee.utf8.write(i,e,o):e.utf8Write?e.utf8Write(i,o):e.write(i,o)}Mt.prototype.string=function(e){var o=ee.Buffer.byteLength(e);return this.uint32(o),o&&this._push(Vc,o,e),this};Mt._configure()});var no=tt((Eb,ka)=>{"use strict";ka.exports=K;var Ct=te(),ro,La=Ct.LongBits,Uc=Ct.utf8;function Rt(i,e){return RangeError("index out of range: "+i.pos+" + "+(e||1)+" > "+i.len)}function K(i){this.buf=i,this.pos=0,this.len=i.length}var Ea=typeof Uint8Array<"u"?function(e){if(e instanceof Uint8Array||Array.isArray(e))return new K(e);throw Error("illegal buffer")}:function(e){if(Array.isArray(e))return new K(e);throw Error("illegal buffer")},$a=function(){return Ct.Buffer?function(o){return(K.create=function(r){return Ct.Buffer.isBuffer(r)?new ro(r):Ea(r)})(o)}:Ea};K.create=$a();K.prototype._slice=Ct.Array.prototype.subarray||Ct.Array.prototype.slice;K.prototype.uint32=function(){var e=4294967295;return function(){if(e=(this.buf[this.pos]&127)>>>0,this.buf[this.pos++]<128||(e=(e|(this.buf[this.pos]&127)<<7)>>>0,this.buf[this.pos++]<128)||(e=(e|(this.buf[this.pos]&127)<<14)>>>0,this.buf[this.pos++]<128)||(e=(e|(this.buf[this.pos]&127)<<21)>>>0,this.buf[this.pos++]<128)||(e=(e|(this.buf[this.pos]&15)<<28)>>>0,this.buf[this.pos++]<128))return e;if((this.pos+=5)>this.len)throw this.pos=this.len,Rt(this,10);return e}}();K.prototype.int32=function(){return this.uint32()|0};K.prototype.sint32=function(){var e=this.uint32();return e>>>1^-(e&1)|0};function eo(){var i=new La(0,0),e=0;if(this.len-this.pos>4){for(;e<4;++e)if(i.lo=(i.lo|(this.buf[this.pos]&127)<>>0,this.buf[this.pos++]<128)return i;if(i.lo=(i.lo|(this.buf[this.pos]&127)<<28)>>>0,i.hi=(i.hi|(this.buf[this.pos]&127)>>4)>>>0,this.buf[this.pos++]<128)return i;e=0}else{for(;e<3;++e){if(this.pos>=this.len)throw Rt(this);if(i.lo=(i.lo|(this.buf[this.pos]&127)<>>0,this.buf[this.pos++]<128)return i}return i.lo=(i.lo|(this.buf[this.pos++]&127)<>>0,i}if(this.len-this.pos>4){for(;e<5;++e)if(i.hi=(i.hi|(this.buf[this.pos]&127)<>>0,this.buf[this.pos++]<128)return i}else for(;e<5;++e){if(this.pos>=this.len)throw Rt(this);if(i.hi=(i.hi|(this.buf[this.pos]&127)<>>0,this.buf[this.pos++]<128)return i}throw Error("invalid varint encoding")}K.prototype.bool=function(){return this.uint32()!==0};function Nr(i,e){return(i[e-4]|i[e-3]<<8|i[e-2]<<16|i[e-1]<<24)>>>0}K.prototype.fixed32=function(){if(this.pos+4>this.len)throw Rt(this,4);return Nr(this.buf,this.pos+=4)};K.prototype.sfixed32=function(){if(this.pos+4>this.len)throw Rt(this,4);return Nr(this.buf,this.pos+=4)|0};function Da(){if(this.pos+8>this.len)throw Rt(this,8);return new La(Nr(this.buf,this.pos+=4),Nr(this.buf,this.pos+=4))}K.prototype.float=function(){if(this.pos+4>this.len)throw Rt(this,4);var e=Ct.float.readFloatLE(this.buf,this.pos);return this.pos+=4,e};K.prototype.double=function(){if(this.pos+8>this.len)throw Rt(this,4);var e=Ct.float.readDoubleLE(this.buf,this.pos);return this.pos+=8,e};K.prototype.bytes=function(){var e=this.uint32(),o=this.pos,t=this.pos+e;if(t>this.len)throw Rt(this,e);if(this.pos+=e,Array.isArray(this.buf))return this.buf.slice(o,t);if(o===t){var r=Ct.Buffer;return r?r.alloc(0):new this.buf.constructor(0)}return this._slice.call(this.buf,o,t)};K.prototype.string=function(){var e=this.bytes();return Uc.read(e,0,e.length)};K.prototype.skip=function(e){if(typeof e=="number"){if(this.pos+e>this.len)throw Rt(this,e);this.pos+=e}else do if(this.pos>=this.len)throw Rt(this);while(this.buf[this.pos++]&128);return this};K.prototype.skipType=function(i){switch(i){case 0:this.skip();break;case 1:this.skip(8);break;case 2:this.skip(this.uint32());break;case 3:for(;(i=this.uint32()&7)!==4;)this.skipType(i);break;case 5:this.skip(4);break;default:throw Error("invalid wire type "+i+" at offset "+this.pos)}return this};K._configure=function(i){ro=i,K.create=$a(),ro._configure();var e=Ct.Long?"toLong":"toNumber";Ct.merge(K.prototype,{int64:function(){return eo.call(this)[e](!1)},uint64:function(){return eo.call(this)[e](!0)},sint64:function(){return eo.call(this).zzDecode()[e](!1)},fixed64:function(){return Da.call(this)[e](!0)},sfixed64:function(){return Da.call(this)[e](!1)}})}});var Ca=tt((Db,Na)=>{"use strict";Na.exports=pe;var Fa=no();(pe.prototype=Object.create(Fa.prototype)).constructor=pe;var Ba=te();function pe(i){Fa.call(this,i)}pe._configure=function(){Ba.Buffer&&(pe.prototype._slice=Ba.Buffer.prototype.slice)};pe.prototype.string=function(){var e=this.uint32();return this.buf.utf8Slice?this.buf.utf8Slice(this.pos,this.pos=Math.min(this.pos+e,this.len)):this.buf.toString("utf-8",this.pos,this.pos=Math.min(this.pos+e,this.len))};pe._configure()});var Ga=tt((Lb,Ra)=>{"use strict";Ra.exports=Je;var oo=te();(Je.prototype=Object.create(oo.EventEmitter.prototype)).constructor=Je;function Je(i,e,o){if(typeof i!="function")throw TypeError("rpcImpl must be a function");oo.EventEmitter.call(this),this.rpcImpl=i,this.requestDelimited=!!e,this.responseDelimited=!!o}Je.prototype.rpcCall=function i(e,o,t,r,n){if(!r)throw TypeError("request must be specified");var s=this;if(!n)return oo.asPromise(i,s,e,o,t,r);if(!s.rpcImpl){setTimeout(function(){n(Error("already ended"))},0);return}try{return s.rpcImpl(e,o[s.requestDelimited?"encodeDelimited":"encode"](r).finish(),function(u,l){if(u)return s.emit("error",u,e),n(u);if(l===null){s.end(!0);return}if(!(l instanceof t))try{l=t[s.responseDelimited?"decodeDelimited":"decode"](l)}catch(f){return s.emit("error",f,e),n(f)}return s.emit("data",l,e),n(null,l)})}catch(a){s.emit("error",a,e),setTimeout(function(){n(a)},0);return}};Je.prototype.end=function(e){return this.rpcImpl&&(e||this.rpcImpl(null,null,null),this.rpcImpl=null,this.emit("end").off()),this}});var Va=tt(Ma=>{"use strict";var zc=Ma;zc.Service=Ga()});var za=tt((kb,Ua)=>{"use strict";Ua.exports={}});var qa=tt(Ha=>{"use strict";var gt=Ha;gt.build="minimal";gt.Writer=to();gt.BufferWriter=Pa();gt.Reader=no();gt.BufferReader=Ca();gt.util=te();gt.rpc=Va();gt.roots=za();gt.configure=Wa;function Wa(){gt.util._configure(),gt.Writer._configure(gt.BufferWriter),gt.Reader._configure(gt.BufferReader)}Wa()});var Xa=tt((Fb,ja)=>{"use strict";ja.exports=qa()});var Le=tt((Nb,Ka)=>{"use strict";var q=Xa(),S=q.Reader,J=q.Writer,h=q.util,p=q.roots.default||(q.roots.default={});p.onnx=function(){var i={};return i.Version=function(){var e={},o=Object.create(e);return o[e[0]="_START_VERSION"]=0,o[e[1]="IR_VERSION_2017_10_10"]=1,o[e[2]="IR_VERSION_2017_10_30"]=2,o[e[3]="IR_VERSION_2017_11_3"]=3,o[e[4]="IR_VERSION_2019_1_22"]=4,o[e[5]="IR_VERSION_2019_3_18"]=5,o[e[6]="IR_VERSION_2019_9_19"]=6,o[e[7]="IR_VERSION_2020_5_8"]=7,o[e[8]="IR_VERSION_2021_7_30"]=8,o[e[9]="IR_VERSION"]=9,o}(),i.AttributeProto=function(){function e(o){if(this.floats=[],this.ints=[],this.strings=[],this.tensors=[],this.graphs=[],this.sparseTensors=[],this.typeProtos=[],o)for(var t=Object.keys(o),r=0;r>>3){case 1:{s.name=t.string();break}case 21:{s.refAttrName=t.string();break}case 13:{s.docString=t.string();break}case 20:{s.type=t.int32();break}case 2:{s.f=t.float();break}case 3:{s.i=t.int64();break}case 4:{s.s=t.bytes();break}case 5:{s.t=p.onnx.TensorProto.decode(t,t.uint32());break}case 6:{s.g=p.onnx.GraphProto.decode(t,t.uint32());break}case 22:{s.sparseTensor=p.onnx.SparseTensorProto.decode(t,t.uint32());break}case 14:{s.tp=p.onnx.TypeProto.decode(t,t.uint32());break}case 7:{if(s.floats&&s.floats.length||(s.floats=[]),(a&7)===2)for(var u=t.uint32()+t.pos;t.pos>>0,t.i.high>>>0).toNumber())),t.s!=null&&(typeof t.s=="string"?h.base64.decode(t.s,r.s=h.newBuffer(h.base64.length(t.s)),0):t.s.length>=0&&(r.s=t.s)),t.t!=null){if(typeof t.t!="object")throw TypeError(".onnx.AttributeProto.t: object expected");r.t=p.onnx.TensorProto.fromObject(t.t)}if(t.g!=null){if(typeof t.g!="object")throw TypeError(".onnx.AttributeProto.g: object expected");r.g=p.onnx.GraphProto.fromObject(t.g)}if(t.sparseTensor!=null){if(typeof t.sparseTensor!="object")throw TypeError(".onnx.AttributeProto.sparseTensor: object expected");r.sparseTensor=p.onnx.SparseTensorProto.fromObject(t.sparseTensor)}if(t.tp!=null){if(typeof t.tp!="object")throw TypeError(".onnx.AttributeProto.tp: object expected");r.tp=p.onnx.TypeProto.fromObject(t.tp)}if(t.floats){if(!Array.isArray(t.floats))throw TypeError(".onnx.AttributeProto.floats: array expected");r.floats=[];for(var n=0;n>>0,t.ints[n].high>>>0).toNumber())}if(t.strings){if(!Array.isArray(t.strings))throw TypeError(".onnx.AttributeProto.strings: array expected");r.strings=[];for(var n=0;n=0&&(r.strings[n]=t.strings[n])}if(t.tensors){if(!Array.isArray(t.tensors))throw TypeError(".onnx.AttributeProto.tensors: array expected");r.tensors=[];for(var n=0;n>>0,t.i.high>>>0).toNumber():t.i),t.s!=null&&t.hasOwnProperty("s")&&(n.s=r.bytes===String?h.base64.encode(t.s,0,t.s.length):r.bytes===Array?Array.prototype.slice.call(t.s):t.s),t.t!=null&&t.hasOwnProperty("t")&&(n.t=p.onnx.TensorProto.toObject(t.t,r)),t.g!=null&&t.hasOwnProperty("g")&&(n.g=p.onnx.GraphProto.toObject(t.g,r)),t.floats&&t.floats.length){n.floats=[];for(var a=0;a>>0,t.ints[a].high>>>0).toNumber():t.ints[a]}if(t.strings&&t.strings.length){n.strings=[];for(var a=0;a>>3){case 1:{s.name=t.string();break}case 2:{s.type=p.onnx.TypeProto.decode(t,t.uint32());break}case 3:{s.docString=t.string();break}default:t.skipType(a&7);break}}return s},e.decodeDelimited=function(t){return t instanceof S||(t=new S(t)),this.decode(t,t.uint32())},e.verify=function(t){if(typeof t!="object"||t===null)return"object expected";if(t.name!=null&&t.hasOwnProperty("name")&&!h.isString(t.name))return"name: string expected";if(t.type!=null&&t.hasOwnProperty("type")){var r=p.onnx.TypeProto.verify(t.type);if(r)return"type."+r}return t.docString!=null&&t.hasOwnProperty("docString")&&!h.isString(t.docString)?"docString: string expected":null},e.fromObject=function(t){if(t instanceof p.onnx.ValueInfoProto)return t;var r=new p.onnx.ValueInfoProto;if(t.name!=null&&(r.name=String(t.name)),t.type!=null){if(typeof t.type!="object")throw TypeError(".onnx.ValueInfoProto.type: object expected");r.type=p.onnx.TypeProto.fromObject(t.type)}return t.docString!=null&&(r.docString=String(t.docString)),r},e.toObject=function(t,r){r||(r={});var n={};return r.defaults&&(n.name="",n.type=null,n.docString=""),t.name!=null&&t.hasOwnProperty("name")&&(n.name=t.name),t.type!=null&&t.hasOwnProperty("type")&&(n.type=p.onnx.TypeProto.toObject(t.type,r)),t.docString!=null&&t.hasOwnProperty("docString")&&(n.docString=t.docString),n},e.prototype.toJSON=function(){return this.constructor.toObject(this,q.util.toJSONOptions)},e.getTypeUrl=function(t){return t===void 0&&(t="type.googleapis.com"),t+"/onnx.ValueInfoProto"},e}(),i.NodeProto=function(){function e(o){if(this.input=[],this.output=[],this.attribute=[],o)for(var t=Object.keys(o),r=0;r>>3){case 1:{s.input&&s.input.length||(s.input=[]),s.input.push(t.string());break}case 2:{s.output&&s.output.length||(s.output=[]),s.output.push(t.string());break}case 3:{s.name=t.string();break}case 4:{s.opType=t.string();break}case 7:{s.domain=t.string();break}case 5:{s.attribute&&s.attribute.length||(s.attribute=[]),s.attribute.push(p.onnx.AttributeProto.decode(t,t.uint32()));break}case 6:{s.docString=t.string();break}default:t.skipType(a&7);break}}return s},e.decodeDelimited=function(t){return t instanceof S||(t=new S(t)),this.decode(t,t.uint32())},e.verify=function(t){if(typeof t!="object"||t===null)return"object expected";if(t.input!=null&&t.hasOwnProperty("input")){if(!Array.isArray(t.input))return"input: array expected";for(var r=0;r>>3){case 1:{s.initialization=p.onnx.GraphProto.decode(t,t.uint32());break}case 2:{s.algorithm=p.onnx.GraphProto.decode(t,t.uint32());break}case 3:{s.initializationBinding&&s.initializationBinding.length||(s.initializationBinding=[]),s.initializationBinding.push(p.onnx.StringStringEntryProto.decode(t,t.uint32()));break}case 4:{s.updateBinding&&s.updateBinding.length||(s.updateBinding=[]),s.updateBinding.push(p.onnx.StringStringEntryProto.decode(t,t.uint32()));break}default:t.skipType(a&7);break}}return s},e.decodeDelimited=function(t){return t instanceof S||(t=new S(t)),this.decode(t,t.uint32())},e.verify=function(t){if(typeof t!="object"||t===null)return"object expected";if(t.initialization!=null&&t.hasOwnProperty("initialization")){var r=p.onnx.GraphProto.verify(t.initialization);if(r)return"initialization."+r}if(t.algorithm!=null&&t.hasOwnProperty("algorithm")){var r=p.onnx.GraphProto.verify(t.algorithm);if(r)return"algorithm."+r}if(t.initializationBinding!=null&&t.hasOwnProperty("initializationBinding")){if(!Array.isArray(t.initializationBinding))return"initializationBinding: array expected";for(var n=0;n>>3){case 1:{s.irVersion=t.int64();break}case 8:{s.opsetImport&&s.opsetImport.length||(s.opsetImport=[]),s.opsetImport.push(p.onnx.OperatorSetIdProto.decode(t,t.uint32()));break}case 2:{s.producerName=t.string();break}case 3:{s.producerVersion=t.string();break}case 4:{s.domain=t.string();break}case 5:{s.modelVersion=t.int64();break}case 6:{s.docString=t.string();break}case 7:{s.graph=p.onnx.GraphProto.decode(t,t.uint32());break}case 14:{s.metadataProps&&s.metadataProps.length||(s.metadataProps=[]),s.metadataProps.push(p.onnx.StringStringEntryProto.decode(t,t.uint32()));break}case 20:{s.trainingInfo&&s.trainingInfo.length||(s.trainingInfo=[]),s.trainingInfo.push(p.onnx.TrainingInfoProto.decode(t,t.uint32()));break}case 25:{s.functions&&s.functions.length||(s.functions=[]),s.functions.push(p.onnx.FunctionProto.decode(t,t.uint32()));break}default:t.skipType(a&7);break}}return s},e.decodeDelimited=function(t){return t instanceof S||(t=new S(t)),this.decode(t,t.uint32())},e.verify=function(t){if(typeof t!="object"||t===null)return"object expected";if(t.irVersion!=null&&t.hasOwnProperty("irVersion")&&!h.isInteger(t.irVersion)&&!(t.irVersion&&h.isInteger(t.irVersion.low)&&h.isInteger(t.irVersion.high)))return"irVersion: integer|Long expected";if(t.opsetImport!=null&&t.hasOwnProperty("opsetImport")){if(!Array.isArray(t.opsetImport))return"opsetImport: array expected";for(var r=0;r>>0,t.irVersion.high>>>0).toNumber())),t.opsetImport){if(!Array.isArray(t.opsetImport))throw TypeError(".onnx.ModelProto.opsetImport: array expected");r.opsetImport=[];for(var n=0;n>>0,t.modelVersion.high>>>0).toNumber())),t.docString!=null&&(r.docString=String(t.docString)),t.graph!=null){if(typeof t.graph!="object")throw TypeError(".onnx.ModelProto.graph: object expected");r.graph=p.onnx.GraphProto.fromObject(t.graph)}if(t.metadataProps){if(!Array.isArray(t.metadataProps))throw TypeError(".onnx.ModelProto.metadataProps: array expected");r.metadataProps=[];for(var n=0;n>>0,t.irVersion.high>>>0).toNumber():t.irVersion),t.producerName!=null&&t.hasOwnProperty("producerName")&&(n.producerName=t.producerName),t.producerVersion!=null&&t.hasOwnProperty("producerVersion")&&(n.producerVersion=t.producerVersion),t.domain!=null&&t.hasOwnProperty("domain")&&(n.domain=t.domain),t.modelVersion!=null&&t.hasOwnProperty("modelVersion")&&(typeof t.modelVersion=="number"?n.modelVersion=r.longs===String?String(t.modelVersion):t.modelVersion:n.modelVersion=r.longs===String?h.Long.prototype.toString.call(t.modelVersion):r.longs===Number?new h.LongBits(t.modelVersion.low>>>0,t.modelVersion.high>>>0).toNumber():t.modelVersion),t.docString!=null&&t.hasOwnProperty("docString")&&(n.docString=t.docString),t.graph!=null&&t.hasOwnProperty("graph")&&(n.graph=p.onnx.GraphProto.toObject(t.graph,r)),t.opsetImport&&t.opsetImport.length){n.opsetImport=[];for(var a=0;a>>3){case 1:{s.key=t.string();break}case 2:{s.value=t.string();break}default:t.skipType(a&7);break}}return s},e.decodeDelimited=function(t){return t instanceof S||(t=new S(t)),this.decode(t,t.uint32())},e.verify=function(t){return typeof t!="object"||t===null?"object expected":t.key!=null&&t.hasOwnProperty("key")&&!h.isString(t.key)?"key: string expected":t.value!=null&&t.hasOwnProperty("value")&&!h.isString(t.value)?"value: string expected":null},e.fromObject=function(t){if(t instanceof p.onnx.StringStringEntryProto)return t;var r=new p.onnx.StringStringEntryProto;return t.key!=null&&(r.key=String(t.key)),t.value!=null&&(r.value=String(t.value)),r},e.toObject=function(t,r){r||(r={});var n={};return r.defaults&&(n.key="",n.value=""),t.key!=null&&t.hasOwnProperty("key")&&(n.key=t.key),t.value!=null&&t.hasOwnProperty("value")&&(n.value=t.value),n},e.prototype.toJSON=function(){return this.constructor.toObject(this,q.util.toJSONOptions)},e.getTypeUrl=function(t){return t===void 0&&(t="type.googleapis.com"),t+"/onnx.StringStringEntryProto"},e}(),i.TensorAnnotation=function(){function e(o){if(this.quantParameterTensorNames=[],o)for(var t=Object.keys(o),r=0;r>>3){case 1:{s.tensorName=t.string();break}case 2:{s.quantParameterTensorNames&&s.quantParameterTensorNames.length||(s.quantParameterTensorNames=[]),s.quantParameterTensorNames.push(p.onnx.StringStringEntryProto.decode(t,t.uint32()));break}default:t.skipType(a&7);break}}return s},e.decodeDelimited=function(t){return t instanceof S||(t=new S(t)),this.decode(t,t.uint32())},e.verify=function(t){if(typeof t!="object"||t===null)return"object expected";if(t.tensorName!=null&&t.hasOwnProperty("tensorName")&&!h.isString(t.tensorName))return"tensorName: string expected";if(t.quantParameterTensorNames!=null&&t.hasOwnProperty("quantParameterTensorNames")){if(!Array.isArray(t.quantParameterTensorNames))return"quantParameterTensorNames: array expected";for(var r=0;r>>3){case 1:{s.node&&s.node.length||(s.node=[]),s.node.push(p.onnx.NodeProto.decode(t,t.uint32()));break}case 2:{s.name=t.string();break}case 5:{s.initializer&&s.initializer.length||(s.initializer=[]),s.initializer.push(p.onnx.TensorProto.decode(t,t.uint32()));break}case 15:{s.sparseInitializer&&s.sparseInitializer.length||(s.sparseInitializer=[]),s.sparseInitializer.push(p.onnx.SparseTensorProto.decode(t,t.uint32()));break}case 10:{s.docString=t.string();break}case 11:{s.input&&s.input.length||(s.input=[]),s.input.push(p.onnx.ValueInfoProto.decode(t,t.uint32()));break}case 12:{s.output&&s.output.length||(s.output=[]),s.output.push(p.onnx.ValueInfoProto.decode(t,t.uint32()));break}case 13:{s.valueInfo&&s.valueInfo.length||(s.valueInfo=[]),s.valueInfo.push(p.onnx.ValueInfoProto.decode(t,t.uint32()));break}case 14:{s.quantizationAnnotation&&s.quantizationAnnotation.length||(s.quantizationAnnotation=[]),s.quantizationAnnotation.push(p.onnx.TensorAnnotation.decode(t,t.uint32()));break}default:t.skipType(a&7);break}}return s},e.decodeDelimited=function(t){return t instanceof S||(t=new S(t)),this.decode(t,t.uint32())},e.verify=function(t){if(typeof t!="object"||t===null)return"object expected";if(t.node!=null&&t.hasOwnProperty("node")){if(!Array.isArray(t.node))return"node: array expected";for(var r=0;r>>3){case 1:{if(s.dims&&s.dims.length||(s.dims=[]),(a&7)===2)for(var u=t.uint32()+t.pos;t.pos>>0,t.dims[n].high>>>0).toNumber())}if(t.dataType!=null&&(r.dataType=t.dataType|0),t.segment!=null){if(typeof t.segment!="object")throw TypeError(".onnx.TensorProto.segment: object expected");r.segment=p.onnx.TensorProto.Segment.fromObject(t.segment)}if(t.floatData){if(!Array.isArray(t.floatData))throw TypeError(".onnx.TensorProto.floatData: array expected");r.floatData=[];for(var n=0;n=0&&(r.stringData[n]=t.stringData[n])}if(t.int64Data){if(!Array.isArray(t.int64Data))throw TypeError(".onnx.TensorProto.int64Data: array expected");r.int64Data=[];for(var n=0;n>>0,t.int64Data[n].high>>>0).toNumber())}if(t.name!=null&&(r.name=String(t.name)),t.docString!=null&&(r.docString=String(t.docString)),t.rawData!=null&&(typeof t.rawData=="string"?h.base64.decode(t.rawData,r.rawData=h.newBuffer(h.base64.length(t.rawData)),0):t.rawData.length>=0&&(r.rawData=t.rawData)),t.externalData){if(!Array.isArray(t.externalData))throw TypeError(".onnx.TensorProto.externalData: array expected");r.externalData=[];for(var n=0;n>>0,t.uint64Data[n].high>>>0).toNumber(!0))}return r},e.toObject=function(t,r){r||(r={});var n={};if((r.arrays||r.defaults)&&(n.dims=[],n.floatData=[],n.int32Data=[],n.stringData=[],n.int64Data=[],n.doubleData=[],n.uint64Data=[],n.externalData=[]),r.defaults&&(n.dataType=0,n.segment=null,n.name="",r.bytes===String?n.rawData="":(n.rawData=[],r.bytes!==Array&&(n.rawData=h.newBuffer(n.rawData))),n.docString="",n.dataLocation=r.enums===String?"DEFAULT":0),t.dims&&t.dims.length){n.dims=[];for(var s=0;s>>0,t.dims[s].high>>>0).toNumber():t.dims[s]}if(t.dataType!=null&&t.hasOwnProperty("dataType")&&(n.dataType=t.dataType),t.segment!=null&&t.hasOwnProperty("segment")&&(n.segment=p.onnx.TensorProto.Segment.toObject(t.segment,r)),t.floatData&&t.floatData.length){n.floatData=[];for(var s=0;s>>0,t.int64Data[s].high>>>0).toNumber():t.int64Data[s]}if(t.name!=null&&t.hasOwnProperty("name")&&(n.name=t.name),t.rawData!=null&&t.hasOwnProperty("rawData")&&(n.rawData=r.bytes===String?h.base64.encode(t.rawData,0,t.rawData.length):r.bytes===Array?Array.prototype.slice.call(t.rawData):t.rawData),t.doubleData&&t.doubleData.length){n.doubleData=[];for(var s=0;s>>0,t.uint64Data[s].high>>>0).toNumber(!0):t.uint64Data[s]}if(t.docString!=null&&t.hasOwnProperty("docString")&&(n.docString=t.docString),t.externalData&&t.externalData.length){n.externalData=[];for(var s=0;s>>3){case 1:{a.begin=r.int64();break}case 2:{a.end=r.int64();break}default:r.skipType(u&7);break}}return a},o.decodeDelimited=function(r){return r instanceof S||(r=new S(r)),this.decode(r,r.uint32())},o.verify=function(r){return typeof r!="object"||r===null?"object expected":r.begin!=null&&r.hasOwnProperty("begin")&&!h.isInteger(r.begin)&&!(r.begin&&h.isInteger(r.begin.low)&&h.isInteger(r.begin.high))?"begin: integer|Long expected":r.end!=null&&r.hasOwnProperty("end")&&!h.isInteger(r.end)&&!(r.end&&h.isInteger(r.end.low)&&h.isInteger(r.end.high))?"end: integer|Long expected":null},o.fromObject=function(r){if(r instanceof p.onnx.TensorProto.Segment)return r;var n=new p.onnx.TensorProto.Segment;return r.begin!=null&&(h.Long?(n.begin=h.Long.fromValue(r.begin)).unsigned=!1:typeof r.begin=="string"?n.begin=parseInt(r.begin,10):typeof r.begin=="number"?n.begin=r.begin:typeof r.begin=="object"&&(n.begin=new h.LongBits(r.begin.low>>>0,r.begin.high>>>0).toNumber())),r.end!=null&&(h.Long?(n.end=h.Long.fromValue(r.end)).unsigned=!1:typeof r.end=="string"?n.end=parseInt(r.end,10):typeof r.end=="number"?n.end=r.end:typeof r.end=="object"&&(n.end=new h.LongBits(r.end.low>>>0,r.end.high>>>0).toNumber())),n},o.toObject=function(r,n){n||(n={});var s={};if(n.defaults){if(h.Long){var a=new h.Long(0,0,!1);s.begin=n.longs===String?a.toString():n.longs===Number?a.toNumber():a}else s.begin=n.longs===String?"0":0;if(h.Long){var a=new h.Long(0,0,!1);s.end=n.longs===String?a.toString():n.longs===Number?a.toNumber():a}else s.end=n.longs===String?"0":0}return r.begin!=null&&r.hasOwnProperty("begin")&&(typeof r.begin=="number"?s.begin=n.longs===String?String(r.begin):r.begin:s.begin=n.longs===String?h.Long.prototype.toString.call(r.begin):n.longs===Number?new h.LongBits(r.begin.low>>>0,r.begin.high>>>0).toNumber():r.begin),r.end!=null&&r.hasOwnProperty("end")&&(typeof r.end=="number"?s.end=n.longs===String?String(r.end):r.end:s.end=n.longs===String?h.Long.prototype.toString.call(r.end):n.longs===Number?new h.LongBits(r.end.low>>>0,r.end.high>>>0).toNumber():r.end),s},o.prototype.toJSON=function(){return this.constructor.toObject(this,q.util.toJSONOptions)},o.getTypeUrl=function(r){return r===void 0&&(r="type.googleapis.com"),r+"/onnx.TensorProto.Segment"},o}(),e.DataLocation=function(){var o={},t=Object.create(o);return t[o[0]="DEFAULT"]=0,t[o[1]="EXTERNAL"]=1,t}(),e}(),i.SparseTensorProto=function(){function e(o){if(this.dims=[],o)for(var t=Object.keys(o),r=0;r>>3){case 1:{s.values=p.onnx.TensorProto.decode(t,t.uint32());break}case 2:{s.indices=p.onnx.TensorProto.decode(t,t.uint32());break}case 3:{if(s.dims&&s.dims.length||(s.dims=[]),(a&7)===2)for(var u=t.uint32()+t.pos;t.pos>>0,t.dims[n].high>>>0).toNumber())}return r},e.toObject=function(t,r){r||(r={});var n={};if((r.arrays||r.defaults)&&(n.dims=[]),r.defaults&&(n.values=null,n.indices=null),t.values!=null&&t.hasOwnProperty("values")&&(n.values=p.onnx.TensorProto.toObject(t.values,r)),t.indices!=null&&t.hasOwnProperty("indices")&&(n.indices=p.onnx.TensorProto.toObject(t.indices,r)),t.dims&&t.dims.length){n.dims=[];for(var s=0;s>>0,t.dims[s].high>>>0).toNumber():t.dims[s]}return n},e.prototype.toJSON=function(){return this.constructor.toObject(this,q.util.toJSONOptions)},e.getTypeUrl=function(t){return t===void 0&&(t="type.googleapis.com"),t+"/onnx.SparseTensorProto"},e}(),i.TensorShapeProto=function(){function e(o){if(this.dim=[],o)for(var t=Object.keys(o),r=0;r>>3){case 1:{s.dim&&s.dim.length||(s.dim=[]),s.dim.push(p.onnx.TensorShapeProto.Dimension.decode(t,t.uint32()));break}default:t.skipType(a&7);break}}return s},e.decodeDelimited=function(t){return t instanceof S||(t=new S(t)),this.decode(t,t.uint32())},e.verify=function(t){if(typeof t!="object"||t===null)return"object expected";if(t.dim!=null&&t.hasOwnProperty("dim")){if(!Array.isArray(t.dim))return"dim: array expected";for(var r=0;r>>3){case 1:{u.dimValue=n.int64();break}case 2:{u.dimParam=n.string();break}case 3:{u.denotation=n.string();break}default:n.skipType(l&7);break}}return u},o.decodeDelimited=function(n){return n instanceof S||(n=new S(n)),this.decode(n,n.uint32())},o.verify=function(n){if(typeof n!="object"||n===null)return"object expected";var s={};if(n.dimValue!=null&&n.hasOwnProperty("dimValue")&&(s.value=1,!h.isInteger(n.dimValue)&&!(n.dimValue&&h.isInteger(n.dimValue.low)&&h.isInteger(n.dimValue.high))))return"dimValue: integer|Long expected";if(n.dimParam!=null&&n.hasOwnProperty("dimParam")){if(s.value===1)return"value: multiple values";if(s.value=1,!h.isString(n.dimParam))return"dimParam: string expected"}return n.denotation!=null&&n.hasOwnProperty("denotation")&&!h.isString(n.denotation)?"denotation: string expected":null},o.fromObject=function(n){if(n instanceof p.onnx.TensorShapeProto.Dimension)return n;var s=new p.onnx.TensorShapeProto.Dimension;return n.dimValue!=null&&(h.Long?(s.dimValue=h.Long.fromValue(n.dimValue)).unsigned=!1:typeof n.dimValue=="string"?s.dimValue=parseInt(n.dimValue,10):typeof n.dimValue=="number"?s.dimValue=n.dimValue:typeof n.dimValue=="object"&&(s.dimValue=new h.LongBits(n.dimValue.low>>>0,n.dimValue.high>>>0).toNumber())),n.dimParam!=null&&(s.dimParam=String(n.dimParam)),n.denotation!=null&&(s.denotation=String(n.denotation)),s},o.toObject=function(n,s){s||(s={});var a={};return s.defaults&&(a.denotation=""),n.dimValue!=null&&n.hasOwnProperty("dimValue")&&(typeof n.dimValue=="number"?a.dimValue=s.longs===String?String(n.dimValue):n.dimValue:a.dimValue=s.longs===String?h.Long.prototype.toString.call(n.dimValue):s.longs===Number?new h.LongBits(n.dimValue.low>>>0,n.dimValue.high>>>0).toNumber():n.dimValue,s.oneofs&&(a.value="dimValue")),n.dimParam!=null&&n.hasOwnProperty("dimParam")&&(a.dimParam=n.dimParam,s.oneofs&&(a.value="dimParam")),n.denotation!=null&&n.hasOwnProperty("denotation")&&(a.denotation=n.denotation),a},o.prototype.toJSON=function(){return this.constructor.toObject(this,q.util.toJSONOptions)},o.getTypeUrl=function(n){return n===void 0&&(n="type.googleapis.com"),n+"/onnx.TensorShapeProto.Dimension"},o}(),e}(),i.TypeProto=function(){function e(t){if(t)for(var r=Object.keys(t),n=0;n>>3){case 1:{a.tensorType=p.onnx.TypeProto.Tensor.decode(r,r.uint32());break}case 4:{a.sequenceType=p.onnx.TypeProto.Sequence.decode(r,r.uint32());break}case 5:{a.mapType=p.onnx.TypeProto.Map.decode(r,r.uint32());break}case 9:{a.optionalType=p.onnx.TypeProto.Optional.decode(r,r.uint32());break}case 8:{a.sparseTensorType=p.onnx.TypeProto.SparseTensor.decode(r,r.uint32());break}case 6:{a.denotation=r.string();break}default:r.skipType(u&7);break}}return a},e.decodeDelimited=function(r){return r instanceof S||(r=new S(r)),this.decode(r,r.uint32())},e.verify=function(r){if(typeof r!="object"||r===null)return"object expected";var n={};if(r.tensorType!=null&&r.hasOwnProperty("tensorType")){n.value=1;{var s=p.onnx.TypeProto.Tensor.verify(r.tensorType);if(s)return"tensorType."+s}}if(r.sequenceType!=null&&r.hasOwnProperty("sequenceType")){if(n.value===1)return"value: multiple values";n.value=1;{var s=p.onnx.TypeProto.Sequence.verify(r.sequenceType);if(s)return"sequenceType."+s}}if(r.mapType!=null&&r.hasOwnProperty("mapType")){if(n.value===1)return"value: multiple values";n.value=1;{var s=p.onnx.TypeProto.Map.verify(r.mapType);if(s)return"mapType."+s}}if(r.optionalType!=null&&r.hasOwnProperty("optionalType")){if(n.value===1)return"value: multiple values";n.value=1;{var s=p.onnx.TypeProto.Optional.verify(r.optionalType);if(s)return"optionalType."+s}}if(r.sparseTensorType!=null&&r.hasOwnProperty("sparseTensorType")){if(n.value===1)return"value: multiple values";n.value=1;{var s=p.onnx.TypeProto.SparseTensor.verify(r.sparseTensorType);if(s)return"sparseTensorType."+s}}return r.denotation!=null&&r.hasOwnProperty("denotation")&&!h.isString(r.denotation)?"denotation: string expected":null},e.fromObject=function(r){if(r instanceof p.onnx.TypeProto)return r;var n=new p.onnx.TypeProto;if(r.tensorType!=null){if(typeof r.tensorType!="object")throw TypeError(".onnx.TypeProto.tensorType: object expected");n.tensorType=p.onnx.TypeProto.Tensor.fromObject(r.tensorType)}if(r.sequenceType!=null){if(typeof r.sequenceType!="object")throw TypeError(".onnx.TypeProto.sequenceType: object expected");n.sequenceType=p.onnx.TypeProto.Sequence.fromObject(r.sequenceType)}if(r.mapType!=null){if(typeof r.mapType!="object")throw TypeError(".onnx.TypeProto.mapType: object expected");n.mapType=p.onnx.TypeProto.Map.fromObject(r.mapType)}if(r.optionalType!=null){if(typeof r.optionalType!="object")throw TypeError(".onnx.TypeProto.optionalType: object expected");n.optionalType=p.onnx.TypeProto.Optional.fromObject(r.optionalType)}if(r.sparseTensorType!=null){if(typeof r.sparseTensorType!="object")throw TypeError(".onnx.TypeProto.sparseTensorType: object expected");n.sparseTensorType=p.onnx.TypeProto.SparseTensor.fromObject(r.sparseTensorType)}return r.denotation!=null&&(n.denotation=String(r.denotation)),n},e.toObject=function(r,n){n||(n={});var s={};return n.defaults&&(s.denotation=""),r.tensorType!=null&&r.hasOwnProperty("tensorType")&&(s.tensorType=p.onnx.TypeProto.Tensor.toObject(r.tensorType,n),n.oneofs&&(s.value="tensorType")),r.sequenceType!=null&&r.hasOwnProperty("sequenceType")&&(s.sequenceType=p.onnx.TypeProto.Sequence.toObject(r.sequenceType,n),n.oneofs&&(s.value="sequenceType")),r.mapType!=null&&r.hasOwnProperty("mapType")&&(s.mapType=p.onnx.TypeProto.Map.toObject(r.mapType,n),n.oneofs&&(s.value="mapType")),r.denotation!=null&&r.hasOwnProperty("denotation")&&(s.denotation=r.denotation),r.sparseTensorType!=null&&r.hasOwnProperty("sparseTensorType")&&(s.sparseTensorType=p.onnx.TypeProto.SparseTensor.toObject(r.sparseTensorType,n),n.oneofs&&(s.value="sparseTensorType")),r.optionalType!=null&&r.hasOwnProperty("optionalType")&&(s.optionalType=p.onnx.TypeProto.Optional.toObject(r.optionalType,n),n.oneofs&&(s.value="optionalType")),s},e.prototype.toJSON=function(){return this.constructor.toObject(this,q.util.toJSONOptions)},e.getTypeUrl=function(r){return r===void 0&&(r="type.googleapis.com"),r+"/onnx.TypeProto"},e.Tensor=function(){function t(r){if(r)for(var n=Object.keys(r),s=0;s>>3){case 1:{u.elemType=n.int32();break}case 2:{u.shape=p.onnx.TensorShapeProto.decode(n,n.uint32());break}default:n.skipType(l&7);break}}return u},t.decodeDelimited=function(n){return n instanceof S||(n=new S(n)),this.decode(n,n.uint32())},t.verify=function(n){if(typeof n!="object"||n===null)return"object expected";if(n.elemType!=null&&n.hasOwnProperty("elemType")&&!h.isInteger(n.elemType))return"elemType: integer expected";if(n.shape!=null&&n.hasOwnProperty("shape")){var s=p.onnx.TensorShapeProto.verify(n.shape);if(s)return"shape."+s}return null},t.fromObject=function(n){if(n instanceof p.onnx.TypeProto.Tensor)return n;var s=new p.onnx.TypeProto.Tensor;if(n.elemType!=null&&(s.elemType=n.elemType|0),n.shape!=null){if(typeof n.shape!="object")throw TypeError(".onnx.TypeProto.Tensor.shape: object expected");s.shape=p.onnx.TensorShapeProto.fromObject(n.shape)}return s},t.toObject=function(n,s){s||(s={});var a={};return s.defaults&&(a.elemType=0,a.shape=null),n.elemType!=null&&n.hasOwnProperty("elemType")&&(a.elemType=n.elemType),n.shape!=null&&n.hasOwnProperty("shape")&&(a.shape=p.onnx.TensorShapeProto.toObject(n.shape,s)),a},t.prototype.toJSON=function(){return this.constructor.toObject(this,q.util.toJSONOptions)},t.getTypeUrl=function(n){return n===void 0&&(n="type.googleapis.com"),n+"/onnx.TypeProto.Tensor"},t}(),e.Sequence=function(){function t(r){if(r)for(var n=Object.keys(r),s=0;s>>3){case 1:{u.elemType=p.onnx.TypeProto.decode(n,n.uint32());break}default:n.skipType(l&7);break}}return u},t.decodeDelimited=function(n){return n instanceof S||(n=new S(n)),this.decode(n,n.uint32())},t.verify=function(n){if(typeof n!="object"||n===null)return"object expected";if(n.elemType!=null&&n.hasOwnProperty("elemType")){var s=p.onnx.TypeProto.verify(n.elemType);if(s)return"elemType."+s}return null},t.fromObject=function(n){if(n instanceof p.onnx.TypeProto.Sequence)return n;var s=new p.onnx.TypeProto.Sequence;if(n.elemType!=null){if(typeof n.elemType!="object")throw TypeError(".onnx.TypeProto.Sequence.elemType: object expected");s.elemType=p.onnx.TypeProto.fromObject(n.elemType)}return s},t.toObject=function(n,s){s||(s={});var a={};return s.defaults&&(a.elemType=null),n.elemType!=null&&n.hasOwnProperty("elemType")&&(a.elemType=p.onnx.TypeProto.toObject(n.elemType,s)),a},t.prototype.toJSON=function(){return this.constructor.toObject(this,q.util.toJSONOptions)},t.getTypeUrl=function(n){return n===void 0&&(n="type.googleapis.com"),n+"/onnx.TypeProto.Sequence"},t}(),e.Map=function(){function t(r){if(r)for(var n=Object.keys(r),s=0;s>>3){case 1:{u.keyType=n.int32();break}case 2:{u.valueType=p.onnx.TypeProto.decode(n,n.uint32());break}default:n.skipType(l&7);break}}return u},t.decodeDelimited=function(n){return n instanceof S||(n=new S(n)),this.decode(n,n.uint32())},t.verify=function(n){if(typeof n!="object"||n===null)return"object expected";if(n.keyType!=null&&n.hasOwnProperty("keyType")&&!h.isInteger(n.keyType))return"keyType: integer expected";if(n.valueType!=null&&n.hasOwnProperty("valueType")){var s=p.onnx.TypeProto.verify(n.valueType);if(s)return"valueType."+s}return null},t.fromObject=function(n){if(n instanceof p.onnx.TypeProto.Map)return n;var s=new p.onnx.TypeProto.Map;if(n.keyType!=null&&(s.keyType=n.keyType|0),n.valueType!=null){if(typeof n.valueType!="object")throw TypeError(".onnx.TypeProto.Map.valueType: object expected");s.valueType=p.onnx.TypeProto.fromObject(n.valueType)}return s},t.toObject=function(n,s){s||(s={});var a={};return s.defaults&&(a.keyType=0,a.valueType=null),n.keyType!=null&&n.hasOwnProperty("keyType")&&(a.keyType=n.keyType),n.valueType!=null&&n.hasOwnProperty("valueType")&&(a.valueType=p.onnx.TypeProto.toObject(n.valueType,s)),a},t.prototype.toJSON=function(){return this.constructor.toObject(this,q.util.toJSONOptions)},t.getTypeUrl=function(n){return n===void 0&&(n="type.googleapis.com"),n+"/onnx.TypeProto.Map"},t}(),e.Optional=function(){function t(r){if(r)for(var n=Object.keys(r),s=0;s>>3){case 1:{u.elemType=p.onnx.TypeProto.decode(n,n.uint32());break}default:n.skipType(l&7);break}}return u},t.decodeDelimited=function(n){return n instanceof S||(n=new S(n)),this.decode(n,n.uint32())},t.verify=function(n){if(typeof n!="object"||n===null)return"object expected";if(n.elemType!=null&&n.hasOwnProperty("elemType")){var s=p.onnx.TypeProto.verify(n.elemType);if(s)return"elemType."+s}return null},t.fromObject=function(n){if(n instanceof p.onnx.TypeProto.Optional)return n;var s=new p.onnx.TypeProto.Optional;if(n.elemType!=null){if(typeof n.elemType!="object")throw TypeError(".onnx.TypeProto.Optional.elemType: object expected");s.elemType=p.onnx.TypeProto.fromObject(n.elemType)}return s},t.toObject=function(n,s){s||(s={});var a={};return s.defaults&&(a.elemType=null),n.elemType!=null&&n.hasOwnProperty("elemType")&&(a.elemType=p.onnx.TypeProto.toObject(n.elemType,s)),a},t.prototype.toJSON=function(){return this.constructor.toObject(this,q.util.toJSONOptions)},t.getTypeUrl=function(n){return n===void 0&&(n="type.googleapis.com"),n+"/onnx.TypeProto.Optional"},t}(),e.SparseTensor=function(){function t(r){if(r)for(var n=Object.keys(r),s=0;s>>3){case 1:{u.elemType=n.int32();break}case 2:{u.shape=p.onnx.TensorShapeProto.decode(n,n.uint32());break}default:n.skipType(l&7);break}}return u},t.decodeDelimited=function(n){return n instanceof S||(n=new S(n)),this.decode(n,n.uint32())},t.verify=function(n){if(typeof n!="object"||n===null)return"object expected";if(n.elemType!=null&&n.hasOwnProperty("elemType")&&!h.isInteger(n.elemType))return"elemType: integer expected";if(n.shape!=null&&n.hasOwnProperty("shape")){var s=p.onnx.TensorShapeProto.verify(n.shape);if(s)return"shape."+s}return null},t.fromObject=function(n){if(n instanceof p.onnx.TypeProto.SparseTensor)return n;var s=new p.onnx.TypeProto.SparseTensor;if(n.elemType!=null&&(s.elemType=n.elemType|0),n.shape!=null){if(typeof n.shape!="object")throw TypeError(".onnx.TypeProto.SparseTensor.shape: object expected");s.shape=p.onnx.TensorShapeProto.fromObject(n.shape)}return s},t.toObject=function(n,s){s||(s={});var a={};return s.defaults&&(a.elemType=0,a.shape=null),n.elemType!=null&&n.hasOwnProperty("elemType")&&(a.elemType=n.elemType),n.shape!=null&&n.hasOwnProperty("shape")&&(a.shape=p.onnx.TensorShapeProto.toObject(n.shape,s)),a},t.prototype.toJSON=function(){return this.constructor.toObject(this,q.util.toJSONOptions)},t.getTypeUrl=function(n){return n===void 0&&(n="type.googleapis.com"),n+"/onnx.TypeProto.SparseTensor"},t}(),e}(),i.OperatorSetIdProto=function(){function e(o){if(o)for(var t=Object.keys(o),r=0;r>>3){case 1:{s.domain=t.string();break}case 2:{s.version=t.int64();break}default:t.skipType(a&7);break}}return s},e.decodeDelimited=function(t){return t instanceof S||(t=new S(t)),this.decode(t,t.uint32())},e.verify=function(t){return typeof t!="object"||t===null?"object expected":t.domain!=null&&t.hasOwnProperty("domain")&&!h.isString(t.domain)?"domain: string expected":t.version!=null&&t.hasOwnProperty("version")&&!h.isInteger(t.version)&&!(t.version&&h.isInteger(t.version.low)&&h.isInteger(t.version.high))?"version: integer|Long expected":null},e.fromObject=function(t){if(t instanceof p.onnx.OperatorSetIdProto)return t;var r=new p.onnx.OperatorSetIdProto;return t.domain!=null&&(r.domain=String(t.domain)),t.version!=null&&(h.Long?(r.version=h.Long.fromValue(t.version)).unsigned=!1:typeof t.version=="string"?r.version=parseInt(t.version,10):typeof t.version=="number"?r.version=t.version:typeof t.version=="object"&&(r.version=new h.LongBits(t.version.low>>>0,t.version.high>>>0).toNumber())),r},e.toObject=function(t,r){r||(r={});var n={};if(r.defaults)if(n.domain="",h.Long){var s=new h.Long(0,0,!1);n.version=r.longs===String?s.toString():r.longs===Number?s.toNumber():s}else n.version=r.longs===String?"0":0;return t.domain!=null&&t.hasOwnProperty("domain")&&(n.domain=t.domain),t.version!=null&&t.hasOwnProperty("version")&&(typeof t.version=="number"?n.version=r.longs===String?String(t.version):t.version:n.version=r.longs===String?h.Long.prototype.toString.call(t.version):r.longs===Number?new h.LongBits(t.version.low>>>0,t.version.high>>>0).toNumber():t.version),n},e.prototype.toJSON=function(){return this.constructor.toObject(this,q.util.toJSONOptions)},e.getTypeUrl=function(t){return t===void 0&&(t="type.googleapis.com"),t+"/onnx.OperatorSetIdProto"},e}(),i.OperatorStatus=function(){var e={},o=Object.create(e);return o[e[0]="EXPERIMENTAL"]=0,o[e[1]="STABLE"]=1,o}(),i.FunctionProto=function(){function e(o){if(this.input=[],this.output=[],this.attribute=[],this.attributeProto=[],this.node=[],this.opsetImport=[],o)for(var t=Object.keys(o),r=0;r>>3){case 1:{s.name=t.string();break}case 4:{s.input&&s.input.length||(s.input=[]),s.input.push(t.string());break}case 5:{s.output&&s.output.length||(s.output=[]),s.output.push(t.string());break}case 6:{s.attribute&&s.attribute.length||(s.attribute=[]),s.attribute.push(t.string());break}case 11:{s.attributeProto&&s.attributeProto.length||(s.attributeProto=[]),s.attributeProto.push(p.onnx.AttributeProto.decode(t,t.uint32()));break}case 7:{s.node&&s.node.length||(s.node=[]),s.node.push(p.onnx.NodeProto.decode(t,t.uint32()));break}case 8:{s.docString=t.string();break}case 9:{s.opsetImport&&s.opsetImport.length||(s.opsetImport=[]),s.opsetImport.push(p.onnx.OperatorSetIdProto.decode(t,t.uint32()));break}case 10:{s.domain=t.string();break}default:t.skipType(a&7);break}}return s},e.decodeDelimited=function(t){return t instanceof S||(t=new S(t)),this.decode(t,t.uint32())},e.verify=function(t){if(typeof t!="object"||t===null)return"object expected";if(t.name!=null&&t.hasOwnProperty("name")&&!h.isString(t.name))return"name: string expected";if(t.input!=null&&t.hasOwnProperty("input")){if(!Array.isArray(t.input))return"input: array expected";for(var r=0;r{"use strict";$r();Hn();j=Ae(Le());ye();de=class{static arraysEqual(e,o){if(e.length!==o.length)return!1;for(let t=0;t1&&f>1)return;a[s-u]=Math.max(l,f)}return a}static index(e,o){let t=new Array(o.length);return i.fillIndex(e,o,t),t}static fillIndex(e,o,t){let r=e.length-o.length;for(let n=0;n=0;w--)l[w]=T%s[w],T=Math.floor(T/s[w]);y||(i.fillIndex(l,e.dims,f),d=e.get(f)),g||(i.fillIndex(l,o.dims,c),m=o.get(c)),u.set(l,t(d,m))}}return u}}static isValidBroadcast(e,o){let t=e.length,r=o.length;if(t>r)return!1;for(let n=1;n<=t;n++)if(e[t-n]!==1&&e[t-n]!==o[r-n])return!1;return!0}static getBroadcastDims(e,o){let t=e.length,r=[];for(let n=0;n1&&a===1&&r.unshift(s)}return r}},Cr=class{static getShapeOfGemmResult(e,o,t,r,n){if(e.length!==2||t.length!==2)throw new Error("shape need to be of size 2");let s,a,u;o?(s=e[1],a=e[0]):(s=e[0],a=e[1]);let l=-1;if(r?(u=t[0],l=1):(u=t[1],l=0),t[l]!==a)throw new Error("dimension mismatch");if(s<=0||u<=0||a<=0)throw new Error("invalid shape specified");if(n&&!dt.isValidBroadcast(n,[s,u]))throw new Error("gemm: invalid bias shape for broadcast");return[s,u,a]}},ut=class i{static tensorDataTypeFromProto(e){switch(e){case j.onnx.TensorProto.DataType.INT8:return"int8";case j.onnx.TensorProto.DataType.UINT8:return"uint8";case j.onnx.TensorProto.DataType.BOOL:return"bool";case j.onnx.TensorProto.DataType.INT16:return"int16";case j.onnx.TensorProto.DataType.UINT16:return"uint16";case j.onnx.TensorProto.DataType.INT32:return"int32";case j.onnx.TensorProto.DataType.UINT32:return"uint32";case j.onnx.TensorProto.DataType.FLOAT:return"float32";case j.onnx.TensorProto.DataType.DOUBLE:return"float64";case j.onnx.TensorProto.DataType.STRING:return"string";case j.onnx.TensorProto.DataType.INT64:return"int32";case j.onnx.TensorProto.DataType.UINT64:return"uint32";default:throw new Error(`unsupported data type: ${j.onnx.TensorProto.DataType[e]}`)}}static tensorDataTypeStringToEnum(e){switch(e){case"int8":return j.onnx.TensorProto.DataType.INT8;case"uint8":return j.onnx.TensorProto.DataType.UINT8;case"bool":return j.onnx.TensorProto.DataType.BOOL;case"int16":return j.onnx.TensorProto.DataType.INT16;case"uint16":return j.onnx.TensorProto.DataType.UINT16;case"int32":return j.onnx.TensorProto.DataType.INT32;case"uint32":return j.onnx.TensorProto.DataType.UINT32;case"float32":return j.onnx.TensorProto.DataType.FLOAT;case"float64":return j.onnx.TensorProto.DataType.DOUBLE;case"string":return j.onnx.TensorProto.DataType.STRING;case"int64":return j.onnx.TensorProto.DataType.INT64;case"uint64":return j.onnx.TensorProto.DataType.UINT64;default:throw new Error(`unsupported data type: ${e}`)}}static tensorDimsFromProto(e){return e.map(o=>jt.isLong(o)?o.toNumber():o)}static tensorValueTypeFromProto(e){return{tensorType:i.tensorDataTypeFromProto(e.elemType),shape:{dims:i.tensorDimsFromProto(e.shape.dim.map(o=>o.dimValue))}}}static tensorDimsFromORTFormat(e){let o=[];for(let t=0;te.length)throw new Error(`invalid dimension of ${o} for sizeFromDimension as Tensor has ${e.length} dimensions.`);return i.getSizeFromDimensionRange(e,o,e.length)}static sizeToDimension(e,o){if(o<0||o>e.length)throw new Error(`invalid dimension of ${o} for sizeToDimension as Tensor has ${e.length} dimensions.`);return i.getSizeFromDimensionRange(e,0,o)}static getSizeFromDimensionRange(e,o,t){let r=1;for(let n=o;n=0;--r)t[r]=t[r+1]*e[r+1];return t}static transpose(e){return e.slice().reverse()}static indicesToOffset(e,o,t){t===void 0&&(t=e.length);let r=0;for(let n=0;n=o)throw new Error("unsupported axis for this operation.");return e<0?e+o:e}static normalizeAxes(e,o){return e.map(t=>this.normalizeAxis(t,o))}static incrementIndex(e,o,t){if(o.length===0||e.length===0)throw new Error("Index incrementing unsupported for scalar Tensor");if(t===void 0)t=o.length;else if(t<=0||t>o.length)throw new Error("Incorrect axis to increment on");for(let r=t-1;r>=0&&(e[r]++,!(e[r]=e.length)throw new Error("the dimension with value zero exceeds the dimension size of the input tensor");r[u]=e[u]}else r[u]=o[u];s*=r[u]}}let a=i.size(e);if(n!==-1){if(a%s!==0)throw new Error(`the input tensor cannot be reshaped to the requested shape. Input shape: [${e}] Output shape: [${o}]`);r[n]=a/s}else if(s!==a)throw new Error("reshapedDims and originalDims don't have matching sizes");return r}static sortBasedOnPerm(e,o){return o?o.map(t=>e[t]):e.slice().reverse()}static padShape(e,o){let t=e.length;return e.map((r,n)=>r+o[n]+o[n+t])}static areEqual(e,o){return e.length!==o.length?!1:e.every((t,r)=>t===o[r])}static validateDimsAndCalcSize(e){if(e.length>6)throw new TypeError("Only rank 0 to 6 is supported for tensor shape.");let o=1;for(let t of e){if(!Number.isInteger(t))throw new TypeError(`Invalid shape: ${t} is not an integer`);if(t<0||t>2147483647)throw new TypeError(`Invalid shape: length ${t} is not allowed`);o*=t}return o}static flattenShape(e,o){o<0&&(o+=e.length);let t=e.reduce((s,a)=>s*a,1),r=e.slice(o).reduce((s,a)=>s*a,1);return[t/r,r]}static squeezeShape(e,o){let t=new Array;o=i.normalizeAxes(o,e.length);for(let r=0;r=0;if(n&&e[r]!==1)throw new Error("squeeze an axis of size different than 1");(o.length===0&&e[r]>1||o.length>0&&!n)&&t.push(e[r])}return t}static unsqueezeShape(e,o){let t=new Array(e.length+o.length);t.fill(0);for(let n=0;n=t.length)throw new Error("'axes' has an out of range axis");if(t[s]!==0)throw new Error("'axes' has a duplicate axis");t[s]=1}let r=0;for(let n=0;n=t.length?t.push(o[a+2]):t[a]=o[a+2];for(let a=0;a=t[a]||s[a+t.length]>=t[a])throw new Error("pads should be smaller than kernel")}}static adjustPadsBasedOnAutoPad(e,o,t,r,n,s){if(s){if(n.length!==2*(e.length-2))throw new Error("length of pads should be twice the length of data dimensions");if(o.length!==e.length-2)throw new Error("length of strides should be the length of data dimensions");if(r.length!==e.length-2)throw new Error("length of kernel shapes should be the length of data dimensions");for(let a=0;a{"use strict";Ya=Ae(Vi());Hn();je();N=Ae(Le());U();ao=P.experimental.fbs,rt=class i{constructor(e,o,t,r,n,s=Ya.Guid.create()){this.dims=e;this.type=o;this.dataProvider=t;this.asyncDataProvider=r;this.cache=n;this.dataId=s;this.size=A.validateDimsAndCalcSize(e);let a=this.size,u=t===void 0&&r===void 0&&n===void 0;if(n!==void 0&&n.length!==a)throw new RangeError("Input dims doesn't match data length.");if(o==="string"){if(n!==void 0&&(!Array.isArray(n)||!n.every(l=>typeof l=="string")))throw new TypeError("cache should be a string array");u&&(this.cache=new Array(a))}else{if(n!==void 0){let l=Qa(o);if(!(n instanceof l))throw new TypeError(`cache should be type ${l.name}`)}if(u){let l=new ArrayBuffer(a*Wc(o));this.cache=Hc(l,o)}}}get data(){if(this.cache===void 0){let e=this.dataProvider(this.dataId);if(e.length!==this.size)throw new Error("Length of data provided by the Data Provider is inconsistent with the dims of this Tensor.");this.cache=e}return this.cache}get stringData(){if(this.type!=="string")throw new TypeError("data type is not string");return this.data}get integerData(){switch(this.type){case"uint8":case"int8":case"uint16":case"int16":case"int32":case"uint32":case"bool":return this.data;default:throw new TypeError("data type is not integer (uint8, int8, uint16, int16, int32, uint32, bool)")}}get floatData(){switch(this.type){case"float32":case"float64":return this.data;default:throw new TypeError("data type is not float (float32, float64)")}}get numberData(){if(this.type!=="string")return this.data;throw new TypeError("type cannot be non-number (string)")}get(e){return this.data[A.indicesToOffset(e,this.strides)]}set(e,o){this.data[A.indicesToOffset(e,this.strides)]=o}async getData(){return this.cache===void 0&&(this.cache=await this.asyncDataProvider(this.dataId)),this.cache}get strides(){return this._strides||(this._strides=A.computeStrides(this.dims)),this._strides}static fromProto(e){if(!e)throw new Error("cannot construct Value from an empty tensor");let o=ut.tensorDataTypeFromProto(e.dataType),t=ut.tensorDimsFromProto(e.dims),r=new i(t,o);if(o==="string")e.stringData.forEach((n,s)=>{r.data[s]=Ye(n)});else if(e.rawData&&typeof e.rawData.byteLength=="number"&&e.rawData.byteLength>0){let n=r.data,s=new DataView(e.rawData.buffer,e.rawData.byteOffset,e.rawData.byteLength),a=Ja(e.dataType),u=e.rawData.byteLength/a;if(e.rawData.byteLength%a!==0)throw new Error("invalid buffer length");if(n.length!==u)throw new Error("buffer length mismatch");for(let l=0;l0){let n=r.data,s=new DataView(e.rawDataArray().buffer,e.rawDataArray().byteOffset,e.rawDataLength()),a=Ja(e.dataType()),u=e.rawDataLength()/a;if(e.rawDataLength()%a!==0)throw new Error("invalid buffer length");if(n.length!==u)throw new Error("buffer length mismatch");for(let l=0;l{"use strict";qc={version:"",attribute:"attribute",varyingVertex:"varying",varyingFrag:"varying",texture2D:"texture2D",output:"gl_FragColor",outputDeclaration:""},jc={version:"#version 300 es",attribute:"in",varyingVertex:"out",varyingFrag:"in",texture2D:"texture",output:"outputColor",outputDeclaration:"out vec4 outputColor;"}});var R=x(()=>{"use strict"});async function uo(i,e=t=>0,o){return new Promise((t,r)=>{let n=0,s=()=>{if(i()){t();return}n++;let a=e(n);if(o!=null&&n>=o){r();return}setTimeout(s,a)};s()})}function Rr(i){return $e(typeof i<"u"&&i.length!==0,()=>"empty string found for sampler name"),"get"+i.charAt(0).toUpperCase()+i.slice(1)}function ns(i){return $e(typeof i<"u"&&i.length!==0,()=>"empty string found for sampler name"),"get"+i.charAt(0).toUpperCase()+i.slice(1)+"AtOutCoords"}function ke(i,e){let o=JSON.parse(JSON.stringify(i));return o=e,o}function Be(i,e){return e.map(o=>i[o]).join(", ")}function ht(i){if(i<=1)return"int";if(i===2)return"ivec2";if(i===3)return"ivec3";if(i===4)return"ivec4";if(i===5)return"ivec5";if(i===6)return"ivec6";throw Error(`GPU for rank ${i} is not yet supported`)}function Gt(i=6){return["x","y","z","w","u","v"].slice(0,i)}var Vt=x(()=>{"use strict";U()});function Xc(i,e){return Gt(e).map(o=>`${i}.${o}`)}function Fe(i,e){return e===1?[i]:Xc(i,e)}function Ut(){return` + float getChannel(vec4 frag, int dim) { + int modCoord = imod(dim, 2); + return modCoord == 0 ? frag.r : frag.g; + } + + float getChannel(vec4 frag, vec2 innerDims) { + vec2 modCoord = mod(innerDims, 2.); + return modCoord.x == 0. ? + (modCoord.y == 0. ? frag.r : frag.g) : + (modCoord.y == 0. ? frag.b : frag.a); + } + `}var ge=x(()=>{"use strict";Vt()});function Jc(i,e,o){if(i===0)return"false";if(i===1)return`rc > ${e[0]}`;let t="";for(let r=i-2;r= ${e[r-i+2]}`,r= ${i[0]} ? 0. : getA(rc + 1), + 0, 0`;let t="r, c",r="r, cp1",n="rp1, c",s="rp1, cp1",a="";if(o>2)for(let u=0;u= ${t}; + bool cEdge = cp1 >= ${o}; + `}var os,Kc,is,as=x(()=>{"use strict";X();R();Vt();ge();os={name:"pack",inputNames:["A"],inputTypes:[1]},Kc=(i,e)=>{let o=D(i.session.backend.glContext.version),t=e.dims,r=t.length,n=e.dims.length,s=ht(n),a=Fe("rc",n),u=Yc(n,a,t[t.length-2],t[t.length-1]),l;r===0?l=[1,1]:r===1?l=[t[0],1]:l=[t[n-1],t[n-2]];let f=Jc(n,l,a),c=Zc(t,a),d=` + void main() { + ${s} rc = getOutputCoords(); + + if(${f}) { + ${o.output} = vec4(0); + } else { + ${u} + + ${o.output} = vec4(${c}); + } + } + `;return{...os,hasMain:!0,output:{dims:e.dims,type:e.type,textureType:2},shaderSource:d}},is=(i,e)=>({...os,get:()=>Kc(i,e)})});function lo(i){if(i.length===0)return[1,1,1];let e=1;for(let o=0;o1?i[i.length-2]:1,i[i.length-1]]}function us(i,e){let o=!1;return i.length===0||e.length===0?o=!0:i.length<2||e.length<2?o=i[i.length-1]===e[e.length-1]:o=i[i.length-1]===e[e.length-1]&&i[i.length-2]===e[e.length-2],o}function ep(i){let e=A.computeStrides(i),o=["b","r","c"],t="index";return` + ivec3 inputCoordsFromReshapedOutCoords(int index) { + ${e.map((n,s)=>{let a=`int ${o[s]} = ${t} / ${n}`,u=s===e.length-1?`int ${o[s+1]} = ${t} - ${o[s]} * ${n}`:`index -= ${o[s]} * ${n}`;return`${a}; ${u};`}).join("")} + return ivec3(b, r, c); + } + `}function rp(i){let e=A.computeStrides(i);return` + int getFlattenedIndex(ivec3 coords) { + // reverse y, z order + return coords.x * ${e[0]} + coords.z * ${e[1]} + coords.y; + } +`}var Qc,tp,ss,ls=x(()=>{"use strict";U();X();R();ge();Qc=i=>({name:"Reshape (packed)",inputTypes:[2],inputNames:["A"],cacheHint:`${i}`}),tp=(i,e,o,t)=>{let r=e.dims,n=t,s="";for(let l=0;l<4;l++){let f="";switch(l){case 0:f="outputCoords = rc;";break;case 1:f="outputCoords = ivec3(rc.x, rc.y+1, rc.z);";break;case 2:f="outputCoords = ivec3(rc.x, rc.y, rc.z+1);";break;case 3:f="outputCoords = ivec3(rc.x, rc.y+1, rc.z+1);";break;default:throw new Error}s+=` + ${f} + ${l>0?"if(outputCoords.y < rows && outputCoords.z < cols){":""} + int flattenedIndex = getFlattenedIndex(outputCoords); + + ivec3 inputRC = inputCoordsFromReshapedOutCoords(flattenedIndex); + vec2 innerDims = vec2(float(inputRC.y),float(inputRC.z)); + + result[${l}] = getChannel(getA(inputRC.x, inputRC.y, inputRC.z), innerDims); + + ${l>0?"}":""} + `}let a=D(i.session.backend.glContext.version),u=` + ${ep(r)} + ${rp(n)} + ${Ut()} + + void main() { + ivec3 rc = getOutputCoords(); + + vec4 result = vec4(0.0); + + ivec3 outputCoords; + int rows = ${n[2]}; + int cols = ${n[1]}; + + ${s} + ${a.output} = result; + } + `;return{...o,output:{dims:n,type:e.type,textureType:2},shaderSource:u,hasMain:!0}},ss=(i,e,o)=>{let t=Qc(o);return{...t,get:()=>tp(i,e,t,o)}}});var fo,fs=x(()=>{"use strict";X();R();fo=(i,e)=>{let o=e.shape,t=D(i.session.backend.glContext.version),r=` + const float FLOAT_MAX = 1.70141184e38; + const float FLOAT_MIN = 1.17549435e-38; + + bool isNaN(float val) { + return (val < 1.0 || 0.0 < val || val == 0.0) ? false : true; + } + + highp vec4 encodeAsUint8(highp float v) { + if (isNaN(v)) { + return vec4(255, 255, 255, 255); + } + + highp float av = abs(v); + + if(av < FLOAT_MIN) { + return vec4(0.0, 0.0, 0.0, 0.0); + } else if(v > FLOAT_MAX) { + return vec4(0.0, 0.0, 128.0, 127.0) / 255.0; + } else if(v < -FLOAT_MAX) { + return vec4(0.0, 0.0, 128.0, 255.0) / 255.0; + } + + highp vec4 c = vec4(0,0,0,0); + + highp float e = floor(log2(av)); + highp float m = exp2(fract(log2(av))) - 1.0; + + c[2] = floor(128.0 * m); + m -= c[2] / 128.0; + c[1] = floor(32768.0 * m); + m -= c[1] / 32768.0; + c[0] = floor(8388608.0 * m); + + highp float ebias = e + 127.0; + c[3] = floor(ebias / 2.0); + ebias -= c[3] * 2.0; + c[2] += floor(ebias) * 128.0; + + c[3] += 128.0 * step(0.0, -v); + + return c / 255.0; + } + + void main() { + float value = ${t.texture2D}(X,TexCoords).r; + ${t.output} = encodeAsUint8(value); + }`,n={name:"Uint8Encode",inputTypes:[0],inputNames:["X"],output:{dims:o,type:e.tensor.type,textureType:3},shaderSource:r,hasMain:!0};return i.executeProgram(n,[e.tensor])}});function op(i,e){if(i===1)return"rc";let o="";for(let t=0;t{"use strict";X();R();Vt();ge();cs={name:"unpack",inputNames:["A"],inputTypes:[2]},np=(i,e)=>{let o=e.dims.length,t=Fe("rc",o),r=t.slice(-2),n=ht(o),s=Ut(),u=e.dims.length===0?"":op(o,t),l=o<=1?"rc":`vec2(${r.join(",")})`,f=D(i.session.backend.glContext.version),c=` + ${s} + void main() { + ${n} rc = getOutputCoords(); + + // Sample the texture with the coords to get the rgba channel value. + vec4 packedInput = getA(${u}); + + ${f.output} = vec4(getChannel(packedInput, ${l}), 0, 0, 0); + } + `;return{...cs,hasMain:!0,output:{dims:e.dims,type:e.type,textureType:0},shaderSource:c}},ps=(i,e)=>({...cs,get:()=>np(i,e)})});var Gr,Qe,Mr,tr=x(()=>{"use strict";It();Gr=class{constructor(e,o=1){if(o===1)this.internalFormat=e.R32F,this.format=e.RED,this.textureType=e.FLOAT,this.channelSize=o;else if(o===4)this.internalFormat=e.RGBA32F,this.format=e.RGBA,this.textureType=e.FLOAT,this.channelSize=o;else throw new Error(`Invalid number of channels: ${o}`)}encode(e,o){let t,r;return e.constructor!==Float32Array&&(z.warning("Encoder","data was not of type Float32; creating new Float32Array"),r=new Float32Array(e)),o*this.channelSize>e.length?(z.warning("Encoder","Source data too small. Allocating larger array"),r=e,t=this.allocate(o*this.channelSize),r.forEach((n,s)=>t[s]=n)):(r=e,t=r),t}allocate(e){return new Float32Array(e*4)}decode(e,o){return this.channelSize===1?e.filter((r,n)=>n%4===0).subarray(0,o):e.subarray(0,o)}},Qe=class{constructor(e,o=1,t){if(o!==1&&o!==4)throw new Error(`Invalid number of channels: ${o}`);this.internalFormat=e.RGBA,this.format=e.RGBA,this.channelSize=o,this.textureType=t||e.FLOAT}encode(e,o){let t=e;return this.channelSize===1&&(z.verbose("Encoder","Exploding into a larger array"),t=this.allocate(o),e.forEach((r,n)=>t[n*4]=r)),t}allocate(e){return new Float32Array(e*4)}decode(e,o){return this.channelSize===1?e.filter((r,n)=>n%4===0).subarray(0,o):e.subarray(0,o)}},Mr=class{constructor(e,o=1){this.channelSize=4;if(o===1)this.internalFormat=e.ALPHA,this.format=e.ALPHA,this.textureType=e.UNSIGNED_BYTE,this.channelSize=o;else if(o===4)this.internalFormat=e.RGBA,this.format=e.RGBA,this.textureType=e.UNSIGNED_BYTE,this.channelSize=o;else throw new Error(`Invalid number of channels: ${o}`)}encode(e,o){return new Uint8Array(e.buffer,e.byteOffset,e.byteLength)}allocate(e){return new Uint8Array(e*this.channelSize)}decode(e,o){if(e instanceof Uint8Array)return e.subarray(0,o);throw new Error(`Invalid array type: ${e.constructor}`)}}});var er,hs,co,ms=x(()=>{"use strict";U();R();er=(i,e,o)=>{let t=o===0||o===1?1:4,r=o===2,n=o===1||o===2,s=o===4?e.length-1:void 0,a=o===4?e.map((u,l)=>l===e.length-1?u*4:u):void 0;return co(i,e,t,a,{isPacked:r,reverseWH:n,breakAxis:s})},hs=(i,e,o)=>{let t=er(i,e,o);return[t.width,t.height]},co=(i,e,o=1,t,r)=>{let n=!!(r&&r.isPacked),[s,a]=i.computeTextureWH(n&&t||e,r),u=e.length,l=e.slice(0);if(u===0&&(l=[1]),o===1)t=e;else if(n){if(o!==4)throw new Error("a packed texture must be 4-channel");t=e,u>0&&(l[u-1]=Math.ceil(l[u-1]/2)),u>1&&(l[u-2]=Math.ceil(l[u-2]/2))}else if(!t)throw new Error("Unpacked shape is needed when using channels > 1");return{width:s,height:a,channels:o,isPacked:n,shape:l,strides:A.computeStrides(l),unpackedShape:t,reversedWH:r&&r.reverseWH}}});var ap,Vr,ys=x(()=>{"use strict";It();ye();U();as();ls();fs();ds();tr();ms();R();ap=(i,e)=>{let o=e.map(r=>`${r.unpackedShape.join(",")};${r.width}x${r.height}`).join("_"),t=i.name;return i.cacheHint&&(t+="["+i.cacheHint+"]"),t+=":"+o,t},Vr=class{constructor(e){this.session=e;this.packedTextureDataCache=new Map,this.unpackedTextureDataCache=new Map}calculateTextureWidthAndHeight(e,o){return hs(this.session.layoutStrategy,e,o)}executeProgram(e,o){if(o.lengththis.readTexture(s),async a=>this.readTextureAsync(s),void 0,n),texture:t};return this.setTextureData(s.tensor.dataId,s,e.isPacked),s}getTextureData(e,o=!1){return this.session.isInitializer(e)?this.session.getTextureData(e,o):o?this.packedTextureDataCache.get(e):this.unpackedTextureDataCache.get(e)}setTextureData(e,o,t=!1){this.session.isInitializer(e)?this.session.setTextureData(e,o,t):(t?this.packedTextureDataCache:this.unpackedTextureDataCache).set(e,o)}isTextureLayoutCached(e,o=!1){return!!this.getTextureData(e.dataId,o)}dispose(){this.session.textureManager.clearActiveTextures(),this.packedTextureDataCache.forEach(e=>this.session.textureManager.releaseTexture(e)),this.packedTextureDataCache=new Map,this.unpackedTextureDataCache.forEach(e=>this.session.textureManager.releaseTexture(e)),this.unpackedTextureDataCache=new Map}readTexture(e){return e.isPacked?this.readTexture(this.unpack(e)):this.session.backend.glContext.isFloat32DownloadSupported?this.session.textureManager.readTexture(e,e.tensor.type,e.channels):this.session.textureManager.readUint8TextureAsFloat(fo(this,e))}async readTextureAsync(e){return e.isPacked?this.readTextureAsync(this.unpack(e)):this.session.backend.glContext.isFloat32DownloadSupported?this.session.textureManager.readTextureAsync(e,e.tensor.type,e.channels):this.session.textureManager.readUint8TextureAsFloat(fo(this,e))}pack(e){return this.executeProgram(is(this,e.tensor),[e.tensor])}unpack(e){return this.executeProgram(ps(this,e.tensor),[e.tensor])}}});var po,F,at=x(()=>{"use strict";po=class{constructor(e){Object.assign(this,e)}get cacheKey(){return this.key||(this.key=Object.getOwnPropertyNames(this).sort().map(e=>`${this[e]}`).join(";")),this.key}},F=i=>new po(i)});var gs,xs,Ts,sp,up,ws=x(()=>{"use strict";at();X();R();gs={name:"BatchNormalization",inputNames:["A","Scale","B","Mean","Variance"],inputTypes:[0,0,0,0,0]},xs=(i,e,o)=>(up(e),[i.run({...gs,cacheHint:o.cacheKey,get:()=>sp(i,e,o)},e)]),Ts=i=>{let e=i.attributes.getFloat("epsilon",1e-5),o=i.attributes.getFloat("momentum",.9),t=i.attributes.getInt("spatial",1);return F({epsilon:e,momentum:o,spatial:t})},sp=(i,e,o)=>{let t=D(i.session.backend.glContext.version),r=e[0].dims.length,[n,s]=i.calculateTextureWidthAndHeight(e[1].dims,0),a=` + float process(int[${r}] indices) { + vec2 position = offsetToCoords(indices[1], ${n}, ${s}); + float scale = getColorAsFloat(${t.texture2D}(Scale, position)); + float mean = getColorAsFloat(${t.texture2D}(Mean, position)); + float variance = getColorAsFloat(${t.texture2D}(Variance, position)); + float b = getColorAsFloat(${t.texture2D}(B, position)); + + return scale * ( (_A(indices) - mean) / sqrt(variance + float(${o.epsilon})) ) + b; + }`;return{...gs,output:{dims:e[0].dims,type:e[0].type,textureType:0},shaderSource:a}},up=i=>{if(!i||i.length!==5)throw new Error("BatchNormalization requires 5 inputs.");let e=i[0],o=i[1],t=i[2],r=i[3],n=i[4];if(e.dims.length<3||o.dims.length!==1||t.dims.length!==1||r.dims.length!==1||n.dims.length!==1)throw new Error("invalid input shape.");if(o.dims[0]!==e.dims[1]||t.dims[0]!==e.dims[1]||r.dims[0]!==e.dims[1]||n.dims[0]!==e.dims[1])throw new Error("invalid input shape.");if(e.type!=="float32"&&e.type!=="float64"||o.type!=="float32"&&o.type!=="float64"||t.type!=="float32"&&t.type!=="float64"||r.type!=="float32"&&r.type!=="float64"||n.type!=="float32"&&n.type!=="float64")throw new Error("invalid input tensor types.")}});var Ur,At,O,rr,zr,Xt=x(()=>{"use strict";Ur=class{constructor(e,o,t,r){this.glContext=e;this.programInfo=o;this.inputTextureLayouts=t;this.outputTextureLayout=r}},At=class{constructor(e){this.context=e}},O=class{constructor(e,o){this.routineBody=e;this.dependencies=o}},rr=class{constructor(e,o,t){this.name=e;t?this.dependencies=t:this.dependencies=[],o&&(this.routineBody=o)}addDependency(e){e&&this.dependencies.push(e)}},zr=class{static returnOrderedNodes(e){if(!e||e.length===0)return[];if(e.length===1)return e;let o=new Set,t=new Set,r=new Array;return this.createOrderedNodes(e,o,t,r),r}static createOrderedNodes(e,o,t,r){for(let n=0;n0)for(let s=0;s b); + } + vec4 ${i}(vec4 v1, vec4 v2) { + return vec4( v1.r > v2.r , + v1.g > v2.g, + v1.b > v2.b, + v1.a > v2.a ); + } + `,name:i,type:0}}function bp(){let i="less_";return{body:` + float ${i}(float a, float b) { + return float(a < b); + } + vec4 ${i}(vec4 v1, vec4 v2) { + return vec4( v1.r < v2.r , + v1.g < v2.g, + v1.b < v2.b, + v1.a < v2.a ); + } + `,name:i,type:0}}function yp(){let i="and_";return{body:` + float ${i}(float a, float b) { + return float( bool(a) && bool(b) ); + } + vec4 ${i}(vec4 v1, vec4 v2) { + bvec4 b1 = bvec4(v1); + bvec4 b2 = bvec4(v2); + return vec4( b1.r && b2.r , + b1.g && b2.g, + b1.b && b2.b, + b1.a && b2.a ); + } + `,name:i,type:0}}function gp(){let i="or_";return{body:` + float ${i}(float a, float b) { + return float( bool(a) || bool(b) ); + } + vec4 ${i}(vec4 v1, vec4 v2) { + bvec4 b1 = bvec4(v1); + bvec4 b2 = bvec4(v2); + return vec4( b1.r || b2.r , + b1.g || b2.g, + b1.b || b2.b, + b1.a || b2.a ); + } + `,name:i,type:0}}function xp(){let i="xor_";return{body:` + float ${i}(float a, float b) { + return float( bool(a) ^^ bool(b) ); + } + vec4 ${i}(vec4 v1, vec4 v2) { + bvec4 b1 = bvec4(v1); + bvec4 b2 = bvec4(v2); + return vec4( b1.r ^^ b2.r , + b1.g ^^ b2.g, + b1.b ^^ b2.b, + b1.a ^^ b2.a ); + } + `,name:i,type:0}}function Tp(){return vp("pow")}function wp(){let i="prelu_";return{body:` + float ${i}(float a, float b) { + return a < 0.0 ? a * b: a; + } + vec4 ${i}(vec4 v1, vec4 v2) { + return vec4( + v1.r < 0.0 ? v1.r * v2.r: v1.r, + v1.g < 0.0 ? v1.g * v2.g: v1.g, + v1.b < 0.0 ? v1.b * v2.b: v1.b, + v1.a < 0.0 ? v1.a * v2.a: v1.a + ); + } + `,name:i,type:0}}function vp(i){let e=`${i}_`;return{body:` + float ${e}(float a, float b) { + return ${i}(a, b); + } + vec4 ${e}(vec4 v1, vec4 v2) { + return ${i}(v1, v2); + } + `,name:e,type:0}}var Pt,Ip,vs,Is,_s,Ss,Os,As,Ps,Es,Ds,Ls,$s,ks,Bs=x(()=>{"use strict";U();Xt();X();R();Pt=(i,e,o,t=e[0].type,r)=>{let n=i.session.pack?2:0;return{name:o.name,inputNames:["A","B"],inputTypes:[n,n],cacheHint:r,get:()=>Ip(i,e,o,t)}},Ip=(i,e,o,t=e[0].type)=>{let r=i.session.pack?2:0,n=!A.areEqual(e[0].dims,e[1].dims),s=e[0].dims,a=i.session.pack;if(n){let f=dt.calcShape(e[0].dims,e[1].dims,!1);if(!f)throw new Error("Can't perform binary op on the given tensors");s=f;let c=s.length,d=e[0].dims.length!==0?e[0].dims.length:1,m=e[1].dims.length!==0?e[1].dims.length:1,y=e[0].dims.length!==0?"bcastIndices_A(indices, aindices);":"aindices[0] = 0;",g=e[1].dims.length!==0?"bcastIndices_B(indices, bindices);":"bindices[0] = 0;",T=D(i.session.backend.glContext.version),_=a?` + ${o.body} + void main() { + vec4 a = getAAtOutCoords(); + vec4 b = getBAtOutCoords(); + vec4 result = ${o.name}(a, b); + ${T.output} = result; + }`:` + ${o.body} + float process(int indices[${c}]) { + int aindices[${d}]; + int bindices[${m}]; + ${y} + ${g} + return ${o.name}(_A(aindices), _B(bindices)); + }`;return{name:o.name,inputNames:["A","B"],inputTypes:[r,r],output:{dims:s,type:t,textureType:r},shaderSource:_,hasMain:a}}let u=D(i.session.backend.glContext.version),l=` + ${o.body} + void main() { + vec4 v1 = ${u.texture2D}(A, TexCoords); + vec4 v2 = ${u.texture2D}(B, TexCoords); + vec4 result = ${o.name}(v1, v2); + ${u.output} = result; + } + `;return{name:o.name,inputNames:["A","B"],inputTypes:[r,r],output:{dims:e[0].dims,type:t,textureType:r},shaderSource:l,hasMain:!0}},vs=(i,e)=>[i.run(Pt(i,e,fp()),e)],Is=(i,e)=>[i.run(Pt(i,e,yp(),"bool"),e)],_s=(i,e)=>[i.run(Pt(i,e,cp()),e)],Ss=(i,e)=>[i.run(Pt(i,e,hp(),"bool"),e)],Os=(i,e)=>[i.run(Pt(i,e,mp(),"bool"),e)],As=(i,e)=>[i.run(Pt(i,e,bp(),"bool"),e)],Ps=(i,e)=>[i.run(Pt(i,e,pp()),e)],Es=(i,e)=>[i.run(Pt(i,e,gp(),"bool"),e)],Ds=(i,e)=>[i.run(Pt(i,e,Tp()),e)],Ls=(i,e)=>[i.run(Pt(i,e,wp()),e)],$s=(i,e)=>[i.run(Pt(i,e,dp()),e)],ks=(i,e)=>[i.run(Pt(i,e,xp(),"bool"),e)]});var Fs,Ns,Sp,Cs=x(()=>{"use strict";U();Fs=(i,e,o)=>(Sp(e),[i.cast(e[0],o)]),Ns=i=>ut.tensorDataTypeFromProto(i.attributes.getInt("to")),Sp=i=>{if(!i||i.length!==1)throw new Error("Cast requires 1 input.");if(i[0].type==="string")throw new Error("Invalid input type.")}});var Op,Ap,Rs,Wr,Gs=x(()=>{"use strict";X();R();Vt();ge();Op=(i,e)=>({name:"Concat (packed)",inputNames:Array.from({length:i},(o,t)=>`X${t}`),inputTypes:Array(i).fill(2),cacheHint:e}),Ap=(i,e,o,t)=>{let r=o[0].dims.slice();if(t>=r.length||t<-1*r.length)throw new Error("axis specified for concat doesn't match input dimensionality");t<0&&(t=r.length+t);let n=r.slice(0);for(let B=1;BB.dims),c=Gt(s),d=new Array(f.length-1);d[0]=f[0][t];for(let B=1;B= ${d[B-1]}) { + return getChannel( + getX${B}(${Wr(c,m,Q)}), + vec2(${Wr(y,m,Q)})); + }`}let _=d.length,w=d[d.length-1];T+=` + return getChannel( + getX${_}(${Wr(c,m,w)}), + vec2(${Wr(y,m,w)}));`;let v=D(i.session.backend.glContext.version),L=` + ${l} + float getValue(${c.map(B=>"int "+B)}) { + ${T} + } + + void main() { + ${u} coords = getOutputCoords(); + int lastDim = coords.${c[s-1]}; + coords.${c[s-1]} = coords.${c[s-2]}; + coords.${c[s-2]} = lastDim; + + vec4 result = vec4(getValue(${a}), 0., 0., 0.); + + ${a[s-1]} = ${a[s-1]} + 1; + if (${a[s-1]} < ${n[s-1]}) { + result.g = getValue(${a}); + } + + ${a[s-2]} = ${a[s-2]} + 1; + if (${a[s-2]} < ${n[s-2]}) { + result.a = getValue(${a}); + } + + ${a[s-1]} = ${a[s-1]} - 1; + if (${a[s-2]} < ${n[s-2]} && + ${a[s-1]} < ${n[s-1]}) { + result.b = getValue(${a}); + } + ${v.output} = result; + } + `;return{...e,output:{dims:n,type:o[0].type,textureType:2},shaderSource:L,hasMain:!0}},Rs=(i,e,o)=>{let t=Op(e.length,o.cacheKey);return{...t,get:()=>Ap(i,t,e,o.axis)}},Wr=(i,e,o)=>{let t=i.indexOf(e);return i.map((n,s)=>s===t?`${n} - ${o}`:n).join()}});var Ms,Pp,Ep,Dp,Vs,Lp,$p,kp,Us,Bp,zs=x(()=>{"use strict";at();R();Gs();Ms=(i,e,o)=>(Bp(e),i.session.pack&&e[0].dims.length>1?[i.run(Rs(i,e,o),e)]:[i.run(Dp(i,e,o),e)]),Pp=(i,e)=>({name:"Concat",inputNames:Array.from({length:i},(o,t)=>`X${t}`),inputTypes:Array(i).fill(0),cacheHint:e}),Ep=(i,e,o,t)=>{let r=o[0].dims.slice();if(t>=r.length||t<-1*r.length)throw new Error("axis specified for concat doesn't match input dimensionality");t<0&&(t=r.length+t);let n=r.slice(0);for(let m=1;m{let t=Pp(e.length,o.cacheKey);return{...t,get:()=>Ep(i,t,e,o.axis)}},Vs=i=>`int getTextureWhereDataResides(int index) { + ${i.map((o,t)=>`if(index<${o}) {return ${t};} +`).join("")} + }`,Lp=i=>Vs(i),$p=(i,e)=>{let o=[`float fetchDataFromCorrectTexture(int textureIndex, int indices[${e}]) {`];for(let t=0;t{let e=["int getSizeInConcatAxisValueFromIndex(int index) {"];for(let o=0;oF({axis:i.attributes.getInt("axis")}),Bp=i=>{if(!i||i.length<1)throw new Error("too few inputs");let e=i[0].type,o=i[0].dims.length;if(e==="string")throw new Error("string tensor is not supported yet");for(let t of i){if(t.type!==e)throw new Error("input tensors should be one type");if(t.dims.length!==o)throw new Error("input tensors should have the same shape")}}});function Fp(){return Et("abs")}function Np(){return Et("acos")}function Cp(){return Et("asin")}function Rp(){return Et("atan")}function Gp(){return Et("ceil")}function Mp(){return Et("cos")}function Vp(i){let e="elu";return{body:` + const float alpha = float(${i}); + + float ${e}_(float a) { + return a >= 0.0 ? a: (exp(a) - 1.0) * alpha; + } + vec4 ${e}_(vec4 v) { + return vec4(${e}_(v.x), ${e}_(v.y), ${e}_(v.z), ${e}_(v.w)); + } + `,name:e,type:0}}function Up(){return Et("exp")}function zp(){return Et("floor")}function ho(i,e){let o="clip";return{body:` + const float min = float(${i}); + const float max = float(${e}); + + float ${o}_(float a) { + return clamp(a, min, max); + } + vec4 ${o}_(vec4 v) { + return clamp(v, min, max); + } + `,name:o,type:0}}function Wp(){let i="indentity";return{body:` + float ${i}_(float a) { + return a; + } + vec4 ${i}_(vec4 v) { + return v; + } + `,name:i,type:0}}function Hp(i){let e="leakyRelu";return{body:` + const float alpha = float(${i}); + + float ${e}_(float a) { + return a < 0.0 ? a * alpha : a; + } + vec4 ${e}_(vec4 v) { + return vec4(${e}_(v.x), ${e}_(v.y), ${e}_(v.z), ${e}_(v.w)); + } + `,name:e,type:0}}function qp(){return Et("log")}function jp(){let i="neg";return{body:` + float ${i}_(float a) { + return -a; + } + vec4 ${i}_(vec4 v) { + return -v; + } + `,name:i,type:0}}function Xp(){let i="not";return{body:` + float ${i}_(float a) { + return float( ! bool(a) ); + } + bool ${i}_(bool a) { + return !a; + } + vec4 ${i}_(vec4 v) { + return vec4(!bool(v.x), !bool(v.y), !bool(v.z), !bool(v.w)); + } + bvec4 ${i}_(bvec4 v) { + return bvec4(!v.x, !v.y, !v.z, !v.w); + } + `,name:i,type:0}}function Kp(){return Et("sin")}function mo(){let i="relu";return{body:` + float ${i}_(float a) { + return max( a, 0.0 ); + } + vec4 ${i}_(vec4 v) { + return max( v, 0.0 ); + } + `,name:i,type:0}}function bo(){let i="sigmoid";return{body:` + float ${i}_(float a) { + return 1.0 / (1.0 + exp(-a)); + } + vec4 ${i}_(vec4 v) { + return 1.0 / (1.0 + exp(-v)); + } + `,name:i,type:0}}function Jp(){return Et("sqrt")}function Zp(){return Et("tan")}function Yp(){let i="tanh";return{body:` + float ${i}_(float a) { + a = clamp(a, -10., 10.); + a = exp(2.*a); + return (a - 1.) / (a + 1.); + } + vec4 ${i}_(vec4 v) { + v = clamp(v, -10., 10.); + v = exp(2.*v); + return (v - 1.) / (v + 1.); + } + `,name:i,type:0}}function Et(i){return{body:` + float ${i}_(float a) { + return ${i}(a); + } + vec4 ${i}_(vec4 v) { + return ${i}(v); + } + `,name:i,type:0}}var Qp,Z,Ws,Hs,qs,js,yo,Xs,Ks,td,Js,Zs,Ys,Qs,tu,eu,go,ru,nu,ou,iu,au,su,uu,lu,fu,cu,pu,xo=x(()=>{"use strict";at();U();Xt();X();R();Qp=(i,e,o,t)=>{let r=i.session.pack?2:0,n=D(i.session.backend.glContext.version);return{...e,output:{dims:o.dims,type:o.type,textureType:r},shaderSource:` + ${t.body} + void main() { + vec4 v = ${n.texture2D}(A, TexCoords); + v = ${t.name}_(v); + ${n.output} = v; + } + `,hasMain:!0}},Z=(i,e,o,t)=>{let r=i.session.pack?2:0,n={name:o.name,inputTypes:[r],inputNames:["A"],cacheHint:t};return{...n,get:()=>Qp(i,n,e,o)}},Ws=(i,e)=>[i.run(Z(i,e[0],Fp()),e)],Hs=(i,e)=>[i.run(Z(i,e[0],Np()),e)],qs=(i,e)=>[i.run(Z(i,e[0],Cp()),e)],js=(i,e)=>[i.run(Z(i,e[0],Rp()),e)],yo=(i,e,o)=>[i.run(Z(i,e[0],ho(o.min,o.max),o.cacheKey),e)],Xs=i=>F({min:i.attributes.getFloat("min",me),max:i.attributes.getFloat("max",be)}),Ks=(i,e)=>{let o=td(i,e);return yo(i,[e[0]],o)},td=(i,e)=>{if(e.length>=3&&(!i.session.isInitializer(e[1].dataId)||!i.session.isInitializer(e[2].dataId)))throw new Error("dynamic clip attributes are not allowed");let o=e.length>=3?e[1].numberData[0]:me,t=e.length>=3?e[2].numberData[0]:be;return F({min:o,max:t})},Js=(i,e)=>[i.run(Z(i,e[0],Gp()),e)],Zs=(i,e)=>[i.run(Z(i,e[0],Mp()),e)],Ys=(i,e,o)=>[i.run(Z(i,e[0],Vp(o.alpha),o.cacheKey),e)],Qs=i=>F({alpha:i.attributes.getFloat("alpha",1)}),tu=(i,e)=>[i.run(Z(i,e[0],Up()),e)],eu=(i,e)=>[i.run(Z(i,e[0],zp()),e)],go=(i,e)=>[i.run(Z(i,e[0],Wp()),e)],ru=(i,e,o)=>[i.run(Z(i,e[0],Hp(o.alpha),o.cacheKey),e)],nu=i=>F({alpha:i.attributes.getFloat("alpha",.01)}),ou=(i,e)=>[i.run(Z(i,e[0],qp()),e)],iu=(i,e)=>[i.run(Z(i,e[0],jp()),e)],au=(i,e)=>[i.run(Z(i,e[0],Xp()),e)],su=(i,e)=>[i.run(Z(i,e[0],mo()),e)],uu=(i,e)=>[i.run(Z(i,e[0],bo()),e)],lu=(i,e)=>[i.run(Z(i,e[0],Kp()),e)],fu=(i,e)=>[i.run(Z(i,e[0],Jp()),e)],cu=(i,e)=>[i.run(Z(i,e[0],Zp()),e)],pu=(i,e)=>[i.run(Z(i,e[0],Yp()),e)]});function zt(i){let e;switch(i.activation){case"Relu":e=mo();break;case"Sigmoid":e=bo();break;case"Clip":e=ho(i.clipMin,i.clipMax);break;default:return{activationFunction:"",applyActivation:""}}let o=e.name,t=e.body,r=`value = ${o}_(value);`;return{activationFunction:t,applyActivation:r}}var Ne,xe=x(()=>{"use strict";U();xo();Ne=i=>{let e=i.getString("activation","");if(e==="Clip"){let[o,t]=i.getFloats("activation_params",[me,be]);return{activation:e,clipMax:t,clipMin:o,activationCacheKey:`${e}:${o},${t}`}}return{activation:e,activationCacheKey:e}}});var rd,nd,du,hu=x(()=>{"use strict";It();X();R();Hr();xe();rd=(i,e)=>({name:"GroupedConv",inputNames:i?["X","W","Bias"]:["X","W"],inputTypes:i?[0,0,0]:[0,0],cacheHint:e}),nd=(i,e,o,t)=>{let n=e.length>2?"value += getBias(output_channel);":"",s=e[0].dims.slice(),a=e[1].dims.slice(),u=a[0]/t.group;z.verbose("GroupedConv",`autpPad:${t.autoPad}, dilations:${t.dilations}, group:${t.group}, kernelShape:${t.kernelShape}, pads:${t.pads}, strides:${t.strides}`);let l=Ce(s,a,t.dilations,t.pads,t.strides),f=D(i.session.backend.glContext.version),{activationFunction:c,applyActivation:d}=zt(t),m=` + const ivec2 strides = ivec2(${t.strides[0]}, ${t.strides[1]}); + const ivec2 pads = ivec2(${t.pads[0]}, ${t.pads[1]}); + ${c} + void main() { + ivec4 coords = getOutputCoords(); + int batch = coords.x; + int output_channel = coords.y; + ivec2 xRCCorner = coords.zw * strides - pads; + int group_id = output_channel / ${u}; + + float value = 0.0; + for (int wInChannel = 0; wInChannel < ${a[1]}; wInChannel++) { + int input_channel = group_id * ${a[1]} + wInChannel; + for (int wHeight = 0; wHeight < ${a[2]}; wHeight++) { + int xHeight = xRCCorner.x + wHeight * ${t.dilations[0]}; + + if (xHeight < 0 || xHeight >= ${s[2]}) { + continue; + } + + for (int wWidth = 0; wWidth < ${a[3]}; wWidth++) { + int xWidth = xRCCorner.y + wWidth * ${t.dilations[1]}; + if (xWidth < 0 || xWidth >= ${s[3]}) { + continue; + } + + float xVal = getX(batch, input_channel, xWidth, xHeight); + float wVal = getW(output_channel, wInChannel, wWidth, wHeight); + value += xVal*wVal; + } + } + } + ${n} + ${d} + ${f.output} = vec4(value, .0, .0, .0); + } +`;return{...o,output:{dims:l,type:e[0].type,textureType:0},shaderSource:m,hasMain:!0}},du=(i,e,o)=>{let t=rd(e.length>2,o.cacheKey);return{...t,get:()=>nd(i,e,t,o)}}});var od,id,mu,bu=x(()=>{"use strict";X();R();ge();od=i=>({name:"Im2Col (packed)",inputNames:["A"],inputTypes:[2],cacheHint:i}),id=(i,e,o,t,r,n)=>{let s=o.dims,a=t.dims,u=2,l=3,f=r.length,c=[a[1]*a[2]*a[3],r[2]*r[3]],d=a[2]*a[3],m=Ut(),y=D(i.session.backend.glContext.version),g="";for(let _=0;_<=1;_++)for(let w=0;w<=1;w++)g+=` + blockIndex = rc.x + ${w}; + pos = rc.y + ${_}; + + if(blockIndex < ${c[1]} && pos < ${c[0]}) { + offsetY = int(blockIndex / (${r[f-1]})) * ${n.strides[0]} - + ${n.pads[0]}; + d0 = offsetY + ${n.dilations[0]} * (imod(pos, ${d}) / ${a[2]}); + + if(d0 < ${s[u]} && d0 >= 0) { + offsetX = imod(blockIndex, ${r[f-1]}) * ${n.strides[1]} - + ${n.pads[1]}; + d1 = offsetX + ${n.dilations[1]} * imod(imod(pos, ${d}), ${a[2]}); + + if(d1 < ${s[l]} && d1 >= 0) { + + ch = int(float(pos)/ ${d}.); + innerDims = vec2(d0, d1); + result[${_*2+w}] = getChannel( + getA(0, ch, int(innerDims.x), + int(innerDims.y)), innerDims); + } + } + } + + `;let T=` + ${m} + + void main() { + ivec2 rc = getOutputCoords(); + vec4 result = vec4(0.0); + int blockIndex, pos, offsetY, d0, offsetX, d1, ch; + vec2 innerDims; + ${g} + ${y.output} = result; + } + `;return{...e,output:{dims:c,type:o.type,textureType:2},shaderSource:T,hasMain:!0}},mu=(i,e,o,t,r)=>{let n=od(r.cacheKey);return{...n,get:()=>id(i,n,e,o,t,r)}}});function sd(i,e,o){let t=e[0].dims,r=e[1].dims,n=dt.calcShape(t,r,!0);if(!n)throw new Error("Can't use matmul on the given tensors");let s=ht(n.length),a=Gt(),{activationFunction:u,applyActivation:l}=zt(o),f=e.length>2,c=f?"value += getBiasForMatmul();":"",d=f?`${wo(s,a,e[2].dims,n,!1)}`:"",m=n.length,y=t.length,g=r.length,T=t[t.length-1],_=` + ${u} + ${d} + float process(int indices[${m}]) { + int a[${y}]; + int b[${g}]; + bcastMatmulIndices_A(indices, a); + bcastMatmulIndices_B(indices, b); + + float value; + for (int k=0; k<${T}; ++k) { + a[${y-1}] = k; + b[${g-2}] = k; + value += _A(a) * _B(b); + } + ${c} + ${l} + return value; + }`;return{...i,output:{dims:n,type:e[0].type,textureType:0},shaderSource:_}}function To(i,e){let o=ad(i.length>2,e.activationCacheKey);return{...o,get:()=>sd(o,i,e)}}function wo(i,e,o,t,r){let n="",s=o.length,a=t.length,u=a-s;a<2&&s>0?n="coords":n=o.map((g,T)=>`coords.${e[T+u]}`).join(", ");let f=dt.getBroadcastDims(o,t).map(g=>`coords.${e[g+u]} = 0;`).join(` +`),d=A.size(o)===1,m="vec4(outputValue.xx, outputValue.yy)";return d&&(m="vec4(outputValue.x)"),r?` +vec4 getBiasForMatmul() { + ${i} coords = getOutputCoords(); + ${f} + vec4 outputValue = getBias(${n}); + return ${m}; +}`:` +float getBiasForMatmul() { + ${i} coords = getOutputCoords(); + ${f} + return getBias(coords.x); +}`}var yu,gu,ad,ud,qr=x(()=>{"use strict";U();R();Vt();xe();vo();yu=(i,e,o)=>(ud(e),i.session.pack?[i.run(jr(i,e,o),e)]:[i.run(To(e,o),e)]),gu=i=>Ne(i.attributes),ad=(i,e)=>({name:"MatMul",inputNames:i?["A","B","Bias"]:["A","B"],inputTypes:i?[0,0,0]:[0,0],cacheHint:e});ud=i=>{if(!i||i.length!==2)throw new Error("MatMul requires 2 inputs.");if(i[0].dims[i[0].dims.length-1]!==i[1].dims[i[1].dims.length-2])throw new Error("shared dimension does not match.");if(i[0].type!=="float32"&&i[0].type!=="float64"||i[1].type!=="float32"&&i[1].type!=="float64")throw new Error("inputs should be float type");if(i[0].type!==i[1].type)throw new Error("inputs types should match")}});function cd(i,e,o,t){let r=[],n=[],s=o[0].dims,a=o[1].dims,u=s.length,l=a.length,f=t.length,c=f-u,d=f-l;r=s.map((v,L)=>`coords.${e[L+c]}`),r[u-1]="i*2",r.join(", "),n=a.map((v,L)=>`coords.${e[L+d]}`),n[l-2]="i*2",n.join(", ");let m=dt.getBroadcastDims(s,t),y=dt.getBroadcastDims(a,t),g=m.map(v=>`coords.${e[v+c]} = 0;`).join(` +`),T=y.map(v=>`coords.${e[v+d]} = 0;`).join(` +`),_=`int lastDim = coords.${e[f-1]}; + coords.${e[f-1]} = coords.${e[f-2]}; + coords.${e[f-2]} = lastDim;`;return` +vec4 getAAtOutCoordsMatmul(int i) { + ${i} coords = getOutputCoords(); + ${_} + ${g} + vec4 outputValue = getA(${r}); + return outputValue; +} + +vec4 getBAtOutCoordsMatmul(int i) { + ${i} coords = getOutputCoords(); + ${_} + ${T} + vec4 outputValue = getB(${n}); + return outputValue; +}`}function pd(i,e){let o="";for(let t=0;t{"use strict";U();X();R();Vt();xe();qr();ld=(i,e)=>({name:"MatMul (packed)",inputNames:i?["A","B","Bias"]:["A","B"],inputTypes:i?[2,2,2]:[2,2],cacheHint:e}),fd=(i,e,o,t)=>{let r=o.length>2,n=r?"value += getBiasForMatmul();":"",s=o[0].dims,a=o[1].dims,u=dt.calcShape(s,a,!0),l=!A.areEqual(o[0].dims,o[1].dims);if(!u)throw new Error("Can't use matmul on the given tensors");let f=s[s.length-1],c=Math.ceil(f/2),d=s.length,m=a.length,y=D(i.session.backend.glContext.version),g=ht(u.length),T=u.length,_=Gt(),{activationFunction:w,applyActivation:v}=zt(t),L=r?`${wo(g,_,o[2].dims,u,!0)}`:"",B=l?`${cd(g,_,o,u)}`:"",Q=l?"getAAtOutCoordsMatmul(i)":`getA(${pd(_,d)})`,pt=l?"getBAtOutCoordsMatmul(i)":`getB(${dd(_,m)})`,Ht=l?"":`${g} rc = + getOutputCoords(); int lastDim = rc.${_[T-1]}; rc.${_[T-1]} = + rc.${_[T-2]}; rc.${_[T-2]} = lastDim; + `,wt=` + ${B} + ${L} + ${w} + void main() { + ${Ht} + + vec4 value = vec4(0); + for (int i = 0; i < ${c}; i++) { + vec4 a = ${Q}; + vec4 b = ${pt}; + + value += (a.rrbb * b.rgrg); + value += (a.ggaa * b.baba); + } + ${n} + ${v} + ${y.output} = value; + }`;return{...e,output:{dims:u,type:o[0].type,textureType:2},shaderSource:wt,hasMain:!0}},jr=(i,e,o)=>{let t=ld(e.length>2,o.activationCacheKey);return{...t,get:()=>fd(i,t,e,o)}}});var xu,Tu=x(()=>{"use strict";Hr();bu();vo();xu=(i,e,o)=>{let t=e[0].dims,r=e[1].dims,n=Ce(t,r,o.dilations,o.pads,o.strides),s=i.run(mu(i,e[0],e[1],n,o),[e[0]]),a=i.reshapePacked(e[1],[r[0],r[1]*r[2]*r[3]]),u=e.length===3?[a,s,e[2]]:[a,s],l=i.run(jr(i,u,o),u);return i.reshapePacked(l,n)}});var hd,md,wu,Io,_o=x(()=>{"use strict";R();hd=i=>({name:"Im2Col",inputNames:["X"],inputTypes:[0],cacheHint:i}),md=(i,e,o,t,r,n)=>{let s=o.dims,a=t.dims,u=r.length,l=Io(s,a,r,4),f=` + const int XC = ${s[1]}; + const int XH = ${s[2]}; + const int XW = ${s[3]}; + const int KH = ${n.kernelShape[0]}; + const int KW = ${n.kernelShape[1]}; + const int dilationH = ${n.dilations[0]}; + const int dilationW = ${n.dilations[1]}; + const int strideH = ${n.strides[0]}; + const int strideW = ${n.strides[1]}; + const int padH = ${n.pads[0]}; + const int padW = ${n.pads[1]}; + const int KHKW = KH*KW; + const int XCKHKW = XC * KHKW; + const int outputChannels = 4; + vec4 process(int indices[${u}]) { + int b = indices[0]; // batch size + int oh = indices[1] * strideH - padH; //output height + int ow = indices[2] * strideW - padW; //output width + int p = indices[3] * outputChannels; //patch + vec4 value = vec4(0.0); + for(int i=0; i < outputChannels; ++i) { + if(p < XCKHKW) { + int patchC = p / KHKW; + int patchH = (p - patchC*KHKW) / KW; + int patchW = (p - patchC*KHKW) - patchH * KW; + int xh2 = oh + patchH * dilationH; + int xw2 = ow + patchW * dilationW; + int x[${s.length}]; + x[0] = b; + x[1] = patchC; + x[2] = xh2; + x[3] = xw2; + if(xh2 >= 0 && + xh2 < XH && + xw2 >= 0 && + xw2 < XW) { + value[i] = _X(x); + } + } + ++p; + } + return value; + } + `;return{...e,output:{dims:l,type:o.type,textureType:4},shaderSource:f}},wu=(i,e,o,t,r)=>{let n=hd(r.cacheKey);return{...n,get:()=>md(i,n,e,o,t,r)}},Io=(i,e,o,t=4)=>[o[0],o[2],o[3],Math.ceil(i[1]*e[2]*e[3]/t)]});var bd,yd,vu,Iu=x(()=>{"use strict";U();X();R();xe();_o();bd=(i,e)=>({name:"ConvDotProduct",inputNames:i?["Im2Col","K","B"]:["Im2Col","K"],inputTypes:i?[0,4,0]:[0,4],cacheKey:e.activationCacheKey}),yd=(i,e,o,t,r)=>{let n=o[0].dims,s=o[1].dims,a=[s[0],Math.ceil(n[1]*s[2]*s[3]/4)],u=Io(n,s,t),[l,f]=i.calculateTextureWidthAndHeight(a,4),c=A.computeStrides(u),[d,m]=i.calculateTextureWidthAndHeight(u,4),y=t.length,g=o.length<3?"0.0":"_B(b)",T=Math.ceil(n[1]*s[2]*s[3]/4),{activationFunction:_,applyActivation:w}=zt(r),v=D(i.session.backend.glContext.version),L=` +${_} +float process(int indices[${y}]) { + int b[1]; + b[0] = indices[1]; + int im2col[4]; + im2col[0] = indices[0]; + im2col[1] = indices[2]; + im2col[2] = indices[3]; + int im2colOffset = im2col[0] * ${c[0]} + im2col[1] * ${c[1]} + im2col[2] * ${c[2]}; + int kernelOffset = indices[1] * ${a[1]}; + float value = ${g}; + for (int i = 0; i < ${T}; ++i) { + vec2 im2colCoords = offsetToCoords(im2colOffset, ${d}, ${m}); + vec2 kernelCoords = offsetToCoords(kernelOffset, ${l}, ${f}); + value += dot(${v.texture2D}(Im2Col, im2colCoords), ${v.texture2D}(K, kernelCoords)); + ++im2colOffset; + ++kernelOffset; + } + ${w} + return value; +}`;return{...e,output:{dims:t,type:o[0].type,textureType:0},shaderSource:L}},vu=(i,e,o,t)=>{let r=bd(e.length>2,t);return{...r,get:()=>yd(i,r,e,o,t)}}});var Ce,So,gd,xd,Td,wd,Oo,vd,Hr=x(()=>{"use strict";at();U();hu();Tu();Iu();xe();_o();qr();Ce=(i,e,o,t,r)=>{let n=i[0],s=i.slice(2),a=s.length,u=e[0],f=e.slice(2).map((y,g)=>y+(y-1)*(o[g]-1)),d=s.map((y,g)=>y+t[g]+t[g+a]).map((y,g)=>Math.floor((y-f[g]+r[g])/r[g]));return[n,u].concat(...d)},So=(i,e,o)=>(vd(e,o),gd(i,e,o)),gd=(i,e,o)=>{let t=wd(o,e),r=i.session.pack,n=t.kernelShape[0]===1&&t.kernelShape[1]===1;return t.group>1?[i.run(du(i,e,t),e)]:n&&r?[xd(i,e,t)]:r&&e[0].dims.length===4&&e[0].dims[0]===1&&!n?[xu(i,e,t)]:[Td(i,e,t)]},xd=(i,e,o)=>{let t=e[0].dims,r=e[1].dims,n=Ce(t,r,o.dilations,o.pads,o.strides),s=i.reshapeUnpacked(e[0],[t[1],t[2]*t[3]]),a=i.reshapeUnpacked(e[1],[r[0],r[1]]),u=e.length>2?[a,s,e[2]]:[a,s],l=i.run(To(u,o),u);return i.reshapeUnpacked(l,n)},Td=(i,e,o)=>{let t=e[0].dims,r=e[1].dims,n=Ce(t,r,o.dilations,o.pads,o.strides),s=i.run(wu(i,e[0],e[1],n,o),[e[0]]),a=e.length===3?[s,e[1],e[2]]:[s,e[1]];return i.run(vu(i,e,n,o),a)},wd=(i,e)=>{let o=i.kernelShape.slice();if(i.kernelShape.length===0)for(let n=2;n{let e=i.attributes,o=Ne(e),t=e.getString("auto_pad","NOTSET"),r=e.getInts("dilations",[1,1]),n=e.getInt("group",1),s=e.getInts("kernel_shape",[]),a=e.getInts("pads",[0,0,0,0]),u=e.getInts("strides",[1,1]);return F({autoPad:t,dilations:r,group:n,kernelShape:s,pads:a,strides:u,...o})},vd=(i,e)=>{if(!i||i.length!==2&&i.length!==3)throw new Error("Conv requires 2 or 3 inputs");if(i[0].dims.length!==4||i[1].dims.length!==4)throw new Error("currently only support 2-dimensional conv");let o=i[0].dims[1],t=i[1].dims[1]*e.group;if(o!==t)throw new Error("FILTER_IN_CHANNEL should be equal to DATA_CHANNEL");if(i.length===3&&(i[2].dims.length!==1||i[1].dims[0]!==i[2].dims[0]))throw new Error("invalid bias");let r=i[0].dims.length-2;if(e.dilations.length!==r)throw new Error(`dilations should be ${r}D`);if(e.strides.length!==r)throw new Error(`strides should be ${r}D`);if(e.pads.length!==r*2)throw new Error(`pads should be ${r*2}D`);if(e.kernelShape.length!==0&&e.kernelShape.length!==i[1].dims.length-2)throw new Error("invalid kernel shape");if(i[0].type!=="float32"||i[1].type!=="float32")throw new Error("Conv input(X,W) should be float tensor");if(i.length===3&&i[2].type!=="float32")throw new Error("Conv input(bias) should be float tensor")}});var Id,_d,Sd,_u,Od,Ad,Pd,Ed,Dd,Ld,Su,$d,Ou=x(()=>{"use strict";at();X();R();xe();Id=(i,e,o,t,r,n)=>(i-1)*e+o+(t-1)*r+1-n,_d=(i,e,o,t,r)=>{let n=Math.floor(i/2);e==="SAME_UPPER"?(o[t]=n,o[r]=i-n):e==="SAME_LOWER"&&(o[t]=i-n,o[r]=n)},Sd=(i,e,o,t,r,n,s,a)=>{let u=i.length-2,l=a.length===0;for(let f=0;f($d(e,o),Od(i,e,o)),Od=(i,e,o)=>{let t=Ld(o,e);return[Dd(i,e,t)]},Ad=(i,e)=>({name:"ConvTranspose",inputNames:i?["X","W","B"]:["X","W"],inputTypes:i?[0,0,0]:[0,0],cacheHint:e}),Pd=(i,e,o,t)=>{let n=e.length>2?"getB(output_channel)":"0.0",s=e[0].dims,a=e[1].dims,u=a[1],l=a[0]/t.group,f=[e[0].dims[0],e[1].dims[1]*t.group,...t.outputShape],c=D(i.session.backend.glContext.version),{activationFunction:d,applyActivation:m}=zt(t),y=` + const ivec2 strides = ivec2(${t.strides[0]}, ${t.strides[1]}); + const ivec2 pads = ivec2(${t.pads[0]}, ${t.pads[1]}); + ${d} + void main() { + ivec4 coords = getOutputCoords(); + int batch = coords.x; + int output_channel = coords.y; + + ivec2 loc = coords.zw + pads; + + int group_id = output_channel / ${u}; + int wOutChannel = output_channel - group_id * ${u}; + + float value = ${n}; + for (int inChannelOffset = 0; inChannelOffset < ${l}; inChannelOffset++) { + int input_channel = group_id * ${l} + inChannelOffset; + for (int wWOff = 0; wWOff < ${a[2]}; wWOff++) { + for (int wHOff = 0; wHOff < ${a[3]}; wHOff++) { + ivec2 wOff = ivec2(wWOff * ${t.dilations[0]}, wHOff * ${t.dilations[1]}); + ivec2 wLoc = loc - wOff; + ivec2 wLocIn = wLoc / strides; + if ( + wLocIn * strides == wLoc && + wLocIn.x >= 0 && wLocIn.x < ${s[2]} && + wLocIn.y >= 0 && wLocIn.y < ${s[3]} + ) { + float xVal = getX(batch, input_channel, wLocIn.y, wLocIn.x); + float wVal = getW(input_channel, wOutChannel, wHOff, wWOff); + value += xVal * wVal; + } + } + } + } + ${m} + ${c.output} = vec4(value, .0, .0, .0); + } +`;return{...o,output:{dims:f,type:e[0].type,textureType:0},shaderSource:y,hasMain:!0}},Ed=(i,e,o)=>{let t=Ad(e.length>2,o.cacheKey);return{...t,get:()=>Pd(i,e,t,o)}},Dd=(i,e,o)=>i.run(Ed(i,e,o),e),Ld=(i,e)=>{let o=i.kernelShape.slice();if(i.kernelShape.length===0)for(let a=2;a{let e=i.attributes,o=Ne(e),t=e.getString("auto_pad","NOTSET"),r=e.getInts("dilations",[1,1]),n=e.getInt("group",1),s=e.getInts("kernel_shape",[]),a=e.getInts("output_padding",[0,0]),u=e.getInts("output_shape",[]),l=e.getInts("pads",[0,0,0,0]),f=e.getInts("strides",[1,1]);return F({autoPad:t,dilations:r,group:n,kernelShape:s,outputPadding:a,outputShape:u,pads:l,strides:f,...o})},$d=(i,e)=>{if(!i||i.length!==2&&i.length!==3)throw new Error("Conv requires 2 or 3 inputs");if(i[0].dims.length!==4||i[1].dims.length!==4)throw new Error("currently only support 2-dimensional conv");let o=i[0].dims[1],t=i[1].dims[0];if(o!==t)throw new Error("FILTER_IN_CHANNEL should be equal to DATA_CHANNEL");let r=i[1].dims[1]*e.group;if(i.length===3&&(i[2].dims.length!==1||i[2].dims[0]!==r))throw new Error("invalid bias");let n=i[0].dims.length-2;if(e.dilations.length!==n)throw new Error(`dilations should be ${n}D`);if(e.strides.length!==n)throw new Error(`strides should be ${n}D`);if(e.pads.length!==n*2)throw new Error(`pads should be ${n*2}D`);if(e.outputPadding.length!==n)throw new Error(`output_padding should be ${n}D`);if(e.kernelShape.length!==0&&e.kernelShape.length!==i[1].dims.length-2)throw new Error("invalid kernel shape");if(e.outputShape.length!==0&&e.outputShape.length!==i[0].dims.length-2)throw new Error("invalid output shape");if(i[0].type!=="float32"||i[1].type!=="float32")throw new Error("ConvTranspose input(X,W) should be float tensor");if(i.length===3&&i[2].type!=="float32")throw new Error("ConvTranspose input(bias) should be float tensor")}});var Au,Te,Pu,kd,Eu,Bd,Fd,Nd,Xr=x(()=>{"use strict";at();U();R();Au={name:"Transpose",inputNames:["A"],inputTypes:[0]},Te=(i,e,o)=>(Nd(e),[i.run({...Au,cacheHint:o.cacheKey,get:()=>kd(i,e[0],o.perm)},e)]),Pu=i=>F({perm:i.attributes.getInts("perm",[])}),kd=(i,e,o)=>{let t=e.dims;o=Eu(t,o);let r=Bd(t,o),n=t.length,s=` + ${Fd("perm",o,n)} + float process(int indices[${n}]) { + int a[${n}]; + perm(a, indices); + return _A(a); + }`;return{...Au,output:{dims:r,type:e.type,textureType:0},shaderSource:s}},Eu=(i,e)=>(e&&e.length!==i.length&&(e=[...i.keys()].reverse()),e),Bd=(i,e)=>(e=Eu(i,e),A.sortBasedOnPerm(i,e)),Fd=(i,e,o)=>{let t=[];t.push(`void ${i}(out int a[${o}], int src[${o}]) {`);for(let r=0;r{if(!i||i.length!==1)throw new Error("Transpose requires 1 input.");if(i[0].type!=="float32"&&i[0].type!=="float64")throw new Error("input should be float tensor")}});var Du,Lu,Cd,$u=x(()=>{"use strict";Xr();Du=(i,e,o)=>{Cd(e);let t=o.blocksize,r=t*t,n=o.mode==="DCR"?[0,3,4,1,5,2]:[0,1,4,2,5,3],s=o.mode==="DCR"?[e[0].dims[0],t,t,e[0].dims[1]/r,e[0].dims[2],e[0].dims[3]]:[e[0].dims[0],e[0].dims[1]/r,t,t,e[0].dims[2],e[0].dims[3]],a=i.reshapeUnpacked(e[0],s),u={perm:n,cacheKey:`${n}`},[l]=Te(i,[a],u),f=[e[0].dims[0],e[0].dims[1]/r,e[0].dims[2]*t,e[0].dims[3]*t];return[i.reshapeUnpacked(l,f)]},Lu=i=>{let e=i.attributes.getInt("blocksize");if(e<1)throw new Error(`blocksize must be >= 1, but got : ${e} for DepthToSpace`);let o=i.attributes.getString("mode","DCR");if(o!=="DCR"&&o!=="CRD")throw new Error(`unrecognized mode: ${o} for DepthToSpace`);return{mode:o,blocksize:e}},Cd=i=>{if(i.length!==1)throw new Error(`DepthToSpace expect 1 inputs, but got ${i.length}`);if(i[0].type==="string"||i[0].dims.length!==4)throw new TypeError("DepthToSpace input should be a 4-D numeric tensor")}});var ku,Bu,Rd,Fu=x(()=>{"use strict";U();ku=(i,e,o)=>{Rd(e,o);let t=A.flattenShape(e[0].dims,o);return[i.reshapeUnpacked(e[0],t)]},Bu=i=>i.attributes.getInt("axis",1),Rd=(i,e)=>{if(!i||i.length!==1)throw new Error("Flatten requires 1 input.");let o=i[0].dims.length;if(o===0)throw new Error("scalar tensor is not supported.");if(e<-o||e>o)throw new Error("Invalid axis");if(i[0].type==="string")throw new Error("string tensor is not supported.")}});var re,nr=x(()=>{"use strict";re=["float32","float64","int32","int16","int8","uint16","uint32","uint8"]});var Nu,Cu,Gd,Md,Vd,Ud,Ru=x(()=>{"use strict";at();nr();U();R();Nu=(i,e,o)=>(Ud(e,o.axis),[i.run(Vd(i,e,o),e)]),Cu=i=>F({axis:i.attributes.getInt("axis",0)}),Gd={name:"Gather",inputNames:["A","B"],inputTypes:[0,0]},Md=(i,e,o,t)=>{let r=o[0].dims.slice(),n=o[1].dims.slice(),s=new Array(r.length+n.length-1);t=A.normalizeAxis(t,r.length);let a=[];for(let d=0;d{let t={...Gd,cacheHint:o.cacheKey};return{...t,get:()=>Md(i,t,e,o.axis)}},Ud=(i,e)=>{if(!i||i.length!==2)throw new Error("Gather requires 2 inputs.");let o=i[0].dims.length;if(o<1)throw new Error("Invalid input shape.");if(e<-o||e>o-1)throw new Error("Invalid axis.");if(re.indexOf(i[0].type)===-1)throw new Error("Invaid input type.");if(i[1].type!=="int32"&&i[1].type!=="int16")throw new Error("Invaid input type.")}});var Ao,Gu,Mu,Vu,zd,Wd,Hd,Uu=x(()=>{"use strict";at();U();R();Ao=(i,e,o)=>(Hd(e,o),[i.run(zd(e,o),e)]),Gu=(i,e)=>{let o=i.attributes.getInt("transA",0)!==0,t=i.attributes.getInt("transB",0)!==0,r=i.attributes.getFloat("alpha",1),n=i.attributes.getFloat("beta",1);return F({transA:o,transB:t,alpha:r,beta:n,isOptionalC:e})},Mu=i=>Gu(i,!1),Vu=i=>Gu(i,!0),zd=(i,e)=>{let o={name:"Gemm",inputNames:i.length===3?["A","B","C"]:["A","B"],inputTypes:i.length===3?[0,0,0]:[0,0],key:e.cacheKey};return{...o,get:()=>Wd(o,i,e)}},Wd=(i,e,o)=>{let t=e[0].dims.slice(),r=e[1].dims.slice(),[n,s]=Cr.getShapeOfGemmResult(t,o.transA,r,o.transB,e.length===3?e[2].dims:void 0),a=[n,s];if(!a)throw new Error("Can't use gemm on the given tensors");let u=t[t.length-1],l="";o.transA&&(u=t[0]),o.transA&&o.transB?l="value += _A_T(a) * _B_T(b);":o.transA&&!o.transB?l="value += _A_T(a) * _B(b);":!o.transA&&o.transB?l="value += _A(a) * _B_T(b);":!o.transA&&!o.transB&&(l="value += _A(a) * _B(b);");let f=a.length,c=e.length===3?`int c[${e[2].dims.length}];`:"",d=e.length===3?"bcastIndices_C(indices, c);":"",m=e.length===3?"value += beta * _C(c);":"",y=` + float process(int indices[${f}]) { + int a[${f}]; + int b[${f}]; + ${c} + + copyVec(indices, a); + copyVec(indices, b); + ${d} + + float value = 0.0; + for (int k=0; k<${u}; ++k) { + a[${f-1}] = k; + b[${f-2}] = k; + ${l} + } + + value = value * alpha; + ${m} + return value; + }`;return{...i,output:{dims:a,type:e[0].type,textureType:0},variables:[{name:"alpha",type:"float",data:o.alpha},{name:"beta",type:"float",data:o.beta}],shaderSource:y}},Hd=(i,e)=>{if(!i)throw new Error("Input is missing");if(e.isOptionalC&&(i.length<2||i.length>3))throw new Error("Invaid input shape.");if(!e.isOptionalC&&i.length!==3)throw new Error("Gemm requires 3 inputs");if(i.length===3&&i[2].dims.length!==1&&i[2].dims.length!==2)throw new Error("Invalid input shape of C");if(i[0].type!=="float32"&&i[0].type!=="float64"||i[1].type!=="float32"&&i[1].type!=="float64"||i.length===3&&i[2].type!=="float32"&&i[2].type!=="float64")throw new Error("Invalid input type.");if(i[0].type!==i[1].type||i.length===3&&i[0].type!==i[2].type)throw new Error("Input types are mismatched")}});var zu,Wu,qd,jd,Xd,Kd,Jd,Hu=x(()=>{"use strict";at();R();zu=(i,e,o)=>(Jd(e),[i.run(Xd(i,e,o),e)]),Wu=i=>{let e=i.attributes.getFloat("scale"),o=i.attributes.getFloats("bias");return F({scale:e,bias:o})},qd={name:"ImageScaler",inputNames:["X"],inputTypes:[0]},jd=(i,e,o,t)=>{let r=o[0].dims.slice(),n=r.length,a=` + ${Kd(t.bias.length)} + float process(int indices[${n}]) { + return _X(indices) * scale + getBias(bias, indices[1]); + }`;return{...e,output:{dims:r,type:o[0].type,textureType:0},variables:[{name:"bias",type:"float",arrayLength:t.bias.length,data:t.bias},{name:"scale",type:"float",data:t.scale}],shaderSource:a}},Xd=(i,e,o)=>{let t={...qd,cacheHint:o.cacheKey};return{...t,get:()=>jd(i,t,e,o)}},Kd=i=>{let e=[`float getBias(float bias[${i}], int channel) {`];for(let o=0;o{if(!i||i.length!==1)throw new Error("ImageScaler requires 1 input.");if(i[0].dims.length!==4)throw new Error("Invalid input shape.");if(i[0].type!=="float32"&&i[0].type!=="float64")throw new Error("Invalid input type.")}});var ju,Xu,qu,Zd,Yd,Qd,th,eh,rh,Ku=x(()=>{"use strict";X();R();ju=(i,e,o)=>{rh(e);let t=i.run(Yd(e[0]),e);return[i.run(eh(i,e[0],o,t.dims),[e[0],t,e[1],e[2]])]},Xu=i=>i.attributes.getFloat("epsilon",1e-5),qu={name:"InstanceNormalization_MeanAndVariance",inputNames:["X"],inputTypes:[0]},Zd=(i,e)=>{let o=e.dims.slice(),t=o[1],r=o[2]*o[3],n=[o[0],t],s=` + vec4 process(int[2] indices) { + vec4 v = vec4(0.0); + int a[4]; + a[0] = indices[0]; + a[1] = indices[1]; + float temp = 0.0; + for(int a2=0; a2<${o[2]}; a2++) { + a[2] = a2; + for(int a3=0; a3<${o[3]}; a3++) { + a[3] = a3; + float x = _X(a); + temp += x; + } + } + float mean = temp / float(${r}); + temp = 0.0; + for(int a2=0; a2<${o[2]}; a2++) { + a[2] = a2; + for(int a3=0; a3<${o[3]}; a3++) { + a[3] = a3; + float x = _X(a); + temp += (x - mean) * (x - mean); + } + } + v.r = mean; + v.g = temp / float(${r}); + + return v; + }`;return{...i,output:{dims:n,type:e.type,textureType:4},shaderSource:s}},Yd=i=>({...qu,get:()=>Zd(qu,i)}),Qd={name:"InstanceNormalization_ComputeOutput",inputNames:["X","MeanAndVariance","Scale","B"],inputTypes:[0,4,0,0]},th=(i,e,o,t,r)=>{let n=D(i.session.backend.glContext.version),[s,a]=i.calculateTextureWidthAndHeight(r,4),[u,l]=[s/4,a],f=` + vec4 get_MeanAndVariance(int[2] mv) { + int offset = indicesToOffset_MeanAndVariance(mv); + vec2 coords = offsetToCoords(offset, ${u}, ${l}); + return ${n.texture2D}(MeanAndVariance, coords); + } + + float process(int[4] indices) { + int mv[2]; + mv[0] = indices[0]; + mv[1] = indices[1]; + vec4 mean_and_variance = get_MeanAndVariance(mv); + float mean = mean_and_variance.r; + float variance = mean_and_variance.g; + + int sb[1]; + sb[0] = indices[1]; + float scale = _Scale(sb); + float b = _B(sb); + + return scale * (_X(indices) - mean) / sqrt(variance + epsilon) + b; + }`;return{...e,output:{dims:o.dims,type:o.type,textureType:0},variables:[{name:"epsilon",type:"float",data:t}],shaderSource:f}},eh=(i,e,o,t)=>{let r={...Qd,cacheHint:`${o}`};return{...r,get:()=>th(i,r,e,o,t)}},rh=i=>{if(!i||i.length!==3)throw new Error("InstanceNormalization requires 3 inputs.");let e=i[0],o=i[1],t=i[2];if(e.dims.length<3||o.dims.length!==1||t.dims.length!==1)throw new Error("Invalid input shape.");if(o.dims[0]!==e.dims[1]||t.dims[0]!==e.dims[1])throw new Error("Input shapes are mismatched.");if(e.type!=="float32"&&e.type!=="float64"||o.type!=="float32"&&o.type!=="float64"||t.type!=="float32"&&t.type!=="float64")throw new Error("Invalid input type.");if(i[0].dims.length!==4)throw new Error("Only support 4-D input shape.")}});function nh(i,e){let o=i[0].dims[1],t=i[0].dims.length,r=-Math.floor((e.size-1)/2),n=Math.ceil((e.size-1)/2),s=`float(${e.alpha}) / float(${e.size})`,a=`float(${e.bias})`,u=`float(${e.beta})`,l=` + float process(int indices[${t}]) { + int c = indices[1]; + float x = _X(indices); + float square_sum = 0.0; + + for (int i = ${r}; i <= ${n}; i++) { + int idx = c + i; + if (c >= 0 && c < ${o}) { + indices[1] = idx; + float j = _X(indices); + square_sum += j * j; + } + } + return x / pow(${a} + ${s} * square_sum, ${u}); + }`;return{...Yu,cacheHint:e.cacheKey,output:{dims:i[0].dims,type:i[0].type,textureType:0},shaderSource:l}}function oh(i,e){return{...Yu,cacheHint:e.cacheKey,get:()=>nh(i,e)}}var Ju,Zu,Yu,ih,Qu=x(()=>{"use strict";at();R();Ju=(i,e,o)=>(ih(e),[i.run(oh(e,o),e)]),Zu=i=>{let e=i.attributes.getFloat("alpha",1e-4),o=i.attributes.getFloat("beta",.75),t=i.attributes.getFloat("bias",1),r=i.attributes.getInt("size");return F({alpha:e,beta:o,bias:t,size:r})},Yu={name:"LRN",inputNames:["X"],inputTypes:[0]};ih=i=>{if(!i||i.length!==1)throw new Error("LRN requires 1 input.");if(i[0].dims.length!==4)throw new Error('currently only support LRN for input with "NCHW" format');if(i[0].type!=="float32")throw new Error("input should be float type")}});var ah,Po,tl,el,rl,sh,uh,lh,fh,ch,ph,dh,hh,nl=x(()=>{"use strict";at();U();X();R();ah={name:"Pad",inputNames:["A"],inputTypes:[0]},Po=(i,e,o)=>(lh(e),[i.run({...ah,cacheHint:o.cacheKey,get:()=>uh(i,e[0],o)},e)]),tl=i=>{let e=i.attributes.getString("mode","constant"),o=i.attributes.getFloat("value",0),t=i.attributes.getInts("pads");return F({mode:e,value:o,pads:t})},el=(i,e,o)=>{fh(e);let t=sh(i,e,o);return Po(i,[e[0]],t)},rl=i=>i.attributes.getString("mode","constant"),sh=(i,e,o)=>{if(!i.session.isInitializer(e[1].dataId)||e.length>=3&&!i.session.isInitializer(e[2].dataId))throw new Error("dynamic pad attributes are not allowed");let t=Array.from(e[1].integerData),r=e.length>=3?e[2].floatData[0]:0;return F({mode:o,pads:t,value:r})},uh=(i,e,o)=>{let t=A.padShape(e.dims.slice(),o.pads),r=t.length,s=` + ${ch(i,e,o)} + float process(int[${r}] indices) { + return padA(indices); + }`;return{name:"Pad",inputNames:["A"],inputTypes:[0],output:{dims:t,type:e.type,textureType:0},shaderSource:s}},lh=i=>{if(!i||i.length!==1)throw new Error("Pad requires 1 input");if(i[0].type!=="float32"&&i[0].type!=="float64")throw new Error("Invalid input type.")},fh=i=>{if(!i||i.length!==2&&i.length!==3)throw new Error("Pad requires 2 or 3 inputs");if(i[1].type!=="int32")throw new Error("Invalid input type.");if(i.length>=3&&i[2].type==="string")throw new Error("Invalid input type.")},ch=(i,e,o)=>{let t=D(i.session.backend.glContext.version),[r,n]=i.calculateTextureWidthAndHeight(e.dims,0),s=A.computeStrides(e.dims);switch(o.mode){case"constant":return ph(t,e.dims,s,r,n,o.pads,o.value);case"reflect":return dh(t,e.dims,s,r,n,o.pads);case"edge":return hh(t,e.dims,s,r,n,o.pads);default:throw new Error("Invalid mode")}},ph=(i,e,o,t,r,n,s)=>{let a=e.length,u="";for(let l=a-1;l>=0;--l)u+=` + k = m[${l}] - ${n[l]}; + if (k < 0) return constant; + if (k >= ${e[l]}) return constant; + offset += k * ${o[l]}; + `;return` + float padA(int m[${a}]) { + const float constant = float(${s}); + int offset = 0; + int k = 0; + ${u} + vec2 coords = offsetToCoords(offset, ${t}, ${r}); + float value = getColorAsFloat(${i.texture2D}(A, coords)); + return value; + } + `},dh=(i,e,o,t,r,n)=>{let s=e.length,a="";for(let u=s-1;u>=0;--u)a+=` + k = m[${u}] - ${n[u]}; + if (k < 0) { k = -k; } + { + const int _2n_1 = ${2*(e[u]-1)}; + k = int( mod( float(k), float(_2n_1) ) ) ; + if(k >= ${e[u]}) { k = _2n_1 - k; } + } + offset += k * ${o[u]}; + `;return` + float padA(int m[${s}]) { + int offset = 0; + int k = 0; + ${a} + vec2 coords = offsetToCoords(offset, ${t}, ${r}); + float value = getColorAsFloat(${i.texture2D}(A, coords)); + return value; + } + `},hh=(i,e,o,t,r,n)=>{let s=e.length,a="";for(let u=s-1;u>=0;--u)a+=` + k = m[${u}] - ${n[u]}; + if (k < 0) k = 0; + if (k >= ${e[u]}) k = ${e[u]-1}; + offset += k * ${o[u]}; + `;return` + float padA(int m[${s}]) { + int offset = 0; + int k = 0; + ${a} + vec2 coords = offsetToCoords(offset, ${t}, ${r}); + float value = getColorAsFloat(${i.texture2D}(A, coords)); + return value; + } + `}});var il,al,sl,ul,ll,fl,cl,pl,dl,mh,ol,hl,Jr,ml,Kr,bh,bl=x(()=>{"use strict";at();U();R();il=(i,e,o)=>{Jr(e);let t={name:"AveragePool",inputNames:["X"],inputTypes:[0],cacheHint:o.cacheKey};return[i.run({...t,get:()=>sl(e,t,!1,o)},e)]},al=i=>{let e=i.attributes.getString("auto_pad","NOTSET"),o=i.attributes.getInt("ceil_mode",0),t=i.attributes.getInt("count_include_pad",0)!==0,r=i.attributes.getInts("kernel_shape"),n=i.attributes.getInts("strides",[]),s=i.attributes.getInts("pads",[]);if(o!==0)throw new Error("using ceil() in shape computation is not yet supported for AveragePool");return F({autoPad:e,ceilMode:o,countIncludePad:t,kernelShape:r,strides:n,pads:s})},sl=(i,e,o,t)=>{let[r,n]=dl(i,t,o),s=A.size(r.kernelShape),a="value += _X(x);",u="";r.countIncludePad?u+=`value /= float(${s});`:u+=`value /= float(${s} - pad);`;let f=` + ${ml(i[0].dims,r,a,u,"0.0")} + `;return{...e,output:{dims:n,type:i[0].type,textureType:0},shaderSource:f}},ul=(i,e,o)=>{Jr(e);let t={name:"GlobalAveragePool",inputNames:["X"],inputTypes:[0],cacheHint:`${o.countIncludePad}`};return[i.run({...t,get:()=>sl(e,t,!0,o)},e)]},ll=i=>{let e=i.attributes.getInt("count_include_pad",0)!==0;return F({autoPad:"",ceilMode:0,countIncludePad:e,kernelShape:[],strides:[],pads:[]})},fl=(i,e,o)=>{Jr(e);let t={name:"MaxPool",inputNames:["X"],inputTypes:[0],cacheHint:o.cacheKey};return[i.run({...t,get:()=>pl(e,t,!1,o)},e)]},cl=i=>{let e=i.attributes.getString("auto_pad","NOTSET"),o=i.attributes.getInt("ceil_mode",0),t=i.attributes.getInts("kernel_shape"),r=i.attributes.getInts("strides",[]),n=i.attributes.getInts("pads",[]),s=i.attributes.getInt("storage_order",0),a=i.attributes.getInts("dilations",[]);if(s!==0)throw new Error("column major storage order is not yet supported for MaxPool");if(o!==0)throw new Error("using ceil() in shape computation is not yet supported for MaxPool");return F({autoPad:e,ceilMode:o,countIncludePad:!1,kernelShape:t,strides:r,pads:n,storageOrder:s,dilations:a})},pl=(i,e,o,t)=>{let[r,n]=dl(i,t,o),s=` + value = max(_X(x), value); + `,a="",l=` + ${ml(i[0].dims,r,s,a,"-1e5")} + `;return{...e,output:{dims:n,type:i[0].type,textureType:0},shaderSource:l}},dl=(i,e,o)=>{let t=i[0].dims.slice(),r=Object.hasOwnProperty.call(e,"dilations"),n=e.kernelShape.slice(),s=e.strides.slice(),a=r?e.dilations.slice():[],u=e.pads.slice();he.adjustPoolAttributes(o,t,n,s,a,u);let l=he.computePoolOutputShape(o,t,s,a,n,u,e.autoPad),f=Object.assign({},e);return r?Object.assign(f,{kernelShape:n,strides:s,pads:u,dilations:a,cacheKey:e.cacheKey}):Object.assign(f,{kernelShape:n,strides:s,pads:u,cacheKey:e.cacheKey}),[f,l]},mh={autoPad:"",ceilMode:0,countIncludePad:!1,kernelShape:[],strides:[],pads:[],storageOrder:0,dilations:[],cacheKey:""},ol={name:"GlobalMaxPool",inputNames:["X"],inputTypes:[0]},hl=(i,e)=>(Jr(e),[i.run({...ol,get:()=>pl(e,ol,!0,mh)},e)]),Jr=i=>{if(!i||i.length!==1)throw new Error("Pool ops requires 1 input.");if(i[0].type!=="float32"&&i[0].type!=="float64")throw new Error("Invalid input type.")},ml=(i,e,o,t,r)=>{let n=i.length;if(e.kernelShape.length<=2){let s=e.kernelShape[e.kernelShape.length-1],a=e.strides[e.strides.length-1],u=e.pads[e.pads.length/2-1],l=e.pads[e.pads.length-1],f=i[n-1],c="",d="",m="";if(u+l!==0?c=` + for (int i = 0; i < ${s}; i++) { + x[${n} - 1] = indices[${n} - 1] * ${a} - ${u} + i; + if (x[${n} - 1] < 0 || x[${n} - 1] >= ${f}) { + pad++; + continue; + } + ${o} + }`:c=` + for (int i = 0; i < ${s}; i++) { + x[${n} - 1] = indices[${n} - 1] * ${a} - ${u} + i; + ${o} + }`,e.kernelShape.length===2){let g=e.kernelShape[e.kernelShape.length-2],T=e.strides[e.strides.length-2],_=e.pads[e.pads.length/2-2],w=e.pads[e.pads.length-2],v=i[n-2];_+w!==0?d=` + for (int j = 0; j < ${g}; j++) { + x[${n} - 2] = indices[${n} - 2] * ${T} - ${_} + j; + if (x[${n} - 2] < 0 || x[${n} - 2] >= ${v}) { + pad+= ${s}; + continue; + } + `:d=` + for (int j = 0; j < ${g}; j++) { + x[${n} - 2] = indices[${n} - 2] * ${T} - ${_} + j; + `,m=` + } + `}return` + float process(int indices[${n}]) { + int x[${n}]; + copyVec(indices, x); + + float value = ${r}; + int pad = 0; + ${d} + ${c} + ${m} + ${t} + return value; + } + `}else{let s=A.size(e.kernelShape),a=A.computeStrides(e.kernelShape),u=a.length,l=e.pads.length,f=bh(u),c=Kr(i,"inputDims"),d=Kr(e.pads,"pads"),m=Kr(a,"kernelStrides"),y=Kr(e.strides,"strides"),g=e.pads.reduce((w,v)=>w+v),T="";return g?T=` + if (x[j] >= inputDims[j] || x[j] < 0) { + pad++; + isPad = true; + break; + } + } + if (!isPad) { + ${o} + }`:T=` + } + ${o} + `,` + ${f} + float process(int indices[${n}]) { + int x[${n}]; + copyVec(indices, x); + int offset[${u}]; + int pads[${l}]; + int inputDims[${n}]; + int kernelStrides[${u}]; + int strides[${u}]; + ${d} + ${c} + ${y} + ${m} + + float value = ${r}; + int pad = 0; + bool isPad = false; + for (int i = 0; i < ${s}; i++) { + offsetToIndices(i, kernelStrides, offset); + isPad = false; + for (int j = ${n} - ${u}; j < ${n}; j++) { + x[j] = indices[j] * strides[j - ${n} + ${u}] + + offset[j - ${n} + ${u}] - pads[j - 2]; + ${T} + } + ${t} + + return value; + } + `}},Kr=(i,e)=>{let o="";for(let t=0;t` + void offsetToIndices(int offset, int[${i}] strides, out int[${i}] indices) { + if (${i} == 0) { + return; + } + for (int i = 0; i < ${i} - 1; ++i) { + indices[i] = offset / strides[i]; + offset -= indices[i] * strides[i]; + } + indices[${i} - 1] = offset; + }`});var we,ne,yh,gh,yl,gl,xl,Tl,wl,vl,Il,_l=x(()=>{"use strict";at();nr();U();R();we=(i,e,o,t,r)=>{gh(e);let n={name:t,inputNames:["A"],inputTypes:[0]};return[i.run({...n,cacheHint:o.cacheKey,get:()=>yh(i,e,o,t,r,n)},e)]},ne=i=>{let e=i.attributes.getInts("axes",[]),o=i.attributes.getInt("keepdims",1)===1;return F({axes:e,keepDims:o})},yh=(i,e,o,t,r,n)=>{let s=[],a=e[0].dims.length||1,u=[],l=A.normalizeAxes(o.axes,e[0].dims.length),f=r(e,l),c=f[1];for(let y=0;y=0||l.length===0?(o.keepDims&&s.push(1),c=` + for(int j${y} = 0; j${y} < ${e[0].dims[y]}; j${y}++) { + inputIdx[${y}] = j${y}; + ${c} + }`):(u.push(`inputIdx[${y}] = outputIdx[${s.length}];`),s.push(e[0].dims[y]));let m=` + float process(int outputIdx[${s.length||1}]) { + float value; // final result + int inputIdx[${a}]; // addressing input data + ${u.join(` +`)} + ${f[0]} // init ops for reduce max/min + ${c} + ${f[2]} // final computation for reduce mean + return value; + }`;return{...n,output:{dims:s,type:e[0].type,textureType:0},shaderSource:m}},gh=i=>{if(!i||i.length!==1)throw new Error("Reduce op requires 1 input.");if(re.indexOf(i[0].type)===-1)throw new Error("Invalid input type.")},yl=(i,e,o)=>we(i,e,o,"ReduceSum",()=>["value = 0.0;","value += _A(inputIdx);",""]),gl=(i,e,o)=>we(i,e,o,"ReduceMean",(r,n)=>{let s=1;for(let a=0;a=0||n.length===0)&&(s*=r[0].dims[a]);return["value = 0.0;","value += _A(inputIdx);",`value /= ${s}.;`]}),xl=(i,e,o)=>we(i,e,o,"ReduceMax",(r,n)=>{let s=[];for(let a=0;a=0||n.length===0)&&s.push(`inputIdx[${a}] = 0;`);return[`${s.join(` +`)} +value = _A(inputIdx);`,"value = max(value, _A(inputIdx));",""]}),Tl=(i,e,o)=>we(i,e,o,"ReduceMin",(r,n)=>{let s=[];for(let a=0;a=0||n.length===0)&&s.push(`inputIdx[${a}] = 0;`);return[`${s.join(` +`)} +value = _A(inputIdx);`,"value = min(value, _A(inputIdx));",""]}),wl=(i,e,o)=>we(i,e,o,"ReduceProd",()=>["value = 1.0;","value *= _A(inputIdx);",""]),vl=(i,e,o)=>we(i,e,o,"ReduceLogSum",()=>["value = 0.0;","value += _A(inputIdx);","value = log(value);"]),Il=(i,e,o)=>we(i,e,o,"ReduceLogSumSquare",()=>["float t; value = 0.0;","t = _A(inputIdx); value += t * t;",""])});var Sl,Ol=x(()=>{"use strict";U();Sl=(i,e)=>{let o=A.calculateReshapedDims(e[0].dims,e[1].integerData);return i.session.pack?[i.reshapePacked(e[0],o)]:[i.reshapeUnpacked(e[0],o)]}});var Al,Eo,Pl,El,or,xh,Do,Zr,Lo=x(()=>{"use strict";at();X();R();Al={name:"Upsample",inputNames:["X"],inputTypes:[0]},Eo=(i,e,o)=>(Do(e,o),[i.run({...Al,cacheHint:o.cacheKey,get:()=>xh(i,e,o)},e)]),Pl=i=>or(i,7),El=i=>or(i,9),or=(i,e)=>{let o=e>=10,t=i.attributes.getString("mode","nearest");if(t!=="nearest"&&t!=="linear"&&(e<11||t!=="cubic"))throw new Error(`unrecognized mode: ${t}`);let r=[];e<9&&(r=i.attributes.getFloats("scales"),Zr(r,t,o));let n=i.attributes.getFloat("extrapolation_value",0),s=e>10?i.attributes.getString("coordinate_transformation_mode","half_pixel"):"asymmetric";if(["asymmetric","pytorch_half_pixel","tf_half_pixel_for_nn","align_corners","tf_crop_and_resize","half_pixel"].indexOf(s)===-1)throw new Error(`coordinate_transform_mode '${s}' is not supported`);let a=s==="tf_crop_and_resize",u=a,l=t==="nearest"&&e>=11?i.attributes.getString("nearest_mode","round_prefer_floor"):"";if(["round_prefer_floor","round_prefer_ceil","floor","ceil",""].indexOf(l)===-1)throw new Error(`nearest_mode '${l}' is not supported`);let f=i.attributes.getFloat("cubic_coeff_a",-.75),c=i.attributes.getInt("exclude_outside",0)!==0;if(c&&t!=="cubic")throw new Error("exclude_outside can be set to 1 only when mode is CUBIC.");let d=e<11?!0:t==="nearest"&&s==="asymmetric"&&l==="floor",m=0,y=0,g=0;return e>10?i.inputs.length>2?(m=1,y=2,g=3):(y=1,g=2):e===9&&(y=1),F({opset:e,isResize:o,mode:t,scales:r,extrapolationValue:n,coordinateTransformMode:s,useExtrapolation:u,needRoiInput:a,nearestMode:l,cubicCoefficientA:f,excludeOutside:c,useNearest2xOptimization:d,roiInputIdx:m,scalesInputIdx:y,sizesInputIdx:g})},xh=(i,e,o)=>{let t=D(i.session.backend.glContext.version),[r,n]=i.calculateTextureWidthAndHeight(e[0].dims,0),s=e[0].dims.map((g,T)=>Math.floor(g*o.scales[T])),[a,u]=i.calculateTextureWidthAndHeight(s,0),l=s.length,f=new Array(l),c=new Array(l),d=` + int output_pitches[${l}]; + int input_pitches[${l}]; + `;for(let g=l-1;g>=0;g--)f[g]=g===l-1?1:f[g+1]*s[g+1],c[g]=g===l-1?1:c[g+1]*e[0].dims[g+1],d+=` + output_pitches[${g}] = ${f[g]}; + input_pitches[${g}] = ${c[g]}; + `;let m=` + float getInputFloat(int index) { + vec2 coords = offsetToCoords(index, ${r}, ${n}); + float value = getColorAsFloat(${t.texture2D}(X, coords)); + return value; + } + `,y=o.mode==="nearest"?` + ${m} + float process(int indices[${l}]) { + int input_index = 0; + int output_index = coordsToOffset(TexCoords, ${a}, ${u}); + + ${d} + + int d, m; + for (int dim = 0; dim < ${l}; ++dim) { + d = output_index / output_pitches[dim]; + m = output_index - d * output_pitches[dim]; + output_index = m; + + if (scales[dim] != 1 && d > 0) { + int d2 = d / scales[dim]; + m = d - d2 * scales[dim]; + d = d2; + } + input_index += input_pitches[dim] * d; + } + + return getInputFloat(input_index); + }`:l===4?` + ${m} + float process(int indices[4]) { + int input_index = 0; + int output_index = coordsToOffset(TexCoords, ${a}, ${u}); + + ${d} + + int m; + int index_of_dim0, index_of_dim1, index_of_dim2, index_of_dim3; + index_of_dim0 = output_index / output_pitches[0]; + m = output_index - index_of_dim0 * output_pitches[0]; + index_of_dim1 = m / output_pitches[1]; + m = m - index_of_dim1 * output_pitches[1]; + index_of_dim2 = m / output_pitches[2]; + m = m - index_of_dim2 * output_pitches[2]; + index_of_dim3 = m; + + int index_of_input_dim2, index_of_input_dim3, x_offset, y_offset; + index_of_input_dim2 = index_of_dim2 / scales[2]; + y_offset = index_of_dim2 - index_of_input_dim2 * scales[2]; + index_of_input_dim3 = index_of_dim3 / scales[3]; + x_offset = index_of_dim3 - index_of_input_dim3 * scales[3]; + + input_index = index_of_dim0 * input_pitches[0] + + index_of_dim1 * input_pitches[1] + + index_of_input_dim2 * input_pitches[2] + + index_of_input_dim3; + + float x00 = getInputFloat(input_index); + float x10, x01, x11; + + bool end_of_dim2 = false; + if (index_of_input_dim2 == (${e[0].dims[2]} - 1)) { + // It's the end in dimension 2 + x01 = x00; + end_of_dim2 = true; + } else { + x01 = getInputFloat(input_index + input_pitches[2]); + } + + if (index_of_input_dim3 == (input_pitches[2] - 1)) { + // It's the end in dimension 3 + x10 = x00; + x11 = x01; + } + else { + x10 = getInputFloat(input_index + 1); + x11 = end_of_dim2 ? x10 : getInputFloat(input_index + input_pitches[2] + 1); + } + + float y0 = x00 + float(y_offset) * (x01 - x00) / float(scales[2]); + float y1 = x10 + float(y_offset) * (x11 - x10) / float(scales[2]); + return y0 + float(x_offset) * (y1 - y0) / float(scales[3]); + }`:` + ${m} + float process(int indices[2]) { + int input_index = 0; + int output_index = coordsToOffset(TexCoords, ${a}, ${u}); + + ${d} + + int m; + int index_of_dim0, index_of_dim1; + index_of_dim0 = output_index / output_pitches[0]; + m = output_index - index_of_dim0 * output_pitches[0]; + index_of_dim1 = m; + + int index_of_input_dim0, index_of_input_dim1, x_offset, y_offset; + index_of_input_dim0 = index_of_dim0 / scales[0]; + y_offset = index_of_dim0 - index_of_input_dim0 * scales[0]; + index_of_input_dim1 = index_of_dim1 / scales[1]; + x_offset = index_of_dim1 - index_of_input_dim1 * scales[1]; + + input_index = index_of_input_dim0 * input_pitches[0] + index_of_input_dim1; + + float x00 = getInputFloat(input_index); + float x10, x01, x11; + + bool end_of_dim0 = false; + if (index_of_input_dim0 == (${e[0].dims[0]} - 1)) { + // It's the end in dimension 0 + x01 = x00; + end_of_dim0 = true; + } else { + x01 = getInputFloat(input_index + input_pitches[0]); + } + + if (index_of_input_dim1 == (input_pitches[0] - 1)) { + // It's the end in dimension 1 + x10 = x00; + x11 = x01; + } + else { + x10 = getInputFloat(input_index + 1); + x11 = end_of_dim0 ? x10 : getInputFloat(input_index + input_pitches[0] + 1); + } + + float y0 = x00 + float(y_offset) * (x01 - x00) / float(scales[0]); + float y1 = x10 + float(y_offset) * (x11 - x10) / float(scales[0]); + return y0 + float(x_offset) * (y1 - y0) / float(scales[1]); + }`;return{...Al,output:{dims:s,type:e[0].type,textureType:0},shaderSource:y,variables:[{name:"scales",type:"int",arrayLength:o.scales.length,data:o.scales.map(g=>Math.ceil(g))}]}},Do=(i,e)=>{if(!i||e.opset<9&&i.length!==1||e.opset>=9&&e.opset<11&&i.length!==2||e.opset>=11&&i.length<2)throw new Error("invalid inputs.");if(e.scales.length>0&&i[0].dims.length!==e.scales.length)throw new Error("Invalid input shape.");if(i[0].type==="string")throw new Error("Invalid input tensor types.")},Zr=(i,e,o)=>{if(o){for(let t of i)if(t<=0)throw new Error("Scale value should be greater than 0.")}else for(let t of i)if(t<1)throw new Error("Scale value should be greater than or equal to 1.");if((e==="linear"||e==="cubic")&&i.length!==2&&(i.length!==4||i[0]!==1||i[1]!==1))throw new Error(`'Linear' mode and 'Cubic' mode only support 2-D inputs ('Bilinear', 'Bicubic') or 4-D inputs with the corresponding outermost 2 scale values being 1 in the ${o?"Resize":"Upsample"} opeartor.`)}});var $o,ko,Dl,Ll,Th,wh,vh,Ih,$l=x(()=>{"use strict";X();R();Vt();ge();Lo();$o={name:"Resize",inputNames:["A"],inputTypes:[2]},ko=(i,e,o)=>(Do(e,o),[i.run({...$o,cacheHint:o.cacheKey,get:()=>Th(i,e,o)},e)]),Dl=i=>or(i,10),Ll=i=>or(i,11),Th=(i,e,o)=>{let t=D(i.session.backend.glContext.version),[r,n]=wh(e,o);if(r.every(v=>v===1)&&o.coordinateTransformMode!=="tf_crop_and_resize")return{...$o,output:{dims:n,type:e[0].type,textureType:2},hasMain:!0,shaderSource:`void main() { + vec4 v = ${t.texture2D}(X, TexCoords); + ${t.output} = v; + }`};let a=n.length;if(a<2)throw new Error(`output dimension should be at least 2, but got ${a}`);let u=n[a-2],l=n[a-1],f=e[0].dims;if(a!==f.length)throw new Error(`output dimension should match input ${f.length}, but got ${a}`);let c=f[a-2],d=f[a-1],m=r[a-2],y=r[a-1],g="";if(o.mode!=="linear")throw new Error(`resize (packed) does not support mode: '${o.mode}'`);switch(o.coordinateTransformMode){case"asymmetric":g=` + vec4 getSourceFracIndex(ivec4 coords) { + return vec4(coords) / scaleWHWH; + } + `;break;case"half_pixel":g=` + vec4 getSourceFracIndex(ivec4 coords) { + return (vec4(coords) + 0.5) / scaleWHWH - 0.5; + } + `;break;case"pytorch_half_pixel":g=` + vec4 getSourceFracIndex(ivec4 coords) { + vec4 fcoords = vec4(coords); + return vec4( + ${l}.0 > 1.0 ? (fcoords.x + 0.5) / scaleWHWH.x - 0.5 : 0.0, + ${u}.0 > 1.0 ? (fcoords.y + 0.5) / scaleWHWH.y - 0.5 : 0.0, + ${l}.0 > 1.0 ? (fcoords.z + 0.5) / scaleWHWH.z - 0.5 : 0.0, + ${u}.0 > 1.0 ? (fcoords.w + 0.5) / scaleWHWH.w - 0.5 : 0.0 + ); + } + `;break;case"align_corners":g=` + vec4 getSourceFracIndex(ivec4 coords) { + vec4 resized = vec4(${l}.0 - 1.0, ${u}.0 - 1.0, ${l}.0 - 1.0, + ${u}.0 - 1.0); + vec4 original = vec4(${d}.0 - 1.0, ${c}.0 - 1.0, ${d}.0 - 1.0, + ${c}.0 - 1.0); + vec4 new_scale = original / resized; + return vec4(coords) * new_scale; + } + `;break;default:throw new Error(`resize (packed) does not support coordinateTransformMode: '${o.coordinateTransformMode}'`)}let T=ht(a),_=Ut(),w=` + const vec2 inputWH = vec2(${c}.0, ${d}.0); + const vec4 scaleWHWH = vec4(float(${m}), float(${y}), float(${m}), float(${y})); + ${_} + ${g} + float getAValue(int x10, int r, int c, int d) { + return getChannel(getA(x10, r, c, d), vec2(c, d)); + } + void main() { + ${T} rc = getOutputCoords(); + + int batch = rc[0]; + int depth = rc[1]; + + // retrieve the 4 coordinates that is used in the 4 packed output values. + ivec4 coords = ivec4(rc.wz, rc.w + 1, rc.z + 1); + + // calculate the source index in fraction + vec4 sourceFrac = getSourceFracIndex(coords); + + // get the lower and upper bound of the 4 values that will be packed into one texel. + ivec4 x00 = ivec4(max(sourceFrac.xy, vec2(0.0)), min(inputWH - 1.0, ceil(sourceFrac.xy))); + ivec4 x01 = ivec4(max(sourceFrac.xw, vec2(0.0)), min(inputWH - 1.0, ceil(sourceFrac.xw))); + ivec4 x10 = ivec4(max(sourceFrac.zy, vec2(0.0)), min(inputWH - 1.0, ceil(sourceFrac.zy))); + ivec4 x11 = ivec4(max(sourceFrac.zw, vec2(0.0)), min(inputWH - 1.0, ceil(sourceFrac.zw))); + + bool hasNextRow = rc.w < ${u-1}; + bool hasNextCol = rc.z < ${l-1}; + + // pack x00, x01, x10, x11's top-left corner into one vec4 structure + vec4 topLeft = vec4( + getAValue(batch, depth, x00.x, x00.y), + hasNextCol ? getAValue(batch, depth, x01.x, x01.y) : 0.0, + hasNextRow ? getAValue(batch, depth, x10.x, x10.y) : 0.0, + (hasNextRow && hasNextCol) ? getAValue(batch, depth, x11.x, x11.y) : 0.0); + + // pack x00, x01, x10, x11's top-right corner into one vec4 structure + vec4 topRight = vec4( + getAValue(batch, depth, x00.x, x00.w), + hasNextCol ? getAValue(batch, depth, x01.x, x01.w) : 0.0, + hasNextRow ? getAValue(batch, depth, x10.x, x10.w) : 0.0, + (hasNextRow && hasNextCol) ? getAValue(batch, depth, x11.x, x11.w) : 0.0); + + // pack x00, x01, x10, x11's bottom-left corner into one vec4 structure + vec4 bottomLeft = vec4( + getAValue(batch, depth, x00.z, x00.y), + hasNextCol ? getAValue(batch, depth, x01.z, x01.y) : 0.0, + hasNextRow ? getAValue(batch, depth, x10.z, x10.y) : 0.0, + (hasNextRow && hasNextCol) ? getAValue(batch, depth, x11.z, x11.y) : 0.0); + + // pack x00, x01, x10, x11's bottom-right corner into one vec4 structure + vec4 bottomRight = vec4( + getAValue(batch, depth, x00.z, x00.w), + hasNextCol ? getAValue(batch, depth, x01.z, x01.w) : 0.0, + hasNextRow ? getAValue(batch, depth, x10.z, x10.w) : 0.0, + (hasNextRow && hasNextCol) ? getAValue(batch, depth, x11.z, x11.w) : 0.0); + + // calculate the interpolation fraction on u and v direction + vec4 frac = vec4(sourceFrac) - floor(sourceFrac); + vec4 clampFrac = clamp(frac, vec4(0.0), vec4(1.0)); + + vec4 top = mix(topLeft, topRight, clampFrac.ywyw); + vec4 bottom = mix(bottomLeft, bottomRight, clampFrac.ywyw); + vec4 newValue = mix(top, bottom, clampFrac.xxzz); + + ${t.output} = vec4(newValue); + } + `;return{...$o,output:{dims:n,type:e[0].type,textureType:2},hasMain:!0,shaderSource:w}},wh=(i,e)=>{let t=i[0].dims,r=e.scales,n;if(r.length===0){let a=i[e.scalesInputIdx];if(a&&a.size!==0){if(i[e.sizesInputIdx])throw new Error("Only one of scales or sizes must be provided as input.");r=vh(a,e.mode,e.isResize)}else{let u=i[e.sizesInputIdx];if(!u||u.size===0)throw new Error("Either scales or sizes MUST be provided as input.");n=Array.from(u.integerData),r=Ih(n,t,e.mode,e.isResize)}}else if(i[e.sizesInputIdx])throw new Error("Only one of scales or sizes must be provided as input.");let s=n||t.map((a,u)=>Math.floor(a*r[u]));return[r,s]},vh=(i,e,o)=>{let t=Array.from(i.floatData);return Zr(t,e,o),t},Ih=(i,e,o,t)=>{let r=e.length,n=new Array(r);for(let s=0,a=r;s{"use strict";ye();kl=(i,e)=>(_h(e),[new rt([e[0].dims.length],"int32",void 0,void 0,new Int32Array(e[0].dims))]),_h=i=>{if(!i||i.length!==1)throw new Error("Shape requires 1 input.")}});var Bo,Fl,Nl,Cl,Sh,Rl,Oh,Ah,Gl=x(()=>{"use strict";at();nr();U();R();Bo={name:"Slice",inputNames:["A"],inputTypes:[0]},Fl=(i,e,o)=>(Sh(e),[i.run({...Bo,cacheHint:o.cacheKey,get:()=>Cl(i,e[0],o)},e)]),Nl=i=>{let e=i.attributes.getInts("starts"),o=i.attributes.getInts("ends"),t=i.attributes.getInts("axes",[]);return F({starts:e,ends:o,axes:t})},Cl=(i,e,o)=>{let t=o.axes.length===0?e.dims.slice(0).map((c,d)=>d):o.axes,r=A.normalizeAxes(t,e.dims.length),n=o.starts.map((c,d)=>c>e.dims[r[d]]-1?e.dims[r[d]]:A.normalizeAxis(c,e.dims[r[d]])),s=o.ends.map((c,d)=>c>e.dims[r[d]]-1?e.dims[r[d]]:A.normalizeAxis(c,e.dims[r[d]])),a=e.dims.slice(),u=[];for(let c=0;c0&&u.push(`outputIdx[${r[c]}] += ${n[c]};`);let f=` + float process(int outputIdx[${a.length}]) { + ${u.join(` + `)} + return _A(outputIdx); + }`;return{...Bo,output:{dims:a,type:e.type,textureType:0},shaderSource:f}},Sh=i=>{if(!i||i.length!==1)throw new Error("Slice requires 1 input.");if(re.indexOf(i[0].type)===-1)throw new Error("Invalid input type.")},Rl=(i,e)=>{Ah(e);let o=Oh(i,e);return[i.run({...Bo,cacheHint:o.cacheKey,get:()=>Cl(i,e[0],o)},[e[0]])]},Oh=(i,e)=>{if(!i.session.isInitializer(e[1].dataId)||!i.session.isInitializer(e[2].dataId)||e.length>=4&&!i.session.isInitializer(e[3].dataId)||e.length>=5&&!i.session.isInitializer(e[4].dataId))throw new Error("dynamic slice attributes are not allowed");if(e.length>=5&&e[4].integerData.some(s=>s!==1))throw new Error("currently non-1 steps is not supported for Slice");let o=Array.from(e[1].integerData),t=Array.from(e[2].integerData),r=e.length>=4?Array.from(e[3].integerData):[],n=`${r};${o};${t}`;return{starts:o,ends:t,axes:r,cacheKey:n}},Ah=i=>{if(!i||i.length<3||i.length>5)throw new Error("Invalid input number.");if(i[1].type!=="int32"||i[1].dims.length!==1)throw new Error("Invalid input type.");if(i[2].type!=="int32"||i[2].dims.length!==1)throw new Error("Invalid input type.");if(i.length>=4&&(i[3].type!=="int32"||i[3].dims.length!==1))throw new Error("Invalid input type.");if(i.length>=5&&(i[4].type!=="int32"||i[4].dims.length!==1))throw new Error("Invalid input type.")}});var Ml,Vl,Ul,zl,Wl,Hl,ql,jl,Ph,Eh,Dh,Xl,Kl=x(()=>{"use strict";at();U();X();R();Xr();Ml={name:"SoftmaxComputeMax",inputNames:["A"],inputTypes:[0]},Vl={name:"SoftmaxComputeScale",inputNames:["A","Max"],inputTypes:[0,0]},Ul={name:"SoftMax",inputNames:["A","Max","Norm"],inputTypes:[0,0,0]},zl=(i,e,o)=>{Xl(e);let t=e[0].dims.slice(),r=A.normalizeAxis(o.axis,t.length),n=A.sizeToDimension(t,r),s=A.sizeFromDimension(t,r);return jl(i,e,o,n,s)},Wl=i=>F({axis:i.attributes.getInt("axis",1)}),Hl=i=>F({axis:i.attributes.getInt("axis",-1)}),ql=(i,e,o)=>{Xl(e);let t=e[0].dims.slice(),r=A.normalizeAxis(o.axis,t.length),n=t.length,s=r!==n-1,a=[],u=[],l=[],f;s&&(u=Array.from({length:n}).map((y,g)=>g),u[r]=n-1,u[n-1]=r,u.map(y=>a.push(t[y])),f=F({perm:u}),l=Te(i,e,f));let c=s?A.sizeToDimension(a,n-1):A.sizeToDimension(t,n-1),d=s?A.sizeFromDimension(a,n-1):A.sizeFromDimension(t,n-1),m=jl(i,s?l:e,o,c,d);return s?Te(i,m,f):m},jl=(i,e,o,t,r)=>{let n=Ph(i,e[0],t,r,[t]),s=i.run({...Ml,cacheHint:o.cacheKey,get:()=>n},e),a=Eh(i,e[0],t,r,n.output.dims,[t]),u=i.run({...Vl,cacheHint:o.cacheKey,get:()=>a},[e[0],s]),l=Dh(i,e[0],t,r,n.output.dims,a.output.dims);return[i.run({...Ul,cacheHint:o.cacheKey,get:()=>l},[e[0],s,u])]},Ph=(i,e,o,t,r)=>{let[n,s]=i.calculateTextureWidthAndHeight(e.dims,0),a=r.length;if(o<1||t<1)throw new Error("Logical row count N and feature count D must be greater than or equal to 1");if(r.length!==1)throw new Error("Dimensionality of the output should be 1");if(r[0]!==o)throw new Error("Shape of the output should be equal to logical row count");let u=D(i.session.backend.glContext.version),l=` + float process(int[${a}] indices) { + int logical_row_start_offset = indices[0] * ${t}; + + float max = getColorAsFloat(${u.texture2D}(A, offsetToCoords(logical_row_start_offset, ${n}, + ${s} ))); + for(int i=1; i<${t}; ++i) + { + float current = getColorAsFloat(${u.texture2D}(A, offsetToCoords(logical_row_start_offset + i, + ${n}, ${s}))); + if(current > max) + max = current; + } + + return max; + }`;return{...Ml,output:{dims:r,type:e.type,textureType:0},shaderSource:l}},Eh=(i,e,o,t,r,n)=>{let[s,a]=i.calculateTextureWidthAndHeight(e.dims,0),u=n.length;if(o<1||t<1)throw new Error("Logical row count N and feature count D must be greater than or equal to 1");if(n.length!==1)throw new Error("Dimensionality of the output should be 1");if(n[0]!==o)throw new Error("Shape of the output should be equal to logical row count");if(r.length!==1)throw new Error("Dimensionality of the intermediate results should be 1");if(r[0]!==o)throw new Error("Shape of the intermediate results should be equal to logical row count");let l=D(i.session.backend.glContext.version),f=` + float process(int[${u}] indices) { + int logical_row_start_offset = indices[0] * ${t}; + + float norm_factor = 0.0; + float max = _Max(indices); + for(int i=0; i<${t}; ++i) + { + norm_factor += exp(getColorAsFloat(${l.texture2D}(A, offsetToCoords(logical_row_start_offset + i, + ${s}, ${a}))) - max); + } + + return norm_factor; + }`;return{...Vl,output:{dims:n,type:e.type,textureType:0},shaderSource:f}},Dh=(i,e,o,t,r,n)=>{let[s,a]=i.calculateTextureWidthAndHeight(e.dims,0),u=e.dims.length;if(o<1||t<1)throw new Error("Logical row count N and feature count D must be greater than or equal to 1");if(r.length!==1||n.length!==1)throw new Error("Dimensionality of the intermediate results should be 1");if(r[0]!==o||n[0]!==o)throw new Error("Shape of the intermediate results should be equal to logical row count");let l=` + float process(int[${u}] indices) { + + // get offset of current logical tensor index from the 2-D texture coordinates (TexCoords) + int offset = coordsToOffset(TexCoords, ${s}, ${a}); + + //determine the logical row for this index + int logical_row_index[1]; + logical_row_index[0] = offset / ${t}; + + float norm_factor = _Norm(logical_row_index); + + // avoid possible division by 0 + // if norm_facor is 0, all elements are zero + // if so, return 0 + if(norm_factor == 0.0) + return 0.0; + + return exp(_A(indices) - _Max(logical_row_index)) / norm_factor; + }`;return{...Ul,output:{dims:e.dims,type:e.type,textureType:0},shaderSource:l}},Xl=i=>{if(!i||i.length!==1)throw new Error("Softmax requires 1 input.");if(i[0].type!=="float32"&&i[0].type!=="float64")throw new Error("Invalid input type")}});var Jl,Zl,Yl,Lh,$h,kh,Ql=x(()=>{"use strict";at();U();R();Jl={name:"Split",inputNames:["A"],inputTypes:[0]},Zl=(i,e,o)=>{kh(e);let t=A.normalizeAxis(o.axis,e[0].dims.length),r=Lh(i,e,t,o),n=[];for(let s=0;s$h(i,e[0],o,t,s)},e));return n},Yl=i=>{let e=i.attributes.getInt("axis",0),o=i.attributes.getInts("split",[]),t=i.outputs.length;return F({axis:e,split:o,numOutputs:t})},Lh=(i,e,o,t)=>{let[,r]=Ze.splitShape(e[0].dims,o,t.split,t.numOutputs);return r.length},$h=(i,e,o,t,r)=>{let[n,s]=Ze.splitShape(e.dims,t,o.split,o.numOutputs),a=s[r],u=n[r],f=` + float process(int indices[${u.length}]) { + indices[${t}] += ${a}; + return _A(indices); + } + `;return{...Jl,cacheHint:`${o.cacheKey}:${r}`,output:{dims:u,type:e.type,textureType:0},shaderSource:f}},kh=i=>{if(!i||i.length!==1)throw new Error("Split requires one input.");if(i[0].type!=="int8"&&i[0].type!=="uint8"&&i[0].type!=="int16"&&i[0].type!=="uint16"&&i[0].type!=="int32"&&i[0].type!=="uint32"&&i[0].type!=="float32"&&i[0].type!=="float64"&&i[0].type!=="bool")throw new Error("Invalid input type.")}});var Fo,tf,ef,Bh,Fh,rf=x(()=>{"use strict";U();Fo=(i,e,o)=>{Bh(e);let t=A.squeezeShape(e[0].dims,o);return[i.reshapeUnpacked(e[0],t)]},tf=(i,e)=>(Fh(e),Fo(i,[e[0]],Array.from(e[1].integerData))),ef=i=>i.attributes.getInts("axes"),Bh=i=>{if(!i||i.length!==1)throw new Error("Squeeze requires 1 input.");if(i[0].type==="string")throw new Error("invalid input tensor types.")},Fh=i=>{if(!i||i.length!==2)throw new Error("Squeeze requires 2 inputs.");if(i[1].type!=="int32")throw new Error("Invalid input type.")}});var nf,Nh,Ch,of=x(()=>{"use strict";X();R();nf=(i,e)=>{Ch(e);let o={name:"Sum",inputNames:e.map((r,n)=>`X${n}`),inputTypes:new Array(e.length).fill(0)};return[i.run({...o,get:()=>Nh(i,e,o)},e)]},Nh=(i,e,o)=>{let t=D(i.session.backend.glContext.version),r=e[0].dims.slice(),s=` + void main() { + vec4 result = ${e.map((a,u)=>`${t.texture2D}(X${u},TexCoords)`).join(" + ")}; + ${t.output} = result; + } + `;return{...o,output:{dims:r,type:e[0].type,textureType:0},hasMain:!0,shaderSource:s}},Ch=i=>{if(!i||i.length===0)throw new Error("Sum requires inputs.");let e=i[0].dims.length;for(let o=1;o{"use strict";nr();R();af=(i,e)=>{Gh(e);let o={name:"Tile",inputNames:["A"],inputTypes:[0]};return[i.run({...o,get:()=>Rh(i,e,o)},e)]},Rh=(i,e,o)=>{let t=e[0].dims.slice(),r=new Array(t.length),n=[];for(let u=0;u{if(!i||i.length!==2)throw new Error("Tile requires 2 input.");if(i[1].dims.length!==1)throw new Error("The second input shape must 1 dimension.");if(i[1].dims[0]!==i[0].dims.length)throw new Error("Invalid input shape.");if(re.indexOf(i[0].type)===-1)throw new Error("Invalid input type.");if(i[1].type!=="int32"&&i[1].type!=="int16")throw new Error("Invalid repeat type.")}});var No,uf,lf,Mh,Vh,ff=x(()=>{"use strict";U();No=(i,e,o)=>{Mh(e);let t=A.unsqueezeShape(e[0].dims,o);return[i.reshapeUnpacked(e[0],t)]},uf=(i,e)=>(Vh(e),No(i,[e[0]],Array.from(e[1].integerData))),lf=i=>i.attributes.getInts("axes"),Mh=i=>{if(!i||i.length!==1)throw new Error("Unsqueeze requires 1 input.");if(i[0].type==="string")throw new Error("invalid input tensor types.")},Vh=i=>{if(!i||i.length!==2)throw new Error("Unsqueeze requires 2 inputs.");if(i[1].type!=="int32")throw new Error("Invalid input type.")}});var cf,pf=x(()=>{"use strict";ws();Bs();Cs();zs();Hr();Ou();$u();Fu();Ru();Uu();Hu();Ku();Qu();qr();nl();bl();_l();Ol();$l();Bl();Gl();Kl();Ql();rf();of();sf();Xr();xo();ff();Lo();cf=[["Abs","","6+",Ws],["Acos","","7+",Hs],["Add","","7+",vs],["And","","7+",Is],["Asin","","7+",qs],["Atan","","7+",js],["AveragePool","","7+",il,al],["BatchNormalization","","7+",xs,Ts],["Cast","","6+",Fs,Ns],["Ceil","","6+",Js],["Clip","","6-10",yo,Xs],["Clip","","11+",Ks],["Concat","","4+",Ms,Us],["Conv","","1+",So,Oo],["ConvTranspose","","1+",_u,Su],["Cos","","7+",Zs],["Div","","7+",_s],["Dropout","","7+",go],["DepthToSpace","","1+",Du,Lu],["Equal","","7+",Ss],["Elu","","6+",Ys,Qs],["Exp","","6+",tu],["Flatten","","1+",ku,Bu],["Floor","","6+",eu],["FusedConv","com.microsoft","1+",So,Oo],["Gather","","1+",Nu,Cu],["Gemm","","7-10",Ao,Mu],["Gemm","","11+",Ao,Vu],["GlobalAveragePool","","1+",ul,ll],["GlobalMaxPool","","1+",hl],["Greater","","7+",Os],["Identity","","1+",go],["ImageScaler","","1+",zu,Wu],["InstanceNormalization","","6+",ju,Xu],["LeakyRelu","","6+",ru,nu],["Less","","7+",As],["LRN","","1+",Ju,Zu],["Log","","6+",ou],["MatMul","","1+",yu,gu],["MaxPool","","1+",fl,cl],["Mul","","7+",Ps],["Neg","","6+",iu],["Not","","1+",au],["Or","","7+",Es],["Pad","","2-10",Po,tl],["Pad","","11+",el,rl],["Pow","","7+",Ds],["PRelu","","7+",Ls],["ReduceLogSum","","1+",vl,ne],["ReduceMax","","1+",xl,ne],["ReduceMean","","1+",gl,ne],["ReduceMin","","1+",Tl,ne],["ReduceProd","","1+",wl,ne],["ReduceSum","","1-12",yl,ne],["ReduceSumSquare","","1+",Il,ne],["Relu","","6+",su],["Reshape","","5+",Sl],["Resize","","10",ko,Dl],["Resize","","11+",ko,Ll],["Shape","","1+",kl],["Sigmoid","","6+",uu],["Sin","","7+",lu],["Slice","","10+",Rl],["Slice","","1-9",Fl,Nl],["Softmax","","1-12",zl,Wl],["Softmax","","13+",ql,Hl],["Split","","2-12",Zl,Yl],["Sqrt","","6+",fu],["Squeeze","","1-12",Fo,ef],["Squeeze","","13+",tf],["Sub","","7+",$s],["Sum","","6+",nf],["Tan","","7+",cu],["Tanh","","6+",pu],["Tile","","6+",af],["Transpose","","1+",Te,Pu],["Upsample","","7-8",Eo,Pl],["Upsample","","9",Eo,El],["Unsqueeze","","1-12",No,lf],["Unsqueeze","","13+",uf],["Xor","","7+",ks]]});function hf(i){let e={},o;for(;(o=df.exec(i))!==null;){let t=o[3].split(",").map(r=>{let n=r.trim().split(" ");return n&&n.length===2?{type:n[0],name:n[1]}:null}).filter(r=>r!==null);e[o[2]]={params:t,body:o[4]}}for(let t in e){let r=Uh.replace("__FUNC__",t),n=new RegExp(r,"gm");for(;(o=n.exec(i))!==null;){let s=o[1],a=o[2],u=o[3].split(","),l=s?`${s} ${a};`:"",f=e[t].body,c="";e[t].params.forEach((m,y)=>{m&&(c+=`${m.type} ${m.name} = ${u[y]}; +`)}),f=`${c} + ${f}`,f=f.replace("return",`${a} = `);let d=` + ${l} + { + ${f} + } + `;i=i.replace(o[0],d)}}return i=i.replace(df,""),i}var df,Uh,mf=x(()=>{"use strict";df=/@inline[\s\n\r]+(\w+)[\s\n\r]+([0-9a-zA-Z_]+)\s*\(([^)]*)\)\s*{(([^}]|[\n\r])*)}/gm,Uh="(\\w+)?\\s+([_0-9a-zA-Z]+)\\s+=\\s+__FUNC__\\((.*)\\)\\s*;"});function Re(i,e){let o=[],t=[],r=e!=null&&Array.isArray(e)&&e.length===0,n=e==null||r?null:zh(e,i).sort(),s=0;for(let a=0;aa)&&i[a]===1&&(o.push(i[a]),t.push(a)),n[s]<=a&&s++}i[a]!==1&&(o.push(i[a]),t.push(a))}return{newShape:o,keptDims:t}}function zh(i,e){let o=e.length;return i=i==null?e.map((t,r)=>r):[].concat(i),$e(i.every(t=>t>=-o&&t`All values in axis param must be in range [-${o}, ${o}) but got axis ${i}`),$e(i.every(Wh),()=>`All values in axis param must be integers but got axis ${i}`),i.map(t=>t<0?o+t:t)}function Wh(i){return i%1===0}function Hh(i){if(i.length===0)return 1;let e=i[0];for(let o=1;o{"use strict";It();U();Yr=class{constructor(e){this.maxTextureSize=e}computeTextureWH(e,o){let t=this.computeTexture(e,o);return o&&o.isPacked&&(t[0]/=2,t[1]/=2),o&&o.reverseWH?[t[1],t[0]]:t}computeTexture(e,o){let t=o&&o.isPacked;if(e.length===0)return t?[2,2]:[1,1];let r=this.maxTextureSize;if(o&&o.breakAxis!==void 0){let a=o.breakAxis>=e.length?1:e.slice(o.breakAxis).reduce((l,f)=>l*f),u=o.breakAxis<=0?1:e.slice(0,o.breakAxis).reduce((l,f)=>l*f);if(a>r||u>r)z.verbose("TextureLayout",`Given width/height preferences were unattainable: shape:${e}, breakAxis:${o.breakAxis}`);else return[a,u]}let n=e.slice(0);t&&(r=r*2,n=n.map((a,u)=>u>=n.length-2?n[u]%2===0?n[u]:n[u]+1:n[u]),n.length===1&&(n=[2,n[0]])),n.length!==2&&(n=Re(n).newShape);let s=Hh(n);return n.length<=1&&s<=r?[1,s]:n.length===2&&n[0]<=r&&n[1]<=r?n:n.length===3&&n[0]*n[1]<=r&&n[2]<=r?[n[0]*n[1],n[2]]:n.length===3&&n[0]<=r&&n[1]*n[2]<=r?[n[0],n[1]*n[2]]:n.length===4&&n[0]*n[1]*n[2]<=r&&n[3]<=r?[n[0]*n[1]*n[2],n[3]]:n.length===4&&n[0]<=r&&n[1]*n[2]*n[3]<=r?[n[0],n[1]*n[2]*n[3]]:t?bf(s/4).map(a=>a*2):bf(s)}}});var Qr,yf=x(()=>{"use strict";U();Xt();X();Co();Vt();Qr=class extends At{constructor(o){super(o)}getFunctions(){return{...this.offsetToCoords(),...this.coordsToOffset(),...this.toVec(),...this.valueFrom(),...this.getCommonUtilFuncs(),...this.getInputsSamplingSnippets(),...this.getOutputSamplingSnippet()}}getCustomTypes(){return{}}offsetToCoords(){let o="offsetToCoords";return{offsetToCoords:new O(` + vec2 ${o}(int offset, int width, int height) { + int t = offset / width; + int s = offset - t*width; + vec2 coords = (vec2(s,t) + vec2(0.5,0.5)) / vec2(width, height); + return coords; + } + `)}}coordsToOffset(){let o="coordsToOffset";return{coordsToOffset:new O(` + int ${o}(vec2 coords, int width, int height) { + float s = coords.s * float(width); + float t = coords.t * float(height); + int offset = int(t) * width + int(s); + return offset; + } + `)}}getOutputSamplingSnippet(){let o=this.context.outputTextureLayout;return o.isPacked?this.getPackedOutputSamplingSnippet(o):this.getUnpackedOutputSamplingSnippet(o)}getPackedOutputSamplingSnippet(o){let t=o.unpackedShape,r=[o.width,o.height],n={},s="getOutputCoords";switch(t.length){case 0:n[s]=this.getOutputScalarCoords();break;case 1:n[s]=this.getOutputPacked1DCoords(t,r);break;case 2:n[s]=this.getOutputPacked2DCoords(t,r);break;case 3:n[s]=this.getOutputPacked3DCoords(t,r);break;default:n[s]=this.getOutputPackedNDCoords(t,r)}let u=` + void setOutput(vec4 val) { + ${D(this.context.glContext.version).output} = val; + } + `,l="floatTextureSetRGBA";return n[l]=new O(u),n}getUnpackedOutputSamplingSnippet(o){let t=o.unpackedShape,r=[o.width,o.height],n={},s="getOutputCoords";switch(t.length){case 0:n[s]=this.getOutputScalarCoords();break;case 1:n[s]=this.getOutputUnpacked1DCoords(t,r);break;case 2:n[s]=this.getOutputUnpacked2DCoords(t,r);break;case 3:n[s]=this.getOutputUnpacked3DCoords(t,r);break;case 4:n[s]=this.getOutputUnpacked4DCoords(t,r);break;case 5:n[s]=this.getOutputUnpacked5DCoords(t,r);break;case 6:n[s]=this.getOutputUnpacked6DCoords(t,r);break;default:throw new Error(`Unsupported output dimensionality: ${t.length}`)}let u=` + void setOutput(float val) { + ${D(this.context.glContext.version).output} = vec4(val, 0, 0, 0); + } + `,l="floatTextureSetR";return n[l]=new O(u),n}getOutputScalarCoords(){return new O(` + int getOutputCoords() { + return 0; + } + `)}getOutputPacked1DCoords(o,t){let r=t,n="";return r[0]===1?(n=` + int getOutputCoords() { + return 2 * int(TexCoords.y * ${r[1]}.0); + } + `,new O(n)):r[1]===1?(n=` + int getOutputCoords() { + return 2 * int(TexCoords.x * ${r[0]}.0); + } + `,new O(n)):(n=` + int getOutputCoords() { + ivec2 resTexRC = ivec2(TexCoords.xy * + vec2(${r[0]}, ${r[1]})); + return 2 * (resTexRC.y * ${r[0]} + resTexRC.x); + } + `,new O(n))}getOutputPacked2DCoords(o,t){let r="";if(de.arraysEqual(o,t))return r=` + ivec2 getOutputCoords() { + return 2 * ivec2(TexCoords.xy * vec2(${t[0]}, ${t[1]})); + } + `,new O(r);let n=t,s=Math.ceil(o[1]/2);return r=` + ivec2 getOutputCoords() { + ivec2 resTexRC = ivec2(TexCoords.xy * + vec2(${n[0]}, ${n[1]})); + + int index = resTexRC.y * ${n[0]} + resTexRC.x; + + // reverse r and c order for packed texture + int r = imod(index, ${s}) * 2; + int c = 2 * (index / ${s}); + + return ivec2(r, c); + } + `,new O(r)}getOutputPacked3DCoords(o,t){let r=[t[0],t[1]],n=Math.ceil(o[2]/2),s=n*Math.ceil(o[1]/2),a=` + ivec3 getOutputCoords() { + ivec2 resTexRC = ivec2(TexCoords.xy * + vec2(${r[0]}, ${r[1]})); + int index = resTexRC.y * ${r[0]} + resTexRC.x; + + int b = index / ${s}; + index -= b * ${s}; + + // reverse r and c order for packed texture + int r = imod(index, ${n}) * 2; + int c = 2 * (index / ${n}); + + return ivec3(b, r, c); + } + `;return new O(a)}getOutputPackedNDCoords(o,t){let r=[t[0],t[1]],n=Math.ceil(o[o.length-1]/2),s=n*Math.ceil(o[o.length-2]/2),a=s,u="",l="b, r, c";for(let c=2;c=0;--l)s[l]=s[l+1]*o[l+1];let a=["r","c","d"],u=s.map((l,f)=>{let c=`int ${a[f]} = index / ${l}`,d=f===s.length-1?`int ${a[f+1]} = index - ${a[f]} * ${l}`:`index -= ${a[f]} * ${l}`;return`${c}; ${d};`}).join("");return r=` + ivec3 getOutputCoords() { + ivec2 resTexRC = ivec2(TexCoords.xy * + vec2(${t[0]}, ${t[1]})); + int index = resTexRC.y * ${t[0]} + resTexRC.x; + ${u} + return ivec3(r, c, d); + } + `,new O(r)}getOutputUnpacked4DCoords(o,t){let r="",n=o.length,s=null;n<2&&(s=[]),s=new Array(n-1),s[n-2]=o[n-1];for(let l=n-3;l>=0;--l)s[l]=s[l+1]*o[l+1];let a=["r","c","d","d2"],u=s.map((l,f)=>{let c=`int ${a[f]} = index / ${l}`,d=f===s.length-1?`int ${a[f+1]} = index - ${a[f]} * ${l}`:`index -= ${a[f]} * ${l}`;return`${c}; ${d};`}).join("");return r=` + ivec4 getOutputCoords() { + ivec2 resTexRC = ivec2(TexCoords.xy * + vec2(${t[0]}, ${t[1]})); + int index = resTexRC.y * ${t[0]} + resTexRC.x; + ${u} + return ivec4(r, c, d, d2); + } + `,new O(r)}getOutputUnpacked5DCoords(o,t){let r="",n=o.length,s=null;n<2&&(s=[]),s=new Array(n-1),s[n-2]=o[n-1];for(let l=n-3;l>=0;--l)s[l]=s[l+1]*o[l+1];let a=["r","c","d","d2","d3"],u=s.map((l,f)=>{let c=`int ${a[f]} = index / ${l}`,d=f===s.length-1?`int ${a[f+1]} = index - ${a[f]} * ${l}`:`index -= ${a[f]} * ${l}`;return`${c}; ${d};`}).join("");return r=` + ivec5 getOutputCoords() { + ivec2 resTexRC = ivec2(TexCoords.xy * + vec2(${t[0]}, ${t[1]})); + int index = resTexRC.y * ${t[0]} + resTexRC.x; + ${u} + return ivec5(r, c, d, d2, d3); + } + `,new O(r)}getOutputUnpacked6DCoords(o,t){let r="",n=o.length,s=null;n<2&&(s=[]),s=new Array(n-1),s[n-2]=o[n-1];for(let l=n-3;l>=0;--l)s[l]=s[l+1]*o[l+1];let a=["r","c","d","d2","d3","d4"],u=s.map((l,f)=>{let c=`int ${a[f]} = index / ${l}`,d=f===s.length-1?`int ${a[f+1]} = index - ${a[f]} * ${l}`:`index -= ${a[f]} * ${l}`;return`${c}; ${d};`}).join("");return r=` + ivec6 getOutputCoords() { + ivec2 resTexRC = ivec2(TexCoords.xy * + vec2(${t[0]}, ${t[1]})); + int index = resTexRC.y * ${t[0]} + resTexRC.x; + ${u} + return ivec6(r, c, d, d2, d3, d4); + } + `,new O(r)}getCommonUtilFuncs(){let o={},t="uvFromFlat";o[t]=new O(` + vec2 uvFromFlat(int texNumR, int texNumC, int index) { + int texC = index / texNumR; + int texR = index - texC * texNumR; + // TODO: swap texR, texC order in following function so row is corresponding to u and column is corresponding to + // v. + return (vec2(texR, texC) + halfCR) / vec2(texNumR, texNumC); + } + `),t="packedUVfrom1D",o[t]=new O(` + vec2 packedUVfrom1D(int texNumR, int texNumC, int index) { + int texelIndex = index / 2; + int texR = texelIndex / texNumC; + int texC = texelIndex - texR * texNumC; + return (vec2(texC, texR) + halfCR) / vec2(texNumC, texNumR); + } + `),t="packedUVfrom2D",o[t]=new O(` + vec2 packedUVfrom2D(int texNumR, int texNumC, int texelsInLogicalRow, int row, int col) { + int texelIndex = (row / 2) * texelsInLogicalRow + (col / 2); + int texR = texelIndex / texNumC; + int texC = texelIndex - texR * texNumC; + return (vec2(texC, texR) + halfCR) / vec2(texNumC, texNumR); + } + `),t="packedUVfrom3D",o[t]=new O(` + vec2 packedUVfrom3D(int texNumR, int texNumC, + int texelsInBatch, int texelsInLogicalRow, int b, + int row, int col) { + int index = b * texelsInBatch + (row / 2) * texelsInLogicalRow + (col / 2); + int texR = index / texNumC; + int texC = index - texR * texNumC; + return (vec2(texC, texR) + halfCR) / vec2(texNumC, texNumR); + } + `),t="sampleTexture";let r=D(this.context.glContext.version);return o[t]=new O(` + float sampleTexture(sampler2D textureSampler, vec2 uv) { + return ${r.texture2D}(textureSampler, uv).r; + }`),o}getInputsSamplingSnippets(){let o={},t=this.context.outputTextureLayout;return this.context.programInfo.inputNames.forEach((r,n)=>{let s=this.context.inputTextureLayouts[n],a=Rr(r);s.isPacked?o[a]=this.getPackedSamplerFromInput(a,r,s):o[a]=this.getUnpackedSamplerFromInput(a,r,s);let u=ns(r);s.unpackedShape.length<=t.unpackedShape.length&&(s.isPacked?o[u]=this.getPackedSamplerAtOutputCoords(u,s,t,r):o[u]=this.getUnpackedSamplerAtOutputCoords(u,s,t,r))}),o}getPackedSamplerAtOutputCoords(o,t,r,n){let s=t.unpackedShape,a=r.unpackedShape,l=Rr(n),f=s.length,c=a.length,d=dt.getBroadcastDims(s,a),m=ht(c),y=c-f,g,T=Gt();f===0?g="":c<2&&d.length>=1?g="coords = 0;":g=d.map(wt=>`coords.${T[wt+y]} = 0;`).join(` +`);let _="";c<2&&f>0?_="coords":_=s.map((wt,mt)=>`coords.${T[mt+y]}`).join(", ");let w="return outputValue;",L=A.size(s)===1,Q=A.size(a)===1;if(f===1&&!L&&!Q)w=` + return vec4(outputValue.xy, outputValue.xy); + `;else if(L&&!Q)c===1?w=` + return vec4(outputValue.x, outputValue.x, 0., 0.); + `:w=` + return vec4(outputValue.x); + `;else if(d.length){let wt=f-2,mt=f-1;d.indexOf(wt)>-1&&d.indexOf(mt)>-1?w="return vec4(outputValue.x);":d.indexOf(wt)>-1?w="return vec4(outputValue.x, outputValue.y, outputValue.x, outputValue.y);":d.indexOf(mt)>-1&&(w="return vec4(outputValue.xx, outputValue.zz);")}let pt=` + int lastDim = coords.${T[c-1]}; + coords.${T[c-1]} = coords.${T[c-2]}; + coords.${T[c-2]} = lastDim; + `,Ht=` + vec4 ${o}() { + ${m} coords = getOutputCoords(); + ${pt} + ${g} + vec4 outputValue = ${l}(${_}); + ${w} + } + `;return new O(Ht,["coordinates.getOutputCoords"])}getUnpackedSamplerAtOutputCoords(o,t,r,n){let s=[r.width,r.height],a=[t.width,t.height],u=t.unpackedShape.length,l=r.unpackedShape.length,f=t.unpackedShape,c=r.unpackedShape,d=Rr(n);if(u===l&&de.arraysEqual(a,s)){let L=` + float ${o}() { + return sampleTexture(${n}, TexCoords); + } + `;return new O(L,["coordinates.sampleTexture"])}let m=ht(l),y=dt.getBroadcastDims(f,c),g=l-u,T,_=Gt();u===0?T="":l<2&&y.length>=1?T="coords = 0;":T=y.map(L=>`coords.${_[L+g]} = 0;`).join(` +`);let w="";l<2&&u>0?w="coords":w=t.unpackedShape.map((L,B)=>`coords.${_[B+g]}`).join(", ");let v=` + float ${o}() { + ${m} coords = getOutputCoords(); + ${T} + return ${d}(${w}); + } + `;return new O(v,["coordinates.getOutputCoords"])}getPackedSamplerFromInput(o,t,r){switch(r.unpackedShape.length){case 0:return this.getPackedSamplerScalar(o,t);case 1:return this.getPackedSampler1D(o,t,r);case 2:return this.getPackedSampler2D(o,t,r);case 3:return this.getPackedSampler3D(o,t,r);default:return this.getPackedSamplerND(o,t,r)}}getUnpackedSamplerFromInput(o,t,r){let n=r.unpackedShape;switch(n.length){case 0:return this.getUnpackedSamplerScalar(o,t,r);case 1:return this.getUnpackedSampler1D(o,t,r);case 2:return this.getUnpackedSampler2D(o,t,r);case 3:return this.getUnpackedSampler3D(o,t,r);case 4:return this.getUnpackedSampler4D(o,t,r);case 5:return this.getUnpackedSampler5D(o,t,r);case 6:return this.getUnpackedSampler6D(o,t,r);default:throw new Error(`Unsupported dimension ${n.length}-D`)}}getPackedSamplerScalar(o,t){let r=D(this.context.glContext.version),n=` + vec4 ${o}() { + return ${r.texture2D}(${t}, halfCR); + } + `;return new O(n)}getPackedSampler1D(o,t,r){let n=[r.width,r.height],s=[n[1],n[0]],a=D(this.context.glContext.version),l=`vec4 ${o}(int index) { + vec2 uv = packedUVfrom1D( + ${s[0]}, ${s[1]}, index); + return ${a.texture2D}(${t}, uv); + }`;return new O(l,["coordinates.packedUVfrom1D"])}getPackedSampler2D(o,t,r){let n=r.unpackedShape,s=[r.width,r.height],a=D(this.context.glContext.version),u=s[0],l=s[1];if(s!=null&&de.arraysEqual(n,s)){let y=`vec4 ${o}(int row, int col) { + vec2 uv = (vec2(col, row) + halfCR) / vec2(${l}.0, ${u}.0); + return ${a.texture2D}(${t}, uv); + }`;return new O(y)}let f=s,c=Math.ceil(n[1]/2),m=`vec4 ${o}(int row, int col) { + vec2 uv = packedUVfrom2D(${f[1]}, ${f[0]}, ${c}, row, col); + return ${a.texture2D}(${t}, uv); + }`;return new O(m,["coordinates.packedUVfrom2D"])}getPackedSampler3D(o,t,r){let n=r.unpackedShape,s=[r.width,r.height],a=[s[0],s[1]],u=D(this.context.glContext.version);if(n[0]===1){let g=n.slice(1),T=[1,2],_=ke(n,g),w=["b","row","col"],v=JSON.parse(JSON.stringify(r));v.unpackedShape=_;let L=this.getPackedSamplerFromInput(o,t,v),Q=`${L.routineBody} + vec4 ${o}(int b, int row, int col) { + return ${o}(${Be(w,T)}); + } `;return new O(Q,L.dependencies)}let l=a[0],f=a[1],c=Math.ceil(n[2]/2),d=c*Math.ceil(n[1]/2),y=`vec4 ${o}(int b, int row, int col) { + vec2 uv = packedUVfrom3D( + ${f}, ${l}, ${d}, ${c}, b, row, col); + return ${u.texture2D}(${t}, uv);}`;return new O(y,["coordinates.packedUVfrom3D"])}getPackedSamplerND(o,t,r){let n=r.unpackedShape,s=n.length,a=[r.width,r.height],u=D(this.context.glContext.version),l=[a[0],a[1]],f=l[1],c=l[0],d=Math.ceil(n[s-1]/2),m=d*Math.ceil(n[s-2]/2),y="int b, int row, int col",g=`b * ${m} + (row / 2) * ${d} + (col / 2)`;for(let w=2;w{let n=this.context.inputTextureLayouts[r],a=(n.unpackedShape.length>0?n.unpackedShape:n.shape).length,u=`_${t}`;o[u]=new O(this.getValueFromSingle(t,a,n.width,n.height,!1),[`shapeUtils.indicesToOffset${u}`,"coordinates.offsetToCoords","fragcolor.getColorAsFloat"]),u=u+"_T",o[u]=new O(this.getValueFromSingle(t,a,n.width,n.height,!0),[`shapeUtils.indicesToOffset${u}`,"coordinates.offsetToCoords","fragcolor.getColorAsFloat"])}),o}getValueFromSingle(o,t,r,n,s){let a=`_${o}`;s&&(a=a+"_T");let u=D(this.context.glContext.version);return` + float ${a}(int m[${t}]) { + int offset = indicesToOffset${a}(m); + vec2 coords = offsetToCoords(offset, ${r}, ${n}); + float value = getColorAsFloat(${u.texture2D}(${o}, coords)); + return value; + } + `}getPackedValueFrom(o,t,r,n,s){let a=`_${o}_Pack`;s&&(a=a+"_T");let u=D(this.context.glContext.version);return` + vec4 ${a}(int m[${t}]) { + int offset = indicesToOffset_${o}(m); + vec2 coords = offsetToCoords(offset, ${r}, ${n}); + return ${u.texture2D}(${o}, coords); + } + `}}});var tn,gf=x(()=>{"use strict";Xt();tn=class i extends At{constructor(e){super(e)}getFunctions(){return{...this.encodeFloat32(),...this.decodeFloat32()}}getCustomTypes(){return{}}encodeFloat32(){return{encode:new O(`highp vec4 encode(highp float f) { + return vec4(f, 0.0, 0.0, 0.0); + } + `)}}decodeFloat32(){return{decode:new O(`highp float decode(highp vec4 rgba) { + return rgba.r; + } + `)}}encodeUint8(){let e=i.isLittleEndian()?"rgba.rgba=rgba.abgr;":"";return{encode:new O(` + highp vec4 encode(highp float f) { + highp float F = abs(f); + highp float Sign = step(0.0,-f); + highp float Exponent = floor(log2(F)); + highp float Mantissa = (exp2(- Exponent) * F); + Exponent = floor(log2(F) + 127.0) + floor(log2(Mantissa)); + highp vec4 rgba; + rgba[0] = 128.0 * Sign + floor(Exponent*exp2(-1.0)); + rgba[1] = 128.0 * mod(Exponent,2.0) + mod(floor(Mantissa*128.0),128.0); + rgba[2] = floor(mod(floor(Mantissa*exp2(23.0 -8.0)),exp2(8.0))); + rgba[3] = floor(exp2(23.0)*mod(Mantissa,exp2(-15.0))); + ${e} + rgba = rgba / 255.0; // values need to be normalized to [0,1] + return rgba; + } + `)}}decodeUint8(){let e=i.isLittleEndian()?"rgba.rgba=rgba.abgr;":"";return{decode:new O(` + highp float decode(highp vec4 rgba) { + rgba = rgba * 255.0; // values need to be de-normalized from [0,1] to [0,255] + ${e} + highp float Sign = 1.0 - step(128.0,rgba[0])*2.0; + highp float Exponent = 2.0 * mod(rgba[0],128.0) + step(128.0,rgba[1]) - 127.0; + highp float Mantissa = mod(rgba[1],128.0)*65536.0 + rgba[2]*256.0 +rgba[3] + float(0x800000); + highp float Result = Sign * exp2(Exponent) * (Mantissa * exp2(-23.0 )); + return Result; + } + `)}}static isLittleEndian(){let e=new ArrayBuffer(4),o=new Uint32Array(e),t=new Uint8Array(e);if(o[0]=3735928559,t[0]===239)return!0;if(t[0]===222)return!1;throw new Error("unknown endianness")}}});var en,xf=x(()=>{"use strict";Xt();X();en=class extends At{constructor(e){super(e)}getFunctions(){return{...this.setFragColor(),...this.getColorAsFloat()}}getCustomTypes(){return{}}setFragColor(){let e=D(this.context.glContext.version);return{setFragColor:new O(` + void setFragColor(float value) { + ${e.output} = encode(value); + } + `,["encoding.encode"])}}getColorAsFloat(){return{getColorAsFloat:new O(` + float getColorAsFloat(vec4 color) { + return decode(color); + } + `,["encoding.decode"])}}}});var rn,Tf=x(()=>{"use strict";Xt();rn=class i extends At{constructor(e){super(e)}getFunctions(){return{...this.bcastIndex(),...this.bcastMatmulIndex(),...this.offsetToIndices(),...this.indicesToOffset(),...this.incrementIndices()}}getCustomTypes(){return{}}bcastIndex(){let e=this.context.outputTextureLayout.shape.length,o={};return this.context.programInfo.inputNames.forEach((t,r)=>{let n=this.context.inputTextureLayouts[r].unpackedShape;if(n.length<=e){let s=n.length,a=e-s,u=`bcastIndices_${t}`,l="";for(let c=0;c{let n=this.context.inputTextureLayouts[r].shape;if(!(n.length<2||n.length>e)){let s=n.length,a=e-s,u=`bcastMatmulIndices_${t}`,l="";for(let c=0;c{let r=this.context.inputTextureLayouts[t].shape,n=this.context.inputTextureLayouts[t].strides,s=r.length,a=`indicesToOffset_${o}`;e[a]=new O(i.indexToOffsetSingle(a,s,n)),a=`indicesToOffset_${o}_T`,e[a]=new O(i.indexToOffsetSingle(a,s,n.slice().reverse()))}),e}static indexToOffsetSingle(e,o,t){let r="";for(let n=o-1;n>=0;--n)r+=` + offset += indices[${n}] * ${t[n]}; + `;return` + int ${e}(int indices[${o}]) { + int offset = 0; + ${r} + return offset; + } + `}offsetToIndices(){let e={};return this.context.programInfo.inputNames.forEach((o,t)=>{let r=this.context.inputTextureLayouts[t].shape,n=this.context.inputTextureLayouts[t].strides,s=r.length,a=`offsetToIndices_${o}`;e[a]=new O(i.offsetToIndicesSingle(a,s,n)),a=`offsetToIndices_${o}_T`,e[a]=new O(i.offsetToIndicesSingle(a,s,n.slice().reverse()))}),e}static offsetToIndicesSingle(e,o,t){let r=[];for(let n=0;n{let r=this.context.inputTextureLayouts[t].shape,n=r.length,s=`incrementIndices_${o}`,a="";for(let l=0;l= 0; --i) { + if(i > axis) continue; + indices[i] += 1; + if(indices[i] < shape[i]) { + break; + } + indices[i] = 0; + } + } + `;e[s]=new O(u)}),e}}});var nn,wf=x(()=>{"use strict";Xt();nn=class extends At{constructor(e){super(e)}getCustomTypes(){return{}}getFunctions(){return{...this.binaryVecFunctions(),...this.copyVec(),...this.setVecItem(),...this.getVecItem()}}binaryVecFunctions(){let o=this.context.outputTextureLayout.shape.length,t={add:"+=",sub:"-=",mul:"*=",div:"/="},r={};for(let n in t){let s=`${n}Vec`,a="";for(let l=0;l{"use strict";yf();gf();xf();Tf();wf();Ro={encoding:tn,fragcolor:en,vec:nn,shapeUtils:rn,coordinates:Qr}});var on,If=x(()=>{"use strict";Xt();mf();vf();X();on=class{constructor(e,o,t,r){this.libs={};this.glslLibRoutineDependencyGraph={};this.context=new Ur(e,o,t,r),Object.keys(Ro).forEach(s=>{let a=new Ro[s](this.context);this.libs[s]=a});let n=this.glslLibRoutineDependencyGraph;for(let s in this.libs){let u=this.libs[s].getFunctions();for(let l in u){let f=s+"."+l,c;n[f]?(c=n[f],c.routineBody=u[l].routineBody):(c=new rr(f,u[l].routineBody),n[f]=c);let d=u[l].dependencies;if(d)for(let m=0;m{let r=t.split(".")[1];e.indexOf(r)!==-1&&o.push(this.glslLibRoutineDependencyGraph[t])}),zr.returnOrderedNodes(o)}getUniforms(e,o){let t=[];if(e)for(let r of e)t.push(`uniform sampler2D ${r};`);if(o)for(let r of o)t.push(`uniform ${r.type} ${r.name}${r.arrayLength?`[${r.arrayLength}]`:""};`);return t.join(` +`)}}});var an,_f=x(()=>{"use strict";kt();It();If();X();an=class{constructor(e,o,t){this.profiler=e;this.glContext=o;this.textureLayoutStrategy=t;this.repo=new Map,this.attributesBound=!1}getArtifact(e){return this.repo.get(e)}setArtifact(e,o){this.repo.set(e,o)}run(e,o,t){this.profiler.event("op",`ProgramManager.run ${e.programInfo.name??"unknown kernel"}`,()=>{let r=this.glContext.gl,n=e.program;r.useProgram(n);try{this.bindOutput(t),this.attributesBound||this.bindAttributes(e.attribLocations),this.bindUniforms(e.uniformLocations,e.programInfo.variables??[],o)}catch(s){throw z.error("ProgramManager",e.programInfo.shaderSource),s}this.profiler.event("backend","GlContext.draw()",()=>{this.glContext.draw()})},this.glContext)}dispose(){this.vertexShader&&this.glContext.deleteShader(this.vertexShader),this.repo.forEach(e=>this.glContext.deleteProgram(e.program))}build(e,o,t){return this.profiler.event("backend","ProgramManager.build",()=>{let r=new on(this.glContext,e,o,t),n=r.preprocess(),s=this.compile(n);return{programInfo:e,program:s,uniformLocations:this.getUniformLocations(s,r.context.programInfo.inputNames,r.context.programInfo.variables),attribLocations:this.getAttribLocations(s)}})}compile(e){if(!this.vertexShader){z.verbose("ProrgramManager","Compiling and caching Vertex shader for the first time");let r=ts(this.glContext.version);this.vertexShader=this.glContext.compileShader(r,this.glContext.gl.VERTEX_SHADER)}$.debug&&z.verbose("ProrgramManager",`FragShader: +${e} +`);let o=this.glContext.compileShader(e,this.glContext.gl.FRAGMENT_SHADER),t=this.glContext.createProgram(this.vertexShader,o);return this.glContext.deleteShader(o),t}bindOutput(e){let o=e.width,t=e.height;z.verbose("ProrgramManager",`Binding output texture to Framebuffer: w/h=${o}/${t}, shape=${e.shape}, type=${e.tensor.type}`),this.glContext.attachFramebuffer(e.texture,o,t)}bindAttributes(e){let o=e.position,t=e.textureCoord;this.glContext.setVertexAttributes(o,t),this.attributesBound=!0}bindUniforms(e,o,t){let r=this.glContext.gl,n=0;for(let{name:s,type:a,location:u,arrayLength:l}of e){let f=o.find(c=>c.name===s)?.data;if(a!=="sampler2D"&&!f)throw new Error(`variable '${s}' does not have data defined in program info`);switch(a){case"sampler2D":this.bindTexture(t[n],u,n),n++;break;case"float":l?r.uniform1fv(u,f):r.uniform1f(u,f);break;case"int":l?r.uniform1iv(u,f):r.uniform1i(u,f);break;default:throw new Error(`Uniform not implemented: ${a}`)}}}bindTexture(e,o,t){this.glContext.bindTextureToUniform(e.texture,t,o)}getAttribLocations(e){return{position:this.getAttribLocation(e,"position"),textureCoord:this.getAttribLocation(e,"textureCoord")}}getUniformLocations(e,o,t){let r=[];if(o)for(let n of o)r.push({name:n,type:"sampler2D",location:this.getUniformLocation(e,n)});if(t)for(let n of t)r.push({...n,location:this.getUniformLocation(e,n.name)});return r}getUniformLocation(e,o){let r=this.glContext.gl.getUniformLocation(e,o);if(r===null)throw new Error(`Uniform ${o} not found.`);return r}getAttribLocation(e,o){return this.glContext.gl.getAttribLocation(e,o)}}});var sn,Sf=x(()=>{"use strict";It();tr();sn=class{constructor(e,o,t,r){this.glContext=e;this.layoutStrategy=o;this.profiler=t;this.config=r;this.pendingRead=new Map;r.reuseTextures&&(this.inUseTextures=new Map,this.idleTextures=new Map,this.textureLookup=new Map)}createTextureFromLayout(e,o,t,r){let n=this.toEncoderType(e),s=this.glContext.getEncoder(n,o.channels||1,r);if(o.isPacked&&r===1)throw new Error("not implemented");let a=o.width,u=o.height,l,f;if(this.config.reuseTextures){l=`${a}x${u}_${s.format}_${s.internalFormat}_${s.textureType}`,f=this.inUseTextures.get(l),f||(f=[],this.inUseTextures.set(l,f));let d=this.idleTextures.get(l);if(d&&d.length>0){let m=d.pop();return f.push(m),r===1&&this.glContext.updateTexture(m,a,u,s,this.toTextureData(e,t)),m}}z.verbose("TextureManager",`Creating new texture of size ${o.width}x${o.height}`);let c=this.glContext.allocateTexture(a,u,s,this.toTextureData(e,t));return this.config.reuseTextures&&(f.push(c),this.textureLookup.set(c,l)),c}readTexture(e,o,t){return t||(t=1),this.profiler.event("backend","TextureManager.readTexture",()=>{let r=e.shape.reduce((s,a)=>s*a)*t,n=this.glContext.readTexture(e.texture,e.width,e.height,r,this.toEncoderType(o),t);return this.toTensorData(o,n)})}async readTextureAsync(e,o,t){let r=e.tensor.dataId;if(t||(t=1),this.pendingRead.has(r)){let n=this.pendingRead.get(r);return new Promise(s=>n?.push(s))}return this.profiler.event("backend","TextureManager.readTextureAsync",async()=>{this.pendingRead.set(r,[]);let n=e.shape.reduce((l,f)=>l*f)*t;await this.glContext.createAndWaitForFence();let s=this.glContext.readTexture(e.texture,e.width,e.height,n,this.toEncoderType(o),t),a=this.toTensorData(o,s),u=this.pendingRead.get(r);return this.pendingRead.delete(r),u?.forEach(l=>l(a)),a})}readUint8TextureAsFloat(e){return this.profiler.event("backend","TextureManager.readUint8TextureAsFloat",()=>{let o=e.shape.reduce((r,n)=>r*n),t=this.glContext.readTexture(e.texture,e.width,e.height,o*4,"byte",4);return new Float32Array(t.buffer,t.byteOffset,o)})}releaseTexture(e,o){let t;if(this.config.reuseTextures&&(t=this.textureLookup.get(e.texture),t)){o&&this.textureLookup.delete(t);let r=this.inUseTextures.get(t);if(r){let n=r.indexOf(e.texture);if(n!==-1){r.splice(n,1);let s=this.idleTextures.get(t);s||(s=[],this.idleTextures.set(t,s)),s.push(e.texture)}}}(!t||o)&&(z.verbose("TextureManager",`Deleting texture of size ${e.width}x${e.height}`),this.glContext.deleteTexture(e.texture))}toTensorData(e,o){switch(e){case"int16":return o instanceof Int16Array?o:Int16Array.from(o);case"int32":return o instanceof Int32Array?o:Int32Array.from(o);case"int8":return o instanceof Int8Array?o:Int8Array.from(o);case"uint16":return o instanceof Uint16Array?o:Uint16Array.from(o);case"uint32":return o instanceof Uint32Array?o:Uint32Array.from(o);case"uint8":case"bool":return o instanceof Uint8Array?o:Uint8Array.from(o);case"float32":return o instanceof Float32Array?o:Float32Array.from(o);case"float64":return o instanceof Float64Array?o:Float64Array.from(o);default:throw new Error(`TensorData type ${e} is not supported`)}}toTextureData(e,o){if(o)return o instanceof Float32Array?o:new Float32Array(o)}toEncoderType(e){return"float"}clearActiveTextures(){this.glContext.clearActiveTextures()}}});var un,Of=x(()=>{"use strict";It();Mi();ys();pf();_f();Co();Sf();un=class{constructor(e,o){this.backend=e;this.context=o;this.layoutStrategy=new Yr(e.glContext.maxTextureSize),this.programManager=new an(this.context.profiler,e.glContext,this.layoutStrategy),this.textureManager=new sn(e.glContext,this.layoutStrategy,this.context.profiler,{reuseTextures:e.textureCacheMode==="full"}),this.packedTextureDataCache=new Map,this.unpackedTextureDataCache=new Map,this.pack=e.pack,this.pack2unpackMap=new Map,this.unpack2packMap=new Map}createInferenceHandler(){return new Vr(this)}onGraphInitialized(e){let o=e.getValues().filter(t=>t.from===-1&&t.tensor).map(t=>t.tensor.dataId);this.initializers=new Set(o)}isInitializer(e){return this.initializers?this.initializers.has(e):!1}addInitializer(e){this.initializers.add(e)}getTextureData(e,o){return o?this.packedTextureDataCache.get(e):this.unpackedTextureDataCache.get(e)}setTextureData(e,o,t=!1){z.verbose("WebGLSessionHandler","Storing Texture data in cache"),t?this.packedTextureDataCache.set(e,o):this.unpackedTextureDataCache.set(e,o)}dispose(){this.programManager.dispose(),this.textureManager.clearActiveTextures(),this.packedTextureDataCache.forEach(e=>this.textureManager.releaseTexture(e,!0)),this.packedTextureDataCache=new Map,this.unpackedTextureDataCache.forEach(e=>this.textureManager.releaseTexture(e,!0)),this.unpackedTextureDataCache=new Map}resolve(e,o,t){let r=Gi(e,o,cf);return{impl:r.opImpl,context:r.opInit?r.opInit(e,t):e}}}});function qh(i){let e=0;for(;e{"use strict";kt();tr();tr();Vt();ir=class{constructor(e,o){this.frameBufferBound=!1;this.itemsToPoll=[];this.gl=e,this.version=o,this.getExtensions(),this.vertexbuffer=this.createVertexbuffer(),this.framebuffer=this.createFramebuffer(),this.queryVitalParameters()}allocateTexture(e,o,t,r){let n=this.gl,s=n.createTexture();n.bindTexture(n.TEXTURE_2D,s),n.texParameteri(n.TEXTURE_2D,n.TEXTURE_MIN_FILTER,n.NEAREST),n.texParameteri(n.TEXTURE_2D,n.TEXTURE_MAG_FILTER,n.NEAREST),n.texParameteri(n.TEXTURE_2D,n.TEXTURE_WRAP_S,n.CLAMP_TO_EDGE),n.texParameteri(n.TEXTURE_2D,n.TEXTURE_WRAP_T,n.CLAMP_TO_EDGE);let a=r?t.encode(r,e*o):null;return n.texImage2D(n.TEXTURE_2D,0,t.internalFormat,e,o,0,t.format,t.textureType,a),this.checkError(),s}updateTexture(e,o,t,r,n){let s=this.gl;s.bindTexture(s.TEXTURE_2D,e);let a=r.encode(n,o*t);s.texSubImage2D(s.TEXTURE_2D,0,0,0,o,t,r.format,r.textureType,a),this.checkError()}attachFramebuffer(e,o,t){let r=this.gl;r.bindTexture(r.TEXTURE_2D,e),r.bindFramebuffer(r.FRAMEBUFFER,this.framebuffer),r.framebufferTexture2D(r.FRAMEBUFFER,r.COLOR_ATTACHMENT0,r.TEXTURE_2D,e,0),this.checkError(),r.viewport(0,0,o,t),r.scissor(0,0,o,t)}readTexture(e,o,t,r,n,s){let a=this.gl;s||(s=1),this.frameBufferBound||this.attachFramebuffer(e,o,t);let u=this.getEncoder(n,s),l=u.allocate(o*t);return a.bindTexture(a.TEXTURE_2D,e),a.framebufferTexture2D(a.FRAMEBUFFER,a.COLOR_ATTACHMENT0,a.TEXTURE_2D,e,0),a.readPixels(0,0,o,t,a.RGBA,u.textureType,l),this.checkError(),u.decode(l,r)}isFramebufferReady(){return!0}getActiveTexture(){let e=this.gl;return`TEXTURE${e.getParameter(this.gl.ACTIVE_TEXTURE)-e.TEXTURE0}`}getTextureBinding(){return this.gl.getParameter(this.gl.TEXTURE_BINDING_2D)}getFramebufferBinding(){return this.gl.getParameter(this.gl.FRAMEBUFFER_BINDING)}setVertexAttributes(e,o){let t=this.gl;t.vertexAttribPointer(e,3,t.FLOAT,!1,20,0),t.enableVertexAttribArray(e),o!==-1&&(t.vertexAttribPointer(o,2,t.FLOAT,!1,20,12),t.enableVertexAttribArray(o)),this.checkError()}createProgram(e,o){let t=this.gl,r=t.createProgram();return t.attachShader(r,e),t.attachShader(r,o),t.linkProgram(r),r}compileShader(e,o){let t=this.gl,r=t.createShader(o);if(!r)throw new Error(`createShader() returned null with type ${o}`);if(t.shaderSource(r,e),t.compileShader(r),t.getShaderParameter(r,t.COMPILE_STATUS)===!1)throw new Error(`Failed to compile shader: ${t.getShaderInfoLog(r)} +Shader source: +${e}`);return r}deleteShader(e){this.gl.deleteShader(e)}bindTextureToUniform(e,o,t){let r=this.gl;r.activeTexture(r.TEXTURE0+o),this.checkError(),r.bindTexture(r.TEXTURE_2D,e),this.checkError(),r.uniform1i(t,o),this.checkError()}draw(){this.gl.drawArrays(this.gl.TRIANGLE_STRIP,0,4),this.checkError()}checkError(){if($.debug){let e=this.gl,o=e.getError(),t="";switch(o){case e.NO_ERROR:return;case e.INVALID_ENUM:t="INVALID_ENUM";break;case e.INVALID_VALUE:t="INVALID_VALUE";break;case e.INVALID_OPERATION:t="INVALID_OPERATION";break;case e.INVALID_FRAMEBUFFER_OPERATION:t="INVALID_FRAMEBUFFER_OPERATION";break;case e.OUT_OF_MEMORY:t="OUT_OF_MEMORY";break;case e.CONTEXT_LOST_WEBGL:t="CONTEXT_LOST_WEBGL";break;default:t=`Unknown WebGL Error: ${o.toString(16)}`}throw new Error(t)}}deleteTexture(e){this.gl.deleteTexture(e)}deleteProgram(e){this.gl.deleteProgram(e)}getEncoder(e,o,t=0){if(this.version===2)return new Gr(this.gl,o);switch(e){case"float":return t===1||this.isRenderFloat32Supported?new Qe(this.gl,o):new Qe(this.gl,o,this.textureHalfFloatExtension.HALF_FLOAT_OES);case"int":throw new Error("not implemented");case"byte":return new Mr(this.gl,o);default:throw new Error(`Invalid dataType: ${e}`)}}clearActiveTextures(){let e=this.gl;for(let o=0;othis.isTimerResultAvailable(e)),this.getTimerResult(e)}async createAndWaitForFence(){let e=this.createFence(this.gl);return this.pollFence(e)}createFence(e){let o,t=e,r=t.fenceSync(t.SYNC_GPU_COMMANDS_COMPLETE,0);return e.flush(),r===null?o=()=>!0:o=()=>{let n=t.clientWaitSync(r,0,0);return n===t.ALREADY_SIGNALED||n===t.CONDITION_SATISFIED},{query:r,isFencePassed:o}}async pollFence(e){return new Promise(o=>{this.addItemToPoll(()=>e.isFencePassed(),()=>o())})}pollItems(){let e=qh(this.itemsToPoll.map(o=>o.isDoneFn));for(let o=0;o<=e;++o){let{resolveFn:t}=this.itemsToPoll[o];t()}this.itemsToPoll=this.itemsToPoll.slice(e+1)}async addItemToPoll(e,o){this.itemsToPoll.push({isDoneFn:e,resolveFn:o}),!(this.itemsToPoll.length>1)&&await uo(()=>(this.pollItems(),this.itemsToPoll.length===0))}}});function Go(i){let e;if((!i||i==="webgl2")&&"webgl2"in Ge?e=Ge.webgl2:(!i||i==="webgl")&&"webgl"in Ge&&(e=Ge.webgl),!e)try{let t=Xh();e=Pf(t,i)}catch{let r=jh();e=Pf(r,i)}i=i||e.version===1?"webgl":"webgl2";let o=e.gl;return Ge[i]=e,o.isContextLost()?(delete Ge[i],Go(i)):(o.disable(o.DEPTH_TEST),o.disable(o.STENCIL_TEST),o.disable(o.BLEND),o.disable(o.DITHER),o.disable(o.POLYGON_OFFSET_FILL),o.disable(o.SAMPLE_COVERAGE),o.enable(o.SCISSOR_TEST),o.enable(o.CULL_FACE),o.cullFace(o.BACK),e)}function Pf(i,e){let o={alpha:!1,depth:!1,antialias:!1,stencil:!1,preserveDrawingBuffer:!1,premultipliedAlpha:!1,failIfMajorPerformanceCaveat:!1},t,r=o;if((!e||e==="webgl2")&&(t=i.getContext("webgl2",r),t))try{return new ir(t,2)}catch(n){z.warning("GlContextFactory",`failed to create WebGLContext using contextId 'webgl2'. Error: ${n}`)}if((!e||e==="webgl")&&(t=i.getContext("webgl",r)||i.getContext("experimental-webgl",r),t))try{return new ir(t,1)}catch(n){z.warning("GlContextFactory",`failed to create WebGLContext using contextId 'webgl' or 'experimental-webgl'. Error: ${n}`)}throw new Error("WebGL is not supported")}function jh(){if(typeof document>"u")throw new TypeError("failed to create canvas: document is not supported");let i=document.createElement("canvas");return i.width=1,i.height=1,i}function Xh(){if(typeof OffscreenCanvas>"u")throw new TypeError("failed to create offscreen canvas: OffscreenCanvas is not supported");return new OffscreenCanvas(1,1)}var Ge,Ef=x(()=>{"use strict";It();Af();Ge={}});var ln,Df=x(()=>{"use strict";kt();It();Of();Ef();ln=class{get contextId(){return $.webgl.contextId}set contextId(e){$.webgl.contextId=e}get matmulMaxBatchSize(){return $.webgl.matmulMaxBatchSize}set matmulMaxBatchSize(e){$.webgl.matmulMaxBatchSize=e}get textureCacheMode(){return $.webgl.textureCacheMode}set textureCacheMode(e){$.webgl.textureCacheMode=e}get pack(){return $.webgl.pack}set pack(e){$.webgl.pack=e}get async(){return $.webgl.async}set async(e){$.webgl.async=e}initialize(){try{return this.glContext=Go(this.contextId),typeof this.matmulMaxBatchSize!="number"&&(this.matmulMaxBatchSize=16),typeof this.textureCacheMode!="string"&&(this.textureCacheMode="full"),typeof this.pack!="boolean"&&(this.pack=!1),typeof this.async!="boolean"&&(this.async=!1),z.setWithEnv($),$.webgl.context||Object.defineProperty($.webgl,"context",{value:this.glContext.gl}),z.verbose("WebGLBackend",`Created WebGLContext: ${typeof this.glContext} with matmulMaxBatchSize: ${this.matmulMaxBatchSize}; textureCacheMode: ${this.textureCacheMode}; pack: ${this.pack}; async: ${this.async}.`),!0}catch(e){return z.warning("WebGLBackend",`Unable to initialize WebGLBackend. ${e}`),!1}}createSessionHandler(e){return new un(this,e)}dispose(){this.glContext.dispose()}}});async function Mo(i){if(i){let e=typeof i=="string"?[i]:i;for(let o of e){let t=Lf.get(o);if(t)return t;let r=await Jh(o);if(r)return r}}else return Mo(["webgl"]);throw new Error("no available backend to use")}async function Jh(i){let e=Kh;if(typeof e[i]<"u"&&Zh(e[i])){let o=e[i],t=o.initialize();if(typeof t=="object"&&"then"in t&&(t=await t),t)return Lf.set(i,o),o}}function Zh(i){let e=i;return"initialize"in e&&typeof e.initialize=="function"&&"createSessionHandler"in e&&typeof e.createSessionHandler=="function"&&"dispose"in e&&typeof e.dispose=="function"}var Lf,Kh,$f=x(()=>{"use strict";Df();Lf=new Map,Kh={webgl:new ln}});var Vo,fn,kf=x(()=>{"use strict";It();Vo=class{constructor(e,o){this.op=e;this.node=o}},fn=class{constructor(e,o,t){this.graph=e;this.profiler=t;this.initialize(o)}initialize(e){this.profiler.event("session","ExecutionPlan.initialize",()=>{let o=this.graph.getNodes();if(o.length!==e.length)throw new Error("The size of nodes and OPs do not match.");this._ops=e.map((t,r)=>new Vo(t,o[r])),this.reset(),this._starter=[],this._ops.forEach((t,r)=>{let n=!0;for(let s of t.node.inputs)if(!this._values[s]&&this.graph.getInputIndices().indexOf(s)===-1){n=!1;break}n&&this._starter.push(r)})})}reset(){this._values=this.graph.getValues().map(e=>e.tensor)}async execute(e,o){return this.profiler.event("session","ExecutionPlan.execute",async()=>{this.reset();let t=e.createInferenceHandler(),r=this.graph.getInputIndices();if(o.length!==r.length)throw new Error(`number of input tensors don't match the number of inputs to the model: actual: ${o.length} expected: ${r.length}`);o.forEach((f,c)=>{let d=r[c];this._values[d]=f});let n=this._starter.slice(0),s=this.graph.getValues(),a=this.graph.getNodes(),u=0;for(;uthis._values[T]);if(d.indexOf(void 0)!==-1)throw new Error(`unresolved input detected: op: ${c.node}`);let m=d;z.verbose("ExecPlan",`Running op:${c.node.name} (${m.map((T,_)=>`'${c.node.inputs[_]}': ${T.type}[${T.dims.join(",")}]`).join(", ")})`);let y=await this.profiler.event("node",c.node.name,async()=>c.op.impl(t,m,c.op.context));if(y.length!==c.node.outputs.length)throw new Error("the size of output does not match model definition.");y.forEach((T,_)=>{let w=c.node.outputs[_];if(this._values[w])throw new Error(`output [${w}] already has value: op:${c.node.name}`);this._values[w]=T});let g=new Set;y.forEach((T,_)=>{let w=c.node.outputs[_];for(let v of s[w].to){let L=a[v],B=!0;for(let Q of L.inputs)if(!this._values[Q]){B=!1;break}B&&g.add(v)}}),n.push(...g)}let l=[];for(let f=0;f{"use strict";je();C=Ae(Le());ye();U();Dt=P.experimental.fbs,ar=class i{constructor(e){if(this._attributes=new Map,e!=null){for(let o of e)o instanceof C.onnx.AttributeProto?this._attributes.set(o.name,[i.getValue(o),i.getType(o)]):o instanceof Dt.Attribute&&this._attributes.set(o.name(),[i.getValue(o),i.getType(o)]);if(this._attributes.sizert.fromProto(n));if(e instanceof Dt.Attribute)return t.map(n=>rt.fromOrtTensor(n))}return o===C.onnx.AttributeProto.AttributeType.STRING&&e instanceof C.onnx.AttributeProto?Ye(t):o===C.onnx.AttributeProto.AttributeType.STRINGS&&e instanceof C.onnx.AttributeProto?t.map(Ye):t}static getValueNoCheck(e){return e instanceof C.onnx.AttributeProto?this.getValueNoCheckFromOnnxFormat(e):this.getValueNoCheckFromOrtFormat(e)}static getValueNoCheckFromOnnxFormat(e){switch(e.type){case C.onnx.AttributeProto.AttributeType.FLOAT:return e.f;case C.onnx.AttributeProto.AttributeType.INT:return e.i;case C.onnx.AttributeProto.AttributeType.STRING:return e.s;case C.onnx.AttributeProto.AttributeType.TENSOR:return e.t;case C.onnx.AttributeProto.AttributeType.GRAPH:return e.g;case C.onnx.AttributeProto.AttributeType.FLOATS:return e.floats;case C.onnx.AttributeProto.AttributeType.INTS:return e.ints;case C.onnx.AttributeProto.AttributeType.STRINGS:return e.strings;case C.onnx.AttributeProto.AttributeType.TENSORS:return e.tensors;case C.onnx.AttributeProto.AttributeType.GRAPHS:return e.graphs;default:throw new Error(`unsupported attribute type: ${C.onnx.AttributeProto.AttributeType[e.type]}`)}}static getValueNoCheckFromOrtFormat(e){switch(e.type()){case Dt.AttributeType.FLOAT:return e.f();case Dt.AttributeType.INT:return e.i();case Dt.AttributeType.STRING:return e.s();case Dt.AttributeType.TENSOR:return e.t();case Dt.AttributeType.GRAPH:return e.g();case Dt.AttributeType.FLOATS:return e.floatsArray();case Dt.AttributeType.INTS:{let o=[];for(let t=0;t{"use strict";Bf();je();zo=Ae(Le());ye();U();cn=P.experimental.fbs,Wo={from:(i,e)=>new Uo(i,e)},Wt=class{constructor(e){this._from=void 0,this._to=[],this.tensor=void 0,this.type=void 0,e&&(this.type=ut.tensorValueTypeFromProto(e.type.tensorType))}get from(){return this._from}get to(){return this._to}},pn=class{constructor(e,o){e instanceof zo.onnx.NodeProto?(this.name=e.name,this.opType=e.opType,this.attributes=new ar(e.attribute)):e instanceof cn.Node&&(this.name=o??e.name(),this.opType=e.opType(),this.attributes=new ar(ut.tensorAttributesFromORTFormat(e))),this.inputs=[],this.outputs=[],this.executeNode=!0}},Uo=class{constructor(e,o){if(!e)throw new TypeError("graph is empty");this.buildGraph(e),this.transformGraph(o),this.checkIsAcyclic()}getInputIndices(){return this._allInputIndices}getInputNames(){return this._allInputNames}getOutputIndices(){return this._allOutputIndices}getOutputNames(){return this._allOutputNames}getValues(){return this._allData}getNodes(){return this._nodes}buildGraph(e){if(e instanceof zo.onnx.GraphProto)this.buildGraphFromOnnxFormat(e);else if(e instanceof cn.Graph)this.buildGraphFromOrtFormat(e);else throw new TypeError("Graph type is not supported.")}buildGraphFromOnnxFormat(e){let o=new Map;this._allData=[],this._allInputIndices=[],this._allInputNames=[],this._allOutputIndices=[],this._allOutputNames=[],this._nodes=[];let t=new Map;if(!e.input)throw new Error("missing information in graph: input");let r=[];for(let n of e.input){if(o.has(n.name))throw new Error(`duplicated input name: ${n.name}`);let s=this._allData.push(new Wt(n))-1;o.set(n.name,s),r.push(n.name)}if(!e.initializer)throw new Error("missing information in graph: initializer");for(let n of e.initializer){let s=o.get(n.name);if(s===void 0){let a=new Wt;a.type={shape:{dims:ut.tensorDimsFromProto(n.dims)},tensorType:ut.tensorDataTypeFromProto(n.dataType)},s=this._allData.push(a)-1,o.set(n.name,s)}this._allData[s]._from=-1,this._allData[s].tensor=rt.fromProto(n)}for(let n=0;n"u"&&(l=this._allData.push(new Wt)-1,o.set(u,l)),s.outputs.push(l),this._allData[l]._from!==void 0)throw new Error(`multiple nodes output to one data value: ${l}`);if(this._allData[l]._from=n,a.opType==="Constant"){if(!a.attribute||a.attribute.length!==1||!a.attribute[0].t)throw new Error("missing attributes or missing tensor value in attributes for this Constant operator");if(!a.output||a.output.length!==1)throw new Error("missing output or incorrect number of outputs for this Constant operator");s.outputs.pop(),s.executeNode=!1,this._allData[l]._from=-1,this._allData[l].tensor=rt.fromProto(a.attribute[0].t)}}}for(let n=0;n"u"){if(u===""&&(a.input.length===3||a.input.length===4)&&a.opType==="Resize")continue;throw new Error(`unrecognized input '${u}' for node: ${a.name}`)}s.inputs.push(l),this._allData[l]._to.push(n)}}return!0}buildGraphFromOrtFormat(e){let o=new Map;this._allData=[],this._allInputIndices=[],this._allInputNames=[],this._allOutputIndices=[],this._allOutputNames=[],this._nodes=[];let t=new Map,r=[];for(let n=0;n"u"&&(f=this._allData.push(new Wt)-1,o.set(l,f)),s.outputs.push(f),this._allData[f]._from!==void 0)throw new Error(`multiple nodes output to one data value: ${f}`);if(this._allData[f]._from=n,a.opType()==="Constant"){if(a.attributesLength()!==1||!a.attributes(0).t())throw new Error("missing attributes or missing tensor value in attributes for this Constant operator");if(a.outputsLength()!==1)throw new Error("missing output or incorrect number of outputs for this Constant operator");s.outputs.pop(),s.executeNode=!1,this._allData[f]._from=-1,this._allData[f].tensor=rt.fromOrtTensor(a.attributes(0).t())}}}for(let n=0;n"u")throw new Error(`unrecognized input '${l}' for node: ${a.name()}`);s.inputs.push(f),this._allData[f]._to.push(n)}}}checkIsAcyclic(){let e=new Set;this._allInputIndices.forEach(r=>{this._allData[r]._to.forEach(s=>{e.add(s)})});let o=Array.from(e),t=new Array(this._nodes.length).fill("white");for(;o.length>0;){let r=o.pop();t[r]==="gray"?t[r]="black":(o.push(r),t[r]="gray",this._nodes[r].outputs.forEach(n=>{let s=this._allData[n];if(typeof s.tensor<"u")throw new Error("node outputs should not be initialized");if(s._from!==r)throw new Error("from property of the Value object doesn't match index of Node being processed");s._to.forEach(a=>{if(t[a]==="gray")throw new Error("model graph is cyclic");t[a]==="white"&&o.push(a)})}))}}transformGraph(e){this.removeAllIdentityNodes(),this.removeAllDropoutNodes(),this.fuseConvActivationNodes(),e&&e.transformGraph(this),this.finalizeGraph()}finalizeGraph(){let e=0,o=new Array(this._nodes.length,0),t=0;for(let r=0;r{this._allData[n]._from=-2});this._nodes.splice(t,this._nodes.length-t);for(let r=0;r=0)n._to[s]=o[n._to[s]];else throw new Error("Trying to update a removed node")}e=0;for(let r=0;r0){let n=-1;this._allData[r].from!==void 0&&this._allData[r].from!==-1?(n=this._nodes[this._allData[r].from].outputs.indexOf(r+e),n!==-1&&(this._nodes[this._allData[r].from].outputs[n]=r)):(n=this._allInputIndices.indexOf(r+e),n!==-1&&(this._allInputIndices[n]=r)),this._allData[r].to.forEach(s=>{n=this._nodes[s].inputs.indexOf(r+e),n!==-1&&(this._nodes[s].inputs[n]=r)}),this._allData[r].to.length===0&&(n=this._allOutputIndices.indexOf(r+e),n!==-1&&(this._allOutputIndices[n]=r))}}}deleteNode(e){let o=this._nodes[e];if(o.outputs.length>1){for(let a=1;a0)throw new Error("Node deletion with more than one output connected to other nodes is not supported. ")}o.executeNode=!1;let t=o.inputs[0],r=o.outputs[0],n=this._allData[r].to;for(let a=0;a0)for(let a of n){let u=this._nodes[a].inputs.indexOf(r);if(u===-1)throw new Error("The Node object doesn't have the output Value in it's 'inputs' property ");this._nodes[a].inputs[u]=t,this._allData[t].to.push(a)}}removeAllDropoutNodes(){let e=0;for(let o of this._nodes){if(o.opType==="Dropout"){if(o.inputs.length!==1)throw new Error("Dropout nodes should only contain one input. ");if(o.outputs.length!==1&&o.outputs.length!==2)throw new Error("Dropout nodes should contain either 1 or 2 output(s)");if(o.outputs.length===2&&this._allData[o.outputs[1]]._to.length!==0)throw new Error("Dropout nodes's second output should not be referenced by other nodes");this.deleteNode(e)}e++}}removeAllIdentityNodes(){let e=0;for(let o of this._nodes)o.opType==="Identity"&&this.deleteNode(e),e++}isActivation(e){switch(e.opType){case"Relu":case"Sigmoid":case"Clip":return!0;default:return!1}}fuseConvActivationNodes(){for(let e of this._nodes)if(e.opType==="Conv"){let o=this._allData[e.outputs[0]]._to;if(o.length===1&&this.isActivation(this._nodes[o[0]])){let t=this._nodes[o[0]];if(t.opType==="Clip")if(t.inputs.length===1)try{e.attributes.set("activation_params","floats",[t.attributes.getFloat("min"),t.attributes.getFloat("max")])}catch{e.attributes.set("activation_params","floats",[me,be])}else if(t.inputs.length>=3&&this._allData[t.inputs[1]].tensor!==void 0&&this._allData[t.inputs[2]].tensor!==void 0)e.attributes.set("activation_params","floats",[this._allData[t.inputs[1]].tensor.floatData[0],this._allData[t.inputs[2]].tensor.floatData[0]]);else continue;e.attributes.set("activation","string",t.opType),this.deleteNode(o[0])}}}}});var Nf,Yh,dn,Cf=x(()=>{"use strict";$r();Ff();je();Nf=Ae(Le());U();Yh=P.experimental.fbs,dn=class{constructor(){}load(e,o,t){let r;if(!t)try{this.loadFromOnnxFormat(e,o);return}catch(n){if(t!==void 0)throw n;r=n}try{this.loadFromOrtFormat(e,o)}catch(n){throw t!==void 0?n:new Error(`Failed to load model as ONNX format: ${r} +as ORT format: ${n}`)}}loadFromOnnxFormat(e,o){let t=Nf.onnx.ModelProto.decode(e);if(xt.longToNumber(t.irVersion)<3)throw new Error("only support ONNX model with IR_VERSION>=3");this._opsets=t.opsetImport.map(n=>({domain:n.domain,version:xt.longToNumber(n.version)})),this._graph=Wo.from(t.graph,o)}loadFromOrtFormat(e,o){let t=new b.ByteBuffer(e),r=Yh.InferenceSession.getRootAsInferenceSession(t).model();if(xt.longToNumber(r.irVersion())<3)throw new Error("only support ONNX model with IR_VERSION>=3");this._opsets=[];for(let s=0;s{"use strict";$f();kf();It();Cf();hn=class{constructor(e={}){this._initialized=!1,this.backendHint=e.backendHint,this.profiler=Dr.create(e.profiler),this.context={profiler:this.profiler,graphInputTypes:[],graphInputDims:[]}}get inputNames(){return this._model.graph.getInputNames()}get outputNames(){return this._model.graph.getOutputNames()}startProfiling(){this.profiler.start()}endProfiling(){this.profiler.stop()}async loadModel(e,o,t){await this.profiler.event("session","Session.loadModel",async()=>{let r=await Mo(this.backendHint);if(this.sessionHandler=r.createSessionHandler(this.context),this._model=new dn,typeof e=="string"){let n=e.endsWith(".ort");{let a=await(await fetch(e)).arrayBuffer();this.initialize(new Uint8Array(a),n)}}else if(ArrayBuffer.isView(e))this.initialize(e);else{let n=new Uint8Array(e,o||0,t||e.byteLength);this.initialize(n)}})}initialize(e,o){if(this._initialized)throw new Error("already initialized");this.profiler.event("session","Session.initialize",()=>{let t=this.sessionHandler.transformGraph?this.sessionHandler:void 0;this._model.load(e,t,o),this.sessionHandler.onGraphInitialized&&this.sessionHandler.onGraphInitialized(this._model.graph),this.initializeOps(this._model.graph),this._executionPlan=new fn(this._model.graph,this._ops,this.profiler)}),this._initialized=!0}async run(e){if(!this._initialized)throw new Error("session not initialized yet");return this.profiler.event("session","Session.run",async()=>{let o=this.normalizeAndValidateInputs(e),t=await this._executionPlan.execute(this.sessionHandler,o);return this.createOutput(t)})}normalizeAndValidateInputs(e){let o=this._model.graph.getInputNames();if(Array.isArray(e)){if(e.length!==o.length)throw new Error(`incorrect input array length: expected ${o.length} but got ${e.length}`)}else{if(e.size!==o.length)throw new Error(`incorrect input map size: expected ${o.length} but got ${e.size}`);let t=new Array(e.size),r=0;for(let n=0;n{"use strict";kt();ye();mn=class{constructor(e){this.session=e;this.inputNames=this.session.inputNames,this.outputNames=this.session.outputNames}async dispose(){}async run(e,o,t){let r=new Map;for(let a in e)if(Object.hasOwnProperty.call(e,a)){let u=e[a];r.set(a,new rt(u.dims,u.type,void 0,void 0,u.data))}let n=await this.session.run(r),s={};return n.forEach((a,u)=>{s[u]=new et(a.type,a.data,a.dims)}),s}startProfiling(){this.session.startProfiling()}endProfiling(){this.session.endProfiling()}}});var Mf={};We(Mf,{onnxjsBackend:()=>Qh});var Ho,Qh,Vf=x(()=>{"use strict";Rf();Gf();Ho=class{async init(){}async createInferenceSessionHandler(e,o){let t=new hn(o);return typeof e=="string"?await t.loadModel(e):await t.loadModel(e),new mn(t)}},Qh=new Ho});var bn=x(()=>{"use strict"});var Wf={};We(Wf,{default:()=>tm});var Uf,zf,tm,Hf=x(()=>{"use strict";qo();ve();sr();Uf="ort-wasm-proxy-worker",zf=globalThis.self?.name===Uf;zf&&(self.onmessage=i=>{let{type:e,in:o}=i.data;try{switch(e){case"init-wasm":yn(o.wasm).then(()=>{gn(o).then(()=>{postMessage({type:e})},t=>{postMessage({type:e,err:t})})},t=>{postMessage({type:e,err:t})});break;case"init-ep":{let{epName:t,env:r}=o;xn(r,t).then(()=>{postMessage({type:e})},n=>{postMessage({type:e,err:n})});break}case"copy-from":{let{buffer:t}=o,r=ur(t);postMessage({type:e,out:r});break}case"create":{let{model:t,options:r}=o;Tn(t,r).then(n=>{postMessage({type:e,out:n})},n=>{postMessage({type:e,err:n})});break}case"release":wn(o),postMessage({type:e});break;case"run":{let{sessionId:t,inputIndices:r,inputs:n,outputIndices:s,options:a}=o;vn(t,r,n,s,new Array(s.length).fill(null),a).then(u=>{u.some(l=>l[3]!=="cpu")?postMessage({type:e,err:"Proxy does not support non-cpu tensor location."}):postMessage({type:e,out:u},_n([...n,...u]))},u=>{postMessage({type:e,err:u})});break}case"end-profiling":In(o),postMessage({type:e});break;default:}}catch(t){postMessage({type:e,err:t})}});tm=zf?null:i=>new Worker(i??Lt,{type:"classic",name:Uf})});var Lt,em,jf,rm,nm,Xf,om,qf,Kf,Jf,sr=x(()=>{"use strict";bn();Lt=!1?void 0:typeof document<"u"?document.currentScript?.src:typeof self<"u"?self.location?.href:void 0,em=!1||typeof location>"u"?void 0:location.origin,jf=(i,e)=>{try{let o=e??Lt;return(o?new URL(i,o):new URL(i)).origin===em}catch{return!1}},rm=(i,e)=>{let o=e??Lt;try{return(o?new URL(i,o):new URL(i)).href}catch{return}},nm=(i,e)=>`${e??"./"}${i}`,Xf=async i=>{let o=await(await fetch(i,{credentials:"same-origin"})).blob();return URL.createObjectURL(o)},om=async i=>(await import(/*webpackIgnore:true*/i)).default,qf=(Hf(),gr(Wf)).default,Kf=async()=>{if(!Lt)throw new Error("Failed to load proxy worker: cannot determine the script source URL.");if(jf(Lt))return[void 0,qf()];let i=await Xf(Lt);return[i,qf(i)]},Jf=async(i,e,o)=>{{let t="ort-wasm-simd-threaded.mjs",r=i??rm(t,e),n=!!1&&o&&r&&!jf(r,e),s=n?await Xf(r):r??nm(t,e);return[n?s:void 0,await om(s)]}}});var jo,Xo,Sn,Zf,im,am,yn,nt,ve=x(()=>{"use strict";sr();Xo=!1,Sn=!1,Zf=!1,im=()=>{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}},am=()=>{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}},yn=async i=>{if(Xo)return Promise.resolve();if(Sn)throw new Error("multiple calls to 'initializeWebAssembly()' detected.");if(Zf)throw new Error("previous call to 'initializeWebAssembly()' failed.");Sn=!0;let e=i.initTimeout,o=i.numThreads;if(!am())throw new Error("WebAssembly SIMD is not supported in the current environment.");let t=im();o>1&&!t&&(typeof self<"u"&&!self.crossOriginIsolated&&console.warn("env.wasm.numThreads is set to "+o+", 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."),i.numThreads=o=1);let r=i.wasmPaths,n=typeof r=="string"?r:void 0,s=r?.mjs,a=s?.href??s,u=r?.wasm,l=u?.href??u,f=i.wasmBinary,[c,d]=await Jf(a,n,o>1),m=!1,y=[];if(e>0&&y.push(new Promise(g=>{setTimeout(()=>{m=!0,g()},e)})),y.push(new Promise((g,T)=>{let _={numThreads:o};f?_.wasmBinary=f:(l||n)&&(_.locateFile=(w,v)=>l??(n??v)+w),d(_).then(w=>{Sn=!1,Xo=!0,jo=w,g(),c&&URL.revokeObjectURL(c)},w=>{Sn=!1,Zf=!0,T(w)})})),await Promise.race(y),m)throw new Error(`WebAssembly backend initializing failed due to timeout: ${e}ms`)},nt=()=>{if(Xo&&jo)return jo;throw new Error("WebAssembly is not initialized yet.")}});var ot,lr,Y,On=x(()=>{"use strict";ve();ot=(i,e)=>{let o=nt(),t=o.lengthBytesUTF8(i)+1,r=o._malloc(t);return o.stringToUTF8(i,r,t),e.push(r),r},lr=(i,e,o,t)=>{if(typeof i=="object"&&i!==null){if(o.has(i))throw new Error("Circular reference in options");o.add(i)}Object.entries(i).forEach(([r,n])=>{let s=e?e+r:r;if(typeof n=="object")lr(n,s+".",o,t);else if(typeof n=="string"||typeof n=="number")t(s,n.toString());else if(typeof n=="boolean")t(s,n?"1":"0");else throw new Error(`Can't handle extra config type: ${typeof n}`)})},Y=i=>{let e=nt(),o=e.stackSave();try{let t=e.stackAlloc(8);e._OrtGetLastError(t,t+4);let r=e.HEAP32[t/4],n=e.HEAPU32[t/4+1],s=n?e.UTF8ToString(n):"";throw new Error(`${i} ERROR_CODE: ${r}, ERROR_MESSAGE: ${s}`)}finally{e.stackRestore(o)}}});var Yf,Qf=x(()=>{"use strict";ve();On();Yf=i=>{let e=nt(),o=0,t=[],r=i||{};try{if(i?.logSeverityLevel===void 0)r.logSeverityLevel=2;else if(typeof i.logSeverityLevel!="number"||!Number.isInteger(i.logSeverityLevel)||i.logSeverityLevel<0||i.logSeverityLevel>4)throw new Error(`log serverity level is not valid: ${i.logSeverityLevel}`);if(i?.logVerbosityLevel===void 0)r.logVerbosityLevel=0;else if(typeof i.logVerbosityLevel!="number"||!Number.isInteger(i.logVerbosityLevel))throw new Error(`log verbosity level is not valid: ${i.logVerbosityLevel}`);i?.terminate===void 0&&(r.terminate=!1);let n=0;return i?.tag!==void 0&&(n=ot(i.tag,t)),o=e._OrtCreateRunOptions(r.logSeverityLevel,r.logVerbosityLevel,!!r.terminate,n),o===0&&Y("Can't create run options."),i?.extra!==void 0&&lr(i.extra,"",new WeakSet,(s,a)=>{let u=ot(s,t),l=ot(a,t);e._OrtAddRunConfigEntry(o,u,l)!==0&&Y(`Can't set a run config entry: ${s} - ${a}.`)}),[o,t]}catch(n){throw o!==0&&e._OrtReleaseRunOptions(o),t.forEach(s=>e._free(s)),n}}});var sm,um,lm,fm,tc,ec=x(()=>{"use strict";ve();On();sm=i=>{switch(i){case"disabled":return 0;case"basic":return 1;case"extended":return 2;case"all":return 99;default:throw new Error(`unsupported graph optimization level: ${i}`)}},um=i=>{switch(i){case"sequential":return 0;case"parallel":return 1;default:throw new Error(`unsupported execution mode: ${i}`)}},lm=i=>{i.extra||(i.extra={}),i.extra.session||(i.extra.session={});let e=i.extra.session;e.use_ort_model_bytes_directly||(e.use_ort_model_bytes_directly="1"),i.executionProviders&&i.executionProviders.some(o=>(typeof o=="string"?o:o.name)==="webgpu")&&(i.enableMemPattern=!1)},fm=(i,e,o)=>{for(let t of e){let r=typeof t=="string"?t:t.name;switch(r){case"webnn":if(r="WEBNN",typeof t!="string"){let a=t?.deviceType;if(a){let u=ot("deviceType",o),l=ot(a,o);nt()._OrtAddSessionConfigEntry(i,u,l)!==0&&Y(`Can't set a session config entry: 'deviceType' - ${a}.`)}}break;case"webgpu":if(r="JS",typeof t!="string"){let s=t;if(s?.preferredLayout){if(s.preferredLayout!=="NCHW"&&s.preferredLayout!=="NHWC")throw new Error(`preferredLayout must be either 'NCHW' or 'NHWC': ${s.preferredLayout}`);let a=ot("preferredLayout",o),u=ot(s.preferredLayout,o);nt()._OrtAddSessionConfigEntry(i,a,u)!==0&&Y(`Can't set a session config entry: 'preferredLayout' - ${s.preferredLayout}.`)}}break;case"wasm":case"cpu":continue;default:throw new Error(`not supported execution provider: ${r}`)}let n=ot(r,o);nt()._OrtAppendExecutionProvider(i,n)!==0&&Y(`Can't append execution provider: ${r}.`)}},tc=i=>{let e=nt(),o=0,t=[],r=i||{};lm(r);try{let n=sm(r.graphOptimizationLevel??"all"),s=um(r.executionMode??"sequential"),a=typeof r.logId=="string"?ot(r.logId,t):0,u=r.logSeverityLevel??2;if(!Number.isInteger(u)||u<0||u>4)throw new Error(`log serverity level is not valid: ${u}`);let l=r.logVerbosityLevel??0;if(!Number.isInteger(l)||l<0||l>4)throw new Error(`log verbosity level is not valid: ${l}`);let f=typeof r.optimizedModelFilePath=="string"?ot(r.optimizedModelFilePath,t):0;if(o=e._OrtCreateSessionOptions(n,!!r.enableCpuMemArena,!!r.enableMemPattern,s,!!r.enableProfiling,0,a,u,l,f),o===0&&Y("Can't create session options."),r.executionProviders&&fm(o,r.executionProviders,t),r.enableGraphCapture!==void 0){if(typeof r.enableGraphCapture!="boolean")throw new Error(`enableGraphCapture must be a boolean value: ${r.enableGraphCapture}`);let c=ot("enableGraphCapture",t),d=ot(r.enableGraphCapture.toString(),t);e._OrtAddSessionConfigEntry(o,c,d)!==0&&Y(`Can't set a session config entry: 'enableGraphCapture' - ${r.enableGraphCapture}.`)}if(r.freeDimensionOverrides)for(let[c,d]of Object.entries(r.freeDimensionOverrides)){if(typeof c!="string")throw new Error(`free dimension override name must be a string: ${c}`);if(typeof d!="number"||!Number.isInteger(d)||d<0)throw new Error(`free dimension override value must be a non-negative integer: ${d}`);let m=ot(c,t);e._OrtAddFreeDimensionOverride(o,m,d)!==0&&Y(`Can't set a free dimension override: ${c} - ${d}.`)}return r.extra!==void 0&&lr(r.extra,"",new WeakSet,(c,d)=>{let m=ot(c,t),y=ot(d,t);e._OrtAddSessionConfigEntry(o,m,y)!==0&&Y(`Can't set a session config entry: ${c} - ${d}.`)}),[o,t]}catch(n){throw o!==0&&e._OrtReleaseSessionOptions(o),t.forEach(s=>e._free(s)),n}}});var fr,rc,cr,nc,oc,An,Pn,ic,Ko=x(()=>{"use strict";fr=i=>{switch(i){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: ${i}`)}},rc=i=>{switch(i){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: ${i}`)}},cr=(i,e)=>{let o=[-1,4,1,1,2,2,4,8,-1,1,2,8,4,8,-1,-1,-1,-1,-1,-1,-1,.5,.5][i],t=typeof e=="number"?e:e.reduce((r,n)=>r*n,1);return o>0?Math.ceil(t*o):void 0},nc=i=>{switch(i){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: ${i}`)}},oc=i=>{switch(i){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: ${i}`)}},An=i=>i==="float32"||i==="float16"||i==="int32"||i==="int64"||i==="uint32"||i==="uint8"||i==="bool"||i==="uint4"||i==="int4",Pn=i=>i==="float32"||i==="float16"||i==="int32"||i==="int64"||i==="uint32"||i==="uint64"||i==="int8"||i==="uint8"||i==="bool",ic=i=>{switch(i){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: ${i}`)}}});var pr,Jo=x(()=>{"use strict";bn();pr=async i=>{if(typeof i=="string")if(!1)try{let{readFile:e}=Bn("node:fs/promises");return new Uint8Array(await e(i))}catch(e){if(e.code==="ERR_FS_FILE_TOO_LARGE"){let{createReadStream:o}=Bn("node:fs"),t=o(i),r=[];for await(let n of t)r.push(n);return new Uint8Array(Buffer.concat(r))}throw e}else{let e=await fetch(i);if(!e.ok)throw new Error(`failed to load external data file: ${i}`);let o=e.headers.get("Content-Length"),t=o?parseInt(o,10):0;if(t<1073741824)return new Uint8Array(await e.arrayBuffer());{if(!e.body)throw new Error(`failed to load external data file: ${i}, no response body.`);let r=e.body.getReader(),n;try{n=new ArrayBuffer(t)}catch(a){if(a instanceof RangeError){let u=Math.ceil(t/65536);n=new WebAssembly.Memory({initial:u,maximum:u}).buffer}else throw a}let s=0;for(;;){let{done:a,value:u}=await r.read();if(a)break;let l=u.byteLength;new Uint8Array(n,s,l).set(u),s+=l}return new Uint8Array(n,0,t)}}else return i instanceof Blob?new Uint8Array(await i.arrayBuffer()):i instanceof Uint8Array?i:new Uint8Array(i)}});var cm,gn,xn,Me,pm,ur,Tn,wn,ac,vn,In,_n,qo=x(()=>{"use strict";Qf();ec();Ko();ve();On();Jo();cm=(i,e)=>{nt()._OrtInit(i,e)!==0&&Y("Can't initialize onnxruntime.")},gn=async i=>{cm(i.wasm.numThreads,oc(i.logLevel))},xn=async(i,e)=>{},Me=new Map,pm=i=>{let e=nt(),o=e.stackSave();try{let t=e.stackAlloc(8);return e._OrtGetInputOutputCount(i,t,t+4)!==0&&Y("Can't get session input/output count."),[e.HEAP32[t/4],e.HEAP32[t/4+1]]}finally{e.stackRestore(o)}},ur=i=>{let e=nt(),o=e._malloc(i.byteLength);if(o===0)throw new Error(`Can't create a session. failed to allocate a buffer of size ${i.byteLength}.`);return e.HEAPU8.set(i,o),[o,i.byteLength]},Tn=async(i,e)=>{let o,t,r=nt();Array.isArray(i)?[o,t]=i:i.buffer===r.HEAPU8.buffer?[o,t]=[i.byteOffset,i.byteLength]:[o,t]=ur(i);let n=0,s=0,a=0,u=[],l=[],f=[];try{if([s,u]=tc(e),e?.externalData&&r.mountExternalData){let w=[];for(let v of e.externalData){let L=typeof v=="string"?v:v.path;w.push(pr(typeof v=="string"?v:v.data).then(B=>{r.mountExternalData(L,B)}))}await Promise.all(w)}for(let w of e?.executionProviders??[])if((typeof w=="string"?w:w.name)==="webnn"){if(r.shouldTransferToMLTensor=!1,r.currentContext)throw new Error("WebNN execution provider is already set.");if(typeof w!="string"){let L=w,B=L?.context,Q=L?.gpuDevice,pt=L?.deviceType,Ht=L?.numThreads,wt=L?.powerPreference;B?r.currentContext=B:Q?r.currentContext=await navigator.ml.createContext(Q):r.currentContext=await navigator.ml.createContext({deviceType:pt,numThreads:Ht,powerPreference:wt})}else r.currentContext=await navigator.ml.createContext();break}n=await r._OrtCreateSession(o,t,s),n===0&&Y("Can't create a session."),r.currentContext&&(r.jsepRegisterMLContext(n,r.currentContext),r.currentContext=void 0,r.shouldTransferToMLTensor=!0);let[c,d]=pm(n),m=!!e?.enableGraphCapture,y=[],g=[],T=[];for(let w=0;wr._OrtFree(d)),f.forEach(d=>r._OrtFree(d)),a!==0&&r._OrtReleaseBinding(a),n!==0&&r._OrtReleaseSession(n),c}finally{r._free(o),s!==0&&r._OrtReleaseSessionOptions(s),u.forEach(c=>r._free(c)),r.unmountExternalData?.()}},wn=i=>{let e=nt(),o=Me.get(i);if(!o)throw new Error(`cannot release session. invalid session id: ${i}`);let[t,r,n,s,a]=o;s&&(a&&e._OrtClearBoundOutputs(s.handle),e._OrtReleaseBinding(s.handle)),e.jsepOnReleaseSession?.(i),r.forEach(u=>e._OrtFree(u)),n.forEach(u=>e._OrtFree(u)),e._OrtReleaseSession(t),Me.delete(i)},ac=(i,e,o,t,r,n=!1)=>{if(!i){e.push(0);return}let s=nt(),a=i[0],u=i[1],l=i[3],f,c;if(a==="string"&&(l==="gpu-buffer"||l==="ml-tensor"))throw new Error("String tensor is not supported on GPU.");if(n&&l!=="gpu-buffer")throw new Error(`External buffer must be provided for input/output index ${r} when enableGraphCapture is true.`);if(l==="gpu-buffer"){let y=i[2].gpuBuffer;c=cr(fr(a),u);let g=s.jsepRegisterBuffer;if(!g)throw new Error('Tensor location "gpu-buffer" is not supported without using WebGPU.');f=g(t,r,y,c)}else if(l==="ml-tensor"){let y=i[2].mlTensor;c=cr(fr(a),u);let g=s.jsepRegisterMLTensor;if(!g)throw new Error('Tensor location "ml-tensor" is not supported without using WebNN.');f=g(y,fr(a),u)}else{let y=i[2];if(Array.isArray(y)){c=4*y.length,f=s._malloc(c),o.push(f);let g=f/4;for(let T=0;Ts.HEAP32[y++]=T);let g=s._OrtCreateTensor(fr(a),f,c,m,u.length,ic(l));g===0&&Y(`Can't create tensor for input/output. session=${t}, index=${r}.`),e.push(g)}finally{s.stackRestore(d)}},vn=async(i,e,o,t,r,n)=>{let s=nt(),a=Me.get(i);if(!a)throw new Error(`cannot run inference. invalid session id: ${i}`);let u=a[0],l=a[1],f=a[2],c=a[3],d=a[4],m=a[5],y=e.length,g=t.length,T=0,_=[],w=[],v=[],L=[],B=s.stackSave(),Q=s.stackAlloc(y*4),pt=s.stackAlloc(y*4),Ht=s.stackAlloc(g*4),wt=s.stackAlloc(g*4);try{s.jsepOnRunStart?.(u),[T,_]=Yf(n);for(let W=0;Wft*bt,1);lt=rc(mr);let br=c?.outputPreferredLocations[t[W]];if(lt==="string"){if(br==="gpu-buffer"||br==="ml-tensor")throw new Error("String tensor is not supported on GPU.");let ft=[],bt=vt/4;for(let Kt=0;Kt0){let ft=s.jsepGetBuffer;if(!ft)throw new Error('preferredLocation "gpu-buffer" is not supported without using WebGPU.');let bt=ft(vt),Kt=cr(mr,ae);if(Kt===void 0||!An(lt))throw new Error(`Unsupported data type: ${lt}`);ze=!0,Se.push([lt,ie,{gpuBuffer:bt,download:s.jsepCreateDownloader(bt,Kt,lt),dispose:()=>{s._OrtReleaseTensor(Oe)}},"gpu-buffer"])}else if(br==="ml-tensor"&&ae>0){let ft=s.jsepEnsureTensor;if(!ft)throw new Error('preferredLocation "ml-tensor" is not supported without using WebNN.');if(cr(mr,ae)===void 0||!Pn(lt))throw new Error(`Unsupported data type: ${lt}`);let Kt=await ft(vt,mr,ie,!1);ze=!0,Se.push([lt,ie,{mlTensor:Kt,download:s.jsepCreateMLTensorDownloader(vt,lt),dispose:()=>{s.jsepReleaseTensorId(vt),s._OrtReleaseTensor(Oe)}},"ml-tensor"])}else{let ft=nc(lt),bt=new ft(ae);new Uint8Array(bt.buffer,bt.byteOffset,bt.byteLength).set(s.HEAPU8.subarray(vt,vt+bt.byteLength)),Se.push([lt,ie,bt,"cpu"])}}finally{s.stackRestore(ei),lt==="string"&&vt&&s._free(vt),ze||s._OrtReleaseTensor(Oe)}}return c&&!d&&(s._OrtClearBoundOutputs(c.handle),Me.set(i,[u,l,f,c,d,!1])),Se}finally{s.stackRestore(B),w.forEach(mt=>s._OrtReleaseTensor(mt)),v.forEach(mt=>s._OrtReleaseTensor(mt)),L.forEach(mt=>s._free(mt)),T!==0&&s._OrtReleaseRunOptions(T),_.forEach(mt=>s._free(mt))}},In=i=>{let e=nt(),o=Me.get(i);if(!o)throw new Error("invalid session id");let t=o[0],r=e._OrtEndProfiling(t);r===0&&Y("Can't get an profile file name."),e._OrtFree(r)},_n=i=>{let e=[];for(let o of i){let t=o[2];!Array.isArray(t)&&"buffer"in t&&e.push(t.buffer)}return e}});var _e,$t,dr,Dn,Ln,En,Zo,Yo,Ve,Ue,hm,sc,uc,lc,fc,cc,pc,dc,Qo=x(()=>{"use strict";kt();qo();ve();sr();_e=()=>!!$.wasm.proxy&&typeof document<"u",dr=!1,Dn=!1,Ln=!1,Yo=new Map,Ve=(i,e)=>{let o=Yo.get(i);o?o.push(e):Yo.set(i,[e])},Ue=()=>{if(dr||!Dn||Ln||!$t)throw new Error("worker not ready")},hm=i=>{switch(i.data.type){case"init-wasm":dr=!1,i.data.err?(Ln=!0,Zo[1](i.data.err)):(Dn=!0,Zo[0]()),En&&(URL.revokeObjectURL(En),En=void 0);break;case"init-ep":case"copy-from":case"create":case"release":case"run":case"end-profiling":{let e=Yo.get(i.data.type);i.data.err?e.shift()[1](i.data.err):e.shift()[0](i.data.out);break}default:}},sc=async()=>{if(!Dn){if(dr)throw new Error("multiple calls to 'initWasm()' detected.");if(Ln)throw new Error("previous call to 'initWasm()' failed.");if(dr=!0,_e())return new Promise((i,e)=>{$t?.terminate(),Kf().then(([o,t])=>{try{$t=t,$t.onerror=n=>e(n),$t.onmessage=hm,Zo=[i,e];let r={type:"init-wasm",in:$};$t.postMessage(r),En=o}catch(r){e(r)}},e)});try{await yn($.wasm),await gn($),Dn=!0}catch(i){throw Ln=!0,i}finally{dr=!1}}},uc=async i=>{if(_e())return Ue(),new Promise((e,o)=>{Ve("init-ep",[e,o]);let t={type:"init-ep",in:{epName:i,env:$}};$t.postMessage(t)});await xn($,i)},lc=async i=>_e()?(Ue(),new Promise((e,o)=>{Ve("copy-from",[e,o]);let t={type:"copy-from",in:{buffer:i}};$t.postMessage(t,[i.buffer])})):ur(i),fc=async(i,e)=>{if(_e()){if(e?.preferredOutputLocation)throw new Error('session option "preferredOutputLocation" is not supported for proxy.');return Ue(),new Promise((o,t)=>{Ve("create",[o,t]);let r={type:"create",in:{model:i,options:{...e}}},n=[];i instanceof Uint8Array&&n.push(i.buffer),$t.postMessage(r,n)})}else return Tn(i,e)},cc=async i=>{if(_e())return Ue(),new Promise((e,o)=>{Ve("release",[e,o]);let t={type:"release",in:i};$t.postMessage(t)});wn(i)},pc=async(i,e,o,t,r,n)=>{if(_e()){if(o.some(s=>s[3]!=="cpu"))throw new Error("input tensor on GPU is not supported for proxy.");if(r.some(s=>s))throw new Error("pre-allocated output tensor is not supported for proxy.");return Ue(),new Promise((s,a)=>{Ve("run",[s,a]);let u=o,l={type:"run",in:{sessionId:i,inputIndices:e,inputs:u,outputIndices:t,options:n}};$t.postMessage(l,_n(u))})}else return vn(i,e,o,t,r,n)},dc=async i=>{if(_e())return Ue(),new Promise((e,o)=>{Ve("end-profiling",[e,o]);let t={type:"end-profiling",in:i};$t.postMessage(t)});In(i)}});var hc,mm,$n,mc=x(()=>{"use strict";kt();Qo();Ko();bn();Jo();hc=(i,e)=>{switch(i.location){case"cpu":return[i.type,i.dims,i.data,"cpu"];case"gpu-buffer":return[i.type,i.dims,{gpuBuffer:i.gpuBuffer},"gpu-buffer"];case"ml-tensor":return[i.type,i.dims,{mlTensor:i.mlTensor},"ml-tensor"];default:throw new Error(`invalid data location: ${i.location} for ${e()}`)}},mm=i=>{switch(i[3]){case"cpu":return new et(i[0],i[2],i[1]);case"gpu-buffer":{let e=i[0];if(!An(e))throw new Error(`not supported data type: ${e} for deserializing GPU tensor`);let{gpuBuffer:o,download:t,dispose:r}=i[2];return et.fromGpuBuffer(o,{dataType:e,dims:i[1],download:t,dispose:r})}case"ml-tensor":{let e=i[0];if(!Pn(e))throw new Error(`not supported data type: ${e} for deserializing MLTensor tensor`);let{mlTensor:o,download:t,dispose:r}=i[2];return et.fromMLTensor(o,{dataType:e,dims:i[1],download:t,dispose:r})}default:throw new Error(`invalid data location: ${i[3]}`)}},$n=class{async fetchModelAndCopyToWasmMemory(e){return lc(await pr(e))}async loadModel(e,o){Jt();let t;typeof e=="string"?!1?t=await pr(e):t=await this.fetchModelAndCopyToWasmMemory(e):t=e,[this.sessionId,this.inputNames,this.outputNames]=await fc(t,o),Zt()}async dispose(){return cc(this.sessionId)}async run(e,o,t){Jt();let r=[],n=[];Object.entries(e).forEach(d=>{let m=d[0],y=d[1],g=this.inputNames.indexOf(m);if(g===-1)throw new Error(`invalid input '${m}'`);r.push(y),n.push(g)});let s=[],a=[];Object.entries(o).forEach(d=>{let m=d[0],y=d[1],g=this.outputNames.indexOf(m);if(g===-1)throw new Error(`invalid output '${m}'`);s.push(y),a.push(g)});let u=r.map((d,m)=>hc(d,()=>`input "${this.inputNames[n[m]]}"`)),l=s.map((d,m)=>d?hc(d,()=>`output "${this.outputNames[a[m]]}"`):null),f=await pc(this.sessionId,n,u,a,l,t),c={};for(let d=0;dkn,initializeFlags:()=>bc,wasmBackend:()=>bm});var bc,kn,bm,gc=x(()=>{"use strict";kt();Qo();mc();sr();bc=()=>{if((typeof $.wasm.initTimeout!="number"||$.wasm.initTimeout<0)&&($.wasm.initTimeout=0),$.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 $.wasm.proxy!="boolean"&&($.wasm.proxy=!1),typeof $.wasm.trace!="boolean"&&($.wasm.trace=!1),typeof $.wasm.numThreads!="number"||!Number.isInteger($.wasm.numThreads)||$.wasm.numThreads<=0)if(typeof self<"u"&&!self.crossOriginIsolated)$.wasm.numThreads=1;else{let i=typeof navigator>"u"?Bn("node:os").cpus().length:navigator.hardwareConcurrency;$.wasm.numThreads=Math.min(4,Math.ceil((i||1)/2))}$.wasm.wasmPaths===void 0&&Lt&&Lt.indexOf("blob:")!==0&&($.wasm.wasmPaths=Lt.substring(0,Lt.lastIndexOf("/")+1))},kn=class{async init(e){bc(),await sc(),await uc(e)}async createInferenceSessionHandler(e,o){let t=new $n;return await t.loadModel(e,o),Promise.resolve(t)}},bm=new kn});var gm={};We(gm,{InferenceSession:()=>Ai,TRACE:()=>Cn,TRACE_FUNC_BEGIN:()=>Jt,TRACE_FUNC_END:()=>Zt,Tensor:()=>et,TrainingSession:()=>Bi,default:()=>ym,env:()=>$,registerBackend:()=>ue});kt();kt();kt();var Ni="1.20.1";var ym=Gn;{let i=(Vf(),gr(Mf)).onnxjsBackend;ue("webgl",i,-10)}{let i=(gc(),gr(yc)).wasmBackend;ue("cpu",i,10),ue("wasm",i,10)}Object.defineProperty($.versions,"web",{value:Ni,enumerable:!0});return gr(gm);})(); +/*! Bundled license information: + +long/index.js: + (** + * @license + * Copyright 2009 The Closure Library Authors + * Copyright 2020 Daniel Wirtz / The long.js Authors. + * + * 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. + * + * SPDX-License-Identifier: Apache-2.0 + *) +*/ +typeof exports=="object"&&typeof module=="object"&&(module.exports=ort); +//# sourceMappingURL=ort.min.js.map diff --git a/webapp/vendor/ort/ort.webgpu.min.js b/webapp/vendor/ort/ort.webgpu.min.js new file mode 100644 index 0000000..fefbba6 --- /dev/null +++ b/webapp/vendor/ort/ort.webgpu.min.js @@ -0,0 +1,2644 @@ +/*! + * ONNX Runtime Web v1.20.1 + * Copyright (c) Microsoft Corporation. All rights reserved. + * Licensed under the MIT License. + */ +"use strict";var ort=(()=>{var gn=Object.defineProperty;var bu=Object.getOwnPropertyDescriptor;var wu=Object.getOwnPropertyNames;var _u=Object.prototype.hasOwnProperty;var yn=(e=>typeof require<"u"?require:typeof Proxy<"u"?new Proxy(e,{get:(t,n)=>(typeof require<"u"?require:t)[n]}):e)(function(e){if(typeof require<"u")return require.apply(this,arguments);throw Error('Dynamic require of "'+e+'" is not supported')});var k=(e,t)=>()=>(e&&(t=e(e=0)),t);var nt=(e,t)=>{for(var n in t)gn(e,n,{get:t[n],enumerable:!0})},$u=(e,t,n,r)=>{if(t&&typeof t=="object"||typeof t=="function")for(let o of wu(t))!_u.call(e,o)&&o!==n&&gn(e,o,{get:()=>t[o],enumerable:!(r=bu(t,o))||r.enumerable});return e};var _t=e=>$u(gn({},"__esModule",{value:!0}),e);var $t,Ge,Le,vu,vt,xt=k(()=>{"use strict";$t=new Map,Ge=[],Le=(e,t,n)=>{if(t&&typeof t.init=="function"&&typeof t.createInferenceSessionHandler=="function"){let r=$t.get(e);if(r===void 0)$t.set(e,{backend:t,priority:n});else{if(r.priority>n)return;if(r.priority===n&&r.backend!==t)throw new Error(`cannot register backend "${e}" using priority ${n}`)}if(n>=0){let o=Ge.indexOf(e);o!==-1&&Ge.splice(o,1);for(let i=0;i{let t=$t.get(e);if(!t)return"backend not found.";if(t.initialized)return t.backend;if(t.aborted)return t.error;{let n=!!t.initPromise;try{return n||(t.initPromise=t.backend.init(e)),await t.initPromise,t.initialized=!0,t.backend}catch(r){return n||(t.error=`${r}`,t.aborted=!0),t.error}finally{delete t.initPromise}}},vt=async e=>{let t=e.executionProviders||[],n=t.map(u=>typeof u=="string"?u:u.name),r=n.length===0?Ge:n,o,i=[],s=new Set;for(let u of r){let d=await vu(u);typeof d=="string"?i.push({name:u,err:d}):(o||(o=d),o===d&&s.add(u))}if(!o)throw new Error(`no available backend found. ERR: ${i.map(u=>`[${u.name}] ${u.err}`).join(", ")}`);for(let{name:u,err:d}of i)n.includes(u)&&console.warn(`removing requested execution provider "${u}" from session options because it is not available: ${d}`);let a=t.filter(u=>s.has(typeof u=="string"?u:u.name));return[o,new Proxy(e,{get:(u,d)=>d==="executionProviders"?a:Reflect.get(u,d)})]}});var dr=k(()=>{"use strict";xt()});var lr,cr=k(()=>{"use strict";lr="1.20.1"});var pr,_e,bn=k(()=>{"use strict";cr();pr="warning",_e={wasm:{},webgl:{},webgpu:{},versions:{common:lr},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}`);pr=e}},get logLevel(){return pr}};Object.defineProperty(_e,"logLevel",{enumerable:!0})});var Y,mr=k(()=>{"use strict";bn();Y=_e});var fr,hr,gr=k(()=>{"use strict";fr=(e,t)=>{let n=typeof document<"u"?document.createElement("canvas"):new OffscreenCanvas(1,1);n.width=e.dims[3],n.height=e.dims[2];let r=n.getContext("2d");if(r!=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 s=t?.format!==void 0?t.format:"RGB",a=t?.norm,u,d;a===void 0||a.mean===void 0?u=[255,255,255,255]:typeof a.mean=="number"?u=[a.mean,a.mean,a.mean,a.mean]:(u=[a.mean[0],a.mean[1],a.mean[2],0],a.mean[3]!==void 0&&(u[3]=a.mean[3])),a===void 0||a.bias===void 0?d=[0,0,0,0]:typeof a.bias=="number"?d=[a.bias,a.bias,a.bias,a.bias]:(d=[a.bias[0],a.bias[1],a.bias[2],0],a.bias[3]!==void 0&&(d[3]=a.bias[3]));let l=i*o,c=0,p=l,h=l*2,m=-1;s==="RGBA"?(c=0,p=l,h=l*2,m=l*3):s==="RGB"?(c=0,p=l,h=l*2):s==="RBG"&&(c=0,h=l,p=l*2);for(let f=0;f{let n=typeof document<"u"?document.createElement("canvas").getContext("2d"):new OffscreenCanvas(1,1).getContext("2d"),r;if(n!=null){let o,i,s;t?.tensorLayout!==void 0&&t.tensorLayout==="NHWC"?(o=e.dims[2],i=e.dims[1],s=e.dims[3]):(o=e.dims[3],i=e.dims[2],s=e.dims[1]);let a=t!==void 0&&t.format!==void 0?t.format:"RGB",u=t?.norm,d,l;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],255],u.mean[3]!==void 0&&(d[3]=u.mean[3])),u===void 0||u.bias===void 0?l=[0,0,0,0]:typeof u.bias=="number"?l=[u.bias,u.bias,u.bias,u.bias]:(l=[u.bias[0],u.bias[1],u.bias[2],0],u.bias[3]!==void 0&&(l[3]=u.bias[3]));let c=i*o;if(t!==void 0&&(t.format!==void 0&&s===4&&t.format!=="RGBA"||s===3&&t.format!=="RGB"&&t.format!=="BGR"))throw new Error("Tensor format doesn't match input tensor dims");let p=4,h=0,m=1,f=2,w=3,y=0,g=c,b=c*2,_=-1;a==="RGBA"?(y=0,g=c,b=c*2,_=c*3):a==="RGB"?(y=0,g=c,b=c*2):a==="RBG"&&(y=0,b=c,g=c*2),r=n.createImageData(o,i);for(let $=0;${"use strict";St();wn=(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:n,width:r}=t,o=t.norm??{mean:255,bias:0},i,s;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"?s=[o.bias,o.bias,o.bias,o.bias]:s=[o.bias[0],o.bias[1],o.bias[2],o.bias[3]??0];let a=t.format!==void 0?t.format:"RGBA",u=t.tensorFormat!==void 0&&t.tensorFormat!==void 0?t.tensorFormat:"RGB",d=n*r,l=u==="RGBA"?new Float32Array(d*4):new Float32Array(d*3),c=4,p=0,h=1,m=2,f=3,w=0,y=d,g=d*2,b=-1;a==="RGB"&&(c=3,p=0,h=1,m=2,f=-1),u==="RGBA"?b=d*3:u==="RBG"?(w=0,g=d,y=d*2):u==="BGR"&&(g=0,y=d,w=d*2);for(let $=0;${let n=typeof HTMLImageElement<"u"&&e instanceof HTMLImageElement,r=typeof ImageData<"u"&&e instanceof ImageData,o=typeof ImageBitmap<"u"&&e instanceof ImageBitmap,i=typeof e=="string",s,a=t??{},u=()=>{if(typeof document<"u")return document.createElement("canvas");if(typeof OffscreenCanvas<"u")return new OffscreenCanvas(1,1);throw new Error("Canvas is not supported")},d=l=>typeof HTMLCanvasElement<"u"&&l instanceof HTMLCanvasElement||l instanceof OffscreenCanvas?l.getContext("2d"):null;if(n){let l=u();l.width=e.width,l.height=e.height;let c=d(l);if(c!=null){let p=e.height,h=e.width;if(t!==void 0&&t.resizedHeight!==void 0&&t.resizedWidth!==void 0&&(p=t.resizedHeight,h=t.resizedWidth),t!==void 0){if(a=t,t.tensorFormat!==void 0)throw new Error("Image input config format must be RGBA for HTMLImageElement");a.tensorFormat="RGBA",a.height=p,a.width=h}else a.tensorFormat="RGBA",a.height=p,a.width=h;c.drawImage(e,0,0),s=c.getImageData(0,0,h,p).data}else throw new Error("Can not access image data")}else if(r){let l,c;if(t!==void 0&&t.resizedWidth!==void 0&&t.resizedHeight!==void 0?(l=t.resizedHeight,c=t.resizedWidth):(l=e.height,c=e.width),t!==void 0&&(a=t),a.format="RGBA",a.height=l,a.width=c,t!==void 0){let p=u();p.width=c,p.height=l;let h=d(p);if(h!=null)h.putImageData(e,0,0),s=h.getImageData(0,0,c,l).data;else throw new Error("Can not access image data")}else s=e.data}else if(o){if(t===void 0)throw new Error("Please provide image config with format for Imagebitmap");let l=u();l.width=e.width,l.height=e.height;let c=d(l);if(c!=null){let p=e.height,h=e.width;return c.drawImage(e,0,0,h,p),s=c.getImageData(0,0,h,p).data,a.height=p,a.width=h,wn(s,a)}else throw new Error("Can not access image data")}else{if(i)return new Promise((l,c)=>{let p=u(),h=d(p);if(!e||!h)return c();let m=new Image;m.crossOrigin="Anonymous",m.src=e,m.onload=()=>{p.width=m.width,p.height=m.height,h.drawImage(m,0,0,p.width,p.height);let f=h.getImageData(0,0,p.width,p.height);a.height=p.height,a.width=p.width,l(wn(f.data,a))}});throw new Error("Input data provided is not supported - aborted tensor creation")}if(s!==void 0)return wn(s,a);throw new Error("Input data provided is not supported - aborted tensor creation")},br=(e,t)=>{let{width:n,height:r,download:o,dispose:i}=t,s=[1,r,n,4];return new me({location:"texture",type:"float32",texture:e,dims:s,download:o,dispose:i})},wr=(e,t)=>{let{dataType:n,dims:r,download:o,dispose:i}=t;return new me({location:"gpu-buffer",type:n??"float32",gpuBuffer:e,dims:r,download:o,dispose:i})},_r=(e,t)=>{let{dataType:n,dims:r,download:o,dispose:i}=t;return new me({location:"ml-tensor",type:n??"float32",mlTensor:e,dims:r,download:o,dispose:i})},$r=(e,t,n)=>new me({location:"cpu-pinned",type:e,data:t,dims:n??[t.length]})});var He,rt,xr,Sr,Ir=k(()=>{"use strict";He=new Map([["float32",Float32Array],["uint8",Uint8Array],["int8",Int8Array],["uint16",Uint16Array],["int16",Int16Array],["int32",Int32Array],["bool",Uint8Array],["float64",Float64Array],["uint32",Uint32Array],["int4",Uint8Array],["uint4",Uint8Array]]),rt=new Map([[Float32Array,"float32"],[Uint8Array,"uint8"],[Int8Array,"int8"],[Uint16Array,"uint16"],[Int16Array,"int16"],[Int32Array,"int32"],[Float64Array,"float64"],[Uint32Array,"uint32"]]),xr=!1,Sr=()=>{if(!xr){xr=!0;let e=typeof BigInt64Array<"u"&&BigInt64Array.from,t=typeof BigUint64Array<"u"&&BigUint64Array.from,n=typeof Float16Array<"u"&&Float16Array.from;e&&(He.set("int64",BigInt64Array),rt.set(BigInt64Array,"int64")),t&&(He.set("uint64",BigUint64Array),rt.set(BigUint64Array,"uint64")),n?(He.set("float16",Float16Array),rt.set(Float16Array,"float16")):He.set("float16",Uint16Array)}}});var Tr,Cr,Ar=k(()=>{"use strict";St();Tr=e=>{let t=1;for(let n=0;n{switch(e.location){case"cpu":return new me(e.type,e.data,t);case"cpu-pinned":return new me({location:"cpu-pinned",data:e.data,type:e.type,dims:t});case"texture":return new me({location:"texture",texture:e.texture,type:e.type,dims:t});case"gpu-buffer":return new me({location:"gpu-buffer",gpuBuffer:e.gpuBuffer,type:e.type,dims:t});case"ml-tensor":return new me({location:"ml-tensor",mlTensor:e.mlTensor,type:e.type,dims:t});default:throw new Error(`tensorReshape: tensor location ${e.location} is not supported`)}}});var me,St=k(()=>{"use strict";gr();vr();Ir();Ar();me=class{constructor(t,n,r){Sr();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 a=He.get(o);if(!a)throw new TypeError(`unsupported type "${o}" to create tensor from pinned buffer`);if(!(t.data instanceof a))throw new TypeError(`buffer should be of type ${a.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 a,u;if(typeof t=="string")if(o=t,u=r,t==="string"){if(!Array.isArray(n))throw new TypeError("A string tensor's data must be a string array.");a=n}else{let d=He.get(t);if(d===void 0)throw new TypeError(`Unsupported tensor type: ${t}.`);if(Array.isArray(n)){if(t==="float16"&&d===Uint16Array||t==="uint4"||t==="int4")throw new TypeError(`Creating a ${t} tensor from number array is not supported. Please use ${d.name} as data.`);t==="uint64"||t==="int64"?a=d.from(n,BigInt):a=d.from(n)}else if(n instanceof d)a=n;else if(n instanceof Uint8ClampedArray)if(t==="uint8")a=Uint8Array.from(n);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 ${d}`)}else if(u=n,Array.isArray(t)){if(t.length===0)throw new TypeError("Tensor type cannot be inferred from an empty array.");let d=typeof t[0];if(d==="string")o="string",a=t;else if(d==="boolean")o="bool",a=Uint8Array.from(t);else throw new TypeError(`Invalid element type of data array: ${d}.`)}else if(t instanceof Uint8ClampedArray)o="uint8",a=Uint8Array.from(t);else{let d=rt.get(t.constructor);if(d===void 0)throw new TypeError(`Unsupported type for tensor data: ${t.constructor}.`);o=d,a=t}if(u===void 0)u=[a.length];else if(!Array.isArray(u))throw new TypeError("A tensor's dims must be a number array");i=u,this.cpuData=a,this.dataLocation="cpu"}let s=Tr(i);if(this.cpuData&&s!==this.cpuData.length&&!((o==="uint4"||o==="int4")&&Math.ceil(s/2)===this.cpuData.length))throw new Error(`Tensor's size(${s}) does not match data length(${this.cpuData.length}).`);this.type=o,this.dims=i,this.size=s}static async fromImage(t,n){return yr(t,n)}static fromTexture(t,n){return br(t,n)}static fromGpuBuffer(t,n){return wr(t,n)}static fromMLTensor(t,n){return _r(t,n)}static fromPinnedBuffer(t,n,r){return $r(t,n,r)}toDataURL(t){return fr(this,t)}toImageData(t){return hr(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 n=await this.downloader();return this.downloader=void 0,this.dataLocation="cpu",this.cpuData=n,t&&this.disposer&&(this.disposer(),this.disposer=void 0),n}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 Cr(this,t)}}});var ce,It=k(()=>{"use strict";St();ce=me});var ot,kr,be,ge,_n=k(()=>{"use strict";bn();ot=(e,t)=>{(typeof _e.trace>"u"?!_e.wasm.trace:!_e.trace)||console.timeStamp(`${e}::ORT::${t}`)},kr=(e,t)=>{let n=new Error().stack?.split(/\r\n|\r|\n/g)||[],r=!1;for(let o=0;o{(typeof _e.trace>"u"?!_e.wasm.trace:!_e.trace)||kr("BEGIN",e)},ge=e=>{(typeof _e.trace>"u"?!_e.wasm.trace:!_e.trace)||kr("END",e)}});var Tt,Er=k(()=>{"use strict";xt();It();_n();Tt=class e{constructor(t){this.handler=t}async run(t,n,r){be();let o={},i={};if(typeof t!="object"||t===null||t instanceof ce||Array.isArray(t))throw new TypeError("'feeds' must be an object that use input names as keys and OnnxValue as corresponding values.");let s=!0;if(typeof n=="object"){if(n===null)throw new TypeError("Unexpected argument[1]: cannot be null.");if(n instanceof ce)throw new TypeError("'fetches' cannot be a Tensor");if(Array.isArray(n)){if(n.length===0)throw new TypeError("'fetches' cannot be an empty array.");s=!1;for(let d of n){if(typeof d!="string")throw new TypeError("'fetches' must be a string array or an object.");if(this.outputNames.indexOf(d)===-1)throw new RangeError(`'fetches' contains invalid output name: ${d}.`);o[d]=null}if(typeof r=="object"&&r!==null)i=r;else if(typeof r<"u")throw new TypeError("'options' must be an object.")}else{let d=!1,l=Object.getOwnPropertyNames(n);for(let c of this.outputNames)if(l.indexOf(c)!==-1){let p=n[c];(p===null||p instanceof ce)&&(d=!0,s=!1,o[c]=p)}if(d){if(typeof r=="object"&&r!==null)i=r;else if(typeof r<"u")throw new TypeError("'options' must be an object.")}else i=n}}else if(typeof n<"u")throw new TypeError("Unexpected argument[1]: must be 'fetches' or 'options'.");for(let d of this.inputNames)if(typeof t[d]>"u")throw new Error(`input '${d}' is missing in 'feeds'.`);if(s)for(let d of this.outputNames)o[d]=null;let a=await this.handler.run(t,o,i),u={};for(let d in a)if(Object.hasOwnProperty.call(a,d)){let l=a[d];l instanceof ce?u[d]=l:u[d]=new ce(l.type,l.data,l.dims)}return ge(),u}async release(){return this.handler.dispose()}static async create(t,n,r,o){be();let i,s={};if(typeof t=="string"){if(i=t,typeof n=="object"&&n!==null)s=n;else if(typeof n<"u")throw new TypeError("'options' must be an object.")}else if(t instanceof Uint8Array){if(i=t,typeof n=="object"&&n!==null)s=n;else if(typeof n<"u")throw new TypeError("'options' must be an object.")}else if(t instanceof ArrayBuffer||typeof SharedArrayBuffer<"u"&&t instanceof SharedArrayBuffer){let l=t,c=0,p=t.byteLength;if(typeof n=="object"&&n!==null)s=n;else if(typeof n=="number"){if(c=n,!Number.isSafeInteger(c))throw new RangeError("'byteOffset' must be an integer.");if(c<0||c>=l.byteLength)throw new RangeError(`'byteOffset' is out of range [0, ${l.byteLength}).`);if(p=t.byteLength-c,typeof r=="number"){if(p=r,!Number.isSafeInteger(p))throw new RangeError("'byteLength' must be an integer.");if(p<=0||c+p>l.byteLength)throw new RangeError(`'byteLength' is out of range (0, ${l.byteLength-c}].`);if(typeof o=="object"&&o!==null)s=o;else if(typeof o<"u")throw new TypeError("'options' must be an object.")}else if(typeof r<"u")throw new TypeError("'byteLength' must be a number.")}else if(typeof n<"u")throw new TypeError("'options' must be an object.");i=new Uint8Array(l,c,p)}else throw new TypeError("Unexpected argument[0]: must be 'path' or 'buffer'.");let[a,u]=await vt(s),d=await a.createInferenceSessionHandler(i,u);return ge(),new e(d)}startProfiling(){this.handler.startProfiling()}endProfiling(){this.handler.endProfiling()}get inputNames(){return this.handler.inputNames}get outputNames(){return this.handler.outputNames}}});var Pr,zr=k(()=>{"use strict";Er();Pr=Tt});var Br=k(()=>{"use strict"});var Dr=k(()=>{"use strict"});var Or=k(()=>{"use strict"});var Rr=k(()=>{"use strict"});var xu,Ct,Mr=k(()=>{"use strict";xt();It();xu="Training backend could not be resolved. Make sure you're using the correct configuration & WebAssembly files.",Ct=class e{constructor(t,n,r){this.handler=t,this.hasOptimizerModel=n,this.hasEvalModel=r}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,n){let r=t.evalModel||"",o=t.optimizerModel||"",i=n||{},[s,a]=await vt(i);if(s.createTrainingSessionHandler){let u=await s.createTrainingSessionHandler(t.checkpointState,t.trainModel,r,o,a);return new e(u,!!t.optimizerModel,!!t.evalModel)}else throw new Error(xu)}typeNarrowingForRunStep(t,n,r,o,i){let s={},a={};if(typeof r!="object"||r===null||r instanceof ce||Array.isArray(r))throw new TypeError("'feeds' must be an object that use input names as keys and OnnxValue as corresponding values.");let u=!0;if(typeof o=="object"){if(o===null)throw new TypeError("Unexpected argument[1]: cannot be null.");if(o instanceof ce)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.");u=!1;for(let d of o){if(typeof d!="string")throw new TypeError("'fetches' must be a string array or an object.");if(n.indexOf(d)===-1)throw new RangeError(`'fetches' contains invalid output name: ${d}.`);s[d]=null}if(typeof i=="object"&&i!==null)a=i;else if(typeof i<"u")throw new TypeError("'options' must be an object.")}else{let d=!1,l=Object.getOwnPropertyNames(o);for(let c of n)if(l.indexOf(c)!==-1){let p=o[c];(p===null||p instanceof ce)&&(d=!0,u=!1,s[c]=p)}if(d){if(typeof i=="object"&&i!==null)a=i;else if(typeof i<"u")throw new TypeError("'options' must be an object.")}else a=o}}else if(typeof o<"u")throw new TypeError("Unexpected argument[1]: must be 'fetches' or 'options'.");for(let d of t)if(typeof r[d]>"u")throw new Error(`input '${d}' is missing in 'feeds'.`);if(u)for(let d of n)s[d]=null;return[s,a]}convertHandlerReturnTypeToMapOfTensors(t){let n={};for(let r in t)if(Object.hasOwnProperty.call(t,r)){let o=t[r];o instanceof ce?n[r]=o:n[r]=new ce(o.type,o.data,o.dims)}return n}async lazyResetGrad(){await this.handler.lazyResetGrad()}async runTrainStep(t,n,r){let[o,i]=this.typeNarrowingForRunStep(this.trainingInputNames,this.trainingOutputNames,t,n,r),s=await this.handler.runTrainStep(t,o,i);return this.convertHandlerReturnTypeToMapOfTensors(s)}async runOptimizerStep(t){if(this.hasOptimizerModel)await this.handler.runOptimizerStep(t||{});else throw new Error("This TrainingSession has no OptimizerModel loaded.")}async runEvalStep(t,n,r){if(this.hasEvalModel){let[o,i]=this.typeNarrowingForRunStep(this.evalInputNames,this.evalOutputNames,t,n,r),s=await this.handler.runEvalStep(t,o,i);return this.convertHandlerReturnTypeToMapOfTensors(s)}else throw new Error("This TrainingSession has no EvalModel loaded.")}async getParametersSize(t=!0){return this.handler.getParametersSize(t)}async loadParametersBuffer(t,n=!0){let r=await this.getParametersSize(n);if(t.length!==4*r)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,n)}async getContiguousParameters(t=!0){return this.handler.getContiguousParameters(t)}async release(){return this.handler.dispose()}}});var Ur,Vr=k(()=>{"use strict";Mr();Ur=Ct});var $n={};nt($n,{InferenceSession:()=>Pr,TRACE:()=>ot,TRACE_FUNC_BEGIN:()=>be,TRACE_FUNC_END:()=>ge,Tensor:()=>ce,TrainingSession:()=>Ur,env:()=>Y,registerBackend:()=>Le});var Se=k(()=>{"use strict";dr();mr();zr();It();Br();Dr();_n();Or();Rr();Vr()});var At=k(()=>{"use strict"});var Gr={};nt(Gr,{default:()=>Su});var Nr,Wr,Su,Hr=k(()=>{"use strict";vn();Ne();it();Nr="ort-wasm-proxy-worker",Wr=globalThis.self?.name===Nr;Wr&&(self.onmessage=e=>{let{type:t,in:n}=e.data;try{switch(t){case"init-wasm":kt(n.wasm).then(()=>{Et(n).then(()=>{postMessage({type:t})},r=>{postMessage({type:t,err:r})})},r=>{postMessage({type:t,err:r})});break;case"init-ep":{let{epName:r,env:o}=n;Pt(o,r).then(()=>{postMessage({type:t})},i=>{postMessage({type:t,err:i})});break}case"copy-from":{let{buffer:r}=n,o=st(r);postMessage({type:t,out:o});break}case"create":{let{model:r,options:o}=n;zt(r,o).then(i=>{postMessage({type:t,out:i})},i=>{postMessage({type:t,err:i})});break}case"release":Bt(n),postMessage({type:t});break;case"run":{let{sessionId:r,inputIndices:o,inputs:i,outputIndices:s,options:a}=n;Dt(r,o,i,s,new Array(s.length).fill(null),a).then(u=>{u.some(d=>d[3]!=="cpu")?postMessage({type:t,err:"Proxy does not support non-cpu tensor location."}):postMessage({type:t,out:u},Rt([...i,...u]))},u=>{postMessage({type:t,err:u})});break}case"end-profiling":Ot(n),postMessage({type:t});break;default:}}catch(r){postMessage({type:t,err:r})}});Su=Wr?null:e=>new Worker(e??Ie,{type:"classic",name:Nr})});var Ie,Iu,Fr,Tu,Cu,Kr,Au,qr,jr,Xr,it=k(()=>{"use strict";At();Ie=!1?void 0:typeof document<"u"?document.currentScript?.src:typeof self<"u"?self.location?.href:void 0,Iu=!1||typeof location>"u"?void 0:location.origin,Fr=(e,t)=>{try{let n=t??Ie;return(n?new URL(e,n):new URL(e)).origin===Iu}catch{return!1}},Tu=(e,t)=>{let n=t??Ie;try{return(n?new URL(e,n):new URL(e)).href}catch{return}},Cu=(e,t)=>`${t??"./"}${e}`,Kr=async e=>{let n=await(await fetch(e,{credentials:"same-origin"})).blob();return URL.createObjectURL(n)},Au=async e=>(await import(/*webpackIgnore:true*/e)).default,qr=(Hr(),_t(Gr)).default,jr=async()=>{if(!Ie)throw new Error("Failed to load proxy worker: cannot determine the script source URL.");if(Fr(Ie))return[void 0,qr()];let e=await Kr(Ie);return[e,qr(e)]},Xr=async(e,t,n)=>{{let r="ort-wasm-simd-threaded.jsep.mjs",o=e??Tu(r,t),i=!!1&&n&&o&&!Fr(o,t),s=i?await Kr(o):o??Cu(r,t);return[i?s:void 0,await Au(s)]}}});var xn,Sn,Mt,Zr,ku,Eu,kt,se,Ne=k(()=>{"use strict";it();Sn=!1,Mt=!1,Zr=!1,ku=()=>{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}},Eu=()=>{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}},kt=async e=>{if(Sn)return Promise.resolve();if(Mt)throw new Error("multiple calls to 'initializeWebAssembly()' detected.");if(Zr)throw new Error("previous call to 'initializeWebAssembly()' failed.");Mt=!0;let t=e.initTimeout,n=e.numThreads;if(!Eu())throw new Error("WebAssembly SIMD is not supported in the current environment.");let r=ku();n>1&&!r&&(typeof self<"u"&&!self.crossOriginIsolated&&console.warn("env.wasm.numThreads is set to "+n+", 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=n=1);let o=e.wasmPaths,i=typeof o=="string"?o:void 0,s=o?.mjs,a=s?.href??s,u=o?.wasm,d=u?.href??u,l=e.wasmBinary,[c,p]=await Xr(a,i,n>1),h=!1,m=[];if(t>0&&m.push(new Promise(f=>{setTimeout(()=>{h=!0,f()},t)})),m.push(new Promise((f,w)=>{let y={numThreads:n};l?y.wasmBinary=l:(d||i)&&(y.locateFile=(g,b)=>d??(i??b)+g),p(y).then(g=>{Mt=!1,Sn=!0,xn=g,f(),c&&URL.revokeObjectURL(c)},g=>{Mt=!1,Zr=!0,w(g)})})),await Promise.race(m),h)throw new Error(`WebAssembly backend initializing failed due to timeout: ${t}ms`)},se=()=>{if(Sn&&xn)return xn;throw new Error("WebAssembly is not initialized yet.")}});var ue,at,oe,Ut=k(()=>{"use strict";Ne();ue=(e,t)=>{let n=se(),r=n.lengthBytesUTF8(e)+1,o=n._malloc(r);return n.stringToUTF8(e,o,r),t.push(o),o},at=(e,t,n,r)=>{if(typeof e=="object"&&e!==null){if(n.has(e))throw new Error("Circular reference in options");n.add(e)}Object.entries(e).forEach(([o,i])=>{let s=t?t+o:o;if(typeof i=="object")at(i,s+".",n,r);else if(typeof i=="string"||typeof i=="number")r(s,i.toString());else if(typeof i=="boolean")r(s,i?"1":"0");else throw new Error(`Can't handle extra config type: ${typeof i}`)})},oe=e=>{let t=se(),n=t.stackSave();try{let r=t.stackAlloc(8);t._OrtGetLastError(r,r+4);let o=t.HEAP32[r/4],i=t.HEAPU32[r/4+1],s=i?t.UTF8ToString(i):"";throw new Error(`${e} ERROR_CODE: ${o}, ERROR_MESSAGE: ${s}`)}finally{t.stackRestore(n)}}});var Qr,Yr=k(()=>{"use strict";Ne();Ut();Qr=e=>{let t=se(),n=0,r=[],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=ue(e.tag,r)),n=t._OrtCreateRunOptions(o.logSeverityLevel,o.logVerbosityLevel,!!o.terminate,i),n===0&&oe("Can't create run options."),e?.extra!==void 0&&at(e.extra,"",new WeakSet,(s,a)=>{let u=ue(s,r),d=ue(a,r);t._OrtAddRunConfigEntry(n,u,d)!==0&&oe(`Can't set a run config entry: ${s} - ${a}.`)}),[n,r]}catch(i){throw n!==0&&t._OrtReleaseRunOptions(n),r.forEach(s=>t._free(s)),i}}});var Pu,zu,Bu,Du,Jr,eo=k(()=>{"use strict";Ne();Ut();Pu=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}`)}},zu=e=>{switch(e){case"sequential":return 0;case"parallel":return 1;default:throw new Error(`unsupported execution mode: ${e}`)}},Bu=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(n=>(typeof n=="string"?n:n.name)==="webgpu")&&(e.enableMemPattern=!1)},Du=(e,t,n)=>{for(let r of t){let o=typeof r=="string"?r:r.name;switch(o){case"webnn":if(o="WEBNN",typeof r!="string"){let a=r?.deviceType;if(a){let u=ue("deviceType",n),d=ue(a,n);se()._OrtAddSessionConfigEntry(e,u,d)!==0&&oe(`Can't set a session config entry: 'deviceType' - ${a}.`)}}break;case"webgpu":if(o="JS",typeof r!="string"){let s=r;if(s?.preferredLayout){if(s.preferredLayout!=="NCHW"&&s.preferredLayout!=="NHWC")throw new Error(`preferredLayout must be either 'NCHW' or 'NHWC': ${s.preferredLayout}`);let a=ue("preferredLayout",n),u=ue(s.preferredLayout,n);se()._OrtAddSessionConfigEntry(e,a,u)!==0&&oe(`Can't set a session config entry: 'preferredLayout' - ${s.preferredLayout}.`)}}break;case"wasm":case"cpu":continue;default:throw new Error(`not supported execution provider: ${o}`)}let i=ue(o,n);se()._OrtAppendExecutionProvider(e,i)!==0&&oe(`Can't append execution provider: ${o}.`)}},Jr=e=>{let t=se(),n=0,r=[],o=e||{};Bu(o);try{let i=Pu(o.graphOptimizationLevel??"all"),s=zu(o.executionMode??"sequential"),a=typeof o.logId=="string"?ue(o.logId,r):0,u=o.logSeverityLevel??2;if(!Number.isInteger(u)||u<0||u>4)throw new Error(`log serverity level is not valid: ${u}`);let d=o.logVerbosityLevel??0;if(!Number.isInteger(d)||d<0||d>4)throw new Error(`log verbosity level is not valid: ${d}`);let l=typeof o.optimizedModelFilePath=="string"?ue(o.optimizedModelFilePath,r):0;if(n=t._OrtCreateSessionOptions(i,!!o.enableCpuMemArena,!!o.enableMemPattern,s,!!o.enableProfiling,0,a,u,d,l),n===0&&oe("Can't create session options."),o.executionProviders&&Du(n,o.executionProviders,r),o.enableGraphCapture!==void 0){if(typeof o.enableGraphCapture!="boolean")throw new Error(`enableGraphCapture must be a boolean value: ${o.enableGraphCapture}`);let c=ue("enableGraphCapture",r),p=ue(o.enableGraphCapture.toString(),r);t._OrtAddSessionConfigEntry(n,c,p)!==0&&oe(`Can't set a session config entry: 'enableGraphCapture' - ${o.enableGraphCapture}.`)}if(o.freeDimensionOverrides)for(let[c,p]of Object.entries(o.freeDimensionOverrides)){if(typeof c!="string")throw new Error(`free dimension override name must be a string: ${c}`);if(typeof p!="number"||!Number.isInteger(p)||p<0)throw new Error(`free dimension override value must be a non-negative integer: ${p}`);let h=ue(c,r);t._OrtAddFreeDimensionOverride(n,h,p)!==0&&oe(`Can't set a free dimension override: ${c} - ${p}.`)}return o.extra!==void 0&&at(o.extra,"",new WeakSet,(c,p)=>{let h=ue(c,r),m=ue(p,r);t._OrtAddSessionConfigEntry(n,h,m)!==0&&oe(`Can't set a session config entry: ${c} - ${p}.`)}),[n,r]}catch(i){throw n!==0&&t._OrtReleaseSessionOptions(n),r.forEach(s=>t._free(s)),i}}});var ut,We,Fe,Vt,dt,Lt,Nt,In,U=k(()=>{"use strict";ut=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}`)}},We=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}`)}},Fe=(e,t)=>{let n=[-1,4,1,1,2,2,4,8,-1,1,2,8,4,8,-1,-1,-1,-1,-1,-1,-1,.5,.5][e],r=typeof t=="number"?t:t.reduce((o,i)=>o*i,1);return n>0?Math.ceil(r*n):void 0},Vt=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}`)}},dt=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}`)}},Lt=e=>e==="float32"||e==="float16"||e==="int32"||e==="int64"||e==="uint32"||e==="uint8"||e==="bool"||e==="uint4"||e==="int4",Nt=e=>e==="float32"||e==="float16"||e==="int32"||e==="int64"||e==="uint32"||e==="uint64"||e==="int8"||e==="uint8"||e==="bool",In=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 lt,Tn=k(()=>{"use strict";At();lt=async e=>{if(typeof e=="string")if(!1)try{let{readFile:t}=yn("node:fs/promises");return new Uint8Array(await t(e))}catch(t){if(t.code==="ERR_FS_FILE_TOO_LARGE"){let{createReadStream:n}=yn("node:fs"),r=n(e),o=[];for await(let i of r)o.push(i);return new Uint8Array(Buffer.concat(o))}throw t}else{let t=await fetch(e);if(!t.ok)throw new Error(`failed to load external data file: ${e}`);let n=t.headers.get("Content-Length"),r=n?parseInt(n,10):0;if(r<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(r)}catch(a){if(a instanceof RangeError){let u=Math.ceil(r/65536);i=new WebAssembly.Memory({initial:u,maximum:u}).buffer}else throw a}let s=0;for(;;){let{done:a,value:u}=await o.read();if(a)break;let d=u.byteLength;new Uint8Array(i,s,d).set(u),s+=d}return new Uint8Array(i,0,r)}}else return e instanceof Blob?new Uint8Array(await e.arrayBuffer()):e instanceof Uint8Array?e:new Uint8Array(e)}});var Ou,Ru,to,no,Wt,Mu,X,Ae=k(()=>{"use strict";U();Ou=["V","I","W","E","F"],Ru=(e,t)=>{console.log(`[${Ou[e]},${new Date().toISOString()}]${t}`)},Wt=(e,t)=>{to=e,no=t},Mu=(e,t)=>{let n=dt(e),r=dt(to);n>=r&&Ru(n,typeof t=="function"?t():t)},X=(...e)=>{no&&Mu(...e)}});var Gt,Cn=k(()=>{"use strict";U();Gt=(e,t)=>new(Vt(t))(e)});var Ht=k(()=>{"use strict"});var ro,An,kn,Uu,Vu,oo,Pn,En,so,ao=k(()=>{"use strict";Ae();Ht();ro=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]]),An=[],kn=e=>Math.ceil(e/16)*16,Uu=e=>{for(let t=0;tVu++,Pn=async(e,t,n,r)=>{let o=kn(n),i=e.device.createBuffer({size:o,usage:GPUBufferUsage.COPY_DST|GPUBufferUsage.MAP_READ});try{let s=e.getCommandEncoder();e.endComputePass(),s.copyBufferToBuffer(t,0,i,0,o),e.flush(),await i.mapAsync(GPUMapMode.READ);let a=i.getMappedRange();if(r){let u=r();return u.set(new Uint8Array(a,0,n)),u}else return new Uint8Array(a.slice(0,n))}finally{i.destroy()}},En=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[n]of ro)An.push(n),this.freeBuffers.set(n,[]),this.freeUniformBuffers.set(n,[])}upload(t,n){let r=n.buffer,o=n.byteOffset,i=n.byteLength,s=kn(i),a=this.storageCache.get(t);if(!a)throw new Error("gpu data for uploading does not exist");if(a.originalSize!==i)throw new Error(`inconsistent data size. gpu data size=${a.originalSize}, data size=${i}`);let u=this.backend.device.createBuffer({mappedAtCreation:!0,size:s,usage:GPUBufferUsage.MAP_WRITE|GPUBufferUsage.COPY_SRC}),d=u.getMappedRange();new Uint8Array(d).set(new Uint8Array(r,o,i)),u.unmap();let l=this.backend.getCommandEncoder();this.backend.endComputePass(),l.copyBufferToBuffer(u,0,a.gpuData.buffer,0,s),X("verbose",()=>`[WebGPU] GpuDataManager.upload(id=${t})`),this.buffersForUploadingPending.push(u)}memcpy(t,n){let r=this.storageCache.get(t);if(!r)throw new Error("source gpu data for memcpy does not exist");let o=this.storageCache.get(n);if(!o)throw new Error("destination gpu data for memcpy does not exist");if(r.originalSize!==o.originalSize)throw new Error("inconsistent source and destination gpu data size");let i=kn(r.originalSize),s=this.backend.getCommandEncoder();this.backend.endComputePass(),s.copyBufferToBuffer(r.gpuData.buffer,0,o.gpuData.buffer,0,i)}registerExternalBuffer(t,n,r){let o;if(r){if(o=r[0],t===r[1])return X("verbose",()=>`[WebGPU] GpuDataManager.registerExternalBuffer(size=${n}) => 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=oo();return this.storageCache.set(o,{gpuData:{id:o,type:0,buffer:t},originalSize:n}),X("verbose",()=>`[WebGPU] GpuDataManager.registerExternalBuffer(size=${n}) => id=${o}, registered.`),o}unregisterExternalBuffer(t){t!==void 0&&(this.storageCache.delete(t),X("verbose",()=>`[WebGPU] GpuDataManager.unregisterExternalBuffer() => id=${t}`))}create(t,n=GPUBufferUsage.STORAGE|GPUBufferUsage.COPY_SRC|GPUBufferUsage.COPY_DST){let r=Uu(t),o,i=(n&GPUBufferUsage.STORAGE)===GPUBufferUsage.STORAGE,s=(n&GPUBufferUsage.UNIFORM)===GPUBufferUsage.UNIFORM;if(i||s){let d=(i?this.freeBuffers:this.freeUniformBuffers).get(r);d?d.length>0?o=d.pop():o=this.backend.device.createBuffer({size:r,usage:n}):o=this.backend.device.createBuffer({size:r,usage:n})}else o=this.backend.device.createBuffer({size:r,usage:n});let a={id:oo(),type:0,buffer:o};return this.storageCache.set(a.id,{gpuData:a,originalSize:t}),X("verbose",()=>`[WebGPU] GpuDataManager.create(size=${t}) => id=${a.id}`),a}get(t){return this.storageCache.get(t)?.gpuData}release(t){let n=this.storageCache.get(t);if(!n)throw new Error("releasing data does not exist");return X("verbose",()=>`[WebGPU] GpuDataManager.release(id=${t}), gpuDataId=${n.gpuData.id}`),this.storageCache.delete(t),this.buffersPending.push(n.gpuData.buffer),n.originalSize}async download(t,n){let r=this.storageCache.get(t);if(!r)throw new Error("data does not exist");await Pn(this.backend,r.gpuData.buffer,r.originalSize,n)}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 n=ro.get(t.size);if((t.usage&GPUBufferUsage.STORAGE)===GPUBufferUsage.STORAGE){let r=this.freeBuffers.get(t.size)||[];n===void 0||r.length>=n?t.destroy():r.push(t)}else if((t.usage&GPUBufferUsage.UNIFORM)===GPUBufferUsage.UNIFORM){let r=this.freeUniformBuffers.get(t.size)||[];n===void 0||r.length>=n?t.destroy():r.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 n of this.buffersPending)t.push(n);this.buffersPending=[]}}dispose(){this.freeBuffers.forEach(t=>{t.forEach(n=>{n.destroy()})}),this.freeUniformBuffers.forEach(t=>{t.forEach(n=>{n.destroy()})}),this.storageCache.forEach(t=>{t.gpuData.buffer.destroy()}),this.capturedPendingBuffers.forEach(t=>{t.forEach(n=>{n.destroy()})}),this.storageCache=new Map,this.freeBuffers=new Map,this.freeUniformBuffers=new Map,this.capturedPendingBuffers=new Map}onReleaseSession(t){let n=this.capturedPendingBuffers.get(t);n&&(n.forEach(r=>{r.destroy()}),this.capturedPendingBuffers.delete(t))}},so=(...e)=>new En(...e)});var zn,V,ae=k(()=>{"use strict";zn=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}},V=e=>new zn(e)});var Bn,Ee,x,Ke,qt,uo,lo,N=k(()=>{"use strict";Bn=class{static calcMatMulShape(t,n){return t[1]!==n[0]?void 0:[t[0],n[1]]}},Ee=class{static calcShape(t,n,r=!1){let o=t.length,i=n.length;if(o===0)return n;if(i===0)return t;let s=Math.max(t.length,n.length),a=new Array(s);if(r){if(o<2||i<2)return;let u=Bn.calcMatMulShape([t[o-2],t[o-1]],[n[i-2],n[i-1]]);if(u===void 0)return;[a[s-2],a[s-1]]=u}for(let u=r?3:1;u<=s;u++){let d=o-u<0?1:t[o-u],l=i-u<0?1:n[i-u];if(d!==l&&d>1&&l>1)return;let c=Math.max(d,l);if(d&&l)a[s-u]=Math.max(d,l);else{if(c>1)return;a[s-u]=0}}return a}static isValidBroadcast(t,n){let r=t.length,o=n.length;if(r>o)return!1;for(let i=1;i<=r;i++)if(t[r-i]!==1&&t[r-i]!==n[o-i])return!1;return!0}},x=class e{static size(t){return e.getSizeFromDimensionRange(t,0,t.length)}static convertShape(t,n=4){let r=t.length;if(r===0)return[];let o=new Array(r),i=r-1;for(;i>=0;){if(t[i]%n===0){o[i]=t[i]/n;break}if(n%t[i]!==0)throw new Error("cannot convert shape");o[i]=1,n/=t[i],i--}for(i--;i>=0;i--)o[i]=t[i];return o}static sizeFromDimension(t,n){if(n<0||n>t.length)throw new Error(`invalid dimension of ${n} for sizeFromDimension as Tensor has ${t.length} dimensions.`);return e.getSizeFromDimensionRange(t,n,t.length)}static sizeToDimension(t,n){if(n<0||n>t.length)throw new Error(`invalid dimension of ${n} for sizeToDimension as Tensor has ${t.length} dimensions.`);return e.getSizeFromDimensionRange(t,0,n)}static getSizeFromDimensionRange(t,n,r){let o=1;for(let i=n;i=0;--o)r[o]=r[o+1]*t[o+1];return r}static normalizeAxis(t,n){if(t<-n&&t>=n)throw new Error("unsupported axis for this operation.");return t<0?t+n:t}static normalizeAxes(t,n){return t.map(r=>this.normalizeAxis(r,n??t.length))}static sortBasedOnPerm(t,n){return n?n.map(r=>t[r]):t.slice().reverse()}static padShape(t,n){let r=t.length;return t.map((o,i)=>o+n[i]+n[i+r])}static areEqual(t,n){return t.length!==n.length?!1:t.every((r,o)=>r===n[o])}},Ke=class e{static adjustPoolAttributes(t,n,r,o,i,s){if(!t&&r.length!==n.length-2)throw new Error("length of specified kernel shapes should be 2 less than length of input dimensions");if(t)for(let a=0;a=r.length?r.push(n[a+2]):r[a]=n[a+2];for(let a=0;a=r[a]||s[a+r.length]>=r[a])throw new Error("pads should be smaller than kernel")}}static adjustPadsBasedOnAutoPad(t,n,r,o,i,s,a){if(a){if(i.length!==2*(t.length-2))throw new Error("length of pads should be twice the length of data dimensions");if(n.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 u=0;u{"use strict";U();N();je=64,On=(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}`)}},J=(e,t=1)=>{let n=On(e,t);return typeof n=="string"?n:n[0]},de=(e,t=1)=>{let n=On(e,t);return typeof n=="string"?n:n[1]},A=(...e)=>{let t=[];return e.forEach(n=>{n.length!==0&&t.push({type:12,data:n},{type:12,data:x.computeStrides(n)})}),t},te=e=>e%4===0?4:e%2===0?2:1,Rn=(e="f32",t,n="0")=>!t||t===1?`${e}(${n})`:`vec${t}<${e}>(${n})`,Xe=(e,t,n)=>e==="f32"?n:t===1?`f32(${n})`:`vec${t}(${n})`,ke=(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,B=(e,t,n,r)=>e.startsWith("uniforms.")&&n>4?typeof t=="string"?r==="f16"?`${e}[(${t}) / 8][(${t}) % 8 / 4][(${t}) % 8 % 4]`:`${e}[(${t}) / 4][(${t}) % 4]`:r==="f16"?`${e}[${Math.floor(t/8)}][${Math.floor(t%8/4)}][${t%8%4}]`:`${e}[${Math.floor(t/4)}][${t%4}]`:n>1?`${e}[${t}]`:e,Mn=(e,t,n,r,o)=>{let i=typeof n=="number",s=i?n:n.length,a=[...new Array(s).keys()],u=s<2?"u32":s<=4?`vec${s}`:`array`,d=On(t,o),l=typeof d=="string"?d:d[1],c=typeof d=="string"?d:d[0],p={indices:u,value:l,storage:c,tensor:t},h=E=>typeof E=="string"?E:`${E}u`,m={offsetToIndices:!1,indicesToOffset:!1,broadcastedIndicesToOffset:!1,set:!1,setByIndices:!1,get:!1,getByIndices:!1},f=i?"uniforms.":"",w=`${f}${e}_shape`,y=`${f}${e}_strides`,g="";for(let E=0;E ${p.indices} { + var indices: ${p.indices}; + var current = offset; + ${g} + return indices; + }`,_=E=>(m.offsetToIndices=!0,s<2?E:`o2i_${e}(${E})`),$=[];if(s>=2)for(let E=s-1;E>=0;E--)$.push(`${B(y,E,s)} * (indices[${E}])`);let v=s<2?"":` + fn i2o_${e}(indices: ${p.indices}) -> u32 { + return ${$.join("+")}; + }`,I=E=>(m.indicesToOffset=!0,s<2?E:`i2o_${e}(${E})`),T=(...E)=>s===0?"0u":`${p.indices}(${E.map(h).join(",")})`,P=(E,D)=>s<2?`${E}`:`${B(E,D,s)}`,z=(E,D,ne)=>s<2?`${E}=${ne};`:`${B(E,D,s)}=${ne};`,R={},Z=(E,D)=>{m.broadcastedIndicesToOffset=!0;let ne=`${D.name}broadcastedIndicesTo${e}Offset`;if(ne in R)return`${ne}(${E})`;let Ce=[];for(let fe=s-1;fe>=0;fe--){let Re=D.indicesGet("outputIndices",fe+D.rank-s);Ce.push(`${P(y,fe)} * (${Re} % ${P(w,fe)})`)}return R[ne]=`fn ${ne}(outputIndices: ${D.type.indices}) -> u32 { + return ${Ce.length>0?Ce.join("+"):"0u"}; + }`,`${ne}(${E})`},G=(E,D)=>(()=>{if(p.storage===p.value)return`${e}[${E}]=${D};`;if(p.storage==="vec2"&&p.value==="i32")return`${e}[${E}]=vec2(u32(${D}), select(0u, 0xFFFFFFFFu, ${D} < 0));`;if(p.storage==="vec2"&&p.value==="u32")return`${e}[${E}]=vec2(u32(${D}), 0u);`;if(p.storage==="u32"&&p.value==="vec4")return`${e}[${E}]=dot(vec4(0x1, 0x100, 0x10000, 0x1000000), vec4(${D}));`;throw new Error(`not supported combination of storage type ${p.storage} and value type ${p.value} yet`)})(),K=E=>(()=>{if(p.storage===p.value)return`${e}[${E}]`;if(p.storage==="vec2"&&p.value==="i32")return`i32(${e}[${E}].x)`;if(p.storage==="vec2"&&p.value==="u32")return`u32(${e}[${E}].x)`;if(p.storage==="u32"&&p.value==="vec4")return`vec4(bool(${e}[${E}] & 0xFFu), bool(${e}[${E}] & 0xFF00u), bool(${e}[${E}] & 0xFF0000u), bool(${e}[${E}] & 0xFF000000u))`;throw new Error(`not supported combination of storage type ${p.storage} and value type ${p.value} yet`)})(),ie=s<2?"":` + fn get_${e}ByIndices(indices: ${p.indices}) -> ${l} { + return ${K(`i2o_${e}(indices)`)}; + }`,M=s<2?"":(()=>{let E=a.map(ne=>`d${ne}: u32`).join(", "),D=a.map(ne=>`d${ne}`).join(", ");return` + fn get_${e}(${E}) -> ${l} { + return get_${e}ByIndices(${T(D)}); + }`})(),Q=(...E)=>{if(E.length!==s)throw new Error(`indices length must be ${s}`);let D=E.map(h).join(",");return s===0?K("0u"):s===1?K(D[0]):(m.get=!0,m.getByIndices=!0,m.indicesToOffset=!0,`get_${e}(${D})`)},re=E=>s<2?K(E):(m.getByIndices=!0,m.indicesToOffset=!0,`get_${e}ByIndices(${E})`),O=s<2?"":` + fn set_${e}ByIndices(indices: ${p.indices}, value: ${l}) { + ${G(`i2o_${e}(indices)`,"value")} + }`,j=s<2?"":(()=>{let E=a.map(ne=>`d${ne}: u32`).join(", "),D=a.map(ne=>`d${ne}`).join(", ");return` + fn set_${e}(${E}, value: ${l}) { + set_${e}ByIndices(${T(D)}, value); + }`})();return{impl:()=>{let E=[],D=!1;return m.offsetToIndices&&(E.push(b),D=!0),m.indicesToOffset&&(E.push(v),D=!0),m.broadcastedIndicesToOffset&&(Object.values(R).forEach(ne=>E.push(ne)),D=!0),m.set&&(E.push(j),D=!0),m.setByIndices&&(E.push(O),D=!0),m.get&&(E.push(M),D=!0),m.getByIndices&&(E.push(ie),D=!0),!i&&D&&E.unshift(`const ${w} = ${p.indices}(${n.join(",")});`,`const ${y} = ${p.indices}(${x.computeStrides(n).join(",")});`),E.join(` +`)},type:p,offsetToIndices:_,indicesToOffset:I,broadcastedIndicesToOffset:Z,indices:T,indicesGet:P,indicesSet:z,set:(...E)=>{if(E.length!==s+1)throw new Error(`indices length must be ${s}`);let D=E[s];if(typeof D!="string")throw new Error("value must be string");let ne=E.slice(0,s).map(h).join(",");return s===0?G("0u",D):s===1?G(ne[0],D):(m.set=!0,m.setByIndices=!0,m.indicesToOffset=!0,`set_${e}(${ne}, ${D})`)},setByOffset:G,setByIndices:(E,D)=>s<2?G(E,D):(m.setByIndices=!0,m.indicesToOffset=!0,`set_${e}ByIndices(${E}, ${D});`),get:Q,getByOffset:K,getByIndices:re,usage:r,name:e,strides:y,shape:w,rank:s}},S=(e,t,n,r=1)=>Mn(e,t,n,"input",r),C=(e,t,n,r=1)=>Mn(e,t,n,"output",r),Ft=(e,t,n,r=1)=>Mn(e,t,n,"internal",r),Dn=class{constructor(t,n){this.normalizedDispatchGroup=t;this.limits=n;this.internalVariables=[];this.variables=[];this.uniforms=[];this.variableIndex=0}guardAgainstOutOfBoundsWorkgroupSizes(t){return`if (global_idx >= ${typeof t=="number"?`${t}u`:t}) { return; }`}mainStart(t=je){let n=typeof t=="number"?t:t[0],r=typeof t=="number"?1:t[1],o=typeof t=="number"?1:t[2];if(n>this.limits.maxComputeWorkgroupSizeX||r>this.limits.maxComputeWorkgroupSizeY||o>this.limits.maxComputeWorkgroupSizeZ)throw new Error(`workgroup size [${n}, ${r}, ${o}] exceeds the maximum workgroup size [${this.limits.maxComputeWorkgroupSizeX}, ${this.limits.maxComputeWorkgroupSizeY}, ${this.limits.maxComputeWorkgroupSizeZ}].`);if(n*r*o>this.limits.maxComputeInvocationsPerWorkgroup)throw new Error(`workgroup size [${n}, ${r}, ${o}] exceeds the maximum workgroup invocations ${this.limits.maxComputeInvocationsPerWorkgroup}.`);let i=this.normalizedDispatchGroup[1]===1&&this.normalizedDispatchGroup[2]===1,s=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`,a=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 * ${n*r*o}u + local_idx;`;return`@compute @workgroup_size(${n}, ${r}, ${o}) + fn main(${s}) { + ${a} + `}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,n){if(t.usage==="internal")throw new Error("cannot use internal variable with declareVariable(). use registerInternalVariables() instead.");this.variables.push(t),this.appendVariableUniforms(t);let r=t.usage==="input"?"read":"read_write",o=t.type.storage;return`@group(0) @binding(${n}) var ${t.name}: array<${o}>;`}declareVariables(...t){return t.map(n=>this.declareVariable(n,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(n=>this.registerInternalVariable(n)),this}registerUniform(t,n,r=1){return this.uniforms.push({name:t,type:n,length:r}),this}registerUniforms(t){return this.uniforms=this.uniforms.concat(t),this}uniformDeclaration(){if(this.uniforms.length===0)return"";let t=[];for(let{name:n,type:r,length:o}of this.uniforms)if(o&&o>4)r==="f16"?t.push(`@align(16) ${n}:array, ${Math.ceil(o/8)}>`):t.push(`${n}:array, ${Math.ceil(o/4)}>`);else{let i=o==null||o===1?r:`vec${o}<${r}>`;t.push(`${n}:${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=n=>[12,10,1,6][["u32","f16","f32","i32"].indexOf(n)];return this.uniforms.map(n=>[t(n.type),n.length??1])}},co=(e,t)=>new Dn(e,t),Ye=(e,t)=>{let n=e.length,r=[];for(let o=0;o1&&s===1&&r.unshift(i)}return r}});var Lu,po,Nu,Wu,Gu,le,mo,fo,Me=k(()=>{"use strict";U();N();ae();W();Lu=e=>{if(!e||e.length!==1)throw new Error("Transpose requires 1 input.")},po=(e,t)=>t&&t.length!==e?[...new Array(e).keys()].reverse():t,Nu=(e,t)=>x.sortBasedOnPerm(e,po(e.length,t)),Wu=(e,t,n,r)=>{let o=`fn perm(i: ${r.type.indices}) -> ${n.type.indices} { + var a: ${n.type.indices};`;for(let i=0;i{let n=[],r=[];for(let o=0;o{let n=e.dataType,r=e.dims.length,o=po(r,t),i=Nu(e.dims,o),{newShape:s,newPerm:a}=Gu(e.dims,o),u=x.areEqual(a,[2,3,1]),d=x.areEqual(a,[3,1,2]),l=s.length===2&&a[0]>a[1]||u||d,c=l?s:e.dims,p=i;l&&(c=u?[s[0],s[1]*s[2]]:d?[s[0]*s[1],s[2]]:s,p=[c[1],c[0]]);let h=S("a",n,c.length),m=C("output",n,p.length),f=16,w;return l?w=y=>` + ${y.registerUniform("output_size","u32").declareVariables(h,m)} + var tile : array, ${f}>; + ${y.mainStart([f,f,1])} + let stride = (uniforms.output_shape[1] - 1) / ${f} + 1; + let workgroup_id_x = workgroup_index % stride; + let workgroup_id_y = workgroup_index / stride; + let input_col = workgroup_id_y * ${f}u + local_id.x; + let input_row = workgroup_id_x * ${f}u + local_id.y; + if (input_row < uniforms.a_shape[0] && input_col < uniforms.a_shape[1]) { + tile[local_id.y][local_id.x] = ${h.getByIndices(`${h.type.indices}(input_row, input_col)`)}; + } + workgroupBarrier(); + + let output_col = workgroup_id_x * ${f}u + local_id.x; + let output_row = workgroup_id_y * ${f}u + local_id.y; + if (output_row < uniforms.output_shape[0] && output_col < uniforms.output_shape[1]) { + ${m.setByIndices(`${m.type.indices}(output_row, output_col)`,"tile[local_id.x][local_id.y]")} + } + }`:w=y=>` + ${y.registerUniform("output_size","u32").declareVariables(h,m)} + + ${Wu(o,r,h,m)} + + ${y.mainStart()} + ${y.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} + + let indices = ${m.offsetToIndices("global_idx")}; + let aIndices = perm(indices); + + ${m.setByOffset("global_idx",h.getByIndices("aIndices"))} + }`,{name:l?"TransposeShared":"Transpose",shaderCache:{hint:`${t}`,inputDependencies:["rank"]},getRunData:()=>{let y=x.size(i);return{outputs:[{dims:i,dataType:e.dataType}],dispatchGroup:l?{x:Math.ceil(p[1]/f),y:Math.ceil(p[0]/f)}:{x:Math.ceil(y/64)},programUniforms:[{type:12,data:y},...A(c,p)]}},getShaderSource:w}},mo=(e,t)=>{Lu(e.inputs),e.compute(le(e.inputs[0],t.perm))},fo=e=>V({perm:e.perm})});var Hu,qu,Fu,Ku,ju,Xu,Zu,Qu,Yu,Ju,Pe,ho,go,yo,bo,wo,_o,$o,vo,xo,So,Io=k(()=>{"use strict";U();N();W();Kt();Me();Hu={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"},qu={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"},Fu={max:"_A[offset]",min:"_A[offset]",mean:"0",sum:"0",prod:"1",sumSquare:"0",logSumExp:"0",l1:"0",l2:"0",logSum:"0"},Ku={max:"bestValue",min:"bestValue",sum:"bestValue",prod:"bestValue",sumSquare:"bestValue",logSumExp:"log(bestValue)",l1:"bestValue",l2:"sqrt(bestValue)",logSum:"log(bestValue)"},ju=(e,t)=>{let n=[];for(let r=t-e;r{let n=[],r=e.length;for(let i=0;ie[i]);return[n,o]},Zu=(e,t)=>{let n=e.length+t.length,r=[],o=0;for(let i=0;i{for(let n=0;n{let n=[];if(!Qu(e,t)){for(let r=0;rn.push(r))}return n},Ju=(e,t,n,r,o,i,s)=>{let a=n[0].dims,u=x.size(i),d=x.size(s),l=S("_A",n[0].dataType,a),c=C("output",o,i),p=32,h=` + var aBestValues : array; + `;return{name:e,shaderCache:t,getShaderSource:f=>` + ${f.registerUniform("reduceSize","u32").declareVariables(l,c)} + ${h} + fn DIV_CEIL(a : u32, b : u32) -> u32 { + return ((a - 1u) / b + 1u); + } + ${f.mainStart(p)} + + let outputIndex = global_idx / ${p}; + let offset = outputIndex * uniforms.reduceSize; + + var bestValue = f32(${Fu[r]}); + let Length = uniforms.reduceSize; + for (var k = local_idx; k < Length; k = k + ${p}) { + let candidate = f32(${l.getByOffset("offset + k")}); + bestValue = ${Hu[r]}; + } + aBestValues[local_idx] = bestValue; + workgroupBarrier(); + + var reduceSize = min(Length, ${p}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 = ${qu[r]}; + aBestValues[local_idx] = bestValue; + } + reduceSize = interval; + workgroupBarrier(); + } + + if (local_idx == 0u) { + ${c.setByOffset("outputIndex",`${r==="mean"?`${c.type.storage}(bestValue / f32(uniforms.reduceSize))`:`${c.type.storage}(${Ku[r]})`}`)}; + } + }`,getRunData:()=>({outputs:[{dims:i,dataType:o}],dispatchGroup:{x:u},programUniforms:[{type:12,data:d}]})}},Pe=(e,t,n,r)=>{let o=e.inputs.length===1?n:Un(e.inputs,n),i=o.axes;i.length===0&&!o.noopWithEmptyAxes&&(i=e.inputs[0].dims.map((h,m)=>m));let s=x.normalizeAxes(i,e.inputs[0].dims.length),a=s,u=e.inputs[0],d=Yu(a,e.inputs[0].dims.length);d.length>0&&(u=e.compute(le(e.inputs[0],d),{inputs:[0],outputs:[-1]})[0],a=ju(a.length,u.dims.length));let[l,c]=Xu(u.dims,a),p=l;o.keepDims&&(p=Zu(l,s)),e.compute(Ju(t,{hint:o.cacheKey,inputDependencies:["type"]},[u],r,e.inputs[0].dataType,p,c),{inputs:[u]})},ho=(e,t)=>{Pe(e,"ReduceMeanShared",t,"mean")},go=(e,t)=>{Pe(e,"ReduceL1Shared",t,"l1")},yo=(e,t)=>{Pe(e,"ReduceL2Shared",t,"l2")},bo=(e,t)=>{Pe(e,"ReduceLogSumExpShared",t,"logSumExp")},wo=(e,t)=>{Pe(e,"ReduceMaxShared",t,"max")},_o=(e,t)=>{Pe(e,"ReduceMinShared",t,"min")},$o=(e,t)=>{Pe(e,"ReduceProdShared",t,"prod")},vo=(e,t)=>{Pe(e,"ReduceSumShared",t,"sum")},xo=(e,t)=>{Pe(e,"ReduceSumSquareShared",t,"sumSquare")},So=(e,t)=>{Pe(e,"ReduceLogSumShared",t,"logSum")}});var ze,ed,jt,Un,Be,td,nd,rd,od,id,sd,ad,ud,dd,ld,De,To,Co,Ao,ko,Eo,Po,zo,Bo,Do,Oo,Kt=k(()=>{"use strict";U();N();ae();W();Io();ze=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.")},ed=e=>["","",`var value = ${e.getByIndices("input_indices")};`,""],jt=(e,t,n,r,o,i,s=!1,a=!1)=>{let u=[],d=n[0].dims,l=d.length,c=x.normalizeAxes(o,l),p=!a&&c.length===0;d.forEach((w,y)=>{p||c.indexOf(y)>=0?s&&u.push(1):u.push(w)});let h=u.length,m=x.size(u);return{name:e,shaderCache:t,getShaderSource:w=>{let y=[],g=S("_A",n[0].dataType,l),b=C("output",i,h),_=r(g,b,c),$=_[2];for(let v=0,I=0;v=0?(s&&I++,$=`for(var j${v}: u32 = 0; j${v} < ${d[v]}; j${v}++) { + ${_[2].includes("last_index")?`let last_index = j${v};`:""} + ${g.indicesSet("input_indices",v,`j${v}`)} + ${$} + }`):(y.push(`${g.indicesSet("input_indices",v,b.indicesGet("output_indices",I))};`),I++);return` + + ${w.registerUniform("output_size","u32").declareVariables(g,b)} + + ${w.mainStart()} + ${w.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} + var input_indices: ${g.type.indices}; + let output_indices = ${b.offsetToIndices("global_idx")}; + + ${y.join(` +`)} + ${_[0]} // init ops for reduce max/min + ${_[1]} + ${$} + ${_[3]} + ${_.length===4?b.setByOffset("global_idx","value"):_.slice(4).join(` +`)} + }`},getRunData:()=>({outputs:[{dims:u,dataType:i}],dispatchGroup:{x:Math.ceil(m/64)},programUniforms:[{type:12,data:m},...A(d,u)]})}},Un=(e,t)=>{let n=[];return e[1].dims[0]>0&&e[1].getBigInt64Array().forEach(r=>n.push(Number(r))),V({axes:n,keepDims:t.keepDims,noopWithEmptyAxes:t.noopWithEmptyAxes})},Be=(e,t,n,r)=>{let o=e.inputs,i=o.length===1?n:Un(o,n);e.compute(jt(t,{hint:i.cacheKey,inputDependencies:["rank"]},[o[0]],i.noopWithEmptyAxes&&i.axes.length===0?ed:r,i.axes,o[0].dataType,i.keepDims,i.noopWithEmptyAxes),{inputs:[0]})},td=(e,t)=>{ze(e.inputs),Be(e,"ReduceLogSum",t,(r,o)=>[`var value = ${o.type.storage}(0);`,"",`value += ${r.getByIndices("input_indices")};`,"value = log(value);"])},nd=(e,t)=>{ze(e.inputs),Be(e,"ReduceL1",t,(r,o)=>[`var value = ${o.type.storage}(0);`,"",`value += abs(${r.getByIndices("input_indices")});`,""])},rd=(e,t)=>{ze(e.inputs),Be(e,"ReduceL2",t,(r,o)=>[`var t = ${o.type.value}(0); var value = ${o.type.value}(0);`,"",`t = ${r.getByIndices("input_indices")}; value += (t * t);`,"value = sqrt(value);"])},od=(e,t)=>{ze(e.inputs),Be(e,"ReduceLogSumExp",t,(r,o)=>[`var value = ${o.type.storage}(0);`,"",`value += exp(${r.getByIndices("input_indices")});`,"value = log(value);"])},id=(e,t)=>{ze(e.inputs),Be(e,"ReduceMax",t,(r,o,i)=>{let s=[];for(let a=0;a=0||i.length===0)&&s.push(r.indicesSet("input_indices",a,0));return[`${s.join(` +`)}`,`var value = ${r.getByIndices("input_indices")};`,`value = max(value, ${r.getByIndices("input_indices")});`,""]})},sd=(e,t)=>{ze(e.inputs),Be(e,"ReduceMean",t,(r,o,i)=>{let s=1;for(let a=0;a=0||i.length===0)&&(s*=e.inputs[0].dims[a]);return["var sum = f32(0);","",`sum += f32(${r.getByIndices("input_indices")});`,`let value = ${o.type.value}(sum / ${s});`]})},ad=(e,t)=>{ze(e.inputs),Be(e,"ReduceMin",t,(r,o,i)=>{let s=[];for(let a=0;a=0||i.length===0)&&s.push(`input_indices[${a}] = 0;`);return[`${s.join(` +`)}`,`var value = ${r.getByIndices("input_indices")};`,`value = min(value, ${r.getByIndices("input_indices")});`,""]})},ud=(e,t)=>{ze(e.inputs),Be(e,"ReduceProd",t,(r,o)=>[`var value = ${o.type.storage}(1);`,"",`value *= ${r.getByIndices("input_indices")};`,""])},dd=(e,t)=>{ze(e.inputs),Be(e,"ReduceSum",t,(r,o)=>[`var value = ${o.type.storage}(0);`,"",`value += ${r.getByIndices("input_indices")};`,""])},ld=(e,t)=>{ze(e.inputs),Be(e,"ReduceSumSquare",t,(r,o)=>[`var t = ${o.type.value}(0); var value = ${o.type.value}(0);`,"",`t = ${r.getByIndices("input_indices")}; value += t * t;`,""])},De=(e,t,n)=>{if(t.length===0)return n;let r=1,o=1;for(let i=0;i1024},To=(e,t)=>{De(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?sd(e,t):ho(e,t)},Co=(e,t)=>{De(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?nd(e,t):go(e,t)},Ao=(e,t)=>{De(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?rd(e,t):yo(e,t)},ko=(e,t)=>{De(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?od(e,t):bo(e,t)},Eo=(e,t)=>{De(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?id(e,t):wo(e,t)},Po=(e,t)=>{De(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?ad(e,t):_o(e,t)},zo=(e,t)=>{De(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?ud(e,t):$o(e,t)},Bo=(e,t)=>{De(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?dd(e,t):vo(e,t)},Do=(e,t)=>{De(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?ld(e,t):xo(e,t)},Oo=(e,t)=>{De(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?td(e,t):So(e,t)}});var Ro,Mo,Uo,Vn,Vo=k(()=>{"use strict";U();ae();Kt();Ro=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.")},Mo=(e,t)=>{Ro(e.inputs);let n=(r,o,i)=>{let s=[];for(let a=0;a=0||i.length===0)&&s.push(`input_indices[${a}] = 0;`);return[`${s.join(` +`)}`,`var value = ${r.getByIndices("input_indices")}; +var best_index : i32 = 0;`,`if (${r.getByIndices("input_indices")} ${t.selectLastIndex>0?"<=":"<"} value) { + value = ${r.getByIndices("input_indices")}; + best_index = i32(last_index); + }`,"",o.setByOffset("global_idx","best_index")]};e.compute(jt("ArgMin",{hint:t.cacheKey,inputDependencies:["rank"]},[e.inputs[0]],n,[t.axis],7,t.keepDims),{inputs:[0]})},Uo=(e,t)=>{Ro(e.inputs);let n=(r,o,i)=>{let s=[];for(let a=0;a=0||i.length===0)&&s.push(`input_indices[${a}] = 0;`);return[`${s.join(` +`)}`,`var value = ${r.getByIndices("input_indices")}; +var best_index : i32 = 0;`,`if (${r.getByIndices("input_indices")} ${t.selectLastIndex>0?">=":">"} value) { + value = ${r.getByIndices("input_indices")}; + best_index = i32(last_index); + }`,"",o.setByOffset("global_idx","best_index")]};e.compute(jt("argMax",{hint:t.cacheKey,inputDependencies:["rank"]},[e.inputs[0]],n,[t.axis],7,t.keepDims),{inputs:[0]})},Vn=e=>V(e)});var cd,pd,md,fd,Je,hd,Lo,Xt=k(()=>{"use strict";U();N();Ht();W();cd=(e,t)=>{let n=e[0],r=e[1],o=e[2],i=e[3],s=e[4],a=e[5];if(s&&a)throw new Error("Attention cannot have both past and attention_bias");if(n.dims.length!==3)throw new Error('Input "input" must have 3 dimensions');let u=n.dims[0],d=n.dims[1],l=n.dims[2];if(o.dims.length!==1)throw new Error('Input "bias" is expected to have 1 dimensions');if(r.dims.length!==2)throw new Error('Input "weights" is expected to have 2 dimensions');if(r.dims[0]!==l)throw new Error("Input 1 dimension 0 should have same length as dimension 2 of input 0");if(o.dims[0]!==r.dims[1])throw new Error('Input "bias" dimension 0 should have same length as dimension 1 of input "weights"');let c=o.dims[0]/3,p=c,h=p;if(t.qkvHiddenSizes.length>0){if(t.qkvHiddenSizes.length!==3)throw new Error("qkv_hidden_sizes attribute should have 3 elements");for(let b of t.qkvHiddenSizes)if(b%t.numHeads!==0)throw new Error("qkv_hidden_sizes should be divisible by num_heads");c=t.qkvHiddenSizes[0],p=t.qkvHiddenSizes[1],h=t.qkvHiddenSizes[2]}let m=d;if(c!==p)throw new Error("qkv_hidden_sizes first element should be same as the second");if(o.dims[0]!==c+p+h)throw new Error('Input "bias" dimension 0 should have same length as sum of Q/K/V hidden sizes');let f=0;if(s){if(p!==h)throw new Error('Input "past" expect k_hidden_size == v_hidden_size');if(s.dims.length!==5)throw new Error('Input "past" must have 5 dimensions');if(s.dims[0]!==2)throw new Error('Input "past" first dimension must be 2');if(s.dims[1]!==u)throw new Error('Input "past" second dimension must be batch_size');if(s.dims[2]!==t.numHeads)throw new Error('Input "past" third dimension must be num_heads');if(s.dims[4]!==p/t.numHeads)throw new Error('Input "past" fifth dimension must be k_hidden_size / num_heads');t.pastPresentShareBuffer||(f=s.dims[3])}let w=m+f,y=-1,g=0;if(i)throw new Error("Mask not supported");if(s)throw new Error("past is not supported");if(a){if(a.dims.length!==4)throw new Error('Input "attention_bias" must have 4 dimensions');if(a.dims[0]!==u||a.dims[1]!==t.numHeads||a.dims[2]!==d||a.dims[3]!==w)throw new Error('Expect "attention_bias" shape (batch_size, num_heads, sequence_length, total_sequence_length)')}return{batchSize:u,sequenceLength:d,pastSequenceLength:f,kvSequenceLength:m,totalSequenceLength:w,maxSequenceLength:y,inputHiddenSize:l,hiddenSize:c,vHiddenSize:h,headSize:Math.floor(c/t.numHeads),vHeadSize:Math.floor(h/t.numHeads),numHeads:t.numHeads,isUnidirectional:!1,pastPresentShareBuffer:!1,maskFilterValue:t.maskFilterValue,maskType:g,scale:t.scale,broadcastResPosBias:!1,passPastInKv:!1,qkvFormat:1}},pd=(e,t,n)=>{let r=te(n),o=64,i=n/r;i{let h=C("x",e.dataType,e.dims,r),m=de(e.dataType),f=[{name:"d_inv",type:"f32"},{name:"d_comp",type:"u32"},{name:"elements_per_thread",type:"u32"}];return` + var thread_max: array; + var thread_sum: array; + ${p.registerUniforms(f).declareVariables(h)} + ${p.mainStart([o,1,1])} + let local_offset = local_idx * uniforms.elements_per_thread; + let offset = (global_idx / ${o}) * uniforms.d_comp + local_offset; + + var thread_max_vector = ${d}(-3.402823e+38f); + for (var i: u32 = 0; i < uniforms.elements_per_thread && i + local_offset < uniforms.d_comp; i++) { + thread_max_vector = max(${d}(x[offset + i]), thread_max_vector); + } + thread_max[local_idx] = ${(()=>{switch(r){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: ${r}`)}})()}; + workgroupBarrier(); + + var max_value = f32(-3.402823e+38f); + for (var i = 0u; i < ${o}; i++) { + max_value = max(thread_max[i], max_value); + } + + var sum_vector = ${d}(0); + for (var i: u32 = 0; i < uniforms.elements_per_thread && i + local_offset < uniforms.d_comp; i++) { + sum_vector += exp(${d}(x[offset + i]) - max_value); + } + thread_sum[local_idx] = ${(()=>{switch(r){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: ${r}`)}})()}; + workgroupBarrier(); + + var sum: f32 = 0; + for (var i = 0u; i < ${o}; i++) { + sum += thread_sum[i]; + } + + if (sum == 0) { + for (var i: u32 = 0; i < uniforms.elements_per_thread && i + local_offset < uniforms.d_comp; i++) { + x[offset + i] = ${h.type.value}(${m}(uniforms.d_inv)); + } + } else { + for (var i: u32 = 0; i < uniforms.elements_per_thread && i + local_offset < uniforms.d_comp; i++) { + var f32input = ${d}(x[offset + i]); + x[offset + i] = ${h.type.value}(exp(f32input - max_value) / sum); + } + } + }`};return{name:"AttentionProbsSoftmax",shaderCache:{hint:`${o};${u};${r}`,inputDependencies:l},getShaderSource:c,getRunData:()=>({outputs:[],dispatchGroup:{x:t},programUniforms:a})}},md=(e,t,n,r,o,i,s,a)=>{let u=a+i.kvSequenceLength,d=[i.batchSize,i.numHeads,i.sequenceLength,u],l=i.kvNumHeads===void 0&&e>1&&r,c=l?[i.batchSize,i.numHeads,u,i.headSize]:void 0,p=s.scale===0?1/Math.sqrt(i.headSize):s.scale,h=te(i.headSize),m=i.headSize/h,f=12,w={x:Math.ceil(u/f),y:Math.ceil(i.sequenceLength/f),z:i.batchSize*i.numHeads},y=[{type:12,data:i.sequenceLength},{type:12,data:m},{type:12,data:u},{type:12,data:i.numHeads},{type:1,data:p},{type:12,data:a},{type:12,data:i.kvSequenceLength}],g=l&&r&&x.size(r.dims)>0,b=["type","type"];g&&b.push("type"),o&&b.push("type");let _=[{dims:d,dataType:t.dataType,gpuDataType:0}];l&&_.push({dims:c,dataType:t.dataType,gpuDataType:0});let $=v=>{let I=S("q",t.dataType,t.dims,h),T=S("key",n.dataType,n.dims,h),P=[I,T];if(g){let K=S("past_key",r.dataType,r.dims,h);P.push(K)}o&&P.push(S("attention_bias",o.dataType,o.dims));let z=C("output",t.dataType,d),R=[z];l&&R.push(C("present_key",t.dataType,c,h));let Z=de(1,h),G=[{name:"M",type:"u32"},{name:"K",type:"u32"},{name:"N",type:"u32"},{name:"num_heads",type:"u32"},{name:"alpha",type:"f32"},{name:"past_sequence_length",type:"u32"},{name:"kv_sequence_length",type:"u32"}];return` + const TILE_SIZE = ${f}u; + + var tileQ: array<${I.type.storage}, ${f*f}>; + var tileK: array<${I.type.storage}, ${f*f}>; + ${v.registerUniforms(G).declareVariables(...P,...R)} + ${v.mainStart([f,f,1])} + // x holds the N and y holds the M + let headIdx = workgroup_id.z; + let m = workgroup_id.y * TILE_SIZE; + let n = workgroup_id.x * TILE_SIZE; + let qOffset = uniforms.M * uniforms.K * headIdx + m * uniforms.K; + ${(()=>g&&l?` + let kOffset = uniforms.kv_sequence_length * uniforms.K * headIdx; + let pastKeyOffset = uniforms.past_sequence_length * uniforms.K * headIdx;`:` + let kOffset = uniforms.N * uniforms.K * headIdx + n * uniforms.K;`)()} + ${l?"let presentKeyOffset = headIdx * uniforms.N * uniforms.K;":""} + var value = ${Z}(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; + ${(()=>g&&l?` + if (n + local_id.y < uniforms.past_sequence_length) { + tileK[idx] = past_key[pastKeyOffset + (n + local_id.y) * uniforms.K + w + local_id.x]; + } else { + tileK[idx] = + key[kOffset + (n + local_id.y - uniforms.past_sequence_length) * uniforms.K + w + local_id.x]; + }`:"tileK[idx] = key[kOffset + local_id.y * uniforms.K + w + local_id.x];")()} + ${l?"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 += ${Z}(tileQ[TILE_SIZE * local_id.y + k] * tileK[TILE_SIZE * local_id.x + k]); + } + + workgroupBarrier(); + } + + let headOffset = headIdx * uniforms.M * uniforms.N; + if (global_id.y < uniforms.M && global_id.x < uniforms.N) { + let outputIdx = headOffset + global_id.y * uniforms.N + global_id.x; + var sum: f32 = ${(()=>{switch(h){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: ${h}`)}})()}; + output[outputIdx] = ${z.type.value} (sum * uniforms.alpha) + ${o?"attention_bias[outputIdx]":"0.0"}; + } + }`};return{name:"AttentionProbs",shaderCache:{hint:`${h};${o!==void 0};${r!==void 0};${e}`,inputDependencies:b},getRunData:()=>({outputs:_,dispatchGroup:w,programUniforms:y}),getShaderSource:$}},fd=(e,t,n,r,o,i)=>{let s=i+o.kvSequenceLength,a=o.nReps?o.nReps:1,u=o.vHiddenSize*a,d=o.kvNumHeads==null&&e>1&&r,l=d?[o.batchSize,o.numHeads,s,o.headSize]:void 0,c=[o.batchSize,o.sequenceLength,u],p=12,h={x:Math.ceil(o.vHeadSize/p),y:Math.ceil(o.sequenceLength/p),z:o.batchSize*o.numHeads},m=[{type:12,data:o.sequenceLength},{type:12,data:s},{type:12,data:o.vHeadSize},{type:12,data:o.numHeads},{type:12,data:u},{type:12,data:i},{type:12,data:o.kvSequenceLength}],f=d&&r&&x.size(r.dims)>0,w=["type","type"];f&&w.push("type");let y=[{dims:c,dataType:t.dataType,gpuDataType:0}];d&&y.push({dims:l,dataType:t.dataType,gpuDataType:0});let g=b=>{let _=S("probs",t.dataType,t.dims),$=S("v",n.dataType,n.dims),v=[_,$];f&&v.push(S("past_value",r.dataType,r.dims));let T=[C("output",t.dataType,c)];d&&T.push(C("present_value",t.dataType,l));let P=[{name:"M",type:"u32"},{name:"K",type:"u32"},{name:"N",type:"u32"},{name:"num_heads",type:"u32"},{name:"v_hidden_size",type:"u32"},{name:"past_sequence_length",type:"u32"},{name:"kv_sequence_length",type:"u32"}];return` + const TILE_SIZE = ${p}u; + var tileQ: array<${_.type.value}, ${p*p}>; + var tileK: array<${_.type.value}, ${p*p}>; + ${b.registerUniforms(P).declareVariables(...v,...T)} + ${b.mainStart([p,p,1])} + let headIdx = workgroup_id.z; + let m = global_id.y; + let n = global_id.x; + + let offsetA = headIdx * (uniforms.M * uniforms.K) + m * uniforms.K; + ${(()=>f&&d?` + let pastValueOffset = headIdx * uniforms.N * uniforms.past_sequence_length + n; + let vOffset = headIdx * uniforms.N * uniforms.kv_sequence_length + n; + `:` + let offsetB = headIdx * uniforms.N * uniforms.K + n; + `)()} + ${d?"let presentValueOffset = headIdx * uniforms.N * uniforms.K + n;":""} + var value = ${_.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; + ${(()=>f&&d?` + if (w + local_id.y < uniforms.past_sequence_length) { + tileK[idx] = past_value[pastValueOffset + (w + local_id.y) * uniforms.N]; + } else { + tileK[idx] = v[vOffset + (w + local_id.y - uniforms.past_sequence_length) * uniforms.N]; + } + `:` + tileK[idx] = v[offsetB + (w + local_id.y) * uniforms.N]; + `)()} + ${d?"present_value[presentValueOffset + (w + local_id.y) * uniforms.N] = tileK[idx];":""} + } + workgroupBarrier(); + for (var k: u32 = 0u; k < TILE_SIZE && w+k < uniforms.K; k++) { + value += tileQ[TILE_SIZE * local_id.y + k] * tileK[TILE_SIZE * k + local_id.x]; + } + workgroupBarrier(); + } + + // we need to transpose output from BNSH_v to BSND_v + let batchIdx = workgroup_id.z / uniforms.num_heads; + let currentBatchHeadNumber = workgroup_id.z % uniforms.num_heads; + if (m < uniforms.M && n < uniforms.N) { + let outputIdx = batchIdx * uniforms.M * uniforms.v_hidden_size + m * uniforms.v_hidden_size + + currentBatchHeadNumber * uniforms.N + n; + output[outputIdx] = value; + } + }`};return{name:"AttentionScore",shaderCache:{hint:`${r!==void 0};${e}`,inputDependencies:w},getRunData:()=>({outputs:y,dispatchGroup:h,programUniforms:m}),getShaderSource:g}},Je=(e,t,n,r,o,i,s,a,u,d,l)=>{let c=Math.min(e.outputCount,1+(s?1:0)+(a?1:0)),p=d.kvNumHeads!==void 0||c>1?d.pastSequenceLength:0,h=p+d.kvSequenceLength,m=u&&x.size(u.dims)>0?u:void 0,f=[t,n];d.kvNumHeads===void 0&&c>1&&s&&x.size(s.dims)>0&&f.push(s),m&&f.push(m);let w=e.compute(md(c,t,n,s,m,d,l,p),{inputs:f,outputs:d.kvNumHeads===void 0&&c>1?[-1,1]:[-1]})[0];e.compute(pd(w,d.batchSize*d.numHeads*d.sequenceLength,h),{inputs:[w],outputs:[]});let y=[w,r];d.kvNumHeads===void 0&&c>1&&a&&x.size(a.dims)>0&&y.push(a),e.compute(fd(c,w,r,a,d,p),{inputs:y,outputs:d.kvNumHeads===void 0&&c>1?[0,2]:[0]})},hd=(e,t)=>{let n=[t.batchSize,t.numHeads,t.sequenceLength,t.headSize],r=t.sequenceLength,o=t.inputHiddenSize,i=t.headSize,s=12,a={x:Math.ceil(t.headSize/s),y:Math.ceil(t.sequenceLength/s),z:t.batchSize*t.numHeads},u=[e.inputs[0],e.inputs[1],e.inputs[2]],d=[{type:12,data:r},{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}],l=c=>{let p=C("output_q",u[0].dataType,n),h=C("output_k",u[0].dataType,n),m=C("output_v",u[0].dataType,n),f=S("input",u[0].dataType,u[0].dims),w=S("weight",u[1].dataType,u[1].dims),y=S("bias",u[2].dataType,u[2].dims),g=f.type.storage,b=[{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 = ${s}u; + var tileInput: array<${g}, ${s*s}>; + var tileWeightQ: array<${g}, ${s*s}>; + var tileWeightK: array<${g}, ${s*s}>; + var tileWeightV: array<${g}, ${s*s}>; + ${c.registerUniforms(b).declareVariables(f,w,y,p,h,m)} + ${c.mainStart([s,s,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 = ${g}(0); + var valueK = ${g}(0); + var valueV = ${g}(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:n,dataType:e.inputs[0].dataType,gpuDataType:0},{dims:n,dataType:e.inputs[0].dataType,gpuDataType:0},{dims:n,dataType:e.inputs[0].dataType,gpuDataType:0}],dispatchGroup:a,programUniforms:d}),getShaderSource:l},{inputs:u,outputs:[-1,-1,-1]})},Lo=(e,t)=>{let n=cd(e.inputs,t),[r,o,i]=hd(e,n);return Je(e,r,o,i,e.inputs[4],void 0,void 0,void 0,e.inputs[5],n,t)}});var gd,yd,bd,No,Wo=k(()=>{"use strict";Se();U();N();ae();W();gd=(e,t)=>{if(!e||e.length!==5)throw new Error("BatchNormalization requires 5 inputs");let n=(r,o,i)=>{let s=o.length;if(s!==r.length)throw new Error(`${i}: num dimensions != ${s}`);o.forEach((a,u)=>{if(a!==r[u])throw new Error(`${i}: dim[${u}] do not match`)})};if(e[0].dims.length>1){let r=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);n(e[1].dims,r,"Invalid input scale"),n(e[2].dims,r,"Invalid input B"),n(e[3].dims,r,"Invalid input mean"),n(e[4].dims,r,"Invalid input var")}else n(e[1].dims,[1],"Invalid input scale"),n(e[2].dims,[1],"Invalid input B"),n(e[3].dims,[1],"Invalid input mean"),n(e[4].dims,[1],"Invalid input var")},yd=(e,t)=>{let{epsilon:n,spatial:r,format:o}=t,i=e[0].dims,s=r?te(i[i.length-1]):1,a=o==="NHWC"&&i.length>1?s:1,u=x.size(i)/s,d=r,l=d?i.length:i,c=S("x",e[0].dataType,e[0].dims,s),p=S("scale",e[1].dataType,e[1].dims,a),h=S("bias",e[2].dataType,e[2].dims,a),m=S("inputMean",e[3].dataType,e[3].dims,a),f=S("inputVar",e[4].dataType,e[4].dims,a),w=C("y",e[0].dataType,l,s),y=()=>{let b="";if(r)b=`let cOffset = ${i.length===1?"0u":o==="NHWC"?`outputIndices[${i.length-1}] / ${s}`:"outputIndices[1]"};`;else if(o==="NCHW")b=` + ${w.indicesSet("outputIndices","0","0")} + let cOffset = ${w.indicesToOffset("outputIndices")};`;else{b=`var cIndices = ${p.type.indices}(0); + cIndices[0] = outputIndices[${i.length-1}];`;for(let _=1;_` + const epsilon = ${n}; + ${b.registerUniform("outputSize","u32").declareVariables(c,p,h,m,f,w)} + ${b.mainStart()} + ${b.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.outputSize")} + var outputIndices = ${w.offsetToIndices(`global_idx * ${s}`)}; + ${y()} + let scale = ${p.getByOffset("cOffset")}; + let bias = ${h.getByOffset("cOffset")}; + let inputMean = ${m.getByOffset("cOffset")}; + let inputVar = ${f.getByOffset("cOffset")}; + let x = ${c.getByOffset("global_idx")}; + let value = (x - inputMean) * inverseSqrt(inputVar + epsilon) * scale + bias; + ${w.setByOffset("global_idx","value")} + }`;return{name:"BatchNormalization",shaderCache:{hint:`${t.epsilon}_${t.format}_${r}_${s}`,inputDependencies:d?["rank","type","type","type","type"]:void 0},getShaderSource:g,getRunData:()=>({outputs:[{dims:e[0].dims,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(u/64)},programUniforms:d?[{type:12,data:u},...A(i)]:[{type:12,data:u}]})}},bd=e=>V(e),No=(e,t)=>{let{inputs:n,outputCount:r}=e,o=bd({...t,outputCount:r});if(Y.webgpu.validateInputContent&&gd(n,o),t.trainingMode)throw new Error("BatchNormalization trainingMode is not supported yet.");e.compute(yd(n,o))}});var wd,_d,Go,Ho=k(()=>{"use strict";N();W();wd=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")},_d=e=>{let t=e[0].dims,n=e[0].dims[2],r=x.size(t)/4,o=e[0].dataType,i=S("input",o,t,4),s=S("bias",o,[n],4),a=S("residual",o,t,4),u=C("output",o,t,4);return{name:"BiasAdd",getRunData:()=>({outputs:[{dims:t,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(r/64)}}),getShaderSource:l=>` + const channels = ${n}u / 4; + ${l.declareVariables(i,s,a,u)} + + ${l.mainStart()} + ${l.guardAgainstOutOfBoundsWorkgroupSizes(r)} + let value = ${i.getByOffset("global_idx")} + + ${s.getByOffset("global_idx % channels")} + ${a.getByOffset("global_idx")}; + ${u.setByOffset("global_idx","value")} + }`}},Go=e=>{wd(e.inputs),e.compute(_d(e.inputs))}});var $d,ee,qo,Fo,Ko,jo,Xo,Zo,Qo,Yo,Jo,vd,ei,ti,ni,ri,ct,oi,Zt,ii,si,ai,ui,di,li,ci,pi,mi,fi,hi,gi,yi,bi,wi,_i,$i,vi,Ln,Nn,xi,Si,Ii,xd,Sd,Ti,Qt=k(()=>{"use strict";U();N();ae();W();$d=(e,t,n,r,o,i,s)=>{let a=Math.ceil(t/4),u="";typeof o=="string"?u=`${o}(a)`:u=o("a");let d=S("inputData",n,[a],4),l=C("outputData",r,[a],4),c=[{name:"vec_size",type:"u32"}];return s&&c.push(...s),` + ${e.registerUniforms(c).declareVariables(d,l)} + + ${i??""} + + ${e.mainStart()} + ${e.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.vec_size")} + + let a = ${d.getByOffset("global_idx")}; + ${l.setByOffset("global_idx",u)} + }`},ee=(e,t,n,r,o,i=e.dataType,s,a)=>{let u=[{type:12,data:Math.ceil(x.size(e.dims)/4)}];return s&&u.push(...s),{name:t,shaderCache:{hint:o,inputDependencies:["type"]},getShaderSource:d=>$d(d,x.size(e.dims),e.dataType,i,n,r,a),getRunData:d=>({outputs:[{dims:e.dims,dataType:i}],dispatchGroup:{x:Math.ceil(x.size(d[0].dims)/64/4)},programUniforms:u})}},qo=e=>{e.compute(ee(e.inputs[0],"Abs","abs"))},Fo=e=>{e.compute(ee(e.inputs[0],"Acos","acos"))},Ko=e=>{e.compute(ee(e.inputs[0],"Acosh","acosh"))},jo=e=>{e.compute(ee(e.inputs[0],"Asin","asin"))},Xo=e=>{e.compute(ee(e.inputs[0],"Asinh","asinh"))},Zo=e=>{e.compute(ee(e.inputs[0],"Atan","atan"))},Qo=e=>{e.compute(ee(e.inputs[0],"Atanh","atanh"))},Yo=e=>V(e),Jo=(e,t)=>{let n;switch(t.to){case 10:n="vec4";break;case 1:n="vec4";break;case 12:n="vec4";break;case 6:n="vec4";break;case 9:n="vec4";break;default:throw new RangeError(`not supported type (specified in attribute 'to' from 'Cast' operator): ${t.to}`)}e.compute(ee(e.inputs[0],"Cast",n,void 0,t.cacheKey,t.to))},vd=e=>{let t,n,r=e.length>=2&&e[1].data!==0,o=e.length>=3&&e[2].data!==0;switch(e[0].dataType){case 1:t=r?e[1].getFloat32Array()[0]:-34028234663852886e22,n=o?e[2].getFloat32Array()[0]:34028234663852886e22;break;case 10:t=r?e[1].getUint16Array()[0]:64511,n=o?e[2].getUint16Array()[0]:31743;break;default:throw new Error("Unsupport data type")}return V({min:t,max:n})},ei=(e,t)=>{let n=t||vd(e.inputs),r=de(e.inputs[0].dataType);e.compute(ee(e.inputs[0],"Clip",o=>`clamp(${o}, vec4<${r}>(uniforms.min), vec4<${r}>(uniforms.max))`,void 0,n.cacheKey,void 0,[{type:e.inputs[0].dataType,data:n.min},{type:e.inputs[0].dataType,data:n.max}],[{name:"min",type:r},{name:"max",type:r}]),{inputs:[0]})},ti=e=>{e.compute(ee(e.inputs[0],"Ceil","ceil"))},ni=e=>{e.compute(ee(e.inputs[0],"Cos","cos"))},ri=e=>{e.compute(ee(e.inputs[0],"Cosh","cosh"))},ct=e=>V(e),oi=(e,t)=>{let n=de(e.inputs[0].dataType);e.compute(ee(e.inputs[0],"Elu",r=>`elu_vf32(${r})`,` + const elu_alpha_ = ${n}(${t.alpha}); + + fn elu_f32(a: ${n}) -> ${n} { + return select((exp(a) - 1.0) * elu_alpha_, a, a >= 0.0); + } + + fn elu_vf32(v: vec4<${n}>) -> vec4<${n}> { + return vec4(elu_f32(v.x), elu_f32(v.y), elu_f32(v.z), elu_f32(v.w)); + }`,t.cacheKey))},Zt=(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)); +}`,ii=e=>{let t=de(e.inputs[0].dataType);e.compute(ee(e.inputs[0],"Erf",n=>`erf_vf32(${n})`,Zt(t)))},si=e=>{e.compute(ee(e.inputs[0],"Exp","exp"))},ai=e=>{e.compute(ee(e.inputs[0],"Floor","floor"))},ui=e=>{let t=de(e.inputs[0].dataType);e.compute(ee(e.inputs[0],"Gelu",n=>`0.5 * ${n} * (1.0 + erf_vf32(${n} * 0.7071067811865475))`,Zt(t)))},di=(e,t)=>{let n=de(e.inputs[0].dataType);e.compute(ee(e.inputs[0],"LeakyRelu",r=>`select(leaky_relu_alpha_ * ${r}, ${r}, ${r} >= vec4<${n}>(0.0))`,`const leaky_relu_alpha_ = ${n}(${t.alpha});`,t.cacheKey))},li=e=>{e.compute(ee(e.inputs[0],"Not",t=>`!${t}`))},ci=e=>{e.compute(ee(e.inputs[0],"Neg",t=>`-${t}`))},pi=e=>{e.compute(ee(e.inputs[0],"Reciprocal",t=>`1.0/${t}`))},mi=e=>{let t=de(e.inputs[0].dataType);e.compute(ee(e.inputs[0],"Relu",n=>`select(vec4<${t}>(0.0), ${n}, ${n} > vec4<${t}>(0.0))`))},fi=e=>{e.compute(ee(e.inputs[0],"Sigmoid",t=>`(1.0 / (1.0 + exp(-${t})))`))},hi=e=>V(e),gi=(e,t)=>{let n=de(e.inputs[0].dataType);e.compute(ee(e.inputs[0],"HardSigmoid",r=>`max(vec4<${n}>(0.0), min(vec4<${n}>(1.0), ${t.alpha} * ${r} + vec4<${n}>(${t.beta})))`,void 0,t.cacheKey))},yi=e=>{e.compute(ee(e.inputs[0],"Sin","sin"))},bi=e=>{e.compute(ee(e.inputs[0],"Sinh","sinh"))},wi=e=>{e.compute(ee(e.inputs[0],"Sqrt","sqrt"))},_i=e=>{e.compute(ee(e.inputs[0],"Tan","tan"))},$i=e=>`sign(${e}) * (1 - exp(-2 * abs(${e}))) / (1 + exp(-2 * abs(${e})))`,vi=e=>{e.compute(ee(e.inputs[0],"Tanh",$i))},Ln=(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 ${$i("v")}; +} +`,Nn=e=>`(fast_gelu_a + fast_gelu_a * tanh_v(${e} * (fast_gelu_c * ${e} * ${e} + fast_gelu_b))) * ${e}`,xi=e=>{let t=de(e.inputs[0].dataType);e.compute(ee(e.inputs[0],"FastGelu",Nn,Ln(t),void 0,e.inputs[0].dataType))},Si=(e,t)=>{let n=de(e.inputs[0].dataType);return e.compute(ee(e.inputs[0],"ThresholdedRelu",r=>`select(vec4<${n}>(0.0), ${r}, ${r} > thresholded_relu_alpha_)`,`const thresholded_relu_alpha_ = vec4<${n}>(${t.alpha});`,t.cacheKey)),0},Ii=e=>{e.compute(ee(e.inputs[0],"Log","log"))},xd=(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; +} +`,Sd=e=>`quick_gelu_impl(${e})`,Ti=(e,t)=>{let n=de(e.inputs[0].dataType);e.compute(ee(e.inputs[0],"QuickGelu",Sd,xd(n,t.alpha),t.cacheKey,e.inputs[0].dataType))}});var Id,Td,Ai,ki=k(()=>{"use strict";N();W();Qt();Id=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")},Td=e=>{let t=e[0].dims.slice();t[2]=t[2]/2;let n=S("input",e[0].dataType,e[0].dims,4),r=S("bias",e[0].dataType,[e[0].dims[2]],4),o=C("output",e[0].dataType,t,4),i=x.size(t)/4,s=J(e[0].dataType);return{name:"BiasSplitGelu",getRunData:()=>({outputs:[{dims:t,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(i/64)}}),getShaderSource:u=>` + const M_SQRT2 = sqrt(2.0); + const halfChannels = ${e[0].dims[2]/4/2}u; + + ${u.declareVariables(n,r,o)} + + ${Zt(s)} + + ${u.mainStart()} + ${u.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")} + }`}},Ai=e=>{Id(e.inputs),e.compute(Td(e.inputs))}});var Cd,Ad,Oe,Ei,Pi,zi,Bi,Di,Oi,Ri,Mi,Ui,Vi,Li=k(()=>{"use strict";U();N();W();Cd=(e,t,n,r,o,i,s,a,u,d,l,c)=>{let p,h;typeof a=="string"?p=h=(g,b)=>`${a}((${g}),(${b}))`:typeof a=="function"?p=h=a:(p=a.scalar,h=a.vector);let m=C("outputData",l,r.length,4),f=S("aData",u,t.length,4),w=S("bData",d,n.length,4),y;if(o)if(i){let g=x.size(t)===1,b=x.size(n)===1,_=t.length>0&&t[t.length-1]%4===0,$=n.length>0&&n[n.length-1]%4===0;g||b?y=m.setByOffset("global_idx",h(g?`${f.type.value}(${f.getByOffset("0")}.x)`:f.getByOffset("global_idx"),b?`${w.type.value}(${w.getByOffset("0")}.x)`:w.getByOffset("global_idx"))):y=` + let outputIndices = ${m.offsetToIndices("global_idx * 4u")}; + let offsetA = ${f.broadcastedIndicesToOffset("outputIndices",m)}; + let offsetB = ${w.broadcastedIndicesToOffset("outputIndices",m)}; + ${m.setByOffset("global_idx",h(s||_?f.getByOffset("offsetA / 4u"):`${f.type.value}(${f.getByOffset("offsetA / 4u")}[offsetA % 4u])`,s||$?w.getByOffset("offsetB / 4u"):`${w.type.value}(${w.getByOffset("offsetB / 4u")}[offsetB % 4u])`))} + `}else y=m.setByOffset("global_idx",h(f.getByOffset("global_idx"),w.getByOffset("global_idx")));else{if(!i)throw new Error("no necessary to use scalar implementation for element-wise binary op implementation.");let g=(b,_,$="")=>{let v=`aData[indexA${_}][componentA${_}]`,I=`bData[indexB${_}][componentB${_}]`;return` + let outputIndices${_} = ${m.offsetToIndices(`global_idx * 4u + ${_}u`)}; + let offsetA${_} = ${f.broadcastedIndicesToOffset(`outputIndices${_}`,m)}; + let offsetB${_} = ${w.broadcastedIndicesToOffset(`outputIndices${_}`,m)}; + let indexA${_} = offsetA${_} / 4u; + let indexB${_} = offsetB${_} / 4u; + let componentA${_} = offsetA${_} % 4u; + let componentB${_} = offsetB${_} % 4u; + ${b}[${_}] = ${$}(${p(v,I)}); + `};l===9?y=` + var data = vec4(0); + ${g("data",0,"u32")} + ${g("data",1,"u32")} + ${g("data",2,"u32")} + ${g("data",3,"u32")} + outputData[global_idx] = dot(vec4(0x1, 0x100, 0x10000, 0x1000000), vec4(data));`:y=` + ${g("outputData[global_idx]",0)} + ${g("outputData[global_idx]",1)} + ${g("outputData[global_idx]",2)} + ${g("outputData[global_idx]",3)} + `}return` + ${e.registerUniform("vec_size","u32").declareVariables(f,w,m)} + + ${c??""} + + ${e.mainStart()} + ${e.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.vec_size")} + ${y} + }`},Ad=(e,t,n,r,o,i,s=n.dataType)=>{let a=!x.areEqual(n.dims,r.dims),u=n.dims,d=x.size(n.dims),l=!1,c=!1,p=[a];if(a){let h=Ee.calcShape(n.dims,r.dims,!1);if(!h)throw new Error("Can't perform binary op on the given tensors");u=h,d=x.size(u);let m=x.size(n.dims)===1,f=x.size(r.dims)===1,w=n.dims.length>0&&n.dims[n.dims.length-1]%4===0,y=r.dims.length>0&&r.dims[r.dims.length-1]%4===0;p.push(m),p.push(f),p.push(w),p.push(y);let g=1;for(let b=1;bh.toString()).join("_"),inputDependencies:["rank","rank"]},getShaderSource:h=>Cd(h,n.dims,r.dims,u,l,a,c,o,n.dataType,r.dataType,s,i),getRunData:()=>({outputs:[{dims:u,dataType:s}],dispatchGroup:{x:Math.ceil(d/64/4)},programUniforms:[{type:12,data:Math.ceil(x.size(u)/4)},...A(n.dims,r.dims,u)]})}},Oe=(e,t,n,r,o,i)=>{e.compute(Ad(t,o??"",e.inputs[0],e.inputs[1],n,r,i))},Ei=e=>{Oe(e,"Add",(t,n)=>`${t}+${n}`)},Pi=e=>{Oe(e,"Div",(t,n)=>`${t}/${n}`)},zi=e=>{Oe(e,"Equal",{scalar:(t,n)=>`u32(${t}==${n})`,vector:(t,n)=>`vec4(${t}==${n})`},void 0,void 0,9)},Bi=e=>{Oe(e,"Mul",(t,n)=>`${t}*${n}`)},Di=e=>{let t=S("input",e.inputs[0].dataType,e.inputs[0].dims).type.value;Oe(e,"Pow",{scalar:(r,o)=>`pow_custom(${r},${o})`,vector:(r,o)=>`pow_vector_custom(${r},${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)); + } + `)},Oi=e=>{Oe(e,"Sub",(t,n)=>`${t}-${n}`)},Ri=e=>{Oe(e,"Greater",{scalar:(t,n)=>`u32(${t}>${n})`,vector:(t,n)=>`vec4(${t}>${n})`},void 0,void 0,9)},Mi=e=>{Oe(e,"Less",{scalar:(t,n)=>`u32(${t}<${n})`,vector:(t,n)=>`vec4(${t}<${n})`},void 0,void 0,9)},Ui=e=>{Oe(e,"GreaterOrEqual",{scalar:(t,n)=>`u32(${t}>=${n})`,vector:(t,n)=>`vec4(${t}>=${n})`},void 0,void 0,9)},Vi=e=>{Oe(e,"LessOrEqual",{scalar:(t,n)=>`u32(${t}<=${n})`,vector:(t,n)=>`vec4(${t}<=${n})`},void 0,void 0,9)}});var Ed,Pd,zd,Bd,Ni,Wi,Gi=k(()=>{"use strict";U();N();ae();W();Ed=(e,t)=>{if(!e||e.length<1)throw new Error("too few inputs");let n=0,r=e[n],o=r.dataType,i=r.dims.length;e.forEach((s,a)=>{if(a!==n){if(s.dataType!==o)throw new Error("input tensors should be one type");if(s.dims.length!==i)throw new Error("input tensors should have the same shape");s.dims.forEach((u,d)=>{if(d!==t&&u!==r.dims[d])throw new Error("non concat dimensions must match")})}})},Pd=(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; + }`,zd=(e,t)=>{let n=e.length,r=[];for(let o=0;o{let o=x.size(n),i=new Array(e.length),s=new Array(e.length),a=0,u=[],d=[],l=[{type:12,data:o}];for(let f=0;f`uniforms.sizeInConcatAxis${f}`).join(","),m=f=>` + + ${(()=>{f.registerUniform("outputSize","u32");for(let w=0;w(${h}); + ${p} -= sizeInConcatAxis[inputIndex - 1u]; + } + + ${zd(s,c)} + }`;return{name:"Concat",shaderCache:{hint:`${t}`,inputDependencies:u},getRunData:()=>({outputs:[{dims:n,dataType:r}],dispatchGroup:{x:Math.ceil(o/64)},programUniforms:l}),getShaderSource:m}},Ni=(e,t)=>{let n=e.inputs,r=n[0].dims,o=x.normalizeAxis(t.axis,r.length);Ed(n,o);let i=r.slice();i[o]=n.reduce((a,u)=>a+(u.dims.length>o?u.dims[o]:0),0);let s=n.filter(a=>x.size(a.dims)>0);e.compute(Bd(s,o,i,n[0].dataType),{inputs:s})},Wi=e=>V({axis:e.axis})});var $e,ve,xe,Yt,Ue=k(()=>{"use strict";U();N();$e=(e,t,n="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}(${n}(uniforms.clip_min)), ${t}(${n}(uniforms.clip_max)));`;case"HardSigmoid":return`value = max(${t}(0.0), min(${t}(1.0), ${n}(uniforms.alpha) * value + ${n}(uniforms.beta)));`;case"LeakyRelu":return`value = select(${n}(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}`)}},ve=(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})},xe=(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"})},Yt=e=>{let t=e?.activation||"";if(t==="HardSigmoid"){let[n,r]=e?.activation_params||[.2,.5];return{activation:t,alpha:n,beta:r}}else if(t==="Clip"){let[n,r]=e?.activation_params||[uo,lo];return{activation:t,clipMax:r,clipMin:n}}else if(t==="LeakyRelu"){let[n]=e?.activation_params||[.01];return{activation:t,alpha:n}}return{activation:t}}});var pe,Jt,pt=k(()=>{"use strict";pe=(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.`)}},Jt=e=>` + ${e?"value = value + getBiasByOutputCoords(coords);":""} + `});var en,Wn=k(()=>{"use strict";en=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 Dd,Od,mt,Hi,Rd,ft,Md,tn,ht=k(()=>{"use strict";U();N();W();Ue();pt();Dd=(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":""}); + `,Od=(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];"} + }`,mt=(e,t,n="f32",r,o=!1,i=32,s=!1,a=32)=>{let u=t[1]*e[1],d=t[0]*e[0],l=o?u:i,c=o?i:u,p=l/t[0],h=i/t[1];if(!((o&&p===4&&e[1]===4||!o&&(p===3||p===4))&&l%t[0]===0&&i%t[1]===0&&e[0]===4))throw new Error(`If transposeA ${o} is true, innerElementSize ${p} and workPerThread[1] ${e[1]} must be 4. + Otherwise, innerElementSize ${p} must be 3 or 4. + tileAWidth ${l} 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, ${l/p}>, ${c}>; +var mm_Bsub: array, ${d/e[0]}>, ${i}>; + +const rowPerThread = ${e[1]}; +const colPerThread = ${e[0]}; +const innerElementSize = ${p}; +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 = ${s?"0":"i32(globalId.z)"}; + ${r?`let batchIndices = ${r.offsetToIndices("u32(batch)")};`:""} + let globalRowStart = i32(workgroupId.y) * ${u}; + + let num_tiles = ${s?`${Math.ceil(a/i)}`:"(uniforms.dim_inner - 1) / tileInner + 1"}; + var kStart = ${s?`i32(globalId.z) * ${a}`:"0"}; + + var acc: array, rowPerThread>; + + // Loop over shared dimension. + let tileRowB = localRow * ${h}; + 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; + ${Dd(o,r)} + } + + // Load one tile of B into local memory. + for (var innerRow = 0; innerRow < ${h}; innerRow = innerRow + 1) { + let inputRow = tileRowB + innerRow; + let inputCol = tileCol; + mm_Bsub[inputRow][inputCol] = mm_readB(batch, kStart + inputRow, globalCol${r?", 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]; + ${p===3?"":"let BCached3 = mm_Bsub[k * innerElementSize + 3][tileCol];"} + + ${Od(o,p)} + } + + workgroupBarrier(); + } + + for (var innerRow = 0; innerRow < rowPerThread; innerRow = innerRow + 1) { + mm_write(batch, globalRow + innerRow, globalCol, acc[innerRow]); + } +}`},Hi=(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":""}); + `,Rd=e=>e?"let ACached = mm_Asub[k][tileRow + innerRow];":"let ACached = mm_Asub[tileRow + innerRow][k];",ft=(e,t,n="f32",r,o=!1,i=32,s=!1,a=32,u=!1)=>{let d=e[1]*t[1],l=e[0]*t[0],c=o?d:i,p=o?i:d;if(!(p%t[1]===0&&c%t[0]===0&&i%t[1]===0))throw new Error(`tileAHight ${p} must be divisible by workgroupSize[1]${t[1]}, tileAWidth ${c} must be divisible by workgroupSize[0]${t[0]}, tileInner ${i} must be divisible by workgroupSize[1]${t[1]}`);let h=p/t[1],m=c/t[0],f=i/t[1],w=u?` + let localRow = i32(localId.y); + let localCol = i32(localId.x); + let globalRowStart = i32(workgroupId.y) * ${d}; + let globalColStart = i32(workgroupId.x) * ${l}; + + // 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 < ${p}; inputRow = inputRow + ${t[1]}) { + for (var inputCol = localCol; inputCol < ${c}; inputCol = inputCol + ${t[0]}) { + ${Hi(o,r)} + } + } + // Load one tile of B into local memory. + for (var inputRow = localRow; inputRow < ${i}; inputRow = inputRow + ${t[1]}) { + for (var inputCol = localCol; inputCol < ${l}; inputCol = inputCol + ${t[0]}) { + mm_Bsub[inputRow][inputCol] = mm_readB(batch, + kStart + inputRow, + globalColStart + inputCol${r?", batchIndices":""}); + } + } + kStart = kStart + tileInner; + workgroupBarrier(); + + // Compute acc values for a single thread. + var BCached : array<${n}, 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) * ${d}; + +let tileRowA = i32(localId.y) * ${h}; +let tileColA = i32(localId.x) * ${m}; +let tileRowB = i32(localId.y) * ${f}; +// 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 < ${h}; innerRow = innerRow + 1) { + for (var innerCol = 0; innerCol < ${m}; innerCol = innerCol + 1) { + let inputRow = tileRowA + innerRow; + let inputCol = tileColA + innerCol; + ${Hi(o,r)} + } + } + + // Load one tile of B into local memory. + for (var innerRow = 0; innerRow < ${f}; 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${r?", batchIndices":""}); + } + } + kStart = kStart + tileInner; + workgroupBarrier(); + + // Compute acc values for a single thread. + var BCached : array<${n}, 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) { + ${Rd(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, ${p}>; + 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 = ${s?"0":"i32(globalId.z)"}; + ${r?`let batchIndices = ${r.offsetToIndices("u32(batch)")};`:""} + let num_tiles = ${s?`${Math.ceil(a/i)}`:"(uniforms.dim_inner - 1) / tileInner + 1"}; + var kStart = ${s?`i32(globalId.z) * ${a}`:"0"}; + + var acc : array, rowPerThread>; + ${w} + } +`},Md=(e,t,n,r,o,i=!1)=>{let[s,a,u]=o,[d,l,c,p]=r,h=Ye(s,u),m=Ye(a,u),f=J(r[0].type.tensor),w=()=>{let b=l.rank,_=d.rank,$=`var aIndices: ${l.type.indices};`;for(let v=b-2-1,I=_-1;v>=0;v--,I--)$+=` +aIndices[${v}] = ${_>1?`batchIndices[${I}]`:"batchIndices"};`;return h.forEach(v=>{$+=` +aIndices[${v}] = 0;`}),$+=` +aIndices[${b-2}] = u32(row); + aIndices[${b-1}] = u32(colIn);`,$},y=()=>{let b=c.rank,_=d.rank,$=`var bIndices: ${c.type.indices};`;for(let v=b-2-1,I=_-1;v>=0;v--,I--)$+=` +bIndices[${v}] = ${_>1?`batchIndices[${I}]`:"batchIndices"};`;return m.forEach(v=>{$+=` +bIndices[${v}] = 0;`}),$+=` +bIndices[${b-2}] = u32(row); + bIndices[${b-1}] = u32(colIn);`,$};return` + fn mm_readA(batch: i32, row: i32, colIn: i32, batchIndices: ${d.type.indices}) -> ${pe(e,f)} { + var value = ${pe(e,f)}(0.0); + let col = colIn * ${e}; + if(row < uniforms.dim_a_outer && col < uniforms.dim_inner) + { + ${w()} + value = ${l.getByIndices("aIndices")}; + } + return value; + } + + fn mm_readB(batch: i32, row: i32, colIn: i32, batchIndices: ${d.type.indices}) -> ${pe(e,f)} { + var value = ${pe(e,f)}(0.0); + let col = colIn * ${e}; + if(row < uniforms.dim_inner && col < uniforms.dim_b_outer) + { + ${y()} + value = ${c.getByIndices("bIndices")}; + } + return value; + } + + fn mm_write(batch: i32, row: i32, colIn: i32, valueIn: ${pe(e,f)}) { + 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]":`${pe(e,f)}(bias[row])`};`:""} + ${n} + ${p.setByIndices("vec3(coords)","value")} + } + } + `},tn=(e,t,n,r,o=!1,i)=>{let s=e[0].dims,a=e[1].dims,u=s.slice(0,-2),d=a.slice(0,-2),l=r?r.slice(0,-2):n.slice(0,-2),c=x.size(l),p=s[s.length-2],h=s[s.length-1],m=a[a.length-1],f=h%4===0&&m%4===0,w=p<=8?[4,1,1]:[4,4,1],y=[8,8,1],g=[Math.ceil(m/y[0]/w[0]),Math.ceil(p/y[1]/w[1]),Math.ceil(c/y[2]/w[2])],b=f?4:1,_=[...u,p,h/b],$=_.length,v=[...d,h,m/b],I=v.length,T=[c,p,m/b],P=[{type:6,data:p},{type:6,data:m},{type:6,data:h}];ve(t,P),P.push(...A(l,_,v));let z=["rank","rank"],R=e.length>2;R&&(P.push(...A(e[2].dims)),z.push("rank")),P.push(...A(T));let Z=G=>{let K=l.length,ie=Ft("batchDims",e[0].dataType,K,1),M=J(e[0].dataType),Q=S("a",e[0].dataType,$,b),re=S("b",e[1].dataType,I,b),O=C("result",e[0].dataType,T.length,b),j=[Q,re];if(R){let D=o?b:1;j.push(S("bias",e[2].dataType,e[2].dims.length,D))}let H=[{name:"dim_a_outer",type:"i32"},{name:"dim_b_outer",type:"i32"},{name:"dim_inner",type:"i32"}];xe(t,H);let q=J(O.type.tensor),F=$e(t,O.type.value,q),E=Md(b,R,F,[ie,Q,re,O],[u,d,l],o);return` + ${G.registerUniforms(H).registerInternalVariables(ie).declareVariables(...j,O)} + ${E} + ${f?mt(w,y,M,ie):ft(w,y,M,ie)} + `};return{name:"MatMul",shaderCache:{hint:`${w};${t.activation};${f};${o}`,inputDependencies:z},getRunData:()=>({outputs:[{dims:i?i(n):n,dataType:e[0].dataType}],dispatchGroup:{x:g[0],y:g[1],z:g[2]},programUniforms:P}),getShaderSource:Z}}});var Ud,qi,Fi=k(()=>{"use strict";U();Ae();W();Ue();pt();Wn();ht();Ud=(e,t,n,r,o=!1,i,s=4,a=4,u=4,d="f32")=>{let l=z=>{switch(z){case 1:return"resData = x[xIndex];";case 3:return`resData = vec3<${d}>(x[xIndex], x[xIndex + 1], x[xIndex + 2]);`;case 4:return"resData = x[xIndex / 4];";default:throw new Error(`innerElementSize ${z} is not supported.`)}},c=z=>{switch(z){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 ${z} is not supported.`)}},p=e?` + let coord = vec4(batch, xRow, xCol, xCh); + `:` + let coord = vec4(batch, xCh, xRow, xCol); + `,h=e?` + let coords = vec4( + batch, + row / outWidth, + row % outWidth, + col); + `:` + let coords = vec4( + batch, + row, + col / outWidth, + col % outWidth); + `,m=e?"i32(uniforms.x_shape[1])":"i32(uniforms.x_shape[2])",f=e?"i32(uniforms.x_shape[2])":"i32(uniforms.x_shape[3])",w=e?"row":"col",y=e?"col":"row",g=` + let inChannels = i32(uniforms.w_shape[2]); + let outWidth = ${e?"i32(uniforms.result_shape[2])":"i32(uniforms.result_shape[3])"}; + let outRow = ${w} / outWidth; + let outCol = ${w} % outWidth; + + let WRow = ${y} / (i32(uniforms.w_shape[1]) * inChannels); + let WCol = ${y} / 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 = ${y} % inChannels; + var resData = ${pe(s,d)}(0.0); + // The bounds checking is always needed since we use it to pad zero for + // the 'same' padding type. + if (xRow >= 0 && xRow < ${m} && xCol >= 0 && xCol < ${f}) { + ${p} + let xIndex = getIndexFromCoords4D(coord, vec4(uniforms.x_shape)); + ${l(s)} + } + return resData;`,b=e?t&&r?` + let col = colIn * ${s}; + ${g}`:` + let col = colIn * ${s}; + if (row < uniforms.dim_a_outer && col < uniforms.dim_inner) { + ${g} + } + return ${pe(s,d)}(0.0);`:r&&n?` + let col = colIn * ${s}; + ${g}`:` + let col = colIn * ${s}; + if (row < uniforms.dim_inner && col < uniforms.dim_b_outer) { + ${g} + } + return ${pe(s,d)}(0.0);`,_=`${c(a)}`,$=pe(u,d),v=e?pe(s,d):pe(a,d),I=e?pe(a,d):pe(s,d),T=$e(i,$,d);return` + fn mm_readA(batch: i32, row : i32, colIn : i32) -> ${v} { + ${e?b:_} + } + + fn mm_readB(batch: i32, row : i32, colIn : i32) -> ${I} { + ${e?_:b} + } + + fn mm_write(batch: i32, row : i32, colIn : i32, valueIn : ${$}) { + let col = colIn * ${u}; + 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])"}; + ${h} + ${Jt(o)} + ${T} + setOutputAtCoords(coords[0], coords[1], coords[2], coords[3], value); + } + }`},qi=(e,t,n,r,o,i,s,a,u)=>{let d=t.format==="NHWC",l=d?e[0].dims[3]:e[0].dims[1],c=n[0],p=d?n[2]:n[3],h=d?n[1]:n[2],m=d?n[3]:n[1],f=d&&(l%4===0||l%3===0)&&m%4===0,w=d?m:p*h,y=d?p*h:m,g=[8,8,1],b=r<=8?[4,1,1]:[4,4,1],_=[Math.ceil(w/g[0]/b[0]),Math.ceil(y/g[1]/b[1]),Math.ceil(c/g[2]/b[2])];X("verbose",()=>`[conv2d_mm_webgpu] dispatch = ${_}`);let $=f?d&&l%4!==0?3:4:1,v=g[1]*b[1],I=g[0]*b[0],T=Math.max(g[0]*$,g[1]),P=r%v===0,z=o%I===0,R=i%T===0,Z=f?[$,4,4]:[1,1,1],G=[{type:6,data:r},{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}];ve(t,G),G.push(...A(e[0].dims,e[1].dims));let K=["rank","rank"];s&&(G.push(...A(e[2].dims)),K.push("rank")),G.push(...A(n));let ie=M=>{let Q=[{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}];xe(t,Q);let re=f?4:1,O=J(e[0].dataType),j=` + fn setOutputAtIndex(flatIndex : i32, value : ${f?`vec4<${O}>`:O}) { + result[flatIndex] = ${f?`vec4<${O}>`:O}(value); + } + fn setOutputAtCoords(d0 : i32, d1 : i32, d2 : i32, d3 : i32, value : ${f?`vec4<${O}>`:O}) { + let flatIndex = getOutputIndexFromCoords(vec4(d0, d1, d2, d3)); + setOutputAtIndex(flatIndex ${f?"/ 4":""}, value); + }`,H=S("x",e[0].dataType,e[0].dims.length,$===3?1:$),q=S("w",e[1].dataType,e[1].dims.length,re),F=[H,q],E=C("result",e[0].dataType,n.length,re);if(s){let D=S("bias",e[2].dataType,e[2].dims.length,re);F.push(D),j+=` + fn getBiasByOutputCoords(coords : vec4) -> ${f?`vec4<${O}>`:O} { + return bias[coords.${d?"w":"y"}${f?"/ 4":""}]; + }`}return` + ${en("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 }; + ${M.registerUniforms(Q).declareVariables(...F,E)} + ${j} + ${Ud(d,P,z,R,s,t,Z[0],Z[1],Z[2],O)} + ${f?mt(b,g,O,void 0,!d,T):ft(b,g,O,void 0,!d,T,!1,void 0,a)}`};return{name:"Conv2DMatMul",shaderCache:{hint:`${t.cacheKey};${$};${f};${P};${z};${R};${v};${I};${T}`,inputDependencies:K},getRunData:()=>({outputs:[{dims:u?u(n):n,dataType:e[0].dataType}],dispatchGroup:{x:_[0],y:_[1],z:_[2]},programUniforms:G}),getShaderSource:ie}}});var Vd,Ki,nn,Ld,ji,Nd,Xi,Zi,Qi=k(()=>{"use strict";U();Ae();N();W();Ue();pt();Vd=e=>{let t=1;for(let n=0;ntypeof e=="number"?[e,e,e]:e,nn=(e,t)=>t<=1?e:e+(e-1)*(t-1),Ld=(e,t,n,r=1)=>{let o=nn(t,r);return Math.floor((e[0]*(n-1)-n+o)/2)},ji=(e,t,n,r,o)=>{o==null&&(o=Ld(e,t[0],r[0]));let i=[0,0,0,n];for(let s=0;s<3;s++)e[s]+2*o>=t[s]&&(i[s]=Math.trunc((e[s]-t[s]+2*o)/r[s]+1));return i},Nd=(e,t,n,r,o,i,s,a,u,d)=>{let l,c,p,h;if(e==="VALID"&&(e=0),typeof e=="number"){l={top:e,bottom:e,left:e,right:e,front:e,back:e};let m=ji([t,n,r,1],[a,u,d],1,[o,i,s],e);c=m[0],p=m[1],h=m[2]}else if(Array.isArray(e)){if(!e.every((f,w,y)=>f===y[0]))throw Error(`Unsupported padding parameter: ${e}`);l={top:e[0],bottom:e[1],left:e[2],right:e[3],front:e[4],back:e[5]};let m=ji([t,n,r,1],[a,u,d],1,[o,i,s],e[0]);c=m[0],p=m[1],h=m[2]}else if(e==="SAME_UPPER"){c=Math.ceil(t/o),p=Math.ceil(n/i),h=Math.ceil(r/s);let m=(c-1)*o+a-t,f=(p-1)*i+u-n,w=(h-1)*s+d-r,y=Math.floor(m/2),g=m-y,b=Math.floor(f/2),_=f-b,$=Math.floor(w/2),v=w-$;l={top:b,bottom:_,left:$,right:v,front:y,back:g}}else throw Error(`Unknown padding parameter: ${e}`);return{padInfo:l,outDepth:c,outHeight:p,outWidth:h}},Xi=(e,t,n,r,o,i=!1,s="channelsLast")=>{let a,u,d,l,c;if(s==="channelsLast")[a,u,d,l,c]=e;else if(s==="channelsFirst")[a,c,u,d,l]=e;else throw new Error(`Unknown dataFormat ${s}`);let[p,,h,m,f]=t,[w,y,g]=Ki(n),[b,_,$]=Ki(r),v=nn(h,b),I=nn(m,_),T=nn(f,$),{padInfo:P,outDepth:z,outHeight:R,outWidth:Z}=Nd(o,u,d,l,w,y,g,v,I,T),G=i?p*c:p,K=[0,0,0,0,0];return s==="channelsFirst"?K=[a,G,z,R,Z]:s==="channelsLast"&&(K=[a,z,R,Z,G]),{batchSize:a,dataFormat:s,inDepth:u,inHeight:d,inWidth:l,inChannels:c,outDepth:z,outHeight:R,outWidth:Z,outChannels:G,padInfo:P,strideDepth:w,strideHeight:y,strideWidth:g,filterDepth:h,filterHeight:m,filterWidth:f,effectiveFilterDepth:v,effectiveFilterHeight:I,effectiveFilterWidth:T,dilationDepth:b,dilationHeight:_,dilationWidth:$,inShape:e,outShape:K,filterShape:t}},Zi=(e,t,n,r,o,i)=>{let s=i==="channelsLast",a=s?e[0].dims[3]:e[0].dims[1],u=!1,d=[64,1,1],l={x:n.map((g,b)=>b)},c=[Math.ceil(Vd(l.x.map(g=>n[g]))/d[0]),1,1];X("verbose",()=>`[conv3d_naive_webgpu] dispatch = ${c}`);let p=u?s&&a%4!==0?3:4:1,h=x.size(n),m=[{type:12,data:h},{type:12,data:r},{type:12,data:o},{type:12,data:t.strides},{type:12,data:t.dilations}];ve(t,m),m.push(...A(e[0].dims,e[1].dims));let f=["rank","rank"],w=e.length===3;w&&(m.push(...A(e[2].dims)),f.push("rank")),m.push(...A(n));let y=g=>{let b=[{name:"output_size",type:"u32"},{name:"filter_dims",type:"u32",length:r.length},{name:"pads",type:"u32",length:o.length},{name:"strides",type:"u32",length:t.strides.length},{name:"dilations",type:"u32",length:t.dilations.length}];xe(t,b);let _=u?4:1,$=J(e[0].dataType),v=S("x",e[0].dataType,e[0].dims.length,p===3?1:p),I=S("W",e[1].dataType,e[1].dims.length,_),T=[v,I],P=C("result",e[0].dataType,n.length,_),z="";if(w){let G=S("bias",e[2].dataType,e[2].dims.length,_);T.push(G),z+=` + fn getBiasByOutputCoords(coords : array) -> ${u?`vec4<${$}>`:$} { + return bias[${s?B("coords",4,5):B("coords",1,5)}${u?"/ 4":""}]; + }`}let R=pe(p,$),Z=$e(t,R,$);return` + ${z} + fn getX(d0 : u32, d1 : u32, d2 : u32, d3 : u32, d4 : u32) -> f32 { + let aIndices = array(d0, d1, d2, d3, d4); + return ${v.getByIndices("aIndices")}; + } + fn getW(d0 : u32, d1 : u32, d2 : u32, d3 : u32, d4 : u32) -> f32 { + let aIndices = array(d0, d1, d2, d3, d4); + return ${I.getByIndices("aIndices")}; + } + ${g.registerUniforms(b).declareVariables(...T,P)} + ${g.mainStart()} + ${g.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} + let coords = ${P.offsetToIndices("global_idx")}; + let batch = ${B("coords",0,v.rank)}; + let d2 = ${s?B("coords",v.rank-1,v.rank):B("coords",1,v.rank)}; + let xFRCCorner = vec3(${s?B("coords",1,v.rank):B("coords",2,v.rank)}, + ${s?B("coords",2,v.rank):B("coords",3,v.rank)}, + ${s?B("coords",3,v.rank):B("coords",4,v.rank)}) * uniforms.strides - uniforms.pads; + let xFCorner = xFRCCorner.x; + let xRCorner = xFRCCorner.y; + let xCCorner = xFRCCorner.z; + let xShapeY = ${s?B("uniforms.x_shape",1,v.rank):B("uniforms.x_shape",2,v.rank)}; + let xShapeZ = ${s?B("uniforms.x_shape",2,v.rank):B("uniforms.x_shape",3,v.rank)}; + let xShapeW = ${s?B("uniforms.x_shape",3,v.rank):B("uniforms.x_shape",4,v.rank)}; + let xShapeU = ${s?B("uniforms.x_shape",4,v.rank):B("uniforms.x_shape",1,v.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) { + ${s?`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) { + ${s?`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) { + ${s?`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) { + ${s?`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); + } + } + } + } + ${w?"value = value + getBiasByOutputCoords(coords)":""}; + ${Z} + result[global_idx] = f32(value); + }`};return{name:"Conv3DNaive",shaderCache:{hint:`${t.cacheKey};${s};${p};${w}`,inputDependencies:f},getRunData:()=>({outputs:[{dims:n,dataType:e[0].dataType}],dispatchGroup:{x:c[0],y:c[1],z:c[2]},programUniforms:m}),getShaderSource:y}}});var Yi,Ji,es=k(()=>{"use strict";U();N();W();Ue();Yi=(e,t,n,r)=>{let o=e.length>2,i=o?"value += b[output_channel];":"",s=e[0].dims,a=e[1].dims,u=t.format==="NHWC",d=u?n[3]:n[1],l=d/t.group,c=u&&l>=4?te(d):1,p=x.size(n)/c,h=[{type:12,data:p},{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:l}];ve(t,h),h.push(...A(s,[a[0],a[1],a[2],a[3]/c]));let m=o?["rank","rank","rank"]:["rank","rank"];h.push(...A([n[0],n[1],n[2],n[3]/c]));let f=w=>{let y=C("output",e[0].dataType,n.length,c),g=J(y.type.tensor),b=$e(t,y.type.value,g),_=S("x",e[0].dataType,s.length),$=S("w",e[1].dataType,a.length,c),v=[_,$];o&&v.push(S("b",e[2].dataType,e[2].dims,c));let I=[{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"}];xe(t,I);let T=u?` + 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 = ${_.get("batch","xHeight","xWidth","input_channel")}; + let wVal = ${$.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 = ${_.get("batch","input_channel","xHeight","xWidth")}; + let wVal = ${$.get("output_channel","wInChannel","wHeight","wWidth")}; + value += xVal * wVal; + } + } + } + `;return` + ${w.registerUniforms(I).declareVariables(...v,y)} + + ${w.mainStart()} + ${w.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} + + let outputIndices = ${y.offsetToIndices("global_idx")}; + let batch: u32 = outputIndices[0]; + let output_channel: u32 = outputIndices[${u?3:1}]; + let xRCCorner: vec2 = vec2(outputIndices[${u?1:2}], outputIndices[${u?2:3}]) * uniforms.strides - uniforms.pads; + let group_id: u32 = output_channel * ${c} / uniforms.output_channels_per_group; + var in_channel_offset = group_id * uniforms.w_shape[${u?2:1}]; + + var value: ${y.type.value} = ${y.type.value}(0); + ${T} + ${i} + ${b} + ${y.setByOffset("global_idx","value")} + }`};return{name:"GroupedConv",shaderCache:{hint:`${t.cacheKey}_${c}`,inputDependencies:m},getRunData:()=>({outputs:[{dims:r?r(n):n,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(p/64)},programUniforms:h}),getShaderSource:f}},Ji=(e,t,n,r)=>{let o=e.length>2,i=te(n[3]),s=te(n[2]),a=x.size(n)/i/s,u=[e[0].dims[0],e[0].dims[1],e[0].dims[2],e[0].dims[3]/i],d=[e[1].dims[0],e[1].dims[1],e[1].dims[2],e[1].dims[3]/i],l=[n[0],n[1],n[2],n[3]/i],c=[{type:12,data:a},{type:6,data:[t.strides[0],t.strides[1]]},{type:6,data:[t.pads[0],t.pads[1]]}];ve(t,c),c.push(...A(u,d,l));let p=(s-1)*t.strides[1]+d[1],h=m=>{let f=C("output",e[0].dataType,l.length,i),w=J(f.type.tensor),y=$e(t,f.type.value,w),g=S("x",e[0].dataType,u.length,i),b=S("w",e[1].dataType,d.length,i),_=[g,b];o&&_.push(S("b",e[2].dataType,e[2].dims,i));let $=o?"value += b[output_channel];":"",v=[{name:"output_size",type:"u32"},{name:"strides",type:"i32",length:2},{name:"pads",type:"i32",length:2}];return xe(t,v),` + ${m.registerUniforms(v).declareVariables(..._,f)} + ${m.mainStart()} + ${m.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] / ${s}u; + let col = (index1 % width1) * ${s}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<${g.type.value}, ${p}>; + var values: array<${f.type.value}, ${s}>; + 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 < ${d[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 < ${p}; i++) { + let x_width = x_corner.y + i; + if (x_width >= 0 && u32(x_width) < uniforms.x_shape[2]) { + x_vals[i] = ${g.get("batch","u32(x_height)","u32(x_width)","input_channel")}; + } else { + x_vals[i] = ${g.type.value}(0); + } + } + for (var w_width: u32 = 0u; w_width < ${d[1]}; w_width++) { + let w_val = ${b.get("w_height","w_width","0","output_channel")}; + for (var i = 0u; i < ${s}u; i++) { + values[i] = fma(x_vals[i * u32(uniforms.strides[1]) + w_width], w_val, values[i]); + } + } + } + } + + for (var i = 0u; i < ${s}u; i++) { + var value = values[i]; + ${$} + ${y} + ${f.set("batch","row","col + i","output_channel","value")}; + } + }`};return{name:"GroupedConv-Vectorize",shaderCache:{hint:`${t.cacheKey};${i};${s};${p};${d[0]};${d[1]}`,inputDependencies:o?["rank","rank","type"]:["rank","rank"]},getRunData:()=>({outputs:[{dims:r?r(n):n,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(a/64)},programUniforms:c}),getShaderSource:h}}});var Gn,Wd,ts,Hn=k(()=>{"use strict";U();N();ht();W();Ue();Gn=(e,t,n,r,o=!1,i)=>{let s=e[0].dims,a=e[1].dims,u=s[s.length-2],d=a[a.length-1],l=s[s.length-1],c=te(d),p=te(l),h=te(u),m=x.size(n)/c/h,f=e.length>2,w=r?r.slice(0,-2):n.slice(0,-2),g=[x.size(w),u,d],b=[{type:12,data:m},{type:12,data:u},{type:12,data:d},{type:12,data:l}];ve(t,b),b.push(...A(w,s,a)),f&&b.push(...A(e[2].dims)),b.push(...A(g));let _=$=>{let v=Ft("batch_dims",e[0].dataType,w.length),I=S("a",e[0].dataType,s.length,p),T=S("b",e[1].dataType,a.length,c),P=C("output",e[0].dataType,g.length,c),z=J(P.type.tensor),R=$e(t,P.type.value,z),Z=[I,T],G="";if(f){let H=o?c:1;Z.push(S("bias",e[2].dataType,e[2].dims.length,H)),G=`${o?`value += bias[col / ${H}];`:`value += ${P.type.value}(bias[row + i]);`}`}let K=s.slice(0,-2),ie=a.slice(0,-2),M=Ye(K,w),Q=Ye(ie,w),re=[{name:"output_size",type:"u32"},{name:"M",type:"u32"},{name:"N",type:"u32"},{name:"K",type:"u32"}];xe(t,re);let O=(H,q)=>{let F=H.rank,E=H.name;if(F===2)return`var ${E}_indices = ${H.type.indices}(0u, 0u);`;let D=v.rank,ne=`var ${E}_indices: ${H.type.indices};`;for(let Ce=F-2-1,fe=D-1;Ce>=0;Ce--,fe--)ne+=` +${E}_indices[${Ce}] = ${D>1?`batch_indices[${fe}]`:"batch_indices"};`;return q.forEach(Ce=>{ne+=` +${E}_indices[${Ce}] = 0;`}),ne+=`${E}_indices[${F-2}] = 0u; + ${E}_indices[${F-1}] = 0u;`,ne},j=()=>{let H=`var a_data: ${I.type.value};`;for(let q=0;q; + for (var k: u32 = 0u; k < uniforms.K; k = k + ${p}) { + ${j()} + } + for (var i = 0u; i < ${h}u; i++) { + var value = values[i]; + ${G} + ${R} + let cur_indices = ${P.type.indices}(batch, row + i, col); + let offset = ${P.indicesToOffset("cur_indices")}; + ${P.setByOffset(`offset / ${c}`,"value")}; + } + } + `};return{name:"MatMulNaive",shaderCache:{hint:`${t.activation};${c};${p};${h};${o}`,inputDependencies:f?["rank","rank","rank"]:["rank","rank"]},getRunData:()=>({outputs:[{dims:i?i(n):n,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(m/64)},programUniforms:b}),getShaderSource:_}},Wd=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.")},ts=e=>{Wd(e.inputs);let t=Ee.calcShape(e.inputs[0].dims,e.inputs[1].dims,!0);if(!t)throw new Error("Can't use matmul on the given tensors");let n=t[t.length-1],r=e.inputs[0].dims[e.inputs[0].dims.length-1];n<8&&r<8?e.compute(Gn(e.inputs,{activation:""},t)):e.compute(tn(e.inputs,{activation:""},t))}});var Gd,qn,Hd,Fn,Kn,ns,qd,Fd,jn,rs=k(()=>{"use strict";N();Fi();Qi();ht();es();Ue();Hn();Me();Gd=(e,t,n,r,o,i)=>{let s=e[0],a=e.slice(i?1:2,i?3:4),u=a.length,d=t[0],c=t.slice(2).map((m,f)=>m+(m-1)*(n[f]-1)),h=a.map((m,f)=>m+r[f]+r[f+u]).map((m,f)=>Math.floor((m-c[f]+o[f])/o[f]));return h.splice(0,0,s),h.splice(i?3:1,0,d),h},qn=[2,3,1,0],Hd=(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 n=e[0].dims[t.format==="NHWC"?e[0].dims.length-1:1],r=e[1].dims[1]*t.group;if(n!==r)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")},Fn=(e,t)=>{let n=e.kernelShape.slice();n.length{let t=Yt(e),n=e.format,r=["NOTSET","VALID","SAME_UPPER","SAME_LOWER"][e.auto_pad],o=e.dilations,i=e.group,s=e.kernel_shape,a=e.pads,u=e.strides,d=e.w_is_const();return{autoPad:r,format:n,dilations:o,group:i,kernelShape:s,pads:a,strides:u,wIsConst:d,...t,cacheKey:`${e.format};${t.activation};`}},ns=(e,t,n,r)=>{let o=n.format==="NHWC",i=Gd(t[0].dims,t[1].dims,n.dilations,n.pads,n.strides,o);if(n.group!==1){let v=[t[0]];if(o){let T=e.kernelCustomData.wT??e.compute(le(t[1],qn),{inputs:[1],outputs:[n.wIsConst?-2:-1]})[0];n.wIsConst&&!e.kernelCustomData.wT&&(e.kernelCustomData.wT=T),v.push(T)}else v.push(t[1]);t.length===3&&v.push(t[2]),!e.adapterInfo.isArchitecture("ampere")&&o&&t[1].dims[0]===n.group&&t[1].dims[1]===1&&n.dilations[0]===1&&n.dilations[1]===1?e.compute(Ji(v,n,i,r),{inputs:v}):e.compute(Yi(v,n,i,r),{inputs:v});return}let s=t.length===3,a=t[0].dims[o?1:2],u=t[0].dims[o?2:3],d=t[0].dims[o?3:1],l=t[1].dims[2],c=t[1].dims[3],p=i[o?1:2],h=i[o?2:3],m=i[o?3:1],f=o&&l===a&&c===u&&n.pads[0]===0&&n.pads[1]===0;if(f||l===1&&c===1&&n.dilations[0]===1&&n.dilations[1]===1&&n.strides[0]===1&&n.strides[1]===1&&n.pads[0]===0&&n.pads[1]===0){let v=i[0],I,T,P,z=[];if(o){let G=e.kernelCustomData.wT??e.compute(le(t[1],qn),{inputs:[1],outputs:[n.wIsConst?-2:-1]})[0];if(n.wIsConst&&!e.kernelCustomData.wT&&(e.kernelCustomData.wT=G),f){let K=a*u*d;I=t[0].reshape([1,v,K]),T=G.reshape([1,K,m]),P=[1,v,m]}else I=t[0].reshape([v,a*u,d]),T=G.reshape([1,d,m]),P=[v,p*h,m];z.push(I),z.push(T)}else I=t[0].reshape([v,d,a*u]),T=t[1].reshape([1,m,d]),P=[v,m,p*h],z.push(T),z.push(I);s&&z.push(t[2]);let R=P[2],Z=z[0].dims[z[0].dims.length-1];R<8&&Z<8?e.compute(Gn(z,n,i,P,o,r),{inputs:z}):e.compute(tn(z,n,i,P,o,r),{inputs:z});return}let w=!0,y=e.kernelCustomData.wT??e.compute(le(t[1],qn),{inputs:[1],outputs:[n.wIsConst?-2:-1]})[0];n.wIsConst&&!e.kernelCustomData.wT&&(e.kernelCustomData.wT=y);let g=[t[0],y];s&&g.push(t[2]);let b=o?p*h:m,_=o?m:p*h,$=l*c*d;e.compute(qi(g,n,i,b,_,$,s,w,r),{inputs:g})},qd=(e,t)=>{let n=t.format==="NHWC",r=[e.inputs[0].reshape(n?[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&&r.push(e.inputs[2]);let o=[0,t.pads[0],0,t.pads[1]],i=[1].concat(t.strides),s=[1].concat(t.dilations),a=[1].concat(t.kernelShape),u=Fn({...t,pads:o,strides:i,dilations:s,kernelShape:a},r);ns(e,r,u,d=>n?[d[0],d[2],d[3]]:[d[0],d[1],d[3]])},Fd=(e,t,n)=>{let r=n.format==="NHWC"?"channelsLast":"channelsFirst",o=Fn(n,t),i=n.autoPad==="NOTSET"?n.pads:n.autoPad,s=Xi(t[0].dims,t[1].dims,n.strides,n.dilations,i,!1,r);e.compute(Zi(t,o,s.outShape,[s.filterDepth,s.filterHeight,s.filterWidth],[s.padInfo.front,s.padInfo.top,s.padInfo.left],r))},jn=(e,t)=>{if(Hd(e.inputs,t),e.inputs[0].dims.length===3)qd(e,t);else if(e.inputs[0].dims.length===5)Fd(e,e.inputs,t);else{let n=Fn(t,e.inputs);ns(e,e.inputs,n)}}});var Kd,os,is=k(()=>{"use strict";U();Ae();W();Ue();pt();Wn();ht();Kd=(e,t=!1,n,r,o=4)=>{let i=y=>{switch(y){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 ${r}(v0, v1, v2, v3); + `;default:throw new Error(`innerElementSize ${y} is not supported.`)}},s=e?` + let coord = vec4(batch, iXR, iXC, xCh); + `:` + let coord = vec4(batch, xCh, iXR, iXC); + `,a=e?` + let coords = vec4( + batch, + row / outWidth, + row % outWidth, + col); + `:` + let coords = vec4( + batch, + row, + col / outWidth, + col % outWidth); + `,u=e?"i32(uniforms.x_shape[1])":"i32(uniforms.x_shape[2])",d=e?"i32(uniforms.x_shape[2])":"i32(uniforms.x_shape[3])",l=e?"row":"col",c=e?"col":"row",p=` + 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 = ${l} / outWidth; + let outCol = ${l} % outWidth; + + let WRow = ${c} / (uniforms.filter_dims[1] * inChannels); + let WCol = ${c} / 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(${u}) || fract(xR) > 0.0) { + return ${r}(0.0); + } + if (xC < 0.0 || xC >= f32(${d}) || fract(xC) > 0.0) { + return ${r}(0.0); + } + let iXR = i32(xR); + let iXC = i32(xC); + let xCh = ${c} % inChannels; + ${s} + return x[getIndexFromCoords4D(coord, vec4(uniforms.x_shape))/${o}];`,h=e?` + let col = colIn * ${o}; + if (row < uniforms.dim_a_outer && col < uniforms.dim_inner) { + ${p} + } + return ${r}(0.0);`:` + let col = colIn * ${o}; + if (row < uniforms.dim_inner && col < uniforms.dim_b_outer) { + ${p} + } + return ${r}(0.0);`,m=` + 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 ${r}(0.0); + `,f=$e(n,r);return` + fn mm_readA(batch: i32, row : i32, colIn : i32) -> ${r} { + ${e?h:m} + } + + fn mm_readB(batch: i32, row : i32, colIn : i32) -> ${r} { + ${e?m:h} + } + + fn mm_write(batch: i32, row : i32, colIn : i32, valueInput : ${r}) { + 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])"}; + ${a} + ${Jt(t)} + ${f} + result[getIndexFromCoords4D(coords, vec4(uniforms.result_shape))/${o}] = value; + } + }`},os=(e,t,n,r,o,i,s,a)=>{let u=t.format==="NHWC",d=u?e[0].dims[3]:e[0].dims[1],l=n[0],c=u?n[2]:n[3],p=u?n[1]:n[2],h=u?n[3]:n[1],m=u&&d%4===0&&d%3&&h%4===0,f=u?h:c*p,w=u?c*p:h,y=[8,8,1],g=r<=8?[4,1,1]:[4,4,1],b=[Math.ceil(f/y[0]/g[0]),Math.ceil(w/y[1]/g[1]),Math.ceil(l/y[2]/g[2])];X("verbose",()=>`[conv_backprop_mm_webgpu] dispatch = ${b}`);let _=m?4:1,$=Math.max(y[0]*_,y[1]),v=m?4:1,I=[t.kernelShape[u?1:2],t.kernelShape[u?2:3]],T=[I[0]+(t.dilations[0]<=1?0:(I[0]-1)*(t.dilations[0]-1)),I[1]+(t.dilations[1]<=1?0:(I[1]-1)*(t.dilations[1]-1))],P=[T[0]-1-Math.floor((t.pads[0]+t.pads[2])/2),T[1]-1-Math.floor((t.pads[1]+t.pads[3])/2)],z=[{type:6,data:r},{type:6,data:o},{type:6,data:i},{type:6,data:t.strides},{type:6,data:t.dilations},{type:6,data:I},{type:6,data:P}];ve(t,z),z.push(...A(e[0].dims,e[1].dims));let R=["rank","rank"];s&&(z.push(...A(e[2].dims)),R.push("rank")),z.push(...A(n));let Z=G=>{let K=S("x",e[0].dataType,e[0].dims.length,v),ie=S("w",e[1].dataType,e[1].dims.length,1),M=C("result",e[0].dataType,n.length,v),Q=[K,ie],re="";if(s){let H=S("bias",e[2].dataType,e[2].dims.length,v);Q.push(H),re+=` + fn getBiasByOutputCoords(coords : vec4) -> ${H.type.value} { + return bias[coords.${u?"w":"y"}${m?"/ 4":""}]; + }`}let O=[{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:I.length},{name:"pads",type:"i32",length:P.length}];xe(t,O);let j=J(e[0].dataType,1);if(j!=="f16"&&j!=="f32")throw new Error(`elemType ${j} is not supported.`);return` + ${en("uniforms.result_strides")} + ${G.registerUniforms(O).declareVariables(...Q,M)}; + ${re} + ${Kd(u,s,t,K.type.value,_)} + ${m?mt(g,y,j,void 0,!u,$):ft(g,y,j,void 0,!u,$,!1,void 0,a)}`};return{name:"Conv2DTransposeMatMul",shaderCache:{hint:`${t.cacheKey};${g};${y};${m}`,inputDependencies:R},getRunData:()=>({outputs:[{dims:n,dataType:e[0].dataType}],dispatchGroup:{x:b[0],y:b[1],z:b[2]},programUniforms:z}),getShaderSource:Z}}});var jd,Xn,ss=k(()=>{"use strict";U();Ae();N();W();jd=(e,t,n,r,o,i=!1,s,a,u=!1)=>{let d=u?1:2,l=u?2:3,c=u?3:1,p=i?2:1,h=` + fn setOutputAtIndex(flatIndex : u32, value : ${i?`vec4<${s}>`:s}) { + result[flatIndex] = ${i?`vec4<${s}>`:s}(value); + }`;r&&(h+=` + fn getBiasByOutputCoords(coords : vec4) -> ${i?`vec4<${s}>`:s} { + return bias[coords.${u?"w":"y"}${i?"/ 4":""}]; + }`);let m=i?4:1,f=S("W",t[1].dataType,t[1].dims.length,m),w=S("Dy",t[0].dataType,t[0].dims.length,m),y=[w,f];r&&y.push(S("bias",t[2].dataType,[n[c]].length,m));let g=C("result",t[0].dataType,n.length,m),b=`{ + 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"} * ${p}; + 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, ${p}>; + for (var i = 0; i < ${p}; i++) { + dotProd[i] = vec4<${s}>(0.0); + } + for (var wR: u32 = 0; wR < uniforms.filter_dims[0]; wR = wR + 1) { + var dyR = (${s}(dyCorner.x) + ${s}(wR)) / ${s}(uniforms.strides.x); + let wRPerm = uniforms.filter_dims[0] - 1 - wR; + if (dyR < 0.0 || dyR >= ${s}(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 = (${s}(dyCorner.y) + ${s}(wC)) / ${s}(uniforms.strides.y); + let dyC2 = (${s}(dyCorner.y) + 1.0 + ${s}(wC)) / ${s}(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 >= ${s}(uniforms.Dy_shape[2]) || + fract(dyC) > 0.0) { + bDyCVal = false; + } + if (dyC2 < 0.0 || dyC2 >= ${s}(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 = ${f.get("u32(wRPerm)","u32(wCPerm)","d1","d2")}; + let wValue1 = ${f.get("u32(wRPerm)","u32(wCPerm)","d1 + 1","d2")}; + let wValue2 = ${f.get("u32(wRPerm)","u32(wCPerm)","d1 + 2","d2")}; + let wValue3 = ${f.get("u32(wRPerm)","u32(wCPerm)","d1 + 3","d2")}; + + var xValue = ${w.get("batch","idyR","idyC","d2")}; + let tmpval = vec4<${s}>(dot(xValue, wValue0), + dot(xValue, wValue1), + dot(xValue, wValue2), + dot(xValue, wValue3)); + dotProd[0] = dotProd[0] + tmpval; + + xValue = ${w.get("batch","idyR","idyC2","d2")}; + + dotProd[1] = dotProd[1] + vec4<${s}>(dot(xValue, wValue0), + dot(xValue, wValue1), + dot(xValue, wValue2), + dot(xValue, wValue3)); + } + } else if (bDyCVal) { + let d2Length = uniforms.Dy_shape[${c}]; + for (var d2: u32 = 0; d2 < d2Length; d2 = d2 + 4) { + let wValue0 = ${f.get("u32(wRPerm)","u32(wCPerm)","d1","d2")}; + let wValue1 = ${f.get("u32(wRPerm)","u32(wCPerm)","d1 + 1","d2")}; + let wValue2 = ${f.get("u32(wRPerm)","u32(wCPerm)","d1 + 2","d2")}; + let wValue3 = ${f.get("u32(wRPerm)","u32(wCPerm)","d1 + 3","d2")}; + + var xValue = ${w.get("batch","idyR","idyC","d2")}; + let tmpval = vec4<${s}>(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 = ${f.get("u32(wRPerm)","u32(wCPerm)","d1","d2")}; + let wValue1 = ${f.get("u32(wRPerm)","u32(wCPerm)","d1 + 1","d2")}; + let wValue2 = ${f.get("u32(wRPerm)","u32(wCPerm)","d1 + 2","d2")}; + let wValue3 = ${f.get("u32(wRPerm)","u32(wCPerm)","d1 + 3","d2")}; + + var xValue = ${w.get("batch","idyR","idyC2","d2")}; + let tmpval = vec4<${s}>(dot(xValue, wValue0), + dot(xValue, wValue1), + dot(xValue, wValue2), + dot(xValue, wValue3)); + dotProd[1] = dotProd[1] + tmpval; + } + } + } + } + + for (var i: u32 = 0; i < ${p}; i = i + 1) { + let value = dotProd[i] + ${r?"bias[c+i]":`vec4<${s}>(0.0)`}; + ${g.set("batch","r","c + i","d1","value")}; + } + }`,_=` + let outputIndices = ${g.offsetToIndices("global_idx")}; + let batch = ${g.indicesGet("outputIndices",0)}; + let d1 = ${g.indicesGet("outputIndices",c)}; + let r = ${g.indicesGet("outputIndices",d)}; + let c = ${g.indicesGet("outputIndices",l)}; + 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 = ${s}(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 = (${s}(dyRCorner) + ${s}(wR)) / ${s}(uniforms.strides[0]); + let wRPerm = uniforms.filter_dims.x - 1 - wR / uniforms.dilations.x; + if (dyR < 0.0 || dyR >= ${s}(uniforms.Dy_shape[${d}]) || 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 = (${s}(dyCCorner) + ${s}(wC)) / ${s}(uniforms.strides.y); + let wCPerm = uniforms.filter_dims.y - 1 - wC / uniforms.dilations.y; + if (dyC < 0.0 || dyC >= ${s}(uniforms.Dy_shape[${l}]) || + 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 = ${u?w.get("batch","idyR","idyC","inputChannel"):w.get("batch","inputChannel","idyR","idyC")}; + let wValue = ${f.get("inputChannel","wOutChannel","u32(wRPerm)","u32(wCPerm)")}; + dotProd = dotProd + xValue * wValue; + inputChannel = inputChannel + 1; + } + } + } + let value = dotProd + ${r?"bias[d1]":`${s}(0.0)`}; + ${g.setByOffset("global_idx","value")}; + `;return` + ${e.registerUniforms(a).declareVariables(...y,g)} + ${h} + + ${e.mainStart()} + ${e.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")}; + ${i?b:_}}`},Xn=(e,t,n)=>{let r=e.length>2,o=t.outputShape,i=x.size(o),s=[Math.ceil(i/64),1,1];X("verbose",()=>`[conv2d_backprop_webgpu] dispatch = ${s}`);let a=t.format==="NHWC",u=["rank","rank"],d=[t.strides[0],t.strides[1]],l=[t.kernelShape[a?1:2],t.kernelShape[a?2:3]],c=[t.dilations[0],t.dilations[1]],p=[l[0]+(t.dilations[0]<=1?0:(t.kernelShape[a?1:2]-1)*(t.dilations[0]-1)),l[1]+(t.dilations[1]<=1?0:(t.kernelShape[a?2:3]-1)*(t.dilations[1]-1))],h=[p[0]-1-Math.floor((t.pads[0]+t.pads[2])/2),p[1]-1-Math.floor(t.pads[1]+t.pads[3])/2],m=!1,f=t.group,w=e[1].dims,y=w[0]/f,g=w[1],b=[{type:12,data:i},{type:12,data:d},{type:12,data:l},{type:12,data:c},{type:12,data:p},{type:6,data:h},{type:12,data:y},{type:12,data:g},...A(e[0].dims,e[1].dims)];r&&(b.push(...A(e[2].dims)),u.push("rank")),b.push(...A(o));let _=s[1]===1&&s[2]===1,$=v=>{let I=[{name:"output_size",type:"u32"},{name:"strides",type:"u32",length:d.length},{name:"filter_dims",type:"u32",length:l.length},{name:"dilations",type:"u32",length:l.length},{name:"effective_filter_dims",type:"u32",length:p.length},{name:"pads",type:"i32",length:h.length},{name:"input_channels_per_group",type:"u32"},{name:"output_channels_per_group",type:"u32"}],T=J(e[0].dataType);return`${jd(v,e,o,r,_,m,T,I,a)}`};return{name:"ConvTranspose2D",shaderCache:{hint:`${t.cacheKey};`,inputDependencies:u},getRunData:()=>({dispatchGroup:{x:s[0],y:s[1],z:s[2]},outputs:[{dims:n?n(o):o,dataType:e[0].dataType}],programUniforms:b}),getShaderSource:$}}});var Xd,Zd,Qd,as,us,Yd,Jd,el,tl,ds,ls=k(()=>{"use strict";is();ss();Ue();Me();Xd=(e,t,n,r,o,i)=>(e-1)*t+n+(r-1)*o+1-i,Zd=(e,t,n,r,o)=>{let i=Math.floor(e/2);t==="SAME_UPPER"?(n[r]=i,n[o]=e-i):t==="SAME_LOWER"&&(n[r]=e-i,n[o]=i)},Qd=(e,t,n,r,o,i,s,a,u,d)=>{let l=e.length-2,c=d.length===0;u.length{let n=e.kernelShape.slice();if(e.kernelShape.length===0||e.kernelShape.reduce((c,p)=>c*p,1)===0){n.length=0;for(let c=2;cc+p,0)===0){let c=t[0].dims.length-2;u=new Array(c).fill(1)}let d=e.strides.slice();if(d.reduce((c,p)=>c+p,0)===0){let c=t[0].dims.length-2;d=new Array(c).fill(1)}Qd(a,n,u,e.autoPad,e.group,o,d,r,s,i);let l=Object.assign({},e);return Object.assign(l,{kernelShape:n,pads:o,outputPadding:s,outputShape:i,dilations:u,strides:d}),l},us=e=>{let t=Yt(e),n=e.format,r=["NOTSET","VALID","SAME_UPPER","SAME_LOWER"][typeof e.autoPad>"u"?0:e.autoPad],o=e.dilations,i=e.group,s=e.kernelShape,a=e.pads,u=e.strides,d=e.wIsConst(),l=e.outputPadding,c=e.outputShape;return{autoPad:r,format:n,dilations:o,group:i,kernelShape:s,outputPadding:l,outputShape:c,pads:a,strides:u,wIsConst:d,...t,cacheKey:`${e.format};${t.activation};`}},Yd=(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 n=e[0].dims[t.format==="NHWC"?e[0].dims.length-1:1],r=e[1].dims[0];if(n!==r)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((l,c)=>l+c,0)>0&&t.dilations.length!==i)throw new Error(`dilations should be ${i}D`);if(t.strides.reduce((l,c)=>l+c,0)>0&&t.strides.length!==i)throw new Error(`strides should be ${i}D`);if(t.pads.reduce((l,c)=>l+c,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((l,c)=>l+c,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")},Jd=[2,3,1,0],el=(e,t,n)=>{let r=as(n,t),o=n.format==="NHWC",i=r.outputShape,s=i[o?3:1],a=t[0].dims[o?3:1];if(r.group!==1||s===1&&a===1){e.compute(Xn(t,r));return}let u=i[o?1:2],d=i[o?2:3],l=t[1].dims[2],c=t[1].dims[3],p=o?u*d:s,h=o?s:u*d,m=l*c*a,f=!0,w=e.kernelCustomData.wT??e.compute(le(t[1],Jd),{inputs:[1],outputs:[n.wIsConst?-2:-1]})[0];n.wIsConst&&!e.kernelCustomData.wT&&(e.kernelCustomData.wT=w);let y=[t[0],w],g=t.length===3;g&&(!o&&t[2].dims.length===1?y.push(t[2].reshape([t[2].dims[0],1,1])):y.push(t[2])),e.compute(os(y,r,i,p,h,m,g,f),{inputs:y})},tl=(e,t)=>{let n=t.format==="NHWC",r=[e.inputs[0].reshape(n?[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&&r.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 s=t.strides;(s.length===0||s[0]===0)&&(s=[1]);let a=t.pads;a.length===0&&(a=[0,0]),a=[0,a[0],0,a[1]],s=[1].concat(s),i=[1].concat(i),o=[1].concat(o);let u=as({...t,pads:a,strides:s,dilations:i,kernelShape:o},r);e.compute(Xn(r,u,d=>n?[d[0],d[2],d[3]]:[d[0],d[1],d[3]]))},ds=(e,t)=>{Yd(e.inputs,t),e.inputs[0].dims.length===3?tl(e,t):el(e,e.inputs,t)}});var nl,cs,ps,ms=k(()=>{"use strict";U();N();ae();W();nl=(e,t,n,r)=>{let o=x.size(t),i=t.length,s=S("input",e,i),a=C("output",e,i),u=n.dataType===6?n.getInt32Array()[0]:Number(n.getBigInt64Array()[0]),d=x.normalizeAxis(u,i),l=c=>{let p=` i32(${s.indicesGet("inputIndices","uniforms.axis")}) `,h=B("uniforms.input_shape","uniforms.axis",i),m=r.reverse?p+(r.exclusive?" + 1":""):"0",f=r.reverse?h:p+(r.exclusive?"":" + 1");return` + ${c.registerUniform("outputSize","u32").registerUniform("axis","u32").declareVariables(s,a)} + ${c.mainStart()} + ${c.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.outputSize")} + var inputIndices = ${a.offsetToIndices("global_idx")}; + var sum = ${a.type.value}(0); + let first : i32 = ${m}; + let last : i32 = ${f}; + for (var i : i32 = first; i < last; i++) { + ${s.indicesSet("inputIndices","uniforms.axis","u32(i)")}; + sum = sum + ${s.getByIndices("inputIndices")}; + } + ${a.setByOffset("global_idx","sum")}; + }`};return{name:"CumSum",shaderCache:{hint:r.cacheKey,inputDependencies:["rank"]},getRunData:()=>({outputs:[{dims:t,dataType:e}],dispatchGroup:{x:Math.ceil(o/64)},programUniforms:[{type:12,data:o},{type:12,data:d},...A(t,t)]}),getShaderSource:l}},cs=(e,t)=>{let n=e.inputs[0].dims,r=e.inputs[0].dataType,o=e.inputs[1];e.compute(nl(r,n,o,t),{inputs:[0]})},ps=e=>{let t=e.exclusive===1,n=e.reverse===1;return V({exclusive:t,reverse:n})}});var rl,ol,il,fs,hs,gs=k(()=>{"use strict";U();N();ae();W();rl=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.")},ol=(e,t,n,r)=>{let o=[];o.push(`fn perm(i: ${r.type.indices}) -> ${n.type.indices} { + var a: ${n.type.indices};`);for(let i=0;i{let n,r,o,i,s,a,u=t.format==="NHWC",d=t.blocksize,l=t.mode==="DCR";u?([n,r,o,i]=e.dims,s=l?[n,r,o,d,d,i/d**2]:[n,r,o,i/d**2,d,d],a=l?[0,1,3,2,4,5]:[0,1,4,2,5,3]):([n,r,o,i]=[e.dims[0],e.dims[2],e.dims[3],e.dims[1]],s=l?[n,d,d,i/d**2,r,o]:[n,i/d**2,d,d,r,o],a=l?[0,3,4,1,5,2]:[0,1,4,2,5,3]);let c=e.reshape(s),p=c.dims.length,h=e.dataType,m=S("a",h,p),f=C("output",h,p),w=y=>` + ${y.registerUniform("output_size","u32").declareVariables(m,f)} + + ${ol(a,p,m,f)} + + ${y.mainStart()} + ${y.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} + + let indices = ${f.offsetToIndices("global_idx")}; + let aIndices = perm(indices); + + ${f.setByOffset("global_idx",m.getByIndices("aIndices"))} + }`;return{name:"DepthToSpace",shaderCache:{hint:`${e.dims};${t.blocksize};${t.mode}`,inputDependencies:["rank"]},getRunData:y=>{let g=u?[n,r*d,o*d,i/d**2]:[n,i/d**2,r*d,o*d],b=x.size(g),_=c.dims,$=x.sortBasedOnPerm(_,a);return{outputs:[{dims:g,dataType:y[0].dataType}],dispatchGroup:{x:Math.ceil(b/64)},programUniforms:[{type:12,data:b},...A(_,$)]}},getShaderSource:w}},fs=(e,t)=>{rl(e.inputs),e.compute(il(e.inputs[0],t))},hs=e=>V({blocksize:e.blocksize,mode:e.mode,format:e.format})});var Zn,rn,ys,sl,al,Qn,Yn,bs,ul,ws,_s,$s=k(()=>{"use strict";U();N();ae();W();Zn="[a-zA-Z]|\\.\\.\\.",rn="("+Zn+")+",ys="^"+rn+"$",sl="("+rn+",)*"+rn,al="^"+sl+"$",Qn=class{constructor(t=-1){this.symbolToIndices=new Map,this.inputIndex=t}addSymbol(t,n){let r=this.symbolToIndices.get(t);r===void 0?r=[n]:r.push(n),this.symbolToIndices.set(t,r)}},Yn=class{constructor(t,n){this.equation=n;this.hasEllipsis=!1,this.symbolToInfo=new Map,this.lhs=new Array,this.outputDims=[];let[r,o]=n.includes("->")?n.split("->",2):[n,""];if(!r.match(RegExp(al)))throw new Error("Invalid LHS term");if(r.split(",").forEach((a,u)=>{let d=t[u].dims.slice();if(!a.match(RegExp(ys)))throw new Error("Invalid LHS term");let l=this.processTerm(a,!0,d,u);this.lhs.push(l)}),o==="")o+=[...this.symbolToInfo.entries()].filter(([a,u])=>u.count===1||a==="...").map(([a])=>a).join("");else if(!o.match(RegExp(rn)))throw new Error("Invalid RHS");o.match(RegExp(Zn,"g"))?.forEach(a=>{if(a==="...")this.outputDims=this.outputDims.concat(this.ellipsisDims);else{let u=this.symbolToInfo.get(a);if(u===void 0)throw new Error("Invalid RHS symbol");this.outputDims.push(u.dimValue)}}),this.rhs=this.processTerm(o,!1,this.outputDims)}addSymbol(t,n,r){let o=this.symbolToInfo.get(t);if(o!==void 0){if(o.dimValue!==n&&o.count!==1)throw new Error("Dimension mismatch");o.count++,o.inputIndices.push(r)}else o={count:1,dimValue:n,inputIndices:[r]};this.symbolToInfo.set(t,o)}processTerm(t,n,r,o=-1){let i=r.length,s=!1,a=[],u=0;if(!t.match(RegExp(ys))&&!n&&t!=="")throw new Error("Invalid LHS term");let d=t.match(RegExp(Zn,"g")),l=new Qn(o);return d?.forEach((c,p)=>{if(c==="..."){if(s)throw new Error("Only one ellipsis is allowed per input term");s=!0;let h=i-d.length+1;if(h<0)throw new Error("Ellipsis out of bounds");if(a=r.slice(u,u+h),this.hasEllipsis){if(this.ellipsisDims.length!==a.length||this.ellipsisDims.toString()!==a.toString())throw new Error("Ellipsis dimensions mismatch")}else if(n)this.hasEllipsis=!0,this.ellipsisDims=a;else throw new Error("Ellipsis must be specified in the LHS");for(let m=0;me+"_max",ul=(e,t,n,r)=>{let i=e.map(l=>l.length).map((l,c)=>S(`input${c}`,t,l)),s=x.size(r),a=C("output",t,r.length),u=[...n.symbolToInfo.keys()].filter(l=>!n.rhs.symbolToIndices.has(l)),d=l=>{let c=[],p="var prod = 1.0;",h="var sum = 0.0;",m="sum += prod;",f=[],w=[],y=[],g=[],b=n.symbolToInfo.size===n.rhs.symbolToIndices.size;n.symbolToInfo.forEach(($,v)=>{if(n.rhs.symbolToIndices.has(v)){let I=n.rhs.symbolToIndices.get(v)?.[0];I!==void 0&&n.lhs.forEach((T,P)=>{if($.inputIndices.includes(P)){let z=T.symbolToIndices.get(v);if(z===void 0)throw new Error("Invalid symbol error");z.forEach(R=>{c.push(`${i[P].indicesSet(`input${P}Indices`,R,a.indicesGet("outputIndices",I))}`)})}})}else n.lhs.forEach((I,T)=>{if($.inputIndices.includes(T)){let P=I.symbolToIndices.get(v);if(P===void 0)throw new Error("Invalid symbol error");P.forEach(z=>{f.push(`${i[T].indicesSet(`input${T}Indices`,z,`${v}`)}`)}),g.push(`prod *= ${i[T].getByIndices(`input${T}Indices`)};`)}}),w.push(`for(var ${v}: u32 = 0; ${v} < uniforms.${bs(v)}; ${v}++) {`),y.push("}")});let _=b?[...c,`let sum = ${i.map(($,v)=>$.getByIndices(`input${v}Indices`)).join(" * ")};`]:[...c,h,...w,...f,p,...g,m,...y];return` + ${l.registerUniforms(u.map($=>({name:`${bs($)}`,type:"u32"}))).registerUniform("outputSize","u32").declareVariables(...i,a)} + + ${l.mainStart()} + ${l.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.outputSize")} + var outputIndices = ${a.offsetToIndices("global_idx")}; + ${i.map(($,v)=>`var input${v}Indices: ${i[v].type.indices};`).join(` +`)} + ${_.join(` +`)}; + ${a.setByOffset("global_idx","sum")}; + }`};return{name:"Einsum",shaderCache:{hint:n.equation,inputDependencies:e.map(()=>"rank")},getRunData:()=>{let l=u.filter(p=>n.symbolToInfo.has(p)).map(p=>({type:12,data:n.symbolToInfo.get(p)?.dimValue||0}));l.push({type:12,data:s});let c=e.map((p,h)=>[...A(p)]).reduce((p,h)=>p.concat(h),l);return c.push(...A(r)),{outputs:[{dims:r,dataType:t}],dispatchGroup:{x:Math.ceil(s/64)},programUniforms:c}},getShaderSource:d}},ws=(e,t)=>{let n=new Yn(e.inputs,t.equation),r=n.outputDims,o=e.inputs.map((i,s)=>i.dims);e.compute(ul(o,e.inputs[0].dataType,n,r))},_s=e=>{let t=e.equation.replace(/\s+/g,"");return V({equation:t})}});var dl,vs,ll,cl,xs,Ss=k(()=>{"use strict";U();N();W();dl=e=>{if(!e||e.length!==2)throw new Error("Expand requires 2 input.");let t=e[0].dims,n=Array.from(e[1].getBigInt64Array(),Number),r=n.length{let n=e.length-t.length,r=[];for(let o=0;oe.length>t.length?vs(e,t):vs(t,e),cl=e=>{let t=e[0].dims,n=Array.from(e[1].getBigInt64Array(),Number),r=ll(t,n),o=e[0].dataType,i=o===9?4:1,s=Math.ceil(x.size(r)/i),a=d=>{let l=S("input",o,t.length,i),c=C("output",o,r.length,i),p;if(o===9){let h=(m,f,w="")=>` + let outputIndices${f} = ${c.offsetToIndices(`outputOffset + ${f}u`)}; + let offset${f} = ${l.broadcastedIndicesToOffset(`outputIndices${f}`,c)}; + let index${f} = offset${f} / 4u; + let component${f} = offset${f} % 4u; + ${m}[${f}] = ${w}(${l.getByOffset(`index${f}`)}[component${f}]); + `;p=` + let outputOffset = global_idx * ${i}; + var data = vec4(0); + ${h("data",0,"u32")} + ${h("data",1,"u32")} + ${h("data",2,"u32")} + ${h("data",3,"u32")} + ${c.setByOffset("global_idx","data")} + }`}else p=` + let outputIndices = ${c.offsetToIndices("global_idx")}; + let inputOffset = ${l.broadcastedIndicesToOffset("outputIndices",c)}; + ${c.setByOffset("global_idx",l.getByOffset("inputOffset"))} + }`;return` + ${d.registerUniform("vec_size","u32").declareVariables(l,c)} + ${d.mainStart()} + ${d.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.vec_size")} + ${p}`},u=[{type:12,data:s},...A(t,r)];return{name:"Expand",shaderCache:{hint:`${r.length}`,inputDependencies:["rank"]},getShaderSource:a,getRunData:()=>({outputs:[{dims:r,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(s/64)},programUniforms:u})}},xs=e=>{dl(e.inputs),e.compute(cl(e.inputs),{inputs:[0]})}});var pl,Is,Ts=k(()=>{"use strict";U();N();W();Qt();pl=e=>{let t=e[0].dataType,n=x.size(e[0].dims),r=x.size(e[1].dims),o=r%4===0,i=s=>{let a=S("x",t,[1],4),u=S("bias",t,[1],4),d=C("y",t,[1],4),l=[{name:"output_vec_size",type:"u32"},{name:"bias_size",type:"u32"}],c=h=>` + let bias${h}_offset: u32 = (global_idx * 4 + ${h}) % uniforms.bias_size; + let bias${h} = ${u.getByOffset(`bias${h}_offset / 4`)}[bias${h}_offset % 4];`,p=o?` + let bias = ${u.getByOffset("global_idx % (uniforms.bias_size / 4)")};`:`${c(0)}${c(1)}${c(2)}${c(3)} + let bias = ${a.type.value}(bias0, bias1, bias2, bias3);`;return`${s.registerUniforms(l).declareVariables(a,u,d)} + + ${Ln(de(t))} + + ${s.mainStart(je)} + ${s.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_vec_size")} + + let x = ${a.getByOffset("global_idx")}; + ${p} + let x_in = x + bias; + ${d.setByOffset("global_idx",Nn("x_in"))} + }`};return{name:"FastGeluWithBias",shaderCache:{hint:`${o}`,inputDependencies:["type","type"]},getShaderSource:i,getRunData:s=>({outputs:[{dims:s[0].dims,dataType:s[0].dataType}],programUniforms:[{type:12,data:Math.ceil(n/4)},{type:12,data:r}],dispatchGroup:{x:Math.ceil(n/je/4)}})}},Is=e=>{e.inputs.length<2||x.size(e.inputs[1].dims)===0?xi(e):e.compute(pl(e.inputs))}});var ml,fl,Cs,As,ks=k(()=>{"use strict";U();N();ae();W();ml=e=>{if(!e||e.length!==2)throw new Error("Gather requires 2 inputs.")},fl=(e,t)=>{let n=e[0].dims,r=e[1].dims,o=n.length,i=x.normalizeAxis(t.axis,o),s=n.slice(0);s.splice(i,1,...r);let a=n[i],u=e[0].dataType===9?4:1,d=Math.ceil(x.size(s)/u),l=[{type:12,data:d},{type:6,data:a},{type:12,data:i},...A(e[0].dims,e[1].dims,s)],c=p=>{let h=S("data",e[0].dataType,e[0].dims.length,u),m=S("inputIndices",e[1].dataType,e[1].dims.length),f=C("output",e[0].dataType,s.length,u),w=g=>{let b=r.length,_=`var indicesIndices${g} = ${m.type.indices}(0);`;for(let $=0;$1?`indicesIndices${g}[${$}]`:`indicesIndices${g}`} = ${s.length>1?`outputIndices${g}[uniforms.axis + ${$}]`:`outputIndices${g}`};`;_+=` + var idx${g} = ${m.getByIndices(`indicesIndices${g}`)}; + if (idx${g} < 0) { + idx${g} = idx${g} + uniforms.axisDimLimit; + } + var dataIndices${g} : ${h.type.indices}; + `;for(let $=0,v=0;$1?`dataIndices${g}[${$}]`:`dataIndices${g}`} = u32(idx${g});`,v+=b):(_+=`${o>1?`dataIndices${g}[${$}]`:`dataIndices${g}`} = ${s.length>1?`outputIndices${g}[${v}]`:`outputIndices${g}`};`,v++);return _},y;if(e[0].dataType===9){let g=(b,_,$="")=>` + let outputIndices${_} = ${f.offsetToIndices(`outputOffset + ${_}u`)}; + ${w(_)}; + let offset${_} = ${h.indicesToOffset(`dataIndices${_}`)}; + let index${_} = offset${_} / 4u; + let component${_} = offset${_} % 4u; + ${b}[${_}] = ${$}(${h.getByOffset(`index${_}`)}[component${_}]); + `;y=` + let outputOffset = global_idx * ${u}; + var value = vec4(0); + ${g("value",0,"u32")} + ${g("value",1,"u32")} + ${g("value",2,"u32")} + ${g("value",3,"u32")} + ${f.setByOffset("global_idx","value")} + `}else y=` + let outputIndices = ${f.offsetToIndices("global_idx")}; + ${w("")}; + let value = ${h.getByIndices("dataIndices")}; + ${f.setByOffset("global_idx","value")}; + `;return` + ${p.registerUniform("outputSize","u32").registerUniform("axisDimLimit","i32").registerUniform("axis","u32").declareVariables(h,m,f)} + ${p.mainStart()} + ${p.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.outputSize")} + ${y} + }`};return{name:"Gather",shaderCache:{hint:t.cacheKey,inputDependencies:["rank","rank"]},getRunData:()=>({outputs:[{dims:s,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(d/64)},programUniforms:l}),getShaderSource:c}},Cs=e=>V({axis:e.axis}),As=(e,t)=>{let n=e.inputs;ml(n),e.compute(fl(e.inputs,t))}});var hl,gl,Es,Ps,zs=k(()=>{"use strict";U();N();ae();W();hl=(e,t)=>{if(e.length<3||e.length>4)throw new Error("GatherBlockQuantized requires 3 or 4 inputs.");let n=x.normalizeAxis(t.quantizeAxis,e[0].dims.length),r=t.blockSize,o=e[0],i=e[2],s=e.length===4?e[3]:void 0;if(i.dims.length!==o.dims.length||!o.dims.map((a,u)=>u===n?Math.ceil(a/r)===i.dims[u]:a===i.dims[u]).reduce((a,u)=>a&&u,!0))throw new Error("Scales must have the same rank as the input tensor and the dims should match except on gatherAxis.");if(s){if(s.dataType!==o.dataType)throw new Error("Zero point must have the same data type as the input tensor.");if(s.dims.length!==i.dims.length||!s.dims.map((a,u)=>a===i.dims[u]).reduce((a,u)=>a&&u,!0))throw new Error("Zero point must have the same rank as the input tensor and the dims should match except on quantizeAxis.")}},gl=(e,t)=>{let n=e[0].dims,r=e[1].dims,o=n.length,i=x.normalizeAxis(t.gatherAxis,o),s=x.normalizeAxis(t.quantizeAxis,o),a=n.slice(0);a.splice(i,1,...r);let u=x.size(a),d=e[2].dataType,c=e[0].dataType===22,p=[{type:12,data:u},{type:12,data:s},{type:12,data:i},{type:12,data:t.blockSize},...A(...e.map((m,f)=>m.dims),a)],h=m=>{let f=S("data",e[0].dataType,e[0].dims.length),w=S("inputIndices",e[1].dataType,e[1].dims.length),y=S("scales",e[2].dataType,e[2].dims.length),g=e.length>3?S("zeroPoint",e[3].dataType,e[3].dims.length):void 0,b=C("output",d,a.length),_=[f,w,y];g&&_.push(g);let $=[{name:"output_size",type:"u32"},{name:"quantize_axis",type:"u32"},{name:"gather_axis",type:"u32"},{name:"block_size",type:"u32"}];return` + ${m.registerUniforms($).declareVariables(..._,b)} + ${m.mainStart()} + let output_indices = ${b.offsetToIndices("global_idx")}; + var indices_indices = ${w.type.indices}(0); + ${(()=>r.length>1?` + for (var i: u32 = 0; i < ${r.length}; i++) { + let index = ${b.indicesGet("output_indices","uniforms.gather_axis + i")}; + ${w.indicesSet("indices_indices","i","index")}; + }`:`indices_indices = ${b.indicesGet("output_indices","uniforms.gather_axis")};`)()}; + var data_indices = ${f.type.indices}(0); + for (var i: u32 = 0; i < uniforms.gather_axis; i++) { + let index = ${b.indicesGet("output_indices","i")}; + ${f.indicesSet("data_indices","i","index")}; + } + var index_from_indices = ${w.getByIndices("indices_indices")}; + if (index_from_indices < 0) { + index_from_indices += ${n[i]}; + } + ${f.indicesSet("data_indices","uniforms.gather_axis","u32(index_from_indices)")}; + for (var i = uniforms.gather_axis + 1; i < ${a.length}; i++) { + let index = ${b.indicesGet("output_indices",`i + ${r.length} - 1`)}; + ${f.indicesSet("data_indices","i","index")}; + } + let data_offset = ${f.indicesToOffset("data_indices")}; + let data_index = data_offset % 8; + // Convert 4-bit packed data to 8-bit packed data. + let packed_4bit_quantized_data = ${f.getByOffset("data_offset / 8")}; + let packed_8bit_quantized_data = (packed_4bit_quantized_data >> (4 * (data_index % 2))) & 0x0f0f0f0f; + let quantized_data_vec = ${c?"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 = ${y.indicesGet("data_indices","uniforms.quantize_axis")} / uniforms.block_size; + ${y.indicesSet("scale_indices","uniforms.quantize_axis","quantize_axis_index")}; + var scale = ${y.getByIndices("scale_indices")}; + ${(()=>g?` + let zero_point_indices = scale_indices; + let zero_point_offset = ${g.indicesToOffset("zero_point_indices")}; + let zero_point_index = zero_point_offset % 8; + let packed_4bit_zero_points = ${g.getByOffset("zero_point_offset / 8")}; + let packed_8bit_zero_points = (packed_4bit_zero_points >> (4 * (zero_point_index % 2))) & 0x0f0f0f0f; + let zero_point_vec = ${c?"unpack4xI8":"unpack4xU8"}(u32(packed_8bit_zero_points)); + let zero_point = zero_point_vec[zero_point_index / 2];`:"var zero_point = 0")()}; + let dequantized_data = ${de(d)}(quantized_data - zero_point) * scale; + ${b.setByOffset("global_idx","dequantized_data")}; + }`};return{name:"GatherBlockQuantized",shaderCache:{hint:`${t.cacheKey};${e.filter((m,f)=>f!==1).map(m=>m.dims.join("_")).join(";")}`,inputDependencies:Array.from({length:e.length},(m,f)=>"rank")},getRunData:()=>({outputs:[{dims:a,dataType:d}],dispatchGroup:{x:Math.ceil(u/64)},programUniforms:p}),getShaderSource:h}},Es=(e,t)=>{let n=e.inputs;hl(n,t),e.compute(gl(e.inputs,t))},Ps=e=>V({blockSize:e.blockSize,gatherAxis:e.gatherAxis,quantizeAxis:e.quantizeAxis})});var yl,bl,Bs,Ds,Os=k(()=>{"use strict";U();N();ae();W();yl=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.`)},bl=(e,t)=>{let n=e[0].dims,r=e[0].dataType,o=n.length,i=e[1].dims,s=e[1].dataType,a=x.normalizeAxis(t.axis,o),u=n[a],d=i.slice(0),l=x.size(d),c=S("input",r,o),p=S("indicesInput",s,i.length),h=C("output",r,d.length),m=[{type:12,data:l},{type:6,data:u},{type:12,data:a}];return m.push(...A(n,i,d)),{name:"GatherElements",shaderCache:{inputDependencies:["rank","rank"]},getRunData:()=>({outputs:[{dims:d,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(l/64)},programUniforms:m}),getShaderSource:y=>` + ${y.registerUniform("outputSize","u32").registerUniform("axisDimLimit","i32").registerUniform("axis","u32").declareVariables(c,p,h)} + ${y.mainStart()} + ${y.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.outputSize")} + + let outputIndices = ${h.offsetToIndices("global_idx")}; + + var idx = ${p.getByOffset("global_idx")}; + if (idx < 0) { + idx = idx + uniforms.axisDimLimit; + } + var inputIndices = ${c.type.indices}(outputIndices); + ${c.indicesSet("inputIndices","uniforms.axis","u32(idx)")}; + let value = ${c.getByIndices("inputIndices")}; + + ${h.setByOffset("global_idx","value")}; + }`}},Bs=e=>V({axis:e.axis}),Ds=(e,t)=>{let n=e.inputs;yl(n),e.compute(bl(e.inputs,t))}});var wl,_l,Rs,Ms,Us=k(()=>{"use strict";U();N();W();wl=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")},_l=(e,t)=>{let n=e[0].dims.slice(),r=e[1].dims.slice(),[o,i,s]=qt.getShapeOfGemmResult(n,t.transA,r,t.transB,e.length===3?e[2].dims:void 0),a=[o,i];if(!a)throw new Error("Can't use gemm on the given tensors");let u=x.size(a),d=[{type:12,data:u},{type:12,data:o},{type:12,data:i},{type:12,data:s},{type:1,data:t.alpha},{type:1,data:t.beta}],l=["type","type"];e.length===3&&(d.push(...A(e[2].dims)),l.push("rank")),d.push(...A(a));let c=p=>{let h="";t.transA&&t.transB?h="value += a[k * uniforms.M + m] * b[n * uniforms.K + k];":t.transA&&!t.transB?h="value += a[k * uniforms.M + m] * b[k * uniforms.N + n];":!t.transA&&t.transB?h="value += a[m * uniforms.K + k] * b[n * uniforms.K + k];":!t.transA&&!t.transB&&(h="value += a[m * uniforms.K + k] * b[k * uniforms.N + n];");let m=t.alpha===1?"":"value *= uniforms.alpha;",f=S("a",e[0].dataType,e[0].dims),w=S("b",e[1].dataType,e[1].dims),y=f.type.value,g=null,b=[f,w];e.length===3&&(g=S("c",e[2].dataType,e[2].dims.length),b.push(g));let _=C("output",e[0].dataType,a.length);b.push(_);let $=[{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` + ${p.registerUniforms($).declareVariables(...b)} + + ${p.mainStart()} + ${p.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} + + let m = global_idx / uniforms.N; + let n = global_idx % uniforms.N; + + var value = ${y}(0); + for (var k: u32 = 0u; k < uniforms.K; k++) { + ${h} + } + + ${m} + ${(()=>g!=null?`let cOffset = ${g.broadcastedIndicesToOffset("vec2(m, n)",_)}; value += ${y}(uniforms.beta) * ${g.getByOffset("cOffset")};`:"")()} + output[global_idx] = value; + }`};return{name:"Gemm",shaderCache:{hint:`${t.cacheKey}`,inputDependencies:l},getRunData:()=>({outputs:[{dims:a,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(u/64)},programUniforms:d}),getShaderSource:c}},Rs=e=>{let t=e.transA,n=e.transB,r=e.alpha,o=e.beta;return{transA:t,transB:n,alpha:r,beta:o,cacheKey:`${e.transA};${e.transB};${e.alpha===1}`}},Ms=(e,t)=>{wl(e.inputs),e.compute(_l(e.inputs,t))}});var we,xl,Ls,Vs,Sl,gt,Ns,Jn=k(()=>{"use strict";U();N();ae();Ht();Xt();W();Me();we=(e,t)=>e.length>t&&e[t].dims.length>0?e[t]:void 0,xl=(e,t)=>{let n=e[0],r=we(e,1),o=we(e,2),i=we(e,3),s=we(e,4),a=we(e,5),u=we(e,6),d=we(e,7);if(n.dims.length!==3&&n.dims.length!==5)throw new Error("Input query is expected to have 3 or 5 dimensions");let l=n.dims[0],c=n.dims[1],p=n.dims.length===3?n.dims[2]:t.numHeads*n.dims[4],h=c,m=0,f=0,w=Math.floor(p/t.numHeads);if(u&&d&&x.size(u.dims)&&x.size(d.dims)){if(u.dims.length!==4)throw new Error('Input "past_key" is expected to have 4 dimensions');if(u.dims[0]!==l||u.dims[1]!==t.numHeads||u.dims[3]!==w)throw new Error('Input "past_key" shape (batch_size, num_heads, past_sequence_length, head_size)');if(d.dims[0]!==l||d.dims[1]!==t.numHeads||d.dims[3]!==w)throw new Error('Input "past_value" shape (batch_size, num_heads, past_sequence_length, head_size)');if(u.dims[2]!==d.dims[2])throw new Error('Input "past_key" and "past_value" shall have same dim 2 (past_sequence_length)');if(d.dims.length!==4)throw new Error('Input "past_value" is expected to have 4 dimensions');m=u.dims[2],f=u.dims[2]}else if(u&&x.size(u.dims)||d&&x.size(d.dims))throw new Error('Input "past_key" and "past_value" shall be both present or both absent');let y;if(r&&x.size(r.dims)>0){if(n.dims.length!==3)throw new Error('Input "query" is expected to have 3 dimensions when key is given');if(r.dims.length<3||r.dims.length>5)throw new Error('Input "key" is expected to have 3, 4, or 5 dimensions');if(n.dims[0]!==r.dims[0])throw new Error('Input "query" and "key" shall have same dim 0 (batch size)');if(r.dims.length===3){if(r.dims[2]!==n.dims[2])throw new Error('Input "query" and "key" shall have same dim 2 (hidden_size)');y=2,h=r.dims[1]}else if(r.dims.length===5){if(r.dims[2]!==t.numHeads||r.dims[3]!==2||r.dims[4]!==w)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.');y=5,h=r.dims[1]}else{if(r.dims[1]!==t.numHeads||r.dims[3]!==w)throw new Error('Expect "key" shape (batch_size, num_heads, kv_sequence_length, head_size) for past_key');y=0,h=r.dims[2]}}else{if(n.dims.length!==5)throw new Error('Input "query" is expected to have 5 dimensions when key is empty');if(n.dims[2]!==t.numHeads||n.dims[3]!==3)throw new Error('Expect "query" shape (batch_size, kv_sequence_length, num_heads, 3, head_size) for packed kv');y=3}if(i&&x.size(i.dims)>0){if(i.dims.length!==1)throw new Error('Input "bias" is expected to have 1 dimension');if(r&&r.dims.length===5&&r.dims[3]===2)throw new Error("bias is not allowed for packed kv.")}let g=m+h,b=0;if(s&&x.size(s.dims)>0){b=8;let I=s.dims;throw I.length===1?I[0]===l?b=1:I[0]===3*l+2&&(b=3):I.length===2&&I[0]===l&&I[1]===g&&(b=5),b===8?new Error('Input "key_padding_mask" shape shall be (batch_size) or (batch_size, total_sequence_length)'):new Error("Mask not supported")}let _=!1,$=p;if(o&&x.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(n.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(h!==o.dims[1])throw new Error('Input "key" and "value" shall have the same dim 1 (kv_sequence_length)');$=o.dims[2]}else{if(h!==o.dims[2])throw new Error('Input "key" and "value" shall have the same dim 2 (kv_sequence_length)');$=o.dims[1]*o.dims[3],_=!0}}let v=!1;if(s&&x.size(s.dims)>0)throw new Error("Key padding mask is not supported");if(a&&x.size(a.dims)>0){if(a.dims.length!==4)throw new Error('Input "attention_bias" is expected to have 4 dimensions');if(a.dims[0]!==l||a.dims[1]!==t.numHeads||a.dims[2]!==c||a.dims[3]!==g)throw new Error('Expect "attention_bias" shape (batch_size, num_heads, sequence_length, total_sequence_length)')}return{batchSize:l,sequenceLength:c,pastSequenceLength:m,kvSequenceLength:h,totalSequenceLength:g,maxSequenceLength:f,inputHiddenSize:0,hiddenSize:p,vHiddenSize:$,headSize:w,vHeadSize:Math.floor($/t.numHeads),numHeads:t.numHeads,isUnidirectional:!1,pastPresentShareBuffer:!1,maskFilterValue:t.maskFilterValue,maskType:b,scale:t.scale,broadcastResPosBias:v,passPastInKv:_,qkvFormat:y}},Ls=e=>V({...e}),Vs=V({perm:[0,2,1,3]}),Sl=(e,t,n,r,o,i,s)=>{let a=[r,o,i],u=x.size(a),d=[{type:12,data:u},{type:12,data:s},{type:12,data:i}],l=c=>{let p=C("qkv_with_bias",t.dataType,a),h=S("qkv",t.dataType,a),m=S("bias",n.dataType,a),f=[{name:"output_size",type:"u32"},{name:"bias_offset",type:"u32"},{name:"hidden_size",type:"u32"}];return` + ${c.registerUniforms(f).declareVariables(h,m,p)} + ${c.mainStart()} + ${c.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:a,dataType:t.dataType,gpuDataType:0}],dispatchGroup:{x:Math.ceil(u/64)},programUniforms:d}),getShaderSource:l},{inputs:[t,n],outputs:[-1]})[0]},gt=(e,t,n,r,o,i,s,a)=>{let u=i;if(s&&x.size(s.dims)>0){if(r===1)throw new Error("AddBiasReshape is not implemented. Please export your model with packed QKV or KV");return u=Sl(e,i,s,t,r,n*o,a),u=u.reshape([t,r,n,o]),n===1||r===1?u:e.compute(le(u,Vs.perm),{inputs:[u],outputs:[-1]})[0]}else return i.dims.length===3&&(u=i.reshape([t,r,n,o])),n===1||r===1?u:e.compute(le(u,Vs.perm),{inputs:[u],outputs:[-1]})[0]},Ns=(e,t)=>{let n=xl(e.inputs,t),r=e.inputs[0],o=we(e.inputs,1),i=we(e.inputs,2),s=we(e.inputs,3),a=we(e.inputs,4),u=we(e.inputs,5),d=we(e.inputs,6),l=we(e.inputs,7);if(r.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 c=o&&i&&o.dims.length===4&&i.dims.length===4,p=gt(e,n.batchSize,n.numHeads,n.sequenceLength,n.headSize,r,s,0);if(c)return Je(e,p,o,i,a,void 0,d,l,u,n,t);if(!o||!i)throw new Error("key and value must be provided");let h=gt(e,n.batchSize,n.numHeads,n.kvSequenceLength,n.headSize,o,s,n.hiddenSize),m=gt(e,n.batchSize,n.numHeads,n.kvSequenceLength,n.vHeadSize,i,s,2*n.hiddenSize);Je(e,p,h,m,a,void 0,d,l,u,n,t)}});var Ws,Il,Tl,er,Gs,tr=k(()=>{"use strict";U();N();W();Ws=e=>Array.from(e.getBigInt64Array(),Number),Il=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(Ws(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")},Tl=(e,t)=>{let n=[];for(let r=0;r{let n=e[0].dims,r=t??Ws(e[1]),o=Tl(n,r),i=x.size(o),s=e[0].dataType,a=S("input",s,n.length),u=C("output",s,o.length),d=l=>` + const inputShape = ${a.indices(...n)}; + ${l.registerUniform("output_size","u32").declareVariables(a,u)} + ${l.mainStart()} + ${l.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} + let output_indices = ${u.offsetToIndices("global_idx")}; + var input_indices: ${a.type.indices}; + for (var i = 0; i < ${n.length}; i++) { + let input_dim_i = ${a.indicesGet("uniforms.input_shape","i")}; + let input_dim_value = ${u.indicesGet("output_indices","i")} % input_dim_i; + + ${a.indicesSet("input_indices","i","input_dim_value")} + } + ${u.setByOffset("global_idx",a.getByIndices("input_indices"))} + }`;return{name:"Tile",shaderCache:{hint:`${r}`,inputDependencies:["rank"]},getRunData:()=>({outputs:[{dims:o,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(i/64)},programUniforms:[{type:12,data:i},...A(e[0].dims,o)]}),getShaderSource:d}},Gs=e=>{Il(e.inputs),e.compute(er(e.inputs),{inputs:[0]})}});var Cl,Hs,Fs,Al,qs,Ks,js=k(()=>{"use strict";U();N();ae();Xt();W();Jn();tr();Me();Cl=(e,t)=>{let n=e[0],r=e[1],o=e[2],i=e[3],s=e[4];if(n.dims.length!==3&&n.dims.length!==5)throw new Error("Input query is expected to have 3 or 5 dimensions");let a=!1,u=n.dims[0],d=n.dims[1],l=n.dims.length===3?a?n.dims[2]/3:n.dims[2]:t.numHeads*n.dims[4],c=d,p=0,h=0,m=Math.floor(l/t.numHeads),f=i&&i.dims.length!==0,w=s&&s.dims.length!==0,y=!0;if(f&&w){if(i.dims.length!==4)throw new Error('Input "past_key" is expected to have 4 dimensions');if(s.dims.length!==4)throw new Error('Input "past_value" is expected to have 4 dimensions');y?(p=i.dims[1],h=i.dims[1]):(p=i.dims[2],h=i.dims[2])}else if(f||w)throw new Error('Input "past_key" and "past_value" shall be both present or both absent');let g;if(r){if(n.dims.length!==3)throw new Error('Input "query" is expected to have 3 dimensions when key is given');if(r.dims.length<3||r.dims.length>5)throw new Error('Input "key" is expected to have 3, 4, or 5 dimensions');if(n.dims[0]!==r.dims[0])throw new Error('Input "query" and "key" shall have same dim 0 (batch size)');if(r.dims.length===3){if(n.dims[2]%r.dims[2]!==0)throw new Error('Dimension 2 of "query" should be a multiple of "key"');g=2,c=r.dims[1]}else if(r.dims.length===5){if(r.dims[2]!==t.numHeads||r.dims[3]!==2||r.dims[4]!==m)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.');g=5,c=r.dims[1]}else{if(r.dims[1]!==t.numHeads||r.dims[3]!==m)throw new Error('Expect "key" shape (batch_size, num_heads, kv_sequence_length, head_size) for past_key');g=0,c=r.dims[2]}}else{if(n.dims.length!==3&&n.dims.length!==5)throw new Error('Input "query" is expected to have 3 or 5 dimensions when key is empty');if(n.dims.length===5&&(n.dims[2]!==t.numHeads||n.dims[3]!==3))throw new Error('Expect "query" shape (batch_size, kv_sequence_length, num_heads, 3, head_size) for packed kv');g=3}let b=0,_=!1,$=l;if(o){if(o.dims.length!==3&&o.dims.length!==4)throw new Error('Input "value" is expected to have 3 or 4 dimensions');if(n.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(c!==o.dims[1])throw new Error('Input "key" and "value" shall have the same dim 1 (kv_sequence_length)');$=o.dims[2]}else{if(c!==o.dims[2])throw new Error('Input "past_key" and "past_value" shall have the same dim 2 (kv_sequence_length)');$=o.dims[1]*o.dims[3],_=!0}}let v=p+c,I=!1;return{batchSize:u,sequenceLength:d,pastSequenceLength:p,kvSequenceLength:c,totalSequenceLength:v,maxSequenceLength:h,inputHiddenSize:0,hiddenSize:l,vHiddenSize:$,headSize:m,vHeadSize:Math.floor($/t.kvNumHeads),numHeads:t.numHeads,kvNumHeads:t.kvNumHeads,nReps:t.numHeads/t.kvNumHeads,pastPresentShareBuffer:!1,maskType:b,scale:t.scale,broadcastResPosBias:I,passPastInKv:_,qkvFormat:g,isPastkvBSNH:y}},Hs=(e,t,n,r)=>{let o=[r.batchSize,r.totalSequenceLength,r.kvNumHeads,r.headSize],i=4,s=x.size(o)/i,a=r.totalSequenceLength,u=C("present_kv",n,o.length,i),d=S("new_kv",e.dataType,e.dims.length,i),l=t?S("past_kv",t.dataType,t.dims.length,i):void 0,c=Math.ceil(r.headSize/i),p={x:a,y:e.dims[0],z:1},h=t?["rank","rank"]:["rank"],m=[{type:12,data:s},{type:12,data:r.pastSequenceLength},{type:12,data:r.kvSequenceLength},{type:12,data:r.totalSequenceLength}],f=[d];l?(m.push(...A(e.dims),...A(t.dims),...A(o)),f.push(l)):m.push(...A(e.dims),...A(o));let w=[{name:"output_size",type:"u32"},{name:"past_seqlen",type:"u32"},{name:"new_seqlen",type:"u32"},{name:"present_seqlen",type:"u32"}],y=` let past_batch_stride = uniforms.past_seqlen * num_heads * H; + var past_head_stride = uniforms.past_seqlen * H; + if (is_bsnh) { + past_head_stride = H; + } + let in_offset = b * past_batch_stride + s * row_stride + n * past_head_stride + h; + present_kv[out_offset] = past_kv[in_offset];`,g=` let new_batch_stride = uniforms.new_seqlen * num_heads * H; + let new_row_stride = num_heads * H; + let new_head_stride = H; + let in_offset = b * new_batch_stride + (s - past_seqlen) * new_row_stride + n * new_head_stride + h; + present_kv[out_offset] = new_kv[in_offset];`,b=t?`if (s < past_seqlen) { + ${y} + } else if (s < past_seqlen + uniforms.new_seqlen) { + ${g} + }`:`if (s < past_seqlen + uniforms.new_seqlen) { + ${g} + }`,_=$=>` + + ${$.registerUniforms(w).declareVariables(...f,u)} + ${$.mainStart([c,r.kvNumHeads,1])} + ${$.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} + var indices = ${u.offsetToIndices("global_idx")}; + let h = local_id.x; + let n = local_id.y; + let s = workgroup_id.x; + let b = workgroup_id.y; + let num_heads = ${r.kvNumHeads}u; + let H = ${c}u; + + let present_seqlen = uniforms.present_seqlen; + let present_batch_stride = present_seqlen * num_heads * H; + var row_stride = H; + let is_bsnh = ${r.isPastkvBSNH}; + + if (is_bsnh) { + row_stride = num_heads * H; + } + var present_head_stride = present_seqlen * H; + if (is_bsnh) { + present_head_stride = H; + } + + let past_seqlen = uniforms.past_seqlen; + + let out_offset = b * present_batch_stride + s * row_stride + n * present_head_stride + h; + ${b} + }`;return{name:"ConcatPastNew",shaderCache:{hint:`${r.kvNumHeads}${c}${!!t}`,inputDependencies:h},getRunData:()=>({outputs:[{dims:o,dataType:n}],dispatchGroup:p,programUniforms:m}),getShaderSource:_}},Fs=e=>V({...e}),Al=V({perm:[0,2,1,3]}),qs=(e,t,n,r,o)=>{let i=t,s=r.kvNumHeads,a=r.nReps;return t.dims.length===3&&r.kvSequenceLength!==0&&(i=t.reshape([r.batchSize,r.kvSequenceLength,s,r.headSize])),n?i=e.compute(Hs(i,n,i.dataType,r),{inputs:[i,n],outputs:[r.isPastkvBSNH?o:-1]})[0]:i=e.compute(Hs(i,void 0,i.dataType,r),{inputs:[i],outputs:[r.isPastkvBSNH?o:-1]})[0],a!==1&&(i=e.compute(er([i],[1,1,1,a]),{inputs:[i],outputs:[-1]})[0],i=i.reshape([r.batchSize,r.totalSequenceLength,s*a,r.headSize])),e.compute(le(i,Al.perm),{inputs:[i],outputs:[-1]})[0]},Ks=(e,t)=>{let n=Cl(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 r=gt(e,n.batchSize,n.numHeads,n.sequenceLength,n.headSize,e.inputs[0],void 0,0),o=e.inputs[3]&&e.inputs[3].dims.length!==0?e.inputs[3]:void 0,i=e.inputs[4]&&e.inputs[4].dims.length!==0?e.inputs[4]:void 0,s=qs(e,e.inputs[1],o,n,1),a=qs(e,e.inputs[2],i,n,2);Je(e,r,s,a,void 0,void 0,void 0,void 0,void 0,n,t)}});var Xs,kl,El,Zs,Qs=k(()=>{"use strict";U();N();Me();W();Xs=(e,t,n,r,o,i,s,a)=>{let u=te(i),d=u===1?"f32":`vec${u}f`,l=u===1?"vec2f":`mat2x${u}f`,c=o*s,p=[o,s,i/u],h=[o,s,2],m=["rank","type","type"],f=[];f.push(...A(p,h));let w=y=>{let g=S("x",t.dataType,3,u),b=S("scale",n.dataType,n.dims),_=S("bias",r.dataType,r.dims),$=C("output",1,3,2),v=[g,b,_,$],I=64;return` + var workgroup_shared : array<${l}, ${I}>; + const workgroup_size = ${I}u; + ${y.declareVariables(...v)} + ${y.mainStart(I)} + 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 = ${d}(0); + var squared_sum = ${d}(0); + for (var h = local_idx; h < hight; h += workgroup_size) { + let value = ${d}(${g.get("batch","channel","h")}); + sum += value; + squared_sum += value * value; + } + workgroup_shared[local_idx] = ${l}(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 = ${ke("workgroup_shared[0][0]",u)} / f32(hight * ${u}); + let squared_sum_final = ${ke("workgroup_shared[0][1]",u)} / f32(hight * ${u}); + + let inv_std_dev = inverseSqrt(squared_sum_final - sum_final * sum_final + f32(${a})); + 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:`${u};${a}`,inputDependencies:m},getRunData:()=>({outputs:[{dims:h,dataType:1}],dispatchGroup:{x:c},programUniforms:f}),getShaderSource:w},{inputs:[t,n,r],outputs:[-1]})[0]},kl=(e,t,n)=>{let r=t[0].dims,o=r,i=2,s=r[0],a=r[1],u=x.sizeFromDimension(r,i),d=te(u),l=x.size(o)/d,c=Xs(e,t[0],t[1],t[2],s,u,a,n.epsilon),p=[s,a,u/d],h=[s,a],m=["type","none"],f=w=>{let y=S("x",t[0].dataType,p.length,d),g=S("scale_shift",1,h.length,2),b=C("output",t[0].dataType,p.length,d),_=[y,g,b];return` + ${w.registerUniform("output_size","u32").declareVariables(..._)} + ${w.mainStart()} + ${w.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} + let outputIndices = ${b.offsetToIndices("global_idx")}; + let batch = outputIndices[0]; + let channel = outputIndices[1]; + let scale_shift = ${g.getByIndices("vec2(batch, channel)")}; + let value = ${y.getByOffset("global_idx")} * ${b.type.value}(scale_shift.x) + ${b.type.value}(scale_shift.y); + ${b.setByOffset("global_idx","value")}; + }`};e.compute({name:"InstanceNormalization",shaderCache:{hint:`${d}`,inputDependencies:m},getRunData:()=>({outputs:[{dims:o,dataType:t[0].dataType}],dispatchGroup:{x:Math.ceil(l/64)},programUniforms:[{type:12,data:l},...A(p,h,p)]}),getShaderSource:f},{inputs:[t[0],c]})},El=(e,t,n)=>{let r=t[0].dims,o=r,i=r[0],s=r[r.length-1],a=x.sizeFromDimension(r,1)/s,u=te(s),d=x.size(o)/u,l=[{type:12,data:a},{type:12,data:Math.floor(s/u)}],c=["type","type"],p=[0,r.length-1];for(let w=0;w{let y=J(t[0].dataType),g=u===1?"vec2f":`mat${u}x2f`,b=v=>{let I=v===0?"x":"y",T=u===1?"f32":`vec${u}f`;switch(u){case 1:return`${y}(${T}(scale.${I}))`;case 2:return`vec2<${y}>(${T}(scale[0].${I}, scale[1].${I}))`;case 4:return`vec4<${y}>(${T}(scale[0].${I}, scale[1].${I}, scale[2].${I}, scale[3].${I}))`;default:throw new Error(`Not supported compoents ${u}`)}},_=S("input",t[0].dataType,t[0].dims,u),$=C("output",t[0].dataType,o,u);return` + @group(0) @binding(0) var input : array<${_.type.storage}>; + @group(0) @binding(1) var scale_input : array<${g}>; + @group(0) @binding(2) var output : array<${$.type.storage}>; + struct Uniforms {H: u32, C : u32}; + @group(0) @binding(3) var uniforms: Uniforms; + + ${w.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], ${b(0)}, ${b(1)}); + }`};e.compute({name:"InstanceNormalizationNHWC",shaderCache:{hint:`${u}`,inputDependencies:c},getRunData:()=>({outputs:[{dims:o,dataType:t[0].dataType}],dispatchGroup:{x:Math.ceil(d/64)},programUniforms:l}),getShaderSource:f},{inputs:[t[0],m]})},Zs=(e,t)=>{t.format==="NHWC"?El(e,e.inputs,t):kl(e,e.inputs,t)}});var Pl,zl,Ys,Js=k(()=>{"use strict";U();N();W();Pl=e=>{if(!e||e.length<2)throw new Error("layerNorm requires at least 2 inputs.")},zl=(e,t,n)=>{let r=t.simplified,o=e[0].dims,i=e[1],s=!r&&e[2],a=o,u=x.normalizeAxis(t.axis,o.length),d=x.sizeToDimension(o,u),l=x.sizeFromDimension(o,u),c=x.size(i.dims),p=s?x.size(s.dims):0;if(c!==l||s&&p!==l)throw new Error(`Size of X.shape()[axis:] == ${l}. + Size of scale and bias (if provided) must match this. + Got scale size of ${c} and bias size of ${p}`);let h=[];for(let $=0;$1,g=n>2,b=$=>{let v=J(e[0].dataType),I=[S("x",e[0].dataType,e[0].dims,m),S("scale",i.dataType,i.dims,m)];s&&I.push(S("bias",s.dataType,s.dims,m)),I.push(C("output",e[0].dataType,a,m)),y&&I.push(C("mean_data_output",1,h)),g&&I.push(C("inv_std_output",1,h));let T=[{name:"norm_count",type:"u32"},{name:"norm_size",type:"f32"},{name:"norm_size_vectorized",type:"u32"},{name:"epsilon",type:"f32"}];return` + ${$.registerUniforms(T).declareVariables(...I)} + ${$.mainStart()} + ${$.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.norm_count")} + let offset = global_idx * uniforms.norm_size_vectorized; + var mean_vector = ${Rn("f32",m)}; + var mean_square_vector = ${Rn("f32",m)}; + + for (var h: u32 = 0u; h < uniforms.norm_size_vectorized; h++) { + let value = ${Xe(v,m,"x[h + offset]")}; + mean_vector += value; + mean_square_vector += value * value; + } + let mean = ${ke("mean_vector",m)} / uniforms.norm_size; + let inv_std_dev = inverseSqrt(${ke("mean_square_vector",m)} / uniforms.norm_size ${r?"":"- mean * mean"} + uniforms.epsilon); + + for (var j: u32 = 0; j < uniforms.norm_size_vectorized; j++) { + let f32input = ${Xe(v,m,"x[j + offset]")}; + let f32scale = ${Xe(v,m,"scale[j]")}; + output[j + offset] = ${I[0].type.value}((f32input ${r?"":"- mean"}) * inv_std_dev * f32scale + ${s?`+ ${Xe(v,m,"bias[j]")}`:""} + ); + } + + ${y?"mean_data_output[global_idx] = mean":""}; + ${g?"inv_std_output[global_idx] = inv_std_dev":""}; + }`},_=[{dims:a,dataType:e[0].dataType}];return y&&_.push({dims:h,dataType:1}),g&&_.push({dims:h,dataType:1}),{name:"LayerNormalization",shaderCache:{hint:`${m};${n};${r}`,inputDependencies:f},getRunData:()=>({outputs:_,dispatchGroup:{x:Math.ceil(d/64)},programUniforms:w}),getShaderSource:b}},Ys=(e,t)=>{Pl(e.inputs),e.compute(zl(e.inputs,t,e.outputCount))}});var Bl,Dl,Ol,ea,ta,na=k(()=>{"use strict";U();N();ae();W();Bl=(e,t)=>{if(e.length<3||e.length>4)throw new Error("MatMulNBits requires 3 or 4 inputs");let n=e[0],r=n.dims.length;if(n.dims[r-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,s=e[1];if(!x.areEqual(s.dims,[t.n,o,i]))throw new Error("The second inputs must be 3D tensor with shape N X nBlocksPerCol X blobSize");let u=e[2].dims;if(x.size(u)!==t.n*o)throw new Error("scales input size error.");if(e.length===4){let l=e[3].dims,c=t.bits>4?t.n*o:t.n*Math.floor((o+1)/2);if(x.size(l)!==c)throw new Error("zeroPoints input size error.")}},Dl=(e,t)=>{let n=e[0].dims,r=n.length,o=n[r-2],i=t.k,s=t.n,a=n.slice(0,r-2),u=x.size(a),l=e[1].dims[2]/4,c=e[0].dataType,p=te(t.k),h=te(l),m=te(s),f=a.concat([o,s]),w=o>1&&s/m%2===0?2:1,y=x.size(f)/m/w,g=64,b=[],_=[u,o,i/p],$=x.convertShape(e[1].dims).slice();$.splice(-1,1,l/h),b.push(...A(_)),b.push(...A($)),b.push(...A(e[2].dims)),e.length===4&&b.push(...A(x.convertShape(e[3].dims)));let v=[u,o,s/m];b.push(...A(v));let I=T=>{let P=_.length,z=S("a",e[0].dataType,P,p),R=S("b",12,$.length,h),Z=S("scales",e[2].dataType,e[2].dims.length),G=[z,R,Z],K=e.length===4?S("zero_points",12,e[3].dims.length):void 0;K&&G.push(K);let ie=v.length,M=C("output",e[0].dataType,ie,m),Q=J(e[0].dataType),re=(()=>{switch(p){case 1:return`array<${Q}, 8>`;case 2:return`mat4x2<${Q}>`;case 4:return`mat2x4<${Q}>`;default:throw new Error(`${p}-component is not supported.`)}})(),O=()=>{let q=` + // reuse a data + var input_offset = ${z.indicesToOffset(`${z.type.indices}(batch, row, word_offset)`)}; + var a_data: ${re}; + for (var j: u32 = 0; j < ${8/p}; j++) { + a_data[j] = ${z.getByOffset("input_offset")}; + input_offset++; + } + `;for(let F=0;F> 4) & b_mask); + b_quantized_values = ${re}(${Array.from({length:4},(E,D)=>`${Q}(b_value_lower[${D}]), ${Q}(b_value_upper[${D}])`).join(", ")}); + b_dequantized_values = ${(()=>p===1?`${re}(${Array.from({length:8},(E,D)=>`(b_quantized_values[${D}] - ${K?`zero_point${F}`:"zero_point"}) * scale${F}`).join(", ")});`:`(b_quantized_values - ${re}(${Array(8).fill(`${K?`zero_point${F}`:"zero_point"}`).join(",")})) * scale${F};`)()}; + workgroup_shared[local_id.x * ${w} + ${Math.floor(F/m)}]${m>1?`[${F%m}]`:""} += ${Array.from({length:8/p},(E,D)=>`${p===1?`a_data[${D}] * b_dequantized_values[${D}]`:`dot(a_data[${D}], b_dequantized_values[${D}])`}`).join(" + ")}; + `;return q},j=()=>{let q=` + var col_index = col * ${m}; + ${K?` + 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 = ${Q}(8);`} + `;for(let F=0;F> 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 = ${K.getByOffset("zero_point_word_index")} >> zero_point_bits_offset; + let zero_point${F} = ${Q}((zero_point_word) & 0xFu);`:""} + col_index += 1;`;return q},H=()=>{let q=`col_index = col * ${m};`;for(let F=0;F; + var b_value_upper: vec4; + var b_quantized_values: ${re}; + var b_dequantized_values: ${re};`,q};return` + var workgroup_shared: array<${M.type.value}, ${w*g}>; + ${T.declareVariables(...G,M)} + ${T.mainStart([g,1,1])} + let output_indices = ${M.offsetToIndices(`(global_idx / ${g}) * ${w}`)}; + 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 += ${g}) { + //process one block + var word_offset: u32 = block * ${t.blockSize/p}; + ${j()} + for (var word: u32 = 0; word < ${l}; word += ${h}) { + ${H()} + for (var i: u32 = 0; i < ${h}; i++) { + ${O()} + word_offset += ${8/p}; + } + } + } + workgroupBarrier(); + + if (local_id.x < ${w}) { + var output_value: ${M.type.value} = ${M.type.value}(0); + var workgroup_shared_offset: u32 = local_id.x; + for (var b: u32 = 0u; b < ${g}u; b++) { + output_value += workgroup_shared[workgroup_shared_offset]; + workgroup_shared_offset += ${w}; + } + ${M.setByIndices(`${M.type.indices}(batch, row, col + local_id.x)`,"output_value")}; + } + }`};return{name:"MatMulNBits",shaderCache:{hint:`${t.blockSize};${t.bits};${p};${h};${m};${w};${g}`,inputDependencies:Array(e.length).fill("rank")},getRunData:()=>({outputs:[{dims:f,dataType:c}],dispatchGroup:{x:y},programUniforms:b}),getShaderSource:I}},Ol=(e,t)=>{let n=e[0].dims,r=n.length,o=n[r-2],i=t.k,s=t.n,a=n.slice(0,r-2),u=x.size(a),l=e[1].dims[2]/4,c=e[0].dataType,p=te(t.k),h=te(l),m=a.concat([o,s]),f=128,w=s%8===0?8:s%4===0?4:1,y=f/w,g=y*h*8,b=g/p,_=g/t.blockSize,$=x.size(m)/w,v=[],I=[u,o,i/p],T=x.convertShape(e[1].dims).slice();T.splice(-1,1,l/h),v.push(...A(I)),v.push(...A(T)),v.push(...A(e[2].dims)),e.length===4&&v.push(...A(x.convertShape(e[3].dims)));let P=[u,o,s];v.push(...A(P));let z=R=>{let Z=I.length,G=S("a",e[0].dataType,Z,p),K=S("b",12,T.length,h),ie=S("scales",e[2].dataType,e[2].dims.length),M=[G,K,ie],Q=e.length===4?S("zero_points",12,e[3].dims.length):void 0;Q&&M.push(Q);let re=P.length,O=C("output",e[0].dataType,re),j=J(e[0].dataType),H=()=>{switch(p){case 1:return` + let a_data0 = vec4<${j}>(sub_a[word_offset], sub_a[word_offset + 1], sub_a[word_offset + 2], sub_a[word_offset + 3]); + let a_data1 = vec4<${j}>(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<${j}>(sub_a[word_offset], sub_a[word_offset + 1]); + let a_data1 = vec4<${j}>(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(`${p}-component is not supported.`)}};return` + var sub_a: array<${G.type.value}, ${b}>; + var inter_results: array, ${w}>; + ${R.declareVariables(...M,O)} + ${R.mainStart([y,w,1])} + let output_indices = ${O.offsetToIndices(`workgroup_index * ${w}`)}; + 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) / ${_} + 1; + + // Loop over shared dimension. + for (var tile: u32 = 0; tile < num_tiles; tile += 1) { + let a_col_start = tile * ${b}; + // load one tile A data into shared memory. + for (var a_offset = local_idx; a_offset < ${b}; a_offset += ${f}) + { + let a_col = a_col_start + a_offset; + if (a_col < uniforms.a_shape[2]) + { + sub_a[a_offset] = ${G.getByIndices(`${G.type.indices}(batch, row, a_col)`)}; + } else { + sub_a[a_offset] = ${G.type.value}(0); + } + } + workgroupBarrier(); + + // each thread process one block + let b_row = col + local_id.y; + let block = tile * ${_} + local_id.x; + ${Q?` + 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 = ${Q.getByOffset("zero_point_word_index")} >> zero_point_bits_offset; + let zero_point = ${j}((zero_point_word) & 0xFu);`:` + // The default zero point is 8 for unsigned 4-bit quantization. + let zero_point = ${j}(8);`} + let scale = ${ie.getByOffset("b_row * n_blocks_per_col + block")}; + let b_data = ${K.getByIndices(`${K.type.indices}(b_row, block, 0)`)}; + var word_offset = local_id.x * ${t.blockSize/p}; + for (var i: u32 = 0; i < ${h}; i++) { + ${H()} + let b_value = ${h===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<${j}>(${Array.from({length:4},(q,F)=>`${j}(b_value_lower[${F}]), ${j}(b_value_upper[${F}])`).join(", ")}); + let b_dequantized_values = (b_quantized_values - mat2x4<${j}>(${Array(8).fill("zero_point").join(",")})) * scale; + inter_results[local_id.y][local_id.x] += ${Array.from({length:2},(q,F)=>`${`dot(a_data${F}, b_dequantized_values[${F}])`}`).join(" + ")}; + word_offset += ${8/p}; + } + workgroupBarrier(); + } + + if (local_idx < ${w}) { + var output_value: ${O.type.value} = ${O.type.value}(0); + for (var b = 0u; b < ${y}; b++) { + output_value += inter_results[local_idx][b]; + } + if (col + local_idx < uniforms.output_shape[2]) + { + ${O.setByIndices(`${O.type.indices}(batch, row, col + local_idx)`,"output_value")} + } + } + }`};return{name:"BlockwiseMatMulNBits32",shaderCache:{hint:`${t.blockSize};${p};${h};${y};${w}`,inputDependencies:Array(e.length).fill("rank")},getRunData:()=>({outputs:[{dims:m,dataType:c}],dispatchGroup:{x:$},programUniforms:v}),getShaderSource:z}},ea=(e,t)=>{Bl(e.inputs,t),t.blockSize===32&&e.adapterInfo.isVendor("intel")&&e.adapterInfo.isArchitecture("gen-12lp")?e.compute(Ol(e.inputs,t)):e.compute(Dl(e.inputs,t))},ta=e=>V(e)});var Rl,Ml,Ul,Vl,Ll,Nl,Wl,Gl,ra,oa=k(()=>{"use strict";U();N();W();Rl=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].")}},Ml=(e,t,n)=>{let r="";for(let o=t-1;o>=0;--o)r+=` + k = i32(${e.indicesGet("indices",o)}) - ${B("uniforms.pads",o,n)}; + if (k < 0) { + break; + } + if (k >= i32(${B("uniforms.x_shape",o,t)})) { + break; + } + offset += k * i32(${B("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; + ${r} + value = x[offset]; + } + `},Ul=(e,t,n)=>{let r="";for(let o=t-1;o>=0;--o)r+=` + k = i32(${e.indicesGet("indices",o)}) - ${B("uniforms.pads",o,n)}; + if (k < 0) { + k = -k; + } + { + let _2n_1 = 2 * (i32(${B("uniforms.x_shape",o,t)}) - 1); + k = k % _2n_1; + if(k >= i32(${B("uniforms.x_shape",o,t)})) { + k = _2n_1 - k; + } + } + offset += k * i32(${B("uniforms.x_strides",o,t)}); + `;return` + var offset = 0; + var k = 0; + ${r} + value = x[offset]; + `},Vl=(e,t,n)=>{let r="";for(let o=t-1;o>=0;--o)r+=` + k = i32(${e.indicesGet("indices",o)}) - ${B("uniforms.pads",o,n)}; + if (k < 0) { + k = 0; + } + if (k >= i32(${B("uniforms.x_shape",o,t)})) { + k = i32(${B("uniforms.x_shape",o,t)}) - 1; + } + offset += k * i32(${B("uniforms.x_strides",o,t)}); + `;return` + var offset = 0; + var k = 0; + ${r} + value = x[offset]; + `},Ll=(e,t,n)=>{let r="";for(let o=t-1;o>=0;--o)r+=` + k = i32(${e.indicesGet("indices",o)}) - ${B("uniforms.pads",o,n)}; + if (k < 0) { + k += i32(${B("uniforms.x_shape",o,t)}]); + } + if (k >= i32(${B("uniforms.x_shape",o,t)})) { + k -= i32(${B("uniforms.x_shape",o,t)}); + } + offset += k * i32(${B("uniforms.x_strides",o,t)}); + `;return` + var offset = 0; + var k = 0; + ${r} + value = x[offset]; + `},Nl=(e,t,n)=>{switch(n.mode){case 0:return Ml(e,t,n.pads.length);case 1:return Ul(e,t,n.pads.length);case 2:return Vl(e,t,n.pads.length);case 3:return Ll(e,t,n.pads.length);default:throw new Error("Invalid mode")}},Wl=(e,t)=>{let n=x.padShape(e[0].dims.slice(),t.pads),r=e[0].dims,o=x.size(n),i=[{type:12,data:o},{type:6,data:t.pads}],s=e.length>=3&&e[2].data;t.mode===0&&i.push({type:s?e[2].dataType:1,data:t.value}),i.push(...A(e[0].dims,n));let a=["rank"],u=d=>{let l=C("output",e[0].dataType,n.length),c=S("x",e[0].dataType,r.length),p=c.type.value,h=Nl(l,r.length,t),m=[{name:"output_size",type:"u32"},{name:"pads",type:"i32",length:t.pads.length}];return t.mode===0&&m.push({name:"constant_value",type:s?p:"f32"}),` + ${d.registerUniforms(m).declareVariables(c,l)} + ${d.mainStart()} + ${d.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} + + let indices = ${l.offsetToIndices("global_idx")}; + + var value = ${p}(0); + ${h} + output[global_idx] = value; + }`};return{name:"Pad",shaderCache:{hint:`${t.mode}${s}`,inputDependencies:a},getRunData:()=>({outputs:[{dims:n,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(x.size(n)/64)},programUniforms:i}),getShaderSource:u}},Gl=(e,t)=>{if(e.length>1){let n=e[1].getBigInt64Array(),r=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 a=e[3].getBigInt64Array();for(let u=0;ui[Number(u)]=Number(a));let s=[];return i.forEach(a=>s.push(a)),{mode:t.mode,value:r,pads:s}}else return t},ra=(e,t)=>{Rl(e.inputs);let n=Gl(e.inputs,t);e.compute(Wl(e.inputs,n),{inputs:[0]})}});var on,ia,sa,aa,ua,Hl,ql,da,la,ca,pa,ma,fa,ha,ga,ya,ba,wa,_a,$a=k(()=>{"use strict";Se();U();N();W();on=e=>{if(Y.webgpu.validateInputContent&&(!e||e.length!==1))throw new Error("Pool ops requires 1 input.")},ia=(e,t,n)=>{let r=t.format==="NHWC",o=e.dims.slice();r&&o.splice(1,0,o.pop());let i=Object.hasOwnProperty.call(t,"dilations"),s=t.kernelShape.slice(),a=t.strides.slice(),u=i?t.dilations.slice():[],d=t.pads.slice();Ke.adjustPoolAttributes(n,o,s,a,u,d);let l=Ke.computePoolOutputShape(n,o,a,u,s,d,t.autoPad),c=Object.assign({},t);i?Object.assign(c,{kernelShape:s,strides:a,pads:d,dilations:u,cacheKey:t.cacheKey}):Object.assign(c,{kernelShape:s,strides:a,pads:d,cacheKey:t.cacheKey});let p=l.slice();return p.push(p.splice(1,1)[0]),[c,r?p:l]},sa=(e,t)=>{let n=t.format==="NHWC",r=x.size(e),o=x.size(t.kernelShape),i=[{type:12,data:r},{type:12,data:o}],s=[{name:"outputSize",type:"u32"},{name:"kernelSize",type:"u32"}];if(t.kernelShape.length<=2){let a=t.kernelShape[t.kernelShape.length-1],u=t.strides[t.strides.length-1],d=t.pads[t.pads.length/2-1],l=t.pads[t.pads.length-1],c=!!(d+l);i.push({type:12,data:a},{type:12,data:u},{type:12,data:d},{type:12,data:l}),s.push({name:"kw",type:"u32"},{name:"sw",type:"u32"},{name:"pwStart",type:"u32"},{name:"pwEnd",type:"u32"});let p=!1;if(t.kernelShape.length===2){let h=t.kernelShape[t.kernelShape.length-2],m=t.strides[t.strides.length-2],f=t.pads[t.pads.length/2-2],w=t.pads[t.pads.length-2];p=!!(f+w),i.push({type:12,data:h},{type:12,data:m},{type:12,data:f},{type:12,data:w}),s.push({name:"kh",type:"u32"},{name:"sh",type:"u32"},{name:"phStart",type:"u32"},{name:"phEnd",type:"u32"})}return[i,s,!0,c,p]}else{if(n)throw new Error("Pooling with kernelShape.length > 2 is not supported for NHWC format.");let a=x.computeStrides(t.kernelShape);i.push({type:12,data:a},{type:12,data:t.pads},{type:12,data:t.strides}),s.push({name:"kernelStrides",type:"u32",length:a.length},{name:"pads",type:"u32",length:t.pads.length},{name:"strides",type:"u32",length:t.strides.length});let u=t.pads.reduce((d,l)=>d+l);return[i,s,!!u,!1,!1]}},aa=(e,t,n,r,o,i,s,a,u,d,l,c)=>{let p=o.format==="NHWC",h=t.type.value,m=C("output",t.type.tensor,r);if(o.kernelShape.length<=2){let f="",w="",y="",g=n-(p?2:1);if(l?f=` + for (var i: u32 = 0u; i < uniforms.kw; i++) { + xIndices[${g}] = indices[${g}] * uniforms.sw - uniforms.pwStart + i; + if (xIndices[${g}] < 0 || xIndices[${g}] + >= uniforms.x_shape[${g}]) { + pad++; + continue; + } + let x_val = x[${t.indicesToOffset("xIndices")}]; + ${i} + }`:f=` + for (var i: u32 = 0u; i < uniforms.kw; i++) { + xIndices[${g}] = indices[${g}] * uniforms.sw - uniforms.pwStart + i; + let x_val = x[${t.indicesToOffset("xIndices")}]; + ${i} + }`,o.kernelShape.length===2){let _=n-(p?3:2);c?w=` + for (var j: u32 = 0u; j < uniforms.kh; j++) { + xIndices[${_}] = indices[${_}] * uniforms.sh - uniforms.phStart + j; + if (xIndices[${_}] < 0 || xIndices[${_}] >= uniforms.x_shape[${_}]) { + pad += i32(uniforms.kw); + continue; + } + `:w=` + for (var j: u32 = 0u; j < uniforms.kh; j++) { + xIndices[${_}] = indices[${_}] * uniforms.sh - uniforms.phStart + j; + `,y=` + } + `}return` + ${e.registerUniforms(u).declareVariables(t,m)} + + ${e.mainStart()} + ${e.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.outputSize")} + + let indices = ${m.offsetToIndices("global_idx")}; + var xIndices = ${m.offsetToIndices("global_idx")}; + + var value = ${h}(${a}); + var pad = 0; + ${w} + ${f} + ${y} + ${s} + + output[global_idx] = value; + }`}else{if(p)throw new Error("Pooling with kernelShape.length > 2 is not supported for NHWC format.");let f=o.kernelShape.length,w=o.pads.length,y="";return d?y=` + if (xIndices[j] >= uniforms.x_shape[j]) { + pad++; + isPad = true; + break; + } + } + if (!isPad) { + let x_val = x[${t.indicesToOffset("xIndices")}]; + ${i} + }`:y=` + } + let x_val = x[${t.indicesToOffset("xIndices")}]; + ${i} + `,` + ${e.registerUniforms(u).declareVariables(t,m)} + + ${e.mainStart()} + ${e.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.outputSize")} + let indices = ${m.offsetToIndices("global_idx")}; + var xIndices = ${m.offsetToIndices("global_idx")}; + + var offsets: array; + + var value = ${h}(${a}); + var pad = 0; + var isPad = false; + + for (var i: u32 = 0u; i < uniforms.kernelSize; i++) { + var offset = i; + for (var j = 0u; j < ${f-1}u; j++) { + offsets[j] = offset / ${B("uniforms.kernelStrides","j",f)}; + offset -= offsets[j] * ${B("uniforms.kernelStrides","j",f)}; + } + offsets[${f-1}] = offset; + + isPad = false; + for (var j = ${n-f}u; j < ${n}u; j++) { + xIndices[j] = indices[j] * ${B("uniforms.strides",`j - ${n-f}u`,f)} + + offsets[j - ${n-f}u] - ${B("uniforms.pads","j - 2u",w)}; + ${y} + } + ${s} + + output[global_idx] = value; + }`}},ua=e=>`${e.format};${e.ceilMode};${e.autoPad};${e.kernelShape.length}`,Hl=e=>`${ua(e)};${e.countIncludePad}`,ql=e=>`${ua(e)};${e.storageOrder};${e.dilations}`,da=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}),la=(e,t,n,r)=>{let[o,i]=ia(t,r,n),s=S("x",t.dataType,t.dims.length),a=s.type.value,u="value += x_val;",d="";o.countIncludePad?d+=`value /= ${a}(uniforms.kernelSize);`:d+=`value /= ${a}(i32(uniforms.kernelSize) - pad);`;let[l,c,p,h,m]=sa(i,o);l.push(...A(t.dims,i));let f=["rank"];return{name:e,shaderCache:{hint:`${r.cacheKey};${p};${h};${m}`,inputDependencies:f},getRunData:()=>({outputs:[{dims:i,dataType:t.dataType}],dispatchGroup:{x:Math.ceil(x.size(i)/64)},programUniforms:l}),getShaderSource:w=>aa(w,s,t.dims.length,i.length,o,u,d,0,c,p,h,m)}},ca=e=>{let t=e.count_include_pad!==0,n=da(e);if(n.ceilMode!==0)throw new Error("using ceil() in shape computation is not yet supported for AveragePool");let r={countIncludePad:t,...n,cacheKey:""};return{...r,cacheKey:Hl(r)}},pa=(e,t)=>{on(e.inputs),e.compute(la("AveragePool",e.inputs[0],!1,t))},ma={autoPad:"",ceilMode:0,countIncludePad:!1,kernelShape:[],strides:[],pads:[],storageOrder:0,dilations:[]},fa=e=>{let t=e.format;return{format:t,...ma,cacheKey:t}},ha=(e,t)=>{on(e.inputs),e.compute(la("GlobalAveragePool",e.inputs[0],!0,t))},ga=(e,t,n,r)=>{let[o,i]=ia(t,r,n),s=` + value = max(x_val, value); + `,a="",u=S("x",t.dataType,t.dims.length),d=["rank"],[l,c,p,h,m]=sa(i,o);return l.push(...A(t.dims,i)),{name:e,shaderCache:{hint:`${r.cacheKey};${p};${h};${m}`,inputDependencies:d},getRunData:()=>({outputs:[{dims:i,dataType:t.dataType}],dispatchGroup:{x:Math.ceil(x.size(i)/64)},programUniforms:l}),getShaderSource:f=>aa(f,u,t.dims.length,i.length,o,s,a,t.dataType===10?-65504:-1e5,c,p,h,m)}},ya=(e,t)=>{on(e.inputs),e.compute(ga("MaxPool",e.inputs[0],!1,t))},ba=e=>{let t=e.storage_order,n=e.dilations,r=da(e);if(t!==0)throw new Error("column major storage order is not yet supported for MaxPool");if(r.ceilMode!==0)throw new Error("using ceil() in shape computation is not yet supported for MaxPool");let o={storageOrder:t,dilations:n,...r,cacheKey:""};return{...o,cacheKey:ql(o)}},wa=e=>{let t=e.format;return{format:t,...ma,cacheKey:t}},_a=(e,t)=>{on(e.inputs),e.compute(ga("GlobalMaxPool",e.inputs[0],!0,t))}});var Kl,jl,va,xa,Sa=k(()=>{"use strict";U();N();ae();W();Kl=(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((n,r)=>n===e[2].dims[r]).reduce((n,r)=>n&&r,!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 n=e[0].dims[t.axis],r=e[1].dims[t.axis];if(t.blockSizeMath.ceil(n/(r-1)-1))throw new Error("blockSize must be with in the range [ceil(dI / Si), ceil(dI / (Si - 1) - 1)].")}},jl=(e,t)=>{let n=x.normalizeAxis(t.axis,e[0].dims.length),r=e[0].dataType,o=r===3,i=e[0].dims,s=e[1].dataType,a=x.size(i),u=r===3||r===2,d=u?[Math.ceil(x.size(e[0].dims)/4)]:e[0].dims,l=e[1].dims,c=e.length>2?e[2]:void 0,p=c?u?[Math.ceil(x.size(c.dims)/4)]:c.dims:void 0,h=l.length===0||l.length===1&&l[0]===1,m=h===!1&&l.length===1,f=te(a),w=h&&(!u||f===4),y=w?f:1,g=w&&!u?f:1,b=S("input",u?12:r,d.length,g),_=S("scale",s,l.length),$=c?S("zero_point",u?12:r,p.length):void 0,v=C("output",s,i.length,y),I=[b,_];$&&I.push($);let T=[d,l];c&&T.push(p);let P=[{type:12,data:a/y},{type:12,data:n},{type:12,data:t.blockSize},...A(...T,i)],z=R=>{let Z=[{name:"output_size",type:"u32"},{name:"axis",type:"u32"},{name:"block_size",type:"u32"}];return` + ${R.registerUniforms(Z).declareVariables(...I,v)} + ${R.mainStart()} + ${R.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} + let output_indices = ${v.offsetToIndices("global_idx")}; + + // Set input x + ${(()=>u?` + let input = ${b.getByOffset("global_idx / 4")}; + let x_vec = ${o?"unpack4xI8(input)":"unpack4xU8(input)"}; + let x_value = ${y===1?"x_vec[global_idx % 4]":"x_vec"};`:`let x_value = ${b.getByOffset("global_idx")};`)()}; + + // Set scale input + ${(()=>h?`let scale_value= ${_.getByOffset("0")}`:m?` + let scale_index = ${v.indicesGet("output_indices","uniforms.axis")}; + let scale_value= ${_.getByOffset("scale_index")};`:` + var scale_indices: ${_.type.indices} = output_indices; + let index = ${_.indicesGet("scale_indices","uniforms.axis")} / uniforms.block_size; + ${_.indicesSet("scale_indices","uniforms.axis","index")}; + let scale_value= ${_.getByIndices("scale_indices")};`)()}; + + // Set zero-point input + ${(()=>$?h?u?` + let zero_point_input = ${$.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 = ${$.getByOffset("0")}`:m?u?` + let zero_point_index = ${v.indicesGet("output_indices","uniforms.axis")}; + let zero_point_input = ${$.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 = ${v.indicesGet("output_indices","uniforms.axis")}; + let zero_point_value = ${$.getByOffset("zero_point_index")};`:u?` + let zero_point_offset = ${_.indicesToOffset("scale_indices")}; + let zero_point_input = ${$.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 = ${$.getByIndices("scale_indices")};`:`let zero_point_value = ${u?o?"i32":"u32":b.type.value}(0);`)()}; + // Compute and write output + ${v.setByOffset("global_idx",`${v.type.value}(x_value - zero_point_value) * scale_value`)}; + }`};return{name:"DequantizeLinear",shaderCache:{hint:t.cacheKey,inputDependencies:$?["rank","rank","rank"]:["rank","rank"]},getShaderSource:z,getRunData:()=>({outputs:[{dims:i,dataType:s}],dispatchGroup:{x:Math.ceil(a/y/64),y:1,z:1},programUniforms:P})}},va=(e,t)=>{Kl(e.inputs,t),e.compute(jl(e.inputs,t))},xa=e=>V({axis:e.axis,blockSize:e.blockSize})});var Xl,Zl,Ia,Ta=k(()=>{"use strict";Se();U();W();Xl=(e,t,n)=>{let r=e===t,o=et&&n>0;if(r||o||i)throw new Error("Range these inputs' contents are invalid.")},Zl=(e,t,n,r)=>{let o=Math.abs(Math.ceil((t-e)/n)),i=[o],s=o,a=[{type:12,data:s},{type:r,data:e},{type:r,data:n},...A(i)],u=d=>{let l=C("output",r,i.length),c=l.type.value,p=[{name:"outputSize",type:"u32"},{name:"start",type:c},{name:"delta",type:c}];return` + ${d.registerUniforms(p).declareVariables(l)} + ${d.mainStart()} + ${d.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.outputSize")} + output[global_idx] = uniforms.start + ${c}(global_idx) * uniforms.delta; + }`};return{name:"Range",shaderCache:{hint:`${r}`},getShaderSource:u,getRunData:()=>({outputs:[{dims:i,dataType:r}],dispatchGroup:{x:Math.ceil(s/64)},programUniforms:a})}},Ia=e=>{let t=0,n=0,r=0;e.inputs[0].dataType===6?(t=e.inputs[0].getInt32Array()[0],n=e.inputs[1].getInt32Array()[0],r=e.inputs[2].getInt32Array()[0]):e.inputs[0].dataType===1&&(t=e.inputs[0].getFloat32Array()[0],n=e.inputs[1].getFloat32Array()[0],r=e.inputs[2].getFloat32Array()[0]),Y.webgpu.validateInputContent&&Xl(t,n,r),e.compute(Zl(t,n,r,e.inputs[0].dataType),{inputs:[]})}});var Ql,Yl,Jl,ec,tc,nc,rc,oc,ic,sc,ac,Ca,uc,dc,lc,cc,pc,Aa,ka,Ea=k(()=>{"use strict";U();N();ae();W();Ql=(e,t)=>{if(e.every(n=>n>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")}},Yl=(e,t,n)=>{t.every(o=>o>=0&&o{throw new Error("Resize requires axes input values to be positive and less than rank")}));let r=new Array(n).fill(1);return t.forEach((o,i)=>r[o]=e[i]),r},Jl=(e,t,n,r,o,i)=>{let[s,a,u]=n>10?[1,2,3]:[-1,e.length>1?1:-1,-1],d=e[0].dims.length;if(s>0&&e.length>s&&e[s].dims.length>0)e[s].getFloat32Array().forEach(l=>i.push(l));else if(t.coordinateTransformMode==="tf_crop_and_resize")throw new Error("Resize requires RoI input to be specified when coordinateTransformMode is tfCropAndResize");if(a>0&&e.length>a&&e[a].dims.length===1&&e[a].dims[0]>0){if(e[a].getFloat32Array().forEach(l=>r.push(l)),r.length!==0&&r.length!==d&&n>=18&&r.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");Ql(r,t),t.axes.length>0&&Yl(r,t.axes,d).forEach((l,c)=>r[c]=l)}if(u>0&&e.length>u&&e[u].dims.length===1&&e[u].dims[0]>0&&(e[u].getBigInt64Array().forEach(l=>o.push(Number(l))),o.length!==0&&o.length!==d&&n>=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(r.length!==0&&r.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 r<"u"&&typeof o<"u"&&r.length>0&&o.length>d)throw new Error("Resize requires only of scales or sizes to be specified")},ec=(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`)}})()+"}",tc=(e,t,n)=>`fn getNearestPixelFromOriginal(xOriginal: ${n}, isDownSample: bool) -> ${n} {`+(()=>{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`)}})()+"}",nc=(e,t,n)=>{let r=new Array(n).fill(0).concat(new Array(n).fill(1)),o=e.length===0?r:e.slice();return t.length>0?(t.forEach((i,s)=>{r[i]=o[s],r[s+n]=o[t.length+s]}),r):o},rc=(e,t,n,r)=>{let o=[];if(n.length>0)if(r.length>0){if(e.forEach(i=>o.push(i)),Math.max(...r)>e.length)throw new Error("axes is out of bound");r.forEach((i,s)=>o[i]=n[s])}else n.forEach(i=>o.push(i));else{if(t.length===0)throw new Error("Resize requires either scales or sizes.");o=e.map((i,s)=>Math.round(i*t[s]))}return o},oc=(e,t,n)=>{let r=(()=>{switch(n.keepAspectRatioPolicy){case"not_larger":return n.axes.length>0?Math.min(...n.axes.map(i=>t[i]),Number.MAX_VALUE):Math.min(...t,Number.MAX_VALUE);case"not_smaller":return n.axes.length>0?Math.max(...n.axes.map(i=>t[i]),Number.MIN_VALUE):Math.max(...t,Number.MIN_VALUE);default:throw new Error(`Keep aspect ratio policy ${n.keepAspectRatioPolicy} is not supported`)}})();t.fill(1,0,t.length);let o=e.slice();return n.axes.length>0?(n.axes.forEach(i=>t[i]=r),n.axes.forEach(i=>o[i]=Math.round(e[i]*t[i]))):(t.fill(r,0,t.length),o.forEach((i,s)=>o[s]=Math.round(i*t[s]))),o},ic=(e,t,n,r,o)=>` + fn calculateOriginalIndicesFromOutputIndices(output_indices: ${e.type.indices}) -> array<${e.type.value}, ${n.length}> { + var original_indices: array<${e.type.value}, ${n.length}>; + for (var i:u32 = 0; i < ${n.length}; i++) { + var output_index = ${e.indicesGet("output_indices","i")}; + var scale = ${B("uniforms.scales","i",r)}; + var roi_low = ${B("uniforms.roi","i",o)}; + var roi_hi = ${B("uniforms.roi",`i + ${t.length}`,o)}; + if (scale == 1.0) { + original_indices[i] = ${e.type.value}(output_index); + } else { + var input_shape_i = ${B("uniforms.input_shape","i",t.length)}; + var output_shape_i = ${B("uniforms.output_shape","i",n.length)}; + original_indices[i] = getOriginalCoordinateFromResizedCoordinate(output_index, scale, output_shape_i, + input_shape_i, roi_low, roi_hi); + } + } + return original_indices; + }`,sc=(e,t,n,r,o,i,s)=>` + fn calculateInputIndicesFromOutputIndices(output_indices: ${t.type.indices}) -> ${e.type.indices} { + var input_indices: ${e.type.indices}; + for (var i:u32 = 0; i < ${r.length}; i++) { + var output_index = ${t.indicesGet("output_indices","i")}; + var input_index: u32; + var scale = ${B("uniforms.scales","i",o)}; + if (scale == 1.0) { + input_index = output_index; + } else { + var roi_low = ${B("uniforms.roi","i",i)}; + var roi_hi = ${B("uniforms.roi",`i + ${n.length}`,i)}; + var input_shape_i = ${B("uniforms.input_shape","i",n.length)}; + var output_shape_i = ${B("uniforms.output_shape","i",r.length)}; + var original_idx = getOriginalCoordinateFromResizedCoordinate(output_index, scale, output_shape_i, + input_shape_i, roi_low, roi_hi); + if (!${s} || (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; + }`,ac=(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 >= ${B("uniforms.input_shape","i",t.length)}) { + return false; + } + } + return true; + }`,Ca=(e,t,n,r)=>e.rank>r?` + ${e.indicesSet("input_indices",t,"channel")}; + ${e.indicesSet("input_indices",n,"batch")}; +`:"",uc=(e,t,n,r,o)=>{let[s,a,u,d]=n.length===2?[-1,0,1,-1]:[0,2,3,1],l=e.type.value;return` + fn getInputValue(batch: u32, channel: u32, row: u32, col: u32) -> ${l} { + var input_indices: ${e.type.indices}; + ${e.indicesSet("input_indices",a,`max(0, min(row, ${n[a]} - 1))`)}; + ${e.indicesSet("input_indices",u,`max(0, min(col, ${n[u]} - 1))`)}; + ${Ca(e,d,s,2)} + return ${e.getByIndices("input_indices")}; + } + + fn bilinearInterpolation(output_indices: ${t.type.indices}) -> ${l} { + var originalIndices = calculateOriginalIndicesFromOutputIndices(output_indices); + var row:${l} = originalIndices[${a}]; + var col:${l} = originalIndices[${u}]; + ${r?`if (row < 0 || row > (${n[a]} - 1) || col < 0 || col > (${n[u]} - 1)) { + return ${o}; + }`:""}; + row = max(0, min(row, ${n[a]} - 1)); + col = max(0, min(col, ${n[u]} - 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 = ${n.length>2?`u32(originalIndices[${d}])`:"0"}; + var batch: u32 = ${n.length>2?`u32(originalIndices[${s}])`:"0"}; + var x11: ${l} = getInputValue(batch, channel, row1, col1); + var x12: ${l} = getInputValue(batch, channel, row1, col2); + var x21: ${l} = getInputValue(batch, channel, row2, col1); + var x22: ${l} = getInputValue(batch, channel, row2, col2); + var dx1: ${l} = abs(row - ${l}(row1)); + var dx2: ${l} = abs(${l}(row2) - row); + var dy1: ${l} = abs(col - ${l}(col1)); + var dy2: ${l} = abs(${l}(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); + }`},dc=(e,t,n,r,o,i,s,a,u,d)=>{let l=n.length===2,c=!0,[p,h]=l?[0,1]:c?[2,3]:[1,2],m=e.type.value,f=w=>{let y=w===p?"row":"col";return` + fn ${y}CubicInterpolation(input_indices: ${e.type.indices}, output_indices: ${t.type.indices}) -> ${m} { + var output_index = ${t.indicesGet("output_indices",w)}; + var originalIdx: ${m} = getOriginalCoordinateFromResizedCoordinate(output_index, ${o[w]}, + ${r[w]}, ${n[w]}, ${i[w]}, ${i[w]} + ${n.length}); + var fractOriginalIdx: ${m} = originalIdx - floor(originalIdx); + var coefs = getCubicInterpolationCoefs(fractOriginalIdx); + + if (${a} && (originalIdx < 0 || originalIdx > (${n[w]} - 1))) { + return ${u}; + } + var data: array<${m}, 4> = array<${m}, 4>(0.0, 0.0, 0.0, 0.0); + for (var i: i32 = -1; i < 3; i++) { + var ${y}: ${m} = originalIdx + ${m}(i); + if (${y} < 0 || ${y} >= ${n[w]}) { + ${(()=>d?`coefs[i + 1] = 0.0; + continue;`:a?`return ${u};`:`${y} = max(0, min(${y}, ${n[w]} - 1));`)()}; + } + var input_indices_copy: ${e.type.indices} = input_indices; + ${e.indicesSet("input_indices_copy",w,`u32(${y})`)}; + data[i + 1] = ${w===p?e.getByIndices("input_indices_copy"):"rowCubicInterpolation(input_indices_copy, output_indices)"}; + } + return cubicInterpolation1D(data, coefs); + }`};return` + ${f(p)}; + ${f(h)}; + fn getCubicInterpolationCoefs(s: ${m}) -> array<${m}, 4> { + var absS = abs(s); + var coeffs: array<${m}, 4> = array<${m}, 4>(0.0, 0.0, 0.0, 0.0); + var oneMinusAbsS: ${m} = 1.0 - absS; + var twoMinusAbsS: ${m} = 2.0 - absS; + var onePlusAbsS: ${m} = 1.0 + absS; + coeffs[0] = ((${s} * onePlusAbsS - 5 * ${s}) * onePlusAbsS + 8 * ${s}) * onePlusAbsS - 4 * ${s}; + coeffs[1] = ((${s} + 2) * absS - (${s} + 3)) * absS * absS + 1; + coeffs[2] = ((${s} + 2) * oneMinusAbsS - (${s} + 3)) * oneMinusAbsS * oneMinusAbsS + 1; + coeffs[3] = ((${s} * twoMinusAbsS - 5 * ${s}) * twoMinusAbsS + 8 * ${s}) * twoMinusAbsS - 4 * ${s}; + return coeffs; + } + + fn cubicInterpolation1D(x: array<${m}, 4>, coefs: array<${m}, 4>) -> ${m} { + var coefsSum: ${m} = 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}) -> ${m} { + var input_indices: ${e.type.indices} = output_indices; + return colCubicInterpolation(input_indices, output_indices); + } + `},lc=(e,t,n,r,o)=>{let[s,a,u,d,l]=n.length===3?[-1,0,1,2,-1]:[0,2,3,4,1],c=e.type.value;return` + fn getInputValue(batch: u32, channel: u32, depth:u32, height: u32, width: u32) -> ${c} { + var input_indices: ${e.type.indices}; + ${e.indicesSet("input_indices",a,`max(0, min(depth, ${n[a]} - 1))`)}; + ${e.indicesSet("input_indices",u,`max(0, min(height, ${n[u]} - 1))`)}; + ${e.indicesSet("input_indices",d,`max(0, min(width, ${n[d]} - 1))`)}; + ${Ca(e,l,s,3)} + return ${e.getByIndices("input_indices")}; + } + + fn trilinearInterpolation(output_indices: ${t.type.indices}) -> ${c} { + var originalIndices = calculateOriginalIndicesFromOutputIndices(output_indices); + var depth:${c} = originalIndices[${a}]; + var height:${c} = originalIndices[${u}]; + var width:${c} = originalIndices[${d}]; + ${r?`if (depth < 0 || depth > (${n[a]} - 1) || height < 0 || height > (${n[u]} - 1) || width < 0 || (width > ${n[d]} - 1)) { + return ${o}; + }`:""}; + + depth = max(0, min(depth, ${n[a]} - 1)); + height = max(0, min(height, ${n[u]} - 1)); + width = max(0, min(width, ${n[d]} - 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 = ${n.length>3?`u32(originalIndices[${l}])`:"0"}; + var batch: u32 = ${n.length>3?`u32(originalIndices[${s}])`:"0"}; + + var x111: ${c} = getInputValue(batch, channel, depth1, height1, width1); + var x112: ${c} = getInputValue(batch, channel, depth1, height1, width2); + var x121: ${c} = getInputValue(batch, channel, depth1, height2, width1); + var x122: ${c} = getInputValue(batch, channel, depth1, height2, width2); + var x211: ${c} = getInputValue(batch, channel, depth2, height1, width1); + var x212: ${c} = getInputValue(batch, channel, depth2, height1, width2); + var x221: ${c} = getInputValue(batch, channel, depth2, height2, width1); + var x222: ${c} = getInputValue(batch, channel, depth2, height2, width2); + var dx1: ${c} = abs(depth - ${c}(depth1)); + var dx2: ${c} = abs(${c}(depth2) - depth); + var dy1: ${c} = abs(height - ${c}(height1)); + var dy2: ${c} = abs(${c}(height2) - height); + var dz1: ${c} = abs(width - ${c}(width1)); + var dz2: ${c} = abs(${c}(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); + }`},cc=(e,t,n,r,o,i)=>{let s=e.dims,a=nc(i,t.axes,s.length),u=rc(s,r,o,t.axes),d=r.slice();r.length===0&&(d=s.map((g,b)=>g===0?1:u[b]/g),t.keepAspectRatioPolicy!=="stretch"&&(u=oc(s,d,t)));let l=C("output",e.dataType,u.length),c=S("input",e.dataType,s.length),p=x.size(u),h=s.length===u.length&&s.every((g,b)=>g===u[b]),m=t.coordinateTransformMode==="tf_crop_and_resize",f=t.extrapolationValue,w=c.type.value,y=g=>` + ${h?"":` + ${ec(t.coordinateTransformMode,w)}; + ${(()=>{switch(t.mode){case"nearest":return` + ${ac(c,s)}; + ${tc(t.nearestMode,n,w)}; + ${sc(c,l,s,u,d.length,a.length,m)}; + `;case"linear":return` + ${ic(l,s,u,d.length,a.length)}; + ${(()=>{if(s.length===2||s.length===4)return`${uc(c,l,s,m,f)}`;if(s.length===3||s.length===5)return`${lc(c,l,s,m,f)}`;throw Error("Linear mode only supports input dims 2, 3, 4 and 5 are supported in linear mode.")})()}; + `;case"cubic":return` + ${(()=>{if(s.length===2||s.length===4)return`${dc(c,l,s,u,d,a,t.cubicCoeffA,m,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")}})()}; + `} + ${g.registerUniform("output_size","u32").registerUniform("scales","f32",d.length).registerUniform("roi","f32",a.length).declareVariables(c,l)} + ${g.mainStart()} + ${g.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} + ${h?"output[global_idx] = input[global_idx];":` + let output_indices = ${l.offsetToIndices("global_idx")}; + var input_indices: ${c.type.indices}; + ${(()=>{switch(t.mode){case"nearest":return`input_indices = calculateInputIndicesFromOutputIndices(output_indices); + if (checkInputIndices(input_indices)) { + output[global_idx] = ${c.getByIndices("input_indices")}; + } else { + output[global_idx] = ${t.extrapolationValue}; + }`;case"linear":return`output[global_idx] = ${s.length===2||s.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}|${n}|${d.length>0?d:""}|${o.length>0?o:""}|${a.length>0?a:""}|${h}|${s}`,inputDependencies:["rank"]},getShaderSource:y,getRunData:()=>({outputs:[{dims:u,dataType:e.dataType}],dispatchGroup:{x:Math.ceil(p/64)},programUniforms:[{type:12,data:p},{type:1,data:d},{type:1,data:a},...A(s,u)]})}},pc=e=>{let t=e.customDataBuffer;return new Uint32Array(t,t.byteOffset,1)[0]},Aa=(e,t)=>{let n=[],r=[],o=[],i=pc(e);if(t.antialias!==0)throw Error("Only default value (0) for Antialias attribute is supported");Jl(e.inputs,t,i,n,r,o),e.compute(cc(e.inputs[0],t,i,n,r,o),{inputs:[0]})},ka=e=>{let t=e.antialias,n=e.axes,r=e.coordinateTransformMode,o=e.cubicCoeffA,i=e.excludeOutside!==0,s=e.extrapolationValue,a=e.keepAspectRatioPolicy,u=e.mode,d=e.nearestMode===""?"simple":e.nearestMode;return V({antialias:t,axes:n,coordinateTransformMode:r,cubicCoeffA:o,excludeOutside:i,extrapolationValue:s,keepAspectRatioPolicy:a,mode:u,nearestMode:d})}});var mc,fc,Pa,za=k(()=>{"use strict";U();N();ae();W();mc=(e,t)=>{let[n,r,o,i]=e,{numHeads:s,rotaryEmbeddingDim:a}=t;if(n.dims.length!==3&&n.dims.length!==4)throw new Error(`Input 'x' is expected to have 3 or 4 dimensions, got ${n.dims.length}`);if(!x.areEqual(r.dims,[])&&!x.areEqual(r.dims,[1])&&r.dims.length!==2)throw new Error(`Input 'position_ids' is expected to have 0, 1, or 2 dimensions, got ${r.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(!x.areEqual(o.dims,i.dims))throw new Error("Inputs 'cos_cache' and 'sin_cache' are expected to have the same shape");if(a>0&&s===0)throw new Error("num_heads must be provided if rotary_embedding_dim is specified");let u=n.dims[0],d=n.dims[n.dims.length-2],l=o.dims[0],c=x.sizeFromDimension(n.dims,1)/d,p=a===0?o.dims[1]*2:c/s;if(a>p)throw new Error("rotary_embedding_dim must be less than or equal to head_size");if(r.dims.length===2){if(u!==r.dims[0])throw new Error(`Input 'position_ids' dimension 0 should be of size batch_size, got ${r.dims[0]}`);if(d!==r.dims[1])throw new Error(`Input 'position_ids' dimension 1 should be of size sequence_length, got ${r.dims[1]}`)}if(p/2!==o.dims[1]&&a/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(d>l)throw new Error("Updating cos_cache and sin_cache in RotaryEmbedding is not currently supported")},fc=(e,t)=>{let{interleaved:n,numHeads:r,rotaryEmbeddingDim:o,scale:i}=t,s=e[0].dims[0],a=x.sizeFromDimension(e[0].dims,1),u=e[0].dims[e[0].dims.length-2],d=a/u,l=e[2].dims[1],c=o===0?l*2:d/r,p=new Array(s,u,d/c,c-l),h=x.computeStrides(p),m=[{type:1,data:i},{type:12,data:p},{type:12,data:h},...e[0].dims.length===3?new Array({type:12,data:[a,d,c,1]}):[],...e[0].dims.length===4?new Array({type:12,data:[a,c,u*c,1]}):[],...A(e[0].dims,e[1].dims,e[2].dims,e[3].dims,e[0].dims)],f=w=>{let y=S("input",e[0].dataType,e[0].dims.length),g=S("position_ids",e[1].dataType,e[1].dims.length),b=S("cos_cache",e[2].dataType,e[2].dims.length),_=S("sin_cache",e[3].dataType,e[3].dims.length),$=C("output",e[0].dataType,e[0].dims.length);return w.registerUniforms([{name:"scale",type:"f32"},{name:"global_shape",type:"u32",length:p.length},{name:"global_strides",type:"u32",length:h.length},{name:"input_output_strides",type:"u32",length:h.length}]),` + ${w.declareVariables(y,g,b,_,$)} + + ${w.mainStart(je)} + let half_rotary_emb_dim = uniforms.${b.name}_shape[1]; + let bsnh = global_idx / uniforms.global_strides % uniforms.global_shape; + let size = uniforms.global_shape[0] * uniforms.global_strides[0]; + ${w.guardAgainstOutOfBoundsWorkgroupSizes("size")} + + if (bsnh[3] < half_rotary_emb_dim) { + let position_ids_idx = + ${g.broadcastedIndicesToOffset("bsnh.xy",C("",g.type.tensor,2))}; + let position_id = + u32(${g.getByOffset("position_ids_idx")}) + select(0, bsnh[1], position_ids_idx == 0); + let i = dot(bsnh, uniforms.input_output_strides) + select(0, bsnh[3], ${n}); + let j = i + select(half_rotary_emb_dim, 1, ${n}); + let re = ${y.getByOffset("i")} * ${b.get("position_id","bsnh[3]")} - + ${y.getByOffset("j")} * ${_.get("position_id","bsnh[3]")}; + ${$.setByOffset("i","re")} + let im = ${y.getByOffset("i")} * ${_.get("position_id","bsnh[3]")} + + ${y.getByOffset("j")} * ${b.get("position_id","bsnh[3]")}; + ${$.setByOffset("j","im")} + } else { + let k = dot(bsnh, uniforms.input_output_strides) + half_rotary_emb_dim; + ${$.setByOffset("k",y.getByOffset("k"))} + } + }`};return{name:"RotaryEmbedding",shaderCache:{hint:V({interleaved:n}).cacheKey,inputDependencies:["rank","rank","rank","rank"]},getShaderSource:f,getRunData:()=>({outputs:[{dims:e[0].dims,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(x.size(p)/je)},programUniforms:m})}},Pa=(e,t)=>{mc(e.inputs,t),e.compute(fc(e.inputs,t))}});var hc,gc,Ba,Da=k(()=>{"use strict";U();N();W();hc=e=>{if(!e||e.length<3)throw new Error("layerNorm requires at least 3 inputs.");let t=e[0],n=e[1],r=e[2];if(t.dataType!==n.dataType||t.dataType!==r.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(n.dims.length!==3&&n.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(n.dims[n.dims.length-1]!==o)throw new Error("Skip must have the same hidden size as input");if(n.dims[n.dims.length-2]!==i)throw new Error("Skip must have the same sequence length as input");if(r.dims.length!==1)throw new Error("Gamma must be 1D");if(r.dims[r.dims.length-1]!==o)throw new Error("Gamma must have the same hidden size as input");if(e.length>3){let s=e[3];if(s.dims.length!==1)throw new Error("Beta must be 1D");if(s.dims[s.dims.length-1]!==o)throw new Error("Beta must have the same hidden size as input")}if(e.length>4){let s=e[4];if(s.dims.length!==1)throw new Error("Bias must be 1D");if(s.dims[s.dims.length-1]!==o)throw new Error("Bias must have the same hidden size as input")}},gc=(e,t,n,r)=>{let o=t.simplified,i=e[0].dims,s=x.size(i),a=i,u=s,d=i.slice(-1)[0],l=r?i.slice(0,-1).concat(1):[],c=!o&&e.length>3,p=e.length>4,h=r&&n>1,m=r&&n>2,f=n>3,w=64,y=te(d),g=[{type:12,data:u},{type:12,data:y},{type:12,data:d},{type:1,data:t.epsilon}],b=$=>{let v=[{name:"output_size",type:"u32"},{name:"components",type:"u32"},{name:"hidden_size",type:"u32"},{name:"epsilon",type:"f32"}],I=[S("x",e[0].dataType,e[0].dims,y),S("skip",e[1].dataType,e[1].dims,y),S("gamma",e[2].dataType,e[2].dims,y)];c&&I.push(S("beta",e[3].dataType,e[3].dims,y)),p&&I.push(S("bias",e[4].dataType,e[4].dims,y)),I.push(C("output",e[0].dataType,a,y)),h&&I.push(C("mean_output",1,l)),m&&I.push(C("inv_std_output",1,l)),f&&I.push(C("input_skip_bias_sum",e[0].dataType,a,y));let T=J(e[0].dataType),P=J(1,y);return` + + ${$.registerUniforms(v).declareVariables(...I)} + var sum_shared : array<${P}, ${w}>; + var sum_squared_shared : array<${P}, ${w}>; + + ${$.mainStart([w,1,1])} + let ix = local_id.x; + let iy = global_id.x / ${w}; + + let hidden_size_vectorized: u32 = uniforms.hidden_size / uniforms.components; + var stride = hidden_size_vectorized / ${w}; + let offset = ix * stride + iy * hidden_size_vectorized; + let offset1d = stride * ix; + if (ix == ${w-1}) { + stride = hidden_size_vectorized - stride * ix; + } + for (var i: u32 = 0; i < stride; i++) { + let skip_value = skip[offset + i]; + let bias_value = ${p?"bias[offset1d + i]":T+"(0.0)"}; + let input_value = x[offset + i]; + let value = input_value + skip_value + bias_value; + ${f?"input_skip_bias_sum[offset + i] = value;":""} + output[offset + i] = value; + let f32_value = ${Xe(T,y,"value")}; + sum_shared[ix] += f32_value; + sum_squared_shared[ix] += f32_value * f32_value; + } + workgroupBarrier(); + + var reduce_size : u32 = ${w}; + 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 = ${ke("sum",y)} / f32(uniforms.hidden_size); + let inv_std_dev = inverseSqrt(${ke("square_sum",y)} / f32(uniforms.hidden_size) ${o?"":"- mean * mean"} + uniforms.epsilon); + ${h?"mean_output[global_idx] = mean;":""} + ${m?"inv_std_output[global_idx] = inv_std_dev;":""} + + for (var i: u32 = 0; i < stride; i++) { + output[offset + i] = (output[offset + i] ${o?"":`- ${T}(mean)`}) * + ${T}(inv_std_dev) * gamma[offset1d + i] + ${c?"+ beta[offset1d + i]":""}; + } + }`},_=[{dims:a,dataType:e[0].dataType}];return n>1&&_.push({dims:l,dataType:1}),n>2&&_.push({dims:l,dataType:1}),n>3&&_.push({dims:i,dataType:e[0].dataType}),{name:"SkipLayerNormalization",shaderCache:{hint:`${y};${h};${m};${f}`,inputDependencies:e.map(($,v)=>"type")},getShaderSource:b,getRunData:()=>({outputs:_,dispatchGroup:{x:Math.ceil(u/d)},programUniforms:g})}},Ba=(e,t)=>{hc(e.inputs);let r=[0];e.outputCount>1&&r.push(-3),e.outputCount>2&&r.push(-3),e.outputCount>3&&r.push(3),e.compute(gc(e.inputs,t,e.outputCount,!1),{outputs:r})}});var yc,sn,bc,Oa,wc,_c,Ra,Ma,Ua=k(()=>{"use strict";U();N();ae();W();yc=(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((n,r)=>{if(e[r+1].dataType!==6&&e[r+1].dataType!==7)throw new Error(`Input ${r} must be an array of int32 or int64`)})},sn=(e,t)=>{let n=[];if(e.length>t)if(e[t].dataType===7)e[t].getBigInt64Array().forEach(r=>n.push(Number(r)));else if(e[t].dataType===6)e[t].getInt32Array().forEach(r=>n.push(Number(r)));else throw new Error(`Input ${t} must be an array of int32 or int64`);return n},bc=(e,t)=>{if(e.length>1){let n=sn(e,1),r=sn(e,2),o=sn(e,3);return o.length===0&&(o=[...Array(e[0].dims.length).keys()]),V({starts:n,ends:r,axes:o})}else return t},Oa=(e,t,n,r,o)=>{let i=e;return e<0&&(i+=n[r[t]]),o[t]<0?Math.max(0,Math.min(i,n[r[t]]-1)):Math.max(0,Math.min(i,n[r[t]]))},wc=(e,t,n)=>`fn calculateInputIndices(output_indices: ${t.type.indices}) -> ${e.type.indices} { + var input_indices: ${e.type.indices}; + var carry = 0u; + for (var i = ${n.length}; i >= 0; i--) { + let input_shape_i = ${B("uniforms.input_shape","i",n.length)}; + let steps_i = ${B("uniforms.steps","i",n.length)}; + let signs_i = ${B("uniforms.signs","i",n.length)}; + let starts_i = ${B("uniforms.starts","i",n.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; + }`,_c=(e,t)=>{let n=e[0].dims,r=x.size(n),o=t.axes.length>0?x.normalizeAxes(t.axes,n.length):[...Array(n.length).keys()],i=sn(e,4);i.forEach(y=>y!==0||(()=>{throw new Error("step cannot be 0")})),i.length===0&&(i=Array(o.length).fill(1));let s=t.starts.map((y,g)=>Oa(y,g,n,o,i)),a=t.ends.map((y,g)=>Oa(y,g,n,o,i));if(o.length!==s.length||o.length!==a.length)throw new Error("start, ends and axes should have the same number of elements");if(o.length!==n.length)for(let y=0;yMath.sign(y));i.forEach((y,g,b)=>{if(y<0){let _=(a[g]-s[g])/y,$=s[g],v=$+_*i[g];s[g]=v,a[g]=$,b[g]=-y}});let d=n.slice(0);o.forEach((y,g)=>{d[y]=Math.ceil((a[y]-s[y])/i[y])});let l={dims:d,dataType:e[0].dataType},c=C("output",e[0].dataType,d.length),p=S("input",e[0].dataType,e[0].dims.length),h=x.size(d),m=[{name:"outputSize",type:"u32"},{name:"starts",type:"u32",length:s.length},{name:"signs",type:"i32",length:u.length},{name:"steps",type:"u32",length:i.length}],f=[{type:12,data:h},{type:12,data:s},{type:6,data:u},{type:12,data:i},...A(e[0].dims,d)],w=y=>` + ${y.registerUniforms(m).declareVariables(p,c)} + ${wc(p,c,n)} + ${y.mainStart()} + ${y.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.outputSize")} + let output_indices = ${c.offsetToIndices("global_idx")}; + let input_indices = calculateInputIndices(output_indices); + ${c.setByOffset("global_idx",p.getByIndices("input_indices"))} + }`;return{name:"Slice",shaderCache:{hint:`${u.length}_${s.length}_${i.length}`,inputDependencies:["rank"]},getShaderSource:w,getRunData:()=>({outputs:[l],dispatchGroup:{x:Math.ceil(r/64)},programUniforms:f})}},Ra=(e,t)=>{yc(e.inputs,t);let n=bc(e.inputs,t);e.compute(_c(e.inputs,n),{inputs:[0]})},Ma=e=>{let t=e.starts,n=e.ends,r=e.axes;return V({starts:t,ends:n,axes:r})}});var $c,vc,Va,La,Na=k(()=>{"use strict";U();N();ae();Me();W();$c=e=>{if(!e||e.length!==1)throw new Error("Softmax op requires 1 input.")},vc=(e,t)=>{let n=e.inputs[0],r=n.dims,o=x.size(r),i=64,s=r.length,a=x.normalizeAxis(t.axis,s),u=aT),l[a]=s-1,l[s-1]=a,d=e.compute(le(n,l),{inputs:[n],outputs:[-1]})[0]):d=n;let c=d.dims,p=c[s-1],h=o/p,m=te(p),f=p/m,w=(I,T)=>T===4?`max(max(${I}.x, ${I}.y), max(${I}.z, ${I}.w))`:T===2?`max(${I}.x, ${I}.y)`:T===3?`max(max(${I}.x, ${I}.y), ${I}.z)`:I,y=S("x",d.dataType,d.dims,m),g=C("result",d.dataType,d.dims,m),b=y.type.value,_=J(d.dataType)==="f32"?`var threadMax = ${b}(-3.402823e+38f);`:`var threadMax = ${b}(-65504.0h);`,$=I=>` + var rowMaxShared : ${b}; + var rowSumShared : ${b}; + var threadShared : array<${b}, ${i}>; + + fn getValue(row: i32, col: i32, row_stride: i32) -> ${b} { + let index = row * row_stride + col; + return x[index]; + } + + fn setValue(row: i32, col: i32, row_stride: i32, value: ${b}) { + let index = row * row_stride + col; + result[index] = value; + } + ${I.registerUniform("packedCols","i32").declareVariables(y,g)} + ${I.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 + ${_} + 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 = ${b}(${w("threadShared[0]",m)}); + } + workgroupBarrier(); + + // find the rows sum + var threadSum = ${b}(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 = ${b}(${ke("threadShared[0]",m)}); + } + 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); + } + }`,v=e.compute({name:"Softmax",shaderCache:{hint:`${m}`,inputDependencies:["type"]},getRunData:()=>({outputs:[{dims:c,dataType:d.dataType}],dispatchGroup:{x:h},programUniforms:[{type:6,data:f}]}),getShaderSource:$},{inputs:[d],outputs:[u?-1:0]})[0];u&&e.compute(le(v,l),{inputs:[v]})},Va=(e,t)=>{$c(e.inputs),vc(e,t)},La=e=>V({axis:e.axis})});var xc,Sc,Ic,Tc,Cc,Wa,Ga,Ha=k(()=>{"use strict";U();N();ae();W();xc=e=>{if(!e||e.length<1)throw new Error("too few inputs")},Sc=(e,t)=>{let n=[],r=t.numOutputs;return e[1].dims[0]>0&&(e[1].getBigInt64Array().forEach(o=>n.push(Number(o))),r=n.length),V({numOutputs:r,axis:t.axis,splitSizes:n})},Ic=e=>` +fn calculateOutputIndex(index: u32) -> u32 { + for (var i: u32 = 0u; i < ${e}u; i += 1u ) { + if (index < ${B("uniforms.size_in_split_axis","i",e)}) { + return i; + } + } + return ${e}u; +}`,Tc=e=>{let t=e.length,n=[];for(let r=0;r{let n=e[0].dims,r=x.size(n),o=e[0].dataType,i=x.normalizeAxis(t.axis,n.length),s=new Array(t.numOutputs),a=S("input",o,n.length),u=new Array(t.numOutputs),d=[],l=[],c=0,p=[{type:12,data:r}];for(let m=0;m` + ${m.registerUniform("input_size","u32").registerUniform("size_in_split_axis","u32",u.length).declareVariables(a,...s)} + ${Ic(u.length)} + ${Tc(s)} + + ${m.mainStart()} + ${m.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.input_size")} + + var indices = ${a.offsetToIndices("global_idx")}; + var index = ${a.indicesGet("indices",i)}; + let output_number = calculateOutputIndex(index); + if (output_number != 0) { + index -= ${B("uniforms.size_in_split_axis","output_number - 1u",u.length)}; + ${a.indicesSet("indices",i,"index")}; + } + writeBufferData(output_number, indices, global_idx); + }`;return{name:"Split",shaderCache:{hint:t.cacheKey,inputDependencies:["rank"]},getShaderSource:h,getRunData:()=>({outputs:d,dispatchGroup:{x:Math.ceil(r/64)},programUniforms:p})}},Wa=(e,t)=>{xc(e.inputs);let n=e.inputs.length===1?t:Sc(e.inputs,t);e.compute(Cc(e.inputs,n),{inputs:[0]})},Ga=e=>{let t=e.axis,n=e.splitSizes,r=e.numOutputs<0?n.length:e.numOutputs;if(r!==n.length)throw new Error("numOutputs and splitSizes lengh must be equal");return V({axis:t,numOutputs:r,splitSizes:n})}});var Ac,kc,qa,Fa=k(()=>{"use strict";U();N();W();Ac=(e,t,n,r,o)=>{let i=C("output_data",o,n.length,4),s=S("a_data",t[1].dataType,t[1].dims.length,4),a=S("b_data",t[2].dataType,t[2].dims.length,4),u=S("c_data",t[0].dataType,t[0].dims.length,4),d,l=(c,p,h)=>`select(${p}, ${c}, ${h})`;if(!r)d=i.setByOffset("global_idx",l(s.getByOffset("global_idx"),a.getByOffset("global_idx"),u.getByOffset("global_idx")));else{let c=(p,h,m="")=>{let f=`a_data[index_a${h}][component_a${h}]`,w=`b_data[index_b${h}][component_b${h}]`,y=`bool(c_data[index_c${h}] & (0xffu << (component_c${h} * 8)))`;return` + let output_indices${h} = ${i.offsetToIndices(`global_idx * 4u + ${h}u`)}; + let offset_a${h} = ${s.broadcastedIndicesToOffset(`output_indices${h}`,i)}; + let offset_b${h} = ${a.broadcastedIndicesToOffset(`output_indices${h}`,i)}; + let offset_c${h} = ${u.broadcastedIndicesToOffset(`output_indices${h}`,i)}; + let index_a${h} = offset_a${h} / 4u; + let index_b${h} = offset_b${h} / 4u; + let index_c${h} = offset_c${h} / 4u; + let component_a${h} = offset_a${h} % 4u; + let component_b${h} = offset_b${h} % 4u; + let component_c${h} = offset_c${h} % 4u; + ${p}[${h}] = ${m}(${l(f,w,y)}); + `};o===9?d=` + var data = vec4(0); + ${c("data",0,"u32")} + ${c("data",1,"u32")} + ${c("data",2,"u32")} + ${c("data",3,"u32")} + output_data[global_idx] = dot(vec4(0x1, 0x100, 0x10000, 0x1000000), vec4(data));`:d=` + ${c("output_data[global_idx]",0)} + ${c("output_data[global_idx]",1)} + ${c("output_data[global_idx]",2)} + ${c("output_data[global_idx]",3)} + `}return` + ${e.registerUniform("vec_size","u32").declareVariables(u,s,a,i)} + ${e.mainStart()} + ${e.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.vec_size")} + ${d} + }`},kc=e=>{let t=e[1].dims,n=e[2].dims,r=e[0].dims,o=e[1].dataType,i=!(x.areEqual(t,n)&&x.areEqual(n,r)),s=t,a=x.size(t);if(i){let d=Ee.calcShape(Ee.calcShape(t,n,!1),r,!1);if(!d)throw new Error("Can't perform where op on the given tensors");s=d,a=x.size(s)}let u=Math.ceil(a/4);return{name:"Where",shaderCache:{inputDependencies:["rank","rank","rank"]},getShaderSource:d=>Ac(d,e,s,i,o),getRunData:()=>({outputs:[{dims:s,dataType:o}],dispatchGroup:{x:Math.ceil(a/64/4)},programUniforms:[{type:12,data:u},...A(r,t,n,s)]})}},qa=e=>{e.compute(kc(e.inputs))}});var Ka,ja=k(()=>{"use strict";Vo();Xt();Wo();Ho();ki();Li();Gi();rs();ls();ms();gs();$s();Ss();Ts();ks();zs();Os();Us();js();Qs();Js();Hn();na();Jn();oa();$a();Sa();Ta();Kt();Ea();za();Da();Ua();Na();Ha();tr();Me();Qt();Fa();Ka=new Map([["Abs",[qo]],["Acos",[Fo]],["Acosh",[Ko]],["Add",[Ei]],["ArgMax",[Uo,Vn]],["ArgMin",[Mo,Vn]],["Asin",[jo]],["Asinh",[Xo]],["Atan",[Zo]],["Atanh",[Qo]],["Attention",[Lo]],["AveragePool",[pa,ca]],["BatchNormalization",[No]],["BiasAdd",[Go]],["BiasSplitGelu",[Ai]],["Cast",[Jo,Yo]],["Ceil",[ti]],["Clip",[ei]],["Concat",[Ni,Wi]],["Conv",[jn,Kn]],["ConvTranspose",[ds,us]],["Cos",[ni]],["Cosh",[ri]],["CumSum",[cs,ps]],["DepthToSpace",[fs,hs]],["DequantizeLinear",[va,xa]],["Div",[Pi]],["Einsum",[ws,_s]],["Elu",[oi,ct]],["Equal",[zi]],["Erf",[ii]],["Exp",[si]],["Expand",[xs]],["FastGelu",[Is]],["Floor",[ai]],["FusedConv",[jn,Kn]],["Gather",[As,Cs]],["GatherElements",[Ds,Bs]],["GatherBlockQuantized",[Es,Ps]],["Gelu",[ui]],["Gemm",[Ms,Rs]],["GlobalAveragePool",[ha,fa]],["GlobalMaxPool",[_a,wa]],["Greater",[Ri]],["GreaterOrEqual",[Ui]],["GroupQueryAttention",[Ks,Fs]],["HardSigmoid",[gi,hi]],["InstanceNormalization",[Zs]],["LayerNormalization",[Ys]],["LeakyRelu",[di,ct]],["Less",[Mi]],["LessOrEqual",[Vi]],["Log",[Ii]],["MatMul",[ts]],["MatMulNBits",[ea,ta]],["MaxPool",[ya,ba]],["Mul",[Bi]],["MultiHeadAttention",[Ns,Ls]],["Neg",[ci]],["Not",[li]],["Pad",[ra]],["Pow",[Di]],["QuickGelu",[Ti,ct]],["Range",[Ia]],["Reciprocal",[pi]],["ReduceMin",[Po]],["ReduceMean",[To]],["ReduceMax",[Eo]],["ReduceSum",[Bo]],["ReduceProd",[zo]],["ReduceL1",[Co]],["ReduceL2",[Ao]],["ReduceLogSum",[Oo]],["ReduceLogSumExp",[ko]],["ReduceSumSquare",[Do]],["Relu",[mi]],["Resize",[Aa,ka]],["RotaryEmbedding",[Pa]],["Sigmoid",[fi]],["Sin",[yi]],["Sinh",[bi]],["Slice",[Ra,Ma]],["SkipLayerNormalization",[Ba]],["Split",[Wa,Ga]],["Sqrt",[wi]],["Softmax",[Va,La]],["Sub",[Oi]],["Tan",[_i]],["Tanh",[vi]],["ThresholdedRelu",[Si,ct]],["Tile",[Gs]],["Transpose",[mo,fo]],["Where",[qa]]])});var an,Xa=k(()=>{"use strict";Se();Ae();W();an=class{constructor(t){this.backend=t;this.repo=new Map,this.attributesBound=!1}getArtifact(t){return this.repo.get(t)}setArtifact(t,n){this.repo.set(t,n)}run(t,n,r,o,i){be(t.programInfo.name);let s=this.backend.device,a=this.backend.getComputePassEncoder();this.backend.writeTimestamp(this.backend.pendingDispatchNumber*2);let u=[];for(let l of n)u.push({binding:u.length,resource:{buffer:l.buffer}});for(let l of r)u.push({binding:u.length,resource:{buffer:l.buffer}});i&&u.push({binding:u.length,resource:i});let d=s.createBindGroup({layout:t.computePipeline.getBindGroupLayout(0),entries:u,label:t.programInfo.name});if(this.backend.sessionStatus==="capturing"){let l={kernelId:this.backend.currentKernelId,computePipeline:t.computePipeline,bindGroup:d,dispatchGroup:o};this.backend.capturedCommandList.get(this.backend.currentSessionId).push(l)}a.setPipeline(t.computePipeline),a.setBindGroup(0,d),a.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(),ge(t.programInfo.name)}dispose(){}build(t,n){be(t.name);let r=this.backend.device,o=[];r.features.has("shader-f16")&&o.push("enable f16;");let i=co(n,this.backend.device.limits),s=t.getShaderSource(i),a=`${o.join(` +`)} +${i.additionalImplementations} +${s}`,u=r.createShaderModule({code:a,label:t.name});X("verbose",()=>`[WebGPU] ${t.name} shader code: ${a}`);let d=r.createComputePipeline({compute:{module:u,entryPoint:"main"},layout:"auto",label:t.name});return ge(t.name),{programInfo:t,computePipeline:d,uniformVariablesInfo:i.variablesInfo}}normalizeDispatchGroupSize(t){let n=typeof t=="number"?t:t.x,r=typeof t=="number"?1:t.y||1,o=typeof t=="number"?1:t.z||1,i=this.backend.device.limits.maxComputeWorkgroupsPerDimension;if(n<=i&&r<=i&&o<=i)return[n,r,o];let s=n*r*o,a=Math.ceil(Math.sqrt(s));if(a>i){if(a=Math.ceil(Math.cbrt(s)),a>i)throw new Error("Total dispatch size exceeds WebGPU maximum.");return[a,a,a]}else return[a,a,1]}}});var Ec,Pc,nr,un,Za=k(()=>{"use strict";Se();U();Ae();Cn();ao();ja();Xa();Ec=(e,t)=>{if(t.length!==e.length)throw new Error(`inputDependencies length ${t.length} is not equal to inputTensors length ${e.length}.`);let n=[];for(let r=0;r{let r=e.name;return e.shaderCache?.hint&&(r+="["+e.shaderCache.hint+"]"),r+=":"+n+`:${Ec(t,e.shaderCache?.inputDependencies??new Array(t.length).fill("dims"))}`,r},nr=class{constructor(t){t&&(this.architecture=t.architecture,this.vendor=t.vendor)}isArchitecture(t){return this.architecture===t}isVendor(t){return this.vendor===t}},un=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,n){this.env=t;let r=[],o={requiredLimits:{maxComputeWorkgroupStorageSize:n.limits.maxComputeWorkgroupStorageSize,maxComputeWorkgroupsPerDimension:n.limits.maxComputeWorkgroupsPerDimension,maxStorageBufferBindingSize:n.limits.maxStorageBufferBindingSize,maxBufferSize:n.limits.maxBufferSize,maxComputeInvocationsPerWorkgroup:n.limits.maxComputeInvocationsPerWorkgroup,maxComputeWorkgroupSizeX:n.limits.maxComputeWorkgroupSizeX,maxComputeWorkgroupSizeY:n.limits.maxComputeWorkgroupSizeY,maxComputeWorkgroupSizeZ:n.limits.maxComputeWorkgroupSizeZ},requiredFeatures:r};n.features.has("chromium-experimental-timestamp-query-inside-passes")?r.push("chromium-experimental-timestamp-query-inside-passes"):n.features.has("timestamp-query")&&r.push("timestamp-query"),n.features.has("shader-f16")&&r.push("shader-f16"),this.device=await n.requestDevice(o),this.adapterInfo=new nr(n.info||await n.requestAdapterInfo()),this.gpuDataManager=so(this),this.programManager=new an(this),this.kernels=new Map,this.kernelPersistentData=new Map,this.kernelCustomData=new Map,Wt(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:n,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(),n={};this.queryType==="at-passes"&&(n.timestampWrites={querySet:this.querySet,beginningOfPassWriteIndex:this.pendingDispatchNumber*2,endOfPassWriteIndex:this.pendingDispatchNumber*2+1}),this.computePassEncoder=t.beginComputePass(n)}return this.computePassEncoder}endComputePass(){this.computePassEncoder&&(this.computePassEncoder.end(),this.computePassEncoder=null)}flush(){if(!this.commandEncoder)return;be(),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 n=new BigUint64Array(t.getMappedRange()),r=this.pendingQueries.get(t);for(let o=0;o"u"&&(this.queryTimeBase=h);let f=Number(h-this.queryTimeBase),w=Number(m-this.queryTimeBase);if(!Number.isSafeInteger(f)||!Number.isSafeInteger(w))throw new RangeError("incorrect timestamp range");if(this.env.webgpu.profiling?.ondata)this.env.webgpu.profiling.ondata({version:1,inputsMetadata:c.map(y=>({dims:y.dims,dataType:We(y.dataType)})),outputsMetadata:p.map(y=>({dims:y.dims,dataType:We(y.dataType)})),kernelId:s,kernelType:u,kernelName:d,programName:l,startTime:f,endTime:w});else{let y="";c.forEach((b,_)=>{y+=`input[${_}]: [${b.dims}] | ${We(b.dataType)}, `});let g="";p.forEach((b,_)=>{g+=`output[${_}]: [${b.dims}] | ${We(b.dataType)}, `}),console.log(`[profiling] kernel "${s}|${u}|${d}|${l}" ${y}${g}execution time: ${w-f} ns`)}ot("GPU",`${l}::${h}::${m}`)}t.unmap(),this.pendingQueries.delete(t)}),ge()}run(t,n,r,o,i,s){be(t.name);let a=[];for(let b=0;b_):r;if(c.length!==u.length)throw new Error(`Output size ${c.length} must be equal to ${u.length}.`);let p=[],h=[];for(let b=0;b=s)throw new Error(`Invalid output index: ${c[b]}`);if(c[b]===-3)continue;let _=c[b]===-1,$=c[b]===-2,v=_||$?i(u[b].dataType,u[b].dims):o(c[b],u[b].dataType,u[b].dims);if(p.push(v),v.data===0)continue;let I=this.gpuDataManager.get(v.data);if(!I)throw new Error(`no GPU data for output: ${v.data}`);if(_&&this.temporaryData.push(I),$){let T=this.kernelPersistentData.get(this.currentKernelId);T||(T=[],this.kernelPersistentData.set(this.currentKernelId,T)),T.push(I)}h.push(I)}if(a.length!==n.length||h.length!==p.length){if(h.length===0)return ge(t.name),p;throw new Error(`Program ${t.name} has zero-sized tensor(s) in inputs or outputs. This is not supported now.`)}let m;if(l){let b=0,_=[];l.forEach(T=>{let P=typeof T.data=="number"?[T.data]:T.data;if(P.length===0)return;let z=T.type===10?2:4,R,Z;T.type===10?(Z=P.length>4?16:P.length>2?8:P.length*z,R=P.length>4?16:z*P.length):(Z=P.length<=2?P.length*z:16,R=16),b=Math.ceil(b/Z)*Z,_.push(b);let G=T.type===10?8:4;b+=P.length>4?Math.ceil(P.length/G)*R:P.length*z});let $=16;b=Math.ceil(b/$)*$;let v=new ArrayBuffer(b);l.forEach((T,P)=>{let z=_[P],R=typeof T.data=="number"?[T.data]:T.data;if(T.type===6)new Int32Array(v,z,R.length).set(R);else if(T.type===12)new Uint32Array(v,z,R.length).set(R);else if(T.type===10)new Uint16Array(v,z,R.length).set(R);else if(T.type===1)new Float32Array(v,z,R.length).set(R);else throw new Error(`Unsupported uniform type: ${We(T.type)}`)});let I=this.gpuDataManager.create(b,GPUBufferUsage.COPY_DST|GPUBufferUsage.UNIFORM);this.device.queue.writeBuffer(I.buffer,0,v,0,b),this.gpuDataManager.release(I.id),m={offset:0,size:b,buffer:I.buffer}}let f=this.programManager.normalizeDispatchGroupSize(d),w=f[1]===1&&f[2]===1,y=Pc(t,n,w),g=this.programManager.getArtifact(y);if(g||(g=this.programManager.build(t,f),this.programManager.setArtifact(y,g),X("info",()=>`[artifact] key: ${y}, programName: ${t.name}`)),l&&g.uniformVariablesInfo){if(l.length!==g.uniformVariablesInfo.length)throw new Error(`Uniform variables count mismatch: expect ${g.uniformVariablesInfo.length}, got ${l.length} in program "${g.programInfo.name}".`);for(let b=0;b`[ProgramManager] run "${t.name}" (key=${y}) with ${f[0]}x${f[1]}x${f[2]}`),this.queryType!=="none"||this.sessionStatus==="capturing"){let b={kernelId:this.currentKernelId,programName:g.programInfo.name,inputTensorViews:n,outputTensorViews:p};this.pendingKernels.push(b),this.sessionStatus==="capturing"&&this.capturedPendingKernels.get(this.currentSessionId).push(b)}return this.programManager.run(g,a,h,f,m),ge(t.name),p}upload(t,n){this.gpuDataManager.upload(t,n)}memcpy(t,n){this.gpuDataManager.memcpy(t,n)}async download(t,n){await this.gpuDataManager.download(t,n)}alloc(t){return this.gpuDataManager.create(t).id}free(t){return this.gpuDataManager.release(t)}createKernel(t,n,r,o){let i=Ka.get(t);if(!i)throw new Error(`kernel not implemented: ${t}`);let s={kernelType:t,kernelName:o,kernelEntry:i[0],attributes:[i[1],r]};this.kernels.set(n,s)}releaseKernel(t){let n=this.kernelPersistentData.get(t);if(n){for(let r of n)this.gpuDataManager.release(r.id);this.kernelPersistentData.delete(t)}this.kernelCustomData.delete(t),this.kernels.delete(t)}computeKernel(t,n,r){let o=this.kernels.get(t);if(!o)throw new Error(`kernel not created: ${t}`);let i=o.kernelType,s=o.kernelName,a=o.kernelEntry,u=o.attributes;if(this.currentKernelId!==null)throw new Error(`kernel "[${i}] ${s}" is not allowed to be called recursively`);this.currentKernelId=t,u[0]&&(u[1]=u[0](u[1]),u[0]=void 0),X("info",()=>`[WebGPU] Start to run kernel "[${i}] ${s}"...`);let d=this.env.debug;this.temporaryData=[];try{return d&&this.device.pushErrorScope("validation"),a(n,u[1]),0}catch(l){return r.push(Promise.resolve(`[WebGPU] Kernel "[${i}] ${s}" failed. ${l}`)),1}finally{d&&r.push(this.device.popErrorScope().then(l=>l?`GPU validation error for kernel "[${i}] ${s}": ${l.message}`:null));for(let l of this.temporaryData)this.gpuDataManager.release(l.id);this.temporaryData=[],this.currentKernelId=null}}registerBuffer(t,n,r,o){let i=this.sessionExternalDataMapping.get(t);i||(i=new Map,this.sessionExternalDataMapping.set(t,i));let s=i.get(n),a=this.gpuDataManager.registerExternalBuffer(r,o,s);return i.set(n,[a,r]),a}unregisterBuffers(t){let n=this.sessionExternalDataMapping.get(t);n&&(n.forEach(r=>this.gpuDataManager.unregisterExternalBuffer(r[0])),this.sessionExternalDataMapping.delete(t))}getBuffer(t){let n=this.gpuDataManager.get(t);if(!n)throw new Error(`no GPU data for buffer: ${t}`);return n.buffer}createDownloader(t,n,r){return async()=>{let o=await Pn(this,t,n);return Gt(o.buffer,r)}}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(){X("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(){X("info","captureEnd"),this.flush(),this.sessionStatus="default"}replay(){X("info","replay"),this.sessionStatus="replaying";let t=this.capturedCommandList.get(this.currentSessionId),n=this.capturedPendingKernels.get(this.currentSessionId),r=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"}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 zc,Qa,dn,rr,Ya,Ja=k(()=>{"use strict";Ae();zc=1,Qa=()=>zc++,dn=class{constructor(t,n){this.mlContext=t;this.tensorEntry=n,this.tensorCache=n?[n]:[]}get tensor(){return this.tensorEntry?.[0]}get context(){if(!this.mlContext)throw new Error("MLContext has not been set.");return this.mlContext}set context(t){if(this.mlContext&&this.mlContext!==t)throw new Error("MLTensor in use in a different MLContext.");this.mlContext=t}destroy(){for(let[t]of this.tensorCache)t.destroy();this.tensorCache=[],this.tensorEntry=void 0}trySelectTensor(t,n){for(let[r,o,i]of this.tensorCache)if(n===r){if(this.context!==t)throw new Error("MLTensor cannot be registered with a different MLContext.");return this.tensorEntry=[r,o,i],!0}return!1}async ensureTensor(t,n,r){if(this.tensorEntry){let[s,a,u]=this.tensorEntry;if(a===t&&u.every((d,l)=>d===n[l]))return s}for(let[s,a,u]of this.tensorCache)if(a===t&&u.every((d,l)=>d===n[l])){if(r&&this.tensorEntry){X("verbose",()=>`[WebNN] Slowdown may occur, having to copy existing tensor {dataType: ${t}, shape: ${n}}`);let d=await this.context.readTensor(this.tensorEntry[0]);this.context.writeTensor(s,d)}return this.tensorEntry=[s,a,u],s}X("verbose",()=>`[WebNN] MLContext.createTensor {dataType: ${t}, shape: ${n}}`);let o=MLTensorUsage.READ|MLTensorUsage.WRITE,i=await this.context.createTensor({dataType:t,shape:n,dimensions:n,usage:o});return this.tensorEntry=[i,t,n],this.tensorCache.push(this.tensorEntry),this.activeUpload&&(this.mlContext?.writeTensor(i,this.activeUpload),this.activeUpload=void 0),i}upload(t){if(!this.tensorEntry){this.activeUpload=new Uint8Array(t);return}this.mlContext?.writeTensor(this.tensorEntry[0],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.tensorEntry)throw new Error("Tensor has not been created.");return t?this.context.readTensor(this.tensorEntry[0],t):this.context.readTensor(this.tensorEntry[0])}},rr=class{constructor(t){this.backend=t;this.tensorsById=new Map;this.tensorIdsByContext=new Map}reserveTensorId(){let t=Qa();return this.tensorsById.set(t,new dn),t}releaseTensorId(t){let n=this.tensorsById.get(t);if(n){n.destroy(),this.tensorsById.delete(t);for(let[r,o]of this.tensorIdsByContext)if(o.has(t)){o.delete(t),o.size===0&&this.tensorIdsByContext.delete(r);break}}}async ensureTensor(t,n,r,o){X("verbose",()=>`[WebNN] TensorManager.ensureTensor {tensorId: ${t}, dataType: ${n}, shape: ${r}, copyOld: ${o}}`);let i=this.tensorsById.get(t);if(!i)throw new Error("Tensor not found.");return i.context=this.backend.currentContext,this.tensorIdsByContext.has(this.backend.currentContext)||this.tensorIdsByContext.set(this.backend.currentContext,new Set),this.tensorIdsByContext.get(this.backend.currentContext)?.add(t),i.ensureTensor(n,r,o)}upload(t,n){this.tensorsById.get(t).upload(n)}async download(t,n){return X("verbose",()=>`[WebNN] TensorManager.download {tensorId: ${t}, dstBuffer: ${n?.byteLength}}`),this.tensorsById.get(t).download(n)}releaseTensorsForContext(t){let n=this.tensorIdsByContext.get(t);if(n){for(let r of n)this.tensorsById.get(r).destroy(),this.tensorsById.delete(r);this.tensorIdsByContext.delete(t)}}registerTensor(t,n,r,o){for(let[a,u]of this.tensorsById)if(u.trySelectTensor(t,n))return a;let i=Qa();this.tensorsById.set(i,new dn(t,[n,r,o]));let s=this.tensorIdsByContext.get(t);return s||(s=new Set,this.tensorIdsByContext.set(t,s)),s.add(i),i}},Ya=(...e)=>new rr(...e)});var eu,ln,tu=k(()=>{"use strict";U();Ne();Cn();Ja();Ae();eu=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=Ya(this);this.mlContextBySessionId=new Map;this.sessionIdsByMLContext=new Map;Wt(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,n){this.mlContextBySessionId.set(t,n);let r=this.sessionIdsByMLContext.get(n);r||(r=new Set,this.sessionIdsByMLContext.set(n,r)),r.add(t)}onReleaseSession(t){let n=this.mlContextBySessionId.get(t);if(!n)return;this.mlContextBySessionId.delete(t);let r=this.sessionIdsByMLContext.get(n);r.delete(t),r.size===0&&(this.sessionIdsByMLContext.delete(n),this.tensorManager.releaseTensorsForContext(n))}getMLContext(t){return this.mlContextBySessionId.get(t)}reserveTensorId(){return this.tensorManager.reserveTensorId()}releaseTensorId(t){X("verbose",()=>`[WebNN] releaseTensorId {tensorId: ${t}}`),this.tensorManager.releaseTensorId(t)}async ensureTensor(t,n,r,o){let i=eu.get(n);if(!i)throw new Error(`Unsupported ONNX data type: ${n}`);return this.tensorManager.ensureTensor(t,i,r,o)}uploadTensor(t,n){if(!se().shouldTransferToMLTensor)throw new Error("Trying to upload to a MLTensor while shouldTransferToMLTensor is false");X("verbose",()=>`[WebNN] uploadTensor {tensorId: ${t}, data: ${n.byteLength}}`),this.tensorManager.upload(t,n)}async downloadTensor(t,n){return this.tensorManager.download(t,n)}createMLTensorDownloader(t,n){return async()=>{let r=await this.tensorManager.download(t);return Gt(r,n)}}registerMLTensor(t,n,r){let o=eu.get(n);if(!o)throw new Error(`Unsupported ONNX data type: ${n}`);let i=this.tensorManager.registerTensor(this.currentContext,t,o,r);return X("verbose",()=>`[WebNN] registerMLTensor {tensor: ${t}, dataType: ${o}, dimensions: ${r}} -> {tensorId: ${i}}`),i}flush(){}}});var nu={};nt(nu,{init:()=>Bc});var yt,or,Bc,ru=k(()=>{"use strict";U();Za();Ae();N();tu();yt=class e{constructor(t,n,r,o){this.module=t;this.dataType=n;this.data=r;this.dims=o}getFloat32Array(){if(this.dataType!==1)throw new Error("Invalid data type");let t=x.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=x.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=x.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=x.size(this.dims);return t===0?new Uint16Array:new Uint16Array(this.module.HEAP8.buffer,this.data,t)}reshape(t){if(x.size(t)!==x.size(this.dims))throw new Error("Invalid new shape");return new e(this.module,this.dataType,this.data,t)}},or=class{constructor(t,n,r){this.module=t;this.backend=n;this.customDataOffset=0;this.customDataSize=0;this.adapterInfo=n.adapterInfo;let o=t.HEAPU32,i=r>>>2;this.opKernelContext=o[i++];let s=o[i++];this.outputCount=o[i++],this.customDataOffset=o[i++],this.customDataSize=o[i++];let a=[];for(let u=0;utypeof a=="number"?this.inputs[a]:a)??this.inputs,o=n?.outputs??[],i=(a,u,d)=>new yt(this.module,u,this.output(a,d),d),s=(a,u)=>{let d=Fe(a,u);if(!d)throw new Error(`Unsupported data type: ${a}`);let l=d>0?this.backend.gpuDataManager.create(d).id:0;return new yt(this.module,a,l,u)};return this.backend.run(t,r,o,i,s,this.outputCount)}output(t,n){let r=this.module.stackSave();try{let o=this.module.stackAlloc((1+n.length)*4),i=o>>2;this.module.HEAPU32[i++]=n.length;for(let s=0;s{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 un;await i.initialize(n,r),o("webgpu",[i,s=>i.alloc(s),s=>i.free(s),(s,a,u,d=!1)=>{if(d)X("verbose",()=>`[WebGPU] jsepCopyGpuToGpu: src=${s}, dst=${a}, size=${u}`),i.memcpy(s,a);else{X("verbose",()=>`[WebGPU] jsepCopyCpuToGpu: dataOffset=${s}, gpuDataId=${a}, size=${u}`);let l=t.HEAPU8.subarray(s>>>0,(s>>>0)+u);i.upload(a,l)}},async(s,a,u)=>{X("verbose",()=>`[WebGPU] jsepCopyGpuToCpu: gpuDataId=${s}, dataOffset=${a}, size=${u}`),await i.download(s,()=>t.HEAPU8.subarray(a>>>0,(a>>>0)+u))},(s,a,u)=>i.createKernel(s,a,u,t.UTF8ToString(t._JsepGetNodeName(a))),s=>i.releaseKernel(s),(s,a,u,d)=>{X("verbose",()=>`[WebGPU] jsepRun: sessionHandle=${u}, kernel=${s}, contextDataOffset=${a}`);let l=new or(t,i,a);return i.computeKernel(s,l,d)},()=>i.captureBegin(),()=>i.captureEnd(),()=>i.replay()])}else{let i=new ln(n);o("webnn",[i,()=>i.reserveTensorId(),s=>i.releaseTensorId(s),async(s,a,u,d)=>i.ensureTensor(s,a,u,d),(s,a)=>{i.uploadTensor(s,a)},async(s,a)=>i.downloadTensor(s,a)])}}});var Dc,Et,Pt,Ze,Oc,st,zt,Bt,ou,Dt,Ot,Rt,vn=k(()=>{"use strict";Yr();eo();U();Ne();Ut();Tn();Dc=(e,t)=>{se()._OrtInit(e,t)!==0&&oe("Can't initialize onnxruntime.")},Et=async e=>{Dc(e.wasm.numThreads,dt(e.logLevel))},Pt=async(e,t)=>{{let n=(ru(),_t(nu)).init;if(t==="webgpu"){if(typeof navigator>"u"||!navigator.gpu)throw new Error("WebGPU is not supported in current environment");let r=e.webgpu.adapter;if(r){if(typeof r.limits!="object"||typeof r.features!="object"||typeof r.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(r=await navigator.gpu.requestAdapter({powerPreference:o,forceFallbackAdapter:i}),!r)throw new Error('Failed to get GPU adapter. You may need to enable flag "--enable-unsafe-webgpu" if you are using Chrome.')}await n("webgpu",se(),e,r)}if(t==="webnn"){if(typeof navigator>"u"||!navigator.ml)throw new Error("WebNN is not supported in current environment");await n("webnn",se(),e)}}},Ze=new Map,Oc=e=>{let t=se(),n=t.stackSave();try{let r=t.stackAlloc(8);return t._OrtGetInputOutputCount(e,r,r+4)!==0&&oe("Can't get session input/output count."),[t.HEAP32[r/4],t.HEAP32[r/4+1]]}finally{t.stackRestore(n)}},st=e=>{let t=se(),n=t._malloc(e.byteLength);if(n===0)throw new Error(`Can't create a session. failed to allocate a buffer of size ${e.byteLength}.`);return t.HEAPU8.set(e,n),[n,e.byteLength]},zt=async(e,t)=>{let n,r,o=se();Array.isArray(e)?[n,r]=e:e.buffer===o.HEAPU8.buffer?[n,r]=[e.byteOffset,e.byteLength]:[n,r]=st(e);let i=0,s=0,a=0,u=[],d=[],l=[];try{if([s,u]=Jr(t),t?.externalData&&o.mountExternalData){let g=[];for(let b of t.externalData){let _=typeof b=="string"?b:b.path;g.push(lt(typeof b=="string"?b:b.data).then($=>{o.mountExternalData(_,$)}))}await Promise.all(g)}for(let g of t?.executionProviders??[])if((typeof g=="string"?g:g.name)==="webnn"){if(o.shouldTransferToMLTensor=!1,o.currentContext)throw new Error("WebNN execution provider is already set.");if(typeof g!="string"){let _=g,$=_?.context,v=_?.gpuDevice,I=_?.deviceType,T=_?.numThreads,P=_?.powerPreference;$?o.currentContext=$:v?o.currentContext=await navigator.ml.createContext(v):o.currentContext=await navigator.ml.createContext({deviceType:I,numThreads:T,powerPreference:P})}else o.currentContext=await navigator.ml.createContext();break}i=await o._OrtCreateSession(n,r,s),i===0&&oe("Can't create a session."),o.currentContext&&(o.jsepRegisterMLContext(i,o.currentContext),o.currentContext=void 0,o.shouldTransferToMLTensor=!0);let[c,p]=Oc(i),h=!!t?.enableGraphCapture,m=[],f=[],w=[];for(let g=0;gg==="gpu-buffer"||g==="ml-tensor")&&(a=o._OrtCreateBinding(i),a===0&&oe("Can't create IO binding."),y={handle:a,outputPreferredLocations:w,outputPreferredLocationsEncoded:w.map(g=>In(g))}),Ze.set(i,[i,d,l,y,h,!1]),[i,m,f]}catch(c){throw d.forEach(p=>o._OrtFree(p)),l.forEach(p=>o._OrtFree(p)),a!==0&&o._OrtReleaseBinding(a),i!==0&&o._OrtReleaseSession(i),c}finally{o._free(n),s!==0&&o._OrtReleaseSessionOptions(s),u.forEach(c=>o._free(c)),o.unmountExternalData?.()}},Bt=e=>{let t=se(),n=Ze.get(e);if(!n)throw new Error(`cannot release session. invalid session id: ${e}`);let[r,o,i,s,a]=n;s&&(a&&t._OrtClearBoundOutputs(s.handle),t._OrtReleaseBinding(s.handle)),t.jsepOnReleaseSession?.(e),o.forEach(u=>t._OrtFree(u)),i.forEach(u=>t._OrtFree(u)),t._OrtReleaseSession(r),Ze.delete(e)},ou=(e,t,n,r,o,i=!1)=>{if(!e){t.push(0);return}let s=se(),a=e[0],u=e[1],d=e[3],l,c;if(a==="string"&&(d==="gpu-buffer"||d==="ml-tensor"))throw new Error("String tensor is not supported on GPU.");if(i&&d!=="gpu-buffer")throw new Error(`External buffer must be provided for input/output index ${o} when enableGraphCapture is true.`);if(d==="gpu-buffer"){let m=e[2].gpuBuffer;c=Fe(ut(a),u);let f=s.jsepRegisterBuffer;if(!f)throw new Error('Tensor location "gpu-buffer" is not supported without using WebGPU.');l=f(r,o,m,c)}else if(d==="ml-tensor"){let m=e[2].mlTensor;c=Fe(ut(a),u);let f=s.jsepRegisterMLTensor;if(!f)throw new Error('Tensor location "ml-tensor" is not supported without using WebNN.');l=f(m,ut(a),u)}else{let m=e[2];if(Array.isArray(m)){c=4*m.length,l=s._malloc(c),n.push(l);let f=l/4;for(let w=0;ws.HEAP32[m++]=w);let f=s._OrtCreateTensor(ut(a),l,c,h,u.length,In(d));f===0&&oe(`Can't create tensor for input/output. session=${r}, index=${o}.`),t.push(f)}finally{s.stackRestore(p)}},Dt=async(e,t,n,r,o,i)=>{let s=se(),a=Ze.get(e);if(!a)throw new Error(`cannot run inference. invalid session id: ${e}`);let u=a[0],d=a[1],l=a[2],c=a[3],p=a[4],h=a[5],m=t.length,f=r.length,w=0,y=[],g=[],b=[],_=[],$=s.stackSave(),v=s.stackAlloc(m*4),I=s.stackAlloc(m*4),T=s.stackAlloc(f*4),P=s.stackAlloc(f*4);try{s.jsepOnRunStart?.(u),[w,y]=Qr(i);for(let M=0;Mhe*ye,1);H=We(D);let wt=c?.outputPreferredLocations[r[M]];if(H==="string"){if(wt==="gpu-buffer"||wt==="ml-tensor")throw new Error("String tensor is not supported on GPU.");let he=[],ye=q/4;for(let Ve=0;Ve0){let he=s.jsepGetBuffer;if(!he)throw new Error('preferredLocation "gpu-buffer" is not supported without using WebGPU.');let ye=he(q),Ve=Fe(D,Re);if(Ve===void 0||!Lt(H))throw new Error(`Unsupported data type: ${H}`);j=!0,ie.push([H,fe,{gpuBuffer:ye,download:s.jsepCreateDownloader(ye,Ve,H),dispose:()=>{s._OrtReleaseTensor(Q)}},"gpu-buffer"])}else if(wt==="ml-tensor"&&Re>0){let he=s.jsepEnsureTensor;if(!he)throw new Error('preferredLocation "ml-tensor" is not supported without using WebNN.');if(Fe(D,Re)===void 0||!Nt(H))throw new Error(`Unsupported data type: ${H}`);let Ve=await he(q,D,fe,!1);j=!0,ie.push([H,fe,{mlTensor:Ve,download:s.jsepCreateMLTensorDownloader(q,H),dispose:()=>{s.jsepReleaseTensorId(q),s._OrtReleaseTensor(Q)}},"ml-tensor"])}else{let he=Vt(H),ye=new he(Re);new Uint8Array(ye.buffer,ye.byteOffset,ye.byteLength).set(s.HEAPU8.subarray(q,q+ye.byteLength)),ie.push([H,fe,ye,"cpu"])}}finally{s.stackRestore(re),H==="string"&&q&&s._free(q),j||s._OrtReleaseTensor(Q)}}return c&&!p&&(s._OrtClearBoundOutputs(c.handle),Ze.set(e,[u,d,l,c,p,!1])),ie}finally{s.stackRestore($),g.forEach(z=>s._OrtReleaseTensor(z)),b.forEach(z=>s._OrtReleaseTensor(z)),_.forEach(z=>s._free(z)),w!==0&&s._OrtReleaseRunOptions(w),y.forEach(z=>s._free(z))}},Ot=e=>{let t=se(),n=Ze.get(e);if(!n)throw new Error("invalid session id");let r=n[0],o=t._OrtEndProfiling(r);o===0&&oe("Can't get an profile file name."),t._OrtFree(o)},Rt=e=>{let t=[];for(let n of e){let r=n[2];!Array.isArray(r)&&"buffer"in r&&t.push(r.buffer)}return t}});var Qe,Te,bt,pn,mn,cn,ir,sr,et,tt,Mc,iu,su,au,uu,du,lu,cu,ar=k(()=>{"use strict";Se();vn();Ne();it();Qe=()=>!!Y.wasm.proxy&&typeof document<"u",bt=!1,pn=!1,mn=!1,sr=new Map,et=(e,t)=>{let n=sr.get(e);n?n.push(t):sr.set(e,[t])},tt=()=>{if(bt||!pn||mn||!Te)throw new Error("worker not ready")},Mc=e=>{switch(e.data.type){case"init-wasm":bt=!1,e.data.err?(mn=!0,ir[1](e.data.err)):(pn=!0,ir[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=sr.get(e.data.type);e.data.err?t.shift()[1](e.data.err):t.shift()[0](e.data.out);break}default:}},iu=async()=>{if(!pn){if(bt)throw new Error("multiple calls to 'initWasm()' detected.");if(mn)throw new Error("previous call to 'initWasm()' failed.");if(bt=!0,Qe())return new Promise((e,t)=>{Te?.terminate(),jr().then(([n,r])=>{try{Te=r,Te.onerror=i=>t(i),Te.onmessage=Mc,ir=[e,t];let o={type:"init-wasm",in:Y};Te.postMessage(o),cn=n}catch(o){t(o)}},t)});try{await kt(Y.wasm),await Et(Y),pn=!0}catch(e){throw mn=!0,e}finally{bt=!1}}},su=async e=>{if(Qe())return tt(),new Promise((t,n)=>{et("init-ep",[t,n]);let r={type:"init-ep",in:{epName:e,env:Y}};Te.postMessage(r)});await Pt(Y,e)},au=async e=>Qe()?(tt(),new Promise((t,n)=>{et("copy-from",[t,n]);let r={type:"copy-from",in:{buffer:e}};Te.postMessage(r,[e.buffer])})):st(e),uu=async(e,t)=>{if(Qe()){if(t?.preferredOutputLocation)throw new Error('session option "preferredOutputLocation" is not supported for proxy.');return tt(),new Promise((n,r)=>{et("create",[n,r]);let o={type:"create",in:{model:e,options:{...t}}},i=[];e instanceof Uint8Array&&i.push(e.buffer),Te.postMessage(o,i)})}else return zt(e,t)},du=async e=>{if(Qe())return tt(),new Promise((t,n)=>{et("release",[t,n]);let r={type:"release",in:e};Te.postMessage(r)});Bt(e)},lu=async(e,t,n,r,o,i)=>{if(Qe()){if(n.some(s=>s[3]!=="cpu"))throw new Error("input tensor on GPU is not supported for proxy.");if(o.some(s=>s))throw new Error("pre-allocated output tensor is not supported for proxy.");return tt(),new Promise((s,a)=>{et("run",[s,a]);let u=n,d={type:"run",in:{sessionId:e,inputIndices:t,inputs:u,outputIndices:r,options:i}};Te.postMessage(d,Rt(u))})}else return Dt(e,t,n,r,o,i)},cu=async e=>{if(Qe())return tt(),new Promise((t,n)=>{et("end-profiling",[t,n]);let r={type:"end-profiling",in:e};Te.postMessage(r)});Ot(e)}});var pu,Uc,fn,mu=k(()=>{"use strict";Se();ar();U();At();Tn();pu=(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()}`)}},Uc=e=>{switch(e[3]){case"cpu":return new ce(e[0],e[2],e[1]);case"gpu-buffer":{let t=e[0];if(!Lt(t))throw new Error(`not supported data type: ${t} for deserializing GPU tensor`);let{gpuBuffer:n,download:r,dispose:o}=e[2];return ce.fromGpuBuffer(n,{dataType:t,dims:e[1],download:r,dispose:o})}case"ml-tensor":{let t=e[0];if(!Nt(t))throw new Error(`not supported data type: ${t} for deserializing MLTensor tensor`);let{mlTensor:n,download:r,dispose:o}=e[2];return ce.fromMLTensor(n,{dataType:t,dims:e[1],download:r,dispose:o})}default:throw new Error(`invalid data location: ${e[3]}`)}},fn=class{async fetchModelAndCopyToWasmMemory(t){return au(await lt(t))}async loadModel(t,n){be();let r;typeof t=="string"?!1?r=await lt(t):r=await this.fetchModelAndCopyToWasmMemory(t):r=t,[this.sessionId,this.inputNames,this.outputNames]=await uu(r,n),ge()}async dispose(){return du(this.sessionId)}async run(t,n,r){be();let o=[],i=[];Object.entries(t).forEach(p=>{let h=p[0],m=p[1],f=this.inputNames.indexOf(h);if(f===-1)throw new Error(`invalid input '${h}'`);o.push(m),i.push(f)});let s=[],a=[];Object.entries(n).forEach(p=>{let h=p[0],m=p[1],f=this.outputNames.indexOf(h);if(f===-1)throw new Error(`invalid output '${h}'`);s.push(m),a.push(f)});let u=o.map((p,h)=>pu(p,()=>`input "${this.inputNames[i[h]]}"`)),d=s.map((p,h)=>p?pu(p,()=>`output "${this.outputNames[a[h]]}"`):null),l=await lu(this.sessionId,i,u,a,d,r),c={};for(let p=0;phn,initializeFlags:()=>fu,wasmBackend:()=>Vc});var fu,hn,Vc,gu=k(()=>{"use strict";Se();ar();mu();it();fu=()=>{if((typeof Y.wasm.initTimeout!="number"||Y.wasm.initTimeout<0)&&(Y.wasm.initTimeout=0),Y.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 Y.wasm.proxy!="boolean"&&(Y.wasm.proxy=!1),typeof Y.wasm.trace!="boolean"&&(Y.wasm.trace=!1),typeof Y.wasm.numThreads!="number"||!Number.isInteger(Y.wasm.numThreads)||Y.wasm.numThreads<=0)if(typeof self<"u"&&!self.crossOriginIsolated)Y.wasm.numThreads=1;else{let e=typeof navigator>"u"?yn("node:os").cpus().length:navigator.hardwareConcurrency;Y.wasm.numThreads=Math.min(4,Math.ceil((e||1)/2))}Y.wasm.wasmPaths===void 0&&Ie&&Ie.indexOf("blob:")!==0&&(Y.wasm.wasmPaths=Ie.substring(0,Ie.lastIndexOf("/")+1))},hn=class{async init(t){fu(),await iu(),await su(t)}async createInferenceSessionHandler(t,n){let r=new fn;return await r.loadModel(t,n),Promise.resolve(r)}},Vc=new hn});var Nc={};nt(Nc,{InferenceSession:()=>Pr,TRACE:()=>ot,TRACE_FUNC_BEGIN:()=>be,TRACE_FUNC_END:()=>ge,Tensor:()=>ce,TrainingSession:()=>Ur,default:()=>Lc,env:()=>Y,registerBackend:()=>Le});Se();Se();Se();var Lr="1.20.1";var Lc=$n;{let e=(gu(),_t(hu)).wasmBackend;Le("webgpu",e,5),Le("webnn",e,5),Le("cpu",e,10),Le("wasm",e,10)}Object.defineProperty(Y.versions,"web",{value:Lr,enumerable:!0});return _t(Nc);})(); +/** + * @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. + * ============================================================================= + */ +typeof exports=="object"&&typeof module=="object"&&(module.exports=ort); +//# sourceMappingURL=ort.webgpu.min.js.map