From 50c6ea93310bf0d1ad57d77b9ef5b1a6fe8eb9c9 Mon Sep 17 00:00:00 2001 From: William Candillon Date: Wed, 11 Dec 2024 17:42:49 +0100 Subject: [PATCH 01/35] Update docs --- README.md | 5 +-- packages/webgpu/README.md | 72 +++++------------------------------- packages/webgpu/package.json | 1 - 3 files changed, 10 insertions(+), 68 deletions(-) diff --git a/README.md b/README.md index 30f5e47c4..b61a27c70 100644 --- a/README.md +++ b/README.md @@ -194,10 +194,6 @@ In "Edit Scheme," uncheck "Metal Validation." Uncheck 'Metal Validation' -### Android - -On an Android simulator, a CPU emulation layer is used which may result in very slow performance. - ## Library Development Make sure to check out the submodules: @@ -235,6 +231,7 @@ You need to have the [Github CLI](https://cli.github.com/) installed: $ yarn $ cd packages/webgpu $ yarn download-artifacts +$ yarn copy-artifacts ``` Alternatively you can also download the prebuilt binaries [here](https://github.com/wcandillon/react-native-webgpu/actions/workflows/build-dawn.yml). diff --git a/packages/webgpu/README.md b/packages/webgpu/README.md index 7d5e7a943..e77d4078c 100644 --- a/packages/webgpu/README.md +++ b/packages/webgpu/README.md @@ -11,15 +11,21 @@ Please note that the package name is `react-native-wgpu`. npm install react-native-wgpu ``` -Below are some examples from the [example app](/package/example/). +Below are some examples from the [example app](/apps/example/). https://github.com/user-attachments/assets/116a41b2-2cf8-49f1-9f16-a5c83637c198 Starting from `r168`, Three.js runs out of the box with React Native WebGPU. -You need to have a slight modification of [the metro config](/package/example/metro.config.js) to resolve Three.js to the WebGPU build. +You need to have a slight modification of [the metro config](/apps/example/metro.config.js) to resolve Three.js to the WebGPU build. +We also support [three-fiber](/apps/example/src/ThreeJS/Fiber.tsx). +For model loading, we also need [the following polyfill](/apps/example/src/App.tsx#29). https://github.com/user-attachments/assets/5b49ef63-0a3c-4679-aeb5-e4b4dddfcc1d +We also provide prebuilt binaries for visionOS and macOS. + +https://github.com/user-attachments/assets/2d5c618e-5b15-4cef-8558-d4ddf8c70667 + ## Usage Currently we recommend to use the `useCanvasEffect` to access the WebGPU context. @@ -123,13 +129,7 @@ const style = StyleSheet.create({ ## Example App -To run the example app you first need to build Dawn. - -```sh -$ git submodule update --init -$ cd package && yarn -$ yarn build-dawn -``` +To run the example app you first need to [build Dawn or download the prebuilt binaries](#building-dawn). From there you will be able to run the example app properly. @@ -184,57 +184,3 @@ device.queue.copyExternalImageToTexture( [imageBitmap.width, imageBitmap.height], ); ``` - -## Troubleshooting - -### iOS - -To run the React Native WebGPU project on the iOS simulator, you need to disable the Metal validation API. -In "Edit Scheme," uncheck "Metal Validation." - -Uncheck 'Metal Validation' - -### Android - -On an Android simulator, a CPU emulation layer is used which may result in very slow performance. - -## Library Development - -Make sure to check out the submodules: - -``` -git submodule update --init -``` - -Make sure you have all the tools required for building the Skia libraries (Android Studio, XCode, Ninja, CMake, Android NDK/build tools). - -### Building - -* `cd packages/webgpu && yarn` -* `yarn build-dawn` - -### Upgrading - -1. `git submodule update --remote` -2. `yarn clean-dawn` -3. `yarn build-dawn` - -### Codegen - -* `cd packages/webgpu && yarn codegen` - -### Testing - -In the `package` folder, to run the test against Chrome for reference: - -``` -yarn test:ref -``` - -To run the e2e test, open the example app on the e2e screen. -By default, it will try to connect to a localhost test server. -If you want to run the test suite on a physical device, you can modify the address [here](/package/example/src/useClient.ts#L4). - -``` -yarn test -``` diff --git a/packages/webgpu/package.json b/packages/webgpu/package.json index 29e0b5627..97010cc71 100644 --- a/packages/webgpu/package.json +++ b/packages/webgpu/package.json @@ -26,7 +26,6 @@ "tsc": "tsc --noEmit", "build": "bob build", "build-dawn": "ts-node scripts/build/dawn.ts", - "copy-headers": "ts-node scripts/build/copy-headers.ts", "clean-dawn": "rimraf ./libs && rimraf ../../externals/dawn/out", "clang-format": "yarn clang-format-ios && yarn clang-format-android && yarn clang-format-common", "clang-format-ios": "find apple/ -iname \"*.h\" -o -iname \"*.mm\" -o -iname \"*.cpp\" | xargs clang-format -i", From 58472f7ca87e70b4e0a81988effdcbcc19c918e4 Mon Sep 17 00:00:00 2001 From: William Candillon Date: Wed, 11 Dec 2024 18:33:45 +0100 Subject: [PATCH 02/35] :wrench: --- apps/example/ios/Podfile.lock | 4 +- apps/example/package.json | 1 + apps/example/src/App.tsx | 2 + apps/example/src/Home.tsx | 4 + apps/example/src/Route.ts | 1 + .../src/TransformerJS/TransformerJS.tsx | 171 ++++++ apps/example/src/TransformerJS/index.ts | 1 + yarn.lock | 514 +++++++++++++++++- 8 files changed, 694 insertions(+), 4 deletions(-) create mode 100644 apps/example/src/TransformerJS/TransformerJS.tsx create mode 100644 apps/example/src/TransformerJS/index.ts diff --git a/apps/example/ios/Podfile.lock b/apps/example/ios/Podfile.lock index 773ce50bf..e29fb8c7c 100644 --- a/apps/example/ios/Podfile.lock +++ b/apps/example/ios/Podfile.lock @@ -937,7 +937,7 @@ PODS: - React-debug - react-native-safe-area-context (4.11.0): - React-Core - - react-native-wgpu (0.1.19): + - react-native-wgpu (0.1.20): - DoubleConversion - glog - hermes-engine @@ -1528,7 +1528,7 @@ SPEC CHECKSUMS: React-logger: 29fa3e048f5f67fe396bc08af7606426d9bd7b5d React-Mapbuffer: bf56147c9775491e53122a94c423ac201417e326 react-native-safe-area-context: 851c62c48dce80ccaa5637b6aa5991a1bc36eca9 - react-native-wgpu: 5fd8cb5fd7bd00c88831d29438697d897fb680e8 + react-native-wgpu: 078c84b159de97704f8926011b0f9bc96591d478 React-nativeconfig: 9f223cd321823afdecf59ed00861ab2d69ee0fc1 React-NativeModulesApple: ff7efaff7098639db5631236cfd91d60abff04c0 React-perflogger: 32ed45d9cee02cf6639acae34251590dccd30994 diff --git a/apps/example/package.json b/apps/example/package.json index 2f5e0518b..33827d685 100644 --- a/apps/example/package.json +++ b/apps/example/package.json @@ -17,6 +17,7 @@ }, "dependencies": { "@callstack/react-native-visionos": "^0.74.0", + "@huggingface/transformers": "^3.1.2", "@react-navigation/native": "^6.1.17", "@react-navigation/stack": "^6.4.0", "@react-three/fiber": "^8.17.6", diff --git a/apps/example/src/App.tsx b/apps/example/src/App.tsx index 755ef5f25..e094a56ad 100644 --- a/apps/example/src/App.tsx +++ b/apps/example/src/App.tsx @@ -27,6 +27,7 @@ import { ReversedZ } from "./ReversedZ"; import { ThreeJS } from "./ThreeJS"; import { GradientTiles } from "./GradientTiles"; import { CanvasAPI } from "./CanvasAPI"; +import { TransformerJS } from "./TransformerJS"; // The two lines below are needed by three.js import "fast-text-encoding"; @@ -50,6 +51,7 @@ function App() { component={HelloTriangleMSAA} /> + diff --git a/apps/example/src/Home.tsx b/apps/example/src/Home.tsx index 99884ad07..127c46eaa 100644 --- a/apps/example/src/Home.tsx +++ b/apps/example/src/Home.tsx @@ -23,6 +23,10 @@ export const examples = [ screen: "ThreeJS", title: "☘️ Three.js", }, + { + screen: "TransformerJS", + title: "🤖 transformer.js", + }, { screen: "Cube", title: "🧊 Cube", diff --git a/apps/example/src/Route.ts b/apps/example/src/Route.ts index f9e88a522..2154c8666 100644 --- a/apps/example/src/Route.ts +++ b/apps/example/src/Route.ts @@ -4,6 +4,7 @@ export type Routes = { HelloTriangleMSAA: undefined; Cube: undefined; ThreeJS: undefined; + TransformerJS: undefined; InstancedCube: undefined; TexturedCube: undefined; FractalCube: undefined; diff --git a/apps/example/src/TransformerJS/TransformerJS.tsx b/apps/example/src/TransformerJS/TransformerJS.tsx new file mode 100644 index 000000000..e15d1a41c --- /dev/null +++ b/apps/example/src/TransformerJS/TransformerJS.tsx @@ -0,0 +1,171 @@ +// @ts-nocheck + +/* eslint-disable camelcase */ +import { + AutoTokenizer, + AutoModelForCausalLM, + TextStreamer, + InterruptableStoppingCriteria, +} from "@huggingface/transformers"; +import { View } from "react-native"; + +/** + * This class uses the Singleton pattern to enable lazy-loading of the pipeline + */ +class TextGenerationPipeline { + static model_id = "onnx-community/Llama-3.2-1B-Instruct-q4f16"; + + static async getInstance(progress_callback = null) { + this.tokenizer ??= AutoTokenizer.from_pretrained(this.model_id, { + progress_callback, + }); + + this.model ??= AutoModelForCausalLM.from_pretrained(this.model_id, { + dtype: "q4f16", + device: "webgpu", + progress_callback, + }); + + return Promise.all([this.tokenizer, this.model]); + } +} + +const stopping_criteria = new InterruptableStoppingCriteria(); + +const past_key_values_cache = null; +async function generate(messages) { + // Retrieve the text-generation pipeline. + const [tokenizer, model] = await TextGenerationPipeline.getInstance(); + + const inputs = tokenizer.apply_chat_template(messages, { + add_generation_prompt: true, + return_dict: true, + }); + + let startTime; + let numTokens = 0; + let tps; + const token_callback_function = () => { + startTime ??= performance.now(); + + if (numTokens++ > 0) { + tps = (numTokens / (performance.now() - startTime)) * 1000; + } + }; + const callback_function = (output) => { + self.postMessage({ + status: "update", + output, + tps, + numTokens, + }); + }; + + const streamer = new TextStreamer(tokenizer, { + skip_prompt: true, + skip_special_tokens: true, + callback_function, + token_callback_function, + }); + + // Tell the main thread we are starting + self.postMessage({ status: "start" }); + + const { past_key_values, sequences } = await model.generate({ + ...inputs, + // TODO: Add when model is fixed + // past_key_values: past_key_values_cache, + + // Sampling + do_sample: false, + + max_new_tokens: 1024, + streamer, + stopping_criteria, + return_dict_in_generate: true, + }); + // past_key_values_cache = past_key_values; + + const decoded = tokenizer.batch_decode(sequences, { + skip_special_tokens: true, + }); + + // Send the output back to the main thread + self.postMessage({ + status: "complete", + output: decoded, + }); +} + +async function check() { + try { + const adapter = await navigator.gpu.requestAdapter(); + if (!adapter) { + throw new Error("WebGPU is not supported (no adapter found)"); + } + } catch (e) { + self.postMessage({ + status: "error", + data: e.toString(), + }); + } +} + +async function load() { + self.postMessage({ + status: "loading", + data: "Loading model...", + }); + + // Load the pipeline and save it for future use. + const [tokenizer, model] = await TextGenerationPipeline.getInstance((x) => { + // We also add a progress callback to the pipeline so that we can + // track model loading. + self.postMessage(x); + }); + + self.postMessage({ + status: "loading", + data: "Compiling shaders and warming up model...", + }); + + // Run model with dummy input to compile shaders + const inputs = tokenizer("a"); + await model.generate({ ...inputs, max_new_tokens: 1 }); + self.postMessage({ status: "ready" }); +} + +load(); + +// Listen for messages from the main thread +// self.addEventListener("message", async (e) => { +// const { type, data } = e.data; + +// switch (type) { +// case "check": +// check(); +// break; + +// case "load": +// load(); +// break; + +// case "generate": +// stopping_criteria.reset(); +// generate(data); +// break; + +// case "interrupt": +// stopping_criteria.interrupt(); +// break; + +// case "reset": +// // past_key_values_cache = null; +// stopping_criteria.reset(); +// break; +// } +// }); + +export const TransformerJS = () => { + return ; +}; diff --git a/apps/example/src/TransformerJS/index.ts b/apps/example/src/TransformerJS/index.ts new file mode 100644 index 000000000..f068c4479 --- /dev/null +++ b/apps/example/src/TransformerJS/index.ts @@ -0,0 +1 @@ +export * from "./TransformerJS"; diff --git a/yarn.lock b/yarn.lock index 98d62110c..e97786fca 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1773,6 +1773,15 @@ __metadata: languageName: node linkType: hard +"@emnapi/runtime@npm:^1.2.0": + version: 1.3.1 + resolution: "@emnapi/runtime@npm:1.3.1" + dependencies: + tslib: ^2.4.0 + checksum: 9a16ae7905a9c0e8956cf1854ef74e5087fbf36739abdba7aa6b308485aafdc993da07c19d7af104cd5f8e425121120852851bb3a0f78e2160e420a36d47f42f + languageName: node + linkType: hard + "@eslint-community/eslint-utils@npm:^4.2.0, @eslint-community/eslint-utils@npm:^4.4.0": version: 4.4.0 resolution: "@eslint-community/eslint-utils@npm:4.4.0" @@ -1831,6 +1840,25 @@ __metadata: languageName: node linkType: hard +"@huggingface/jinja@npm:^0.3.2": + version: 0.3.2 + resolution: "@huggingface/jinja@npm:0.3.2" + checksum: 4bc7d00b6f8655a0032c2d89e38a095d0a87ef81a1c12fb6fd0404e1319e1ef6eef87734502689c1df39db4e77a7bb5996e7b6c1b4d6a768ecfa5a48f2a939a7 + languageName: node + linkType: hard + +"@huggingface/transformers@npm:^3.1.2": + version: 3.1.2 + resolution: "@huggingface/transformers@npm:3.1.2" + dependencies: + "@huggingface/jinja": ^0.3.2 + onnxruntime-node: 1.20.1 + onnxruntime-web: 1.21.0-dev.20241205-d27fecd3d3 + sharp: ^0.33.5 + checksum: f87517d7e77a46a61610336120df35e6091e0d67ef549f1eab39501bbe27aeff95769b042536c558de768479ee1d5a253d628e964ad22c2fa70d206dfea15d92 + languageName: node + linkType: hard + "@humanwhocodes/config-array@npm:^0.11.14": version: 0.11.14 resolution: "@humanwhocodes/config-array@npm:0.11.14" @@ -1856,6 +1884,181 @@ __metadata: languageName: node linkType: hard +"@img/sharp-darwin-arm64@npm:0.33.5": + version: 0.33.5 + resolution: "@img/sharp-darwin-arm64@npm:0.33.5" + dependencies: + "@img/sharp-libvips-darwin-arm64": 1.0.4 + dependenciesMeta: + "@img/sharp-libvips-darwin-arm64": + optional: true + conditions: os=darwin & cpu=arm64 + languageName: node + linkType: hard + +"@img/sharp-darwin-x64@npm:0.33.5": + version: 0.33.5 + resolution: "@img/sharp-darwin-x64@npm:0.33.5" + dependencies: + "@img/sharp-libvips-darwin-x64": 1.0.4 + dependenciesMeta: + "@img/sharp-libvips-darwin-x64": + optional: true + conditions: os=darwin & cpu=x64 + languageName: node + linkType: hard + +"@img/sharp-libvips-darwin-arm64@npm:1.0.4": + version: 1.0.4 + resolution: "@img/sharp-libvips-darwin-arm64@npm:1.0.4" + conditions: os=darwin & cpu=arm64 + languageName: node + linkType: hard + +"@img/sharp-libvips-darwin-x64@npm:1.0.4": + version: 1.0.4 + resolution: "@img/sharp-libvips-darwin-x64@npm:1.0.4" + conditions: os=darwin & cpu=x64 + languageName: node + linkType: hard + +"@img/sharp-libvips-linux-arm64@npm:1.0.4": + version: 1.0.4 + resolution: "@img/sharp-libvips-linux-arm64@npm:1.0.4" + conditions: os=linux & cpu=arm64 & libc=glibc + languageName: node + linkType: hard + +"@img/sharp-libvips-linux-arm@npm:1.0.5": + version: 1.0.5 + resolution: "@img/sharp-libvips-linux-arm@npm:1.0.5" + conditions: os=linux & cpu=arm & libc=glibc + languageName: node + linkType: hard + +"@img/sharp-libvips-linux-s390x@npm:1.0.4": + version: 1.0.4 + resolution: "@img/sharp-libvips-linux-s390x@npm:1.0.4" + conditions: os=linux & cpu=s390x & libc=glibc + languageName: node + linkType: hard + +"@img/sharp-libvips-linux-x64@npm:1.0.4": + version: 1.0.4 + resolution: "@img/sharp-libvips-linux-x64@npm:1.0.4" + conditions: os=linux & cpu=x64 & libc=glibc + languageName: node + linkType: hard + +"@img/sharp-libvips-linuxmusl-arm64@npm:1.0.4": + version: 1.0.4 + resolution: "@img/sharp-libvips-linuxmusl-arm64@npm:1.0.4" + conditions: os=linux & cpu=arm64 & libc=musl + languageName: node + linkType: hard + +"@img/sharp-libvips-linuxmusl-x64@npm:1.0.4": + version: 1.0.4 + resolution: "@img/sharp-libvips-linuxmusl-x64@npm:1.0.4" + conditions: os=linux & cpu=x64 & libc=musl + languageName: node + linkType: hard + +"@img/sharp-linux-arm64@npm:0.33.5": + version: 0.33.5 + resolution: "@img/sharp-linux-arm64@npm:0.33.5" + dependencies: + "@img/sharp-libvips-linux-arm64": 1.0.4 + dependenciesMeta: + "@img/sharp-libvips-linux-arm64": + optional: true + conditions: os=linux & cpu=arm64 & libc=glibc + languageName: node + linkType: hard + +"@img/sharp-linux-arm@npm:0.33.5": + version: 0.33.5 + resolution: "@img/sharp-linux-arm@npm:0.33.5" + dependencies: + "@img/sharp-libvips-linux-arm": 1.0.5 + dependenciesMeta: + "@img/sharp-libvips-linux-arm": + optional: true + conditions: os=linux & cpu=arm & libc=glibc + languageName: node + linkType: hard + +"@img/sharp-linux-s390x@npm:0.33.5": + version: 0.33.5 + resolution: "@img/sharp-linux-s390x@npm:0.33.5" + dependencies: + "@img/sharp-libvips-linux-s390x": 1.0.4 + dependenciesMeta: + "@img/sharp-libvips-linux-s390x": + optional: true + conditions: os=linux & cpu=s390x & libc=glibc + languageName: node + linkType: hard + +"@img/sharp-linux-x64@npm:0.33.5": + version: 0.33.5 + resolution: "@img/sharp-linux-x64@npm:0.33.5" + dependencies: + "@img/sharp-libvips-linux-x64": 1.0.4 + dependenciesMeta: + "@img/sharp-libvips-linux-x64": + optional: true + conditions: os=linux & cpu=x64 & libc=glibc + languageName: node + linkType: hard + +"@img/sharp-linuxmusl-arm64@npm:0.33.5": + version: 0.33.5 + resolution: "@img/sharp-linuxmusl-arm64@npm:0.33.5" + dependencies: + "@img/sharp-libvips-linuxmusl-arm64": 1.0.4 + dependenciesMeta: + "@img/sharp-libvips-linuxmusl-arm64": + optional: true + conditions: os=linux & cpu=arm64 & libc=musl + languageName: node + linkType: hard + +"@img/sharp-linuxmusl-x64@npm:0.33.5": + version: 0.33.5 + resolution: "@img/sharp-linuxmusl-x64@npm:0.33.5" + dependencies: + "@img/sharp-libvips-linuxmusl-x64": 1.0.4 + dependenciesMeta: + "@img/sharp-libvips-linuxmusl-x64": + optional: true + conditions: os=linux & cpu=x64 & libc=musl + languageName: node + linkType: hard + +"@img/sharp-wasm32@npm:0.33.5": + version: 0.33.5 + resolution: "@img/sharp-wasm32@npm:0.33.5" + dependencies: + "@emnapi/runtime": ^1.2.0 + conditions: cpu=wasm32 + languageName: node + linkType: hard + +"@img/sharp-win32-ia32@npm:0.33.5": + version: 0.33.5 + resolution: "@img/sharp-win32-ia32@npm:0.33.5" + conditions: os=win32 & cpu=ia32 + languageName: node + linkType: hard + +"@img/sharp-win32-x64@npm:0.33.5": + version: 0.33.5 + resolution: "@img/sharp-win32-x64@npm:0.33.5" + conditions: os=win32 & cpu=x64 + languageName: node + linkType: hard + "@isaacs/cliui@npm:^8.0.2": version: 8.0.2 resolution: "@isaacs/cliui@npm:8.0.2" @@ -1870,6 +2073,15 @@ __metadata: languageName: node linkType: hard +"@isaacs/fs-minipass@npm:^4.0.0": + version: 4.0.1 + resolution: "@isaacs/fs-minipass@npm:4.0.1" + dependencies: + minipass: ^7.0.4 + checksum: 5d36d289960e886484362d9eb6a51d1ea28baed5f5d0140bbe62b99bac52eaf06cc01c2bc0d3575977962f84f6b2c4387b043ee632216643d4787b0999465bf2 + languageName: node + linkType: hard + "@isaacs/ttlcache@npm:^1.4.1": version: 1.4.1 resolution: "@isaacs/ttlcache@npm:1.4.1" @@ -2283,6 +2495,79 @@ __metadata: languageName: node linkType: hard +"@protobufjs/aspromise@npm:^1.1.1, @protobufjs/aspromise@npm:^1.1.2": + version: 1.1.2 + resolution: "@protobufjs/aspromise@npm:1.1.2" + checksum: 011fe7ef0826b0fd1a95935a033a3c0fd08483903e1aa8f8b4e0704e3233406abb9ee25350ec0c20bbecb2aad8da0dcea58b392bbd77d6690736f02c143865d2 + languageName: node + linkType: hard + +"@protobufjs/base64@npm:^1.1.2": + version: 1.1.2 + resolution: "@protobufjs/base64@npm:1.1.2" + checksum: 67173ac34de1e242c55da52c2f5bdc65505d82453893f9b51dc74af9fe4c065cf4a657a4538e91b0d4a1a1e0a0642215e31894c31650ff6e3831471061e1ee9e + languageName: node + linkType: hard + +"@protobufjs/codegen@npm:^2.0.4": + version: 2.0.4 + resolution: "@protobufjs/codegen@npm:2.0.4" + checksum: 59240c850b1d3d0b56d8f8098dd04787dcaec5c5bd8de186fa548de86b86076e1c50e80144b90335e705a044edf5bc8b0998548474c2a10a98c7e004a1547e4b + languageName: node + linkType: hard + +"@protobufjs/eventemitter@npm:^1.1.0": + version: 1.1.0 + resolution: "@protobufjs/eventemitter@npm:1.1.0" + checksum: 0369163a3d226851682f855f81413cbf166cd98f131edb94a0f67f79e75342d86e89df9d7a1df08ac28be2bc77e0a7f0200526bb6c2a407abbfee1f0262d5fd7 + languageName: node + linkType: hard + +"@protobufjs/fetch@npm:^1.1.0": + version: 1.1.0 + resolution: "@protobufjs/fetch@npm:1.1.0" + dependencies: + "@protobufjs/aspromise": ^1.1.1 + "@protobufjs/inquire": ^1.1.0 + checksum: 3fce7e09eb3f1171dd55a192066450f65324fd5f7cc01a431df01bb00d0a895e6bfb5b0c5561ce157ee1d886349c90703d10a4e11a1a256418ff591b969b3477 + languageName: node + linkType: hard + +"@protobufjs/float@npm:^1.0.2": + version: 1.0.2 + resolution: "@protobufjs/float@npm:1.0.2" + checksum: 5781e1241270b8bd1591d324ca9e3a3128d2f768077a446187a049e36505e91bc4156ed5ac3159c3ce3d2ba3743dbc757b051b2d723eea9cd367bfd54ab29b2f + languageName: node + linkType: hard + +"@protobufjs/inquire@npm:^1.1.0": + version: 1.1.0 + resolution: "@protobufjs/inquire@npm:1.1.0" + checksum: ca06f02eaf65ca36fb7498fc3492b7fc087bfcc85c702bac5b86fad34b692bdce4990e0ef444c1e2aea8c034227bd1f0484be02810d5d7e931c55445555646f4 + languageName: node + linkType: hard + +"@protobufjs/path@npm:^1.1.2": + version: 1.1.2 + resolution: "@protobufjs/path@npm:1.1.2" + checksum: 856eeb532b16a7aac071cacde5c5620df800db4c80cee6dbc56380524736205aae21e5ae47739114bf669ab5e8ba0e767a282ad894f3b5e124197cb9224445ee + languageName: node + linkType: hard + +"@protobufjs/pool@npm:^1.1.0": + version: 1.1.0 + resolution: "@protobufjs/pool@npm:1.1.0" + checksum: d6a34fbbd24f729e2a10ee915b74e1d77d52214de626b921b2d77288bd8f2386808da2315080f2905761527cceffe7ec34c7647bd21a5ae41a25e8212ff79451 + languageName: node + linkType: hard + +"@protobufjs/utf8@npm:^1.1.0": + version: 1.1.0 + resolution: "@protobufjs/utf8@npm:1.1.0" + checksum: f9bf3163d13aaa3b6f5e6fbf37a116e094ea021c0e1f2a7ccd0e12a29e2ce08dafba4e8b36e13f8ed7397e1591610ce880ed1289af4d66cf4ace8a36a9557278 + languageName: node + linkType: hard + "@puppeteer/browsers@npm:2.3.0": version: 2.3.0 resolution: "@puppeteer/browsers@npm:2.3.0" @@ -3586,6 +3871,15 @@ __metadata: languageName: node linkType: hard +"@types/node@npm:>=13.7.0": + version: 22.10.2 + resolution: "@types/node@npm:22.10.2" + dependencies: + undici-types: ~6.20.0 + checksum: b22401e6e7d1484e437d802c72f5560e18100b1257b9ad0574d6fe05bebe4dbcb620ea68627d1f1406775070d29ace8b6b51f57e7b1c7b8bafafe6da7f29c843 + languageName: node + linkType: hard + "@types/node@npm:^18.0.0": version: 18.19.47 resolution: "@types/node@npm:18.19.47" @@ -4271,6 +4565,7 @@ __metadata: "@babel/preset-env": ^7.20.0 "@babel/runtime": ^7.20.0 "@callstack/react-native-visionos": ^0.74.0 + "@huggingface/transformers": ^3.1.2 "@react-native/babel-preset": 0.74.84 "@react-native/eslint-config": 0.74.84 "@react-native/metro-config": 0.74.84 @@ -5133,6 +5428,13 @@ __metadata: languageName: node linkType: hard +"chownr@npm:^3.0.0": + version: 3.0.0 + resolution: "chownr@npm:3.0.0" + checksum: fd73a4bab48b79e66903fe1cafbdc208956f41ea4f856df883d0c7277b7ab29fd33ee65f93b2ec9192fc0169238f2f8307b7735d27c155821d886b84aa97aa8d + languageName: node + linkType: hard + "chrome-launcher@npm:^0.15.2": version: 0.15.2 resolution: "chrome-launcher@npm:0.15.2" @@ -5782,6 +6084,13 @@ __metadata: languageName: node linkType: hard +"detect-libc@npm:^2.0.3": + version: 2.0.3 + resolution: "detect-libc@npm:2.0.3" + checksum: 2ba6a939ae55f189aea996ac67afceb650413c7a34726ee92c40fb0deb2400d57ef94631a8a3f052055eea7efb0f99a9b5e6ce923415daa3e68221f963cfc27d + languageName: node + linkType: hard + "detect-newline@npm:^3.0.0": version: 3.1.0 resolution: "detect-newline@npm:3.1.0" @@ -6831,6 +7140,13 @@ __metadata: languageName: node linkType: hard +"flatbuffers@npm:^1.12.0": + version: 1.12.0 + resolution: "flatbuffers@npm:1.12.0" + checksum: 8a6461ec80a8f850c623439fbc3d031bac52dfd7dee27fbadf1d850e96fd92cbd782c28bf2a08f2d852a3ac329cc31e2ad21e133ab68993cf0df69d3dd32fd12 + languageName: node + linkType: hard + "flatted@npm:^3.2.9": version: 3.3.1 resolution: "flatted@npm:3.3.1" @@ -7191,6 +7507,13 @@ __metadata: languageName: node linkType: hard +"guid-typescript@npm:^1.0.9": + version: 1.0.9 + resolution: "guid-typescript@npm:1.0.9" + checksum: 829dd87866800a5138aafa0873994028bbc446eb20ff4cae6452d471a2a3d26f7025bed3eb980692c0f022fd22f95ea7396122b46b45a4b5084958505a4fc50c + languageName: node + linkType: hard + "hard-rejection@npm:^2.1.0": version: 2.1.0 resolution: "hard-rejection@npm:2.1.0" @@ -8861,6 +9184,13 @@ __metadata: languageName: node linkType: hard +"long@npm:^5.0.0, long@npm:^5.2.3": + version: 5.2.3 + resolution: "long@npm:5.2.3" + checksum: 885ede7c3de4facccbd2cacc6168bae3a02c3e836159ea4252c87b6e34d40af819824b2d4edce330bfb5c4d6e8ce3ec5864bdcf9473fa1f53a4f8225860e5897 + languageName: node + linkType: hard + "loose-envify@npm:^1.0.0, loose-envify@npm:^1.1.0, loose-envify@npm:^1.4.0": version: 1.4.0 resolution: "loose-envify@npm:1.4.0" @@ -9438,7 +9768,7 @@ __metadata: languageName: node linkType: hard -"minipass@npm:^5.0.0 || ^6.0.2 || ^7.0.0, minipass@npm:^7.0.2, minipass@npm:^7.0.3, minipass@npm:^7.1.2": +"minipass@npm:^5.0.0 || ^6.0.2 || ^7.0.0, minipass@npm:^7.0.2, minipass@npm:^7.0.3, minipass@npm:^7.0.4, minipass@npm:^7.1.2": version: 7.1.2 resolution: "minipass@npm:7.1.2" checksum: 2bfd325b95c555f2b4d2814d49325691c7bee937d753814861b0b49d5edcda55cbbf22b6b6a60bb91eddac8668771f03c5ff647dcd9d0f798e9548b9cdc46ee3 @@ -9455,6 +9785,16 @@ __metadata: languageName: node linkType: hard +"minizlib@npm:^3.0.1": + version: 3.0.1 + resolution: "minizlib@npm:3.0.1" + dependencies: + minipass: ^7.0.4 + rimraf: ^5.0.5 + checksum: da0a53899252380475240c587e52c824f8998d9720982ba5c4693c68e89230718884a209858c156c6e08d51aad35700a3589987e540593c36f6713fe30cd7338 + languageName: node + linkType: hard + "mitt@npm:3.0.1": version: 3.0.1 resolution: "mitt@npm:3.0.1" @@ -9806,6 +10146,45 @@ __metadata: languageName: node linkType: hard +"onnxruntime-common@npm:1.20.1": + version: 1.20.1 + resolution: "onnxruntime-common@npm:1.20.1" + checksum: 5cde8fae546c9a4a2d8f13e18cc4c346d77e733d08d1f6b95f4958fb09618592113d232db64049fafadbd18913ec8564e6c06c47dadc4c2aac8df4ed18b2956c + languageName: node + linkType: hard + +"onnxruntime-common@npm:1.21.0-dev.20241205-6ed77cc374": + version: 1.21.0-dev.20241205-6ed77cc374 + resolution: "onnxruntime-common@npm:1.21.0-dev.20241205-6ed77cc374" + checksum: f490d6b1a8c059ce5665a468ac1c38de4c3729ead0bae173a0c9334c32a67fb2899972b6e185cc6c42f05e61f2c3da2738a814dbc89b5577206a7b17e29f4190 + languageName: node + linkType: hard + +"onnxruntime-node@npm:1.20.1": + version: 1.20.1 + resolution: "onnxruntime-node@npm:1.20.1" + dependencies: + onnxruntime-common: 1.20.1 + tar: ^7.0.1 + checksum: 6b5467eb1d08e1f5931ed1bff77e180f8600be917b690bad5edcfad61fcb797d29f74c5cff5eeb1f8bc95a36d261647d68ca88e149b0aa88412d8dea90901042 + conditions: (os=win32 | os=darwin | os=linux) + languageName: node + linkType: hard + +"onnxruntime-web@npm:1.21.0-dev.20241205-d27fecd3d3": + version: 1.21.0-dev.20241205-d27fecd3d3 + resolution: "onnxruntime-web@npm:1.21.0-dev.20241205-d27fecd3d3" + dependencies: + flatbuffers: ^1.12.0 + guid-typescript: ^1.0.9 + long: ^5.2.3 + onnxruntime-common: 1.21.0-dev.20241205-6ed77cc374 + platform: ^1.3.6 + protobufjs: ^7.2.4 + checksum: f668b638440dc8122209ce04c9e06b449bd2d7d0ce05be0d0618468d98746310e4a4d1a15afea30c86e98cea0053496d1c0fef5e6785153f16be8530f24018b8 + languageName: node + linkType: hard + "open@npm:^6.2.0": version: 6.4.0 resolution: "open@npm:6.4.0" @@ -10120,6 +10499,13 @@ __metadata: languageName: node linkType: hard +"platform@npm:^1.3.6": + version: 1.3.6 + resolution: "platform@npm:1.3.6" + checksum: 6f472a09c61d418c7e26c1c16d0bdc029549d512dbec6526216a1e59ec68100d07007d0097dcba69dddad883d6f2a83361b4bdfe0094a3d9a2af24158643d85e + languageName: node + linkType: hard + "pngjs@npm:^6.0.0": version: 6.0.0 resolution: "pngjs@npm:6.0.0" @@ -10268,6 +10654,26 @@ __metadata: languageName: node linkType: hard +"protobufjs@npm:^7.2.4": + version: 7.4.0 + resolution: "protobufjs@npm:7.4.0" + dependencies: + "@protobufjs/aspromise": ^1.1.2 + "@protobufjs/base64": ^1.1.2 + "@protobufjs/codegen": ^2.0.4 + "@protobufjs/eventemitter": ^1.1.0 + "@protobufjs/fetch": ^1.1.0 + "@protobufjs/float": ^1.0.2 + "@protobufjs/inquire": ^1.1.0 + "@protobufjs/path": ^1.1.2 + "@protobufjs/pool": ^1.1.0 + "@protobufjs/utf8": ^1.1.0 + "@types/node": ">=13.7.0" + long: ^5.0.0 + checksum: ba0e6b60541bbf818bb148e90f5eb68bd99004e29a6034ad9895a381cbd352be8dce5376e47ae21b2e05559f2505b4a5f4a3c8fa62402822c6ab4dcdfb89ffb3 + languageName: node + linkType: hard + "proxy-agent@npm:^6.4.0": version: 6.4.0 resolution: "proxy-agent@npm:6.4.0" @@ -11080,7 +11486,7 @@ __metadata: languageName: node linkType: hard -"rimraf@npm:^5.0.7": +"rimraf@npm:^5.0.5, rimraf@npm:^5.0.7": version: 5.0.10 resolution: "rimraf@npm:5.0.10" dependencies: @@ -11315,6 +11721,75 @@ __metadata: languageName: node linkType: hard +"sharp@npm:^0.33.5": + version: 0.33.5 + resolution: "sharp@npm:0.33.5" + dependencies: + "@img/sharp-darwin-arm64": 0.33.5 + "@img/sharp-darwin-x64": 0.33.5 + "@img/sharp-libvips-darwin-arm64": 1.0.4 + "@img/sharp-libvips-darwin-x64": 1.0.4 + "@img/sharp-libvips-linux-arm": 1.0.5 + "@img/sharp-libvips-linux-arm64": 1.0.4 + "@img/sharp-libvips-linux-s390x": 1.0.4 + "@img/sharp-libvips-linux-x64": 1.0.4 + "@img/sharp-libvips-linuxmusl-arm64": 1.0.4 + "@img/sharp-libvips-linuxmusl-x64": 1.0.4 + "@img/sharp-linux-arm": 0.33.5 + "@img/sharp-linux-arm64": 0.33.5 + "@img/sharp-linux-s390x": 0.33.5 + "@img/sharp-linux-x64": 0.33.5 + "@img/sharp-linuxmusl-arm64": 0.33.5 + "@img/sharp-linuxmusl-x64": 0.33.5 + "@img/sharp-wasm32": 0.33.5 + "@img/sharp-win32-ia32": 0.33.5 + "@img/sharp-win32-x64": 0.33.5 + color: ^4.2.3 + detect-libc: ^2.0.3 + semver: ^7.6.3 + dependenciesMeta: + "@img/sharp-darwin-arm64": + optional: true + "@img/sharp-darwin-x64": + optional: true + "@img/sharp-libvips-darwin-arm64": + optional: true + "@img/sharp-libvips-darwin-x64": + optional: true + "@img/sharp-libvips-linux-arm": + optional: true + "@img/sharp-libvips-linux-arm64": + optional: true + "@img/sharp-libvips-linux-s390x": + optional: true + "@img/sharp-libvips-linux-x64": + optional: true + "@img/sharp-libvips-linuxmusl-arm64": + optional: true + "@img/sharp-libvips-linuxmusl-x64": + optional: true + "@img/sharp-linux-arm": + optional: true + "@img/sharp-linux-arm64": + optional: true + "@img/sharp-linux-s390x": + optional: true + "@img/sharp-linux-x64": + optional: true + "@img/sharp-linuxmusl-arm64": + optional: true + "@img/sharp-linuxmusl-x64": + optional: true + "@img/sharp-wasm32": + optional: true + "@img/sharp-win32-ia32": + optional: true + "@img/sharp-win32-x64": + optional: true + checksum: 04beae89910ac65c5f145f88de162e8466bec67705f497ace128de849c24d168993e016f33a343a1f3c30b25d2a90c3e62b017a9a0d25452371556f6cd2471e4 + languageName: node + linkType: hard + "shebang-command@npm:^2.0.0": version: 2.0.0 resolution: "shebang-command@npm:2.0.0" @@ -11893,6 +12368,20 @@ __metadata: languageName: node linkType: hard +"tar@npm:^7.0.1": + version: 7.4.3 + resolution: "tar@npm:7.4.3" + dependencies: + "@isaacs/fs-minipass": ^4.0.0 + chownr: ^3.0.0 + minipass: ^7.1.2 + minizlib: ^3.0.1 + mkdirp: ^3.0.1 + yallist: ^5.0.0 + checksum: 8485350c0688331c94493031f417df069b778aadb25598abdad51862e007c39d1dd5310702c7be4a6784731a174799d8885d2fde0484269aea205b724d7b2ffa + languageName: node + linkType: hard + "teapot@npm:^1.0.0": version: 1.0.0 resolution: "teapot@npm:1.0.0" @@ -12115,6 +12604,13 @@ __metadata: languageName: node linkType: hard +"tslib@npm:^2.4.0": + version: 2.8.1 + resolution: "tslib@npm:2.8.1" + checksum: e4aba30e632b8c8902b47587fd13345e2827fa639e7c3121074d5ee0880723282411a8838f830b55100cbe4517672f84a2472667d355b81e8af165a55dc6203a + languageName: node + linkType: hard + "tsutils@npm:^3.21.0": version: 3.21.0 resolution: "tsutils@npm:3.21.0" @@ -12412,6 +12908,13 @@ __metadata: languageName: node linkType: hard +"undici-types@npm:~6.20.0": + version: 6.20.0 + resolution: "undici-types@npm:6.20.0" + checksum: b7bc50f012dc6afbcce56c9fd62d7e86b20a62ff21f12b7b5cbf1973b9578d90f22a9c7fe50e638e96905d33893bf2f9f16d98929c4673c2480de05c6c96ea8b + languageName: node + linkType: hard + "unicode-canonical-property-names-ecmascript@npm:^2.0.0": version: 2.0.0 resolution: "unicode-canonical-property-names-ecmascript@npm:2.0.0" @@ -12871,6 +13374,13 @@ __metadata: languageName: node linkType: hard +"yallist@npm:^5.0.0": + version: 5.0.0 + resolution: "yallist@npm:5.0.0" + checksum: eba51182400b9f35b017daa7f419f434424410691bbc5de4f4240cc830fdef906b504424992700dc047f16b4d99100a6f8b8b11175c193f38008e9c96322b6a5 + languageName: node + linkType: hard + "yaml@npm:^1.10.0": version: 1.10.2 resolution: "yaml@npm:1.10.2" From 6b0a53e734dc8800694154cd5af6e54cc7ba4103 Mon Sep 17 00:00:00 2001 From: William Candillon Date: Thu, 12 Dec 2024 09:27:26 +0100 Subject: [PATCH 03/35] :wrench: --- apps/example/babel.config.js | 3 +- apps/example/package.json | 1 + yarn.lock | 70 ++++++++++++++++++++++++++++++++++++ 3 files changed, 73 insertions(+), 1 deletion(-) diff --git a/apps/example/babel.config.js b/apps/example/babel.config.js index 32bed768c..cf93fdd58 100644 --- a/apps/example/babel.config.js +++ b/apps/example/babel.config.js @@ -1,6 +1,7 @@ module.exports = { presets: ['module:@react-native/babel-preset'], plugins: [ - 'react-native-reanimated/plugin' + 'react-native-reanimated/plugin', + 'babel-plugin-transform-import-meta' ], }; diff --git a/apps/example/package.json b/apps/example/package.json index 33827d685..4ad184bf5 100644 --- a/apps/example/package.json +++ b/apps/example/package.json @@ -21,6 +21,7 @@ "@react-navigation/native": "^6.1.17", "@react-navigation/stack": "^6.4.0", "@react-three/fiber": "^8.17.6", + "babel-plugin-transform-import-meta": "^2.2.1", "fast-text-encoding": "^1.0.6", "react": "18.2.0", "react-native": "0.74.2", diff --git a/yarn.lock b/yarn.lock index e97786fca..2b9586f84 100644 --- a/yarn.lock +++ b/yarn.lock @@ -25,6 +25,17 @@ __metadata: languageName: node linkType: hard +"@babel/code-frame@npm:^7.25.9": + version: 7.26.2 + resolution: "@babel/code-frame@npm:7.26.2" + dependencies: + "@babel/helper-validator-identifier": ^7.25.9 + js-tokens: ^4.0.0 + picocolors: ^1.0.0 + checksum: db13f5c42d54b76c1480916485e6900748bbcb0014a8aca87f50a091f70ff4e0d0a6db63cade75eb41fcc3d2b6ba0a7f89e343def4f96f00269b41b8ab8dd7b8 + languageName: node + linkType: hard + "@babel/compat-data@npm:^7.20.5, @babel/compat-data@npm:^7.22.6, @babel/compat-data@npm:^7.25.2, @babel/compat-data@npm:^7.25.4": version: 7.25.4 resolution: "@babel/compat-data@npm:7.25.4" @@ -270,6 +281,13 @@ __metadata: languageName: node linkType: hard +"@babel/helper-string-parser@npm:^7.25.9": + version: 7.25.9 + resolution: "@babel/helper-string-parser@npm:7.25.9" + checksum: 6435ee0849e101681c1849868278b5aee82686ba2c1e27280e5e8aca6233af6810d39f8e4e693d2f2a44a3728a6ccfd66f72d71826a94105b86b731697cdfa99 + languageName: node + linkType: hard + "@babel/helper-validator-identifier@npm:^7.24.7": version: 7.24.7 resolution: "@babel/helper-validator-identifier@npm:7.24.7" @@ -277,6 +295,13 @@ __metadata: languageName: node linkType: hard +"@babel/helper-validator-identifier@npm:^7.25.9": + version: 7.25.9 + resolution: "@babel/helper-validator-identifier@npm:7.25.9" + checksum: 5b85918cb1a92a7f3f508ea02699e8d2422fe17ea8e82acd445006c0ef7520fbf48e3dbcdaf7b0a1d571fc3a2715a29719e5226636cb6042e15fe6ed2a590944 + languageName: node + linkType: hard + "@babel/helper-validator-option@npm:^7.24.7, @babel/helper-validator-option@npm:^7.24.8": version: 7.24.8 resolution: "@babel/helper-validator-option@npm:7.24.8" @@ -328,6 +353,17 @@ __metadata: languageName: node linkType: hard +"@babel/parser@npm:^7.25.9": + version: 7.26.3 + resolution: "@babel/parser@npm:7.26.3" + dependencies: + "@babel/types": ^7.26.3 + bin: + parser: ./bin/babel-parser.js + checksum: e2bff2e9fa6540ee18fecc058bc74837eda2ddcecbe13454667314a93fc0ba26c1fb862c812d84f6d5f225c3bd8d191c3a42d4296e287a882c4e1f82ff2815ff + languageName: node + linkType: hard + "@babel/plugin-bugfix-firefox-class-in-computed-class-key@npm:^7.25.3": version: 7.25.3 resolution: "@babel/plugin-bugfix-firefox-class-in-computed-class-key@npm:7.25.3" @@ -1668,6 +1704,17 @@ __metadata: languageName: node linkType: hard +"@babel/template@npm:^7.4.4": + version: 7.25.9 + resolution: "@babel/template@npm:7.25.9" + dependencies: + "@babel/code-frame": ^7.25.9 + "@babel/parser": ^7.25.9 + "@babel/types": ^7.25.9 + checksum: 103641fea19c7f4e82dc913aa6b6ac157112a96d7c724d513288f538b84bae04fb87b1f1e495ac1736367b1bc30e10f058b30208fb25f66038e1f1eb4e426472 + languageName: node + linkType: hard + "@babel/traverse@npm:^7.20.0, @babel/traverse@npm:^7.24.7, @babel/traverse@npm:^7.24.8, @babel/traverse@npm:^7.25.0, @babel/traverse@npm:^7.25.1, @babel/traverse@npm:^7.25.2, @babel/traverse@npm:^7.25.3, @babel/traverse@npm:^7.25.4": version: 7.25.4 resolution: "@babel/traverse@npm:7.25.4" @@ -1694,6 +1741,16 @@ __metadata: languageName: node linkType: hard +"@babel/types@npm:^7.25.9, @babel/types@npm:^7.26.3": + version: 7.26.3 + resolution: "@babel/types@npm:7.26.3" + dependencies: + "@babel/helper-string-parser": ^7.25.9 + "@babel/helper-validator-identifier": ^7.25.9 + checksum: 195f428080dcaadbcecc9445df7f91063beeaa91b49ccd78f38a5af6b75a6a58391d0c6614edb1ea322e57889a1684a0aab8e667951f820196901dd341f931e9 + languageName: node + linkType: hard + "@bcoe/v8-coverage@npm:^0.2.3": version: 0.2.3 resolution: "@bcoe/v8-coverage@npm:0.2.3" @@ -4580,6 +4637,7 @@ __metadata: "@types/three": 0.168.0 "@webgpu/types": 0.1.49 babel-jest: ^29.6.3 + babel-plugin-transform-import-meta: ^2.2.1 eslint: ^8.19.0 eslint-config-react-native-wcandillon: ^3.10.2 fast-text-encoding: ^1.0.6 @@ -5091,6 +5149,18 @@ __metadata: languageName: node linkType: hard +"babel-plugin-transform-import-meta@npm:^2.2.1": + version: 2.2.1 + resolution: "babel-plugin-transform-import-meta@npm:2.2.1" + dependencies: + "@babel/template": ^7.4.4 + tslib: ^2.4.0 + peerDependencies: + "@babel/core": ^7.10.0 + checksum: 2868e3ac22fc403003b19750dd04f9f05303d2ade3d005e2e557ba8e260ac0b8ee3717f772ec4db8a9e6a8aac13dc61eb82b79a6479f933290cef2d89e4b731b + languageName: node + linkType: hard + "babel-preset-current-node-syntax@npm:^1.0.0": version: 1.1.0 resolution: "babel-preset-current-node-syntax@npm:1.1.0" From 1f80070284f91ef7036a4e4fc958c395a7f47247 Mon Sep 17 00:00:00 2001 From: William Candillon Date: Thu, 12 Dec 2024 10:42:40 +0100 Subject: [PATCH 04/35] :wrench: --- apps/example/ios/Podfile.lock | 4 ++-- apps/example/src/App.tsx | 2 ++ apps/example/src/Home.tsx | 4 ++++ apps/example/src/MNISTInference/MNISTInference.tsx | 5 +++++ apps/example/src/MNISTInference/index.ts | 1 + apps/example/src/Route.ts | 1 + 6 files changed, 15 insertions(+), 2 deletions(-) create mode 100644 apps/example/src/MNISTInference/MNISTInference.tsx create mode 100644 apps/example/src/MNISTInference/index.ts diff --git a/apps/example/ios/Podfile.lock b/apps/example/ios/Podfile.lock index 773ce50bf..34836dfb3 100644 --- a/apps/example/ios/Podfile.lock +++ b/apps/example/ios/Podfile.lock @@ -937,7 +937,7 @@ PODS: - React-debug - react-native-safe-area-context (4.11.0): - React-Core - - react-native-wgpu (0.1.19): + - react-native-wgpu (0.1.21): - DoubleConversion - glog - hermes-engine @@ -1528,7 +1528,7 @@ SPEC CHECKSUMS: React-logger: 29fa3e048f5f67fe396bc08af7606426d9bd7b5d React-Mapbuffer: bf56147c9775491e53122a94c423ac201417e326 react-native-safe-area-context: 851c62c48dce80ccaa5637b6aa5991a1bc36eca9 - react-native-wgpu: 5fd8cb5fd7bd00c88831d29438697d897fb680e8 + react-native-wgpu: 5308faeb6d85925394351968f7970cd00eead0cc React-nativeconfig: 9f223cd321823afdecf59ed00861ab2d69ee0fc1 React-NativeModulesApple: ff7efaff7098639db5631236cfd91d60abff04c0 React-perflogger: 32ed45d9cee02cf6639acae34251590dccd30994 diff --git a/apps/example/src/App.tsx b/apps/example/src/App.tsx index 755ef5f25..222bdd064 100644 --- a/apps/example/src/App.tsx +++ b/apps/example/src/App.tsx @@ -18,6 +18,7 @@ import { RenderBundles } from "./RenderBundles"; import { ABuffer } from "./ABuffer"; import { OcclusionQuery } from "./OcclusionQuery"; import { ComputeBoids } from "./ComputeBoids"; +import { MNISTInference } from "./MNISTInference"; import { Wireframe } from "./Wireframe"; import { Resize } from "./Resize"; import { Particules } from "./Particles"; @@ -64,6 +65,7 @@ function App() { + diff --git a/apps/example/src/Home.tsx b/apps/example/src/Home.tsx index 99884ad07..7de6d7339 100644 --- a/apps/example/src/Home.tsx +++ b/apps/example/src/Home.tsx @@ -67,6 +67,10 @@ export const examples = [ screen: "ComputeBoids", title: "🐦‍⬛ Compute Boids", }, + { + screen: "MNISTInference", + title: "1️⃣ MNIST Inference", + }, ...(Platform.OS !== "ios" ? ([ { diff --git a/apps/example/src/MNISTInference/MNISTInference.tsx b/apps/example/src/MNISTInference/MNISTInference.tsx new file mode 100644 index 000000000..3193602ff --- /dev/null +++ b/apps/example/src/MNISTInference/MNISTInference.tsx @@ -0,0 +1,5 @@ +import { View } from "react-native"; + +export const MNISTInference = () => { + return ; +}; diff --git a/apps/example/src/MNISTInference/index.ts b/apps/example/src/MNISTInference/index.ts new file mode 100644 index 000000000..d99268d25 --- /dev/null +++ b/apps/example/src/MNISTInference/index.ts @@ -0,0 +1 @@ +export * from "./MNISTInference"; diff --git a/apps/example/src/Route.ts b/apps/example/src/Route.ts index f9e88a522..1bca3e69e 100644 --- a/apps/example/src/Route.ts +++ b/apps/example/src/Route.ts @@ -12,6 +12,7 @@ export type Routes = { RenderBundles: undefined; OcclusionQuery: undefined; ComputeBoids: undefined; + MNISTInference: undefined; ABuffer: undefined; Particles: undefined; ShadowMapping: undefined; From d581dc259e8029c4d4259a81abfac163e848d214 Mon Sep 17 00:00:00 2001 From: William Candillon Date: Thu, 12 Dec 2024 11:44:35 +0100 Subject: [PATCH 05/35] :wrench: --- apps/example/ios/Podfile.lock | 27 ++ apps/example/package.json | 1 + apps/example/src/App.tsx | 2 +- apps/example/src/MNISTInference/Lib.ts | 252 ++++++++++++++++++ .../src/MNISTInference/MNISTInference.tsx | 63 ++++- yarn.lock | 33 ++- 6 files changed, 372 insertions(+), 6 deletions(-) create mode 100644 apps/example/src/MNISTInference/Lib.ts diff --git a/apps/example/ios/Podfile.lock b/apps/example/ios/Podfile.lock index 34836dfb3..78e3aed5c 100644 --- a/apps/example/ios/Podfile.lock +++ b/apps/example/ios/Podfile.lock @@ -937,6 +937,29 @@ PODS: - React-debug - react-native-safe-area-context (4.11.0): - React-Core + - react-native-skia (1.7.2): + - DoubleConversion + - glog + - hermes-engine + - RCT-Folly (= 2024.01.01.00) + - RCTRequired + - RCTTypeSafety + - React + - React-callinvoker + - React-Codegen + - React-Core + - React-debug + - React-Fabric + - React-featureflags + - React-graphics + - React-ImageManager + - React-NativeModulesApple + - React-RCTFabric + - React-rendererdebug + - React-utils + - ReactCommon/turbomodule/bridging + - ReactCommon/turbomodule/core + - Yoga - react-native-wgpu (0.1.21): - DoubleConversion - glog @@ -1336,6 +1359,7 @@ DEPENDENCIES: - React-logger (from `../../../node_modules/react-native/ReactCommon/logger`) - React-Mapbuffer (from `../../../node_modules/react-native/ReactCommon`) - react-native-safe-area-context (from `../../../node_modules/react-native-safe-area-context`) + - "react-native-skia (from `../../../node_modules/@shopify/react-native-skia`)" - react-native-wgpu (from `../../../node_modules/react-native-wgpu`) - React-nativeconfig (from `../../../node_modules/react-native/ReactCommon`) - React-NativeModulesApple (from `../../../node_modules/react-native/ReactCommon/react/nativemodule/core/platform/ios`) @@ -1435,6 +1459,8 @@ EXTERNAL SOURCES: :path: "../../../node_modules/react-native/ReactCommon" react-native-safe-area-context: :path: "../../../node_modules/react-native-safe-area-context" + react-native-skia: + :path: "../../../node_modules/@shopify/react-native-skia" react-native-wgpu: :path: "../../../node_modules/react-native-wgpu" React-nativeconfig: @@ -1528,6 +1554,7 @@ SPEC CHECKSUMS: React-logger: 29fa3e048f5f67fe396bc08af7606426d9bd7b5d React-Mapbuffer: bf56147c9775491e53122a94c423ac201417e326 react-native-safe-area-context: 851c62c48dce80ccaa5637b6aa5991a1bc36eca9 + react-native-skia: c85483f709f2b58d30a11fc005c2938ee87d6656 react-native-wgpu: 5308faeb6d85925394351968f7970cd00eead0cc React-nativeconfig: 9f223cd321823afdecf59ed00861ab2d69ee0fc1 React-NativeModulesApple: ff7efaff7098639db5631236cfd91d60abff04c0 diff --git a/apps/example/package.json b/apps/example/package.json index 2f5e0518b..768be31e6 100644 --- a/apps/example/package.json +++ b/apps/example/package.json @@ -20,6 +20,7 @@ "@react-navigation/native": "^6.1.17", "@react-navigation/stack": "^6.4.0", "@react-three/fiber": "^8.17.6", + "@shopify/react-native-skia": "^1.7.2", "fast-text-encoding": "^1.0.6", "react": "18.2.0", "react-native": "0.74.2", diff --git a/apps/example/src/App.tsx b/apps/example/src/App.tsx index 222bdd064..7fecfffc6 100644 --- a/apps/example/src/App.tsx +++ b/apps/example/src/App.tsx @@ -43,7 +43,7 @@ function App() { return ( - + { + const root = tgpu.initFromDevice({ device }); + const canvasData = new Array(SIZE ** 2).fill(0); + + // Shader code + + const layerShader = /* wgsl */ ` + @binding(0) @group(0) var input: array; + @binding(1) @group(0) var output: array; + + @binding(0) @group(1) var weights: array; + @binding(1) @group(1) var biases: array; + + fn relu(x: f32) -> f32 { + return max(0.0, x); + } + + @compute @workgroup_size(1) + fn main(@builtin(global_invocation_id) gid: vec3u) { + let inputSize = arrayLength( &input ); + + let i = gid.x; + + let weightsOffset = i * inputSize; + var sum = 0.0; + + for (var j = 0u; j < inputSize; j = j + 1) { + sum = sum + input[j] * weights[weightsOffset + j]; + } + + sum = sum + biases[i]; + output[i] = relu(sum); + } +`; + + const ReadonlyFloats = { + storage: (n: number) => arrayOf(f32, n), + access: "readonly", + } as const; + + const MutableFloats = { + storage: (n: number) => arrayOf(f32, n), + access: "mutable", + } as const; + + const ioLayout = tgpu.bindGroupLayout({ + input: ReadonlyFloats, + output: MutableFloats, + }); + + const weightsBiasesLayout = tgpu.bindGroupLayout({ + weights: ReadonlyFloats, + biases: ReadonlyFloats, + }); + + const pipeline = device.createComputePipeline({ + layout: device.createPipelineLayout({ + bindGroupLayouts: [ + root.unwrap(ioLayout), + root.unwrap(weightsBiasesLayout), + ], + }), + compute: { + module: device.createShaderModule({ + code: layerShader, + }), + }, + }); + + // Definitions for the network + + interface LayerData { + shape: readonly [number] | readonly [number, number]; + buffer: TgpuBuffer> & Storage; + } + + interface Layer { + weights: TgpuBuffer> & Storage; + biases: TgpuBuffer> & Storage; + state: TgpuBuffer> & Storage; + } + + interface Network { + layers: Layer[]; + input: TgpuBuffer> & Storage; + output: TgpuBuffer> & Storage; + + inference(data: number[]): Promise; + } + + /** + * Creates a network from a list of pairs of weights and biases + * + * It automates the creation of state buffers that are used to store the intermediate results of the network + * as well as the input layer buffer + * + * It provides an inference function that takes an array of input data and returns an array of output data + */ + function createNetwork(layers: [LayerData, LayerData][]): Network { + const buffers = layers.map(([weights, biases]) => { + if (weights.shape[1] !== biases.shape[0]) { + throw new Error(`Shape mismatch: ${weights.shape} and ${biases.shape}`); + } + + return { + weights: weights.buffer, + biases: biases.buffer, + state: root + .createBuffer(arrayOf(f32, biases.shape[0])) + .$usage("storage"), + }; + }); + + const input = root + .createBuffer(arrayOf(f32, layers[0][0].shape[0])) + .$usage("storage"); + const output = buffers[buffers.length - 1].state; + + const ioBindGroups = buffers.map((_, i) => + ioLayout.populate({ + input: i === 0 ? input : buffers[i - 1].state, + output: buffers[i].state, + }), + ); + + const weightsBindGroups = buffers.map((layer) => + weightsBiasesLayout.populate({ + weights: layer.weights, + biases: layer.biases, + }), + ); + + async function inference(data: number[]): Promise { + // verify the length of the data matches the input layer + if (data.length !== layers[0][0].shape[0]) { + throw new Error( + `Data length ${data.length} does not match input shape ${layers[0][0].shape[0]}`, + ); + } + input.write(data); + + // Run the network + const encoder = device.createCommandEncoder(); + for (let i = 0; i < buffers.length; i++) { + const pass = encoder.beginComputePass(); + pass.setPipeline(pipeline); + pass.setBindGroup(0, root.unwrap(ioBindGroups[i])); + pass.setBindGroup(1, root.unwrap(weightsBindGroups[i])); + pass.dispatchWorkgroups(buffers[i].biases.dataType.size); //.length + pass.end(); + } + device.queue.submit([encoder.finish()]); + await device.queue.onSubmittedWorkDone(); + + // Read the output + return await output.read(); + } + + return { + layers: buffers, + input, + output, + inference, + }; + } + + const network = createNetwork(await downloadLayers()); + + // #region Downloading weights & biases + + /** + * Create a LayerData object from a layer ArrayBuffer + * + * The function extracts the header, shape and data from the layer + * If there are any issues with the layer, an error is thrown + * + * Automatically creates appropriate buffer initialized with the data + */ + function getLayerData(layer: ArrayBuffer): LayerData { + const headerLen = new Uint16Array(layer.slice(8, 10)); + + const header = new TextDecoder().decode( + new Uint8Array(layer.slice(10, 10 + headerLen[0])), + ); + + // shape can be found in the header in the format: 'shape': (x, y) or 'shape': (x,) for bias + const shapeMatch = header.match(/'shape': \((\d+), ?(\d+)?\)/); + if (!shapeMatch) { + throw new Error("Shape not found in header"); + } + + // To accommodate .npy weirdness - if we have a 2d shape we need to switch the order + const X = Number.parseInt(shapeMatch[1], 10); + const Y = Number.parseInt(shapeMatch[2], 10); + const shape = Number.isNaN(Y) ? ([X] as const) : ([Y, X] as const); + + const data = new Float32Array(layer.slice(10 + headerLen[0])); + + // Verify the length of the data matches the shape + if (data.length !== shape[0] * (shape[1] || 1)) { + throw new Error( + `Data length ${data.length} does not match shape ${shape}`, + ); + } + + const buffer = root + .createBuffer(arrayOf(f32, data.length), [...data]) + .$usage("storage"); + + return { + shape, + buffer, + }; + } + + function downloadLayers(): Promise<[LayerData, LayerData][]> { + const downloadLayer = async (fileName: string): Promise => { + const buffer = await fetch( + `/TypeGPU/assets/mnist-weights/${fileName}`, + ).then((res) => res.arrayBuffer()); + + return getLayerData(buffer); + }; + + return Promise.all( + [0, 1, 2, 3, 4, 5, 6, 7].map((layer) => + Promise.all([ + downloadLayer(`layer${layer}.weight.npy`), + downloadLayer(`layer${layer}.bias.npy`), + ]), + ), + ); + } + + // #endregion + + // #region User Interface + + // #endregion + + // #region Resource cleanup + + function onCleanup() { + root.destroy(); + } + + // #endregion +}; diff --git a/apps/example/src/MNISTInference/MNISTInference.tsx b/apps/example/src/MNISTInference/MNISTInference.tsx index 3193602ff..76f1497cb 100644 --- a/apps/example/src/MNISTInference/MNISTInference.tsx +++ b/apps/example/src/MNISTInference/MNISTInference.tsx @@ -1,5 +1,60 @@ -import { View } from "react-native"; +import React from "react"; +import { Dimensions, StyleSheet, View } from "react-native"; +import type { SkImage } from "@shopify/react-native-skia"; +import { + Canvas, + Fill, + Skia, + Image, + PaintStyle, +} from "@shopify/react-native-skia"; +import { Gesture, GestureDetector } from "react-native-gesture-handler"; +import { useSharedValue } from "react-native-reanimated"; -export const MNISTInference = () => { - return ; -}; +import { SIZE } from "./Lib"; + +const { width } = Dimensions.get("window"); + +const surface = Skia.Surface.MakeOffscreen(SIZE, SIZE)!; +const path = Skia.Path.Make(); +path.moveTo(0, 0); +path.lineTo(SIZE, SIZE); +path.close(); +const paint = Skia.Paint(); +paint.setColor(Skia.Color("black")); +paint.setStyle(PaintStyle.Stroke); +paint.setStrokeWidth(1); +surface.getCanvas().drawPath(path, paint); +surface.flush(); +const img = surface.makeImageSnapshot().makeNonTextureImage(); + +export function MNISTInference() { + const image = useSharedValue(img); + const gesture = Gesture.Pan().onChange((e) => {}); + return ( + + + + + + + + ); +} + +const style = StyleSheet.create({ + container: { + flex: 1, + }, + canvas: { + width, + height: width, + }, +}); diff --git a/yarn.lock b/yarn.lock index 98d62110c..dc5cc62c0 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3339,6 +3339,27 @@ __metadata: languageName: node linkType: hard +"@shopify/react-native-skia@npm:^1.7.2": + version: 1.7.2 + resolution: "@shopify/react-native-skia@npm:1.7.2" + dependencies: + canvaskit-wasm: 0.39.1 + react-reconciler: 0.27.0 + peerDependencies: + react: ">=18.0" + react-native: ">=0.64" + react-native-reanimated: ">=2.0.0" + peerDependenciesMeta: + react-native: + optional: true + react-native-reanimated: + optional: true + bin: + setup-skia-web: ./scripts/setup-canvaskit.js + checksum: b23ba33f23150199cdbf973b50580f756b94763d9331d129b0844cb8a6db618e2257df2759601b27ca91d7b8f41bf550f7965bb16f4ff8b15b07b082110da489 + languageName: node + linkType: hard + "@sideway/address@npm:^4.1.5": version: 4.1.5 resolution: "@sideway/address@npm:4.1.5" @@ -4279,6 +4300,7 @@ __metadata: "@react-navigation/stack": ^6.4.0 "@react-three/fiber": ^8.17.6 "@rnx-kit/metro-config": ^1.3.17 + "@shopify/react-native-skia": ^1.7.2 "@types/node": ^20.14.7 "@types/react": ^18.2.6 "@types/react-test-renderer": ^18.0.0 @@ -5098,6 +5120,15 @@ __metadata: languageName: node linkType: hard +"canvaskit-wasm@npm:0.39.1": + version: 0.39.1 + resolution: "canvaskit-wasm@npm:0.39.1" + dependencies: + "@webgpu/types": 0.1.21 + checksum: da62926fc81f424a781e148b4d76bb5fc9b0188f136090b3b287522dc653cb002bfb406e2eff45b55fcc1cafbc7629f988e20ad6c777bab85c1bb09e1091a5e2 + languageName: node + linkType: hard + "chalk@npm:^2.4.2": version: 2.4.2 resolution: "chalk@npm:2.4.2" @@ -10703,7 +10734,7 @@ __metadata: languageName: node linkType: hard -"react-reconciler@npm:^0.27.0": +"react-reconciler@npm:0.27.0, react-reconciler@npm:^0.27.0": version: 0.27.0 resolution: "react-reconciler@npm:0.27.0" dependencies: From 8250410ec49d4eb5498ea41c4b807d3ce0857710 Mon Sep 17 00:00:00 2001 From: William Candillon Date: Thu, 12 Dec 2024 11:55:05 +0100 Subject: [PATCH 06/35] :wrench: --- .../src/MNISTInference/MNISTInference.tsx | 30 ++++++++++++------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/apps/example/src/MNISTInference/MNISTInference.tsx b/apps/example/src/MNISTInference/MNISTInference.tsx index 76f1497cb..26288c307 100644 --- a/apps/example/src/MNISTInference/MNISTInference.tsx +++ b/apps/example/src/MNISTInference/MNISTInference.tsx @@ -1,6 +1,6 @@ import React from "react"; import { Dimensions, StyleSheet, View } from "react-native"; -import type { SkImage } from "@shopify/react-native-skia"; +import type { SkImage, SkSurface } from "@shopify/react-native-skia"; import { Canvas, Fill, @@ -15,22 +15,30 @@ import { SIZE } from "./Lib"; const { width } = Dimensions.get("window"); -const surface = Skia.Surface.MakeOffscreen(SIZE, SIZE)!; -const path = Skia.Path.Make(); -path.moveTo(0, 0); -path.lineTo(SIZE, SIZE); -path.close(); const paint = Skia.Paint(); paint.setColor(Skia.Color("black")); paint.setStyle(PaintStyle.Stroke); paint.setStrokeWidth(1); -surface.getCanvas().drawPath(path, paint); -surface.flush(); -const img = surface.makeImageSnapshot().makeNonTextureImage(); + +const f = (1 / width) * SIZE; export function MNISTInference() { - const image = useSharedValue(img); - const gesture = Gesture.Pan().onChange((e) => {}); + const surface = useSharedValue(null); + const path = useSharedValue(Skia.Path.Make()); + const image = useSharedValue(null); + const gesture = Gesture.Pan() + .onStart((e) => { + path.value.moveTo(e.x * f, e.y * f); + }) + .onChange((e) => { + path.value.lineTo(e.x * f, e.y * f); + if (surface.value === null) { + surface.value = Skia.Surface.MakeOffscreen(SIZE, SIZE)!; + } + surface.value.getCanvas().drawPath(path.value, paint); + surface.value.flush(); + image.value = surface.value.makeImageSnapshot(); + }); return ( From f69ad3ea614897b27f2b362234178eca5ab030df Mon Sep 17 00:00:00 2001 From: William Candillon Date: Thu, 12 Dec 2024 12:02:43 +0100 Subject: [PATCH 07/35] :wrench: --- .../src/MNISTInference/MNISTInference.tsx | 29 +++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/apps/example/src/MNISTInference/MNISTInference.tsx b/apps/example/src/MNISTInference/MNISTInference.tsx index 26288c307..0f4e663d2 100644 --- a/apps/example/src/MNISTInference/MNISTInference.tsx +++ b/apps/example/src/MNISTInference/MNISTInference.tsx @@ -7,6 +7,7 @@ import { Skia, Image, PaintStyle, + Path, } from "@shopify/react-native-skia"; import { Gesture, GestureDetector } from "react-native-gesture-handler"; import { useSharedValue } from "react-native-reanimated"; @@ -18,9 +19,26 @@ const { width } = Dimensions.get("window"); const paint = Skia.Paint(); paint.setColor(Skia.Color("black")); paint.setStyle(PaintStyle.Stroke); -paint.setStrokeWidth(1); +paint.setStrokeWidth(0.5); -const f = (1 / width) * SIZE; +const grid = Skia.Path.Make(); +const cellSize = width / SIZE; + +grid.moveTo(0, 0); + +// Draw vertical lines +for (let i = 0; i <= SIZE; i++) { + grid.moveTo(i * cellSize, 0); + grid.lineTo(i * cellSize, width); +} + +// Draw horizontal lines +for (let i = 0; i <= SIZE; i++) { + grid.moveTo(0, i * cellSize); + grid.lineTo(width, i * cellSize); +} + +const f = 1 / cellSize; export function MNISTInference() { const surface = useSharedValue(null); @@ -43,6 +61,13 @@ export function MNISTInference() { + + Date: Thu, 12 Dec 2024 12:07:49 +0100 Subject: [PATCH 08/35] :wrench: --- apps/example/src/MNISTInference/Lib.ts | 5 ++--- apps/example/src/MNISTInference/MNISTInference.tsx | 13 +++++++++++-- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/apps/example/src/MNISTInference/Lib.ts b/apps/example/src/MNISTInference/Lib.ts index 784b56077..23a6421a7 100644 --- a/apps/example/src/MNISTInference/Lib.ts +++ b/apps/example/src/MNISTInference/Lib.ts @@ -4,7 +4,6 @@ import { type F32, type TgpuArray, arrayOf, f32 } from "typegpu/data"; export const SIZE = 28; export const createDemo = async (device: GPUDevice) => { const root = tgpu.initFromDevice({ device }); - const canvasData = new Array(SIZE ** 2).fill(0); // Shader code @@ -150,7 +149,7 @@ export const createDemo = async (device: GPUDevice) => { pass.setPipeline(pipeline); pass.setBindGroup(0, root.unwrap(ioBindGroups[i])); pass.setBindGroup(1, root.unwrap(weightsBindGroups[i])); - pass.dispatchWorkgroups(buffers[i].biases.dataType.size); //.length + pass.dispatchWorkgroups(buffers[i].biases.dataType.elementCount); //.length pass.end(); } device.queue.submit([encoder.finish()]); @@ -220,7 +219,7 @@ export const createDemo = async (device: GPUDevice) => { function downloadLayers(): Promise<[LayerData, LayerData][]> { const downloadLayer = async (fileName: string): Promise => { const buffer = await fetch( - `/TypeGPU/assets/mnist-weights/${fileName}`, + `https://docs.swmansion.com/TypeGPU/assets/mnist-weights/${fileName}`, ).then((res) => res.arrayBuffer()); return getLayerData(buffer); diff --git a/apps/example/src/MNISTInference/MNISTInference.tsx b/apps/example/src/MNISTInference/MNISTInference.tsx index 0f4e663d2..647c2a99f 100644 --- a/apps/example/src/MNISTInference/MNISTInference.tsx +++ b/apps/example/src/MNISTInference/MNISTInference.tsx @@ -1,4 +1,4 @@ -import React from "react"; +import React, { useEffect } from "react"; import { Dimensions, StyleSheet, View } from "react-native"; import type { SkImage, SkSurface } from "@shopify/react-native-skia"; import { @@ -11,8 +11,9 @@ import { } from "@shopify/react-native-skia"; import { Gesture, GestureDetector } from "react-native-gesture-handler"; import { useSharedValue } from "react-native-reanimated"; +import { useDevice } from "react-native-wgpu"; -import { SIZE } from "./Lib"; +import { createDemo, SIZE } from "./Lib"; const { width } = Dimensions.get("window"); @@ -41,6 +42,7 @@ for (let i = 0; i <= SIZE; i++) { const f = 1 / cellSize; export function MNISTInference() { + const { device } = useDevice(); const surface = useSharedValue(null); const path = useSharedValue(Skia.Path.Make()); const image = useSharedValue(null); @@ -57,6 +59,13 @@ export function MNISTInference() { surface.value.flush(); image.value = surface.value.makeImageSnapshot(); }); + useEffect(() => { + (async () => { + if (device) { + createDemo(device); + } + })(); + }, [device]); return ( From e53d01df74773679c5557bcf7f187c0429b01baf Mon Sep 17 00:00:00 2001 From: William Candillon Date: Thu, 12 Dec 2024 13:01:23 +0100 Subject: [PATCH 09/35] :wrench: --- apps/example/src/MNISTInference/Lib.ts | 70 +++++++++++++------ .../src/MNISTInference/MNISTInference.tsx | 41 +++++++++-- 2 files changed, 83 insertions(+), 28 deletions(-) diff --git a/apps/example/src/MNISTInference/Lib.ts b/apps/example/src/MNISTInference/Lib.ts index 23a6421a7..d9e7cc59d 100644 --- a/apps/example/src/MNISTInference/Lib.ts +++ b/apps/example/src/MNISTInference/Lib.ts @@ -2,6 +2,53 @@ import tgpu, { type TgpuBuffer, type Storage } from "typegpu"; import { type F32, type TgpuArray, arrayOf, f32 } from "typegpu/data"; export const SIZE = 28; + +// Definitions for the network + +interface LayerData { + shape: readonly [number] | readonly [number, number]; + buffer: TgpuBuffer> & Storage; +} + +interface Layer { + weights: TgpuBuffer> & Storage; + biases: TgpuBuffer> & Storage; + state: TgpuBuffer> & Storage; +} + +export interface Network { + layers: Layer[]; + input: TgpuBuffer> & Storage; + output: TgpuBuffer> & Storage; + + inference(data: number[]): Promise; +} + +export const centerData = (data: Uint8Array) => { + "worklet"; + const mass = data.reduce((acc, value) => acc + value, 0); + const x = data.reduce((acc, value, i) => acc + value * (i % SIZE), 0) / mass; + const y = + data.reduce((acc, value, i) => acc + value * Math.floor(i / SIZE), 0) / + mass; + + const offsetX = Math.round(SIZE / 2 - x); + const offsetY = Math.round(SIZE / 2 - y); + + const newData = new Array(SIZE * SIZE).fill(0); + for (let i = 0; i < SIZE; i++) { + for (let j = 0; j < SIZE; j++) { + const index = i * SIZE + j; + const newIndex = (i + offsetY) * SIZE + j + offsetX; + if (newIndex >= 0 && newIndex < SIZE * SIZE) { + newData[newIndex] = data[index]; + } + } + } + + return newData; +}; + export const createDemo = async (device: GPUDevice) => { const root = tgpu.initFromDevice({ device }); @@ -70,27 +117,6 @@ export const createDemo = async (device: GPUDevice) => { }, }); - // Definitions for the network - - interface LayerData { - shape: readonly [number] | readonly [number, number]; - buffer: TgpuBuffer> & Storage; - } - - interface Layer { - weights: TgpuBuffer> & Storage; - biases: TgpuBuffer> & Storage; - state: TgpuBuffer> & Storage; - } - - interface Network { - layers: Layer[]; - input: TgpuBuffer> & Storage; - output: TgpuBuffer> & Storage; - - inference(data: number[]): Promise; - } - /** * Creates a network from a list of pairs of weights and biases * @@ -246,6 +272,6 @@ export const createDemo = async (device: GPUDevice) => { function onCleanup() { root.destroy(); } - + return network; // #endregion }; diff --git a/apps/example/src/MNISTInference/MNISTInference.tsx b/apps/example/src/MNISTInference/MNISTInference.tsx index 647c2a99f..25a48a56c 100644 --- a/apps/example/src/MNISTInference/MNISTInference.tsx +++ b/apps/example/src/MNISTInference/MNISTInference.tsx @@ -1,4 +1,4 @@ -import React, { useEffect } from "react"; +import React, { useCallback, useEffect, useRef } from "react"; import { Dimensions, StyleSheet, View } from "react-native"; import type { SkImage, SkSurface } from "@shopify/react-native-skia"; import { @@ -8,12 +8,15 @@ import { Image, PaintStyle, Path, + ColorType, + AlphaType, } from "@shopify/react-native-skia"; import { Gesture, GestureDetector } from "react-native-gesture-handler"; -import { useSharedValue } from "react-native-reanimated"; +import { runOnJS, runOnUI, useSharedValue } from "react-native-reanimated"; import { useDevice } from "react-native-wgpu"; -import { createDemo, SIZE } from "./Lib"; +import type { Network } from "./Lib"; +import { createDemo, centerData, SIZE } from "./Lib"; const { width } = Dimensions.get("window"); @@ -43,9 +46,21 @@ const f = 1 / cellSize; export function MNISTInference() { const { device } = useDevice(); + const network = useRef(); const surface = useSharedValue(null); const path = useSharedValue(Skia.Path.Make()); const image = useSharedValue(null); + const runInference = useCallback(async (data: number[]) => { + if (network.current === undefined) { + return; + } + const certainties = await network.current.inference(data); + const max = Math.max(...certainties); + const index = certainties.indexOf(max); + const sum = certainties.reduce((a, b) => a + b, 0); + const normalized = certainties.map((x) => x / sum); + console.log("Result:", index, normalized); + }, []); const gesture = Gesture.Pan() .onStart((e) => { path.value.moveTo(e.x * f, e.y * f); @@ -53,19 +68,33 @@ export function MNISTInference() { .onChange((e) => { path.value.lineTo(e.x * f, e.y * f); if (surface.value === null) { - surface.value = Skia.Surface.MakeOffscreen(SIZE, SIZE)!; + return; } + console.log("draw"); surface.value.getCanvas().drawPath(path.value, paint); surface.value.flush(); image.value = surface.value.makeImageSnapshot(); + const pixels = surface.value.getCanvas().readPixels(0, 0, { + width: SIZE, + height: SIZE, + alphaType: AlphaType.Unpremul, + colorType: ColorType.RGBA_8888, + }); + runOnJS(runInference)( + centerData(pixels!).map((x) => (x / 255) * 3.24 - 0.42), + ); }); useEffect(() => { + runOnUI(() => { + surface.value = Skia.Surface.MakeOffscreen(SIZE, SIZE)!; + })(); (async () => { if (device) { - createDemo(device); + const demo = await createDemo(device); + network.current = demo; } })(); - }, [device]); + }, [device, network, surface]); return ( From ade7d8b3d9cd7f988495be575153c6927e94c6f1 Mon Sep 17 00:00:00 2001 From: William Candillon Date: Thu, 12 Dec 2024 13:22:11 +0100 Subject: [PATCH 10/35] :wrench: --- apps/example/src/MNISTInference/MNISTInference.tsx | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/apps/example/src/MNISTInference/MNISTInference.tsx b/apps/example/src/MNISTInference/MNISTInference.tsx index 25a48a56c..92a9c40b3 100644 --- a/apps/example/src/MNISTInference/MNISTInference.tsx +++ b/apps/example/src/MNISTInference/MNISTInference.tsx @@ -77,9 +77,11 @@ export function MNISTInference() { const pixels = surface.value.getCanvas().readPixels(0, 0, { width: SIZE, height: SIZE, - alphaType: AlphaType.Unpremul, - colorType: ColorType.RGBA_8888, + alphaType: AlphaType.Opaque, + colorType: ColorType.Alpha_8, }); + console.log(pixels?.slice(0, 16)); + console.log(pixels?.length === SIZE * SIZE); runOnJS(runInference)( centerData(pixels!).map((x) => (x / 255) * 3.24 - 0.42), ); From 6c17cb5d7f340d5ad6c5f9035a740dc9bb337580 Mon Sep 17 00:00:00 2001 From: William Candillon Date: Thu, 12 Dec 2024 15:06:42 +0100 Subject: [PATCH 11/35] :wrench: --- apps/example/src/MNISTInference/Lib.ts | 2 +- .../src/MNISTInference/MNISTInference.tsx | 51 ++++++++++++------- 2 files changed, 33 insertions(+), 20 deletions(-) diff --git a/apps/example/src/MNISTInference/Lib.ts b/apps/example/src/MNISTInference/Lib.ts index d9e7cc59d..a34fa817e 100644 --- a/apps/example/src/MNISTInference/Lib.ts +++ b/apps/example/src/MNISTInference/Lib.ts @@ -272,6 +272,6 @@ export const createDemo = async (device: GPUDevice) => { function onCleanup() { root.destroy(); } - return network; + return { network, onCleanup }; // #endregion }; diff --git a/apps/example/src/MNISTInference/MNISTInference.tsx b/apps/example/src/MNISTInference/MNISTInference.tsx index 92a9c40b3..34a90efb7 100644 --- a/apps/example/src/MNISTInference/MNISTInference.tsx +++ b/apps/example/src/MNISTInference/MNISTInference.tsx @@ -1,5 +1,5 @@ import React, { useCallback, useEffect, useRef } from "react"; -import { Dimensions, StyleSheet, View } from "react-native"; +import { Dimensions, Platform, StyleSheet, View } from "react-native"; import type { SkImage, SkSurface } from "@shopify/react-native-skia"; import { Canvas, @@ -10,8 +10,11 @@ import { Path, ColorType, AlphaType, + matchFont, + Text, } from "@shopify/react-native-skia"; import { Gesture, GestureDetector } from "react-native-gesture-handler"; +import type { SharedValue } from "react-native-reanimated"; import { runOnJS, runOnUI, useSharedValue } from "react-native-reanimated"; import { useDevice } from "react-native-wgpu"; @@ -20,6 +23,13 @@ import { createDemo, centerData, SIZE } from "./Lib"; const { width } = Dimensions.get("window"); +const fontFamily = Platform.select({ ios: "Helvetica", default: "serif" }); +const fontStyle = { + fontFamily, + fontSize: 200, +}; +const font = matchFont(fontStyle); + const paint = Skia.Paint(); paint.setColor(Skia.Color("black")); paint.setStyle(PaintStyle.Stroke); @@ -47,20 +57,25 @@ const f = 1 / cellSize; export function MNISTInference() { const { device } = useDevice(); const network = useRef(); + const text = useSharedValue("0"); const surface = useSharedValue(null); const path = useSharedValue(Skia.Path.Make()); const image = useSharedValue(null); - const runInference = useCallback(async (data: number[]) => { - if (network.current === undefined) { - return; - } - const certainties = await network.current.inference(data); - const max = Math.max(...certainties); - const index = certainties.indexOf(max); - const sum = certainties.reduce((a, b) => a + b, 0); - const normalized = certainties.map((x) => x / sum); - console.log("Result:", index, normalized); - }, []); + const runInference = useCallback( + async (data: number[]) => { + if (network.current === undefined) { + return; + } + const certainties = await network.current.inference(data); + const max = Math.max(...certainties); + const index = certainties.indexOf(max); + //const sum = certainties.reduce((a, b) => a + b, 0); + //const normalized = certainties.map((x) => x / sum); + console.log(`${index}`); + text.value = `${index}`; + }, + [text], + ); const gesture = Gesture.Pan() .onStart((e) => { path.value.moveTo(e.x * f, e.y * f); @@ -70,7 +85,6 @@ export function MNISTInference() { if (surface.value === null) { return; } - console.log("draw"); surface.value.getCanvas().drawPath(path.value, paint); surface.value.flush(); image.value = surface.value.makeImageSnapshot(); @@ -79,11 +93,9 @@ export function MNISTInference() { height: SIZE, alphaType: AlphaType.Opaque, colorType: ColorType.Alpha_8, - }); - console.log(pixels?.slice(0, 16)); - console.log(pixels?.length === SIZE * SIZE); + })!; runOnJS(runInference)( - centerData(pixels!).map((x) => (x / 255) * 3.24 - 0.42), + centerData(pixels as Uint8Array).map((x) => (x / 255) * 3.24 - 0.42), ); }); useEffect(() => { @@ -93,7 +105,7 @@ export function MNISTInference() { (async () => { if (device) { const demo = await createDemo(device); - network.current = demo; + network.current = demo.network; } })(); }, [device, network, surface]); @@ -116,6 +128,7 @@ export function MNISTInference() { height={width} fit="cover" /> + @@ -128,6 +141,6 @@ const style = StyleSheet.create({ }, canvas: { width, - height: width, + height: width * 2, }, }); From dfe3d77350dded6a8d8b02ca17e77179b4d61bda Mon Sep 17 00:00:00 2001 From: William Candillon Date: Thu, 12 Dec 2024 15:52:53 +0100 Subject: [PATCH 12/35] :wrench: --- apps/example/package.json | 1 - .../src/TransformerJS/TransformerJS.tsx | 21 +- apps/example/src/TransformerJS/runtime.js | 28 + .../example/src/TransformerJS/transformers.js | 37540 ++++++++++++++++ yarn.lock | 507 +- 5 files changed, 37588 insertions(+), 509 deletions(-) create mode 100644 apps/example/src/TransformerJS/runtime.js create mode 100644 apps/example/src/TransformerJS/transformers.js diff --git a/apps/example/package.json b/apps/example/package.json index 4ad184bf5..859a494d4 100644 --- a/apps/example/package.json +++ b/apps/example/package.json @@ -17,7 +17,6 @@ }, "dependencies": { "@callstack/react-native-visionos": "^0.74.0", - "@huggingface/transformers": "^3.1.2", "@react-navigation/native": "^6.1.17", "@react-navigation/stack": "^6.4.0", "@react-three/fiber": "^8.17.6", diff --git a/apps/example/src/TransformerJS/TransformerJS.tsx b/apps/example/src/TransformerJS/TransformerJS.tsx index e15d1a41c..993cf5b19 100644 --- a/apps/example/src/TransformerJS/TransformerJS.tsx +++ b/apps/example/src/TransformerJS/TransformerJS.tsx @@ -1,13 +1,24 @@ // @ts-nocheck /* eslint-disable camelcase */ +import { View } from "react-native"; + import { AutoTokenizer, AutoModelForCausalLM, TextStreamer, InterruptableStoppingCriteria, -} from "@huggingface/transformers"; -import { View } from "react-native"; +} from "./transformers"; + +class Self { + constructor() {} + + postMessage(data) { + console.log(data); + } +} + +const self = new Self(); /** * This class uses the Singleton pattern to enable lazy-loading of the pipeline @@ -135,7 +146,11 @@ async function load() { self.postMessage({ status: "ready" }); } -load(); +load().then((r) => { + console.log(r); + + check(); +}); // Listen for messages from the main thread // self.addEventListener("message", async (e) => { diff --git a/apps/example/src/TransformerJS/runtime.js b/apps/example/src/TransformerJS/runtime.js new file mode 100644 index 000000000..84fb9e48a --- /dev/null +++ b/apps/example/src/TransformerJS/runtime.js @@ -0,0 +1,28 @@ +var __defProp = Object.defineProperty; +var __getOwnPropNames = Object.getOwnPropertyNames; +var __typeError = (msg) => { + throw TypeError(msg); +}; +var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; +var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, { + get: (a, b) => (typeof require !== "undefined" ? require : a)[b] +}) : x)(function(x) { + if (typeof require !== "undefined") return require.apply(this, arguments); + throw Error('Dynamic require of "' + x + '" is not supported'); +}); +var __commonJS = (cb, mod) => function __require2() { + return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports; +}; +var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value); +var __accessCheck = (obj, member, msg) => member.has(obj) || __typeError("Cannot " + msg); +var __privateAdd = (obj, member, value) => member.has(obj) ? __typeError("Cannot add the same private member more than once") : member instanceof WeakSet ? member.add(obj) : member.set(obj, value); +var __privateMethod = (obj, member, method) => (__accessCheck(obj, member, "access private method"), method); + +export { + __require, + __commonJS, + __publicField, + __privateAdd, + __privateMethod +}; +//# sourceMappingURL=chunk-KZF2VKC5.js.map diff --git a/apps/example/src/TransformerJS/transformers.js b/apps/example/src/TransformerJS/transformers.js new file mode 100644 index 000000000..aa6d8658a --- /dev/null +++ b/apps/example/src/TransformerJS/transformers.js @@ -0,0 +1,37540 @@ +import { + __publicField, + __require + } from "./runtime"; + +const META_URL = "http://localhost:5173/"; + + // node_modules/@huggingface/transformers/dist/transformers.js + var __webpack_modules__ = { + /***/ + "./node_modules/onnxruntime-web/dist/ort-wasm-simd-threaded.jsep.wasm": ( + /*!****************************************************************************!*\ + !*** ./node_modules/onnxruntime-web/dist/ort-wasm-simd-threaded.jsep.wasm ***! + \****************************************************************************/ + /***/ + (module, __unused_webpack_exports, __webpack_require__2) => { + module.exports = __webpack_require__2.p + "ort-wasm-simd-threaded.jsep.wasm"; + } + ), + /***/ + "?2ce3": ( + /*!**********************************!*\ + !*** onnxruntime-node (ignored) ***! + \**********************************/ + /***/ + () => { + } + ), + /***/ + "?7a2c": ( + /*!********************!*\ + !*** fs (ignored) ***! + \********************/ + /***/ + () => { + } + ), + /***/ + "?a42a": ( + /*!**********************!*\ + !*** path (ignored) ***! + \**********************/ + /***/ + () => { + } + ), + /***/ + "?2b25": ( + /*!***********************!*\ + !*** sharp (ignored) ***! + \***********************/ + /***/ + () => { + } + ), + /***/ + "?569f": ( + /*!********************!*\ + !*** fs (ignored) ***! + \********************/ + /***/ + () => { + } + ), + /***/ + "?3f59": ( + /*!**********************!*\ + !*** path (ignored) ***! + \**********************/ + /***/ + () => { + } + ), + /***/ + "?154a": ( + /*!*********************!*\ + !*** url (ignored) ***! + \*********************/ + /***/ + () => { + } + ), + /***/ + "./node_modules/@huggingface/jinja/dist/index.js": ( + /*!*******************************************************!*\ + !*** ./node_modules/@huggingface/jinja/dist/index.js ***! + \*******************************************************/ + /***/ + (__unused_webpack___webpack_module__, __webpack_exports__2, __webpack_require__2) => { + __webpack_require__2.r(__webpack_exports__2); + __webpack_require__2.d(__webpack_exports__2, { + /* harmony export */ + Environment: () => ( + /* binding */ + Environment + ), + /* harmony export */ + Interpreter: () => ( + /* binding */ + Interpreter + ), + /* harmony export */ + Template: () => ( + /* binding */ + Template + ), + /* harmony export */ + parse: () => ( + /* binding */ + parse + ), + /* harmony export */ + tokenize: () => ( + /* binding */ + tokenize + ) + /* harmony export */ + }); + var TOKEN_TYPES = Object.freeze({ + Text: "Text", + // The text between Jinja statements or expressions + NumericLiteral: "NumericLiteral", + // e.g., 123 + BooleanLiteral: "BooleanLiteral", + // true or false + StringLiteral: "StringLiteral", + // 'string' + Identifier: "Identifier", + // Variables, functions, etc. + Equals: "Equals", + // = + OpenParen: "OpenParen", + // ( + CloseParen: "CloseParen", + // ) + OpenStatement: "OpenStatement", + // {% + CloseStatement: "CloseStatement", + // %} + OpenExpression: "OpenExpression", + // {{ + CloseExpression: "CloseExpression", + // }} + OpenSquareBracket: "OpenSquareBracket", + // [ + CloseSquareBracket: "CloseSquareBracket", + // ] + OpenCurlyBracket: "OpenCurlyBracket", + // { + CloseCurlyBracket: "CloseCurlyBracket", + // } + Comma: "Comma", + // , + Dot: "Dot", + // . + Colon: "Colon", + // : + Pipe: "Pipe", + // | + CallOperator: "CallOperator", + // () + AdditiveBinaryOperator: "AdditiveBinaryOperator", + // + - + MultiplicativeBinaryOperator: "MultiplicativeBinaryOperator", + // * / % + ComparisonBinaryOperator: "ComparisonBinaryOperator", + // < > <= >= == != + UnaryOperator: "UnaryOperator", + // ! - + + // Keywords + Set: "Set", + If: "If", + For: "For", + In: "In", + Is: "Is", + NotIn: "NotIn", + Else: "Else", + EndIf: "EndIf", + ElseIf: "ElseIf", + EndFor: "EndFor", + And: "And", + Or: "Or", + Not: "UnaryOperator", + Macro: "Macro", + EndMacro: "EndMacro" + }); + var KEYWORDS = Object.freeze({ + set: TOKEN_TYPES.Set, + for: TOKEN_TYPES.For, + in: TOKEN_TYPES.In, + is: TOKEN_TYPES.Is, + if: TOKEN_TYPES.If, + else: TOKEN_TYPES.Else, + endif: TOKEN_TYPES.EndIf, + elif: TOKEN_TYPES.ElseIf, + endfor: TOKEN_TYPES.EndFor, + and: TOKEN_TYPES.And, + or: TOKEN_TYPES.Or, + not: TOKEN_TYPES.Not, + "not in": TOKEN_TYPES.NotIn, + macro: TOKEN_TYPES.Macro, + endmacro: TOKEN_TYPES.EndMacro, + // Literals + true: TOKEN_TYPES.BooleanLiteral, + false: TOKEN_TYPES.BooleanLiteral, + // NOTE: According to the Jinja docs: The special constants true, false, and none are indeed lowercase. + // Because that caused confusion in the past, (True used to expand to an undefined variable that was considered false), + // all three can now also be written in title case (True, False, and None). However, for consistency, (all Jinja identifiers are lowercase) + // you should use the lowercase versions. + True: TOKEN_TYPES.BooleanLiteral, + False: TOKEN_TYPES.BooleanLiteral + }); + var Token = class { + /** + * Constructs a new Token. + * @param {string} value The raw value as seen inside the source code. + * @param {TokenType} type The type of token. + */ + constructor(value, type) { + this.value = value; + this.type = type; + } + }; + function isWord(char) { + return /\w/.test(char); + } + function isInteger(char) { + return /[0-9]/.test(char); + } + var ORDERED_MAPPING_TABLE = [ + // Control sequences + ["{%", TOKEN_TYPES.OpenStatement], + ["%}", TOKEN_TYPES.CloseStatement], + ["{{", TOKEN_TYPES.OpenExpression], + ["}}", TOKEN_TYPES.CloseExpression], + // Single character tokens + ["(", TOKEN_TYPES.OpenParen], + [")", TOKEN_TYPES.CloseParen], + ["{", TOKEN_TYPES.OpenCurlyBracket], + ["}", TOKEN_TYPES.CloseCurlyBracket], + ["[", TOKEN_TYPES.OpenSquareBracket], + ["]", TOKEN_TYPES.CloseSquareBracket], + [",", TOKEN_TYPES.Comma], + [".", TOKEN_TYPES.Dot], + [":", TOKEN_TYPES.Colon], + ["|", TOKEN_TYPES.Pipe], + // Comparison operators + ["<=", TOKEN_TYPES.ComparisonBinaryOperator], + [">=", TOKEN_TYPES.ComparisonBinaryOperator], + ["==", TOKEN_TYPES.ComparisonBinaryOperator], + ["!=", TOKEN_TYPES.ComparisonBinaryOperator], + ["<", TOKEN_TYPES.ComparisonBinaryOperator], + [">", TOKEN_TYPES.ComparisonBinaryOperator], + // Arithmetic operators + ["+", TOKEN_TYPES.AdditiveBinaryOperator], + ["-", TOKEN_TYPES.AdditiveBinaryOperator], + ["*", TOKEN_TYPES.MultiplicativeBinaryOperator], + ["/", TOKEN_TYPES.MultiplicativeBinaryOperator], + ["%", TOKEN_TYPES.MultiplicativeBinaryOperator], + // Assignment operator + ["=", TOKEN_TYPES.Equals] + ]; + var ESCAPE_CHARACTERS = /* @__PURE__ */ new Map([ + ["n", "\n"], + // New line + ["t", " "], + // Horizontal tab + ["r", "\r"], + // Carriage return + ["b", "\b"], + // Backspace + ["f", "\f"], + // Form feed + ["v", "\v"], + // Vertical tab + ["'", "'"], + // Single quote + ['"', '"'], + // Double quote + ["\\", "\\"] + // Backslash + ]); + function preprocess(template, options = {}) { + if (template.endsWith("\n")) { + template = template.slice(0, -1); + } + template = template.replace(/{#.*?#}/gs, "{##}"); + if (options.lstrip_blocks) { + template = template.replace(/^[ \t]*({[#%])/gm, "$1"); + } + if (options.trim_blocks) { + template = template.replace(/([#%]})\n/g, "$1"); + } + return template.replace(/{##}/g, "").replace(/-%}\s*/g, "%}").replace(/\s*{%-/g, "{%").replace(/-}}\s*/g, "}}").replace(/\s*{{-/g, "{{"); + } + function tokenize(source, options = {}) { + var _a, _b, _c; + const tokens = []; + const src = preprocess(source, options); + let cursorPosition = 0; + const consumeWhile = (predicate) => { + let str = ""; + while (predicate(src[cursorPosition])) { + if (src[cursorPosition] === "\\") { + ++cursorPosition; + if (cursorPosition >= src.length) + throw new SyntaxError("Unexpected end of input"); + const escaped = src[cursorPosition++]; + const unescaped = ESCAPE_CHARACTERS.get(escaped); + if (unescaped === void 0) { + throw new SyntaxError(`Unexpected escaped character: ${escaped}`); + } + str += unescaped; + continue; + } + str += src[cursorPosition++]; + if (cursorPosition >= src.length) + throw new SyntaxError("Unexpected end of input"); + } + return str; + }; + main: + while (cursorPosition < src.length) { + const lastTokenType = (_a = tokens.at(-1)) == null ? void 0 : _a.type; + if (lastTokenType === void 0 || lastTokenType === TOKEN_TYPES.CloseStatement || lastTokenType === TOKEN_TYPES.CloseExpression) { + let text = ""; + while (cursorPosition < src.length && // Keep going until we hit the next Jinja statement or expression + !(src[cursorPosition] === "{" && (src[cursorPosition + 1] === "%" || src[cursorPosition + 1] === "{"))) { + text += src[cursorPosition++]; + } + if (text.length > 0) { + tokens.push(new Token(text, TOKEN_TYPES.Text)); + continue; + } + } + consumeWhile((char2) => /\s/.test(char2)); + const char = src[cursorPosition]; + if (char === "-" || char === "+") { + const lastTokenType2 = (_b = tokens.at(-1)) == null ? void 0 : _b.type; + if (lastTokenType2 === TOKEN_TYPES.Text || lastTokenType2 === void 0) { + throw new SyntaxError(`Unexpected character: ${char}`); + } + switch (lastTokenType2) { + case TOKEN_TYPES.Identifier: + case TOKEN_TYPES.NumericLiteral: + case TOKEN_TYPES.BooleanLiteral: + case TOKEN_TYPES.StringLiteral: + case TOKEN_TYPES.CloseParen: + case TOKEN_TYPES.CloseSquareBracket: + break; + default: { + ++cursorPosition; + const num = consumeWhile(isInteger); + tokens.push( + new Token(`${char}${num}`, num.length > 0 ? TOKEN_TYPES.NumericLiteral : TOKEN_TYPES.UnaryOperator) + ); + continue; + } + } + } + for (const [char2, token] of ORDERED_MAPPING_TABLE) { + const slice2 = src.slice(cursorPosition, cursorPosition + char2.length); + if (slice2 === char2) { + tokens.push(new Token(char2, token)); + cursorPosition += char2.length; + continue main; + } + } + if (char === "'" || char === '"') { + ++cursorPosition; + const str = consumeWhile((c) => c !== char); + tokens.push(new Token(str, TOKEN_TYPES.StringLiteral)); + ++cursorPosition; + continue; + } + if (isInteger(char)) { + const num = consumeWhile(isInteger); + tokens.push(new Token(num, TOKEN_TYPES.NumericLiteral)); + continue; + } + if (isWord(char)) { + const word = consumeWhile(isWord); + const type = Object.hasOwn(KEYWORDS, word) ? KEYWORDS[word] : TOKEN_TYPES.Identifier; + if (type === TOKEN_TYPES.In && ((_c = tokens.at(-1)) == null ? void 0 : _c.type) === TOKEN_TYPES.Not) { + tokens.pop(); + tokens.push(new Token("not in", TOKEN_TYPES.NotIn)); + } else { + tokens.push(new Token(word, type)); + } + continue; + } + throw new SyntaxError(`Unexpected character: ${char}`); + } + return tokens; + } + var Statement = class { + constructor() { + __publicField(this, "type", "Statement"); + } + }; + var Program = class extends Statement { + constructor(body) { + super(); + __publicField(this, "type", "Program"); + this.body = body; + } + }; + var If = class extends Statement { + constructor(test, body, alternate) { + super(); + __publicField(this, "type", "If"); + this.test = test; + this.body = body; + this.alternate = alternate; + } + }; + var For = class extends Statement { + constructor(loopvar, iterable, body, defaultBlock) { + super(); + __publicField(this, "type", "For"); + this.loopvar = loopvar; + this.iterable = iterable; + this.body = body; + this.defaultBlock = defaultBlock; + } + }; + var SetStatement = class extends Statement { + constructor(assignee, value) { + super(); + __publicField(this, "type", "Set"); + this.assignee = assignee; + this.value = value; + } + }; + var Macro = class extends Statement { + constructor(name, args, body) { + super(); + __publicField(this, "type", "Macro"); + this.name = name; + this.args = args; + this.body = body; + } + }; + var Expression = class extends Statement { + constructor() { + super(...arguments); + __publicField(this, "type", "Expression"); + } + }; + var MemberExpression = class extends Expression { + constructor(object, property, computed) { + super(); + __publicField(this, "type", "MemberExpression"); + this.object = object; + this.property = property; + this.computed = computed; + } + }; + var CallExpression = class extends Expression { + constructor(callee, args) { + super(); + __publicField(this, "type", "CallExpression"); + this.callee = callee; + this.args = args; + } + }; + var Identifier = class extends Expression { + /** + * @param {string} value The name of the identifier + */ + constructor(value) { + super(); + __publicField(this, "type", "Identifier"); + this.value = value; + } + }; + var Literal = class extends Expression { + constructor(value) { + super(); + __publicField(this, "type", "Literal"); + this.value = value; + } + }; + var NumericLiteral = class extends Literal { + constructor() { + super(...arguments); + __publicField(this, "type", "NumericLiteral"); + } + }; + var StringLiteral = class extends Literal { + constructor() { + super(...arguments); + __publicField(this, "type", "StringLiteral"); + } + }; + var BooleanLiteral = class extends Literal { + constructor() { + super(...arguments); + __publicField(this, "type", "BooleanLiteral"); + } + }; + var ArrayLiteral = class extends Literal { + constructor() { + super(...arguments); + __publicField(this, "type", "ArrayLiteral"); + } + }; + var TupleLiteral = class extends Literal { + constructor() { + super(...arguments); + __publicField(this, "type", "TupleLiteral"); + } + }; + var ObjectLiteral = class extends Literal { + constructor() { + super(...arguments); + __publicField(this, "type", "ObjectLiteral"); + } + }; + var BinaryExpression = class extends Expression { + constructor(operator, left, right) { + super(); + __publicField(this, "type", "BinaryExpression"); + this.operator = operator; + this.left = left; + this.right = right; + } + }; + var FilterExpression = class extends Expression { + constructor(operand, filter) { + super(); + __publicField(this, "type", "FilterExpression"); + this.operand = operand; + this.filter = filter; + } + }; + var SelectExpression = class extends Expression { + constructor(iterable, test) { + super(); + __publicField(this, "type", "SelectExpression"); + this.iterable = iterable; + this.test = test; + } + }; + var TestExpression = class extends Expression { + constructor(operand, negate, test) { + super(); + __publicField(this, "type", "TestExpression"); + this.operand = operand; + this.negate = negate; + this.test = test; + } + }; + var UnaryExpression = class extends Expression { + constructor(operator, argument) { + super(); + __publicField(this, "type", "UnaryExpression"); + this.operator = operator; + this.argument = argument; + } + }; + var SliceExpression = class extends Expression { + constructor(start = void 0, stop = void 0, step = void 0) { + super(); + __publicField(this, "type", "SliceExpression"); + this.start = start; + this.stop = stop; + this.step = step; + } + }; + var KeywordArgumentExpression = class extends Expression { + constructor(key, value) { + super(); + __publicField(this, "type", "KeywordArgumentExpression"); + this.key = key; + this.value = value; + } + }; + function parse(tokens) { + const program = new Program([]); + let current = 0; + function expect(type, error) { + const prev = tokens[current++]; + if (!prev || prev.type !== type) { + throw new Error(`Parser Error: ${error}. ${prev.type} !== ${type}.`); + } + return prev; + } + function parseAny() { + switch (tokens[current].type) { + case TOKEN_TYPES.Text: + return parseText(); + case TOKEN_TYPES.OpenStatement: + return parseJinjaStatement(); + case TOKEN_TYPES.OpenExpression: + return parseJinjaExpression(); + default: + throw new SyntaxError(`Unexpected token type: ${tokens[current].type}`); + } + } + function not(...types) { + return current + types.length <= tokens.length && types.some((type, i) => type !== tokens[current + i].type); + } + function is(...types) { + return current + types.length <= tokens.length && types.every((type, i) => type === tokens[current + i].type); + } + function parseText() { + return new StringLiteral(expect(TOKEN_TYPES.Text, "Expected text token").value); + } + function parseJinjaStatement() { + expect(TOKEN_TYPES.OpenStatement, "Expected opening statement token"); + let result; + switch (tokens[current].type) { + case TOKEN_TYPES.Set: + ++current; + result = parseSetStatement(); + expect(TOKEN_TYPES.CloseStatement, "Expected closing statement token"); + break; + case TOKEN_TYPES.If: + ++current; + result = parseIfStatement(); + expect(TOKEN_TYPES.OpenStatement, "Expected {% token"); + expect(TOKEN_TYPES.EndIf, "Expected endif token"); + expect(TOKEN_TYPES.CloseStatement, "Expected %} token"); + break; + case TOKEN_TYPES.Macro: + ++current; + result = parseMacroStatement(); + expect(TOKEN_TYPES.OpenStatement, "Expected {% token"); + expect(TOKEN_TYPES.EndMacro, "Expected endmacro token"); + expect(TOKEN_TYPES.CloseStatement, "Expected %} token"); + break; + case TOKEN_TYPES.For: + ++current; + result = parseForStatement(); + expect(TOKEN_TYPES.OpenStatement, "Expected {% token"); + expect(TOKEN_TYPES.EndFor, "Expected endfor token"); + expect(TOKEN_TYPES.CloseStatement, "Expected %} token"); + break; + default: + throw new SyntaxError(`Unknown statement type: ${tokens[current].type}`); + } + return result; + } + function parseJinjaExpression() { + expect(TOKEN_TYPES.OpenExpression, "Expected opening expression token"); + const result = parseExpression(); + expect(TOKEN_TYPES.CloseExpression, "Expected closing expression token"); + return result; + } + function parseSetStatement() { + const left = parseExpression(); + if (is(TOKEN_TYPES.Equals)) { + ++current; + const value = parseSetStatement(); + return new SetStatement(left, value); + } + return left; + } + function parseIfStatement() { + var _a, _b, _c, _d, _e, _f, _g, _h; + const test = parseExpression(); + expect(TOKEN_TYPES.CloseStatement, "Expected closing statement token"); + const body = []; + const alternate = []; + while (!(((_a = tokens[current]) == null ? void 0 : _a.type) === TOKEN_TYPES.OpenStatement && (((_b = tokens[current + 1]) == null ? void 0 : _b.type) === TOKEN_TYPES.ElseIf || ((_c = tokens[current + 1]) == null ? void 0 : _c.type) === TOKEN_TYPES.Else || ((_d = tokens[current + 1]) == null ? void 0 : _d.type) === TOKEN_TYPES.EndIf))) { + body.push(parseAny()); + } + if (((_e = tokens[current]) == null ? void 0 : _e.type) === TOKEN_TYPES.OpenStatement && ((_f = tokens[current + 1]) == null ? void 0 : _f.type) !== TOKEN_TYPES.EndIf) { + ++current; + if (is(TOKEN_TYPES.ElseIf)) { + expect(TOKEN_TYPES.ElseIf, "Expected elseif token"); + alternate.push(parseIfStatement()); + } else { + expect(TOKEN_TYPES.Else, "Expected else token"); + expect(TOKEN_TYPES.CloseStatement, "Expected closing statement token"); + while (!(((_g = tokens[current]) == null ? void 0 : _g.type) === TOKEN_TYPES.OpenStatement && ((_h = tokens[current + 1]) == null ? void 0 : _h.type) === TOKEN_TYPES.EndIf)) { + alternate.push(parseAny()); + } + } + } + return new If(test, body, alternate); + } + function parseMacroStatement() { + const name = parsePrimaryExpression(); + if (name.type !== "Identifier") { + throw new SyntaxError(`Expected identifier following macro statement`); + } + const args = parseArgs(); + expect(TOKEN_TYPES.CloseStatement, "Expected closing statement token"); + const body = []; + while (not(TOKEN_TYPES.OpenStatement, TOKEN_TYPES.EndMacro)) { + body.push(parseAny()); + } + return new Macro(name, args, body); + } + function parseExpressionSequence(primary = false) { + const fn = primary ? parsePrimaryExpression : parseExpression; + const expressions = [fn()]; + const isTuple = is(TOKEN_TYPES.Comma); + while (isTuple) { + ++current; + expressions.push(fn()); + if (!is(TOKEN_TYPES.Comma)) { + break; + } + } + return isTuple ? new TupleLiteral(expressions) : expressions[0]; + } + function parseForStatement() { + const loopVariable = parseExpressionSequence(true); + if (!(loopVariable instanceof Identifier || loopVariable instanceof TupleLiteral)) { + throw new SyntaxError(`Expected identifier/tuple for the loop variable, got ${loopVariable.type} instead`); + } + expect(TOKEN_TYPES.In, "Expected `in` keyword following loop variable"); + const iterable = parseExpression(); + expect(TOKEN_TYPES.CloseStatement, "Expected closing statement token"); + const body = []; + while (not(TOKEN_TYPES.OpenStatement, TOKEN_TYPES.EndFor) && not(TOKEN_TYPES.OpenStatement, TOKEN_TYPES.Else)) { + body.push(parseAny()); + } + const alternative = []; + if (is(TOKEN_TYPES.OpenStatement, TOKEN_TYPES.Else)) { + ++current; + ++current; + expect(TOKEN_TYPES.CloseStatement, "Expected closing statement token"); + while (not(TOKEN_TYPES.OpenStatement, TOKEN_TYPES.EndFor)) { + alternative.push(parseAny()); + } + } + return new For(loopVariable, iterable, body, alternative); + } + function parseExpression() { + return parseIfExpression(); + } + function parseIfExpression() { + const a = parseLogicalOrExpression(); + if (is(TOKEN_TYPES.If)) { + ++current; + const predicate = parseLogicalOrExpression(); + if (is(TOKEN_TYPES.Else)) { + ++current; + const b = parseLogicalOrExpression(); + return new If(predicate, [a], [b]); + } else { + return new SelectExpression(a, predicate); + } + } + return a; + } + function parseLogicalOrExpression() { + let left = parseLogicalAndExpression(); + while (is(TOKEN_TYPES.Or)) { + const operator = tokens[current]; + ++current; + const right = parseLogicalAndExpression(); + left = new BinaryExpression(operator, left, right); + } + return left; + } + function parseLogicalAndExpression() { + let left = parseLogicalNegationExpression(); + while (is(TOKEN_TYPES.And)) { + const operator = tokens[current]; + ++current; + const right = parseLogicalNegationExpression(); + left = new BinaryExpression(operator, left, right); + } + return left; + } + function parseLogicalNegationExpression() { + let right; + while (is(TOKEN_TYPES.Not)) { + const operator = tokens[current]; + ++current; + const arg = parseLogicalNegationExpression(); + right = new UnaryExpression(operator, arg); + } + return right ?? parseComparisonExpression(); + } + function parseComparisonExpression() { + let left = parseAdditiveExpression(); + while (is(TOKEN_TYPES.ComparisonBinaryOperator) || is(TOKEN_TYPES.In) || is(TOKEN_TYPES.NotIn)) { + const operator = tokens[current]; + ++current; + const right = parseAdditiveExpression(); + left = new BinaryExpression(operator, left, right); + } + return left; + } + function parseAdditiveExpression() { + let left = parseMultiplicativeExpression(); + while (is(TOKEN_TYPES.AdditiveBinaryOperator)) { + const operator = tokens[current]; + ++current; + const right = parseMultiplicativeExpression(); + left = new BinaryExpression(operator, left, right); + } + return left; + } + function parseCallMemberExpression() { + const member = parseMemberExpression(); + if (is(TOKEN_TYPES.OpenParen)) { + return parseCallExpression(member); + } + return member; + } + function parseCallExpression(callee) { + let callExpression = new CallExpression(callee, parseArgs()); + if (is(TOKEN_TYPES.OpenParen)) { + callExpression = parseCallExpression(callExpression); + } + return callExpression; + } + function parseArgs() { + expect(TOKEN_TYPES.OpenParen, "Expected opening parenthesis for arguments list"); + const args = parseArgumentsList(); + expect(TOKEN_TYPES.CloseParen, "Expected closing parenthesis for arguments list"); + return args; + } + function parseArgumentsList() { + const args = []; + while (!is(TOKEN_TYPES.CloseParen)) { + let argument = parseExpression(); + if (is(TOKEN_TYPES.Equals)) { + ++current; + if (!(argument instanceof Identifier)) { + throw new SyntaxError(`Expected identifier for keyword argument`); + } + const value = parseExpression(); + argument = new KeywordArgumentExpression(argument, value); + } + args.push(argument); + if (is(TOKEN_TYPES.Comma)) { + ++current; + } + } + return args; + } + function parseMemberExpressionArgumentsList() { + const slices = []; + let isSlice = false; + while (!is(TOKEN_TYPES.CloseSquareBracket)) { + if (is(TOKEN_TYPES.Colon)) { + slices.push(void 0); + ++current; + isSlice = true; + } else { + slices.push(parseExpression()); + if (is(TOKEN_TYPES.Colon)) { + ++current; + isSlice = true; + } + } + } + if (slices.length === 0) { + throw new SyntaxError(`Expected at least one argument for member/slice expression`); + } + if (isSlice) { + if (slices.length > 3) { + throw new SyntaxError(`Expected 0-3 arguments for slice expression`); + } + return new SliceExpression(...slices); + } + return slices[0]; + } + function parseMemberExpression() { + let object = parsePrimaryExpression(); + while (is(TOKEN_TYPES.Dot) || is(TOKEN_TYPES.OpenSquareBracket)) { + const operator = tokens[current]; + ++current; + let property; + const computed = operator.type !== TOKEN_TYPES.Dot; + if (computed) { + property = parseMemberExpressionArgumentsList(); + expect(TOKEN_TYPES.CloseSquareBracket, "Expected closing square bracket"); + } else { + property = parsePrimaryExpression(); + if (property.type !== "Identifier") { + throw new SyntaxError(`Expected identifier following dot operator`); + } + } + object = new MemberExpression(object, property, computed); + } + return object; + } + function parseMultiplicativeExpression() { + let left = parseTestExpression(); + while (is(TOKEN_TYPES.MultiplicativeBinaryOperator)) { + const operator = tokens[current]; + ++current; + const right = parseTestExpression(); + left = new BinaryExpression(operator, left, right); + } + return left; + } + function parseTestExpression() { + let operand = parseFilterExpression(); + while (is(TOKEN_TYPES.Is)) { + ++current; + const negate = is(TOKEN_TYPES.Not); + if (negate) { + ++current; + } + let filter = parsePrimaryExpression(); + if (filter instanceof BooleanLiteral) { + filter = new Identifier(filter.value.toString()); + } + if (!(filter instanceof Identifier)) { + throw new SyntaxError(`Expected identifier for the test`); + } + operand = new TestExpression(operand, negate, filter); + } + return operand; + } + function parseFilterExpression() { + let operand = parseCallMemberExpression(); + while (is(TOKEN_TYPES.Pipe)) { + ++current; + let filter = parsePrimaryExpression(); + if (!(filter instanceof Identifier)) { + throw new SyntaxError(`Expected identifier for the filter`); + } + if (is(TOKEN_TYPES.OpenParen)) { + filter = parseCallExpression(filter); + } + operand = new FilterExpression(operand, filter); + } + return operand; + } + function parsePrimaryExpression() { + const token = tokens[current]; + switch (token.type) { + case TOKEN_TYPES.NumericLiteral: + ++current; + return new NumericLiteral(Number(token.value)); + case TOKEN_TYPES.StringLiteral: + ++current; + return new StringLiteral(token.value); + case TOKEN_TYPES.BooleanLiteral: + ++current; + return new BooleanLiteral(token.value.toLowerCase() === "true"); + case TOKEN_TYPES.Identifier: + ++current; + return new Identifier(token.value); + case TOKEN_TYPES.OpenParen: { + ++current; + const expression = parseExpressionSequence(); + if (tokens[current].type !== TOKEN_TYPES.CloseParen) { + throw new SyntaxError(`Expected closing parenthesis, got ${tokens[current].type} instead`); + } + ++current; + return expression; + } + case TOKEN_TYPES.OpenSquareBracket: { + ++current; + const values = []; + while (!is(TOKEN_TYPES.CloseSquareBracket)) { + values.push(parseExpression()); + if (is(TOKEN_TYPES.Comma)) { + ++current; + } + } + ++current; + return new ArrayLiteral(values); + } + case TOKEN_TYPES.OpenCurlyBracket: { + ++current; + const values = /* @__PURE__ */ new Map(); + while (!is(TOKEN_TYPES.CloseCurlyBracket)) { + const key = parseExpression(); + expect(TOKEN_TYPES.Colon, "Expected colon between key and value in object literal"); + const value = parseExpression(); + values.set(key, value); + if (is(TOKEN_TYPES.Comma)) { + ++current; + } + } + ++current; + return new ObjectLiteral(values); + } + default: + throw new SyntaxError(`Unexpected token: ${token.type}`); + } + } + while (current < tokens.length) { + program.body.push(parseAny()); + } + return program; + } + function range(start, stop, step = 1) { + if (stop === void 0) { + stop = start; + start = 0; + } + const result = []; + for (let i = start; i < stop; i += step) { + result.push(i); + } + return result; + } + function slice(array, start, stop, step = 1) { + const direction = Math.sign(step); + if (direction >= 0) { + start = (start ?? (start = 0)) < 0 ? Math.max(array.length + start, 0) : Math.min(start, array.length); + stop = (stop ?? (stop = array.length)) < 0 ? Math.max(array.length + stop, 0) : Math.min(stop, array.length); + } else { + start = (start ?? (start = array.length - 1)) < 0 ? Math.max(array.length + start, -1) : Math.min(start, array.length - 1); + stop = (stop ?? (stop = -1)) < -1 ? Math.max(array.length + stop, -1) : Math.min(stop, array.length - 1); + } + const result = []; + for (let i = start; direction * i < direction * stop; i += step) { + result.push(array[i]); + } + return result; + } + function titleCase(value) { + return value.replace(/\b\w/g, (c) => c.toUpperCase()); + } + var RuntimeValue = class { + /** + * Creates a new RuntimeValue. + */ + constructor(value = void 0) { + __publicField(this, "type", "RuntimeValue"); + __publicField(this, "value"); + /** + * A collection of built-in functions for this type. + */ + __publicField(this, "builtins", /* @__PURE__ */ new Map()); + this.value = value; + } + /** + * Determines truthiness or falsiness of the runtime value. + * This function should be overridden by subclasses if it has custom truthiness criteria. + * @returns {BooleanValue} BooleanValue(true) if the value is truthy, BooleanValue(false) otherwise. + */ + __bool__() { + return new BooleanValue(!!this.value); + } + }; + var NumericValue = class extends RuntimeValue { + constructor() { + super(...arguments); + __publicField(this, "type", "NumericValue"); + } + }; + var StringValue = class extends RuntimeValue { + constructor() { + super(...arguments); + __publicField(this, "type", "StringValue"); + __publicField(this, "builtins", /* @__PURE__ */ new Map([ + [ + "upper", + new FunctionValue(() => { + return new StringValue(this.value.toUpperCase()); + }) + ], + [ + "lower", + new FunctionValue(() => { + return new StringValue(this.value.toLowerCase()); + }) + ], + [ + "strip", + new FunctionValue(() => { + return new StringValue(this.value.trim()); + }) + ], + [ + "title", + new FunctionValue(() => { + return new StringValue(titleCase(this.value)); + }) + ], + ["length", new NumericValue(this.value.length)] + ])); + } + }; + var BooleanValue = class extends RuntimeValue { + constructor() { + super(...arguments); + __publicField(this, "type", "BooleanValue"); + } + }; + var ObjectValue = class extends RuntimeValue { + constructor() { + super(...arguments); + __publicField(this, "type", "ObjectValue"); + __publicField(this, "builtins", /* @__PURE__ */ new Map([ + [ + "get", + new FunctionValue(([key, defaultValue]) => { + if (!(key instanceof StringValue)) { + throw new Error(`Object key must be a string: got ${key.type}`); + } + return this.value.get(key.value) ?? defaultValue ?? new NullValue(); + }) + ], + [ + "items", + new FunctionValue(() => { + return new ArrayValue( + Array.from(this.value.entries()).map(([key, value]) => new ArrayValue([new StringValue(key), value])) + ); + }) + ] + ])); + } + /** + * NOTE: necessary to override since all JavaScript arrays are considered truthy, + * while only non-empty Python arrays are consider truthy. + * + * e.g., + * - JavaScript: {} && 5 -> 5 + * - Python: {} and 5 -> {} + */ + __bool__() { + return new BooleanValue(this.value.size > 0); + } + }; + var KeywordArgumentsValue = class extends ObjectValue { + constructor() { + super(...arguments); + __publicField(this, "type", "KeywordArgumentsValue"); + } + }; + var ArrayValue = class extends RuntimeValue { + constructor() { + super(...arguments); + __publicField(this, "type", "ArrayValue"); + __publicField(this, "builtins", /* @__PURE__ */ new Map([["length", new NumericValue(this.value.length)]])); + } + /** + * NOTE: necessary to override since all JavaScript arrays are considered truthy, + * while only non-empty Python arrays are consider truthy. + * + * e.g., + * - JavaScript: [] && 5 -> 5 + * - Python: [] and 5 -> [] + */ + __bool__() { + return new BooleanValue(this.value.length > 0); + } + }; + var TupleValue = class extends ArrayValue { + constructor() { + super(...arguments); + __publicField(this, "type", "TupleValue"); + } + }; + var FunctionValue = class extends RuntimeValue { + constructor() { + super(...arguments); + __publicField(this, "type", "FunctionValue"); + } + }; + var NullValue = class extends RuntimeValue { + constructor() { + super(...arguments); + __publicField(this, "type", "NullValue"); + } + }; + var UndefinedValue = class extends RuntimeValue { + constructor() { + super(...arguments); + __publicField(this, "type", "UndefinedValue"); + } + }; + var Environment = class { + constructor(parent) { + /** + * The variables declared in this environment. + */ + __publicField(this, "variables", /* @__PURE__ */ new Map([ + [ + "namespace", + new FunctionValue((args) => { + if (args.length === 0) { + return new ObjectValue(/* @__PURE__ */ new Map()); + } + if (args.length !== 1 || !(args[0] instanceof ObjectValue)) { + throw new Error("`namespace` expects either zero arguments or a single object argument"); + } + return args[0]; + }) + ] + ])); + /** + * The tests available in this environment. + */ + __publicField(this, "tests", /* @__PURE__ */ new Map([ + ["boolean", (operand) => operand.type === "BooleanValue"], + ["callable", (operand) => operand instanceof FunctionValue], + [ + "odd", + (operand) => { + if (operand.type !== "NumericValue") { + throw new Error(`Cannot apply test "odd" to type: ${operand.type}`); + } + return operand.value % 2 !== 0; + } + ], + [ + "even", + (operand) => { + if (operand.type !== "NumericValue") { + throw new Error(`Cannot apply test "even" to type: ${operand.type}`); + } + return operand.value % 2 === 0; + } + ], + ["false", (operand) => operand.type === "BooleanValue" && !operand.value], + ["true", (operand) => operand.type === "BooleanValue" && operand.value], + ["string", (operand) => operand.type === "StringValue"], + ["number", (operand) => operand.type === "NumericValue"], + ["integer", (operand) => operand.type === "NumericValue" && Number.isInteger(operand.value)], + ["iterable", (operand) => operand instanceof ArrayValue || operand instanceof StringValue], + [ + "lower", + (operand) => { + const str = operand.value; + return operand.type === "StringValue" && str === str.toLowerCase(); + } + ], + [ + "upper", + (operand) => { + const str = operand.value; + return operand.type === "StringValue" && str === str.toUpperCase(); + } + ], + ["none", (operand) => operand.type === "NullValue"], + ["defined", (operand) => operand.type !== "UndefinedValue"], + ["undefined", (operand) => operand.type === "UndefinedValue"], + ["equalto", (a, b) => a.value === b.value], + ["eq", (a, b) => a.value === b.value] + ])); + this.parent = parent; + } + /** + * Set the value of a variable in the current environment. + */ + set(name, value) { + return this.declareVariable(name, convertToRuntimeValues(value)); + } + declareVariable(name, value) { + if (this.variables.has(name)) { + throw new SyntaxError(`Variable already declared: ${name}`); + } + this.variables.set(name, value); + return value; + } + // private assignVariable(name: string, value: AnyRuntimeValue): AnyRuntimeValue { + // const env = this.resolve(name); + // env.variables.set(name, value); + // return value; + // } + /** + * Set variable in the current scope. + * See https://jinja.palletsprojects.com/en/3.0.x/templates/#assignments for more information. + */ + setVariable(name, value) { + this.variables.set(name, value); + return value; + } + /** + * Resolve the environment in which the variable is declared. + * @param {string} name The name of the variable. + * @returns {Environment} The environment in which the variable is declared. + */ + resolve(name) { + if (this.variables.has(name)) { + return this; + } + if (this.parent) { + return this.parent.resolve(name); + } + throw new Error(`Unknown variable: ${name}`); + } + lookupVariable(name) { + try { + return this.resolve(name).variables.get(name) ?? new UndefinedValue(); + } catch { + return new UndefinedValue(); + } + } + }; + var Interpreter = class { + constructor(env) { + __publicField(this, "global"); + this.global = env ?? new Environment(); + } + /** + * Run the program. + */ + run(program) { + return this.evaluate(program, this.global); + } + /** + * Evaluates expressions following the binary operation type. + */ + evaluateBinaryExpression(node, environment) { + const left = this.evaluate(node.left, environment); + switch (node.operator.value) { + case "and": + return left.__bool__().value ? this.evaluate(node.right, environment) : left; + case "or": + return left.__bool__().value ? left : this.evaluate(node.right, environment); + } + const right = this.evaluate(node.right, environment); + switch (node.operator.value) { + case "==": + return new BooleanValue(left.value == right.value); + case "!=": + return new BooleanValue(left.value != right.value); + } + if (left instanceof UndefinedValue || right instanceof UndefinedValue) { + throw new Error("Cannot perform operation on undefined values"); + } else if (left instanceof NullValue || right instanceof NullValue) { + throw new Error("Cannot perform operation on null values"); + } else if (left instanceof NumericValue && right instanceof NumericValue) { + switch (node.operator.value) { + case "+": + return new NumericValue(left.value + right.value); + case "-": + return new NumericValue(left.value - right.value); + case "*": + return new NumericValue(left.value * right.value); + case "/": + return new NumericValue(left.value / right.value); + case "%": + return new NumericValue(left.value % right.value); + case "<": + return new BooleanValue(left.value < right.value); + case ">": + return new BooleanValue(left.value > right.value); + case ">=": + return new BooleanValue(left.value >= right.value); + case "<=": + return new BooleanValue(left.value <= right.value); + } + } else if (left instanceof ArrayValue && right instanceof ArrayValue) { + switch (node.operator.value) { + case "+": + return new ArrayValue(left.value.concat(right.value)); + } + } else if (right instanceof ArrayValue) { + const member = right.value.find((x) => x.value === left.value) !== void 0; + switch (node.operator.value) { + case "in": + return new BooleanValue(member); + case "not in": + return new BooleanValue(!member); + } + } + if (left instanceof StringValue || right instanceof StringValue) { + switch (node.operator.value) { + case "+": + return new StringValue(left.value.toString() + right.value.toString()); + } + } + if (left instanceof StringValue && right instanceof StringValue) { + switch (node.operator.value) { + case "in": + return new BooleanValue(right.value.includes(left.value)); + case "not in": + return new BooleanValue(!right.value.includes(left.value)); + } + } + if (left instanceof StringValue && right instanceof ObjectValue) { + switch (node.operator.value) { + case "in": + return new BooleanValue(right.value.has(left.value)); + case "not in": + return new BooleanValue(!right.value.has(left.value)); + } + } + throw new SyntaxError(`Unknown operator "${node.operator.value}" between ${left.type} and ${right.type}`); + } + evaluateArguments(args, environment) { + const positionalArguments = []; + const keywordArguments = /* @__PURE__ */ new Map(); + for (const argument of args) { + if (argument.type === "KeywordArgumentExpression") { + const kwarg = argument; + keywordArguments.set(kwarg.key.value, this.evaluate(kwarg.value, environment)); + } else { + if (keywordArguments.size > 0) { + throw new Error("Positional arguments must come before keyword arguments"); + } + positionalArguments.push(this.evaluate(argument, environment)); + } + } + return [positionalArguments, keywordArguments]; + } + /** + * Evaluates expressions following the filter operation type. + */ + evaluateFilterExpression(node, environment) { + const operand = this.evaluate(node.operand, environment); + if (node.filter.type === "Identifier") { + const filter = node.filter; + if (filter.value === "tojson") { + return new StringValue(toJSON(operand)); + } + if (operand instanceof ArrayValue) { + switch (filter.value) { + case "list": + return operand; + case "first": + return operand.value[0]; + case "last": + return operand.value[operand.value.length - 1]; + case "length": + return new NumericValue(operand.value.length); + case "reverse": + return new ArrayValue(operand.value.reverse()); + case "sort": + return new ArrayValue( + operand.value.sort((a, b) => { + if (a.type !== b.type) { + throw new Error(`Cannot compare different types: ${a.type} and ${b.type}`); + } + switch (a.type) { + case "NumericValue": + return a.value - b.value; + case "StringValue": + return a.value.localeCompare(b.value); + default: + throw new Error(`Cannot compare type: ${a.type}`); + } + }) + ); + default: + throw new Error(`Unknown ArrayValue filter: ${filter.value}`); + } + } else if (operand instanceof StringValue) { + switch (filter.value) { + case "length": + return new NumericValue(operand.value.length); + case "upper": + return new StringValue(operand.value.toUpperCase()); + case "lower": + return new StringValue(operand.value.toLowerCase()); + case "title": + return new StringValue(titleCase(operand.value)); + case "capitalize": + return new StringValue(operand.value.charAt(0).toUpperCase() + operand.value.slice(1)); + case "trim": + return new StringValue(operand.value.trim()); + case "indent": + return new StringValue( + operand.value.split("\n").map( + (x, i) => ( + // By default, don't indent the first line or empty lines + i === 0 || x.length === 0 ? x : " " + x + ) + ).join("\n") + ); + case "string": + return operand; + default: + throw new Error(`Unknown StringValue filter: ${filter.value}`); + } + } else if (operand instanceof NumericValue) { + switch (filter.value) { + case "abs": + return new NumericValue(Math.abs(operand.value)); + default: + throw new Error(`Unknown NumericValue filter: ${filter.value}`); + } + } else if (operand instanceof ObjectValue) { + switch (filter.value) { + case "items": + return new ArrayValue( + Array.from(operand.value.entries()).map(([key, value]) => new ArrayValue([new StringValue(key), value])) + ); + case "length": + return new NumericValue(operand.value.size); + default: + throw new Error(`Unknown ObjectValue filter: ${filter.value}`); + } + } + throw new Error(`Cannot apply filter "${filter.value}" to type: ${operand.type}`); + } else if (node.filter.type === "CallExpression") { + const filter = node.filter; + if (filter.callee.type !== "Identifier") { + throw new Error(`Unknown filter: ${filter.callee.type}`); + } + const filterName = filter.callee.value; + if (filterName === "tojson") { + const [, kwargs] = this.evaluateArguments(filter.args, environment); + const indent = kwargs.get("indent") ?? new NullValue(); + if (!(indent instanceof NumericValue || indent instanceof NullValue)) { + throw new Error("If set, indent must be a number"); + } + return new StringValue(toJSON(operand, indent.value)); + } + if (operand instanceof ArrayValue) { + switch (filterName) { + case "selectattr": { + if (operand.value.some((x) => !(x instanceof ObjectValue))) { + throw new Error("`selectattr` can only be applied to array of objects"); + } + if (filter.args.some((x) => x.type !== "StringLiteral")) { + throw new Error("arguments of `selectattr` must be strings"); + } + const [attr, testName, value] = filter.args.map((x) => this.evaluate(x, environment)); + let testFunction; + if (testName) { + const test = environment.tests.get(testName.value); + if (!test) { + throw new Error(`Unknown test: ${testName.value}`); + } + testFunction = test; + } else { + testFunction = (...x) => x[0].__bool__().value; + } + const filtered = operand.value.filter((item) => { + const a = item.value.get(attr.value); + if (a) { + return testFunction(a, value); + } + return false; + }); + return new ArrayValue(filtered); + } + case "map": { + const [, kwargs] = this.evaluateArguments(filter.args, environment); + if (kwargs.has("attribute")) { + const attr = kwargs.get("attribute"); + if (!(attr instanceof StringValue)) { + throw new Error("attribute must be a string"); + } + const defaultValue = kwargs.get("default"); + const mapped = operand.value.map((item) => { + if (!(item instanceof ObjectValue)) { + throw new Error("items in map must be an object"); + } + return item.value.get(attr.value) ?? defaultValue ?? new UndefinedValue(); + }); + return new ArrayValue(mapped); + } else { + throw new Error("`map` expressions without `attribute` set are not currently supported."); + } + } + } + throw new Error(`Unknown ArrayValue filter: ${filterName}`); + } else if (operand instanceof StringValue) { + switch (filterName) { + case "indent": { + const [args, kwargs] = this.evaluateArguments(filter.args, environment); + const width = args.at(0) ?? kwargs.get("width") ?? new NumericValue(4); + if (!(width instanceof NumericValue)) { + throw new Error("width must be a number"); + } + const first = args.at(1) ?? kwargs.get("first") ?? new BooleanValue(false); + const blank = args.at(2) ?? kwargs.get("blank") ?? new BooleanValue(false); + const lines = operand.value.split("\n"); + const indent = " ".repeat(width.value); + const indented = lines.map( + (x, i) => !first.value && i === 0 || !blank.value && x.length === 0 ? x : indent + x + ); + return new StringValue(indented.join("\n")); + } + } + throw new Error(`Unknown StringValue filter: ${filterName}`); + } else { + throw new Error(`Cannot apply filter "${filterName}" to type: ${operand.type}`); + } + } + throw new Error(`Unknown filter: ${node.filter.type}`); + } + /** + * Evaluates expressions following the test operation type. + */ + evaluateTestExpression(node, environment) { + const operand = this.evaluate(node.operand, environment); + const test = environment.tests.get(node.test.value); + if (!test) { + throw new Error(`Unknown test: ${node.test.value}`); + } + const result = test(operand); + return new BooleanValue(node.negate ? !result : result); + } + /** + * Evaluates expressions following the unary operation type. + */ + evaluateUnaryExpression(node, environment) { + const argument = this.evaluate(node.argument, environment); + switch (node.operator.value) { + case "not": + return new BooleanValue(!argument.value); + default: + throw new SyntaxError(`Unknown operator: ${node.operator.value}`); + } + } + evalProgram(program, environment) { + return this.evaluateBlock(program.body, environment); + } + evaluateBlock(statements, environment) { + let result = ""; + for (const statement of statements) { + const lastEvaluated = this.evaluate(statement, environment); + if (lastEvaluated.type !== "NullValue" && lastEvaluated.type !== "UndefinedValue") { + result += lastEvaluated.value; + } + } + return new StringValue(result); + } + evaluateIdentifier(node, environment) { + return environment.lookupVariable(node.value); + } + evaluateCallExpression(expr, environment) { + const [args, kwargs] = this.evaluateArguments(expr.args, environment); + if (kwargs.size > 0) { + args.push(new KeywordArgumentsValue(kwargs)); + } + const fn = this.evaluate(expr.callee, environment); + if (fn.type !== "FunctionValue") { + throw new Error(`Cannot call something that is not a function: got ${fn.type}`); + } + return fn.value(args, environment); + } + evaluateSliceExpression(object, expr, environment) { + if (!(object instanceof ArrayValue || object instanceof StringValue)) { + throw new Error("Slice object must be an array or string"); + } + const start = this.evaluate(expr.start, environment); + const stop = this.evaluate(expr.stop, environment); + const step = this.evaluate(expr.step, environment); + if (!(start instanceof NumericValue || start instanceof UndefinedValue)) { + throw new Error("Slice start must be numeric or undefined"); + } + if (!(stop instanceof NumericValue || stop instanceof UndefinedValue)) { + throw new Error("Slice stop must be numeric or undefined"); + } + if (!(step instanceof NumericValue || step instanceof UndefinedValue)) { + throw new Error("Slice step must be numeric or undefined"); + } + if (object instanceof ArrayValue) { + return new ArrayValue(slice(object.value, start.value, stop.value, step.value)); + } else { + return new StringValue(slice(Array.from(object.value), start.value, stop.value, step.value).join("")); + } + } + evaluateMemberExpression(expr, environment) { + const object = this.evaluate(expr.object, environment); + let property; + if (expr.computed) { + if (expr.property.type === "SliceExpression") { + return this.evaluateSliceExpression(object, expr.property, environment); + } else { + property = this.evaluate(expr.property, environment); + } + } else { + property = new StringValue(expr.property.value); + } + let value; + if (object instanceof ObjectValue) { + if (!(property instanceof StringValue)) { + throw new Error(`Cannot access property with non-string: got ${property.type}`); + } + value = object.value.get(property.value) ?? object.builtins.get(property.value); + } else if (object instanceof ArrayValue || object instanceof StringValue) { + if (property instanceof NumericValue) { + value = object.value.at(property.value); + if (object instanceof StringValue) { + value = new StringValue(object.value.at(property.value)); + } + } else if (property instanceof StringValue) { + value = object.builtins.get(property.value); + } else { + throw new Error(`Cannot access property with non-string/non-number: got ${property.type}`); + } + } else { + if (!(property instanceof StringValue)) { + throw new Error(`Cannot access property with non-string: got ${property.type}`); + } + value = object.builtins.get(property.value); + } + return value instanceof RuntimeValue ? value : new UndefinedValue(); + } + evaluateSet(node, environment) { + const rhs = this.evaluate(node.value, environment); + if (node.assignee.type === "Identifier") { + const variableName = node.assignee.value; + environment.setVariable(variableName, rhs); + } else if (node.assignee.type === "MemberExpression") { + const member = node.assignee; + const object = this.evaluate(member.object, environment); + if (!(object instanceof ObjectValue)) { + throw new Error("Cannot assign to member of non-object"); + } + if (member.property.type !== "Identifier") { + throw new Error("Cannot assign to member with non-identifier property"); + } + object.value.set(member.property.value, rhs); + } else { + throw new Error(`Invalid LHS inside assignment expression: ${JSON.stringify(node.assignee)}`); + } + return new NullValue(); + } + evaluateIf(node, environment) { + const test = this.evaluate(node.test, environment); + return this.evaluateBlock(test.__bool__().value ? node.body : node.alternate, environment); + } + evaluateFor(node, environment) { + const scope = new Environment(environment); + let test, iterable; + if (node.iterable.type === "SelectExpression") { + const select = node.iterable; + iterable = this.evaluate(select.iterable, scope); + test = select.test; + } else { + iterable = this.evaluate(node.iterable, scope); + } + if (!(iterable instanceof ArrayValue)) { + throw new Error(`Expected iterable type in for loop: got ${iterable.type}`); + } + const items = []; + const scopeUpdateFunctions = []; + for (let i = 0; i < iterable.value.length; ++i) { + const loopScope = new Environment(scope); + const current = iterable.value[i]; + let scopeUpdateFunction; + if (node.loopvar.type === "Identifier") { + scopeUpdateFunction = (scope2) => scope2.setVariable(node.loopvar.value, current); + } else if (node.loopvar.type === "TupleLiteral") { + const loopvar = node.loopvar; + if (current.type !== "ArrayValue") { + throw new Error(`Cannot unpack non-iterable type: ${current.type}`); + } + const c = current; + if (loopvar.value.length !== c.value.length) { + throw new Error(`Too ${loopvar.value.length > c.value.length ? "few" : "many"} items to unpack`); + } + scopeUpdateFunction = (scope2) => { + for (let j = 0; j < loopvar.value.length; ++j) { + if (loopvar.value[j].type !== "Identifier") { + throw new Error(`Cannot unpack non-identifier type: ${loopvar.value[j].type}`); + } + scope2.setVariable(loopvar.value[j].value, c.value[j]); + } + }; + } else { + throw new Error(`Invalid loop variable(s): ${node.loopvar.type}`); + } + if (test) { + scopeUpdateFunction(loopScope); + const testValue = this.evaluate(test, loopScope); + if (!testValue.__bool__().value) { + continue; + } + } + items.push(current); + scopeUpdateFunctions.push(scopeUpdateFunction); + } + let result = ""; + let noIteration = true; + for (let i = 0; i < items.length; ++i) { + const loop = /* @__PURE__ */ new Map([ + ["index", new NumericValue(i + 1)], + ["index0", new NumericValue(i)], + ["revindex", new NumericValue(items.length - i)], + ["revindex0", new NumericValue(items.length - i - 1)], + ["first", new BooleanValue(i === 0)], + ["last", new BooleanValue(i === items.length - 1)], + ["length", new NumericValue(items.length)], + ["previtem", i > 0 ? items[i - 1] : new UndefinedValue()], + ["nextitem", i < items.length - 1 ? items[i + 1] : new UndefinedValue()] + ]); + scope.setVariable("loop", new ObjectValue(loop)); + scopeUpdateFunctions[i](scope); + const evaluated = this.evaluateBlock(node.body, scope); + result += evaluated.value; + noIteration = false; + } + if (noIteration) { + const defaultEvaluated = this.evaluateBlock(node.defaultBlock, scope); + result += defaultEvaluated.value; + } + return new StringValue(result); + } + /** + * See https://jinja.palletsprojects.com/en/3.1.x/templates/#macros for more information. + */ + evaluateMacro(node, environment) { + environment.setVariable( + node.name.value, + new FunctionValue((args, scope) => { + var _a; + const macroScope = new Environment(scope); + args = args.slice(); + let kwargs; + if (((_a = args.at(-1)) == null ? void 0 : _a.type) === "KeywordArgumentsValue") { + kwargs = args.pop(); + } + for (let i = 0; i < node.args.length; ++i) { + const nodeArg = node.args[i]; + const passedArg = args[i]; + if (nodeArg.type === "Identifier") { + const identifier = nodeArg; + if (!passedArg) { + throw new Error(`Missing positional argument: ${identifier.value}`); + } + macroScope.setVariable(identifier.value, passedArg); + } else if (nodeArg.type === "KeywordArgumentExpression") { + const kwarg = nodeArg; + const value = passedArg ?? // Try positional arguments first + (kwargs == null ? void 0 : kwargs.value.get(kwarg.key.value)) ?? // Look in user-passed kwargs + this.evaluate(kwarg.value, macroScope); + macroScope.setVariable(kwarg.key.value, value); + } else { + throw new Error(`Unknown argument type: ${nodeArg.type}`); + } + } + return this.evaluateBlock(node.body, macroScope); + }) + ); + return new NullValue(); + } + evaluate(statement, environment) { + if (statement === void 0) + return new UndefinedValue(); + switch (statement.type) { + case "Program": + return this.evalProgram(statement, environment); + case "Set": + return this.evaluateSet(statement, environment); + case "If": + return this.evaluateIf(statement, environment); + case "For": + return this.evaluateFor(statement, environment); + case "Macro": + return this.evaluateMacro(statement, environment); + case "NumericLiteral": + return new NumericValue(Number(statement.value)); + case "StringLiteral": + return new StringValue(statement.value); + case "BooleanLiteral": + return new BooleanValue(statement.value); + case "ArrayLiteral": + return new ArrayValue(statement.value.map((x) => this.evaluate(x, environment))); + case "TupleLiteral": + return new TupleValue(statement.value.map((x) => this.evaluate(x, environment))); + case "ObjectLiteral": { + const mapping = /* @__PURE__ */ new Map(); + for (const [key, value] of statement.value) { + const evaluatedKey = this.evaluate(key, environment); + if (!(evaluatedKey instanceof StringValue)) { + throw new Error(`Object keys must be strings: got ${evaluatedKey.type}`); + } + mapping.set(evaluatedKey.value, this.evaluate(value, environment)); + } + return new ObjectValue(mapping); + } + case "Identifier": + return this.evaluateIdentifier(statement, environment); + case "CallExpression": + return this.evaluateCallExpression(statement, environment); + case "MemberExpression": + return this.evaluateMemberExpression(statement, environment); + case "UnaryExpression": + return this.evaluateUnaryExpression(statement, environment); + case "BinaryExpression": + return this.evaluateBinaryExpression(statement, environment); + case "FilterExpression": + return this.evaluateFilterExpression(statement, environment); + case "TestExpression": + return this.evaluateTestExpression(statement, environment); + default: + throw new SyntaxError(`Unknown node type: ${statement.type}`); + } + } + }; + function convertToRuntimeValues(input) { + switch (typeof input) { + case "number": + return new NumericValue(input); + case "string": + return new StringValue(input); + case "boolean": + return new BooleanValue(input); + case "undefined": + return new UndefinedValue(); + case "object": + if (input === null) { + return new NullValue(); + } else if (Array.isArray(input)) { + return new ArrayValue(input.map(convertToRuntimeValues)); + } else { + return new ObjectValue( + new Map(Object.entries(input).map(([key, value]) => [key, convertToRuntimeValues(value)])) + ); + } + case "function": + return new FunctionValue((args, _scope) => { + const result = input(...args.map((x) => x.value)) ?? null; + return convertToRuntimeValues(result); + }); + default: + throw new Error(`Cannot convert to runtime value: ${input}`); + } + } + function toJSON(input, indent, depth) { + const currentDepth = depth ?? 0; + switch (input.type) { + case "NullValue": + case "UndefinedValue": + return "null"; + case "NumericValue": + case "StringValue": + case "BooleanValue": + return JSON.stringify(input.value); + case "ArrayValue": + case "ObjectValue": { + const indentValue = indent ? " ".repeat(indent) : ""; + const basePadding = "\n" + indentValue.repeat(currentDepth); + const childrenPadding = basePadding + indentValue; + if (input.type === "ArrayValue") { + const core = input.value.map((x) => toJSON(x, indent, currentDepth + 1)); + return indent ? `[${childrenPadding}${core.join(`,${childrenPadding}`)}${basePadding}]` : `[${core.join(", ")}]`; + } else { + const core = Array.from(input.value.entries()).map(([key, value]) => { + const v = `"${key}": ${toJSON(value, indent, currentDepth + 1)}`; + return indent ? `${childrenPadding}${v}` : v; + }); + return indent ? `{${core.join(",")}${basePadding}}` : `{${core.join(", ")}}`; + } + } + default: + throw new Error(`Cannot convert to JSON: ${input.type}`); + } + } + var Template = class { + /** + * @param {string} template The template string + */ + constructor(template) { + __publicField(this, "parsed"); + const tokens = tokenize(template, { + lstrip_blocks: true, + trim_blocks: true + }); + this.parsed = parse(tokens); + } + render(items) { + const env = new Environment(); + env.set("false", false); + env.set("true", true); + env.set("raise_exception", (args) => { + throw new Error(args); + }); + env.set("range", range); + for (const [key, value] of Object.entries(items)) { + env.set(key, value); + } + const interpreter = new Interpreter(env); + const result = interpreter.run(this.parsed); + return result.value; + } + }; + } + ), + /***/ + "./node_modules/onnxruntime-common/dist/esm/backend-impl.js": ( + /*!******************************************************************!*\ + !*** ./node_modules/onnxruntime-common/dist/esm/backend-impl.js ***! + \******************************************************************/ + /***/ + (__unused_webpack___webpack_module__, __webpack_exports__2, __webpack_require__2) => { + __webpack_require__2.r(__webpack_exports__2); + __webpack_require__2.d(__webpack_exports__2, { + /* harmony export */ + registerBackend: () => ( + /* binding */ + registerBackend + ), + /* harmony export */ + resolveBackendAndExecutionProviders: () => ( + /* binding */ + resolveBackendAndExecutionProviders + ) + /* harmony export */ + }); + const backends = /* @__PURE__ */ new Map(); + const backendsSortedByPriority = []; + const registerBackend = (name, backend, priority) => { + if (backend && typeof backend.init === "function" && typeof backend.createInferenceSessionHandler === "function") { + const currentBackend = backends.get(name); + if (currentBackend === void 0) { + backends.set(name, { backend, priority }); + } else if (currentBackend.priority > priority) { + return; + } else if (currentBackend.priority === priority) { + if (currentBackend.backend !== backend) { + throw new Error(`cannot register backend "${name}" using priority ${priority}`); + } + } + if (priority >= 0) { + const i = backendsSortedByPriority.indexOf(name); + if (i !== -1) { + backendsSortedByPriority.splice(i, 1); + } + for (let i2 = 0; i2 < backendsSortedByPriority.length; i2++) { + if (backends.get(backendsSortedByPriority[i2]).priority <= priority) { + backendsSortedByPriority.splice(i2, 0, name); + return; + } + } + backendsSortedByPriority.push(name); + } + return; + } + throw new TypeError("not a valid backend"); + }; + const tryResolveAndInitializeBackend = async (backendName) => { + const backendInfo = backends.get(backendName); + if (!backendInfo) { + return "backend not found."; + } + if (backendInfo.initialized) { + return backendInfo.backend; + } else if (backendInfo.aborted) { + return backendInfo.error; + } else { + const isInitializing = !!backendInfo.initPromise; + try { + if (!isInitializing) { + backendInfo.initPromise = backendInfo.backend.init(backendName); + } + await backendInfo.initPromise; + backendInfo.initialized = true; + return backendInfo.backend; + } catch (e) { + if (!isInitializing) { + backendInfo.error = `${e}`; + backendInfo.aborted = true; + } + return backendInfo.error; + } finally { + delete backendInfo.initPromise; + } + } + }; + const resolveBackendAndExecutionProviders = async (options) => { + const eps = options.executionProviders || []; + const backendHints = eps.map((i) => typeof i === "string" ? i : i.name); + const backendNames = backendHints.length === 0 ? backendsSortedByPriority : backendHints; + let backend; + const errors = []; + const availableBackendNames = /* @__PURE__ */ new Set(); + for (const backendName of backendNames) { + const resolveResult = await tryResolveAndInitializeBackend(backendName); + if (typeof resolveResult === "string") { + errors.push({ name: backendName, err: resolveResult }); + } else { + if (!backend) { + backend = resolveResult; + } + if (backend === resolveResult) { + availableBackendNames.add(backendName); + } + } + } + if (!backend) { + throw new Error(`no available backend found. ERR: ${errors.map((e) => `[${e.name}] ${e.err}`).join(", ")}`); + } + for (const { name, err } of errors) { + if (backendHints.includes(name)) { + console.warn(`removing requested execution provider "${name}" from session options because it is not available: ${err}`); + } + } + const filteredEps = eps.filter((i) => availableBackendNames.has(typeof i === "string" ? i : i.name)); + return [ + backend, + new Proxy(options, { + get: (target, prop) => { + if (prop === "executionProviders") { + return filteredEps; + } + return Reflect.get(target, prop); + } + }) + ]; + }; + } + ), + /***/ + "./node_modules/onnxruntime-common/dist/esm/backend.js": ( + /*!*************************************************************!*\ + !*** ./node_modules/onnxruntime-common/dist/esm/backend.js ***! + \*************************************************************/ + /***/ + (__unused_webpack___webpack_module__, __webpack_exports__2, __webpack_require__2) => { + __webpack_require__2.r(__webpack_exports__2); + __webpack_require__2.d(__webpack_exports__2, { + /* harmony export */ + registerBackend: () => ( + /* reexport safe */ + _backend_impl_js__WEBPACK_IMPORTED_MODULE_0__.registerBackend + ) + /* harmony export */ + }); + var _backend_impl_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__2( + /*! ./backend-impl.js */ + "./node_modules/onnxruntime-common/dist/esm/backend-impl.js" + ); + } + ), + /***/ + "./node_modules/onnxruntime-common/dist/esm/env-impl.js": ( + /*!**************************************************************!*\ + !*** ./node_modules/onnxruntime-common/dist/esm/env-impl.js ***! + \**************************************************************/ + /***/ + (__unused_webpack___webpack_module__, __webpack_exports__2, __webpack_require__2) => { + __webpack_require__2.r(__webpack_exports__2); + __webpack_require__2.d(__webpack_exports__2, { + /* harmony export */ + env: () => ( + /* binding */ + env + ) + /* harmony export */ + }); + var _version_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__2( + /*! ./version.js */ + "./node_modules/onnxruntime-common/dist/esm/version.js" + ); + let logLevelValue = "warning"; + const env = { + wasm: {}, + webgl: {}, + webgpu: {}, + versions: { common: _version_js__WEBPACK_IMPORTED_MODULE_0__.version }, + set logLevel(value) { + if (value === void 0) { + return; + } + if (typeof value !== "string" || ["verbose", "info", "warning", "error", "fatal"].indexOf(value) === -1) { + throw new Error(`Unsupported logging level: ${value}`); + } + logLevelValue = value; + }, + get logLevel() { + return logLevelValue; + } + }; + Object.defineProperty(env, "logLevel", { enumerable: true }); + } + ), + /***/ + "./node_modules/onnxruntime-common/dist/esm/env.js": ( + /*!*********************************************************!*\ + !*** ./node_modules/onnxruntime-common/dist/esm/env.js ***! + \*********************************************************/ + /***/ + (__unused_webpack___webpack_module__, __webpack_exports__2, __webpack_require__2) => { + __webpack_require__2.r(__webpack_exports__2); + __webpack_require__2.d(__webpack_exports__2, { + /* harmony export */ + env: () => ( + /* binding */ + env + ) + /* harmony export */ + }); + var _env_impl_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__2( + /*! ./env-impl.js */ + "./node_modules/onnxruntime-common/dist/esm/env-impl.js" + ); + const env = _env_impl_js__WEBPACK_IMPORTED_MODULE_0__.env; + } + ), + /***/ + "./node_modules/onnxruntime-common/dist/esm/index.js": ( + /*!***********************************************************!*\ + !*** ./node_modules/onnxruntime-common/dist/esm/index.js ***! + \***********************************************************/ + /***/ + (__unused_webpack___webpack_module__, __webpack_exports__2, __webpack_require__2) => { + __webpack_require__2.r(__webpack_exports__2); + __webpack_require__2.d(__webpack_exports__2, { + /* harmony export */ + InferenceSession: () => ( + /* reexport safe */ + _inference_session_js__WEBPACK_IMPORTED_MODULE_2__.InferenceSession + ), + /* harmony export */ + TRACE: () => ( + /* reexport safe */ + _trace_js__WEBPACK_IMPORTED_MODULE_6__.TRACE + ), + /* harmony export */ + TRACE_FUNC_BEGIN: () => ( + /* reexport safe */ + _trace_js__WEBPACK_IMPORTED_MODULE_6__.TRACE_FUNC_BEGIN + ), + /* harmony export */ + TRACE_FUNC_END: () => ( + /* reexport safe */ + _trace_js__WEBPACK_IMPORTED_MODULE_6__.TRACE_FUNC_END + ), + /* harmony export */ + Tensor: () => ( + /* reexport safe */ + _tensor_js__WEBPACK_IMPORTED_MODULE_3__.Tensor + ), + /* harmony export */ + TrainingSession: () => ( + /* reexport safe */ + _training_session_js__WEBPACK_IMPORTED_MODULE_9__.TrainingSession + ), + /* harmony export */ + env: () => ( + /* reexport safe */ + _env_js__WEBPACK_IMPORTED_MODULE_1__.env + ), + /* harmony export */ + registerBackend: () => ( + /* reexport safe */ + _backend_js__WEBPACK_IMPORTED_MODULE_0__.registerBackend + ) + /* harmony export */ + }); + var _backend_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__2( + /*! ./backend.js */ + "./node_modules/onnxruntime-common/dist/esm/backend.js" + ); + var _env_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__2( + /*! ./env.js */ + "./node_modules/onnxruntime-common/dist/esm/env.js" + ); + var _inference_session_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__2( + /*! ./inference-session.js */ + "./node_modules/onnxruntime-common/dist/esm/inference-session.js" + ); + var _tensor_js__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__2( + /*! ./tensor.js */ + "./node_modules/onnxruntime-common/dist/esm/tensor.js" + ); + var _tensor_conversion_js__WEBPACK_IMPORTED_MODULE_4__ = __webpack_require__2( + /*! ./tensor-conversion.js */ + "./node_modules/onnxruntime-common/dist/esm/tensor-conversion.js" + ); + var _tensor_factory_js__WEBPACK_IMPORTED_MODULE_5__ = __webpack_require__2( + /*! ./tensor-factory.js */ + "./node_modules/onnxruntime-common/dist/esm/tensor-factory.js" + ); + var _trace_js__WEBPACK_IMPORTED_MODULE_6__ = __webpack_require__2( + /*! ./trace.js */ + "./node_modules/onnxruntime-common/dist/esm/trace.js" + ); + var _onnx_model_js__WEBPACK_IMPORTED_MODULE_7__ = __webpack_require__2( + /*! ./onnx-model.js */ + "./node_modules/onnxruntime-common/dist/esm/onnx-model.js" + ); + var _onnx_value_js__WEBPACK_IMPORTED_MODULE_8__ = __webpack_require__2( + /*! ./onnx-value.js */ + "./node_modules/onnxruntime-common/dist/esm/onnx-value.js" + ); + var _training_session_js__WEBPACK_IMPORTED_MODULE_9__ = __webpack_require__2( + /*! ./training-session.js */ + "./node_modules/onnxruntime-common/dist/esm/training-session.js" + ); + } + ), + /***/ + "./node_modules/onnxruntime-common/dist/esm/inference-session-impl.js": ( + /*!****************************************************************************!*\ + !*** ./node_modules/onnxruntime-common/dist/esm/inference-session-impl.js ***! + \****************************************************************************/ + /***/ + (__unused_webpack___webpack_module__, __webpack_exports__2, __webpack_require__2) => { + __webpack_require__2.r(__webpack_exports__2); + __webpack_require__2.d(__webpack_exports__2, { + /* harmony export */ + InferenceSession: () => ( + /* binding */ + InferenceSession + ) + /* harmony export */ + }); + var _backend_impl_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__2( + /*! ./backend-impl.js */ + "./node_modules/onnxruntime-common/dist/esm/backend-impl.js" + ); + var _tensor_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__2( + /*! ./tensor.js */ + "./node_modules/onnxruntime-common/dist/esm/tensor.js" + ); + var _trace_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__2( + /*! ./trace.js */ + "./node_modules/onnxruntime-common/dist/esm/trace.js" + ); + class InferenceSession { + constructor(handler) { + this.handler = handler; + } + async run(feeds, arg1, arg2) { + (0, _trace_js__WEBPACK_IMPORTED_MODULE_2__.TRACE_FUNC_BEGIN)(); + const fetches = {}; + let options = {}; + if (typeof feeds !== "object" || feeds === null || feeds instanceof _tensor_js__WEBPACK_IMPORTED_MODULE_1__.Tensor || Array.isArray(feeds)) { + throw new TypeError("'feeds' must be an object that use input names as keys and OnnxValue as corresponding values."); + } + let isFetchesEmpty = true; + if (typeof arg1 === "object") { + if (arg1 === null) { + throw new TypeError("Unexpected argument[1]: cannot be null."); + } + if (arg1 instanceof _tensor_js__WEBPACK_IMPORTED_MODULE_1__.Tensor) { + throw new TypeError("'fetches' cannot be a Tensor"); + } + if (Array.isArray(arg1)) { + if (arg1.length === 0) { + throw new TypeError("'fetches' cannot be an empty array."); + } + isFetchesEmpty = false; + for (const name of arg1) { + if (typeof name !== "string") { + throw new TypeError("'fetches' must be a string array or an object."); + } + if (this.outputNames.indexOf(name) === -1) { + throw new RangeError(`'fetches' contains invalid output name: ${name}.`); + } + fetches[name] = null; + } + if (typeof arg2 === "object" && arg2 !== null) { + options = arg2; + } else if (typeof arg2 !== "undefined") { + throw new TypeError("'options' must be an object."); + } + } else { + let isFetches = false; + const arg1Keys = Object.getOwnPropertyNames(arg1); + for (const name of this.outputNames) { + if (arg1Keys.indexOf(name) !== -1) { + const v = arg1[name]; + if (v === null || v instanceof _tensor_js__WEBPACK_IMPORTED_MODULE_1__.Tensor) { + isFetches = true; + isFetchesEmpty = false; + fetches[name] = v; + } + } + } + if (isFetches) { + if (typeof arg2 === "object" && arg2 !== null) { + options = arg2; + } else if (typeof arg2 !== "undefined") { + throw new TypeError("'options' must be an object."); + } + } else { + options = arg1; + } + } + } else if (typeof arg1 !== "undefined") { + throw new TypeError("Unexpected argument[1]: must be 'fetches' or 'options'."); + } + for (const name of this.inputNames) { + if (typeof feeds[name] === "undefined") { + throw new Error(`input '${name}' is missing in 'feeds'.`); + } + } + if (isFetchesEmpty) { + for (const name of this.outputNames) { + fetches[name] = null; + } + } + const results = await this.handler.run(feeds, fetches, options); + const returnValue = {}; + for (const key in results) { + if (Object.hasOwnProperty.call(results, key)) { + const result = results[key]; + if (result instanceof _tensor_js__WEBPACK_IMPORTED_MODULE_1__.Tensor) { + returnValue[key] = result; + } else { + returnValue[key] = new _tensor_js__WEBPACK_IMPORTED_MODULE_1__.Tensor(result.type, result.data, result.dims); + } + } + } + (0, _trace_js__WEBPACK_IMPORTED_MODULE_2__.TRACE_FUNC_END)(); + return returnValue; + } + async release() { + return this.handler.dispose(); + } + static async create(arg0, arg1, arg2, arg3) { + (0, _trace_js__WEBPACK_IMPORTED_MODULE_2__.TRACE_FUNC_BEGIN)(); + let filePathOrUint8Array; + let options = {}; + if (typeof arg0 === "string") { + filePathOrUint8Array = arg0; + if (typeof arg1 === "object" && arg1 !== null) { + options = arg1; + } else if (typeof arg1 !== "undefined") { + throw new TypeError("'options' must be an object."); + } + } else if (arg0 instanceof Uint8Array) { + filePathOrUint8Array = arg0; + if (typeof arg1 === "object" && arg1 !== null) { + options = arg1; + } else if (typeof arg1 !== "undefined") { + throw new TypeError("'options' must be an object."); + } + } else if (arg0 instanceof ArrayBuffer || typeof SharedArrayBuffer !== "undefined" && arg0 instanceof SharedArrayBuffer) { + const buffer = arg0; + let byteOffset = 0; + let byteLength = arg0.byteLength; + if (typeof arg1 === "object" && arg1 !== null) { + options = arg1; + } else if (typeof arg1 === "number") { + byteOffset = arg1; + if (!Number.isSafeInteger(byteOffset)) { + throw new RangeError("'byteOffset' must be an integer."); + } + if (byteOffset < 0 || byteOffset >= buffer.byteLength) { + throw new RangeError(`'byteOffset' is out of range [0, ${buffer.byteLength}).`); + } + byteLength = arg0.byteLength - byteOffset; + if (typeof arg2 === "number") { + byteLength = arg2; + if (!Number.isSafeInteger(byteLength)) { + throw new RangeError("'byteLength' must be an integer."); + } + if (byteLength <= 0 || byteOffset + byteLength > buffer.byteLength) { + throw new RangeError(`'byteLength' is out of range (0, ${buffer.byteLength - byteOffset}].`); + } + if (typeof arg3 === "object" && arg3 !== null) { + options = arg3; + } else if (typeof arg3 !== "undefined") { + throw new TypeError("'options' must be an object."); + } + } else if (typeof arg2 !== "undefined") { + throw new TypeError("'byteLength' must be a number."); + } + } else if (typeof arg1 !== "undefined") { + throw new TypeError("'options' must be an object."); + } + filePathOrUint8Array = new Uint8Array(buffer, byteOffset, byteLength); + } else { + throw new TypeError("Unexpected argument[0]: must be 'path' or 'buffer'."); + } + const [backend, optionsWithValidatedEPs] = await (0, _backend_impl_js__WEBPACK_IMPORTED_MODULE_0__.resolveBackendAndExecutionProviders)(options); + const handler = await backend.createInferenceSessionHandler(filePathOrUint8Array, optionsWithValidatedEPs); + (0, _trace_js__WEBPACK_IMPORTED_MODULE_2__.TRACE_FUNC_END)(); + return new InferenceSession(handler); + } + startProfiling() { + this.handler.startProfiling(); + } + endProfiling() { + this.handler.endProfiling(); + } + get inputNames() { + return this.handler.inputNames; + } + get outputNames() { + return this.handler.outputNames; + } + } + } + ), + /***/ + "./node_modules/onnxruntime-common/dist/esm/inference-session.js": ( + /*!***********************************************************************!*\ + !*** ./node_modules/onnxruntime-common/dist/esm/inference-session.js ***! + \***********************************************************************/ + /***/ + (__unused_webpack___webpack_module__, __webpack_exports__2, __webpack_require__2) => { + __webpack_require__2.r(__webpack_exports__2); + __webpack_require__2.d(__webpack_exports__2, { + /* harmony export */ + InferenceSession: () => ( + /* binding */ + InferenceSession + ) + /* harmony export */ + }); + var _inference_session_impl_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__2( + /*! ./inference-session-impl.js */ + "./node_modules/onnxruntime-common/dist/esm/inference-session-impl.js" + ); + const InferenceSession = _inference_session_impl_js__WEBPACK_IMPORTED_MODULE_0__.InferenceSession; + } + ), + /***/ + "./node_modules/onnxruntime-common/dist/esm/onnx-model.js": ( + /*!****************************************************************!*\ + !*** ./node_modules/onnxruntime-common/dist/esm/onnx-model.js ***! + \****************************************************************/ + /***/ + (__unused_webpack___webpack_module__, __webpack_exports__2, __webpack_require__2) => { + __webpack_require__2.r(__webpack_exports__2); + } + ), + /***/ + "./node_modules/onnxruntime-common/dist/esm/onnx-value.js": ( + /*!****************************************************************!*\ + !*** ./node_modules/onnxruntime-common/dist/esm/onnx-value.js ***! + \****************************************************************/ + /***/ + (__unused_webpack___webpack_module__, __webpack_exports__2, __webpack_require__2) => { + __webpack_require__2.r(__webpack_exports__2); + } + ), + /***/ + "./node_modules/onnxruntime-common/dist/esm/tensor-conversion-impl.js": ( + /*!****************************************************************************!*\ + !*** ./node_modules/onnxruntime-common/dist/esm/tensor-conversion-impl.js ***! + \****************************************************************************/ + /***/ + (__unused_webpack___webpack_module__, __webpack_exports__2, __webpack_require__2) => { + __webpack_require__2.r(__webpack_exports__2); + __webpack_require__2.d(__webpack_exports__2, { + /* harmony export */ + tensorToDataURL: () => ( + /* binding */ + tensorToDataURL + ), + /* harmony export */ + tensorToImageData: () => ( + /* binding */ + tensorToImageData + ) + /* harmony export */ + }); + const tensorToDataURL = (tensor, options) => { + const canvas = typeof document !== "undefined" ? document.createElement("canvas") : new OffscreenCanvas(1, 1); + canvas.width = tensor.dims[3]; + canvas.height = tensor.dims[2]; + const pixels2DContext = canvas.getContext("2d"); + if (pixels2DContext != null) { + let width; + let height; + if ((options == null ? void 0 : options.tensorLayout) !== void 0 && options.tensorLayout === "NHWC") { + width = tensor.dims[2]; + height = tensor.dims[3]; + } else { + width = tensor.dims[3]; + height = tensor.dims[2]; + } + const inputformat = (options == null ? void 0 : options.format) !== void 0 ? options.format : "RGB"; + const norm = options == null ? void 0 : options.norm; + let normMean; + let normBias; + if (norm === void 0 || norm.mean === void 0) { + normMean = [255, 255, 255, 255]; + } else { + if (typeof norm.mean === "number") { + normMean = [norm.mean, norm.mean, norm.mean, norm.mean]; + } else { + normMean = [norm.mean[0], norm.mean[1], norm.mean[2], 0]; + if (norm.mean[3] !== void 0) { + normMean[3] = norm.mean[3]; + } + } + } + if (norm === void 0 || norm.bias === void 0) { + normBias = [0, 0, 0, 0]; + } else { + if (typeof norm.bias === "number") { + normBias = [norm.bias, norm.bias, norm.bias, norm.bias]; + } else { + normBias = [norm.bias[0], norm.bias[1], norm.bias[2], 0]; + if (norm.bias[3] !== void 0) { + normBias[3] = norm.bias[3]; + } + } + } + const stride = height * width; + let rTensorPointer = 0, gTensorPointer = stride, bTensorPointer = stride * 2, aTensorPointer = -1; + if (inputformat === "RGBA") { + rTensorPointer = 0; + gTensorPointer = stride; + bTensorPointer = stride * 2; + aTensorPointer = stride * 3; + } else if (inputformat === "RGB") { + rTensorPointer = 0; + gTensorPointer = stride; + bTensorPointer = stride * 2; + } else if (inputformat === "RBG") { + rTensorPointer = 0; + bTensorPointer = stride; + gTensorPointer = stride * 2; + } + for (let i = 0; i < height; i++) { + for (let j = 0; j < width; j++) { + const R = (tensor.data[rTensorPointer++] - normBias[0]) * normMean[0]; + const G = (tensor.data[gTensorPointer++] - normBias[1]) * normMean[1]; + const B = (tensor.data[bTensorPointer++] - normBias[2]) * normMean[2]; + const A = aTensorPointer === -1 ? 255 : (tensor.data[aTensorPointer++] - normBias[3]) * normMean[3]; + pixels2DContext.fillStyle = "rgba(" + R + "," + G + "," + B + "," + A + ")"; + pixels2DContext.fillRect(j, i, 1, 1); + } + } + if ("toDataURL" in canvas) { + return canvas.toDataURL(); + } else { + throw new Error("toDataURL is not supported"); + } + } else { + throw new Error("Can not access image data"); + } + }; + const tensorToImageData = (tensor, options) => { + const pixels2DContext = typeof document !== "undefined" ? document.createElement("canvas").getContext("2d") : new OffscreenCanvas(1, 1).getContext("2d"); + let image; + if (pixels2DContext != null) { + let width; + let height; + let channels; + if ((options == null ? void 0 : options.tensorLayout) !== void 0 && options.tensorLayout === "NHWC") { + width = tensor.dims[2]; + height = tensor.dims[1]; + channels = tensor.dims[3]; + } else { + width = tensor.dims[3]; + height = tensor.dims[2]; + channels = tensor.dims[1]; + } + const inputformat = options !== void 0 ? options.format !== void 0 ? options.format : "RGB" : "RGB"; + const norm = options == null ? void 0 : options.norm; + let normMean; + let normBias; + if (norm === void 0 || norm.mean === void 0) { + normMean = [255, 255, 255, 255]; + } else { + if (typeof norm.mean === "number") { + normMean = [norm.mean, norm.mean, norm.mean, norm.mean]; + } else { + normMean = [norm.mean[0], norm.mean[1], norm.mean[2], 255]; + if (norm.mean[3] !== void 0) { + normMean[3] = norm.mean[3]; + } + } + } + if (norm === void 0 || norm.bias === void 0) { + normBias = [0, 0, 0, 0]; + } else { + if (typeof norm.bias === "number") { + normBias = [norm.bias, norm.bias, norm.bias, norm.bias]; + } else { + normBias = [norm.bias[0], norm.bias[1], norm.bias[2], 0]; + if (norm.bias[3] !== void 0) { + normBias[3] = norm.bias[3]; + } + } + } + const stride = height * width; + if (options !== void 0) { + if (options.format !== void 0 && (channels === 4 && options.format !== "RGBA") || channels === 3 && (options.format !== "RGB" && options.format !== "BGR")) { + throw new Error("Tensor format doesn't match input tensor dims"); + } + } + const step = 4; + let rImagePointer = 0, gImagePointer = 1, bImagePointer = 2, aImagePointer = 3; + let rTensorPointer = 0, gTensorPointer = stride, bTensorPointer = stride * 2, aTensorPointer = -1; + if (inputformat === "RGBA") { + rTensorPointer = 0; + gTensorPointer = stride; + bTensorPointer = stride * 2; + aTensorPointer = stride * 3; + } else if (inputformat === "RGB") { + rTensorPointer = 0; + gTensorPointer = stride; + bTensorPointer = stride * 2; + } else if (inputformat === "RBG") { + rTensorPointer = 0; + bTensorPointer = stride; + gTensorPointer = stride * 2; + } + image = pixels2DContext.createImageData(width, height); + for (let i = 0; i < height * width; rImagePointer += step, gImagePointer += step, bImagePointer += step, aImagePointer += step, i++) { + image.data[rImagePointer] = (tensor.data[rTensorPointer++] - normBias[0]) * normMean[0]; + image.data[gImagePointer] = (tensor.data[gTensorPointer++] - normBias[1]) * normMean[1]; + image.data[bImagePointer] = (tensor.data[bTensorPointer++] - normBias[2]) * normMean[2]; + image.data[aImagePointer] = aTensorPointer === -1 ? 255 : (tensor.data[aTensorPointer++] - normBias[3]) * normMean[3]; + } + } else { + throw new Error("Can not access image data"); + } + return image; + }; + } + ), + /***/ + "./node_modules/onnxruntime-common/dist/esm/tensor-conversion.js": ( + /*!***********************************************************************!*\ + !*** ./node_modules/onnxruntime-common/dist/esm/tensor-conversion.js ***! + \***********************************************************************/ + /***/ + (__unused_webpack___webpack_module__, __webpack_exports__2, __webpack_require__2) => { + __webpack_require__2.r(__webpack_exports__2); + } + ), + /***/ + "./node_modules/onnxruntime-common/dist/esm/tensor-factory-impl.js": ( + /*!*************************************************************************!*\ + !*** ./node_modules/onnxruntime-common/dist/esm/tensor-factory-impl.js ***! + \*************************************************************************/ + /***/ + (__unused_webpack___webpack_module__, __webpack_exports__2, __webpack_require__2) => { + __webpack_require__2.r(__webpack_exports__2); + __webpack_require__2.d(__webpack_exports__2, { + /* harmony export */ + bufferToTensor: () => ( + /* binding */ + bufferToTensor + ), + /* harmony export */ + tensorFromGpuBuffer: () => ( + /* binding */ + tensorFromGpuBuffer + ), + /* harmony export */ + tensorFromImage: () => ( + /* binding */ + tensorFromImage + ), + /* harmony export */ + tensorFromPinnedBuffer: () => ( + /* binding */ + tensorFromPinnedBuffer + ), + /* harmony export */ + tensorFromTexture: () => ( + /* binding */ + tensorFromTexture + ) + /* harmony export */ + }); + var _tensor_impl_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__2( + /*! ./tensor-impl.js */ + "./node_modules/onnxruntime-common/dist/esm/tensor-impl.js" + ); + const bufferToTensor = (buffer, options) => { + if (buffer === void 0) { + throw new Error("Image buffer must be defined"); + } + if (options.height === void 0 || options.width === void 0) { + throw new Error("Image height and width must be defined"); + } + if (options.tensorLayout === "NHWC") { + throw new Error("NHWC Tensor layout is not supported yet"); + } + const { height, width } = options; + const norm = options.norm ?? { mean: 255, bias: 0 }; + let normMean; + let normBias; + if (typeof norm.mean === "number") { + normMean = [norm.mean, norm.mean, norm.mean, norm.mean]; + } else { + normMean = [norm.mean[0], norm.mean[1], norm.mean[2], norm.mean[3] ?? 255]; + } + if (typeof norm.bias === "number") { + normBias = [norm.bias, norm.bias, norm.bias, norm.bias]; + } else { + normBias = [norm.bias[0], norm.bias[1], norm.bias[2], norm.bias[3] ?? 0]; + } + const inputformat = options.format !== void 0 ? options.format : "RGBA"; + const outputformat = options.tensorFormat !== void 0 ? options.tensorFormat !== void 0 ? options.tensorFormat : "RGB" : "RGB"; + const stride = height * width; + const float32Data = outputformat === "RGBA" ? new Float32Array(stride * 4) : new Float32Array(stride * 3); + let step = 4, rImagePointer = 0, gImagePointer = 1, bImagePointer = 2, aImagePointer = 3; + let rTensorPointer = 0, gTensorPointer = stride, bTensorPointer = stride * 2, aTensorPointer = -1; + if (inputformat === "RGB") { + step = 3; + rImagePointer = 0; + gImagePointer = 1; + bImagePointer = 2; + aImagePointer = -1; + } + if (outputformat === "RGBA") { + aTensorPointer = stride * 3; + } else if (outputformat === "RBG") { + rTensorPointer = 0; + bTensorPointer = stride; + gTensorPointer = stride * 2; + } else if (outputformat === "BGR") { + bTensorPointer = 0; + gTensorPointer = stride; + rTensorPointer = stride * 2; + } + for (let i = 0; i < stride; i++, rImagePointer += step, bImagePointer += step, gImagePointer += step, aImagePointer += step) { + float32Data[rTensorPointer++] = (buffer[rImagePointer] + normBias[0]) / normMean[0]; + float32Data[gTensorPointer++] = (buffer[gImagePointer] + normBias[1]) / normMean[1]; + float32Data[bTensorPointer++] = (buffer[bImagePointer] + normBias[2]) / normMean[2]; + if (aTensorPointer !== -1 && aImagePointer !== -1) { + float32Data[aTensorPointer++] = (buffer[aImagePointer] + normBias[3]) / normMean[3]; + } + } + const outputTensor = outputformat === "RGBA" ? new _tensor_impl_js__WEBPACK_IMPORTED_MODULE_0__.Tensor("float32", float32Data, [1, 4, height, width]) : new _tensor_impl_js__WEBPACK_IMPORTED_MODULE_0__.Tensor("float32", float32Data, [1, 3, height, width]); + return outputTensor; + }; + const tensorFromImage = async (image, options) => { + const isHTMLImageEle = typeof HTMLImageElement !== "undefined" && image instanceof HTMLImageElement; + const isImageDataEle = typeof ImageData !== "undefined" && image instanceof ImageData; + const isImageBitmap = typeof ImageBitmap !== "undefined" && image instanceof ImageBitmap; + const isString = typeof image === "string"; + let data; + let bufferToTensorOptions = options ?? {}; + const createCanvas = () => { + if (typeof document !== "undefined") { + return document.createElement("canvas"); + } else if (typeof OffscreenCanvas !== "undefined") { + return new OffscreenCanvas(1, 1); + } else { + throw new Error("Canvas is not supported"); + } + }; + const createCanvasContext = (canvas) => { + if (canvas instanceof HTMLCanvasElement) { + return canvas.getContext("2d"); + } else if (canvas instanceof OffscreenCanvas) { + return canvas.getContext("2d"); + } else { + return null; + } + }; + if (isHTMLImageEle) { + const canvas = createCanvas(); + canvas.width = image.width; + canvas.height = image.height; + const pixels2DContext = createCanvasContext(canvas); + if (pixels2DContext != null) { + let height = image.height; + let width = image.width; + if (options !== void 0 && options.resizedHeight !== void 0 && options.resizedWidth !== void 0) { + height = options.resizedHeight; + width = options.resizedWidth; + } + if (options !== void 0) { + bufferToTensorOptions = options; + if (options.tensorFormat !== void 0) { + throw new Error("Image input config format must be RGBA for HTMLImageElement"); + } else { + bufferToTensorOptions.tensorFormat = "RGBA"; + } + bufferToTensorOptions.height = height; + bufferToTensorOptions.width = width; + } else { + bufferToTensorOptions.tensorFormat = "RGBA"; + bufferToTensorOptions.height = height; + bufferToTensorOptions.width = width; + } + pixels2DContext.drawImage(image, 0, 0); + data = pixels2DContext.getImageData(0, 0, width, height).data; + } else { + throw new Error("Can not access image data"); + } + } else if (isImageDataEle) { + let height; + let width; + if (options !== void 0 && options.resizedWidth !== void 0 && options.resizedHeight !== void 0) { + height = options.resizedHeight; + width = options.resizedWidth; + } else { + height = image.height; + width = image.width; + } + if (options !== void 0) { + bufferToTensorOptions = options; + } + bufferToTensorOptions.format = "RGBA"; + bufferToTensorOptions.height = height; + bufferToTensorOptions.width = width; + if (options !== void 0) { + const tempCanvas = createCanvas(); + tempCanvas.width = width; + tempCanvas.height = height; + const pixels2DContext = createCanvasContext(tempCanvas); + if (pixels2DContext != null) { + pixels2DContext.putImageData(image, 0, 0); + data = pixels2DContext.getImageData(0, 0, width, height).data; + } else { + throw new Error("Can not access image data"); + } + } else { + data = image.data; + } + } else if (isImageBitmap) { + if (options === void 0) { + throw new Error("Please provide image config with format for Imagebitmap"); + } + const canvas = createCanvas(); + canvas.width = image.width; + canvas.height = image.height; + const pixels2DContext = createCanvasContext(canvas); + if (pixels2DContext != null) { + const height = image.height; + const width = image.width; + pixels2DContext.drawImage(image, 0, 0, width, height); + data = pixels2DContext.getImageData(0, 0, width, height).data; + bufferToTensorOptions.height = height; + bufferToTensorOptions.width = width; + return bufferToTensor(data, bufferToTensorOptions); + } else { + throw new Error("Can not access image data"); + } + } else if (isString) { + return new Promise((resolve, reject) => { + const canvas = createCanvas(); + const context = createCanvasContext(canvas); + if (!image || !context) { + return reject(); + } + const newImage = new Image(); + newImage.crossOrigin = "Anonymous"; + newImage.src = image; + newImage.onload = () => { + canvas.width = newImage.width; + canvas.height = newImage.height; + context.drawImage(newImage, 0, 0, canvas.width, canvas.height); + const img = context.getImageData(0, 0, canvas.width, canvas.height); + bufferToTensorOptions.height = canvas.height; + bufferToTensorOptions.width = canvas.width; + resolve(bufferToTensor(img.data, bufferToTensorOptions)); + }; + }); + } else { + throw new Error("Input data provided is not supported - aborted tensor creation"); + } + if (data !== void 0) { + return bufferToTensor(data, bufferToTensorOptions); + } else { + throw new Error("Input data provided is not supported - aborted tensor creation"); + } + }; + const tensorFromTexture = (texture, options) => { + const { width, height, download, dispose } = options; + const dims = [1, height, width, 4]; + return new _tensor_impl_js__WEBPACK_IMPORTED_MODULE_0__.Tensor({ location: "texture", type: "float32", texture, dims, download, dispose }); + }; + const tensorFromGpuBuffer = (gpuBuffer, options) => { + const { dataType, dims, download, dispose } = options; + return new _tensor_impl_js__WEBPACK_IMPORTED_MODULE_0__.Tensor({ location: "gpu-buffer", type: dataType ?? "float32", gpuBuffer, dims, download, dispose }); + }; + const tensorFromPinnedBuffer = (type, buffer, dims) => new _tensor_impl_js__WEBPACK_IMPORTED_MODULE_0__.Tensor({ location: "cpu-pinned", type, data: buffer, dims: dims ?? [buffer.length] }); + } + ), + /***/ + "./node_modules/onnxruntime-common/dist/esm/tensor-factory.js": ( + /*!********************************************************************!*\ + !*** ./node_modules/onnxruntime-common/dist/esm/tensor-factory.js ***! + \********************************************************************/ + /***/ + (__unused_webpack___webpack_module__, __webpack_exports__2, __webpack_require__2) => { + __webpack_require__2.r(__webpack_exports__2); + } + ), + /***/ + "./node_modules/onnxruntime-common/dist/esm/tensor-impl-type-mapping.js": ( + /*!******************************************************************************!*\ + !*** ./node_modules/onnxruntime-common/dist/esm/tensor-impl-type-mapping.js ***! + \******************************************************************************/ + /***/ + (__unused_webpack___webpack_module__, __webpack_exports__2, __webpack_require__2) => { + __webpack_require__2.r(__webpack_exports__2); + __webpack_require__2.d(__webpack_exports__2, { + /* harmony export */ + NUMERIC_TENSOR_TYPEDARRAY_TO_TYPE_MAP: () => ( + /* binding */ + NUMERIC_TENSOR_TYPEDARRAY_TO_TYPE_MAP + ), + /* harmony export */ + NUMERIC_TENSOR_TYPE_TO_TYPEDARRAY_MAP: () => ( + /* binding */ + NUMERIC_TENSOR_TYPE_TO_TYPEDARRAY_MAP + ), + /* harmony export */ + checkTypedArray: () => ( + /* binding */ + checkTypedArray + ) + /* harmony export */ + }); + const NUMERIC_TENSOR_TYPE_TO_TYPEDARRAY_MAP = /* @__PURE__ */ new Map([ + ["float32", Float32Array], + ["uint8", Uint8Array], + ["int8", Int8Array], + ["uint16", Uint16Array], + ["int16", Int16Array], + ["int32", Int32Array], + ["bool", Uint8Array], + ["float64", Float64Array], + ["uint32", Uint32Array] + ]); + const NUMERIC_TENSOR_TYPEDARRAY_TO_TYPE_MAP = /* @__PURE__ */ new Map([ + [Float32Array, "float32"], + [Uint8Array, "uint8"], + [Int8Array, "int8"], + [Uint16Array, "uint16"], + [Int16Array, "int16"], + [Int32Array, "int32"], + [Float64Array, "float64"], + [Uint32Array, "uint32"] + ]); + let isTypedArrayChecked = false; + const checkTypedArray = () => { + if (!isTypedArrayChecked) { + isTypedArrayChecked = true; + const isBigInt64ArrayAvailable = typeof BigInt64Array !== "undefined" && BigInt64Array.from; + const isBigUint64ArrayAvailable = typeof BigUint64Array !== "undefined" && BigUint64Array.from; + const isFloat16ArrayAvailable = typeof Float16Array !== "undefined" && Float16Array.from; + if (isBigInt64ArrayAvailable) { + NUMERIC_TENSOR_TYPE_TO_TYPEDARRAY_MAP.set("int64", BigInt64Array); + NUMERIC_TENSOR_TYPEDARRAY_TO_TYPE_MAP.set(BigInt64Array, "int64"); + } + if (isBigUint64ArrayAvailable) { + NUMERIC_TENSOR_TYPE_TO_TYPEDARRAY_MAP.set("uint64", BigUint64Array); + NUMERIC_TENSOR_TYPEDARRAY_TO_TYPE_MAP.set(BigUint64Array, "uint64"); + } + if (isFloat16ArrayAvailable) { + NUMERIC_TENSOR_TYPE_TO_TYPEDARRAY_MAP.set("float16", Float16Array); + NUMERIC_TENSOR_TYPEDARRAY_TO_TYPE_MAP.set(Float16Array, "float16"); + } else { + NUMERIC_TENSOR_TYPE_TO_TYPEDARRAY_MAP.set("float16", Uint16Array); + } + } + }; + } + ), + /***/ + "./node_modules/onnxruntime-common/dist/esm/tensor-impl.js": ( + /*!*****************************************************************!*\ + !*** ./node_modules/onnxruntime-common/dist/esm/tensor-impl.js ***! + \*****************************************************************/ + /***/ + (__unused_webpack___webpack_module__, __webpack_exports__2, __webpack_require__2) => { + __webpack_require__2.r(__webpack_exports__2); + __webpack_require__2.d(__webpack_exports__2, { + /* harmony export */ + Tensor: () => ( + /* binding */ + Tensor + ) + /* harmony export */ + }); + var _tensor_conversion_impl_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__2( + /*! ./tensor-conversion-impl.js */ + "./node_modules/onnxruntime-common/dist/esm/tensor-conversion-impl.js" + ); + var _tensor_factory_impl_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__2( + /*! ./tensor-factory-impl.js */ + "./node_modules/onnxruntime-common/dist/esm/tensor-factory-impl.js" + ); + var _tensor_impl_type_mapping_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__2( + /*! ./tensor-impl-type-mapping.js */ + "./node_modules/onnxruntime-common/dist/esm/tensor-impl-type-mapping.js" + ); + var _tensor_utils_impl_js__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__2( + /*! ./tensor-utils-impl.js */ + "./node_modules/onnxruntime-common/dist/esm/tensor-utils-impl.js" + ); + class Tensor { + /** + * implementation. + */ + constructor(arg0, arg1, arg2) { + (0, _tensor_impl_type_mapping_js__WEBPACK_IMPORTED_MODULE_2__.checkTypedArray)(); + let type; + let dims; + if (typeof arg0 === "object" && "location" in arg0) { + this.dataLocation = arg0.location; + type = arg0.type; + dims = arg0.dims; + switch (arg0.location) { + case "cpu-pinned": { + const expectedTypedArrayConstructor = _tensor_impl_type_mapping_js__WEBPACK_IMPORTED_MODULE_2__.NUMERIC_TENSOR_TYPE_TO_TYPEDARRAY_MAP.get(type); + if (!expectedTypedArrayConstructor) { + throw new TypeError(`unsupported type "${type}" to create tensor from pinned buffer`); + } + if (!(arg0.data instanceof expectedTypedArrayConstructor)) { + throw new TypeError(`buffer should be of type ${expectedTypedArrayConstructor.name}`); + } + this.cpuData = arg0.data; + break; + } + case "texture": { + if (type !== "float32") { + throw new TypeError(`unsupported type "${type}" to create tensor from texture`); + } + this.gpuTextureData = arg0.texture; + this.downloader = arg0.download; + this.disposer = arg0.dispose; + break; + } + case "gpu-buffer": { + if (type !== "float32" && type !== "float16" && type !== "int32" && type !== "int64" && type !== "uint32" && type !== "uint8" && type !== "bool") { + throw new TypeError(`unsupported type "${type}" to create tensor from gpu buffer`); + } + this.gpuBufferData = arg0.gpuBuffer; + this.downloader = arg0.download; + this.disposer = arg0.dispose; + break; + } + default: + throw new Error(`Tensor constructor: unsupported location '${this.dataLocation}'`); + } + } else { + let data; + let maybeDims; + if (typeof arg0 === "string") { + type = arg0; + maybeDims = arg2; + if (arg0 === "string") { + if (!Array.isArray(arg1)) { + throw new TypeError("A string tensor's data must be a string array."); + } + data = arg1; + } else { + const typedArrayConstructor = _tensor_impl_type_mapping_js__WEBPACK_IMPORTED_MODULE_2__.NUMERIC_TENSOR_TYPE_TO_TYPEDARRAY_MAP.get(arg0); + if (typedArrayConstructor === void 0) { + throw new TypeError(`Unsupported tensor type: ${arg0}.`); + } + if (Array.isArray(arg1)) { + if (arg0 === "float16" && typedArrayConstructor === Uint16Array) { + throw new TypeError("Creating a float16 tensor from number array is not supported. Please use Uint16Array as data."); + } else if (arg0 === "uint64" || arg0 === "int64") { + data = typedArrayConstructor.from(arg1, BigInt); + } else { + data = typedArrayConstructor.from(arg1); + } + } else if (arg1 instanceof typedArrayConstructor) { + data = arg1; + } else { + throw new TypeError(`A ${type} tensor's data must be type of ${typedArrayConstructor}`); + } + } + } else { + maybeDims = arg1; + if (Array.isArray(arg0)) { + if (arg0.length === 0) { + throw new TypeError("Tensor type cannot be inferred from an empty array."); + } + const firstElementType = typeof arg0[0]; + if (firstElementType === "string") { + type = "string"; + data = arg0; + } else if (firstElementType === "boolean") { + type = "bool"; + data = Uint8Array.from(arg0); + } else { + throw new TypeError(`Invalid element type of data array: ${firstElementType}.`); + } + } else { + const mappedType = _tensor_impl_type_mapping_js__WEBPACK_IMPORTED_MODULE_2__.NUMERIC_TENSOR_TYPEDARRAY_TO_TYPE_MAP.get(arg0.constructor); + if (mappedType === void 0) { + throw new TypeError(`Unsupported type for tensor data: ${arg0.constructor}.`); + } + type = mappedType; + data = arg0; + } + } + if (maybeDims === void 0) { + maybeDims = [data.length]; + } else if (!Array.isArray(maybeDims)) { + throw new TypeError("A tensor's dims must be a number array"); + } + dims = maybeDims; + this.cpuData = data; + this.dataLocation = "cpu"; + } + const size = (0, _tensor_utils_impl_js__WEBPACK_IMPORTED_MODULE_3__.calculateSize)(dims); + if (this.cpuData && size !== this.cpuData.length) { + throw new Error(`Tensor's size(${size}) does not match data length(${this.cpuData.length}).`); + } + this.type = type; + this.dims = dims; + this.size = size; + } + // #endregion + // #region factory + static async fromImage(image, options) { + return (0, _tensor_factory_impl_js__WEBPACK_IMPORTED_MODULE_1__.tensorFromImage)(image, options); + } + static fromTexture(texture, options) { + return (0, _tensor_factory_impl_js__WEBPACK_IMPORTED_MODULE_1__.tensorFromTexture)(texture, options); + } + static fromGpuBuffer(gpuBuffer, options) { + return (0, _tensor_factory_impl_js__WEBPACK_IMPORTED_MODULE_1__.tensorFromGpuBuffer)(gpuBuffer, options); + } + static fromPinnedBuffer(type, buffer, dims) { + return (0, _tensor_factory_impl_js__WEBPACK_IMPORTED_MODULE_1__.tensorFromPinnedBuffer)(type, buffer, dims); + } + // #endregion + // #region conversions + toDataURL(options) { + return (0, _tensor_conversion_impl_js__WEBPACK_IMPORTED_MODULE_0__.tensorToDataURL)(this, options); + } + toImageData(options) { + return (0, _tensor_conversion_impl_js__WEBPACK_IMPORTED_MODULE_0__.tensorToImageData)(this, options); + } + // #endregion + // #region properties + get data() { + this.ensureValid(); + if (!this.cpuData) { + throw new Error("The data is not on CPU. Use `getData()` to download GPU data to CPU, or use `texture` or `gpuBuffer` property to access the GPU data directly."); + } + return this.cpuData; + } + get location() { + return this.dataLocation; + } + get texture() { + this.ensureValid(); + if (!this.gpuTextureData) { + throw new Error("The data is not stored as a WebGL texture."); + } + return this.gpuTextureData; + } + get gpuBuffer() { + this.ensureValid(); + if (!this.gpuBufferData) { + throw new Error("The data is not stored as a WebGPU buffer."); + } + return this.gpuBufferData; + } + // #endregion + // #region methods + async getData(releaseData) { + this.ensureValid(); + switch (this.dataLocation) { + case "cpu": + case "cpu-pinned": + return this.data; + case "texture": + case "gpu-buffer": { + if (!this.downloader) { + throw new Error("The current tensor is not created with a specified data downloader."); + } + if (this.isDownloading) { + throw new Error("The current tensor is being downloaded."); + } + try { + this.isDownloading = true; + const data = await this.downloader(); + this.downloader = void 0; + this.dataLocation = "cpu"; + this.cpuData = data; + if (releaseData && this.disposer) { + this.disposer(); + this.disposer = void 0; + } + return data; + } finally { + this.isDownloading = false; + } + } + default: + throw new Error(`cannot get data from location: ${this.dataLocation}`); + } + } + dispose() { + if (this.isDownloading) { + throw new Error("The current tensor is being downloaded."); + } + if (this.disposer) { + this.disposer(); + this.disposer = void 0; + } + this.cpuData = void 0; + this.gpuTextureData = void 0; + this.gpuBufferData = void 0; + this.downloader = void 0; + this.isDownloading = void 0; + this.dataLocation = "none"; + } + // #endregion + // #region tensor utilities + ensureValid() { + if (this.dataLocation === "none") { + throw new Error("The tensor is disposed."); + } + } + reshape(dims) { + this.ensureValid(); + if (this.downloader || this.disposer) { + throw new Error("Cannot reshape a tensor that owns GPU resource."); + } + return (0, _tensor_utils_impl_js__WEBPACK_IMPORTED_MODULE_3__.tensorReshape)(this, dims); + } + } + } + ), + /***/ + "./node_modules/onnxruntime-common/dist/esm/tensor-utils-impl.js": ( + /*!***********************************************************************!*\ + !*** ./node_modules/onnxruntime-common/dist/esm/tensor-utils-impl.js ***! + \***********************************************************************/ + /***/ + (__unused_webpack___webpack_module__, __webpack_exports__2, __webpack_require__2) => { + __webpack_require__2.r(__webpack_exports__2); + __webpack_require__2.d(__webpack_exports__2, { + /* harmony export */ + calculateSize: () => ( + /* binding */ + calculateSize + ), + /* harmony export */ + tensorReshape: () => ( + /* binding */ + tensorReshape + ) + /* harmony export */ + }); + var _tensor_impl_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__2( + /*! ./tensor-impl.js */ + "./node_modules/onnxruntime-common/dist/esm/tensor-impl.js" + ); + const calculateSize = (dims) => { + let size = 1; + for (let i = 0; i < dims.length; i++) { + const dim = dims[i]; + if (typeof dim !== "number" || !Number.isSafeInteger(dim)) { + throw new TypeError(`dims[${i}] must be an integer, got: ${dim}`); + } + if (dim < 0) { + throw new RangeError(`dims[${i}] must be a non-negative integer, got: ${dim}`); + } + size *= dim; + } + return size; + }; + const tensorReshape = (tensor, dims) => { + switch (tensor.location) { + case "cpu": + return new _tensor_impl_js__WEBPACK_IMPORTED_MODULE_0__.Tensor(tensor.type, tensor.data, dims); + case "cpu-pinned": + return new _tensor_impl_js__WEBPACK_IMPORTED_MODULE_0__.Tensor({ + location: "cpu-pinned", + data: tensor.data, + type: tensor.type, + dims + }); + case "texture": + return new _tensor_impl_js__WEBPACK_IMPORTED_MODULE_0__.Tensor({ + location: "texture", + texture: tensor.texture, + type: tensor.type, + dims + }); + case "gpu-buffer": + return new _tensor_impl_js__WEBPACK_IMPORTED_MODULE_0__.Tensor({ + location: "gpu-buffer", + gpuBuffer: tensor.gpuBuffer, + type: tensor.type, + dims + }); + default: + throw new Error(`tensorReshape: tensor location ${tensor.location} is not supported`); + } + }; + } + ), + /***/ + "./node_modules/onnxruntime-common/dist/esm/tensor.js": ( + /*!************************************************************!*\ + !*** ./node_modules/onnxruntime-common/dist/esm/tensor.js ***! + \************************************************************/ + /***/ + (__unused_webpack___webpack_module__, __webpack_exports__2, __webpack_require__2) => { + __webpack_require__2.r(__webpack_exports__2); + __webpack_require__2.d(__webpack_exports__2, { + /* harmony export */ + Tensor: () => ( + /* binding */ + Tensor + ) + /* harmony export */ + }); + var _tensor_impl_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__2( + /*! ./tensor-impl.js */ + "./node_modules/onnxruntime-common/dist/esm/tensor-impl.js" + ); + const Tensor = _tensor_impl_js__WEBPACK_IMPORTED_MODULE_0__.Tensor; + } + ), + /***/ + "./node_modules/onnxruntime-common/dist/esm/trace.js": ( + /*!***********************************************************!*\ + !*** ./node_modules/onnxruntime-common/dist/esm/trace.js ***! + \***********************************************************/ + /***/ + (__unused_webpack___webpack_module__, __webpack_exports__2, __webpack_require__2) => { + __webpack_require__2.r(__webpack_exports__2); + __webpack_require__2.d(__webpack_exports__2, { + /* harmony export */ + TRACE: () => ( + /* binding */ + TRACE + ), + /* harmony export */ + TRACE_FUNC_BEGIN: () => ( + /* binding */ + TRACE_FUNC_BEGIN + ), + /* harmony export */ + TRACE_FUNC_END: () => ( + /* binding */ + TRACE_FUNC_END + ) + /* harmony export */ + }); + var _env_impl_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__2( + /*! ./env-impl.js */ + "./node_modules/onnxruntime-common/dist/esm/env-impl.js" + ); + const TRACE = (deviceType, label) => { + if (typeof _env_impl_js__WEBPACK_IMPORTED_MODULE_0__.env.trace === "undefined" ? !_env_impl_js__WEBPACK_IMPORTED_MODULE_0__.env.wasm.trace : !_env_impl_js__WEBPACK_IMPORTED_MODULE_0__.env.trace) { + return; + } + console.timeStamp(`${deviceType}::ORT::${label}`); + }; + const TRACE_FUNC = (msg, extraMsg) => { + var _a; + const stack = ((_a = new Error().stack) == null ? void 0 : _a.split(/\r\n|\r|\n/g)) || []; + let hasTraceFunc = false; + for (let i = 0; i < stack.length; i++) { + if (hasTraceFunc && !stack[i].includes("TRACE_FUNC")) { + let label = `FUNC_${msg}::${stack[i].trim().split(" ")[1]}`; + if (extraMsg) { + label += `::${extraMsg}`; + } + TRACE("CPU", label); + return; + } + if (stack[i].includes("TRACE_FUNC")) { + hasTraceFunc = true; + } + } + }; + const TRACE_FUNC_BEGIN = (extraMsg) => { + if (typeof _env_impl_js__WEBPACK_IMPORTED_MODULE_0__.env.trace === "undefined" ? !_env_impl_js__WEBPACK_IMPORTED_MODULE_0__.env.wasm.trace : !_env_impl_js__WEBPACK_IMPORTED_MODULE_0__.env.trace) { + return; + } + TRACE_FUNC("BEGIN", extraMsg); + }; + const TRACE_FUNC_END = (extraMsg) => { + if (typeof _env_impl_js__WEBPACK_IMPORTED_MODULE_0__.env.trace === "undefined" ? !_env_impl_js__WEBPACK_IMPORTED_MODULE_0__.env.wasm.trace : !_env_impl_js__WEBPACK_IMPORTED_MODULE_0__.env.trace) { + return; + } + TRACE_FUNC("END", extraMsg); + }; + } + ), + /***/ + "./node_modules/onnxruntime-common/dist/esm/training-session-impl.js": ( + /*!***************************************************************************!*\ + !*** ./node_modules/onnxruntime-common/dist/esm/training-session-impl.js ***! + \***************************************************************************/ + /***/ + (__unused_webpack___webpack_module__, __webpack_exports__2, __webpack_require__2) => { + __webpack_require__2.r(__webpack_exports__2); + __webpack_require__2.d(__webpack_exports__2, { + /* harmony export */ + TrainingSession: () => ( + /* binding */ + TrainingSession + ) + /* harmony export */ + }); + var _backend_impl_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__2( + /*! ./backend-impl.js */ + "./node_modules/onnxruntime-common/dist/esm/backend-impl.js" + ); + var _tensor_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__2( + /*! ./tensor.js */ + "./node_modules/onnxruntime-common/dist/esm/tensor.js" + ); + const noBackendErrMsg = "Training backend could not be resolved. Make sure you're using the correct configuration & WebAssembly files."; + class TrainingSession { + constructor(handler, hasOptimizerModel, hasEvalModel) { + this.handler = handler; + this.hasOptimizerModel = hasOptimizerModel; + this.hasEvalModel = hasEvalModel; + } + get trainingInputNames() { + return this.handler.inputNames; + } + get trainingOutputNames() { + return this.handler.outputNames; + } + get evalInputNames() { + if (this.hasEvalModel) { + return this.handler.evalInputNames; + } else { + throw new Error("This training session has no evalModel loaded."); + } + } + get evalOutputNames() { + if (this.hasEvalModel) { + return this.handler.evalOutputNames; + } else { + throw new Error("This training session has no evalModel loaded."); + } + } + static async create(trainingOptions, sessionOptions) { + const evalModel = trainingOptions.evalModel || ""; + const optimizerModel = trainingOptions.optimizerModel || ""; + const options = sessionOptions || {}; + const [backend, optionsWithValidatedEPs] = await (0, _backend_impl_js__WEBPACK_IMPORTED_MODULE_0__.resolveBackendAndExecutionProviders)(options); + if (backend.createTrainingSessionHandler) { + const handler = await backend.createTrainingSessionHandler(trainingOptions.checkpointState, trainingOptions.trainModel, evalModel, optimizerModel, optionsWithValidatedEPs); + return new TrainingSession(handler, !!trainingOptions.optimizerModel, !!trainingOptions.evalModel); + } else { + throw new Error(noBackendErrMsg); + } + } + /** + * Helper function for runTrainStep and future runStep methods that handles the type-narrowing conversion from + * the given parameters to SessionHandler.FetchesType and RunOptions. + * + * @param inputNames the feeds object is checked that they contain all input names in the provided list of input + * names. + * @param outputNames the fetches object is checked that their keys match up with valid names in the list of output + * names. + * @param feeds the required input + * @param arg1 narrowed & converted into the SessionHandler.FetchesType or RunOptions object + * @param arg2 optional RunOptions object. + * @returns + */ + typeNarrowingForRunStep(inputNames, outputNames, feeds, arg1, arg2) { + const fetches = {}; + let options = {}; + if (typeof feeds !== "object" || feeds === null || feeds instanceof _tensor_js__WEBPACK_IMPORTED_MODULE_1__.Tensor || Array.isArray(feeds)) { + throw new TypeError("'feeds' must be an object that use input names as keys and OnnxValue as corresponding values."); + } + let isFetchesEmpty = true; + if (typeof arg1 === "object") { + if (arg1 === null) { + throw new TypeError("Unexpected argument[1]: cannot be null."); + } + if (arg1 instanceof _tensor_js__WEBPACK_IMPORTED_MODULE_1__.Tensor) { + throw new TypeError("'fetches' cannot be a Tensor"); + } + if (Array.isArray(arg1)) { + if (arg1.length === 0) { + throw new TypeError("'fetches' cannot be an empty array."); + } + isFetchesEmpty = false; + for (const name of arg1) { + if (typeof name !== "string") { + throw new TypeError("'fetches' must be a string array or an object."); + } + if (outputNames.indexOf(name) === -1) { + throw new RangeError(`'fetches' contains invalid output name: ${name}.`); + } + fetches[name] = null; + } + if (typeof arg2 === "object" && arg2 !== null) { + options = arg2; + } else if (typeof arg2 !== "undefined") { + throw new TypeError("'options' must be an object."); + } + } else { + let isFetches = false; + const arg1Keys = Object.getOwnPropertyNames(arg1); + for (const name of outputNames) { + if (arg1Keys.indexOf(name) !== -1) { + const v = arg1[name]; + if (v === null || v instanceof _tensor_js__WEBPACK_IMPORTED_MODULE_1__.Tensor) { + isFetches = true; + isFetchesEmpty = false; + fetches[name] = v; + } + } + } + if (isFetches) { + if (typeof arg2 === "object" && arg2 !== null) { + options = arg2; + } else if (typeof arg2 !== "undefined") { + throw new TypeError("'options' must be an object."); + } + } else { + options = arg1; + } + } + } else if (typeof arg1 !== "undefined") { + throw new TypeError("Unexpected argument[1]: must be 'fetches' or 'options'."); + } + for (const name of inputNames) { + if (typeof feeds[name] === "undefined") { + throw new Error(`input '${name}' is missing in 'feeds'.`); + } + } + if (isFetchesEmpty) { + for (const name of outputNames) { + fetches[name] = null; + } + } + return [fetches, options]; + } + /** + * Helper method for runTrainStep and any other runStep methods. Takes the ReturnType result from the SessionHandler + * and changes it into a map of Tensors. + * + * @param results + * @returns + */ + convertHandlerReturnTypeToMapOfTensors(results) { + const returnValue = {}; + for (const key in results) { + if (Object.hasOwnProperty.call(results, key)) { + const result = results[key]; + if (result instanceof _tensor_js__WEBPACK_IMPORTED_MODULE_1__.Tensor) { + returnValue[key] = result; + } else { + returnValue[key] = new _tensor_js__WEBPACK_IMPORTED_MODULE_1__.Tensor(result.type, result.data, result.dims); + } + } + } + return returnValue; + } + async lazyResetGrad() { + await this.handler.lazyResetGrad(); + } + async runTrainStep(feeds, arg1, arg2) { + const [fetches, options] = this.typeNarrowingForRunStep(this.trainingInputNames, this.trainingOutputNames, feeds, arg1, arg2); + const results = await this.handler.runTrainStep(feeds, fetches, options); + return this.convertHandlerReturnTypeToMapOfTensors(results); + } + async runOptimizerStep(options) { + if (this.hasOptimizerModel) { + await this.handler.runOptimizerStep(options || {}); + } else { + throw new Error("This TrainingSession has no OptimizerModel loaded."); + } + } + async runEvalStep(feeds, arg1, arg2) { + if (this.hasEvalModel) { + const [fetches, options] = this.typeNarrowingForRunStep(this.evalInputNames, this.evalOutputNames, feeds, arg1, arg2); + const results = await this.handler.runEvalStep(feeds, fetches, options); + return this.convertHandlerReturnTypeToMapOfTensors(results); + } else { + throw new Error("This TrainingSession has no EvalModel loaded."); + } + } + async getParametersSize(trainableOnly = true) { + return this.handler.getParametersSize(trainableOnly); + } + async loadParametersBuffer(array, trainableOnly = true) { + const paramsSize = await this.getParametersSize(trainableOnly); + if (array.length !== 4 * paramsSize) { + throw new Error("Size of the buffer passed into loadParametersBuffer must match the number of parameters in the model. Please use getParametersSize method to check."); + } + return this.handler.loadParametersBuffer(array, trainableOnly); + } + async getContiguousParameters(trainableOnly = true) { + return this.handler.getContiguousParameters(trainableOnly); + } + async release() { + return this.handler.dispose(); + } + } + } + ), + /***/ + "./node_modules/onnxruntime-common/dist/esm/training-session.js": ( + /*!**********************************************************************!*\ + !*** ./node_modules/onnxruntime-common/dist/esm/training-session.js ***! + \**********************************************************************/ + /***/ + (__unused_webpack___webpack_module__, __webpack_exports__2, __webpack_require__2) => { + __webpack_require__2.r(__webpack_exports__2); + __webpack_require__2.d(__webpack_exports__2, { + /* harmony export */ + TrainingSession: () => ( + /* binding */ + TrainingSession + ) + /* harmony export */ + }); + var _training_session_impl_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__2( + /*! ./training-session-impl.js */ + "./node_modules/onnxruntime-common/dist/esm/training-session-impl.js" + ); + const TrainingSession = _training_session_impl_js__WEBPACK_IMPORTED_MODULE_0__.TrainingSession; + } + ), + /***/ + "./node_modules/onnxruntime-common/dist/esm/version.js": ( + /*!*************************************************************!*\ + !*** ./node_modules/onnxruntime-common/dist/esm/version.js ***! + \*************************************************************/ + /***/ + (__unused_webpack___webpack_module__, __webpack_exports__2, __webpack_require__2) => { + __webpack_require__2.r(__webpack_exports__2); + __webpack_require__2.d(__webpack_exports__2, { + /* harmony export */ + version: () => ( + /* binding */ + version + ) + /* harmony export */ + }); + const version = "1.19.2"; + } + ), + /***/ + "./node_modules/onnxruntime-web/dist/ort.webgpu.bundle.min.mjs": ( + /*!*********************************************************************!*\ + !*** ./node_modules/onnxruntime-web/dist/ort.webgpu.bundle.min.mjs ***! + \*********************************************************************/ + /***/ + (__unused_webpack___webpack_module__, __webpack_exports__2, __webpack_require__2) => { + __webpack_require__2.r(__webpack_exports__2); + __webpack_require__2.d(__webpack_exports__2, { + /* harmony export */ + InferenceSession: () => ( + /* binding */ + Ip + ), + /* harmony export */ + TRACE: () => ( + /* binding */ + Sr + ), + /* harmony export */ + TRACE_FUNC_BEGIN: () => ( + /* binding */ + Le + ), + /* harmony export */ + TRACE_FUNC_END: () => ( + /* binding */ + Ve + ), + /* harmony export */ + Tensor: () => ( + /* binding */ + De + ), + /* harmony export */ + TrainingSession: () => ( + /* binding */ + Ap + ), + /* harmony export */ + "default": () => ( + /* binding */ + Hx + ), + /* harmony export */ + env: () => ( + /* binding */ + _e + ), + /* harmony export */ + registerBackend: () => ( + /* binding */ + St + ) + /* harmony export */ + }); + var Un = Object.defineProperty; + var vp = Object.getOwnPropertyDescriptor; + var $p = Object.getOwnPropertyNames; + var xp = Object.prototype.hasOwnProperty; + var Vn = ((e) => typeof __require < "u" ? __require : typeof Proxy < "u" ? new Proxy(e, { get: (t, r) => (typeof __require < "u" ? __require : t)[r] }) : e)(function(e) { + if (typeof __require < "u") return __require.apply(this, arguments); + throw Error('Dynamic require of "' + e + '" is not supported'); + }); + var V = (e, t) => () => (e && (t = e(e = 0)), t); + var Gt = (e, t) => { + for (var r in t) Un(e, r, { get: t[r], enumerable: true }); + }, Sp = (e, t, r, n) => { + if (t && typeof t == "object" || typeof t == "function") for (let o of $p(t)) !xp.call(e, o) && o !== r && Un(e, o, { get: () => t[o], enumerable: !(n = vp(t, o)) || n.enumerable }); + return e; + }; + var br = (e) => Sp(Un({}, "__esModule", { value: true }), e); + var wr, xt, St, Tp, _r, vr = V(() => { + "use strict"; + wr = /* @__PURE__ */ new Map(), xt = [], St = (e, t, r) => { + if (t && typeof t.init == "function" && typeof t.createInferenceSessionHandler == "function") { + let n = wr.get(e); + if (n === void 0) wr.set(e, { backend: t, priority: r }); + else { + if (n.priority > r) return; + if (n.priority === r && n.backend !== t) throw new Error(`cannot register backend "${e}" using priority ${r}`); + } + if (r >= 0) { + let o = xt.indexOf(e); + o !== -1 && xt.splice(o, 1); + for (let i = 0; i < xt.length; i++) if (wr.get(xt[i]).priority <= r) { + xt.splice(i, 0, e); + return; + } + xt.push(e); + } + return; + } + throw new TypeError("not a valid backend"); + }, Tp = async (e) => { + let t = wr.get(e); + if (!t) return "backend not found."; + if (t.initialized) return t.backend; + if (t.aborted) return t.error; + { + let r = !!t.initPromise; + try { + return r || (t.initPromise = t.backend.init(e)), await t.initPromise, t.initialized = true, t.backend; + } catch (n) { + return r || (t.error = `${n}`, t.aborted = true), t.error; + } finally { + delete t.initPromise; + } + } + }, _r = async (e) => { + let t = e.executionProviders || [], r = t.map((l) => typeof l == "string" ? l : l.name), n = r.length === 0 ? xt : r, o, i = [], a = /* @__PURE__ */ new Set(); + for (let l of n) { + let c = await Tp(l); + typeof c == "string" ? i.push({ name: l, err: c }) : (o || (o = c), o === c && a.add(l)); + } + if (!o) throw new Error(`no available backend found. ERR: ${i.map((l) => `[${l.name}] ${l.err}`).join(", ")}`); + for (let { name: l, err: c } of i) r.includes(l) && console.warn(`removing requested execution provider "${l}" from session options because it is not available: ${c}`); + let d = t.filter((l) => a.has(typeof l == "string" ? l : l.name)); + return [o, new Proxy(e, { get: (l, c) => c === "executionProviders" ? d : Reflect.get(l, c) })]; + }; + }); + var Zi = V(() => { + "use strict"; + vr(); + }); + var Qi, Ji = V(() => { + "use strict"; + Qi = "1.20.0-dev.20241016-2b8fc5529b"; + }); + var ea, We, Nn = V(() => { + "use strict"; + Ji(); + ea = "warning", We = { wasm: {}, webgl: {}, webgpu: {}, versions: { common: Qi }, 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}`); + ea = e; + } + }, get logLevel() { + return ea; + } }; + Object.defineProperty(We, "logLevel", { enumerable: true }); + }); + var _e, ta = V(() => { + "use strict"; + Nn(); + _e = We; + }); + var ra, na, oa = V(() => { + "use strict"; + ra = (e, t) => { + let r = typeof document < "u" ? document.createElement("canvas") : new OffscreenCanvas(1, 1); + r.width = e.dims[3], r.height = e.dims[2]; + let n = r.getContext("2d"); + if (n != null) { + let o, i; + (t == null ? void 0 : t.tensorLayout) !== void 0 && t.tensorLayout === "NHWC" ? (o = e.dims[2], i = e.dims[3]) : (o = e.dims[3], i = e.dims[2]); + let a = (t == null ? void 0 : t.format) !== void 0 ? t.format : "RGB", d = t == null ? void 0 : t.norm, l, c; + d === void 0 || d.mean === void 0 ? l = [255, 255, 255, 255] : typeof d.mean == "number" ? l = [d.mean, d.mean, d.mean, d.mean] : (l = [d.mean[0], d.mean[1], d.mean[2], 0], d.mean[3] !== void 0 && (l[3] = d.mean[3])), d === void 0 || d.bias === void 0 ? c = [0, 0, 0, 0] : typeof d.bias == "number" ? c = [d.bias, d.bias, d.bias, d.bias] : (c = [d.bias[0], d.bias[1], d.bias[2], 0], d.bias[3] !== void 0 && (c[3] = d.bias[3])); + let m = i * o, u = 0, h = m, w = m * 2, g = -1; + a === "RGBA" ? (u = 0, h = m, w = m * 2, g = m * 3) : a === "RGB" ? (u = 0, h = m, w = m * 2) : a === "RBG" && (u = 0, w = m, h = m * 2); + for (let y = 0; y < i; y++) for (let S = 0; S < o; S++) { + let $ = (e.data[u++] - c[0]) * l[0], v = (e.data[h++] - c[1]) * l[1], x = (e.data[w++] - c[2]) * l[2], T = g === -1 ? 255 : (e.data[g++] - c[3]) * l[3]; + n.fillStyle = "rgba(" + $ + "," + v + "," + x + "," + T + ")", n.fillRect(S, y, 1, 1); + } + if ("toDataURL" in r) return r.toDataURL(); + throw new Error("toDataURL is not supported"); + } else throw new Error("Can not access image data"); + }, na = (e, t) => { + let r = typeof document < "u" ? document.createElement("canvas").getContext("2d") : new OffscreenCanvas(1, 1).getContext("2d"), n; + if (r != null) { + let o, i, a; + (t == null ? void 0 : t.tensorLayout) !== void 0 && t.tensorLayout === "NHWC" ? (o = e.dims[2], i = e.dims[1], a = e.dims[3]) : (o = e.dims[3], i = e.dims[2], a = e.dims[1]); + let d = t !== void 0 && t.format !== void 0 ? t.format : "RGB", l = t == null ? void 0 : t.norm, c, m; + l === void 0 || l.mean === void 0 ? c = [255, 255, 255, 255] : typeof l.mean == "number" ? c = [l.mean, l.mean, l.mean, l.mean] : (c = [l.mean[0], l.mean[1], l.mean[2], 255], l.mean[3] !== void 0 && (c[3] = l.mean[3])), l === void 0 || l.bias === void 0 ? m = [0, 0, 0, 0] : typeof l.bias == "number" ? m = [l.bias, l.bias, l.bias, l.bias] : (m = [l.bias[0], l.bias[1], l.bias[2], 0], l.bias[3] !== void 0 && (m[3] = l.bias[3])); + let u = i * o; + if (t !== void 0 && (t.format !== void 0 && a === 4 && t.format !== "RGBA" || a === 3 && t.format !== "RGB" && t.format !== "BGR")) throw new Error("Tensor format doesn't match input tensor dims"); + let h = 4, w = 0, g = 1, y = 2, S = 3, $ = 0, v = u, x = u * 2, T = -1; + d === "RGBA" ? ($ = 0, v = u, x = u * 2, T = u * 3) : d === "RGB" ? ($ = 0, v = u, x = u * 2) : d === "RBG" && ($ = 0, x = u, v = u * 2), n = r.createImageData(o, i); + for (let C = 0; C < i * o; w += h, g += h, y += h, S += h, C++) n.data[w] = (e.data[$++] - m[0]) * c[0], n.data[g] = (e.data[v++] - m[1]) * c[1], n.data[y] = (e.data[x++] - m[2]) * c[2], n.data[S] = T === -1 ? 255 : (e.data[T++] - m[3]) * c[3]; + } else throw new Error("Can not access image data"); + return n; + }; + }); + var Wn, ia, aa, sa, ua, da, la = V(() => { + "use strict"; + $r(); + 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: r, width: n } = t, o = t.norm ?? { mean: 255, bias: 0 }, i, a; + typeof o.mean == "number" ? i = [o.mean, o.mean, o.mean, o.mean] : i = [o.mean[0], o.mean[1], o.mean[2], o.mean[3] ?? 255], typeof o.bias == "number" ? a = [o.bias, o.bias, o.bias, o.bias] : a = [o.bias[0], o.bias[1], o.bias[2], o.bias[3] ?? 0]; + let d = t.format !== void 0 ? t.format : "RGBA", l = t.tensorFormat !== void 0 && t.tensorFormat !== void 0 ? t.tensorFormat : "RGB", c = r * n, m = l === "RGBA" ? new Float32Array(c * 4) : new Float32Array(c * 3), u = 4, h = 0, w = 1, g = 2, y = 3, S = 0, $ = c, v = c * 2, x = -1; + d === "RGB" && (u = 3, h = 0, w = 1, g = 2, y = -1), l === "RGBA" ? x = c * 3 : l === "RBG" ? (S = 0, v = c, $ = c * 2) : l === "BGR" && (v = 0, $ = c, S = c * 2); + for (let C = 0; C < c; C++, h += u, g += u, w += u, y += u) m[S++] = (e[h] + a[0]) / i[0], m[$++] = (e[w] + a[1]) / i[1], m[v++] = (e[g] + a[2]) / i[2], x !== -1 && y !== -1 && (m[x++] = (e[y] + a[3]) / i[3]); + return l === "RGBA" ? new Be("float32", m, [1, 4, r, n]) : new Be("float32", m, [1, 3, r, n]); + }, ia = async (e, t) => { + let r = typeof HTMLImageElement < "u" && e instanceof HTMLImageElement, n = typeof ImageData < "u" && e instanceof ImageData, o = typeof ImageBitmap < "u" && e instanceof ImageBitmap, i = typeof e == "string", a, d = t ?? {}, l = () => { + if (typeof document < "u") return document.createElement("canvas"); + if (typeof OffscreenCanvas < "u") return new OffscreenCanvas(1, 1); + throw new Error("Canvas is not supported"); + }, c = (m) => typeof HTMLCanvasElement < "u" && m instanceof HTMLCanvasElement || m instanceof OffscreenCanvas ? m.getContext("2d") : null; + if (r) { + let m = l(); + m.width = e.width, m.height = e.height; + let u = c(m); + if (u != null) { + let h = e.height, w = e.width; + if (t !== void 0 && t.resizedHeight !== void 0 && t.resizedWidth !== void 0 && (h = t.resizedHeight, w = t.resizedWidth), t !== void 0) { + if (d = t, t.tensorFormat !== void 0) throw new Error("Image input config format must be RGBA for HTMLImageElement"); + d.tensorFormat = "RGBA", d.height = h, d.width = w; + } else d.tensorFormat = "RGBA", d.height = h, d.width = w; + u.drawImage(e, 0, 0), a = u.getImageData(0, 0, w, h).data; + } else throw new Error("Can not access image data"); + } else if (n) { + let m, u; + if (t !== void 0 && t.resizedWidth !== void 0 && t.resizedHeight !== void 0 ? (m = t.resizedHeight, u = t.resizedWidth) : (m = e.height, u = e.width), t !== void 0 && (d = t), d.format = "RGBA", d.height = m, d.width = u, t !== void 0) { + let h = l(); + h.width = u, h.height = m; + let w = c(h); + if (w != null) w.putImageData(e, 0, 0), a = w.getImageData(0, 0, u, m).data; + else throw new Error("Can not access image data"); + } else a = e.data; + } else if (o) { + if (t === void 0) throw new Error("Please provide image config with format for Imagebitmap"); + let m = l(); + m.width = e.width, m.height = e.height; + let u = c(m); + if (u != null) { + let h = e.height, w = e.width; + return u.drawImage(e, 0, 0, w, h), a = u.getImageData(0, 0, w, h).data, d.height = h, d.width = w, Wn(a, d); + } else throw new Error("Can not access image data"); + } else { + if (i) return new Promise((m, u) => { + let h = l(), w = c(h); + if (!e || !w) return u(); + let g = new Image(); + g.crossOrigin = "Anonymous", g.src = e, g.onload = () => { + h.width = g.width, h.height = g.height, w.drawImage(g, 0, 0, h.width, h.height); + let y = w.getImageData(0, 0, h.width, h.height); + d.height = h.height, d.width = h.width, m(Wn(y.data, d)); + }; + }); + throw new Error("Input data provided is not supported - aborted tensor creation"); + } + if (a !== void 0) return Wn(a, d); + throw new Error("Input data provided is not supported - aborted tensor creation"); + }, aa = (e, t) => { + let { width: r, height: n, download: o, dispose: i } = t, a = [1, n, r, 4]; + return new Be({ location: "texture", type: "float32", texture: e, dims: a, download: o, dispose: i }); + }, sa = (e, t) => { + let { dataType: r, dims: n, download: o, dispose: i } = t; + return new Be({ location: "gpu-buffer", type: r ?? "float32", gpuBuffer: e, dims: n, download: o, dispose: i }); + }, ua = (e, t) => { + let { dataType: r, dims: n, download: o, dispose: i } = t; + return new Be({ location: "ml-tensor", type: r ?? "float32", mlTensor: e, dims: n, download: o, dispose: i }); + }, da = (e, t, r) => new Be({ location: "cpu-pinned", type: e, data: t, dims: r ?? [t.length] }); + }); + var Tt, Ft, ca, pa, ma = V(() => { + "use strict"; + Tt = /* @__PURE__ */ new Map([["float32", Float32Array], ["uint8", Uint8Array], ["int8", Int8Array], ["uint16", Uint16Array], ["int16", Int16Array], ["int32", Int32Array], ["bool", Uint8Array], ["float64", Float64Array], ["uint32", Uint32Array], ["int4", Uint8Array], ["uint4", Uint8Array]]), Ft = /* @__PURE__ */ new Map([[Float32Array, "float32"], [Uint8Array, "uint8"], [Int8Array, "int8"], [Uint16Array, "uint16"], [Int16Array, "int16"], [Int32Array, "int32"], [Float64Array, "float64"], [Uint32Array, "uint32"]]), ca = false, pa = () => { + if (!ca) { + ca = true; + let e = typeof BigInt64Array < "u" && BigInt64Array.from, t = typeof BigUint64Array < "u" && BigUint64Array.from, r = typeof Float16Array < "u" && Float16Array.from; + e && (Tt.set("int64", BigInt64Array), Ft.set(BigInt64Array, "int64")), t && (Tt.set("uint64", BigUint64Array), Ft.set(BigUint64Array, "uint64")), r ? (Tt.set("float16", Float16Array), Ft.set(Float16Array, "float16")) : Tt.set("float16", Uint16Array); + } + }; + }); + var fa, ha, ga = V(() => { + "use strict"; + $r(); + fa = (e) => { + let t = 1; + for (let r = 0; r < e.length; r++) { + let n = e[r]; + if (typeof n != "number" || !Number.isSafeInteger(n)) throw new TypeError(`dims[${r}] must be an integer, got: ${n}`); + if (n < 0) throw new RangeError(`dims[${r}] must be a non-negative integer, got: ${n}`); + t *= n; + } + return t; + }, ha = (e, t) => { + switch (e.location) { + case "cpu": + return new Be(e.type, e.data, t); + case "cpu-pinned": + return new Be({ location: "cpu-pinned", data: e.data, type: e.type, dims: t }); + case "texture": + return new Be({ location: "texture", texture: e.texture, type: e.type, dims: t }); + case "gpu-buffer": + return new Be({ location: "gpu-buffer", gpuBuffer: e.gpuBuffer, type: e.type, dims: t }); + case "ml-tensor": + return new Be({ location: "ml-tensor", mlTensor: e.mlTensor, type: e.type, dims: t }); + default: + throw new Error(`tensorReshape: tensor location ${e.location} is not supported`); + } + }; + }); + var Be, $r = V(() => { + "use strict"; + oa(); + la(); + ma(); + ga(); + Be = class { + constructor(t, r, n) { + pa(); + let o, i; + if (typeof t == "object" && "location" in t) switch (this.dataLocation = t.location, o = t.type, i = t.dims, t.location) { + case "cpu-pinned": { + let d = Tt.get(o); + if (!d) throw new TypeError(`unsupported type "${o}" to create tensor from pinned buffer`); + if (!(t.data instanceof d)) throw new TypeError(`buffer should be of type ${d.name}`); + this.cpuData = t.data; + break; + } + case "texture": { + if (o !== "float32") throw new TypeError(`unsupported type "${o}" to create tensor from texture`); + this.gpuTextureData = t.texture, this.downloader = t.download, this.disposer = t.dispose; + break; + } + case "gpu-buffer": { + if (o !== "float32" && o !== "float16" && o !== "int32" && o !== "int64" && o !== "uint32" && o !== "uint8" && o !== "bool" && o !== "uint4" && o !== "int4") throw new TypeError(`unsupported type "${o}" to create tensor from gpu buffer`); + this.gpuBufferData = t.gpuBuffer, this.downloader = t.download, this.disposer = t.dispose; + break; + } + case "ml-tensor": { + if (o !== "float32" && o !== "float16" && o !== "int32" && o !== "int64" && o !== "uint32" && o !== "uint64" && o !== "int8" && o !== "uint8" && o !== "bool") throw new TypeError(`unsupported type "${o}" to create tensor from MLTensor`); + this.mlTensorData = t.mlTensor, this.downloader = t.download, this.disposer = t.dispose; + break; + } + default: + throw new Error(`Tensor constructor: unsupported location '${this.dataLocation}'`); + } + else { + let d, l; + if (typeof t == "string") if (o = t, l = n, t === "string") { + if (!Array.isArray(r)) throw new TypeError("A string tensor's data must be a string array."); + d = r; + } else { + let c = Tt.get(t); + if (c === void 0) throw new TypeError(`Unsupported tensor type: ${t}.`); + if (Array.isArray(r)) { + if (t === "float16" && c === Uint16Array || t === "uint4" || t === "int4") throw new TypeError(`Creating a ${t} tensor from number array is not supported. Please use ${c.name} as data.`); + t === "uint64" || t === "int64" ? d = c.from(r, BigInt) : d = c.from(r); + } else if (r instanceof c) d = r; + else if (r instanceof Uint8ClampedArray) if (t === "uint8") d = Uint8Array.from(r); + else throw new TypeError("A Uint8ClampedArray tensor's data must be type of uint8"); + else throw new TypeError(`A ${o} tensor's data must be type of ${c}`); + } + else if (l = r, Array.isArray(t)) { + if (t.length === 0) throw new TypeError("Tensor type cannot be inferred from an empty array."); + let c = typeof t[0]; + if (c === "string") o = "string", d = t; + else if (c === "boolean") o = "bool", d = Uint8Array.from(t); + else throw new TypeError(`Invalid element type of data array: ${c}.`); + } else if (t instanceof Uint8ClampedArray) o = "uint8", d = Uint8Array.from(t); + else { + let c = Ft.get(t.constructor); + if (c === void 0) throw new TypeError(`Unsupported type for tensor data: ${t.constructor}.`); + o = c, d = t; + } + if (l === void 0) l = [d.length]; + else if (!Array.isArray(l)) throw new TypeError("A tensor's dims must be a number array"); + i = l, this.cpuData = d, this.dataLocation = "cpu"; + } + let a = fa(i); + if (this.cpuData && a !== this.cpuData.length && !((o === "uint4" || o === "int4") && Math.ceil(a / 2) === this.cpuData.length)) throw new Error(`Tensor's size(${a}) does not match data length(${this.cpuData.length}).`); + this.type = o, this.dims = i, this.size = a; + } + static async fromImage(t, r) { + return ia(t, r); + } + static fromTexture(t, r) { + return aa(t, r); + } + static fromGpuBuffer(t, r) { + return sa(t, r); + } + static fromMLTensor(t, r) { + return ua(t, r); + } + static fromPinnedBuffer(t, r, n) { + return da(t, r, n); + } + toDataURL(t) { + return ra(this, t); + } + toImageData(t) { + return na(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 = true; + let r = await this.downloader(); + return this.downloader = void 0, this.dataLocation = "cpu", this.cpuData = r, t && this.disposer && (this.disposer(), this.disposer = void 0), r; + } finally { + this.isDownloading = false; + } + } + default: + throw new Error(`cannot get data from location: ${this.dataLocation}`); + } + } + dispose() { + if (this.isDownloading) throw new Error("The current tensor is being downloaded."); + 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 ha(this, t); + } + }; + }); + var De, xr = V(() => { + "use strict"; + $r(); + De = Be; + }); + var Sr, ya, Le, Ve, Ln = V(() => { + "use strict"; + Nn(); + Sr = (e, t) => { + (typeof We.trace > "u" ? !We.wasm.trace : !We.trace) || console.timeStamp(`${e}::ORT::${t}`); + }, ya = (e, t) => { + var _a2; + let r = ((_a2 = new Error().stack) == null ? void 0 : _a2.split(/\r\n|\r|\n/g)) || [], n = false; + for (let o = 0; o < r.length; o++) { + if (n && !r[o].includes("TRACE_FUNC")) { + let i = `FUNC_${e}::${r[o].trim().split(" ")[1]}`; + t && (i += `::${t}`), Sr("CPU", i); + return; + } + r[o].includes("TRACE_FUNC") && (n = true); + } + }, Le = (e) => { + (typeof We.trace > "u" ? !We.wasm.trace : !We.trace) || ya("BEGIN", e); + }, Ve = (e) => { + (typeof We.trace > "u" ? !We.wasm.trace : !We.trace) || ya("END", e); + }; + }); + var Tr, ba = V(() => { + "use strict"; + vr(); + xr(); + Ln(); + Tr = class e { + constructor(t) { + this.handler = t; + } + async run(t, r, n) { + Le(); + let o = {}, i = {}; + if (typeof t != "object" || t === null || t instanceof De || Array.isArray(t)) throw new TypeError("'feeds' must be an object that use input names as keys and OnnxValue as corresponding values."); + let a = true; + if (typeof r == "object") { + if (r === null) throw new TypeError("Unexpected argument[1]: cannot be null."); + if (r instanceof De) throw new TypeError("'fetches' cannot be a Tensor"); + if (Array.isArray(r)) { + if (r.length === 0) throw new TypeError("'fetches' cannot be an empty array."); + a = false; + for (let c of r) { + if (typeof c != "string") throw new TypeError("'fetches' must be a string array or an object."); + if (this.outputNames.indexOf(c) === -1) throw new RangeError(`'fetches' contains invalid output name: ${c}.`); + o[c] = null; + } + if (typeof n == "object" && n !== null) i = n; + else if (typeof n < "u") throw new TypeError("'options' must be an object."); + } else { + let c = false, m = Object.getOwnPropertyNames(r); + for (let u of this.outputNames) if (m.indexOf(u) !== -1) { + let h = r[u]; + (h === null || h instanceof De) && (c = true, a = false, o[u] = h); + } + if (c) { + if (typeof n == "object" && n !== null) i = n; + else if (typeof n < "u") throw new TypeError("'options' must be an object."); + } else i = r; + } + } else if (typeof r < "u") throw new TypeError("Unexpected argument[1]: must be 'fetches' or 'options'."); + for (let c of this.inputNames) if (typeof t[c] > "u") throw new Error(`input '${c}' is missing in 'feeds'.`); + if (a) for (let c of this.outputNames) o[c] = null; + let d = await this.handler.run(t, o, i), l = {}; + for (let c in d) if (Object.hasOwnProperty.call(d, c)) { + let m = d[c]; + m instanceof De ? l[c] = m : l[c] = new De(m.type, m.data, m.dims); + } + return Ve(), l; + } + async release() { + return this.handler.dispose(); + } + static async create(t, r, n, o) { + Le(); + let i, a = {}; + if (typeof t == "string") { + if (i = t, typeof r == "object" && r !== null) a = r; + else if (typeof r < "u") throw new TypeError("'options' must be an object."); + } else if (t instanceof Uint8Array) { + if (i = t, typeof r == "object" && r !== null) a = r; + else if (typeof r < "u") throw new TypeError("'options' must be an object."); + } else if (t instanceof ArrayBuffer || typeof SharedArrayBuffer < "u" && t instanceof SharedArrayBuffer) { + let m = t, u = 0, h = t.byteLength; + if (typeof r == "object" && r !== null) a = r; + else if (typeof r == "number") { + if (u = r, !Number.isSafeInteger(u)) throw new RangeError("'byteOffset' must be an integer."); + if (u < 0 || u >= m.byteLength) throw new RangeError(`'byteOffset' is out of range [0, ${m.byteLength}).`); + if (h = t.byteLength - u, typeof n == "number") { + if (h = n, !Number.isSafeInteger(h)) throw new RangeError("'byteLength' must be an integer."); + if (h <= 0 || u + h > m.byteLength) throw new RangeError(`'byteLength' is out of range (0, ${m.byteLength - u}].`); + if (typeof o == "object" && o !== null) a = o; + else if (typeof o < "u") throw new TypeError("'options' must be an object."); + } else if (typeof n < "u") throw new TypeError("'byteLength' must be a number."); + } else if (typeof r < "u") throw new TypeError("'options' must be an object."); + i = new Uint8Array(m, u, h); + } else throw new TypeError("Unexpected argument[0]: must be 'path' or 'buffer'."); + let [d, l] = await _r(a), c = await d.createInferenceSessionHandler(i, l); + return Ve(), new e(c); + } + startProfiling() { + this.handler.startProfiling(); + } + endProfiling() { + this.handler.endProfiling(); + } + get inputNames() { + return this.handler.inputNames; + } + get outputNames() { + return this.handler.outputNames; + } + }; + }); + var Ip, wa = V(() => { + "use strict"; + ba(); + Ip = Tr; + }); + var _a = V(() => { + "use strict"; + }); + var va = V(() => { + "use strict"; + }); + var $a = V(() => { + "use strict"; + }); + var xa = V(() => { + "use strict"; + }); + var Cp, Ir, Sa = V(() => { + "use strict"; + vr(); + xr(); + Cp = "Training backend could not be resolved. Make sure you're using the correct configuration & WebAssembly files.", Ir = class e { + constructor(t, r, n) { + this.handler = t, this.hasOptimizerModel = r, this.hasEvalModel = n; + } + get trainingInputNames() { + return this.handler.inputNames; + } + get trainingOutputNames() { + return this.handler.outputNames; + } + get evalInputNames() { + if (this.hasEvalModel) return this.handler.evalInputNames; + throw new Error("This training session has no evalModel loaded."); + } + get evalOutputNames() { + if (this.hasEvalModel) return this.handler.evalOutputNames; + throw new Error("This training session has no evalModel loaded."); + } + static async create(t, r) { + let n = t.evalModel || "", o = t.optimizerModel || "", i = r || {}, [a, d] = await _r(i); + if (a.createTrainingSessionHandler) { + let l = await a.createTrainingSessionHandler(t.checkpointState, t.trainModel, n, o, d); + return new e(l, !!t.optimizerModel, !!t.evalModel); + } else throw new Error(Cp); + } + typeNarrowingForRunStep(t, r, n, o, i) { + let a = {}, d = {}; + if (typeof n != "object" || n === null || n instanceof De || Array.isArray(n)) throw new TypeError("'feeds' must be an object that use input names as keys and OnnxValue as corresponding values."); + let l = true; + if (typeof o == "object") { + if (o === null) throw new TypeError("Unexpected argument[1]: cannot be null."); + if (o instanceof De) throw new TypeError("'fetches' cannot be a Tensor"); + if (Array.isArray(o)) { + if (o.length === 0) throw new TypeError("'fetches' cannot be an empty array."); + l = false; + for (let c of o) { + if (typeof c != "string") throw new TypeError("'fetches' must be a string array or an object."); + if (r.indexOf(c) === -1) throw new RangeError(`'fetches' contains invalid output name: ${c}.`); + a[c] = null; + } + if (typeof i == "object" && i !== null) d = i; + else if (typeof i < "u") throw new TypeError("'options' must be an object."); + } else { + let c = false, m = Object.getOwnPropertyNames(o); + for (let u of r) if (m.indexOf(u) !== -1) { + let h = o[u]; + (h === null || h instanceof De) && (c = true, l = false, a[u] = h); + } + if (c) { + if (typeof i == "object" && i !== null) d = i; + else if (typeof i < "u") throw new TypeError("'options' must be an object."); + } else d = o; + } + } else if (typeof o < "u") throw new TypeError("Unexpected argument[1]: must be 'fetches' or 'options'."); + for (let c of t) if (typeof n[c] > "u") throw new Error(`input '${c}' is missing in 'feeds'.`); + if (l) for (let c of r) a[c] = null; + return [a, d]; + } + convertHandlerReturnTypeToMapOfTensors(t) { + let r = {}; + for (let n in t) if (Object.hasOwnProperty.call(t, n)) { + let o = t[n]; + o instanceof De ? r[n] = o : r[n] = new De(o.type, o.data, o.dims); + } + return r; + } + async lazyResetGrad() { + await this.handler.lazyResetGrad(); + } + async runTrainStep(t, r, n) { + let [o, i] = this.typeNarrowingForRunStep(this.trainingInputNames, this.trainingOutputNames, t, r, n), a = await this.handler.runTrainStep(t, o, i); + return this.convertHandlerReturnTypeToMapOfTensors(a); + } + async runOptimizerStep(t) { + if (this.hasOptimizerModel) await this.handler.runOptimizerStep(t || {}); + else throw new Error("This TrainingSession has no OptimizerModel loaded."); + } + async runEvalStep(t, r, n) { + if (this.hasEvalModel) { + let [o, i] = this.typeNarrowingForRunStep(this.evalInputNames, this.evalOutputNames, t, r, n), a = await this.handler.runEvalStep(t, o, i); + return this.convertHandlerReturnTypeToMapOfTensors(a); + } else throw new Error("This TrainingSession has no EvalModel loaded."); + } + async getParametersSize(t = true) { + return this.handler.getParametersSize(t); + } + async loadParametersBuffer(t, r = true) { + let n = await this.getParametersSize(r); + if (t.length !== 4 * n) throw new Error("Size of the buffer passed into loadParametersBuffer must match the number of parameters in the model. Please use getParametersSize method to check."); + return this.handler.loadParametersBuffer(t, r); + } + async getContiguousParameters(t = true) { + return this.handler.getContiguousParameters(t); + } + async release() { + return this.handler.dispose(); + } + }; + }); + var Ap, Ta = V(() => { + "use strict"; + Sa(); + Ap = Ir; + }); + var Hn = {}; + Gt(Hn, { InferenceSession: () => Ip, TRACE: () => Sr, TRACE_FUNC_BEGIN: () => Le, TRACE_FUNC_END: () => Ve, Tensor: () => De, TrainingSession: () => Ap, env: () => _e, registerBackend: () => St }); + var Ke = V(() => { + "use strict"; + Zi(); + ta(); + wa(); + xr(); + _a(); + va(); + Ln(); + $a(); + xa(); + Ta(); + }); + var Cr = V(() => { + "use strict"; + }); + var ka = {}; + Gt(ka, { default: () => kp }); + var Ca, Aa, kp, Ea = V(() => { + "use strict"; + var _a2; + Gn(); + gt(); + qt(); + Ca = "ort-wasm-proxy-worker", Aa = ((_a2 = globalThis.self) == null ? void 0 : _a2.name) === Ca; + Aa && (self.onmessage = (e) => { + let { type: t, in: r } = e.data; + try { + switch (t) { + case "init-wasm": + Ar(r.wasm).then(() => { + kr(r).then(() => { + postMessage({ type: t }); + }, (n) => { + postMessage({ type: t, err: n }); + }); + }, (n) => { + postMessage({ type: t, err: n }); + }); + break; + case "init-ep": { + let { epName: n, env: o } = r; + Er(o, n).then(() => { + postMessage({ type: t }); + }, (i) => { + postMessage({ type: t, err: i }); + }); + break; + } + case "copy-from": { + let { buffer: n } = r, o = jt(n); + postMessage({ type: t, out: o }); + break; + } + case "create": { + let { model: n, options: o } = r; + Pr(n, o).then((i) => { + postMessage({ type: t, out: i }); + }, (i) => { + postMessage({ type: t, err: i }); + }); + break; + } + case "release": + zr(r), postMessage({ type: t }); + break; + case "run": { + let { sessionId: n, inputIndices: o, inputs: i, outputIndices: a, options: d } = r; + Or(n, o, i, a, new Array(a.length).fill(null), d).then((l) => { + l.some((c) => c[3] !== "cpu") ? postMessage({ type: t, err: "Proxy does not support non-cpu tensor location." }) : postMessage({ type: t, out: l }, Dr([...i, ...l])); + }, (l) => { + postMessage({ type: t, err: l }); + }); + break; + } + case "end-profiling": + Br(r), postMessage({ type: t }); + break; + default: + } + } catch (n) { + postMessage({ type: t, err: n }); + } + }); + kp = Aa ? null : (e) => new Worker(e ?? Ut, { type: "module", name: Ca }); + }); + var za = {}; + Gt(za, { default: () => Ep }); + var Fn, Pa, Ep, Oa = V(() => { + "use strict"; + var _a2; + Pa = (Fn = META_URL, async function(e = {}) { + function t() { + return de.buffer != ce.buffer && Ce(), ce; + } + function r() { + return de.buffer != ce.buffer && Ce(), q; + } + function n() { + return de.buffer != ce.buffer && Ce(), ue; + } + function o() { + return de.buffer != ce.buffer && Ce(), re; + } + function i() { + return de.buffer != ce.buffer && Ce(), ne; + } + function a() { + return de.buffer != ce.buffer && Ce(), oe; + } + function d() { + return de.buffer != ce.buffer && Ce(), U; + } + function l() { + return de.buffer != ce.buffer && Ce(), Re; + } + var c, m, u = Object.assign({}, e), h = new Promise((s, p) => { + c = s, m = p; + }), w = typeof window == "object", g = typeof importScripts == "function", y = g && self.name == "em-pthread"; + u.mountExternalData = (s, p) => { + s.startsWith("./") && (s = s.substring(2)), (u.Fb || (u.Fb = /* @__PURE__ */ new Map())).set(s, p); + }, u.unmountExternalData = () => { + delete u.Fb; + }; + var S = globalThis.SharedArrayBuffer ?? new WebAssembly.Memory({ initial: 0, maximum: 0, shared: true }).buffer.constructor; + let $ = () => { + let s = (f, b, _) => (...I) => { + let O = et, D = b == null ? void 0 : b(); + I = f(...I); + let L = b == null ? void 0 : b(); + return D !== L && (f = L, _(D), b = _ = null), et != O ? new Promise((H, X) => { + En = { resolve: H, reject: X }; + }) : I; + }, p = (f) => async (...b) => { + var _a3; + try { + if (u.Eb) throw Error("Session already started"); + let _ = u.Eb = { fc: b[0], errors: [] }, I = await f(...b); + if (u.Eb !== _) throw Error("Session mismatch"); + (_a3 = u.Gb) == null ? void 0 : _a3.flush(); + let O = _.errors; + if (0 < O.length) { + let D = await Promise.all(O); + if (D = D.filter((L) => L), 0 < D.length) throw Error(D.join(` + `)); + } + return I; + } finally { + u.Eb = null; + } + }; + u._OrtCreateSession = s(u._OrtCreateSession, () => u._OrtCreateSession, (f) => u._OrtCreateSession = f), u._OrtRun = p(s(u._OrtRun, () => u._OrtRun, (f) => u._OrtRun = f)), u._OrtRunWithBinding = p(s(u._OrtRunWithBinding, () => u._OrtRunWithBinding, (f) => u._OrtRunWithBinding = f)), u._OrtBindInput = s(u._OrtBindInput, () => u._OrtBindInput, (f) => u._OrtBindInput = f), $ = void 0; + }; + u.jsepInit = (s, p) => { + if ($ == null ? void 0 : $(), s === "webgpu") { + [u.Gb, u.Ub, u.Yb, u.Nb, u.Xb, u.jb, u.Zb, u.bc, u.Vb, u.Wb, u.$b] = p; + let f = u.Gb; + u.jsepRegisterBuffer = (b, _, I, O) => f.registerBuffer(b, _, I, O), u.jsepGetBuffer = (b) => f.getBuffer(b), u.jsepCreateDownloader = (b, _, I) => f.createDownloader(b, _, I), u.jsepOnReleaseSession = (b) => { + f.onReleaseSession(b); + }, u.jsepOnRunStart = (b) => f.onRunStart(b), u.cc = (b, _) => { + f.upload(b, _); + }; + } else if (s === "webnn") { + [u.Gb, u.ac, u.Ob, u.jsepEnsureTensor, u.dc, u.jsepDownloadTensor] = p, u.jsepReleaseTensorId = u.Ob; + let f = u.Gb; + u.jsepOnRunStart = (b) => f.onRunStart(b), u.jsepRegisterMLContext = (b, _) => { + f.registerMLContext(b, _); + }, u.jsepOnReleaseSession = (b) => { + f.onReleaseSession(b); + }, u.jsepCreateMLTensorDownloader = (b, _) => f.createMLTensorDownloader(b, _), u.jsepRegisterMLTensor = (b, _, I) => f.registerMLTensor(b, _, I); + } + }; + var v, x, T = Object.assign({}, u), C = "./this.program", A = (s, p) => { + throw p; + }, P = ""; + (w || g) && (g ? P = self.location.href : typeof document < "u" && document.currentScript && (P = document.currentScript.src), Fn && (P = Fn), P = P.startsWith("blob:") ? "" : P.substr(0, P.replace(/[?#].*/, "").lastIndexOf("/") + 1), g && (x = (s) => { + var p = new XMLHttpRequest(); + return p.open("GET", s, false), p.responseType = "arraybuffer", p.send(null), new Uint8Array(p.response); + }), v = (s, p, f) => { + var b = new XMLHttpRequest(); + b.open("GET", s, true), b.responseType = "arraybuffer", b.onload = () => { + b.status == 200 || b.status == 0 && b.response ? p(b.response) : f(); + }, b.onerror = f, b.send(null); + }); + var B, N = console.log.bind(console), W = console.error.bind(console), K = N, Z = W; + if (Object.assign(u, T), T = null, y) { + let s = function(p) { + try { + var f = p.data, b = f.cmd; + if (b === "load") { + let _ = []; + self.onmessage = (I) => _.push(I), self.startWorker = () => { + postMessage({ cmd: "loaded" }); + for (let I of _) s(I); + self.onmessage = s; + }; + for (let I of f.handlers) u[I] && !u[I].proxy || (u[I] = (...O) => { + postMessage({ Mb: "callHandler", oc: I, args: O }); + }, I == "print" && (K = u[I]), I == "printErr" && (Z = u[I])); + de = f.wasmMemory, Ce(), ee(f.wasmModule); + } else if (b === "run") { + Bn(f.pthread_ptr, 0, 0, 1, 0, 0), Cn(f.pthread_ptr), ic(), Lo(), se || (Vi(), se = true); + try { + ac(f.start_routine, f.arg); + } catch (_) { + if (_ != "unwind") throw _; + } + } else b === "cancel" ? Rt() && gr(-1) : f.target !== "setimmediate" && (b === "checkMailbox" ? se && sr() : b && (Z(`worker: received unknown command ${b}`), Z(f))); + } catch (_) { + throw Ni(), _; + } + }; + var jh = s, ee, se = false; + Z = function(...p) { + p = p.join(" "), console.error(p); + }, self.alert = function(...p) { + postMessage({ Mb: "alert", text: p.join(" "), qc: Rt() }); + }, u.instantiateWasm = (p, f) => new Promise((b) => { + ee = (_) => { + _ = new WebAssembly.Instance(_, Ro()), f(_), b(); + }; + }), self.onunhandledrejection = (p) => { + throw p.reason || p; + }, self.onmessage = s; + } + u.wasmBinary && (B = u.wasmBinary); + var de, Y, le, ce, q, ue, re, ne, oe, U, G, ye, Re, $e = false; + function Ce() { + var s = de.buffer; + u.HEAP8 = ce = new Int8Array(s), u.HEAP16 = ue = new Int16Array(s), u.HEAPU8 = q = new Uint8Array(s), u.HEAPU16 = re = new Uint16Array(s), u.HEAP32 = ne = new Int32Array(s), u.HEAPU32 = oe = new Uint32Array(s), u.HEAPF32 = U = new Float32Array(s), u.HEAPF64 = Re = new Float64Array(s), u.HEAP64 = G = new BigInt64Array(s), u.HEAPU64 = ye = new BigUint64Array(s); + } + if (!y) { + if (!((de = new WebAssembly.Memory({ initial: 256, maximum: 65536, shared: true })).buffer instanceof S)) throw Z("requested a shared WebAssembly.Memory but the returned buffer is not a SharedArrayBuffer, indicating that while the browser has SharedArrayBuffer it does not have WebAssembly threads support - you may need to set a flag"), Error("bad memory"); + Ce(); + } + var bt = [], Ae = [], Me = [], Ue = 0, zt = null, wt = null; + function zo() { + if (--Ue == 0 && (zt !== null && (clearInterval(zt), zt = null), wt)) { + var s = wt; + wt = null, s(); + } + } + function Ot(s) { + throw Z(s = "Aborted(" + s + ")"), $e = true, le = 1, s = new WebAssembly.RuntimeError(s + ". Build with -sASSERTIONS for more info."), m(s), s; + } + var hn, Oo = (s) => s.startsWith("data:application/octet-stream;base64,"), Bo = (s) => s.startsWith("file://"); + function Do(s) { + if (s == hn && B) return new Uint8Array(B); + if (x) return x(s); + throw "both async and sync fetching of the wasm failed"; + } + function Mo(s, p, f) { + return function(b) { + if (!B && (w || g)) { + if (typeof fetch == "function" && !Bo(b)) return fetch(b, { credentials: "same-origin" }).then((_) => { + if (!_.ok) throw `failed to load wasm binary file at '${b}'`; + return _.arrayBuffer(); + }).catch(() => Do(b)); + if (v) return new Promise((_, I) => { + v(b, (O) => _(new Uint8Array(O)), I); + }); + } + return Promise.resolve().then(() => Do(b)); + }(s).then((b) => WebAssembly.instantiate(b, p)).then(f, (b) => { + Z(`failed to asynchronously prepare wasm: ${b}`), Ot(b); + }); + } + function Ro() { + return { a: { O: oc, Aa: nc, b: uc, aa: qo, B: Yo, qa: Xo, Y: Qo, _: Jo, ra: ei, oa: ti, ha: ri, na: ni, L: oi, Z: ii, W: ai, pa: si, X: ui, wa: dc, F: cc, Q: pc, P: fc, E: gc, u: yc, q: bc, G: wc, A: Ic, R: Cc, ua: Ac, ka: kc, U: Ec, ba: Pc, H: zc, ja: Cn, ta: Oc, t: Bc, x: Rc, o: Uc, l: Nc, c: Tn, n: Wc, j: Gc, w: Fc, p: qc, g: jc, s: Kc, m: Yc, e: Xc, k: Zc, i: Qc, h: Jc, d: ep, ea: tp, fa: rp, ga: np, ca: $i, da: xi, T: op, f: ip, D: ap, I: sp, M: up, y: dp, sa: lp, V: cp, v: Ti, z: pp, N: mp, S: fp, za: hp, ya: gp, la: Ai, ma: ki, $: _n, C: Ei, K: Pi, ia: zi, J: Oi, a: de, xa: wn, va: Mi, r: wp } }; + } + var gn = { 867620: (s, p, f, b, _) => { + if (u === void 0 || !u.Fb) return 1; + if ((s = ze(s >>> 0)).startsWith("./") && (s = s.substring(2)), !(s = u.Fb.get(s))) return 2; + if (b >>>= 0, (p >>>= 0) + (f >>>= 0) > s.byteLength) return 3; + try { + let I = s.subarray(p, p + f); + switch (_) { + case 0: + r().set(I, b >>> 0); + break; + case 1: + u.cc(b, I); + break; + default: + return 4; + } + return 0; + } catch { + return 4; + } + }, 868303: (s, p, f) => { + u.dc(s, r().subarray(p >>> 0, p + f >>> 0)); + }, 868366: () => u.ac(), 868407: (s) => { + u.Ob(s); + }, 868443: () => { + u.Vb(); + }, 868474: () => { + u.Wb(); + }, 868503: () => { + u.$b(); + }, 868528: (s) => u.Ub(s), 868561: (s) => u.Yb(s), 868593: (s, p, f) => { + u.Nb(s, p, f, true); + }, 868632: (s, p, f) => { + u.Nb(s, p, f); + }, 868665: () => typeof wasmOffsetConverter < "u", 868722: (s) => { + u.jb("Abs", s, void 0); + }, 868773: (s) => { + u.jb("Neg", s, void 0); + }, 868824: (s) => { + u.jb("Floor", s, void 0); + }, 868877: (s) => { + u.jb("Ceil", s, void 0); + }, 868929: (s) => { + u.jb("Reciprocal", s, void 0); + }, 868987: (s) => { + u.jb("Sqrt", s, void 0); + }, 869039: (s) => { + u.jb("Exp", s, void 0); + }, 869090: (s) => { + u.jb("Erf", s, void 0); + }, 869141: (s) => { + u.jb("Sigmoid", s, void 0); + }, 869196: (s, p, f) => { + u.jb("HardSigmoid", s, { alpha: p, beta: f }); + }, 869275: (s) => { + u.jb("Log", s, void 0); + }, 869326: (s) => { + u.jb("Sin", s, void 0); + }, 869377: (s) => { + u.jb("Cos", s, void 0); + }, 869428: (s) => { + u.jb("Tan", s, void 0); + }, 869479: (s) => { + u.jb("Asin", s, void 0); + }, 869531: (s) => { + u.jb("Acos", s, void 0); + }, 869583: (s) => { + u.jb("Atan", s, void 0); + }, 869635: (s) => { + u.jb("Sinh", s, void 0); + }, 869687: (s) => { + u.jb("Cosh", s, void 0); + }, 869739: (s) => { + u.jb("Asinh", s, void 0); + }, 869792: (s) => { + u.jb("Acosh", s, void 0); + }, 869845: (s) => { + u.jb("Atanh", s, void 0); + }, 869898: (s) => { + u.jb("Tanh", s, void 0); + }, 869950: (s) => { + u.jb("Not", s, void 0); + }, 870001: (s, p, f) => { + u.jb("Clip", s, { min: p, max: f }); + }, 870070: (s) => { + u.jb("Clip", s, void 0); + }, 870122: (s, p) => { + u.jb("Elu", s, { alpha: p }); + }, 870180: (s) => { + u.jb("Gelu", s, void 0); + }, 870232: (s) => { + u.jb("Relu", s, void 0); + }, 870284: (s, p) => { + u.jb("LeakyRelu", s, { alpha: p }); + }, 870348: (s, p) => { + u.jb("ThresholdedRelu", s, { alpha: p }); + }, 870418: (s, p) => { + u.jb("Cast", s, { to: p }); + }, 870476: (s) => { + u.jb("Add", s, void 0); + }, 870527: (s) => { + u.jb("Sub", s, void 0); + }, 870578: (s) => { + u.jb("Mul", s, void 0); + }, 870629: (s) => { + u.jb("Div", s, void 0); + }, 870680: (s) => { + u.jb("Pow", s, void 0); + }, 870731: (s) => { + u.jb("Equal", s, void 0); + }, 870784: (s) => { + u.jb("Greater", s, void 0); + }, 870839: (s) => { + u.jb("GreaterOrEqual", s, void 0); + }, 870901: (s) => { + u.jb("Less", s, void 0); + }, 870953: (s) => { + u.jb("LessOrEqual", s, void 0); + }, 871012: (s, p, f, b, _) => { + u.jb("ReduceMean", s, { keepDims: !!p, noopWithEmptyAxes: !!f, axes: b ? Array.from(i().subarray(b >>> 0, _ >>> 0)) : [] }); + }, 871171: (s, p, f, b, _) => { + u.jb("ReduceMax", s, { keepDims: !!p, noopWithEmptyAxes: !!f, axes: b ? Array.from(i().subarray(b >>> 0, _ >>> 0)) : [] }); + }, 871329: (s, p, f, b, _) => { + u.jb("ReduceMin", s, { keepDims: !!p, noopWithEmptyAxes: !!f, axes: b ? Array.from(i().subarray(b >>> 0, _ >>> 0)) : [] }); + }, 871487: (s, p, f, b, _) => { + u.jb("ReduceProd", s, { keepDims: !!p, noopWithEmptyAxes: !!f, axes: b ? Array.from(i().subarray(b >>> 0, _ >>> 0)) : [] }); + }, 871646: (s, p, f, b, _) => { + u.jb("ReduceSum", s, { keepDims: !!p, noopWithEmptyAxes: !!f, axes: b ? Array.from(i().subarray(b >>> 0, _ >>> 0)) : [] }); + }, 871804: (s, p, f, b, _) => { + u.jb("ReduceL1", s, { keepDims: !!p, noopWithEmptyAxes: !!f, axes: b ? Array.from(i().subarray(b >>> 0, _ >>> 0)) : [] }); + }, 871961: (s, p, f, b, _) => { + u.jb("ReduceL2", s, { keepDims: !!p, noopWithEmptyAxes: !!f, axes: b ? Array.from(i().subarray(b >>> 0, _ >>> 0)) : [] }); + }, 872118: (s, p, f, b, _) => { + u.jb("ReduceLogSum", s, { keepDims: !!p, noopWithEmptyAxes: !!f, axes: b ? Array.from(i().subarray(b >>> 0, _ >>> 0)) : [] }); + }, 872279: (s, p, f, b, _) => { + u.jb("ReduceSumSquare", s, { keepDims: !!p, noopWithEmptyAxes: !!f, axes: b ? Array.from(i().subarray(b >>> 0, _ >>> 0)) : [] }); + }, 872443: (s, p, f, b, _) => { + u.jb("ReduceLogSumExp", s, { keepDims: !!p, noopWithEmptyAxes: !!f, axes: b ? Array.from(i().subarray(b >>> 0, _ >>> 0)) : [] }); + }, 872607: (s) => { + u.jb("Where", s, void 0); + }, 872660: (s, p, f) => { + u.jb("Transpose", s, { perm: p ? Array.from(i().subarray(p >>> 0, f >>> 0)) : [] }); + }, 872768: (s, p, f, b) => { + u.jb("DepthToSpace", s, { blocksize: p, mode: ze(f), format: b ? "NHWC" : "NCHW" }); + }, 872901: (s, p, f, b) => { + u.jb("DepthToSpace", s, { blocksize: p, mode: ze(f), format: b ? "NHWC" : "NCHW" }); + }, 873034: (s, p, f, b, _, I, O, D, L, H, X, fe, be, z, pe) => { + u.jb("ConvTranspose", s, { format: L ? "NHWC" : "NCHW", autoPad: p, dilations: [f], group: b, kernelShape: [_], pads: [I, O], strides: [D], wIsConst: () => !!t()[H >>> 0], outputPadding: X ? Array.from(i().subarray(X >>> 0, fe >>> 0)) : [], outputShape: be ? Array.from(i().subarray(be >>> 0, z >>> 0)) : [], activation: ze(pe) }); + }, 873435: (s, p, f, b, _, I, O, D, L, H, X, fe, be, z) => { + u.jb("ConvTranspose", s, { format: D ? "NHWC" : "NCHW", autoPad: p, dilations: Array.from(i().subarray(f >>> 0, 2 + (f >>> 0) >>> 0)), group: b, kernelShape: Array.from(i().subarray(_ >>> 0, 2 + (_ >>> 0) >>> 0)), pads: Array.from(i().subarray(I >>> 0, 4 + (I >>> 0) >>> 0)), strides: Array.from(i().subarray(O >>> 0, 2 + (O >>> 0) >>> 0)), wIsConst: () => !!t()[L >>> 0], outputPadding: H ? Array.from(i().subarray(H >>> 0, X >>> 0)) : [], outputShape: fe ? Array.from(i().subarray(fe >>> 0, be >>> 0)) : [], activation: ze(z) }); + }, 874e3: (s, p, f, b, _, I, O, D, L, H, X, fe, be, z, pe) => { + u.jb("ConvTranspose", s, { format: L ? "NHWC" : "NCHW", autoPad: p, dilations: [f], group: b, kernelShape: [_], pads: [I, O], strides: [D], wIsConst: () => !!t()[H >>> 0], outputPadding: X ? Array.from(i().subarray(X >>> 0, fe >>> 0)) : [], outputShape: be ? Array.from(i().subarray(be >>> 0, z >>> 0)) : [], activation: ze(pe) }); + }, 874401: (s, p, f, b, _, I, O, D, L, H, X, fe, be, z) => { + u.jb("ConvTranspose", s, { format: D ? "NHWC" : "NCHW", autoPad: p, dilations: Array.from(i().subarray(f >>> 0, 2 + (f >>> 0) >>> 0)), group: b, kernelShape: Array.from(i().subarray(_ >>> 0, 2 + (_ >>> 0) >>> 0)), pads: Array.from(i().subarray(I >>> 0, 4 + (I >>> 0) >>> 0)), strides: Array.from(i().subarray(O >>> 0, 2 + (O >>> 0) >>> 0)), wIsConst: () => !!t()[L >>> 0], outputPadding: H ? Array.from(i().subarray(H >>> 0, X >>> 0)) : [], outputShape: fe ? Array.from(i().subarray(fe >>> 0, be >>> 0)) : [], activation: ze(z) }); + }, 874966: (s, p) => { + u.jb("GlobalAveragePool", s, { format: p ? "NHWC" : "NCHW" }); + }, 875057: (s, p, f, b, _, I, O, D, L, H, X, fe, be, z) => { + u.jb("AveragePool", s, { format: z ? "NHWC" : "NCHW", auto_pad: p, ceil_mode: f, count_include_pad: b, storage_order: _, dilations: I ? Array.from(i().subarray(I >>> 0, O >>> 0)) : [], kernel_shape: D ? Array.from(i().subarray(D >>> 0, L >>> 0)) : [], pads: H ? Array.from(i().subarray(H >>> 0, X >>> 0)) : [], strides: fe ? Array.from(i().subarray(fe >>> 0, be >>> 0)) : [] }); + }, 875472: (s, p) => { + u.jb("GlobalAveragePool", s, { format: p ? "NHWC" : "NCHW" }); + }, 875563: (s, p, f, b, _, I, O, D, L, H, X, fe, be, z) => { + u.jb("AveragePool", s, { format: z ? "NHWC" : "NCHW", auto_pad: p, ceil_mode: f, count_include_pad: b, storage_order: _, dilations: I ? Array.from(i().subarray(I >>> 0, O >>> 0)) : [], kernel_shape: D ? Array.from(i().subarray(D >>> 0, L >>> 0)) : [], pads: H ? Array.from(i().subarray(H >>> 0, X >>> 0)) : [], strides: fe ? Array.from(i().subarray(fe >>> 0, be >>> 0)) : [] }); + }, 875978: (s, p) => { + u.jb("GlobalMaxPool", s, { format: p ? "NHWC" : "NCHW" }); + }, 876065: (s, p, f, b, _, I, O, D, L, H, X, fe, be, z) => { + u.jb("MaxPool", s, { format: z ? "NHWC" : "NCHW", auto_pad: p, ceil_mode: f, count_include_pad: b, storage_order: _, dilations: I ? Array.from(i().subarray(I >>> 0, O >>> 0)) : [], kernel_shape: D ? Array.from(i().subarray(D >>> 0, L >>> 0)) : [], pads: H ? Array.from(i().subarray(H >>> 0, X >>> 0)) : [], strides: fe ? Array.from(i().subarray(fe >>> 0, be >>> 0)) : [] }); + }, 876476: (s, p) => { + u.jb("GlobalMaxPool", s, { format: p ? "NHWC" : "NCHW" }); + }, 876563: (s, p, f, b, _, I, O, D, L, H, X, fe, be, z) => { + u.jb("MaxPool", s, { format: z ? "NHWC" : "NCHW", auto_pad: p, ceil_mode: f, count_include_pad: b, storage_order: _, dilations: I ? Array.from(i().subarray(I >>> 0, O >>> 0)) : [], kernel_shape: D ? Array.from(i().subarray(D >>> 0, L >>> 0)) : [], pads: H ? Array.from(i().subarray(H >>> 0, X >>> 0)) : [], strides: fe ? Array.from(i().subarray(fe >>> 0, be >>> 0)) : [] }); + }, 876974: (s, p, f, b, _) => { + u.jb("Gemm", s, { alpha: p, beta: f, transA: b, transB: _ }); + }, 877078: (s) => { + u.jb("MatMul", s, void 0); + }, 877132: (s, p, f, b) => { + u.jb("ArgMax", s, { keepDims: !!p, selectLastIndex: !!f, axis: b }); + }, 877240: (s, p, f, b) => { + u.jb("ArgMin", s, { keepDims: !!p, selectLastIndex: !!f, axis: b }); + }, 877348: (s, p) => { + u.jb("Softmax", s, { axis: p }); + }, 877411: (s, p) => { + u.jb("Concat", s, { axis: p }); + }, 877471: (s, p, f, b, _) => { + u.jb("Split", s, { axis: p, numOutputs: f, splitSizes: b ? Array.from(i().subarray(b >>> 0, _ >>> 0)) : [] }); + }, 877611: (s) => { + u.jb("Expand", s, void 0); + }, 877665: (s, p) => { + u.jb("Gather", s, { axis: Number(p) }); + }, 877736: (s, p) => { + u.jb("GatherElements", s, { axis: Number(p) }); + }, 877815: (s, p, f, b, _, I, O, D, L, H, X) => { + u.jb("Resize", s, { antialias: p, axes: f ? Array.from(i().subarray(f >>> 0, b >>> 0)) : [], coordinateTransformMode: ze(_), cubicCoeffA: I, excludeOutside: O, extrapolationValue: D, keepAspectRatioPolicy: ze(L), mode: ze(H), nearestMode: ze(X) }); + }, 878161: (s, p, f, b, _, I, O) => { + u.jb("Slice", s, { starts: p ? Array.from(i().subarray(p >>> 0, f >>> 0)) : [], ends: b ? Array.from(i().subarray(b >>> 0, _ >>> 0)) : [], axes: I ? Array.from(i().subarray(I >>> 0, O >>> 0)) : [] }); + }, 878377: (s) => { + u.jb("Tile", s, void 0); + }, 878429: (s, p, f) => { + u.jb("InstanceNormalization", s, { epsilon: p, format: f ? "NHWC" : "NCHW" }); + }, 878543: (s, p, f) => { + u.jb("InstanceNormalization", s, { epsilon: p, format: f ? "NHWC" : "NCHW" }); + }, 878657: (s) => { + u.jb("Range", s, void 0); + }, 878710: (s, p) => { + u.jb("Einsum", s, { equation: ze(p) }); + }, 878791: (s, p, f, b, _) => { + u.jb("Pad", s, { mode: p, value: f, pads: b ? Array.from(i().subarray(b >>> 0, _ >>> 0)) : [] }); + }, 878918: (s, p, f, b, _, I) => { + u.jb("BatchNormalization", s, { epsilon: p, momentum: f, spatial: !!_, trainingMode: !!b, format: I ? "NHWC" : "NCHW" }); + }, 879087: (s, p, f, b, _, I) => { + u.jb("BatchNormalization", s, { epsilon: p, momentum: f, spatial: !!_, trainingMode: !!b, format: I ? "NHWC" : "NCHW" }); + }, 879256: (s, p, f) => { + u.jb("CumSum", s, { exclusive: Number(p), reverse: Number(f) }); + }, 879353: (s, p, f) => { + u.jb("DequantizeLinear", s, { axis: p, blockSize: f }); + }, 879443: (s, p, f, b, _, I, O, D, L) => { + u.jb("Attention", s, { numHeads: p, isUnidirectional: f, maskFilterValue: b, scale: _, doRotary: I, qkvHiddenSizes: O ? Array.from(i().subarray(Number(D) >>> 0, Number(D) + O >>> 0)) : [], pastPresentShareBuffer: !!L }); + }, 879715: (s) => { + u.jb("BiasAdd", s, void 0); + }, 879770: (s) => { + u.jb("BiasSplitGelu", s, void 0); + }, 879831: (s) => { + u.jb("FastGelu", s, void 0); + }, 879887: (s, p, f, b, _, I, O, D, L, H, X, fe, be, z, pe, Se) => { + u.jb("Conv", s, { format: fe ? "NHWC" : "NCHW", auto_pad: p, dilations: f ? Array.from(i().subarray(f >>> 0, b >>> 0)) : [], group: _, kernel_shape: I ? Array.from(i().subarray(I >>> 0, O >>> 0)) : [], pads: D ? Array.from(i().subarray(D >>> 0, L >>> 0)) : [], strides: H ? Array.from(i().subarray(H >>> 0, X >>> 0)) : [], w_is_const: () => !!t()[be >>> 0], activation: ze(z), activation_params: pe ? Array.from(d().subarray(pe >>> 0, Se >>> 0)) : [] }); + }, 880383: (s) => { + u.jb("Gelu", s, void 0); + }, 880435: (s, p, f, b) => { + u.jb("GroupQueryAttention", s, { numHeads: p, kvNumHeads: f, scale: b }); + }, 880548: (s, p, f, b) => { + u.jb("LayerNormalization", s, { axis: p, epsilon: f, simplified: !!b }); + }, 880659: (s, p, f, b) => { + u.jb("LayerNormalization", s, { axis: p, epsilon: f, simplified: !!b }); + }, 880770: (s, p, f, b, _, I) => { + u.jb("MatMulNBits", s, { k: p, n: f, accuracyLevel: b, bits: _, blockSize: I }); + }, 880897: (s, p, f, b, _, I) => { + u.jb("MultiHeadAttention", s, { numHeads: p, isUnidirectional: f, maskFilterValue: b, scale: _, doRotary: I }); + }, 881056: (s, p) => { + u.jb("QuickGelu", s, { alpha: p }); + }, 881120: (s, p, f, b, _) => { + u.jb("RotaryEmbedding", s, { interleaved: !!p, numHeads: f, rotaryEmbeddingDim: b, scale: _ }); + }, 881259: (s, p, f) => { + u.jb("SkipLayerNormalization", s, { epsilon: p, simplified: !!f }); + }, 881361: (s, p, f) => { + u.jb("SkipLayerNormalization", s, { epsilon: p, simplified: !!f }); + }, 881463: (s, p, f, b) => { + u.jb("GatherBlockQuantized", s, { gatherAxis: p, quantizeAxis: f, blockSize: b }); + }, 881584: (s) => { + u.Zb(s); + }, 881618: (s, p) => u.bc(s, p, u.Eb.fc, u.Eb.errors) }; + function nc(s, p, f) { + return yi(async () => { + await u.Xb(s, p, f); + }); + } + function oc() { + return typeof wasmOffsetConverter < "u"; + } + function yn(s) { + this.name = "ExitStatus", this.message = `Program terminated with exit(${s})`, this.status = s; + } + var bn = (s) => { + s.terminate(), s.onmessage = () => { + }; + }, Uo = (s) => { + pt.length == 0 && (Go(), Ho(pt[0])); + var p = pt.pop(); + if (!p) return 6; + vt.push(p), Qe[s.Ab] = p, p.Ab = s.Ab; + var f = { cmd: "run", start_routine: s.hc, arg: s.Qb, pthread_ptr: s.Ab }; + return p.postMessage(f, s.mc), 0; + }, _t = 0, xe = (s, p, ...f) => { + for (var b = 2 * f.length, _ = Rn(), I = Mn(8 * b), O = I >>> 3, D = 0; D < f.length; D++) { + var L = f[D]; + typeof L == "bigint" ? (G[O + 2 * D] = 1n, G[O + 2 * D + 1] = L) : (G[O + 2 * D] = 0n, l()[O + 2 * D + 1 >>> 0] = L); + } + return s = Wi(s, 0, b, I, p), yr(_), s; + }; + function wn(s) { + if (y) return xe(0, 1, s); + if (le = s, !(0 < _t)) { + for (var p of vt) bn(p); + for (p of pt) bn(p); + pt = [], vt = [], Qe = [], $e = true; + } + A(s, new yn(s)); + } + function Vo(s) { + if (y) return xe(1, 0, s); + _n(s); + } + var _n = (s) => { + if (le = s, y) throw Vo(s), "unwind"; + wn(s); + }, pt = [], vt = [], No = [], Qe = {}, Wo = (s) => { + var p = s.Ab; + delete Qe[p], pt.push(s), vt.splice(vt.indexOf(s), 1), s.Ab = 0, Dn(p); + }; + function Lo() { + No.forEach((s) => s()); + } + var Ho = (s) => new Promise((p) => { + s.onmessage = (_) => { + var I = (_ = _.data).cmd; + if (_.targetThread && _.targetThread != Rt()) { + var O = Qe[_.targetThread]; + O ? O.postMessage(_, _.transferList) : Z(`Internal error! Worker sent a message "${I}" to target pthread ${_.targetThread}, but that thread no longer exists!`); + } else I === "checkMailbox" ? sr() : I === "spawnThread" ? Uo(_) : I === "cleanupThread" ? Wo(Qe[_.thread]) : I === "killThread" ? (_ = _.thread, I = Qe[_], delete Qe[_], bn(I), Dn(_), vt.splice(vt.indexOf(I), 1), I.Ab = 0) : I === "cancelThread" ? Qe[_.thread].postMessage({ cmd: "cancel" }) : I === "loaded" ? (s.loaded = true, p(s)) : I === "alert" ? alert(`Thread ${_.threadId}: ${_.text}`) : _.target === "setimmediate" ? s.postMessage(_) : I === "callHandler" ? u[_.handler](..._.args) : I && Z(`worker sent an unknown command ${I}`); + }, s.onerror = (_) => { + throw Z(`worker sent an error! ${_.filename}:${_.lineno}: ${_.message}`), _; + }; + var f, b = []; + for (f of []) u.hasOwnProperty(f) && b.push(f); + s.postMessage({ cmd: "load", handlers: b, wasmMemory: de, wasmModule: Y }); + }); + function Go() { + var s = new Worker(new URL(META_URL), { type: "module", workerData: "em-pthread", name: "em-pthread" }); + pt.push(s); + } + var ar = (s) => { + for (; 0 < s.length; ) s.shift()(u); + }, ic = () => { + var s = Rt(), p = a()[s + 52 >>> 2 >>> 0]; + s = a()[s + 56 >>> 2 >>> 0], Hi(p, p - s), yr(p); + }, ac = (s, p) => { + _t = 0, s = Gi(s, p), 0 < _t ? le = s : gr(s); + }; + class sc { + constructor(p) { + this.Jb = p - 24; + } + } + function uc(s, p, f) { + var b = new sc(s >>>= 0); + throw p >>>= 0, f >>>= 0, a()[b.Jb + 16 >>> 2 >>> 0] = 0, a()[b.Jb + 4 >>> 2 >>> 0] = p, a()[b.Jb + 8 >>> 2 >>> 0] = f, s; + } + function Fo(s, p, f, b) { + return y ? xe(2, 1, s, p, f, b) : qo(s, p, f, b); + } + function qo(s, p, f, b) { + if (s >>>= 0, p >>>= 0, f >>>= 0, b >>>= 0, S === void 0) return Z("Current environment does not support SharedArrayBuffer, pthreads are not available!"), 6; + var _ = []; + return y && _.length === 0 ? Fo(s, p, f, b) : (s = { hc: f, Ab: s, Qb: b, mc: _ }, y ? (s.Mb = "spawnThread", postMessage(s, _), 0) : Uo(s)); + } + var jo = typeof TextDecoder < "u" ? new TextDecoder("utf8") : void 0, Ko = (s, p, f) => { + var b = (p >>>= 0) + f; + for (f = p; s[f] && !(f >= b); ) ++f; + if (16 < f - p && s.buffer && jo) return jo.decode(s.buffer instanceof S ? s.slice(p, f) : s.subarray(p, f)); + for (b = ""; p < f; ) { + var _ = s[p++]; + if (128 & _) { + var I = 63 & s[p++]; + if ((224 & _) == 192) b += String.fromCharCode((31 & _) << 6 | I); + else { + var O = 63 & s[p++]; + 65536 > (_ = (240 & _) == 224 ? (15 & _) << 12 | I << 6 | O : (7 & _) << 18 | I << 12 | O << 6 | 63 & s[p++]) ? b += String.fromCharCode(_) : (_ -= 65536, b += String.fromCharCode(55296 | _ >> 10, 56320 | 1023 & _)); + } + } else b += String.fromCharCode(_); + } + return b; + }, ze = (s, p) => (s >>>= 0) ? Ko(r(), s, p) : ""; + function Yo(s, p, f) { + return y ? xe(3, 1, s, p, f) : 0; + } + function Xo(s, p) { + if (y) return xe(4, 1, s, p); + } + var vn = (s) => { + for (var p = 0, f = 0; f < s.length; ++f) { + var b = s.charCodeAt(f); + 127 >= b ? p++ : 2047 >= b ? p += 2 : 55296 <= b && 57343 >= b ? (p += 4, ++f) : p += 3; + } + return p; + }, Zo = (s, p, f, b) => { + if (!(0 < b)) return 0; + var _ = f >>>= 0; + b = f + b - 1; + for (var I = 0; I < s.length; ++I) { + var O = s.charCodeAt(I); + if (55296 <= O && 57343 >= O && (O = 65536 + ((1023 & O) << 10) | 1023 & s.charCodeAt(++I)), 127 >= O) { + if (f >= b) break; + p[f++ >>> 0] = O; + } else { + if (2047 >= O) { + if (f + 1 >= b) break; + p[f++ >>> 0] = 192 | O >> 6; + } else { + if (65535 >= O) { + if (f + 2 >= b) break; + p[f++ >>> 0] = 224 | O >> 12; + } else { + if (f + 3 >= b) break; + p[f++ >>> 0] = 240 | O >> 18, p[f++ >>> 0] = 128 | O >> 12 & 63; + } + p[f++ >>> 0] = 128 | O >> 6 & 63; + } + p[f++ >>> 0] = 128 | 63 & O; + } + } + return p[f >>> 0] = 0, f - _; + }, Bt = (s, p, f) => Zo(s, r(), p, f); + function Qo(s, p) { + if (y) return xe(5, 1, s, p); + } + function Jo(s, p, f) { + if (y) return xe(6, 1, s, p, f); + } + function ei(s, p, f) { + return y ? xe(7, 1, s, p, f) : 0; + } + function ti(s, p) { + if (y) return xe(8, 1, s, p); + } + function ri(s, p, f) { + if (y) return xe(9, 1, s, p, f); + } + function ni(s, p, f, b) { + if (y) return xe(10, 1, s, p, f, b); + } + function oi(s, p, f, b) { + if (y) return xe(11, 1, s, p, f, b); + } + function ii(s, p, f, b) { + if (y) return xe(12, 1, s, p, f, b); + } + function ai(s) { + if (y) return xe(13, 1, s); + } + function si(s, p) { + if (y) return xe(14, 1, s, p); + } + function ui(s, p, f) { + if (y) return xe(15, 1, s, p, f); + } + var di, mt, dc = () => { + Ot(""); + }, Je = (s) => { + for (var p = ""; r()[s >>> 0]; ) p += di[r()[s++ >>> 0]]; + return p; + }, $n = {}, xn = {}, lc = {}; + function ut(s, p, f = {}) { + if (!("argPackAdvance" in p)) throw new TypeError("registerType registeredInstance requires argPackAdvance"); + return function(b, _, I = {}) { + var O = _.name; + if (!b) throw new mt(`type "${O}" must have a positive integer typeid pointer`); + if (xn.hasOwnProperty(b)) { + if (I.Sb) return; + throw new mt(`Cannot register type '${O}' twice`); + } + xn[b] = _, delete lc[b], $n.hasOwnProperty(b) && (_ = $n[b], delete $n[b], _.forEach((D) => D())); + }(s, p, f); + } + var li = (s, p, f) => { + switch (p) { + case 1: + return f ? (b) => t()[b >>> 0] : (b) => r()[b >>> 0]; + case 2: + return f ? (b) => n()[b >>> 1 >>> 0] : (b) => o()[b >>> 1 >>> 0]; + case 4: + return f ? (b) => i()[b >>> 2 >>> 0] : (b) => a()[b >>> 2 >>> 0]; + case 8: + return f ? (b) => G[b >>> 3] : (b) => ye[b >>> 3]; + default: + throw new TypeError(`invalid integer width (${p}): ${s}`); + } + }; + function cc(s, p, f) { + f >>>= 0, ut(s >>>= 0, { name: p = Je(p >>> 0), fromWireType: (b) => b, toWireType: function(b, _) { + if (typeof _ != "bigint" && typeof _ != "number") throw _ = _ === null ? "null" : (b = typeof _) == "object" || b === "array" || b === "function" ? _.toString() : "" + _, new TypeError(`Cannot convert "${_}" to ${this.name}`); + return typeof _ == "number" && (_ = BigInt(_)), _; + }, argPackAdvance: ft, readValueFromPointer: li(p, f, p.indexOf("u") == -1), Db: null }); + } + var ft = 8; + function pc(s, p, f, b) { + ut(s >>>= 0, { name: p = Je(p >>> 0), fromWireType: function(_) { + return !!_; + }, toWireType: function(_, I) { + return I ? f : b; + }, argPackAdvance: ft, readValueFromPointer: function(_) { + return this.fromWireType(r()[_ >>> 0]); + }, Db: null }); + } + var Sn = [], dt = []; + function Tn(s) { + 9 < (s >>>= 0) && --dt[s + 1] == 0 && (dt[s] = void 0, Sn.push(s)); + } + var qe = (s) => { + if (!s) throw new mt("Cannot use deleted val. handle = " + s); + return dt[s]; + }, je = (s) => { + switch (s) { + case void 0: + return 2; + case null: + return 4; + case true: + return 6; + case false: + return 8; + default: + let p = Sn.pop() || dt.length; + return dt[p] = s, dt[p + 1] = 1, p; + } + }; + function In(s) { + return this.fromWireType(a()[s >>> 2 >>> 0]); + } + var mc = { name: "emscripten::val", fromWireType: (s) => { + var p = qe(s); + return Tn(s), p; + }, toWireType: (s, p) => je(p), argPackAdvance: ft, readValueFromPointer: In, Db: null }; + function fc(s) { + return ut(s >>> 0, mc); + } + var hc = (s, p) => { + switch (p) { + case 4: + return function(f) { + return this.fromWireType(d()[f >>> 2 >>> 0]); + }; + case 8: + return function(f) { + return this.fromWireType(l()[f >>> 3 >>> 0]); + }; + default: + throw new TypeError(`invalid float width (${p}): ${s}`); + } + }; + function gc(s, p, f) { + f >>>= 0, ut(s >>>= 0, { name: p = Je(p >>> 0), fromWireType: (b) => b, toWireType: (b, _) => _, argPackAdvance: ft, readValueFromPointer: hc(p, f), Db: null }); + } + function yc(s, p, f, b, _) { + if (s >>>= 0, f >>>= 0, p = Je(p >>> 0), _ === -1 && (_ = 4294967295), _ = (D) => D, b === 0) { + var I = 32 - 8 * f; + _ = (D) => D << I >>> I; + } + var O = p.includes("unsigned") ? function(D, L) { + return L >>> 0; + } : function(D, L) { + return L; + }; + ut(s, { name: p, fromWireType: _, toWireType: O, argPackAdvance: ft, readValueFromPointer: li(p, f, b !== 0), Db: null }); + } + function bc(s, p, f) { + function b(I) { + var O = a()[I >>> 2 >>> 0]; + return I = a()[I + 4 >>> 2 >>> 0], new _(t().buffer, I, O); + } + var _ = [Int8Array, Uint8Array, Int16Array, Uint16Array, Int32Array, Uint32Array, Float32Array, Float64Array, BigInt64Array, BigUint64Array][p]; + ut(s >>>= 0, { name: f = Je(f >>> 0), fromWireType: b, argPackAdvance: ft, readValueFromPointer: b }, { Sb: true }); + } + function wc(s, p) { + s >>>= 0; + var f = (p = Je(p >>> 0)) === "std::string"; + ut(s, { name: p, fromWireType: function(b) { + var _ = a()[b >>> 2 >>> 0], I = b + 4; + if (f) for (var O = I, D = 0; D <= _; ++D) { + var L = I + D; + if (D == _ || r()[L >>> 0] == 0) { + if (O = ze(O, L - O), H === void 0) var H = O; + else H += String.fromCharCode(0), H += O; + O = L + 1; + } + } + else { + for (H = Array(_), D = 0; D < _; ++D) H[D] = String.fromCharCode(r()[I + D >>> 0]); + H = H.join(""); + } + return tt(b), H; + }, toWireType: function(b, _) { + _ instanceof ArrayBuffer && (_ = new Uint8Array(_)); + var I = typeof _ == "string"; + if (!(I || _ instanceof Uint8Array || _ instanceof Uint8ClampedArray || _ instanceof Int8Array)) throw new mt("Cannot pass non-string to std::string"); + var O = f && I ? vn(_) : _.length, D = hr(4 + O + 1), L = D + 4; + if (a()[D >>> 2 >>> 0] = O, f && I) Bt(_, L, O + 1); + else if (I) for (I = 0; I < O; ++I) { + var H = _.charCodeAt(I); + if (255 < H) throw tt(L), new mt("String has UTF-16 code units that do not fit in 8 bits"); + r()[L + I >>> 0] = H; + } + else for (I = 0; I < O; ++I) r()[L + I >>> 0] = _[I]; + return b !== null && b.push(tt, D), D; + }, argPackAdvance: ft, readValueFromPointer: In, Db(b) { + tt(b); + } }); + } + var ci = typeof TextDecoder < "u" ? new TextDecoder("utf-16le") : void 0, _c = (s, p) => { + for (var f = s >> 1, b = f + p / 2; !(f >= b) && o()[f >>> 0]; ) ++f; + if (32 < (f <<= 1) - s && ci) return ci.decode(r().slice(s, f)); + for (f = "", b = 0; !(b >= p / 2); ++b) { + var _ = n()[s + 2 * b >>> 1 >>> 0]; + if (_ == 0) break; + f += String.fromCharCode(_); + } + return f; + }, vc = (s, p, f) => { + if (f ?? (f = 2147483647), 2 > f) return 0; + var b = p; + f = (f -= 2) < 2 * s.length ? f / 2 : s.length; + for (var _ = 0; _ < f; ++_) { + var I = s.charCodeAt(_); + n()[p >>> 1 >>> 0] = I, p += 2; + } + return n()[p >>> 1 >>> 0] = 0, p - b; + }, $c = (s) => 2 * s.length, xc = (s, p) => { + for (var f = 0, b = ""; !(f >= p / 4); ) { + var _ = i()[s + 4 * f >>> 2 >>> 0]; + if (_ == 0) break; + ++f, 65536 <= _ ? (_ -= 65536, b += String.fromCharCode(55296 | _ >> 10, 56320 | 1023 & _)) : b += String.fromCharCode(_); + } + return b; + }, Sc = (s, p, f) => { + if (p >>>= 0, f ?? (f = 2147483647), 4 > f) return 0; + var b = p; + f = b + f - 4; + for (var _ = 0; _ < s.length; ++_) { + var I = s.charCodeAt(_); + if (55296 <= I && 57343 >= I && (I = 65536 + ((1023 & I) << 10) | 1023 & s.charCodeAt(++_)), i()[p >>> 2 >>> 0] = I, (p += 4) + 4 > f) break; + } + return i()[p >>> 2 >>> 0] = 0, p - b; + }, Tc = (s) => { + for (var p = 0, f = 0; f < s.length; ++f) { + var b = s.charCodeAt(f); + 55296 <= b && 57343 >= b && ++f, p += 4; + } + return p; + }; + function Ic(s, p, f) { + if (s >>>= 0, p >>>= 0, f = Je(f >>>= 0), p === 2) var b = _c, _ = vc, I = $c, O = (D) => o()[D >>> 1 >>> 0]; + else p === 4 && (b = xc, _ = Sc, I = Tc, O = (D) => a()[D >>> 2 >>> 0]); + ut(s, { name: f, fromWireType: (D) => { + for (var L, H = a()[D >>> 2 >>> 0], X = D + 4, fe = 0; fe <= H; ++fe) { + var be = D + 4 + fe * p; + fe != H && O(be) != 0 || (X = b(X, be - X), L === void 0 ? L = X : (L += String.fromCharCode(0), L += X), X = be + p); + } + return tt(D), L; + }, toWireType: (D, L) => { + if (typeof L != "string") throw new mt(`Cannot pass non-string to C++ string type ${f}`); + var H = I(L), X = hr(4 + H + p); + return a()[X >>> 2 >>> 0] = H / p, _(L, X + 4, H + p), D !== null && D.push(tt, X), X; + }, argPackAdvance: ft, readValueFromPointer: In, Db(D) { + tt(D); + } }); + } + function Cc(s, p) { + ut(s >>>= 0, { Tb: true, name: p = Je(p >>> 0), argPackAdvance: 0, fromWireType: () => { + }, toWireType: () => { + } }); + } + var Ac = () => 1; + function kc(s) { + Bn(s >>> 0, !g, 1, !w, 131072, false), Lo(); + } + var pi = (s) => { + if (!$e) try { + if (s(), !(0 < _t)) try { + y ? gr(le) : _n(le); + } catch (p) { + p instanceof yn || p == "unwind" || A(1, p); + } + } catch (p) { + p instanceof yn || p == "unwind" || A(1, p); + } + }; + function Cn(s) { + s >>>= 0, typeof Atomics.nc == "function" && (Atomics.nc(i(), s >>> 2, s).value.then(sr), s += 128, Atomics.store(i(), s >>> 2, 1)); + } + var sr = () => { + var s = Rt(); + s && (Cn(s), pi(Li)); + }; + function Ec(s, p) { + (s >>>= 0) == p >>> 0 ? setTimeout(sr) : y ? postMessage({ targetThread: s, cmd: "checkMailbox" }) : (s = Qe[s]) && s.postMessage({ cmd: "checkMailbox" }); + } + var An = []; + function Pc(s, p, f, b, _) { + for (p >>>= 0, b /= 2, An.length = b, f = _ >>> 0 >>> 3, _ = 0; _ < b; _++) An[_] = G[f + 2 * _] ? G[f + 2 * _ + 1] : l()[f + 2 * _ + 1 >>> 0]; + return (p ? gn[p] : _p[s])(...An); + } + function zc(s) { + s >>>= 0, y ? postMessage({ cmd: "cleanupThread", thread: s }) : Wo(Qe[s]); + } + function Oc(s) { + } + var kn = (s, p) => { + var f = xn[s]; + if (f === void 0) throw s = Ui(s), f = Je(s), tt(s), new mt(`${p} has unknown type ${f}`); + return f; + }, mi = (s, p, f) => { + var b = []; + return s = s.toWireType(b, f), b.length && (a()[p >>> 2 >>> 0] = je(b)), s; + }; + function Bc(s, p, f) { + return p >>>= 0, f >>>= 0, s = qe(s >>> 0), p = kn(p, "emval::as"), mi(p, f, s); + } + var ur = (s) => { + try { + s(); + } catch (p) { + Ot(p); + } + }, ht = 0, et = null, fi = 0, dr = [], hi = {}, gi = {}, Dc = 0, En = null, Mc = []; + function yi(s) { + return function(p) { + if (!$e) { + if (ht === 0) { + var f = false, b = false; + p((_ = 0) => { + if (!$e && (fi = _, f = true, b)) { + ht = 2, ur(() => ji(et)), typeof Browser < "u" && Browser.Kb.Rb && Browser.Kb.resume(), _ = false; + try { + var I = function() { + var L = i()[et + 8 >>> 2 >>> 0]; + return L = j[gi[L]], --_t, L(); + }(); + } catch (L) { + I = L, _ = true; + } + var O = false; + if (!et) { + var D = En; + D && (En = null, (_ ? D.reject : D.resolve)(I), O = true); + } + if (_ && !O) throw I; + } + }), b = true, f || (ht = 1, et = function() { + var _ = hr(65548), I = _ + 12; + a()[_ >>> 2 >>> 0] = I, a()[_ + 4 >>> 2 >>> 0] = I + 65536, I = dr[0]; + var O = hi[I]; + return O === void 0 && (O = Dc++, hi[I] = O, gi[O] = I), I = O, i()[_ + 8 >>> 2 >>> 0] = I, _; + }(), typeof Browser < "u" && Browser.Kb.Rb && Browser.Kb.pause(), ur(() => Fi(et))); + } else ht === 2 ? (ht = 0, ur(Ki), tt(et), et = null, Mc.forEach(pi)) : Ot(`invalid state: ${ht}`); + return fi; + } + }((p) => { + s().then(p); + }); + } + function Rc(s) { + return s >>>= 0, yi(() => (s = qe(s)).then(je)); + } + var lr = []; + function Uc(s, p, f, b) { + return f >>>= 0, b >>>= 0, (s = lr[s >>> 0])(null, p = qe(p >>> 0), f, b); + } + var Vc = {}, cr = (s) => { + var p = Vc[s]; + return p === void 0 ? Je(s) : p; + }; + function Nc(s, p, f, b, _) { + return f >>>= 0, b >>>= 0, _ >>>= 0, (s = lr[s >>> 0])(p = qe(p >>> 0), p[f = cr(f)], b, _); + } + var bi = () => typeof globalThis == "object" ? globalThis : Function("return this")(); + function Wc(s) { + return (s >>>= 0) == 0 ? je(bi()) : (s = cr(s), je(bi()[s])); + } + var Lc = (s) => { + var p = lr.length; + return lr.push(s), p; + }, Hc = (s, p) => { + for (var f = Array(s), b = 0; b < s; ++b) f[b] = kn(a()[p + 4 * b >>> 2 >>> 0], "parameter " + b); + return f; + }, wi = (s, p) => Object.defineProperty(p, "name", { value: s }); + function Gc(s, p, f) { + var b = (p = Hc(s, p >>> 0)).shift(); + s--; + var _ = `return function (obj, func, destructorsRef, args) { + `, I = 0, O = []; + f === 0 && O.push("obj"); + for (var D = ["retType"], L = [b], H = 0; H < s; ++H) O.push("arg" + H), D.push("argType" + H), L.push(p[H]), _ += ` var arg${H} = argType${H}.readValueFromPointer(args${I ? "+" + I : ""}); + `, I += p[H].argPackAdvance; + return _ += ` var rv = ${f === 1 ? "new func" : "func.call"}(${O.join(", ")}); + `, b.Tb || (D.push("emval_returnValue"), L.push(mi), _ += ` return emval_returnValue(retType, destructorsRef, rv); + `), D.push(_ + `}; + `), s = function(X) { + var fe = Function; + if (!(fe instanceof Function)) throw new TypeError(`new_ called with constructor type ${typeof fe} which is not a function`); + var be = wi(fe.name || "unknownFunctionName", function() { + }); + return be.prototype = fe.prototype, be = new be(), (X = fe.apply(be, X)) instanceof Object ? X : be; + }(D)(...L), f = `methodCaller<(${p.map((X) => X.name).join(", ")}) => ${b.name}>`, Lc(wi(f, s)); + } + function Fc(s) { + return s = cr(s >>> 0), je(u[s]); + } + function qc(s, p) { + return p >>>= 0, s = qe(s >>> 0), p = qe(p), je(s[p]); + } + function jc(s) { + 9 < (s >>>= 0) && (dt[s + 1] += 1); + } + function Kc() { + return je([]); + } + function Yc(s) { + s = qe(s >>> 0); + for (var p = Array(s.length), f = 0; f < s.length; f++) p[f] = s[f]; + return je(p); + } + function Xc(s) { + return je(cr(s >>> 0)); + } + function Zc() { + return je({}); + } + function Qc(s) { + for (var p = qe(s >>>= 0); p.length; ) { + var f = p.pop(); + p.pop()(f); + } + Tn(s); + } + function Jc(s, p, f) { + p >>>= 0, f >>>= 0, s = qe(s >>> 0), p = qe(p), f = qe(f), s[p] = f; + } + function ep(s, p) { + return p >>>= 0, s = (s = kn(s >>> 0, "_emval_take_value")).readValueFromPointer(p), je(s); + } + function tp(s, p) { + s = -9007199254740992 > s || 9007199254740992 < s ? NaN : Number(s), p >>>= 0, s = new Date(1e3 * s), i()[p >>> 2 >>> 0] = s.getUTCSeconds(), i()[p + 4 >>> 2 >>> 0] = s.getUTCMinutes(), i()[p + 8 >>> 2 >>> 0] = s.getUTCHours(), i()[p + 12 >>> 2 >>> 0] = s.getUTCDate(), i()[p + 16 >>> 2 >>> 0] = s.getUTCMonth(), i()[p + 20 >>> 2 >>> 0] = s.getUTCFullYear() - 1900, i()[p + 24 >>> 2 >>> 0] = s.getUTCDay(), s = (s.getTime() - Date.UTC(s.getUTCFullYear(), 0, 1, 0, 0, 0, 0)) / 864e5 | 0, i()[p + 28 >>> 2 >>> 0] = s; + } + var Dt = (s) => s % 4 == 0 && (s % 100 != 0 || s % 400 == 0), _i = [0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335], vi = [0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334]; + function rp(s, p) { + s = -9007199254740992 > s || 9007199254740992 < s ? NaN : Number(s), p >>>= 0, s = new Date(1e3 * s), i()[p >>> 2 >>> 0] = s.getSeconds(), i()[p + 4 >>> 2 >>> 0] = s.getMinutes(), i()[p + 8 >>> 2 >>> 0] = s.getHours(), i()[p + 12 >>> 2 >>> 0] = s.getDate(), i()[p + 16 >>> 2 >>> 0] = s.getMonth(), i()[p + 20 >>> 2 >>> 0] = s.getFullYear() - 1900, i()[p + 24 >>> 2 >>> 0] = s.getDay(); + var f = (Dt(s.getFullYear()) ? _i : vi)[s.getMonth()] + s.getDate() - 1 | 0; + i()[p + 28 >>> 2 >>> 0] = f, i()[p + 36 >>> 2 >>> 0] = -60 * s.getTimezoneOffset(), f = new Date(s.getFullYear(), 6, 1).getTimezoneOffset(); + var b = new Date(s.getFullYear(), 0, 1).getTimezoneOffset(); + s = 0 | (f != b && s.getTimezoneOffset() == Math.min(b, f)), i()[p + 32 >>> 2 >>> 0] = s; + } + function np(s) { + s >>>= 0; + var p = new Date(i()[s + 20 >>> 2 >>> 0] + 1900, i()[s + 16 >>> 2 >>> 0], i()[s + 12 >>> 2 >>> 0], i()[s + 8 >>> 2 >>> 0], i()[s + 4 >>> 2 >>> 0], i()[s >>> 2 >>> 0], 0), f = i()[s + 32 >>> 2 >>> 0], b = p.getTimezoneOffset(), _ = new Date(p.getFullYear(), 6, 1).getTimezoneOffset(), I = new Date(p.getFullYear(), 0, 1).getTimezoneOffset(), O = Math.min(I, _); + return 0 > f ? i()[s + 32 >>> 2 >>> 0] = +(_ != I && O == b) : 0 < f != (O == b) && (_ = Math.max(I, _), p.setTime(p.getTime() + 6e4 * ((0 < f ? O : _) - b))), i()[s + 24 >>> 2 >>> 0] = p.getDay(), f = (Dt(p.getFullYear()) ? _i : vi)[p.getMonth()] + p.getDate() - 1 | 0, i()[s + 28 >>> 2 >>> 0] = f, i()[s >>> 2 >>> 0] = p.getSeconds(), i()[s + 4 >>> 2 >>> 0] = p.getMinutes(), i()[s + 8 >>> 2 >>> 0] = p.getHours(), i()[s + 12 >>> 2 >>> 0] = p.getDate(), i()[s + 16 >>> 2 >>> 0] = p.getMonth(), i()[s + 20 >>> 2 >>> 0] = p.getYear(), s = p.getTime(), BigInt(isNaN(s) ? -1 : s / 1e3); + } + function $i(s, p, f, b, _, I, O) { + return y ? xe(16, 1, s, p, f, b, _, I, O) : -52; + } + function xi(s, p, f, b, _, I) { + if (y) return xe(17, 1, s, p, f, b, _, I); + } + function op(s, p, f, b) { + s >>>= 0, p >>>= 0, f >>>= 0, b >>>= 0; + var _ = (/* @__PURE__ */ new Date()).getFullYear(), I = new Date(_, 0, 1), O = new Date(_, 6, 1); + _ = I.getTimezoneOffset(); + var D = O.getTimezoneOffset(), L = Math.max(_, D); + a()[s >>> 2 >>> 0] = 60 * L, i()[p >>> 2 >>> 0] = +(_ != D), I = (s = (H) => H.toLocaleTimeString(void 0, { hour12: false, timeZoneName: "short" }).split(" ")[1])(I), O = s(O), D < _ ? (Bt(I, f, 17), Bt(O, b, 17)) : (Bt(I, b, 17), Bt(O, f, 17)); + } + var Pn = [], Si = (s, p) => { + Pn.length = 0; + for (var f; f = r()[s++ >>> 0]; ) { + var b = f != 105; + p += (b &= f != 112) && p % 8 ? 4 : 0, Pn.push(f == 112 ? a()[p >>> 2 >>> 0] : f == 106 ? G[p >>> 3] : f == 105 ? i()[p >>> 2 >>> 0] : l()[p >>> 3 >>> 0]), p += b ? 8 : 4; + } + return Pn; + }; + function ip(s, p, f) { + return s >>>= 0, p = Si(p >>> 0, f >>> 0), gn[s](...p); + } + function ap(s, p, f) { + return s >>>= 0, p = Si(p >>> 0, f >>> 0), gn[s](...p); + } + var sp = () => { + }, up = () => Date.now(); + function dp(s, p) { + return Z(ze(s >>> 0, p >>> 0)); + } + var Ti, lp = () => { + throw _t += 1, "unwind"; + }; + function cp() { + return 4294901760; + } + Ti = () => performance.timeOrigin + performance.now(); + var pp = () => navigator.hardwareConcurrency; + function mp() { + return Ot("Cannot use emscripten_pc_get_function without -sUSE_OFFSET_CONVERTER"), 0; + } + function fp(s) { + s >>>= 0; + var p = r().length; + if (s <= p || 4294901760 < s) return false; + for (var f = 1; 4 >= f; f *= 2) { + var b = p * (1 + 0.2 / f); + b = Math.min(b, s + 100663296); + var _ = Math; + b = Math.max(s, b); + e: { + _ = (_.min.call(_, 4294901760, b + (65536 - b % 65536) % 65536) - de.buffer.byteLength + 65535) / 65536; + try { + de.grow(_), Ce(); + var I = 1; + break e; + } catch { + } + I = void 0; + } + if (I) return true; + } + return false; + } + var pr = () => (Ot("Cannot use convertFrameToPC (needed by __builtin_return_address) without -sUSE_OFFSET_CONVERTER"), 0), Mt = {}, Ii = (s) => { + s.forEach((p) => { + var f = pr(); + f && (Mt[f] = p); + }); + }; + function hp() { + var s = Error().stack.toString().split(` + `); + return s[0] == "Error" && s.shift(), Ii(s), Mt.Pb = pr(), Mt.ec = s, Mt.Pb; + } + function gp(s, p, f) { + if (s >>>= 0, p >>>= 0, Mt.Pb == s) var b = Mt.ec; + else (b = Error().stack.toString().split(` + `))[0] == "Error" && b.shift(), Ii(b); + for (var _ = 3; b[_] && pr() != s; ) ++_; + for (s = 0; s < f && b[s + _]; ++s) i()[p + 4 * s >>> 2 >>> 0] = pr(); + return s; + } + var zn, On = {}, Ci = () => { + if (!zn) { + var s, p = { USER: "web_user", LOGNAME: "web_user", PATH: "/", PWD: "/", HOME: "/home/web_user", LANG: (typeof navigator == "object" && navigator.languages && navigator.languages[0] || "C").replace("-", "_") + ".UTF-8", _: C || "./this.program" }; + for (s in On) On[s] === void 0 ? delete p[s] : p[s] = On[s]; + var f = []; + for (s in p) f.push(`${s}=${p[s]}`); + zn = f; + } + return zn; + }; + function Ai(s, p) { + if (y) return xe(18, 1, s, p); + s >>>= 0, p >>>= 0; + var f = 0; + return Ci().forEach((b, _) => { + var I = p + f; + for (_ = a()[s + 4 * _ >>> 2 >>> 0] = I, I = 0; I < b.length; ++I) t()[_++ >>> 0] = b.charCodeAt(I); + t()[_ >>> 0] = 0, f += b.length + 1; + }), 0; + } + function ki(s, p) { + if (y) return xe(19, 1, s, p); + s >>>= 0, p >>>= 0; + var f = Ci(); + a()[s >>> 2 >>> 0] = f.length; + var b = 0; + return f.forEach((_) => b += _.length + 1), a()[p >>> 2 >>> 0] = b, 0; + } + function Ei(s) { + return y ? xe(20, 1, s) : 52; + } + function Pi(s, p, f, b) { + return y ? xe(21, 1, s, p, f, b) : 52; + } + function zi(s, p, f, b) { + return y ? xe(22, 1, s, p, f, b) : 70; + } + var yp = [null, [], []]; + function Oi(s, p, f, b) { + if (y) return xe(23, 1, s, p, f, b); + p >>>= 0, f >>>= 0, b >>>= 0; + for (var _ = 0, I = 0; I < f; I++) { + var O = a()[p >>> 2 >>> 0], D = a()[p + 4 >>> 2 >>> 0]; + p += 8; + for (var L = 0; L < D; L++) { + var H = r()[O + L >>> 0], X = yp[s]; + H === 0 || H === 10 ? ((s === 1 ? K : Z)(Ko(X, 0)), X.length = 0) : X.push(H); + } + _ += D; + } + return a()[b >>> 2 >>> 0] = _, 0; + } + var Bi = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31], Di = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31], bp = (s, p) => { + t().set(s, p >>> 0); + }; + function Mi(s, p, f, b) { + function _(z, pe, Se) { + for (z = typeof z == "number" ? z.toString() : z || ""; z.length < pe; ) z = Se[0] + z; + return z; + } + function I(z, pe) { + return _(z, pe, "0"); + } + function O(z, pe) { + function Se(Xi) { + return 0 > Xi ? -1 : 0 < Xi ? 1 : 0; + } + var $t; + return ($t = Se(z.getFullYear() - pe.getFullYear())) === 0 && ($t = Se(z.getMonth() - pe.getMonth())) === 0 && ($t = Se(z.getDate() - pe.getDate())), $t; + } + function D(z) { + switch (z.getDay()) { + case 0: + return new Date(z.getFullYear() - 1, 11, 29); + case 1: + return z; + case 2: + return new Date(z.getFullYear(), 0, 3); + case 3: + return new Date(z.getFullYear(), 0, 2); + case 4: + return new Date(z.getFullYear(), 0, 1); + case 5: + return new Date(z.getFullYear() - 1, 11, 31); + case 6: + return new Date(z.getFullYear() - 1, 11, 30); + } + } + function L(z) { + var pe = z.Bb; + for (z = new Date(new Date(z.Cb + 1900, 0, 1).getTime()); 0 < pe; ) { + var Se = z.getMonth(), $t = (Dt(z.getFullYear()) ? Bi : Di)[Se]; + if (!(pe > $t - z.getDate())) { + z.setDate(z.getDate() + pe); + break; + } + pe -= $t - z.getDate() + 1, z.setDate(1), 11 > Se ? z.setMonth(Se + 1) : (z.setMonth(0), z.setFullYear(z.getFullYear() + 1)); + } + return Se = new Date(z.getFullYear() + 1, 0, 4), pe = D(new Date(z.getFullYear(), 0, 4)), Se = D(Se), 0 >= O(pe, z) ? 0 >= O(Se, z) ? z.getFullYear() + 1 : z.getFullYear() : z.getFullYear() - 1; + } + s >>>= 0, p >>>= 0, f >>>= 0, b >>>= 0; + var H = a()[b + 40 >>> 2 >>> 0]; + for (var X in b = { kc: i()[b >>> 2 >>> 0], jc: i()[b + 4 >>> 2 >>> 0], Hb: i()[b + 8 >>> 2 >>> 0], Lb: i()[b + 12 >>> 2 >>> 0], Ib: i()[b + 16 >>> 2 >>> 0], Cb: i()[b + 20 >>> 2 >>> 0], ub: i()[b + 24 >>> 2 >>> 0], Bb: i()[b + 28 >>> 2 >>> 0], rc: i()[b + 32 >>> 2 >>> 0], ic: i()[b + 36 >>> 2 >>> 0], lc: H ? ze(H) : "" }, f = ze(f), H = { "%c": "%a %b %d %H:%M:%S %Y", "%D": "%m/%d/%y", "%F": "%Y-%m-%d", "%h": "%b", "%r": "%I:%M:%S %p", "%R": "%H:%M", "%T": "%H:%M:%S", "%x": "%m/%d/%y", "%X": "%H:%M:%S", "%Ec": "%c", "%EC": "%C", "%Ex": "%m/%d/%y", "%EX": "%H:%M:%S", "%Ey": "%y", "%EY": "%Y", "%Od": "%d", "%Oe": "%e", "%OH": "%H", "%OI": "%I", "%Om": "%m", "%OM": "%M", "%OS": "%S", "%Ou": "%u", "%OU": "%U", "%OV": "%V", "%Ow": "%w", "%OW": "%W", "%Oy": "%y" }) f = f.replace(new RegExp(X, "g"), H[X]); + var fe = "Sunday Monday Tuesday Wednesday Thursday Friday Saturday".split(" "), be = "January February March April May June July August September October November December".split(" "); + for (X in H = { "%a": (z) => fe[z.ub].substring(0, 3), "%A": (z) => fe[z.ub], "%b": (z) => be[z.Ib].substring(0, 3), "%B": (z) => be[z.Ib], "%C": (z) => I((z.Cb + 1900) / 100 | 0, 2), "%d": (z) => I(z.Lb, 2), "%e": (z) => _(z.Lb, 2, " "), "%g": (z) => L(z).toString().substring(2), "%G": L, "%H": (z) => I(z.Hb, 2), "%I": (z) => ((z = z.Hb) == 0 ? z = 12 : 12 < z && (z -= 12), I(z, 2)), "%j": (z) => { + for (var pe = 0, Se = 0; Se <= z.Ib - 1; pe += (Dt(z.Cb + 1900) ? Bi : Di)[Se++]) ; + return I(z.Lb + pe, 3); + }, "%m": (z) => I(z.Ib + 1, 2), "%M": (z) => I(z.jc, 2), "%n": () => ` + `, "%p": (z) => 0 <= z.Hb && 12 > z.Hb ? "AM" : "PM", "%S": (z) => I(z.kc, 2), "%t": () => " ", "%u": (z) => z.ub || 7, "%U": (z) => I(Math.floor((z.Bb + 7 - z.ub) / 7), 2), "%V": (z) => { + var pe = Math.floor((z.Bb + 7 - (z.ub + 6) % 7) / 7); + if (2 >= (z.ub + 371 - z.Bb - 2) % 7 && pe++, pe) pe == 53 && ((Se = (z.ub + 371 - z.Bb) % 7) == 4 || Se == 3 && Dt(z.Cb) || (pe = 1)); + else { + pe = 52; + var Se = (z.ub + 7 - z.Bb - 1) % 7; + (Se == 4 || Se == 5 && Dt(z.Cb % 400 - 1)) && pe++; + } + return I(pe, 2); + }, "%w": (z) => z.ub, "%W": (z) => I(Math.floor((z.Bb + 7 - (z.ub + 6) % 7) / 7), 2), "%y": (z) => (z.Cb + 1900).toString().substring(2), "%Y": (z) => z.Cb + 1900, "%z": (z) => { + var pe = 0 <= (z = z.ic); + return z = Math.abs(z) / 60, (pe ? "+" : "-") + ("0000" + (z / 60 * 100 + z % 60)).slice(-4); + }, "%Z": (z) => z.lc, "%%": () => "%" }, f = f.replace(/%%/g, "\0\0"), H) f.includes(X) && (f = f.replace(new RegExp(X, "g"), H[X](b))); + return X = function(z) { + var pe = Array(vn(z) + 1); + return Zo(z, pe, 0, pe.length), pe; + }(f = f.replace(/\0\0/g, "%")), X.length > p ? 0 : (bp(X, s), X.length - 1); + } + function wp(s, p, f, b) { + return Mi(s >>> 0, p >>> 0, f >>> 0, b >>> 0); + } + y || function() { + for (var s = u.numThreads - 1; s--; ) Go(); + bt.unshift(() => { + Ue++, function(p) { + y ? p() : Promise.all(pt.map(Ho)).then(p); + }(() => zo()); + }); + }(); + for (var Ri = Array(256), mr = 0; 256 > mr; ++mr) Ri[mr] = String.fromCharCode(mr); + di = Ri, mt = u.BindingError = class extends Error { + constructor(s) { + super(s), this.name = "BindingError"; + } + }, u.InternalError = class extends Error { + constructor(s) { + super(s), this.name = "InternalError"; + } + }, dt.push(0, 1, void 0, 1, null, 1, true, 1, false, 1), u.count_emval_handles = () => dt.length / 2 - 5 - Sn.length; + var _p = [wn, Vo, Fo, Yo, Xo, Qo, Jo, ei, ti, ri, ni, oi, ii, ai, si, ui, $i, xi, Ai, ki, Ei, Pi, zi, Oi], j = function() { + function s(f, b) { + return j = f.exports, j = function() { + var _ = j, I = {}; + for (let [O, D] of Object.entries(_)) I[O] = typeof D == "function" ? (...L) => { + dr.push(O); + try { + return D(...L); + } finally { + $e || (dr.pop(), et && ht === 1 && dr.length === 0 && (ht = 0, _t += 1, ur(qi), typeof Fibers < "u" && Fibers.sc())); + } + } : D; + return I; + }(), j = function() { + var _ = j, I = (D) => (L) => D(L) >>> 0, O = (D) => () => D() >>> 0; + return (_ = Object.assign({}, _)).Ca = I(_.Ca), _.fb = O(_.fb), _.gb = I(_.gb), _.emscripten_main_runtime_thread_id = O(_.emscripten_main_runtime_thread_id), _.sb = I(_.sb), _.tb = O(_.tb), _; + }(), No.push(j.ib), Ae.unshift(j.Ba), Y = b, zo(), j; + } + var p = Ro(); + if (Ue++, u.instantiateWasm) try { + return u.instantiateWasm(p, s); + } catch (f) { + Z(`Module.instantiateWasm callback failed with error: ${f}`), m(f); + } + return hn || (hn = u.locateFile ? Oo("ort-wasm-simd-threaded.jsep.wasm") ? "ort-wasm-simd-threaded.jsep.wasm" : u.locateFile ? u.locateFile("ort-wasm-simd-threaded.jsep.wasm", P) : P + "ort-wasm-simd-threaded.jsep.wasm" : new URL( + /* asset import */ + __webpack_require__2( + /*! ort-wasm-simd-threaded.jsep.wasm */ + "./node_modules/onnxruntime-web/dist/ort-wasm-simd-threaded.jsep.wasm" + ), + __webpack_require__2.b + ).href), function(f, b) { + var _ = hn; + return B || typeof WebAssembly.instantiateStreaming != "function" || Oo(_) || Bo(_) || typeof fetch != "function" ? Mo(_, f, b) : fetch(_, { credentials: "same-origin" }).then((I) => WebAssembly.instantiateStreaming(I, f).then(b, function(O) { + return Z(`wasm streaming compile failed: ${O}`), Z("falling back to ArrayBuffer instantiation"), Mo(_, f, b); + })); + }(p, function(f) { + s(f.instance, f.module); + }).catch(m), {}; + }(), Ui = (s) => (Ui = j.Ca)(s), Vi = () => (Vi = j.Da)(); + u._OrtInit = (s, p) => (u._OrtInit = j.Ea)(s, p), u._OrtGetLastError = (s, p) => (u._OrtGetLastError = j.Fa)(s, p), u._OrtCreateSessionOptions = (s, p, f, b, _, I, O, D, L, H) => (u._OrtCreateSessionOptions = j.Ga)(s, p, f, b, _, I, O, D, L, H), u._OrtAppendExecutionProvider = (s, p) => (u._OrtAppendExecutionProvider = j.Ha)(s, p), u._OrtAddFreeDimensionOverride = (s, p, f) => (u._OrtAddFreeDimensionOverride = j.Ia)(s, p, f), u._OrtAddSessionConfigEntry = (s, p, f) => (u._OrtAddSessionConfigEntry = j.Ja)(s, p, f), u._OrtReleaseSessionOptions = (s) => (u._OrtReleaseSessionOptions = j.Ka)(s), u._OrtCreateSession = (s, p, f) => (u._OrtCreateSession = j.La)(s, p, f), u._OrtReleaseSession = (s) => (u._OrtReleaseSession = j.Ma)(s), u._OrtGetInputOutputCount = (s, p, f) => (u._OrtGetInputOutputCount = j.Na)(s, p, f), u._OrtGetInputName = (s, p) => (u._OrtGetInputName = j.Oa)(s, p), u._OrtGetOutputName = (s, p) => (u._OrtGetOutputName = j.Pa)(s, p), u._OrtFree = (s) => (u._OrtFree = j.Qa)(s), u._OrtCreateTensor = (s, p, f, b, _, I) => (u._OrtCreateTensor = j.Ra)(s, p, f, b, _, I), u._OrtGetTensorData = (s, p, f, b, _) => (u._OrtGetTensorData = j.Sa)(s, p, f, b, _), u._OrtReleaseTensor = (s) => (u._OrtReleaseTensor = j.Ta)(s), u._OrtCreateRunOptions = (s, p, f, b) => (u._OrtCreateRunOptions = j.Ua)(s, p, f, b), u._OrtAddRunConfigEntry = (s, p, f) => (u._OrtAddRunConfigEntry = j.Va)(s, p, f), u._OrtReleaseRunOptions = (s) => (u._OrtReleaseRunOptions = j.Wa)(s), u._OrtCreateBinding = (s) => (u._OrtCreateBinding = j.Xa)(s), u._OrtBindInput = (s, p, f) => (u._OrtBindInput = j.Ya)(s, p, f), u._OrtBindOutput = (s, p, f, b) => (u._OrtBindOutput = j.Za)(s, p, f, b), u._OrtClearBoundOutputs = (s) => (u._OrtClearBoundOutputs = j._a)(s), u._OrtReleaseBinding = (s) => (u._OrtReleaseBinding = j.$a)(s), u._OrtRunWithBinding = (s, p, f, b, _) => (u._OrtRunWithBinding = j.ab)(s, p, f, b, _), u._OrtRun = (s, p, f, b, _, I, O, D) => (u._OrtRun = j.bb)(s, p, f, b, _, I, O, D), u._OrtEndProfiling = (s) => (u._OrtEndProfiling = j.cb)(s), u._JsepOutput = (s, p, f) => (u._JsepOutput = j.db)(s, p, f), u._JsepGetNodeName = (s) => (u._JsepGetNodeName = j.eb)(s); + var fr, Rt = () => (Rt = j.fb)(), hr = u._malloc = (s) => (hr = u._malloc = j.gb)(s), tt = u._free = (s) => (tt = u._free = j.hb)(s), Bn = (s, p, f, b, _, I) => (Bn = j.kb)(s, p, f, b, _, I), Ni = () => (Ni = j.lb)(), Wi = (s, p, f, b, _) => (Wi = j.mb)(s, p, f, b, _), Dn = (s) => (Dn = j.nb)(s), gr = (s) => (gr = j.ob)(s), Li = () => (Li = j.pb)(), Hi = (s, p) => (Hi = j.qb)(s, p), yr = (s) => (yr = j.rb)(s), Mn = (s) => (Mn = j.sb)(s), Rn = () => (Rn = j.tb)(), Gi = u.dynCall_ii = (s, p) => (Gi = u.dynCall_ii = j.vb)(s, p), Fi = (s) => (Fi = j.wb)(s), qi = () => (qi = j.xb)(), ji = (s) => (ji = j.yb)(s), Ki = () => (Ki = j.zb)(); + function Yi() { + 0 < Ue || (y ? (c(u), y || ar(Ae), startWorker(u)) : (ar(bt), 0 < Ue || fr || (fr = true, u.calledRun = true, $e || (y || ar(Ae), c(u), y || ar(Me))))); + } + return u.___start_em_js = 881730, u.___stop_em_js = 881952, u.stackSave = () => Rn(), u.stackRestore = (s) => yr(s), u.stackAlloc = (s) => Mn(s), u.UTF8ToString = ze, u.stringToUTF8 = Bt, u.lengthBytesUTF8 = vn, wt = function s() { + fr || Yi(), fr || (wt = s); + }, Yi(), h; + }), Ep = Pa; + ((_a2 = globalThis.self) == null ? void 0 : _a2.name) === "em-pthread" && Pa(); + }); + var Ut, Pp, zp, Op, Ba, Da, Bp, Ma, qt = V(() => { + "use strict"; + var _a2, _b; + Cr(); + Ut = false ? 0 : META_URL ?? (typeof document < "u" ? (_a2 = document.currentScript) == null ? void 0 : _a2.src : typeof self < "u" ? (_b = self.location) == null ? void 0 : _b.href : void 0), Pp = typeof location > "u" ? void 0 : location.origin, zp = (e, t) => { + try { + let r = t ?? Ut; + return (r ? new URL(e, r) : new URL(e)).origin === Pp; + } catch { + return false; + } + }, Op = async (e) => { + let r = await (await fetch(e, { credentials: "same-origin" })).blob(); + return URL.createObjectURL(r); + }, Ba = (Ea(), br(ka)).default, Da = async () => { + if (!Ut) throw new Error("Failed to load proxy worker: cannot determine the script source URL."); + if (zp(Ut)) return [void 0, Ba()]; + let e = await Op(Ut); + return [e, Ba(e)]; + }, Bp = (Oa(), br(za)).default, Ma = async (e, t, r) => [void 0, Bp]; + }); + var qn, jn, Mr, Ra, Dp, Mp, Ar, Te, gt = V(() => { + "use strict"; + qt(); + jn = false, Mr = false, Ra = false, Dp = () => { + if (typeof SharedArrayBuffer > "u") return false; + 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 false; + } + }, Mp = () => { + try { + return WebAssembly.validate(new Uint8Array([0, 97, 115, 109, 1, 0, 0, 0, 1, 4, 1, 96, 0, 0, 3, 2, 1, 0, 10, 30, 1, 28, 0, 65, 0, 253, 15, 253, 12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 253, 186, 1, 26, 11])); + } catch { + return false; + } + }, Ar = async (e) => { + if (jn) return Promise.resolve(); + if (Mr) throw new Error("multiple calls to 'initializeWebAssembly()' detected."); + if (Ra) throw new Error("previous call to 'initializeWebAssembly()' failed."); + Mr = true; + let t = e.initTimeout, r = e.numThreads; + if (!Mp()) throw new Error("WebAssembly SIMD is not supported in the current environment."); + let n = Dp(); + r > 1 && !n && (typeof self < "u" && !self.crossOriginIsolated && console.warn("env.wasm.numThreads is set to " + r + ", but this will not work unless you enable crossOriginIsolated mode. See https://web.dev/cross-origin-isolation-guide/ for more info."), console.warn("WebAssembly multi-threading is not supported in the current environment. Falling back to single-threading."), e.numThreads = r = 1); + let o = e.wasmPaths, i = typeof o == "string" ? o : void 0, a = o == null ? void 0 : o.mjs, d = (a == null ? void 0 : a.href) ?? a, l = o == null ? void 0 : o.wasm, c = (l == null ? void 0 : l.href) ?? l, m = e.wasmBinary, [u, h] = await Ma(d, i, r > 1), w = false, g = []; + if (t > 0 && g.push(new Promise((y) => { + setTimeout(() => { + w = true, y(); + }, t); + })), g.push(new Promise((y, S) => { + let $ = { numThreads: r }; + m ? $.wasmBinary = m : (c || i) && ($.locateFile = (v, x) => c ?? (i ?? x) + v), h($).then((v) => { + Mr = false, jn = true, qn = v, y(), u && URL.revokeObjectURL(u); + }, (v) => { + Mr = false, Ra = true, S(v); + }); + })), await Promise.race(g), w) throw new Error(`WebAssembly backend initializing failed due to timeout: ${t}ms`); + }, Te = () => { + if (jn && qn) return qn; + throw new Error("WebAssembly is not initialized yet."); + }; + }); + var ke, Kt, ve, Rr = V(() => { + "use strict"; + gt(); + ke = (e, t) => { + let r = Te(), n = r.lengthBytesUTF8(e) + 1, o = r._malloc(n); + return r.stringToUTF8(e, o, n), t.push(o), o; + }, Kt = (e, t, r, n) => { + if (typeof e == "object" && e !== null) { + if (r.has(e)) throw new Error("Circular reference in options"); + r.add(e); + } + Object.entries(e).forEach(([o, i]) => { + let a = t ? t + o : o; + if (typeof i == "object") Kt(i, a + ".", r, n); + else if (typeof i == "string" || typeof i == "number") n(a, i.toString()); + else if (typeof i == "boolean") n(a, i ? "1" : "0"); + else throw new Error(`Can't handle extra config type: ${typeof i}`); + }); + }, ve = (e) => { + let t = Te(), r = t.stackSave(); + try { + let n = t.stackAlloc(8); + t._OrtGetLastError(n, n + 4); + let o = t.HEAP32[n / 4], i = t.HEAPU32[n / 4 + 1], a = i ? t.UTF8ToString(i) : ""; + throw new Error(`${e} ERROR_CODE: ${o}, ERROR_MESSAGE: ${a}`); + } finally { + t.stackRestore(r); + } + }; + }); + var Ua, Va = V(() => { + "use strict"; + gt(); + Rr(); + Ua = (e) => { + let t = Te(), r = 0, n = [], o = e || {}; + try { + if ((e == null ? void 0 : 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 == null ? void 0 : 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 == null ? void 0 : e.terminate) === void 0 && (o.terminate = false); + let i = 0; + return (e == null ? void 0 : e.tag) !== void 0 && (i = ke(e.tag, n)), r = t._OrtCreateRunOptions(o.logSeverityLevel, o.logVerbosityLevel, !!o.terminate, i), r === 0 && ve("Can't create run options."), (e == null ? void 0 : e.extra) !== void 0 && Kt(e.extra, "", /* @__PURE__ */ new WeakSet(), (a, d) => { + let l = ke(a, n), c = ke(d, n); + t._OrtAddRunConfigEntry(r, l, c) !== 0 && ve(`Can't set a run config entry: ${a} - ${d}.`); + }), [r, n]; + } catch (i) { + throw r !== 0 && t._OrtReleaseRunOptions(r), n.forEach((a) => t._free(a)), i; + } + }; + }); + var Rp, Up, Vp, Np, Na, Wa = V(() => { + "use strict"; + gt(); + Rr(); + Rp = (e) => { + switch (e) { + case "disabled": + return 0; + case "basic": + return 1; + case "extended": + return 2; + case "all": + return 99; + default: + throw new Error(`unsupported graph optimization level: ${e}`); + } + }, Up = (e) => { + switch (e) { + case "sequential": + return 0; + case "parallel": + return 1; + default: + throw new Error(`unsupported execution mode: ${e}`); + } + }, Vp = (e) => { + e.extra || (e.extra = {}), e.extra.session || (e.extra.session = {}); + let t = e.extra.session; + t.use_ort_model_bytes_directly || (t.use_ort_model_bytes_directly = "1"), e.executionProviders && e.executionProviders.some((r) => (typeof r == "string" ? r : r.name) === "webgpu") && (e.enableMemPattern = false); + }, Np = (e, t, r) => { + for (let n of t) { + let o = typeof n == "string" ? n : n.name; + switch (o) { + case "webnn": + if (o = "WEBNN", typeof n != "string") { + let d = n == null ? void 0 : n.deviceType; + if (d) { + let l = ke("deviceType", r), c = ke(d, r); + Te()._OrtAddSessionConfigEntry(e, l, c) !== 0 && ve(`Can't set a session config entry: 'deviceType' - ${d}.`); + } + } + break; + case "webgpu": + if (o = "JS", typeof n != "string") { + let a = n; + if (a == null ? void 0 : a.preferredLayout) { + if (a.preferredLayout !== "NCHW" && a.preferredLayout !== "NHWC") throw new Error(`preferredLayout must be either 'NCHW' or 'NHWC': ${a.preferredLayout}`); + let d = ke("preferredLayout", r), l = ke(a.preferredLayout, r); + Te()._OrtAddSessionConfigEntry(e, d, l) !== 0 && ve(`Can't set a session config entry: 'preferredLayout' - ${a.preferredLayout}.`); + } + } + break; + case "wasm": + case "cpu": + continue; + default: + throw new Error(`not supported execution provider: ${o}`); + } + let i = ke(o, r); + Te()._OrtAppendExecutionProvider(e, i) !== 0 && ve(`Can't append execution provider: ${o}.`); + } + }, Na = (e) => { + let t = Te(), r = 0, n = [], o = e || {}; + Vp(o); + try { + let i = Rp(o.graphOptimizationLevel ?? "all"), a = Up(o.executionMode ?? "sequential"), d = typeof o.logId == "string" ? ke(o.logId, n) : 0, l = o.logSeverityLevel ?? 2; + if (!Number.isInteger(l) || l < 0 || l > 4) throw new Error(`log serverity level is not valid: ${l}`); + let c = o.logVerbosityLevel ?? 0; + if (!Number.isInteger(c) || c < 0 || c > 4) throw new Error(`log verbosity level is not valid: ${c}`); + let m = typeof o.optimizedModelFilePath == "string" ? ke(o.optimizedModelFilePath, n) : 0; + if (r = t._OrtCreateSessionOptions(i, !!o.enableCpuMemArena, !!o.enableMemPattern, a, !!o.enableProfiling, 0, d, l, c, m), r === 0 && ve("Can't create session options."), o.executionProviders && Np(r, o.executionProviders, n), o.enableGraphCapture !== void 0) { + if (typeof o.enableGraphCapture != "boolean") throw new Error(`enableGraphCapture must be a boolean value: ${o.enableGraphCapture}`); + let u = ke("enableGraphCapture", n), h = ke(o.enableGraphCapture.toString(), n); + t._OrtAddSessionConfigEntry(r, u, h) !== 0 && ve(`Can't set a session config entry: 'enableGraphCapture' - ${o.enableGraphCapture}.`); + } + if (o.freeDimensionOverrides) for (let [u, h] of Object.entries(o.freeDimensionOverrides)) { + if (typeof u != "string") throw new Error(`free dimension override name must be a string: ${u}`); + if (typeof h != "number" || !Number.isInteger(h) || h < 0) throw new Error(`free dimension override value must be a non-negative integer: ${h}`); + let w = ke(u, n); + t._OrtAddFreeDimensionOverride(r, w, h) !== 0 && ve(`Can't set a free dimension override: ${u} - ${h}.`); + } + return o.extra !== void 0 && Kt(o.extra, "", /* @__PURE__ */ new WeakSet(), (u, h) => { + let w = ke(u, n), g = ke(h, n); + t._OrtAddSessionConfigEntry(r, w, g) !== 0 && ve(`Can't set a session config entry: ${u} - ${h}.`); + }), [r, n]; + } catch (i) { + throw r !== 0 && t._OrtReleaseSessionOptions(r), n.forEach((a) => t._free(a)), i; + } + }; + }); + var Yt, yt, It, Ur, Xt, Vr, Nr, Kn, Q = V(() => { + "use strict"; + Yt = (e) => { + switch (e) { + case "int8": + return 3; + case "uint8": + return 2; + case "bool": + return 9; + case "int16": + return 5; + case "uint16": + return 4; + case "int32": + return 6; + case "uint32": + return 12; + case "float16": + return 10; + case "float32": + return 1; + case "float64": + return 11; + case "string": + return 8; + case "int64": + return 7; + case "uint64": + return 13; + case "int4": + return 22; + case "uint4": + return 21; + default: + throw new Error(`unsupported data type: ${e}`); + } + }, yt = (e) => { + switch (e) { + case 3: + return "int8"; + case 2: + return "uint8"; + case 9: + return "bool"; + case 5: + return "int16"; + case 4: + return "uint16"; + case 6: + return "int32"; + case 12: + return "uint32"; + case 10: + return "float16"; + case 1: + return "float32"; + case 11: + return "float64"; + case 8: + return "string"; + case 7: + return "int64"; + case 13: + return "uint64"; + case 22: + return "int4"; + case 21: + return "uint4"; + default: + throw new Error(`unsupported data type: ${e}`); + } + }, It = (e, t) => { + let r = [-1, 4, 1, 1, 2, 2, 4, 8, -1, 1, 2, 8, 4, 8, -1, -1, -1, -1, -1, -1, -1, 0.5, 0.5][e], n = typeof t == "number" ? t : t.reduce((o, i) => o * i, 1); + return r > 0 ? Math.ceil(n * r) : void 0; + }, Ur = (e) => { + switch (e) { + case "float16": + return typeof Float16Array < "u" && Float16Array.from ? Float16Array : Uint16Array; + case "float32": + return Float32Array; + case "uint8": + return Uint8Array; + case "int8": + return Int8Array; + case "uint16": + return Uint16Array; + case "int16": + return Int16Array; + case "int32": + return Int32Array; + case "bool": + return Uint8Array; + case "float64": + return Float64Array; + case "uint32": + return Uint32Array; + case "int64": + return BigInt64Array; + case "uint64": + return BigUint64Array; + default: + throw new Error(`unsupported type: ${e}`); + } + }, Xt = (e) => { + switch (e) { + case "verbose": + return 0; + case "info": + return 1; + case "warning": + return 2; + case "error": + return 3; + case "fatal": + return 4; + default: + throw new Error(`unsupported logging level: ${e}`); + } + }, Vr = (e) => e === "float32" || e === "float16" || e === "int32" || e === "int64" || e === "uint32" || e === "uint8" || e === "bool" || e === "uint4" || e === "int4", Nr = (e) => e === "float32" || e === "float16" || e === "int32" || e === "int64" || e === "uint32" || e === "uint64" || e === "int8" || e === "uint8" || e === "bool", Kn = (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 Zt, Yn = V(() => { + "use strict"; + Cr(); + Zt = async (e) => { + if (typeof e == "string") if (false) { + } else { + let t = await fetch(e); + if (!t.ok) throw new Error(`failed to load external data file: ${e}`); + let r = t.headers.get("Content-Length"), n = r ? parseInt(r, 10) : 0; + if (n < 1073741824) return new Uint8Array(await t.arrayBuffer()); + { + if (!t.body) throw new Error(`failed to load external data file: ${e}, no response body.`); + + //console.log(t); + let o = t.body.getReader(), i; + try { + i = new ArrayBuffer(n); + } catch (d) { + if (d instanceof RangeError) { + let l = Math.ceil(n / 65536); + i = new WebAssembly.Memory({ initial: l, maximum: l }).buffer; + } else throw d; + } + let a = 0; + for (; ; ) { + let { done: d, value: l } = await o.read(); + if (d) break; + let c = l.byteLength; + new Uint8Array(i, a, c).set(l), a += c; + } + return new Uint8Array(i, 0, n); + } + } + else return e instanceof Blob ? new Uint8Array(await e.arrayBuffer()) : e instanceof Uint8Array ? e : new Uint8Array(e); + }; + }); + var Wp, Lp, La, Ha, Wr, Hp, me, Xe = V(() => { + "use strict"; + Q(); + Wp = ["V", "I", "W", "E", "F"], Lp = (e, t) => { + console.log(`[${Wp[e]},${(/* @__PURE__ */ new Date()).toISOString()}]${t}`); + }, Wr = (e, t) => { + La = e, Ha = t; + }, Hp = (e, t) => { + let r = Xt(e), n = Xt(La); + r >= n && Lp(r, typeof t == "function" ? t() : t); + }, me = (...e) => { + Ha && Hp(...e); + }; + }); + var Lr, Xn = V(() => { + "use strict"; + Q(); + Lr = (e, t) => new (Ur(t))(e); + }); + var Hr = V(() => { + "use strict"; + }); + var Ga, Zn, Qn, Gp, Fp, Fa, eo, Jn, ja, Ka = V(() => { + "use strict"; + Xe(); + Hr(); + Ga = /* @__PURE__ */ new Map([[64, 250], [128, 200], [256, 200], [512, 200], [2048, 230], [4096, 200], [8192, 50], [16384, 50], [32768, 50], [65536, 50], [131072, 50], [262144, 50], [524288, 50], [1048576, 50], [2097152, 30], [4194304, 20], [8388608, 10], [12582912, 10], [16777216, 10], [26214400, 15], [33554432, 22], [44236800, 2], [58982400, 6], [67108864, 6], [134217728, 6], [167772160, 6]]), Zn = [], Qn = (e) => Math.ceil(e / 16) * 16, Gp = (e) => { + for (let t = 0; t < Zn.length; t++) { + let r = Zn[t]; + if (e <= r) return r; + } + return Math.ceil(e / 16) * 16; + }, Fp = 1, Fa = () => Fp++, eo = async (e, t, r, n) => { + let o = Qn(r), i = e.device.createBuffer({ size: o, usage: GPUBufferUsage.COPY_DST | GPUBufferUsage.MAP_READ }); + try { + let a = e.getCommandEncoder(); + e.endComputePass(), a.copyBufferToBuffer(t, 0, i, 0, o), e.flush(), await i.mapAsync(GPUMapMode.READ); + let d = i.getMappedRange(); + if (n) { + let l = n(); + return l.set(new Uint8Array(d, 0, r)), l; + } else return new Uint8Array(d.slice(0, r)); + } finally { + i.destroy(); + } + }, Jn = class { + constructor(t) { + this.backend = t; + this.storageCache = /* @__PURE__ */ new Map(), this.freeBuffers = /* @__PURE__ */ new Map(), this.freeUniformBuffers = /* @__PURE__ */ new Map(), this.buffersForUploadingPending = [], this.buffersPending = [], this.capturedPendingBuffers = /* @__PURE__ */ new Map(); + for (let [r] of Ga) Zn.push(r), this.freeBuffers.set(r, []), this.freeUniformBuffers.set(r, []); + } + upload(t, r) { + let n = r.buffer, o = r.byteOffset, i = r.byteLength, a = Qn(i), d = this.storageCache.get(t); + if (!d) throw new Error("gpu data for uploading does not exist"); + if (d.originalSize !== i) throw new Error(`inconsistent data size. gpu data size=${d.originalSize}, data size=${i}`); + let l = this.backend.device.createBuffer({ mappedAtCreation: true, size: a, usage: GPUBufferUsage.MAP_WRITE | GPUBufferUsage.COPY_SRC }), c = l.getMappedRange(); + new Uint8Array(c).set(new Uint8Array(n, o, i)), l.unmap(); + let m = this.backend.getCommandEncoder(); + this.backend.endComputePass(), m.copyBufferToBuffer(l, 0, d.gpuData.buffer, 0, a), me("verbose", () => `[WebGPU] GpuDataManager.upload(id=${t})`), this.buffersForUploadingPending.push(l); + } + memcpy(t, r) { + let n = this.storageCache.get(t); + if (!n) throw new Error("source gpu data for memcpy does not exist"); + let o = this.storageCache.get(r); + if (!o) throw new Error("destination gpu data for memcpy does not exist"); + if (n.originalSize !== o.originalSize) throw new Error("inconsistent source and destination gpu data size"); + let i = Qn(n.originalSize), a = this.backend.getCommandEncoder(); + this.backend.endComputePass(), a.copyBufferToBuffer(n.gpuData.buffer, 0, o.gpuData.buffer, 0, i); + } + registerExternalBuffer(t, r, n) { + let o; + if (n) { + if (o = n[0], t === n[1]) return me("verbose", () => `[WebGPU] GpuDataManager.registerExternalBuffer(size=${r}) => id=${o}, buffer is the same, skip.`), o; + if (this.backend.capturedCommandList.has(this.backend.currentSessionId)) throw new Error(`Registering a different external buffer under graph capture mode is not supported yet. + Please use the previous external buffer!`); + } else o = Fa(); + return this.storageCache.set(o, { gpuData: { id: o, type: 0, buffer: t }, originalSize: r }), me("verbose", () => `[WebGPU] GpuDataManager.registerExternalBuffer(size=${r}) => id=${o}, registered.`), o; + } + unregisterExternalBuffer(t) { + t !== void 0 && (this.storageCache.delete(t), me("verbose", () => `[WebGPU] GpuDataManager.unregisterExternalBuffer() => id=${t}`)); + } + create(t, r = GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_SRC | GPUBufferUsage.COPY_DST) { + let n = Gp(t), o, i = (r & GPUBufferUsage.STORAGE) === GPUBufferUsage.STORAGE, a = (r & GPUBufferUsage.UNIFORM) === GPUBufferUsage.UNIFORM; + if (i || a) { + let c = (i ? this.freeBuffers : this.freeUniformBuffers).get(n); + c ? c.length > 0 ? o = c.pop() : o = this.backend.device.createBuffer({ size: n, usage: r }) : o = this.backend.device.createBuffer({ size: n, usage: r }); + } else o = this.backend.device.createBuffer({ size: n, usage: r }); + let d = { id: Fa(), type: 0, buffer: o }; + return this.storageCache.set(d.id, { gpuData: d, originalSize: t }), me("verbose", () => `[WebGPU] GpuDataManager.create(size=${t}) => id=${d.id}`), d; + } + get(t) { + var _a2; + return (_a2 = this.storageCache.get(t)) == null ? void 0 : _a2.gpuData; + } + release(t) { + let r = this.storageCache.get(t); + if (!r) throw new Error("releasing data does not exist"); + return me("verbose", () => `[WebGPU] GpuDataManager.release(id=${t}), gpuDataId=${r.gpuData.id}`), this.storageCache.delete(t), this.buffersPending.push(r.gpuData.buffer), r.originalSize; + } + async download(t, r) { + let n = this.storageCache.get(t); + if (!n) throw new Error("data does not exist"); + await eo(this.backend, n.gpuData.buffer, n.originalSize, r); + } + refreshPendingBuffers() { + for (let t of this.buffersForUploadingPending) t.destroy(); + if (this.buffersForUploadingPending = [], this.buffersPending.length !== 0) if (this.backend.sessionStatus === "default") { + for (let t of this.buffersPending) { + let r = Ga.get(t.size); + if ((t.usage & GPUBufferUsage.STORAGE) === GPUBufferUsage.STORAGE) { + let n = this.freeBuffers.get(t.size) || []; + r === void 0 || n.length >= r ? t.destroy() : n.push(t); + } else if ((t.usage & GPUBufferUsage.UNIFORM) === GPUBufferUsage.UNIFORM) { + let n = this.freeUniformBuffers.get(t.size) || []; + r === void 0 || n.length >= r ? t.destroy() : n.push(t); + } else t.destroy(); + } + this.buffersPending = []; + } else { + let t = this.capturedPendingBuffers.get(this.backend.currentSessionId); + t || (t = [], this.capturedPendingBuffers.set(this.backend.currentSessionId, t)); + for (let r of this.buffersPending) t.push(r); + this.buffersPending = []; + } + } + dispose() { + this.freeBuffers.forEach((t) => { + t.forEach((r) => { + r.destroy(); + }); + }), this.freeUniformBuffers.forEach((t) => { + t.forEach((r) => { + r.destroy(); + }); + }), this.storageCache.forEach((t) => { + t.gpuData.buffer.destroy(); + }), this.capturedPendingBuffers.forEach((t) => { + t.forEach((r) => { + r.destroy(); + }); + }), this.storageCache = /* @__PURE__ */ new Map(), this.freeBuffers = /* @__PURE__ */ new Map(), this.freeUniformBuffers = /* @__PURE__ */ new Map(), this.capturedPendingBuffers = /* @__PURE__ */ new Map(); + } + onReleaseSession(t) { + let r = this.capturedPendingBuffers.get(t); + r && (r.forEach((n) => { + n.destroy(); + }), this.capturedPendingBuffers.delete(t)); + } + }, ja = (...e) => new Jn(...e); + }); + var to, J, Ie = V(() => { + "use strict"; + to = class { + constructor(t) { + Object.assign(this, t); + } + get cacheKey() { + return this.key || (this.key = Object.getOwnPropertyNames(this).sort().map((t) => `${this[t]}`).join(";")), this.key; + } + }, J = (e) => new to(e); + }); + var ro, rt, k, Ct, Gr, Ya, Xa, ie = V(() => { + "use strict"; + ro = class { + static calcMatMulShape(t, r) { + return t[1] !== r[0] ? void 0 : [t[0], r[1]]; + } + }, rt = class { + static calcShape(t, r, n = false) { + let o = t.length, i = r.length; + if (o === 0) return r; + if (i === 0) return t; + let a = Math.max(t.length, r.length), d = new Array(a); + if (n) { + if (o < 2 || i < 2) return; + let l = ro.calcMatMulShape([t[o - 2], t[o - 1]], [r[i - 2], r[i - 1]]); + if (l === void 0) return; + [d[a - 2], d[a - 1]] = l; + } + for (let l = n ? 3 : 1; l <= a; l++) { + let c = o - l < 0 ? 1 : t[o - l], m = i - l < 0 ? 1 : r[i - l]; + if (c !== m && c > 1 && m > 1) return; + let u = Math.max(c, m); + if (c && m) d[a - l] = Math.max(c, m); + else { + if (u > 1) return; + d[a - l] = 0; + } + } + return d; + } + static isValidBroadcast(t, r) { + let n = t.length, o = r.length; + if (n > o) return false; + for (let i = 1; i <= n; i++) if (t[n - i] !== 1 && t[n - i] !== r[o - i]) return false; + return true; + } + }, k = class e { + static size(t) { + return e.getSizeFromDimensionRange(t, 0, t.length); + } + static convertShape(t, r = 4) { + let n = t.length; + if (n === 0) return []; + let o = new Array(n), i = n - 1; + for (; i >= 0; ) { + if (t[i] % r === 0) { + o[i] = t[i] / r; + break; + } + if (r % t[i] !== 0) throw new Error("cannot convert shape"); + o[i] = 1, r /= t[i], i--; + } + for (i--; i >= 0; i--) o[i] = t[i]; + return o; + } + static sizeFromDimension(t, r) { + if (r < 0 || r > t.length) throw new Error(`invalid dimension of ${r} for sizeFromDimension as Tensor has ${t.length} dimensions.`); + return e.getSizeFromDimensionRange(t, r, t.length); + } + static sizeToDimension(t, r) { + if (r < 0 || r > t.length) throw new Error(`invalid dimension of ${r} for sizeToDimension as Tensor has ${t.length} dimensions.`); + return e.getSizeFromDimensionRange(t, 0, r); + } + static getSizeFromDimensionRange(t, r, n) { + let o = 1; + for (let i = r; i < n; i++) { + if (t[i] < 0) throw new Error("cannot get valid size from specified dimension range. Most likely the range contains negative values in them."); + o *= t[i]; + } + return o; + } + static computeStrides(t) { + let r = t.length; + if (r === 0) return []; + if (r === 1) return [1]; + let n = new Array(r); + n[r - 1] = 1, n[r - 2] = t[r - 1]; + for (let o = r - 3; o >= 0; --o) n[o] = n[o + 1] * t[o + 1]; + return n; + } + static normalizeAxis(t, r) { + if (t < -r && t >= r) throw new Error("unsupported axis for this operation."); + return t < 0 ? t + r : t; + } + static normalizeAxes(t, r) { + return t.map((n) => this.normalizeAxis(n, r ?? t.length)); + } + static sortBasedOnPerm(t, r) { + return r ? r.map((n) => t[n]) : t.slice().reverse(); + } + static padShape(t, r) { + let n = t.length; + return t.map((o, i) => o + r[i] + r[i + n]); + } + static areEqual(t, r) { + return t.length !== r.length ? false : t.every((n, o) => n === r[o]); + } + }, Ct = class e { + static adjustPoolAttributes(t, r, n, o, i, a) { + if (!t && n.length !== r.length - 2) throw new Error("length of specified kernel shapes should be 2 less than length of input dimensions"); + if (t) for (let d = 0; d < r.length - 2; d++) d >= n.length ? n.push(r[d + 2]) : n[d] = r[d + 2]; + for (let d = 0; d < n.length; d++) if (d < o.length) { + if (o[d] < 0) throw new Error("strides should be greater than or equal to 1"); + } else o.push(1); + for (let d = 0; d < n.length; d++) if (d < i.length) { + if (i[d] < 0) throw new Error("dilations should be greater than or equal to 1"); + } else i.push(1); + for (let d = 0; d < n.length * 2; d++) if (d < a.length) { + if (a[d] < 0) throw new Error("pad should be greater than or equal to 1"); + } else a.push(0); + for (let d = 0; d < n.length; d++) { + if (n[d] <= 0) throw new Error("kernel shapes need to be greater than 0"); + if (a[d] >= n[d] || a[d + n.length] >= n[d]) throw new Error("pads should be smaller than kernel"); + } + } + static adjustPadsBasedOnAutoPad(t, r, n, o, i, a, d) { + if (d) { + if (i.length !== 2 * (t.length - 2)) throw new Error("length of pads should be twice the length of data dimensions"); + if (r.length !== t.length - 2) throw new Error("length of strides should be the length of data dimensions"); + if (o.length !== t.length - 2) throw new Error("length of kernel shapes should be the length of data dimensions"); + for (let l = 0; l < t.length - 2; l++) e.adjustPadAndReturnShape(t[l + (a ? 1 : 2)], r[l], n[l], o[l], i, l, l + t.length - 2, d); + } + } + static computePoolOutputShape(t, r, n, o, i, a, d) { + if (r.length <= 0) throw new Error("input shape must be of size greater than 0"); + let l = [r[0], r[1]]; + return e.computeShapeHelper(t, r, l, n, o, i, a, d), l; + } + static computeConvOutputShape(t, r, n, o, i, a, d) { + if (t.length <= 0 || r.length <= 0) throw new Error("invalid input tensor dims or invalid filter tensor dims"); + let l = [t[0], r[0]]; + return e.computeShapeHelper(false, t, l, n, o, i, a, d), l; + } + static computeShapeHelper(t, r, n, o, i, a, d, l) { + if (t) for (let c = 0; c < r.length - 2; c++) n.push(1); + else for (let c = 0; c < r.length - 2; c++) n.push(e.adjustPadAndReturnShape(r[c + 2], o[c], i[c], a[c], d, c, c + r.length - 2, l)); + } + static adjustPadAndReturnShape(t, r, n, o, i, a, d, l) { + let c = n * (o - 1) + 1; + if (l && l !== "NOTSET") switch (l) { + case "VALID": + return i[a] = 0, i[d] = 0, Math.floor((t - c) / r + 1); + case "SAME_LOWER": + case "SAME_UPPER": + if (n !== 1) throw new Error("Dilation not supported for SAME_UPPER or SAME_LOWER"); + { + let u = ((t + r - 1) / r - 1) * r + o - t; + return i[a] = Math.floor(l === "SAME_LOWER" ? (u + 1) / 2 : u / 2), i[d] = u - i[a], Math.floor((t + u - o) / r + 1); + } + default: + throw new Error("Unsupported AutoPad type"); + } + else return Math.floor((t + i[a] + i[d] - c) / r + 1); + } + }, Gr = class { + static getShapeOfGemmResult(t, r, n, o, i) { + if (t.length !== 2 || n.length !== 2) throw new Error("shape need to be of size 2"); + let a, d, l; + r ? (a = t[1], d = t[0]) : (a = t[0], d = t[1]); + let c = -1; + if (o ? (l = n[0], c = 1) : (l = n[1], c = 0), n[c] !== d) throw new Error("dimension mismatch"); + if (a <= 0 || l <= 0 || d <= 0) throw new Error("invalid shape specified"); + if (i && !rt.isValidBroadcast(i, [a, l])) throw new Error("gemm: invalid bias shape for broadcast"); + return [a, l, d]; + } + }, Ya = -34028234663852886e22, Xa = 34028234663852886e22; + }); + var At, oo, he, Ee, R, we, io, kt, Ze, F, ao, E, M, Fr, no, Za, Nt, ae = V(() => { + "use strict"; + Q(); + ie(); + At = 64, oo = (e, t) => { + if (t === 3) throw new Error("vec3 has same alignment as vec4, use vec4 instead"); + switch (e) { + case 10: + return t > 1 ? `vec${t}` : "f16"; + case 1: + return t > 1 ? `vec${t}` : "f32"; + case 6: + return t > 1 ? `vec${t}` : "i32"; + case 12: + return t > 1 ? `vec${t}` : "u32"; + case 7: + if (t > 1) throw new Error("currently not supported vecX of uint64 yet"); + return ["vec2", "i32"]; + case 13: + if (t > 1) throw new Error("currently not supported vecX of uint64 yet"); + return ["vec2", "u32"]; + case 9: + if (t !== 4) throw new Error("bool must be vec4"); + return ["u32", "vec4"]; + case 22: + return "i32"; + case 21: + return "u32"; + default: + throw new Error(`Unknown data type: ${e}`); + } + }, he = (e, t = 1) => { + let r = oo(e, t); + return typeof r == "string" ? r : r[0]; + }, Ee = (e, t = 1) => { + let r = oo(e, t); + return typeof r == "string" ? r : r[1]; + }, R = (...e) => { + let t = []; + return e.forEach((r) => { + r.length !== 0 && t.push({ type: 12, data: r }, { type: 12, data: k.computeStrides(r) }); + }), t; + }, we = (e) => e % 4 === 0 ? 4 : e % 2 === 0 ? 2 : 1, io = (e = "f32", t, r = "0") => !t || t === 1 ? `${e}(${r})` : `vec${t}<${e}>(${r})`, kt = (e, t, r) => e === "f32" ? r : t === 1 ? `f32(${r})` : `vec${t}(${r})`, Ze = (e, t) => t === 4 ? `(${e}.x + ${e}.y + ${e}.z + ${e}.w)` : t === 2 ? `(${e}.x + ${e}.y)` : t === 3 ? `(${e}.x + ${e}.y + ${e}.z)` : e, F = (e, t, r, n) => e.startsWith("uniforms.") && r > 4 ? typeof t == "string" ? n === "f16" ? `${e}[(${t}) / 8][(${t}) % 8 / 4][(${t}) % 8 % 4]` : `${e}[(${t}) / 4][(${t}) % 4]` : n === "f16" ? `${e}[${Math.floor(t / 8)}][${Math.floor(t % 8 / 4)}][${t % 8 % 4}]` : `${e}[${Math.floor(t / 4)}][${t % 4}]` : r > 1 ? `${e}[${t}]` : e, ao = (e, t, r, n, o) => { + let i = typeof r == "number", a = i ? r : r.length, d = [...new Array(a).keys()], l = a < 2 ? "u32" : a <= 4 ? `vec${a}` : `array`, c = oo(t, o), m = typeof c == "string" ? c : c[1], u = typeof c == "string" ? c : c[0], h = { indices: l, value: m, storage: u, tensor: t }, w = (U) => typeof U == "string" ? U : `${U}u`, g = { offsetToIndices: false, indicesToOffset: false, broadcastedIndicesToOffset: false, set: false, setByIndices: false, get: false, getByIndices: false }, y = i ? "uniforms." : "", S = `${y}${e}_shape`, $ = `${y}${e}_strides`, v = ""; + for (let U = 0; U < a - 1; U++) v += ` + let dim${U} = current / ${F($, U, a)}; + let rest${U} = current % ${F($, U, a)}; + indices[${U}] = dim${U}; + current = rest${U}; + `; + v += `indices[${a - 1}] = current;`; + let x = a < 2 ? "" : ` + fn o2i_${e}(offset: u32) -> ${h.indices} { + var indices: ${h.indices}; + var current = offset; + ${v} + return indices; + }`, T = (U) => (g.offsetToIndices = true, a < 2 ? U : `o2i_${e}(${U})`), C = []; + if (a >= 2) for (let U = a - 1; U >= 0; U--) C.push(`${F($, U, a)} * (indices[${U}])`); + let A = a < 2 ? "" : ` + fn i2o_${e}(indices: ${h.indices}) -> u32 { + return ${C.join("+")}; + }`, P = (U) => (g.indicesToOffset = true, a < 2 ? U : `i2o_${e}(${U})`), B = (...U) => a === 0 ? "0u" : `${h.indices}(${U.map(w).join(",")})`, N = (U, G) => a < 2 ? `${U}` : `${F(U, G, a)}`, W = (U, G, ye) => a < 2 ? `${U}=${ye};` : `${F(U, G, a)}=${ye};`, K = {}, Z = (U, G) => { + g.broadcastedIndicesToOffset = true; + let ye = `${G.name}broadcastedIndicesTo${e}Offset`; + if (ye in K) return `${ye}(${U})`; + let Re = []; + for (let $e = a - 1; $e >= 0; $e--) { + let Ce = G.indicesGet("outputIndices", $e + G.rank - a); + Re.push(`${N($, $e)} * (${Ce} % ${N(S, $e)})`); + } + return K[ye] = `fn ${ye}(outputIndices: ${G.type.indices}) -> u32 { + return ${Re.length > 0 ? Re.join("+") : "0u"}; + }`, `${ye}(${U})`; + }, ee = (U, G) => (() => { + if (h.storage === h.value) return `${e}[${U}]=${G};`; + if (h.storage === "vec2" && h.value === "i32") return `${e}[${U}]=vec2(u32(${G}), select(0u, 0xFFFFFFFFu, ${G} < 0));`; + if (h.storage === "vec2" && h.value === "u32") return `${e}[${U}]=vec2(u32(${G}), 0u);`; + if (h.storage === "u32" && h.value === "vec4") return `${e}[${U}]=dot(vec4(0x1, 0x100, 0x10000, 0x1000000), vec4(${G}));`; + throw new Error(`not supported combination of storage type ${h.storage} and value type ${h.value} yet`); + })(), se = (U) => (() => { + if (h.storage === h.value) return `${e}[${U}]`; + if (h.storage === "vec2" && h.value === "i32") return `i32(${e}[${U}].x)`; + if (h.storage === "vec2" && h.value === "u32") return `u32(${e}[${U}].x)`; + if (h.storage === "u32" && h.value === "vec4") return `vec4(bool(${e}[${U}] & 0xFFu), bool(${e}[${U}] & 0xFF00u), bool(${e}[${U}] & 0xFF0000u), bool(${e}[${U}] & 0xFF000000u))`; + throw new Error(`not supported combination of storage type ${h.storage} and value type ${h.value} yet`); + })(), de = a < 2 ? "" : ` + fn get_${e}ByIndices(indices: ${h.indices}) -> ${m} { + return ${se(`i2o_${e}(indices)`)}; + }`, Y = a < 2 ? "" : (() => { + let U = d.map((ye) => `d${ye}: u32`).join(", "), G = d.map((ye) => `d${ye}`).join(", "); + return ` + fn get_${e}(${U}) -> ${m} { + return get_${e}ByIndices(${B(G)}); + }`; + })(), le = (...U) => { + if (U.length !== a) throw new Error(`indices length must be ${a}`); + let G = U.map(w).join(","); + return a === 0 ? se("0u") : a === 1 ? se(G[0]) : (g.get = true, g.getByIndices = true, g.indicesToOffset = true, `get_${e}(${G})`); + }, ce = (U) => a < 2 ? se(U) : (g.getByIndices = true, g.indicesToOffset = true, `get_${e}ByIndices(${U})`), q = a < 2 ? "" : ` + fn set_${e}ByIndices(indices: ${h.indices}, value: ${m}) { + ${ee(`i2o_${e}(indices)`, "value")} + }`, ue = a < 2 ? "" : (() => { + let U = d.map((ye) => `d${ye}: u32`).join(", "), G = d.map((ye) => `d${ye}`).join(", "); + return ` + fn set_${e}(${U}, value: ${m}) { + set_${e}ByIndices(${B(G)}, value); + }`; + })(); + return { impl: () => { + let U = [], G = false; + return g.offsetToIndices && (U.push(x), G = true), g.indicesToOffset && (U.push(A), G = true), g.broadcastedIndicesToOffset && (Object.values(K).forEach((ye) => U.push(ye)), G = true), g.set && (U.push(ue), G = true), g.setByIndices && (U.push(q), G = true), g.get && (U.push(Y), G = true), g.getByIndices && (U.push(de), G = true), !i && G && U.unshift(`const ${S} = ${h.indices}(${r.join(",")});`, `const ${$} = ${h.indices}(${k.computeStrides(r).join(",")});`), U.join(` + `); + }, type: h, offsetToIndices: T, indicesToOffset: P, broadcastedIndicesToOffset: Z, indices: B, indicesGet: N, indicesSet: W, set: (...U) => { + if (U.length !== a + 1) throw new Error(`indices length must be ${a}`); + let G = U[a]; + if (typeof G != "string") throw new Error("value must be string"); + let ye = U.slice(0, a).map(w).join(","); + return a === 0 ? ee("0u", G) : a === 1 ? ee(ye[0], G) : (g.set = true, g.setByIndices = true, g.indicesToOffset = true, `set_${e}(${ye}, ${G})`); + }, setByOffset: ee, setByIndices: (U, G) => a < 2 ? ee(U, G) : (g.setByIndices = true, g.indicesToOffset = true, `set_${e}ByIndices(${U}, ${G});`), get: le, getByOffset: se, getByIndices: ce, usage: n, name: e, strides: $, shape: S, rank: a }; + }, E = (e, t, r, n = 1) => ao(e, t, r, "input", n), M = (e, t, r, n = 1) => ao(e, t, r, "output", n), Fr = (e, t, r, n = 1) => ao(e, t, r, "internal", n), no = class { + constructor(t, r) { + this.normalizedDispatchGroup = t; + this.limits = r; + this.internalVariables = []; + this.variables = []; + this.uniforms = []; + this.variableIndex = 0; + } + guardAgainstOutOfBoundsWorkgroupSizes(t) { + return `if (global_idx >= ${typeof t == "number" ? `${t}u` : t}) { return; }`; + } + mainStart(t = At) { + let r = typeof t == "number" ? t : t[0], n = typeof t == "number" ? 1 : t[1], o = typeof t == "number" ? 1 : t[2]; + if (r > this.limits.maxComputeWorkgroupSizeX || n > this.limits.maxComputeWorkgroupSizeY || o > this.limits.maxComputeWorkgroupSizeZ) throw new Error(`workgroup size [${r}, ${n}, ${o}] exceeds the maximum workgroup size [${this.limits.maxComputeWorkgroupSizeX}, ${this.limits.maxComputeWorkgroupSizeY}, ${this.limits.maxComputeWorkgroupSizeZ}].`); + if (r * n * o > this.limits.maxComputeInvocationsPerWorkgroup) throw new Error(`workgroup size [${r}, ${n}, ${o}] exceeds the maximum workgroup invocations ${this.limits.maxComputeInvocationsPerWorkgroup}.`); + let i = this.normalizedDispatchGroup[1] === 1 && this.normalizedDispatchGroup[2] === 1, a = i ? `@builtin(global_invocation_id) global_id : vec3, + @builtin(workgroup_id) workgroup_id : vec3, + @builtin(local_invocation_index) local_idx : u32, + @builtin(local_invocation_id) local_id : vec3` : `@builtin(global_invocation_id) global_id : vec3, + @builtin(local_invocation_id) local_id : vec3, + @builtin(local_invocation_index) local_idx : u32, + @builtin(workgroup_id) workgroup_id : vec3, + @builtin(num_workgroups) num_workgroups : vec3`, d = i ? `let global_idx = global_id.x; + let workgroup_index = workgroup_id.x;` : `let workgroup_index = workgroup_id.z * num_workgroups[0] * num_workgroups[1] + + workgroup_id.y * num_workgroups[0] + workgroup_id.x; + let global_idx = workgroup_index * ${r * n * o}u + local_idx;`; + return `@compute @workgroup_size(${r}, ${n}, ${o}) + fn main(${a}) { + ${d} + `; + } + appendVariableUniforms(t) { + t.rank !== 0 && (t.shape.startsWith("uniforms.") && this.uniforms.push({ name: t.shape.replace("uniforms.", ""), type: "u32", length: t.rank }), t.strides.startsWith("uniforms.") && this.uniforms.push({ name: t.strides.replace("uniforms.", ""), type: "u32", length: t.rank })); + } + declareVariable(t, r) { + if (t.usage === "internal") throw new Error("cannot use internal variable with declareVariable(). use registerInternalVariables() instead."); + this.variables.push(t), this.appendVariableUniforms(t); + let n = t.usage === "input" ? "read" : "read_write", o = t.type.storage; + return `@group(0) @binding(${r}) var ${t.name}: array<${o}>;`; + } + declareVariables(...t) { + return t.map((r) => this.declareVariable(r, this.variableIndex++)).join(` + `); + } + registerInternalVariable(t) { + if (t.usage !== "internal") throw new Error("cannot use input or output variable with registerInternalVariable(). use declareVariables() instead."); + this.internalVariables.push(t), this.appendVariableUniforms(t); + } + registerInternalVariables(...t) { + return t.forEach((r) => this.registerInternalVariable(r)), this; + } + registerUniform(t, r, n = 1) { + return this.uniforms.push({ name: t, type: r, length: n }), this; + } + registerUniforms(t) { + return this.uniforms = this.uniforms.concat(t), this; + } + uniformDeclaration() { + if (this.uniforms.length === 0) return ""; + let t = []; + for (let { name: r, type: n, length: o } of this.uniforms) if (o && o > 4) n === "f16" ? t.push(`@align(16) ${r}:array, ${Math.ceil(o / 8)}>`) : t.push(`${r}:array, ${Math.ceil(o / 4)}>`); + else { + let i = o == null || o === 1 ? n : `vec${o}<${n}>`; + t.push(`${r}:${i}`); + } + return ` + struct Uniforms { ${t.join(", ")} }; + @group(0) @binding(${this.variableIndex}) var uniforms: Uniforms;`; + } + get additionalImplementations() { + return this.uniformDeclaration() + this.variables.map((t) => t.impl()).join(` + `) + this.internalVariables.map((t) => t.impl()).join(` + `); + } + get variablesInfo() { + if (this.uniforms.length === 0) return; + let t = (r) => [12, 10, 1, 6][["u32", "f16", "f32", "i32"].indexOf(r)]; + return this.uniforms.map((r) => [t(r.type), r.length ?? 1]); + } + }, Za = (e, t) => new no(e, t), Nt = (e, t) => { + let r = e.length, n = []; + for (let o = 0; o < r; o++) { + let i = r - 1 - o, a = e[i] || 1; + (t[t.length - 1 - o] || 1) > 1 && a === 1 && n.unshift(i); + } + return n; + }; + }); + var qp, Qa, jp, Kp, Yp, Pe, Ja, es, lt = V(() => { + "use strict"; + Q(); + ie(); + Ie(); + ae(); + qp = (e) => { + if (!e || e.length !== 1) throw new Error("Transpose requires 1 input."); + }, Qa = (e, t) => t && t.length !== e ? [...new Array(e).keys()].reverse() : t, jp = (e, t) => k.sortBasedOnPerm(e, Qa(e.length, t)), Kp = (e, t, r, n) => { + let o = `fn perm(i: ${n.type.indices}) -> ${r.type.indices} { + var a: ${r.type.indices};`; + for (let i = 0; i < t; ++i) o += r.indicesSet("a", e[i], `i[${i}]`); + return o += "return a;}"; + }, Yp = (e, t) => { + let r = [], n = []; + for (let o = 0; o < e.length; ++o) e[o] !== 1 && r.push(e[o]), e[t[o]] !== 1 && n.push(t[o]); + return { newShape: r, newPerm: n }; + }, Pe = (e, t) => { + let r = e.dataType, n = e.dims.length, o = Qa(n, t), i = jp(e.dims, o), { newShape: a, newPerm: d } = Yp(e.dims, o), l = k.areEqual(d, [2, 3, 1]), c = k.areEqual(d, [3, 1, 2]), m = a.length === 2 && d[0] > d[1] || l || c, u = m ? a : e.dims, h = i; + m && (u = l ? [a[0], a[1] * a[2]] : c ? [a[0] * a[1], a[2]] : a, h = [u[1], u[0]]); + let w = E("a", r, u.length), g = M("output", r, h.length), y = 16, S; + return m ? S = ($) => ` + ${$.registerUniform("output_size", "u32").declareVariables(w, g)} + var tile : array, ${y}>; + ${$.mainStart([y, y, 1])} + let stride = (uniforms.output_shape[1] - 1) / ${y} + 1; + let workgroup_id_x = workgroup_index % stride; + let workgroup_id_y = workgroup_index / stride; + let input_col = workgroup_id_y * ${y}u + local_id.x; + let input_row = workgroup_id_x * ${y}u + local_id.y; + if (input_row < uniforms.a_shape[0] && input_col < uniforms.a_shape[1]) { + tile[local_id.y][local_id.x] = ${w.getByIndices(`${w.type.indices}(input_row, input_col)`)}; + } + workgroupBarrier(); + + let output_col = workgroup_id_x * ${y}u + local_id.x; + let output_row = workgroup_id_y * ${y}u + local_id.y; + if (output_row < uniforms.output_shape[0] && output_col < uniforms.output_shape[1]) { + ${g.setByIndices(`${g.type.indices}(output_row, output_col)`, "tile[local_id.x][local_id.y]")} + } + }` : S = ($) => ` + ${$.registerUniform("output_size", "u32").declareVariables(w, g)} + + ${Kp(o, n, w, g)} + + ${$.mainStart()} + ${$.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} + + let indices = ${g.offsetToIndices("global_idx")}; + let aIndices = perm(indices); + + ${g.setByOffset("global_idx", w.getByIndices("aIndices"))} + }`, { name: m ? "TransposeShared" : "Transpose", shaderCache: { hint: `${t}`, inputDependencies: ["rank"] }, getRunData: () => { + let $ = k.size(i); + return { outputs: [{ dims: i, dataType: e.dataType }], dispatchGroup: m ? { x: Math.ceil(h[1] / y), y: Math.ceil(h[0] / y) } : { x: Math.ceil($ / 64) }, programUniforms: [{ type: 12, data: $ }, ...R(u, h)] }; + }, getShaderSource: S }; + }, Ja = (e, t) => { + qp(e.inputs), e.compute(Pe(e.inputs[0], t.perm)); + }, es = (e) => J({ perm: e.perm }); + }); + var Xp, Zp, Qp, Jp, em, tm, rm, nm, om, im, nt, ts, rs, ns, os, is, as, ss, us, ds, ls, cs = V(() => { + "use strict"; + Q(); + ie(); + ae(); + qr(); + lt(); + Xp = { max: "select(bestValue, candidate, candidate > bestValue)", min: "select(bestValue, candidate, candidate < bestValue)", mean: "bestValue + candidate", sum: "bestValue + candidate", prod: "bestValue * candidate", sumSquare: "bestValue + candidate * candidate", logSumExp: "bestValue + exp(candidate)", l1: "bestValue + abs(candidate)", l2: "bestValue + candidate * candidate", logSum: "bestValue + candidate" }, Zp = { 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" }, Qp = { max: "_A[offset]", min: "_A[offset]", mean: "0", sum: "0", prod: "1", sumSquare: "0", logSumExp: "0", l1: "0", l2: "0", logSum: "0" }, Jp = { max: "bestValue", min: "bestValue", sum: "bestValue", prod: "bestValue", sumSquare: "bestValue", logSumExp: "log(bestValue)", l1: "bestValue", l2: "sqrt(bestValue)", logSum: "log(bestValue)" }, em = (e, t) => { + let r = []; + for (let n = t - e; n < t; ++n) r.push(n); + return r; + }, tm = (e, t) => { + let r = [], n = e.length; + for (let i = 0; i < n; i++) t.indexOf(i) === -1 && r.push(e[i]); + let o = t.map((i) => e[i]); + return [r, o]; + }, rm = (e, t) => { + let r = e.length + t.length, n = [], o = 0; + for (let i = 0; i < r; i++) t.indexOf(i) === -1 ? n.push(e[o++]) : n.push(1); + return n; + }, nm = (e, t) => { + for (let r = 0; r < e.length; ++r) if (e[e.length - r - 1] !== t - 1 - r) return false; + return true; + }, om = (e, t) => { + let r = []; + if (!nm(e, t)) { + for (let n = 0; n < t; ++n) e.indexOf(n) === -1 && r.push(n); + e.forEach((n) => r.push(n)); + } + return r; + }, im = (e, t, r, n, o, i, a) => { + let d = r[0].dims, l = k.size(i), c = k.size(a), m = E("_A", r[0].dataType, d), u = M("output", o, i), h = 32, w = ` + var aBestValues : array; + `; + return { name: e, shaderCache: t, getShaderSource: (y) => ` + ${y.registerUniform("reduceSize", "u32").declareVariables(m, u)} + ${w} + fn DIV_CEIL(a : u32, b : u32) -> u32 { + return ((a - 1u) / b + 1u); + } + ${y.mainStart(h)} + + let outputIndex = global_idx / ${h}; + let offset = outputIndex * uniforms.reduceSize; + + var bestValue = f32(${Qp[n]}); + let Length = uniforms.reduceSize; + for (var k = local_idx; k < Length; k = k + ${h}) { + let candidate = f32(${m.getByOffset("offset + k")}); + bestValue = ${Xp[n]}; + } + aBestValues[local_idx] = bestValue; + workgroupBarrier(); + + var reduceSize = min(Length, ${h}u); + for (var currentSize = reduceSize / 2u; reduceSize > 1u; + currentSize = reduceSize / 2u) { + let interval = DIV_CEIL(reduceSize, 2u); + if (local_idx < currentSize) { + let candidate = aBestValues[local_idx + interval]; + bestValue = ${Zp[n]}; + aBestValues[local_idx] = bestValue; + } + reduceSize = interval; + workgroupBarrier(); + } + + if (local_idx == 0u) { + ${u.setByOffset("outputIndex", `${n === "mean" ? `${u.type.storage}(bestValue / f32(uniforms.reduceSize))` : `${u.type.storage}(${Jp[n]})`}`)}; + } + }`, getRunData: () => ({ outputs: [{ dims: i, dataType: o }], dispatchGroup: { x: l }, programUniforms: [{ type: 12, data: c }] }) }; + }, nt = (e, t, r, n) => { + let o = e.inputs.length === 1 ? r : so(e.inputs, r), i = o.axes; + i.length === 0 && !o.noopWithEmptyAxes && (i = e.inputs[0].dims.map((w, g) => g)); + let a = k.normalizeAxes(i, e.inputs[0].dims.length), d = a, l = e.inputs[0], c = om(d, e.inputs[0].dims.length); + c.length > 0 && (l = e.compute(Pe(e.inputs[0], c), { inputs: [0], outputs: [-1] })[0], d = em(d.length, l.dims.length)); + let [m, u] = tm(l.dims, d), h = m; + o.keepDims && (h = rm(m, a)), e.compute(im(t, { hint: o.cacheKey, inputDependencies: ["type"] }, [l], n, e.inputs[0].dataType, h, u), { inputs: [l] }); + }, ts = (e, t) => { + nt(e, "ReduceMeanShared", t, "mean"); + }, rs = (e, t) => { + nt(e, "ReduceL1Shared", t, "l1"); + }, ns = (e, t) => { + nt(e, "ReduceL2Shared", t, "l2"); + }, os = (e, t) => { + nt(e, "ReduceLogSumExpShared", t, "logSumExp"); + }, is = (e, t) => { + nt(e, "ReduceMaxShared", t, "max"); + }, as = (e, t) => { + nt(e, "ReduceMinShared", t, "min"); + }, ss = (e, t) => { + nt(e, "ReduceProdShared", t, "prod"); + }, us = (e, t) => { + nt(e, "ReduceSumShared", t, "sum"); + }, ds = (e, t) => { + nt(e, "ReduceSumSquareShared", t, "sumSquare"); + }, ls = (e, t) => { + nt(e, "ReduceLogSumShared", t, "logSum"); + }; + }); + var ot, am, jr, so, it, sm, um, dm, lm, cm, pm, mm, fm, hm, gm, at, ps, ms, fs, hs, gs, ys, bs, ws, _s, vs, qr = V(() => { + "use strict"; + Q(); + ie(); + Ie(); + ae(); + cs(); + ot = (e) => { + if (!e || e.length === 0 || e.length > 2) throw new Error("Reduce op requires 1 or 2 inputs."); + if (e.length === 2 && e[1].dims.length !== 1) throw new Error("Invalid axes input dims."); + }, am = (e) => ["", "", `var value = ${e.getByIndices("input_indices")};`, ""], jr = (e, t, r, n, o, i, a = false, d = false) => { + let l = [], c = r[0].dims, m = c.length, u = k.normalizeAxes(o, m), h = !d && u.length === 0; + c.forEach((S, $) => { + h || u.indexOf($) >= 0 ? a && l.push(1) : l.push(S); + }); + let w = l.length, g = k.size(l); + return { name: e, shaderCache: t, getShaderSource: (S) => { + let $ = [], v = E("_A", r[0].dataType, m), x = M("output", i, w), T = n(v, x, u), C = T[2]; + for (let A = 0, P = 0; A < m; A++) h || u.indexOf(A) >= 0 ? (a && P++, C = `for(var j${A}: u32 = 0; j${A} < ${c[A]}; j${A}++) { + ${T[2].includes("last_index") ? `let last_index = j${A};` : ""} + ${v.indicesSet("input_indices", A, `j${A}`)} + ${C} + }`) : ($.push(`${v.indicesSet("input_indices", A, x.indicesGet("output_indices", P))};`), P++); + return ` + + ${S.registerUniform("output_size", "u32").declareVariables(v, x)} + + ${S.mainStart()} + ${S.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} + var input_indices: ${v.type.indices}; + let output_indices = ${x.offsetToIndices("global_idx")}; + + ${$.join(` + `)} + ${T[0]} // init ops for reduce max/min + ${T[1]} + ${C} + ${T[3]} + ${T.length === 4 ? x.setByOffset("global_idx", "value") : T.slice(4).join(` + `)} + }`; + }, getRunData: () => ({ outputs: [{ dims: l, dataType: i }], dispatchGroup: { x: Math.ceil(g / 64) }, programUniforms: [{ type: 12, data: g }, ...R(c, l)] }) }; + }, so = (e, t) => { + let r = []; + return e[1].dims[0] > 0 && e[1].getBigInt64Array().forEach((n) => r.push(Number(n))), J({ axes: r, keepDims: t.keepDims, noopWithEmptyAxes: t.noopWithEmptyAxes }); + }, it = (e, t, r, n) => { + let o = e.inputs, i = o.length === 1 ? r : so(o, r); + e.compute(jr(t, { hint: i.cacheKey, inputDependencies: ["rank"] }, [o[0]], i.noopWithEmptyAxes && i.axes.length === 0 ? am : n, i.axes, o[0].dataType, i.keepDims, i.noopWithEmptyAxes), { inputs: [0] }); + }, sm = (e, t) => { + ot(e.inputs), it(e, "ReduceLogSum", t, (n, o) => [`var value = ${o.type.storage}(0);`, "", `value += ${n.getByIndices("input_indices")};`, "value = log(value);"]); + }, um = (e, t) => { + ot(e.inputs), it(e, "ReduceL1", t, (n, o) => [`var value = ${o.type.storage}(0);`, "", `value += abs(${n.getByIndices("input_indices")});`, ""]); + }, dm = (e, t) => { + ot(e.inputs), it(e, "ReduceL2", t, (n, o) => [`var t = ${o.type.value}(0); var value = ${o.type.value}(0);`, "", `t = ${n.getByIndices("input_indices")}; value += (t * t);`, "value = sqrt(value);"]); + }, lm = (e, t) => { + ot(e.inputs), it(e, "ReduceLogSumExp", t, (n, o) => [`var value = ${o.type.storage}(0);`, "", `value += exp(${n.getByIndices("input_indices")});`, "value = log(value);"]); + }, cm = (e, t) => { + ot(e.inputs), it(e, "ReduceMax", t, (n, o, i) => { + let a = []; + for (let d = 0; d < n.rank; d++) (i.indexOf(d) >= 0 || i.length === 0) && a.push(n.indicesSet("input_indices", d, 0)); + return [`${a.join(` + `)}`, `var value = ${n.getByIndices("input_indices")};`, `value = max(value, ${n.getByIndices("input_indices")});`, ""]; + }); + }, pm = (e, t) => { + ot(e.inputs), it(e, "ReduceMean", t, (n, o, i) => { + let a = 1; + for (let d = 0; d < n.rank; d++) (i.indexOf(d) >= 0 || i.length === 0) && (a *= e.inputs[0].dims[d]); + return ["var sum = f32(0);", "", `sum += f32(${n.getByIndices("input_indices")});`, `let value = ${o.type.value}(sum / ${a});`]; + }); + }, mm = (e, t) => { + ot(e.inputs), it(e, "ReduceMin", t, (n, o, i) => { + let a = []; + for (let d = 0; d < n.rank; d++) (i.indexOf(d) >= 0 || i.length === 0) && a.push(`input_indices[${d}] = 0;`); + return [`${a.join(` + `)}`, `var value = ${n.getByIndices("input_indices")};`, `value = min(value, ${n.getByIndices("input_indices")});`, ""]; + }); + }, fm = (e, t) => { + ot(e.inputs), it(e, "ReduceProd", t, (n, o) => [`var value = ${o.type.storage}(1);`, "", `value *= ${n.getByIndices("input_indices")};`, ""]); + }, hm = (e, t) => { + ot(e.inputs), it(e, "ReduceSum", t, (n, o) => [`var value = ${o.type.storage}(0);`, "", `value += ${n.getByIndices("input_indices")};`, ""]); + }, gm = (e, t) => { + ot(e.inputs), it(e, "ReduceSumSquare", t, (n, o) => [`var t = ${o.type.value}(0); var value = ${o.type.value}(0);`, "", `t = ${n.getByIndices("input_indices")}; value += t * t;`, ""]); + }, at = (e, t, r) => { + if (t.length === 0) return r; + let n = 1, o = 1; + for (let i = 0; i < t.length; i++) t.indexOf(i) === -1 ? n *= e[i] : o *= e[i]; + return o < 32 && n > 1024; + }, ps = (e, t) => { + at(e.inputs[0].dims, t.axes, t.noopWithEmptyAxes) ? pm(e, t) : ts(e, t); + }, ms = (e, t) => { + at(e.inputs[0].dims, t.axes, t.noopWithEmptyAxes) ? um(e, t) : rs(e, t); + }, fs = (e, t) => { + at(e.inputs[0].dims, t.axes, t.noopWithEmptyAxes) ? dm(e, t) : ns(e, t); + }, hs = (e, t) => { + at(e.inputs[0].dims, t.axes, t.noopWithEmptyAxes) ? lm(e, t) : os(e, t); + }, gs = (e, t) => { + at(e.inputs[0].dims, t.axes, t.noopWithEmptyAxes) ? cm(e, t) : is(e, t); + }, ys = (e, t) => { + at(e.inputs[0].dims, t.axes, t.noopWithEmptyAxes) ? mm(e, t) : as(e, t); + }, bs = (e, t) => { + at(e.inputs[0].dims, t.axes, t.noopWithEmptyAxes) ? fm(e, t) : ss(e, t); + }, ws = (e, t) => { + at(e.inputs[0].dims, t.axes, t.noopWithEmptyAxes) ? hm(e, t) : us(e, t); + }, _s = (e, t) => { + at(e.inputs[0].dims, t.axes, t.noopWithEmptyAxes) ? gm(e, t) : ds(e, t); + }, vs = (e, t) => { + at(e.inputs[0].dims, t.axes, t.noopWithEmptyAxes) ? sm(e, t) : ls(e, t); + }; + }); + var $s, xs, Ss, uo, Ts = V(() => { + "use strict"; + Q(); + Ie(); + qr(); + $s = (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."); + }, xs = (e, t) => { + $s(e.inputs); + let r = (n, o, i) => { + let a = []; + for (let d = 0; d < n.rank; d++) (i.indexOf(d) >= 0 || i.length === 0) && a.push(`input_indices[${d}] = 0;`); + return [`${a.join(` + `)}`, `var value = ${n.getByIndices("input_indices")}; + var best_index : i32 = 0;`, `if (${n.getByIndices("input_indices")} ${t.selectLastIndex > 0 ? "<=" : "<"} value) { + value = ${n.getByIndices("input_indices")}; + best_index = i32(last_index); + }`, "", o.setByOffset("global_idx", "best_index")]; + }; + e.compute(jr("ArgMin", { hint: t.cacheKey, inputDependencies: ["rank"] }, [e.inputs[0]], r, [t.axis], 7, t.keepDims), { inputs: [0] }); + }, Ss = (e, t) => { + $s(e.inputs); + let r = (n, o, i) => { + let a = []; + for (let d = 0; d < n.rank; d++) (i.indexOf(d) >= 0 || i.length === 0) && a.push(`input_indices[${d}] = 0;`); + return [`${a.join(` + `)}`, `var value = ${n.getByIndices("input_indices")}; + var best_index : i32 = 0;`, `if (${n.getByIndices("input_indices")} ${t.selectLastIndex > 0 ? ">=" : ">"} value) { + value = ${n.getByIndices("input_indices")}; + best_index = i32(last_index); + }`, "", o.setByOffset("global_idx", "best_index")]; + }; + e.compute(jr("argMax", { hint: t.cacheKey, inputDependencies: ["rank"] }, [e.inputs[0]], r, [t.axis], 7, t.keepDims), { inputs: [0] }); + }, uo = (e) => J(e); + }); + var ym, bm, wm, _m, Wt, vm, Is, Kr = V(() => { + "use strict"; + Q(); + ie(); + Hr(); + ae(); + ym = (e, t) => { + let r = e[0], n = e[1], o = e[2], i = e[3], a = e[4], d = e[5]; + if (a && d) throw new Error("Attention cannot have both past and attention_bias"); + if (r.dims.length !== 3) throw new Error('Input "input" must have 3 dimensions'); + let l = r.dims[0], c = r.dims[1], m = r.dims[2]; + if (o.dims.length !== 1) throw new Error('Input "bias" is expected to have 1 dimensions'); + if (n.dims.length !== 2) throw new Error('Input "weights" is expected to have 2 dimensions'); + if (n.dims[0] !== m) throw new Error("Input 1 dimension 0 should have same length as dimension 2 of input 0"); + if (o.dims[0] !== n.dims[1]) throw new Error('Input "bias" dimension 0 should have same length as dimension 1 of input "weights"'); + let u = o.dims[0] / 3, h = u, w = h; + if (t.qkvHiddenSizes.length > 0) { + if (t.qkvHiddenSizes.length !== 3) throw new Error("qkv_hidden_sizes attribute should have 3 elements"); + for (let x of t.qkvHiddenSizes) if (x % t.numHeads !== 0) throw new Error("qkv_hidden_sizes should be divisible by num_heads"); + u = t.qkvHiddenSizes[0], h = t.qkvHiddenSizes[1], w = t.qkvHiddenSizes[2]; + } + let g = c; + if (u !== h) throw new Error("qkv_hidden_sizes first element should be same as the second"); + if (o.dims[0] !== u + h + w) throw new Error('Input "bias" dimension 0 should have same length as sum of Q/K/V hidden sizes'); + let y = 0; + if (a) { + if (h !== w) throw new Error('Input "past" expect k_hidden_size == v_hidden_size'); + if (a.dims.length !== 5) throw new Error('Input "past" must have 5 dimensions'); + if (a.dims[0] !== 2) throw new Error('Input "past" first dimension must be 2'); + if (a.dims[1] !== l) throw new Error('Input "past" second dimension must be batch_size'); + if (a.dims[2] !== t.numHeads) throw new Error('Input "past" third dimension must be num_heads'); + if (a.dims[4] !== h / t.numHeads) throw new Error('Input "past" fifth dimension must be k_hidden_size / num_heads'); + t.pastPresentShareBuffer || (y = a.dims[3]); + } + let S = g + y, $ = -1, v = 0; + if (i) throw new Error("Mask not supported"); + if (a) throw new Error("past is not supported"); + if (d) { + if (d.dims.length !== 4) throw new Error('Input "attention_bias" must have 4 dimensions'); + if (d.dims[0] !== l || d.dims[1] !== t.numHeads || d.dims[2] !== c || d.dims[3] !== S) throw new Error('Expect "attention_bias" shape (batch_size, num_heads, sequence_length, total_sequence_length)'); + } + return { batchSize: l, sequenceLength: c, pastSequenceLength: y, kvSequenceLength: g, totalSequenceLength: S, maxSequenceLength: $, inputHiddenSize: m, hiddenSize: u, vHiddenSize: w, headSize: Math.floor(u / t.numHeads), vHeadSize: Math.floor(w / t.numHeads), numHeads: t.numHeads, isUnidirectional: false, pastPresentShareBuffer: false, maskFilterValue: t.maskFilterValue, maskType: v, scale: t.scale, broadcastResPosBias: false, passPastInKv: false, qkvFormat: 1 }; + }, bm = (e, t, r) => { + let n = we(r), o = 64, i = r / n; + i < o && (o = 32); + let a = Math.ceil(r / n / o), d = [{ type: 1, data: 1 / r }, { type: 12, data: i }, { type: 12, data: a }], l = he(e.dataType, n), c = Ee(1, n), m = ["type"], u = (h) => { + let w = M("x", e.dataType, e.dims, n), g = Ee(e.dataType), y = [{ 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; + ${h.registerUniforms(y).declareVariables(w)} + ${h.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 = ${c}(-3.402823e+38f); + for (var i: u32 = 0; i < uniforms.elements_per_thread && i + local_offset < uniforms.d_comp; i++) { + thread_max_vector = max(${c}(x[offset + i]), thread_max_vector); + } + thread_max[local_idx] = ${(() => { + switch (n) { + 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: ${n}`); + } + })()}; + 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 = ${c}(0); + for (var i: u32 = 0; i < uniforms.elements_per_thread && i + local_offset < uniforms.d_comp; i++) { + sum_vector += exp(${c}(x[offset + i]) - max_value); + } + thread_sum[local_idx] = ${(() => { + switch (n) { + 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: ${n}`); + } + })()}; + 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] = ${w.type.value}(${g}(uniforms.d_inv)); + } + } else { + for (var i: u32 = 0; i < uniforms.elements_per_thread && i + local_offset < uniforms.d_comp; i++) { + var f32input = ${c}(x[offset + i]); + x[offset + i] = ${w.type.value}(exp(f32input - max_value) / sum); + } + } + }`; + }; + return { name: "AttentionProbsSoftmax", shaderCache: { hint: `${o};${l};${n}`, inputDependencies: m }, getShaderSource: u, getRunData: () => ({ outputs: [], dispatchGroup: { x: t }, programUniforms: d }) }; + }, wm = (e, t, r, n, o, i, a, d) => { + let l = d + i.kvSequenceLength, c = [i.batchSize, i.numHeads, i.sequenceLength, l], m = i.kvNumHeads === void 0 && e > 1 && n, u = m ? [i.batchSize, i.numHeads, l, i.headSize] : void 0, h = a.scale === 0 ? 1 / Math.sqrt(i.headSize) : a.scale, w = we(i.headSize), g = i.headSize / w, y = 12, S = { x: Math.ceil(l / y), y: Math.ceil(i.sequenceLength / y), z: i.batchSize * i.numHeads }, $ = [{ type: 12, data: i.sequenceLength }, { type: 12, data: g }, { type: 12, data: l }, { type: 12, data: i.numHeads }, { type: 1, data: h }, { type: 12, data: d }, { type: 12, data: i.kvSequenceLength }], v = m && n && k.size(n.dims) > 0, x = ["type", "type"]; + v && x.push("type"), o && x.push("type"); + let T = [{ dims: c, dataType: t.dataType, gpuDataType: 0 }]; + m && T.push({ dims: u, dataType: t.dataType, gpuDataType: 0 }); + let C = (A) => { + let P = E("q", t.dataType, t.dims, w), B = E("key", r.dataType, r.dims, w), N = [P, B]; + if (v) { + let se = E("past_key", n.dataType, n.dims, w); + N.push(se); + } + o && N.push(E("attention_bias", o.dataType, o.dims)); + let W = M("output", t.dataType, c), K = [W]; + m && K.push(M("present_key", t.dataType, u, w)); + let Z = Ee(1, w), ee = [{ 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 = ${y}u; + + var tileQ: array<${P.type.storage}, ${y * y}>; + var tileK: array<${P.type.storage}, ${y * y}>; + ${A.registerUniforms(ee).declareVariables(...N, ...K)} + ${A.mainStart([y, y, 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; + ${/* @__PURE__ */ (() => v && m ? ` + 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;`)()} + ${m ? "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; + ${/* @__PURE__ */ (() => v && m ? ` + 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];")()} + ${m ? "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 (w) { + 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: ${w}`); + } + })()}; + output[outputIdx] = ${W.type.value} (sum * uniforms.alpha) + ${o ? "attention_bias[outputIdx]" : "0.0"}; + } + }`; + }; + return { name: "AttentionProbs", shaderCache: { hint: `${w};${o !== void 0};${n !== void 0};${e}`, inputDependencies: x }, getRunData: () => ({ outputs: T, dispatchGroup: S, programUniforms: $ }), getShaderSource: C }; + }, _m = (e, t, r, n, o, i) => { + let a = i + o.kvSequenceLength, d = o.nReps ? o.nReps : 1, l = o.vHiddenSize * d, c = o.kvNumHeads == null && e > 1 && n, m = c ? [o.batchSize, o.numHeads, a, o.headSize] : void 0, u = [o.batchSize, o.sequenceLength, l], h = 12, w = { x: Math.ceil(o.vHeadSize / h), y: Math.ceil(o.sequenceLength / h), z: o.batchSize * o.numHeads }, g = [{ type: 12, data: o.sequenceLength }, { type: 12, data: a }, { type: 12, data: o.vHeadSize }, { type: 12, data: o.numHeads }, { type: 12, data: l }, { type: 12, data: i }, { type: 12, data: o.kvSequenceLength }], y = c && n && k.size(n.dims) > 0, S = ["type", "type"]; + y && S.push("type"); + let $ = [{ dims: u, dataType: t.dataType, gpuDataType: 0 }]; + c && $.push({ dims: m, dataType: t.dataType, gpuDataType: 0 }); + let v = (x) => { + let T = E("probs", t.dataType, t.dims), C = E("v", r.dataType, r.dims), A = [T, C]; + y && A.push(E("past_value", n.dataType, n.dims)); + let B = [M("output", t.dataType, u)]; + c && B.push(M("present_value", t.dataType, m)); + let N = [{ 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 = ${h}u; + var tileQ: array<${T.type.value}, ${h * h}>; + var tileK: array<${T.type.value}, ${h * h}>; + ${x.registerUniforms(N).declareVariables(...A, ...B)} + ${x.mainStart([h, h, 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; + ${/* @__PURE__ */ (() => y && c ? ` + 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; + `)()} + ${c ? "let presentValueOffset = headIdx * uniforms.N * uniforms.K + n;" : ""} + var value = ${T.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; + ${/* @__PURE__ */ (() => y && c ? ` + 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]; + `)()} + ${c ? "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: `${n !== void 0};${e}`, inputDependencies: S }, getRunData: () => ({ outputs: $, dispatchGroup: w, programUniforms: g }), getShaderSource: v }; + }, Wt = (e, t, r, n, o, i, a, d, l, c, m) => { + let u = Math.min(e.outputCount, 1 + (a ? 1 : 0) + (d ? 1 : 0)), h = c.kvNumHeads !== void 0 || u > 1 ? c.pastSequenceLength : 0, w = h + c.kvSequenceLength, g = l && k.size(l.dims) > 0 ? l : void 0, y = [t, r]; + c.kvNumHeads === void 0 && u > 1 && a && k.size(a.dims) > 0 && y.push(a), g && y.push(g); + let S = e.compute(wm(u, t, r, a, g, c, m, h), { inputs: y, outputs: c.kvNumHeads === void 0 && u > 1 ? [-1, 1] : [-1] })[0]; + e.compute(bm(S, c.batchSize * c.numHeads * c.sequenceLength, w), { inputs: [S], outputs: [] }); + let $ = [S, n]; + c.kvNumHeads === void 0 && u > 1 && d && k.size(d.dims) > 0 && $.push(d), e.compute(_m(u, S, n, d, c, h), { inputs: $, outputs: c.kvNumHeads === void 0 && u > 1 ? [0, 2] : [0] }); + }, vm = (e, t) => { + let r = [t.batchSize, t.numHeads, t.sequenceLength, t.headSize], n = t.sequenceLength, o = t.inputHiddenSize, i = t.headSize, a = 12, d = { x: Math.ceil(t.headSize / a), y: Math.ceil(t.sequenceLength / a), z: t.batchSize * t.numHeads }, l = [e.inputs[0], e.inputs[1], e.inputs[2]], c = [{ type: 12, data: n }, { type: 12, data: o }, { type: 12, data: i }, { type: 12, data: t.numHeads }, { type: 12, data: t.headSize }, { type: 12, data: t.hiddenSize }, { type: 12, data: t.hiddenSize + t.hiddenSize + t.vHiddenSize }], m = (u) => { + let h = M("output_q", l[0].dataType, r), w = M("output_k", l[0].dataType, r), g = M("output_v", l[0].dataType, r), y = E("input", l[0].dataType, l[0].dims), S = E("weight", l[1].dataType, l[1].dims), $ = E("bias", l[2].dataType, l[2].dims), v = y.type.storage, x = [{ name: "M", type: "u32" }, { name: "K", type: "u32" }, { name: "N", type: "u32" }, { name: "num_heads", type: "u32" }, { name: "head_size", type: "u32" }, { name: "hidden_size", type: "u32" }, { name: "ldb", type: "u32" }]; + return ` + const TILE_SIZE = ${a}u; + var tileInput: array<${v}, ${a * a}>; + var tileWeightQ: array<${v}, ${a * a}>; + var tileWeightK: array<${v}, ${a * a}>; + var tileWeightV: array<${v}, ${a * a}>; + ${u.registerUniforms(x).declareVariables(y, S, $, h, w, g)} + ${u.mainStart([a, a, 1])} + let batchIndex = workgroup_id.z / uniforms.num_heads; + let headNumber = workgroup_id.z % uniforms.num_heads; + let m = global_id.y; + let n = global_id.x; + + let inputOffset = batchIndex * (uniforms.M * uniforms.K) + m * uniforms.K; + let biasOffsetQ = headNumber * uniforms.head_size; + let biasOffsetK = uniforms.hidden_size + biasOffsetQ; + let biasOffsetV = uniforms.hidden_size + biasOffsetK; + + var valueQ = ${v}(0); + var valueK = ${v}(0); + var valueV = ${v}(0); + for (var w: u32 = 0u; w < uniforms.K; w += TILE_SIZE) { + if (m < uniforms.M && w + local_id.x < uniforms.K) { + tileInput[TILE_SIZE * local_id.y + local_id.x] = input[inputOffset + w + local_id.x]; + } + if (n < uniforms.N && w + local_id.y < uniforms.K) { + let offset = n + (w + local_id.y) * uniforms.ldb; + tileWeightQ[TILE_SIZE * local_id.y + local_id.x] = weight[biasOffsetQ + offset]; + tileWeightK[TILE_SIZE * local_id.y + local_id.x] = weight[biasOffsetK + offset]; + tileWeightV[TILE_SIZE * local_id.y + local_id.x] = weight[biasOffsetV + offset]; + } + workgroupBarrier(); + for (var k: u32 = 0u; k ({ outputs: [{ dims: r, dataType: e.inputs[0].dataType, gpuDataType: 0 }, { dims: r, dataType: e.inputs[0].dataType, gpuDataType: 0 }, { dims: r, dataType: e.inputs[0].dataType, gpuDataType: 0 }], dispatchGroup: d, programUniforms: c }), getShaderSource: m }, { inputs: l, outputs: [-1, -1, -1] }); + }, Is = (e, t) => { + let r = ym(e.inputs, t), [n, o, i] = vm(e, r); + return Wt(e, n, o, i, e.inputs[4], void 0, void 0, void 0, e.inputs[5], r, t); + }; + }); + var $m, xm, Sm, Cs, As = V(() => { + "use strict"; + Ke(); + Q(); + ie(); + Ie(); + ae(); + $m = (e, t) => { + if (!e || e.length !== 5) throw new Error("BatchNormalization requires 5 inputs"); + let r = (n, o, i) => { + let a = o.length; + if (a !== n.length) throw new Error(`${i}: num dimensions != ${a}`); + o.forEach((d, l) => { + if (d !== n[l]) throw new Error(`${i}: dim[${l}] do not match`); + }); + }; + if (e[0].dims.length > 1) { + let n = t.format === "NHWC" ? t.spatial ? e[0].dims.slice(-1) : e[0].dims.slice(-1).concat(e[0].dims.slice(1, e[0].dims.length - 1)) : e[0].dims.slice(1, t.spatial ? 2 : void 0); + r(e[1].dims, n, "Invalid input scale"), r(e[2].dims, n, "Invalid input B"), r(e[3].dims, n, "Invalid input mean"), r(e[4].dims, n, "Invalid input var"); + } else r(e[1].dims, [1], "Invalid input scale"), r(e[2].dims, [1], "Invalid input B"), r(e[3].dims, [1], "Invalid input mean"), r(e[4].dims, [1], "Invalid input var"); + }, xm = (e, t) => { + let { epsilon: r, spatial: n, format: o } = t, i = e[0].dims, a = n ? we(i[i.length - 1]) : 1, d = o === "NHWC" && i.length > 1 ? a : 1, l = k.size(i) / a, c = n, m = c ? i.length : i, u = E("x", e[0].dataType, e[0].dims, a), h = E("scale", e[1].dataType, e[1].dims, d), w = E("bias", e[2].dataType, e[2].dims, d), g = E("inputMean", e[3].dataType, e[3].dims, d), y = E("inputVar", e[4].dataType, e[4].dims, d), S = M("y", e[0].dataType, m, a), $ = () => { + let x = ""; + if (n) x = `let cOffset = ${i.length === 1 ? "0u" : o === "NHWC" ? `outputIndices[${i.length - 1}] / ${a}` : "outputIndices[1]"};`; + else if (o === "NCHW") x = ` + ${S.indicesSet("outputIndices", "0", "0")} + let cOffset = ${S.indicesToOffset("outputIndices")};`; + else { + x = `var cIndices = ${h.type.indices}(0); + cIndices[0] = outputIndices[${i.length - 1}];`; + for (let T = 1; T < h.rank; T++) x += `cIndices[${T}] = outputIndices[${T}];`; + x += `let cOffset = ${h.indicesToOffset("cIndices")};`; + } + return x; + }, v = (x) => ` + const epsilon = ${r}; + ${x.registerUniform("outputSize", "u32").declareVariables(u, h, w, g, y, S)} + ${x.mainStart()} + ${x.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.outputSize")} + var outputIndices = ${S.offsetToIndices(`global_idx * ${a}`)}; + ${$()} + let scale = ${h.getByOffset("cOffset")}; + let bias = ${w.getByOffset("cOffset")}; + let inputMean = ${g.getByOffset("cOffset")}; + let inputVar = ${y.getByOffset("cOffset")}; + let x = ${u.getByOffset("global_idx")}; + let value = (x - inputMean) * inverseSqrt(inputVar + epsilon) * scale + bias; + ${S.setByOffset("global_idx", "value")} + }`; + return { name: "BatchNormalization", shaderCache: { hint: `${t.epsilon}_${t.format}_${n}_${a}`, inputDependencies: c ? ["rank", "type", "type", "type", "type"] : void 0 }, getShaderSource: v, getRunData: () => ({ outputs: [{ dims: e[0].dims, dataType: e[0].dataType }], dispatchGroup: { x: Math.ceil(l / 64) }, programUniforms: c ? [{ type: 12, data: l }, ...R(i)] : [{ type: 12, data: l }] }) }; + }, Sm = (e) => J(e), Cs = (e, t) => { + let { inputs: r, outputCount: n } = e, o = Sm({ ...t, outputCount: n }); + if (_e.webgpu.validateInputContent && $m(r, o), t.trainingMode) throw new Error("BatchNormalization trainingMode is not supported yet."); + e.compute(xm(r, o)); + }; + }); + var Tm, Im, ks, Es = V(() => { + "use strict"; + ie(); + ae(); + Tm = (e) => { + if (e[0].dims.length !== 3) throw new Error("input should have 3 dimensions"); + if (![320, 640, 1280].includes(e[0].dims[2])) throw new Error("number of channels should be 320, 640 or 1280"); + if (e[1].dims.length !== 1) throw new Error("bias is expected to have 1 dimensions"); + if (e[0].dims[2] !== e[1].dims[0]) throw new Error("last dimension of input and bias are not the same"); + }, Im = (e) => { + let t = e[0].dims, r = e[0].dims[2], n = k.size(t) / 4, o = e[0].dataType, i = E("input", o, t, 4), a = E("bias", o, [r], 4), d = E("residual", o, t, 4), l = M("output", o, t, 4); + return { name: "BiasAdd", getRunData: () => ({ outputs: [{ dims: t, dataType: e[0].dataType }], dispatchGroup: { x: Math.ceil(n / 64) } }), getShaderSource: (m) => ` + const channels = ${r}u / 4; + ${m.declareVariables(i, a, d, l)} + + ${m.mainStart()} + ${m.guardAgainstOutOfBoundsWorkgroupSizes(n)} + let value = ${i.getByOffset("global_idx")} + + ${a.getByOffset("global_idx % channels")} + ${d.getByOffset("global_idx")}; + ${l.setByOffset("global_idx", "value")} + }` }; + }, ks = (e) => { + Tm(e.inputs), e.compute(Im(e.inputs)); + }; + }); + var Cm, ge, Ps, zs, Os, Bs, Ds, Ms, Rs, Us, Vs, Am, Ns, Ws, Ls, Hs, Qt, Gs, Yr, Fs, qs, js, Ks, Ys, Xs, Zs, Qs, Js, eu, tu, ru, nu, ou, iu, au, su, uu, lo, co, du, lu, cu, km, Em, pu, Xr = V(() => { + "use strict"; + Q(); + ie(); + Ie(); + ae(); + Cm = (e, t, r, n, o, i, a) => { + let d = Math.ceil(t / 4), l = ""; + typeof o == "string" ? l = `${o}(a)` : l = o("a"); + let c = E("inputData", r, [d], 4), m = M("outputData", n, [d], 4), u = [{ name: "vec_size", type: "u32" }]; + return a && u.push(...a), ` + ${e.registerUniforms(u).declareVariables(c, m)} + + ${i ?? ""} + + ${e.mainStart()} + ${e.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.vec_size")} + + let a = ${c.getByOffset("global_idx")}; + ${m.setByOffset("global_idx", l)} + }`; + }, ge = (e, t, r, n, o, i = e.dataType, a, d) => { + let l = [{ type: 12, data: Math.ceil(k.size(e.dims) / 4) }]; + return a && l.push(...a), { name: t, shaderCache: { hint: o, inputDependencies: ["type"] }, getShaderSource: (c) => Cm(c, k.size(e.dims), e.dataType, i, r, n, d), getRunData: (c) => ({ outputs: [{ dims: e.dims, dataType: i }], dispatchGroup: { x: Math.ceil(k.size(c[0].dims) / 64 / 4) }, programUniforms: l }) }; + }, Ps = (e) => { + e.compute(ge(e.inputs[0], "Abs", "abs")); + }, zs = (e) => { + e.compute(ge(e.inputs[0], "Acos", "acos")); + }, Os = (e) => { + e.compute(ge(e.inputs[0], "Acosh", "acosh")); + }, Bs = (e) => { + e.compute(ge(e.inputs[0], "Asin", "asin")); + }, Ds = (e) => { + e.compute(ge(e.inputs[0], "Asinh", "asinh")); + }, Ms = (e) => { + e.compute(ge(e.inputs[0], "Atan", "atan")); + }, Rs = (e) => { + e.compute(ge(e.inputs[0], "Atanh", "atanh")); + }, Us = (e) => J(e), Vs = (e, t) => { + let r; + switch (t.to) { + case 10: + r = "vec4"; + break; + case 1: + r = "vec4"; + break; + case 12: + r = "vec4"; + break; + case 6: + r = "vec4"; + break; + case 9: + r = "vec4"; + break; + default: + throw new RangeError(`not supported type (specified in attribute 'to' from 'Cast' operator): ${t.to}`); + } + e.compute(ge(e.inputs[0], "Cast", r, void 0, t.cacheKey, t.to)); + }, Am = (e) => { + let t, r, n = e.length >= 2 && e[1].data !== 0, o = e.length >= 3 && e[2].data !== 0; + switch (e[0].dataType) { + case 1: + t = n ? e[1].getFloat32Array()[0] : -34028234663852886e22, r = o ? e[2].getFloat32Array()[0] : 34028234663852886e22; + break; + case 10: + t = n ? e[1].getUint16Array()[0] : 64511, r = o ? e[2].getUint16Array()[0] : 31743; + break; + default: + throw new Error("Unsupport data type"); + } + return J({ min: t, max: r }); + }, Ns = (e, t) => { + let r = t || Am(e.inputs), n = Ee(e.inputs[0].dataType); + e.compute(ge(e.inputs[0], "Clip", (o) => `clamp(${o}, vec4<${n}>(uniforms.min), vec4<${n}>(uniforms.max))`, void 0, r.cacheKey, void 0, [{ type: e.inputs[0].dataType, data: r.min }, { type: e.inputs[0].dataType, data: r.max }], [{ name: "min", type: n }, { name: "max", type: n }]), { inputs: [0] }); + }, Ws = (e) => { + e.compute(ge(e.inputs[0], "Ceil", "ceil")); + }, Ls = (e) => { + e.compute(ge(e.inputs[0], "Cos", "cos")); + }, Hs = (e) => { + e.compute(ge(e.inputs[0], "Cosh", "cosh")); + }, Qt = (e) => J(e), Gs = (e, t) => { + let r = Ee(e.inputs[0].dataType); + e.compute(ge(e.inputs[0], "Elu", (n) => `elu_vf32(${n})`, ` + const elu_alpha_ = ${r}(${t.alpha}); + + fn elu_f32(a: ${r}) -> ${r} { + return select((exp(a) - 1.0) * elu_alpha_, a, a >= 0.0); + } + + fn elu_vf32(v: vec4<${r}>) -> vec4<${r}> { + return vec4(elu_f32(v.x), elu_f32(v.y), elu_f32(v.z), elu_f32(v.w)); + }`, t.cacheKey)); + }, Yr = (e = "f32") => ` + const r0: ${e} = 0.3275911; + const r1: ${e} = 0.254829592; + const r2: ${e} = -0.284496736; + const r3: ${e} = 1.421413741; + const r4: ${e} = -1.453152027; + const r5: ${e} = 1.061405429; + + fn erf_vf32(v: vec4<${e}>) -> vec4<${e}> { + let absv = abs(v); + let x = 1.0 / (1.0 + r0 * absv); + return sign(v) * (1.0 - ((((r5 * x + r4) * x + r3) * x + r2) * x + r1) * x * exp(-absv * absv)); + }`, Fs = (e) => { + let t = Ee(e.inputs[0].dataType); + e.compute(ge(e.inputs[0], "Erf", (r) => `erf_vf32(${r})`, Yr(t))); + }, qs = (e) => { + e.compute(ge(e.inputs[0], "Exp", "exp")); + }, js = (e) => { + e.compute(ge(e.inputs[0], "Floor", "floor")); + }, Ks = (e) => { + let t = Ee(e.inputs[0].dataType); + e.compute(ge(e.inputs[0], "Gelu", (r) => `0.5 * ${r} * (1.0 + erf_vf32(${r} * 0.7071067811865475))`, Yr(t))); + }, Ys = (e, t) => { + let r = Ee(e.inputs[0].dataType); + e.compute(ge(e.inputs[0], "LeakyRelu", (n) => `select(leaky_relu_alpha_ * ${n}, ${n}, ${n} >= vec4<${r}>(0.0))`, `const leaky_relu_alpha_ = ${r}(${t.alpha});`, t.cacheKey)); + }, Xs = (e) => { + e.compute(ge(e.inputs[0], "Not", (t) => `!${t}`)); + }, Zs = (e) => { + e.compute(ge(e.inputs[0], "Neg", (t) => `-${t}`)); + }, Qs = (e) => { + e.compute(ge(e.inputs[0], "Reciprocal", (t) => `1.0/${t}`)); + }, Js = (e) => { + let t = Ee(e.inputs[0].dataType); + e.compute(ge(e.inputs[0], "Relu", (r) => `select(vec4<${t}>(0.0), ${r}, ${r} > vec4<${t}>(0.0))`)); + }, eu = (e) => { + e.compute(ge(e.inputs[0], "Sigmoid", (t) => `(1.0 / (1.0 + exp(-${t})))`)); + }, tu = (e) => J(e), ru = (e, t) => { + let r = Ee(e.inputs[0].dataType); + e.compute(ge(e.inputs[0], "HardSigmoid", (n) => `max(vec4<${r}>(0.0), min(vec4<${r}>(1.0), ${t.alpha} * ${n} + vec4<${r}>(${t.beta})))`, void 0, t.cacheKey)); + }, nu = (e) => { + e.compute(ge(e.inputs[0], "Sin", "sin")); + }, ou = (e) => { + e.compute(ge(e.inputs[0], "Sinh", "sinh")); + }, iu = (e) => { + e.compute(ge(e.inputs[0], "Sqrt", "sqrt")); + }, au = (e) => { + e.compute(ge(e.inputs[0], "Tan", "tan")); + }, su = (e) => `sign(${e}) * (1 - exp(-2 * abs(${e}))) / (1 + exp(-2 * abs(${e})))`, uu = (e) => { + e.compute(ge(e.inputs[0], "Tanh", su)); + }, lo = (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 ${su("v")}; + } + `, co = (e) => `(fast_gelu_a + fast_gelu_a * tanh_v(${e} * (fast_gelu_c * ${e} * ${e} + fast_gelu_b))) * ${e}`, du = (e) => { + let t = Ee(e.inputs[0].dataType); + e.compute(ge(e.inputs[0], "FastGelu", co, lo(t), void 0, e.inputs[0].dataType)); + }, lu = (e, t) => { + let r = Ee(e.inputs[0].dataType); + return e.compute(ge(e.inputs[0], "ThresholdedRelu", (n) => `select(vec4<${r}>(0.0), ${n}, ${n} > thresholded_relu_alpha_)`, `const thresholded_relu_alpha_ = vec4<${r}>(${t.alpha});`, t.cacheKey)), 0; + }, cu = (e) => { + e.compute(ge(e.inputs[0], "Log", "log")); + }, km = (e, t) => ` + const alpha = vec4<${e}>(${t}); + const one = ${e}(1.0); + const zero = ${e}(0.0); + + fn quick_gelu_impl(x: vec4<${e}>) -> vec4<${e}> { + let v = x *alpha; + var x1 : vec4<${e}>; + for (var i = 0; i < 4; i = i + 1) { + if (v[i] >= zero) { + x1[i] = one / (one + exp(-v[i])); + } else { + x1[i] = one - one / (one + exp(v[i])); + } + } + return x * x1; + } + `, Em = (e) => `quick_gelu_impl(${e})`, pu = (e, t) => { + let r = Ee(e.inputs[0].dataType); + e.compute(ge(e.inputs[0], "QuickGelu", Em, km(r, t.alpha), t.cacheKey, e.inputs[0].dataType)); + }; + }); + var Pm, zm, fu, hu = V(() => { + "use strict"; + ie(); + ae(); + Xr(); + Pm = (e) => { + if (e[0].dims.length !== 3) throw new Error("input should have 3 dimensions"); + if (![2560, 5120, 10240].includes(e[0].dims[2])) throw new Error("hidden state should be 2560, 5120 or 10240"); + if (e[1].dims.length !== 1) throw new Error("bias is expected to have 1 dimensions"); + if (e[0].dims[2] !== e[1].dims[0]) throw new Error("last dimension of input and bias are not the same"); + }, zm = (e) => { + let t = e[0].dims.slice(); + t[2] = t[2] / 2; + let r = E("input", e[0].dataType, e[0].dims, 4), n = E("bias", e[0].dataType, [e[0].dims[2]], 4), o = M("output", e[0].dataType, t, 4), i = k.size(t) / 4, a = he(e[0].dataType); + return { name: "BiasSplitGelu", getRunData: () => ({ outputs: [{ dims: t, dataType: e[0].dataType }], dispatchGroup: { x: Math.ceil(i / 64) } }), getShaderSource: (l) => ` + const M_SQRT2 = sqrt(2.0); + const halfChannels = ${e[0].dims[2] / 4 / 2}u; + + ${l.declareVariables(r, n, o)} + + ${Yr(a)} + + ${l.mainStart()} + ${l.guardAgainstOutOfBoundsWorkgroupSizes(i)} + let biasIdx = global_idx % halfChannels; + let batchIndex = global_idx / halfChannels; + let inputOffset = biasIdx + batchIndex * halfChannels * 2; + let valueLeft = input[inputOffset] + bias[biasIdx]; + let valueRight = input[inputOffset + halfChannels] + bias[biasIdx + halfChannels]; + let geluRight = valueRight * 0.5 * (erf_vf32(valueRight / M_SQRT2) + 1); + + ${o.setByOffset("global_idx", "valueLeft * geluRight")} + }` }; + }, fu = (e) => { + Pm(e.inputs), e.compute(zm(e.inputs)); + }; + }); + var Om, Bm, st, gu, yu, bu, wu, _u, vu, $u, xu, Su, Tu, Iu = V(() => { + "use strict"; + Q(); + ie(); + ae(); + Om = (e, t, r, n, o, i, a, d, l, c, m, u) => { + let h, w; + typeof d == "string" ? h = w = (v, x) => `${d}((${v}),(${x}))` : typeof d == "function" ? h = w = d : (h = d.scalar, w = d.vector); + let g = M("outputData", m, n.length, 4), y = E("aData", l, t.length, 4), S = E("bData", c, r.length, 4), $; + if (o) if (i) { + let v = k.size(t) === 1, x = k.size(r) === 1, T = t.length > 0 && t[t.length - 1] % 4 === 0, C = r.length > 0 && r[r.length - 1] % 4 === 0; + v || x ? $ = g.setByOffset("global_idx", w(v ? `${y.type.value}(${y.getByOffset("0")}.x)` : y.getByOffset("global_idx"), x ? `${S.type.value}(${S.getByOffset("0")}.x)` : S.getByOffset("global_idx"))) : $ = ` + let outputIndices = ${g.offsetToIndices("global_idx * 4u")}; + let offsetA = ${y.broadcastedIndicesToOffset("outputIndices", g)}; + let offsetB = ${S.broadcastedIndicesToOffset("outputIndices", g)}; + ${g.setByOffset("global_idx", w(a || T ? y.getByOffset("offsetA / 4u") : `${y.type.value}(${y.getByOffset("offsetA / 4u")}[offsetA % 4u])`, a || C ? S.getByOffset("offsetB / 4u") : `${S.type.value}(${S.getByOffset("offsetB / 4u")}[offsetB % 4u])`))} + `; + } else $ = g.setByOffset("global_idx", w(y.getByOffset("global_idx"), S.getByOffset("global_idx"))); + else { + if (!i) throw new Error("no necessary to use scalar implementation for element-wise binary op implementation."); + let v = (x, T, C = "") => { + let A = `aData[indexA${T}][componentA${T}]`, P = `bData[indexB${T}][componentB${T}]`; + return ` + let outputIndices${T} = ${g.offsetToIndices(`global_idx * 4u + ${T}u`)}; + let offsetA${T} = ${y.broadcastedIndicesToOffset(`outputIndices${T}`, g)}; + let offsetB${T} = ${S.broadcastedIndicesToOffset(`outputIndices${T}`, g)}; + let indexA${T} = offsetA${T} / 4u; + let indexB${T} = offsetB${T} / 4u; + let componentA${T} = offsetA${T} % 4u; + let componentB${T} = offsetB${T} % 4u; + ${x}[${T}] = ${C}(${h(A, P)}); + `; + }; + m === 9 ? $ = ` + var data = vec4(0); + ${v("data", 0, "u32")} + ${v("data", 1, "u32")} + ${v("data", 2, "u32")} + ${v("data", 3, "u32")} + outputData[global_idx] = dot(vec4(0x1, 0x100, 0x10000, 0x1000000), vec4(data));` : $ = ` + ${v("outputData[global_idx]", 0)} + ${v("outputData[global_idx]", 1)} + ${v("outputData[global_idx]", 2)} + ${v("outputData[global_idx]", 3)} + `; + } + return ` + ${e.registerUniform("vec_size", "u32").declareVariables(y, S, g)} + + ${u ?? ""} + + ${e.mainStart()} + ${e.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.vec_size")} + ${$} + }`; + }, Bm = (e, t, r, n, o, i, a = r.dataType) => { + let d = !k.areEqual(r.dims, n.dims), l = r.dims, c = k.size(r.dims), m = false, u = false, h = [d]; + if (d) { + let w = rt.calcShape(r.dims, n.dims, false); + if (!w) throw new Error("Can't perform binary op on the given tensors"); + l = w, c = k.size(l); + let g = k.size(r.dims) === 1, y = k.size(n.dims) === 1, S = r.dims.length > 0 && r.dims[r.dims.length - 1] % 4 === 0, $ = n.dims.length > 0 && n.dims[n.dims.length - 1] % 4 === 0; + h.push(g), h.push(y), h.push(S), h.push($); + let v = 1; + for (let x = 1; x < l.length; x++) { + let T = r.dims[r.dims.length - x] ?? 1, C = n.dims[n.dims.length - x] ?? 1; + if (T === C) v *= T; + else break; + } + v % 4 === 0 ? (u = true, m = true) : (g || y || S || $) && (m = true); + } else m = true; + return h.push(m), { name: e, shaderCache: { hint: t + h.map((w) => w.toString()).join("_"), inputDependencies: ["rank", "rank"] }, getShaderSource: (w) => Om(w, r.dims, n.dims, l, m, d, u, o, r.dataType, n.dataType, a, i), getRunData: () => ({ outputs: [{ dims: l, dataType: a }], dispatchGroup: { x: Math.ceil(c / 64 / 4) }, programUniforms: [{ type: 12, data: Math.ceil(k.size(l) / 4) }, ...R(r.dims, n.dims, l)] }) }; + }, st = (e, t, r, n, o, i) => { + e.compute(Bm(t, o ?? "", e.inputs[0], e.inputs[1], r, n, i)); + }, gu = (e) => { + st(e, "Add", (t, r) => `${t}+${r}`); + }, yu = (e) => { + st(e, "Div", (t, r) => `${t}/${r}`); + }, bu = (e) => { + st(e, "Equal", { scalar: (t, r) => `u32(${t}==${r})`, vector: (t, r) => `vec4(${t}==${r})` }, void 0, void 0, 9); + }, wu = (e) => { + st(e, "Mul", (t, r) => `${t}*${r}`); + }, _u = (e) => { + let t = E("input", e.inputs[0].dataType, e.inputs[0].dims).type.value; + st(e, "Pow", { scalar: (n, o) => `pow_custom(${n},${o})`, vector: (n, o) => `pow_vector_custom(${n},${o})` }, ` + fn pow_custom(a : ${t}, b : ${t}) -> ${t} { + if (b == ${t}(0.0)) { + return ${t}(1.0); + } else if (a < ${t}(0.0) && f32(b) != floor(f32(b))) { + return ${t}(pow(f32(a), f32(b))); // NaN + } + return select(sign(a), ${t}(1.0), round(f32(abs(b) % ${t}(2.0))) != 1.0) * ${t}(${t === "i32" ? "round" : ""}(pow(f32(abs(a)), f32(b)))); + } + fn pow_vector_custom(a : vec4<${t}>, b : vec4<${t}>) -> vec4<${t}> { + // TODO: implement vectorized pow + return vec4<${t}>(pow_custom(a.x, b.x), pow_custom(a.y, b.y), pow_custom(a.z, b.z), pow_custom(a.w, b.w)); + } + `); + }, vu = (e) => { + st(e, "Sub", (t, r) => `${t}-${r}`); + }, $u = (e) => { + st(e, "Greater", { scalar: (t, r) => `u32(${t}>${r})`, vector: (t, r) => `vec4(${t}>${r})` }, void 0, void 0, 9); + }, xu = (e) => { + st(e, "Less", { scalar: (t, r) => `u32(${t}<${r})`, vector: (t, r) => `vec4(${t}<${r})` }, void 0, void 0, 9); + }, Su = (e) => { + st(e, "GreaterOrEqual", { scalar: (t, r) => `u32(${t}>=${r})`, vector: (t, r) => `vec4(${t}>=${r})` }, void 0, void 0, 9); + }, Tu = (e) => { + st(e, "LessOrEqual", { scalar: (t, r) => `u32(${t}<=${r})`, vector: (t, r) => `vec4(${t}<=${r})` }, void 0, void 0, 9); + }; + }); + var Mm, Rm, Um, Vm, Cu, Au, ku = V(() => { + "use strict"; + Q(); + ie(); + Ie(); + ae(); + Mm = (e, t) => { + if (!e || e.length < 1) throw new Error("too few inputs"); + let r = 0, n = e[r], o = n.dataType, i = n.dims.length; + e.forEach((a, d) => { + if (d !== r) { + if (a.dataType !== o) throw new Error("input tensors should be one type"); + if (a.dims.length !== i) throw new Error("input tensors should have the same shape"); + a.dims.forEach((l, c) => { + if (c !== t && l !== n.dims[c]) throw new Error("non concat dimensions must match"); + }); + } + }); + }, Rm = (e, t) => ` + fn calculateInputIndex(index: u32) -> u32 { + let sizeInConcatAxis = array(${t}); + for (var i: u32 = 0u; i < ${e}; i += 1u ) { + if (index < sizeInConcatAxis[i]) { + return i; + } + } + return ${e}u; + }`, Um = (e, t) => { + let r = e.length, n = []; + for (let o = 0; o < r; ++o) { + let i = t.setByOffset("global_idx", e[o].getByIndices("indices")); + r === 1 ? n.push(i) : o === 0 ? n.push(`if (inputIndex == ${o}u) { ${i} }`) : o === r - 1 ? n.push(`else { ${i} }`) : n.push(`else if (inputIndex == ${o}) { ${i} }`); + } + return n.join(` + `); + }, Vm = (e, t, r, n) => { + let o = k.size(r), i = new Array(e.length), a = new Array(e.length), d = 0, l = [], c = [], m = [{ type: 12, data: o }]; + for (let y = 0; y < e.length; ++y) d += e[y].dims[t], i[y] = d, c.push(e[y].dims.length), a[y] = E(`input${y}`, n, c[y]), l.push("rank"), m.push({ type: 12, data: i[y] }); + for (let y = 0; y < e.length; ++y) m.push(...R(e[y].dims)); + m.push(...R(r)); + let u = M("output", n, r.length), h = u.indicesGet("indices", t), w = Array.from(Array(i.length).keys()).map((y) => `uniforms.sizeInConcatAxis${y}`).join(","), g = (y) => ` + + ${(() => { + y.registerUniform("outputSize", "u32"); + for (let S = 0; S < e.length; S++) y.registerUniform(`sizeInConcatAxis${S}`, "u32"); + return y.declareVariables(...a, u); + })()} + + ${Rm(i.length, w)} + + ${y.mainStart()} + ${y.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.outputSize")} + + var indices = ${u.offsetToIndices("global_idx")}; + + let inputIndex = calculateInputIndex(${h}); + if (inputIndex != 0u) { + let sizeInConcatAxis = array(${w}); + ${h} -= sizeInConcatAxis[inputIndex - 1u]; + } + + ${Um(a, u)} + }`; + return { name: "Concat", shaderCache: { hint: `${t}`, inputDependencies: l }, getRunData: () => ({ outputs: [{ dims: r, dataType: n }], dispatchGroup: { x: Math.ceil(o / 64) }, programUniforms: m }), getShaderSource: g }; + }, Cu = (e, t) => { + let r = e.inputs, n = r[0].dims, o = k.normalizeAxis(t.axis, n.length); + Mm(r, o); + let i = n.slice(); + i[o] = r.reduce((d, l) => d + (l.dims.length > o ? l.dims[o] : 0), 0); + let a = r.filter((d) => k.size(d.dims) > 0); + e.compute(Vm(a, o, i, r[0].dataType), { inputs: a }); + }, Au = (e) => J({ axis: e.axis }); + }); + var He, Ge, Fe, Zr, ct = V(() => { + "use strict"; + Q(); + ie(); + He = (e, t, r = "f32") => { + switch (e.activation) { + case "Relu": + return `value = max(value, ${t}(0.0));`; + case "Sigmoid": + return `value = (${t}(1.0) / (${t}(1.0) + exp(-value)));`; + case "Clip": + return `value = clamp(value, ${t}(${r}(uniforms.clip_min)), ${t}(${r}(uniforms.clip_max)));`; + case "HardSigmoid": + return `value = max(${t}(0.0), min(${t}(1.0), ${r}(uniforms.alpha) * value + ${r}(uniforms.beta)));`; + case "LeakyRelu": + return `value = select(${r}(uniforms.alpha) * value, value, value >= ${t}(0.0));`; + case "Tanh": + return `let e2x = exp(-2.0 * abs(value)); + value = sign(value) * (1.0 - e2x) / (1.0 + e2x); + `; + case "": + return ""; + default: + throw new Error(`Unsupported activation ${e.activation}`); + } + }, Ge = (e, t) => { + e.activation === "Clip" ? t.push({ type: 1, data: e.clipMax }, { type: 1, data: e.clipMin }) : e.activation === "HardSigmoid" ? t.push({ type: 1, data: e.alpha }, { type: 1, data: e.beta }) : e.activation === "LeakyRelu" && t.push({ type: 1, data: e.alpha }); + }, Fe = (e, t) => { + e.activation === "Clip" ? t.push({ name: "clip_max", type: "f32" }, { name: "clip_min", type: "f32" }) : e.activation === "HardSigmoid" ? t.push({ name: "alpha", type: "f32" }, { name: "beta", type: "f32" }) : e.activation === "LeakyRelu" && t.push({ name: "alpha", type: "f32" }); + }, Zr = (e) => { + let t = (e == null ? void 0 : e.activation) || ""; + if (t === "HardSigmoid") { + let [r, n] = (e == null ? void 0 : e.activation_params) || [0.2, 0.5]; + return { activation: t, alpha: r, beta: n }; + } else if (t === "Clip") { + let [r, n] = (e == null ? void 0 : e.activation_params) || [Ya, Xa]; + return { activation: t, clipMax: n, clipMin: r }; + } else if (t === "LeakyRelu") { + let [r] = (e == null ? void 0 : e.activation_params) || [0.01]; + return { activation: t, alpha: r }; + } + return { activation: t }; + }; + }); + var Oe, Qr, Jt = V(() => { + "use strict"; + Oe = (e, t) => { + switch (e) { + case 1: + return t; + case 2: + return `vec2<${t}>`; + case 3: + return `vec3<${t}>`; + case 4: + return `vec4<${t}>`; + default: + throw new Error(`${e}-component is not supported.`); + } + }, Qr = (e) => ` + ${e ? "value = value + getBiasByOutputCoords(coords);" : ""} + `; + }); + var Jr, po = V(() => { + "use strict"; + Jr = (e) => ` + fn getIndexFromCoords4D(coords : vec4, shape : vec4) -> i32 { + return dot(coords, vec4( + shape.y * shape.z * shape.w, shape.z * shape.w, shape.w, 1)); + } + fn getOutputIndexFromCoords(coords : vec4) -> i32 { + return dot(coords, vec4( + i32(${e}.x), i32(${e}.y), i32(${e}.z), 1)); + } + `; + }); + var Nm, Wm, er, Eu, Lm, tr, Hm, en, rr = V(() => { + "use strict"; + Q(); + ie(); + ae(); + ct(); + Jt(); + Nm = (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" : ""}); + `, Wm = (e, t) => e ? ` + let ACached0 = mm_Asub[k * innerElementSize][localRow]; + let ACached1 = mm_Asub[k * innerElementSize + 1][localRow]; + let ACached2 = mm_Asub[k * innerElementSize + 2][localRow]; + ${t === 3 ? "" : "let ACached3 = mm_Asub[k * innerElementSize + 3][localRow];"} + for (var i = 0; i < rowPerThread; i = i + 1) { + acc[i] = BCached0 * ACached0[i] + acc[i]; + acc[i] = BCached1 * ACached1[i] + acc[i]; + acc[i] = BCached2 * ACached2[i] + acc[i]; + ${t === 3 ? "" : "acc[i] = BCached3 * ACached3[i] + acc[i];"} + }` : ` + for (var i = 0; i < rowPerThread; i = i + 1) { + let ACached = mm_Asub[tileRow + i][k]; + acc[i] = BCached0 * ACached.x + acc[i]; + acc[i] = BCached1 * ACached.y + acc[i]; + acc[i] = BCached2 * ACached.z + acc[i]; + ${t === 3 ? "" : "acc[i] = BCached3 * ACached.w + acc[i];"} + }`, er = (e, t, r = "f32", n, o = false, i = 32, a = false, d = 32) => { + let l = t[1] * e[1], c = t[0] * e[0], m = o ? l : i, u = o ? i : l, h = m / t[0], w = i / t[1]; + if (!((o && h === 4 && e[1] === 4 || !o && (h === 3 || h === 4)) && m % t[0] === 0 && i % t[1] === 0 && e[0] === 4)) throw new Error(`If transposeA ${o} is true, innerElementSize ${h} and workPerThread[1] ${e[1]} must be 4. + Otherwise, innerElementSize ${h} must be 3 or 4. + tileAWidth ${m} must be divisible by workgroupSize[0]${t[0]}. tileInner ${i} must be divisible by workgroupSize[1] ${t[1]}. colPerThread ${e[0]} must be 4.`); + return ` + var mm_Asub: array, ${m / h}>, ${u}>; + var mm_Bsub: array, ${c / e[0]}>, ${i}>; + + const rowPerThread = ${e[1]}; + const colPerThread = ${e[0]}; + const innerElementSize = ${h}; + const tileInner = ${i}; + + @compute @workgroup_size(${t[0]}, ${t[1]}, ${t[2]}) + fn main(@builtin(local_invocation_id) localId : vec3, + @builtin(global_invocation_id) globalId : vec3, + @builtin(workgroup_id) workgroupId : vec3) { + let localRow = i32(localId.y); + let tileRow = localRow * rowPerThread; + let tileCol = i32(localId.x); + + let globalRow =i32(globalId.y) * rowPerThread; + let globalCol = i32(globalId.x); + let batch = ${a ? "0" : "i32(globalId.z)"}; + ${n ? `let batchIndices = ${n.offsetToIndices("u32(batch)")};` : ""} + let globalRowStart = i32(workgroupId.y) * ${l}; + + let num_tiles = ${a ? `${Math.ceil(d / i)}` : "(uniforms.dim_inner - 1) / tileInner + 1"}; + var kStart = ${a ? `i32(globalId.z) * ${d}` : "0"}; + + var acc: array, rowPerThread>; + + // Loop over shared dimension. + let tileRowB = localRow * ${w}; + for (var t = 0; t < num_tiles; t = t + 1) { + // Load one tile of A into local memory. + for (var innerRow = 0; innerRow < rowPerThread; innerRow = innerRow + 1) { + let inputRow = tileRow + innerRow; + let inputCol = tileCol; + ${Nm(o, n)} + } + + // Load one tile of B into local memory. + for (var innerRow = 0; innerRow < ${w}; innerRow = innerRow + 1) { + let inputRow = tileRowB + innerRow; + let inputCol = tileCol; + mm_Bsub[inputRow][inputCol] = mm_readB(batch, kStart + inputRow, globalCol${n ? ", batchIndices" : ""}); + } + kStart = kStart + tileInner; + workgroupBarrier(); + + // Compute acc values for a single thread. + for (var k = 0; k < tileInner / innerElementSize; k = k + 1) { + let BCached0 = mm_Bsub[k * innerElementSize][tileCol]; + let BCached1 = mm_Bsub[k * innerElementSize + 1][tileCol]; + let BCached2 = mm_Bsub[k * innerElementSize + 2][tileCol]; + ${h === 3 ? "" : "let BCached3 = mm_Bsub[k * innerElementSize + 3][tileCol];"} + + ${Wm(o, h)} + } + + workgroupBarrier(); + } + + for (var innerRow = 0; innerRow < rowPerThread; innerRow = innerRow + 1) { + mm_write(batch, globalRow + innerRow, globalCol, acc[innerRow]); + } + }`; + }, Eu = (e, t) => e ? ` + mm_Asub[inputRow][inputCol] = mm_readA(batch, + kStart + inputRow, + globalRowStart + inputCol${t ? ", batchIndices" : ""}); + ` : ` + mm_Asub[inputRow][inputCol] = mm_readA(batch, + globalRowStart + inputRow, + kStart + inputCol${t ? ", batchIndices" : ""}); + `, Lm = (e) => e ? "let ACached = mm_Asub[k][tileRow + innerRow];" : "let ACached = mm_Asub[tileRow + innerRow][k];", tr = (e, t, r = "f32", n, o = false, i = 32, a = false, d = 32, l = false) => { + let c = e[1] * t[1], m = e[0] * t[0], u = o ? c : i, h = o ? i : c; + if (!(h % t[1] === 0 && u % t[0] === 0 && i % t[1] === 0)) throw new Error(`tileAHight ${h} must be divisible by workgroupSize[1]${t[1]}, tileAWidth ${u} must be divisible by workgroupSize[0]${t[0]}, tileInner ${i} must be divisible by workgroupSize[1]${t[1]}`); + let w = h / t[1], g = u / t[0], y = i / t[1], S = l ? ` + let localRow = i32(localId.y); + let localCol = i32(localId.x); + let globalRowStart = i32(workgroupId.y) * ${c}; + let globalColStart = i32(workgroupId.x) * ${m}; + + // Loop over shared dimension. + for (var t = 0; t < num_tiles; t = t + 1) { + // Load one tile of A into local memory. + for (var inputRow = localRow; inputRow < ${h}; inputRow = inputRow + ${t[1]}) { + for (var inputCol = localCol; inputCol < ${u}; inputCol = inputCol + ${t[0]}) { + ${Eu(o, n)} + } + } + // Load one tile of B into local memory. + for (var inputRow = localRow; inputRow < ${i}; inputRow = inputRow + ${t[1]}) { + for (var inputCol = localCol; inputCol < ${m}; inputCol = inputCol + ${t[0]}) { + mm_Bsub[inputRow][inputCol] = mm_readB(batch, + kStart + inputRow, + globalColStart + inputCol${n ? ", batchIndices" : ""}); + } + } + kStart = kStart + tileInner; + workgroupBarrier(); + + // Compute acc values for a single thread. + var BCached : array<${r}, colPerThread>; + for (var k = 0; k < tileInner; k = k + 1) { + for (var inner = 0; inner < colPerThread; inner = inner + 1) { + BCached[inner] = mm_Bsub[k][localCol + inner * ${t[0]}]; + } + for (var innerRow = 0; innerRow < rowPerThread; innerRow = innerRow + 1) { + let ACached = ${o ? `mm_Asub[k][localRow + innerRow * ${t[1]}];` : `mm_Asub[localRow + innerRow * ${t[1]}][k];`} + for (var innerCol = 0; innerCol < colPerThread; innerCol = innerCol + 1) { + acc[innerRow][innerCol] = acc[innerRow][innerCol] + + ACached * BCached[innerCol]; + } + } + } + workgroupBarrier(); + } + for (var innerRow = 0; innerRow < rowPerThread; innerRow = innerRow + 1) { + let gRow = globalRowStart + localRow + innerRow * ${t[1]}; + for (var innerCol = 0; innerCol < colPerThread; innerCol = innerCol + 1) { + let gCol = globalColStart + localCol + innerCol * ${t[0]}; + mm_write(batch, gRow, gCol, acc[innerRow][innerCol]); + } + } + ` : ` + let tileRow = i32(localId.y) * rowPerThread; + let tileCol = i32(localId.x) * colPerThread; + + let globalRow = i32(globalId.y) * rowPerThread; + let globalCol = i32(globalId.x) * colPerThread; + let globalRowStart = i32(workgroupId.y) * ${c}; + + let tileRowA = i32(localId.y) * ${w}; + let tileColA = i32(localId.x) * ${g}; + let tileRowB = i32(localId.y) * ${y}; + // Loop over shared dimension. + for (var t = 0; t < num_tiles; t = t + 1) { + // Load one tile of A into local memory. + for (var innerRow = 0; innerRow < ${w}; innerRow = innerRow + 1) { + for (var innerCol = 0; innerCol < ${g}; innerCol = innerCol + 1) { + let inputRow = tileRowA + innerRow; + let inputCol = tileColA + innerCol; + ${Eu(o, n)} + } + } + + // Load one tile of B into local memory. + for (var innerRow = 0; innerRow < ${y}; innerRow = innerRow + 1) { + for (var innerCol = 0; innerCol < colPerThread; innerCol = innerCol + 1) { + let inputRow = tileRowB + innerRow; + let inputCol = tileCol + innerCol; + mm_Bsub[inputRow][inputCol] = mm_readB(batch, + kStart + inputRow, + globalCol + innerCol${n ? ", batchIndices" : ""}); + } + } + kStart = kStart + tileInner; + workgroupBarrier(); + + // Compute acc values for a single thread. + var BCached : array<${r}, colPerThread>; + for (var k = 0; k < tileInner; k = k + 1) { + for (var inner = 0; inner < colPerThread; inner = inner + 1) { + BCached[inner] = mm_Bsub[k][tileCol + inner]; + } + + for (var innerRow = 0; innerRow < rowPerThread; innerRow = innerRow + 1) { + ${Lm(o)} + for (var innerCol = 0; innerCol < colPerThread; innerCol = innerCol + 1) { + acc[innerRow][innerCol] = acc[innerRow][innerCol] + ACached * BCached[innerCol]; + } + } + } + + workgroupBarrier(); + } + + for (var innerRow = 0; innerRow < rowPerThread; innerRow = innerRow + 1) { + for (var innerCol = 0; innerCol < colPerThread; innerCol = innerCol + 1) { + mm_write(batch, globalRow + innerRow, globalCol + innerCol, + acc[innerRow][innerCol]); + } + } + `; + return ` + var mm_Asub : array, ${h}>; + var mm_Bsub : array, ${i}>; + const rowPerThread = ${e[1]}; + const colPerThread = ${e[0]}; + const tileInner = ${i}; + + @compute @workgroup_size(${t[0]}, ${t[1]}, ${t[2]}) + fn main(@builtin(local_invocation_id) localId : vec3, + @builtin(global_invocation_id) globalId : vec3, + @builtin(workgroup_id) workgroupId : vec3) { + let batch = ${a ? "0" : "i32(globalId.z)"}; + ${n ? `let batchIndices = ${n.offsetToIndices("u32(batch)")};` : ""} + let num_tiles = ${a ? `${Math.ceil(d / i)}` : "(uniforms.dim_inner - 1) / tileInner + 1"}; + var kStart = ${a ? `i32(globalId.z) * ${d}` : "0"}; + + var acc : array, rowPerThread>; + ${S} + } + `; + }, Hm = (e, t, r, n, o, i = false) => { + let [a, d, l] = o, [c, m, u, h] = n, w = Nt(a, l), g = Nt(d, l), y = he(n[0].type.tensor), S = () => { + let x = m.rank, T = c.rank, C = `var aIndices: ${m.type.indices};`; + for (let A = x - 2 - 1, P = T - 1; A >= 0; A--, P--) C += ` + aIndices[${A}] = ${T > 1 ? `batchIndices[${P}]` : "batchIndices"};`; + return w.forEach((A) => { + C += ` + aIndices[${A}] = 0;`; + }), C += ` + aIndices[${x - 2}] = u32(row); + aIndices[${x - 1}] = u32(colIn);`, C; + }, $ = () => { + let x = u.rank, T = c.rank, C = `var bIndices: ${u.type.indices};`; + for (let A = x - 2 - 1, P = T - 1; A >= 0; A--, P--) C += ` + bIndices[${A}] = ${T > 1 ? `batchIndices[${P}]` : "batchIndices"};`; + return g.forEach((A) => { + C += ` + bIndices[${A}] = 0;`; + }), C += ` + bIndices[${x - 2}] = u32(row); + bIndices[${x - 1}] = u32(colIn);`, C; + }; + return ` + fn mm_readA(batch: i32, row: i32, colIn: i32, batchIndices: ${c.type.indices}) -> ${Oe(e, y)} { + var value = ${Oe(e, y)}(0.0); + let col = colIn * ${e}; + if(row < uniforms.dim_a_outer && col < uniforms.dim_inner) + { + ${S()} + value = ${m.getByIndices("aIndices")}; + } + return value; + } + + fn mm_readB(batch: i32, row: i32, colIn: i32, batchIndices: ${c.type.indices}) -> ${Oe(e, y)} { + var value = ${Oe(e, y)}(0.0); + let col = colIn * ${e}; + if(row < uniforms.dim_inner && col < uniforms.dim_b_outer) + { + ${$()} + value = ${u.getByIndices("bIndices")}; + } + return value; + } + + fn mm_write(batch: i32, row: i32, colIn: i32, valueIn: ${Oe(e, y)}) { + let col = colIn * ${e}; + if (row < uniforms.dim_a_outer && col < uniforms.dim_b_outer) { + var value = valueIn; + let coords = vec3(batch, row, colIn); + ${t ? `value = value + ${i ? "bias[colIn]" : `${Oe(e, y)}(bias[row])`};` : ""} + ${r} + ${h.setByIndices("vec3(coords)", "value")} + } + } + `; + }, en = (e, t, r, n, o = false, i) => { + let a = e[0].dims, d = e[1].dims, l = a.slice(0, -2), c = d.slice(0, -2), m = n ? n.slice(0, -2) : r.slice(0, -2), u = k.size(m), h = a[a.length - 2], w = a[a.length - 1], g = d[d.length - 1], y = w % 4 === 0 && g % 4 === 0, S = h <= 8 ? [4, 1, 1] : [4, 4, 1], $ = [8, 8, 1], v = [Math.ceil(g / $[0] / S[0]), Math.ceil(h / $[1] / S[1]), Math.ceil(u / $[2] / S[2])], x = y ? 4 : 1, T = [...l, h, w / x], C = T.length, A = [...c, w, g / x], P = A.length, B = [u, h, g / x], N = [{ type: 6, data: h }, { type: 6, data: g }, { type: 6, data: w }]; + Ge(t, N), N.push(...R(m, T, A)); + let W = ["rank", "rank"], K = e.length > 2; + K && (N.push(...R(e[2].dims)), W.push("rank")), N.push(...R(B)); + let Z = (ee) => { + let se = m.length, de = Fr("batchDims", e[0].dataType, se, 1), Y = he(e[0].dataType), le = E("a", e[0].dataType, C, x), ce = E("b", e[1].dataType, P, x), q = M("result", e[0].dataType, B.length, x), ue = [le, ce]; + if (K) { + let G = o ? x : 1; + ue.push(E("bias", e[2].dataType, e[2].dims.length, G)); + } + let re = [{ name: "dim_a_outer", type: "i32" }, { name: "dim_b_outer", type: "i32" }, { name: "dim_inner", type: "i32" }]; + Fe(t, re); + let ne = he(q.type.tensor), oe = He(t, q.type.value, ne), U = Hm(x, K, oe, [de, le, ce, q], [l, c, m], o); + return ` + ${ee.registerUniforms(re).registerInternalVariables(de).declareVariables(...ue, q)} + ${U} + ${y ? er(S, $, Y, de) : tr(S, $, Y, de)} + `; + }; + return { name: "MatMul", shaderCache: { hint: `${S};${t.activation};${y};${o}`, inputDependencies: W }, getRunData: () => ({ outputs: [{ dims: i ? i(r) : r, dataType: e[0].dataType }], dispatchGroup: { x: v[0], y: v[1], z: v[2] }, programUniforms: N }), getShaderSource: Z }; + }; + }); + var Gm, Pu, zu = V(() => { + "use strict"; + Q(); + Xe(); + ae(); + ct(); + Jt(); + po(); + rr(); + Gm = (e, t, r, n, o = false, i, a = 4, d = 4, l = 4, c = "f32") => { + let m = (W) => { + switch (W) { + case 1: + return "resData = x[xIndex];"; + case 3: + return `resData = vec3<${c}>(x[xIndex], x[xIndex + 1], x[xIndex + 2]);`; + case 4: + return "resData = x[xIndex / 4];"; + default: + throw new Error(`innerElementSize ${W} is not supported.`); + } + }, u = (W) => { + switch (W) { + 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 ${W} is not supported.`); + } + }, h = e ? ` + let coord = vec4(batch, xRow, xCol, xCh); + ` : ` + let coord = vec4(batch, xCh, xRow, xCol); + `, w = e ? ` + let coords = vec4( + batch, + row / outWidth, + row % outWidth, + col); + ` : ` + let coords = vec4( + batch, + row, + col / outWidth, + col % outWidth); + `, g = e ? "i32(uniforms.x_shape[1])" : "i32(uniforms.x_shape[2])", y = e ? "i32(uniforms.x_shape[2])" : "i32(uniforms.x_shape[3])", S = e ? "row" : "col", $ = e ? "col" : "row", v = ` + let inChannels = i32(uniforms.w_shape[2]); + let outWidth = ${e ? "i32(uniforms.result_shape[2])" : "i32(uniforms.result_shape[3])"}; + let outRow = ${S} / outWidth; + let outCol = ${S} % outWidth; + + let WRow = ${$} / (i32(uniforms.w_shape[1]) * inChannels); + let WCol = ${$} / inChannels % i32(uniforms.w_shape[1]); + let xRow = outRow * uniforms.stride[0] + uniforms.dilation[0] * WRow - uniforms.pad[0]; + let xCol = outCol * uniforms.stride[1] + uniforms.dilation[1] * WCol - uniforms.pad[1]; + let xCh = ${$} % inChannels; + var resData = ${Oe(a, c)}(0.0); + // The bounds checking is always needed since we use it to pad zero for + // the 'same' padding type. + if (xRow >= 0 && xRow < ${g} && xCol >= 0 && xCol < ${y}) { + ${h} + let xIndex = getIndexFromCoords4D(coord, vec4(uniforms.x_shape)); + ${m(a)} + } + return resData;`, x = e ? t && n ? ` + let col = colIn * ${a}; + ${v}` : ` + let col = colIn * ${a}; + if (row < uniforms.dim_a_outer && col < uniforms.dim_inner) { + ${v} + } + return ${Oe(a, c)}(0.0);` : n && r ? ` + let col = colIn * ${a}; + ${v}` : ` + let col = colIn * ${a}; + if (row < uniforms.dim_inner && col < uniforms.dim_b_outer) { + ${v} + } + return ${Oe(a, c)}(0.0);`, T = `${u(d)}`, C = Oe(l, c), A = e ? Oe(a, c) : Oe(d, c), P = e ? Oe(d, c) : Oe(a, c), B = He(i, C, c); + return ` + fn mm_readA(batch: i32, row : i32, colIn : i32) -> ${A} { + ${e ? x : T} + } + + fn mm_readB(batch: i32, row : i32, colIn : i32) -> ${P} { + ${e ? T : x} + } + + fn mm_write(batch: i32, row : i32, colIn : i32, valueIn : ${C}) { + let col = colIn * ${l}; + if (row < uniforms.dim_a_outer && col < uniforms.dim_b_outer) + { + var value = valueIn; + let outWidth = ${e ? "i32(uniforms.result_shape[2])" : "i32(uniforms.result_shape[3])"}; + ${w} + ${Qr(o)} + ${B} + setOutputAtCoords(coords[0], coords[1], coords[2], coords[3], value); + } + }`; + }, Pu = (e, t, r, n, o, i, a, d, l) => { + let c = t.format === "NHWC", m = c ? e[0].dims[3] : e[0].dims[1], u = r[0], h = c ? r[2] : r[3], w = c ? r[1] : r[2], g = c ? r[3] : r[1], y = c && (m % 4 === 0 || m % 3 === 0) && g % 4 === 0, S = c ? g : h * w, $ = c ? h * w : g, v = [8, 8, 1], x = n <= 8 ? [4, 1, 1] : [4, 4, 1], T = [Math.ceil(S / v[0] / x[0]), Math.ceil($ / v[1] / x[1]), Math.ceil(u / v[2] / x[2])]; + me("verbose", () => `[conv2d_mm_webgpu] dispatch = ${T}`); + let C = y ? c && m % 4 !== 0 ? 3 : 4 : 1, A = v[1] * x[1], P = v[0] * x[0], B = Math.max(v[0] * C, v[1]), N = n % A === 0, W = o % P === 0, K = i % B === 0, Z = y ? [C, 4, 4] : [1, 1, 1], ee = [{ type: 6, data: n }, { type: 6, data: o }, { type: 6, data: i }, { type: 6, data: [t.pads[0], t.pads[1]] }, { type: 6, data: t.strides }, { type: 6, data: t.dilations }]; + Ge(t, ee), ee.push(...R(e[0].dims, e[1].dims)); + let se = ["rank", "rank"]; + a && (ee.push(...R(e[2].dims)), se.push("rank")), ee.push(...R(r)); + let de = (Y) => { + let le = [{ name: "dim_a_outer", type: "i32" }, { name: "dim_b_outer", type: "i32" }, { name: "dim_inner", type: "i32" }, { name: "pad", type: "i32", length: 2 }, { name: "stride", type: "i32", length: 2 }, { name: "dilation", type: "i32", length: 2 }]; + Fe(t, le); + let ce = y ? 4 : 1, q = he(e[0].dataType), ue = ` + fn setOutputAtIndex(flatIndex : i32, value : ${y ? `vec4<${q}>` : q}) { + result[flatIndex] = ${y ? `vec4<${q}>` : q}(value); + } + fn setOutputAtCoords(d0 : i32, d1 : i32, d2 : i32, d3 : i32, value : ${y ? `vec4<${q}>` : q}) { + let flatIndex = getOutputIndexFromCoords(vec4(d0, d1, d2, d3)); + setOutputAtIndex(flatIndex ${y ? "/ 4" : ""}, value); + }`, re = E("x", e[0].dataType, e[0].dims.length, C === 3 ? 1 : C), ne = E("w", e[1].dataType, e[1].dims.length, ce), oe = [re, ne], U = M("result", e[0].dataType, r.length, ce); + if (a) { + let G = E("bias", e[2].dataType, e[2].dims.length, ce); + oe.push(G), ue += ` + fn getBiasByOutputCoords(coords : vec4) -> ${y ? `vec4<${q}>` : q} { + return bias[coords.${c ? "w" : "y"}${y ? "/ 4" : ""}]; + }`; + } + return ` + ${Jr("uniforms.result_strides")} + //struct Uniforms { xShape : vec4, wShape : vec4, outShape : vec4, + // outShapeStrides: vec3, filterDims : vec2, pad : vec2, stride : vec2, + // dilation : vec2, dimAOuter : i32, dimBOuter : i32, dimInner : i32 }; + ${Y.registerUniforms(le).declareVariables(...oe, U)} + ${ue} + ${Gm(c, N, W, K, a, t, Z[0], Z[1], Z[2], q)} + ${y ? er(x, v, q, void 0, !c, B) : tr(x, v, q, void 0, !c, B, false, void 0, d)}`; + }; + return { name: "Conv2DMatMul", shaderCache: { hint: `${t.cacheKey};${C};${y};${N};${W};${K};${A};${P};${B}`, inputDependencies: se }, getRunData: () => ({ outputs: [{ dims: l ? l(r) : r, dataType: e[0].dataType }], dispatchGroup: { x: T[0], y: T[1], z: T[2] }, programUniforms: ee }), getShaderSource: de }; + }; + }); + var Fm, Ou, tn, qm, Bu, jm, Du, Mu, Ru = V(() => { + "use strict"; + Q(); + Xe(); + ie(); + ae(); + ct(); + Jt(); + Fm = (e) => { + let t = 1; + for (let r = 0; r < e.length; r++) t *= e[r]; + return t; + }, Ou = (e) => typeof e == "number" ? [e, e, e] : e, tn = (e, t) => t <= 1 ? e : e + (e - 1) * (t - 1), qm = (e, t, r, n = 1) => { + let o = tn(t, n); + return Math.floor((e[0] * (r - 1) - r + o) / 2); + }, Bu = (e, t, r, n, o) => { + o == null && (o = qm(e, t[0], n[0])); + let i = [0, 0, 0, r]; + for (let a = 0; a < 3; a++) e[a] + 2 * o >= t[a] && (i[a] = Math.trunc((e[a] - t[a] + 2 * o) / n[a] + 1)); + return i; + }, jm = (e, t, r, n, o, i, a, d, l, c) => { + let m, u, h, w; + if (e === "VALID" && (e = 0), typeof e == "number") { + m = { top: e, bottom: e, left: e, right: e, front: e, back: e }; + let g = Bu([t, r, n, 1], [d, l, c], 1, [o, i, a], e); + u = g[0], h = g[1], w = g[2]; + } else if (Array.isArray(e)) { + if (!e.every((y, S, $) => y === $[0])) throw Error(`Unsupported padding parameter: ${e}`); + m = { top: e[0], bottom: e[1], left: e[2], right: e[3], front: e[4], back: e[5] }; + let g = Bu([t, r, n, 1], [d, l, c], 1, [o, i, a], e[0]); + u = g[0], h = g[1], w = g[2]; + } else if (e === "SAME_UPPER") { + u = Math.ceil(t / o), h = Math.ceil(r / i), w = Math.ceil(n / a); + let g = (u - 1) * o + d - t, y = (h - 1) * i + l - r, S = (w - 1) * a + c - n, $ = Math.floor(g / 2), v = g - $, x = Math.floor(y / 2), T = y - x, C = Math.floor(S / 2), A = S - C; + m = { top: x, bottom: T, left: C, right: A, front: $, back: v }; + } else throw Error(`Unknown padding parameter: ${e}`); + return { padInfo: m, outDepth: u, outHeight: h, outWidth: w }; + }, Du = (e, t, r, n, o, i = false, a = "channelsLast") => { + let d, l, c, m, u; + if (a === "channelsLast") [d, l, c, m, u] = e; + else if (a === "channelsFirst") [d, u, l, c, m] = e; + else throw new Error(`Unknown dataFormat ${a}`); + let [h, , w, g, y] = t, [S, $, v] = Ou(r), [x, T, C] = Ou(n), A = tn(w, x), P = tn(g, T), B = tn(y, C), { padInfo: N, outDepth: W, outHeight: K, outWidth: Z } = jm(o, l, c, m, S, $, v, A, P, B), ee = i ? h * u : h, se = [0, 0, 0, 0, 0]; + return a === "channelsFirst" ? se = [d, ee, W, K, Z] : a === "channelsLast" && (se = [d, W, K, Z, ee]), { batchSize: d, dataFormat: a, inDepth: l, inHeight: c, inWidth: m, inChannels: u, outDepth: W, outHeight: K, outWidth: Z, outChannels: ee, padInfo: N, strideDepth: S, strideHeight: $, strideWidth: v, filterDepth: w, filterHeight: g, filterWidth: y, effectiveFilterDepth: A, effectiveFilterHeight: P, effectiveFilterWidth: B, dilationDepth: x, dilationHeight: T, dilationWidth: C, inShape: e, outShape: se, filterShape: t }; + }, Mu = (e, t, r, n, o, i) => { + let a = i === "channelsLast", d = a ? e[0].dims[3] : e[0].dims[1], l = false, c = [64, 1, 1], m = { x: r.map((v, x) => x) }, u = [Math.ceil(Fm(m.x.map((v) => r[v])) / c[0]), 1, 1]; + me("verbose", () => `[conv3d_naive_webgpu] dispatch = ${u}`); + let h = l ? a && d % 4 !== 0 ? 3 : 4 : 1, w = k.size(r), g = [{ type: 12, data: w }, { type: 12, data: n }, { type: 12, data: o }, { type: 12, data: t.strides }, { type: 12, data: t.dilations }]; + Ge(t, g), g.push(...R(e[0].dims, e[1].dims)); + let y = ["rank", "rank"], S = e.length === 3; + S && (g.push(...R(e[2].dims)), y.push("rank")), g.push(...R(r)); + let $ = (v) => { + let x = [{ name: "output_size", type: "u32" }, { name: "filter_dims", type: "u32", length: n.length }, { name: "pads", type: "u32", length: o.length }, { name: "strides", type: "u32", length: t.strides.length }, { name: "dilations", type: "u32", length: t.dilations.length }]; + Fe(t, x); + let T = l ? 4 : 1, C = he(e[0].dataType), A = E("x", e[0].dataType, e[0].dims.length, h === 3 ? 1 : h), P = E("W", e[1].dataType, e[1].dims.length, T), B = [A, P], N = M("result", e[0].dataType, r.length, T), W = ""; + if (S) { + let ee = E("bias", e[2].dataType, e[2].dims.length, T); + B.push(ee), W += ` + fn getBiasByOutputCoords(coords : array) -> ${l ? `vec4<${C}>` : C} { + return bias[${a ? F("coords", 4, 5) : F("coords", 1, 5)}${l ? "/ 4" : ""}]; + }`; + } + let K = Oe(h, C), Z = He(t, K, C); + return ` + ${W} + fn getX(d0 : u32, d1 : u32, d2 : u32, d3 : u32, d4 : u32) -> f32 { + let aIndices = array(d0, d1, d2, d3, d4); + return ${A.getByIndices("aIndices")}; + } + fn getW(d0 : u32, d1 : u32, d2 : u32, d3 : u32, d4 : u32) -> f32 { + let aIndices = array(d0, d1, d2, d3, d4); + return ${P.getByIndices("aIndices")}; + } + ${v.registerUniforms(x).declareVariables(...B, N)} + ${v.mainStart()} + ${v.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} + let coords = ${N.offsetToIndices("global_idx")}; + let batch = ${F("coords", 0, A.rank)}; + let d2 = ${a ? F("coords", A.rank - 1, A.rank) : F("coords", 1, A.rank)}; + let xFRCCorner = vec3(${a ? F("coords", 1, A.rank) : F("coords", 2, A.rank)}, + ${a ? F("coords", 2, A.rank) : F("coords", 3, A.rank)}, + ${a ? F("coords", 3, A.rank) : F("coords", 4, A.rank)}) * uniforms.strides - uniforms.pads; + let xFCorner = xFRCCorner.x; + let xRCorner = xFRCCorner.y; + let xCCorner = xFRCCorner.z; + let xShapeY = ${a ? F("uniforms.x_shape", 1, A.rank) : F("uniforms.x_shape", 2, A.rank)}; + let xShapeZ = ${a ? F("uniforms.x_shape", 2, A.rank) : F("uniforms.x_shape", 3, A.rank)}; + let xShapeW = ${a ? F("uniforms.x_shape", 3, A.rank) : F("uniforms.x_shape", 4, A.rank)}; + let xShapeU = ${a ? F("uniforms.x_shape", 4, A.rank) : F("uniforms.x_shape", 1, A.rank)}; + let inputDepthNearestVec4 = (xShapeU / 4) * 4; + let inputDepthVec4Remainder = xShapeU % 4; + + var value = 0.0; + for (var wF = 0u; wF < uniforms.filter_dims[0]; wF++) { + let xF = xFCorner + wF * uniforms.dilations[0]; + if (xF < 0 || xF >= xShapeY) { + continue; + } + + for (var wR = 0u; wR < uniforms.filter_dims[1]; wR++) { + let xR = xRCorner + wR * uniforms.dilations[1]; + if (xR < 0 || xR >= xShapeZ) { + continue; + } + + for (var wC = 0u; wC < uniforms.filter_dims[2]; wC++) { + let xC = xCCorner + wC * uniforms.dilations[2]; + if (xC < 0 || xC >= xShapeW) { + continue; + } + + for (var d1 = 0u; d1 < inputDepthNearestVec4; d1 += 4) { + ${a ? `let xValues = vec4( + getX(batch, xF, xR, xC, d1), + getX(batch, xF, xR, xC, d1 + 1), + getX(batch, xF, xR, xC, d1 + 2), + getX(batch, xF, xR, xC, d1 + 3)); + ` : `let xValues = vec4( + getX(batch, d1, xF, xR, xC), + getX(batch, d1 + 1, xF, xR, xC), + getX(batch, d1 + 2, xF, xR, xC), + getX(batch, d1 + 3, xF, xR, xC)); + `} + let wValues = vec4( + getW(d2, d1, wF, wR, wC), + getW(d2, d1 + 1, wF, wR, wC), + getW(d2, d1 + 2, wF, wR, wC), + getW(d2, d1 + 3, wF, wR, wC)); + value += dot(xValues, wValues); + } + if (inputDepthVec4Remainder == 1) { + ${a ? `value += getX(batch, xF, xR, xC, inputDepthNearestVec4) + * getW(d2, inputDepthNearestVec4, wF, wR, wC);` : `value += getX(batch, inputDepthNearestVec4, xF, xR, xC) + * getW(d2, inputDepthNearestVec4, wF, wR, wC);`} + } else if (inputDepthVec4Remainder == 2) { + ${a ? `let xValues = vec2( + getX(batch, xF, xR, xC, inputDepthNearestVec4), + getX(batch, xF, xR, xC, inputDepthNearestVec4 + 1)); + ` : `let xValues = vec2( + getX(batch, inputDepthNearestVec4, xF, xR, xC), + getX(batch, inputDepthNearestVec4 + 1, xF, xR, xC)); + `} + let wValues = vec2( + getW(d2, inputDepthNearestVec4, wF, wR, wC), + getW(d2, inputDepthNearestVec4 + 1, wF, wR, wC)); + value += dot(xValues, wValues); + } else if (inputDepthVec4Remainder == 3) { + ${a ? `let xValues = vec3( + getX(batch, xF, xR, xC, inputDepthNearestVec4), + getX(batch, xF, xR, xC, inputDepthNearestVec4 + 1), + getX(batch, xF, xR, xC, inputDepthNearestVec4 + 2)); + ` : `let xValues = vec3( + getX(batch, inputDepthNearestVec4, xF, xR, xC), + getX(batch, inputDepthNearestVec4 + 1, xF, xR, xC), + getX(batch, inputDepthNearestVec4 + 2, xF, xR, xC)); + `} + let wValues = vec3( + getW(d2, inputDepthNearestVec4, wF, wR, wC), + getW(d2, inputDepthNearestVec4 + 1, wF, wR, wC), + getW(d2, inputDepthNearestVec4 + 2, wF, wR, wC)); + value += dot(xValues, wValues); + } + } + } + } + ${S ? "value = value + getBiasByOutputCoords(coords)" : ""}; + ${Z} + result[global_idx] = f32(value); + }`; + }; + return { name: "Conv3DNaive", shaderCache: { hint: `${t.cacheKey};${a};${h};${S}`, inputDependencies: y }, getRunData: () => ({ outputs: [{ dims: r, dataType: e[0].dataType }], dispatchGroup: { x: u[0], y: u[1], z: u[2] }, programUniforms: g }), getShaderSource: $ }; + }; + }); + var Uu, Vu, Nu = V(() => { + "use strict"; + Q(); + ie(); + ae(); + ct(); + Uu = (e, t, r, n) => { + let o = e.length > 2, i = o ? "value += b[output_channel];" : "", a = e[0].dims, d = e[1].dims, l = t.format === "NHWC", c = l ? r[3] : r[1], m = c / t.group, u = l && m >= 4 ? we(c) : 1, h = k.size(r) / u, w = [{ type: 12, data: h }, { type: 12, data: t.dilations }, { type: 12, data: [t.strides[0], t.strides[1]] }, { type: 12, data: [t.pads[0], t.pads[1]] }, { type: 12, data: m }]; + Ge(t, w), w.push(...R(a, [d[0], d[1], d[2], d[3] / u])); + let g = o ? ["rank", "rank", "rank"] : ["rank", "rank"]; + w.push(...R([r[0], r[1], r[2], r[3] / u])); + let y = (S) => { + let $ = M("output", e[0].dataType, r.length, u), v = he($.type.tensor), x = He(t, $.type.value, v), T = E("x", e[0].dataType, a.length), C = E("w", e[1].dataType, d.length, u), A = [T, C]; + o && A.push(E("b", e[2].dataType, e[2].dims, u)); + let P = [{ name: "output_size", type: "u32" }, { name: "dilations", type: "u32", length: t.dilations.length }, { name: "strides", type: "u32", length: 2 }, { name: "pads", type: "u32", length: 2 }, { name: "output_channels_per_group", type: "u32" }]; + Fe(t, P); + let B = l ? ` + for (var wHeight: u32 = 0u; wHeight < uniforms.w_shape[0]; wHeight++) { + let xHeight = xRCCorner.x + wHeight * uniforms.dilations[0]; + + if (xHeight < 0u || xHeight >= uniforms.x_shape[1]) { + continue; + } + + for (var wWidth: u32 = 0u; wWidth < uniforms.w_shape[1]; wWidth++) { + let xWidth = xRCCorner.y + wWidth * uniforms.dilations[1]; + if (xWidth < 0u || xWidth >= uniforms.x_shape[2]) { + continue; + } + + for (var wInChannel: u32 = 0u; wInChannel < uniforms.w_shape[2]; wInChannel++) { + let input_channel = in_channel_offset + wInChannel; + let xVal = ${T.get("batch", "xHeight", "xWidth", "input_channel")}; + let wVal = ${C.get("wHeight", "wWidth", "wInChannel", "output_channel")}; + value += xVal * wVal; + } + } + } + ` : ` + for (var wInChannel: u32 = 0u; wInChannel < uniforms.w_shape[1]; wInChannel++) { + let input_channel = in_channel_offset + wInChannel; + for (var wHeight: u32 = 0u; wHeight < uniforms.w_shape[2]; wHeight++) { + let xHeight = xRCCorner.x + wHeight * uniforms.dilations[0]; + + if (xHeight < 0u || xHeight >= uniforms.x_shape[2]) { + continue; + } + + for (var wWidth: u32 = 0u; wWidth < uniforms.w_shape[3]; wWidth++) { + let xWidth = xRCCorner.y + wWidth * uniforms.dilations[1]; + if (xWidth < 0u || xWidth >= uniforms.x_shape[3]) { + continue; + } + + let xVal = ${T.get("batch", "input_channel", "xHeight", "xWidth")}; + let wVal = ${C.get("output_channel", "wInChannel", "wHeight", "wWidth")}; + value += xVal * wVal; + } + } + } + `; + return ` + ${S.registerUniforms(P).declareVariables(...A, $)} + + ${S.mainStart()} + ${S.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} + + let outputIndices = ${$.offsetToIndices("global_idx")}; + let batch: u32 = outputIndices[0]; + let output_channel: u32 = outputIndices[${l ? 3 : 1}]; + let xRCCorner: vec2 = vec2(outputIndices[${l ? 1 : 2}], outputIndices[${l ? 2 : 3}]) * uniforms.strides - uniforms.pads; + let group_id: u32 = output_channel * ${u} / uniforms.output_channels_per_group; + var in_channel_offset = group_id * uniforms.w_shape[${l ? 2 : 1}]; + + var value: ${$.type.value} = ${$.type.value}(0); + ${B} + ${i} + ${x} + ${$.setByOffset("global_idx", "value")} + }`; + }; + return { name: "GroupedConv", shaderCache: { hint: `${t.cacheKey}_${u}`, inputDependencies: g }, getRunData: () => ({ outputs: [{ dims: n ? n(r) : r, dataType: e[0].dataType }], dispatchGroup: { x: Math.ceil(h / 64) }, programUniforms: w }), getShaderSource: y }; + }, Vu = (e, t, r, n) => { + let o = e.length > 2, i = we(r[3]), a = we(r[2]), d = k.size(r) / i / a, l = [e[0].dims[0], e[0].dims[1], e[0].dims[2], e[0].dims[3] / i], c = [e[1].dims[0], e[1].dims[1], e[1].dims[2], e[1].dims[3] / i], m = [r[0], r[1], r[2], r[3] / i], u = [{ type: 12, data: d }, { type: 6, data: [t.strides[0], t.strides[1]] }, { type: 6, data: [t.pads[0], t.pads[1]] }]; + Ge(t, u), u.push(...R(l, c, m)); + let h = (a - 1) * t.strides[1] + c[1], w = (g) => { + let y = M("output", e[0].dataType, m.length, i), S = he(y.type.tensor), $ = He(t, y.type.value, S), v = E("x", e[0].dataType, l.length, i), x = E("w", e[1].dataType, c.length, i), T = [v, x]; + o && T.push(E("b", e[2].dataType, e[2].dims, i)); + let C = o ? "value += b[output_channel];" : "", A = [{ name: "output_size", type: "u32" }, { name: "strides", type: "i32", length: 2 }, { name: "pads", type: "i32", length: 2 }]; + return Fe(t, A), ` + ${g.registerUniforms(A).declareVariables(...T, y)} + ${g.mainStart()} + ${g.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} + let width0 = uniforms.output_shape[3]; + let output_channel = global_idx % width0; + var index1 = global_idx / width0; + let width1 = uniforms.output_shape[2] / ${a}u; + let col = (index1 % width1) * ${a}u; + index1 = index1 / width1; + let row = index1 % uniforms.output_shape[1]; + let batch = index1 / uniforms.output_shape[1]; + + let x_corner = vec2(i32(row), i32(col)) * uniforms.strides - uniforms.pads; + + var x_vals: array<${v.type.value}, ${h}>; + var values: array<${y.type.value}, ${a}>; + let input_channel = output_channel; + // Use constant instead of uniform can give better performance for w's height/width. + for (var w_height: u32 = 0u; w_height < ${c[0]}; w_height++) { + let x_height = x_corner.x + i32(w_height); + if (x_height >= 0 && u32(x_height) < uniforms.x_shape[1]) { + for (var i = 0; i < ${h}; i++) { + let x_width = x_corner.y + i; + if (x_width >= 0 && u32(x_width) < uniforms.x_shape[2]) { + x_vals[i] = ${v.get("batch", "u32(x_height)", "u32(x_width)", "input_channel")}; + } else { + x_vals[i] = ${v.type.value}(0); + } + } + for (var w_width: u32 = 0u; w_width < ${c[1]}; w_width++) { + let w_val = ${x.get("w_height", "w_width", "0", "output_channel")}; + for (var i = 0u; i < ${a}u; i++) { + values[i] = fma(x_vals[i * u32(uniforms.strides[1]) + w_width], w_val, values[i]); + } + } + } + } + + for (var i = 0u; i < ${a}u; i++) { + var value = values[i]; + ${C} + ${$} + ${y.set("batch", "row", "col + i", "output_channel", "value")}; + } + }`; + }; + return { name: "GroupedConv-Vectorize", shaderCache: { hint: `${t.cacheKey};${i};${a};${h};${c[0]};${c[1]}`, inputDependencies: o ? ["rank", "rank", "type"] : ["rank", "rank"] }, getRunData: () => ({ outputs: [{ dims: n ? n(r) : r, dataType: e[0].dataType }], dispatchGroup: { x: Math.ceil(d / 64) }, programUniforms: u }), getShaderSource: w }; + }; + }); + var mo, Km, Wu, fo = V(() => { + "use strict"; + Q(); + ie(); + rr(); + ae(); + ct(); + mo = (e, t, r, n, o = false, i) => { + let a = e[0].dims, d = e[1].dims, l = a[a.length - 2], c = d[d.length - 1], m = a[a.length - 1], u = we(c), h = we(m), w = we(l), g = k.size(r) / u / w, y = e.length > 2, S = n ? n.slice(0, -2) : r.slice(0, -2), v = [k.size(S), l, c], x = [{ type: 12, data: g }, { type: 12, data: l }, { type: 12, data: c }, { type: 12, data: m }]; + Ge(t, x), x.push(...R(S, a, d)), y && x.push(...R(e[2].dims)), x.push(...R(v)); + let T = (C) => { + let A = Fr("batch_dims", e[0].dataType, S.length), P = E("a", e[0].dataType, a.length, h), B = E("b", e[1].dataType, d.length, u), N = M("output", e[0].dataType, v.length, u), W = he(N.type.tensor), K = He(t, N.type.value, W), Z = [P, B], ee = ""; + if (y) { + let re = o ? u : 1; + Z.push(E("bias", e[2].dataType, e[2].dims.length, re)), ee = `${o ? `value += bias[col / ${re}];` : `value += ${N.type.value}(bias[row + i]);`}`; + } + let se = a.slice(0, -2), de = d.slice(0, -2), Y = Nt(se, S), le = Nt(de, S), ce = [{ name: "output_size", type: "u32" }, { name: "M", type: "u32" }, { name: "N", type: "u32" }, { name: "K", type: "u32" }]; + Fe(t, ce); + let q = (re, ne) => { + let oe = re.rank, U = re.name; + if (oe === 2) return `var ${U}_indices = ${re.type.indices}(0u, 0u);`; + let G = A.rank, ye = `var ${U}_indices: ${re.type.indices};`; + for (let Re = oe - 2 - 1, $e = G - 1; Re >= 0; Re--, $e--) ye += ` + ${U}_indices[${Re}] = ${G > 1 ? `batch_indices[${$e}]` : "batch_indices"};`; + return ne.forEach((Re) => { + ye += ` + ${U}_indices[${Re}] = 0;`; + }), ye += `${U}_indices[${oe - 2}] = 0u; + ${U}_indices[${oe - 1}] = 0u;`, ye; + }, ue = () => { + let re = `var a_data: ${P.type.value};`; + for (let ne = 0; ne < h; ne++) re += ` + let b_data${ne} = b[(b_offset + (k + ${ne}) * uniforms.N + col) / ${u}];`; + for (let ne = 0; ne < w; ne++) { + re += `a_data = a[(a_offset + (row + ${ne}) * uniforms.K + k) / ${h}];`; + for (let oe = 0; oe < h; oe++) re += ` + values[${ne}] = fma(${B.type.value}(a_data${h === 1 ? "" : `[${oe}]`}), b_data${oe}, values[${ne}]); + `; + } + return re; + }; + return ` + ${C.registerUniforms(ce).registerInternalVariables(A).declareVariables(...Z, N)} + ${C.mainStart()} + ${C.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} + let col = (global_idx % (uniforms.N / ${u})) * ${u}; + var index1 = global_idx / (uniforms.N / ${u}); + let stride1 = uniforms.M / ${w}; + let row = (index1 % stride1) * ${w}; + let batch = index1 / stride1; + + ${r.length === 2 ? "" : `let batch_indices = ${A.offsetToIndices("batch")};`} + ${q(P, Y)} + let a_offset = ${P.indicesToOffset("a_indices")}; + ${q(B, le)} + let b_offset = ${B.indicesToOffset("b_indices")}; + var values: array<${N.type.value}, ${w}>; + for (var k: u32 = 0u; k < uniforms.K; k = k + ${h}) { + ${ue()} + } + for (var i = 0u; i < ${w}u; i++) { + var value = values[i]; + ${ee} + ${K} + let cur_indices = ${N.type.indices}(batch, row + i, col); + let offset = ${N.indicesToOffset("cur_indices")}; + ${N.setByOffset(`offset / ${u}`, "value")}; + } + } + `; + }; + return { name: "MatMulNaive", shaderCache: { hint: `${t.activation};${u};${h};${w};${o}`, inputDependencies: y ? ["rank", "rank", "rank"] : ["rank", "rank"] }, getRunData: () => ({ outputs: [{ dims: i ? i(r) : r, dataType: e[0].dataType }], dispatchGroup: { x: Math.ceil(g / 64) }, programUniforms: x }), getShaderSource: T }; + }, Km = (e) => { + if (!e || e.length !== 2) throw new Error("MatMul requires 2 inputs."); + if (e[0].dims[e[0].dims.length - 1] !== e[1].dims[e[1].dims.length - 2]) throw new Error("shared dimension does not match."); + }, Wu = (e) => { + Km(e.inputs); + let t = rt.calcShape(e.inputs[0].dims, e.inputs[1].dims, true); + if (!t) throw new Error("Can't use matmul on the given tensors"); + let r = t[t.length - 1], n = e.inputs[0].dims[e.inputs[0].dims.length - 1]; + r < 8 && n < 8 ? e.compute(mo(e.inputs, { activation: "" }, t)) : e.compute(en(e.inputs, { activation: "" }, t)); + }; + }); + var Ym, ho, Xm, go, yo, Lu, Zm, Qm, bo, Hu = V(() => { + "use strict"; + ie(); + zu(); + Ru(); + rr(); + Nu(); + ct(); + fo(); + lt(); + Ym = (e, t, r, n, o, i) => { + let a = e[0], d = e.slice(i ? 1 : 2, i ? 3 : 4), l = d.length, c = t[0], u = t.slice(2).map((g, y) => g + (g - 1) * (r[y] - 1)), w = d.map((g, y) => g + n[y] + n[y + l]).map((g, y) => Math.floor((g - u[y] + o[y]) / o[y])); + return w.splice(0, 0, a), w.splice(i ? 3 : 1, 0, c), w; + }, ho = [2, 3, 1, 0], Xm = (e, t) => { + if (!e || e.length !== 2 && e.length !== 3) throw new Error("Conv requires 2 or 3 inputs"); + if (e[0].dims.length > 5) throw new Error("greater than 5D is not supported"); + if (e[0].dims.length !== e[1].dims.length) throw new Error("filter does not have same dimension as input"); + let r = e[0].dims[t.format === "NHWC" ? e[0].dims.length - 1 : 1], n = e[1].dims[1] * t.group; + if (r !== n) throw new Error("FILTER_IN_CHANNEL should be equal to DATA_CHANNEL"); + if (e.length === 3 && (e[2].dims.length !== 1 || e[1].dims[0] !== e[2].dims[0])) throw new Error("invalid bias"); + let o = e[0].dims.length - 2; + if (t.dilations.length !== o) throw new Error(`dilations should be ${o}D`); + if (t.strides.length !== o) throw new Error(`strides should be ${o}D`); + if (t.pads.length !== o * 2) throw new Error(`pads should be ${o * 2}D`); + if (t.kernelShape.length !== 0 && t.kernelShape.length !== e[1].dims.length - 2) throw new Error("invalid kernel shape"); + }, go = (e, t) => { + let r = e.kernelShape.slice(); + r.length < t[1].dims.length - 2 && r.push(...Array(t[1].dims.length - 2 - r.length).fill(0)); + for (let i = 2; i < t[1].dims.length; ++i) r[i - 2] === 0 && (r[i - 2] = t[1].dims[i]); + let n = e.pads.slice(); + Ct.adjustPadsBasedOnAutoPad(t[0].dims, e.strides, e.dilations, r, n, e.format === "NHWC", e.autoPad); + let o = Object.assign({}, e); + return Object.assign(o, { kernelShape: r, pads: n }), o; + }, yo = (e) => { + let t = Zr(e), r = e.format, n = ["NOTSET", "VALID", "SAME_UPPER", "SAME_LOWER"][e.auto_pad], o = e.dilations, i = e.group, a = e.kernel_shape, d = e.pads, l = e.strides, c = e.w_is_const(); + return { autoPad: n, format: r, dilations: o, group: i, kernelShape: a, pads: d, strides: l, wIsConst: c, ...t, cacheKey: `${e.format};${t.activation};` }; + }, Lu = (e, t, r, n) => { + let o = r.format === "NHWC", i = Ym(t[0].dims, t[1].dims, r.dilations, r.pads, r.strides, o); + if (r.group !== 1) { + let A = [t[0]]; + if (o) { + let B = e.kernelCustomData.wT ?? e.compute(Pe(t[1], ho), { inputs: [1], outputs: [r.wIsConst ? -2 : -1] })[0]; + r.wIsConst && !e.kernelCustomData.wT && (e.kernelCustomData.wT = B), A.push(B); + } else A.push(t[1]); + t.length === 3 && A.push(t[2]), !e.adapterInfo.isArchitecture("ampere") && o && t[1].dims[0] === r.group && t[1].dims[1] === 1 && r.dilations[0] === 1 && r.dilations[1] === 1 ? e.compute(Vu(A, r, i, n), { inputs: A }) : e.compute(Uu(A, r, i, n), { inputs: A }); + return; + } + let a = t.length === 3, d = t[0].dims[o ? 1 : 2], l = t[0].dims[o ? 2 : 3], c = t[0].dims[o ? 3 : 1], m = t[1].dims[2], u = t[1].dims[3], h = i[o ? 1 : 2], w = i[o ? 2 : 3], g = i[o ? 3 : 1], y = o && m === d && u === l && r.pads[0] === 0 && r.pads[1] === 0; + if (y || m === 1 && u === 1 && r.dilations[0] === 1 && r.dilations[1] === 1 && r.strides[0] === 1 && r.strides[1] === 1 && r.pads[0] === 0 && r.pads[1] === 0) { + let A = i[0], P, B, N, W = []; + if (o) { + let ee = e.kernelCustomData.wT ?? e.compute(Pe(t[1], ho), { inputs: [1], outputs: [r.wIsConst ? -2 : -1] })[0]; + if (r.wIsConst && !e.kernelCustomData.wT && (e.kernelCustomData.wT = ee), y) { + let se = d * l * c; + P = t[0].reshape([1, A, se]), B = ee.reshape([1, se, g]), N = [1, A, g]; + } else P = t[0].reshape([A, d * l, c]), B = ee.reshape([1, c, g]), N = [A, h * w, g]; + W.push(P), W.push(B); + } else P = t[0].reshape([A, c, d * l]), B = t[1].reshape([1, g, c]), N = [A, g, h * w], W.push(B), W.push(P); + a && W.push(t[2]); + let K = N[2], Z = W[0].dims[W[0].dims.length - 1]; + K < 8 && Z < 8 ? e.compute(mo(W, r, i, N, o, n), { inputs: W }) : e.compute(en(W, r, i, N, o, n), { inputs: W }); + return; + } + let S = true, $ = e.kernelCustomData.wT ?? e.compute(Pe(t[1], ho), { inputs: [1], outputs: [r.wIsConst ? -2 : -1] })[0]; + r.wIsConst && !e.kernelCustomData.wT && (e.kernelCustomData.wT = $); + let v = [t[0], $]; + a && v.push(t[2]); + let x = o ? h * w : g, T = o ? g : h * w, C = m * u * c; + e.compute(Pu(v, r, i, x, T, C, a, S, n), { inputs: v }); + }, Zm = (e, t) => { + let r = t.format === "NHWC", n = [e.inputs[0].reshape(r ? [e.inputs[0].dims[0], 1, e.inputs[0].dims[1], e.inputs[0].dims[2]] : [e.inputs[0].dims[0], e.inputs[0].dims[1], 1, e.inputs[0].dims[2]]), e.inputs[1].reshape([e.inputs[1].dims[0], e.inputs[1].dims[1], 1, e.inputs[1].dims[2]])]; + e.inputs.length === 3 && n.push(e.inputs[2]); + let o = [0, t.pads[0], 0, t.pads[1]], i = [1].concat(t.strides), a = [1].concat(t.dilations), d = [1].concat(t.kernelShape), l = go({ ...t, pads: o, strides: i, dilations: a, kernelShape: d }, n); + Lu(e, n, l, (c) => r ? [c[0], c[2], c[3]] : [c[0], c[1], c[3]]); + }, Qm = (e, t, r) => { + let n = r.format === "NHWC" ? "channelsLast" : "channelsFirst", o = go(r, t), i = r.autoPad === "NOTSET" ? r.pads : r.autoPad, a = Du(t[0].dims, t[1].dims, r.strides, r.dilations, i, false, n); + e.compute(Mu(t, o, a.outShape, [a.filterDepth, a.filterHeight, a.filterWidth], [a.padInfo.front, a.padInfo.top, a.padInfo.left], n)); + }, bo = (e, t) => { + if (Xm(e.inputs, t), e.inputs[0].dims.length === 3) Zm(e, t); + else if (e.inputs[0].dims.length === 5) Qm(e, e.inputs, t); + else { + let r = go(t, e.inputs); + Lu(e, e.inputs, r); + } + }; + }); + var Jm, Gu, Fu = V(() => { + "use strict"; + Q(); + Xe(); + ae(); + ct(); + Jt(); + po(); + rr(); + Jm = (e, t = false, r, n, o = 4) => { + let i = ($) => { + switch ($) { + case 1: + return "return w[getIndexFromCoords4D(coord, vec4(uniforms.w_shape))];"; + case 4: + return ` + let coord1 = vec4(coordX, coordY, col + 1, rowInner); + let coord2 = vec4(coordX, coordY, col + 2, rowInner); + let coord3 = vec4(coordX, coordY, col + 3, rowInner); + let v0 = w[getIndexFromCoords4D(coord, vec4(uniforms.w_shape))]; + let v1 = w[getIndexFromCoords4D(coord1, vec4(uniforms.w_shape))]; + let v2 = w[getIndexFromCoords4D(coord2, vec4(uniforms.w_shape))]; + let v3 = w[getIndexFromCoords4D(coord3, vec4(uniforms.w_shape))]; + return ${n}(v0, v1, v2, v3); + `; + default: + throw new Error(`innerElementSize ${$} is not supported.`); + } + }, a = e ? ` + let coord = vec4(batch, iXR, iXC, xCh); + ` : ` + let coord = vec4(batch, xCh, iXR, iXC); + `, d = e ? ` + let coords = vec4( + batch, + row / outWidth, + row % outWidth, + col); + ` : ` + let coords = vec4( + batch, + row, + col / outWidth, + col % outWidth); + `, l = e ? "i32(uniforms.x_shape[1])" : "i32(uniforms.x_shape[2])", c = e ? "i32(uniforms.x_shape[2])" : "i32(uniforms.x_shape[3])", m = e ? "row" : "col", u = e ? "col" : "row", h = ` + let inChannels = ${e ? "i32(uniforms.x_shape[3])" : "i32(uniforms.x_shape[1])"}; + let outWidth = ${e ? "i32(uniforms.result_shape[2])" : "i32(uniforms.result_shape[3])"}; + let outRow = ${m} / outWidth; + let outCol = ${m} % outWidth; + + let WRow = ${u} / (uniforms.filter_dims[1] * inChannels); + let WCol = ${u} / inChannels % uniforms.filter_dims[1]; + let xR = f32(outRow - uniforms.pads[0] + uniforms.dilations[0] * WRow) / f32(uniforms.strides[0]); + let xC = f32(outCol - uniforms.pads[1] + uniforms.dilations[1] * WCol) / f32(uniforms.strides[1]); + if (xR < 0.0 || xR >= f32(${l}) || fract(xR) > 0.0) { + return ${n}(0.0); + } + if (xC < 0.0 || xC >= f32(${c}) || fract(xC) > 0.0) { + return ${n}(0.0); + } + let iXR = i32(xR); + let iXC = i32(xC); + let xCh = ${u} % inChannels; + ${a} + return x[getIndexFromCoords4D(coord, vec4(uniforms.x_shape))/${o}];`, w = e ? ` + let col = colIn * ${o}; + if (row < uniforms.dim_a_outer && col < uniforms.dim_inner) { + ${h} + } + return ${n}(0.0);` : ` + let col = colIn * ${o}; + if (row < uniforms.dim_inner && col < uniforms.dim_b_outer) { + ${h} + } + return ${n}(0.0);`, g = ` + let col = colIn * ${o}; + let inChannels = ${e ? "i32(uniforms.x_shape[3])" : "i32(uniforms.x_shape[1])"}; + let coordX = uniforms.filter_dims[0] - 1 - row / (uniforms.filter_dims[1] * inChannels); + let coordY = uniforms.filter_dims[1] - 1 - (row / inChannels) % uniforms.filter_dims[1]; + if (${e ? "row < uniforms.dim_inner && col < uniforms.dim_b_outer" : "row < uniforms.dim_inner && col < uniforms.dim_a_outer"} && coordX >= 0 && coordY >= 0) { + let rowInner = row % inChannels; + let coord = vec4(coordX, coordY, col, rowInner); + ${i(o)} + } + return ${n}(0.0); + `, y = He(r, n); + return ` + fn mm_readA(batch: i32, row : i32, colIn : i32) -> ${n} { + ${e ? w : g} + } + + fn mm_readB(batch: i32, row : i32, colIn : i32) -> ${n} { + ${e ? g : w} + } + + fn mm_write(batch: i32, row : i32, colIn : i32, valueInput : ${n}) { + let col = colIn * ${o}; + if (row < uniforms.dim_a_outer && col < uniforms.dim_b_outer) { + var value = valueInput; + let outWidth = ${e ? "i32(uniforms.result_shape[2])" : "i32(uniforms.result_shape[3])"}; + ${d} + ${Qr(t)} + ${y} + result[getIndexFromCoords4D(coords, vec4(uniforms.result_shape))/${o}] = value; + } + }`; + }, Gu = (e, t, r, n, o, i, a, d) => { + let l = t.format === "NHWC", c = l ? e[0].dims[3] : e[0].dims[1], m = r[0], u = l ? r[2] : r[3], h = l ? r[1] : r[2], w = l ? r[3] : r[1], g = l && c % 4 === 0 && c % 3 && w % 4 === 0, y = l ? w : u * h, S = l ? u * h : w, $ = [8, 8, 1], v = n <= 8 ? [4, 1, 1] : [4, 4, 1], x = [Math.ceil(y / $[0] / v[0]), Math.ceil(S / $[1] / v[1]), Math.ceil(m / $[2] / v[2])]; + me("verbose", () => `[conv_backprop_mm_webgpu] dispatch = ${x}`); + let T = g ? 4 : 1, C = Math.max($[0] * T, $[1]), A = g ? 4 : 1, P = [t.kernelShape[l ? 1 : 2], t.kernelShape[l ? 2 : 3]], B = [P[0] + (t.dilations[0] <= 1 ? 0 : (P[0] - 1) * (t.dilations[0] - 1)), P[1] + (t.dilations[1] <= 1 ? 0 : (P[1] - 1) * (t.dilations[1] - 1))], N = [B[0] - 1 - Math.floor((t.pads[0] + t.pads[2]) / 2), B[1] - 1 - Math.floor((t.pads[1] + t.pads[3]) / 2)], W = [{ type: 6, data: n }, { type: 6, data: o }, { type: 6, data: i }, { type: 6, data: t.strides }, { type: 6, data: t.dilations }, { type: 6, data: P }, { type: 6, data: N }]; + Ge(t, W), W.push(...R(e[0].dims, e[1].dims)); + let K = ["rank", "rank"]; + a && (W.push(...R(e[2].dims)), K.push("rank")), W.push(...R(r)); + let Z = (ee) => { + let se = E("x", e[0].dataType, e[0].dims.length, A), de = E("w", e[1].dataType, e[1].dims.length, 1), Y = M("result", e[0].dataType, r.length, A), le = [se, de], ce = ""; + if (a) { + let re = E("bias", e[2].dataType, e[2].dims.length, A); + le.push(re), ce += ` + fn getBiasByOutputCoords(coords : vec4) -> ${re.type.value} { + return bias[coords.${l ? "w" : "y"}${g ? "/ 4" : ""}]; + }`; + } + let q = [{ name: "dim_a_outer", type: "i32" }, { name: "dim_b_outer", type: "i32" }, { name: "dim_inner", type: "i32" }, { name: "strides", type: "i32", length: 2 }, { name: "dilations", type: "i32", length: 2 }, { name: "filter_dims", type: "i32", length: P.length }, { name: "pads", type: "i32", length: N.length }]; + Fe(t, q); + let ue = he(e[0].dataType, 1); + if (ue !== "f16" && ue !== "f32") throw new Error(`elemType ${ue} is not supported.`); + return ` + ${Jr("uniforms.result_strides")} + ${ee.registerUniforms(q).declareVariables(...le, Y)}; + ${ce} + ${Jm(l, a, t, se.type.value, T)} + ${g ? er(v, $, ue, void 0, !l, C) : tr(v, $, ue, void 0, !l, C, false, void 0, d)}`; + }; + return { name: "Conv2DTransposeMatMul", shaderCache: { hint: `${t.cacheKey};${v};${$};${g}`, inputDependencies: K }, getRunData: () => ({ outputs: [{ dims: r, dataType: e[0].dataType }], dispatchGroup: { x: x[0], y: x[1], z: x[2] }, programUniforms: W }), getShaderSource: Z }; + }; + }); + var ef, wo, qu = V(() => { + "use strict"; + Q(); + Xe(); + ie(); + ae(); + ef = (e, t, r, n, o, i = false, a, d, l = false) => { + let c = l ? 1 : 2, m = l ? 2 : 3, u = l ? 3 : 1, h = i ? 2 : 1, w = ` + fn setOutputAtIndex(flatIndex : u32, value : ${i ? `vec4<${a}>` : a}) { + result[flatIndex] = ${i ? `vec4<${a}>` : a}(value); + }`; + n && (w += ` + fn getBiasByOutputCoords(coords : vec4) -> ${i ? `vec4<${a}>` : a} { + return bias[coords.${l ? "w" : "y"}${i ? "/ 4" : ""}]; + }`); + let g = i ? 4 : 1, y = E("W", t[1].dataType, t[1].dims.length, g), S = E("Dy", t[0].dataType, t[0].dims.length, g), $ = [S, y]; + n && $.push(E("bias", t[2].dataType, [r[u]].length, g)); + let v = M("result", t[0].dataType, r.length, g), x = `{ + let batch: u32 = ${o ? "global_id.z" : "workgroup_id.z"} / uniforms.result_shape[1]; + let r = ${o ? "global_id.z" : "workgroup_id.z"} % uniforms.result_shape[1]; + let c = ${o ? "global_id.y" : "workgroup_id.y"} * ${h}; + let d1: u32 = ${o ? "global_id.x" : "workgroup_id.x"} * 4; + + let dyCorner = vec2(i32(r), i32(c)) - vec2(uniforms.pads); + + // Convolve dy(?, ?, d2) with w(:, :, d1, d2) to compute dx(xR, xC, d1). + // ? = to be determined. : = across all values in that axis. + var dotProd: array, ${h}>; + for (var i = 0; i < ${h}; i++) { + dotProd[i] = vec4<${a}>(0.0); + } + for (var wR: u32 = 0; wR < uniforms.filter_dims[0]; wR = wR + 1) { + var dyR = (${a}(dyCorner.x) + ${a}(wR)) / ${a}(uniforms.strides.x); + let wRPerm = uniforms.filter_dims[0] - 1 - wR; + if (dyR < 0.0 || dyR >= ${a}(uniforms.Dy_shape[1]) || + fract(dyR) > 0.0 || wRPerm < 0) { + continue; + } + let idyR: u32 = u32(dyR); + + for (var wC: u32 = 0; wC < uniforms.filter_dims[1]; wC = wC + 1) { + let dyC = (${a}(dyCorner.y) + ${a}(wC)) / ${a}(uniforms.strides.y); + let dyC2 = (${a}(dyCorner.y) + 1.0 + ${a}(wC)) / ${a}(uniforms.strides.y); + let wCPerm = uniforms.filter_dims[1] - 1 - wC; + if (wCPerm < 0) { + continue; + } + var bDyCVal = true; + var bDyCVal2 = true; + if (dyC < 0.0 || dyC >= ${a}(uniforms.Dy_shape[2]) || + fract(dyC) > 0.0) { + bDyCVal = false; + } + if (dyC2 < 0.0 || dyC2 >= ${a}(uniforms.Dy_shape[2]) || + fract(dyC2) > 0.0) { + bDyCVal2 = false; + } + + let idyC: u32 = u32(dyC); + let idyC2: u32 = u32(dyC2); + if (bDyCVal && bDyCVal2) { + let d2Length = uniforms.Dy_shape[3]; + for (var d2 :u32 = 0; d2 < d2Length; d2 = d2 + 4) { + let wValue0 = ${y.get("u32(wRPerm)", "u32(wCPerm)", "d1", "d2")}; + let wValue1 = ${y.get("u32(wRPerm)", "u32(wCPerm)", "d1 + 1", "d2")}; + let wValue2 = ${y.get("u32(wRPerm)", "u32(wCPerm)", "d1 + 2", "d2")}; + let wValue3 = ${y.get("u32(wRPerm)", "u32(wCPerm)", "d1 + 3", "d2")}; + + var xValue = ${S.get("batch", "idyR", "idyC", "d2")}; + let tmpval = vec4<${a}>(dot(xValue, wValue0), + dot(xValue, wValue1), + dot(xValue, wValue2), + dot(xValue, wValue3)); + dotProd[0] = dotProd[0] + tmpval; + + xValue = ${S.get("batch", "idyR", "idyC2", "d2")}; + + dotProd[1] = dotProd[1] + vec4<${a}>(dot(xValue, wValue0), + dot(xValue, wValue1), + dot(xValue, wValue2), + dot(xValue, wValue3)); + } + } else if (bDyCVal) { + let d2Length = uniforms.Dy_shape[${u}]; + for (var d2: u32 = 0; d2 < d2Length; d2 = d2 + 4) { + let wValue0 = ${y.get("u32(wRPerm)", "u32(wCPerm)", "d1", "d2")}; + let wValue1 = ${y.get("u32(wRPerm)", "u32(wCPerm)", "d1 + 1", "d2")}; + let wValue2 = ${y.get("u32(wRPerm)", "u32(wCPerm)", "d1 + 2", "d2")}; + let wValue3 = ${y.get("u32(wRPerm)", "u32(wCPerm)", "d1 + 3", "d2")}; + + var xValue = ${S.get("batch", "idyR", "idyC", "d2")}; + let tmpval = vec4<${a}>(dot(xValue, wValue0), + dot(xValue, wValue1), + dot(xValue, wValue2), + dot(xValue, wValue3)); + dotProd[0] = dotProd[0] + tmpval; + } + } else if (bDyCVal2) { + let d2Length = uniforms.Dy_shape[3]; + for (var d2: u32 = 0; d2 < d2Length; d2 = d2 + 4) { + let wValue0 = ${y.get("u32(wRPerm)", "u32(wCPerm)", "d1", "d2")}; + let wValue1 = ${y.get("u32(wRPerm)", "u32(wCPerm)", "d1 + 1", "d2")}; + let wValue2 = ${y.get("u32(wRPerm)", "u32(wCPerm)", "d1 + 2", "d2")}; + let wValue3 = ${y.get("u32(wRPerm)", "u32(wCPerm)", "d1 + 3", "d2")}; + + var xValue = ${S.get("batch", "idyR", "idyC2", "d2")}; + let tmpval = vec4<${a}>(dot(xValue, wValue0), + dot(xValue, wValue1), + dot(xValue, wValue2), + dot(xValue, wValue3)); + dotProd[1] = dotProd[1] + tmpval; + } + } + } + } + + for (var i: u32 = 0; i < ${h}; i = i + 1) { + let value = dotProd[i] + ${n ? "bias[c+i]" : `vec4<${a}>(0.0)`}; + ${v.set("batch", "r", "c + i", "d1", "value")}; + } + }`, T = ` + let outputIndices = ${v.offsetToIndices("global_idx")}; + let batch = ${v.indicesGet("outputIndices", 0)}; + let d1 = ${v.indicesGet("outputIndices", u)}; + let r = ${v.indicesGet("outputIndices", c)}; + let c = ${v.indicesGet("outputIndices", m)}; + let dyCorner = vec2(i32(r), i32(c)) - uniforms.pads; + let dyRCorner = dyCorner.x; + let dyCCorner = dyCorner.y; + let groupId = d1 / uniforms.output_channels_per_group; + let wOutChannel = d1 - groupId * uniforms.output_channels_per_group; + // Convolve dy(?, ?, d2) with w(:, :, d1, d2) to compute dx(xR, xC, d1). + // ? = to be determined. : = across all values in that axis. + var dotProd = ${a}(0.0); + for (var wR: u32 = 0; wR < uniforms.effective_filter_dims.x; wR = wR + 1) { + if (wR % uniforms.dilations.x != 0) { + continue; + } + let dyR = (${a}(dyRCorner) + ${a}(wR)) / ${a}(uniforms.strides[0]); + let wRPerm = uniforms.filter_dims.x - 1 - wR / uniforms.dilations.x; + if (dyR < 0.0 || dyR >= ${a}(uniforms.Dy_shape[${c}]) || fract(dyR) > 0.0 || + wRPerm < 0) { + continue; + } + let idyR: u32 = u32(dyR); + + for (var wC: u32 = 0; wC < uniforms.effective_filter_dims.y; wC = wC + 1) { + if (wC % uniforms.dilations.y != 0) { + continue; + } + let dyC = (${a}(dyCCorner) + ${a}(wC)) / ${a}(uniforms.strides.y); + let wCPerm = uniforms.filter_dims.y - 1 - wC / uniforms.dilations.y; + if (dyC < 0.0 || dyC >= ${a}(uniforms.Dy_shape[${m}]) || + fract(dyC) > 0.0 || wCPerm < 0) { + continue; + } + let idyC: u32 = u32(dyC); + var inputChannel = groupId * uniforms.input_channels_per_group; + for (var d2: u32 = 0; d2 < uniforms.input_channels_per_group; d2 = d2 + 1) { + let xValue = ${l ? S.get("batch", "idyR", "idyC", "inputChannel") : S.get("batch", "inputChannel", "idyR", "idyC")}; + let wValue = ${y.get("inputChannel", "wOutChannel", "u32(wRPerm)", "u32(wCPerm)")}; + dotProd = dotProd + xValue * wValue; + inputChannel = inputChannel + 1; + } + } + } + let value = dotProd + ${n ? "bias[d1]" : `${a}(0.0)`}; + ${v.setByOffset("global_idx", "value")}; + `; + return ` + ${e.registerUniforms(d).declareVariables(...$, v)} + ${w} + + ${e.mainStart()} + ${e.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")}; + ${i ? x : T}}`; + }, wo = (e, t, r) => { + let n = e.length > 2, o = t.outputShape, i = k.size(o), a = [Math.ceil(i / 64), 1, 1]; + me("verbose", () => `[conv2d_backprop_webgpu] dispatch = ${a}`); + let d = t.format === "NHWC", l = ["rank", "rank"], c = [t.strides[0], t.strides[1]], m = [t.kernelShape[d ? 1 : 2], t.kernelShape[d ? 2 : 3]], u = [t.dilations[0], t.dilations[1]], h = [m[0] + (t.dilations[0] <= 1 ? 0 : (t.kernelShape[d ? 1 : 2] - 1) * (t.dilations[0] - 1)), m[1] + (t.dilations[1] <= 1 ? 0 : (t.kernelShape[d ? 2 : 3] - 1) * (t.dilations[1] - 1))], w = [h[0] - 1 - Math.floor((t.pads[0] + t.pads[2]) / 2), h[1] - 1 - Math.floor(t.pads[1] + t.pads[3]) / 2], g = false, y = t.group, S = e[1].dims, $ = S[0] / y, v = S[1], x = [{ type: 12, data: i }, { type: 12, data: c }, { type: 12, data: m }, { type: 12, data: u }, { type: 12, data: h }, { type: 6, data: w }, { type: 12, data: $ }, { type: 12, data: v }, ...R(e[0].dims, e[1].dims)]; + n && (x.push(...R(e[2].dims)), l.push("rank")), x.push(...R(o)); + let T = a[1] === 1 && a[2] === 1, C = (A) => { + let P = [{ name: "output_size", type: "u32" }, { name: "strides", type: "u32", length: c.length }, { name: "filter_dims", type: "u32", length: m.length }, { name: "dilations", type: "u32", length: m.length }, { name: "effective_filter_dims", type: "u32", length: h.length }, { name: "pads", type: "i32", length: w.length }, { name: "input_channels_per_group", type: "u32" }, { name: "output_channels_per_group", type: "u32" }], B = he(e[0].dataType); + return `${ef(A, e, o, n, T, g, B, P, d)}`; + }; + return { name: "ConvTranspose2D", shaderCache: { hint: `${t.cacheKey};`, inputDependencies: l }, getRunData: () => ({ dispatchGroup: { x: a[0], y: a[1], z: a[2] }, outputs: [{ dims: r ? r(o) : o, dataType: e[0].dataType }], programUniforms: x }), getShaderSource: C }; + }; + }); + var tf, rf, nf, ju, Ku, of, af, sf, uf, Yu, Xu = V(() => { + "use strict"; + Fu(); + qu(); + ct(); + lt(); + tf = (e, t, r, n, o, i) => (e - 1) * t + r + (n - 1) * o + 1 - i, rf = (e, t, r, n, o) => { + let i = Math.floor(e / 2); + t === "SAME_UPPER" ? (r[n] = i, r[o] = e - i) : t === "SAME_LOWER" && (r[n] = e - i, r[o] = i); + }, nf = (e, t, r, n, o, i, a, d, l, c) => { + let m = e.length - 2, u = c.length === 0; + l.length < m && l.push(...Array(m - l.length).fill(0)); + let h = e[0], w = t[d ? 3 : 1] * o; + for (let g = 0, y = e.length - m - (d ? 1 : 0); g < m; ++g, ++y) { + let S = e[y], $ = u ? S * a[g] : c[g], v = tf(S, a[g], i[g], t[y], r[g], $); + rf(v, n, i, g, g + m), u && c.push(a[g] * (S - 1) + l[g] + (t[y] - 1) * r[g] + 1 - i[g] - i[g + m]); + } + c.splice(0, 0, h), c.splice(d ? 3 : 1, 0, w); + }, ju = (e, t) => { + let r = e.kernelShape.slice(); + if (e.kernelShape.length === 0 || e.kernelShape.reduce((u, h) => u * h, 1) === 0) { + r.length = 0; + for (let u = 2; u < t[1].dims.length; ++u) r.push(t[1].dims[u]); + } + let n = e.format === "NHWC"; + r.splice(0, 0, t[1].dims[0]), r.splice(n ? 3 : 1, 0, t[1].dims[1]); + let o = e.pads.slice(), i = e.outputShape.slice(), a = e.outputPadding.slice(), d = t[0].dims, l = e.dilations.slice(); + if (l.reduce((u, h) => u + h, 0) === 0) { + let u = t[0].dims.length - 2; + l = new Array(u).fill(1); + } + let c = e.strides.slice(); + if (c.reduce((u, h) => u + h, 0) === 0) { + let u = t[0].dims.length - 2; + c = new Array(u).fill(1); + } + nf(d, r, l, e.autoPad, e.group, o, c, n, a, i); + let m = Object.assign({}, e); + return Object.assign(m, { kernelShape: r, pads: o, outputPadding: a, outputShape: i, dilations: l, strides: c }), m; + }, Ku = (e) => { + let t = Zr(e), r = e.format, n = ["NOTSET", "VALID", "SAME_UPPER", "SAME_LOWER"][typeof e.autoPad > "u" ? 0 : e.autoPad], o = e.dilations, i = e.group, a = e.kernelShape, d = e.pads, l = e.strides, c = e.wIsConst(), m = e.outputPadding, u = e.outputShape; + return { autoPad: n, format: r, dilations: o, group: i, kernelShape: a, outputPadding: m, outputShape: u, pads: d, strides: l, wIsConst: c, ...t, cacheKey: `${e.format};${t.activation};` }; + }, of = (e, t) => { + if (!e || e.length !== 2 && e.length !== 3) throw new Error("Conv requires 2 or 3 inputs"); + if (e[0].dims.length !== 4 && e[0].dims.length !== 3) throw new Error("currently only support 2-dimensional conv"); + if (e[0].dims.length !== e[1].dims.length) throw new Error("filter does not have same dimension as input"); + let r = e[0].dims[t.format === "NHWC" ? e[0].dims.length - 1 : 1], n = e[1].dims[0]; + if (r !== n) throw new Error("FILTER_IN_CHANNEL should be equal to DATA_CHANNEL"); + let o = e[1].dims[1] * t.group; + if (e.length === 3 && (e[2].dims.length !== 1 || e[2].dims[0] !== o)) throw new Error("invalid bias"); + let i = e[0].dims.length - 2; + if (t.dilations.reduce((m, u) => m + u, 0) > 0 && t.dilations.length !== i) throw new Error(`dilations should be ${i}D`); + if (t.strides.reduce((m, u) => m + u, 0) > 0 && t.strides.length !== i) throw new Error(`strides should be ${i}D`); + if (t.pads.reduce((m, u) => m + u, 0) > 0 && t.pads.length !== i * 2) throw new Error(`pads should be ${i * 2}D`); + if (t.outputPadding.length !== i && t.outputPadding.length !== 0) throw new Error(`output_padding should be ${i}D`); + if (t.kernelShape.reduce((m, u) => m + u, 0) > 0 && t.kernelShape.length !== 0 && t.kernelShape.length !== e[1].dims.length - 2) throw new Error("invalid kernel shape"); + if (t.outputShape.length !== 0 && t.outputShape.length !== e[0].dims.length - 2) throw new Error("invalid output shape"); + }, af = [2, 3, 1, 0], sf = (e, t, r) => { + let n = ju(r, t), o = r.format === "NHWC", i = n.outputShape, a = i[o ? 3 : 1], d = t[0].dims[o ? 3 : 1]; + if (n.group !== 1 || a === 1 && d === 1) { + e.compute(wo(t, n)); + return; + } + let l = i[o ? 1 : 2], c = i[o ? 2 : 3], m = t[1].dims[2], u = t[1].dims[3], h = o ? l * c : a, w = o ? a : l * c, g = m * u * d, y = true, S = e.kernelCustomData.wT ?? e.compute(Pe(t[1], af), { inputs: [1], outputs: [r.wIsConst ? -2 : -1] })[0]; + r.wIsConst && !e.kernelCustomData.wT && (e.kernelCustomData.wT = S); + let $ = [t[0], S], v = t.length === 3; + v && (!o && t[2].dims.length === 1 ? $.push(t[2].reshape([t[2].dims[0], 1, 1])) : $.push(t[2])), e.compute(Gu($, n, i, h, w, g, v, y), { inputs: $ }); + }, uf = (e, t) => { + let r = t.format === "NHWC", n = [e.inputs[0].reshape(r ? [e.inputs[0].dims[0], 1, e.inputs[0].dims[1], e.inputs[0].dims[2]] : [e.inputs[0].dims[0], e.inputs[0].dims[1], 1, e.inputs[0].dims[2]]), e.inputs[1].reshape([e.inputs[1].dims[0], e.inputs[1].dims[1], 1, e.inputs[1].dims[2]])]; + e.inputs.length === 3 && n.push(e.inputs[2]); + let o = t.kernelShape; + (o.length === 0 || o[0] === 0) && (o = [e.inputs[1].dims[2]]); + let i = t.dilations; + (i.length === 0 || i[0] === 0) && (i = [1]); + let a = t.strides; + (a.length === 0 || a[0] === 0) && (a = [1]); + let d = t.pads; + d.length === 0 && (d = [0, 0]), d = [0, d[0], 0, d[1]], a = [1].concat(a), i = [1].concat(i), o = [1].concat(o); + let l = ju({ ...t, pads: d, strides: a, dilations: i, kernelShape: o }, n); + e.compute(wo(n, l, (c) => r ? [c[0], c[2], c[3]] : [c[0], c[1], c[3]])); + }, Yu = (e, t) => { + of(e.inputs, t), e.inputs[0].dims.length === 3 ? uf(e, t) : sf(e, e.inputs, t); + }; + }); + var df, Zu, Qu, Ju = V(() => { + "use strict"; + Q(); + ie(); + Ie(); + ae(); + df = (e, t, r, n) => { + let o = k.size(t), i = t.length, a = E("input", e, i), d = M("output", e, i), l = r.dataType === 6 ? r.getInt32Array()[0] : Number(r.getBigInt64Array()[0]), c = k.normalizeAxis(l, i), m = (u) => { + let h = ` i32(${a.indicesGet("inputIndices", "uniforms.axis")}) `, w = F("uniforms.input_shape", "uniforms.axis", i), g = n.reverse ? h + (n.exclusive ? " + 1" : "") : "0", y = n.reverse ? w : h + (n.exclusive ? "" : " + 1"); + return ` + ${u.registerUniform("outputSize", "u32").registerUniform("axis", "u32").declareVariables(a, d)} + ${u.mainStart()} + ${u.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.outputSize")} + var inputIndices = ${d.offsetToIndices("global_idx")}; + var sum = ${d.type.value}(0); + let first : i32 = ${g}; + let last : i32 = ${y}; + for (var i : i32 = first; i < last; i++) { + ${a.indicesSet("inputIndices", "uniforms.axis", "u32(i)")}; + sum = sum + ${a.getByIndices("inputIndices")}; + } + ${d.setByOffset("global_idx", "sum")}; + }`; + }; + return { name: "CumSum", shaderCache: { hint: n.cacheKey, inputDependencies: ["rank"] }, getRunData: () => ({ outputs: [{ dims: t, dataType: e }], dispatchGroup: { x: Math.ceil(o / 64) }, programUniforms: [{ type: 12, data: o }, { type: 12, data: c }, ...R(t, t)] }), getShaderSource: m }; + }, Zu = (e, t) => { + let r = e.inputs[0].dims, n = e.inputs[0].dataType, o = e.inputs[1]; + e.compute(df(n, r, o, t), { inputs: [0] }); + }, Qu = (e) => { + let t = e.exclusive === 1, r = e.reverse === 1; + return J({ exclusive: t, reverse: r }); + }; + }); + var lf, cf, pf, ed, td, rd = V(() => { + "use strict"; + Q(); + ie(); + Ie(); + ae(); + lf = (e) => { + if (!e || e.length !== 1) throw new Error("DepthToSpace requires 1 input."); + if (e[0].dims.length !== 4) throw new Error("DepthToSpace requires 4D input."); + }, cf = (e, t, r, n) => { + let o = []; + o.push(`fn perm(i: ${n.type.indices}) -> ${r.type.indices} { + var a: ${r.type.indices};`); + for (let i = 0; i < t; ++i) o.push(r.indicesSet("a", e[i], `i[${i}]`)); + return o.push("return a;}"), o.join(` + `); + }, pf = (e, t) => { + let r, n, o, i, a, d, l = t.format === "NHWC", c = t.blocksize, m = t.mode === "DCR"; + l ? ([r, n, o, i] = e.dims, a = m ? [r, n, o, c, c, i / c ** 2] : [r, n, o, i / c ** 2, c, c], d = m ? [0, 1, 3, 2, 4, 5] : [0, 1, 4, 2, 5, 3]) : ([r, n, o, i] = [e.dims[0], e.dims[2], e.dims[3], e.dims[1]], a = m ? [r, c, c, i / c ** 2, n, o] : [r, i / c ** 2, c, c, n, o], d = m ? [0, 3, 4, 1, 5, 2] : [0, 1, 4, 2, 5, 3]); + let u = e.reshape(a), h = u.dims.length, w = e.dataType, g = E("a", w, h), y = M("output", w, h), S = ($) => ` + ${$.registerUniform("output_size", "u32").declareVariables(g, y)} + + ${cf(d, h, g, y)} + + ${$.mainStart()} + ${$.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} + + let indices = ${y.offsetToIndices("global_idx")}; + let aIndices = perm(indices); + + ${y.setByOffset("global_idx", g.getByIndices("aIndices"))} + }`; + return { name: "DepthToSpace", shaderCache: { hint: `${e.dims};${t.blocksize};${t.mode}`, inputDependencies: ["rank"] }, getRunData: ($) => { + let v = l ? [r, n * c, o * c, i / c ** 2] : [r, i / c ** 2, n * c, o * c], x = k.size(v), T = u.dims, C = k.sortBasedOnPerm(T, d); + return { outputs: [{ dims: v, dataType: $[0].dataType }], dispatchGroup: { x: Math.ceil(x / 64) }, programUniforms: [{ type: 12, data: x }, ...R(T, C)] }; + }, getShaderSource: S }; + }, ed = (e, t) => { + lf(e.inputs), e.compute(pf(e.inputs[0], t)); + }, td = (e) => J({ blocksize: e.blocksize, mode: e.mode, format: e.format }); + }); + var _o, rn, nd, mf, ff, vo, $o, od, hf, id, ad, sd = V(() => { + "use strict"; + Q(); + ie(); + Ie(); + ae(); + _o = "[a-zA-Z]|\\.\\.\\.", rn = "(" + _o + ")+", nd = "^" + rn + "$", mf = "(" + rn + ",)*" + rn, ff = "^" + mf + "$", vo = class { + constructor(t = -1) { + this.symbolToIndices = /* @__PURE__ */ new Map(), this.inputIndex = t; + } + addSymbol(t, r) { + let n = this.symbolToIndices.get(t); + n === void 0 ? n = [r] : n.push(r), this.symbolToIndices.set(t, n); + } + }, $o = class { + constructor(t, r) { + var _a2; + this.equation = r; + this.hasEllipsis = false, this.symbolToInfo = /* @__PURE__ */ new Map(), this.lhs = new Array(), this.outputDims = []; + let [n, o] = r.includes("->") ? r.split("->", 2) : [r, ""]; + if (!n.match(RegExp(ff))) throw new Error("Invalid LHS term"); + if (n.split(",").forEach((d, l) => { + let c = t[l].dims.slice(); + if (!d.match(RegExp(nd))) throw new Error("Invalid LHS term"); + let m = this.processTerm(d, true, c, l); + this.lhs.push(m); + }), o === "") o += [...this.symbolToInfo.entries()].filter(([d, l]) => l.count === 1 || d === "...").map(([d]) => d).join(""); + else if (!o.match(RegExp(rn))) throw new Error("Invalid RHS"); + (_a2 = o.match(RegExp(_o, "g"))) == null ? void 0 : _a2.forEach((d) => { + if (d === "...") this.outputDims = this.outputDims.concat(this.ellipsisDims); + else { + let l = this.symbolToInfo.get(d); + if (l === void 0) throw new Error("Invalid RHS symbol"); + this.outputDims.push(l.dimValue); + } + }), this.rhs = this.processTerm(o, false, this.outputDims); + } + addSymbol(t, r, n) { + let o = this.symbolToInfo.get(t); + if (o !== void 0) { + if (o.dimValue !== r && o.count !== 1) throw new Error("Dimension mismatch"); + o.count++, o.inputIndices.push(n); + } else o = { count: 1, dimValue: r, inputIndices: [n] }; + this.symbolToInfo.set(t, o); + } + processTerm(t, r, n, o = -1) { + let i = n.length, a = false, d = [], l = 0; + if (!t.match(RegExp(nd)) && !r && t !== "") throw new Error("Invalid LHS term"); + let c = t.match(RegExp(_o, "g")), m = new vo(o); + return c == null ? void 0 : c.forEach((u, h) => { + if (u === "...") { + if (a) throw new Error("Only one ellipsis is allowed per input term"); + a = true; + let w = i - c.length + 1; + if (w < 0) throw new Error("Ellipsis out of bounds"); + if (d = n.slice(l, l + w), this.hasEllipsis) { + if (this.ellipsisDims.length !== d.length || this.ellipsisDims.toString() !== d.toString()) throw new Error("Ellipsis dimensions mismatch"); + } else if (r) this.hasEllipsis = true, this.ellipsisDims = d; + else throw new Error("Ellipsis must be specified in the LHS"); + for (let g = 0; g < d.length; g++) { + let y = String.fromCharCode("0".charCodeAt(0) + g); + m.addSymbol(y, h + g), this.addSymbol(y, n[l++], o); + } + } else m.addSymbol(u, h + (this.hasEllipsis ? this.ellipsisDims.length - 1 : 0)), this.addSymbol(u, n[l++], o); + }), m; + } + }, od = (e) => e + "_max", hf = (e, t, r, n) => { + let i = e.map((m) => m.length).map((m, u) => E(`input${u}`, t, m)), a = k.size(n), d = M("output", t, n.length), l = [...r.symbolToInfo.keys()].filter((m) => !r.rhs.symbolToIndices.has(m)), c = (m) => { + let u = [], h = "var prod = 1.0;", w = "var sum = 0.0;", g = "sum += prod;", y = [], S = [], $ = [], v = [], x = r.symbolToInfo.size === r.rhs.symbolToIndices.size; + r.symbolToInfo.forEach((C, A) => { + var _a2; + if (r.rhs.symbolToIndices.has(A)) { + let P = (_a2 = r.rhs.symbolToIndices.get(A)) == null ? void 0 : _a2[0]; + P !== void 0 && r.lhs.forEach((B, N) => { + if (C.inputIndices.includes(N)) { + let W = B.symbolToIndices.get(A); + if (W === void 0) throw new Error("Invalid symbol error"); + W.forEach((K) => { + u.push(`${i[N].indicesSet(`input${N}Indices`, K, d.indicesGet("outputIndices", P))}`); + }); + } + }); + } else r.lhs.forEach((P, B) => { + if (C.inputIndices.includes(B)) { + let N = P.symbolToIndices.get(A); + if (N === void 0) throw new Error("Invalid symbol error"); + N.forEach((W) => { + y.push(`${i[B].indicesSet(`input${B}Indices`, W, `${A}`)}`); + }), v.push(`prod *= ${i[B].getByIndices(`input${B}Indices`)};`); + } + }), S.push(`for(var ${A}: u32 = 0; ${A} < uniforms.${od(A)}; ${A}++) {`), $.push("}"); + }); + let T = x ? [...u, `let sum = ${i.map((C, A) => C.getByIndices(`input${A}Indices`)).join(" * ")};`] : [...u, w, ...S, ...y, h, ...v, g, ...$]; + return ` + ${m.registerUniforms(l.map((C) => ({ name: `${od(C)}`, type: "u32" }))).registerUniform("outputSize", "u32").declareVariables(...i, d)} + + ${m.mainStart()} + ${m.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.outputSize")} + var outputIndices = ${d.offsetToIndices("global_idx")}; + ${i.map((C, A) => `var input${A}Indices: ${i[A].type.indices};`).join(` + `)} + ${T.join(` + `)}; + ${d.setByOffset("global_idx", "sum")}; + }`; + }; + return { name: "Einsum", shaderCache: { hint: r.equation, inputDependencies: e.map(() => "rank") }, getRunData: () => { + let m = l.filter((h) => r.symbolToInfo.has(h)).map((h) => { + var _a2; + return { type: 12, data: ((_a2 = r.symbolToInfo.get(h)) == null ? void 0 : _a2.dimValue) || 0 }; + }); + m.push({ type: 12, data: a }); + let u = e.map((h, w) => [...R(h)]).reduce((h, w) => h.concat(w), m); + return u.push(...R(n)), { outputs: [{ dims: n, dataType: t }], dispatchGroup: { x: Math.ceil(a / 64) }, programUniforms: u }; + }, getShaderSource: c }; + }, id = (e, t) => { + let r = new $o(e.inputs, t.equation), n = r.outputDims, o = e.inputs.map((i, a) => i.dims); + e.compute(hf(o, e.inputs[0].dataType, r, n)); + }, ad = (e) => { + let t = e.equation.replace(/\s+/g, ""); + return J({ equation: t }); + }; + }); + var gf, ud, yf, bf, dd, ld = V(() => { + "use strict"; + Q(); + ie(); + ae(); + gf = (e) => { + if (!e || e.length !== 2) throw new Error("Expand requires 2 input."); + let t = e[0].dims, r = Array.from(e[1].getBigInt64Array(), Number), n = r.length < t.length ? 0 : r.length - t.length, o = t.length < r.length ? 0 : t.length - r.length; + for (; n < r.length && o < t.length; ++n, ++o) if (r[n] !== t[o] && r[n] !== 1 && t[o] !== 1) throw new Error("Expand requires shape to be broadcastable to input"); + }, ud = (e, t) => { + let r = e.length - t.length, n = []; + for (let o = 0; o < r; ++o) n.push(e[o]); + for (let o = 0; o < t.length; ++o) n.push(t[o] === 1 ? e[o + r] : t[o]); + return n; + }, yf = (e, t) => e.length > t.length ? ud(e, t) : ud(t, e), bf = (e) => { + let t = e[0].dims, r = Array.from(e[1].getBigInt64Array(), Number), n = yf(t, r), o = e[0].dataType, i = o === 9 ? 4 : 1, a = Math.ceil(k.size(n) / i), d = (c) => { + let m = E("input", o, t.length, i), u = M("output", o, n.length, i), h; + if (o === 9) { + let w = (g, y, S = "") => ` + let outputIndices${y} = ${u.offsetToIndices(`outputOffset + ${y}u`)}; + let offset${y} = ${m.broadcastedIndicesToOffset(`outputIndices${y}`, u)}; + let index${y} = offset${y} / 4u; + let component${y} = offset${y} % 4u; + ${g}[${y}] = ${S}(${m.getByOffset(`index${y}`)}[component${y}]); + `; + h = ` + let outputOffset = global_idx * ${i}; + var data = vec4(0); + ${w("data", 0, "u32")} + ${w("data", 1, "u32")} + ${w("data", 2, "u32")} + ${w("data", 3, "u32")} + ${u.setByOffset("global_idx", "data")} + }`; + } else h = ` + let outputIndices = ${u.offsetToIndices("global_idx")}; + let inputOffset = ${m.broadcastedIndicesToOffset("outputIndices", u)}; + ${u.setByOffset("global_idx", m.getByOffset("inputOffset"))} + }`; + return ` + ${c.registerUniform("vec_size", "u32").declareVariables(m, u)} + ${c.mainStart()} + ${c.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.vec_size")} + ${h}`; + }, l = [{ type: 12, data: a }, ...R(t, n)]; + return { name: "Expand", shaderCache: { hint: `${n.length}`, inputDependencies: ["rank"] }, getShaderSource: d, getRunData: () => ({ outputs: [{ dims: n, dataType: e[0].dataType }], dispatchGroup: { x: Math.ceil(a / 64) }, programUniforms: l }) }; + }, dd = (e) => { + gf(e.inputs), e.compute(bf(e.inputs), { inputs: [0] }); + }; + }); + var wf, cd, pd = V(() => { + "use strict"; + Q(); + ie(); + ae(); + Xr(); + wf = (e) => { + let t = e[0].dataType, r = k.size(e[0].dims), n = k.size(e[1].dims), o = n % 4 === 0, i = (a) => { + let d = E("x", t, [1], 4), l = E("bias", t, [1], 4), c = M("y", t, [1], 4), m = [{ name: "output_vec_size", type: "u32" }, { name: "bias_size", type: "u32" }], u = (w) => ` + let bias${w}_offset: u32 = (global_idx * 4 + ${w}) % uniforms.bias_size; + let bias${w} = ${l.getByOffset(`bias${w}_offset / 4`)}[bias${w}_offset % 4];`, h = o ? ` + let bias = ${l.getByOffset("global_idx % (uniforms.bias_size / 4)")};` : `${u(0)}${u(1)}${u(2)}${u(3)} + let bias = ${d.type.value}(bias0, bias1, bias2, bias3);`; + return `${a.registerUniforms(m).declareVariables(d, l, c)} + + ${lo(Ee(t))} + + ${a.mainStart(At)} + ${a.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_vec_size")} + + let x = ${d.getByOffset("global_idx")}; + ${h} + let x_in = x + bias; + ${c.setByOffset("global_idx", co("x_in"))} + }`; + }; + return { name: "FastGeluWithBias", shaderCache: { hint: `${o}`, inputDependencies: ["type", "type"] }, getShaderSource: i, getRunData: (a) => ({ outputs: [{ dims: a[0].dims, dataType: a[0].dataType }], programUniforms: [{ type: 12, data: Math.ceil(r / 4) }, { type: 12, data: n }], dispatchGroup: { x: Math.ceil(r / At / 4) } }) }; + }, cd = (e) => { + e.inputs.length < 2 || k.size(e.inputs[1].dims) === 0 ? du(e) : e.compute(wf(e.inputs)); + }; + }); + var _f, vf, md, fd, hd = V(() => { + "use strict"; + Q(); + ie(); + Ie(); + ae(); + _f = (e) => { + if (!e || e.length !== 2) throw new Error("Gather requires 2 inputs."); + }, vf = (e, t) => { + let r = e[0].dims, n = e[1].dims, o = r.length, i = k.normalizeAxis(t.axis, o), a = r.slice(0); + a.splice(i, 1, ...n); + let d = r[i], l = e[0].dataType === 9 ? 4 : 1, c = Math.ceil(k.size(a) / l), m = [{ type: 12, data: c }, { type: 6, data: d }, { type: 12, data: i }, ...R(e[0].dims, e[1].dims, a)], u = (h) => { + let w = E("data", e[0].dataType, e[0].dims.length, l), g = E("inputIndices", e[1].dataType, e[1].dims.length), y = M("output", e[0].dataType, a.length, l), S = (v) => { + let x = n.length, T = `var indicesIndices${v} = ${g.type.indices}(0);`; + for (let C = 0; C < x; C++) T += `${x > 1 ? `indicesIndices${v}[${C}]` : `indicesIndices${v}`} = ${a.length > 1 ? `outputIndices${v}[uniforms.axis + ${C}]` : `outputIndices${v}`};`; + T += ` + var idx${v} = ${g.getByIndices(`indicesIndices${v}`)}; + if (idx${v} < 0) { + idx${v} = idx${v} + uniforms.axisDimLimit; + } + var dataIndices${v} : ${w.type.indices}; + `; + for (let C = 0, A = 0; C < o; C++) C === i ? (T += `${o > 1 ? `dataIndices${v}[${C}]` : `dataIndices${v}`} = u32(idx${v});`, A += x) : (T += `${o > 1 ? `dataIndices${v}[${C}]` : `dataIndices${v}`} = ${a.length > 1 ? `outputIndices${v}[${A}]` : `outputIndices${v}`};`, A++); + return T; + }, $; + if (e[0].dataType === 9) { + let v = (x, T, C = "") => ` + let outputIndices${T} = ${y.offsetToIndices(`outputOffset + ${T}u`)}; + ${S(T)}; + let offset${T} = ${w.indicesToOffset(`dataIndices${T}`)}; + let index${T} = offset${T} / 4u; + let component${T} = offset${T} % 4u; + ${x}[${T}] = ${C}(${w.getByOffset(`index${T}`)}[component${T}]); + `; + $ = ` + let outputOffset = global_idx * ${l}; + var value = vec4(0); + ${v("value", 0, "u32")} + ${v("value", 1, "u32")} + ${v("value", 2, "u32")} + ${v("value", 3, "u32")} + ${y.setByOffset("global_idx", "value")} + `; + } else $ = ` + let outputIndices = ${y.offsetToIndices("global_idx")}; + ${S("")}; + let value = ${w.getByIndices("dataIndices")}; + ${y.setByOffset("global_idx", "value")}; + `; + return ` + ${h.registerUniform("outputSize", "u32").registerUniform("axisDimLimit", "i32").registerUniform("axis", "u32").declareVariables(w, g, y)} + ${h.mainStart()} + ${h.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.outputSize")} + ${$} + }`; + }; + return { name: "Gather", shaderCache: { hint: t.cacheKey, inputDependencies: ["rank", "rank"] }, getRunData: () => ({ outputs: [{ dims: a, dataType: e[0].dataType }], dispatchGroup: { x: Math.ceil(c / 64) }, programUniforms: m }), getShaderSource: u }; + }, md = (e) => J({ axis: e.axis }), fd = (e, t) => { + let r = e.inputs; + _f(r), e.compute(vf(e.inputs, t)); + }; + }); + var $f, xf, gd, yd, bd = V(() => { + "use strict"; + Q(); + ie(); + Ie(); + ae(); + $f = (e, t) => { + if (e.length < 3 || e.length > 4) throw new Error("GatherBlockQuantized requires 3 or 4 inputs."); + let r = k.normalizeAxis(t.quantizeAxis, e[0].dims.length), n = t.blockSize, o = e[0], i = e[2], a = e.length === 4 ? e[3] : void 0; + if (i.dims.length !== o.dims.length || !o.dims.map((d, l) => l === r ? Math.ceil(d / n) === i.dims[l] : d === i.dims[l]).reduce((d, l) => d && l, true)) throw new Error("Scales must have the same rank as the input tensor and the dims should match except on gatherAxis."); + if (a) { + if (a.dataType !== o.dataType) throw new Error("Zero point must have the same data type as the input tensor."); + if (a.dims.length !== i.dims.length || !a.dims.map((d, l) => d === i.dims[l]).reduce((d, l) => d && l, true)) throw new Error("Zero point must have the same rank as the input tensor and the dims should match except on quantizeAxis."); + } + }, xf = (e, t) => { + let r = e[0].dims, n = e[1].dims, o = r.length, i = k.normalizeAxis(t.gatherAxis, o), a = k.normalizeAxis(t.quantizeAxis, o), d = r.slice(0); + d.splice(i, 1, ...n); + let l = k.size(d), c = e[2].dataType, u = e[0].dataType === 22, h = [{ type: 12, data: l }, { type: 12, data: a }, { type: 12, data: i }, { type: 12, data: t.blockSize }, ...R(...e.map((g, y) => g.dims), d)], w = (g) => { + let y = E("data", e[0].dataType, e[0].dims.length), S = E("inputIndices", e[1].dataType, e[1].dims.length), $ = E("scales", e[2].dataType, e[2].dims.length), v = e.length > 3 ? E("zeroPoint", e[3].dataType, e[3].dims.length) : void 0, x = M("output", c, d.length), T = [y, S, $]; + v && T.push(v); + let C = [{ name: "output_size", type: "u32" }, { name: "quantize_axis", type: "u32" }, { name: "gather_axis", type: "u32" }, { name: "block_size", type: "u32" }]; + return ` + ${g.registerUniforms(C).declareVariables(...T, x)} + ${g.mainStart()} + let output_indices = ${x.offsetToIndices("global_idx")}; + var indices_indices = ${S.type.indices}(0); + ${(() => n.length > 1 ? ` + for (var i: u32 = 0; i < ${n.length}; i++) { + let index = ${x.indicesGet("output_indices", "uniforms.gather_axis + i")}; + ${S.indicesSet("indices_indices", "i", "index")}; + }` : `indices_indices = ${x.indicesGet("output_indices", "uniforms.gather_axis")};`)()}; + var data_indices = ${y.type.indices}(0); + for (var i: u32 = 0; i < uniforms.gather_axis; i++) { + let index = ${x.indicesGet("output_indices", "i")}; + ${y.indicesSet("data_indices", "i", "index")}; + } + var index_from_indices = ${S.getByIndices("indices_indices")}; + if (index_from_indices < 0) { + index_from_indices += ${r[i]}; + } + ${y.indicesSet("data_indices", "uniforms.gather_axis", "u32(index_from_indices)")}; + for (var i = uniforms.gather_axis + 1; i < ${d.length}; i++) { + let index = ${x.indicesGet("output_indices", `i + ${n.length} - 1`)}; + ${y.indicesSet("data_indices", "i", "index")}; + } + let data_offset = ${y.indicesToOffset("data_indices")}; + let data_index = data_offset % 8; + // Convert 4-bit packed data to 8-bit packed data. + let packed_4bit_quantized_data = ${y.getByOffset("data_offset / 8")}; + let packed_8bit_quantized_data = (packed_4bit_quantized_data >> (4 * (data_index % 2))) & 0x0f0f0f0f; + let quantized_data_vec = ${u ? "unpack4xI8" : "unpack4xU8"}(u32(packed_8bit_quantized_data)); + let quantized_data = quantized_data_vec[data_index / 2]; + var scale_indices = data_indices; + let quantize_axis_index = ${$.indicesGet("data_indices", "uniforms.quantize_axis")} / uniforms.block_size; + ${$.indicesSet("scale_indices", "uniforms.quantize_axis", "quantize_axis_index")}; + var scale = ${$.getByIndices("scale_indices")}; + ${(() => v ? ` + let zero_point_indices = scale_indices; + let zero_point_offset = ${v.indicesToOffset("zero_point_indices")}; + let zero_point_index = zero_point_offset % 8; + let packed_4bit_zero_points = ${v.getByOffset("zero_point_offset / 8")}; + let packed_8bit_zero_points = (packed_4bit_zero_points >> (4 * (zero_point_index % 2))) & 0x0f0f0f0f; + let zero_point_vec = ${u ? "unpack4xI8" : "unpack4xU8"}(u32(packed_8bit_zero_points)); + let zero_point = zero_point_vec[zero_point_index / 2];` : "var zero_point = 0")()}; + let dequantized_data = ${Ee(c)}(quantized_data - zero_point) * scale; + ${x.setByOffset("global_idx", "dequantized_data")}; + }`; + }; + return { name: "GatherBlockQuantized", shaderCache: { hint: `${t.cacheKey};${e.filter((g, y) => y !== 1).map((g) => g.dims.join("_")).join(";")}`, inputDependencies: Array.from({ length: e.length }, (g, y) => "rank") }, getRunData: () => ({ outputs: [{ dims: d, dataType: c }], dispatchGroup: { x: Math.ceil(l / 64) }, programUniforms: h }), getShaderSource: w }; + }, gd = (e, t) => { + let r = e.inputs; + $f(r, t), e.compute(xf(e.inputs, t)); + }, yd = (e) => J({ blockSize: e.blockSize, gatherAxis: e.gatherAxis, quantizeAxis: e.quantizeAxis }); + }); + var Sf, Tf, wd, _d, vd = V(() => { + "use strict"; + Q(); + ie(); + Ie(); + ae(); + Sf = (e) => { + if (!e || e.length !== 2) throw new Error("GatherElements requires 2 inputs."); + if (e[0].dims.length < 1) throw new Error("GatherElements requires that the data input be rank >= 1."); + if (e[0].dims.length !== e[1].dims.length) throw new Error(`GatherElements requires that the data input and + indices input tensors be of same rank.`); + }, Tf = (e, t) => { + let r = e[0].dims, n = e[0].dataType, o = r.length, i = e[1].dims, a = e[1].dataType, d = k.normalizeAxis(t.axis, o), l = r[d], c = i.slice(0), m = k.size(c), u = E("input", n, o), h = E("indicesInput", a, i.length), w = M("output", n, c.length), g = [{ type: 12, data: m }, { type: 6, data: l }, { type: 12, data: d }]; + return g.push(...R(r, i, c)), { name: "GatherElements", shaderCache: { inputDependencies: ["rank", "rank"] }, getRunData: () => ({ outputs: [{ dims: c, dataType: e[0].dataType }], dispatchGroup: { x: Math.ceil(m / 64) }, programUniforms: g }), getShaderSource: ($) => ` + ${$.registerUniform("outputSize", "u32").registerUniform("axisDimLimit", "i32").registerUniform("axis", "u32").declareVariables(u, h, w)} + ${$.mainStart()} + ${$.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.outputSize")} + + let outputIndices = ${w.offsetToIndices("global_idx")}; + + var idx = ${h.getByOffset("global_idx")}; + if (idx < 0) { + idx = idx + uniforms.axisDimLimit; + } + var inputIndices = ${u.type.indices}(outputIndices); + ${u.indicesSet("inputIndices", "uniforms.axis", "u32(idx)")}; + let value = ${u.getByIndices("inputIndices")}; + + ${w.setByOffset("global_idx", "value")}; + }` }; + }, wd = (e) => J({ axis: e.axis }), _d = (e, t) => { + let r = e.inputs; + Sf(r), e.compute(Tf(e.inputs, t)); + }; + }); + var If, Cf, $d, xd, Sd = V(() => { + "use strict"; + Q(); + ie(); + ae(); + If = (e) => { + if (!e) throw new Error("Input is missing"); + if (e.length < 2 || e.length > 3) throw new Error("Invaid input number."); + if (e.length === 3 && e[2].dims.length > 2) throw new Error("Invalid input shape of C"); + if (e[0].dataType !== e[1].dataType || e.length === 3 && e[0].dataType !== e[2].dataType) throw new Error("Input types are mismatched"); + }, Cf = (e, t) => { + let r = e[0].dims.slice(), n = e[1].dims.slice(), [o, i, a] = Gr.getShapeOfGemmResult(r, t.transA, n, t.transB, e.length === 3 ? e[2].dims : void 0), d = [o, i]; + if (!d) throw new Error("Can't use gemm on the given tensors"); + let l = k.size(d), c = [{ type: 12, data: l }, { type: 12, data: o }, { type: 12, data: i }, { type: 12, data: a }, { type: 1, data: t.alpha }, { type: 1, data: t.beta }], m = ["type", "type"]; + e.length === 3 && (c.push(...R(e[2].dims)), m.push("rank")), c.push(...R(d)); + let u = (h) => { + let w = ""; + t.transA && t.transB ? w = "value += a[k * uniforms.M + m] * b[n * uniforms.K + k];" : t.transA && !t.transB ? w = "value += a[k * uniforms.M + m] * b[k * uniforms.N + n];" : !t.transA && t.transB ? w = "value += a[m * uniforms.K + k] * b[n * uniforms.K + k];" : !t.transA && !t.transB && (w = "value += a[m * uniforms.K + k] * b[k * uniforms.N + n];"); + let g = t.alpha === 1 ? "" : "value *= uniforms.alpha;", y = E("a", e[0].dataType, e[0].dims), S = E("b", e[1].dataType, e[1].dims), $ = y.type.value, v = null, x = [y, S]; + e.length === 3 && (v = E("c", e[2].dataType, e[2].dims.length), x.push(v)); + let T = M("output", e[0].dataType, d.length); + x.push(T); + let C = [{ name: "output_size", type: "u32" }, { name: "M", type: "u32" }, { name: "N", type: "u32" }, { name: "K", type: "u32" }, { name: "alpha", type: "f32" }, { name: "beta", type: "f32" }]; + return ` + ${h.registerUniforms(C).declareVariables(...x)} + + ${h.mainStart()} + ${h.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} + + let m = global_idx / uniforms.N; + let n = global_idx % uniforms.N; + + var value = ${$}(0); + for (var k: u32 = 0u; k < uniforms.K; k++) { + ${w} + } + + ${g} + ${(() => v != null ? `let cOffset = ${v.broadcastedIndicesToOffset("vec2(m, n)", T)}; value += ${$}(uniforms.beta) * ${v.getByOffset("cOffset")};` : "")()} + output[global_idx] = value; + }`; + }; + return { name: "Gemm", shaderCache: { hint: `${t.cacheKey}`, inputDependencies: m }, getRunData: () => ({ outputs: [{ dims: d, dataType: e[0].dataType }], dispatchGroup: { x: Math.ceil(l / 64) }, programUniforms: c }), getShaderSource: u }; + }, $d = (e) => { + let t = e.transA, r = e.transB, n = e.alpha, o = e.beta; + return { transA: t, transB: r, alpha: n, beta: o, cacheKey: `${e.transA};${e.transB};${e.alpha === 1}` }; + }, xd = (e, t) => { + If(e.inputs), e.compute(Cf(e.inputs, t)); + }; + }); + var Ne, Ef, Id, Td, Pf, nr, Cd, xo = V(() => { + "use strict"; + Q(); + ie(); + Ie(); + Hr(); + Kr(); + ae(); + lt(); + Ne = (e, t) => e.length > t && e[t].dims.length > 0 ? e[t] : void 0, Ef = (e, t) => { + let r = e[0], n = Ne(e, 1), o = Ne(e, 2), i = Ne(e, 3), a = Ne(e, 4), d = Ne(e, 5), l = Ne(e, 6), c = Ne(e, 7); + if (r.dims.length !== 3 && r.dims.length !== 5) throw new Error("Input query is expected to have 3 or 5 dimensions"); + let m = r.dims[0], u = r.dims[1], h = r.dims.length === 3 ? r.dims[2] : t.numHeads * r.dims[4], w = u, g = 0, y = 0, S = Math.floor(h / t.numHeads); + if (l && c && k.size(l.dims) && k.size(c.dims)) { + if (l.dims.length !== 4) throw new Error('Input "past_key" is expected to have 4 dimensions'); + if (l.dims[0] !== m || l.dims[1] !== t.numHeads || l.dims[3] !== S) throw new Error('Input "past_key" shape (batch_size, num_heads, past_sequence_length, head_size)'); + if (c.dims[0] !== m || c.dims[1] !== t.numHeads || c.dims[3] !== S) throw new Error('Input "past_value" shape (batch_size, num_heads, past_sequence_length, head_size)'); + if (l.dims[2] !== c.dims[2]) throw new Error('Input "past_key" and "past_value" shall have same dim 2 (past_sequence_length)'); + if (c.dims.length !== 4) throw new Error('Input "past_value" is expected to have 4 dimensions'); + g = l.dims[2], y = l.dims[2]; + } else if (l && k.size(l.dims) || c && k.size(c.dims)) throw new Error('Input "past_key" and "past_value" shall be both present or both absent'); + let $; + if (n && k.size(n.dims) > 0) { + if (r.dims.length !== 3) throw new Error('Input "query" is expected to have 3 dimensions when key is given'); + if (n.dims.length < 3 || n.dims.length > 5) throw new Error('Input "key" is expected to have 3, 4, or 5 dimensions'); + if (r.dims[0] !== n.dims[0]) throw new Error('Input "query" and "key" shall have same dim 0 (batch size)'); + if (n.dims.length === 3) { + if (n.dims[2] !== r.dims[2]) throw new Error('Input "query" and "key" shall have same dim 2 (hidden_size)'); + $ = 2, w = n.dims[1]; + } else if (n.dims.length === 5) { + if (n.dims[2] !== t.numHeads || n.dims[3] !== 2 || n.dims[4] !== S) throw new Error('Expect "key" shape (batch_size, kv_sequence_length, num_heads, 2, head_size) for packed kv'); + if (o) throw new Error('Expect "value" be none when "key" has packed kv format.'); + $ = 5, w = n.dims[1]; + } else { + if (n.dims[1] !== t.numHeads || n.dims[3] !== S) throw new Error('Expect "key" shape (batch_size, num_heads, kv_sequence_length, head_size) for past_key'); + $ = 0, w = n.dims[2]; + } + } else { + if (r.dims.length !== 5) throw new Error('Input "query" is expected to have 5 dimensions when key is empty'); + if (r.dims[2] !== t.numHeads || r.dims[3] !== 3) throw new Error('Expect "query" shape (batch_size, kv_sequence_length, num_heads, 3, head_size) for packed kv'); + $ = 3; + } + if (i && k.size(i.dims) > 0) { + if (i.dims.length !== 1) throw new Error('Input "bias" is expected to have 1 dimension'); + if (n && n.dims.length === 5 && n.dims[3] === 2) throw new Error("bias is not allowed for packed kv."); + } + let v = g + w, x = 0; + if (a && k.size(a.dims) > 0) { + x = 8; + let P = a.dims; + throw P.length === 1 ? P[0] === m ? x = 1 : P[0] === 3 * m + 2 && (x = 3) : P.length === 2 && P[0] === m && P[1] === v && (x = 5), x === 8 ? new Error('Input "key_padding_mask" shape shall be (batch_size) or (batch_size, total_sequence_length)') : new Error("Mask not supported"); + } + let T = false, C = h; + if (o && k.size(o.dims) > 0) { + if (o.dims.length !== 3 && o.dims.length !== 4) throw new Error('Input "value" is expected to have 3 or 4 dimensions'); + if (r.dims[0] !== o.dims[0]) throw new Error('Input "query" and "value" shall have same dim 0 (batch_size)'); + if (o.dims.length === 3) { + if (w !== o.dims[1]) throw new Error('Input "key" and "value" shall have the same dim 1 (kv_sequence_length)'); + C = o.dims[2]; + } else { + if (w !== o.dims[2]) throw new Error('Input "key" and "value" shall have the same dim 2 (kv_sequence_length)'); + C = o.dims[1] * o.dims[3], T = true; + } + } + let A = false; + if (a && k.size(a.dims) > 0) throw new Error("Key padding mask is not supported"); + if (d && k.size(d.dims) > 0) { + if (d.dims.length !== 4) throw new Error('Input "attention_bias" is expected to have 4 dimensions'); + if (d.dims[0] !== m || d.dims[1] !== t.numHeads || d.dims[2] !== u || d.dims[3] !== v) throw new Error('Expect "attention_bias" shape (batch_size, num_heads, sequence_length, total_sequence_length)'); + } + return { batchSize: m, sequenceLength: u, pastSequenceLength: g, kvSequenceLength: w, totalSequenceLength: v, maxSequenceLength: y, inputHiddenSize: 0, hiddenSize: h, vHiddenSize: C, headSize: S, vHeadSize: Math.floor(C / t.numHeads), numHeads: t.numHeads, isUnidirectional: false, pastPresentShareBuffer: false, maskFilterValue: t.maskFilterValue, maskType: x, scale: t.scale, broadcastResPosBias: A, passPastInKv: T, qkvFormat: $ }; + }, Id = (e) => J({ ...e }), Td = J({ perm: [0, 2, 1, 3] }), Pf = (e, t, r, n, o, i, a) => { + let d = [n, o, i], l = k.size(d), c = [{ type: 12, data: l }, { type: 12, data: a }, { type: 12, data: i }], m = (u) => { + let h = M("qkv_with_bias", t.dataType, d), w = E("qkv", t.dataType, d), g = E("bias", r.dataType, d), y = [{ name: "output_size", type: "u32" }, { name: "bias_offset", type: "u32" }, { name: "hidden_size", type: "u32" }]; + return ` + ${u.registerUniforms(y).declareVariables(w, g, h)} + ${u.mainStart()} + ${u.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} + let bias_offset_idx = (global_idx % uniforms.hidden_size) + uniforms.bias_offset; + + qkv_with_bias[global_idx] = qkv[global_idx] + bias[bias_offset_idx]; + }`; + }; + return e.compute({ name: "MultiHeadAttentionAddBias", shaderCache: { inputDependencies: ["type", "type"] }, getRunData: () => ({ outputs: [{ dims: d, dataType: t.dataType, gpuDataType: 0 }], dispatchGroup: { x: Math.ceil(l / 64) }, programUniforms: c }), getShaderSource: m }, { inputs: [t, r], outputs: [-1] })[0]; + }, nr = (e, t, r, n, o, i, a, d) => { + let l = i; + if (a && k.size(a.dims) > 0) { + if (n === 1) throw new Error("AddBiasReshape is not implemented. Please export your model with packed QKV or KV"); + return l = Pf(e, i, a, t, n, r * o, d), l = l.reshape([t, n, r, o]), r === 1 || n === 1 ? l : e.compute(Pe(l, Td.perm), { inputs: [l], outputs: [-1] })[0]; + } else return i.dims.length === 3 && (l = i.reshape([t, n, r, o])), r === 1 || n === 1 ? l : e.compute(Pe(l, Td.perm), { inputs: [l], outputs: [-1] })[0]; + }, Cd = (e, t) => { + let r = Ef(e.inputs, t), n = e.inputs[0], o = Ne(e.inputs, 1), i = Ne(e.inputs, 2), a = Ne(e.inputs, 3), d = Ne(e.inputs, 4), l = Ne(e.inputs, 5), c = Ne(e.inputs, 6), m = Ne(e.inputs, 7); + if (n.dims.length === 5) throw new Error("Packed QKV is not implemented"); + if ((o == null ? void 0 : o.dims.length) === 5) throw new Error("Packed KV is not implemented"); + let u = o && i && o.dims.length === 4 && i.dims.length === 4, h = nr(e, r.batchSize, r.numHeads, r.sequenceLength, r.headSize, n, a, 0); + if (u) return Wt(e, h, o, i, d, void 0, c, m, l, r, t); + if (!o || !i) throw new Error("key and value must be provided"); + let w = nr(e, r.batchSize, r.numHeads, r.kvSequenceLength, r.headSize, o, a, r.hiddenSize), g = nr(e, r.batchSize, r.numHeads, r.kvSequenceLength, r.vHeadSize, i, a, 2 * r.hiddenSize); + Wt(e, h, w, g, d, void 0, c, m, l, r, t); + }; + }); + var Ad, zf, Of, So, kd, To = V(() => { + "use strict"; + Q(); + ie(); + ae(); + Ad = (e) => Array.from(e.getBigInt64Array(), Number), zf = (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 (Ad(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"); + }, Of = (e, t) => { + let r = []; + for (let n = 0; n < e.length; ++n) r.push(e[n] * t[n]); + return r; + }, So = (e, t) => { + let r = e[0].dims, n = t ?? Ad(e[1]), o = Of(r, n), i = k.size(o), a = e[0].dataType, d = E("input", a, r.length), l = M("output", a, o.length), c = (m) => ` + const inputShape = ${d.indices(...r)}; + ${m.registerUniform("output_size", "u32").declareVariables(d, l)} + ${m.mainStart()} + ${m.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} + let output_indices = ${l.offsetToIndices("global_idx")}; + var input_indices: ${d.type.indices}; + for (var i = 0; i < ${r.length}; i++) { + let input_dim_i = ${d.indicesGet("uniforms.input_shape", "i")}; + let input_dim_value = ${l.indicesGet("output_indices", "i")} % input_dim_i; + + ${d.indicesSet("input_indices", "i", "input_dim_value")} + } + ${l.setByOffset("global_idx", d.getByIndices("input_indices"))} + }`; + return { name: "Tile", shaderCache: { hint: `${n}`, inputDependencies: ["rank"] }, getRunData: () => ({ outputs: [{ dims: o, dataType: e[0].dataType }], dispatchGroup: { x: Math.ceil(i / 64) }, programUniforms: [{ type: 12, data: i }, ...R(e[0].dims, o)] }), getShaderSource: c }; + }, kd = (e) => { + zf(e.inputs), e.compute(So(e.inputs), { inputs: [0] }); + }; + }); + var Bf, Ed, zd, Df, Pd, Od, Bd = V(() => { + "use strict"; + Q(); + ie(); + Ie(); + Kr(); + ae(); + xo(); + To(); + lt(); + Bf = (e, t) => { + let r = e[0], n = e[1], o = e[2], i = e[3], a = e[4]; + if (r.dims.length !== 3 && r.dims.length !== 5) throw new Error("Input query is expected to have 3 or 5 dimensions"); + let d = false, l = r.dims[0], c = r.dims[1], m = r.dims.length === 3 ? d ? r.dims[2] / 3 : r.dims[2] : t.numHeads * r.dims[4], u = c, h = 0, w = 0, g = Math.floor(m / t.numHeads), y = i && i.dims.length !== 0, S = a && a.dims.length !== 0, $ = true; + if (y && S) { + if (i.dims.length !== 4) throw new Error('Input "past_key" is expected to have 4 dimensions'); + if (a.dims.length !== 4) throw new Error('Input "past_value" is expected to have 4 dimensions'); + $ ? (h = i.dims[1], w = i.dims[1]) : (h = i.dims[2], w = i.dims[2]); + } else if (y || S) throw new Error('Input "past_key" and "past_value" shall be both present or both absent'); + let v; + if (n) { + if (r.dims.length !== 3) throw new Error('Input "query" is expected to have 3 dimensions when key is given'); + if (n.dims.length < 3 || n.dims.length > 5) throw new Error('Input "key" is expected to have 3, 4, or 5 dimensions'); + if (r.dims[0] !== n.dims[0]) throw new Error('Input "query" and "key" shall have same dim 0 (batch size)'); + if (n.dims.length === 3) { + if (r.dims[2] % n.dims[2] !== 0) throw new Error('Dimension 2 of "query" should be a multiple of "key"'); + v = 2, u = n.dims[1]; + } else if (n.dims.length === 5) { + if (n.dims[2] !== t.numHeads || n.dims[3] !== 2 || n.dims[4] !== g) throw new Error('Expect "key" shape (batch_size, kv_sequence_length, num_heads, 2, head_size) for packed kv'); + if (o) throw new Error('Expect "value" be none when "key" has packed kv format.'); + v = 5, u = n.dims[1]; + } else { + if (n.dims[1] !== t.numHeads || n.dims[3] !== g) throw new Error('Expect "key" shape (batch_size, num_heads, kv_sequence_length, head_size) for past_key'); + v = 0, u = n.dims[2]; + } + } else { + if (r.dims.length !== 3 && r.dims.length !== 5) throw new Error('Input "query" is expected to have 3 or 5 dimensions when key is empty'); + if (r.dims.length === 5 && (r.dims[2] !== t.numHeads || r.dims[3] !== 3)) throw new Error('Expect "query" shape (batch_size, kv_sequence_length, num_heads, 3, head_size) for packed kv'); + v = 3; + } + let x = 0, T = false, C = m; + 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 (r.dims[0] !== o.dims[0]) throw new Error('Input "query" and "value" shall have same dim 0 (batch_size)'); + if (o.dims.length === 3) { + if (u !== o.dims[1]) throw new Error('Input "key" and "value" shall have the same dim 1 (kv_sequence_length)'); + C = o.dims[2]; + } else { + if (u !== o.dims[2]) throw new Error('Input "past_key" and "past_value" shall have the same dim 2 (kv_sequence_length)'); + C = o.dims[1] * o.dims[3], T = true; + } + } + let A = h + u, P = false; + return { batchSize: l, sequenceLength: c, pastSequenceLength: h, kvSequenceLength: u, totalSequenceLength: A, maxSequenceLength: w, inputHiddenSize: 0, hiddenSize: m, vHiddenSize: C, headSize: g, vHeadSize: Math.floor(C / t.kvNumHeads), numHeads: t.numHeads, kvNumHeads: t.kvNumHeads, nReps: t.numHeads / t.kvNumHeads, pastPresentShareBuffer: false, maskType: x, scale: t.scale, broadcastResPosBias: P, passPastInKv: T, qkvFormat: v, isPastkvBSNH: $ }; + }, Ed = (e, t, r, n) => { + let o = [n.batchSize, n.totalSequenceLength, n.kvNumHeads, n.headSize], i = 4, a = k.size(o) / i, d = n.totalSequenceLength, l = M("present_kv", r, o.length, i), c = E("new_kv", e.dataType, e.dims.length, i), m = t ? E("past_kv", t.dataType, t.dims.length, i) : void 0, u = Math.ceil(n.headSize / i), h = { x: d, y: e.dims[0], z: 1 }, w = t ? ["rank", "rank"] : ["rank"], g = [{ type: 12, data: a }, { type: 12, data: n.pastSequenceLength }, { type: 12, data: n.kvSequenceLength }, { type: 12, data: n.totalSequenceLength }], y = [c]; + m ? (g.push(...R(e.dims), ...R(t.dims), ...R(o)), y.push(m)) : g.push(...R(e.dims), ...R(o)); + let S = [{ name: "output_size", type: "u32" }, { name: "past_seqlen", type: "u32" }, { name: "new_seqlen", type: "u32" }, { name: "present_seqlen", type: "u32" }], $ = ` 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];`, v = ` 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];`, x = t ? `if (s < past_seqlen) { + ${$} + } else if (s < past_seqlen + uniforms.new_seqlen) { + ${v} + }` : `if (s < past_seqlen + uniforms.new_seqlen) { + ${v} + }`, T = (C) => ` + + ${C.registerUniforms(S).declareVariables(...y, l)} + ${C.mainStart([u, n.kvNumHeads, 1])} + ${C.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} + var indices = ${l.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 = ${n.kvNumHeads}u; + let H = ${u}u; + + let present_seqlen = uniforms.present_seqlen; + let present_batch_stride = present_seqlen * num_heads * H; + var row_stride = H; + let is_bsnh = ${n.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; + ${x} + }`; + return { name: "ConcatPastNew", shaderCache: { hint: `${n.kvNumHeads}${u}${!!t}`, inputDependencies: w }, getRunData: () => ({ outputs: [{ dims: o, dataType: r }], dispatchGroup: h, programUniforms: g }), getShaderSource: T }; + }, zd = (e) => J({ ...e }), Df = J({ perm: [0, 2, 1, 3] }), Pd = (e, t, r, n, o) => { + let i = t, a = n.kvNumHeads, d = n.nReps; + return t.dims.length === 3 && n.kvSequenceLength !== 0 && (i = t.reshape([n.batchSize, n.kvSequenceLength, a, n.headSize])), r ? i = e.compute(Ed(i, r, i.dataType, n), { inputs: [i, r], outputs: [n.isPastkvBSNH ? o : -1] })[0] : i = e.compute(Ed(i, void 0, i.dataType, n), { inputs: [i], outputs: [n.isPastkvBSNH ? o : -1] })[0], d !== 1 && (i = e.compute(So([i], [1, 1, 1, d]), { inputs: [i], outputs: [-1] })[0], i = i.reshape([n.batchSize, n.totalSequenceLength, a * d, n.headSize])), e.compute(Pe(i, Df.perm), { inputs: [i], outputs: [-1] })[0]; + }, Od = (e, t) => { + var _a2; + let r = Bf(e.inputs, t); + if (e.inputs[0].dims.length === 5) throw new Error("Packed QKV is not implemented"); + if (((_a2 = e.inputs[1]) == null ? void 0 : _a2.dims.length) === 5) throw new Error("Packed KV is not implemented"); + let n = nr(e, r.batchSize, r.numHeads, r.sequenceLength, r.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, a = Pd(e, e.inputs[1], o, r, 1), d = Pd(e, e.inputs[2], i, r, 2); + Wt(e, n, a, d, void 0, void 0, void 0, void 0, void 0, r, t); + }; + }); + var Dd, Mf, Rf, Md, Rd = V(() => { + "use strict"; + Q(); + ie(); + lt(); + ae(); + Dd = (e, t, r, n, o, i, a, d) => { + let l = we(i), c = l === 1 ? "f32" : `vec${l}f`, m = l === 1 ? "vec2f" : `mat2x${l}f`, u = o * a, h = [o, a, i / l], w = [o, a, 2], g = ["rank", "type", "type"], y = []; + y.push(...R(h, w)); + let S = ($) => { + let v = E("x", t.dataType, 3, l), x = E("scale", r.dataType, r.dims), T = E("bias", n.dataType, n.dims), C = M("output", 1, 3, 2), A = [v, x, T, C], P = 64; + return ` + var workgroup_shared : array<${m}, ${P}>; + const workgroup_size = ${P}u; + ${$.declareVariables(...A)} + ${$.mainStart(P)} + let batch = workgroup_index / uniforms.x_shape[1]; + let channel = workgroup_index % uniforms.x_shape[1]; + let hight = uniforms.x_shape[2]; + // initialize workgroup memory + var sum = ${c}(0); + var squared_sum = ${c}(0); + for (var h = local_idx; h < hight; h += workgroup_size) { + let value = ${c}(${v.get("batch", "channel", "h")}); + sum += value; + squared_sum += value * value; + } + workgroup_shared[local_idx] = ${m}(sum, squared_sum); + workgroupBarrier(); + + for (var currSize = workgroup_size >> 1; currSize > 0; currSize = currSize >> 1) { + if (local_idx < currSize) { + workgroup_shared[local_idx] = workgroup_shared[local_idx] + workgroup_shared[local_idx + currSize]; + } + workgroupBarrier(); + } + if (local_idx == 0) { + let sum_final = ${Ze("workgroup_shared[0][0]", l)} / f32(hight * ${l}); + let squared_sum_final = ${Ze("workgroup_shared[0][1]", l)} / f32(hight * ${l}); + + let inv_std_dev = inverseSqrt(squared_sum_final - sum_final * sum_final + f32(${d})); + let channel_scale = inv_std_dev * f32(scale[channel]); + let channel_shift = f32(bias[channel]) - sum_final * channel_scale; + output[workgroup_index] = vec2f(channel_scale, channel_shift); + } + }`; + }; + return e.compute({ name: "InstanceNormComputeChannelScaleShift", shaderCache: { hint: `${l};${d}`, inputDependencies: g }, getRunData: () => ({ outputs: [{ dims: w, dataType: 1 }], dispatchGroup: { x: u }, programUniforms: y }), getShaderSource: S }, { inputs: [t, r, n], outputs: [-1] })[0]; + }, Mf = (e, t, r) => { + let n = t[0].dims, o = n, i = 2, a = n[0], d = n[1], l = k.sizeFromDimension(n, i), c = we(l), m = k.size(o) / c, u = Dd(e, t[0], t[1], t[2], a, l, d, r.epsilon), h = [a, d, l / c], w = [a, d], g = ["type", "none"], y = (S) => { + let $ = E("x", t[0].dataType, h.length, c), v = E("scale_shift", 1, w.length, 2), x = M("output", t[0].dataType, h.length, c), T = [$, v, x]; + return ` + ${S.registerUniform("output_size", "u32").declareVariables(...T)} + ${S.mainStart()} + ${S.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} + let outputIndices = ${x.offsetToIndices("global_idx")}; + let batch = outputIndices[0]; + let channel = outputIndices[1]; + let scale_shift = ${v.getByIndices("vec2(batch, channel)")}; + let value = ${$.getByOffset("global_idx")} * ${x.type.value}(scale_shift.x) + ${x.type.value}(scale_shift.y); + ${x.setByOffset("global_idx", "value")}; + }`; + }; + e.compute({ name: "InstanceNormalization", shaderCache: { hint: `${c}`, inputDependencies: g }, getRunData: () => ({ outputs: [{ dims: o, dataType: t[0].dataType }], dispatchGroup: { x: Math.ceil(m / 64) }, programUniforms: [{ type: 12, data: m }, ...R(h, w, h)] }), getShaderSource: y }, { inputs: [t[0], u] }); + }, Rf = (e, t, r) => { + let n = t[0].dims, o = n, i = n[0], a = n[n.length - 1], d = k.sizeFromDimension(n, 1) / a, l = we(a), c = k.size(o) / l, m = [{ type: 12, data: d }, { type: 12, data: Math.floor(a / l) }], u = ["type", "type"], h = [0, n.length - 1]; + for (let S = 0; S < n.length - 2; S++) h.push(S + 1); + let w = e.compute(Pe(e.inputs[0], h), { inputs: [e.inputs[0]], outputs: [-1] })[0], g = Dd(e, w, t[1], t[2], i, d, a, r.epsilon), y = (S) => { + let $ = he(t[0].dataType), v = l === 1 ? "vec2f" : `mat${l}x2f`, x = (A) => { + let P = A === 0 ? "x" : "y", B = l === 1 ? "f32" : `vec${l}f`; + switch (l) { + case 1: + return `${$}(${B}(scale.${P}))`; + case 2: + return `vec2<${$}>(${B}(scale[0].${P}, scale[1].${P}))`; + case 4: + return `vec4<${$}>(${B}(scale[0].${P}, scale[1].${P}, scale[2].${P}, scale[3].${P}))`; + default: + throw new Error(`Not supported compoents ${l}`); + } + }, T = E("input", t[0].dataType, t[0].dims, l), C = M("output", t[0].dataType, o, l); + return ` + @group(0) @binding(0) var input : array<${T.type.storage}>; + @group(0) @binding(1) var scale_input : array<${v}>; + @group(0) @binding(2) var output : array<${C.type.storage}>; + struct Uniforms {H: u32, C : u32}; + @group(0) @binding(3) var uniforms: Uniforms; + + ${S.mainStart()} + let current_image_number = global_idx / (uniforms.C * uniforms.H); + let current_channel_number = global_idx % uniforms.C; + + let scale_offset = current_image_number * uniforms.C + current_channel_number; + let scale = scale_input[scale_offset]; + output[global_idx] = fma(input[global_idx], ${x(0)}, ${x(1)}); + }`; + }; + e.compute({ name: "InstanceNormalizationNHWC", shaderCache: { hint: `${l}`, inputDependencies: u }, getRunData: () => ({ outputs: [{ dims: o, dataType: t[0].dataType }], dispatchGroup: { x: Math.ceil(c / 64) }, programUniforms: m }), getShaderSource: y }, { inputs: [t[0], g] }); + }, Md = (e, t) => { + t.format === "NHWC" ? Rf(e, e.inputs, t) : Mf(e, e.inputs, t); + }; + }); + var Uf, Vf, Ud, Vd = V(() => { + "use strict"; + Q(); + ie(); + ae(); + Uf = (e) => { + if (!e || e.length < 2) throw new Error("layerNorm requires at least 2 inputs."); + }, Vf = (e, t, r) => { + let n = t.simplified, o = e[0].dims, i = e[1], a = !n && e[2], d = o, l = k.normalizeAxis(t.axis, o.length), c = k.sizeToDimension(o, l), m = k.sizeFromDimension(o, l), u = k.size(i.dims), h = a ? k.size(a.dims) : 0; + if (u !== m || a && h !== m) throw new Error(`Size of X.shape()[axis:] == ${m}. + Size of scale and bias (if provided) must match this. + Got scale size of ${u} and bias size of ${h}`); + let w = []; + for (let C = 0; C < o.length; ++C) C < l ? w.push(o[C]) : w.push(1); + let g = we(m), y = ["type", "type"], S = [{ type: 12, data: c }, { type: 1, data: m }, { type: 12, data: Math.floor(m / g) }, { type: 1, data: t.epsilon }]; + a && y.push("type"); + let $ = r > 1, v = r > 2, x = (C) => { + let A = he(e[0].dataType), P = [E("x", e[0].dataType, e[0].dims, g), E("scale", i.dataType, i.dims, g)]; + a && P.push(E("bias", a.dataType, a.dims, g)), P.push(M("output", e[0].dataType, d, g)), $ && P.push(M("mean_data_output", 1, w)), v && P.push(M("inv_std_output", 1, w)); + let B = [{ name: "norm_count", type: "u32" }, { name: "norm_size", type: "f32" }, { name: "norm_size_vectorized", type: "u32" }, { name: "epsilon", type: "f32" }]; + return ` + ${C.registerUniforms(B).declareVariables(...P)} + ${C.mainStart()} + ${C.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.norm_count")} + let offset = global_idx * uniforms.norm_size_vectorized; + var mean_vector = ${io("f32", g)}; + var mean_square_vector = ${io("f32", g)}; + + for (var h: u32 = 0u; h < uniforms.norm_size_vectorized; h++) { + let value = ${kt(A, g, "x[h + offset]")}; + mean_vector += value; + mean_square_vector += value * value; + } + let mean = ${Ze("mean_vector", g)} / uniforms.norm_size; + let inv_std_dev = inverseSqrt(${Ze("mean_square_vector", g)} / uniforms.norm_size ${n ? "" : "- mean * mean"} + uniforms.epsilon); + + for (var j: u32 = 0; j < uniforms.norm_size_vectorized; j++) { + let f32input = ${kt(A, g, "x[j + offset]")}; + let f32scale = ${kt(A, g, "scale[j]")}; + output[j + offset] = ${P[0].type.value}((f32input ${n ? "" : "- mean"}) * inv_std_dev * f32scale + ${a ? `+ ${kt(A, g, "bias[j]")}` : ""} + ); + } + + ${$ ? "mean_data_output[global_idx] = mean" : ""}; + ${v ? "inv_std_output[global_idx] = inv_std_dev" : ""}; + }`; + }, T = [{ dims: d, dataType: e[0].dataType }]; + return $ && T.push({ dims: w, dataType: 1 }), v && T.push({ dims: w, dataType: 1 }), { name: "LayerNormalization", shaderCache: { hint: `${g};${r};${n}`, inputDependencies: y }, getRunData: () => ({ outputs: T, dispatchGroup: { x: Math.ceil(c / 64) }, programUniforms: S }), getShaderSource: x }; + }, Ud = (e, t) => { + Uf(e.inputs), e.compute(Vf(e.inputs, t, e.outputCount)); + }; + }); + var Nf, Wf, Lf, Nd, Wd, Ld = V(() => { + "use strict"; + Q(); + ie(); + Ie(); + ae(); + Nf = (e, t) => { + if (e.length < 3 || e.length > 4) throw new Error("MatMulNBits requires 3 or 4 inputs"); + let r = e[0], n = r.dims.length; + if (r.dims[n - 1] !== t.k) throw new Error("The last dim of input shape does not match the k value"); + let o = Math.floor((t.k + t.blockSize - 1) / t.blockSize), i = t.blockSize / 8 * t.bits, a = e[1]; + if (!k.areEqual(a.dims, [t.n, o, i])) throw new Error("The second inputs must be 3D tensor with shape N X nBlocksPerCol X blobSize"); + let l = e[2].dims; + if (k.size(l) !== t.n * o) throw new Error("scales input size error."); + if (e.length === 4) { + let m = e[3].dims, u = t.bits > 4 ? t.n * o : t.n * Math.floor((o + 1) / 2); + if (k.size(m) !== u) throw new Error("zeroPoints input size error."); + } + }, Wf = (e, t) => { + let r = e[0].dims, n = r.length, o = r[n - 2], i = t.k, a = t.n, d = r.slice(0, n - 2), l = k.size(d), m = e[1].dims[2] / 4, u = e[0].dataType, h = we(t.k), w = we(m), g = we(a), y = d.concat([o, a]), S = o > 1 && a / g % 2 === 0 ? 2 : 1, $ = k.size(y) / g / S, v = 64, x = [], T = [l, o, i / h], C = k.convertShape(e[1].dims).slice(); + C.splice(-1, 1, m / w), x.push(...R(T)), x.push(...R(C)), x.push(...R(e[2].dims)), e.length === 4 && x.push(...R(k.convertShape(e[3].dims))); + let A = [l, o, a / g]; + x.push(...R(A)); + let P = (B) => { + let N = T.length, W = E("a", e[0].dataType, N, h), K = E("b", 12, C.length, w), Z = E("scales", e[2].dataType, e[2].dims.length), ee = [W, K, Z], se = e.length === 4 ? E("zero_points", 12, e[3].dims.length) : void 0; + se && ee.push(se); + let de = A.length, Y = M("output", e[0].dataType, de, g), le = he(e[0].dataType), ce = (() => { + switch (h) { + case 1: + return `array<${le}, 8>`; + case 2: + return `mat4x2<${le}>`; + case 4: + return `mat2x4<${le}>`; + default: + throw new Error(`${h}-component is not supported.`); + } + })(), q = () => { + let ne = ` + // reuse a data + var input_offset = ${W.indicesToOffset(`${W.type.indices}(batch, row, word_offset)`)}; + var a_data: ${ce}; + for (var j: u32 = 0; j < ${8 / h}; j++) { + a_data[j] = ${W.getByOffset("input_offset")}; + input_offset++; + } + `; + for (let oe = 0; oe < g * S; oe++) ne += ` + b_value = ${w === 1 ? `b${oe}_data` : `b${oe}_data[i]`}; + b_value_lower = unpack4xU8(b_value & b_mask); + b_value_upper = unpack4xU8((b_value >> 4) & b_mask); + b_quantized_values = ${ce}(${Array.from({ length: 4 }, (U, G) => `${le}(b_value_lower[${G}]), ${le}(b_value_upper[${G}])`).join(", ")}); + b_dequantized_values = ${(() => h === 1 ? `${ce}(${Array.from({ length: 8 }, (U, G) => `(b_quantized_values[${G}] - ${se ? `zero_point${oe}` : "zero_point"}) * scale${oe}`).join(", ")});` : `(b_quantized_values - ${ce}(${Array(8).fill(`${se ? `zero_point${oe}` : "zero_point"}`).join(",")})) * scale${oe};`)()}; + workgroup_shared[local_id.x * ${S} + ${Math.floor(oe / g)}]${g > 1 ? `[${oe % g}]` : ""} += ${Array.from({ length: 8 / h }, (U, G) => `${h === 1 ? `a_data[${G}] * b_dequantized_values[${G}]` : `dot(a_data[${G}], b_dequantized_values[${G}])`}`).join(" + ")}; + `; + return ne; + }, ue = () => { + let ne = ` + var col_index = col * ${g}; + ${se ? ` + 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 = ${le}(8);`} + `; + for (let oe = 0; oe < g * S; oe++) ne += ` + let scale${oe} = ${Z.getByOffset("col_index * nBlocksPerCol + block")}; + ${se ? ` + zero_point_byte_count = col_index * zero_point_bytes_per_col + (block >> 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 = ${se.getByOffset("zero_point_word_index")} >> zero_point_bits_offset; + let zero_point${oe} = ${le}((zero_point_word) & 0xFu);` : ""} + col_index += 1;`; + return ne; + }, re = () => { + let ne = `col_index = col * ${g};`; + for (let oe = 0; oe < g * S; oe++) ne += ` + let b${oe}_data = ${K.getByIndices(`${K.type.indices}(col_index, block, word)`)}; + col_index += 1;`; + return ne += ` + var b_value: u32; + let b_mask: u32 = 0x0F0F0F0Fu; + var b_value_lower: vec4; + var b_value_upper: vec4; + var b_quantized_values: ${ce}; + var b_dequantized_values: ${ce};`, ne; + }; + return ` + var workgroup_shared: array<${Y.type.value}, ${S * v}>; + ${B.declareVariables(...ee, Y)} + ${B.mainStart([v, 1, 1])} + let output_indices = ${Y.offsetToIndices(`(global_idx / ${v}) * ${S}`)}; + let col = output_indices[2]; + let row = output_indices[1]; + let batch = output_indices[0]; + let nBlocksPerCol = uniforms.b_shape[1]; + + for (var block = local_id.x; block < nBlocksPerCol; block += ${v}) { + //process one block + var word_offset: u32 = block * ${t.blockSize / h}; + ${ue()} + for (var word: u32 = 0; word < ${m}; word += ${w}) { + ${re()} + for (var i: u32 = 0; i < ${w}; i++) { + ${q()} + word_offset += ${8 / h}; + } + } + } + workgroupBarrier(); + + if (local_id.x < ${S}) { + var output_value: ${Y.type.value} = ${Y.type.value}(0); + var workgroup_shared_offset: u32 = local_id.x; + for (var b: u32 = 0u; b < ${v}u; b++) { + output_value += workgroup_shared[workgroup_shared_offset]; + workgroup_shared_offset += ${S}; + } + ${Y.setByIndices(`${Y.type.indices}(batch, row, col + local_id.x)`, "output_value")}; + } + }`; + }; + return { name: "MatMulNBits", shaderCache: { hint: `${t.blockSize};${t.bits};${h};${w};${g};${S};${v}`, inputDependencies: Array(e.length).fill("rank") }, getRunData: () => ({ outputs: [{ dims: y, dataType: u }], dispatchGroup: { x: $ }, programUniforms: x }), getShaderSource: P }; + }, Lf = (e, t) => { + let r = e[0].dims, n = r.length, o = r[n - 2], i = t.k, a = t.n, d = r.slice(0, n - 2), l = k.size(d), m = e[1].dims[2] / 4, u = e[0].dataType, h = we(t.k), w = we(m), g = d.concat([o, a]), y = 128, S = a % 8 === 0 ? 8 : a % 4 === 0 ? 4 : 1, $ = y / S, v = $ * w * 8, x = v / h, T = v / t.blockSize, C = k.size(g) / S, A = [], P = [l, o, i / h], B = k.convertShape(e[1].dims).slice(); + B.splice(-1, 1, m / w), A.push(...R(P)), A.push(...R(B)), A.push(...R(e[2].dims)), e.length === 4 && A.push(...R(k.convertShape(e[3].dims))); + let N = [l, o, a]; + A.push(...R(N)); + let W = (K) => { + let Z = P.length, ee = E("a", e[0].dataType, Z, h), se = E("b", 12, B.length, w), de = E("scales", e[2].dataType, e[2].dims.length), Y = [ee, se, de], le = e.length === 4 ? E("zero_points", 12, e[3].dims.length) : void 0; + le && Y.push(le); + let ce = N.length, q = M("output", e[0].dataType, ce), ue = he(e[0].dataType), re = () => { + switch (h) { + case 1: + return ` + let a_data0 = vec4<${ue}>(sub_a[word_offset], sub_a[word_offset + 1], sub_a[word_offset + 2], sub_a[word_offset + 3]); + let a_data1 = vec4<${ue}>(sub_a[word_offset + 4], sub_a[word_offset + 5], sub_a[word_offset + 6], sub_a[word_offset + 7]);`; + case 2: + return ` + let a_data0 = vec4<${ue}>(sub_a[word_offset], sub_a[word_offset + 1]); + let a_data1 = vec4<${ue}>(sub_a[word_offset + 2], sub_a[word_offset + 3]);`; + case 4: + return ` + let a_data0 = sub_a[word_offset]; + let a_data1 = sub_a[word_offset + 1];`; + default: + throw new Error(`${h}-component is not supported.`); + } + }; + return ` + var sub_a: array<${ee.type.value}, ${x}>; + var inter_results: array, ${S}>; + ${K.declareVariables(...Y, q)} + ${K.mainStart([$, S, 1])} + let output_indices = ${q.offsetToIndices(`workgroup_index * ${S}`)}; + let col = output_indices[2]; + let row = output_indices[1]; + let batch = output_indices[0]; + let n_blocks_per_col = uniforms.b_shape[1]; + let num_tiles = (n_blocks_per_col - 1) / ${T} + 1; + + // Loop over shared dimension. + for (var tile: u32 = 0; tile < num_tiles; tile += 1) { + let a_col_start = tile * ${x}; + // load one tile A data into shared memory. + for (var a_offset = local_idx; a_offset < ${x}; a_offset += ${y}) + { + let a_col = a_col_start + a_offset; + if (a_col < uniforms.a_shape[2]) + { + sub_a[a_offset] = ${ee.getByIndices(`${ee.type.indices}(batch, row, a_col)`)}; + } else { + sub_a[a_offset] = ${ee.type.value}(0); + } + } + workgroupBarrier(); + + // each thread process one block + let b_row = col + local_id.y; + let block = tile * ${T} + local_id.x; + ${le ? ` + 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 = ${le.getByOffset("zero_point_word_index")} >> zero_point_bits_offset; + let zero_point = ${ue}((zero_point_word) & 0xFu);` : ` + // The default zero point is 8 for unsigned 4-bit quantization. + let zero_point = ${ue}(8);`} + let scale = ${de.getByOffset("b_row * n_blocks_per_col + block")}; + let b_data = ${se.getByIndices(`${se.type.indices}(b_row, block, 0)`)}; + var word_offset = local_id.x * ${t.blockSize / h}; + for (var i: u32 = 0; i < ${w}; i++) { + ${re()} + let b_value = ${w === 1 ? "b_data" : "b_data[i]"}; + let b_value_lower = unpack4xU8(b_value & 0x0F0F0F0Fu); + let b_value_upper = unpack4xU8((b_value >> 4) & 0x0F0F0F0Fu); + let b_quantized_values = mat2x4<${ue}>(${Array.from({ length: 4 }, (ne, oe) => `${ue}(b_value_lower[${oe}]), ${ue}(b_value_upper[${oe}])`).join(", ")}); + let b_dequantized_values = (b_quantized_values - mat2x4<${ue}>(${Array(8).fill("zero_point").join(",")})) * scale; + inter_results[local_id.y][local_id.x] += ${Array.from({ length: 2 }, (ne, oe) => `${`dot(a_data${oe}, b_dequantized_values[${oe}])`}`).join(" + ")}; + word_offset += ${8 / h}; + } + workgroupBarrier(); + } + + if (local_idx < ${S}) { + var output_value: ${q.type.value} = ${q.type.value}(0); + for (var b = 0u; b < ${$}; b++) { + output_value += inter_results[local_idx][b]; + } + if (col + local_idx < uniforms.output_shape[2]) + { + ${q.setByIndices(`${q.type.indices}(batch, row, col + local_idx)`, "output_value")} + } + } + }`; + }; + return { name: "BlockwiseMatMulNBits32", shaderCache: { hint: `${t.blockSize};${h};${w};${$};${S}`, inputDependencies: Array(e.length).fill("rank") }, getRunData: () => ({ outputs: [{ dims: g, dataType: u }], dispatchGroup: { x: C }, programUniforms: A }), getShaderSource: W }; + }, Nd = (e, t) => { + Nf(e.inputs, t), t.blockSize === 32 && e.adapterInfo.isVendor("intel") && e.adapterInfo.isArchitecture("gen-12lp") ? e.compute(Lf(e.inputs, t)) : e.compute(Wf(e.inputs, t)); + }, Wd = (e) => J(e); + }); + var Hf, Gf, Ff, qf, jf, Kf, Yf, Xf, Hd, Gd = V(() => { + "use strict"; + Q(); + ie(); + ae(); + Hf = (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]."); + } + }, Gf = (e, t, r) => { + let n = ""; + for (let o = t - 1; o >= 0; --o) n += ` + k = i32(${e.indicesGet("indices", o)}) - ${F("uniforms.pads", o, r)}; + if (k < 0) { + break; + } + if (k >= i32(${F("uniforms.x_shape", o, t)})) { + break; + } + offset += k * i32(${F("uniforms.x_strides", o, t)}); + `; + return ` + value = ${e.type.value}(uniforms.constant_value); + for (var i = 0; i < 1; i++) { + var offset = 0; + var k = 0; + ${n} + value = x[offset]; + } + `; + }, Ff = (e, t, r) => { + let n = ""; + for (let o = t - 1; o >= 0; --o) n += ` + k = i32(${e.indicesGet("indices", o)}) - ${F("uniforms.pads", o, r)}; + if (k < 0) { + k = -k; + } + { + let _2n_1 = 2 * (i32(${F("uniforms.x_shape", o, t)}) - 1); + k = k % _2n_1; + if(k >= i32(${F("uniforms.x_shape", o, t)})) { + k = _2n_1 - k; + } + } + offset += k * i32(${F("uniforms.x_strides", o, t)}); + `; + return ` + var offset = 0; + var k = 0; + ${n} + value = x[offset]; + `; + }, qf = (e, t, r) => { + let n = ""; + for (let o = t - 1; o >= 0; --o) n += ` + k = i32(${e.indicesGet("indices", o)}) - ${F("uniforms.pads", o, r)}; + if (k < 0) { + k = 0; + } + if (k >= i32(${F("uniforms.x_shape", o, t)})) { + k = i32(${F("uniforms.x_shape", o, t)}) - 1; + } + offset += k * i32(${F("uniforms.x_strides", o, t)}); + `; + return ` + var offset = 0; + var k = 0; + ${n} + value = x[offset]; + `; + }, jf = (e, t, r) => { + let n = ""; + for (let o = t - 1; o >= 0; --o) n += ` + k = i32(${e.indicesGet("indices", o)}) - ${F("uniforms.pads", o, r)}; + if (k < 0) { + k += i32(${F("uniforms.x_shape", o, t)}]); + } + if (k >= i32(${F("uniforms.x_shape", o, t)})) { + k -= i32(${F("uniforms.x_shape", o, t)}); + } + offset += k * i32(${F("uniforms.x_strides", o, t)}); + `; + return ` + var offset = 0; + var k = 0; + ${n} + value = x[offset]; + `; + }, Kf = (e, t, r) => { + switch (r.mode) { + case 0: + return Gf(e, t, r.pads.length); + case 1: + return Ff(e, t, r.pads.length); + case 2: + return qf(e, t, r.pads.length); + case 3: + return jf(e, t, r.pads.length); + default: + throw new Error("Invalid mode"); + } + }, Yf = (e, t) => { + let r = k.padShape(e[0].dims.slice(), t.pads), n = e[0].dims, o = k.size(r), i = [{ type: 12, data: o }, { type: 6, data: t.pads }], a = e.length >= 3 && e[2].data; + t.mode === 0 && i.push({ type: a ? e[2].dataType : 1, data: t.value }), i.push(...R(e[0].dims, r)); + let d = ["rank"], l = (c) => { + let m = M("output", e[0].dataType, r.length), u = E("x", e[0].dataType, n.length), h = u.type.value, w = Kf(m, n.length, t), g = [{ name: "output_size", type: "u32" }, { name: "pads", type: "i32", length: t.pads.length }]; + return t.mode === 0 && g.push({ name: "constant_value", type: a ? h : "f32" }), ` + ${c.registerUniforms(g).declareVariables(u, m)} + ${c.mainStart()} + ${c.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} + + let indices = ${m.offsetToIndices("global_idx")}; + + var value = ${h}(0); + ${w} + output[global_idx] = value; + }`; + }; + return { name: "Pad", shaderCache: { hint: `${t.mode}${a}`, inputDependencies: d }, getRunData: () => ({ outputs: [{ dims: r, dataType: e[0].dataType }], dispatchGroup: { x: Math.ceil(k.size(r) / 64) }, programUniforms: i }), getShaderSource: l }; + }, Xf = (e, t) => { + if (e.length > 1) { + let r = e[1].getBigInt64Array(), n = e.length >= 3 && e[2].data ? e[2].dataType === 10 ? e[2].getUint16Array()[0] : e[2].getFloat32Array()[0] : 0, o = e[0].dims.length, i = new Int32Array(2 * o).fill(0); + if (e.length >= 4) { + let d = e[3].getBigInt64Array(); + for (let l = 0; l < d.length; l++) i[Number(d[l])] = Number(r[l]), i[Number(d[l]) + o] = Number(r[l + d.length]); + } else r.forEach((d, l) => i[Number(l)] = Number(d)); + let a = []; + return i.forEach((d) => a.push(d)), { mode: t.mode, value: n, pads: a }; + } else return t; + }, Hd = (e, t) => { + Hf(e.inputs); + let r = Xf(e.inputs, t); + e.compute(Yf(e.inputs, r), { inputs: [0] }); + }; + }); + var nn, Fd, qd, jd, Kd, Zf, Qf, Yd, Xd, Zd, Qd, Jd, el, tl, rl, nl, ol, il, al, sl = V(() => { + "use strict"; + Ke(); + Q(); + ie(); + ae(); + nn = (e) => { + if (_e.webgpu.validateInputContent && (!e || e.length !== 1)) throw new Error("Pool ops requires 1 input."); + }, Fd = (e, t, r) => { + let n = t.format === "NHWC", o = e.dims.slice(); + n && o.splice(1, 0, o.pop()); + let i = Object.hasOwnProperty.call(t, "dilations"), a = t.kernelShape.slice(), d = t.strides.slice(), l = i ? t.dilations.slice() : [], c = t.pads.slice(); + Ct.adjustPoolAttributes(r, o, a, d, l, c); + let m = Ct.computePoolOutputShape(r, o, d, l, a, c, t.autoPad), u = Object.assign({}, t); + i ? Object.assign(u, { kernelShape: a, strides: d, pads: c, dilations: l, cacheKey: t.cacheKey }) : Object.assign(u, { kernelShape: a, strides: d, pads: c, cacheKey: t.cacheKey }); + let h = m.slice(); + return h.push(h.splice(1, 1)[0]), [u, n ? h : m]; + }, qd = (e, t) => { + let r = t.format === "NHWC", n = k.size(e), o = k.size(t.kernelShape), i = [{ type: 12, data: n }, { type: 12, data: o }], a = [{ name: "outputSize", type: "u32" }, { name: "kernelSize", type: "u32" }]; + if (t.kernelShape.length <= 2) { + let d = t.kernelShape[t.kernelShape.length - 1], l = t.strides[t.strides.length - 1], c = t.pads[t.pads.length / 2 - 1], m = t.pads[t.pads.length - 1], u = !!(c + m); + i.push({ type: 12, data: d }, { type: 12, data: l }, { type: 12, data: c }, { type: 12, data: m }), a.push({ name: "kw", type: "u32" }, { name: "sw", type: "u32" }, { name: "pwStart", type: "u32" }, { name: "pwEnd", type: "u32" }); + let h = false; + if (t.kernelShape.length === 2) { + let w = t.kernelShape[t.kernelShape.length - 2], g = t.strides[t.strides.length - 2], y = t.pads[t.pads.length / 2 - 2], S = t.pads[t.pads.length - 2]; + h = !!(y + S), i.push({ type: 12, data: w }, { type: 12, data: g }, { type: 12, data: y }, { type: 12, data: S }), a.push({ name: "kh", type: "u32" }, { name: "sh", type: "u32" }, { name: "phStart", type: "u32" }, { name: "phEnd", type: "u32" }); + } + return [i, a, true, u, h]; + } else { + if (r) throw new Error("Pooling with kernelShape.length > 2 is not supported for NHWC format."); + let d = k.computeStrides(t.kernelShape); + i.push({ type: 12, data: d }, { type: 12, data: t.pads }, { type: 12, data: t.strides }), a.push({ name: "kernelStrides", type: "u32", length: d.length }, { name: "pads", type: "u32", length: t.pads.length }, { name: "strides", type: "u32", length: t.strides.length }); + let l = t.pads.reduce((c, m) => c + m); + return [i, a, !!l, false, false]; + } + }, jd = (e, t, r, n, o, i, a, d, l, c, m, u) => { + let h = o.format === "NHWC", w = t.type.value, g = M("output", t.type.tensor, n); + if (o.kernelShape.length <= 2) { + let y = "", S = "", $ = "", v = r - (h ? 2 : 1); + if (m ? y = ` + for (var i: u32 = 0u; i < uniforms.kw; i++) { + xIndices[${v}] = indices[${v}] * uniforms.sw - uniforms.pwStart + i; + if (xIndices[${v}] < 0 || xIndices[${v}] + >= uniforms.x_shape[${v}]) { + pad++; + continue; + } + let x_val = x[${t.indicesToOffset("xIndices")}]; + ${i} + }` : y = ` + for (var i: u32 = 0u; i < uniforms.kw; i++) { + xIndices[${v}] = indices[${v}] * uniforms.sw - uniforms.pwStart + i; + let x_val = x[${t.indicesToOffset("xIndices")}]; + ${i} + }`, o.kernelShape.length === 2) { + let T = r - (h ? 3 : 2); + u ? S = ` + for (var j: u32 = 0u; j < uniforms.kh; j++) { + xIndices[${T}] = indices[${T}] * uniforms.sh - uniforms.phStart + j; + if (xIndices[${T}] < 0 || xIndices[${T}] >= uniforms.x_shape[${T}]) { + pad += i32(uniforms.kw); + continue; + } + ` : S = ` + for (var j: u32 = 0u; j < uniforms.kh; j++) { + xIndices[${T}] = indices[${T}] * uniforms.sh - uniforms.phStart + j; + `, $ = ` + } + `; + } + return ` + ${e.registerUniforms(l).declareVariables(t, g)} + + ${e.mainStart()} + ${e.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.outputSize")} + + let indices = ${g.offsetToIndices("global_idx")}; + var xIndices = ${g.offsetToIndices("global_idx")}; + + var value = ${w}(${d}); + var pad = 0; + ${S} + ${y} + ${$} + ${a} + + output[global_idx] = value; + }`; + } else { + if (h) throw new Error("Pooling with kernelShape.length > 2 is not supported for NHWC format."); + let y = o.kernelShape.length, S = o.pads.length, $ = ""; + return c ? $ = ` + if (xIndices[j] >= uniforms.x_shape[j]) { + pad++; + isPad = true; + break; + } + } + if (!isPad) { + let x_val = x[${t.indicesToOffset("xIndices")}]; + ${i} + }` : $ = ` + } + let x_val = x[${t.indicesToOffset("xIndices")}]; + ${i} + `, ` + ${e.registerUniforms(l).declareVariables(t, g)} + + ${e.mainStart()} + ${e.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.outputSize")} + let indices = ${g.offsetToIndices("global_idx")}; + var xIndices = ${g.offsetToIndices("global_idx")}; + + var offsets: array; + + var value = ${w}(${d}); + var pad = 0; + var isPad = false; + + for (var i: u32 = 0u; i < uniforms.kernelSize; i++) { + var offset = i; + for (var j = 0u; j < ${y - 1}u; j++) { + offsets[j] = offset / ${F("uniforms.kernelStrides", "j", y)}; + offset -= offsets[j] * ${F("uniforms.kernelStrides", "j", y)}; + } + offsets[${y - 1}] = offset; + + isPad = false; + for (var j = ${r - y}u; j < ${r}u; j++) { + xIndices[j] = indices[j] * ${F("uniforms.strides", `j - ${r - y}u`, y)} + + offsets[j - ${r - y}u] - ${F("uniforms.pads", "j - 2u", S)}; + ${$} + } + ${a} + + output[global_idx] = value; + }`; + } + }, Kd = (e) => `${e.format};${e.ceilMode};${e.autoPad};${e.kernelShape.length}`, Zf = (e) => `${Kd(e)};${e.countIncludePad}`, Qf = (e) => `${Kd(e)};${e.storageOrder};${e.dilations}`, Yd = (e) => ({ format: e.format, autoPad: ["NOTSET", "VALID", "SAME_UPPER", "SAME_LOWER"][e.auto_pad], ceilMode: e.ceil_mode, kernelShape: e.kernel_shape, strides: e.strides, pads: e.pads }), Xd = (e, t, r, n) => { + let [o, i] = Fd(t, n, r), a = E("x", t.dataType, t.dims.length), d = a.type.value, l = "value += x_val;", c = ""; + o.countIncludePad ? c += `value /= ${d}(uniforms.kernelSize);` : c += `value /= ${d}(i32(uniforms.kernelSize) - pad);`; + let [m, u, h, w, g] = qd(i, o); + m.push(...R(t.dims, i)); + let y = ["rank"]; + return { name: e, shaderCache: { hint: `${n.cacheKey};${h};${w};${g}`, inputDependencies: y }, getRunData: () => ({ outputs: [{ dims: i, dataType: t.dataType }], dispatchGroup: { x: Math.ceil(k.size(i) / 64) }, programUniforms: m }), getShaderSource: (S) => jd(S, a, t.dims.length, i.length, o, l, c, 0, u, h, w, g) }; + }, Zd = (e) => { + let t = e.count_include_pad !== 0, r = Yd(e); + if (r.ceilMode !== 0) throw new Error("using ceil() in shape computation is not yet supported for AveragePool"); + let n = { countIncludePad: t, ...r, cacheKey: "" }; + return { ...n, cacheKey: Zf(n) }; + }, Qd = (e, t) => { + nn(e.inputs), e.compute(Xd("AveragePool", e.inputs[0], false, t)); + }, Jd = { autoPad: "", ceilMode: 0, countIncludePad: false, kernelShape: [], strides: [], pads: [], storageOrder: 0, dilations: [] }, el = (e) => { + let t = e.format; + return { format: t, ...Jd, cacheKey: t }; + }, tl = (e, t) => { + nn(e.inputs), e.compute(Xd("GlobalAveragePool", e.inputs[0], true, t)); + }, rl = (e, t, r, n) => { + let [o, i] = Fd(t, n, r), a = ` + value = max(x_val, value); + `, d = "", l = E("x", t.dataType, t.dims.length), c = ["rank"], [m, u, h, w, g] = qd(i, o); + return m.push(...R(t.dims, i)), { name: e, shaderCache: { hint: `${n.cacheKey};${h};${w};${g}`, inputDependencies: c }, getRunData: () => ({ outputs: [{ dims: i, dataType: t.dataType }], dispatchGroup: { x: Math.ceil(k.size(i) / 64) }, programUniforms: m }), getShaderSource: (y) => jd(y, l, t.dims.length, i.length, o, a, d, t.dataType === 10 ? -65504 : -1e5, u, h, w, g) }; + }, nl = (e, t) => { + nn(e.inputs), e.compute(rl("MaxPool", e.inputs[0], false, t)); + }, ol = (e) => { + let t = e.storage_order, r = e.dilations, n = Yd(e); + if (t !== 0) throw new Error("column major storage order is not yet supported for MaxPool"); + if (n.ceilMode !== 0) throw new Error("using ceil() in shape computation is not yet supported for MaxPool"); + let o = { storageOrder: t, dilations: r, ...n, cacheKey: "" }; + return { ...o, cacheKey: Qf(o) }; + }, il = (e) => { + let t = e.format; + return { format: t, ...Jd, cacheKey: t }; + }, al = (e, t) => { + nn(e.inputs), e.compute(rl("GlobalMaxPool", e.inputs[0], true, t)); + }; + }); + var eh, th, ul, dl, ll = V(() => { + "use strict"; + Q(); + ie(); + Ie(); + ae(); + eh = (e, t) => { + if (e.length < 2 || e.length > 3) throw new Error("DequantizeLinear requires 2 or 3 inputs."); + if (e.length === 3 && e[1].dims === e[2].dims) throw new Error("x-scale and x-zero-point must have the same shape."); + if (e.length === 3 && e[0].dataType !== e[2].dataType) throw new Error("x and x-zero-point must have the same data type."); + if (e[0].dataType === 6 && e.length > 2) throw new Error("In the case of dequantizing int32 there is no zero point."); + if (e[1].dims.length !== 0 && e[1].dims.length !== 1 && e[1].dims.length !== e[0].dims.length) throw new Error("scale input must be a scalar, a 1D tensor, or have the same rank as the input tensor."); + if (e.length > 2) { + if (e[0].dataType !== e[2].dataType) throw new Error("x and x-zero-point must have the same data type."); + if (e[1].dims.length !== e[2].dims.length) throw new Error("scale and zero-point inputs must have the same rank."); + if (!e[1].dims.map((r, n) => r === e[2].dims[n]).reduce((r, n) => r && n, true)) 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, true)) throw new Error("For block qunatization, scale input shape to match the input shape except for the axis"); + if (e[1].dims.length !== e[0].dims.length) throw new Error("For block qunatization the scale input rank must be the same as the x rank."); + let r = e[0].dims[t.axis], n = e[1].dims[t.axis]; + if (t.blockSize < Math.ceil(r / n) || t.blockSize > Math.ceil(r / (n - 1) - 1)) throw new Error("blockSize must be with in the range [ceil(dI / Si), ceil(dI / (Si - 1) - 1)]."); + } + }, th = (e, t) => { + let r = k.normalizeAxis(t.axis, e[0].dims.length), n = e[0].dataType, o = n === 3, i = e[0].dims, a = e[1].dataType, d = k.size(i), l = n === 3 || n === 2, c = l ? [Math.ceil(k.size(e[0].dims) / 4)] : e[0].dims, m = e[1].dims, u = e.length > 2 ? e[2] : void 0, h = u ? l ? [Math.ceil(k.size(u.dims) / 4)] : u.dims : void 0, w = m.length === 0 || m.length === 1 && m[0] === 1, g = w === false && m.length === 1, y = we(d), S = w && (!l || y === 4), $ = S ? y : 1, v = S && !l ? y : 1, x = E("input", l ? 12 : n, c.length, v), T = E("scale", a, m.length), C = u ? E("zero_point", l ? 12 : n, h.length) : void 0, A = M("output", a, i.length, $), P = [x, T]; + C && P.push(C); + let B = [c, m]; + u && B.push(h); + let N = [{ type: 12, data: d / $ }, { type: 12, data: r }, { type: 12, data: t.blockSize }, ...R(...B, i)], W = (K) => { + let Z = [{ name: "output_size", type: "u32" }, { name: "axis", type: "u32" }, { name: "block_size", type: "u32" }]; + return ` + ${K.registerUniforms(Z).declareVariables(...P, A)} + ${K.mainStart()} + ${K.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} + let output_indices = ${A.offsetToIndices("global_idx")}; + + // Set input x + ${(() => l ? ` + let input = ${x.getByOffset("global_idx / 4")}; + let x_vec = ${o ? "unpack4xI8(input)" : "unpack4xU8(input)"}; + let x_value = ${$ === 1 ? "x_vec[global_idx % 4]" : "x_vec"};` : `let x_value = ${x.getByOffset("global_idx")};`)()}; + + // Set scale input + ${(() => w ? `let scale_value= ${T.getByOffset("0")}` : g ? ` + let scale_index = ${A.indicesGet("output_indices", "uniforms.axis")}; + let scale_value= ${T.getByOffset("scale_index")};` : ` + var scale_indices: ${T.type.indices} = output_indices; + let index = ${T.indicesGet("scale_indices", "uniforms.axis")} / uniforms.block_size; + ${T.indicesSet("scale_indices", "uniforms.axis", "index")}; + let scale_value= ${T.getByIndices("scale_indices")};`)()}; + + // Set zero-point input + ${(() => C ? w ? l ? ` + let zero_point_input = ${C.getByOffset("0")}; + let zero_point_vec = ${o ? "unpack4xI8(zero_point_input)" : "unpack4xU8(zero_point_input)"}; + let zero_point_value= zero_point_vec[0]` : `let zero_point_value = ${C.getByOffset("0")}` : g ? l ? ` + let zero_point_index = ${A.indicesGet("output_indices", "uniforms.axis")}; + let zero_point_input = ${C.getByOffset("zero_point_index / 4")}; + let zero_point_vec = ${o ? "unpack4xI8(zero_point_input)" : "unpack4xU8(zero_point_input)"}; + let zero_point_value = zero_point_vec[zero_point_index % 4]` : ` + let zero_point_index = ${A.indicesGet("output_indices", "uniforms.axis")}; + let zero_point_value = ${C.getByOffset("zero_point_index")};` : l ? ` + let zero_point_offset = ${T.indicesToOffset("scale_indices")}; + let zero_point_input = ${C.getByOffset("zero_point_offset / 4")}; + let zero_point_vec = ${o ? "unpack4xI8(zero_point_input)" : "unpack4xU8(zero_point_input)"}; + let zero_point_value = zero_point_vec[zero_point_offset % 4];` : `let zero_point_value = ${C.getByIndices("scale_indices")};` : `let zero_point_value = ${l ? o ? "i32" : "u32" : x.type.value}(0);`)()}; + // Compute and write output + ${A.setByOffset("global_idx", `${A.type.value}(x_value - zero_point_value) * scale_value`)}; + }`; + }; + return { name: "DequantizeLinear", shaderCache: { hint: t.cacheKey, inputDependencies: C ? ["rank", "rank", "rank"] : ["rank", "rank"] }, getShaderSource: W, getRunData: () => ({ outputs: [{ dims: i, dataType: a }], dispatchGroup: { x: Math.ceil(d / $ / 64), y: 1, z: 1 }, programUniforms: N }) }; + }, ul = (e, t) => { + eh(e.inputs, t), e.compute(th(e.inputs, t)); + }, dl = (e) => J({ axis: e.axis, blockSize: e.blockSize }); + }); + var rh, nh, cl, pl = V(() => { + "use strict"; + Ke(); + Q(); + ae(); + rh = (e, t, r) => { + let n = e === t, o = e < t && r < 0, i = e > t && r > 0; + if (n || o || i) throw new Error("Range these inputs' contents are invalid."); + }, nh = (e, t, r, n) => { + let o = Math.abs(Math.ceil((t - e) / r)), i = [o], a = o, d = [{ type: 12, data: a }, { type: n, data: e }, { type: n, data: r }, ...R(i)], l = (c) => { + let m = M("output", n, i.length), u = m.type.value, h = [{ name: "outputSize", type: "u32" }, { name: "start", type: u }, { name: "delta", type: u }]; + return ` + ${c.registerUniforms(h).declareVariables(m)} + ${c.mainStart()} + ${c.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.outputSize")} + output[global_idx] = uniforms.start + ${u}(global_idx) * uniforms.delta; + }`; + }; + return { name: "Range", shaderCache: { hint: `${n}` }, getShaderSource: l, getRunData: () => ({ outputs: [{ dims: i, dataType: n }], dispatchGroup: { x: Math.ceil(a / 64) }, programUniforms: d }) }; + }, cl = (e) => { + let t = 0, r = 0, n = 0; + e.inputs[0].dataType === 6 ? (t = e.inputs[0].getInt32Array()[0], r = e.inputs[1].getInt32Array()[0], n = e.inputs[2].getInt32Array()[0]) : e.inputs[0].dataType === 1 && (t = e.inputs[0].getFloat32Array()[0], r = e.inputs[1].getFloat32Array()[0], n = e.inputs[2].getFloat32Array()[0]), _e.webgpu.validateInputContent && rh(t, r, n), e.compute(nh(t, r, n, e.inputs[0].dataType), { inputs: [] }); + }; + }); + var oh, ih, ah, sh, uh, dh, lh, ch, ph, mh, fh, ml, hh, gh, yh, bh, wh, fl, hl, gl = V(() => { + "use strict"; + Q(); + ie(); + Ie(); + ae(); + oh = (e, t) => { + if (e.every((r) => r > 0 || (() => { + throw new Error("Resize requires scales input values to be positive"); + })), e.length > 0) { + if (t.mode === "linear") { + if (!(e.length === 2 || e.length === 3 || e.length === 4 && e[0] === 1 && e[1] === 1 || e.length === 4 && e[0] === 1 && e[3] === 1 || e.length === 5 && e[0] === 1 && e[1] === 1)) throw new Error(`For linear mode, Resize requires scales to be 2D, 3D, 4D with either two outermost or one innermost and + one outermost scale values equal to 1, or 5D with two outermost scale values equal to 1`); + } else if (t.mode === "cubic" && !(e.length === 2 || e.length === 4 && e[0] === 1 && e[1] === 1 || e.length === 4 && e[0] === 1 && e[3] === 1)) throw new Error("Resize requires scales input size to be 2 or 4 for cubic mode"); + } + }, ih = (e, t, r) => { + t.every((o) => o >= 0 && o < r || (() => { + throw new Error("Resize requires axes input values to be positive and less than rank"); + })); + let n = new Array(r).fill(1); + return t.forEach((o, i) => n[o] = e[i]), n; + }, ah = (e, t, r, n, o, i) => { + let [a, d, l] = r > 10 ? [1, 2, 3] : [-1, e.length > 1 ? 1 : -1, -1], c = e[0].dims.length; + if (a > 0 && e.length > a && e[a].dims.length > 0) e[a].getFloat32Array().forEach((m) => i.push(m)); + else if (t.coordinateTransformMode === "tf_crop_and_resize") throw new Error("Resize requires RoI input to be specified when coordinateTransformMode is tfCropAndResize"); + if (d > 0 && e.length > d && e[d].dims.length === 1 && e[d].dims[0] > 0) { + if (e[d].getFloat32Array().forEach((m) => n.push(m)), n.length !== 0 && n.length !== c && r >= 18 && n.length !== t.axes.length) throw new Error("Resize requires scales input size to be same as input rank or axes size for opset 18 and up"); + oh(n, t), t.axes.length > 0 && ih(n, t.axes, c).forEach((m, u) => n[u] = m); + } + if (l > 0 && e.length > l && e[l].dims.length === 1 && e[l].dims[0] > 0 && (e[l].getBigInt64Array().forEach((m) => o.push(Number(m))), o.length !== 0 && o.length !== c && r >= 18 && o.length !== t.axes.length)) throw new Error("Resize requires sizes input size to be same as input rank or axes size for opset 18 and up"); + if (t.axes.length > 0) { + if (n.length !== 0 && n.length !== t.axes.length) throw new Error('Resize requires "scales" input size to be of axes rank when axes attributes is specified'); + if (o.length !== 0 && o.length !== t.axes.length) throw new Error('Resize requires "sizes" input size to be of rank axes rank when axes attributes is specified'); + } + if (typeof n < "u" && typeof o < "u" && n.length > 0 && o.length > c) throw new Error("Resize requires only of scales or sizes to be specified"); + }, sh = (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`); + } + })() + "}", uh = (e, t, r) => `fn getNearestPixelFromOriginal(xOriginal: ${r}, isDownSample: bool) -> ${r} {` + (() => { + switch (e) { + case "round_prefer_ceil": + return "if (fract(xOriginal) == 0.5) { return ceil(xOriginal); } else { return round(xOriginal); }"; + case "floor": + return "return floor(xOriginal);"; + case "ceil": + return "return ceil(xOriginal);"; + case "round_prefer_floor": + return "if (fract(xOriginal) == 0.5) { return floor(xOriginal); } else { return round(xOriginal); }"; + case "simple": + default: + if (t < 11) return "if (isDownSample) { return ceil(xOriginal); } else { return xOriginal; }"; + throw new Error(`Nearest mode ${e} is not supported`); + } + })() + "}", dh = (e, t, r) => { + let n = new Array(r).fill(0).concat(new Array(r).fill(1)), o = e.length === 0 ? n : e.slice(); + return t.length > 0 ? (t.forEach((i, a) => { + n[i] = o[a], n[a + r] = o[t.length + a]; + }), n) : o; + }, lh = (e, t, r, n) => { + let o = []; + if (r.length > 0) if (n.length > 0) { + if (e.forEach((i) => o.push(i)), Math.max(...n) > e.length) throw new Error("axes is out of bound"); + n.forEach((i, a) => o[i] = r[a]); + } else r.forEach((i) => o.push(i)); + else { + if (t.length === 0) throw new Error("Resize requires either scales or sizes."); + o = e.map((i, a) => Math.round(i * t[a])); + } + return o; + }, ch = (e, t, r) => { + let n = (() => { + switch (r.keepAspectRatioPolicy) { + case "not_larger": + return r.axes.length > 0 ? Math.min(...r.axes.map((i) => t[i]), Number.MAX_VALUE) : Math.min(...t, Number.MAX_VALUE); + case "not_smaller": + return r.axes.length > 0 ? Math.max(...r.axes.map((i) => t[i]), Number.MIN_VALUE) : Math.max(...t, Number.MIN_VALUE); + default: + throw new Error(`Keep aspect ratio policy ${r.keepAspectRatioPolicy} is not supported`); + } + })(); + t.fill(1, 0, t.length); + let o = e.slice(); + return r.axes.length > 0 ? (r.axes.forEach((i) => t[i] = n), r.axes.forEach((i) => o[i] = Math.round(e[i] * t[i]))) : (t.fill(n, 0, t.length), o.forEach((i, a) => o[a] = Math.round(i * t[a]))), o; + }, ph = (e, t, r, n, o) => ` + fn calculateOriginalIndicesFromOutputIndices(output_indices: ${e.type.indices}) -> array<${e.type.value}, ${r.length}> { + var original_indices: array<${e.type.value}, ${r.length}>; + for (var i:u32 = 0; i < ${r.length}; i++) { + var output_index = ${e.indicesGet("output_indices", "i")}; + var scale = ${F("uniforms.scales", "i", n)}; + var roi_low = ${F("uniforms.roi", "i", o)}; + var roi_hi = ${F("uniforms.roi", `i + ${t.length}`, o)}; + if (scale == 1.0) { + original_indices[i] = ${e.type.value}(output_index); + } else { + var input_shape_i = ${F("uniforms.input_shape", "i", t.length)}; + var output_shape_i = ${F("uniforms.output_shape", "i", r.length)}; + original_indices[i] = getOriginalCoordinateFromResizedCoordinate(output_index, scale, output_shape_i, + input_shape_i, roi_low, roi_hi); + } + } + return original_indices; + }`, mh = (e, t, r, n, o, i, a) => ` + fn calculateInputIndicesFromOutputIndices(output_indices: ${t.type.indices}) -> ${e.type.indices} { + var input_indices: ${e.type.indices}; + for (var i:u32 = 0; i < ${n.length}; i++) { + var output_index = ${t.indicesGet("output_indices", "i")}; + var input_index: u32; + var scale = ${F("uniforms.scales", "i", o)}; + if (scale == 1.0) { + input_index = output_index; + } else { + var roi_low = ${F("uniforms.roi", "i", i)}; + var roi_hi = ${F("uniforms.roi", `i + ${r.length}`, i)}; + var input_shape_i = ${F("uniforms.input_shape", "i", r.length)}; + var output_shape_i = ${F("uniforms.output_shape", "i", n.length)}; + var original_idx = getOriginalCoordinateFromResizedCoordinate(output_index, scale, output_shape_i, + input_shape_i, roi_low, roi_hi); + if (!${a} || (original_idx >= 0 && original_idx < ${t.type.value}(input_shape_i))) { + if (original_idx < 0) { + input_index = 0; + } else if (original_idx > ${t.type.value}(input_shape_i - 1)) { + input_index = input_shape_i - 1; + } else { + input_index = u32(getNearestPixelFromOriginal(original_idx, scale < 1)); + } + } else { + input_index = u32(original_idx); + } + } + ${e.indicesSet("input_indices", "i", " input_index")} + } + return input_indices; + }`, fh = (e, t) => ` + fn checkInputIndices(input_indices: ${e.type.indices}) -> bool { + for (var i:u32 = 0; i < ${t.length}; i++) { + var input_index = ${e.indicesGet("input_indices", "i")}; + if (input_index < 0 || input_index >= ${F("uniforms.input_shape", "i", t.length)}) { + return false; + } + } + return true; + }`, ml = (e, t, r, n) => e.rank > n ? ` + ${e.indicesSet("input_indices", t, "channel")}; + ${e.indicesSet("input_indices", r, "batch")}; + ` : "", hh = (e, t, r, n, o) => { + let [a, d, l, c] = r.length === 2 ? [-1, 0, 1, -1] : [0, 2, 3, 1], m = e.type.value; + return ` + fn getInputValue(batch: u32, channel: u32, row: u32, col: u32) -> ${m} { + var input_indices: ${e.type.indices}; + ${e.indicesSet("input_indices", d, `max(0, min(row, ${r[d]} - 1))`)}; + ${e.indicesSet("input_indices", l, `max(0, min(col, ${r[l]} - 1))`)}; + ${ml(e, c, a, 2)} + return ${e.getByIndices("input_indices")}; + } + + fn bilinearInterpolation(output_indices: ${t.type.indices}) -> ${m} { + var originalIndices = calculateOriginalIndicesFromOutputIndices(output_indices); + var row:${m} = originalIndices[${d}]; + var col:${m} = originalIndices[${l}]; + ${n ? `if (row < 0 || row > (${r[d]} - 1) || col < 0 || col > (${r[l]} - 1)) { + return ${o}; + }` : ""}; + row = max(0, min(row, ${r[d]} - 1)); + col = max(0, min(col, ${r[l]} - 1)); + var row1: u32 = u32(row); + var col1: u32 = u32(col); + var row2: u32 = u32(row + 1); + var col2: u32 = u32(col + 1); + var channel: u32 = ${r.length > 2 ? `u32(originalIndices[${c}])` : "0"}; + var batch: u32 = ${r.length > 2 ? `u32(originalIndices[${a}])` : "0"}; + var x11: ${m} = getInputValue(batch, channel, row1, col1); + var x12: ${m} = getInputValue(batch, channel, row1, col2); + var x21: ${m} = getInputValue(batch, channel, row2, col1); + var x22: ${m} = getInputValue(batch, channel, row2, col2); + var dx1: ${m} = abs(row - ${m}(row1)); + var dx2: ${m} = abs(${m}(row2) - row); + var dy1: ${m} = abs(col - ${m}(col1)); + var dy2: ${m} = abs(${m}(col2) - col); + if (row1 == row2) { + dx1 = 0.5; + dx2 = 0.5; + } + if (col1 == col2) { + dy1 = 0.5; + dy2 = 0.5; + } + return (x11 * dx2 * dy2 + x12 * dx2 * dy1 + x21 * dx1 * dy2 + x22 * dx1 * dy1); + }`; + }, gh = (e, t, r, n, o, i, a, d, l, c) => { + let m = r.length === 2, u = true, [h, w] = m ? [0, 1] : u ? [2, 3] : [1, 2], g = e.type.value, y = (S) => { + let $ = S === h ? "row" : "col"; + return ` + fn ${$}CubicInterpolation(input_indices: ${e.type.indices}, output_indices: ${t.type.indices}) -> ${g} { + var output_index = ${t.indicesGet("output_indices", S)}; + var originalIdx: ${g} = getOriginalCoordinateFromResizedCoordinate(output_index, ${o[S]}, + ${n[S]}, ${r[S]}, ${i[S]}, ${i[S]} + ${r.length}); + var fractOriginalIdx: ${g} = originalIdx - floor(originalIdx); + var coefs = getCubicInterpolationCoefs(fractOriginalIdx); + + if (${d} && (originalIdx < 0 || originalIdx > (${r[S]} - 1))) { + return ${l}; + } + var data: array<${g}, 4> = array<${g}, 4>(0.0, 0.0, 0.0, 0.0); + for (var i: i32 = -1; i < 3; i++) { + var ${$}: ${g} = originalIdx + ${g}(i); + if (${$} < 0 || ${$} >= ${r[S]}) { + ${(() => c ? `coefs[i + 1] = 0.0; + continue;` : d ? `return ${l};` : `${$} = max(0, min(${$}, ${r[S]} - 1));`)()}; + } + var input_indices_copy: ${e.type.indices} = input_indices; + ${e.indicesSet("input_indices_copy", S, `u32(${$})`)}; + data[i + 1] = ${S === h ? e.getByIndices("input_indices_copy") : "rowCubicInterpolation(input_indices_copy, output_indices)"}; + } + return cubicInterpolation1D(data, coefs); + }`; + }; + return ` + ${y(h)}; + ${y(w)}; + fn getCubicInterpolationCoefs(s: ${g}) -> array<${g}, 4> { + var absS = abs(s); + var coeffs: array<${g}, 4> = array<${g}, 4>(0.0, 0.0, 0.0, 0.0); + var oneMinusAbsS: ${g} = 1.0 - absS; + var twoMinusAbsS: ${g} = 2.0 - absS; + var onePlusAbsS: ${g} = 1.0 + absS; + coeffs[0] = ((${a} * onePlusAbsS - 5 * ${a}) * onePlusAbsS + 8 * ${a}) * onePlusAbsS - 4 * ${a}; + coeffs[1] = ((${a} + 2) * absS - (${a} + 3)) * absS * absS + 1; + coeffs[2] = ((${a} + 2) * oneMinusAbsS - (${a} + 3)) * oneMinusAbsS * oneMinusAbsS + 1; + coeffs[3] = ((${a} * twoMinusAbsS - 5 * ${a}) * twoMinusAbsS + 8 * ${a}) * twoMinusAbsS - 4 * ${a}; + return coeffs; + } + + fn cubicInterpolation1D(x: array<${g}, 4>, coefs: array<${g}, 4>) -> ${g} { + var coefsSum: ${g} = coefs[0] + coefs[1] + coefs[2] + coefs[3]; + return (x[0] * coefs[0] + x[1] * coefs[1]+ x[2] * coefs[2]+ x[3] * coefs[3]) / coefsSum; + } + + fn bicubicInterpolation(output_indices: ${t.type.indices}) -> ${g} { + var input_indices: ${e.type.indices} = output_indices; + return colCubicInterpolation(input_indices, output_indices); + } + `; + }, yh = (e, t, r, n, o) => { + let [a, d, l, c, m] = r.length === 3 ? [-1, 0, 1, 2, -1] : [0, 2, 3, 4, 1], u = e.type.value; + return ` + fn getInputValue(batch: u32, channel: u32, depth:u32, height: u32, width: u32) -> ${u} { + var input_indices: ${e.type.indices}; + ${e.indicesSet("input_indices", d, `max(0, min(depth, ${r[d]} - 1))`)}; + ${e.indicesSet("input_indices", l, `max(0, min(height, ${r[l]} - 1))`)}; + ${e.indicesSet("input_indices", c, `max(0, min(width, ${r[c]} - 1))`)}; + ${ml(e, m, a, 3)} + return ${e.getByIndices("input_indices")}; + } + + fn trilinearInterpolation(output_indices: ${t.type.indices}) -> ${u} { + var originalIndices = calculateOriginalIndicesFromOutputIndices(output_indices); + var depth:${u} = originalIndices[${d}]; + var height:${u} = originalIndices[${l}]; + var width:${u} = originalIndices[${c}]; + ${n ? `if (depth < 0 || depth > (${r[d]} - 1) || height < 0 || height > (${r[l]} - 1) || width < 0 || (width > ${r[c]} - 1)) { + return ${o}; + }` : ""}; + + depth = max(0, min(depth, ${r[d]} - 1)); + height = max(0, min(height, ${r[l]} - 1)); + width = max(0, min(width, ${r[c]} - 1)); + var depth1: u32 = u32(depth); + var height1: u32 = u32(height); + var width1: u32 = u32(width); + var depth2: u32 = u32(depth + 1); + var height2: u32 = u32(height + 1); + var width2: u32 = u32(width + 1); + var channel: u32 = ${r.length > 3 ? `u32(originalIndices[${m}])` : "0"}; + var batch: u32 = ${r.length > 3 ? `u32(originalIndices[${a}])` : "0"}; + + var x111: ${u} = getInputValue(batch, channel, depth1, height1, width1); + var x112: ${u} = getInputValue(batch, channel, depth1, height1, width2); + var x121: ${u} = getInputValue(batch, channel, depth1, height2, width1); + var x122: ${u} = getInputValue(batch, channel, depth1, height2, width2); + var x211: ${u} = getInputValue(batch, channel, depth2, height1, width1); + var x212: ${u} = getInputValue(batch, channel, depth2, height1, width2); + var x221: ${u} = getInputValue(batch, channel, depth2, height2, width1); + var x222: ${u} = getInputValue(batch, channel, depth2, height2, width2); + var dx1: ${u} = abs(depth - ${u}(depth1)); + var dx2: ${u} = abs(${u}(depth2) - depth); + var dy1: ${u} = abs(height - ${u}(height1)); + var dy2: ${u} = abs(${u}(height2) - height); + var dz1: ${u} = abs(width - ${u}(width1)); + var dz2: ${u} = abs(${u}(width2) - width); + if (depth1 == depth2) { + dx1 = 0.5; + dx2 = 0.5; + } + if (height1 == height2) { + dy1 = 0.5; + dy2 = 0.5; + } + if (width1 == width2) { + dz1 = 0.5; + dz2 = 0.5; + } + return (x111 * dx2 * dy2 * dz2 + x112 * dx2 * dy2 * dz1 + x121 * dx2 * dy1 *dz2 + x122 * dx2 * dy1 * dz1 + + x211 * dx1 * dy2 * dz2 + x212 * dx1 * dy2 * dz1 + x221 * dx1 * dy1 *dz2 + x222 * dx1 * dy1 * dz1); + }`; + }, bh = (e, t, r, n, o, i) => { + let a = e.dims, d = dh(i, t.axes, a.length), l = lh(a, n, o, t.axes), c = n.slice(); + n.length === 0 && (c = a.map((v, x) => v === 0 ? 1 : l[x] / v), t.keepAspectRatioPolicy !== "stretch" && (l = ch(a, c, t))); + let m = M("output", e.dataType, l.length), u = E("input", e.dataType, a.length), h = k.size(l), w = a.length === l.length && a.every((v, x) => v === l[x]), g = t.coordinateTransformMode === "tf_crop_and_resize", y = t.extrapolationValue, S = u.type.value, $ = (v) => ` + ${w ? "" : ` + ${sh(t.coordinateTransformMode, S)}; + ${(() => { + switch (t.mode) { + case "nearest": + return ` + ${fh(u, a)}; + ${uh(t.nearestMode, r, S)}; + ${mh(u, m, a, l, c.length, d.length, g)}; + `; + case "linear": + return ` + ${ph(m, a, l, c.length, d.length)}; + ${(() => { + if (a.length === 2 || a.length === 4) return `${hh(u, m, a, g, y)}`; + if (a.length === 3 || a.length === 5) return `${yh(u, m, a, g, y)}`; + throw Error("Linear mode only supports input dims 2, 3, 4 and 5 are supported in linear mode."); + })()}; + `; + case "cubic": + return ` + ${(() => { + if (a.length === 2 || a.length === 4) return `${gh(u, m, a, l, c, d, t.cubicCoeffA, g, t.extrapolationValue, t.excludeOutside)}`; + throw Error("Cubic mode only supports input dims 2 and 4 are supported in linear mode."); + })()}; + `; + default: + throw Error("Invalid resize mode"); + } + })()}; + `} + ${v.registerUniform("output_size", "u32").registerUniform("scales", "f32", c.length).registerUniform("roi", "f32", d.length).declareVariables(u, m)} + ${v.mainStart()} + ${v.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} + ${w ? "output[global_idx] = input[global_idx];" : ` + let output_indices = ${m.offsetToIndices("global_idx")}; + var input_indices: ${u.type.indices}; + ${(() => { + switch (t.mode) { + case "nearest": + return `input_indices = calculateInputIndicesFromOutputIndices(output_indices); + if (checkInputIndices(input_indices)) { + output[global_idx] = ${u.getByIndices("input_indices")}; + } else { + output[global_idx] = ${t.extrapolationValue}; + }`; + case "linear": + return `output[global_idx] = ${a.length === 2 || a.length === 4 ? "bilinearInterpolation" : "trilinearInterpolation"}(output_indices);`; + case "cubic": + return "output[global_idx] = bicubicInterpolation(output_indices);"; + default: + throw Error(`Unsupported resize mode: ${t.mode}`); + } + })()}; + `} + }`; + return { name: "Resize", shaderCache: { hint: `${t.cacheKey}|${r}|${c.length > 0 ? c : ""}|${o.length > 0 ? o : ""}|${d.length > 0 ? d : ""}|${w}|${a}`, inputDependencies: ["rank"] }, getShaderSource: $, getRunData: () => ({ outputs: [{ dims: l, dataType: e.dataType }], dispatchGroup: { x: Math.ceil(h / 64) }, programUniforms: [{ type: 12, data: h }, { type: 1, data: c }, { type: 1, data: d }, ...R(a, l)] }) }; + }, wh = (e) => { + let t = e.customDataBuffer; + return new Uint32Array(t, t.byteOffset, 1)[0]; + }, fl = (e, t) => { + let r = [], n = [], o = [], i = wh(e); + if (t.antialias !== 0) throw Error("Only default value (0) for Antialias attribute is supported"); + ah(e.inputs, t, i, r, n, o), e.compute(bh(e.inputs[0], t, i, r, n, o), { inputs: [0] }); + }, hl = (e) => { + let t = e.antialias, r = e.axes, n = e.coordinateTransformMode, o = e.cubicCoeffA, i = e.excludeOutside !== 0, a = e.extrapolationValue, d = e.keepAspectRatioPolicy, l = e.mode, c = e.nearestMode === "" ? "simple" : e.nearestMode; + return J({ antialias: t, axes: r, coordinateTransformMode: n, cubicCoeffA: o, excludeOutside: i, extrapolationValue: a, keepAspectRatioPolicy: d, mode: l, nearestMode: c }); + }; + }); + var _h, vh, yl, bl = V(() => { + "use strict"; + Q(); + ie(); + Ie(); + ae(); + _h = (e, t) => { + let [r, n, o, i] = e, { numHeads: a, rotaryEmbeddingDim: d } = t; + if (r.dims.length !== 3 && r.dims.length !== 4) throw new Error(`Input 'x' is expected to have 3 or 4 dimensions, got ${r.dims.length}`); + if (!k.areEqual(n.dims, []) && !k.areEqual(n.dims, [1]) && n.dims.length !== 2) throw new Error(`Input 'position_ids' is expected to have 0, 1, or 2 dimensions, got ${n.dims.length}`); + if (o.dims.length !== 2) throw new Error(`Input 'cos_cache' is expected to have 2 dimensions, got ${o.dims.length}`); + if (i.dims.length !== 2) throw new Error(`Input 'sin_cache' is expected to have 2 dimensions, got ${i.dims.length}`); + if (!k.areEqual(o.dims, i.dims)) throw new Error("Inputs 'cos_cache' and 'sin_cache' are expected to have the same shape"); + if (d > 0 && a === 0) throw new Error("num_heads must be provided if rotary_embedding_dim is specified"); + let l = r.dims[0], c = r.dims[r.dims.length - 2], m = o.dims[0], u = k.sizeFromDimension(r.dims, 1) / c, h = d === 0 ? o.dims[1] * 2 : u / a; + if (d > h) throw new Error("rotary_embedding_dim must be less than or equal to head_size"); + if (n.dims.length === 2) { + if (l !== n.dims[0]) throw new Error(`Input 'position_ids' dimension 0 should be of size batch_size, got ${n.dims[0]}`); + if (c !== n.dims[1]) throw new Error(`Input 'position_ids' dimension 1 should be of size sequence_length, got ${n.dims[1]}`); + } + if (h / 2 !== o.dims[1] && d / 2 !== o.dims[1]) throw new Error(`Input 'cos_cache' dimension 1 should be same as head_size / 2 or rotary_embedding_dim / 2, got ${o.dims[1]}`); + if (c > m) throw new Error("Updating cos_cache and sin_cache in RotaryEmbedding is not currently supported"); + }, vh = (e, t) => { + let { interleaved: r, numHeads: n, rotaryEmbeddingDim: o, scale: i } = t, a = e[0].dims[0], d = k.sizeFromDimension(e[0].dims, 1), l = e[0].dims[e[0].dims.length - 2], c = d / l, m = e[2].dims[1], u = o === 0 ? m * 2 : c / n, h = new Array(a, l, c / u, u - m), w = k.computeStrides(h), g = [{ type: 1, data: i }, { type: 12, data: h }, { type: 12, data: w }, ...e[0].dims.length === 3 ? new Array({ type: 12, data: [d, c, u, 1] }) : [], ...e[0].dims.length === 4 ? new Array({ type: 12, data: [d, u, l * u, 1] }) : [], ...R(e[0].dims, e[1].dims, e[2].dims, e[3].dims, e[0].dims)], y = (S) => { + let $ = E("input", e[0].dataType, e[0].dims.length), v = E("position_ids", e[1].dataType, e[1].dims.length), x = E("cos_cache", e[2].dataType, e[2].dims.length), T = E("sin_cache", e[3].dataType, e[3].dims.length), C = M("output", e[0].dataType, e[0].dims.length); + return S.registerUniforms([{ name: "scale", type: "f32" }, { name: "global_shape", type: "u32", length: h.length }, { name: "global_strides", type: "u32", length: w.length }, { name: "input_output_strides", type: "u32", length: w.length }]), ` + ${S.declareVariables($, v, x, T, C)} + + ${S.mainStart(At)} + let half_rotary_emb_dim = uniforms.${x.name}_shape[1]; + let bsnh = global_idx / uniforms.global_strides % uniforms.global_shape; + let size = uniforms.global_shape[0] * uniforms.global_strides[0]; + ${S.guardAgainstOutOfBoundsWorkgroupSizes("size")} + + if (bsnh[3] < half_rotary_emb_dim) { + let position_ids_idx = + ${v.broadcastedIndicesToOffset("bsnh.xy", M("", v.type.tensor, 2))}; + let position_id = + u32(${v.getByOffset("position_ids_idx")}) + select(0, bsnh[1], position_ids_idx == 0); + let i = dot(bsnh, uniforms.input_output_strides) + select(0, bsnh[3], ${r}); + let j = i + select(half_rotary_emb_dim, 1, ${r}); + let re = ${$.getByOffset("i")} * ${x.get("position_id", "bsnh[3]")} - + ${$.getByOffset("j")} * ${T.get("position_id", "bsnh[3]")}; + ${C.setByOffset("i", "re")} + let im = ${$.getByOffset("i")} * ${T.get("position_id", "bsnh[3]")} + + ${$.getByOffset("j")} * ${x.get("position_id", "bsnh[3]")}; + ${C.setByOffset("j", "im")} + } else { + let k = dot(bsnh, uniforms.input_output_strides) + half_rotary_emb_dim; + ${C.setByOffset("k", $.getByOffset("k"))} + } + }`; + }; + return { name: "RotaryEmbedding", shaderCache: { hint: J({ interleaved: r }).cacheKey, inputDependencies: ["rank", "rank", "rank", "rank"] }, getShaderSource: y, getRunData: () => ({ outputs: [{ dims: e[0].dims, dataType: e[0].dataType }], dispatchGroup: { x: Math.ceil(k.size(h) / At) }, programUniforms: g }) }; + }, yl = (e, t) => { + _h(e.inputs, t), e.compute(vh(e.inputs, t)); + }; + }); + var $h, xh, wl, _l = V(() => { + "use strict"; + Q(); + ie(); + ae(); + $h = (e) => { + if (!e || e.length < 3) throw new Error("layerNorm requires at least 3 inputs."); + let t = e[0], r = e[1], n = e[2]; + if (t.dataType !== r.dataType || t.dataType !== n.dataType) throw new Error("All inputs must have the same data type"); + if (t.dims.length !== 3 && t.dims.length !== 2) throw new Error("Input must be 2D or 3D"); + if (r.dims.length !== 3 && r.dims.length !== 2) throw new Error("Skip must be 2D or 3D"); + let o = t.dims[t.dims.length - 1], i = t.dims[t.dims.length - 2]; + if (r.dims[r.dims.length - 1] !== o) throw new Error("Skip must have the same hidden size as input"); + if (r.dims[r.dims.length - 2] !== i) throw new Error("Skip must have the same sequence length as input"); + if (n.dims.length !== 1) throw new Error("Gamma must be 1D"); + if (n.dims[n.dims.length - 1] !== o) throw new Error("Gamma must have the same hidden size as input"); + if (e.length > 3) { + let a = e[3]; + if (a.dims.length !== 1) throw new Error("Beta must be 1D"); + if (a.dims[a.dims.length - 1] !== o) throw new Error("Beta must have the same hidden size as input"); + } + if (e.length > 4) { + let a = e[4]; + if (a.dims.length !== 1) throw new Error("Bias must be 1D"); + if (a.dims[a.dims.length - 1] !== o) throw new Error("Bias must have the same hidden size as input"); + } + }, xh = (e, t, r, n) => { + let o = t.simplified, i = e[0].dims, a = k.size(i), d = i, l = a, c = i.slice(-1)[0], m = n ? i.slice(0, -1).concat(1) : [], u = !o && e.length > 3, h = e.length > 4, w = n && r > 1, g = n && r > 2, y = r > 3, S = 64, $ = we(c), v = [{ type: 12, data: l }, { type: 12, data: $ }, { type: 12, data: c }, { type: 1, data: t.epsilon }], x = (C) => { + let A = [{ name: "output_size", type: "u32" }, { name: "components", type: "u32" }, { name: "hidden_size", type: "u32" }, { name: "epsilon", type: "f32" }], P = [E("x", e[0].dataType, e[0].dims, $), E("skip", e[1].dataType, e[1].dims, $), E("gamma", e[2].dataType, e[2].dims, $)]; + u && P.push(E("beta", e[3].dataType, e[3].dims, $)), h && P.push(E("bias", e[4].dataType, e[4].dims, $)), P.push(M("output", e[0].dataType, d, $)), w && P.push(M("mean_output", 1, m)), g && P.push(M("inv_std_output", 1, m)), y && P.push(M("input_skip_bias_sum", e[0].dataType, d, $)); + let B = he(e[0].dataType), N = he(1, $); + return ` + + ${C.registerUniforms(A).declareVariables(...P)} + var sum_shared : array<${N}, ${S}>; + var sum_squared_shared : array<${N}, ${S}>; + + ${C.mainStart([S, 1, 1])} + let ix = local_id.x; + let iy = global_id.x / ${S}; + + let hidden_size_vectorized: u32 = uniforms.hidden_size / uniforms.components; + var stride = hidden_size_vectorized / ${S}; + let offset = ix * stride + iy * hidden_size_vectorized; + let offset1d = stride * ix; + if (ix == ${S - 1}) { + stride = hidden_size_vectorized - stride * ix; + } + for (var i: u32 = 0; i < stride; i++) { + let skip_value = skip[offset + i]; + let bias_value = ${h ? "bias[offset1d + i]" : B + "(0.0)"}; + let input_value = x[offset + i]; + let value = input_value + skip_value + bias_value; + ${y ? "input_skip_bias_sum[offset + i] = value;" : ""} + output[offset + i] = value; + let f32_value = ${kt(B, $, "value")}; + sum_shared[ix] += f32_value; + sum_squared_shared[ix] += f32_value * f32_value; + } + workgroupBarrier(); + + var reduce_size : u32 = ${S}; + for (var curr_size = reduce_size >> 1; curr_size > 0; curr_size = reduce_size >> 1) { + reduce_size = curr_size + (reduce_size & 1); + if (ix < curr_size) { + sum_shared[ix] += sum_shared[ix + reduce_size]; + sum_squared_shared[ix] += sum_squared_shared[ix + reduce_size]; + } + workgroupBarrier(); + } + + let sum = sum_shared[0]; + let square_sum = sum_squared_shared[0]; + let mean = ${Ze("sum", $)} / f32(uniforms.hidden_size); + let inv_std_dev = inverseSqrt(${Ze("square_sum", $)} / f32(uniforms.hidden_size) ${o ? "" : "- mean * mean"} + uniforms.epsilon); + ${w ? "mean_output[global_idx] = mean;" : ""} + ${g ? "inv_std_output[global_idx] = inv_std_dev;" : ""} + + for (var i: u32 = 0; i < stride; i++) { + output[offset + i] = (output[offset + i] ${o ? "" : `- ${B}(mean)`}) * + ${B}(inv_std_dev) * gamma[offset1d + i] + ${u ? "+ beta[offset1d + i]" : ""}; + } + }`; + }, T = [{ dims: d, dataType: e[0].dataType }]; + return r > 1 && T.push({ dims: m, dataType: 1 }), r > 2 && T.push({ dims: m, dataType: 1 }), r > 3 && T.push({ dims: i, dataType: e[0].dataType }), { name: "SkipLayerNormalization", shaderCache: { hint: `${$};${w};${g};${y}`, inputDependencies: e.map((C, A) => "type") }, getShaderSource: x, getRunData: () => ({ outputs: T, dispatchGroup: { x: Math.ceil(l / c) }, programUniforms: v }) }; + }, wl = (e, t) => { + $h(e.inputs); + let n = [0]; + e.outputCount > 1 && n.push(-3), e.outputCount > 2 && n.push(-3), e.outputCount > 3 && n.push(3), e.compute(xh(e.inputs, t, e.outputCount, false), { outputs: n }); + }; + }); + var Sh, on, Th, vl, Ih, Ch, $l, xl, Sl = V(() => { + "use strict"; + Q(); + ie(); + Ie(); + ae(); + Sh = (e, t) => { + if (!e || e.length < 1) throw new Error("too few inputs"); + if (t.axes.length !== 0) { + if (t.axes.length !== t.starts.length || t.axes.length !== t.ends.length) throw new Error("axes, starts and ends must have the same length"); + } else if (t.starts.length !== t.ends.length) throw new Error("starts and ends must have the same length"); + e.slice(1).forEach((r, n) => { + if (e[n + 1].dataType !== 6 && e[n + 1].dataType !== 7) throw new Error(`Input ${n} must be an array of int32 or int64`); + }); + }, on = (e, t) => { + let r = []; + if (e.length > t) if (e[t].dataType === 7) e[t].getBigInt64Array().forEach((n) => r.push(Number(n))); + else if (e[t].dataType === 6) e[t].getInt32Array().forEach((n) => r.push(Number(n))); + else throw new Error(`Input ${t} must be an array of int32 or int64`); + return r; + }, Th = (e, t) => { + if (e.length > 1) { + let r = on(e, 1), n = on(e, 2), o = on(e, 3); + return o.length === 0 && (o = [...Array(e[0].dims.length).keys()]), J({ starts: r, ends: n, axes: o }); + } else return t; + }, vl = (e, t, r, n, o) => { + let i = e; + return e < 0 && (i += r[n[t]]), o[t] < 0 ? Math.max(0, Math.min(i, r[n[t]] - 1)) : Math.max(0, Math.min(i, r[n[t]])); + }, Ih = (e, t, r) => `fn calculateInputIndices(output_indices: ${t.type.indices}) -> ${e.type.indices} { + var input_indices: ${e.type.indices}; + var carry = 0u; + for (var i = ${r.length}; i >= 0; i--) { + let input_shape_i = ${F("uniforms.input_shape", "i", r.length)}; + let steps_i = ${F("uniforms.steps", "i", r.length)}; + let signs_i = ${F("uniforms.signs", "i", r.length)}; + let starts_i = ${F("uniforms.starts", "i", r.length)}; + var output_index = ${t.indicesGet("output_indices", "i")}; + var input_index = output_index * steps_i + starts_i + carry; + carry = input_index / input_shape_i; + input_index = input_index % input_shape_i; + if (signs_i < 0) { + input_index = input_shape_i - input_index - 1u + starts_i; + } + ${e.indicesSet("input_indices", "i", "input_index")}; + } + return input_indices; + }`, Ch = (e, t) => { + let r = e[0].dims, n = k.size(r), o = t.axes.length > 0 ? k.normalizeAxes(t.axes, r.length) : [...Array(r.length).keys()], i = on(e, 4); + i.forEach(($) => $ !== 0 || (() => { + throw new Error("step cannot be 0"); + })), i.length === 0 && (i = Array(o.length).fill(1)); + let a = t.starts.map(($, v) => vl($, v, r, o, i)), d = t.ends.map(($, v) => vl($, v, r, o, i)); + if (o.length !== a.length || o.length !== d.length) throw new Error("start, ends and axes should have the same number of elements"); + if (o.length !== r.length) for (let $ = 0; $ < r.length; ++$) o.includes($) || (a.splice($, 0, 0), d.splice($, 0, r[$]), i.splice($, 0, 1)); + let l = i.map(($) => Math.sign($)); + i.forEach(($, v, x) => { + if ($ < 0) { + let T = (d[v] - a[v]) / $, C = a[v], A = C + T * i[v]; + a[v] = A, d[v] = C, x[v] = -$; + } + }); + let c = r.slice(0); + o.forEach(($, v) => { + c[$] = Math.ceil((d[$] - a[$]) / i[$]); + }); + let m = { dims: c, dataType: e[0].dataType }, u = M("output", e[0].dataType, c.length), h = E("input", e[0].dataType, e[0].dims.length), w = k.size(c), g = [{ name: "outputSize", type: "u32" }, { name: "starts", type: "u32", length: a.length }, { name: "signs", type: "i32", length: l.length }, { name: "steps", type: "u32", length: i.length }], y = [{ type: 12, data: w }, { type: 12, data: a }, { type: 6, data: l }, { type: 12, data: i }, ...R(e[0].dims, c)], S = ($) => ` + ${$.registerUniforms(g).declareVariables(h, u)} + ${Ih(h, u, r)} + ${$.mainStart()} + ${$.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.outputSize")} + let output_indices = ${u.offsetToIndices("global_idx")}; + let input_indices = calculateInputIndices(output_indices); + ${u.setByOffset("global_idx", h.getByIndices("input_indices"))} + }`; + return { name: "Slice", shaderCache: { hint: `${l.length}_${a.length}_${i.length}`, inputDependencies: ["rank"] }, getShaderSource: S, getRunData: () => ({ outputs: [m], dispatchGroup: { x: Math.ceil(n / 64) }, programUniforms: y }) }; + }, $l = (e, t) => { + Sh(e.inputs, t); + let r = Th(e.inputs, t); + e.compute(Ch(e.inputs, r), { inputs: [0] }); + }, xl = (e) => { + let t = e.starts, r = e.ends, n = e.axes; + return J({ starts: t, ends: r, axes: n }); + }; + }); + var Ah, kh, Tl, Il, Cl = V(() => { + "use strict"; + Q(); + ie(); + Ie(); + lt(); + ae(); + Ah = (e) => { + if (!e || e.length !== 1) throw new Error("Softmax op requires 1 input."); + }, kh = (e, t) => { + let r = e.inputs[0], n = r.dims, o = k.size(n), i = 64, a = n.length, d = k.normalizeAxis(t.axis, a), l = d < n.length - 1, c, m = []; + l ? (m = Array.from({ length: a }, (P, B) => B), m[d] = a - 1, m[a - 1] = d, c = e.compute(Pe(r, m), { inputs: [r], outputs: [-1] })[0]) : c = r; + let u = c.dims, h = u[a - 1], w = o / h, g = we(h), y = h / g, S = (P, B) => B === 4 ? `max(max(${P}.x, ${P}.y), max(${P}.z, ${P}.w))` : B === 2 ? `max(${P}.x, ${P}.y)` : B === 3 ? `max(max(${P}.x, ${P}.y), ${P}.z)` : P, $ = E("x", c.dataType, c.dims, g), v = M("result", c.dataType, c.dims, g), x = $.type.value, T = he(c.dataType) === "f32" ? `var threadMax = ${x}(-3.402823e+38f);` : `var threadMax = ${x}(-65504.0h);`, C = (P) => ` + var rowMaxShared : ${x}; + var rowSumShared : ${x}; + var threadShared : array<${x}, ${i}>; + + fn getValue(row: i32, col: i32, row_stride: i32) -> ${x} { + let index = row * row_stride + col; + return x[index]; + } + + fn setValue(row: i32, col: i32, row_stride: i32, value: ${x}) { + let index = row * row_stride + col; + result[index] = value; + } + ${P.registerUniform("packedCols", "i32").declareVariables($, v)} + ${P.mainStart()} + let gindex = i32(global_idx); + let lindex = i32(local_idx); + const wg = ${i}; + let row = gindex / wg; + let cols = uniforms.packedCols; + let row_stride : i32 = uniforms.packedCols; + + // find the rows max + ${T} + for (var col = lindex; col < cols; col += wg) { + let value = getValue(row, col, row_stride); + threadMax = max(threadMax, value); + } + if (lindex < cols) { + threadShared[lindex] = threadMax; + } + workgroupBarrier(); + + var reduceSize = min(cols, wg); + for (var currSize = reduceSize >> 1; currSize > 0; currSize = reduceSize >> 1) { + reduceSize = currSize + (reduceSize & 1); + if (lindex < currSize) { + threadShared[lindex] = max(threadShared[lindex], threadShared[lindex + reduceSize]); + } + workgroupBarrier(); + } + if (lindex == 0) { + rowMaxShared = ${x}(${S("threadShared[0]", g)}); + } + workgroupBarrier(); + + // find the rows sum + var threadSum = ${x}(0.0); + for (var col = lindex; col < cols; col += wg) { + let subExp = exp(getValue(row, col, row_stride) - rowMaxShared); + threadSum += subExp; + } + threadShared[lindex] = threadSum; + workgroupBarrier(); + + for (var currSize = wg >> 1; currSize > 0; currSize = currSize >> 1) { + if (lindex < currSize) { + threadShared[lindex] = threadShared[lindex] + threadShared[lindex + currSize]; + } + workgroupBarrier(); + } + if (lindex == 0) { + rowSumShared = ${x}(${Ze("threadShared[0]", g)}); + } + workgroupBarrier(); + + // calculate final value for each element in the row + for (var col = lindex; col < cols; col += wg) { + let value = exp(getValue(row, col, row_stride) - rowMaxShared) / rowSumShared; + setValue(row, col, row_stride, value); + } + }`, A = e.compute({ name: "Softmax", shaderCache: { hint: `${g}`, inputDependencies: ["type"] }, getRunData: () => ({ outputs: [{ dims: u, dataType: c.dataType }], dispatchGroup: { x: w }, programUniforms: [{ type: 6, data: y }] }), getShaderSource: C }, { inputs: [c], outputs: [l ? -1 : 0] })[0]; + l && e.compute(Pe(A, m), { inputs: [A] }); + }, Tl = (e, t) => { + Ah(e.inputs), kh(e, t); + }, Il = (e) => J({ axis: e.axis }); + }); + var Eh, Ph, zh, Oh, Bh, Al, kl, El = V(() => { + "use strict"; + Q(); + ie(); + Ie(); + ae(); + Eh = (e) => { + if (!e || e.length < 1) throw new Error("too few inputs"); + }, Ph = (e, t) => { + let r = [], n = t.numOutputs; + return e[1].dims[0] > 0 && (e[1].getBigInt64Array().forEach((o) => r.push(Number(o))), n = r.length), J({ numOutputs: n, axis: t.axis, splitSizes: r }); + }, zh = (e) => ` + fn calculateOutputIndex(index: u32) -> u32 { + for (var i: u32 = 0u; i < ${e}u; i += 1u ) { + if (index < ${F("uniforms.size_in_split_axis", "i", e)}) { + return i; + } + } + return ${e}u; + }`, Oh = (e) => { + let t = e.length, r = []; + for (let n = 0; n < t; ++n) { + let o = e[n].setByIndices("indices", "input[global_idx]"); + t === 1 ? r.push(o) : n === 0 ? r.push(`if (output_number == ${n}u) { ${o} }`) : n === t - 1 ? r.push(`else { ${o} }`) : r.push(`else if (output_number == ${n}) { ${o} }`); + } + return ` + fn writeBufferData(output_number: u32, indices: ${e[0].type.indices}, global_idx: u32) { + ${r.join(` + `)} + }`; + }, Bh = (e, t) => { + let r = e[0].dims, n = k.size(r), o = e[0].dataType, i = k.normalizeAxis(t.axis, r.length), a = new Array(t.numOutputs), d = E("input", o, r.length), l = new Array(t.numOutputs), c = [], m = [], u = 0, h = [{ type: 12, data: n }]; + for (let g = 0; g < t.numOutputs; g++) { + u += t.splitSizes[g], l[g] = u; + let y = r.slice(); + y[i] = t.splitSizes[g], m.push(y), a[g] = M(`output${g}`, o, y.length), c.push({ dims: m[g], dataType: e[0].dataType }); + } + h.push({ type: 12, data: l }, ...R(r, ...m)); + let w = (g) => ` + ${g.registerUniform("input_size", "u32").registerUniform("size_in_split_axis", "u32", l.length).declareVariables(d, ...a)} + ${zh(l.length)} + ${Oh(a)} + + ${g.mainStart()} + ${g.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.input_size")} + + var indices = ${d.offsetToIndices("global_idx")}; + var index = ${d.indicesGet("indices", i)}; + let output_number = calculateOutputIndex(index); + if (output_number != 0) { + index -= ${F("uniforms.size_in_split_axis", "output_number - 1u", l.length)}; + ${d.indicesSet("indices", i, "index")}; + } + writeBufferData(output_number, indices, global_idx); + }`; + return { name: "Split", shaderCache: { hint: t.cacheKey, inputDependencies: ["rank"] }, getShaderSource: w, getRunData: () => ({ outputs: c, dispatchGroup: { x: Math.ceil(n / 64) }, programUniforms: h }) }; + }, Al = (e, t) => { + Eh(e.inputs); + let r = e.inputs.length === 1 ? t : Ph(e.inputs, t); + e.compute(Bh(e.inputs, r), { inputs: [0] }); + }, kl = (e) => { + let t = e.axis, r = e.splitSizes, n = e.numOutputs < 0 ? r.length : e.numOutputs; + if (n !== r.length) throw new Error("numOutputs and splitSizes lengh must be equal"); + return J({ axis: t, numOutputs: n, splitSizes: r }); + }; + }); + var Dh, Mh, Pl, zl = V(() => { + "use strict"; + Q(); + ie(); + ae(); + Dh = (e, t, r, n, o) => { + let i = M("output_data", o, r.length, 4), a = E("a_data", t[1].dataType, t[1].dims.length, 4), d = E("b_data", t[2].dataType, t[2].dims.length, 4), l = E("c_data", t[0].dataType, t[0].dims.length, 4), c, m = (u, h, w) => `select(${h}, ${u}, ${w})`; + if (!n) c = i.setByOffset("global_idx", m(a.getByOffset("global_idx"), d.getByOffset("global_idx"), l.getByOffset("global_idx"))); + else { + let u = (h, w, g = "") => { + let y = `a_data[index_a${w}][component_a${w}]`, S = `b_data[index_b${w}][component_b${w}]`, $ = `bool(c_data[index_c${w}] & (0xffu << (component_c${w} * 8)))`; + return ` + let output_indices${w} = ${i.offsetToIndices(`global_idx * 4u + ${w}u`)}; + let offset_a${w} = ${a.broadcastedIndicesToOffset(`output_indices${w}`, i)}; + let offset_b${w} = ${d.broadcastedIndicesToOffset(`output_indices${w}`, i)}; + let offset_c${w} = ${l.broadcastedIndicesToOffset(`output_indices${w}`, i)}; + let index_a${w} = offset_a${w} / 4u; + let index_b${w} = offset_b${w} / 4u; + let index_c${w} = offset_c${w} / 4u; + let component_a${w} = offset_a${w} % 4u; + let component_b${w} = offset_b${w} % 4u; + let component_c${w} = offset_c${w} % 4u; + ${h}[${w}] = ${g}(${m(y, S, $)}); + `; + }; + o === 9 ? c = ` + var data = vec4(0); + ${u("data", 0, "u32")} + ${u("data", 1, "u32")} + ${u("data", 2, "u32")} + ${u("data", 3, "u32")} + output_data[global_idx] = dot(vec4(0x1, 0x100, 0x10000, 0x1000000), vec4(data));` : c = ` + ${u("output_data[global_idx]", 0)} + ${u("output_data[global_idx]", 1)} + ${u("output_data[global_idx]", 2)} + ${u("output_data[global_idx]", 3)} + `; + } + return ` + ${e.registerUniform("vec_size", "u32").declareVariables(l, a, d, i)} + ${e.mainStart()} + ${e.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.vec_size")} + ${c} + }`; + }, Mh = (e) => { + let t = e[1].dims, r = e[2].dims, n = e[0].dims, o = e[1].dataType, i = !(k.areEqual(t, r) && k.areEqual(r, n)), a = t, d = k.size(t); + if (i) { + let c = rt.calcShape(rt.calcShape(t, r, false), n, false); + if (!c) throw new Error("Can't perform where op on the given tensors"); + a = c, d = k.size(a); + } + let l = Math.ceil(d / 4); + return { name: "Where", shaderCache: { inputDependencies: ["rank", "rank", "rank"] }, getShaderSource: (c) => Dh(c, e, a, i, o), getRunData: () => ({ outputs: [{ dims: a, dataType: o }], dispatchGroup: { x: Math.ceil(d / 64 / 4) }, programUniforms: [{ type: 12, data: l }, ...R(n, t, r, a)] }) }; + }, Pl = (e) => { + e.compute(Mh(e.inputs)); + }; + }); + var Ol, Bl = V(() => { + "use strict"; + Ts(); + Kr(); + As(); + Es(); + hu(); + Iu(); + ku(); + Hu(); + Xu(); + Ju(); + rd(); + sd(); + ld(); + pd(); + hd(); + bd(); + vd(); + Sd(); + Bd(); + Rd(); + Vd(); + fo(); + Ld(); + xo(); + Gd(); + sl(); + ll(); + pl(); + qr(); + gl(); + bl(); + _l(); + Sl(); + Cl(); + El(); + To(); + lt(); + Xr(); + zl(); + Ol = /* @__PURE__ */ new Map([["Abs", [Ps]], ["Acos", [zs]], ["Acosh", [Os]], ["Add", [gu]], ["ArgMax", [Ss, uo]], ["ArgMin", [xs, uo]], ["Asin", [Bs]], ["Asinh", [Ds]], ["Atan", [Ms]], ["Atanh", [Rs]], ["Attention", [Is]], ["AveragePool", [Qd, Zd]], ["BatchNormalization", [Cs]], ["BiasAdd", [ks]], ["BiasSplitGelu", [fu]], ["Cast", [Vs, Us]], ["Ceil", [Ws]], ["Clip", [Ns]], ["Concat", [Cu, Au]], ["Conv", [bo, yo]], ["ConvTranspose", [Yu, Ku]], ["Cos", [Ls]], ["Cosh", [Hs]], ["CumSum", [Zu, Qu]], ["DepthToSpace", [ed, td]], ["DequantizeLinear", [ul, dl]], ["Div", [yu]], ["Einsum", [id, ad]], ["Elu", [Gs, Qt]], ["Equal", [bu]], ["Erf", [Fs]], ["Exp", [qs]], ["Expand", [dd]], ["FastGelu", [cd]], ["Floor", [js]], ["FusedConv", [bo, yo]], ["Gather", [fd, md]], ["GatherElements", [_d, wd]], ["GatherBlockQuantized", [gd, yd]], ["Gelu", [Ks]], ["Gemm", [xd, $d]], ["GlobalAveragePool", [tl, el]], ["GlobalMaxPool", [al, il]], ["Greater", [$u]], ["GreaterOrEqual", [Su]], ["GroupQueryAttention", [Od, zd]], ["HardSigmoid", [ru, tu]], ["InstanceNormalization", [Md]], ["LayerNormalization", [Ud]], ["LeakyRelu", [Ys, Qt]], ["Less", [xu]], ["LessOrEqual", [Tu]], ["Log", [cu]], ["MatMul", [Wu]], ["MatMulNBits", [Nd, Wd]], ["MaxPool", [nl, ol]], ["Mul", [wu]], ["MultiHeadAttention", [Cd, Id]], ["Neg", [Zs]], ["Not", [Xs]], ["Pad", [Hd]], ["Pow", [_u]], ["QuickGelu", [pu, Qt]], ["Range", [cl]], ["Reciprocal", [Qs]], ["ReduceMin", [ys]], ["ReduceMean", [ps]], ["ReduceMax", [gs]], ["ReduceSum", [ws]], ["ReduceProd", [bs]], ["ReduceL1", [ms]], ["ReduceL2", [fs]], ["ReduceLogSum", [vs]], ["ReduceLogSumExp", [hs]], ["ReduceSumSquare", [_s]], ["Relu", [Js]], ["Resize", [fl, hl]], ["RotaryEmbedding", [yl]], ["Sigmoid", [eu]], ["Sin", [nu]], ["Sinh", [ou]], ["Slice", [$l, xl]], ["SkipLayerNormalization", [wl]], ["Split", [Al, kl]], ["Sqrt", [iu]], ["Softmax", [Tl, Il]], ["Sub", [vu]], ["Tan", [au]], ["Tanh", [uu]], ["ThresholdedRelu", [lu, Qt]], ["Tile", [kd]], ["Transpose", [Ja, es]], ["Where", [Pl]]]); + }); + var an, Dl = V(() => { + "use strict"; + Ke(); + Xe(); + ae(); + an = class { + constructor(t) { + this.backend = t; + this.repo = /* @__PURE__ */ new Map(), this.attributesBound = false; + } + getArtifact(t) { + return this.repo.get(t); + } + setArtifact(t, r) { + this.repo.set(t, r); + } + run(t, r, n, o, i) { + Le(t.programInfo.name); + let a = this.backend.device, d = this.backend.getComputePassEncoder(); + this.backend.writeTimestamp(this.backend.pendingDispatchNumber * 2); + let l = []; + for (let m of r) l.push({ binding: l.length, resource: { buffer: m.buffer } }); + for (let m of n) l.push({ binding: l.length, resource: { buffer: m.buffer } }); + i && l.push({ binding: l.length, resource: i }); + let c = a.createBindGroup({ layout: t.computePipeline.getBindGroupLayout(0), entries: l, label: t.programInfo.name }); + if (this.backend.sessionStatus === "capturing") { + let m = { kernelId: this.backend.currentKernelId, computePipeline: t.computePipeline, bindGroup: c, dispatchGroup: o }; + this.backend.capturedCommandList.get(this.backend.currentSessionId).push(m); + } + d.setPipeline(t.computePipeline), d.setBindGroup(0, c), d.dispatchWorkgroups(...o), this.backend.writeTimestamp(this.backend.pendingDispatchNumber * 2 + 1), this.backend.pendingDispatchNumber++, (this.backend.pendingDispatchNumber >= this.backend.maxDispatchNumber || this.backend.queryType === "at-passes") && this.backend.endComputePass(), this.backend.pendingDispatchNumber >= this.backend.maxDispatchNumber && this.backend.flush(), Ve(t.programInfo.name); + } + dispose() { + } + build(t, r) { + Le(t.name); + let n = this.backend.device, o = []; + n.features.has("shader-f16") && o.push("enable f16;"); + let i = Za(r, this.backend.device.limits), a = t.getShaderSource(i), d = `${o.join(` + `)} + ${i.additionalImplementations} + ${a}`, l = n.createShaderModule({ code: d, label: t.name }); + me("verbose", () => `[WebGPU] ${t.name} shader code: ${d}`); + let c = n.createComputePipeline({ compute: { module: l, entryPoint: "main" }, layout: "auto", label: t.name }); + return Ve(t.name), { programInfo: t, computePipeline: c, uniformVariablesInfo: i.variablesInfo }; + } + normalizeDispatchGroupSize(t) { + let r = typeof t == "number" ? t : t.x, n = typeof t == "number" ? 1 : t.y || 1, o = typeof t == "number" ? 1 : t.z || 1, i = this.backend.device.limits.maxComputeWorkgroupsPerDimension; + if (r <= i && n <= i && o <= i) return [r, n, o]; + let a = r * n * o, d = Math.ceil(Math.sqrt(a)); + if (d > i) { + if (d = Math.ceil(Math.cbrt(a)), d > i) throw new Error("Total dispatch size exceeds WebGPU maximum."); + return [d, d, d]; + } else return [d, d, 1]; + } + }; + }); + var Rh, Uh, Io, sn, Ml = V(() => { + "use strict"; + Ke(); + Q(); + Xe(); + Xn(); + Ka(); + Bl(); + Dl(); + Rh = (e, t) => { + if (t.length !== e.length) throw new Error(`inputDependencies length ${t.length} is not equal to inputTensors length ${e.length}.`); + let r = []; + for (let n = 0; n < e.length; ++n) { + let o = e[n].dataType; + switch (t[n]) { + case "none": { + r.push(""); + break; + } + case "type": { + r.push(`${o}`); + break; + } + case "rank": { + let i = e[n].dims.length; + r.push(`${o};${i}`); + break; + } + case "dims": { + let i = e[n].dims.join(","); + r.push(`${o};${i}`); + break; + } + default: + throw new Error(`unsupported input dependency: ${t[n]}`); + } + } + return r.join("|"); + }, Uh = (e, t, r) => { + var _a2, _b; + let n = e.name; + return ((_a2 = e.shaderCache) == null ? void 0 : _a2.hint) && (n += "[" + e.shaderCache.hint + "]"), n += ":" + r + `:${Rh(t, ((_b = e.shaderCache) == null ? void 0 : _b.inputDependencies) ?? new Array(t.length).fill("dims"))}`, n; + }, Io = class { + constructor(t) { + t && (this.architecture = t.architecture, this.vendor = t.vendor); + } + isArchitecture(t) { + return this.architecture === t; + } + isVendor(t) { + return this.vendor === t; + } + }, sn = class { + constructor() { + this.currentSessionId = null; + this.currentKernelId = null; + this.commandEncoder = null; + this.computePassEncoder = null; + this.maxDispatchNumber = 16; + this.pendingDispatchNumber = 0; + this.pendingKernels = []; + this.pendingQueries = /* @__PURE__ */ new Map(); + this.sessionStatus = "default"; + this.capturedCommandList = /* @__PURE__ */ new Map(); + this.capturedPendingKernels = /* @__PURE__ */ new Map(); + this.sessionExternalDataMapping = /* @__PURE__ */ new Map(); + } + get currentKernelCustomData() { + if (this.currentKernelId === null) throw new Error("currentKernelCustomData(): currentKernelId is null. (should not happen)"); + let t = this.kernelCustomData.get(this.currentKernelId); + return t || (t = {}, this.kernelCustomData.set(this.currentKernelId, t)), t; + } + async initialize(t, r) { + this.env = t; + let n = [], o = { requiredLimits: { maxComputeWorkgroupStorageSize: r.limits.maxComputeWorkgroupStorageSize, maxComputeWorkgroupsPerDimension: r.limits.maxComputeWorkgroupsPerDimension, maxStorageBufferBindingSize: r.limits.maxStorageBufferBindingSize, maxBufferSize: r.limits.maxBufferSize, maxComputeInvocationsPerWorkgroup: r.limits.maxComputeInvocationsPerWorkgroup, maxComputeWorkgroupSizeX: r.limits.maxComputeWorkgroupSizeX, maxComputeWorkgroupSizeY: r.limits.maxComputeWorkgroupSizeY, maxComputeWorkgroupSizeZ: r.limits.maxComputeWorkgroupSizeZ }, requiredFeatures: n }; + r.features.has("chromium-experimental-timestamp-query-inside-passes") ? n.push("chromium-experimental-timestamp-query-inside-passes") : r.features.has("timestamp-query") && n.push("timestamp-query"), r.features.has("shader-f16") && n.push("shader-f16"), this.device = await r.requestDevice(o), this.adapterInfo = new Io(r.info || await r.requestAdapterInfo()), this.gpuDataManager = ja(this), this.programManager = new an(this), this.kernels = /* @__PURE__ */ new Map(), this.kernelPersistentData = /* @__PURE__ */ new Map(), this.kernelCustomData = /* @__PURE__ */ new Map(), Wr(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: false, enumerable: true, configurable: false }), Object.defineProperty(this.env.webgpu, "adapter", { value: r, writable: false, enumerable: true, configurable: false }), this.setQueryType(); + } + dispose() { + typeof this.querySet < "u" && this.querySet.destroy(), this.gpuDataManager.dispose(); + } + getCommandEncoder() { + return this.commandEncoder || (this.commandEncoder = this.device.createCommandEncoder()), this.commandEncoder; + } + getComputePassEncoder() { + if (!this.computePassEncoder) { + let t = this.getCommandEncoder(), r = {}; + this.queryType === "at-passes" && (r.timestampWrites = { querySet: this.querySet, beginningOfPassWriteIndex: this.pendingDispatchNumber * 2, endOfPassWriteIndex: this.pendingDispatchNumber * 2 + 1 }), this.computePassEncoder = t.beginComputePass(r); + } + return this.computePassEncoder; + } + endComputePass() { + this.computePassEncoder && (this.computePassEncoder.end(), this.computePassEncoder = null); + } + flush() { + if (!this.commandEncoder) return; + Le(), this.endComputePass(); + let t; + this.queryType !== "none" && (this.commandEncoder.resolveQuerySet(this.querySet, 0, this.pendingDispatchNumber * 2, this.queryResolveBuffer, 0), t = this.device.createBuffer({ size: this.pendingDispatchNumber * 2 * 8, usage: GPUBufferUsage.MAP_READ | GPUBufferUsage.COPY_DST }), this.pendingQueries.set(t, this.pendingKernels), this.pendingKernels = [], this.commandEncoder.copyBufferToBuffer(this.queryResolveBuffer, 0, t, 0, this.pendingDispatchNumber * 2 * 8)), this.device.queue.submit([this.commandEncoder.finish()]), this.gpuDataManager.refreshPendingBuffers(), this.commandEncoder = null, this.pendingDispatchNumber = 0, this.queryType !== "none" && t.mapAsync(GPUMapMode.READ).then(() => { + var _a2; + let r = new BigUint64Array(t.getMappedRange()), n = this.pendingQueries.get(t); + for (let o = 0; o < r.length / 2; o++) { + let i = n[o], a = i.kernelId, d = this.kernels.get(a), l = d.kernelType, c = d.kernelName, m = i.programName, u = i.inputTensorViews, h = i.outputTensorViews, w = r[o * 2], g = r[o * 2 + 1]; + typeof this.queryTimeBase > "u" && (this.queryTimeBase = w); + let y = Number(w - this.queryTimeBase), S = Number(g - this.queryTimeBase); + if (!Number.isSafeInteger(y) || !Number.isSafeInteger(S)) throw new RangeError("incorrect timestamp range"); + if ((_a2 = this.env.webgpu.profiling) == null ? void 0 : _a2.ondata) this.env.webgpu.profiling.ondata({ version: 1, inputsMetadata: u.map(($) => ({ dims: $.dims, dataType: yt($.dataType) })), outputsMetadata: h.map(($) => ({ dims: $.dims, dataType: yt($.dataType) })), kernelId: a, kernelType: l, kernelName: c, programName: m, startTime: y, endTime: S }); + else { + let $ = ""; + u.forEach((x, T) => { + $ += `input[${T}]: [${x.dims}] | ${yt(x.dataType)}, `; + }); + let v = ""; + h.forEach((x, T) => { + v += `output[${T}]: [${x.dims}] | ${yt(x.dataType)}, `; + }), console.log(`[profiling] kernel "${a}|${l}|${c}|${m}" ${$}${v}execution time: ${S - y} ns`); + } + Sr("GPU", `${m}::${w}::${g}`); + } + t.unmap(), this.pendingQueries.delete(t); + }), Ve(); + } + run(t, r, n, o, i, a) { + Le(t.name); + let d = []; + for (let x = 0; x < r.length; ++x) { + let T = r[x].data; + if (T === 0) continue; + let C = this.gpuDataManager.get(T); + if (!C) throw new Error(`no GPU data for input: ${T}`); + d.push(C); + } + let { outputs: l, dispatchGroup: c, programUniforms: m } = t.getRunData(r), u = n.length === 0 ? l.map((x, T) => T) : n; + if (u.length !== l.length) throw new Error(`Output size ${u.length} must be equal to ${l.length}.`); + let h = [], w = []; + for (let x = 0; x < l.length; ++x) { + if (!Number.isInteger(u[x]) || u[x] < -3 || u[x] >= a) throw new Error(`Invalid output index: ${u[x]}`); + if (u[x] === -3) continue; + let T = u[x] === -1, C = u[x] === -2, A = T || C ? i(l[x].dataType, l[x].dims) : o(u[x], l[x].dataType, l[x].dims); + if (h.push(A), A.data === 0) continue; + let P = this.gpuDataManager.get(A.data); + if (!P) throw new Error(`no GPU data for output: ${A.data}`); + if (T && this.temporaryData.push(P), C) { + let B = this.kernelPersistentData.get(this.currentKernelId); + B || (B = [], this.kernelPersistentData.set(this.currentKernelId, B)), B.push(P); + } + w.push(P); + } + if (d.length !== r.length || w.length !== h.length) { + if (w.length === 0) return Ve(t.name), h; + throw new Error(`Program ${t.name} has zero-sized tensor(s) in inputs or outputs. This is not supported now.`); + } + let g; + if (m) { + let x = 0, T = []; + m.forEach((B) => { + let N = typeof B.data == "number" ? [B.data] : B.data; + if (N.length === 0) return; + let W = B.type === 10 ? 2 : 4, K, Z; + B.type === 10 ? (Z = N.length > 4 ? 16 : N.length > 2 ? 8 : N.length * W, K = N.length > 4 ? 16 : W * N.length) : (Z = N.length <= 2 ? N.length * W : 16, K = 16), x = Math.ceil(x / Z) * Z, T.push(x); + let ee = B.type === 10 ? 8 : 4; + x += N.length > 4 ? Math.ceil(N.length / ee) * K : N.length * W; + }); + let C = 16; + x = Math.ceil(x / C) * C; + let A = new ArrayBuffer(x); + m.forEach((B, N) => { + let W = T[N], K = typeof B.data == "number" ? [B.data] : B.data; + if (B.type === 6) new Int32Array(A, W, K.length).set(K); + else if (B.type === 12) new Uint32Array(A, W, K.length).set(K); + else if (B.type === 10) new Uint16Array(A, W, K.length).set(K); + else if (B.type === 1) new Float32Array(A, W, K.length).set(K); + else throw new Error(`Unsupported uniform type: ${yt(B.type)}`); + }); + let P = this.gpuDataManager.create(x, GPUBufferUsage.COPY_DST | GPUBufferUsage.UNIFORM); + this.device.queue.writeBuffer(P.buffer, 0, A, 0, x), this.gpuDataManager.release(P.id), g = { offset: 0, size: x, buffer: P.buffer }; + } + let y = this.programManager.normalizeDispatchGroupSize(c), S = y[1] === 1 && y[2] === 1, $ = Uh(t, r, S), v = this.programManager.getArtifact($); + if (v || (v = this.programManager.build(t, y), this.programManager.setArtifact($, v), me("info", () => `[artifact] key: ${$}, programName: ${t.name}`)), m && v.uniformVariablesInfo) { + if (m.length !== v.uniformVariablesInfo.length) throw new Error(`Uniform variables count mismatch: expect ${v.uniformVariablesInfo.length}, got ${m.length} in program "${v.programInfo.name}".`); + for (let x = 0; x < m.length; x++) { + let T = m[x], C = T.type, A = typeof T.data == "number" ? 1 : T.data.length, [P, B] = v.uniformVariablesInfo[x]; + if (C !== P || A !== B) throw new Error(`Uniform variable ${x} mismatch: expect type ${P} with size ${B}, got type ${C} with size ${A} in program "${v.programInfo.name}".`); + } + } + if (me("info", () => `[ProgramManager] run "${t.name}" (key=${$}) with ${y[0]}x${y[1]}x${y[2]}`), this.queryType !== "none" || this.sessionStatus === "capturing") { + let x = { kernelId: this.currentKernelId, programName: v.programInfo.name, inputTensorViews: r, outputTensorViews: h }; + this.pendingKernels.push(x), this.sessionStatus === "capturing" && this.capturedPendingKernels.get(this.currentSessionId).push(x); + } + return this.programManager.run(v, d, w, y, g), Ve(t.name), h; + } + upload(t, r) { + this.gpuDataManager.upload(t, r); + } + memcpy(t, r) { + this.gpuDataManager.memcpy(t, r); + } + async download(t, r) { + await this.gpuDataManager.download(t, r); + } + alloc(t) { + return this.gpuDataManager.create(t).id; + } + free(t) { + return this.gpuDataManager.release(t); + } + createKernel(t, r, n, o) { + let i = Ol.get(t); + if (!i) throw new Error(`kernel not implemented: ${t}`); + let a = { kernelType: t, kernelName: o, kernelEntry: i[0], attributes: [i[1], n] }; + this.kernels.set(r, a); + } + releaseKernel(t) { + let r = this.kernelPersistentData.get(t); + if (r) { + for (let n of r) this.gpuDataManager.release(n.id); + this.kernelPersistentData.delete(t); + } + this.kernelCustomData.delete(t), this.kernels.delete(t); + } + computeKernel(t, r, n) { + let o = this.kernels.get(t); + if (!o) throw new Error(`kernel not created: ${t}`); + let i = o.kernelType, a = o.kernelName, d = o.kernelEntry, l = o.attributes; + if (this.currentKernelId !== null) throw new Error(`kernel "[${i}] ${a}" is not allowed to be called recursively`); + this.currentKernelId = t, l[0] && (l[1] = l[0](l[1]), l[0] = void 0), me("info", () => `[WebGPU] Start to run kernel "[${i}] ${a}"...`); + let c = this.env.debug; + this.temporaryData = []; + try { + return c && this.device.pushErrorScope("validation"), d(r, l[1]), 0; + } catch (m) { + return n.push(Promise.resolve(`[WebGPU] Kernel "[${i}] ${a}" failed. ${m}`)), 1; + } finally { + c && n.push(this.device.popErrorScope().then((m) => m ? `GPU validation error for kernel "[${i}] ${a}": ${m.message}` : null)); + for (let m of this.temporaryData) this.gpuDataManager.release(m.id); + this.temporaryData = [], this.currentKernelId = null; + } + } + registerBuffer(t, r, n, o) { + let i = this.sessionExternalDataMapping.get(t); + i || (i = /* @__PURE__ */ new Map(), this.sessionExternalDataMapping.set(t, i)); + let a = i.get(r), d = this.gpuDataManager.registerExternalBuffer(n, o, a); + return i.set(r, [d, n]), d; + } + unregisterBuffers(t) { + let r = this.sessionExternalDataMapping.get(t); + r && (r.forEach((n) => this.gpuDataManager.unregisterExternalBuffer(n[0])), this.sessionExternalDataMapping.delete(t)); + } + getBuffer(t) { + let r = this.gpuDataManager.get(t); + if (!r) throw new Error(`no GPU data for buffer: ${t}`); + return r.buffer; + } + createDownloader(t, r, n) { + return async () => { + let o = await eo(this, t, r); + return Lr(o.buffer, n); + }; + } + writeTimestamp(t) { + this.queryType === "inside-passes" && this.computePassEncoder.writeTimestamp(this.querySet, t); + } + setQueryType() { + var _a2; + this.queryType = "none", (((_a2 = this.env.webgpu.profiling) == null ? void 0 : _a2.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() { + me("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() { + me("info", "captureEnd"), this.flush(), this.sessionStatus = "default"; + } + replay() { + me("info", "replay"), this.sessionStatus = "replaying"; + let t = this.capturedCommandList.get(this.currentSessionId), r = this.capturedPendingKernels.get(this.currentSessionId), n = t.length; + this.pendingKernels = []; + for (let o = 0; o < n; o++) { + let i = this.getComputePassEncoder(), a = t[o]; + this.writeTimestamp(this.pendingDispatchNumber * 2), i.setPipeline(a.computePipeline), i.setBindGroup(0, a.bindGroup), i.dispatchWorkgroups(...a.dispatchGroup), this.writeTimestamp(this.pendingDispatchNumber * 2 + 1), this.pendingDispatchNumber++, this.queryType !== "none" && this.pendingKernels.push(r[o]), (this.pendingDispatchNumber >= 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 Vh, Rl, un, Co, Ul, Vl = V(() => { + "use strict"; + Xe(); + Vh = 1, Rl = () => Vh++, un = class { + constructor(t, r) { + this.mlContext = t; + this.tensorEntry = r, this.tensorCache = r ? [r] : []; + } + get tensor() { + var _a2; + return (_a2 = this.tensorEntry) == null ? void 0 : _a2[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, r) { + for (let [n, o, i] of this.tensorCache) if (r === n) { + if (this.context !== t) throw new Error("MLTensor cannot be registered with a different MLContext."); + return this.tensorEntry = [n, o, i], true; + } + return false; + } + async ensureTensor(t, r, n) { + var _a2; + if (this.tensorEntry) { + let [a, d, l] = this.tensorEntry; + if (d === t && l.every((c, m) => c === r[m])) return a; + } + for (let [a, d, l] of this.tensorCache) if (d === t && l.every((c, m) => c === r[m])) { + if (n && this.tensorEntry) { + me("verbose", () => `[WebNN] Slowdown may occur, having to copy existing tensor {dataType: ${t}, shape: ${r}}`); + let c = await this.context.readTensor(this.tensorEntry[0]); + this.context.writeTensor(a, c); + } + return this.tensorEntry = [a, d, l], a; + } + me("verbose", () => `[WebNN] MLContext.createTensor {dataType: ${t}, shape: ${r}}`); + let o = MLTensorUsage.READ | MLTensorUsage.WRITE, i = await this.context.createTensor({ dataType: t, shape: r, dimensions: r, usage: o }); + return this.tensorEntry = [i, t, r], this.tensorCache.push(this.tensorEntry), this.activeUpload && ((_a2 = this.mlContext) == null ? void 0 : _a2.writeTensor(i, this.activeUpload), this.activeUpload = void 0), i; + } + upload(t) { + var _a2; + if (!this.tensorEntry) { + this.activeUpload = new Uint8Array(t); + return; + } + (_a2 = this.mlContext) == null ? void 0 : _a2.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]); + } + }, Co = class { + constructor(t) { + this.backend = t; + this.tensorsById = /* @__PURE__ */ new Map(); + this.tensorIdsByContext = /* @__PURE__ */ new Map(); + } + reserveTensorId() { + let t = Rl(); + return this.tensorsById.set(t, new un()), t; + } + releaseTensorId(t) { + let r = this.tensorsById.get(t); + if (r) { + r.destroy(), this.tensorsById.delete(t); + for (let [n, o] of this.tensorIdsByContext) if (o.has(t)) { + o.delete(t), o.size === 0 && this.tensorIdsByContext.delete(n); + break; + } + } + } + async ensureTensor(t, r, n, o) { + var _a2; + me("verbose", () => `[WebNN] TensorManager.ensureTensor {tensorId: ${t}, dataType: ${r}, shape: ${n}, 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, /* @__PURE__ */ new Set()), (_a2 = this.tensorIdsByContext.get(this.backend.currentContext)) == null ? void 0 : _a2.add(t), i.ensureTensor(r, n, o); + } + upload(t, r) { + this.tensorsById.get(t).upload(r); + } + async download(t, r) { + return me("verbose", () => `[WebNN] TensorManager.download {tensorId: ${t}, dstBuffer: ${r == null ? void 0 : r.byteLength}}`), this.tensorsById.get(t).download(r); + } + releaseTensorsForContext(t) { + let r = this.tensorIdsByContext.get(t); + if (r) { + for (let n of r) this.tensorsById.get(n).destroy(), this.tensorsById.delete(n); + this.tensorIdsByContext.delete(t); + } + } + registerTensor(t, r, n, o) { + for (let [d, l] of this.tensorsById) if (l.trySelectTensor(t, r)) return d; + let i = Rl(); + this.tensorsById.set(i, new un(t, [r, n, o])); + let a = this.tensorIdsByContext.get(t); + return a || (a = /* @__PURE__ */ new Set(), this.tensorIdsByContext.set(t, a)), a.add(i), i; + } + }, Ul = (...e) => new Co(...e); + }); + var Nl, dn, Wl = V(() => { + "use strict"; + Q(); + gt(); + Xn(); + Vl(); + Xe(); + Nl = /* @__PURE__ */ new Map([[1, "float32"], [10, "float16"], [6, "int32"], [12, "uint32"], [7, "int64"], [13, "uint64"], [3, "int8"], [2, "uint8"], [9, "uint8"]]), dn = class { + constructor(t) { + this.tensorManager = Ul(this); + this.mlContextBySessionId = /* @__PURE__ */ new Map(); + this.sessionIdsByMLContext = /* @__PURE__ */ new Map(); + Wr(t.logLevel, !!t.debug); + } + get currentSessionId() { + if (this.activeSessionId === void 0) throw new Error("No active session"); + return this.activeSessionId; + } + onRunStart(t) { + this.activeSessionId = t; + } + get currentContext() { + let t = this.getMLContext(this.currentSessionId); + if (!t) throw new Error(`No MLContext found for session ${this.currentSessionId}`); + return t; + } + registerMLContext(t, r) { + this.mlContextBySessionId.set(t, r); + let n = this.sessionIdsByMLContext.get(r); + n || (n = /* @__PURE__ */ new Set(), this.sessionIdsByMLContext.set(r, n)), n.add(t); + } + onReleaseSession(t) { + let r = this.mlContextBySessionId.get(t); + if (!r) return; + this.mlContextBySessionId.delete(t); + let n = this.sessionIdsByMLContext.get(r); + n.delete(t), n.size === 0 && (this.sessionIdsByMLContext.delete(r), this.tensorManager.releaseTensorsForContext(r)); + } + getMLContext(t) { + return this.mlContextBySessionId.get(t); + } + reserveTensorId() { + return this.tensorManager.reserveTensorId(); + } + releaseTensorId(t) { + me("verbose", () => `[WebNN] releaseTensorId {tensorId: ${t}}`), this.tensorManager.releaseTensorId(t); + } + async ensureTensor(t, r, n, o) { + let i = Nl.get(r); + if (!i) throw new Error(`Unsupported ONNX data type: ${r}`); + return this.tensorManager.ensureTensor(t, i, n, o); + } + uploadTensor(t, r) { + if (!Te().shouldTransferToMLTensor) throw new Error("Trying to upload to a MLTensor while shouldTransferToMLTensor is false"); + me("verbose", () => `[WebNN] uploadTensor {tensorId: ${t}, data: ${r.byteLength}}`), this.tensorManager.upload(t, r); + } + async downloadTensor(t, r) { + return this.tensorManager.download(t, r); + } + createMLTensorDownloader(t, r) { + return async () => { + let n = await this.tensorManager.download(t); + return Lr(n, r); + }; + } + registerMLTensor(t, r, n) { + let o = Nl.get(r); + if (!o) throw new Error(`Unsupported ONNX data type: ${r}`); + let i = this.tensorManager.registerTensor(this.currentContext, t, o, n); + return me("verbose", () => `[WebNN] registerMLTensor {tensor: ${t}, dataType: ${o}, dimensions: ${n}} -> {tensorId: ${i}}`), i; + } + flush() { + } + }; + }); + var Ll = {}; + Gt(Ll, { init: () => Nh }); + var or, Ao, Nh, Hl = V(() => { + "use strict"; + Q(); + Ml(); + Xe(); + ie(); + Wl(); + or = class e { + constructor(t, r, n, o) { + this.module = t; + this.dataType = r; + this.data = n; + this.dims = o; + } + getFloat32Array() { + if (this.dataType !== 1) throw new Error("Invalid data type"); + let t = k.size(this.dims); + return t === 0 ? new Float32Array() : new Float32Array(this.module.HEAP8.buffer, this.data, t); + } + getBigInt64Array() { + if (this.dataType !== 7) throw new Error("Invalid data type"); + let t = k.size(this.dims); + return t === 0 ? new BigInt64Array() : new BigInt64Array(this.module.HEAP8.buffer, this.data, t); + } + getInt32Array() { + if (this.dataType !== 6) throw new Error("Invalid data type"); + let t = k.size(this.dims); + return t === 0 ? new Int32Array() : new Int32Array(this.module.HEAP8.buffer, this.data, t); + } + getUint16Array() { + if (this.dataType !== 10 && this.dataType !== 4) throw new Error("Invalid data type"); + let t = k.size(this.dims); + return t === 0 ? new Uint16Array() : new Uint16Array(this.module.HEAP8.buffer, this.data, t); + } + reshape(t) { + if (k.size(t) !== k.size(this.dims)) throw new Error("Invalid new shape"); + return new e(this.module, this.dataType, this.data, t); + } + }, Ao = class { + constructor(t, r, n) { + this.module = t; + this.backend = r; + this.customDataOffset = 0; + this.customDataSize = 0; + this.adapterInfo = r.adapterInfo; + let o = t.HEAPU32, i = n >>> 2; + this.opKernelContext = o[i++]; + let a = o[i++]; + this.outputCount = o[i++], this.customDataOffset = o[i++], this.customDataSize = o[i++]; + let d = []; + for (let l = 0; l < a; l++) { + let c = o[i++], m = o[i++], u = o[i++], h = []; + for (let w = 0; w < u; w++) h.push(o[i++]); + d.push(new or(t, c, m, h)); + } + this.inputs = d; + } + get kernelCustomData() { + return this.backend.currentKernelCustomData; + } + get customDataBuffer() { + return this.module.HEAPU8.subarray(this.customDataOffset, this.customDataOffset + this.customDataSize); + } + getMaxComputeWorkgroupSizes() { + return [this.backend.device.limits.maxComputeWorkgroupSizeX, this.backend.device.limits.maxComputeWorkgroupSizeY, this.backend.device.limits.maxComputeWorkgroupSizeZ]; + } + getMaxComputeWorkgroupStoragesize() { + return this.backend.device.limits.maxComputeWorkgroupStorageSize; + } + compute(t, r) { + var _a2; + let n = ((_a2 = r == null ? void 0 : r.inputs) == null ? void 0 : _a2.map((d) => typeof d == "number" ? this.inputs[d] : d)) ?? this.inputs, o = (r == null ? void 0 : r.outputs) ?? [], i = (d, l, c) => new or(this.module, l, this.output(d, c), c), a = (d, l) => { + let c = It(d, l); + if (!c) throw new Error(`Unsupported data type: ${d}`); + let m = c > 0 ? this.backend.gpuDataManager.create(c).id : 0; + return new or(this.module, d, m, l); + }; + return this.backend.run(t, n, o, i, a, this.outputCount); + } + output(t, r) { + let n = this.module.stackSave(); + try { + let o = this.module.stackAlloc((1 + r.length) * 4), i = o >> 2; + this.module.HEAPU32[i++] = r.length; + for (let a = 0; a < r.length; a++) this.module.HEAPU32[i++] = r[a]; + return this.module._JsepOutput(this.opKernelContext, t, o); + } catch (o) { + throw new Error(`Failed to generate kernel's output[${t}] with dims [${r}]. If you are running with pre-allocated output, please make sure the output type/dims are correct. Error: ${o}`); + } finally { + this.module.stackRestore(n); + } + } + }, Nh = async (e, t, r, n) => { + let o = t.jsepInit; + if (!o) throw new Error("Failed to initialize JSEP. The WebAssembly module is not built with JSEP support."); + if (e === "webgpu") { + let i = new sn(); + await i.initialize(r, n), o("webgpu", [i, (a) => i.alloc(a), (a) => i.free(a), (a, d, l, c = false) => { + if (c) me("verbose", () => `[WebGPU] jsepCopyGpuToGpu: src=${a}, dst=${d}, size=${l}`), i.memcpy(a, d); + else { + me("verbose", () => `[WebGPU] jsepCopyCpuToGpu: dataOffset=${a}, gpuDataId=${d}, size=${l}`); + let m = t.HEAPU8.subarray(a >>> 0, (a >>> 0) + l); + i.upload(d, m); + } + }, async (a, d, l) => { + me("verbose", () => `[WebGPU] jsepCopyGpuToCpu: gpuDataId=${a}, dataOffset=${d}, size=${l}`), await i.download(a, () => t.HEAPU8.subarray(d >>> 0, (d >>> 0) + l)); + }, (a, d, l) => i.createKernel(a, d, l, t.UTF8ToString(t._JsepGetNodeName(d))), (a) => i.releaseKernel(a), (a, d, l, c) => { + me("verbose", () => `[WebGPU] jsepRun: sessionHandle=${l}, kernel=${a}, contextDataOffset=${d}`); + let m = new Ao(t, i, d); + return i.computeKernel(a, m, c); + }, () => i.captureBegin(), () => i.captureEnd(), () => i.replay()]); + } else { + let i = new dn(r); + o("webnn", [i, () => i.reserveTensorId(), (a) => i.releaseTensorId(a), async (a, d, l, c) => i.ensureTensor(a, d, l, c), (a, d) => { + i.uploadTensor(a, d); + }, async (a, d) => i.downloadTensor(a, d)]); + } + }; + }); + var Wh, kr, Er, Et, Lh, jt, Pr, zr, Gl, Or, Br, Dr, Gn = V(() => { + "use strict"; + Va(); + Wa(); + Q(); + gt(); + Rr(); + Yn(); + Wh = (e, t) => { + Te()._OrtInit(e, t) !== 0 && ve("Can't initialize onnxruntime."); + }, kr = async (e) => { + Wh(e.wasm.numThreads, Xt(e.logLevel)); + }, Er = async (e, t) => { + { + let r = (Hl(), br(Ll)).init; + if (t === "webgpu") { + if (typeof navigator > "u" || !navigator.gpu) throw new Error("WebGPU is not supported in current environment"); + let n = e.webgpu.adapter; + if (n) { + if (typeof n.limits != "object" || typeof n.features != "object" || typeof n.requestDevice != "function") throw new Error("Invalid GPU adapter set in `env.webgpu.adapter`. It must be a GPUAdapter object."); + } else { + let o = e.webgpu.powerPreference; + if (o !== void 0 && o !== "low-power" && o !== "high-performance") throw new Error(`Invalid powerPreference setting: "${o}"`); + let i = e.webgpu.forceFallbackAdapter; + if (i !== void 0 && typeof i != "boolean") throw new Error(`Invalid forceFallbackAdapter setting: "${i}"`); + if (n = await navigator.gpu.requestAdapter({ powerPreference: o, forceFallbackAdapter: i }), !n) throw new Error('Failed to get GPU adapter. You may need to enable flag "--enable-unsafe-webgpu" if you are using Chrome.'); + } + await r("webgpu", Te(), e, n); + } + if (t === "webnn") { + if (typeof navigator > "u" || !navigator.ml) throw new Error("WebNN is not supported in current environment"); + await r("webnn", Te(), e); + } + } + }, Et = /* @__PURE__ */ new Map(), Lh = (e) => { + let t = Te(), r = t.stackSave(); + try { + let n = t.stackAlloc(8); + return t._OrtGetInputOutputCount(e, n, n + 4) !== 0 && ve("Can't get session input/output count."), [t.HEAP32[n / 4], t.HEAP32[n / 4 + 1]]; + } finally { + t.stackRestore(r); + } + }, jt = (e) => { + let t = Te(), r = t._malloc(e.byteLength); + if (r === 0) throw new Error(`Can't create a session. failed to allocate a buffer of size ${e.byteLength}.`); + return t.HEAPU8.set(e, r), [r, e.byteLength]; + }, Pr = async (e, t) => { + var _a2, _b; + let r, n, o = Te(); + Array.isArray(e) ? [r, n] = e : e.buffer === o.HEAPU8.buffer ? [r, n] = [e.byteOffset, e.byteLength] : [r, n] = jt(e); + let i = 0, a = 0, d = 0, l = [], c = [], m = []; + try { + if ([a, l] = Na(t), (t == null ? void 0 : t.externalData) && o.mountExternalData) { + let v = []; + for (let x of t.externalData) { + let T = typeof x == "string" ? x : x.path; + v.push(Zt(typeof x == "string" ? x : x.data).then((C) => { + o.mountExternalData(T, C); + })); + } + await Promise.all(v); + } + for (let v of (t == null ? void 0 : t.executionProviders) ?? []) if ((typeof v == "string" ? v : v.name) === "webnn") { + if (o.shouldTransferToMLTensor = false, o.currentContext) throw new Error("WebNN execution provider is already set."); + if (typeof v != "string") { + let T = v, C = T == null ? void 0 : T.context, A = T == null ? void 0 : T.gpuDevice, P = T == null ? void 0 : T.deviceType, B = T == null ? void 0 : T.numThreads, N = T == null ? void 0 : T.powerPreference; + C ? o.currentContext = C : A ? o.currentContext = await navigator.ml.createContext(A) : o.currentContext = await navigator.ml.createContext({ deviceType: P, numThreads: B, powerPreference: N }); + } else o.currentContext = await navigator.ml.createContext(); + break; + } + i = await o._OrtCreateSession(r, n, a), i === 0 && ve("Can't create a session."), o.currentContext && (o.jsepRegisterMLContext(i, o.currentContext), o.currentContext = void 0, o.shouldTransferToMLTensor = true); + let [u, h] = Lh(i), w = !!(t == null ? void 0 : t.enableGraphCapture), g = [], y = [], S = []; + for (let v = 0; v < u; v++) { + let x = o._OrtGetInputName(i, v); + x === 0 && ve("Can't get an input name."), c.push(x), g.push(o.UTF8ToString(x)); + } + for (let v = 0; v < h; v++) { + let x = o._OrtGetOutputName(i, v); + x === 0 && ve("Can't get an output name."), m.push(x); + let T = o.UTF8ToString(x); + y.push(T); + { + if (w && (t == null ? void 0 : t.preferredOutputLocation) === void 0) { + S.push("gpu-buffer"); + continue; + } + let C = typeof (t == null ? void 0 : t.preferredOutputLocation) == "string" ? t.preferredOutputLocation : ((_a2 = t == null ? void 0 : t.preferredOutputLocation) == null ? void 0 : _a2[T]) ?? "cpu"; + if (C !== "cpu" && C !== "cpu-pinned" && C !== "gpu-buffer" && C !== "ml-tensor") throw new Error(`Not supported preferred output location: ${C}.`); + if (w && C !== "gpu-buffer") throw new Error(`Not supported preferred output location: ${C}. Only 'gpu-buffer' location is supported when enableGraphCapture is true.`); + S.push(C); + } + } + let $ = null; + return S.some((v) => v === "gpu-buffer" || v === "ml-tensor") && (d = o._OrtCreateBinding(i), d === 0 && ve("Can't create IO binding."), $ = { handle: d, outputPreferredLocations: S, outputPreferredLocationsEncoded: S.map((v) => Kn(v)) }), Et.set(i, [i, c, m, $, w, false]), [i, g, y]; + } catch (u) { + throw c.forEach((h) => o._OrtFree(h)), m.forEach((h) => o._OrtFree(h)), d !== 0 && o._OrtReleaseBinding(d), i !== 0 && o._OrtReleaseSession(i), u; + } finally { + o._free(r), a !== 0 && o._OrtReleaseSessionOptions(a), l.forEach((u) => o._free(u)), (_b = o.unmountExternalData) == null ? void 0 : _b.call(o); + } + }, zr = (e) => { + var _a2; + let t = Te(), r = Et.get(e); + if (!r) throw new Error(`cannot release session. invalid session id: ${e}`); + let [n, o, i, a, d] = r; + a && (d && t._OrtClearBoundOutputs(a.handle), t._OrtReleaseBinding(a.handle)), (_a2 = t.jsepOnReleaseSession) == null ? void 0 : _a2.call(t, e), o.forEach((l) => t._OrtFree(l)), i.forEach((l) => t._OrtFree(l)), t._OrtReleaseSession(n), Et.delete(e); + }, Gl = (e, t, r, n, o, i = false) => { + if (!e) { + t.push(0); + return; + } + let a = Te(), d = e[0], l = e[1], c = e[3], m, u; + if (d === "string" && (c === "gpu-buffer" || c === "ml-tensor")) throw new Error("String tensor is not supported on GPU."); + if (i && c !== "gpu-buffer") throw new Error(`External buffer must be provided for input/output index ${o} when enableGraphCapture is true.`); + if (c === "gpu-buffer") { + let g = e[2].gpuBuffer; + u = It(Yt(d), l); + let y = a.jsepRegisterBuffer; + if (!y) throw new Error('Tensor location "gpu-buffer" is not supported without using WebGPU.'); + m = y(n, o, g, u); + } else if (c === "ml-tensor") { + let g = e[2].mlTensor; + u = It(Yt(d), l); + let y = a.jsepRegisterMLTensor; + if (!y) throw new Error('Tensor location "ml-tensor" is not supported without using WebNN.'); + m = y(g, Yt(d), l); + } else { + let g = e[2]; + if (Array.isArray(g)) { + u = 4 * g.length, m = a._malloc(u), r.push(m); + let y = m / 4; + for (let S = 0; S < g.length; S++) { + if (typeof g[S] != "string") throw new TypeError(`tensor data at index ${S} is not a string`); + a.HEAPU32[y++] = ke(g[S], r); + } + } else u = g.byteLength, m = a._malloc(u), r.push(m), a.HEAPU8.set(new Uint8Array(g.buffer, g.byteOffset, u), m); + } + let h = a.stackSave(), w = a.stackAlloc(4 * l.length); + try { + let g = w / 4; + l.forEach((S) => a.HEAP32[g++] = S); + let y = a._OrtCreateTensor(Yt(d), m, u, w, l.length, Kn(c)); + y === 0 && ve(`Can't create tensor for input/output. session=${n}, index=${o}.`), t.push(y); + } finally { + a.stackRestore(h); + } + }, Or = async (e, t, r, n, o, i) => { + var _a2, _b; + let a = Te(), d = Et.get(e); + if (!d) throw new Error(`cannot run inference. invalid session id: ${e}`); + let l = d[0], c = d[1], m = d[2], u = d[3], h = d[4], w = d[5], g = t.length, y = n.length, S = 0, $ = [], v = [], x = [], T = [], C = a.stackSave(), A = a.stackAlloc(g * 4), P = a.stackAlloc(g * 4), B = a.stackAlloc(y * 4), N = a.stackAlloc(y * 4); + try { + (_a2 = a.jsepOnRunStart) == null ? void 0 : _a2.call(a, l), [S, $] = Ua(i); + for (let Y = 0; Y < g; Y++) Gl(r[Y], v, T, e, t[Y], h); + for (let Y = 0; Y < y; Y++) Gl(o[Y], x, T, e, g + n[Y], h); + let W = A / 4, K = P / 4, Z = B / 4, ee = N / 4; + for (let Y = 0; Y < g; Y++) a.HEAPU32[W++] = v[Y], a.HEAPU32[K++] = c[t[Y]]; + for (let Y = 0; Y < y; Y++) a.HEAPU32[Z++] = x[Y], a.HEAPU32[ee++] = m[n[Y]]; + if (u && !w) { + let { handle: Y, outputPreferredLocations: le, outputPreferredLocationsEncoded: ce } = u; + if (c.length !== g) throw new Error(`input count from feeds (${g}) is expected to be always equal to model's input count (${c.length}).`); + for (let q = 0; q < g; q++) { + let ue = t[q]; + await a._OrtBindInput(Y, c[ue], v[q]) !== 0 && ve(`Can't bind input[${q}] for session=${e}.`); + } + for (let q = 0; q < y; q++) { + let ue = n[q]; + ((_b = o[q]) == null ? void 0 : _b[3]) ? a._OrtBindOutput(Y, m[ue], x[q], 0) !== 0 && ve(`Can't bind pre-allocated output[${q}] for session=${e}.`) : a._OrtBindOutput(Y, m[ue], 0, ce[ue]) !== 0 && ve(`Can't bind output[${q}] to ${le[q]} for session=${e}.`); + } + Et.set(e, [l, c, m, u, h, true]); + } + let se; + u ? se = await a._OrtRunWithBinding(l, u.handle, y, B, S) : se = await a._OrtRun(l, P, A, g, N, y, B, S), se !== 0 && ve("failed to call OrtRun()."); + let de = []; + for (let Y = 0; Y < y; Y++) { + let le = a.HEAPU32[B / 4 + Y]; + if (le === x[Y]) { + de.push(o[Y]); + continue; + } + let ce = a.stackSave(), q = a.stackAlloc(4 * 4), ue = false, re, ne = 0; + try { + a._OrtGetTensorData(le, q, q + 4, q + 8, q + 12) !== 0 && ve(`Can't access output tensor data on index ${Y}.`); + let U = q / 4, G = a.HEAPU32[U++]; + ne = a.HEAPU32[U++]; + let ye = a.HEAPU32[U++], Re = a.HEAPU32[U++], $e = []; + for (let Ae = 0; Ae < Re; Ae++) $e.push(a.HEAPU32[ye / 4 + Ae]); + a._OrtFree(ye); + let Ce = $e.reduce((Ae, Me) => Ae * Me, 1); + re = yt(G); + let bt = u == null ? void 0 : u.outputPreferredLocations[n[Y]]; + if (re === "string") { + if (bt === "gpu-buffer" || bt === "ml-tensor") throw new Error("String tensor is not supported on GPU."); + let Ae = [], Me = ne / 4; + for (let Ue = 0; Ue < Ce; Ue++) { + let zt = a.HEAPU32[Me++], wt = Ue === Ce - 1 ? void 0 : a.HEAPU32[Me] - zt; + Ae.push(a.UTF8ToString(zt, wt)); + } + de.push([re, $e, Ae, "cpu"]); + } else if (bt === "gpu-buffer" && Ce > 0) { + let Ae = a.jsepGetBuffer; + if (!Ae) throw new Error('preferredLocation "gpu-buffer" is not supported without using WebGPU.'); + let Me = Ae(ne), Ue = It(G, Ce); + if (Ue === void 0 || !Vr(re)) throw new Error(`Unsupported data type: ${re}`); + ue = true, de.push([re, $e, { gpuBuffer: Me, download: a.jsepCreateDownloader(Me, Ue, re), dispose: () => { + a._OrtReleaseTensor(le); + } }, "gpu-buffer"]); + } else if (bt === "ml-tensor" && Ce > 0) { + let Ae = a.jsepEnsureTensor; + if (!Ae) throw new Error('preferredLocation "ml-tensor" is not supported without using WebNN.'); + if (It(G, Ce) === void 0 || !Nr(re)) throw new Error(`Unsupported data type: ${re}`); + let Ue = await Ae(ne, G, $e, false); + ue = true, de.push([re, $e, { mlTensor: Ue, download: a.jsepCreateMLTensorDownloader(ne, re), dispose: () => { + a.jsepReleaseTensorId(ne), a._OrtReleaseTensor(le); + } }, "ml-tensor"]); + } else { + let Ae = Ur(re), Me = new Ae(Ce); + new Uint8Array(Me.buffer, Me.byteOffset, Me.byteLength).set(a.HEAPU8.subarray(ne, ne + Me.byteLength)), de.push([re, $e, Me, "cpu"]); + } + } finally { + a.stackRestore(ce), re === "string" && ne && a._free(ne), ue || a._OrtReleaseTensor(le); + } + } + return u && !h && (a._OrtClearBoundOutputs(u.handle), Et.set(e, [l, c, m, u, h, false])), de; + } finally { + a.stackRestore(C), v.forEach((W) => a._OrtReleaseTensor(W)), x.forEach((W) => a._OrtReleaseTensor(W)), T.forEach((W) => a._free(W)), S !== 0 && a._OrtReleaseRunOptions(S), $.forEach((W) => a._free(W)); + } + }, Br = (e) => { + let t = Te(), r = Et.get(e); + if (!r) throw new Error("invalid session id"); + let n = r[0], o = t._OrtEndProfiling(n); + o === 0 && ve("Can't get an profile file name."), t._OrtFree(o); + }, Dr = (e) => { + let t = []; + for (let r of e) { + let n = r[2]; + !Array.isArray(n) && "buffer" in n && t.push(n.buffer); + } + return t; + }; + }); + var Pt, Ye, ir, cn, pn, ln, ko, Eo, Lt, Ht, Gh, Fl, ql, jl, Kl, Yl, Xl, Zl, Po = V(() => { + "use strict"; + Ke(); + Gn(); + gt(); + qt(); + Pt = () => !!_e.wasm.proxy && typeof document < "u", ir = false, cn = false, pn = false, Eo = /* @__PURE__ */ new Map(), Lt = (e, t) => { + let r = Eo.get(e); + r ? r.push(t) : Eo.set(e, [t]); + }, Ht = () => { + if (ir || !cn || pn || !Ye) throw new Error("worker not ready"); + }, Gh = (e) => { + switch (e.data.type) { + case "init-wasm": + ir = false, e.data.err ? (pn = true, ko[1](e.data.err)) : (cn = true, ko[0]()), ln && (URL.revokeObjectURL(ln), ln = void 0); + break; + case "init-ep": + case "copy-from": + case "create": + case "release": + case "run": + case "end-profiling": { + let t = Eo.get(e.data.type); + e.data.err ? t.shift()[1](e.data.err) : t.shift()[0](e.data.out); + break; + } + default: + } + }, Fl = async () => { + if (!cn) { + if (ir) throw new Error("multiple calls to 'initWasm()' detected."); + if (pn) throw new Error("previous call to 'initWasm()' failed."); + if (ir = true, Pt()) return new Promise((e, t) => { + Ye == null ? void 0 : Ye.terminate(), Da().then(([r, n]) => { + try { + Ye = n, Ye.onerror = (i) => t(i), Ye.onmessage = Gh, ko = [e, t]; + let o = { type: "init-wasm", in: _e }; + Ye.postMessage(o), ln = r; + } catch (o) { + t(o); + } + }, t); + }); + try { + await Ar(_e.wasm), await kr(_e), cn = true; + } catch (e) { + throw pn = true, e; + } finally { + ir = false; + } + } + }, ql = async (e) => { + if (Pt()) return Ht(), new Promise((t, r) => { + Lt("init-ep", [t, r]); + let n = { type: "init-ep", in: { epName: e, env: _e } }; + Ye.postMessage(n); + }); + await Er(_e, e); + }, jl = async (e) => Pt() ? (Ht(), new Promise((t, r) => { + Lt("copy-from", [t, r]); + let n = { type: "copy-from", in: { buffer: e } }; + Ye.postMessage(n, [e.buffer]); + })) : jt(e), Kl = async (e, t) => { + if (Pt()) { + if (t == null ? void 0 : t.preferredOutputLocation) throw new Error('session option "preferredOutputLocation" is not supported for proxy.'); + return Ht(), new Promise((r, n) => { + Lt("create", [r, n]); + let o = { type: "create", in: { model: e, options: { ...t } } }, i = []; + e instanceof Uint8Array && i.push(e.buffer), Ye.postMessage(o, i); + }); + } else return Pr(e, t); + }, Yl = async (e) => { + if (Pt()) return Ht(), new Promise((t, r) => { + Lt("release", [t, r]); + let n = { type: "release", in: e }; + Ye.postMessage(n); + }); + zr(e); + }, Xl = async (e, t, r, n, o, i) => { + if (Pt()) { + if (r.some((a) => a[3] !== "cpu")) throw new Error("input tensor on GPU is not supported for proxy."); + if (o.some((a) => a)) throw new Error("pre-allocated output tensor is not supported for proxy."); + return Ht(), new Promise((a, d) => { + Lt("run", [a, d]); + let l = r, c = { type: "run", in: { sessionId: e, inputIndices: t, inputs: l, outputIndices: n, options: i } }; + Ye.postMessage(c, Dr(l)); + }); + } else return Or(e, t, r, n, o, i); + }, Zl = async (e) => { + if (Pt()) return Ht(), new Promise((t, r) => { + Lt("end-profiling", [t, r]); + let n = { type: "end-profiling", in: e }; + Ye.postMessage(n); + }); + Br(e); + }; + }); + var Ql, Fh, mn, Jl = V(() => { + "use strict"; + Ke(); + Po(); + Q(); + Cr(); + Yn(); + Ql = (e, t) => { + switch (e.location) { + case "cpu": + return [e.type, e.dims, e.data, "cpu"]; + case "gpu-buffer": + return [e.type, e.dims, { gpuBuffer: e.gpuBuffer }, "gpu-buffer"]; + case "ml-tensor": + return [e.type, e.dims, { mlTensor: e.mlTensor }, "ml-tensor"]; + default: + throw new Error(`invalid data location: ${e.location} for ${t()}`); + } + }, Fh = (e) => { + switch (e[3]) { + case "cpu": + return new De(e[0], e[2], e[1]); + case "gpu-buffer": { + let t = e[0]; + if (!Vr(t)) throw new Error(`not supported data type: ${t} for deserializing GPU tensor`); + let { gpuBuffer: r, download: n, dispose: o } = e[2]; + return De.fromGpuBuffer(r, { dataType: t, dims: e[1], download: n, dispose: o }); + } + case "ml-tensor": { + let t = e[0]; + if (!Nr(t)) throw new Error(`not supported data type: ${t} for deserializing MLTensor tensor`); + let { mlTensor: r, download: n, dispose: o } = e[2]; + return De.fromMLTensor(r, { dataType: t, dims: e[1], download: n, dispose: o }); + } + default: + throw new Error(`invalid data location: ${e[3]}`); + } + }, mn = class { + async fetchModelAndCopyToWasmMemory(t) { + return jl(await Zt(t)); + } + async loadModel(t, r) { + Le(); + let n; + typeof t == "string" ? false ? 0 : n = await this.fetchModelAndCopyToWasmMemory(t) : n = t, [this.sessionId, this.inputNames, this.outputNames] = await Kl(n, r), Ve(); + } + async dispose() { + return Yl(this.sessionId); + } + async run(t, r, n) { + Le(); + let o = [], i = []; + Object.entries(t).forEach((h) => { + let w = h[0], g = h[1], y = this.inputNames.indexOf(w); + if (y === -1) throw new Error(`invalid input '${w}'`); + o.push(g), i.push(y); + }); + let a = [], d = []; + Object.entries(r).forEach((h) => { + let w = h[0], g = h[1], y = this.outputNames.indexOf(w); + if (y === -1) throw new Error(`invalid output '${w}'`); + a.push(g), d.push(y); + }); + let l = o.map((h, w) => Ql(h, () => `input "${this.inputNames[i[w]]}"`)), c = a.map((h, w) => h ? Ql(h, () => `output "${this.outputNames[d[w]]}"`) : null), m = await Xl(this.sessionId, i, l, d, c, n), u = {}; + for (let h = 0; h < m.length; h++) u[this.outputNames[d[h]]] = a[h] ?? Fh(m[h]); + return Ve(), u; + } + startProfiling() { + } + endProfiling() { + Zl(this.sessionId); + } + }; + }); + var tc = {}; + Gt(tc, { OnnxruntimeWebAssemblyBackend: () => fn, initializeFlags: () => ec, wasmBackend: () => qh }); + var ec, fn, qh, rc = V(() => { + "use strict"; + Ke(); + Po(); + Jl(); + qt(); + ec = () => { + if ((typeof _e.wasm.initTimeout != "number" || _e.wasm.initTimeout < 0) && (_e.wasm.initTimeout = 0), _e.wasm.simd === false && console.warn('Deprecated property "env.wasm.simd" is set to false. non-SIMD build is no longer provided, and this setting will be ignored.'), typeof _e.wasm.proxy != "boolean" && (_e.wasm.proxy = false), typeof _e.wasm.trace != "boolean" && (_e.wasm.trace = false), typeof _e.wasm.numThreads != "number" || !Number.isInteger(_e.wasm.numThreads) || _e.wasm.numThreads <= 0) if (typeof self < "u" && !self.crossOriginIsolated) _e.wasm.numThreads = 1; + else { + let e = typeof navigator > "u" ? Vn("node:os").cpus().length : navigator.hardwareConcurrency; + _e.wasm.numThreads = Math.min(4, Math.ceil((e || 1) / 2)); + } + }, fn = class { + async init(t) { + ec(), await Fl(), await ql(t); + } + async createInferenceSessionHandler(t, r) { + let n = new mn(); + return await n.loadModel(t, r), Promise.resolve(n); + } + }, qh = new fn(); + }); + Ke(); + Ke(); + Ke(); + var Ia = "1.20.0-dev.20241016-2b8fc5529b"; + var Hx = Hn; + { + let e = (rc(), br(tc)).wasmBackend; + St("webgpu", e, 5), St("webnn", e, 5), St("cpu", e, 10), St("wasm", e, 10); + } + Object.defineProperty(_e.versions, "web", { value: Ia, enumerable: true }); + } + ), + /***/ + "./src/backends/onnx.js": ( + /*!******************************!*\ + !*** ./src/backends/onnx.js ***! + \******************************/ + /***/ + (__unused_webpack___webpack_module__, __webpack_exports__2, __webpack_require__2) => { + var onnxruntime_node__WEBPACK_IMPORTED_MODULE_1___namespace_cache; + __webpack_require__2.r(__webpack_exports__2); + __webpack_require__2.d(__webpack_exports__2, { + /* harmony export */ + Tensor: () => ( + /* reexport safe */ + onnxruntime_common__WEBPACK_IMPORTED_MODULE_3__.Tensor + ), + /* harmony export */ + createInferenceSession: () => ( + /* binding */ + createInferenceSession + ), + /* harmony export */ + deviceToExecutionProviders: () => ( + /* binding */ + deviceToExecutionProviders + ), + /* harmony export */ + isONNXProxy: () => ( + /* binding */ + isONNXProxy + ), + /* harmony export */ + isONNXTensor: () => ( + /* binding */ + isONNXTensor + ) + /* harmony export */ + }); + var _env_js__WEBPACK_IMPORTED_MODULE_0__2 = __webpack_require__2( + /*! ../env.js */ + "./src/env.js" + ); + var onnxruntime_node__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__2( + /*! onnxruntime-node */ + "?2ce3" + ); + var _onnxruntime_webgpu__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__2( + /*! #onnxruntime-webgpu */ + "./node_modules/onnxruntime-web/dist/ort.webgpu.bundle.min.mjs" + ); + var onnxruntime_common__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__2( + /*! onnxruntime-common */ + "./node_modules/onnxruntime-common/dist/esm/index.js" + ); + const DEVICE_TO_EXECUTION_PROVIDER_MAPPING = Object.freeze({ + auto: null, + // Auto-detect based on device and environment + gpu: null, + // Auto-detect GPU + cpu: "cpu", + // CPU + wasm: "wasm", + // WebAssembly + webgpu: "webgpu", + // WebGPU + cuda: "cuda", + // CUDA + dml: "dml", + // DirectML + webnn: { name: "webnn", deviceType: "cpu" }, + // WebNN (default) + "webnn-npu": { name: "webnn", deviceType: "npu" }, + // WebNN NPU + "webnn-gpu": { name: "webnn", deviceType: "gpu" }, + // WebNN GPU + "webnn-cpu": { name: "webnn", deviceType: "cpu" } + // WebNN CPU + }); + const supportedDevices = []; + let defaultDevices; + let ONNX; + const ORT_SYMBOL = Symbol.for("onnxruntime"); + if (ORT_SYMBOL in globalThis) { + ONNX = globalThis[ORT_SYMBOL]; + } else if (_env_js__WEBPACK_IMPORTED_MODULE_0__2.apis.IS_NODE_ENV) { + ONNX = onnxruntime_node__WEBPACK_IMPORTED_MODULE_1__ ?? (onnxruntime_node__WEBPACK_IMPORTED_MODULE_1___namespace_cache || (onnxruntime_node__WEBPACK_IMPORTED_MODULE_1___namespace_cache = __webpack_require__2.t(onnxruntime_node__WEBPACK_IMPORTED_MODULE_1__, 2))); + switch (process.platform) { + case "win32": + supportedDevices.push("dml"); + break; + case "linux": + if (process.arch === "x64") { + supportedDevices.push("cuda"); + } + break; + case "darwin": + break; + } + supportedDevices.push("cpu"); + defaultDevices = ["cpu"]; + } else { + ONNX = _onnxruntime_webgpu__WEBPACK_IMPORTED_MODULE_2__; + if (_env_js__WEBPACK_IMPORTED_MODULE_0__2.apis.IS_WEBNN_AVAILABLE) { + supportedDevices.push("webnn-npu", "webnn-gpu", "webnn-cpu", "webnn"); + } + if (_env_js__WEBPACK_IMPORTED_MODULE_0__2.apis.IS_WEBGPU_AVAILABLE) { + supportedDevices.push("webgpu"); + } + supportedDevices.push("wasm"); + defaultDevices = ["wasm"]; + } + const InferenceSession = ONNX.InferenceSession; + function deviceToExecutionProviders(device = null) { + if (!device) return defaultDevices; + switch (device) { + case "auto": + return supportedDevices; + case "gpu": + return supportedDevices.filter( + (x) => ["webgpu", "cuda", "dml", "webnn-gpu"].includes(x) + ); + } + if (supportedDevices.includes(device)) { + return [DEVICE_TO_EXECUTION_PROVIDER_MAPPING[device] ?? device]; + } + throw new Error(`Unsupported device: "${device}". Should be one of: ${supportedDevices.join(", ")}.`); + } + let wasmInitPromise = null; + async function createInferenceSession(buffer, session_options, session_config) { + if (wasmInitPromise) { + await wasmInitPromise; + } + const sessionPromise = InferenceSession.create(buffer, session_options); + wasmInitPromise ?? (wasmInitPromise = sessionPromise); + const session = await sessionPromise; + session.config = session_config; + return session; + } + function isONNXTensor(x) { + return x instanceof ONNX.Tensor; + } + const ONNX_ENV = ONNX == null ? void 0 : ONNX.env; + if (ONNX_ENV == null ? void 0 : ONNX_ENV.wasm) { + ONNX_ENV.wasm.wasmPaths = `https://cdn.jsdelivr.net/npm/@huggingface/transformers@${_env_js__WEBPACK_IMPORTED_MODULE_0__2.env.version}/dist/`; + ONNX_ENV.wasm.proxy = false; + if (typeof crossOriginIsolated === "undefined" || !crossOriginIsolated) { + ONNX_ENV.wasm.numThreads = 1; + } + } + if (ONNX_ENV == null ? void 0 : ONNX_ENV.webgpu) { + ONNX_ENV.webgpu.powerPreference = "high-performance"; + } + function isONNXProxy() { + var _a; + return (_a = ONNX_ENV == null ? void 0 : ONNX_ENV.wasm) == null ? void 0 : _a.proxy; + } + _env_js__WEBPACK_IMPORTED_MODULE_0__2.env.backends.onnx = ONNX_ENV; + } + ), + /***/ + "./src/configs.js": ( + /*!************************!*\ + !*** ./src/configs.js ***! + \************************/ + /***/ + (__unused_webpack___webpack_module__, __webpack_exports__2, __webpack_require__2) => { + __webpack_require__2.r(__webpack_exports__2); + __webpack_require__2.d(__webpack_exports__2, { + /* harmony export */ + AutoConfig: () => ( + /* binding */ + AutoConfig + ), + /* harmony export */ + PretrainedConfig: () => ( + /* binding */ + PretrainedConfig + ), + /* harmony export */ + getKeyValueShapes: () => ( + /* binding */ + getKeyValueShapes + ) + /* harmony export */ + }); + var _utils_core_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__2( + /*! ./utils/core.js */ + "./src/utils/core.js" + ); + var _utils_hub_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__2( + /*! ./utils/hub.js */ + "./src/utils/hub.js" + ); + async function loadConfig(pretrained_model_name_or_path, options) { + return await (0, _utils_hub_js__WEBPACK_IMPORTED_MODULE_1__.getModelJSON)(pretrained_model_name_or_path, "config.json", true, options); + } + function getNormalizedConfig(config) { + const mapping = {}; + let init_normalized_config = {}; + switch (config.model_type) { + case "llava": + case "paligemma": + case "florence2": + init_normalized_config = getNormalizedConfig(config.text_config); + break; + case "moondream1": + init_normalized_config = getNormalizedConfig(config.phi_config); + break; + case "musicgen": + init_normalized_config = getNormalizedConfig(config.decoder); + break; + case "gpt2": + case "gptj": + case "jais": + case "codegen": + case "gpt_bigcode": + mapping["num_heads"] = "n_head"; + mapping["num_layers"] = "n_layer"; + mapping["hidden_size"] = "n_embd"; + break; + case "gpt_neox": + case "stablelm": + case "opt": + case "phi": + case "phi3": + case "falcon": + mapping["num_heads"] = "num_attention_heads"; + mapping["num_layers"] = "num_hidden_layers"; + mapping["hidden_size"] = "hidden_size"; + break; + case "llama": + case "granite": + case "cohere": + case "mistral": + case "starcoder2": + case "qwen2": + mapping["num_heads"] = "num_key_value_heads"; + mapping["num_layers"] = "num_hidden_layers"; + mapping["hidden_size"] = "hidden_size"; + mapping["num_attention_heads"] = "num_attention_heads"; + break; + case "gemma": + case "gemma2": + mapping["num_heads"] = "num_key_value_heads"; + mapping["num_layers"] = "num_hidden_layers"; + mapping["dim_kv"] = "head_dim"; + break; + case "openelm": + mapping["num_heads"] = "num_kv_heads"; + mapping["num_layers"] = "num_transformer_layers"; + mapping["dim_kv"] = "head_dim"; + break; + case "gpt_neo": + case "donut-swin": + mapping["num_heads"] = "num_heads"; + mapping["num_layers"] = "num_layers"; + mapping["hidden_size"] = "hidden_size"; + break; + case "bloom": + mapping["num_heads"] = "n_head"; + mapping["num_layers"] = "n_layer"; + mapping["hidden_size"] = "hidden_size"; + break; + case "mpt": + mapping["num_heads"] = "n_heads"; + mapping["num_layers"] = "n_layers"; + mapping["hidden_size"] = "d_model"; + break; + case "t5": + case "mt5": + case "longt5": + mapping["num_decoder_layers"] = "num_decoder_layers"; + mapping["num_decoder_heads"] = "num_heads"; + mapping["decoder_dim_kv"] = "d_kv"; + mapping["num_encoder_layers"] = "num_layers"; + mapping["num_encoder_heads"] = "num_heads"; + mapping["encoder_dim_kv"] = "d_kv"; + break; + case "bart": + case "mbart": + case "marian": + case "whisper": + case "m2m_100": + case "blenderbot": + case "blenderbot-small": + case "florence2_language": + mapping["num_decoder_layers"] = "decoder_layers"; + mapping["num_decoder_heads"] = "decoder_attention_heads"; + mapping["decoder_hidden_size"] = "d_model"; + mapping["num_encoder_layers"] = "encoder_layers"; + mapping["num_encoder_heads"] = "encoder_attention_heads"; + mapping["encoder_hidden_size"] = "d_model"; + break; + case "speecht5": + mapping["num_decoder_layers"] = "decoder_layers"; + mapping["num_decoder_heads"] = "decoder_attention_heads"; + mapping["decoder_hidden_size"] = "hidden_size"; + mapping["num_encoder_layers"] = "encoder_layers"; + mapping["num_encoder_heads"] = "encoder_attention_heads"; + mapping["encoder_hidden_size"] = "hidden_size"; + break; + case "trocr": + mapping["num_encoder_layers"] = mapping["num_decoder_layers"] = "decoder_layers"; + mapping["num_encoder_heads"] = mapping["num_decoder_heads"] = "decoder_attention_heads"; + mapping["encoder_hidden_size"] = mapping["decoder_hidden_size"] = "d_model"; + break; + case "musicgen_decoder": + mapping["num_encoder_layers"] = mapping["num_decoder_layers"] = "num_hidden_layers"; + mapping["num_encoder_heads"] = mapping["num_decoder_heads"] = "num_attention_heads"; + mapping["encoder_hidden_size"] = mapping["decoder_hidden_size"] = "hidden_size"; + break; + case "vision-encoder-decoder": + const decoderConfig = getNormalizedConfig(config.decoder); + const add_encoder_pkv = "num_decoder_layers" in decoderConfig; + const result = (0, _utils_core_js__WEBPACK_IMPORTED_MODULE_0__.pick)(config, ["model_type", "is_encoder_decoder"]); + if (add_encoder_pkv) { + result.num_decoder_layers = decoderConfig.num_decoder_layers; + result.num_decoder_heads = decoderConfig.num_decoder_heads; + result.decoder_hidden_size = decoderConfig.decoder_hidden_size; + result.num_encoder_layers = decoderConfig.num_encoder_layers; + result.num_encoder_heads = decoderConfig.num_encoder_heads; + result.encoder_hidden_size = decoderConfig.encoder_hidden_size; + } else { + result.num_layers = decoderConfig.num_layers; + result.num_heads = decoderConfig.num_heads; + result.hidden_size = decoderConfig.hidden_size; + } + return result; + } + const normalized_config = { + ...init_normalized_config, + ...(0, _utils_core_js__WEBPACK_IMPORTED_MODULE_0__.pick)(config, ["model_type", "multi_query", "is_encoder_decoder"]) + }; + for (const key in mapping) { + normalized_config[key] = config[mapping[key]]; + } + return normalized_config; + } + function getKeyValueShapes(config, { + prefix = "past_key_values" + } = {}) { + const decoderFeeds = {}; + const normalized_config = config.normalized_config; + const batch_size = 1; + if (normalized_config.is_encoder_decoder && ("num_encoder_heads" in normalized_config && "num_decoder_heads" in normalized_config)) { + const encoder_dim_kv = normalized_config.encoder_dim_kv ?? normalized_config.encoder_hidden_size / normalized_config.num_encoder_heads; + const decoder_dim_kv = normalized_config.decoder_dim_kv ?? normalized_config.decoder_hidden_size / normalized_config.num_decoder_heads; + const encoder_dims = [batch_size, normalized_config.num_encoder_heads, 0, encoder_dim_kv]; + const decoder_dims = [batch_size, normalized_config.num_decoder_heads, 0, decoder_dim_kv]; + for (let i = 0; i < normalized_config.num_decoder_layers; ++i) { + decoderFeeds[`${prefix}.${i}.encoder.key`] = encoder_dims; + decoderFeeds[`${prefix}.${i}.encoder.value`] = encoder_dims; + decoderFeeds[`${prefix}.${i}.decoder.key`] = decoder_dims; + decoderFeeds[`${prefix}.${i}.decoder.value`] = decoder_dims; + } + } else { + const num_heads = normalized_config.num_heads; + const num_layers = normalized_config.num_layers; + const dim_kv = normalized_config.dim_kv ?? normalized_config.hidden_size / (normalized_config.num_attention_heads ?? num_heads); + if (normalized_config.model_type === "falcon") { + const dims = [batch_size * num_heads, 0, dim_kv]; + for (let i = 0; i < num_layers; ++i) { + decoderFeeds[`${prefix}.${i}.key`] = dims; + decoderFeeds[`${prefix}.${i}.value`] = dims; + } + } else if (normalized_config.multi_query) { + const dims = [batch_size * num_heads, 0, 2 * dim_kv]; + for (let i = 0; i < num_layers; ++i) { + decoderFeeds[`${prefix}.${i}.key_value`] = dims; + } + } else if (normalized_config.model_type === "bloom") { + const keyDims = [batch_size * num_heads, dim_kv, 0]; + const valueDims = [batch_size * num_heads, 0, dim_kv]; + for (let i = 0; i < num_layers; ++i) { + decoderFeeds[`${prefix}.${i}.key`] = keyDims; + decoderFeeds[`${prefix}.${i}.value`] = valueDims; + } + } else if (normalized_config.model_type === "openelm") { + for (let i = 0; i < num_layers; ++i) { + const dims = [batch_size, num_heads[i], 0, dim_kv]; + decoderFeeds[`${prefix}.${i}.key`] = dims; + decoderFeeds[`${prefix}.${i}.value`] = dims; + } + } else { + const dims = [batch_size, num_heads, 0, dim_kv]; + for (let i = 0; i < num_layers; ++i) { + decoderFeeds[`${prefix}.${i}.key`] = dims; + decoderFeeds[`${prefix}.${i}.value`] = dims; + } + } + } + return decoderFeeds; + } + class PretrainedConfig { + /** + * Create a new PreTrainedTokenizer instance. + * @param {Object} configJSON The JSON of the config. + */ + constructor(configJSON) { + // NOTE: Typo in original + /** @type {string|null} */ + __publicField(this, "model_type", null); + /** @type {boolean} */ + __publicField(this, "is_encoder_decoder", false); + /** @type {number} */ + __publicField(this, "max_position_embeddings"); + /** @type {TransformersJSConfig} */ + __publicField(this, "transformers.js_config"); + Object.assign(this, configJSON); + this.normalized_config = getNormalizedConfig(this); + } + /** + * Loads a pre-trained config from the given `pretrained_model_name_or_path`. + * + * @param {string} pretrained_model_name_or_path The path to the pre-trained config. + * @param {PretrainedOptions} options Additional options for loading the config. + * @throws {Error} Throws an error if the config.json is not found in the `pretrained_model_name_or_path`. + * + * @returns {Promise} A new instance of the `PretrainedConfig` class. + */ + static async from_pretrained(pretrained_model_name_or_path, { + progress_callback = null, + config = null, + cache_dir = null, + local_files_only = false, + revision = "main" + } = {}) { + if (config && !(config instanceof PretrainedConfig)) { + config = new PretrainedConfig(config); + } + const data = config ?? await loadConfig(pretrained_model_name_or_path, { + progress_callback, + config, + cache_dir, + local_files_only, + revision + }); + return new this(data); + } + } + class AutoConfig { + /** @type {typeof PretrainedConfig.from_pretrained} */ + static async from_pretrained(...args) { + return PretrainedConfig.from_pretrained(...args); + } + } + } + ), + /***/ + "./src/env.js": ( + /*!********************!*\ + !*** ./src/env.js ***! + \********************/ + /***/ + (__unused_webpack___webpack_module__, __webpack_exports__2, __webpack_require__2) => { + var _a; + __webpack_require__2.r(__webpack_exports__2); + __webpack_require__2.d(__webpack_exports__2, { + /* harmony export */ + apis: () => ( + /* binding */ + apis + ), + /* harmony export */ + env: () => ( + /* binding */ + env + ) + /* harmony export */ + }); + var fs__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__2( + /*! fs */ + "?569f" + ); + var path__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__2( + /*! path */ + "?3f59" + ); + var url__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__2( + /*! url */ + "?154a" + ); + const VERSION = "3.0.0"; + const IS_BROWSER_ENV = typeof self !== "undefined"; + const IS_WEBWORKER_ENV = IS_BROWSER_ENV && self.constructor.name === "DedicatedWorkerGlobalScope"; + const IS_WEB_CACHE_AVAILABLE = IS_BROWSER_ENV && "caches" in self; + const IS_WEBGPU_AVAILABLE = typeof navigator !== "undefined" && "gpu" in navigator; + const IS_WEBNN_AVAILABLE = typeof navigator !== "undefined" && "ml" in navigator; + const IS_PROCESS_AVAILABLE = typeof process !== "undefined"; + const IS_NODE_ENV = IS_PROCESS_AVAILABLE && ((_a = process == null ? void 0 : process.release) == null ? void 0 : _a.name) === "node"; + const IS_FS_AVAILABLE = !isEmpty(fs__WEBPACK_IMPORTED_MODULE_0__); + const IS_PATH_AVAILABLE = !isEmpty(path__WEBPACK_IMPORTED_MODULE_1__); + const apis = Object.freeze({ + /** Whether we are running in a browser environment */ + IS_BROWSER_ENV, + /** Whether we are running in a web worker environment */ + IS_WEBWORKER_ENV, + /** Whether the Cache API is available */ + IS_WEB_CACHE_AVAILABLE, + /** Whether the WebGPU API is available */ + IS_WEBGPU_AVAILABLE, + /** Whether the WebNN API is available */ + IS_WEBNN_AVAILABLE, + /** Whether the Node.js process API is available */ + IS_PROCESS_AVAILABLE, + /** Whether we are running in a Node.js environment */ + IS_NODE_ENV, + /** Whether the filesystem API is available */ + IS_FS_AVAILABLE, + /** Whether the path API is available */ + IS_PATH_AVAILABLE + }); + const RUNNING_LOCALLY = IS_FS_AVAILABLE && IS_PATH_AVAILABLE; + const dirname__ = RUNNING_LOCALLY ? path__WEBPACK_IMPORTED_MODULE_1__.dirname(path__WEBPACK_IMPORTED_MODULE_1__.dirname(url__WEBPACK_IMPORTED_MODULE_2__.fileURLToPath(META_URL))) : "./"; + const DEFAULT_CACHE_DIR = RUNNING_LOCALLY ? path__WEBPACK_IMPORTED_MODULE_1__.join(dirname__, "/.cache/") : null; + const DEFAULT_LOCAL_MODEL_PATH = "/models/"; + const localModelPath = RUNNING_LOCALLY ? path__WEBPACK_IMPORTED_MODULE_1__.join(dirname__, DEFAULT_LOCAL_MODEL_PATH) : DEFAULT_LOCAL_MODEL_PATH; + const env = { + version: VERSION, + /////////////////// Backends settings /////////////////// + // NOTE: These will be populated later by the backends themselves. + backends: { + // onnxruntime-web/onnxruntime-node + onnx: {} + }, + /////////////////// Model settings /////////////////// + allowRemoteModels: true, + remoteHost: "https://huggingface.co/", + remotePathTemplate: "{model}/resolve/{revision}/", + allowLocalModels: !IS_BROWSER_ENV, + localModelPath, + useFS: IS_FS_AVAILABLE, + /////////////////// Cache settings /////////////////// + useBrowserCache: IS_WEB_CACHE_AVAILABLE, + useFSCache: IS_FS_AVAILABLE, + cacheDir: DEFAULT_CACHE_DIR, + useCustomCache: false, + customCache: null + ////////////////////////////////////////////////////// + }; + function isEmpty(obj) { + return Object.keys(obj).length === 0; + } + } + ), + /***/ + "./src/generation/configuration_utils.js": ( + /*!***********************************************!*\ + !*** ./src/generation/configuration_utils.js ***! + \***********************************************/ + /***/ + (__unused_webpack___webpack_module__, __webpack_exports__2, __webpack_require__2) => { + __webpack_require__2.r(__webpack_exports__2); + __webpack_require__2.d(__webpack_exports__2, { + /* harmony export */ + GenerationConfig: () => ( + /* binding */ + GenerationConfig + ) + /* harmony export */ + }); + var _utils_core_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__2( + /*! ../utils/core.js */ + "./src/utils/core.js" + ); + class GenerationConfig { + /** + * + * @param {GenerationConfig|import('../configs.js').PretrainedConfig} config + */ + constructor(config) { + // Parameters that control the length of the output + /** + * The maximum length the generated tokens can have. + * Corresponds to the length of the input prompt + `max_new_tokens`. + * Its effect is overridden by `max_new_tokens`, if also set. + * @type {number} + * @default 20 + */ + __publicField(this, "max_length", 20); + /** + * The maximum numbers of tokens to generate, ignoring the number of tokens in the prompt. + * @type {number} + * @default null + */ + __publicField(this, "max_new_tokens", null); + /** + * The minimum length of the sequence to be generated. + * Corresponds to the length of the input prompt + `min_new_tokens`. + * Its effect is overridden by `min_new_tokens`, if also set. + * @type {number} + * @default 0 + */ + __publicField(this, "min_length", 0); + /** + * The minimum numbers of tokens to generate, ignoring the number of tokens in the prompt. + * @type {number} + * @default null + */ + __publicField(this, "min_new_tokens", null); + /** + * Controls the stopping condition for beam-based methods, like beam-search. It accepts the following values: + * - `true`, where the generation stops as soon as there are `num_beams` complete candidates; + * - `false`, where an heuristic is applied and the generation stops when is it very unlikely to find better candidates; + * - `"never"`, where the beam search procedure only stops when there cannot be better candidates (canonical beam search algorithm). + * @type {boolean|"never"} + * @default false + */ + __publicField(this, "early_stopping", false); + /** + * The maximum amount of time you allow the computation to run for in seconds. + * Generation will still finish the current pass after allocated time has been passed. + * @type {number} + * @default null + */ + __publicField(this, "max_time", null); + // Parameters that control the generation strategy used + /** + * Whether or not to use sampling; use greedy decoding otherwise. + * @type {boolean} + * @default false + */ + __publicField(this, "do_sample", false); + /** + * Number of beams for beam search. 1 means no beam search. + * @type {number} + * @default 1 + */ + __publicField(this, "num_beams", 1); + /** + * Number of groups to divide `num_beams` into in order to ensure diversity among different groups of beams. + * See [this paper](https://arxiv.org/pdf/1610.02424.pdf) for more details. + * @type {number} + * @default 1 + */ + __publicField(this, "num_beam_groups", 1); + /** + * The values balance the model confidence and the degeneration penalty in contrastive search decoding. + * @type {number} + * @default null + */ + __publicField(this, "penalty_alpha", null); + /** + * Whether or not the model should use the past last key/values attentions (if applicable to the model) to speed up decoding. + * @type {boolean} + * @default true + */ + __publicField(this, "use_cache", true); + // Parameters for manipulation of the model output logits + /** + * The value used to modulate the next token probabilities. + * @type {number} + * @default 1.0 + */ + __publicField(this, "temperature", 1); + /** + * The number of highest probability vocabulary tokens to keep for top-k-filtering. + * @type {number} + * @default 50 + */ + __publicField(this, "top_k", 50); + /** + * If set to float < 1, only the smallest set of most probable tokens with probabilities that add up to `top_p` or higher are kept for generation. + * @type {number} + * @default 1.0 + */ + __publicField(this, "top_p", 1); + /** + * Local typicality measures how similar the conditional probability of predicting a target token next is to the expected conditional probability of predicting a random token next, given the partial text already generated. + * If set to float < 1, the smallest set of the most locally typical tokens with probabilities that add up to `typical_p` or higher are kept for generation. + * See [this paper](https://arxiv.org/pdf/2202.00666.pdf) for more details. + * @type {number} + * @default 1.0 + */ + __publicField(this, "typical_p", 1); + /** + * If set to float strictly between 0 and 1, only tokens with a conditional probability greater than `epsilon_cutoff` will be sampled. + * In the paper, suggested values range from 3e-4 to 9e-4, depending on the size of the model. + * See [Truncation Sampling as Language Model Desmoothing](https://arxiv.org/abs/2210.15191) for more details. + * @type {number} + * @default 0.0 + */ + __publicField(this, "epsilon_cutoff", 0); + /** + * Eta sampling is a hybrid of locally typical sampling and epsilon sampling. + * If set to float strictly between 0 and 1, a token is only considered if it is greater than either `eta_cutoff` or `sqrt(eta_cutoff) * exp(-entropy(softmax(next_token_logits)))`. + * The latter term is intuitively the expected next token probability, scaled by `sqrt(eta_cutoff)`. In the paper, suggested values range from 3e-4 to 2e-3, depending on the size of the model. + * See [Truncation Sampling as Language Model Desmoothing](https://arxiv.org/abs/2210.15191) for more details. + * @type {number} + * @default 0.0 + */ + __publicField(this, "eta_cutoff", 0); + /** + * This value is subtracted from a beam's score if it generates a token same as any beam from other group at a particular time. + * Note that `diversity_penalty` is only effective if `group beam search` is enabled. + * @type {number} + * @default 0.0 + */ + __publicField(this, "diversity_penalty", 0); + /** + * The parameter for repetition penalty. 1.0 means no penalty. + * See [this paper](https://arxiv.org/pdf/1909.05858.pdf) for more details. + * @type {number} + * @default 1.0 + */ + __publicField(this, "repetition_penalty", 1); + /** + * The paramater for encoder_repetition_penalty. + * An exponential penalty on sequences that are not in the original input. + * 1.0 means no penalty. + * @type {number} + * @default 1.0 + */ + __publicField(this, "encoder_repetition_penalty", 1); + /** + * Exponential penalty to the length that is used with beam-based generation. + * It is applied as an exponent to the sequence length, which in turn is used to divide the score of the sequence. + * Since the score is the log likelihood of the sequence (i.e. negative), `length_penalty` > 0.0 promotes longer sequences, while `length_penalty` < 0.0 encourages shorter sequences. + * @type {number} + * @default 1.0 + */ + __publicField(this, "length_penalty", 1); + /** + * If set to int > 0, all ngrams of that size can only occur once. + * @type {number} + * @default 0 + */ + __publicField(this, "no_repeat_ngram_size", 0); + /** + * List of token ids that are not allowed to be generated. + * In order to get the token ids of the words that should not appear in the generated text, use + * `tokenizer(bad_words, { add_prefix_space: true, add_special_tokens: false }).input_ids`. + * @type {number[][]} + * @default null + */ + __publicField(this, "bad_words_ids", null); + /** + * List of token ids that must be generated. + * If given a `number[][]`, this is treated as a simple list of words that must be included, the opposite to `bad_words_ids`. + * If given `number[][][]`, this triggers a [disjunctive constraint](https://github.com/huggingface/transformers/issues/14081), where one can allow different forms of each word. + * @type {number[][]|number[][][]} + * @default null + */ + __publicField(this, "force_words_ids", null); + /** + * Whether to renormalize the logits after applying all the logits processors or warpers (including the custom ones). + * It's highly recommended to set this flag to `true` as the search algorithms suppose the score logits are normalized but some logit processors or warpers break the normalization. + * @type {boolean} + * @default false + */ + __publicField(this, "renormalize_logits", false); + /** + * Custom constraints that can be added to the generation to ensure that the output will contain the use of certain tokens as defined by `Constraint` objects, in the most sensible way possible. + * @type {Object[]} + * @default null + */ + __publicField(this, "constraints", null); + /** + * The id of the token to force as the first generated token after the `decoder_start_token_id`. + * Useful for multilingual models like mBART where the first generated token needs to be the target language token. + * @type {number} + * @default null + */ + __publicField(this, "forced_bos_token_id", null); + /** + * The id of the token to force as the last generated token when `max_length` is reached. + * Optionally, use a list to set multiple *end-of-sequence* tokens. + * @type {number|number[]} + * @default null + */ + __publicField(this, "forced_eos_token_id", null); + /** + * Whether to remove possible *nan* and *inf* outputs of the model to prevent the generation method to crash. Note that using `remove_invalid_values` can slow down generation. + * @type {boolean} + */ + __publicField(this, "remove_invalid_values", false); + /** + * This Tuple adds an exponentially increasing length penalty, after a certain amount of tokens have been generated. + * The tuple shall consist of: `(start_index, decay_factor)` where `start_index` indicates where penalty starts and `decay_factor` represents the factor of exponential decay. + * @type {[number, number]} + * @default null + */ + __publicField(this, "exponential_decay_length_penalty", null); + /** + * A list of tokens that will be suppressed at generation. + * The `SuppressTokens` logit processor will set their log probs to `-inf` so that they are not sampled. + * @type {number[]} + * @default null + */ + __publicField(this, "suppress_tokens", null); + /** + * A list of tokens that will be suppressed at the beginning of the generation. + * The `SuppressBeginTokens` logit processor will set their log probs to `-inf` so that they are not sampled. + * @type {number[]} + * @default null + */ + __publicField(this, "begin_suppress_tokens", null); + /** + * A list of pairs of integers which indicates a mapping from generation indices to token indices that will be forced before sampling. + * For example, `[[1, 123]]` means the second generated token will always be a token of index 123. + * @type {[number, number][]} + * @default null + */ + __publicField(this, "forced_decoder_ids", null); + /** + * The guidance scale for classifier free guidance (CFG). CFG is enabled by setting `guidance_scale > 1`. + * Higher guidance scale encourages the model to generate samples that are more closely linked to the input + * prompt, usually at the expense of poorer quality. + * @type {number} + * @default null + */ + __publicField(this, "guidance_scale", null); + // Parameters that define the output variables of `generate` + /** + * The number of independently computed returned sequences for each element in the batch. + * @type {number} + * @default 1 + */ + __publicField(this, "num_return_sequences", 1); + /** + * Whether or not to return the attentions tensors of all attention layers. + * See `attentions` under returned tensors for more details. + * @type {boolean} + * @default false + */ + __publicField(this, "output_attentions", false); + /** + * Whether or not to return the hidden states of all layers. + * See `hidden_states` under returned tensors for more details. + * @type {boolean} + * @default false + */ + __publicField(this, "output_hidden_states", false); + /** + * Whether or not to return the prediction scores. + * See `scores` under returned tensors for more details. + * @type {boolean} + * @default false + */ + __publicField(this, "output_scores", false); + /** + * Whether or not to return a `ModelOutput` instead of a plain tuple. + * @type {boolean} + * @default false + */ + __publicField(this, "return_dict_in_generate", false); + // Special tokens that can be used at generation time + /** + * The id of the *padding* token. + * @type {number} + * @default null + */ + __publicField(this, "pad_token_id", null); + /** + * The id of the *beginning-of-sequence* token. + * @type {number} + * @default null + */ + __publicField(this, "bos_token_id", null); + /** + * The id of the *end-of-sequence* token. + * Optionally, use a list to set multiple *end-of-sequence* tokens. + * @type {number|number[]} + * @default null + */ + __publicField(this, "eos_token_id", null); + // Generation parameters exclusive to encoder-decoder models + /** + * If set to int > 0, all ngrams of that size that occur in the `encoder_input_ids` cannot occur in the `decoder_input_ids`. + * @type {number} + * @default 0 + */ + __publicField(this, "encoder_no_repeat_ngram_size", 0); + /** + * If an encoder-decoder model starts decoding with a different token than *bos*, the id of that token. + * @type {number} + * @default null + */ + __publicField(this, "decoder_start_token_id", null); + // Wild card + /** + * Additional generation kwargs will be forwarded to the `generate` function of the model. + * Kwargs that are not present in `generate`'s signature will be used in the model forward pass. + * @type {Object} + * @default {} + */ + __publicField(this, "generation_kwargs", {}); + Object.assign(this, (0, _utils_core_js__WEBPACK_IMPORTED_MODULE_0__.pick)(config, Object.getOwnPropertyNames(this))); + } + } + } + ), + /***/ + "./src/generation/logits_process.js": ( + /*!******************************************!*\ + !*** ./src/generation/logits_process.js ***! + \******************************************/ + /***/ + (__unused_webpack___webpack_module__, __webpack_exports__2, __webpack_require__2) => { + __webpack_require__2.r(__webpack_exports__2); + __webpack_require__2.d(__webpack_exports__2, { + /* harmony export */ + ClassifierFreeGuidanceLogitsProcessor: () => ( + /* binding */ + ClassifierFreeGuidanceLogitsProcessor + ), + /* harmony export */ + ForcedBOSTokenLogitsProcessor: () => ( + /* binding */ + ForcedBOSTokenLogitsProcessor + ), + /* harmony export */ + ForcedEOSTokenLogitsProcessor: () => ( + /* binding */ + ForcedEOSTokenLogitsProcessor + ), + /* harmony export */ + LogitsProcessor: () => ( + /* binding */ + LogitsProcessor + ), + /* harmony export */ + LogitsProcessorList: () => ( + /* binding */ + LogitsProcessorList + ), + /* harmony export */ + LogitsWarper: () => ( + /* binding */ + LogitsWarper + ), + /* harmony export */ + MinLengthLogitsProcessor: () => ( + /* binding */ + MinLengthLogitsProcessor + ), + /* harmony export */ + MinNewTokensLengthLogitsProcessor: () => ( + /* binding */ + MinNewTokensLengthLogitsProcessor + ), + /* harmony export */ + NoBadWordsLogitsProcessor: () => ( + /* binding */ + NoBadWordsLogitsProcessor + ), + /* harmony export */ + NoRepeatNGramLogitsProcessor: () => ( + /* binding */ + NoRepeatNGramLogitsProcessor + ), + /* harmony export */ + RepetitionPenaltyLogitsProcessor: () => ( + /* binding */ + RepetitionPenaltyLogitsProcessor + ), + /* harmony export */ + SuppressTokensAtBeginLogitsProcessor: () => ( + /* binding */ + SuppressTokensAtBeginLogitsProcessor + ), + /* harmony export */ + TemperatureLogitsWarper: () => ( + /* binding */ + TemperatureLogitsWarper + ), + /* harmony export */ + TopKLogitsWarper: () => ( + /* binding */ + TopKLogitsWarper + ), + /* harmony export */ + TopPLogitsWarper: () => ( + /* binding */ + TopPLogitsWarper + ), + /* harmony export */ + WhisperTimeStampLogitsProcessor: () => ( + /* binding */ + WhisperTimeStampLogitsProcessor + ) + /* harmony export */ + }); + var _utils_generic_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__2( + /*! ../utils/generic.js */ + "./src/utils/generic.js" + ); + var _utils_tensor_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__2( + /*! ../utils/tensor.js */ + "./src/utils/tensor.js" + ); + var _utils_maths_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__2( + /*! ../utils/maths.js */ + "./src/utils/maths.js" + ); + class LogitsProcessor extends _utils_generic_js__WEBPACK_IMPORTED_MODULE_0__.Callable { + /** + * Apply the processor to the input logits. + * + * @abstract + * @param {bigint[][]} input_ids The input ids. + * @param {Tensor} logits The logits to process. + * @throws {Error} Throws an error if `_call` is not implemented in the subclass. + */ + _call(input_ids, logits) { + throw Error("`_call` should be implemented in a subclass"); + } + } + class LogitsWarper extends _utils_generic_js__WEBPACK_IMPORTED_MODULE_0__.Callable { + /** + * Apply the processor to the input logits. + * + * @abstract + * @param {bigint[][]} input_ids The input ids. + * @param {Tensor} logits The logits to process. + * @throws {Error} Throws an error if `_call` is not implemented in the subclass. + */ + _call(input_ids, logits) { + throw Error("`_call` should be implemented in a subclass"); + } + } + class LogitsProcessorList extends _utils_generic_js__WEBPACK_IMPORTED_MODULE_0__.Callable { + /** + * Constructs a new instance of `LogitsProcessorList`. + */ + constructor() { + super(); + this.processors = []; + } + /** + * Adds a new logits processor to the list. + * + * @param {LogitsProcessor} item The logits processor function to add. + */ + push(item) { + this.processors.push(item); + } + /** + * Adds multiple logits processors to the list. + * + * @param {LogitsProcessor[]} items The logits processor functions to add. + */ + extend(items) { + this.processors.push(...items); + } + /** + * Applies all logits processors in the list to a batch of logits, modifying them in-place. + * + * @param {bigint[][]} input_ids The input IDs for the language model. + * @param {Tensor} logits + */ + _call(input_ids, logits) { + let toReturn = logits; + for (const processor of this.processors) { + toReturn = processor(input_ids, toReturn); + } + return toReturn; + } + [Symbol.iterator]() { + return this.processors.values(); + } + } + class ForcedBOSTokenLogitsProcessor extends LogitsProcessor { + /** + * Create a ForcedBOSTokenLogitsProcessor. + * @param {number} bos_token_id The ID of the beginning-of-sequence token to be forced. + */ + constructor(bos_token_id) { + super(); + this.bos_token_id = bos_token_id; + } + /** + * Apply the BOS token forcing to the logits. + * @param {bigint[][]} input_ids The input IDs. + * @param {Tensor} logits The logits. + * @returns {Object} The logits with BOS token forcing. + */ + _call(input_ids, logits) { + for (let i = 0; i < input_ids.length; ++i) { + if (input_ids[i].length === 1) { + const batch_logits_data = ( + /** @type {Float32Array} */ + logits[i].data + ); + batch_logits_data.fill(-Infinity); + batch_logits_data[this.bos_token_id] = 0; + } + } + return logits; + } + } + class ForcedEOSTokenLogitsProcessor extends LogitsProcessor { + /** + * Create a ForcedEOSTokenLogitsProcessor. + * @param {number} max_length The maximum length of the sequence to be generated. + * @param {number|number[]} eos_token_id The id(s) of the *end-of-sequence* token. + */ + constructor(max_length, eos_token_id) { + super(); + this.max_length = max_length; + this.eos_token_id = Array.isArray(eos_token_id) ? eos_token_id : [eos_token_id]; + } + /** + * Apply the processor to input_ids and logits. + * + * @param {bigint[][]} input_ids The input ids. + * @param {Tensor} logits The logits tensor. + */ + _call(input_ids, logits) { + for (let i = 0; i < input_ids.length; ++i) { + if (input_ids[i].length === this.max_length - 1) { + const batch_logits_data = ( + /** @type {Float32Array} */ + logits[i].data + ); + batch_logits_data.fill(-Infinity); + for (const eos_token of this.eos_token_id) { + batch_logits_data[eos_token] = 0; + } + } + } + return logits; + } + } + class SuppressTokensAtBeginLogitsProcessor extends LogitsProcessor { + /** + * Create a SuppressTokensAtBeginLogitsProcessor. + * @param {number[]} begin_suppress_tokens The IDs of the tokens to suppress. + * @param {number} begin_index The number of tokens to generate before suppressing tokens. + */ + constructor(begin_suppress_tokens, begin_index) { + super(); + this.begin_suppress_tokens = begin_suppress_tokens; + this.begin_index = begin_index; + } + /** + * Apply the BOS token forcing to the logits. + * @param {bigint[][]} input_ids The input IDs. + * @param {Tensor} logits The logits. + * @returns {Object} The logits with BOS token forcing. + */ + _call(input_ids, logits) { + for (let i = 0; i < input_ids.length; ++i) { + if (input_ids[i].length === this.begin_index) { + const batch_logits_data = ( + /** @type {Float32Array} */ + logits[i].data + ); + for (const token_id of this.begin_suppress_tokens) { + batch_logits_data[token_id] = -Infinity; + } + } + } + return logits; + } + } + class WhisperTimeStampLogitsProcessor extends LogitsProcessor { + /** + * Constructs a new WhisperTimeStampLogitsProcessor. + * @param {import('../models/whisper/generation_whisper.js').WhisperGenerationConfig} generate_config The config object passed to the `generate()` method of a transformer model. + * @param {number[]} init_tokens The initial tokens of the input sequence. + */ + constructor(generate_config, init_tokens) { + super(); + this.eos_token_id = Array.isArray(generate_config.eos_token_id) ? generate_config.eos_token_id[0] : generate_config.eos_token_id; + this.no_timestamps_token_id = generate_config.no_timestamps_token_id; + this.timestamp_begin = this.no_timestamps_token_id + 1; + this.begin_index = init_tokens.length; + if (init_tokens.at(-1) === this.no_timestamps_token_id) { + this.begin_index -= 1; + } + this.max_initial_timestamp_index = generate_config.max_initial_timestamp_index; + } + /** + * Modify the logits to handle timestamp tokens. + * @param {bigint[][]} input_ids The input sequence of tokens. + * @param {Tensor} logits The logits output by the model. + * @returns {Tensor} The modified logits. + */ + _call(input_ids, logits) { + for (let i = 0; i < input_ids.length; ++i) { + const batch_logits_data = ( + /** @type {Float32Array} */ + logits[i].data + ); + batch_logits_data[this.no_timestamps_token_id] = -Infinity; + if (input_ids[i].length === this.begin_index - 1) { + batch_logits_data.fill(-Infinity); + batch_logits_data[this.timestamp_begin] = 0; + continue; + } + const seq = input_ids[i].slice(this.begin_index); + const last_was_timestamp = seq.length >= 1 && seq[seq.length - 1] >= this.timestamp_begin; + const penultimate_was_timestamp = seq.length < 2 || seq[seq.length - 2] >= this.timestamp_begin; + if (last_was_timestamp) { + if (penultimate_was_timestamp) { + batch_logits_data.subarray(this.timestamp_begin).fill(-Infinity); + } else { + batch_logits_data.subarray(0, this.eos_token_id).fill(-Infinity); + } + } + if (input_ids[i].length === this.begin_index && this.max_initial_timestamp_index !== null) { + const last_allowed = this.timestamp_begin + this.max_initial_timestamp_index; + batch_logits_data.subarray(last_allowed + 1).fill(-Infinity); + } + const logprobs = (0, _utils_maths_js__WEBPACK_IMPORTED_MODULE_2__.log_softmax)(batch_logits_data); + const timestamp_logprob = Math.log(logprobs.subarray(this.timestamp_begin).map(Math.exp).reduce((a, b) => a + b)); + const max_text_token_logprob = (0, _utils_maths_js__WEBPACK_IMPORTED_MODULE_2__.max)(logprobs.subarray(0, this.timestamp_begin))[0]; + if (timestamp_logprob > max_text_token_logprob) { + batch_logits_data.subarray(0, this.timestamp_begin).fill(-Infinity); + } + } + return logits; + } + } + class NoRepeatNGramLogitsProcessor extends LogitsProcessor { + /** + * Create a NoRepeatNGramLogitsProcessor. + * @param {number} no_repeat_ngram_size The no-repeat-ngram size. All ngrams of this size can only occur once. + */ + constructor(no_repeat_ngram_size) { + super(); + this.no_repeat_ngram_size = no_repeat_ngram_size; + } + /** + * Generate n-grams from a sequence of token ids. + * @param {bigint[]} prevInputIds List of previous input ids + * @returns {Map} Map of generated n-grams + */ + getNgrams(prevInputIds) { + const curLen = prevInputIds.length; + const ngrams = []; + for (let j = 0; j < curLen + 1 - this.no_repeat_ngram_size; ++j) { + const ngram = []; + for (let k = 0; k < this.no_repeat_ngram_size; ++k) { + ngram.push(prevInputIds[j + k]); + } + ngrams.push(ngram.map(Number)); + } + const generatedNgram = /* @__PURE__ */ new Map(); + for (const ngram of ngrams) { + const prevNgram = ngram.slice(0, ngram.length - 1); + const prevNgramKey = JSON.stringify(prevNgram); + const prevNgramValue = generatedNgram.get(prevNgramKey) ?? []; + prevNgramValue.push(ngram[ngram.length - 1]); + generatedNgram.set(prevNgramKey, prevNgramValue); + } + return generatedNgram; + } + /** + * Generate n-grams from a sequence of token ids. + * @param {Map} bannedNgrams Map of banned n-grams + * @param {bigint[]} prevInputIds List of previous input ids + * @returns {number[]} Map of generated n-grams + */ + getGeneratedNgrams(bannedNgrams, prevInputIds) { + const ngramIdx = prevInputIds.slice(prevInputIds.length + 1 - this.no_repeat_ngram_size, prevInputIds.length); + const banned = bannedNgrams.get(JSON.stringify(ngramIdx.map(Number))) ?? []; + return banned; + } + /** + * Calculate banned n-gram tokens + * @param {bigint[]} prevInputIds List of previous input ids + * @returns {number[]} Map of generated n-grams + */ + calcBannedNgramTokens(prevInputIds) { + const bannedTokens = []; + if (prevInputIds.length + 1 < this.no_repeat_ngram_size) { + return bannedTokens; + } else { + const generatedNgrams = this.getNgrams(prevInputIds); + const bannedTokens2 = this.getGeneratedNgrams(generatedNgrams, prevInputIds); + return bannedTokens2; + } + } + /** + * Apply the no-repeat-ngram processor to the logits. + * @param {bigint[][]} input_ids The input IDs. + * @param {Tensor} logits The logits. + * @returns {Object} The logits with no-repeat-ngram processing. + */ + _call(input_ids, logits) { + for (let i = 0; i < input_ids.length; ++i) { + const batch_logits_data = ( + /** @type {Float32Array} */ + logits[i].data + ); + const bannedTokens = this.calcBannedNgramTokens(input_ids[i]); + for (const token of bannedTokens) { + batch_logits_data[token] = -Infinity; + } + } + return logits; + } + } + class RepetitionPenaltyLogitsProcessor extends LogitsProcessor { + /** + * Create a RepetitionPenaltyLogitsProcessor. + * @param {number} penalty The penalty to apply for repeated tokens. + */ + constructor(penalty) { + super(); + this.penalty = penalty; + } + /** + * Apply the repetition penalty to the logits. + * @param {bigint[][]} input_ids The input IDs. + * @param {Tensor} logits The logits. + * @returns {Object} The logits with repetition penalty processing. + */ + _call(input_ids, logits) { + for (let i = 0; i < input_ids.length; ++i) { + const batch_logits_data = ( + /** @type {Float32Array} */ + logits[i].data + ); + for (const input_id of input_ids[i]) { + const token = Number(input_id); + if (batch_logits_data[token] < 0) { + batch_logits_data[token] *= this.penalty; + } else { + batch_logits_data[token] /= this.penalty; + } + } + } + return logits; + } + } + class MinLengthLogitsProcessor extends LogitsProcessor { + /** + * Create a MinLengthLogitsProcessor. + * @param {number} min_length The minimum length below which the score of `eos_token_id` is set to negative infinity. + * @param {number|number[]} eos_token_id The ID/IDs of the end-of-sequence token. + */ + constructor(min_length, eos_token_id) { + super(); + this.min_length = min_length; + this.eos_token_id = Array.isArray(eos_token_id) ? eos_token_id : [eos_token_id]; + } + /** + * Apply logit processor. + * @param {bigint[][]} input_ids The input IDs. + * @param {Tensor} logits The logits. + * @returns {Object} The processed logits. + */ + _call(input_ids, logits) { + for (let i = 0; i < input_ids.length; ++i) { + if (input_ids[i].length < this.min_length) { + const batch_logits_data = ( + /** @type {Float32Array} */ + logits[i].data + ); + for (const eos_token of this.eos_token_id) { + batch_logits_data[eos_token] = -Infinity; + } + } + } + return logits; + } + } + class MinNewTokensLengthLogitsProcessor extends LogitsProcessor { + /** + * Create a MinNewTokensLengthLogitsProcessor. + * @param {number} prompt_length_to_skip The input tokens length. + * @param {number} min_new_tokens The minimum *new* tokens length below which the score of `eos_token_id` is set to negative infinity. + * @param {number|number[]} eos_token_id The ID/IDs of the end-of-sequence token. + */ + constructor(prompt_length_to_skip, min_new_tokens, eos_token_id) { + super(); + this.prompt_length_to_skip = prompt_length_to_skip; + this.min_new_tokens = min_new_tokens; + this.eos_token_id = Array.isArray(eos_token_id) ? eos_token_id : [eos_token_id]; + } + /** + * Apply logit processor. + * @param {bigint[][]} input_ids The input IDs. + * @param {Tensor} logits The logits. + * @returns {Object} The processed logits. + */ + _call(input_ids, logits) { + for (let i = 0; i < input_ids.length; ++i) { + const new_tokens_length = input_ids[i].length - this.prompt_length_to_skip; + if (new_tokens_length < this.min_new_tokens) { + const batch_logits_data = ( + /** @type {Float32Array} */ + logits[i].data + ); + for (const eos_token of this.eos_token_id) { + batch_logits_data[eos_token] = -Infinity; + } + } + } + return logits; + } + } + class NoBadWordsLogitsProcessor extends LogitsProcessor { + /** + * Create a `NoBadWordsLogitsProcessor`. + * @param {number[][]} bad_words_ids List of list of token ids that are not allowed to be generated. + * @param {number|number[]} eos_token_id The id of the *end-of-sequence* token. Optionally, use a list to set multiple *end-of-sequence* tokens. + */ + constructor(bad_words_ids, eos_token_id) { + super(); + this.bad_words_ids = bad_words_ids; + this.eos_token_id = Array.isArray(eos_token_id) ? eos_token_id : [eos_token_id]; + } + /** + * Apply logit processor. + * @param {bigint[][]} input_ids The input IDs. + * @param {Tensor} logits The logits. + * @returns {Object} The processed logits. + */ + _call(input_ids, logits) { + for (let i = 0; i < input_ids.length; ++i) { + const batch_logits_data = ( + /** @type {Float32Array} */ + logits[i].data + ); + const ids = input_ids[i]; + for (const bad_word_ids of this.bad_words_ids) { + let mark = true; + for (let j = 1; j <= bad_word_ids.length - 1 && bad_word_ids.length < ids.length; ++j) { + if (bad_word_ids.at(-j - 1) != ids.at(-j)) { + mark = false; + break; + } + } + if (mark) { + batch_logits_data[bad_word_ids.at(-1)] = -Infinity; + } + } + } + return logits; + } + } + class ClassifierFreeGuidanceLogitsProcessor extends LogitsProcessor { + /** + * Create a `ClassifierFreeGuidanceLogitsProcessor`. + * @param {number} guidance_scale The guidance scale for classifier free guidance (CFG). CFG is enabled by setting `guidance_scale > 1`. + * Higher guidance scale encourages the model to generate samples that are more closely linked to the input + * prompt, usually at the expense of poorer quality. + */ + constructor(guidance_scale) { + super(); + if (guidance_scale <= 1) { + throw new Error( + `Require guidance scale >1 to use the classifier free guidance processor, got guidance scale ${guidance_scale}.` + ); + } + this.guidance_scale = guidance_scale; + } + /** + * Apply logit processor. + * @param {bigint[][]} input_ids The input IDs. + * @param {Tensor} logits The logits. + * @returns {Object} The processed logits. + */ + _call(input_ids, logits) { + if (logits.dims[0] !== 2 * input_ids.length) { + throw new Error( + `Logits should have twice the batch size of the input ids, the first half of batches corresponding to the conditional inputs, and the second half of batches corresponding to the unconditional inputs. Got batch size ${logits.dims[0]} for the logits and ${input_ids.length} for the input ids.` + ); + } + const unguided_bsz = input_ids.length; + const cond_logits = logits.slice([0, unguided_bsz], null); + const uncond_logits = logits.slice([unguided_bsz, logits.dims[0]], null); + for (let i = 0; i < uncond_logits.data.length; ++i) { + uncond_logits.data[i] += (cond_logits.data[i] - uncond_logits.data[i]) * this.guidance_scale; + } + return uncond_logits; + } + } + class TemperatureLogitsWarper extends LogitsWarper { + /** + * Create a `TemperatureLogitsWarper`. + * @param {number} temperature Strictly positive float value used to modulate the logits distribution. + * A value smaller than `1` decreases randomness (and vice versa), with `0` being equivalent to shifting + * all probability mass to the most likely token. + */ + constructor(temperature) { + super(); + if (typeof temperature !== "number" || temperature <= 0) { + let errorMessage = `\`temperature\` (=${temperature}) must be a strictly positive float, otherwise your next token scores will be invalid.`; + if (temperature === 0) { + errorMessage += " If you're looking for greedy decoding strategies, set `do_sample=false`."; + } + } + this.temperature = temperature; + } + /** + * Apply logit warper. + * @param {bigint[][]} input_ids The input IDs. + * @param {Tensor} logits The logits. + * @returns {Object} The processed logits. + */ + _call(input_ids, logits) { + const batch_logits_data = ( + /** @type {Float32Array} */ + logits.data + ); + for (let i = 0; i < batch_logits_data.length; ++i) { + batch_logits_data[i] /= this.temperature; + } + return logits; + } + } + class TopPLogitsWarper extends LogitsWarper { + /** + * Create a `TopPLogitsWarper`. + * @param {number} top_p If set to < 1, only the smallest set of most probable tokens with + * probabilities that add up to `top_p` or higher are kept for generation. + * @param {Object} options Additional options for the top-p sampling. + * @param {number} [options.filter_value=-Infinity] All filtered values will be set to this float value. + * @param {number} [options.min_tokens_to_keep=1] Minimum number of tokens that cannot be filtered. + */ + constructor(top_p, { + filter_value = -Infinity, + min_tokens_to_keep = 1 + } = {}) { + super(); + if (top_p < 0 || top_p > 1) { + throw new Error(`\`top_p\` must be a float > 0 and < 1, but is ${top_p}`); + } + if (!Number.isInteger(min_tokens_to_keep) || min_tokens_to_keep < 1) { + throw new Error(`\`min_tokens_to_keep\` must be a positive integer, but is ${min_tokens_to_keep}`); + } + this.top_p = top_p; + this.filter_value = filter_value; + this.min_tokens_to_keep = min_tokens_to_keep; + } + } + class TopKLogitsWarper extends LogitsWarper { + /** + * Create a `TopKLogitsWarper`. + * @param {number} top_k If set to > 0, only the top `top_k` tokens are kept for generation. + * @param {Object} options Additional options for the top-k sampling. + * @param {number} [options.filter_value=-Infinity] All filtered values will be set to this float value. + * @param {number} [options.min_tokens_to_keep=1] Minimum number of tokens that cannot be filtered. + */ + constructor(top_k, { + filter_value = -Infinity, + min_tokens_to_keep = 1 + } = {}) { + super(); + if (!Number.isInteger(top_k) || top_k < 0) { + throw new Error(`\`top_k\` must be a positive integer, but is ${top_k}`); + } + this.top_k = Math.max(top_k, min_tokens_to_keep); + this.filter_value = filter_value; + } + } + } + ), + /***/ + "./src/generation/logits_sampler.js": ( + /*!******************************************!*\ + !*** ./src/generation/logits_sampler.js ***! + \******************************************/ + /***/ + (__unused_webpack___webpack_module__, __webpack_exports__2, __webpack_require__2) => { + __webpack_require__2.r(__webpack_exports__2); + __webpack_require__2.d(__webpack_exports__2, { + /* harmony export */ + LogitsSampler: () => ( + /* binding */ + LogitsSampler + ) + /* harmony export */ + }); + var _utils_generic_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__2( + /*! ../utils/generic.js */ + "./src/utils/generic.js" + ); + var _utils_tensor_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__2( + /*! ../utils/tensor.js */ + "./src/utils/tensor.js" + ); + var _utils_maths_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__2( + /*! ../utils/maths.js */ + "./src/utils/maths.js" + ); + var _generation_configuration_utils_js__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__2( + /*! ../generation/configuration_utils.js */ + "./src/generation/configuration_utils.js" + ); + class LogitsSampler extends _utils_generic_js__WEBPACK_IMPORTED_MODULE_0__.Callable { + /** + * Creates a new Sampler object with the specified generation config. + * @param {GenerationConfig} generation_config The generation config. + */ + constructor(generation_config) { + super(); + this.generation_config = generation_config; + } + /** + * Executes the sampler, using the specified logits. + * @param {Tensor} logits + * @returns {Promise<[bigint, number][]>} + */ + async _call(logits) { + return this.sample(logits); + } + /** + * Abstract method for sampling the logits. + * @param {Tensor} logits + * @throws {Error} If not implemented in subclass. + * @returns {Promise<[bigint, number][]>} + */ + async sample(logits) { + throw Error("sample should be implemented in subclasses."); + } + /** + * Returns the specified logits as an array, with temperature applied. + * @param {Tensor} logits + * @param {number} index + * @returns {Float32Array} + */ + getLogits(logits, index) { + let vocabSize = logits.dims.at(-1); + let logs = ( + /** @type {Float32Array} */ + logits.data + ); + if (index === -1) { + logs = logs.slice(-vocabSize); + } else { + let startIndex = index * vocabSize; + logs = logs.slice(startIndex, startIndex + vocabSize); + } + return logs; + } + /** + * Selects an item randomly based on the specified probabilities. + * @param {import("../transformers.js").DataArray} probabilities An array of probabilities to use for selection. + * @returns {number} The index of the selected item. + */ + randomSelect(probabilities) { + let sumProbabilities = 0; + for (let i = 0; i < probabilities.length; ++i) { + sumProbabilities += probabilities[i]; + } + let r = Math.random() * sumProbabilities; + for (let i = 0; i < probabilities.length; ++i) { + r -= probabilities[i]; + if (r <= 0) { + return i; + } + } + return 0; + } + /** + * Returns a Sampler object based on the specified options. + * @param {GenerationConfig} generation_config An object containing options for the sampler. + * @returns {LogitsSampler} A Sampler object. + */ + static getSampler(generation_config) { + if (generation_config.do_sample) { + return new MultinomialSampler(generation_config); + } else if (generation_config.num_beams > 1) { + return new BeamSearchSampler(generation_config); + } else { + if (generation_config.num_return_sequences > 1) { + throw Error(`num_return_sequences has to be 1 when doing greedy search, but is ${generation_config.num_return_sequences}.`); + } + return new GreedySampler(generation_config); + } + } + } + class GreedySampler extends LogitsSampler { + /** + * Sample the maximum probability of a given logits tensor. + * @param {Tensor} logits + * @returns {Promise<[bigint, number][]>} An array with a single tuple, containing the index of the maximum value and a meaningless score (since this is a greedy search). + */ + async sample(logits) { + const argmax = (0, _utils_maths_js__WEBPACK_IMPORTED_MODULE_2__.max)(logits.data)[1]; + return [ + [BigInt(argmax), 0] + ]; + } + } + class MultinomialSampler extends LogitsSampler { + /** + * Sample from the logits. + * @param {Tensor} logits + * @returns {Promise<[bigint, number][]>} + */ + async sample(logits) { + let k = logits.dims.at(-1); + if (this.generation_config.top_k > 0) { + k = Math.min(this.generation_config.top_k, k); + } + const [v, i] = await (0, _utils_tensor_js__WEBPACK_IMPORTED_MODULE_1__.topk)(logits, k); + const probabilities = (0, _utils_maths_js__WEBPACK_IMPORTED_MODULE_2__.softmax)( + /** @type {Float32Array} */ + v.data + ); + return Array.from({ length: this.generation_config.num_beams }, () => { + const sampledIndex = this.randomSelect(probabilities); + return [ + i.data[sampledIndex], + // token id + Math.log(probabilities[sampledIndex]) + // score + ]; + }); + } + } + class BeamSearchSampler extends LogitsSampler { + /** + * Sample from the logits. + * @param {Tensor} logits + * @returns {Promise<[bigint, number][]>} + */ + async sample(logits) { + let k = logits.dims.at(-1); + if (this.generation_config.top_k > 0) { + k = Math.min(this.generation_config.top_k, k); + } + const [v, i] = await (0, _utils_tensor_js__WEBPACK_IMPORTED_MODULE_1__.topk)(logits, k); + const probabilities = (0, _utils_maths_js__WEBPACK_IMPORTED_MODULE_2__.softmax)( + /** @type {Float32Array} */ + v.data + ); + return Array.from({ length: this.generation_config.num_beams }, (_, x) => { + return [ + i.data[x], + // token id + Math.log(probabilities[x]) + // score + ]; + }); + } + } + } + ), + /***/ + "./src/generation/stopping_criteria.js": ( + /*!*********************************************!*\ + !*** ./src/generation/stopping_criteria.js ***! + \*********************************************/ + /***/ + (__unused_webpack___webpack_module__, __webpack_exports__2, __webpack_require__2) => { + __webpack_require__2.r(__webpack_exports__2); + __webpack_require__2.d(__webpack_exports__2, { + /* harmony export */ + EosTokenCriteria: () => ( + /* binding */ + EosTokenCriteria + ), + /* harmony export */ + InterruptableStoppingCriteria: () => ( + /* binding */ + InterruptableStoppingCriteria + ), + /* harmony export */ + MaxLengthCriteria: () => ( + /* binding */ + MaxLengthCriteria + ), + /* harmony export */ + StoppingCriteria: () => ( + /* binding */ + StoppingCriteria + ), + /* harmony export */ + StoppingCriteriaList: () => ( + /* binding */ + StoppingCriteriaList + ) + /* harmony export */ + }); + var _utils_generic_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__2( + /*! ../utils/generic.js */ + "./src/utils/generic.js" + ); + class StoppingCriteria extends _utils_generic_js__WEBPACK_IMPORTED_MODULE_0__.Callable { + /** + * + * @param {number[][]} input_ids (`number[][]` of shape `(batch_size, sequence_length)`): + * Indices of input sequence tokens in the vocabulary. + * @param {number[][]} scores scores (`number[][]` of shape `(batch_size, config.vocab_size)`): + * Prediction scores of a language modeling head. These can be scores for each vocabulary token before SoftMax + * or scores for each vocabulary token after SoftMax. + * @returns {boolean[]} A list of booleans indicating whether each sequence should be stopped. + */ + _call(input_ids, scores) { + throw Error("StoppingCriteria needs to be subclassed"); + } + } + class StoppingCriteriaList extends _utils_generic_js__WEBPACK_IMPORTED_MODULE_0__.Callable { + /** + * Constructs a new instance of `StoppingCriteriaList`. + */ + constructor() { + super(); + this.criteria = []; + } + /** + * Adds a new stopping criterion to the list. + * + * @param {StoppingCriteria} item The stopping criterion to add. + */ + push(item) { + this.criteria.push(item); + } + /** + * Adds multiple stopping criteria to the list. + * + * @param {StoppingCriteria|StoppingCriteriaList|StoppingCriteria[]} items The stopping criteria to add. + */ + extend(items) { + if (items instanceof StoppingCriteriaList) { + items = items.criteria; + } else if (items instanceof StoppingCriteria) { + items = [items]; + } + this.criteria.push(...items); + } + _call(input_ids, scores) { + const is_done = new Array(input_ids.length).fill(false); + for (const criterion of this.criteria) { + const criterion_done = criterion(input_ids, scores); + for (let i = 0; i < is_done.length; ++i) { + is_done[i] || (is_done[i] = criterion_done[i]); + } + } + return is_done; + } + [Symbol.iterator]() { + return this.criteria.values(); + } + } + class MaxLengthCriteria extends StoppingCriteria { + /** + * + * @param {number} max_length The maximum length that the output sequence can have in number of tokens. + * @param {number} [max_position_embeddings=null] The maximum model length, as defined by the model's `config.max_position_embeddings` attribute. + */ + constructor(max_length, max_position_embeddings = null) { + super(); + this.max_length = max_length; + this.max_position_embeddings = max_position_embeddings; + } + _call(input_ids) { + return input_ids.map((ids) => ids.length >= this.max_length); + } + } + class EosTokenCriteria extends StoppingCriteria { + /** + * + * @param {number|number[]} eos_token_id The id of the *end-of-sequence* token. + * Optionally, use a list to set multiple *end-of-sequence* tokens. + */ + constructor(eos_token_id) { + super(); + if (!Array.isArray(eos_token_id)) { + eos_token_id = [eos_token_id]; + } + this.eos_token_id = eos_token_id; + } + /** + * + * @param {number[][]} input_ids + * @param {number[][]} scores + * @returns {boolean[]} + */ + _call(input_ids, scores) { + return input_ids.map((ids) => { + const last = ids.at(-1); + return this.eos_token_id.some((eos_id) => last == eos_id); + }); + } + } + class InterruptableStoppingCriteria extends StoppingCriteria { + constructor() { + super(); + this.interrupted = false; + } + interrupt() { + this.interrupted = true; + } + reset() { + this.interrupted = false; + } + _call(input_ids, scores) { + return new Array(input_ids.length).fill(this.interrupted); + } + } + } + ), + /***/ + "./src/generation/streamers.js": ( + /*!*************************************!*\ + !*** ./src/generation/streamers.js ***! + \*************************************/ + /***/ + (__unused_webpack___webpack_module__, __webpack_exports__2, __webpack_require__2) => { + __webpack_require__2.r(__webpack_exports__2); + __webpack_require__2.d(__webpack_exports__2, { + /* harmony export */ + BaseStreamer: () => ( + /* binding */ + BaseStreamer + ), + /* harmony export */ + TextStreamer: () => ( + /* binding */ + TextStreamer + ), + /* harmony export */ + WhisperTextStreamer: () => ( + /* binding */ + WhisperTextStreamer + ) + /* harmony export */ + }); + var _utils_core_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__2( + /*! ../utils/core.js */ + "./src/utils/core.js" + ); + var _tokenizers_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__2( + /*! ../tokenizers.js */ + "./src/tokenizers.js" + ); + var _env_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__2( + /*! ../env.js */ + "./src/env.js" + ); + class BaseStreamer { + /** + * Function that is called by `.generate()` to push new tokens + * @param {bigint[][]} value + */ + put(value) { + throw Error("Not implemented"); + } + /** + * Function that is called by `.generate()` to signal the end of generation + */ + end() { + throw Error("Not implemented"); + } + } + const stdout_write = _env_js__WEBPACK_IMPORTED_MODULE_2__.apis.IS_PROCESS_AVAILABLE ? (x) => process.stdout.write(x) : (x) => console.log(x); + class TextStreamer extends BaseStreamer { + /** + * + * @param {import('../tokenizers.js').PreTrainedTokenizer} tokenizer + */ + constructor(tokenizer, { + skip_prompt = false, + callback_function = null, + token_callback_function = null, + decode_kwargs = {}, + ...kwargs + } = {}) { + super(); + this.tokenizer = tokenizer; + this.skip_prompt = skip_prompt; + this.callback_function = callback_function ?? stdout_write; + this.token_callback_function = token_callback_function; + this.decode_kwargs = { ...decode_kwargs, ...kwargs }; + this.token_cache = []; + this.print_len = 0; + this.next_tokens_are_prompt = true; + } + /** + * Receives tokens, decodes them, and prints them to stdout as soon as they form entire words. + * @param {bigint[][]} value + */ + put(value) { + var _a; + if (value.length > 1) { + throw Error("TextStreamer only supports batch size of 1"); + } + if (this.skip_prompt && this.next_tokens_are_prompt) { + this.next_tokens_are_prompt = false; + return; + } + const tokens = value[0]; + (_a = this.token_callback_function) == null ? void 0 : _a.call(this, tokens); + this.token_cache = (0, _utils_core_js__WEBPACK_IMPORTED_MODULE_0__.mergeArrays)(this.token_cache, tokens); + const text = this.tokenizer.decode(this.token_cache, this.decode_kwargs); + let printable_text; + if (text.endsWith("\n")) { + printable_text = text.slice(this.print_len); + this.token_cache = []; + this.print_len = 0; + } else if (text.length > 0 && (0, _tokenizers_js__WEBPACK_IMPORTED_MODULE_1__.is_chinese_char)(text.charCodeAt(text.length - 1))) { + printable_text = text.slice(this.print_len); + this.print_len += printable_text.length; + } else { + printable_text = text.slice(this.print_len, text.lastIndexOf(" ") + 1); + this.print_len += printable_text.length; + } + this.on_finalized_text(printable_text, false); + } + /** + * Flushes any remaining cache and prints a newline to stdout. + */ + end() { + let printable_text; + if (this.token_cache.length > 0) { + const text = this.tokenizer.decode(this.token_cache, this.decode_kwargs); + printable_text = text.slice(this.print_len); + this.token_cache = []; + this.print_len = 0; + } else { + printable_text = ""; + } + this.next_tokens_are_prompt = true; + this.on_finalized_text(printable_text, true); + } + /** + * Prints the new text to stdout. If the stream is ending, also prints a newline. + * @param {string} text + * @param {boolean} stream_end + */ + on_finalized_text(text, stream_end) { + var _a, _b; + if (text.length > 0) { + (_a = this.callback_function) == null ? void 0 : _a.call(this, text); + } + if (stream_end && this.callback_function === stdout_write && _env_js__WEBPACK_IMPORTED_MODULE_2__.apis.IS_PROCESS_AVAILABLE) { + (_b = this.callback_function) == null ? void 0 : _b.call(this, "\n"); + } + } + } + class WhisperTextStreamer extends TextStreamer { + /** + * @param {import('../tokenizers.js').WhisperTokenizer} tokenizer + * @param {Object} options + * @param {boolean} [options.skip_prompt=false] Whether to skip the prompt tokens + * @param {function(string): void} [options.callback_function=null] Function to call when a piece of text is ready to display + * @param {function(string): void} [options.token_callback_function=null] Function to call when a new token is generated + * @param {function(number): void} [options.on_chunk_start=null] Function to call when a new chunk starts + * @param {function(number): void} [options.on_chunk_end=null] Function to call when a chunk ends + * @param {function(): void} [options.on_finalize=null] Function to call when the stream is finalized + * @param {number} [options.time_precision=0.02] Precision of the timestamps + * @param {boolean} [options.skip_special_tokens=true] Whether to skip special tokens when decoding + * @param {Object} [options.decode_kwargs={}] Additional keyword arguments to pass to the tokenizer's decode method + */ + constructor(tokenizer, { + skip_prompt = false, + callback_function = null, + token_callback_function = null, + on_chunk_start = null, + on_chunk_end = null, + on_finalize = null, + time_precision = 0.02, + skip_special_tokens = true, + decode_kwargs = {} + } = {}) { + super(tokenizer, { + skip_prompt, + callback_function, + token_callback_function, + decode_kwargs: { skip_special_tokens, ...decode_kwargs } + }); + this.timestamp_begin = tokenizer.timestamp_begin; + this.on_chunk_start = on_chunk_start; + this.on_chunk_end = on_chunk_end; + this.on_finalize = on_finalize; + this.time_precision = time_precision; + this.waiting_for_timestamp = false; + } + /** + * @param {bigint[][]} value + */ + put(value) { + var _a, _b; + if (value.length > 1) { + throw Error("WhisperTextStreamer only supports batch size of 1"); + } + const tokens = value[0]; + if (tokens.length === 1) { + const offset = Number(tokens[0]) - this.timestamp_begin; + if (offset >= 0) { + const time = offset * this.time_precision; + if (this.waiting_for_timestamp) { + (_a = this.on_chunk_end) == null ? void 0 : _a.call(this, time); + } else { + (_b = this.on_chunk_start) == null ? void 0 : _b.call(this, time); + } + this.waiting_for_timestamp = !this.waiting_for_timestamp; + value = [[]]; + } + } + return super.put(value); + } + end() { + var _a; + super.end(); + (_a = this.on_finalize) == null ? void 0 : _a.call(this); + } + } + } + ), + /***/ + "./src/models.js": ( + /*!***********************!*\ + !*** ./src/models.js ***! + \***********************/ + /***/ + (__unused_webpack___webpack_module__, __webpack_exports__2, __webpack_require__2) => { + __webpack_require__2.r(__webpack_exports__2); + __webpack_require__2.d(__webpack_exports__2, { + /* harmony export */ + ASTForAudioClassification: () => ( + /* binding */ + ASTForAudioClassification + ), + /* harmony export */ + ASTModel: () => ( + /* binding */ + ASTModel + ), + /* harmony export */ + ASTPreTrainedModel: () => ( + /* binding */ + ASTPreTrainedModel + ), + /* harmony export */ + AlbertForMaskedLM: () => ( + /* binding */ + AlbertForMaskedLM + ), + /* harmony export */ + AlbertForQuestionAnswering: () => ( + /* binding */ + AlbertForQuestionAnswering + ), + /* harmony export */ + AlbertForSequenceClassification: () => ( + /* binding */ + AlbertForSequenceClassification + ), + /* harmony export */ + AlbertModel: () => ( + /* binding */ + AlbertModel + ), + /* harmony export */ + AlbertPreTrainedModel: () => ( + /* binding */ + AlbertPreTrainedModel + ), + /* harmony export */ + AutoModel: () => ( + /* binding */ + AutoModel + ), + /* harmony export */ + AutoModelForAudioClassification: () => ( + /* binding */ + AutoModelForAudioClassification + ), + /* harmony export */ + AutoModelForAudioFrameClassification: () => ( + /* binding */ + AutoModelForAudioFrameClassification + ), + /* harmony export */ + AutoModelForCTC: () => ( + /* binding */ + AutoModelForCTC + ), + /* harmony export */ + AutoModelForCausalLM: () => ( + /* binding */ + AutoModelForCausalLM + ), + /* harmony export */ + AutoModelForDepthEstimation: () => ( + /* binding */ + AutoModelForDepthEstimation + ), + /* harmony export */ + AutoModelForDocumentQuestionAnswering: () => ( + /* binding */ + AutoModelForDocumentQuestionAnswering + ), + /* harmony export */ + AutoModelForImageClassification: () => ( + /* binding */ + AutoModelForImageClassification + ), + /* harmony export */ + AutoModelForImageFeatureExtraction: () => ( + /* binding */ + AutoModelForImageFeatureExtraction + ), + /* harmony export */ + AutoModelForImageMatting: () => ( + /* binding */ + AutoModelForImageMatting + ), + /* harmony export */ + AutoModelForImageSegmentation: () => ( + /* binding */ + AutoModelForImageSegmentation + ), + /* harmony export */ + AutoModelForImageToImage: () => ( + /* binding */ + AutoModelForImageToImage + ), + /* harmony export */ + AutoModelForMaskGeneration: () => ( + /* binding */ + AutoModelForMaskGeneration + ), + /* harmony export */ + AutoModelForMaskedLM: () => ( + /* binding */ + AutoModelForMaskedLM + ), + /* harmony export */ + AutoModelForNormalEstimation: () => ( + /* binding */ + AutoModelForNormalEstimation + ), + /* harmony export */ + AutoModelForObjectDetection: () => ( + /* binding */ + AutoModelForObjectDetection + ), + /* harmony export */ + AutoModelForQuestionAnswering: () => ( + /* binding */ + AutoModelForQuestionAnswering + ), + /* harmony export */ + AutoModelForSemanticSegmentation: () => ( + /* binding */ + AutoModelForSemanticSegmentation + ), + /* harmony export */ + AutoModelForSeq2SeqLM: () => ( + /* binding */ + AutoModelForSeq2SeqLM + ), + /* harmony export */ + AutoModelForSequenceClassification: () => ( + /* binding */ + AutoModelForSequenceClassification + ), + /* harmony export */ + AutoModelForSpeechSeq2Seq: () => ( + /* binding */ + AutoModelForSpeechSeq2Seq + ), + /* harmony export */ + AutoModelForTextToSpectrogram: () => ( + /* binding */ + AutoModelForTextToSpectrogram + ), + /* harmony export */ + AutoModelForTextToWaveform: () => ( + /* binding */ + AutoModelForTextToWaveform + ), + /* harmony export */ + AutoModelForTokenClassification: () => ( + /* binding */ + AutoModelForTokenClassification + ), + /* harmony export */ + AutoModelForUniversalSegmentation: () => ( + /* binding */ + AutoModelForUniversalSegmentation + ), + /* harmony export */ + AutoModelForVision2Seq: () => ( + /* binding */ + AutoModelForVision2Seq + ), + /* harmony export */ + AutoModelForXVector: () => ( + /* binding */ + AutoModelForXVector + ), + /* harmony export */ + AutoModelForZeroShotObjectDetection: () => ( + /* binding */ + AutoModelForZeroShotObjectDetection + ), + /* harmony export */ + BartForConditionalGeneration: () => ( + /* binding */ + BartForConditionalGeneration + ), + /* harmony export */ + BartForSequenceClassification: () => ( + /* binding */ + BartForSequenceClassification + ), + /* harmony export */ + BartModel: () => ( + /* binding */ + BartModel + ), + /* harmony export */ + BartPretrainedModel: () => ( + /* binding */ + BartPretrainedModel + ), + /* harmony export */ + BaseModelOutput: () => ( + /* binding */ + BaseModelOutput + ), + /* harmony export */ + BeitForImageClassification: () => ( + /* binding */ + BeitForImageClassification + ), + /* harmony export */ + BeitModel: () => ( + /* binding */ + BeitModel + ), + /* harmony export */ + BeitPreTrainedModel: () => ( + /* binding */ + BeitPreTrainedModel + ), + /* harmony export */ + BertForMaskedLM: () => ( + /* binding */ + BertForMaskedLM + ), + /* harmony export */ + BertForQuestionAnswering: () => ( + /* binding */ + BertForQuestionAnswering + ), + /* harmony export */ + BertForSequenceClassification: () => ( + /* binding */ + BertForSequenceClassification + ), + /* harmony export */ + BertForTokenClassification: () => ( + /* binding */ + BertForTokenClassification + ), + /* harmony export */ + BertModel: () => ( + /* binding */ + BertModel + ), + /* harmony export */ + BertPreTrainedModel: () => ( + /* binding */ + BertPreTrainedModel + ), + /* harmony export */ + BlenderbotForConditionalGeneration: () => ( + /* binding */ + BlenderbotForConditionalGeneration + ), + /* harmony export */ + BlenderbotModel: () => ( + /* binding */ + BlenderbotModel + ), + /* harmony export */ + BlenderbotPreTrainedModel: () => ( + /* binding */ + BlenderbotPreTrainedModel + ), + /* harmony export */ + BlenderbotSmallForConditionalGeneration: () => ( + /* binding */ + BlenderbotSmallForConditionalGeneration + ), + /* harmony export */ + BlenderbotSmallModel: () => ( + /* binding */ + BlenderbotSmallModel + ), + /* harmony export */ + BlenderbotSmallPreTrainedModel: () => ( + /* binding */ + BlenderbotSmallPreTrainedModel + ), + /* harmony export */ + BloomForCausalLM: () => ( + /* binding */ + BloomForCausalLM + ), + /* harmony export */ + BloomModel: () => ( + /* binding */ + BloomModel + ), + /* harmony export */ + BloomPreTrainedModel: () => ( + /* binding */ + BloomPreTrainedModel + ), + /* harmony export */ + CLIPModel: () => ( + /* binding */ + CLIPModel + ), + /* harmony export */ + CLIPPreTrainedModel: () => ( + /* binding */ + CLIPPreTrainedModel + ), + /* harmony export */ + CLIPSegForImageSegmentation: () => ( + /* binding */ + CLIPSegForImageSegmentation + ), + /* harmony export */ + CLIPSegModel: () => ( + /* binding */ + CLIPSegModel + ), + /* harmony export */ + CLIPSegPreTrainedModel: () => ( + /* binding */ + CLIPSegPreTrainedModel + ), + /* harmony export */ + CLIPTextModel: () => ( + /* binding */ + CLIPTextModel + ), + /* harmony export */ + CLIPTextModelWithProjection: () => ( + /* binding */ + CLIPTextModelWithProjection + ), + /* harmony export */ + CLIPVisionModel: () => ( + /* binding */ + CLIPVisionModel + ), + /* harmony export */ + CLIPVisionModelWithProjection: () => ( + /* binding */ + CLIPVisionModelWithProjection + ), + /* harmony export */ + CamembertForMaskedLM: () => ( + /* binding */ + CamembertForMaskedLM + ), + /* harmony export */ + CamembertForQuestionAnswering: () => ( + /* binding */ + CamembertForQuestionAnswering + ), + /* harmony export */ + CamembertForSequenceClassification: () => ( + /* binding */ + CamembertForSequenceClassification + ), + /* harmony export */ + CamembertForTokenClassification: () => ( + /* binding */ + CamembertForTokenClassification + ), + /* harmony export */ + CamembertModel: () => ( + /* binding */ + CamembertModel + ), + /* harmony export */ + CamembertPreTrainedModel: () => ( + /* binding */ + CamembertPreTrainedModel + ), + /* harmony export */ + CausalLMOutput: () => ( + /* binding */ + CausalLMOutput + ), + /* harmony export */ + CausalLMOutputWithPast: () => ( + /* binding */ + CausalLMOutputWithPast + ), + /* harmony export */ + ChineseCLIPModel: () => ( + /* binding */ + ChineseCLIPModel + ), + /* harmony export */ + ChineseCLIPPreTrainedModel: () => ( + /* binding */ + ChineseCLIPPreTrainedModel + ), + /* harmony export */ + ClapAudioModelWithProjection: () => ( + /* binding */ + ClapAudioModelWithProjection + ), + /* harmony export */ + ClapModel: () => ( + /* binding */ + ClapModel + ), + /* harmony export */ + ClapPreTrainedModel: () => ( + /* binding */ + ClapPreTrainedModel + ), + /* harmony export */ + ClapTextModelWithProjection: () => ( + /* binding */ + ClapTextModelWithProjection + ), + /* harmony export */ + CodeGenForCausalLM: () => ( + /* binding */ + CodeGenForCausalLM + ), + /* harmony export */ + CodeGenModel: () => ( + /* binding */ + CodeGenModel + ), + /* harmony export */ + CodeGenPreTrainedModel: () => ( + /* binding */ + CodeGenPreTrainedModel + ), + /* harmony export */ + CohereForCausalLM: () => ( + /* binding */ + CohereForCausalLM + ), + /* harmony export */ + CohereModel: () => ( + /* binding */ + CohereModel + ), + /* harmony export */ + CoherePreTrainedModel: () => ( + /* binding */ + CoherePreTrainedModel + ), + /* harmony export */ + ConvBertForMaskedLM: () => ( + /* binding */ + ConvBertForMaskedLM + ), + /* harmony export */ + ConvBertForQuestionAnswering: () => ( + /* binding */ + ConvBertForQuestionAnswering + ), + /* harmony export */ + ConvBertForSequenceClassification: () => ( + /* binding */ + ConvBertForSequenceClassification + ), + /* harmony export */ + ConvBertForTokenClassification: () => ( + /* binding */ + ConvBertForTokenClassification + ), + /* harmony export */ + ConvBertModel: () => ( + /* binding */ + ConvBertModel + ), + /* harmony export */ + ConvBertPreTrainedModel: () => ( + /* binding */ + ConvBertPreTrainedModel + ), + /* harmony export */ + ConvNextForImageClassification: () => ( + /* binding */ + ConvNextForImageClassification + ), + /* harmony export */ + ConvNextModel: () => ( + /* binding */ + ConvNextModel + ), + /* harmony export */ + ConvNextPreTrainedModel: () => ( + /* binding */ + ConvNextPreTrainedModel + ), + /* harmony export */ + ConvNextV2ForImageClassification: () => ( + /* binding */ + ConvNextV2ForImageClassification + ), + /* harmony export */ + ConvNextV2Model: () => ( + /* binding */ + ConvNextV2Model + ), + /* harmony export */ + ConvNextV2PreTrainedModel: () => ( + /* binding */ + ConvNextV2PreTrainedModel + ), + /* harmony export */ + DPTForDepthEstimation: () => ( + /* binding */ + DPTForDepthEstimation + ), + /* harmony export */ + DPTModel: () => ( + /* binding */ + DPTModel + ), + /* harmony export */ + DPTPreTrainedModel: () => ( + /* binding */ + DPTPreTrainedModel + ), + /* harmony export */ + DebertaForMaskedLM: () => ( + /* binding */ + DebertaForMaskedLM + ), + /* harmony export */ + DebertaForQuestionAnswering: () => ( + /* binding */ + DebertaForQuestionAnswering + ), + /* harmony export */ + DebertaForSequenceClassification: () => ( + /* binding */ + DebertaForSequenceClassification + ), + /* harmony export */ + DebertaForTokenClassification: () => ( + /* binding */ + DebertaForTokenClassification + ), + /* harmony export */ + DebertaModel: () => ( + /* binding */ + DebertaModel + ), + /* harmony export */ + DebertaPreTrainedModel: () => ( + /* binding */ + DebertaPreTrainedModel + ), + /* harmony export */ + DebertaV2ForMaskedLM: () => ( + /* binding */ + DebertaV2ForMaskedLM + ), + /* harmony export */ + DebertaV2ForQuestionAnswering: () => ( + /* binding */ + DebertaV2ForQuestionAnswering + ), + /* harmony export */ + DebertaV2ForSequenceClassification: () => ( + /* binding */ + DebertaV2ForSequenceClassification + ), + /* harmony export */ + DebertaV2ForTokenClassification: () => ( + /* binding */ + DebertaV2ForTokenClassification + ), + /* harmony export */ + DebertaV2Model: () => ( + /* binding */ + DebertaV2Model + ), + /* harmony export */ + DebertaV2PreTrainedModel: () => ( + /* binding */ + DebertaV2PreTrainedModel + ), + /* harmony export */ + DecisionTransformerModel: () => ( + /* binding */ + DecisionTransformerModel + ), + /* harmony export */ + DecisionTransformerPreTrainedModel: () => ( + /* binding */ + DecisionTransformerPreTrainedModel + ), + /* harmony export */ + DeiTForImageClassification: () => ( + /* binding */ + DeiTForImageClassification + ), + /* harmony export */ + DeiTModel: () => ( + /* binding */ + DeiTModel + ), + /* harmony export */ + DeiTPreTrainedModel: () => ( + /* binding */ + DeiTPreTrainedModel + ), + /* harmony export */ + DepthAnythingForDepthEstimation: () => ( + /* binding */ + DepthAnythingForDepthEstimation + ), + /* harmony export */ + DepthAnythingPreTrainedModel: () => ( + /* binding */ + DepthAnythingPreTrainedModel + ), + /* harmony export */ + DepthProForDepthEstimation: () => ( + /* binding */ + DepthProForDepthEstimation + ), + /* harmony export */ + DepthProPreTrainedModel: () => ( + /* binding */ + DepthProPreTrainedModel + ), + /* harmony export */ + DetrForObjectDetection: () => ( + /* binding */ + DetrForObjectDetection + ), + /* harmony export */ + DetrForSegmentation: () => ( + /* binding */ + DetrForSegmentation + ), + /* harmony export */ + DetrModel: () => ( + /* binding */ + DetrModel + ), + /* harmony export */ + DetrObjectDetectionOutput: () => ( + /* binding */ + DetrObjectDetectionOutput + ), + /* harmony export */ + DetrPreTrainedModel: () => ( + /* binding */ + DetrPreTrainedModel + ), + /* harmony export */ + DetrSegmentationOutput: () => ( + /* binding */ + DetrSegmentationOutput + ), + /* harmony export */ + Dinov2ForImageClassification: () => ( + /* binding */ + Dinov2ForImageClassification + ), + /* harmony export */ + Dinov2Model: () => ( + /* binding */ + Dinov2Model + ), + /* harmony export */ + Dinov2PreTrainedModel: () => ( + /* binding */ + Dinov2PreTrainedModel + ), + /* harmony export */ + DistilBertForMaskedLM: () => ( + /* binding */ + DistilBertForMaskedLM + ), + /* harmony export */ + DistilBertForQuestionAnswering: () => ( + /* binding */ + DistilBertForQuestionAnswering + ), + /* harmony export */ + DistilBertForSequenceClassification: () => ( + /* binding */ + DistilBertForSequenceClassification + ), + /* harmony export */ + DistilBertForTokenClassification: () => ( + /* binding */ + DistilBertForTokenClassification + ), + /* harmony export */ + DistilBertModel: () => ( + /* binding */ + DistilBertModel + ), + /* harmony export */ + DistilBertPreTrainedModel: () => ( + /* binding */ + DistilBertPreTrainedModel + ), + /* harmony export */ + DonutSwinModel: () => ( + /* binding */ + DonutSwinModel + ), + /* harmony export */ + DonutSwinPreTrainedModel: () => ( + /* binding */ + DonutSwinPreTrainedModel + ), + /* harmony export */ + EfficientNetForImageClassification: () => ( + /* binding */ + EfficientNetForImageClassification + ), + /* harmony export */ + EfficientNetModel: () => ( + /* binding */ + EfficientNetModel + ), + /* harmony export */ + EfficientNetPreTrainedModel: () => ( + /* binding */ + EfficientNetPreTrainedModel + ), + /* harmony export */ + ElectraForMaskedLM: () => ( + /* binding */ + ElectraForMaskedLM + ), + /* harmony export */ + ElectraForQuestionAnswering: () => ( + /* binding */ + ElectraForQuestionAnswering + ), + /* harmony export */ + ElectraForSequenceClassification: () => ( + /* binding */ + ElectraForSequenceClassification + ), + /* harmony export */ + ElectraForTokenClassification: () => ( + /* binding */ + ElectraForTokenClassification + ), + /* harmony export */ + ElectraModel: () => ( + /* binding */ + ElectraModel + ), + /* harmony export */ + ElectraPreTrainedModel: () => ( + /* binding */ + ElectraPreTrainedModel + ), + /* harmony export */ + EsmForMaskedLM: () => ( + /* binding */ + EsmForMaskedLM + ), + /* harmony export */ + EsmForSequenceClassification: () => ( + /* binding */ + EsmForSequenceClassification + ), + /* harmony export */ + EsmForTokenClassification: () => ( + /* binding */ + EsmForTokenClassification + ), + /* harmony export */ + EsmModel: () => ( + /* binding */ + EsmModel + ), + /* harmony export */ + EsmPreTrainedModel: () => ( + /* binding */ + EsmPreTrainedModel + ), + /* harmony export */ + FalconForCausalLM: () => ( + /* binding */ + FalconForCausalLM + ), + /* harmony export */ + FalconModel: () => ( + /* binding */ + FalconModel + ), + /* harmony export */ + FalconPreTrainedModel: () => ( + /* binding */ + FalconPreTrainedModel + ), + /* harmony export */ + FastViTForImageClassification: () => ( + /* binding */ + FastViTForImageClassification + ), + /* harmony export */ + FastViTModel: () => ( + /* binding */ + FastViTModel + ), + /* harmony export */ + FastViTPreTrainedModel: () => ( + /* binding */ + FastViTPreTrainedModel + ), + /* harmony export */ + Florence2ForConditionalGeneration: () => ( + /* binding */ + Florence2ForConditionalGeneration + ), + /* harmony export */ + Florence2PreTrainedModel: () => ( + /* binding */ + Florence2PreTrainedModel + ), + /* harmony export */ + GLPNForDepthEstimation: () => ( + /* binding */ + GLPNForDepthEstimation + ), + /* harmony export */ + GLPNModel: () => ( + /* binding */ + GLPNModel + ), + /* harmony export */ + GLPNPreTrainedModel: () => ( + /* binding */ + GLPNPreTrainedModel + ), + /* harmony export */ + GPT2LMHeadModel: () => ( + /* binding */ + GPT2LMHeadModel + ), + /* harmony export */ + GPT2Model: () => ( + /* binding */ + GPT2Model + ), + /* harmony export */ + GPT2PreTrainedModel: () => ( + /* binding */ + GPT2PreTrainedModel + ), + /* harmony export */ + GPTBigCodeForCausalLM: () => ( + /* binding */ + GPTBigCodeForCausalLM + ), + /* harmony export */ + GPTBigCodeModel: () => ( + /* binding */ + GPTBigCodeModel + ), + /* harmony export */ + GPTBigCodePreTrainedModel: () => ( + /* binding */ + GPTBigCodePreTrainedModel + ), + /* harmony export */ + GPTJForCausalLM: () => ( + /* binding */ + GPTJForCausalLM + ), + /* harmony export */ + GPTJModel: () => ( + /* binding */ + GPTJModel + ), + /* harmony export */ + GPTJPreTrainedModel: () => ( + /* binding */ + GPTJPreTrainedModel + ), + /* harmony export */ + GPTNeoForCausalLM: () => ( + /* binding */ + GPTNeoForCausalLM + ), + /* harmony export */ + GPTNeoModel: () => ( + /* binding */ + GPTNeoModel + ), + /* harmony export */ + GPTNeoPreTrainedModel: () => ( + /* binding */ + GPTNeoPreTrainedModel + ), + /* harmony export */ + GPTNeoXForCausalLM: () => ( + /* binding */ + GPTNeoXForCausalLM + ), + /* harmony export */ + GPTNeoXModel: () => ( + /* binding */ + GPTNeoXModel + ), + /* harmony export */ + GPTNeoXPreTrainedModel: () => ( + /* binding */ + GPTNeoXPreTrainedModel + ), + /* harmony export */ + Gemma2ForCausalLM: () => ( + /* binding */ + Gemma2ForCausalLM + ), + /* harmony export */ + Gemma2Model: () => ( + /* binding */ + Gemma2Model + ), + /* harmony export */ + Gemma2PreTrainedModel: () => ( + /* binding */ + Gemma2PreTrainedModel + ), + /* harmony export */ + GemmaForCausalLM: () => ( + /* binding */ + GemmaForCausalLM + ), + /* harmony export */ + GemmaModel: () => ( + /* binding */ + GemmaModel + ), + /* harmony export */ + GemmaPreTrainedModel: () => ( + /* binding */ + GemmaPreTrainedModel + ), + /* harmony export */ + GraniteForCausalLM: () => ( + /* binding */ + GraniteForCausalLM + ), + /* harmony export */ + GraniteModel: () => ( + /* binding */ + GraniteModel + ), + /* harmony export */ + GranitePreTrainedModel: () => ( + /* binding */ + GranitePreTrainedModel + ), + /* harmony export */ + GroupViTModel: () => ( + /* binding */ + GroupViTModel + ), + /* harmony export */ + GroupViTPreTrainedModel: () => ( + /* binding */ + GroupViTPreTrainedModel + ), + /* harmony export */ + HieraForImageClassification: () => ( + /* binding */ + HieraForImageClassification + ), + /* harmony export */ + HieraModel: () => ( + /* binding */ + HieraModel + ), + /* harmony export */ + HieraPreTrainedModel: () => ( + /* binding */ + HieraPreTrainedModel + ), + /* harmony export */ + HubertForCTC: () => ( + /* binding */ + HubertForCTC + ), + /* harmony export */ + HubertForSequenceClassification: () => ( + /* binding */ + HubertForSequenceClassification + ), + /* harmony export */ + HubertModel: () => ( + /* binding */ + HubertModel + ), + /* harmony export */ + HubertPreTrainedModel: () => ( + /* binding */ + HubertPreTrainedModel + ), + /* harmony export */ + ImageMattingOutput: () => ( + /* binding */ + ImageMattingOutput + ), + /* harmony export */ + JAISLMHeadModel: () => ( + /* binding */ + JAISLMHeadModel + ), + /* harmony export */ + JAISModel: () => ( + /* binding */ + JAISModel + ), + /* harmony export */ + JAISPreTrainedModel: () => ( + /* binding */ + JAISPreTrainedModel + ), + /* harmony export */ + LlamaForCausalLM: () => ( + /* binding */ + LlamaForCausalLM + ), + /* harmony export */ + LlamaModel: () => ( + /* binding */ + LlamaModel + ), + /* harmony export */ + LlamaPreTrainedModel: () => ( + /* binding */ + LlamaPreTrainedModel + ), + /* harmony export */ + LlavaForConditionalGeneration: () => ( + /* binding */ + LlavaForConditionalGeneration + ), + /* harmony export */ + LlavaPreTrainedModel: () => ( + /* binding */ + LlavaPreTrainedModel + ), + /* harmony export */ + LongT5ForConditionalGeneration: () => ( + /* binding */ + LongT5ForConditionalGeneration + ), + /* harmony export */ + LongT5Model: () => ( + /* binding */ + LongT5Model + ), + /* harmony export */ + LongT5PreTrainedModel: () => ( + /* binding */ + LongT5PreTrainedModel + ), + /* harmony export */ + M2M100ForConditionalGeneration: () => ( + /* binding */ + M2M100ForConditionalGeneration + ), + /* harmony export */ + M2M100Model: () => ( + /* binding */ + M2M100Model + ), + /* harmony export */ + M2M100PreTrainedModel: () => ( + /* binding */ + M2M100PreTrainedModel + ), + /* harmony export */ + MBartForCausalLM: () => ( + /* binding */ + MBartForCausalLM + ), + /* harmony export */ + MBartForConditionalGeneration: () => ( + /* binding */ + MBartForConditionalGeneration + ), + /* harmony export */ + MBartForSequenceClassification: () => ( + /* binding */ + MBartForSequenceClassification + ), + /* harmony export */ + MBartModel: () => ( + /* binding */ + MBartModel + ), + /* harmony export */ + MBartPreTrainedModel: () => ( + /* binding */ + MBartPreTrainedModel + ), + /* harmony export */ + MPNetForMaskedLM: () => ( + /* binding */ + MPNetForMaskedLM + ), + /* harmony export */ + MPNetForQuestionAnswering: () => ( + /* binding */ + MPNetForQuestionAnswering + ), + /* harmony export */ + MPNetForSequenceClassification: () => ( + /* binding */ + MPNetForSequenceClassification + ), + /* harmony export */ + MPNetForTokenClassification: () => ( + /* binding */ + MPNetForTokenClassification + ), + /* harmony export */ + MPNetModel: () => ( + /* binding */ + MPNetModel + ), + /* harmony export */ + MPNetPreTrainedModel: () => ( + /* binding */ + MPNetPreTrainedModel + ), + /* harmony export */ + MT5ForConditionalGeneration: () => ( + /* binding */ + MT5ForConditionalGeneration + ), + /* harmony export */ + MT5Model: () => ( + /* binding */ + MT5Model + ), + /* harmony export */ + MT5PreTrainedModel: () => ( + /* binding */ + MT5PreTrainedModel + ), + /* harmony export */ + MarianMTModel: () => ( + /* binding */ + MarianMTModel + ), + /* harmony export */ + MarianModel: () => ( + /* binding */ + MarianModel + ), + /* harmony export */ + MarianPreTrainedModel: () => ( + /* binding */ + MarianPreTrainedModel + ), + /* harmony export */ + MaskFormerForInstanceSegmentation: () => ( + /* binding */ + MaskFormerForInstanceSegmentation + ), + /* harmony export */ + MaskFormerModel: () => ( + /* binding */ + MaskFormerModel + ), + /* harmony export */ + MaskFormerPreTrainedModel: () => ( + /* binding */ + MaskFormerPreTrainedModel + ), + /* harmony export */ + MaskedLMOutput: () => ( + /* binding */ + MaskedLMOutput + ), + /* harmony export */ + MistralForCausalLM: () => ( + /* binding */ + MistralForCausalLM + ), + /* harmony export */ + MistralModel: () => ( + /* binding */ + MistralModel + ), + /* harmony export */ + MistralPreTrainedModel: () => ( + /* binding */ + MistralPreTrainedModel + ), + /* harmony export */ + MobileBertForMaskedLM: () => ( + /* binding */ + MobileBertForMaskedLM + ), + /* harmony export */ + MobileBertForQuestionAnswering: () => ( + /* binding */ + MobileBertForQuestionAnswering + ), + /* harmony export */ + MobileBertForSequenceClassification: () => ( + /* binding */ + MobileBertForSequenceClassification + ), + /* harmony export */ + MobileBertModel: () => ( + /* binding */ + MobileBertModel + ), + /* harmony export */ + MobileBertPreTrainedModel: () => ( + /* binding */ + MobileBertPreTrainedModel + ), + /* harmony export */ + MobileNetV1ForImageClassification: () => ( + /* binding */ + MobileNetV1ForImageClassification + ), + /* harmony export */ + MobileNetV1Model: () => ( + /* binding */ + MobileNetV1Model + ), + /* harmony export */ + MobileNetV1PreTrainedModel: () => ( + /* binding */ + MobileNetV1PreTrainedModel + ), + /* harmony export */ + MobileNetV2ForImageClassification: () => ( + /* binding */ + MobileNetV2ForImageClassification + ), + /* harmony export */ + MobileNetV2Model: () => ( + /* binding */ + MobileNetV2Model + ), + /* harmony export */ + MobileNetV2PreTrainedModel: () => ( + /* binding */ + MobileNetV2PreTrainedModel + ), + /* harmony export */ + MobileNetV3ForImageClassification: () => ( + /* binding */ + MobileNetV3ForImageClassification + ), + /* harmony export */ + MobileNetV3Model: () => ( + /* binding */ + MobileNetV3Model + ), + /* harmony export */ + MobileNetV3PreTrainedModel: () => ( + /* binding */ + MobileNetV3PreTrainedModel + ), + /* harmony export */ + MobileNetV4ForImageClassification: () => ( + /* binding */ + MobileNetV4ForImageClassification + ), + /* harmony export */ + MobileNetV4Model: () => ( + /* binding */ + MobileNetV4Model + ), + /* harmony export */ + MobileNetV4PreTrainedModel: () => ( + /* binding */ + MobileNetV4PreTrainedModel + ), + /* harmony export */ + MobileViTForImageClassification: () => ( + /* binding */ + MobileViTForImageClassification + ), + /* harmony export */ + MobileViTModel: () => ( + /* binding */ + MobileViTModel + ), + /* harmony export */ + MobileViTPreTrainedModel: () => ( + /* binding */ + MobileViTPreTrainedModel + ), + /* harmony export */ + MobileViTV2ForImageClassification: () => ( + /* binding */ + MobileViTV2ForImageClassification + ), + /* harmony export */ + MobileViTV2Model: () => ( + /* binding */ + MobileViTV2Model + ), + /* harmony export */ + MobileViTV2PreTrainedModel: () => ( + /* binding */ + MobileViTV2PreTrainedModel + ), + /* harmony export */ + ModelOutput: () => ( + /* binding */ + ModelOutput + ), + /* harmony export */ + Moondream1ForConditionalGeneration: () => ( + /* binding */ + Moondream1ForConditionalGeneration + ), + /* harmony export */ + MptForCausalLM: () => ( + /* binding */ + MptForCausalLM + ), + /* harmony export */ + MptModel: () => ( + /* binding */ + MptModel + ), + /* harmony export */ + MptPreTrainedModel: () => ( + /* binding */ + MptPreTrainedModel + ), + /* harmony export */ + MusicgenForCausalLM: () => ( + /* binding */ + MusicgenForCausalLM + ), + /* harmony export */ + MusicgenForConditionalGeneration: () => ( + /* binding */ + MusicgenForConditionalGeneration + ), + /* harmony export */ + MusicgenModel: () => ( + /* binding */ + MusicgenModel + ), + /* harmony export */ + MusicgenPreTrainedModel: () => ( + /* binding */ + MusicgenPreTrainedModel + ), + /* harmony export */ + NomicBertModel: () => ( + /* binding */ + NomicBertModel + ), + /* harmony export */ + NomicBertPreTrainedModel: () => ( + /* binding */ + NomicBertPreTrainedModel + ), + /* harmony export */ + OPTForCausalLM: () => ( + /* binding */ + OPTForCausalLM + ), + /* harmony export */ + OPTModel: () => ( + /* binding */ + OPTModel + ), + /* harmony export */ + OPTPreTrainedModel: () => ( + /* binding */ + OPTPreTrainedModel + ), + /* harmony export */ + OpenELMForCausalLM: () => ( + /* binding */ + OpenELMForCausalLM + ), + /* harmony export */ + OpenELMModel: () => ( + /* binding */ + OpenELMModel + ), + /* harmony export */ + OpenELMPreTrainedModel: () => ( + /* binding */ + OpenELMPreTrainedModel + ), + /* harmony export */ + OwlViTForObjectDetection: () => ( + /* binding */ + OwlViTForObjectDetection + ), + /* harmony export */ + OwlViTModel: () => ( + /* binding */ + OwlViTModel + ), + /* harmony export */ + OwlViTPreTrainedModel: () => ( + /* binding */ + OwlViTPreTrainedModel + ), + /* harmony export */ + Owlv2ForObjectDetection: () => ( + /* binding */ + Owlv2ForObjectDetection + ), + /* harmony export */ + Owlv2Model: () => ( + /* binding */ + Owlv2Model + ), + /* harmony export */ + Owlv2PreTrainedModel: () => ( + /* binding */ + Owlv2PreTrainedModel + ), + /* harmony export */ + Phi3ForCausalLM: () => ( + /* binding */ + Phi3ForCausalLM + ), + /* harmony export */ + Phi3Model: () => ( + /* binding */ + Phi3Model + ), + /* harmony export */ + Phi3PreTrainedModel: () => ( + /* binding */ + Phi3PreTrainedModel + ), + /* harmony export */ + PhiForCausalLM: () => ( + /* binding */ + PhiForCausalLM + ), + /* harmony export */ + PhiModel: () => ( + /* binding */ + PhiModel + ), + /* harmony export */ + PhiPreTrainedModel: () => ( + /* binding */ + PhiPreTrainedModel + ), + /* harmony export */ + PreTrainedModel: () => ( + /* binding */ + PreTrainedModel + ), + /* harmony export */ + PretrainedMixin: () => ( + /* binding */ + PretrainedMixin + ), + /* harmony export */ + PvtForImageClassification: () => ( + /* binding */ + PvtForImageClassification + ), + /* harmony export */ + PvtModel: () => ( + /* binding */ + PvtModel + ), + /* harmony export */ + PvtPreTrainedModel: () => ( + /* binding */ + PvtPreTrainedModel + ), + /* harmony export */ + PyAnnoteForAudioFrameClassification: () => ( + /* binding */ + PyAnnoteForAudioFrameClassification + ), + /* harmony export */ + PyAnnoteModel: () => ( + /* binding */ + PyAnnoteModel + ), + /* harmony export */ + PyAnnotePreTrainedModel: () => ( + /* binding */ + PyAnnotePreTrainedModel + ), + /* harmony export */ + QuestionAnsweringModelOutput: () => ( + /* binding */ + QuestionAnsweringModelOutput + ), + /* harmony export */ + Qwen2ForCausalLM: () => ( + /* binding */ + Qwen2ForCausalLM + ), + /* harmony export */ + Qwen2Model: () => ( + /* binding */ + Qwen2Model + ), + /* harmony export */ + Qwen2PreTrainedModel: () => ( + /* binding */ + Qwen2PreTrainedModel + ), + /* harmony export */ + RTDetrForObjectDetection: () => ( + /* binding */ + RTDetrForObjectDetection + ), + /* harmony export */ + RTDetrModel: () => ( + /* binding */ + RTDetrModel + ), + /* harmony export */ + RTDetrObjectDetectionOutput: () => ( + /* binding */ + RTDetrObjectDetectionOutput + ), + /* harmony export */ + RTDetrPreTrainedModel: () => ( + /* binding */ + RTDetrPreTrainedModel + ), + /* harmony export */ + ResNetForImageClassification: () => ( + /* binding */ + ResNetForImageClassification + ), + /* harmony export */ + ResNetModel: () => ( + /* binding */ + ResNetModel + ), + /* harmony export */ + ResNetPreTrainedModel: () => ( + /* binding */ + ResNetPreTrainedModel + ), + /* harmony export */ + RoFormerForMaskedLM: () => ( + /* binding */ + RoFormerForMaskedLM + ), + /* harmony export */ + RoFormerForQuestionAnswering: () => ( + /* binding */ + RoFormerForQuestionAnswering + ), + /* harmony export */ + RoFormerForSequenceClassification: () => ( + /* binding */ + RoFormerForSequenceClassification + ), + /* harmony export */ + RoFormerForTokenClassification: () => ( + /* binding */ + RoFormerForTokenClassification + ), + /* harmony export */ + RoFormerModel: () => ( + /* binding */ + RoFormerModel + ), + /* harmony export */ + RoFormerPreTrainedModel: () => ( + /* binding */ + RoFormerPreTrainedModel + ), + /* harmony export */ + RobertaForMaskedLM: () => ( + /* binding */ + RobertaForMaskedLM + ), + /* harmony export */ + RobertaForQuestionAnswering: () => ( + /* binding */ + RobertaForQuestionAnswering + ), + /* harmony export */ + RobertaForSequenceClassification: () => ( + /* binding */ + RobertaForSequenceClassification + ), + /* harmony export */ + RobertaForTokenClassification: () => ( + /* binding */ + RobertaForTokenClassification + ), + /* harmony export */ + RobertaModel: () => ( + /* binding */ + RobertaModel + ), + /* harmony export */ + RobertaPreTrainedModel: () => ( + /* binding */ + RobertaPreTrainedModel + ), + /* harmony export */ + SamImageSegmentationOutput: () => ( + /* binding */ + SamImageSegmentationOutput + ), + /* harmony export */ + SamModel: () => ( + /* binding */ + SamModel + ), + /* harmony export */ + SamPreTrainedModel: () => ( + /* binding */ + SamPreTrainedModel + ), + /* harmony export */ + SapiensForDepthEstimation: () => ( + /* binding */ + SapiensForDepthEstimation + ), + /* harmony export */ + SapiensForNormalEstimation: () => ( + /* binding */ + SapiensForNormalEstimation + ), + /* harmony export */ + SapiensForSemanticSegmentation: () => ( + /* binding */ + SapiensForSemanticSegmentation + ), + /* harmony export */ + SapiensPreTrainedModel: () => ( + /* binding */ + SapiensPreTrainedModel + ), + /* harmony export */ + SegformerForImageClassification: () => ( + /* binding */ + SegformerForImageClassification + ), + /* harmony export */ + SegformerForSemanticSegmentation: () => ( + /* binding */ + SegformerForSemanticSegmentation + ), + /* harmony export */ + SegformerModel: () => ( + /* binding */ + SegformerModel + ), + /* harmony export */ + SegformerPreTrainedModel: () => ( + /* binding */ + SegformerPreTrainedModel + ), + /* harmony export */ + Seq2SeqLMOutput: () => ( + /* binding */ + Seq2SeqLMOutput + ), + /* harmony export */ + SequenceClassifierOutput: () => ( + /* binding */ + SequenceClassifierOutput + ), + /* harmony export */ + SiglipModel: () => ( + /* binding */ + SiglipModel + ), + /* harmony export */ + SiglipPreTrainedModel: () => ( + /* binding */ + SiglipPreTrainedModel + ), + /* harmony export */ + SiglipTextModel: () => ( + /* binding */ + SiglipTextModel + ), + /* harmony export */ + SiglipVisionModel: () => ( + /* binding */ + SiglipVisionModel + ), + /* harmony export */ + SpeechT5ForSpeechToText: () => ( + /* binding */ + SpeechT5ForSpeechToText + ), + /* harmony export */ + SpeechT5ForTextToSpeech: () => ( + /* binding */ + SpeechT5ForTextToSpeech + ), + /* harmony export */ + SpeechT5HifiGan: () => ( + /* binding */ + SpeechT5HifiGan + ), + /* harmony export */ + SpeechT5Model: () => ( + /* binding */ + SpeechT5Model + ), + /* harmony export */ + SpeechT5PreTrainedModel: () => ( + /* binding */ + SpeechT5PreTrainedModel + ), + /* harmony export */ + SqueezeBertForMaskedLM: () => ( + /* binding */ + SqueezeBertForMaskedLM + ), + /* harmony export */ + SqueezeBertForQuestionAnswering: () => ( + /* binding */ + SqueezeBertForQuestionAnswering + ), + /* harmony export */ + SqueezeBertForSequenceClassification: () => ( + /* binding */ + SqueezeBertForSequenceClassification + ), + /* harmony export */ + SqueezeBertModel: () => ( + /* binding */ + SqueezeBertModel + ), + /* harmony export */ + SqueezeBertPreTrainedModel: () => ( + /* binding */ + SqueezeBertPreTrainedModel + ), + /* harmony export */ + StableLmForCausalLM: () => ( + /* binding */ + StableLmForCausalLM + ), + /* harmony export */ + StableLmModel: () => ( + /* binding */ + StableLmModel + ), + /* harmony export */ + StableLmPreTrainedModel: () => ( + /* binding */ + StableLmPreTrainedModel + ), + /* harmony export */ + Starcoder2ForCausalLM: () => ( + /* binding */ + Starcoder2ForCausalLM + ), + /* harmony export */ + Starcoder2Model: () => ( + /* binding */ + Starcoder2Model + ), + /* harmony export */ + Starcoder2PreTrainedModel: () => ( + /* binding */ + Starcoder2PreTrainedModel + ), + /* harmony export */ + Swin2SRForImageSuperResolution: () => ( + /* binding */ + Swin2SRForImageSuperResolution + ), + /* harmony export */ + Swin2SRModel: () => ( + /* binding */ + Swin2SRModel + ), + /* harmony export */ + Swin2SRPreTrainedModel: () => ( + /* binding */ + Swin2SRPreTrainedModel + ), + /* harmony export */ + SwinForImageClassification: () => ( + /* binding */ + SwinForImageClassification + ), + /* harmony export */ + SwinModel: () => ( + /* binding */ + SwinModel + ), + /* harmony export */ + SwinPreTrainedModel: () => ( + /* binding */ + SwinPreTrainedModel + ), + /* harmony export */ + T5ForConditionalGeneration: () => ( + /* binding */ + T5ForConditionalGeneration + ), + /* harmony export */ + T5Model: () => ( + /* binding */ + T5Model + ), + /* harmony export */ + T5PreTrainedModel: () => ( + /* binding */ + T5PreTrainedModel + ), + /* harmony export */ + TableTransformerForObjectDetection: () => ( + /* binding */ + TableTransformerForObjectDetection + ), + /* harmony export */ + TableTransformerModel: () => ( + /* binding */ + TableTransformerModel + ), + /* harmony export */ + TableTransformerObjectDetectionOutput: () => ( + /* binding */ + TableTransformerObjectDetectionOutput + ), + /* harmony export */ + TableTransformerPreTrainedModel: () => ( + /* binding */ + TableTransformerPreTrainedModel + ), + /* harmony export */ + TokenClassifierOutput: () => ( + /* binding */ + TokenClassifierOutput + ), + /* harmony export */ + TrOCRForCausalLM: () => ( + /* binding */ + TrOCRForCausalLM + ), + /* harmony export */ + TrOCRPreTrainedModel: () => ( + /* binding */ + TrOCRPreTrainedModel + ), + /* harmony export */ + UniSpeechForCTC: () => ( + /* binding */ + UniSpeechForCTC + ), + /* harmony export */ + UniSpeechForSequenceClassification: () => ( + /* binding */ + UniSpeechForSequenceClassification + ), + /* harmony export */ + UniSpeechModel: () => ( + /* binding */ + UniSpeechModel + ), + /* harmony export */ + UniSpeechPreTrainedModel: () => ( + /* binding */ + UniSpeechPreTrainedModel + ), + /* harmony export */ + UniSpeechSatForAudioFrameClassification: () => ( + /* binding */ + UniSpeechSatForAudioFrameClassification + ), + /* harmony export */ + UniSpeechSatForCTC: () => ( + /* binding */ + UniSpeechSatForCTC + ), + /* harmony export */ + UniSpeechSatForSequenceClassification: () => ( + /* binding */ + UniSpeechSatForSequenceClassification + ), + /* harmony export */ + UniSpeechSatModel: () => ( + /* binding */ + UniSpeechSatModel + ), + /* harmony export */ + UniSpeechSatPreTrainedModel: () => ( + /* binding */ + UniSpeechSatPreTrainedModel + ), + /* harmony export */ + ViTForImageClassification: () => ( + /* binding */ + ViTForImageClassification + ), + /* harmony export */ + ViTMAEModel: () => ( + /* binding */ + ViTMAEModel + ), + /* harmony export */ + ViTMAEPreTrainedModel: () => ( + /* binding */ + ViTMAEPreTrainedModel + ), + /* harmony export */ + ViTMSNForImageClassification: () => ( + /* binding */ + ViTMSNForImageClassification + ), + /* harmony export */ + ViTMSNModel: () => ( + /* binding */ + ViTMSNModel + ), + /* harmony export */ + ViTMSNPreTrainedModel: () => ( + /* binding */ + ViTMSNPreTrainedModel + ), + /* harmony export */ + ViTModel: () => ( + /* binding */ + ViTModel + ), + /* harmony export */ + ViTPreTrainedModel: () => ( + /* binding */ + ViTPreTrainedModel + ), + /* harmony export */ + VisionEncoderDecoderModel: () => ( + /* binding */ + VisionEncoderDecoderModel + ), + /* harmony export */ + VitMatteForImageMatting: () => ( + /* binding */ + VitMatteForImageMatting + ), + /* harmony export */ + VitMattePreTrainedModel: () => ( + /* binding */ + VitMattePreTrainedModel + ), + /* harmony export */ + VitsModel: () => ( + /* binding */ + VitsModel + ), + /* harmony export */ + VitsModelOutput: () => ( + /* binding */ + VitsModelOutput + ), + /* harmony export */ + VitsPreTrainedModel: () => ( + /* binding */ + VitsPreTrainedModel + ), + /* harmony export */ + Wav2Vec2BertForCTC: () => ( + /* binding */ + Wav2Vec2BertForCTC + ), + /* harmony export */ + Wav2Vec2BertForSequenceClassification: () => ( + /* binding */ + Wav2Vec2BertForSequenceClassification + ), + /* harmony export */ + Wav2Vec2BertModel: () => ( + /* binding */ + Wav2Vec2BertModel + ), + /* harmony export */ + Wav2Vec2BertPreTrainedModel: () => ( + /* binding */ + Wav2Vec2BertPreTrainedModel + ), + /* harmony export */ + Wav2Vec2ForAudioFrameClassification: () => ( + /* binding */ + Wav2Vec2ForAudioFrameClassification + ), + /* harmony export */ + Wav2Vec2ForCTC: () => ( + /* binding */ + Wav2Vec2ForCTC + ), + /* harmony export */ + Wav2Vec2ForSequenceClassification: () => ( + /* binding */ + Wav2Vec2ForSequenceClassification + ), + /* harmony export */ + Wav2Vec2Model: () => ( + /* binding */ + Wav2Vec2Model + ), + /* harmony export */ + Wav2Vec2PreTrainedModel: () => ( + /* binding */ + Wav2Vec2PreTrainedModel + ), + /* harmony export */ + WavLMForAudioFrameClassification: () => ( + /* binding */ + WavLMForAudioFrameClassification + ), + /* harmony export */ + WavLMForCTC: () => ( + /* binding */ + WavLMForCTC + ), + /* harmony export */ + WavLMForSequenceClassification: () => ( + /* binding */ + WavLMForSequenceClassification + ), + /* harmony export */ + WavLMForXVector: () => ( + /* binding */ + WavLMForXVector + ), + /* harmony export */ + WavLMModel: () => ( + /* binding */ + WavLMModel + ), + /* harmony export */ + WavLMPreTrainedModel: () => ( + /* binding */ + WavLMPreTrainedModel + ), + /* harmony export */ + WeSpeakerResNetModel: () => ( + /* binding */ + WeSpeakerResNetModel + ), + /* harmony export */ + WeSpeakerResNetPreTrainedModel: () => ( + /* binding */ + WeSpeakerResNetPreTrainedModel + ), + /* harmony export */ + WhisperForConditionalGeneration: () => ( + /* binding */ + WhisperForConditionalGeneration + ), + /* harmony export */ + WhisperModel: () => ( + /* binding */ + WhisperModel + ), + /* harmony export */ + WhisperPreTrainedModel: () => ( + /* binding */ + WhisperPreTrainedModel + ), + /* harmony export */ + XLMForQuestionAnswering: () => ( + /* binding */ + XLMForQuestionAnswering + ), + /* harmony export */ + XLMForSequenceClassification: () => ( + /* binding */ + XLMForSequenceClassification + ), + /* harmony export */ + XLMForTokenClassification: () => ( + /* binding */ + XLMForTokenClassification + ), + /* harmony export */ + XLMModel: () => ( + /* binding */ + XLMModel + ), + /* harmony export */ + XLMPreTrainedModel: () => ( + /* binding */ + XLMPreTrainedModel + ), + /* harmony export */ + XLMRobertaForMaskedLM: () => ( + /* binding */ + XLMRobertaForMaskedLM + ), + /* harmony export */ + XLMRobertaForQuestionAnswering: () => ( + /* binding */ + XLMRobertaForQuestionAnswering + ), + /* harmony export */ + XLMRobertaForSequenceClassification: () => ( + /* binding */ + XLMRobertaForSequenceClassification + ), + /* harmony export */ + XLMRobertaForTokenClassification: () => ( + /* binding */ + XLMRobertaForTokenClassification + ), + /* harmony export */ + XLMRobertaModel: () => ( + /* binding */ + XLMRobertaModel + ), + /* harmony export */ + XLMRobertaPreTrainedModel: () => ( + /* binding */ + XLMRobertaPreTrainedModel + ), + /* harmony export */ + XLMWithLMHeadModel: () => ( + /* binding */ + XLMWithLMHeadModel + ), + /* harmony export */ + XVectorOutput: () => ( + /* binding */ + XVectorOutput + ), + /* harmony export */ + YolosForObjectDetection: () => ( + /* binding */ + YolosForObjectDetection + ), + /* harmony export */ + YolosModel: () => ( + /* binding */ + YolosModel + ), + /* harmony export */ + YolosObjectDetectionOutput: () => ( + /* binding */ + YolosObjectDetectionOutput + ), + /* harmony export */ + YolosPreTrainedModel: () => ( + /* binding */ + YolosPreTrainedModel + ) + /* harmony export */ + }); + var _configs_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__2( + /*! ./configs.js */ + "./src/configs.js" + ); + var _backends_onnx_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__2( + /*! ./backends/onnx.js */ + "./src/backends/onnx.js" + ); + var _utils_dtypes_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__2( + /*! ./utils/dtypes.js */ + "./src/utils/dtypes.js" + ); + var _utils_generic_js__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__2( + /*! ./utils/generic.js */ + "./src/utils/generic.js" + ); + var _utils_core_js__WEBPACK_IMPORTED_MODULE_4__ = __webpack_require__2( + /*! ./utils/core.js */ + "./src/utils/core.js" + ); + var _utils_hub_js__WEBPACK_IMPORTED_MODULE_5__ = __webpack_require__2( + /*! ./utils/hub.js */ + "./src/utils/hub.js" + ); + var _utils_constants_js__WEBPACK_IMPORTED_MODULE_6__ = __webpack_require__2( + /*! ./utils/constants.js */ + "./src/utils/constants.js" + ); + var _generation_logits_process_js__WEBPACK_IMPORTED_MODULE_7__ = __webpack_require__2( + /*! ./generation/logits_process.js */ + "./src/generation/logits_process.js" + ); + var _generation_configuration_utils_js__WEBPACK_IMPORTED_MODULE_8__ = __webpack_require__2( + /*! ./generation/configuration_utils.js */ + "./src/generation/configuration_utils.js" + ); + var _utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__ = __webpack_require__2( + /*! ./utils/tensor.js */ + "./src/utils/tensor.js" + ); + var _utils_maths_js__WEBPACK_IMPORTED_MODULE_10__ = __webpack_require__2( + /*! ./utils/maths.js */ + "./src/utils/maths.js" + ); + var _generation_stopping_criteria_js__WEBPACK_IMPORTED_MODULE_11__2 = __webpack_require__2( + /*! ./generation/stopping_criteria.js */ + "./src/generation/stopping_criteria.js" + ); + var _generation_logits_sampler_js__WEBPACK_IMPORTED_MODULE_12__ = __webpack_require__2( + /*! ./generation/logits_sampler.js */ + "./src/generation/logits_sampler.js" + ); + var _env_js__WEBPACK_IMPORTED_MODULE_13__ = __webpack_require__2( + /*! ./env.js */ + "./src/env.js" + ); + var _models_whisper_generation_whisper_js__WEBPACK_IMPORTED_MODULE_14__ = __webpack_require__2( + /*! ./models/whisper/generation_whisper.js */ + "./src/models/whisper/generation_whisper.js" + ); + var _models_whisper_common_whisper_js__WEBPACK_IMPORTED_MODULE_15__ = __webpack_require__2( + /*! ./models/whisper/common_whisper.js */ + "./src/models/whisper/common_whisper.js" + ); + const MODEL_TYPES = { + EncoderOnly: 0, + EncoderDecoder: 1, + Seq2Seq: 2, + Vision2Seq: 3, + DecoderOnly: 4, + MaskGeneration: 5, + ImageTextToText: 6, + Musicgen: 7 + }; + const MODEL_TYPE_MAPPING = /* @__PURE__ */ new Map(); + const MODEL_NAME_TO_CLASS_MAPPING = /* @__PURE__ */ new Map(); + const MODEL_CLASS_TO_NAME_MAPPING = /* @__PURE__ */ new Map(); + async function getSession(pretrained_model_name_or_path, fileName, options) { + var _a; + const custom_config = ((_a = options.config) == null ? void 0 : _a["transformers.js_config"]) ?? {}; + let device = options.device ?? custom_config.device; + if (device && typeof device !== "string") { + if (device.hasOwnProperty(fileName)) { + device = device[fileName]; + } else { + console.warn(`device not specified for "${fileName}". Using the default device.`); + device = null; + } + } + const selectedDevice = ( + /** @type {import("./utils/devices.js").DeviceType} */ + device ?? (_env_js__WEBPACK_IMPORTED_MODULE_13__.apis.IS_NODE_ENV ? "cpu" : "wasm") + ); + const executionProviders = (0, _backends_onnx_js__WEBPACK_IMPORTED_MODULE_1__.deviceToExecutionProviders)(selectedDevice); + let dtype = options.dtype ?? custom_config.dtype; + if (typeof dtype !== "string") { + if (dtype && dtype.hasOwnProperty(fileName)) { + dtype = dtype[fileName]; + } else { + dtype = _utils_dtypes_js__WEBPACK_IMPORTED_MODULE_2__.DEFAULT_DEVICE_DTYPE_MAPPING[selectedDevice] ?? _utils_dtypes_js__WEBPACK_IMPORTED_MODULE_2__.DATA_TYPES.fp32; + console.warn(`dtype not specified for "${fileName}". Using the default dtype (${dtype}) for this device (${selectedDevice}).`); + } + } + const selectedDtype = ( + /** @type {import("./utils/dtypes.js").DataType} */ + dtype + ); + if (!_utils_dtypes_js__WEBPACK_IMPORTED_MODULE_2__.DEFAULT_DTYPE_SUFFIX_MAPPING.hasOwnProperty(selectedDtype)) { + throw new Error(`Invalid dtype: ${selectedDtype}. Should be one of: ${Object.keys(_utils_dtypes_js__WEBPACK_IMPORTED_MODULE_2__.DATA_TYPES).join(", ")}`); + } else if (selectedDtype === _utils_dtypes_js__WEBPACK_IMPORTED_MODULE_2__.DATA_TYPES.fp16 && selectedDevice === "webgpu" && !await (0, _utils_dtypes_js__WEBPACK_IMPORTED_MODULE_2__.isWebGpuFp16Supported)()) { + throw new Error(`The device (${selectedDevice}) does not support fp16.`); + } + const kv_cache_dtype = custom_config.kv_cache_dtype ? typeof custom_config.kv_cache_dtype === "string" ? custom_config.kv_cache_dtype : custom_config.kv_cache_dtype[selectedDtype] ?? "float32" : void 0; + if (kv_cache_dtype && !["float32", "float16"].includes(kv_cache_dtype)) { + throw new Error(`Invalid kv_cache_dtype: ${kv_cache_dtype}. Should be one of: float32, float16`); + } + const session_config = { + dtype: selectedDtype, + kv_cache_dtype + }; + const suffix = _utils_dtypes_js__WEBPACK_IMPORTED_MODULE_2__.DEFAULT_DTYPE_SUFFIX_MAPPING[selectedDtype]; + const modelFileName = `${options.subfolder ?? ""}/${fileName}${suffix}.onnx`; + const session_options = { ...options.session_options }; + session_options.executionProviders ?? (session_options.executionProviders = executionProviders); + const free_dimension_overrides = custom_config.free_dimension_overrides; + if (free_dimension_overrides) { + session_options.freeDimensionOverrides ?? (session_options.freeDimensionOverrides = free_dimension_overrides); + } else if (selectedDevice.startsWith("webnn") && !session_options.freeDimensionOverrides) { + console.warn( + 'WebNN does not currently support dynamic shapes and requires `free_dimension_overrides` to be set in config.json as a field within "transformers.js_config". When `free_dimension_overrides` is not set, you may experience significant performance degradation.' + ); + } + const bufferPromise = (0, _utils_hub_js__WEBPACK_IMPORTED_MODULE_5__.getModelFile)(pretrained_model_name_or_path, modelFileName, true, options); + const use_external_data_format = options.use_external_data_format ?? custom_config.use_external_data_format; + let externalDataPromises = []; + if (use_external_data_format && (use_external_data_format === true || typeof use_external_data_format === "object" && use_external_data_format.hasOwnProperty(fileName) && use_external_data_format[fileName] === true)) { + if (_env_js__WEBPACK_IMPORTED_MODULE_13__.apis.IS_NODE_ENV) { + throw new Error("External data format is not yet supported in Node.js"); + } + const path = `${fileName}${suffix}.onnx_data`; + const fullPath = `${options.subfolder ?? ""}/${path}`; + externalDataPromises.push(new Promise(async (resolve, reject) => { + const data = await (0, _utils_hub_js__WEBPACK_IMPORTED_MODULE_5__.getModelFile)(pretrained_model_name_or_path, fullPath, true, options); + resolve({ path, data }); + })); + } else if (session_options.externalData !== void 0) { + externalDataPromises = session_options.externalData.map(async (ext) => { + if (typeof ext.data === "string") { + const ext_buffer = await (0, _utils_hub_js__WEBPACK_IMPORTED_MODULE_5__.getModelFile)(pretrained_model_name_or_path, ext.data, true, options); + return { ...ext, data: ext_buffer }; + } + return ext; + }); + } + if (externalDataPromises.length > 0) { + session_options.externalData = await Promise.all(externalDataPromises); + } + if (selectedDevice === "webgpu") { + const shapes = (0, _configs_js__WEBPACK_IMPORTED_MODULE_0__.getKeyValueShapes)(options.config, { + prefix: "present" + }); + if (Object.keys(shapes).length > 0 && !(0, _backends_onnx_js__WEBPACK_IMPORTED_MODULE_1__.isONNXProxy)()) { + const preferredOutputLocation = {}; + for (const key in shapes) { + preferredOutputLocation[key] = "gpu-buffer"; + } + session_options.preferredOutputLocation = preferredOutputLocation; + } + } + const buffer = await bufferPromise; + return { buffer, session_options, session_config }; + } + async function constructSessions(pretrained_model_name_or_path, names, options) { + return Object.fromEntries(await Promise.all( + Object.keys(names).map(async (name) => { + const { buffer, session_options, session_config } = await getSession(pretrained_model_name_or_path, names[name], options); + const session = await (0, _backends_onnx_js__WEBPACK_IMPORTED_MODULE_1__.createInferenceSession)(buffer, session_options, session_config); + return [name, session]; + }) + )); + } + async function getOptionalConfigs(pretrained_model_name_or_path, names, options) { + return Object.fromEntries(await Promise.all( + Object.keys(names).map(async (name) => { + const config = await (0, _utils_hub_js__WEBPACK_IMPORTED_MODULE_5__.getModelJSON)(pretrained_model_name_or_path, names[name], false, options); + return [name, config]; + }) + )); + } + function validateInputs(session, inputs) { + const checkedInputs = /* @__PURE__ */ Object.create(null); + const missingInputs = []; + for (const inputName of session.inputNames) { + const tensor = inputs[inputName]; + if (!(tensor instanceof _utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.Tensor)) { + missingInputs.push(inputName); + continue; + } + checkedInputs[inputName] = (0, _backends_onnx_js__WEBPACK_IMPORTED_MODULE_1__.isONNXProxy)() ? tensor.clone() : tensor; + } + if (missingInputs.length > 0) { + throw new Error( + `An error occurred during model execution: "Missing the following inputs: ${missingInputs.join(", ")}.` + ); + } + const numInputsProvided = Object.keys(inputs).length; + const numInputsNeeded = session.inputNames.length; + if (numInputsProvided > numInputsNeeded) { + let ignored = Object.keys(inputs).filter((inputName) => !session.inputNames.includes(inputName)); + console.warn(`WARNING: Too many inputs were provided (${numInputsProvided} > ${numInputsNeeded}). The following inputs will be ignored: "${ignored.join(", ")}".`); + } + return checkedInputs; + } + async function sessionRun(session, inputs) { + const checkedInputs = validateInputs(session, inputs); + try { + const ortFeed = Object.fromEntries(Object.entries(checkedInputs).map(([k, v]) => [k, v.ort_tensor])); + let output = await session.run(ortFeed); + output = replaceTensors(output); + return output; + } catch (e) { + console.error(`An error occurred during model execution: "${e}".`); + console.error("Inputs given to model:", checkedInputs); + throw e; + } + } + function replaceTensors(obj) { + for (let prop in obj) { + if ((0, _backends_onnx_js__WEBPACK_IMPORTED_MODULE_1__.isONNXTensor)(obj[prop])) { + obj[prop] = new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.Tensor(obj[prop]); + } else if (typeof obj[prop] === "object") { + replaceTensors(obj[prop]); + } + } + return obj; + } + function toI64Tensor(items) { + if (items instanceof _utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.Tensor) { + return items; + } + if (items.length === 0) { + throw Error("items must be non-empty"); + } + if (Array.isArray(items[0])) { + if (items.some((x) => x.length !== items[0].length)) { + throw Error("Unable to create tensor, you should probably activate truncation and/or padding with 'padding=True' and/or 'truncation=True' to have batched tensors with the same length."); + } + return new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.Tensor( + "int64", + BigInt64Array.from(items.flat().map((x) => BigInt(x))), + [items.length, items[0].length] + ); + } else { + return new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.Tensor( + "int64", + BigInt64Array.from(items.map((x) => BigInt(x))), + [1, items.length] + ); + } + } + function boolTensor(value) { + return new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.Tensor("bool", [value], [1]); + } + async function seq2seqForward(self2, model_inputs) { + let { encoder_outputs, input_ids, decoder_input_ids, ...other_decoder_inputs } = model_inputs; + if (!encoder_outputs) { + const encoder_inputs = (0, _utils_core_js__WEBPACK_IMPORTED_MODULE_4__.pick)(model_inputs, self2.sessions["model"].inputNames); + encoder_outputs = (await encoderForward(self2, encoder_inputs)).last_hidden_state; + } + other_decoder_inputs.input_ids = decoder_input_ids; + other_decoder_inputs.encoder_hidden_states = encoder_outputs; + if (self2.sessions["decoder_model_merged"].inputNames.includes("encoder_attention_mask")) { + other_decoder_inputs.encoder_attention_mask = model_inputs.attention_mask; + } + const decoderResults = await decoderForward(self2, other_decoder_inputs, true); + return decoderResults; + } + async function encoderForward(self2, model_inputs) { + const session = self2.sessions["model"]; + const encoderFeeds = (0, _utils_core_js__WEBPACK_IMPORTED_MODULE_4__.pick)(model_inputs, session.inputNames); + if (session.inputNames.includes("inputs_embeds") && !encoderFeeds.inputs_embeds) { + if (!model_inputs.input_ids) { + throw new Error("Both `input_ids` and `inputs_embeds` are missing in the model inputs."); + } + encoderFeeds.inputs_embeds = await self2.encode_text({ input_ids: model_inputs.input_ids }); + } + if (session.inputNames.includes("token_type_ids") && !encoderFeeds.token_type_ids) { + encoderFeeds.token_type_ids = new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.Tensor( + "int64", + new BigInt64Array(encoderFeeds.input_ids.data.length), + encoderFeeds.input_ids.dims + ); + } + return await sessionRun(session, encoderFeeds); + } + async function decoderForward(self2, model_inputs, is_encoder_decoder = false) { + const session = self2.sessions[is_encoder_decoder ? "decoder_model_merged" : "model"]; + const { past_key_values, ...new_model_inputs } = model_inputs; + if (session.inputNames.includes("use_cache_branch")) { + new_model_inputs.use_cache_branch = boolTensor(!!past_key_values); + } + if (session.inputNames.includes("position_ids") && new_model_inputs.attention_mask && !new_model_inputs.position_ids) { + new_model_inputs.position_ids = createPositionIds(new_model_inputs, past_key_values); + } + self2.addPastKeyValues(new_model_inputs, past_key_values); + const fixed = (0, _utils_core_js__WEBPACK_IMPORTED_MODULE_4__.pick)(new_model_inputs, session.inputNames); + return await sessionRun(session, fixed); + } + async function imageTextToTextForward(self2, { + // Produced by the tokenizer/processor: + input_ids = null, + attention_mask = null, + pixel_values = null, + // Used during generation: + position_ids = null, + inputs_embeds = null, + past_key_values = null, + // Generic generation parameters + generation_config = null, + logits_processor = null, + // TODO: needed? + ...kwargs + }) { + if (!inputs_embeds) { + inputs_embeds = await self2.encode_text({ input_ids }); + if (pixel_values && input_ids.dims[1] !== 1) { + const image_features = await self2.encode_image({ pixel_values }); + ({ inputs_embeds, attention_mask } = self2._merge_input_ids_with_image_features({ + image_features, + inputs_embeds, + input_ids, + attention_mask + })); + } else if (past_key_values && pixel_values && input_ids.dims[1] === 1) { + const target_length = input_ids.dims[1]; + const past_length = Object.values(past_key_values)[0].dims.at(-2); + attention_mask = (0, _utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.cat)([ + (0, _utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.ones)([input_ids.dims[0], past_length]), + attention_mask.slice(null, [attention_mask.dims[1] - target_length, attention_mask.dims[1]]) + ], 1); + } + } + const outputs = await decoderForward(self2, { + inputs_embeds, + past_key_values, + attention_mask, + position_ids, + generation_config, + logits_processor + }, true); + return outputs; + } + function createPositionIds(model_inputs, past_key_values = null) { + const { input_ids, inputs_embeds, attention_mask } = model_inputs; + const [bz, seq_len] = attention_mask.dims; + const data = new BigInt64Array(attention_mask.data.length); + for (let i = 0; i < bz; ++i) { + const start = i * seq_len; + let sum = BigInt(0); + for (let j = 0; j < seq_len; ++j) { + const index = start + j; + if (attention_mask.data[index] === 0n) { + data[index] = BigInt(1); + } else { + data[index] = sum; + sum += attention_mask.data[index]; + } + } + } + let position_ids = new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.Tensor("int64", data, attention_mask.dims); + if (past_key_values) { + const offset = -(input_ids ?? inputs_embeds).dims.at(1); + position_ids = position_ids.slice(null, [offset, null]); + } + return position_ids; + } + function decoder_prepare_inputs_for_generation(self2, input_ids, model_inputs, generation_config) { + if (model_inputs.past_key_values) { + const past_length = Object.values(model_inputs.past_key_values)[0].dims.at(-2); + const { input_ids: input_ids2, attention_mask } = model_inputs; + if (attention_mask && attention_mask.dims[1] > input_ids2.dims[1]) { + } else if (past_length < input_ids2.dims[1]) { + model_inputs.input_ids = input_ids2.slice(null, [past_length, null]); + } else { + if ( + // NOTE: Only used by VLMs (!= so that null matches undefined) + self2.config.image_token_index != null && // Equivalent to `self.config.image_token_index in input_ids` (== so that int matches bigint) + input_ids2.data.some((x) => x == self2.config.image_token_index) + ) { + const num_image_tokens = self2.config.num_image_tokens; + if (!num_image_tokens) { + throw new Error("`num_image_tokens` is missing in the model configuration."); + } + const num_new_tokens = input_ids2.dims[1] - (past_length - num_image_tokens); + model_inputs.input_ids = input_ids2.slice(null, [-num_new_tokens, null]); + model_inputs.attention_mask = (0, _utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.ones)([1, past_length + num_new_tokens]); + } + } + } + return model_inputs; + } + function encoder_decoder_prepare_inputs_for_generation(self2, input_ids, model_inputs, generation_config) { + if (model_inputs.past_key_values) { + input_ids = input_ids.map((x) => [x.at(-1)]); + } + return { + ...model_inputs, + decoder_input_ids: toI64Tensor(input_ids) + }; + } + function image_text_to_text_prepare_inputs_for_generation(self2, ...args) { + if (self2.config.is_encoder_decoder) { + return encoder_decoder_prepare_inputs_for_generation(self2, ...args); + } else { + return decoder_prepare_inputs_for_generation(self2, ...args); + } + } + class PreTrainedModel extends _utils_generic_js__WEBPACK_IMPORTED_MODULE_3__.Callable { + /** + * Creates a new instance of the `PreTrainedModel` class. + * @param {import('./configs.js').PretrainedConfig} config The model configuration. + * @param {Record} sessions The inference sessions for the model. + * @param {Record} configs Additional configuration files (e.g., generation_config.json). + */ + constructor(config, sessions, configs) { + super(); + __publicField(this, "main_input_name", "input_ids"); + __publicField(this, "forward_params", ["input_ids", "attention_mask"]); + this.config = config; + this.sessions = sessions; + this.configs = configs; + const modelName = MODEL_CLASS_TO_NAME_MAPPING.get(this.constructor); + const modelType = MODEL_TYPE_MAPPING.get(modelName); + this.can_generate = false; + this._forward = null; + this._prepare_inputs_for_generation = null; + switch (modelType) { + case MODEL_TYPES.DecoderOnly: + this.can_generate = true; + this._forward = decoderForward; + this._prepare_inputs_for_generation = decoder_prepare_inputs_for_generation; + break; + case MODEL_TYPES.Seq2Seq: + case MODEL_TYPES.Vision2Seq: + case MODEL_TYPES.Musicgen: + this.can_generate = true; + this._forward = seq2seqForward; + this._prepare_inputs_for_generation = encoder_decoder_prepare_inputs_for_generation; + break; + case MODEL_TYPES.EncoderDecoder: + this._forward = seq2seqForward; + break; + case MODEL_TYPES.ImageTextToText: + this.can_generate = true; + this._forward = imageTextToTextForward; + this._prepare_inputs_for_generation = image_text_to_text_prepare_inputs_for_generation; + break; + default: + this._forward = encoderForward; + break; + } + if (this.can_generate) { + this.forward_params.push("past_key_values"); + } + this.custom_config = this.config["transformers.js_config"] ?? {}; + } + /** + * Disposes of all the ONNX sessions that were created during inference. + * @returns {Promise} An array of promises, one for each ONNX session that is being disposed. + * @todo Use https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/FinalizationRegistry + */ + async dispose() { + var _a; + const promises = []; + for (const session of Object.values(this.sessions)) { + if ((_a = session == null ? void 0 : session.handler) == null ? void 0 : _a.dispose) { + promises.push(session.handler.dispose()); + } + } + return await Promise.all(promises); + } + /** + * Instantiate one of the model classes of the library from a pretrained model. + * + * The model class to instantiate is selected based on the `model_type` property of the config object + * (either passed as an argument or loaded from `pretrained_model_name_or_path` if possible) + * + * @param {string} pretrained_model_name_or_path The name or path of the pretrained model. Can be either: + * - A string, the *model id* of a pretrained model hosted inside a model repo on huggingface.co. + * Valid model ids can be located at the root-level, like `bert-base-uncased`, or namespaced under a + * user or organization name, like `dbmdz/bert-base-german-cased`. + * - A path to a *directory* containing model weights, e.g., `./my_model_directory/`. + * @param {import('./utils/hub.js').PretrainedModelOptions} options Additional options for loading the model. + * + * @returns {Promise} A new instance of the `PreTrainedModel` class. + */ + static async from_pretrained(pretrained_model_name_or_path, { + progress_callback = null, + config = null, + cache_dir = null, + local_files_only = false, + revision = "main", + model_file_name = null, + subfolder = "onnx", + device = null, + dtype = null, + use_external_data_format = null, + session_options = {} + } = {}) { + let options = { + progress_callback, + config, + cache_dir, + local_files_only, + revision, + model_file_name, + subfolder, + device, + dtype, + use_external_data_format, + session_options + }; + const modelName = MODEL_CLASS_TO_NAME_MAPPING.get(this); + const modelType = MODEL_TYPE_MAPPING.get(modelName); + config = options.config = await _configs_js__WEBPACK_IMPORTED_MODULE_0__.AutoConfig.from_pretrained(pretrained_model_name_or_path, options); + let info; + if (modelType === MODEL_TYPES.DecoderOnly) { + info = await Promise.all([ + constructSessions(pretrained_model_name_or_path, { + model: options.model_file_name ?? "model" + }, options), + getOptionalConfigs(pretrained_model_name_or_path, { + generation_config: "generation_config.json" + }, options) + ]); + } else if (modelType === MODEL_TYPES.Seq2Seq || modelType === MODEL_TYPES.Vision2Seq) { + info = await Promise.all([ + constructSessions(pretrained_model_name_or_path, { + model: "encoder_model", + decoder_model_merged: "decoder_model_merged" + }, options), + getOptionalConfigs(pretrained_model_name_or_path, { + generation_config: "generation_config.json" + }, options) + ]); + } else if (modelType === MODEL_TYPES.MaskGeneration) { + info = await Promise.all([ + constructSessions(pretrained_model_name_or_path, { + model: "vision_encoder", + prompt_encoder_mask_decoder: "prompt_encoder_mask_decoder" + }, options) + ]); + } else if (modelType === MODEL_TYPES.EncoderDecoder) { + info = await Promise.all([ + constructSessions(pretrained_model_name_or_path, { + model: "encoder_model", + decoder_model_merged: "decoder_model_merged" + }, options) + ]); + } else if (modelType === MODEL_TYPES.ImageTextToText) { + const sessions = { + embed_tokens: "embed_tokens", + vision_encoder: "vision_encoder", + decoder_model_merged: "decoder_model_merged" + }; + if (config.is_encoder_decoder) { + sessions["model"] = "encoder_model"; + } + info = await Promise.all([ + constructSessions(pretrained_model_name_or_path, sessions, options), + getOptionalConfigs(pretrained_model_name_or_path, { + generation_config: "generation_config.json" + }, options) + ]); + } else if (modelType === MODEL_TYPES.Musicgen) { + info = await Promise.all([ + constructSessions(pretrained_model_name_or_path, { + model: "text_encoder", + decoder_model_merged: "decoder_model_merged", + encodec_decode: "encodec_decode" + }, options), + getOptionalConfigs(pretrained_model_name_or_path, { + generation_config: "generation_config.json" + }, options) + ]); + } else { + if (modelType !== MODEL_TYPES.EncoderOnly) { + console.warn(`Model type for '${modelName ?? (config == null ? void 0 : config.model_type)}' not found, assuming encoder-only architecture. Please report this at ${_utils_constants_js__WEBPACK_IMPORTED_MODULE_6__.GITHUB_ISSUE_URL}.`); + } + info = await Promise.all([ + constructSessions(pretrained_model_name_or_path, { + model: options.model_file_name ?? "model" + }, options) + ]); + } + return new this(config, ...info); + } + /** + * Runs the model with the provided inputs + * @param {Object} model_inputs Object containing input tensors + * @returns {Promise} Object containing output tensors + */ + async _call(model_inputs) { + return await this.forward(model_inputs); + } + /** + * Forward method for a pretrained model. If not overridden by a subclass, the correct forward method + * will be chosen based on the model type. + * @param {Object} model_inputs The input data to the model in the format specified in the ONNX model. + * @returns {Promise} The output data from the model in the format specified in the ONNX model. + * @throws {Error} This method must be implemented in subclasses. + */ + async forward(model_inputs) { + return await this._forward(this, model_inputs); + } + /** + * Get the model's generation config, if it exists. + * @returns {GenerationConfig|null} The model's generation config if it exists, otherwise `null`. + */ + get generation_config() { + var _a; + return ((_a = this.configs) == null ? void 0 : _a.generation_config) ?? null; + } + /** + * This function returns a [`LogitsProcessorList`] list object that contains all relevant [`LogitsWarper`] + * instances used for multinomial sampling. + * @param {GenerationConfig} generation_config The generation config. + * @returns {LogitsProcessorList} generation_config + */ + _get_logits_warper(generation_config) { + const warpers = new _generation_logits_process_js__WEBPACK_IMPORTED_MODULE_7__.LogitsProcessorList(); + if (generation_config.temperature !== null && generation_config.temperature !== 1) { + warpers.push(new _generation_logits_process_js__WEBPACK_IMPORTED_MODULE_7__.TemperatureLogitsWarper(generation_config.temperature)); + } + if (generation_config.top_k !== null && generation_config.top_k !== 0) { + warpers.push(new _generation_logits_process_js__WEBPACK_IMPORTED_MODULE_7__.TopKLogitsWarper(generation_config.top_k)); + } + if (generation_config.top_p !== null && generation_config.top_p < 1) { + warpers.push(new _generation_logits_process_js__WEBPACK_IMPORTED_MODULE_7__.TopPLogitsWarper(generation_config.top_p)); + } + return warpers; + } + /** + * @param {GenerationConfig} generation_config + * @param {number} input_ids_seq_length The starting sequence length for the input ids. + * @returns {LogitsProcessorList} + * @private + */ + _get_logits_processor(generation_config, input_ids_seq_length, logits_processor = null) { + const processors = new _generation_logits_process_js__WEBPACK_IMPORTED_MODULE_7__.LogitsProcessorList(); + if (generation_config.repetition_penalty !== null && generation_config.repetition_penalty !== 1) { + processors.push(new _generation_logits_process_js__WEBPACK_IMPORTED_MODULE_7__.RepetitionPenaltyLogitsProcessor(generation_config.repetition_penalty)); + } + if (generation_config.no_repeat_ngram_size !== null && generation_config.no_repeat_ngram_size > 0) { + processors.push(new _generation_logits_process_js__WEBPACK_IMPORTED_MODULE_7__.NoRepeatNGramLogitsProcessor(generation_config.no_repeat_ngram_size)); + } + if (generation_config.bad_words_ids !== null) { + processors.push(new _generation_logits_process_js__WEBPACK_IMPORTED_MODULE_7__.NoBadWordsLogitsProcessor(generation_config.bad_words_ids, generation_config.eos_token_id)); + } + if (generation_config.min_length !== null && generation_config.eos_token_id !== null && generation_config.min_length > 0) { + processors.push(new _generation_logits_process_js__WEBPACK_IMPORTED_MODULE_7__.MinLengthLogitsProcessor(generation_config.min_length, generation_config.eos_token_id)); + } + if (generation_config.min_new_tokens !== null && generation_config.eos_token_id !== null && generation_config.min_new_tokens > 0) { + processors.push(new _generation_logits_process_js__WEBPACK_IMPORTED_MODULE_7__.MinNewTokensLengthLogitsProcessor( + input_ids_seq_length, + generation_config.min_new_tokens, + generation_config.eos_token_id + )); + } + if (generation_config.forced_bos_token_id !== null) { + processors.push(new _generation_logits_process_js__WEBPACK_IMPORTED_MODULE_7__.ForcedBOSTokenLogitsProcessor(generation_config.forced_bos_token_id)); + } + if (generation_config.forced_eos_token_id !== null) { + processors.push(new _generation_logits_process_js__WEBPACK_IMPORTED_MODULE_7__.ForcedEOSTokenLogitsProcessor( + generation_config.max_length, + generation_config.forced_eos_token_id + )); + } + if (generation_config.begin_suppress_tokens !== null) { + const begin_index = input_ids_seq_length > 1 || generation_config.forced_bos_token_id === null ? input_ids_seq_length : input_ids_seq_length + 1; + processors.push(new _generation_logits_process_js__WEBPACK_IMPORTED_MODULE_7__.SuppressTokensAtBeginLogitsProcessor(generation_config.begin_suppress_tokens, begin_index)); + } + if (generation_config.guidance_scale !== null && generation_config.guidance_scale > 1) { + processors.push(new _generation_logits_process_js__WEBPACK_IMPORTED_MODULE_7__.ClassifierFreeGuidanceLogitsProcessor(generation_config.guidance_scale)); + } + if (logits_processor !== null) { + processors.extend(logits_processor); + } + return processors; + } + /** + * This function merges multiple generation configs together to form a final generation config to be used by the model for text generation. + * It first creates an empty `GenerationConfig` object, then it applies the model's own `generation_config` property to it. Finally, if a `generation_config` object was passed in the arguments, it overwrites the corresponding properties in the final config with those of the passed config object. + * @param {GenerationConfig|null} generation_config A `GenerationConfig` object containing generation parameters. + * @param {Object} kwargs Additional generation parameters to be used in place of those in the `generation_config` object. + * @returns {GenerationConfig} The final generation config object to be used by the model for text generation. + */ + _prepare_generation_config(generation_config, kwargs, cls = _generation_configuration_utils_js__WEBPACK_IMPORTED_MODULE_8__.GenerationConfig) { + const config = { ...this.config }; + for (const key of ["decoder", "generator", "text_config"]) { + if (key in config) { + Object.assign(config, config[key]); + } + } + const gen_config = new cls(config); + Object.assign(gen_config, this.generation_config ?? {}); + if (generation_config) { + Object.assign(gen_config, generation_config); + } + if (kwargs) { + Object.assign(gen_config, (0, _utils_core_js__WEBPACK_IMPORTED_MODULE_4__.pick)(kwargs, Object.getOwnPropertyNames(gen_config))); + } + return gen_config; + } + /** + * + * @param {GenerationConfig} generation_config + * @param {StoppingCriteriaList} [stopping_criteria=null] + */ + _get_stopping_criteria(generation_config, stopping_criteria = null) { + const criteria = new _generation_stopping_criteria_js__WEBPACK_IMPORTED_MODULE_11__2.StoppingCriteriaList(); + if (generation_config.max_length !== null) { + criteria.push(new _generation_stopping_criteria_js__WEBPACK_IMPORTED_MODULE_11__2.MaxLengthCriteria( + generation_config.max_length, + this.config.max_position_embeddings ?? null + )); + } + if (generation_config.eos_token_id !== null) { + criteria.push(new _generation_stopping_criteria_js__WEBPACK_IMPORTED_MODULE_11__2.EosTokenCriteria(generation_config.eos_token_id)); + } + if (stopping_criteria) { + criteria.extend(stopping_criteria); + } + return criteria; + } + /** + * Confirms that the model class is compatible with generation. + * If not, raises an exception that points to the right class to use. + */ + _validate_model_class() { + if (!this.can_generate) { + const generate_compatible_mappings = [ + MODEL_FOR_CAUSAL_LM_MAPPING_NAMES, + // MODEL_FOR_CAUSAL_IMAGE_MODELING_MAPPING, // TODO + MODEL_FOR_VISION_2_SEQ_MAPPING_NAMES, + MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING_NAMES, + MODEL_FOR_SPEECH_SEQ_2_SEQ_MAPPING_NAMES + ]; + const modelName = MODEL_CLASS_TO_NAME_MAPPING.get(this.constructor); + const generate_compatible_classes = /* @__PURE__ */ new Set(); + const modelType = this.config.model_type; + for (const model_mapping of generate_compatible_mappings) { + const supported_models = model_mapping.get(modelType); + if (supported_models) { + generate_compatible_classes.add(supported_models[0]); + } + } + let errorMessage = `The current model class (${modelName}) is not compatible with \`.generate()\`, as it doesn't have a language model head.`; + if (generate_compatible_classes.size > 0) { + errorMessage += ` Please use the following class instead: ${[...generate_compatible_classes].join(", ")}`; + } + throw Error(errorMessage); + } + } + prepare_inputs_for_generation(...args) { + return this._prepare_inputs_for_generation(this, ...args); + } + /** + * + * @param {Object} inputs + * @param {bigint[][]} inputs.generated_input_ids + * @param {Object} inputs.outputs + * @param {Object} inputs.model_inputs + * @param {boolean} inputs.is_encoder_decoder + * @returns {Object} The updated model inputs for the next generation iteration. + */ + _update_model_kwargs_for_generation({ generated_input_ids, outputs, model_inputs, is_encoder_decoder }) { + model_inputs["past_key_values"] = this.getPastKeyValues(outputs, model_inputs.past_key_values); + model_inputs["input_ids"] = new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.Tensor("int64", generated_input_ids.flat(), [generated_input_ids.length, 1]); + if (!is_encoder_decoder) { + model_inputs.attention_mask = (0, _utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.cat)( + [ + model_inputs.attention_mask, + (0, _utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.ones)([model_inputs.attention_mask.dims[0], 1]) + ], + 1 + ); + } else if ("decoder_attention_mask" in model_inputs) { + } + model_inputs["position_ids"] = null; + return model_inputs; + } + /** + * This function extracts the model-specific `inputs` for generation. + * @param {Object} params + * @param {Tensor} [params.inputs=null] + * @param {number} [params.bos_token_id=null] + * @param {Record} [params.model_kwargs] + * @returns {{inputs_tensor: Tensor, model_inputs: Record, model_input_name: string}} The model-specific inputs for generation. + */ + _prepare_model_inputs({ inputs, bos_token_id, model_kwargs }) { + const model_inputs = (0, _utils_core_js__WEBPACK_IMPORTED_MODULE_4__.pick)(model_kwargs, this.forward_params); + const input_name = this.main_input_name; + if (input_name in model_inputs) { + if (inputs) { + throw new Error( + "`inputs`: {inputs}` were passed alongside {input_name} which is not allowed. Make sure to either pass {inputs} or {input_name}=..." + ); + } + } else { + model_inputs[input_name] = inputs; + } + const inputs_tensor = model_inputs[input_name]; + return { inputs_tensor, model_inputs, model_input_name: input_name }; + } + async _prepare_encoder_decoder_kwargs_for_generation({ inputs_tensor, model_inputs, model_input_name, generation_config }) { + if (this.sessions["model"].inputNames.includes("inputs_embeds") && !model_inputs.inputs_embeds && "_prepare_inputs_embeds" in this) { + const { input_ids, pixel_values, attention_mask, ...kwargs } = model_inputs; + const prepared_inputs = await this._prepare_inputs_embeds(model_inputs); + model_inputs = { + ...kwargs, + ...(0, _utils_core_js__WEBPACK_IMPORTED_MODULE_4__.pick)(prepared_inputs, ["inputs_embeds", "attention_mask"]) + }; + } + let { last_hidden_state } = await encoderForward(this, model_inputs); + if (generation_config.guidance_scale !== null && generation_config.guidance_scale > 1) { + last_hidden_state = (0, _utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.cat)([ + last_hidden_state, + (0, _utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.full_like)(last_hidden_state, 0) + ], 0); + if ("attention_mask" in model_inputs) { + model_inputs["attention_mask"] = (0, _utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.cat)([ + model_inputs["attention_mask"], + (0, _utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.zeros_like)(model_inputs["attention_mask"]) + ], 0); + } + } else if (model_inputs.decoder_input_ids) { + const decoder_input_ids_batch_size = toI64Tensor(model_inputs.decoder_input_ids).dims[0]; + if (decoder_input_ids_batch_size !== last_hidden_state.dims[0]) { + if (last_hidden_state.dims[0] !== 1) { + throw new Error( + `The encoder outputs have a different batch size (${last_hidden_state.dims[0]}) than the decoder inputs (${decoder_input_ids_batch_size}).` + ); + } + last_hidden_state = (0, _utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.cat)(Array.from({ length: decoder_input_ids_batch_size }, () => last_hidden_state), 0); + } + } + model_inputs["encoder_outputs"] = last_hidden_state; + return model_inputs; + } + /** + * Prepares `decoder_input_ids` for generation with encoder-decoder models + * @param {*} param0 + */ + _prepare_decoder_input_ids_for_generation({ batch_size, model_input_name, model_kwargs, decoder_start_token_id, bos_token_id, generation_config }) { + let { decoder_input_ids, ...model_inputs } = model_kwargs; + if (!decoder_input_ids) { + decoder_start_token_id ?? (decoder_start_token_id = bos_token_id); + if (this.config.model_type === "musicgen") { + decoder_input_ids = Array.from({ + length: batch_size * this.config.decoder.num_codebooks + }, () => [decoder_start_token_id]); + } else if (Array.isArray(decoder_start_token_id)) { + if (decoder_start_token_id.length !== batch_size) { + throw new Error( + `\`decoder_start_token_id\` expcted to have length ${batch_size} but got ${decoder_start_token_id.length}` + ); + } + decoder_input_ids = decoder_start_token_id; + } else { + decoder_input_ids = Array.from({ + length: batch_size + }, () => [decoder_start_token_id]); + } + } else if (!Array.isArray(decoder_input_ids[0])) { + decoder_input_ids = Array.from({ + length: batch_size + }, () => decoder_input_ids); + } + decoder_input_ids = toI64Tensor(decoder_input_ids); + model_kwargs["decoder_attention_mask"] = (0, _utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.ones_like)(decoder_input_ids); + return { input_ids: decoder_input_ids, model_inputs }; + } + /** + * Generates sequences of token ids for models with a language modeling head. + * @param {import('./generation/parameters.js').GenerationFunctionParameters} options + * @returns {Promise} The output of the model, which can contain the generated token ids, attentions, and scores. + */ + async generate({ + inputs = null, + generation_config = null, + logits_processor = null, + stopping_criteria = null, + streamer = null, + // inputs_attention_mask = null, + ...kwargs + }) { + this._validate_model_class(); + generation_config = this._prepare_generation_config(generation_config, kwargs); + let { inputs_tensor, model_inputs, model_input_name } = this._prepare_model_inputs({ + inputs, + model_kwargs: kwargs + }); + const is_encoder_decoder = this.config.is_encoder_decoder; + if (!is_encoder_decoder) { + } else if (!("encoder_outputs" in model_inputs)) { + model_inputs = await this._prepare_encoder_decoder_kwargs_for_generation( + { inputs_tensor, model_inputs, model_input_name, generation_config } + ); + } + let input_ids; + if (is_encoder_decoder) { + ({ input_ids, model_inputs } = this._prepare_decoder_input_ids_for_generation({ + batch_size: model_inputs[model_input_name].dims.at(0), + model_input_name, + model_kwargs: model_inputs, + decoder_start_token_id: generation_config.decoder_start_token_id, + bos_token_id: generation_config.bos_token_id, + generation_config + })); + } else { + input_ids = model_inputs[model_input_name]; + } + let input_ids_length = input_ids.dims.at(-1); + if (generation_config.max_new_tokens !== null) { + generation_config.max_length = input_ids_length + generation_config.max_new_tokens; + } + const prepared_logits_processor = this._get_logits_processor( + generation_config, + input_ids_length, + logits_processor + ); + const prepared_stopping_criteria = this._get_stopping_criteria( + generation_config, + stopping_criteria + ); + const numInputs = model_inputs[model_input_name].dims.at(0); + const sampler = _generation_logits_sampler_js__WEBPACK_IMPORTED_MODULE_12__.LogitsSampler.getSampler(generation_config); + const scores = new Array(numInputs).fill(0); + const all_input_ids = input_ids.tolist(); + if (streamer) { + streamer.put(all_input_ids); + } + let outputs; + let attentions = {}; + while (true) { + model_inputs = this.prepare_inputs_for_generation(all_input_ids, model_inputs, generation_config); + outputs = await this.forward(model_inputs); + if (generation_config.output_attentions && generation_config.return_dict_in_generate) { + const token_attentions = this.getAttentions(outputs); + for (const key in token_attentions) { + if (!(key in attentions)) { + attentions[key] = []; + } + attentions[key].push(token_attentions[key]); + } + } + const logits = outputs.logits.slice(null, -1, null); + const next_tokens_scores = prepared_logits_processor(all_input_ids, logits); + const generated_input_ids = []; + for (let batch_idx = 0; batch_idx < next_tokens_scores.dims.at(0); ++batch_idx) { + const logs = next_tokens_scores[batch_idx]; + const sampledTokens = await sampler(logs); + for (const [newTokenId, logProb] of sampledTokens) { + const bigint = BigInt(newTokenId); + scores[batch_idx] += logProb; + all_input_ids[batch_idx].push(bigint); + generated_input_ids.push([bigint]); + break; + } + } + if (streamer) { + streamer.put(generated_input_ids); + } + const stop = prepared_stopping_criteria(all_input_ids); + if (stop.every((x) => x)) { + break; + } + model_inputs = this._update_model_kwargs_for_generation({ + generated_input_ids, + outputs, + model_inputs, + is_encoder_decoder + }); + } + if (streamer) { + streamer.end(); + } + const past_key_values = this.getPastKeyValues(outputs, model_inputs.past_key_values, true); + const sequences = new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.Tensor("int64", all_input_ids.flat(), [all_input_ids.length, all_input_ids[0].length]); + if (generation_config.return_dict_in_generate) { + return { + sequences, + past_key_values, + ...attentions + // TODO: + // scores, + // logits, + }; + } else { + for (const tensor of Object.values(outputs)) { + if (tensor.location === "gpu-buffer") { + tensor.dispose(); + } + } + return sequences; + } + } + /** + * Returns an object containing past key values from the given decoder results object. + * + * @param {Object} decoderResults The decoder results object. + * @param {Object} pastKeyValues The previous past key values. + * @returns {Object} An object containing past key values. + */ + getPastKeyValues(decoderResults, pastKeyValues, disposeEncoderPKVs = false) { + const pkvs = /* @__PURE__ */ Object.create(null); + for (const name in decoderResults) { + if (name.startsWith("present")) { + const newName = name.replace("present", "past_key_values"); + const is_encoder_pkv = name.includes("encoder"); + if (is_encoder_pkv && pastKeyValues) { + pkvs[newName] = pastKeyValues[newName]; + } else { + pkvs[newName] = decoderResults[name]; + } + if (pastKeyValues && (!is_encoder_pkv || disposeEncoderPKVs)) { + const t = pastKeyValues[newName]; + if (t.location === "gpu-buffer") { + t.dispose(); + } + } + } + } + return pkvs; + } + /** + * Returns an object containing attentions from the given model output object. + * + * @param {Object} model_output The output of the model. + * @returns {{cross_attentions?: Tensor[]}} An object containing attentions. + */ + getAttentions(model_output) { + const attentions = {}; + for (const attnName of ["cross_attentions", "encoder_attentions", "decoder_attentions"]) { + for (const name in model_output) { + if (name.startsWith(attnName)) { + if (!(attnName in attentions)) { + attentions[attnName] = []; + } + attentions[attnName].push(model_output[name]); + } + } + } + return attentions; + } + /** + * Adds past key values to the decoder feeds object. If pastKeyValues is null, creates new tensors for past key values. + * + * @param {Object} decoderFeeds The decoder feeds object to add past key values to. + * @param {Object} pastKeyValues An object containing past key values. + */ + addPastKeyValues(decoderFeeds, pastKeyValues) { + var _a; + if (pastKeyValues) { + Object.assign(decoderFeeds, pastKeyValues); + } else { + const session = this.sessions["decoder_model_merged"] ?? this.sessions["model"]; + const dtype = ((_a = session == null ? void 0 : session.config) == null ? void 0 : _a.kv_cache_dtype) ?? "float32"; + const empty = dtype === "float16" ? new Uint16Array() : []; + const shapes = (0, _configs_js__WEBPACK_IMPORTED_MODULE_0__.getKeyValueShapes)(this.config); + for (const name in shapes) { + decoderFeeds[name] = new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.Tensor(dtype, empty, shapes[name]); + } + } + } + async encode_image({ pixel_values }) { + const features = (await sessionRun(this.sessions["vision_encoder"], { pixel_values })).image_features; + if (!this.config.num_image_tokens) { + console.warn( + `The number of image tokens was not set in the model configuration. Setting it to the number of features detected by the vision encoder (${features.dims[1]}).` + ); + this.config.num_image_tokens = features.dims[1]; + } + return features; + } + async encode_text({ input_ids }) { + return (await sessionRun(this.sessions["embed_tokens"], { input_ids })).inputs_embeds; + } + } + class ModelOutput { + } + class BaseModelOutput extends ModelOutput { + /** + * @param {Object} output The output of the model. + * @param {Tensor} output.last_hidden_state Sequence of hidden-states at the output of the last layer of the model. + * @param {Tensor} [output.hidden_states] Hidden-states of the model at the output of each layer plus the optional initial embedding outputs. + * @param {Tensor} [output.attentions] Attentions weights after the attention softmax, used to compute the weighted average in the self-attention heads. + */ + constructor({ last_hidden_state, hidden_states = null, attentions = null }) { + super(); + this.last_hidden_state = last_hidden_state; + this.hidden_states = hidden_states; + this.attentions = attentions; + } + } + class BertPreTrainedModel extends PreTrainedModel { + } + class BertModel extends BertPreTrainedModel { + } + class BertForMaskedLM extends BertPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for masked language modeling. + */ + async _call(model_inputs) { + return new MaskedLMOutput(await super._call(model_inputs)); + } + } + class BertForSequenceClassification extends BertPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for sequence classification. + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } + } + class BertForTokenClassification extends BertPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for token classification. + */ + async _call(model_inputs) { + return new TokenClassifierOutput(await super._call(model_inputs)); + } + } + class BertForQuestionAnswering extends BertPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for question answering. + */ + async _call(model_inputs) { + return new QuestionAnsweringModelOutput(await super._call(model_inputs)); + } + } + class NomicBertPreTrainedModel extends PreTrainedModel { + } + class NomicBertModel extends NomicBertPreTrainedModel { + } + class RoFormerPreTrainedModel extends PreTrainedModel { + } + class RoFormerModel extends RoFormerPreTrainedModel { + } + class RoFormerForMaskedLM extends RoFormerPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for masked language modeling. + */ + async _call(model_inputs) { + return new MaskedLMOutput(await super._call(model_inputs)); + } + } + class RoFormerForSequenceClassification extends RoFormerPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for sequence classification. + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } + } + class RoFormerForTokenClassification extends RoFormerPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for token classification. + */ + async _call(model_inputs) { + return new TokenClassifierOutput(await super._call(model_inputs)); + } + } + class RoFormerForQuestionAnswering extends RoFormerPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for question answering. + */ + async _call(model_inputs) { + return new QuestionAnsweringModelOutput(await super._call(model_inputs)); + } + } + class ConvBertPreTrainedModel extends PreTrainedModel { + } + class ConvBertModel extends ConvBertPreTrainedModel { + } + class ConvBertForMaskedLM extends ConvBertPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for masked language modeling. + */ + async _call(model_inputs) { + return new MaskedLMOutput(await super._call(model_inputs)); + } + } + class ConvBertForSequenceClassification extends ConvBertPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for sequence classification. + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } + } + class ConvBertForTokenClassification extends ConvBertPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for token classification. + */ + async _call(model_inputs) { + return new TokenClassifierOutput(await super._call(model_inputs)); + } + } + class ConvBertForQuestionAnswering extends ConvBertPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for question answering. + */ + async _call(model_inputs) { + return new QuestionAnsweringModelOutput(await super._call(model_inputs)); + } + } + class ElectraPreTrainedModel extends PreTrainedModel { + } + class ElectraModel extends ElectraPreTrainedModel { + } + class ElectraForMaskedLM extends ElectraPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for masked language modeling. + */ + async _call(model_inputs) { + return new MaskedLMOutput(await super._call(model_inputs)); + } + } + class ElectraForSequenceClassification extends ElectraPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for sequence classification. + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } + } + class ElectraForTokenClassification extends ElectraPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for token classification. + */ + async _call(model_inputs) { + return new TokenClassifierOutput(await super._call(model_inputs)); + } + } + class ElectraForQuestionAnswering extends ElectraPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for question answering. + */ + async _call(model_inputs) { + return new QuestionAnsweringModelOutput(await super._call(model_inputs)); + } + } + class CamembertPreTrainedModel extends PreTrainedModel { + } + class CamembertModel extends CamembertPreTrainedModel { + } + class CamembertForMaskedLM extends CamembertPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for masked language modeling. + */ + async _call(model_inputs) { + return new MaskedLMOutput(await super._call(model_inputs)); + } + } + class CamembertForSequenceClassification extends CamembertPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for sequence classification. + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } + } + class CamembertForTokenClassification extends CamembertPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for token classification. + */ + async _call(model_inputs) { + return new TokenClassifierOutput(await super._call(model_inputs)); + } + } + class CamembertForQuestionAnswering extends CamembertPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for question answering. + */ + async _call(model_inputs) { + return new QuestionAnsweringModelOutput(await super._call(model_inputs)); + } + } + class DebertaPreTrainedModel extends PreTrainedModel { + } + class DebertaModel extends DebertaPreTrainedModel { + } + class DebertaForMaskedLM extends DebertaPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for masked language modeling. + */ + async _call(model_inputs) { + return new MaskedLMOutput(await super._call(model_inputs)); + } + } + class DebertaForSequenceClassification extends DebertaPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for sequence classification. + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } + } + class DebertaForTokenClassification extends DebertaPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for token classification. + */ + async _call(model_inputs) { + return new TokenClassifierOutput(await super._call(model_inputs)); + } + } + class DebertaForQuestionAnswering extends DebertaPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for question answering. + */ + async _call(model_inputs) { + return new QuestionAnsweringModelOutput(await super._call(model_inputs)); + } + } + class DebertaV2PreTrainedModel extends PreTrainedModel { + } + class DebertaV2Model extends DebertaV2PreTrainedModel { + } + class DebertaV2ForMaskedLM extends DebertaV2PreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for masked language modeling. + */ + async _call(model_inputs) { + return new MaskedLMOutput(await super._call(model_inputs)); + } + } + class DebertaV2ForSequenceClassification extends DebertaV2PreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for sequence classification. + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } + } + class DebertaV2ForTokenClassification extends DebertaV2PreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for token classification. + */ + async _call(model_inputs) { + return new TokenClassifierOutput(await super._call(model_inputs)); + } + } + class DebertaV2ForQuestionAnswering extends DebertaV2PreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for question answering. + */ + async _call(model_inputs) { + return new QuestionAnsweringModelOutput(await super._call(model_inputs)); + } + } + class DistilBertPreTrainedModel extends PreTrainedModel { + } + class DistilBertModel extends DistilBertPreTrainedModel { + } + class DistilBertForSequenceClassification extends DistilBertPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for sequence classification. + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } + } + class DistilBertForTokenClassification extends DistilBertPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for token classification. + */ + async _call(model_inputs) { + return new TokenClassifierOutput(await super._call(model_inputs)); + } + } + class DistilBertForQuestionAnswering extends DistilBertPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for question answering. + */ + async _call(model_inputs) { + return new QuestionAnsweringModelOutput(await super._call(model_inputs)); + } + } + class DistilBertForMaskedLM extends DistilBertPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} returned object + */ + async _call(model_inputs) { + return new MaskedLMOutput(await super._call(model_inputs)); + } + } + class EsmPreTrainedModel extends PreTrainedModel { + } + class EsmModel extends EsmPreTrainedModel { + } + class EsmForMaskedLM extends EsmPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for masked language modeling. + */ + async _call(model_inputs) { + return new MaskedLMOutput(await super._call(model_inputs)); + } + } + class EsmForSequenceClassification extends EsmPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for sequence classification. + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } + } + class EsmForTokenClassification extends EsmPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for token classification. + */ + async _call(model_inputs) { + return new TokenClassifierOutput(await super._call(model_inputs)); + } + } + class MobileBertPreTrainedModel extends PreTrainedModel { + } + class MobileBertModel extends MobileBertPreTrainedModel { + } + class MobileBertForMaskedLM extends MobileBertPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} returned object + */ + async _call(model_inputs) { + return new MaskedLMOutput(await super._call(model_inputs)); + } + } + class MobileBertForSequenceClassification extends MobileBertPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} returned object + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } + } + class MobileBertForQuestionAnswering extends MobileBertPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} returned object + */ + async _call(model_inputs) { + return new QuestionAnsweringModelOutput(await super._call(model_inputs)); + } + } + class MPNetPreTrainedModel extends PreTrainedModel { + } + class MPNetModel extends MPNetPreTrainedModel { + } + class MPNetForMaskedLM extends MPNetPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for masked language modeling. + */ + async _call(model_inputs) { + return new MaskedLMOutput(await super._call(model_inputs)); + } + } + class MPNetForSequenceClassification extends MPNetPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for sequence classification. + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } + } + class MPNetForTokenClassification extends MPNetPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for token classification. + */ + async _call(model_inputs) { + return new TokenClassifierOutput(await super._call(model_inputs)); + } + } + class MPNetForQuestionAnswering extends MPNetPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for question answering. + */ + async _call(model_inputs) { + return new QuestionAnsweringModelOutput(await super._call(model_inputs)); + } + } + class SqueezeBertPreTrainedModel extends PreTrainedModel { + } + class SqueezeBertModel extends SqueezeBertPreTrainedModel { + } + class SqueezeBertForMaskedLM extends SqueezeBertPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} returned object + */ + async _call(model_inputs) { + return new MaskedLMOutput(await super._call(model_inputs)); + } + } + class SqueezeBertForSequenceClassification extends SqueezeBertPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} returned object + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } + } + class SqueezeBertForQuestionAnswering extends SqueezeBertPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} returned object + */ + async _call(model_inputs) { + return new QuestionAnsweringModelOutput(await super._call(model_inputs)); + } + } + class AlbertPreTrainedModel extends PreTrainedModel { + } + class AlbertModel extends AlbertPreTrainedModel { + } + class AlbertForSequenceClassification extends AlbertPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} returned object + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } + } + class AlbertForQuestionAnswering extends AlbertPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} returned object + */ + async _call(model_inputs) { + return new QuestionAnsweringModelOutput(await super._call(model_inputs)); + } + } + class AlbertForMaskedLM extends AlbertPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} returned object + */ + async _call(model_inputs) { + return new MaskedLMOutput(await super._call(model_inputs)); + } + } + class T5PreTrainedModel extends PreTrainedModel { + constructor() { + super(...arguments); + __publicField(this, "forward_params", [ + "input_ids", + "attention_mask", + "encoder_outputs", + "decoder_input_ids", + "decoder_attention_mask", + "past_key_values" + ]); + } + } + ; + class T5Model extends T5PreTrainedModel { + } + class T5ForConditionalGeneration extends T5PreTrainedModel { + } + class LongT5PreTrainedModel extends PreTrainedModel { + } + ; + class LongT5Model extends LongT5PreTrainedModel { + } + class LongT5ForConditionalGeneration extends LongT5PreTrainedModel { + } + class MT5PreTrainedModel extends PreTrainedModel { + } + ; + class MT5Model extends MT5PreTrainedModel { + } + class MT5ForConditionalGeneration extends MT5PreTrainedModel { + } + class BartPretrainedModel extends PreTrainedModel { + } + ; + class BartModel extends BartPretrainedModel { + } + class BartForConditionalGeneration extends BartPretrainedModel { + } + class BartForSequenceClassification extends BartPretrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for sequence classification. + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } + } + class MBartPreTrainedModel extends PreTrainedModel { + } + ; + class MBartModel extends MBartPreTrainedModel { + } + class MBartForConditionalGeneration extends MBartPreTrainedModel { + } + class MBartForSequenceClassification extends MBartPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for sequence classification. + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } + } + class MBartForCausalLM extends MBartPreTrainedModel { + } + class BlenderbotPreTrainedModel extends PreTrainedModel { + } + ; + class BlenderbotModel extends BlenderbotPreTrainedModel { + } + class BlenderbotForConditionalGeneration extends BlenderbotPreTrainedModel { + } + class BlenderbotSmallPreTrainedModel extends PreTrainedModel { + } + ; + class BlenderbotSmallModel extends BlenderbotSmallPreTrainedModel { + } + class BlenderbotSmallForConditionalGeneration extends BlenderbotSmallPreTrainedModel { + } + class RobertaPreTrainedModel extends PreTrainedModel { + } + class RobertaModel extends RobertaPreTrainedModel { + } + class RobertaForMaskedLM extends RobertaPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} returned object + */ + async _call(model_inputs) { + return new MaskedLMOutput(await super._call(model_inputs)); + } + } + class RobertaForSequenceClassification extends RobertaPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} returned object + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } + } + class RobertaForTokenClassification extends RobertaPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for token classification. + */ + async _call(model_inputs) { + return new TokenClassifierOutput(await super._call(model_inputs)); + } + } + class RobertaForQuestionAnswering extends RobertaPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} returned object + */ + async _call(model_inputs) { + return new QuestionAnsweringModelOutput(await super._call(model_inputs)); + } + } + class XLMPreTrainedModel extends PreTrainedModel { + } + class XLMModel extends XLMPreTrainedModel { + } + class XLMWithLMHeadModel extends XLMPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} returned object + */ + async _call(model_inputs) { + return new MaskedLMOutput(await super._call(model_inputs)); + } + } + class XLMForSequenceClassification extends XLMPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} returned object + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } + } + class XLMForTokenClassification extends XLMPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for token classification. + */ + async _call(model_inputs) { + return new TokenClassifierOutput(await super._call(model_inputs)); + } + } + class XLMForQuestionAnswering extends XLMPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} returned object + */ + async _call(model_inputs) { + return new QuestionAnsweringModelOutput(await super._call(model_inputs)); + } + } + class XLMRobertaPreTrainedModel extends PreTrainedModel { + } + class XLMRobertaModel extends XLMRobertaPreTrainedModel { + } + class XLMRobertaForMaskedLM extends XLMRobertaPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} returned object + */ + async _call(model_inputs) { + return new MaskedLMOutput(await super._call(model_inputs)); + } + } + class XLMRobertaForSequenceClassification extends XLMRobertaPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} returned object + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } + } + class XLMRobertaForTokenClassification extends XLMRobertaPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for token classification. + */ + async _call(model_inputs) { + return new TokenClassifierOutput(await super._call(model_inputs)); + } + } + class XLMRobertaForQuestionAnswering extends XLMRobertaPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} returned object + */ + async _call(model_inputs) { + return new QuestionAnsweringModelOutput(await super._call(model_inputs)); + } + } + class ASTPreTrainedModel extends PreTrainedModel { + } + ; + class ASTModel extends ASTPreTrainedModel { + } + class ASTForAudioClassification extends ASTPreTrainedModel { + } + class WhisperPreTrainedModel extends PreTrainedModel { + constructor() { + super(...arguments); + __publicField(this, "requires_attention_mask", false); + __publicField(this, "main_input_name", "input_features"); + __publicField(this, "forward_params", [ + "input_features", + "attention_mask", + "decoder_input_ids", + "decoder_attention_mask", + "past_key_values" + ]); + } + } + ; + class WhisperModel extends WhisperPreTrainedModel { + } + class WhisperForConditionalGeneration extends WhisperPreTrainedModel { + _prepare_generation_config(generation_config, kwargs) { + return ( + /** @type {WhisperGenerationConfig} */ + super._prepare_generation_config(generation_config, kwargs, _models_whisper_generation_whisper_js__WEBPACK_IMPORTED_MODULE_14__.WhisperGenerationConfig) + ); + } + /** + * + * @param {WhisperGenerationConfig} generation_config + */ + _retrieve_init_tokens(generation_config) { + const init_tokens = [generation_config.decoder_start_token_id]; + let language = generation_config.language; + const task = generation_config.task; + if (generation_config.is_multilingual) { + if (!language) { + console.warn("No language specified - defaulting to English (en)."); + language = "en"; + } + const language_code = (0, _models_whisper_common_whisper_js__WEBPACK_IMPORTED_MODULE_15__.whisper_language_to_code)(language); + const language_token = `<|${language_code}|>`; + init_tokens.push(generation_config.lang_to_id[language_token]); + init_tokens.push(generation_config.task_to_id[task ?? "transcribe"]); + } else if (language || task) { + throw new Error( + "Cannot specify `task` or `language` for an English-only model. If the model is intended to be multilingual, pass `is_multilingual=true` to generate, or update the generation config." + ); + } + if (!generation_config.return_timestamps && generation_config.no_timestamps_token_id && init_tokens.at(-1) !== generation_config.no_timestamps_token_id) { + init_tokens.push(generation_config.no_timestamps_token_id); + } else if (generation_config.return_timestamps && init_tokens.at(-1) === generation_config.no_timestamps_token_id) { + console.warn("<|notimestamps|> prompt token is removed from generation_config since `return_timestamps` is set to `true`."); + init_tokens.pop(); + } + return init_tokens.filter((token) => token != null); + } + /** + * Transcribes or translates log-mel input features to a sequence of auto-regressively generated token ids. + * @param {import('./models/whisper/generation_whisper.js').WhisperGenerationFunctionParameters} options + * @returns {Promise} The output of the model, which can contain the generated token ids, attentions, and scores. + */ + async generate({ + inputs = null, + generation_config = null, + logits_processor = null, + stopping_criteria = null, + // Whisper-specific options (passed to kwargs) + // prompt_ids = null, + // language = null, + // task = null, + ...kwargs + }) { + generation_config = this._prepare_generation_config(generation_config, kwargs); + const init_tokens = kwargs.decoder_input_ids ?? this._retrieve_init_tokens(generation_config); + if (generation_config.return_timestamps) { + logits_processor ?? (logits_processor = new _generation_logits_process_js__WEBPACK_IMPORTED_MODULE_7__.LogitsProcessorList()); + logits_processor.push( + new _generation_logits_process_js__WEBPACK_IMPORTED_MODULE_7__.WhisperTimeStampLogitsProcessor(generation_config, init_tokens) + ); + } + if (generation_config.begin_suppress_tokens) { + logits_processor ?? (logits_processor = new _generation_logits_process_js__WEBPACK_IMPORTED_MODULE_7__.LogitsProcessorList()); + logits_processor.push( + new _generation_logits_process_js__WEBPACK_IMPORTED_MODULE_7__.SuppressTokensAtBeginLogitsProcessor(generation_config.begin_suppress_tokens, init_tokens.length) + ); + } + if (generation_config.return_token_timestamps) { + if (!generation_config.alignment_heads) { + throw new Error( + "Model generation config has no `alignment_heads`, token-level timestamps not available. See https://gist.github.com/hollance/42e32852f24243b748ae6bc1f985b13a on how to add this property to the generation config." + ); + } + if (generation_config.task === "translate") { + console.warn("Token-level timestamps may not be reliable for task 'translate'."); + } + generation_config.output_attentions = true; + generation_config.return_dict_in_generate = true; + } + const outputs = await super.generate({ + inputs, + generation_config, + logits_processor, + decoder_input_ids: init_tokens, + ...kwargs + }); + if (generation_config.return_token_timestamps) { + outputs["token_timestamps"] = this._extract_token_timestamps( + outputs, + generation_config.alignment_heads, + generation_config.num_frames + ); + } + return outputs; + } + /** + * Calculates token-level timestamps using the encoder-decoder cross-attentions and + * dynamic time-warping (DTW) to map each output token to a position in the input audio. + * If `num_frames` is specified, the encoder-decoder cross-attentions will be cropped before applying DTW. + * @param {Object} generate_outputs Outputs generated by the model + * @param {Tensor[][]} generate_outputs.cross_attentions The cross attentions output by the model + * @param {Tensor} generate_outputs.sequences The sequences output by the model + * @param {number[][]} alignment_heads Alignment heads of the model + * @param {number} [num_frames=null] Number of frames in the input audio. + * @param {number} [time_precision=0.02] Precision of the timestamps in seconds + * @returns {Tensor} tensor containing the timestamps in seconds for each predicted token + */ + _extract_token_timestamps(generate_outputs, alignment_heads, num_frames = null, time_precision = 0.02) { + if (!generate_outputs.cross_attentions) { + throw new Error( + "Model outputs must contain cross attentions to extract timestamps. This is most likely because the model was not exported with `output_attentions=True`." + ); + } + if (num_frames == null) { + console.warn( + "`num_frames` has not been set, meaning the entire audio will be analyzed. This may lead to inaccurate token-level timestamps for short audios (< 30 seconds)." + ); + } + let median_filter_width = this.config.median_filter_width; + if (median_filter_width === void 0) { + console.warn("Model config has no `median_filter_width`, using default value of 7."); + median_filter_width = 7; + } + const batch = generate_outputs.cross_attentions; + const cross_attentions = Array.from( + { length: this.config.decoder_layers }, + // Concatenate the cross attentions for each layer across sequence length dimension. + (_, i) => (0, _utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.cat)(batch.map((x) => x[i]), 2) + ); + const weights = (0, _utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.stack)(alignment_heads.map(([l, h]) => { + if (l >= cross_attentions.length) { + throw new Error(`Layer index ${l} is out of bounds for cross attentions (length ${cross_attentions.length}).`); + } + return num_frames ? cross_attentions[l].slice(null, h, null, [0, num_frames]) : cross_attentions[l].slice(null, h); + })).transpose(1, 0, 2, 3); + const [std, calculatedMean] = (0, _utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.std_mean)(weights, -2, 0, true); + const smoothedWeights = weights.clone(); + for (let a = 0; a < smoothedWeights.dims[0]; ++a) { + const aTensor = smoothedWeights[a]; + for (let b = 0; b < aTensor.dims[0]; ++b) { + const bTensor = aTensor[b]; + const stdTensorData = std[a][b][0].data; + const meanTensorData = calculatedMean[a][b][0].data; + for (let c = 0; c < bTensor.dims[0]; ++c) { + let cTensorData = bTensor[c].data; + for (let d = 0; d < cTensorData.length; ++d) { + cTensorData[d] = (cTensorData[d] - meanTensorData[d]) / stdTensorData[d]; + } + cTensorData.set((0, _utils_maths_js__WEBPACK_IMPORTED_MODULE_10__.medianFilter)(cTensorData, median_filter_width)); + } + } + } + const batchedMatrices = [(0, _utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.mean)(smoothedWeights, 1)]; + const timestampsShape = generate_outputs.sequences.dims; + const timestamps = new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.Tensor( + "float32", + new Float32Array(timestampsShape[0] * timestampsShape[1]), + timestampsShape + ); + for (let batch_idx = 0; batch_idx < timestampsShape[0]; ++batch_idx) { + const matrix = batchedMatrices[batch_idx].neg().squeeze_(0); + const [text_indices, time_indices] = (0, _utils_maths_js__WEBPACK_IMPORTED_MODULE_10__.dynamic_time_warping)(matrix.tolist()); + const diffs = Array.from({ length: text_indices.length - 1 }, (v, i) => text_indices[i + 1] - text_indices[i]); + const jumps = (0, _utils_core_js__WEBPACK_IMPORTED_MODULE_4__.mergeArrays)([1], diffs).map((x) => !!x); + const jump_times = []; + for (let i = 0; i < jumps.length; ++i) { + if (jumps[i]) { + jump_times.push(time_indices[i] * time_precision); + } + } + timestamps[batch_idx].data.set(jump_times, 1); + } + return timestamps; + } + } + class VisionEncoderDecoderModel extends PreTrainedModel { + constructor() { + super(...arguments); + __publicField(this, "main_input_name", "pixel_values"); + __publicField(this, "forward_params", [ + "pixel_values", + "input_ids", + "encoder_hidden_states", + "past_key_values" + ]); + } + } + class LlavaPreTrainedModel extends PreTrainedModel { + constructor() { + super(...arguments); + __publicField(this, "forward_params", [ + "input_ids", + "pixel_values", + "attention_mask", + "position_ids", + "past_key_values" + ]); + } + } + class LlavaForConditionalGeneration extends LlavaPreTrainedModel { + _merge_input_ids_with_image_features({ + inputs_embeds, + image_features, + input_ids, + attention_mask + }) { + const image_token_index = this.config.image_token_index; + const idsList = input_ids.tolist(); + const indexOfImage = idsList.map((x) => x.findIndex((x2) => x2 == image_token_index)); + const noImages = indexOfImage.every((x) => x === -1); + const allImages = indexOfImage.every((x) => x !== -1); + if (!noImages && !allImages) { + throw new Error("Every input should contain either 0 or 1 image token."); + } + if (noImages) { + return { + inputs_embeds, + attention_mask + }; + } + const stacked = []; + const stacked_attention_mask = []; + for (let i = 0; i < indexOfImage.length; ++i) { + const index = indexOfImage[i]; + const e = inputs_embeds[i]; + const im = image_features[i]; + const am = attention_mask[i]; + stacked.push( + (0, _utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.cat)([ + e.slice([0, index]), + im, + e.slice([index + 1, e.dims[0]]) + ], 0) + ); + stacked_attention_mask.push( + (0, _utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.cat)([ + am.slice([0, index]), + (0, _utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.ones)([im.dims[0]]), + am.slice([index + 1, am.dims[0]]) + ], 0) + ); + } + return { + inputs_embeds: (0, _utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.stack)(stacked, 0), + attention_mask: (0, _utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.stack)(stacked_attention_mask, 0) + }; + } + } + class Moondream1ForConditionalGeneration extends LlavaForConditionalGeneration { + } + class Florence2PreTrainedModel extends PreTrainedModel { + constructor() { + super(...arguments); + __publicField(this, "forward_params", [ + // Encoder inputs + "input_ids", + "inputs_embeds", + "attention_mask", + "pixel_values", + // Decoder inputs + "encoder_outputs", + "decoder_input_ids", + "decoder_inputs_embeds", + "decoder_attention_mask", + "past_key_values" + ]); + __publicField(this, "main_input_name", "inputs_embeds"); + } + } + class Florence2ForConditionalGeneration extends Florence2PreTrainedModel { + _merge_input_ids_with_image_features({ + inputs_embeds, + image_features, + input_ids, + attention_mask + }) { + return { + inputs_embeds: (0, _utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.cat)([ + image_features, + // image embeds + inputs_embeds + // task prefix embeds + ], 1), + attention_mask: (0, _utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.cat)([ + (0, _utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.ones)(image_features.dims.slice(0, 2)), + // image attention mask + attention_mask + // task prefix attention mask + ], 1) + }; + } + async _prepare_inputs_embeds({ input_ids, pixel_values, inputs_embeds, attention_mask }) { + if (!input_ids && !pixel_values) { + throw new Error("Either `input_ids` or `pixel_values` should be provided."); + } + let text_features, image_features; + if (input_ids) { + text_features = await this.encode_text({ input_ids }); + } + if (pixel_values) { + image_features = await this.encode_image({ pixel_values }); + } + if (text_features && image_features) { + ({ inputs_embeds, attention_mask } = this._merge_input_ids_with_image_features({ + inputs_embeds: text_features, + image_features, + input_ids, + attention_mask + })); + } else { + inputs_embeds = text_features || image_features; + } + return { inputs_embeds, attention_mask }; + } + async forward({ + input_ids, + pixel_values, + attention_mask, + decoder_input_ids, + decoder_attention_mask, + encoder_outputs, + past_key_values, + inputs_embeds, + decoder_inputs_embeds + }) { + if (!inputs_embeds) { + ({ inputs_embeds, attention_mask } = await this._prepare_inputs_embeds({ input_ids, pixel_values, inputs_embeds, attention_mask })); + } + if (!encoder_outputs) { + let { last_hidden_state } = await encoderForward(this, { inputs_embeds, attention_mask }); + encoder_outputs = last_hidden_state; + } + if (!decoder_inputs_embeds) { + if (!decoder_input_ids) { + throw new Error("Either `decoder_input_ids` or `decoder_inputs_embeds` should be provided."); + } + decoder_inputs_embeds = await this.encode_text({ input_ids: decoder_input_ids }); + } + const decoderFeeds = { + inputs_embeds: decoder_inputs_embeds, + attention_mask: decoder_attention_mask, + encoder_attention_mask: attention_mask, + encoder_hidden_states: encoder_outputs, + past_key_values + }; + const decoder_outputs = await decoderForward(this, decoderFeeds, true); + return decoder_outputs; + } + } + class CLIPPreTrainedModel extends PreTrainedModel { + } + class CLIPModel extends CLIPPreTrainedModel { + } + class CLIPTextModel extends CLIPPreTrainedModel { + /** @type {PreTrainedModel.from_pretrained} */ + static async from_pretrained(pretrained_model_name_or_path, options = {}) { + options.model_file_name ?? (options.model_file_name = "text_model"); + return super.from_pretrained(pretrained_model_name_or_path, options); + } + } + class CLIPTextModelWithProjection extends CLIPPreTrainedModel { + /** @type {PreTrainedModel.from_pretrained} */ + static async from_pretrained(pretrained_model_name_or_path, options = {}) { + options.model_file_name ?? (options.model_file_name = "text_model"); + return super.from_pretrained(pretrained_model_name_or_path, options); + } + } + class CLIPVisionModel extends CLIPPreTrainedModel { + /** @type {PreTrainedModel.from_pretrained} */ + static async from_pretrained(pretrained_model_name_or_path, options = {}) { + options.model_file_name ?? (options.model_file_name = "vision_model"); + return super.from_pretrained(pretrained_model_name_or_path, options); + } + } + class CLIPVisionModelWithProjection extends CLIPPreTrainedModel { + /** @type {PreTrainedModel.from_pretrained} */ + static async from_pretrained(pretrained_model_name_or_path, options = {}) { + options.model_file_name ?? (options.model_file_name = "vision_model"); + return super.from_pretrained(pretrained_model_name_or_path, options); + } + } + class SiglipPreTrainedModel extends PreTrainedModel { + } + class SiglipModel extends SiglipPreTrainedModel { + } + class SiglipTextModel extends SiglipPreTrainedModel { + /** @type {PreTrainedModel.from_pretrained} */ + static async from_pretrained(pretrained_model_name_or_path, options = {}) { + options.model_file_name ?? (options.model_file_name = "text_model"); + return super.from_pretrained(pretrained_model_name_or_path, options); + } + } + class SiglipVisionModel extends CLIPPreTrainedModel { + /** @type {PreTrainedModel.from_pretrained} */ + static async from_pretrained(pretrained_model_name_or_path, options = {}) { + options.model_file_name ?? (options.model_file_name = "vision_model"); + return super.from_pretrained(pretrained_model_name_or_path, options); + } + } + class ChineseCLIPPreTrainedModel extends PreTrainedModel { + } + class ChineseCLIPModel extends ChineseCLIPPreTrainedModel { + } + class CLIPSegPreTrainedModel extends PreTrainedModel { + } + class CLIPSegModel extends CLIPSegPreTrainedModel { + } + class CLIPSegForImageSegmentation extends CLIPSegPreTrainedModel { + } + class GPT2PreTrainedModel extends PreTrainedModel { + } + class GPT2Model extends GPT2PreTrainedModel { + } + class GPT2LMHeadModel extends GPT2PreTrainedModel { + } + class JAISPreTrainedModel extends PreTrainedModel { + } + class JAISModel extends JAISPreTrainedModel { + } + class JAISLMHeadModel extends JAISPreTrainedModel { + } + class GPTNeoPreTrainedModel extends PreTrainedModel { + } + class GPTNeoModel extends GPTNeoPreTrainedModel { + } + class GPTNeoForCausalLM extends GPTNeoPreTrainedModel { + } + class GPTNeoXPreTrainedModel extends PreTrainedModel { + } + class GPTNeoXModel extends GPTNeoXPreTrainedModel { + } + class GPTNeoXForCausalLM extends GPTNeoXPreTrainedModel { + } + class GPTJPreTrainedModel extends PreTrainedModel { + } + class GPTJModel extends GPTJPreTrainedModel { + } + class GPTJForCausalLM extends GPTJPreTrainedModel { + } + class GPTBigCodePreTrainedModel extends PreTrainedModel { + } + class GPTBigCodeModel extends GPTBigCodePreTrainedModel { + } + class GPTBigCodeForCausalLM extends GPTBigCodePreTrainedModel { + } + class CodeGenPreTrainedModel extends PreTrainedModel { + } + class CodeGenModel extends CodeGenPreTrainedModel { + } + class CodeGenForCausalLM extends CodeGenPreTrainedModel { + } + class LlamaPreTrainedModel extends PreTrainedModel { + } + class LlamaModel extends LlamaPreTrainedModel { + } + class LlamaForCausalLM extends LlamaPreTrainedModel { + } + class GranitePreTrainedModel extends PreTrainedModel { + } + class GraniteModel extends GranitePreTrainedModel { + } + class GraniteForCausalLM extends GranitePreTrainedModel { + } + class CoherePreTrainedModel extends PreTrainedModel { + } + class CohereModel extends CoherePreTrainedModel { + } + class CohereForCausalLM extends CoherePreTrainedModel { + } + class GemmaPreTrainedModel extends PreTrainedModel { + } + class GemmaModel extends GemmaPreTrainedModel { + } + class GemmaForCausalLM extends GemmaPreTrainedModel { + } + class Gemma2PreTrainedModel extends PreTrainedModel { + } + class Gemma2Model extends Gemma2PreTrainedModel { + } + class Gemma2ForCausalLM extends Gemma2PreTrainedModel { + } + class OpenELMPreTrainedModel extends PreTrainedModel { + } + class OpenELMModel extends OpenELMPreTrainedModel { + } + class OpenELMForCausalLM extends OpenELMPreTrainedModel { + } + class Qwen2PreTrainedModel extends PreTrainedModel { + } + class Qwen2Model extends Qwen2PreTrainedModel { + } + class Qwen2ForCausalLM extends Qwen2PreTrainedModel { + } + class PhiPreTrainedModel extends PreTrainedModel { + } + class PhiModel extends PhiPreTrainedModel { + } + class PhiForCausalLM extends PhiPreTrainedModel { + } + class Phi3PreTrainedModel extends PreTrainedModel { + } + class Phi3Model extends Phi3PreTrainedModel { + } + class Phi3ForCausalLM extends Phi3PreTrainedModel { + } + class BloomPreTrainedModel extends PreTrainedModel { + } + class BloomModel extends BloomPreTrainedModel { + } + class BloomForCausalLM extends BloomPreTrainedModel { + } + class MptPreTrainedModel extends PreTrainedModel { + } + class MptModel extends MptPreTrainedModel { + } + class MptForCausalLM extends MptPreTrainedModel { + } + class OPTPreTrainedModel extends PreTrainedModel { + } + class OPTModel extends OPTPreTrainedModel { + } + class OPTForCausalLM extends OPTPreTrainedModel { + } + class ViTPreTrainedModel extends PreTrainedModel { + } + class ViTModel extends ViTPreTrainedModel { + } + class ViTForImageClassification extends ViTPreTrainedModel { + /** + * @param {any} model_inputs + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } + } + class PvtPreTrainedModel extends PreTrainedModel { + } + class PvtModel extends PvtPreTrainedModel { + } + class PvtForImageClassification extends PvtPreTrainedModel { + /** + * @param {any} model_inputs + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } + } + class ViTMAEPreTrainedModel extends PreTrainedModel { + } + class ViTMAEModel extends ViTMAEPreTrainedModel { + } + class ViTMSNPreTrainedModel extends PreTrainedModel { + } + class ViTMSNModel extends ViTMSNPreTrainedModel { + } + class ViTMSNForImageClassification extends ViTMSNPreTrainedModel { + /** + * @param {any} model_inputs + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } + } + class GroupViTPreTrainedModel extends PreTrainedModel { + } + class GroupViTModel extends GroupViTPreTrainedModel { + } + class FastViTPreTrainedModel extends PreTrainedModel { + } + class FastViTModel extends FastViTPreTrainedModel { + } + class FastViTForImageClassification extends FastViTPreTrainedModel { + /** + * @param {any} model_inputs + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } + } + class VitMattePreTrainedModel extends PreTrainedModel { + } + class VitMatteForImageMatting extends VitMattePreTrainedModel { + /** + * @param {any} model_inputs + */ + async _call(model_inputs) { + return new ImageMattingOutput(await super._call(model_inputs)); + } + } + class MobileViTPreTrainedModel extends PreTrainedModel { + } + class MobileViTModel extends MobileViTPreTrainedModel { + } + class MobileViTForImageClassification extends MobileViTPreTrainedModel { + /** + * @param {any} model_inputs + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } + } + class MobileViTV2PreTrainedModel extends PreTrainedModel { + } + class MobileViTV2Model extends MobileViTV2PreTrainedModel { + } + class MobileViTV2ForImageClassification extends MobileViTV2PreTrainedModel { + /** + * @param {any} model_inputs + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } + } + class OwlViTPreTrainedModel extends PreTrainedModel { + } + class OwlViTModel extends OwlViTPreTrainedModel { + } + class OwlViTForObjectDetection extends OwlViTPreTrainedModel { + } + class Owlv2PreTrainedModel extends PreTrainedModel { + } + class Owlv2Model extends Owlv2PreTrainedModel { + } + class Owlv2ForObjectDetection extends Owlv2PreTrainedModel { + } + class BeitPreTrainedModel extends PreTrainedModel { + } + class BeitModel extends BeitPreTrainedModel { + } + class BeitForImageClassification extends BeitPreTrainedModel { + /** + * @param {any} model_inputs + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } + } + class DetrPreTrainedModel extends PreTrainedModel { + } + class DetrModel extends DetrPreTrainedModel { + } + class DetrForObjectDetection extends DetrPreTrainedModel { + /** + * @param {any} model_inputs + */ + async _call(model_inputs) { + return new DetrObjectDetectionOutput(await super._call(model_inputs)); + } + } + class DetrForSegmentation extends DetrPreTrainedModel { + /** + * Runs the model with the provided inputs + * @param {Object} model_inputs Model inputs + * @returns {Promise} Object containing segmentation outputs + */ + async _call(model_inputs) { + return new DetrSegmentationOutput(await super._call(model_inputs)); + } + } + class DetrObjectDetectionOutput extends ModelOutput { + /** + * @param {Object} output The output of the model. + * @param {Tensor} output.logits Classification logits (including no-object) for all queries. + * @param {Tensor} output.pred_boxes Normalized boxes coordinates for all queries, represented as (center_x, center_y, width, height). + * These values are normalized in [0, 1], relative to the size of each individual image in the batch (disregarding possible padding). + */ + constructor({ logits, pred_boxes }) { + super(); + this.logits = logits; + this.pred_boxes = pred_boxes; + } + } + class DetrSegmentationOutput extends ModelOutput { + /** + * @param {Object} output The output of the model. + * @param {Tensor} output.logits The output logits of the model. + * @param {Tensor} output.pred_boxes Predicted boxes. + * @param {Tensor} output.pred_masks Predicted masks. + */ + constructor({ logits, pred_boxes, pred_masks }) { + super(); + this.logits = logits; + this.pred_boxes = pred_boxes; + this.pred_masks = pred_masks; + } + } + class RTDetrPreTrainedModel extends PreTrainedModel { + } + class RTDetrModel extends RTDetrPreTrainedModel { + } + class RTDetrForObjectDetection extends RTDetrPreTrainedModel { + /** + * @param {any} model_inputs + */ + async _call(model_inputs) { + return new RTDetrObjectDetectionOutput(await super._call(model_inputs)); + } + } + class RTDetrObjectDetectionOutput extends ModelOutput { + /** + * @param {Object} output The output of the model. + * @param {Tensor} output.logits Classification logits (including no-object) for all queries. + * @param {Tensor} output.pred_boxes Normalized boxes coordinates for all queries, represented as (center_x, center_y, width, height). + * These values are normalized in [0, 1], relative to the size of each individual image in the batch (disregarding possible padding). + */ + constructor({ logits, pred_boxes }) { + super(); + this.logits = logits; + this.pred_boxes = pred_boxes; + } + } + class TableTransformerPreTrainedModel extends PreTrainedModel { + } + class TableTransformerModel extends TableTransformerPreTrainedModel { + } + class TableTransformerForObjectDetection extends TableTransformerPreTrainedModel { + /** + * @param {any} model_inputs + */ + async _call(model_inputs) { + return new TableTransformerObjectDetectionOutput(await super._call(model_inputs)); + } + } + class TableTransformerObjectDetectionOutput extends DetrObjectDetectionOutput { + } + class DeiTPreTrainedModel extends PreTrainedModel { + } + class DeiTModel extends DeiTPreTrainedModel { + } + class DeiTForImageClassification extends DeiTPreTrainedModel { + /** + * @param {any} model_inputs + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } + } + class HieraPreTrainedModel extends PreTrainedModel { + } + class HieraModel extends HieraPreTrainedModel { + } + class HieraForImageClassification extends HieraPreTrainedModel { + /** + * @param {any} model_inputs + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } + } + class ResNetPreTrainedModel extends PreTrainedModel { + } + class ResNetModel extends ResNetPreTrainedModel { + } + class ResNetForImageClassification extends ResNetPreTrainedModel { + /** + * @param {any} model_inputs + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } + } + class SwinPreTrainedModel extends PreTrainedModel { + } + class SwinModel extends SwinPreTrainedModel { + } + class SwinForImageClassification extends SwinPreTrainedModel { + /** + * @param {any} model_inputs + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } + } + class Swin2SRPreTrainedModel extends PreTrainedModel { + } + class Swin2SRModel extends Swin2SRPreTrainedModel { + } + class Swin2SRForImageSuperResolution extends Swin2SRPreTrainedModel { + } + class DPTPreTrainedModel extends PreTrainedModel { + } + class DPTModel extends DPTPreTrainedModel { + } + class DPTForDepthEstimation extends DPTPreTrainedModel { + } + class DepthAnythingPreTrainedModel extends PreTrainedModel { + } + class DepthAnythingForDepthEstimation extends DepthAnythingPreTrainedModel { + } + class SapiensPreTrainedModel extends PreTrainedModel { + } + class SapiensForSemanticSegmentation extends SapiensPreTrainedModel { + } + class SapiensForDepthEstimation extends SapiensPreTrainedModel { + } + class SapiensForNormalEstimation extends SapiensPreTrainedModel { + } + class DepthProPreTrainedModel extends PreTrainedModel { + } + class DepthProForDepthEstimation extends DepthProPreTrainedModel { + } + class MaskFormerPreTrainedModel extends PreTrainedModel { + } + class MaskFormerModel extends MaskFormerPreTrainedModel { + } + class MaskFormerForInstanceSegmentation extends MaskFormerPreTrainedModel { + } + class GLPNPreTrainedModel extends PreTrainedModel { + } + class GLPNModel extends GLPNPreTrainedModel { + } + class GLPNForDepthEstimation extends GLPNPreTrainedModel { + } + class DonutSwinPreTrainedModel extends PreTrainedModel { + } + class DonutSwinModel extends DonutSwinPreTrainedModel { + } + class ConvNextPreTrainedModel extends PreTrainedModel { + } + class ConvNextModel extends ConvNextPreTrainedModel { + } + class ConvNextForImageClassification extends ConvNextPreTrainedModel { + /** + * @param {any} model_inputs + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } + } + class ConvNextV2PreTrainedModel extends PreTrainedModel { + } + class ConvNextV2Model extends ConvNextV2PreTrainedModel { + } + class ConvNextV2ForImageClassification extends ConvNextV2PreTrainedModel { + /** + * @param {any} model_inputs + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } + } + class Dinov2PreTrainedModel extends PreTrainedModel { + } + class Dinov2Model extends Dinov2PreTrainedModel { + } + class Dinov2ForImageClassification extends Dinov2PreTrainedModel { + /** + * @param {any} model_inputs + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } + } + class YolosPreTrainedModel extends PreTrainedModel { + } + class YolosModel extends YolosPreTrainedModel { + } + class YolosForObjectDetection extends YolosPreTrainedModel { + /** + * @param {any} model_inputs + */ + async _call(model_inputs) { + return new YolosObjectDetectionOutput(await super._call(model_inputs)); + } + } + class YolosObjectDetectionOutput extends ModelOutput { + /** + * @param {Object} output The output of the model. + * @param {Tensor} output.logits Classification logits (including no-object) for all queries. + * @param {Tensor} output.pred_boxes Normalized boxes coordinates for all queries, represented as (center_x, center_y, width, height). + * These values are normalized in [0, 1], relative to the size of each individual image in the batch (disregarding possible padding). + */ + constructor({ logits, pred_boxes }) { + super(); + this.logits = logits; + this.pred_boxes = pred_boxes; + } + } + class SamPreTrainedModel extends PreTrainedModel { + } + class SamModel extends SamPreTrainedModel { + /** + * Compute image embeddings and positional image embeddings, given the pixel values of an image. + * @param {Object} model_inputs Object containing the model inputs. + * @param {Tensor} model_inputs.pixel_values Pixel values obtained using a `SamProcessor`. + * @returns {Promise<{ image_embeddings: Tensor, image_positional_embeddings: Tensor }>} The image embeddings and positional image embeddings. + */ + async get_image_embeddings({ pixel_values }) { + return await encoderForward(this, { pixel_values }); + } + /** + * @typedef {Object} SamModelInputs Object containing the model inputs. + * @property {Tensor} pixel_values Pixel values as a Tensor with shape `(batch_size, num_channels, height, width)`. + * These can be obtained using a `SamProcessor`. + * @property {Tensor} [input_points] Input 2D spatial points with shape `(batch_size, num_points, 2)`. + * This is used by the prompt encoder to encode the prompt. + * @property {Tensor} [input_labels] Input labels for the points, as a Tensor of shape `(batch_size, point_batch_size, num_points)`. + * This is used by the prompt encoder to encode the prompt. There are 4 types of labels: + * - `1`: the point is a point that contains the object of interest + * - `0`: the point is a point that does not contain the object of interest + * - `-1`: the point corresponds to the background + * - `-10`: the point is a padding point, thus should be ignored by the prompt encoder + * @property {Tensor} [input_boxes] Input bounding boxes with shape `(batch_size, num_boxes, 4)`. + * @property {Tensor} [image_embeddings] Image embeddings used by the mask decoder. + * @property {Tensor} [image_positional_embeddings] Image positional embeddings used by the mask decoder. + */ + /** + * @param {SamModelInputs} model_inputs Object containing the model inputs. + * @returns {Promise} The output of the model. + */ + async forward(model_inputs) { + if (!model_inputs.image_embeddings || !model_inputs.image_positional_embeddings) { + model_inputs = { + ...model_inputs, + ...await this.get_image_embeddings(model_inputs) + }; + } + if (!model_inputs.input_labels && model_inputs.input_points) { + const shape = model_inputs.input_points.dims.slice(0, -1); + const numElements = shape.reduce((a, b) => a * b, 1); + model_inputs.input_labels = new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.Tensor( + "int64", + new BigInt64Array(numElements).fill(1n), + shape + ); + } + const decoder_inputs = { + image_embeddings: model_inputs.image_embeddings, + image_positional_embeddings: model_inputs.image_positional_embeddings + }; + if (model_inputs.input_points) { + decoder_inputs.input_points = model_inputs.input_points; + } + if (model_inputs.input_labels) { + decoder_inputs.input_labels = model_inputs.input_labels; + } + if (model_inputs.input_boxes) { + decoder_inputs.input_boxes = model_inputs.input_boxes; + } + return await sessionRun(this.sessions["prompt_encoder_mask_decoder"], decoder_inputs); + } + /** + * Runs the model with the provided inputs + * @param {Object} model_inputs Model inputs + * @returns {Promise} Object containing segmentation outputs + */ + async _call(model_inputs) { + return new SamImageSegmentationOutput(await super._call(model_inputs)); + } + } + class SamImageSegmentationOutput extends ModelOutput { + /** + * @param {Object} output The output of the model. + * @param {Tensor} output.iou_scores The output logits of the model. + * @param {Tensor} output.pred_masks Predicted boxes. + */ + constructor({ iou_scores, pred_masks }) { + super(); + this.iou_scores = iou_scores; + this.pred_masks = pred_masks; + } + } + class MarianPreTrainedModel extends PreTrainedModel { + } + ; + class MarianModel extends MarianPreTrainedModel { + } + class MarianMTModel extends MarianPreTrainedModel { + } + class M2M100PreTrainedModel extends PreTrainedModel { + } + ; + class M2M100Model extends M2M100PreTrainedModel { + } + class M2M100ForConditionalGeneration extends M2M100PreTrainedModel { + } + class Wav2Vec2PreTrainedModel extends PreTrainedModel { + } + ; + class Wav2Vec2Model extends Wav2Vec2PreTrainedModel { + } + class Wav2Vec2ForCTC extends Wav2Vec2PreTrainedModel { + /** + * @param {Object} model_inputs + * @param {Tensor} model_inputs.input_values Float values of input raw speech waveform. + * @param {Tensor} model_inputs.attention_mask Mask to avoid performing convolution and attention on padding token indices. Mask values selected in [0, 1] + */ + async _call(model_inputs) { + return new CausalLMOutput(await super._call(model_inputs)); + } + } + class Wav2Vec2ForSequenceClassification extends Wav2Vec2PreTrainedModel { + /** + * Calls the model on new inputs. + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for sequence classification. + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } + } + class Wav2Vec2ForAudioFrameClassification extends Wav2Vec2PreTrainedModel { + /** + * Calls the model on new inputs. + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for sequence classification. + */ + async _call(model_inputs) { + return new TokenClassifierOutput(await super._call(model_inputs)); + } + } + class PyAnnotePreTrainedModel extends PreTrainedModel { + } + ; + class PyAnnoteModel extends PyAnnotePreTrainedModel { + } + class PyAnnoteForAudioFrameClassification extends PyAnnotePreTrainedModel { + /** + * Calls the model on new inputs. + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for sequence classification. + */ + async _call(model_inputs) { + return new TokenClassifierOutput(await super._call(model_inputs)); + } + } + class WeSpeakerResNetPreTrainedModel extends PreTrainedModel { + } + ; + class WeSpeakerResNetModel extends WeSpeakerResNetPreTrainedModel { + } + class UniSpeechPreTrainedModel extends PreTrainedModel { + } + ; + class UniSpeechModel extends UniSpeechPreTrainedModel { + } + class UniSpeechForCTC extends UniSpeechPreTrainedModel { + /** + * @param {Object} model_inputs + * @param {Tensor} model_inputs.input_values Float values of input raw speech waveform. + * @param {Tensor} model_inputs.attention_mask Mask to avoid performing convolution and attention on padding token indices. Mask values selected in [0, 1] + */ + async _call(model_inputs) { + return new CausalLMOutput(await super._call(model_inputs)); + } + } + class UniSpeechForSequenceClassification extends UniSpeechPreTrainedModel { + /** + * Calls the model on new inputs. + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for sequence classification. + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } + } + class UniSpeechSatPreTrainedModel extends PreTrainedModel { + } + ; + class UniSpeechSatModel extends UniSpeechSatPreTrainedModel { + } + class UniSpeechSatForCTC extends UniSpeechSatPreTrainedModel { + /** + * @param {Object} model_inputs + * @param {Tensor} model_inputs.input_values Float values of input raw speech waveform. + * @param {Tensor} model_inputs.attention_mask Mask to avoid performing convolution and attention on padding token indices. Mask values selected in [0, 1] + */ + async _call(model_inputs) { + return new CausalLMOutput(await super._call(model_inputs)); + } + } + class UniSpeechSatForSequenceClassification extends UniSpeechSatPreTrainedModel { + /** + * Calls the model on new inputs. + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for sequence classification. + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } + } + class UniSpeechSatForAudioFrameClassification extends UniSpeechSatPreTrainedModel { + /** + * Calls the model on new inputs. + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for sequence classification. + */ + async _call(model_inputs) { + return new TokenClassifierOutput(await super._call(model_inputs)); + } + } + class Wav2Vec2BertPreTrainedModel extends PreTrainedModel { + } + ; + class Wav2Vec2BertModel extends Wav2Vec2BertPreTrainedModel { + } + class Wav2Vec2BertForCTC extends Wav2Vec2BertPreTrainedModel { + /** + * @param {Object} model_inputs + * @param {Tensor} model_inputs.input_features Float values of input mel-spectrogram. + * @param {Tensor} model_inputs.attention_mask Mask to avoid performing convolution and attention on padding token indices. Mask values selected in [0, 1] + */ + async _call(model_inputs) { + return new CausalLMOutput(await super._call(model_inputs)); + } + } + class Wav2Vec2BertForSequenceClassification extends Wav2Vec2BertPreTrainedModel { + /** + * Calls the model on new inputs. + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for sequence classification. + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } + } + class HubertPreTrainedModel extends PreTrainedModel { + } + class HubertModel extends Wav2Vec2PreTrainedModel { + } + class HubertForCTC extends Wav2Vec2PreTrainedModel { + /** + * @param {Object} model_inputs + * @param {Tensor} model_inputs.input_values Float values of input raw speech waveform. + * @param {Tensor} model_inputs.attention_mask Mask to avoid performing convolution and attention on padding token indices. Mask values selected in [0, 1] + */ + async _call(model_inputs) { + return new CausalLMOutput(await super._call(model_inputs)); + } + } + class HubertForSequenceClassification extends Wav2Vec2PreTrainedModel { + /** + * Calls the model on new inputs. + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for sequence classification. + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } + } + class WavLMPreTrainedModel extends PreTrainedModel { + } + ; + class WavLMModel extends WavLMPreTrainedModel { + } + class WavLMForCTC extends WavLMPreTrainedModel { + /** + * @param {Object} model_inputs + * @param {Tensor} model_inputs.input_values Float values of input raw speech waveform. + * @param {Tensor} model_inputs.attention_mask Mask to avoid performing convolution and attention on padding token indices. Mask values selected in [0, 1] + */ + async _call(model_inputs) { + return new CausalLMOutput(await super._call(model_inputs)); + } + } + class WavLMForSequenceClassification extends WavLMPreTrainedModel { + /** + * Calls the model on new inputs. + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for sequence classification. + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } + } + class WavLMForXVector extends WavLMPreTrainedModel { + /** + * Calls the model on new inputs. + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits and speaker embeddings. + */ + async _call(model_inputs) { + return new XVectorOutput(await super._call(model_inputs)); + } + } + class WavLMForAudioFrameClassification extends WavLMPreTrainedModel { + /** + * Calls the model on new inputs. + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for sequence classification. + */ + async _call(model_inputs) { + return new TokenClassifierOutput(await super._call(model_inputs)); + } + } + class SpeechT5PreTrainedModel extends PreTrainedModel { + } + ; + class SpeechT5Model extends SpeechT5PreTrainedModel { + } + ; + class SpeechT5ForSpeechToText extends SpeechT5PreTrainedModel { + } + class SpeechT5ForTextToSpeech extends SpeechT5PreTrainedModel { + /** + * @typedef {Object} SpeechOutput + * @property {Tensor} [spectrogram] The predicted log-mel spectrogram of shape + * `(output_sequence_length, config.num_mel_bins)`. Returned when no `vocoder` is provided + * @property {Tensor} [waveform] The predicted waveform of shape `(num_frames,)`. Returned when a `vocoder` is provided. + * @property {Tensor} [cross_attentions] The outputs of the decoder's cross-attention layers of shape + * `(config.decoder_layers, config.decoder_attention_heads, output_sequence_length, input_sequence_length)`. returned when `output_cross_attentions` is `true`. + */ + /** + * Converts a sequence of input tokens into a sequence of mel spectrograms, which are subsequently turned into a speech waveform using a vocoder. + * @param {Tensor} input_values Indices of input sequence tokens in the vocabulary. + * @param {Tensor} speaker_embeddings Tensor containing the speaker embeddings. + * @param {Object} options Optional parameters for generating speech. + * @param {number} [options.threshold=0.5] The generated sequence ends when the predicted stop token probability exceeds this value. + * @param {number} [options.minlenratio=0.0] Used to calculate the minimum required length for the output sequence. + * @param {number} [options.maxlenratio=20.0] Used to calculate the maximum allowed length for the output sequence. + * @param {Object} [options.vocoder=null] The vocoder that converts the mel spectrogram into a speech waveform. If `null`, the output is the mel spectrogram. + * @param {boolean} [options.output_cross_attentions=false] Whether or not to return the attentions tensors of the decoder's cross-attention layers. + * @returns {Promise} A promise which resolves to an object containing the spectrogram, waveform, and cross-attention tensors. + */ + async generate_speech(input_values, speaker_embeddings, { + threshold = 0.5, + minlenratio = 0, + maxlenratio = 20, + vocoder = null + // output_cross_attentions = false, // TODO add + } = {}) { + const model_inputs = { + input_ids: input_values + }; + const { encoder_outputs, encoder_attention_mask } = await encoderForward(this, model_inputs); + const r = encoder_outputs.dims[1] / this.config.reduction_factor; + const maxlen = Math.floor(r * maxlenratio); + const minlen = Math.floor(r * minlenratio); + const num_mel_bins = this.config.num_mel_bins; + let spectrogramParts = []; + let past_key_values = null; + let decoder_outputs = null; + let idx = 0; + while (true) { + ++idx; + const use_cache_branch = boolTensor(!!decoder_outputs); + let output_sequence; + if (decoder_outputs) { + output_sequence = decoder_outputs.output_sequence_out; + } else { + output_sequence = new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.Tensor( + "float32", + new Float32Array(num_mel_bins), + [1, 1, num_mel_bins] + ); + } + let decoderFeeds = { + use_cache_branch, + output_sequence, + encoder_attention_mask, + speaker_embeddings, + encoder_hidden_states: encoder_outputs + }; + this.addPastKeyValues(decoderFeeds, past_key_values); + decoder_outputs = await sessionRun(this.sessions["decoder_model_merged"], decoderFeeds); + past_key_values = this.getPastKeyValues(decoder_outputs, past_key_values); + const { prob, spectrum } = decoder_outputs; + spectrogramParts.push(spectrum); + if (idx >= minlen && // Finished when stop token or maximum length is reached. + (Array.from(prob.data).filter((p) => p >= threshold).length > 0 || idx >= maxlen)) { + break; + } + } + const spectrogram = (0, _utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.cat)(spectrogramParts); + const { waveform } = await sessionRun(vocoder.sessions["model"], { spectrogram }); + return { + spectrogram, + waveform + // cross_attentions: null, // TODO add + }; + } + } + class SpeechT5HifiGan extends PreTrainedModel { + constructor() { + super(...arguments); + __publicField(this, "main_input_name", "spectrogram"); + } + } + class TrOCRPreTrainedModel extends PreTrainedModel { + } + class TrOCRForCausalLM extends TrOCRPreTrainedModel { + } + class MistralPreTrainedModel extends PreTrainedModel { + } + class MistralModel extends MistralPreTrainedModel { + } + class MistralForCausalLM extends MistralPreTrainedModel { + } + class Starcoder2PreTrainedModel extends PreTrainedModel { + } + class Starcoder2Model extends Starcoder2PreTrainedModel { + } + class Starcoder2ForCausalLM extends Starcoder2PreTrainedModel { + } + class FalconPreTrainedModel extends PreTrainedModel { + } + class FalconModel extends FalconPreTrainedModel { + } + class FalconForCausalLM extends FalconPreTrainedModel { + } + class ClapPreTrainedModel extends PreTrainedModel { + } + class ClapModel extends ClapPreTrainedModel { + } + class ClapTextModelWithProjection extends ClapPreTrainedModel { + /** @type {PreTrainedModel.from_pretrained} */ + static async from_pretrained(pretrained_model_name_or_path, options = {}) { + options.model_file_name ?? (options.model_file_name = "text_model"); + return super.from_pretrained(pretrained_model_name_or_path, options); + } + } + class ClapAudioModelWithProjection extends ClapPreTrainedModel { + /** @type {PreTrainedModel.from_pretrained} */ + static async from_pretrained(pretrained_model_name_or_path, options = {}) { + options.model_file_name ?? (options.model_file_name = "audio_model"); + return super.from_pretrained(pretrained_model_name_or_path, options); + } + } + class VitsPreTrainedModel extends PreTrainedModel { + } + class VitsModel extends VitsPreTrainedModel { + /** + * Calls the model on new inputs. + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} The outputs for the VITS model. + */ + async _call(model_inputs) { + return new VitsModelOutput(await super._call(model_inputs)); + } + } + class SegformerPreTrainedModel extends PreTrainedModel { + } + class SegformerModel extends SegformerPreTrainedModel { + } + class SegformerForImageClassification extends SegformerPreTrainedModel { + } + class SegformerForSemanticSegmentation extends SegformerPreTrainedModel { + } + class StableLmPreTrainedModel extends PreTrainedModel { + } + class StableLmModel extends StableLmPreTrainedModel { + } + class StableLmForCausalLM extends StableLmPreTrainedModel { + } + class EfficientNetPreTrainedModel extends PreTrainedModel { + } + class EfficientNetModel extends EfficientNetPreTrainedModel { + } + class EfficientNetForImageClassification extends EfficientNetPreTrainedModel { + /** + * @param {any} model_inputs + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } + } + class MusicgenPreTrainedModel extends PreTrainedModel { + } + class MusicgenModel extends MusicgenPreTrainedModel { + } + class MusicgenForCausalLM extends MusicgenPreTrainedModel { + } + class MusicgenForConditionalGeneration extends PreTrainedModel { + constructor() { + super(...arguments); + // NOTE: not MusicgenPreTrainedModel + __publicField(this, "forward_params", [ + "input_ids", + "attention_mask", + "encoder_outputs", + "decoder_input_ids", + "decoder_attention_mask", + "past_key_values" + ]); + } + /** + * Apply the pattern mask to the final ids, + * then revert the pattern delay mask by filtering the pad token id in a single step. + * @param {Tensor} outputs The output tensor from the model. + * @returns {Tensor} The filtered output tensor. + */ + _apply_and_filter_by_delay_pattern_mask(outputs) { + const [bs_x_codebooks, seqLength] = outputs.dims; + const num_codebooks = this.config.decoder.num_codebooks; + const upperBound = seqLength - num_codebooks; + let newDataSize = 0; + for (let i = 0; i < outputs.size; ++i) { + if (outputs.data[i] === this.config.decoder.pad_token_id) { + continue; + } + const row = i % seqLength; + const col = Math.floor(i / seqLength) % num_codebooks; + const diff = row - col; + if (diff > 0 && diff <= upperBound) { + outputs.data[newDataSize++] = outputs.data[i]; + } + } + const batch_size = Math.floor(bs_x_codebooks / num_codebooks); + const inferred = newDataSize / (batch_size * num_codebooks); + return new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.Tensor( + outputs.type, + outputs.data.slice(0, newDataSize), + [batch_size, num_codebooks, inferred] + ); + } + prepare_inputs_for_generation(input_ids, model_inputs, generation_config) { + let clonedInputIds = structuredClone(input_ids); + for (let i = 0; i < clonedInputIds.length; ++i) { + for (let j = 0; j < clonedInputIds[i].length; ++j) { + if (i % this.config.decoder.num_codebooks >= j) { + clonedInputIds[i][j] = BigInt(this.config.decoder.pad_token_id); + } + } + } + if (generation_config.guidance_scale !== null && generation_config.guidance_scale > 1) { + clonedInputIds = clonedInputIds.concat(clonedInputIds); + } + const prepped = super.prepare_inputs_for_generation(clonedInputIds, model_inputs, generation_config); + return prepped; + } + /** + * Generates sequences of token ids for models with a language modeling head. + * @param {import('./generation/parameters.js').GenerationFunctionParameters} options + * @returns {Promise} The output of the model, which can contain the generated token ids, attentions, and scores. + */ + async generate(options) { + const output_ids = await super.generate(options); + const audio_codes = this._apply_and_filter_by_delay_pattern_mask( + /** @type {Tensor} */ + output_ids + ).unsqueeze_(0); + const { audio_values } = await sessionRun(this.sessions["encodec_decode"], { audio_codes }); + return audio_values; + } + } + class MobileNetV1PreTrainedModel extends PreTrainedModel { + } + class MobileNetV1Model extends MobileNetV1PreTrainedModel { + } + class MobileNetV1ForImageClassification extends MobileNetV1PreTrainedModel { + /** + * @param {any} model_inputs + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } + } + class MobileNetV2PreTrainedModel extends PreTrainedModel { + } + class MobileNetV2Model extends MobileNetV2PreTrainedModel { + } + class MobileNetV2ForImageClassification extends MobileNetV2PreTrainedModel { + /** + * @param {any} model_inputs + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } + } + class MobileNetV3PreTrainedModel extends PreTrainedModel { + } + class MobileNetV3Model extends MobileNetV3PreTrainedModel { + } + class MobileNetV3ForImageClassification extends MobileNetV3PreTrainedModel { + /** + * @param {any} model_inputs + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } + } + class MobileNetV4PreTrainedModel extends PreTrainedModel { + } + class MobileNetV4Model extends MobileNetV4PreTrainedModel { + } + class MobileNetV4ForImageClassification extends MobileNetV4PreTrainedModel { + /** + * @param {any} model_inputs + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } + } + class DecisionTransformerPreTrainedModel extends PreTrainedModel { + } + class DecisionTransformerModel extends DecisionTransformerPreTrainedModel { + } + class PretrainedMixin { + /** @type {typeof PreTrainedModel.from_pretrained} */ + static async from_pretrained(pretrained_model_name_or_path, { + progress_callback = null, + config = null, + cache_dir = null, + local_files_only = false, + revision = "main", + model_file_name = null, + subfolder = "onnx", + device = null, + dtype = null, + use_external_data_format = null, + session_options = {} + } = {}) { + const options = { + progress_callback, + config, + cache_dir, + local_files_only, + revision, + model_file_name, + subfolder, + device, + dtype, + use_external_data_format, + session_options + }; + options.config = await _configs_js__WEBPACK_IMPORTED_MODULE_0__.AutoConfig.from_pretrained(pretrained_model_name_or_path, options); + if (!this.MODEL_CLASS_MAPPINGS) { + throw new Error("`MODEL_CLASS_MAPPINGS` not implemented for this type of `AutoClass`: " + this.name); + } + for (const MODEL_CLASS_MAPPING of this.MODEL_CLASS_MAPPINGS) { + const modelInfo = MODEL_CLASS_MAPPING.get(options.config.model_type); + if (!modelInfo) { + continue; + } + return await modelInfo[1].from_pretrained(pretrained_model_name_or_path, options); + } + if (this.BASE_IF_FAIL) { + console.warn(`Unknown model class "${options.config.model_type}", attempting to construct from base class.`); + return await PreTrainedModel.from_pretrained(pretrained_model_name_or_path, options); + } else { + throw Error(`Unsupported model type: ${options.config.model_type}`); + } + } + } + /** + * Mapping from model type to model class. + * @type {Map[]} + */ + __publicField(PretrainedMixin, "MODEL_CLASS_MAPPINGS", null); + /** + * Whether to attempt to instantiate the base class (`PretrainedModel`) if + * the model type is not found in the mapping. + */ + __publicField(PretrainedMixin, "BASE_IF_FAIL", false); + const MODEL_MAPPING_NAMES_ENCODER_ONLY = /* @__PURE__ */ new Map([ + ["bert", ["BertModel", BertModel]], + ["nomic_bert", ["NomicBertModel", NomicBertModel]], + ["roformer", ["RoFormerModel", RoFormerModel]], + ["electra", ["ElectraModel", ElectraModel]], + ["esm", ["EsmModel", EsmModel]], + ["convbert", ["ConvBertModel", ConvBertModel]], + ["camembert", ["CamembertModel", CamembertModel]], + ["deberta", ["DebertaModel", DebertaModel]], + ["deberta-v2", ["DebertaV2Model", DebertaV2Model]], + ["mpnet", ["MPNetModel", MPNetModel]], + ["albert", ["AlbertModel", AlbertModel]], + ["distilbert", ["DistilBertModel", DistilBertModel]], + ["roberta", ["RobertaModel", RobertaModel]], + ["xlm", ["XLMModel", XLMModel]], + ["xlm-roberta", ["XLMRobertaModel", XLMRobertaModel]], + ["clap", ["ClapModel", ClapModel]], + ["clip", ["CLIPModel", CLIPModel]], + ["clipseg", ["CLIPSegModel", CLIPSegModel]], + ["chinese_clip", ["ChineseCLIPModel", ChineseCLIPModel]], + ["siglip", ["SiglipModel", SiglipModel]], + ["mobilebert", ["MobileBertModel", MobileBertModel]], + ["squeezebert", ["SqueezeBertModel", SqueezeBertModel]], + ["wav2vec2", ["Wav2Vec2Model", Wav2Vec2Model]], + ["wav2vec2-bert", ["Wav2Vec2BertModel", Wav2Vec2BertModel]], + ["unispeech", ["UniSpeechModel", UniSpeechModel]], + ["unispeech-sat", ["UniSpeechSatModel", UniSpeechSatModel]], + ["hubert", ["HubertModel", HubertModel]], + ["wavlm", ["WavLMModel", WavLMModel]], + ["audio-spectrogram-transformer", ["ASTModel", ASTModel]], + ["vits", ["VitsModel", VitsModel]], + ["pyannote", ["PyAnnoteModel", PyAnnoteModel]], + ["wespeaker-resnet", ["WeSpeakerResNetModel", WeSpeakerResNetModel]], + ["detr", ["DetrModel", DetrModel]], + ["rt_detr", ["RTDetrModel", RTDetrModel]], + ["table-transformer", ["TableTransformerModel", TableTransformerModel]], + ["vit", ["ViTModel", ViTModel]], + ["pvt", ["PvtModel", PvtModel]], + ["vit_msn", ["ViTMSNModel", ViTMSNModel]], + ["vit_mae", ["ViTMAEModel", ViTMAEModel]], + ["groupvit", ["GroupViTModel", GroupViTModel]], + ["fastvit", ["FastViTModel", FastViTModel]], + ["mobilevit", ["MobileViTModel", MobileViTModel]], + ["mobilevitv2", ["MobileViTV2Model", MobileViTV2Model]], + ["owlvit", ["OwlViTModel", OwlViTModel]], + ["owlv2", ["Owlv2Model", Owlv2Model]], + ["beit", ["BeitModel", BeitModel]], + ["deit", ["DeiTModel", DeiTModel]], + ["hiera", ["HieraModel", HieraModel]], + ["convnext", ["ConvNextModel", ConvNextModel]], + ["convnextv2", ["ConvNextV2Model", ConvNextV2Model]], + ["dinov2", ["Dinov2Model", Dinov2Model]], + ["resnet", ["ResNetModel", ResNetModel]], + ["swin", ["SwinModel", SwinModel]], + ["swin2sr", ["Swin2SRModel", Swin2SRModel]], + ["donut-swin", ["DonutSwinModel", DonutSwinModel]], + ["yolos", ["YolosModel", YolosModel]], + ["dpt", ["DPTModel", DPTModel]], + ["glpn", ["GLPNModel", GLPNModel]], + ["hifigan", ["SpeechT5HifiGan", SpeechT5HifiGan]], + ["efficientnet", ["EfficientNetModel", EfficientNetModel]], + ["decision_transformer", ["DecisionTransformerModel", DecisionTransformerModel]], + ["mobilenet_v1", ["MobileNetV1Model", MobileNetV1Model]], + ["mobilenet_v2", ["MobileNetV2Model", MobileNetV2Model]], + ["mobilenet_v3", ["MobileNetV3Model", MobileNetV3Model]], + ["mobilenet_v4", ["MobileNetV4Model", MobileNetV4Model]], + ["maskformer", ["MaskFormerModel", MaskFormerModel]] + ]); + const MODEL_MAPPING_NAMES_ENCODER_DECODER = /* @__PURE__ */ new Map([ + ["t5", ["T5Model", T5Model]], + ["longt5", ["LongT5Model", LongT5Model]], + ["mt5", ["MT5Model", MT5Model]], + ["bart", ["BartModel", BartModel]], + ["mbart", ["MBartModel", MBartModel]], + ["marian", ["MarianModel", MarianModel]], + ["whisper", ["WhisperModel", WhisperModel]], + ["m2m_100", ["M2M100Model", M2M100Model]], + ["blenderbot", ["BlenderbotModel", BlenderbotModel]], + ["blenderbot-small", ["BlenderbotSmallModel", BlenderbotSmallModel]] + ]); + const MODEL_MAPPING_NAMES_DECODER_ONLY = /* @__PURE__ */ new Map([ + ["bloom", ["BloomModel", BloomModel]], + ["jais", ["JAISModel", JAISModel]], + ["gpt2", ["GPT2Model", GPT2Model]], + ["gptj", ["GPTJModel", GPTJModel]], + ["gpt_bigcode", ["GPTBigCodeModel", GPTBigCodeModel]], + ["gpt_neo", ["GPTNeoModel", GPTNeoModel]], + ["gpt_neox", ["GPTNeoXModel", GPTNeoXModel]], + ["codegen", ["CodeGenModel", CodeGenModel]], + ["llama", ["LlamaModel", LlamaModel]], + ["granite", ["GraniteModel", GraniteModel]], + ["cohere", ["CohereModel", CohereModel]], + ["gemma", ["GemmaModel", GemmaModel]], + ["gemma2", ["Gemma2Model", Gemma2Model]], + ["openelm", ["OpenELMModel", OpenELMModel]], + ["qwen2", ["Qwen2Model", Qwen2Model]], + ["phi", ["PhiModel", PhiModel]], + ["phi3", ["Phi3Model", Phi3Model]], + ["mpt", ["MptModel", MptModel]], + ["opt", ["OPTModel", OPTModel]], + ["mistral", ["MistralModel", MistralModel]], + ["starcoder2", ["Starcoder2Model", Starcoder2Model]], + ["falcon", ["FalconModel", FalconModel]], + ["stablelm", ["StableLmModel", StableLmModel]] + ]); + const MODEL_FOR_SPEECH_SEQ_2_SEQ_MAPPING_NAMES = /* @__PURE__ */ new Map([ + ["speecht5", ["SpeechT5ForSpeechToText", SpeechT5ForSpeechToText]], + ["whisper", ["WhisperForConditionalGeneration", WhisperForConditionalGeneration]] + ]); + const MODEL_FOR_TEXT_TO_SPECTROGRAM_MAPPING_NAMES = /* @__PURE__ */ new Map([ + ["speecht5", ["SpeechT5ForTextToSpeech", SpeechT5ForTextToSpeech]] + ]); + const MODEL_FOR_TEXT_TO_WAVEFORM_MAPPING_NAMES = /* @__PURE__ */ new Map([ + ["vits", ["VitsModel", VitsModel]], + ["musicgen", ["MusicgenForConditionalGeneration", MusicgenForConditionalGeneration]] + ]); + const MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING_NAMES = /* @__PURE__ */ new Map([ + ["bert", ["BertForSequenceClassification", BertForSequenceClassification]], + ["roformer", ["RoFormerForSequenceClassification", RoFormerForSequenceClassification]], + ["electra", ["ElectraForSequenceClassification", ElectraForSequenceClassification]], + ["esm", ["EsmForSequenceClassification", EsmForSequenceClassification]], + ["convbert", ["ConvBertForSequenceClassification", ConvBertForSequenceClassification]], + ["camembert", ["CamembertForSequenceClassification", CamembertForSequenceClassification]], + ["deberta", ["DebertaForSequenceClassification", DebertaForSequenceClassification]], + ["deberta-v2", ["DebertaV2ForSequenceClassification", DebertaV2ForSequenceClassification]], + ["mpnet", ["MPNetForSequenceClassification", MPNetForSequenceClassification]], + ["albert", ["AlbertForSequenceClassification", AlbertForSequenceClassification]], + ["distilbert", ["DistilBertForSequenceClassification", DistilBertForSequenceClassification]], + ["roberta", ["RobertaForSequenceClassification", RobertaForSequenceClassification]], + ["xlm", ["XLMForSequenceClassification", XLMForSequenceClassification]], + ["xlm-roberta", ["XLMRobertaForSequenceClassification", XLMRobertaForSequenceClassification]], + ["bart", ["BartForSequenceClassification", BartForSequenceClassification]], + ["mbart", ["MBartForSequenceClassification", MBartForSequenceClassification]], + ["mobilebert", ["MobileBertForSequenceClassification", MobileBertForSequenceClassification]], + ["squeezebert", ["SqueezeBertForSequenceClassification", SqueezeBertForSequenceClassification]] + ]); + const MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING_NAMES = /* @__PURE__ */ new Map([ + ["bert", ["BertForTokenClassification", BertForTokenClassification]], + ["roformer", ["RoFormerForTokenClassification", RoFormerForTokenClassification]], + ["electra", ["ElectraForTokenClassification", ElectraForTokenClassification]], + ["esm", ["EsmForTokenClassification", EsmForTokenClassification]], + ["convbert", ["ConvBertForTokenClassification", ConvBertForTokenClassification]], + ["camembert", ["CamembertForTokenClassification", CamembertForTokenClassification]], + ["deberta", ["DebertaForTokenClassification", DebertaForTokenClassification]], + ["deberta-v2", ["DebertaV2ForTokenClassification", DebertaV2ForTokenClassification]], + ["mpnet", ["MPNetForTokenClassification", MPNetForTokenClassification]], + ["distilbert", ["DistilBertForTokenClassification", DistilBertForTokenClassification]], + ["roberta", ["RobertaForTokenClassification", RobertaForTokenClassification]], + ["xlm", ["XLMForTokenClassification", XLMForTokenClassification]], + ["xlm-roberta", ["XLMRobertaForTokenClassification", XLMRobertaForTokenClassification]] + ]); + const MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING_NAMES = /* @__PURE__ */ new Map([ + ["t5", ["T5ForConditionalGeneration", T5ForConditionalGeneration]], + ["longt5", ["LongT5ForConditionalGeneration", LongT5ForConditionalGeneration]], + ["mt5", ["MT5ForConditionalGeneration", MT5ForConditionalGeneration]], + ["bart", ["BartForConditionalGeneration", BartForConditionalGeneration]], + ["mbart", ["MBartForConditionalGeneration", MBartForConditionalGeneration]], + ["marian", ["MarianMTModel", MarianMTModel]], + ["m2m_100", ["M2M100ForConditionalGeneration", M2M100ForConditionalGeneration]], + ["blenderbot", ["BlenderbotForConditionalGeneration", BlenderbotForConditionalGeneration]], + ["blenderbot-small", ["BlenderbotSmallForConditionalGeneration", BlenderbotSmallForConditionalGeneration]] + ]); + const MODEL_FOR_CAUSAL_LM_MAPPING_NAMES = /* @__PURE__ */ new Map([ + ["bloom", ["BloomForCausalLM", BloomForCausalLM]], + ["gpt2", ["GPT2LMHeadModel", GPT2LMHeadModel]], + ["jais", ["JAISLMHeadModel", JAISLMHeadModel]], + ["gptj", ["GPTJForCausalLM", GPTJForCausalLM]], + ["gpt_bigcode", ["GPTBigCodeForCausalLM", GPTBigCodeForCausalLM]], + ["gpt_neo", ["GPTNeoForCausalLM", GPTNeoForCausalLM]], + ["gpt_neox", ["GPTNeoXForCausalLM", GPTNeoXForCausalLM]], + ["codegen", ["CodeGenForCausalLM", CodeGenForCausalLM]], + ["llama", ["LlamaForCausalLM", LlamaForCausalLM]], + ["granite", ["GraniteForCausalLM", GraniteForCausalLM]], + ["cohere", ["CohereForCausalLM", CohereForCausalLM]], + ["gemma", ["GemmaForCausalLM", GemmaForCausalLM]], + ["gemma2", ["Gemma2ForCausalLM", Gemma2ForCausalLM]], + ["openelm", ["OpenELMForCausalLM", OpenELMForCausalLM]], + ["qwen2", ["Qwen2ForCausalLM", Qwen2ForCausalLM]], + ["phi", ["PhiForCausalLM", PhiForCausalLM]], + ["phi3", ["Phi3ForCausalLM", Phi3ForCausalLM]], + ["mpt", ["MptForCausalLM", MptForCausalLM]], + ["opt", ["OPTForCausalLM", OPTForCausalLM]], + ["mbart", ["MBartForCausalLM", MBartForCausalLM]], + ["mistral", ["MistralForCausalLM", MistralForCausalLM]], + ["starcoder2", ["Starcoder2ForCausalLM", Starcoder2ForCausalLM]], + ["falcon", ["FalconForCausalLM", FalconForCausalLM]], + ["trocr", ["TrOCRForCausalLM", TrOCRForCausalLM]], + ["stablelm", ["StableLmForCausalLM", StableLmForCausalLM]] + ]); + const MODEL_FOR_MASKED_LM_MAPPING_NAMES = /* @__PURE__ */ new Map([ + ["bert", ["BertForMaskedLM", BertForMaskedLM]], + ["roformer", ["RoFormerForMaskedLM", RoFormerForMaskedLM]], + ["electra", ["ElectraForMaskedLM", ElectraForMaskedLM]], + ["esm", ["EsmForMaskedLM", EsmForMaskedLM]], + ["convbert", ["ConvBertForMaskedLM", ConvBertForMaskedLM]], + ["camembert", ["CamembertForMaskedLM", CamembertForMaskedLM]], + ["deberta", ["DebertaForMaskedLM", DebertaForMaskedLM]], + ["deberta-v2", ["DebertaV2ForMaskedLM", DebertaV2ForMaskedLM]], + ["mpnet", ["MPNetForMaskedLM", MPNetForMaskedLM]], + ["albert", ["AlbertForMaskedLM", AlbertForMaskedLM]], + ["distilbert", ["DistilBertForMaskedLM", DistilBertForMaskedLM]], + ["roberta", ["RobertaForMaskedLM", RobertaForMaskedLM]], + ["xlm", ["XLMWithLMHeadModel", XLMWithLMHeadModel]], + ["xlm-roberta", ["XLMRobertaForMaskedLM", XLMRobertaForMaskedLM]], + ["mobilebert", ["MobileBertForMaskedLM", MobileBertForMaskedLM]], + ["squeezebert", ["SqueezeBertForMaskedLM", SqueezeBertForMaskedLM]] + ]); + const MODEL_FOR_QUESTION_ANSWERING_MAPPING_NAMES = /* @__PURE__ */ new Map([ + ["bert", ["BertForQuestionAnswering", BertForQuestionAnswering]], + ["roformer", ["RoFormerForQuestionAnswering", RoFormerForQuestionAnswering]], + ["electra", ["ElectraForQuestionAnswering", ElectraForQuestionAnswering]], + ["convbert", ["ConvBertForQuestionAnswering", ConvBertForQuestionAnswering]], + ["camembert", ["CamembertForQuestionAnswering", CamembertForQuestionAnswering]], + ["deberta", ["DebertaForQuestionAnswering", DebertaForQuestionAnswering]], + ["deberta-v2", ["DebertaV2ForQuestionAnswering", DebertaV2ForQuestionAnswering]], + ["mpnet", ["MPNetForQuestionAnswering", MPNetForQuestionAnswering]], + ["albert", ["AlbertForQuestionAnswering", AlbertForQuestionAnswering]], + ["distilbert", ["DistilBertForQuestionAnswering", DistilBertForQuestionAnswering]], + ["roberta", ["RobertaForQuestionAnswering", RobertaForQuestionAnswering]], + ["xlm", ["XLMForQuestionAnswering", XLMForQuestionAnswering]], + ["xlm-roberta", ["XLMRobertaForQuestionAnswering", XLMRobertaForQuestionAnswering]], + ["mobilebert", ["MobileBertForQuestionAnswering", MobileBertForQuestionAnswering]], + ["squeezebert", ["SqueezeBertForQuestionAnswering", SqueezeBertForQuestionAnswering]] + ]); + const MODEL_FOR_VISION_2_SEQ_MAPPING_NAMES = /* @__PURE__ */ new Map([ + ["vision-encoder-decoder", ["VisionEncoderDecoderModel", VisionEncoderDecoderModel]] + ]); + const MODEL_FOR_IMAGE_TEXT_TO_TEXT_MAPPING_NAMES = /* @__PURE__ */ new Map([ + ["llava", ["LlavaForConditionalGeneration", LlavaForConditionalGeneration]], + ["moondream1", ["Moondream1ForConditionalGeneration", Moondream1ForConditionalGeneration]], + ["florence2", ["Florence2ForConditionalGeneration", Florence2ForConditionalGeneration]] + ]); + const MODEL_FOR_DOCUMENT_QUESTION_ANSWERING_MAPPING_NAMES = /* @__PURE__ */ new Map([ + ["vision-encoder-decoder", ["VisionEncoderDecoderModel", VisionEncoderDecoderModel]] + ]); + const MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING_NAMES = /* @__PURE__ */ new Map([ + ["vit", ["ViTForImageClassification", ViTForImageClassification]], + ["pvt", ["PvtForImageClassification", PvtForImageClassification]], + ["vit_msn", ["ViTMSNForImageClassification", ViTMSNForImageClassification]], + ["fastvit", ["FastViTForImageClassification", FastViTForImageClassification]], + ["mobilevit", ["MobileViTForImageClassification", MobileViTForImageClassification]], + ["mobilevitv2", ["MobileViTV2ForImageClassification", MobileViTV2ForImageClassification]], + ["beit", ["BeitForImageClassification", BeitForImageClassification]], + ["deit", ["DeiTForImageClassification", DeiTForImageClassification]], + ["hiera", ["HieraForImageClassification", HieraForImageClassification]], + ["convnext", ["ConvNextForImageClassification", ConvNextForImageClassification]], + ["convnextv2", ["ConvNextV2ForImageClassification", ConvNextV2ForImageClassification]], + ["dinov2", ["Dinov2ForImageClassification", Dinov2ForImageClassification]], + ["resnet", ["ResNetForImageClassification", ResNetForImageClassification]], + ["swin", ["SwinForImageClassification", SwinForImageClassification]], + ["segformer", ["SegformerForImageClassification", SegformerForImageClassification]], + ["efficientnet", ["EfficientNetForImageClassification", EfficientNetForImageClassification]], + ["mobilenet_v1", ["MobileNetV1ForImageClassification", MobileNetV1ForImageClassification]], + ["mobilenet_v2", ["MobileNetV2ForImageClassification", MobileNetV2ForImageClassification]], + ["mobilenet_v3", ["MobileNetV3ForImageClassification", MobileNetV3ForImageClassification]], + ["mobilenet_v4", ["MobileNetV4ForImageClassification", MobileNetV4ForImageClassification]] + ]); + const MODEL_FOR_OBJECT_DETECTION_MAPPING_NAMES = /* @__PURE__ */ new Map([ + ["detr", ["DetrForObjectDetection", DetrForObjectDetection]], + ["rt_detr", ["RTDetrForObjectDetection", RTDetrForObjectDetection]], + ["table-transformer", ["TableTransformerForObjectDetection", TableTransformerForObjectDetection]], + ["yolos", ["YolosForObjectDetection", YolosForObjectDetection]] + ]); + const MODEL_FOR_ZERO_SHOT_OBJECT_DETECTION_MAPPING_NAMES = /* @__PURE__ */ new Map([ + ["owlvit", ["OwlViTForObjectDetection", OwlViTForObjectDetection]], + ["owlv2", ["Owlv2ForObjectDetection", Owlv2ForObjectDetection]] + ]); + const MODEL_FOR_IMAGE_SEGMENTATION_MAPPING_NAMES = /* @__PURE__ */ new Map([ + // TODO: Do not add new models here + ["detr", ["DetrForSegmentation", DetrForSegmentation]], + ["clipseg", ["CLIPSegForImageSegmentation", CLIPSegForImageSegmentation]] + ]); + const MODEL_FOR_SEMANTIC_SEGMENTATION_MAPPING_NAMES = /* @__PURE__ */ new Map([ + ["segformer", ["SegformerForSemanticSegmentation", SegformerForSemanticSegmentation]], + ["sapiens", ["SapiensForSemanticSegmentation", SapiensForSemanticSegmentation]] + ]); + const MODEL_FOR_UNIVERSAL_SEGMENTATION_MAPPING_NAMES = /* @__PURE__ */ new Map([ + ["detr", ["DetrForSegmentation", DetrForSegmentation]], + ["maskformer", ["MaskFormerForInstanceSegmentation", MaskFormerForInstanceSegmentation]] + ]); + const MODEL_FOR_MASK_GENERATION_MAPPING_NAMES = /* @__PURE__ */ new Map([ + ["sam", ["SamModel", SamModel]] + ]); + const MODEL_FOR_CTC_MAPPING_NAMES = /* @__PURE__ */ new Map([ + ["wav2vec2", ["Wav2Vec2ForCTC", Wav2Vec2ForCTC]], + ["wav2vec2-bert", ["Wav2Vec2BertForCTC", Wav2Vec2BertForCTC]], + ["unispeech", ["UniSpeechForCTC", UniSpeechForCTC]], + ["unispeech-sat", ["UniSpeechSatForCTC", UniSpeechSatForCTC]], + ["wavlm", ["WavLMForCTC", WavLMForCTC]], + ["hubert", ["HubertForCTC", HubertForCTC]] + ]); + const MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING_NAMES = /* @__PURE__ */ new Map([ + ["wav2vec2", ["Wav2Vec2ForSequenceClassification", Wav2Vec2ForSequenceClassification]], + ["wav2vec2-bert", ["Wav2Vec2BertForSequenceClassification", Wav2Vec2BertForSequenceClassification]], + ["unispeech", ["UniSpeechForSequenceClassification", UniSpeechForSequenceClassification]], + ["unispeech-sat", ["UniSpeechSatForSequenceClassification", UniSpeechSatForSequenceClassification]], + ["wavlm", ["WavLMForSequenceClassification", WavLMForSequenceClassification]], + ["hubert", ["HubertForSequenceClassification", HubertForSequenceClassification]], + ["audio-spectrogram-transformer", ["ASTForAudioClassification", ASTForAudioClassification]] + ]); + const MODEL_FOR_AUDIO_XVECTOR_MAPPING_NAMES = /* @__PURE__ */ new Map([ + ["wavlm", ["WavLMForXVector", WavLMForXVector]] + ]); + const MODEL_FOR_AUDIO_FRAME_CLASSIFICATION_MAPPING_NAMES = /* @__PURE__ */ new Map([ + ["unispeech-sat", ["UniSpeechSatForAudioFrameClassification", UniSpeechSatForAudioFrameClassification]], + ["wavlm", ["WavLMForAudioFrameClassification", WavLMForAudioFrameClassification]], + ["wav2vec2", ["Wav2Vec2ForAudioFrameClassification", Wav2Vec2ForAudioFrameClassification]], + ["pyannote", ["PyAnnoteForAudioFrameClassification", PyAnnoteForAudioFrameClassification]] + ]); + const MODEL_FOR_IMAGE_MATTING_MAPPING_NAMES = /* @__PURE__ */ new Map([ + ["vitmatte", ["VitMatteForImageMatting", VitMatteForImageMatting]] + ]); + const MODEL_FOR_IMAGE_TO_IMAGE_MAPPING_NAMES = /* @__PURE__ */ new Map([ + ["swin2sr", ["Swin2SRForImageSuperResolution", Swin2SRForImageSuperResolution]] + ]); + const MODEL_FOR_DEPTH_ESTIMATION_MAPPING_NAMES = /* @__PURE__ */ new Map([ + ["dpt", ["DPTForDepthEstimation", DPTForDepthEstimation]], + ["depth_anything", ["DepthAnythingForDepthEstimation", DepthAnythingForDepthEstimation]], + ["glpn", ["GLPNForDepthEstimation", GLPNForDepthEstimation]], + ["sapiens", ["SapiensForDepthEstimation", SapiensForDepthEstimation]], + ["depth_pro", ["DepthProForDepthEstimation", DepthProForDepthEstimation]] + ]); + const MODEL_FOR_NORMAL_ESTIMATION_MAPPING_NAMES = /* @__PURE__ */ new Map([ + ["sapiens", ["SapiensForNormalEstimation", SapiensForNormalEstimation]] + ]); + const MODEL_FOR_IMAGE_FEATURE_EXTRACTION_MAPPING_NAMES = /* @__PURE__ */ new Map([ + ["clip", ["CLIPVisionModelWithProjection", CLIPVisionModelWithProjection]], + ["siglip", ["SiglipVisionModel", SiglipVisionModel]] + ]); + const MODEL_CLASS_TYPE_MAPPING = [ + [MODEL_MAPPING_NAMES_ENCODER_ONLY, MODEL_TYPES.EncoderOnly], + [MODEL_MAPPING_NAMES_ENCODER_DECODER, MODEL_TYPES.EncoderDecoder], + [MODEL_MAPPING_NAMES_DECODER_ONLY, MODEL_TYPES.DecoderOnly], + [MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING_NAMES, MODEL_TYPES.EncoderOnly], + [MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING_NAMES, MODEL_TYPES.EncoderOnly], + [MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING_NAMES, MODEL_TYPES.Seq2Seq], + [MODEL_FOR_SPEECH_SEQ_2_SEQ_MAPPING_NAMES, MODEL_TYPES.Seq2Seq], + [MODEL_FOR_CAUSAL_LM_MAPPING_NAMES, MODEL_TYPES.DecoderOnly], + [MODEL_FOR_MASKED_LM_MAPPING_NAMES, MODEL_TYPES.EncoderOnly], + [MODEL_FOR_QUESTION_ANSWERING_MAPPING_NAMES, MODEL_TYPES.EncoderOnly], + [MODEL_FOR_VISION_2_SEQ_MAPPING_NAMES, MODEL_TYPES.Vision2Seq], + [MODEL_FOR_IMAGE_TEXT_TO_TEXT_MAPPING_NAMES, MODEL_TYPES.ImageTextToText], + [MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING_NAMES, MODEL_TYPES.EncoderOnly], + [MODEL_FOR_IMAGE_SEGMENTATION_MAPPING_NAMES, MODEL_TYPES.EncoderOnly], + [MODEL_FOR_UNIVERSAL_SEGMENTATION_MAPPING_NAMES, MODEL_TYPES.EncoderOnly], + [MODEL_FOR_SEMANTIC_SEGMENTATION_MAPPING_NAMES, MODEL_TYPES.EncoderOnly], + [MODEL_FOR_IMAGE_MATTING_MAPPING_NAMES, MODEL_TYPES.EncoderOnly], + [MODEL_FOR_IMAGE_TO_IMAGE_MAPPING_NAMES, MODEL_TYPES.EncoderOnly], + [MODEL_FOR_DEPTH_ESTIMATION_MAPPING_NAMES, MODEL_TYPES.EncoderOnly], + [MODEL_FOR_NORMAL_ESTIMATION_MAPPING_NAMES, MODEL_TYPES.EncoderOnly], + [MODEL_FOR_OBJECT_DETECTION_MAPPING_NAMES, MODEL_TYPES.EncoderOnly], + [MODEL_FOR_ZERO_SHOT_OBJECT_DETECTION_MAPPING_NAMES, MODEL_TYPES.EncoderOnly], + [MODEL_FOR_MASK_GENERATION_MAPPING_NAMES, MODEL_TYPES.MaskGeneration], + [MODEL_FOR_CTC_MAPPING_NAMES, MODEL_TYPES.EncoderOnly], + [MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING_NAMES, MODEL_TYPES.EncoderOnly], + [MODEL_FOR_TEXT_TO_SPECTROGRAM_MAPPING_NAMES, MODEL_TYPES.Seq2Seq], + [MODEL_FOR_TEXT_TO_WAVEFORM_MAPPING_NAMES, MODEL_TYPES.EncoderOnly], + [MODEL_FOR_AUDIO_XVECTOR_MAPPING_NAMES, MODEL_TYPES.EncoderOnly], + [MODEL_FOR_AUDIO_FRAME_CLASSIFICATION_MAPPING_NAMES, MODEL_TYPES.EncoderOnly], + // Custom: + [MODEL_FOR_IMAGE_FEATURE_EXTRACTION_MAPPING_NAMES, MODEL_TYPES.EncoderOnly] + ]; + for (const [mappings, type] of MODEL_CLASS_TYPE_MAPPING) { + for (const [name, model] of mappings.values()) { + MODEL_TYPE_MAPPING.set(name, type); + MODEL_CLASS_TO_NAME_MAPPING.set(model, name); + MODEL_NAME_TO_CLASS_MAPPING.set(name, model); + } + } + const CUSTOM_MAPPING = [ + // OVERRIDE: + // TODO: Refactor to allow class to specify model + ["MusicgenForConditionalGeneration", MusicgenForConditionalGeneration, MODEL_TYPES.Musicgen], + ["CLIPTextModelWithProjection", CLIPTextModelWithProjection, MODEL_TYPES.EncoderOnly], + ["SiglipTextModel", SiglipTextModel, MODEL_TYPES.EncoderOnly], + ["ClapTextModelWithProjection", ClapTextModelWithProjection, MODEL_TYPES.EncoderOnly], + ["ClapAudioModelWithProjection", ClapAudioModelWithProjection, MODEL_TYPES.EncoderOnly] + ]; + for (const [name, model, type] of CUSTOM_MAPPING) { + MODEL_TYPE_MAPPING.set(name, type); + MODEL_CLASS_TO_NAME_MAPPING.set(model, name); + MODEL_NAME_TO_CLASS_MAPPING.set(name, model); + } + class AutoModel extends PretrainedMixin { + } + /** @type {Map[]} */ + // @ts-ignore + __publicField(AutoModel, "MODEL_CLASS_MAPPINGS", MODEL_CLASS_TYPE_MAPPING.map((x) => x[0])); + __publicField(AutoModel, "BASE_IF_FAIL", true); + class AutoModelForSequenceClassification extends PretrainedMixin { + } + __publicField(AutoModelForSequenceClassification, "MODEL_CLASS_MAPPINGS", [MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING_NAMES]); + class AutoModelForTokenClassification extends PretrainedMixin { + } + __publicField(AutoModelForTokenClassification, "MODEL_CLASS_MAPPINGS", [MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING_NAMES]); + class AutoModelForSeq2SeqLM extends PretrainedMixin { + } + __publicField(AutoModelForSeq2SeqLM, "MODEL_CLASS_MAPPINGS", [MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING_NAMES]); + class AutoModelForSpeechSeq2Seq extends PretrainedMixin { + } + __publicField(AutoModelForSpeechSeq2Seq, "MODEL_CLASS_MAPPINGS", [MODEL_FOR_SPEECH_SEQ_2_SEQ_MAPPING_NAMES]); + class AutoModelForTextToSpectrogram extends PretrainedMixin { + } + __publicField(AutoModelForTextToSpectrogram, "MODEL_CLASS_MAPPINGS", [MODEL_FOR_TEXT_TO_SPECTROGRAM_MAPPING_NAMES]); + class AutoModelForTextToWaveform extends PretrainedMixin { + } + __publicField(AutoModelForTextToWaveform, "MODEL_CLASS_MAPPINGS", [MODEL_FOR_TEXT_TO_WAVEFORM_MAPPING_NAMES]); + class AutoModelForCausalLM extends PretrainedMixin { + } + __publicField(AutoModelForCausalLM, "MODEL_CLASS_MAPPINGS", [MODEL_FOR_CAUSAL_LM_MAPPING_NAMES]); + class AutoModelForMaskedLM extends PretrainedMixin { + } + __publicField(AutoModelForMaskedLM, "MODEL_CLASS_MAPPINGS", [MODEL_FOR_MASKED_LM_MAPPING_NAMES]); + class AutoModelForQuestionAnswering extends PretrainedMixin { + } + __publicField(AutoModelForQuestionAnswering, "MODEL_CLASS_MAPPINGS", [MODEL_FOR_QUESTION_ANSWERING_MAPPING_NAMES]); + class AutoModelForVision2Seq extends PretrainedMixin { + } + __publicField(AutoModelForVision2Seq, "MODEL_CLASS_MAPPINGS", [MODEL_FOR_VISION_2_SEQ_MAPPING_NAMES]); + class AutoModelForImageClassification extends PretrainedMixin { + } + __publicField(AutoModelForImageClassification, "MODEL_CLASS_MAPPINGS", [MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING_NAMES]); + class AutoModelForImageSegmentation extends PretrainedMixin { + } + __publicField(AutoModelForImageSegmentation, "MODEL_CLASS_MAPPINGS", [MODEL_FOR_IMAGE_SEGMENTATION_MAPPING_NAMES]); + class AutoModelForSemanticSegmentation extends PretrainedMixin { + } + __publicField(AutoModelForSemanticSegmentation, "MODEL_CLASS_MAPPINGS", [MODEL_FOR_SEMANTIC_SEGMENTATION_MAPPING_NAMES]); + class AutoModelForUniversalSegmentation extends PretrainedMixin { + } + __publicField(AutoModelForUniversalSegmentation, "MODEL_CLASS_MAPPINGS", [MODEL_FOR_UNIVERSAL_SEGMENTATION_MAPPING_NAMES]); + class AutoModelForObjectDetection extends PretrainedMixin { + } + __publicField(AutoModelForObjectDetection, "MODEL_CLASS_MAPPINGS", [MODEL_FOR_OBJECT_DETECTION_MAPPING_NAMES]); + class AutoModelForZeroShotObjectDetection extends PretrainedMixin { + } + __publicField(AutoModelForZeroShotObjectDetection, "MODEL_CLASS_MAPPINGS", [MODEL_FOR_ZERO_SHOT_OBJECT_DETECTION_MAPPING_NAMES]); + class AutoModelForMaskGeneration extends PretrainedMixin { + } + __publicField(AutoModelForMaskGeneration, "MODEL_CLASS_MAPPINGS", [MODEL_FOR_MASK_GENERATION_MAPPING_NAMES]); + class AutoModelForCTC extends PretrainedMixin { + } + __publicField(AutoModelForCTC, "MODEL_CLASS_MAPPINGS", [MODEL_FOR_CTC_MAPPING_NAMES]); + class AutoModelForAudioClassification extends PretrainedMixin { + } + __publicField(AutoModelForAudioClassification, "MODEL_CLASS_MAPPINGS", [MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING_NAMES]); + class AutoModelForXVector extends PretrainedMixin { + } + __publicField(AutoModelForXVector, "MODEL_CLASS_MAPPINGS", [MODEL_FOR_AUDIO_XVECTOR_MAPPING_NAMES]); + class AutoModelForAudioFrameClassification extends PretrainedMixin { + } + __publicField(AutoModelForAudioFrameClassification, "MODEL_CLASS_MAPPINGS", [MODEL_FOR_AUDIO_FRAME_CLASSIFICATION_MAPPING_NAMES]); + class AutoModelForDocumentQuestionAnswering extends PretrainedMixin { + } + __publicField(AutoModelForDocumentQuestionAnswering, "MODEL_CLASS_MAPPINGS", [MODEL_FOR_DOCUMENT_QUESTION_ANSWERING_MAPPING_NAMES]); + class AutoModelForImageMatting extends PretrainedMixin { + } + __publicField(AutoModelForImageMatting, "MODEL_CLASS_MAPPINGS", [MODEL_FOR_IMAGE_MATTING_MAPPING_NAMES]); + class AutoModelForImageToImage extends PretrainedMixin { + } + __publicField(AutoModelForImageToImage, "MODEL_CLASS_MAPPINGS", [MODEL_FOR_IMAGE_TO_IMAGE_MAPPING_NAMES]); + class AutoModelForDepthEstimation extends PretrainedMixin { + } + __publicField(AutoModelForDepthEstimation, "MODEL_CLASS_MAPPINGS", [MODEL_FOR_DEPTH_ESTIMATION_MAPPING_NAMES]); + class AutoModelForNormalEstimation extends PretrainedMixin { + } + __publicField(AutoModelForNormalEstimation, "MODEL_CLASS_MAPPINGS", [MODEL_FOR_NORMAL_ESTIMATION_MAPPING_NAMES]); + class AutoModelForImageFeatureExtraction extends PretrainedMixin { + } + __publicField(AutoModelForImageFeatureExtraction, "MODEL_CLASS_MAPPINGS", [MODEL_FOR_IMAGE_FEATURE_EXTRACTION_MAPPING_NAMES]); + class Seq2SeqLMOutput extends ModelOutput { + /** + * @param {Object} output The output of the model. + * @param {Tensor} output.logits The output logits of the model. + * @param {Tensor} output.past_key_values An tensor of key/value pairs that represent the previous state of the model. + * @param {Tensor} output.encoder_outputs The output of the encoder in a sequence-to-sequence model. + * @param {Tensor} [output.decoder_attentions] Attentions weights of the decoder, after the attention softmax, used to compute the weighted average in the self-attention heads. + * @param {Tensor} [output.cross_attentions] Attentions weights of the decoder's cross-attention layer, after the attention softmax, used to compute the weighted average in the cross-attention heads. + */ + constructor({ logits, past_key_values, encoder_outputs, decoder_attentions = null, cross_attentions = null }) { + super(); + this.logits = logits; + this.past_key_values = past_key_values; + this.encoder_outputs = encoder_outputs; + this.decoder_attentions = decoder_attentions; + this.cross_attentions = cross_attentions; + } + } + class SequenceClassifierOutput extends ModelOutput { + /** + * @param {Object} output The output of the model. + * @param {Tensor} output.logits classification (or regression if config.num_labels==1) scores (before SoftMax). + */ + constructor({ logits }) { + super(); + this.logits = logits; + } + } + class XVectorOutput extends ModelOutput { + /** + * @param {Object} output The output of the model. + * @param {Tensor} output.logits Classification hidden states before AMSoftmax, of shape `(batch_size, config.xvector_output_dim)`. + * @param {Tensor} output.embeddings Utterance embeddings used for vector similarity-based retrieval, of shape `(batch_size, config.xvector_output_dim)`. + */ + constructor({ logits, embeddings }) { + super(); + this.logits = logits; + this.embeddings = embeddings; + } + } + class TokenClassifierOutput extends ModelOutput { + /** + * @param {Object} output The output of the model. + * @param {Tensor} output.logits Classification scores (before SoftMax). + */ + constructor({ logits }) { + super(); + this.logits = logits; + } + } + class MaskedLMOutput extends ModelOutput { + /** + * @param {Object} output The output of the model. + * @param {Tensor} output.logits Prediction scores of the language modeling head (scores for each vocabulary token before SoftMax). + */ + constructor({ logits }) { + super(); + this.logits = logits; + } + } + class QuestionAnsweringModelOutput extends ModelOutput { + /** + * @param {Object} output The output of the model. + * @param {Tensor} output.start_logits Span-start scores (before SoftMax). + * @param {Tensor} output.end_logits Span-end scores (before SoftMax). + */ + constructor({ start_logits, end_logits }) { + super(); + this.start_logits = start_logits; + this.end_logits = end_logits; + } + } + class CausalLMOutput extends ModelOutput { + /** + * @param {Object} output The output of the model. + * @param {Tensor} output.logits Prediction scores of the language modeling head (scores for each vocabulary token before softmax). + */ + constructor({ logits }) { + super(); + this.logits = logits; + } + } + class CausalLMOutputWithPast extends ModelOutput { + /** + * @param {Object} output The output of the model. + * @param {Tensor} output.logits Prediction scores of the language modeling head (scores for each vocabulary token before softmax). + * @param {Tensor} output.past_key_values Contains pre-computed hidden-states (key and values in the self-attention blocks) + * that can be used (see `past_key_values` input) to speed up sequential decoding. + */ + constructor({ logits, past_key_values }) { + super(); + this.logits = logits; + this.past_key_values = past_key_values; + } + } + class ImageMattingOutput extends ModelOutput { + /** + * @param {Object} output The output of the model. + * @param {Tensor} output.alphas Estimated alpha values, of shape `(batch_size, num_channels, height, width)`. + */ + constructor({ alphas }) { + super(); + this.alphas = alphas; + } + } + class VitsModelOutput extends ModelOutput { + /** + * @param {Object} output The output of the model. + * @param {Tensor} output.waveform The final audio waveform predicted by the model, of shape `(batch_size, sequence_length)`. + * @param {Tensor} output.spectrogram The log-mel spectrogram predicted at the output of the flow model. + * This spectrogram is passed to the Hi-Fi GAN decoder model to obtain the final audio waveform. + */ + constructor({ waveform, spectrogram }) { + super(); + this.waveform = waveform; + this.spectrogram = spectrogram; + } + } + } + ), + /***/ + "./src/models/whisper/common_whisper.js": ( + /*!**********************************************!*\ + !*** ./src/models/whisper/common_whisper.js ***! + \**********************************************/ + /***/ + (__unused_webpack___webpack_module__, __webpack_exports__2, __webpack_require__2) => { + __webpack_require__2.r(__webpack_exports__2); + __webpack_require__2.d(__webpack_exports__2, { + /* harmony export */ + WHISPER_LANGUAGE_MAPPING: () => ( + /* binding */ + WHISPER_LANGUAGE_MAPPING + ), + /* harmony export */ + WHISPER_TO_LANGUAGE_CODE_MAPPING: () => ( + /* binding */ + WHISPER_TO_LANGUAGE_CODE_MAPPING + ), + /* harmony export */ + whisper_language_to_code: () => ( + /* binding */ + whisper_language_to_code + ) + /* harmony export */ + }); + const WHISPER_LANGUAGES = [ + ["en", "english"], + ["zh", "chinese"], + ["de", "german"], + ["es", "spanish"], + ["ru", "russian"], + ["ko", "korean"], + ["fr", "french"], + ["ja", "japanese"], + ["pt", "portuguese"], + ["tr", "turkish"], + ["pl", "polish"], + ["ca", "catalan"], + ["nl", "dutch"], + ["ar", "arabic"], + ["sv", "swedish"], + ["it", "italian"], + ["id", "indonesian"], + ["hi", "hindi"], + ["fi", "finnish"], + ["vi", "vietnamese"], + ["he", "hebrew"], + ["uk", "ukrainian"], + ["el", "greek"], + ["ms", "malay"], + ["cs", "czech"], + ["ro", "romanian"], + ["da", "danish"], + ["hu", "hungarian"], + ["ta", "tamil"], + ["no", "norwegian"], + ["th", "thai"], + ["ur", "urdu"], + ["hr", "croatian"], + ["bg", "bulgarian"], + ["lt", "lithuanian"], + ["la", "latin"], + ["mi", "maori"], + ["ml", "malayalam"], + ["cy", "welsh"], + ["sk", "slovak"], + ["te", "telugu"], + ["fa", "persian"], + ["lv", "latvian"], + ["bn", "bengali"], + ["sr", "serbian"], + ["az", "azerbaijani"], + ["sl", "slovenian"], + ["kn", "kannada"], + ["et", "estonian"], + ["mk", "macedonian"], + ["br", "breton"], + ["eu", "basque"], + ["is", "icelandic"], + ["hy", "armenian"], + ["ne", "nepali"], + ["mn", "mongolian"], + ["bs", "bosnian"], + ["kk", "kazakh"], + ["sq", "albanian"], + ["sw", "swahili"], + ["gl", "galician"], + ["mr", "marathi"], + ["pa", "punjabi"], + ["si", "sinhala"], + ["km", "khmer"], + ["sn", "shona"], + ["yo", "yoruba"], + ["so", "somali"], + ["af", "afrikaans"], + ["oc", "occitan"], + ["ka", "georgian"], + ["be", "belarusian"], + ["tg", "tajik"], + ["sd", "sindhi"], + ["gu", "gujarati"], + ["am", "amharic"], + ["yi", "yiddish"], + ["lo", "lao"], + ["uz", "uzbek"], + ["fo", "faroese"], + ["ht", "haitian creole"], + ["ps", "pashto"], + ["tk", "turkmen"], + ["nn", "nynorsk"], + ["mt", "maltese"], + ["sa", "sanskrit"], + ["lb", "luxembourgish"], + ["my", "myanmar"], + ["bo", "tibetan"], + ["tl", "tagalog"], + ["mg", "malagasy"], + ["as", "assamese"], + ["tt", "tatar"], + ["haw", "hawaiian"], + ["ln", "lingala"], + ["ha", "hausa"], + ["ba", "bashkir"], + ["jw", "javanese"], + ["su", "sundanese"] + ]; + const WHISPER_LANGUAGE_MAPPING = new Map(WHISPER_LANGUAGES); + const WHISPER_TO_LANGUAGE_CODE_MAPPING = new Map([ + ...WHISPER_LANGUAGES.map(([k, v]) => [v, k]), + ...[ + ["burmese", "my"], + ["valencian", "ca"], + ["flemish", "nl"], + ["haitian", "ht"], + ["letzeburgesch", "lb"], + ["pushto", "ps"], + ["panjabi", "pa"], + ["moldavian", "ro"], + ["moldovan", "ro"], + ["sinhalese", "si"], + ["castilian", "es"] + ] + ]); + function whisper_language_to_code(language) { + language = language.toLowerCase(); + let language_code = WHISPER_TO_LANGUAGE_CODE_MAPPING.get(language); + if (language_code === void 0) { + if (WHISPER_LANGUAGE_MAPPING.has(language)) { + language_code = language; + } else { + const is_language_code = language.length === 2; + const langs = is_language_code ? WHISPER_LANGUAGE_MAPPING.keys() : WHISPER_LANGUAGE_MAPPING.values(); + throw new Error(`Language "${language}" is not supported. Must be one of: ${JSON.stringify(langs)}`); + } + } + return language_code; + } + } + ), + /***/ + "./src/models/whisper/generation_whisper.js": ( + /*!**************************************************!*\ + !*** ./src/models/whisper/generation_whisper.js ***! + \**************************************************/ + /***/ + (__unused_webpack___webpack_module__, __webpack_exports__2, __webpack_require__2) => { + __webpack_require__2.r(__webpack_exports__2); + __webpack_require__2.d(__webpack_exports__2, { + /* harmony export */ + WhisperGenerationConfig: () => ( + /* binding */ + WhisperGenerationConfig + ) + /* harmony export */ + }); + var _generation_configuration_utils_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__2( + /*! ../../generation/configuration_utils.js */ + "./src/generation/configuration_utils.js" + ); + class WhisperGenerationConfig extends _generation_configuration_utils_js__WEBPACK_IMPORTED_MODULE_0__.GenerationConfig { + constructor() { + super(...arguments); + /** + * Whether to return the timestamps with the text. This enables the `WhisperTimestampsLogitsProcessor`. + * @type {boolean} + */ + __publicField(this, "return_timestamps", null); + /** + * Whether to return token-level timestamps + * with the text. This can be used with or without the `return_timestamps` option. To get word-level + * timestamps, use the tokenizer to group the tokens into words. + * @type {boolean} + */ + __publicField(this, "return_token_timestamps", null); + /** + * The number of audio frames available in this chunk. This is only used generating word-level timestamps. + * @type {number} + */ + __publicField(this, "num_frames", null); + /** + * Alignment heads to predict word-level timestamps. This is a list of [layer, head] pairs that + * select the cross-attention heads that are highly correlated to word-level timing. + * @type {[number, number][]} + */ + __publicField(this, "alignment_heads", null); + /** + * Task to use for generation, either "translate" or "transcribe". + * @type {string} + */ + __publicField(this, "task", null); + /** + * Language token to use for generation, can be either in the form of `<|en|>`, `en` or `english`. + * You can find all the possible language tokens in the `model.generation_config.lang_to_id` dictionary. + * @type {string} + */ + __publicField(this, "language", null); + /** + * The id of the `"<|notimestamps|>"` token. + * @type {number} + */ + __publicField(this, "no_timestamps_token_id", null); + /** + * Rank-1 list of token IDs created by passing text to [`~WhisperProcessor.get_prompt_ids`] that is + * provided as a prompt to each chunk. This can be used to provide or "prompt-engineer" a context for + * transcription, e.g. custom vocabularies or proper nouns to make it more likely to predict those words + * correctly. It cannot be used in conjunction with `decoder_start_token_id` as it overwrites this value. + * @type {number[]} + */ + __publicField(this, "prompt_ids", null); + /** + * Whether the model is multilingual or not. + * @type {boolean} + */ + __publicField(this, "is_multilingual", null); + /** + * (Optional) A mapping from language tokens to their corresponding IDs. + * Only required if the model is multilingual. + * @type {Record|null} + */ + __publicField(this, "lang_to_id", null); + /** + * (Optional) A mapping from task tokens to their corresponding IDs. + * @type {Record|null} + */ + __publicField(this, "task_to_id", null); + /** + * Used to set the maximum value of the initial timestamp. This is used to prevent the model from + * predicting timestamps that are too far in the future. + * @type {number} + */ + __publicField(this, "max_initial_timestamp_index", 1); + } + } + } + ), + /***/ + "./src/ops/registry.js": ( + /*!*****************************!*\ + !*** ./src/ops/registry.js ***! + \*****************************/ + /***/ + (__unused_webpack___webpack_module__, __webpack_exports__2, __webpack_require__2) => { + __webpack_require__2.r(__webpack_exports__2); + __webpack_require__2.d(__webpack_exports__2, { + /* harmony export */ + TensorOpRegistry: () => ( + /* binding */ + TensorOpRegistry + ) + /* harmony export */ + }); + var _backends_onnx_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__2( + /*! ../backends/onnx.js */ + "./src/backends/onnx.js" + ); + var _utils_tensor_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__2( + /*! ../utils/tensor.js */ + "./src/utils/tensor.js" + ); + const wrap = async (session_bytes, session_options, names) => { + const session = await (0, _backends_onnx_js__WEBPACK_IMPORTED_MODULE_0__.createInferenceSession)( + new Uint8Array(session_bytes), + session_options + ); + return ( + /** @type {any} */ + async (inputs) => { + const ortFeed = Object.fromEntries(Object.entries(inputs).map(([k, v]) => [k, v.ort_tensor])); + const outputs = await session.run(ortFeed); + if (Array.isArray(names)) { + return names.map((n) => new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_1__.Tensor(outputs[n])); + } else { + return new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_1__.Tensor(outputs[ + /** @type {string} */ + names + ]); + } + } + ); + }; + class TensorOpRegistry { + static get bilinear_interpolate_4d() { + if (!this._bilinear_interpolate_4d) { + this._bilinear_interpolate_4d = wrap( + [8, 9, 18, 0, 58, 128, 1, 10, 40, 10, 1, 120, 10, 0, 10, 0, 10, 1, 115, 18, 1, 121, 34, 6, 82, 101, 115, 105, 122, 101, 42, 17, 10, 4, 109, 111, 100, 101, 34, 6, 108, 105, 110, 101, 97, 114, 160, 1, 3, 18, 1, 114, 90, 31, 10, 1, 120, 18, 26, 10, 24, 8, 1, 18, 20, 10, 3, 18, 1, 98, 10, 3, 18, 1, 99, 10, 3, 18, 1, 104, 10, 3, 18, 1, 119, 90, 15, 10, 1, 115, 18, 10, 10, 8, 8, 7, 18, 4, 10, 2, 8, 4, 98, 31, 10, 1, 121, 18, 26, 10, 24, 8, 1, 18, 20, 10, 3, 18, 1, 98, 10, 3, 18, 1, 99, 10, 3, 18, 1, 104, 10, 3, 18, 1, 119, 66, 2, 16, 20], + this.session_options, + "y" + ); + } + return this._bilinear_interpolate_4d; + } + static get bicubic_interpolate_4d() { + if (!this._bicubic_interpolate_4d) { + this._bicubic_interpolate_4d = wrap( + [8, 9, 18, 0, 58, 127, 10, 39, 10, 1, 120, 10, 0, 10, 0, 10, 1, 115, 18, 1, 121, 34, 6, 82, 101, 115, 105, 122, 101, 42, 16, 10, 4, 109, 111, 100, 101, 34, 5, 99, 117, 98, 105, 99, 160, 1, 3, 18, 1, 114, 90, 31, 10, 1, 120, 18, 26, 10, 24, 8, 1, 18, 20, 10, 3, 18, 1, 98, 10, 3, 18, 1, 99, 10, 3, 18, 1, 104, 10, 3, 18, 1, 119, 90, 15, 10, 1, 115, 18, 10, 10, 8, 8, 7, 18, 4, 10, 2, 8, 4, 98, 31, 10, 1, 121, 18, 26, 10, 24, 8, 1, 18, 20, 10, 3, 18, 1, 98, 10, 3, 18, 1, 99, 10, 3, 18, 1, 104, 10, 3, 18, 1, 119, 66, 2, 16, 20], + this.session_options, + "y" + ); + } + return this._bicubic_interpolate_4d; + } + static get matmul() { + if (!this._matmul) { + this._matmul = wrap( + [8, 9, 18, 0, 58, 55, 10, 17, 10, 1, 97, 10, 1, 98, 18, 1, 99, 34, 6, 77, 97, 116, 77, 117, 108, 18, 1, 114, 90, 9, 10, 1, 97, 18, 4, 10, 2, 8, 1, 90, 9, 10, 1, 98, 18, 4, 10, 2, 8, 1, 98, 9, 10, 1, 99, 18, 4, 10, 2, 8, 1, 66, 2, 16, 20], + this.session_options, + "c" + ); + } + return this._matmul; + } + static get stft() { + if (!this._stft) { + this._stft = wrap( + [8, 7, 18, 0, 58, 148, 1, 10, 38, 10, 1, 115, 10, 1, 106, 10, 1, 119, 10, 1, 108, 18, 1, 111, 34, 4, 83, 84, 70, 84, 42, 15, 10, 8, 111, 110, 101, 115, 105, 100, 101, 100, 24, 1, 160, 1, 2, 18, 1, 115, 90, 26, 10, 1, 115, 18, 21, 10, 19, 8, 1, 18, 15, 10, 3, 18, 1, 98, 10, 3, 18, 1, 115, 10, 3, 18, 1, 99, 90, 11, 10, 1, 106, 18, 6, 10, 4, 8, 7, 18, 0, 90, 16, 10, 1, 119, 18, 11, 10, 9, 8, 1, 18, 5, 10, 3, 18, 1, 119, 90, 11, 10, 1, 108, 18, 6, 10, 4, 8, 7, 18, 0, 98, 31, 10, 1, 111, 18, 26, 10, 24, 8, 1, 18, 20, 10, 3, 18, 1, 98, 10, 3, 18, 1, 102, 10, 3, 18, 1, 100, 10, 3, 18, 1, 99, 66, 2, 16, 17], + this.session_options, + "o" + ); + } + return this._stft; + } + static get rfft() { + if (!this._rfft) { + this._rfft = wrap( + [8, 9, 18, 0, 58, 97, 10, 33, 10, 1, 120, 10, 0, 10, 1, 97, 18, 1, 121, 34, 3, 68, 70, 84, 42, 15, 10, 8, 111, 110, 101, 115, 105, 100, 101, 100, 24, 1, 160, 1, 2, 18, 1, 100, 90, 21, 10, 1, 120, 18, 16, 10, 14, 8, 1, 18, 10, 10, 3, 18, 1, 115, 10, 3, 18, 1, 99, 90, 11, 10, 1, 97, 18, 6, 10, 4, 8, 7, 18, 0, 98, 21, 10, 1, 121, 18, 16, 10, 14, 8, 1, 18, 10, 10, 3, 18, 1, 115, 10, 3, 18, 1, 99, 66, 2, 16, 20], + this.session_options, + "y" + ); + } + return this._rfft; + } + static get top_k() { + if (!this._top_k) { + this._top_k = wrap( + [8, 10, 18, 0, 58, 73, 10, 18, 10, 1, 120, 10, 1, 107, 18, 1, 118, 18, 1, 105, 34, 4, 84, 111, 112, 75, 18, 1, 116, 90, 9, 10, 1, 120, 18, 4, 10, 2, 8, 1, 90, 15, 10, 1, 107, 18, 10, 10, 8, 8, 7, 18, 4, 10, 2, 8, 1, 98, 9, 10, 1, 118, 18, 4, 10, 2, 8, 1, 98, 9, 10, 1, 105, 18, 4, 10, 2, 8, 7, 66, 2, 16, 21], + this.session_options, + [ + /* Values */ + "v", + /* Indices */ + "i" + ] + ); + } + return this._top_k; + } + } + __publicField(TensorOpRegistry, "session_options", { + // TODO: Allow for multiple execution providers + // executionProviders: ['webgpu'], + }); + } + ), + /***/ + "./src/pipelines.js": ( + /*!**************************!*\ + !*** ./src/pipelines.js ***! + \**************************/ + /***/ + (__unused_webpack___webpack_module__, __webpack_exports__2, __webpack_require__2) => { + __webpack_require__2.r(__webpack_exports__2); + __webpack_require__2.d(__webpack_exports__2, { + /* harmony export */ + AudioClassificationPipeline: () => ( + /* binding */ + AudioClassificationPipeline + ), + /* harmony export */ + AutomaticSpeechRecognitionPipeline: () => ( + /* binding */ + AutomaticSpeechRecognitionPipeline + ), + /* harmony export */ + DepthEstimationPipeline: () => ( + /* binding */ + DepthEstimationPipeline + ), + /* harmony export */ + DocumentQuestionAnsweringPipeline: () => ( + /* binding */ + DocumentQuestionAnsweringPipeline + ), + /* harmony export */ + FeatureExtractionPipeline: () => ( + /* binding */ + FeatureExtractionPipeline + ), + /* harmony export */ + FillMaskPipeline: () => ( + /* binding */ + FillMaskPipeline + ), + /* harmony export */ + ImageClassificationPipeline: () => ( + /* binding */ + ImageClassificationPipeline + ), + /* harmony export */ + ImageFeatureExtractionPipeline: () => ( + /* binding */ + ImageFeatureExtractionPipeline + ), + /* harmony export */ + ImageSegmentationPipeline: () => ( + /* binding */ + ImageSegmentationPipeline + ), + /* harmony export */ + ImageToImagePipeline: () => ( + /* binding */ + ImageToImagePipeline + ), + /* harmony export */ + ImageToTextPipeline: () => ( + /* binding */ + ImageToTextPipeline + ), + /* harmony export */ + ObjectDetectionPipeline: () => ( + /* binding */ + ObjectDetectionPipeline + ), + /* harmony export */ + Pipeline: () => ( + /* binding */ + Pipeline + ), + /* harmony export */ + QuestionAnsweringPipeline: () => ( + /* binding */ + QuestionAnsweringPipeline + ), + /* harmony export */ + SummarizationPipeline: () => ( + /* binding */ + SummarizationPipeline + ), + /* harmony export */ + Text2TextGenerationPipeline: () => ( + /* binding */ + Text2TextGenerationPipeline + ), + /* harmony export */ + TextClassificationPipeline: () => ( + /* binding */ + TextClassificationPipeline + ), + /* harmony export */ + TextGenerationPipeline: () => ( + /* binding */ + TextGenerationPipeline + ), + /* harmony export */ + TextToAudioPipeline: () => ( + /* binding */ + TextToAudioPipeline + ), + /* harmony export */ + TokenClassificationPipeline: () => ( + /* binding */ + TokenClassificationPipeline + ), + /* harmony export */ + TranslationPipeline: () => ( + /* binding */ + TranslationPipeline + ), + /* harmony export */ + ZeroShotAudioClassificationPipeline: () => ( + /* binding */ + ZeroShotAudioClassificationPipeline + ), + /* harmony export */ + ZeroShotClassificationPipeline: () => ( + /* binding */ + ZeroShotClassificationPipeline + ), + /* harmony export */ + ZeroShotImageClassificationPipeline: () => ( + /* binding */ + ZeroShotImageClassificationPipeline + ), + /* harmony export */ + ZeroShotObjectDetectionPipeline: () => ( + /* binding */ + ZeroShotObjectDetectionPipeline + ), + /* harmony export */ + pipeline: () => ( + /* binding */ + pipeline + ) + /* harmony export */ + }); + var _tokenizers_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__2( + /*! ./tokenizers.js */ + "./src/tokenizers.js" + ); + var _models_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__2( + /*! ./models.js */ + "./src/models.js" + ); + var _processors_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__2( + /*! ./processors.js */ + "./src/processors.js" + ); + var _utils_generic_js__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__2( + /*! ./utils/generic.js */ + "./src/utils/generic.js" + ); + var _utils_core_js__WEBPACK_IMPORTED_MODULE_4__ = __webpack_require__2( + /*! ./utils/core.js */ + "./src/utils/core.js" + ); + var _utils_maths_js__WEBPACK_IMPORTED_MODULE_5__ = __webpack_require__2( + /*! ./utils/maths.js */ + "./src/utils/maths.js" + ); + var _utils_audio_js__WEBPACK_IMPORTED_MODULE_6__2 = __webpack_require__2( + /*! ./utils/audio.js */ + "./src/utils/audio.js" + ); + var _utils_tensor_js__WEBPACK_IMPORTED_MODULE_7__ = __webpack_require__2( + /*! ./utils/tensor.js */ + "./src/utils/tensor.js" + ); + var _utils_image_js__WEBPACK_IMPORTED_MODULE_8__ = __webpack_require__2( + /*! ./utils/image.js */ + "./src/utils/image.js" + ); + async function prepareImages(images) { + if (!Array.isArray(images)) { + images = [images]; + } + return await Promise.all(images.map((x) => _utils_image_js__WEBPACK_IMPORTED_MODULE_8__.RawImage.read(x))); + } + async function prepareAudios(audios, sampling_rate) { + if (!Array.isArray(audios)) { + audios = [audios]; + } + return await Promise.all(audios.map((x) => { + if (typeof x === "string" || x instanceof URL) { + return (0, _utils_audio_js__WEBPACK_IMPORTED_MODULE_6__2.read_audio)(x, sampling_rate); + } else if (x instanceof Float64Array) { + return new Float32Array(x); + } + return x; + })); + } + function get_bounding_box(box, asInteger) { + if (asInteger) { + box = box.map((x) => x | 0); + } + const [xmin, ymin, xmax, ymax] = box; + return { xmin, ymin, xmax, ymax }; + } + class Pipeline extends _utils_generic_js__WEBPACK_IMPORTED_MODULE_3__.Callable { + /** + * Create a new Pipeline. + * @param {Object} options An object containing the following properties: + * @param {string} [options.task] The task of the pipeline. Useful for specifying subtasks. + * @param {PreTrainedModel} [options.model] The model used by the pipeline. + * @param {PreTrainedTokenizer} [options.tokenizer=null] The tokenizer used by the pipeline (if any). + * @param {Processor} [options.processor=null] The processor used by the pipeline (if any). + */ + constructor({ task, model, tokenizer = null, processor = null }) { + super(); + this.task = task; + this.model = model; + this.tokenizer = tokenizer; + this.processor = processor; + } + /** @type {DisposeType} */ + async dispose() { + await this.model.dispose(); + } + } + class TextClassificationPipeline extends /** @type {new (options: TextPipelineConstructorArgs) => TextClassificationPipelineType} */ + Pipeline { + /** + * Create a new TextClassificationPipeline. + * @param {TextPipelineConstructorArgs} options An object used to instantiate the pipeline. + */ + constructor(options) { + super(options); + } + /** @type {TextClassificationPipelineCallback} */ + async _call(texts, { + top_k = 1 + } = {}) { + const model_inputs = this.tokenizer(texts, { + padding: true, + truncation: true + }); + const outputs = await this.model(model_inputs); + const function_to_apply = this.model.config.problem_type === "multi_label_classification" ? (batch) => batch.sigmoid() : (batch) => new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_7__.Tensor( + "float32", + (0, _utils_maths_js__WEBPACK_IMPORTED_MODULE_5__.softmax)(batch.data), + batch.dims + ); + const id2label = this.model.config.id2label; + const toReturn = []; + for (const batch of outputs.logits) { + const output = function_to_apply(batch); + const scores = await (0, _utils_tensor_js__WEBPACK_IMPORTED_MODULE_7__.topk)(output, top_k); + const values = scores[0].tolist(); + const indices = scores[1].tolist(); + const vals = indices.map((x, i) => ({ + label: id2label ? id2label[x] : `LABEL_${x}`, + score: values[i] + })); + if (top_k === 1) { + toReturn.push(...vals); + } else { + toReturn.push(vals); + } + } + return Array.isArray(texts) || top_k === 1 ? ( + /** @type {TextClassificationOutput} */ + toReturn + ) : ( + /** @type {TextClassificationOutput[]} */ + toReturn[0] + ); + } + } + class TokenClassificationPipeline extends /** @type {new (options: TextPipelineConstructorArgs) => TokenClassificationPipelineType} */ + Pipeline { + /** + * Create a new TokenClassificationPipeline. + * @param {TextPipelineConstructorArgs} options An object used to instantiate the pipeline. + */ + constructor(options) { + super(options); + } + /** @type {TokenClassificationPipelineCallback} */ + async _call(texts, { + ignore_labels = ["O"] + } = {}) { + const isBatched = Array.isArray(texts); + const model_inputs = this.tokenizer(isBatched ? texts : [texts], { + padding: true, + truncation: true + }); + const outputs = await this.model(model_inputs); + const logits = outputs.logits; + const id2label = this.model.config.id2label; + const toReturn = []; + for (let i = 0; i < logits.dims[0]; ++i) { + const ids = model_inputs.input_ids[i]; + const batch = logits[i]; + const tokens = []; + for (let j = 0; j < batch.dims[0]; ++j) { + const tokenData = batch[j]; + const topScoreIndex = (0, _utils_maths_js__WEBPACK_IMPORTED_MODULE_5__.max)(tokenData.data)[1]; + const entity = id2label ? id2label[topScoreIndex] : `LABEL_${topScoreIndex}`; + if (ignore_labels.includes(entity)) { + continue; + } + const word = this.tokenizer.decode([ids[j].item()], { skip_special_tokens: true }); + if (word === "") { + continue; + } + const scores = (0, _utils_maths_js__WEBPACK_IMPORTED_MODULE_5__.softmax)(tokenData.data); + tokens.push({ + entity, + score: scores[topScoreIndex], + index: j, + word + // TODO: Add support for start and end + // start: null, + // end: null, + }); + } + toReturn.push(tokens); + } + return isBatched ? toReturn : toReturn[0]; + } + } + class QuestionAnsweringPipeline extends /** @type {new (options: TextPipelineConstructorArgs) => QuestionAnsweringPipelineType} */ + Pipeline { + /** + * Create a new QuestionAnsweringPipeline. + * @param {TextPipelineConstructorArgs} options An object used to instantiate the pipeline. + */ + constructor(options) { + super(options); + } + /** @type {QuestionAnsweringPipelineCallback} */ + async _call(question, context, { + top_k = 1 + } = {}) { + const inputs = this.tokenizer(question, { + text_pair: context, + padding: true, + truncation: true + }); + const { start_logits, end_logits } = await this.model(inputs); + const input_ids = inputs.input_ids.tolist(); + const attention_mask = inputs.attention_mask.tolist(); + const special_tokens = this.tokenizer.all_special_ids; + const toReturn = []; + for (let j = 0; j < start_logits.dims[0]; ++j) { + const ids = input_ids[j]; + const sepIndex = ids.findIndex( + (x) => ( + // We use == to match bigint with number + // @ts-ignore + x == this.tokenizer.sep_token_id + ) + ); + const valid_mask = attention_mask[j].map((y, ix) => y == 1 && (ix === 0 || ix > sepIndex && special_tokens.findIndex((x) => x == ids[ix]) === -1)); + const start = start_logits[j].tolist(); + const end = end_logits[j].tolist(); + for (let i = 1; i < start.length; ++i) { + if (attention_mask[j] == 0 || i <= sepIndex || special_tokens.findIndex((x) => x == ids[i]) !== -1) { + start[i] = -Infinity; + end[i] = -Infinity; + } + } + const start_scores = (0, _utils_maths_js__WEBPACK_IMPORTED_MODULE_5__.softmax)(start).map((x, i) => [x, i]); + const end_scores = (0, _utils_maths_js__WEBPACK_IMPORTED_MODULE_5__.softmax)(end).map((x, i) => [x, i]); + start_scores[0][0] = 0; + end_scores[0][0] = 0; + const options = (0, _utils_core_js__WEBPACK_IMPORTED_MODULE_4__.product)(start_scores, end_scores).filter((x) => x[0][1] <= x[1][1]).map((x) => [x[0][1], x[1][1], x[0][0] * x[1][0]]).sort((a, b) => b[2] - a[2]); + for (let k = 0; k < Math.min(options.length, top_k); ++k) { + const [start2, end2, score] = options[k]; + const answer_tokens = ids.slice(start2, end2 + 1); + const answer = this.tokenizer.decode(answer_tokens, { + skip_special_tokens: true + }); + toReturn.push({ + answer, + score + }); + } + } + return top_k === 1 ? toReturn[0] : toReturn; + } + } + class FillMaskPipeline extends /** @type {new (options: TextPipelineConstructorArgs) => FillMaskPipelineType} */ + Pipeline { + /** + * Create a new FillMaskPipeline. + * @param {TextPipelineConstructorArgs} options An object used to instantiate the pipeline. + */ + constructor(options) { + super(options); + } + /** @type {FillMaskPipelineCallback} */ + async _call(texts, { + top_k = 5 + } = {}) { + const model_inputs = this.tokenizer(texts, { + padding: true, + truncation: true + }); + const { logits } = await this.model(model_inputs); + const toReturn = []; + const input_ids = model_inputs.input_ids.tolist(); + for (let i = 0; i < input_ids.length; ++i) { + const ids = input_ids[i]; + const mask_token_index = ids.findIndex( + (x) => ( + // We use == to match bigint with number + // @ts-ignore + x == this.tokenizer.mask_token_id + ) + ); + if (mask_token_index === -1) { + throw Error(`Mask token (${this.tokenizer.mask_token}) not found in text.`); + } + const itemLogits = logits[i][mask_token_index]; + const scores = await (0, _utils_tensor_js__WEBPACK_IMPORTED_MODULE_7__.topk)(new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_7__.Tensor( + "float32", + (0, _utils_maths_js__WEBPACK_IMPORTED_MODULE_5__.softmax)(itemLogits.data), + itemLogits.dims + ), top_k); + const values = scores[0].tolist(); + const indices = scores[1].tolist(); + toReturn.push(indices.map((x, i2) => { + const sequence = ids.slice(); + sequence[mask_token_index] = x; + return { + score: values[i2], + token: Number(x), + token_str: this.tokenizer.model.vocab[x], + sequence: this.tokenizer.decode(sequence, { skip_special_tokens: true }) + }; + })); + } + return Array.isArray(texts) ? toReturn : toReturn[0]; + } + } + class Text2TextGenerationPipeline extends /** @type {new (options: TextPipelineConstructorArgs) => Text2TextGenerationPipelineType} */ + Pipeline { + /** + * Create a new Text2TextGenerationPipeline. + * @param {TextPipelineConstructorArgs} options An object used to instantiate the pipeline. + */ + constructor(options) { + super(options); + /** @type {'generated_text'} */ + __publicField(this, "_key", "generated_text"); + } + /** @type {Text2TextGenerationPipelineCallback} */ + async _call(texts, generate_kwargs = {}) { + if (!Array.isArray(texts)) { + texts = [texts]; + } + if (this.model.config.prefix) { + texts = texts.map((x) => this.model.config.prefix + x); + } + const task_specific_params = this.model.config.task_specific_params; + if (task_specific_params && task_specific_params[this.task]) { + if (task_specific_params[this.task].prefix) { + texts = texts.map((x) => task_specific_params[this.task].prefix + x); + } + } + const tokenizer = this.tokenizer; + const tokenizer_options = { + padding: true, + truncation: true + }; + let inputs; + if (this instanceof TranslationPipeline && "_build_translation_inputs" in tokenizer) { + inputs = tokenizer._build_translation_inputs(texts, tokenizer_options, generate_kwargs); + } else { + inputs = tokenizer(texts, tokenizer_options); + } + const outputTokenIds = await this.model.generate({ ...inputs, ...generate_kwargs }); + return tokenizer.batch_decode( + /** @type {Tensor} */ + outputTokenIds, + { + skip_special_tokens: true + } + ).map((text) => ({ [this._key]: text })); + } + } + class SummarizationPipeline extends /** @type {new (options: TextPipelineConstructorArgs) => SummarizationPipelineType} */ + /** @type {any} */ + Text2TextGenerationPipeline { + /** + * Create a new SummarizationPipeline. + * @param {TextPipelineConstructorArgs} options An object used to instantiate the pipeline. + */ + constructor(options) { + super(options); + /** @type {'summary_text'} */ + __publicField(this, "_key", "summary_text"); + } + } + class TranslationPipeline extends /** @type {new (options: TextPipelineConstructorArgs) => TranslationPipelineType} */ + /** @type {any} */ + Text2TextGenerationPipeline { + /** + * Create a new TranslationPipeline. + * @param {TextPipelineConstructorArgs} options An object used to instantiate the pipeline. + */ + constructor(options) { + super(options); + /** @type {'translation_text'} */ + __publicField(this, "_key", "translation_text"); + } + } + function isChat(x) { + return Array.isArray(x) && x.every((x2) => "role" in x2 && "content" in x2); + } + class TextGenerationPipeline extends /** @type {new (options: TextPipelineConstructorArgs) => TextGenerationPipelineType} */ + Pipeline { + /** + * Create a new TextGenerationPipeline. + * @param {TextPipelineConstructorArgs} options An object used to instantiate the pipeline. + */ + constructor(options) { + super(options); + } + /** @type {TextGenerationPipelineCallback} */ + async _call(texts, generate_kwargs = {}) { + let isBatched = false; + let isChatInput = false; + let inputs; + if (typeof texts === "string") { + inputs = texts = [texts]; + } else if (Array.isArray(texts) && texts.every((x) => typeof x === "string")) { + isBatched = true; + inputs = /** @type {string[]} */ + texts; + } else { + if (isChat(texts)) { + texts = [ + /** @type {Chat} */ + texts + ]; + } else if (Array.isArray(texts) && texts.every(isChat)) { + isBatched = true; + } else { + throw new Error("Input must be a string, an array of strings, a Chat, or an array of Chats"); + } + isChatInput = true; + inputs = /** @type {string[]} */ + /** @type {Chat[]} */ + texts.map( + (x) => this.tokenizer.apply_chat_template(x, { + tokenize: false, + add_generation_prompt: true + }) + ); + } + const add_special_tokens = generate_kwargs.add_special_tokens ?? false; + const return_full_text = isChatInput ? false : generate_kwargs.return_full_text ?? true; + this.tokenizer.padding_side = "left"; + const text_inputs = this.tokenizer(inputs, { + add_special_tokens, + padding: true, + truncation: true + }); + const outputTokenIds = ( + /** @type {Tensor} */ + await this.model.generate({ + ...text_inputs, + ...generate_kwargs + }) + ); + const decoded = this.tokenizer.batch_decode(outputTokenIds, { + skip_special_tokens: true + }); + let promptLengths; + if (!return_full_text && text_inputs.input_ids.dims.at(-1) > 0) { + promptLengths = this.tokenizer.batch_decode(text_inputs.input_ids, { + skip_special_tokens: true + }).map((x) => x.length); + } + const toReturn = Array.from({ length: texts.length }, (_) => []); + for (let i = 0; i < decoded.length; ++i) { + const textIndex = Math.floor(i / outputTokenIds.dims[0] * texts.length); + if (promptLengths) { + decoded[i] = decoded[i].slice(promptLengths[textIndex]); + } + toReturn[textIndex].push({ + generated_text: isChatInput ? [ + .../** @type {Chat[]} */ + texts[textIndex], + { role: "assistant", content: decoded[i] } + ] : decoded[i] + }); + } + return !isBatched && toReturn.length === 1 ? toReturn[0] : toReturn; + } + } + class ZeroShotClassificationPipeline extends /** @type {new (options: TextPipelineConstructorArgs) => ZeroShotClassificationPipelineType} */ + Pipeline { + /** + * Create a new ZeroShotClassificationPipeline. + * @param {TextPipelineConstructorArgs} options An object used to instantiate the pipeline. + */ + constructor(options) { + super(options); + this.label2id = Object.fromEntries( + Object.entries( + /** @type {any} */ + this.model.config.label2id + ).map( + ([k, v]) => [k.toLowerCase(), v] + ) + ); + this.entailment_id = this.label2id["entailment"]; + if (this.entailment_id === void 0) { + console.warn("Could not find 'entailment' in label2id mapping. Using 2 as entailment_id."); + this.entailment_id = 2; + } + this.contradiction_id = this.label2id["contradiction"] ?? this.label2id["not_entailment"]; + if (this.contradiction_id === void 0) { + console.warn("Could not find 'contradiction' in label2id mapping. Using 0 as contradiction_id."); + this.contradiction_id = 0; + } + } + /** @type {ZeroShotClassificationPipelineCallback} */ + async _call(texts, candidate_labels, { + hypothesis_template = "This example is {}.", + multi_label = false + } = {}) { + const isBatched = Array.isArray(texts); + if (!isBatched) { + texts = [ + /** @type {string} */ + texts + ]; + } + if (!Array.isArray(candidate_labels)) { + candidate_labels = [candidate_labels]; + } + const hypotheses = candidate_labels.map( + (x) => hypothesis_template.replace("{}", x) + ); + const softmaxEach = multi_label || candidate_labels.length === 1; + const toReturn = []; + for (const premise of texts) { + const entails_logits = []; + for (const hypothesis of hypotheses) { + const inputs = this.tokenizer(premise, { + text_pair: hypothesis, + padding: true, + truncation: true + }); + const outputs = await this.model(inputs); + if (softmaxEach) { + entails_logits.push([ + outputs.logits.data[this.contradiction_id], + outputs.logits.data[this.entailment_id] + ]); + } else { + entails_logits.push(outputs.logits.data[this.entailment_id]); + } + } + const scores = softmaxEach ? entails_logits.map((x) => (0, _utils_maths_js__WEBPACK_IMPORTED_MODULE_5__.softmax)(x)[1]) : (0, _utils_maths_js__WEBPACK_IMPORTED_MODULE_5__.softmax)(entails_logits); + const scores_sorted = scores.map((x, i) => [x, i]).sort((a, b) => b[0] - a[0]); + toReturn.push({ + sequence: premise, + labels: scores_sorted.map((x) => candidate_labels[x[1]]), + scores: scores_sorted.map((x) => x[0]) + }); + } + return isBatched ? toReturn : toReturn[0]; + } + } + class FeatureExtractionPipeline extends /** @type {new (options: TextPipelineConstructorArgs) => FeatureExtractionPipelineType} */ + Pipeline { + /** + * Create a new FeatureExtractionPipeline. + * @param {TextPipelineConstructorArgs} options An object used to instantiate the pipeline. + */ + constructor(options) { + super(options); + } + /** @type {FeatureExtractionPipelineCallback} */ + async _call(texts, { + pooling = ( + /** @type {'none'} */ + "none" + ), + normalize = false, + quantize = false, + precision = ( + /** @type {'binary'} */ + "binary" + ) + } = {}) { + const model_inputs = this.tokenizer(texts, { + padding: true, + truncation: true + }); + const outputs = await this.model(model_inputs); + let result = outputs.last_hidden_state ?? outputs.logits ?? outputs.token_embeddings; + if (pooling === "none") { + } else if (pooling === "mean") { + result = (0, _utils_tensor_js__WEBPACK_IMPORTED_MODULE_7__.mean_pooling)(result, model_inputs.attention_mask); + } else if (pooling === "cls") { + result = result.slice(null, 0); + } else { + throw Error(`Pooling method '${pooling}' not supported.`); + } + if (normalize) { + result = result.normalize(2, -1); + } + if (quantize) { + result = (0, _utils_tensor_js__WEBPACK_IMPORTED_MODULE_7__.quantize_embeddings)(result, precision); + } + return result; + } + } + class ImageFeatureExtractionPipeline extends /** @type {new (options: ImagePipelineConstructorArgs) => ImageFeatureExtractionPipelineType} */ + Pipeline { + /** + * Create a new ImageFeatureExtractionPipeline. + * @param {ImagePipelineConstructorArgs} options An object used to instantiate the pipeline. + */ + constructor(options) { + super(options); + } + /** @type {ImageFeatureExtractionPipelineCallback} */ + async _call(images, { + pool = null + } = {}) { + const preparedImages = await prepareImages(images); + const { pixel_values } = await this.processor(preparedImages); + const outputs = await this.model({ pixel_values }); + let result; + if (pool) { + if (!("pooler_output" in outputs)) { + throw Error(`No pooled output was returned. Make sure the model has a 'pooler' layer when using the 'pool' option.`); + } + result = outputs.pooler_output; + } else { + result = outputs.last_hidden_state ?? outputs.logits ?? outputs.image_embeds; + } + return result; + } + } + class AudioClassificationPipeline extends /** @type {new (options: AudioPipelineConstructorArgs) => AudioClassificationPipelineType} */ + Pipeline { + /** + * Create a new AudioClassificationPipeline. + * @param {AudioPipelineConstructorArgs} options An object used to instantiate the pipeline. + */ + constructor(options) { + super(options); + } + /** @type {AudioClassificationPipelineCallback} */ + async _call(audio, { + top_k = 5 + } = {}) { + const sampling_rate = this.processor.feature_extractor.config.sampling_rate; + const preparedAudios = await prepareAudios(audio, sampling_rate); + const id2label = this.model.config.id2label; + const toReturn = []; + for (const aud of preparedAudios) { + const inputs = await this.processor(aud); + const output = await this.model(inputs); + const logits = output.logits[0]; + const scores = await (0, _utils_tensor_js__WEBPACK_IMPORTED_MODULE_7__.topk)(new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_7__.Tensor( + "float32", + (0, _utils_maths_js__WEBPACK_IMPORTED_MODULE_5__.softmax)(logits.data), + logits.dims + ), top_k); + const values = scores[0].tolist(); + const indices = scores[1].tolist(); + const vals = indices.map((x, i) => ({ + label: ( + /** @type {string} */ + id2label ? id2label[x] : `LABEL_${x}` + ), + score: ( + /** @type {number} */ + values[i] + ) + })); + toReturn.push(vals); + } + ; + return Array.isArray(audio) ? toReturn : toReturn[0]; + } + } + class ZeroShotAudioClassificationPipeline extends /** @type {new (options: TextAudioPipelineConstructorArgs) => ZeroShotAudioClassificationPipelineType} */ + Pipeline { + /** + * Create a new ZeroShotAudioClassificationPipeline. + * @param {TextAudioPipelineConstructorArgs} options An object used to instantiate the pipeline. + */ + constructor(options) { + super(options); + } + /** @type {ZeroShotAudioClassificationPipelineCallback} */ + async _call(audio, candidate_labels, { + hypothesis_template = "This is a sound of {}." + } = {}) { + const single = !Array.isArray(audio); + if (single) { + audio = [ + /** @type {AudioInput} */ + audio + ]; + } + const texts = candidate_labels.map( + (x) => hypothesis_template.replace("{}", x) + ); + const text_inputs = this.tokenizer(texts, { + padding: true, + truncation: true + }); + const sampling_rate = this.processor.feature_extractor.config.sampling_rate; + const preparedAudios = await prepareAudios(audio, sampling_rate); + const toReturn = []; + for (const aud of preparedAudios) { + const audio_inputs = await this.processor(aud); + const output = await this.model({ ...text_inputs, ...audio_inputs }); + const probs = (0, _utils_maths_js__WEBPACK_IMPORTED_MODULE_5__.softmax)(output.logits_per_audio.data); + toReturn.push([...probs].map((x, i) => ({ + score: x, + label: candidate_labels[i] + }))); + } + return single ? toReturn[0] : toReturn; + } + } + class AutomaticSpeechRecognitionPipeline extends /** @type {new (options: TextAudioPipelineConstructorArgs) => AutomaticSpeechRecognitionPipelineType} */ + Pipeline { + /** + * Create a new AutomaticSpeechRecognitionPipeline. + * @param {TextAudioPipelineConstructorArgs} options An object used to instantiate the pipeline. + */ + constructor(options) { + super(options); + } + /** @type {AutomaticSpeechRecognitionPipelineCallback} */ + async _call(audio, kwargs = {}) { + switch (this.model.config.model_type) { + case "whisper": + return this._call_whisper(audio, kwargs); + case "wav2vec2": + case "wav2vec2-bert": + case "unispeech": + case "unispeech-sat": + case "hubert": + return this._call_wav2vec2(audio, kwargs); + default: + throw new Error(`AutomaticSpeechRecognitionPipeline does not support model type '${this.model.config.model_type}'.`); + } + } + /** + * @type {AutomaticSpeechRecognitionPipelineCallback} + * @private + */ + async _call_wav2vec2(audio, kwargs) { + if (kwargs.language) { + console.warn('`language` parameter is not yet supported for `wav2vec2` models, defaulting to "English".'); + } + if (kwargs.task) { + console.warn('`task` parameter is not yet supported for `wav2vec2` models, defaulting to "transcribe".'); + } + const single = !Array.isArray(audio); + if (single) { + audio = [ + /** @type {AudioInput} */ + audio + ]; + } + const sampling_rate = this.processor.feature_extractor.config.sampling_rate; + const preparedAudios = await prepareAudios(audio, sampling_rate); + const toReturn = []; + for (const aud of preparedAudios) { + const inputs = await this.processor(aud); + const output = await this.model(inputs); + const logits = output.logits[0]; + const predicted_ids = []; + for (const item of logits) { + predicted_ids.push((0, _utils_maths_js__WEBPACK_IMPORTED_MODULE_5__.max)(item.data)[1]); + } + const predicted_sentences = this.tokenizer.decode(predicted_ids); + toReturn.push({ text: predicted_sentences }); + } + return single ? toReturn[0] : toReturn; + } + /** + * @type {AutomaticSpeechRecognitionPipelineCallback} + * @private + */ + async _call_whisper(audio, kwargs) { + const return_timestamps = kwargs.return_timestamps ?? false; + const chunk_length_s = kwargs.chunk_length_s ?? 0; + const force_full_sequences = kwargs.force_full_sequences ?? false; + let stride_length_s = kwargs.stride_length_s ?? null; + const generation_config = { ...kwargs }; + if (return_timestamps === "word") { + generation_config["return_token_timestamps"] = true; + generation_config["return_timestamps"] = false; + } + const single = !Array.isArray(audio); + if (single) { + audio = [ + /** @type {AudioInput} */ + audio + ]; + } + const time_precision = this.processor.feature_extractor.config.chunk_length / this.model.config.max_source_positions; + const hop_length = this.processor.feature_extractor.config.hop_length; + const sampling_rate = this.processor.feature_extractor.config.sampling_rate; + const preparedAudios = await prepareAudios(audio, sampling_rate); + const toReturn = []; + for (const aud of preparedAudios) { + let chunks = []; + if (chunk_length_s > 0) { + if (stride_length_s === null) { + stride_length_s = chunk_length_s / 6; + } else if (chunk_length_s <= stride_length_s) { + throw Error("`chunk_length_s` must be larger than `stride_length_s`."); + } + const window2 = sampling_rate * chunk_length_s; + const stride = sampling_rate * stride_length_s; + const jump = window2 - 2 * stride; + let offset = 0; + while (true) { + const offset_end = offset + window2; + const subarr = aud.subarray(offset, offset_end); + const feature = await this.processor(subarr); + const is_first = offset === 0; + const is_last = offset_end >= aud.length; + chunks.push({ + stride: [ + subarr.length, + is_first ? 0 : stride, + is_last ? 0 : stride + ], + input_features: feature.input_features, + is_last + }); + if (is_last) break; + offset += jump; + } + } else { + chunks = [{ + stride: [aud.length, 0, 0], + input_features: (await this.processor(aud)).input_features, + is_last: true + }]; + } + for (const chunk of chunks) { + generation_config.num_frames = Math.floor(chunk.stride[0] / hop_length); + const data = await this.model.generate({ + inputs: chunk.input_features, + ...generation_config + }); + if (return_timestamps === "word") { + chunk.tokens = data.sequences.tolist()[0]; + chunk.token_timestamps = data.token_timestamps.tolist()[0].map( + (x) => (0, _utils_maths_js__WEBPACK_IMPORTED_MODULE_5__.round)(x, 2) + ); + } else { + chunk.tokens = /** @type {Tensor} */ + data[0].tolist(); + } + chunk.stride = chunk.stride.map((x) => x / sampling_rate); + } + const [full_text, optional] = this.tokenizer._decode_asr(chunks, { + time_precision, + return_timestamps, + force_full_sequences + }); + toReturn.push({ text: full_text, ...optional }); + } + return single ? toReturn[0] : toReturn; + } + } + class ImageToTextPipeline extends /** @type {new (options: TextImagePipelineConstructorArgs) => ImageToTextPipelineType} */ + Pipeline { + /** + * Create a new ImageToTextPipeline. + * @param {TextImagePipelineConstructorArgs} options An object used to instantiate the pipeline. + */ + constructor(options) { + super(options); + } + /** @type {ImageToTextPipelineCallback} */ + async _call(images, generate_kwargs = {}) { + const isBatched = Array.isArray(images); + const preparedImages = await prepareImages(images); + const { pixel_values } = await this.processor(preparedImages); + const toReturn = []; + for (const batch of pixel_values) { + batch.dims = [1, ...batch.dims]; + const output = await this.model.generate({ inputs: batch, ...generate_kwargs }); + const decoded = this.tokenizer.batch_decode( + /** @type {Tensor} */ + output, + { + skip_special_tokens: true + } + ).map((x) => ({ generated_text: x.trim() })); + toReturn.push(decoded); + } + return isBatched ? toReturn : toReturn[0]; + } + } + class ImageClassificationPipeline extends /** @type {new (options: ImagePipelineConstructorArgs) => ImageClassificationPipelineType} */ + Pipeline { + /** + * Create a new ImageClassificationPipeline. + * @param {ImagePipelineConstructorArgs} options An object used to instantiate the pipeline. + */ + constructor(options) { + super(options); + } + /** @type {ImageClassificationPipelineCallback} */ + async _call(images, { + top_k = 5 + } = {}) { + const preparedImages = await prepareImages(images); + const { pixel_values } = await this.processor(preparedImages); + const output = await this.model({ pixel_values }); + const id2label = this.model.config.id2label; + const toReturn = []; + for (const batch of output.logits) { + const scores = await (0, _utils_tensor_js__WEBPACK_IMPORTED_MODULE_7__.topk)(new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_7__.Tensor( + "float32", + (0, _utils_maths_js__WEBPACK_IMPORTED_MODULE_5__.softmax)(batch.data), + batch.dims + ), top_k); + const values = scores[0].tolist(); + const indices = scores[1].tolist(); + const vals = indices.map((x, i) => ({ + label: ( + /** @type {string} */ + id2label ? id2label[x] : `LABEL_${x}` + ), + score: ( + /** @type {number} */ + values[i] + ) + })); + toReturn.push(vals); + } + return Array.isArray(images) ? toReturn : toReturn[0]; + } + } + class ImageSegmentationPipeline extends /** @type {new (options: ImagePipelineConstructorArgs) => ImageSegmentationPipelineType} */ + Pipeline { + /** + * Create a new ImageSegmentationPipeline. + * @param {ImagePipelineConstructorArgs} options An object used to instantiate the pipeline. + */ + constructor(options) { + super(options); + this.subtasks_mapping = { + // Mapping of subtasks to their corresponding post-processing function names. + panoptic: "post_process_panoptic_segmentation", + instance: "post_process_instance_segmentation", + semantic: "post_process_semantic_segmentation" + }; + } + /** @type {ImageSegmentationPipelineCallback} */ + async _call(images, { + threshold = 0.5, + mask_threshold = 0.5, + overlap_mask_area_threshold = 0.8, + label_ids_to_fuse = null, + target_sizes = null, + subtask = null + } = {}) { + const isBatched = Array.isArray(images); + if (isBatched && images.length !== 1) { + throw Error("Image segmentation pipeline currently only supports a batch size of 1."); + } + const preparedImages = await prepareImages(images); + const imageSizes = preparedImages.map((x) => [x.height, x.width]); + const { pixel_values, pixel_mask } = await this.processor(preparedImages); + const output = await this.model({ pixel_values, pixel_mask }); + let fn = null; + if (subtask !== null) { + fn = this.subtasks_mapping[subtask]; + } else { + for (let [task, func] of Object.entries(this.subtasks_mapping)) { + if (func in this.processor.feature_extractor) { + fn = this.processor.feature_extractor[func].bind(this.processor.feature_extractor); + subtask = task; + break; + } + } + } + const id2label = this.model.config.id2label; + const annotation = []; + if (subtask === "panoptic" || subtask === "instance") { + const processed = fn( + output, + threshold, + mask_threshold, + overlap_mask_area_threshold, + label_ids_to_fuse, + target_sizes ?? imageSizes + // TODO FIX? + )[0]; + const segmentation = processed.segmentation; + for (const segment of processed.segments_info) { + const maskData = new Uint8ClampedArray(segmentation.data.length); + for (let i = 0; i < segmentation.data.length; ++i) { + if (segmentation.data[i] === segment.id) { + maskData[i] = 255; + } + } + const mask = new _utils_image_js__WEBPACK_IMPORTED_MODULE_8__.RawImage(maskData, segmentation.dims[1], segmentation.dims[0], 1); + annotation.push({ + score: segment.score, + label: id2label[segment.label_id], + mask + }); + } + } else if (subtask === "semantic") { + const { segmentation, labels } = fn(output, target_sizes ?? imageSizes)[0]; + for (const label of labels) { + const maskData = new Uint8ClampedArray(segmentation.data.length); + for (let i = 0; i < segmentation.data.length; ++i) { + if (segmentation.data[i] === label) { + maskData[i] = 255; + } + } + const mask = new _utils_image_js__WEBPACK_IMPORTED_MODULE_8__.RawImage(maskData, segmentation.dims[1], segmentation.dims[0], 1); + annotation.push({ + score: null, + label: id2label[label], + mask + }); + } + } else { + throw Error(`Subtask ${subtask} not supported.`); + } + return annotation; + } + } + class ZeroShotImageClassificationPipeline extends /** @type {new (options: TextImagePipelineConstructorArgs) => ZeroShotImageClassificationPipelineType} */ + Pipeline { + /** + * Create a new ZeroShotImageClassificationPipeline. + * @param {TextImagePipelineConstructorArgs} options An object used to instantiate the pipeline. + */ + constructor(options) { + super(options); + } + /** @type {ZeroShotImageClassificationPipelineCallback} */ + async _call(images, candidate_labels, { + hypothesis_template = "This is a photo of {}" + } = {}) { + const isBatched = Array.isArray(images); + const preparedImages = await prepareImages(images); + const texts = candidate_labels.map( + (x) => hypothesis_template.replace("{}", x) + ); + const text_inputs = this.tokenizer(texts, { + padding: this.model.config.model_type === "siglip" ? "max_length" : true, + truncation: true + }); + const { pixel_values } = await this.processor(preparedImages); + const output = await this.model({ ...text_inputs, pixel_values }); + const function_to_apply = this.model.config.model_type === "siglip" ? (batch) => batch.sigmoid().data : (batch) => (0, _utils_maths_js__WEBPACK_IMPORTED_MODULE_5__.softmax)(batch.data); + const toReturn = []; + for (const batch of output.logits_per_image) { + const probs = function_to_apply(batch); + const result = [...probs].map((x, i) => ({ + score: x, + label: candidate_labels[i] + })); + result.sort((a, b) => b.score - a.score); + toReturn.push(result); + } + return isBatched ? toReturn : toReturn[0]; + } + } + class ObjectDetectionPipeline extends /** @type {new (options: ImagePipelineConstructorArgs) => ObjectDetectionPipelineType} */ + Pipeline { + /** + * Create a new ObjectDetectionPipeline. + * @param {ImagePipelineConstructorArgs} options An object used to instantiate the pipeline. + */ + constructor(options) { + super(options); + } + /** @type {ObjectDetectionPipelineCallback} */ + async _call(images, { + threshold = 0.9, + percentage = false + } = {}) { + const isBatched = Array.isArray(images); + if (isBatched && images.length !== 1) { + throw Error("Object detection pipeline currently only supports a batch size of 1."); + } + const preparedImages = await prepareImages(images); + const imageSizes = percentage ? null : preparedImages.map((x) => [x.height, x.width]); + const { pixel_values, pixel_mask } = await this.processor(preparedImages); + const output = await this.model({ pixel_values, pixel_mask }); + const processed = this.processor.feature_extractor.post_process_object_detection(output, threshold, imageSizes); + const id2label = this.model.config.id2label; + const result = processed.map((batch) => batch.boxes.map((box, i) => ({ + score: batch.scores[i], + label: id2label[batch.classes[i]], + box: get_bounding_box(box, !percentage) + }))); + return isBatched ? result : result[0]; + } + } + class ZeroShotObjectDetectionPipeline extends /** @type {new (options: TextImagePipelineConstructorArgs) => ZeroShotObjectDetectionPipelineType} */ + Pipeline { + /** + * Create a new ZeroShotObjectDetectionPipeline. + * @param {TextImagePipelineConstructorArgs} options An object used to instantiate the pipeline. + */ + constructor(options) { + super(options); + } + /** @type {ZeroShotObjectDetectionPipelineCallback} */ + async _call(images, candidate_labels, { + threshold = 0.1, + top_k = null, + percentage = false + } = {}) { + const isBatched = Array.isArray(images); + const preparedImages = await prepareImages(images); + const text_inputs = this.tokenizer(candidate_labels, { + padding: true, + truncation: true + }); + const model_inputs = await this.processor(preparedImages); + const toReturn = []; + for (let i = 0; i < preparedImages.length; ++i) { + const image = preparedImages[i]; + const imageSize = percentage ? null : [[image.height, image.width]]; + const pixel_values = model_inputs.pixel_values[i].unsqueeze_(0); + const output = await this.model({ ...text_inputs, pixel_values }); + const processed = this.processor.feature_extractor.post_process_object_detection(output, threshold, imageSize, true)[0]; + let result = processed.boxes.map((box, i2) => ({ + score: processed.scores[i2], + label: candidate_labels[processed.classes[i2]], + box: get_bounding_box(box, !percentage) + })).sort((a, b) => b.score - a.score); + if (top_k !== null) { + result = result.slice(0, top_k); + } + toReturn.push(result); + } + return isBatched ? toReturn : toReturn[0]; + } + } + class DocumentQuestionAnsweringPipeline extends /** @type {new (options: TextImagePipelineConstructorArgs) => DocumentQuestionAnsweringPipelineType} */ + Pipeline { + /** + * Create a new DocumentQuestionAnsweringPipeline. + * @param {TextImagePipelineConstructorArgs} options An object used to instantiate the pipeline. + */ + constructor(options) { + super(options); + } + /** @type {DocumentQuestionAnsweringPipelineCallback} */ + async _call(image, question, generate_kwargs = {}) { + throw new Error("This pipeline is not yet supported in Transformers.js v3."); + const preparedImage = (await prepareImages(image))[0]; + const { pixel_values } = await this.processor(preparedImage); + const task_prompt = `${question}`; + const decoder_input_ids = this.tokenizer(task_prompt, { + add_special_tokens: false, + padding: true, + truncation: true + }).input_ids; + const output = await this.model.generate({ + inputs: pixel_values, + max_length: this.model.config.decoder.max_position_embeddings, + decoder_input_ids, + ...generate_kwargs + }); + const decoded = this.tokenizer.batch_decode( + /** @type {Tensor} */ + output + )[0]; + const match = decoded.match(/(.*?)<\/s_answer>/); + let answer = null; + if (match && match.length >= 2) { + answer = match[1].trim(); + } + return [{ answer }]; + } + } + class TextToAudioPipeline extends /** @type {new (options: TextToAudioPipelineConstructorArgs) => TextToAudioPipelineType} */ + Pipeline { + /** + * Create a new TextToAudioPipeline. + * @param {TextToAudioPipelineConstructorArgs} options An object used to instantiate the pipeline. + */ + constructor(options) { + super(options); + __publicField(this, "DEFAULT_VOCODER_ID", "Xenova/speecht5_hifigan"); + this.vocoder = options.vocoder ?? null; + } + /** @type {TextToAudioPipelineCallback} */ + async _call(text_inputs, { + speaker_embeddings = null + } = {}) { + if (this.processor) { + return this._call_text_to_spectrogram(text_inputs, { speaker_embeddings }); + } else { + return this._call_text_to_waveform(text_inputs); + } + } + async _call_text_to_waveform(text_inputs) { + const inputs = this.tokenizer(text_inputs, { + padding: true, + truncation: true + }); + const { waveform } = await this.model(inputs); + const sampling_rate = this.model.config.sampling_rate; + return { + audio: waveform.data, + sampling_rate + }; + } + async _call_text_to_spectrogram(text_inputs, { speaker_embeddings }) { + if (!this.vocoder) { + console.log("No vocoder specified, using default HifiGan vocoder."); + this.vocoder = await _models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModel.from_pretrained(this.DEFAULT_VOCODER_ID, { dtype: "fp32" }); + } + if (typeof speaker_embeddings === "string" || speaker_embeddings instanceof URL) { + speaker_embeddings = new Float32Array( + await (await fetch(speaker_embeddings)).arrayBuffer() + ); + } + if (speaker_embeddings instanceof Float32Array) { + speaker_embeddings = new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_7__.Tensor( + "float32", + speaker_embeddings, + [1, speaker_embeddings.length] + ); + } else if (!(speaker_embeddings instanceof _utils_tensor_js__WEBPACK_IMPORTED_MODULE_7__.Tensor)) { + throw new Error("Speaker embeddings must be a `Tensor`, `Float32Array`, `string`, or `URL`."); + } + const { input_ids } = this.tokenizer(text_inputs, { + padding: true, + truncation: true + }); + const { waveform } = await this.model.generate_speech(input_ids, speaker_embeddings, { vocoder: this.vocoder }); + const sampling_rate = this.processor.feature_extractor.config.sampling_rate; + return { + audio: waveform.data, + sampling_rate + }; + } + } + class ImageToImagePipeline extends /** @type {new (options: ImagePipelineConstructorArgs) => ImageToImagePipelineType} */ + Pipeline { + /** + * Create a new ImageToImagePipeline. + * @param {ImagePipelineConstructorArgs} options An object used to instantiate the pipeline. + */ + constructor(options) { + super(options); + } + /** @type {ImageToImagePipelineCallback} */ + async _call(images) { + const preparedImages = await prepareImages(images); + const inputs = await this.processor(preparedImages); + const outputs = await this.model(inputs); + const toReturn = []; + for (const batch of outputs.reconstruction) { + const output = batch.squeeze().clamp_(0, 1).mul_(255).round_().to("uint8"); + toReturn.push(_utils_image_js__WEBPACK_IMPORTED_MODULE_8__.RawImage.fromTensor(output)); + } + return toReturn.length > 1 ? toReturn : toReturn[0]; + } + } + class DepthEstimationPipeline extends /** @type {new (options: ImagePipelineConstructorArgs) => DepthEstimationPipelineType} */ + Pipeline { + /** + * Create a new DepthEstimationPipeline. + * @param {ImagePipelineConstructorArgs} options An object used to instantiate the pipeline. + */ + constructor(options) { + super(options); + } + /** @type {DepthEstimationPipelineCallback} */ + async _call(images) { + const preparedImages = await prepareImages(images); + const inputs = await this.processor(preparedImages); + const { predicted_depth } = await this.model(inputs); + const toReturn = []; + for (let i = 0; i < preparedImages.length; ++i) { + const prediction = (0, _utils_tensor_js__WEBPACK_IMPORTED_MODULE_7__.interpolate)(predicted_depth[i], preparedImages[i].size.reverse(), "bilinear", false); + const formatted = prediction.mul_(255 / (0, _utils_maths_js__WEBPACK_IMPORTED_MODULE_5__.max)(prediction.data)[0]).to("uint8"); + toReturn.push({ + predicted_depth: predicted_depth[i], + depth: _utils_image_js__WEBPACK_IMPORTED_MODULE_8__.RawImage.fromTensor(formatted) + }); + } + return toReturn.length > 1 ? toReturn : toReturn[0]; + } + } + const SUPPORTED_TASKS = Object.freeze({ + "text-classification": { + "tokenizer": _tokenizers_js__WEBPACK_IMPORTED_MODULE_0__.AutoTokenizer, + "pipeline": TextClassificationPipeline, + "model": _models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModelForSequenceClassification, + "default": { + // TODO: replace with original + // "model": "distilbert-base-uncased-finetuned-sst-2-english", + "model": "Xenova/distilbert-base-uncased-finetuned-sst-2-english" + }, + "type": "text" + }, + "token-classification": { + "tokenizer": _tokenizers_js__WEBPACK_IMPORTED_MODULE_0__.AutoTokenizer, + "pipeline": TokenClassificationPipeline, + "model": _models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModelForTokenClassification, + "default": { + // TODO: replace with original + // "model": "Davlan/bert-base-multilingual-cased-ner-hrl", + "model": "Xenova/bert-base-multilingual-cased-ner-hrl" + }, + "type": "text" + }, + "question-answering": { + "tokenizer": _tokenizers_js__WEBPACK_IMPORTED_MODULE_0__.AutoTokenizer, + "pipeline": QuestionAnsweringPipeline, + "model": _models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModelForQuestionAnswering, + "default": { + // TODO: replace with original + // "model": "distilbert-base-cased-distilled-squad", + "model": "Xenova/distilbert-base-cased-distilled-squad" + }, + "type": "text" + }, + "fill-mask": { + "tokenizer": _tokenizers_js__WEBPACK_IMPORTED_MODULE_0__.AutoTokenizer, + "pipeline": FillMaskPipeline, + "model": _models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModelForMaskedLM, + "default": { + // TODO: replace with original + // "model": "bert-base-uncased", + "model": "Xenova/bert-base-uncased" + }, + "type": "text" + }, + "summarization": { + "tokenizer": _tokenizers_js__WEBPACK_IMPORTED_MODULE_0__.AutoTokenizer, + "pipeline": SummarizationPipeline, + "model": _models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModelForSeq2SeqLM, + "default": { + // TODO: replace with original + // "model": "sshleifer/distilbart-cnn-6-6", + "model": "Xenova/distilbart-cnn-6-6" + }, + "type": "text" + }, + "translation": { + "tokenizer": _tokenizers_js__WEBPACK_IMPORTED_MODULE_0__.AutoTokenizer, + "pipeline": TranslationPipeline, + "model": _models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModelForSeq2SeqLM, + "default": { + // TODO: replace with original + // "model": "t5-small", + "model": "Xenova/t5-small" + }, + "type": "text" + }, + "text2text-generation": { + "tokenizer": _tokenizers_js__WEBPACK_IMPORTED_MODULE_0__.AutoTokenizer, + "pipeline": Text2TextGenerationPipeline, + "model": _models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModelForSeq2SeqLM, + "default": { + // TODO: replace with original + // "model": "google/flan-t5-small", + "model": "Xenova/flan-t5-small" + }, + "type": "text" + }, + "text-generation": { + "tokenizer": _tokenizers_js__WEBPACK_IMPORTED_MODULE_0__.AutoTokenizer, + "pipeline": TextGenerationPipeline, + "model": _models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModelForCausalLM, + "default": { + // TODO: replace with original + // "model": "gpt2", + "model": "Xenova/gpt2" + }, + "type": "text" + }, + "zero-shot-classification": { + "tokenizer": _tokenizers_js__WEBPACK_IMPORTED_MODULE_0__.AutoTokenizer, + "pipeline": ZeroShotClassificationPipeline, + "model": _models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModelForSequenceClassification, + "default": { + // TODO: replace with original + // "model": "typeform/distilbert-base-uncased-mnli", + "model": "Xenova/distilbert-base-uncased-mnli" + }, + "type": "text" + }, + "audio-classification": { + "pipeline": AudioClassificationPipeline, + "model": _models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModelForAudioClassification, + "processor": _processors_js__WEBPACK_IMPORTED_MODULE_2__.AutoProcessor, + "default": { + // TODO: replace with original + // "model": "superb/wav2vec2-base-superb-ks", + "model": "Xenova/wav2vec2-base-superb-ks" + }, + "type": "audio" + }, + "zero-shot-audio-classification": { + "tokenizer": _tokenizers_js__WEBPACK_IMPORTED_MODULE_0__.AutoTokenizer, + "pipeline": ZeroShotAudioClassificationPipeline, + "model": _models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModel, + "processor": _processors_js__WEBPACK_IMPORTED_MODULE_2__.AutoProcessor, + "default": { + // TODO: replace with original + // "model": "laion/clap-htsat-fused", + "model": "Xenova/clap-htsat-unfused" + }, + "type": "multimodal" + }, + "automatic-speech-recognition": { + "tokenizer": _tokenizers_js__WEBPACK_IMPORTED_MODULE_0__.AutoTokenizer, + "pipeline": AutomaticSpeechRecognitionPipeline, + "model": [_models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModelForSpeechSeq2Seq, _models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModelForCTC], + "processor": _processors_js__WEBPACK_IMPORTED_MODULE_2__.AutoProcessor, + "default": { + // TODO: replace with original + // "model": "openai/whisper-tiny.en", + "model": "Xenova/whisper-tiny.en" + }, + "type": "multimodal" + }, + "text-to-audio": { + "tokenizer": _tokenizers_js__WEBPACK_IMPORTED_MODULE_0__.AutoTokenizer, + "pipeline": TextToAudioPipeline, + "model": [_models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModelForTextToWaveform, _models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModelForTextToSpectrogram], + "processor": [ + _processors_js__WEBPACK_IMPORTED_MODULE_2__.AutoProcessor, + /* Some don't use a processor */ + null + ], + "default": { + // TODO: replace with original + // "model": "microsoft/speecht5_tts", + "model": "Xenova/speecht5_tts" + }, + "type": "text" + }, + "image-to-text": { + "tokenizer": _tokenizers_js__WEBPACK_IMPORTED_MODULE_0__.AutoTokenizer, + "pipeline": ImageToTextPipeline, + "model": _models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModelForVision2Seq, + "processor": _processors_js__WEBPACK_IMPORTED_MODULE_2__.AutoProcessor, + "default": { + // TODO: replace with original + // "model": "nlpconnect/vit-gpt2-image-captioning", + "model": "Xenova/vit-gpt2-image-captioning" + }, + "type": "multimodal" + }, + "image-classification": { + // no tokenizer + "pipeline": ImageClassificationPipeline, + "model": _models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModelForImageClassification, + "processor": _processors_js__WEBPACK_IMPORTED_MODULE_2__.AutoProcessor, + "default": { + // TODO: replace with original + // "model": "google/vit-base-patch16-224", + "model": "Xenova/vit-base-patch16-224" + }, + "type": "multimodal" + }, + "image-segmentation": { + // no tokenizer + "pipeline": ImageSegmentationPipeline, + "model": [_models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModelForImageSegmentation, _models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModelForSemanticSegmentation, _models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModelForUniversalSegmentation], + "processor": _processors_js__WEBPACK_IMPORTED_MODULE_2__.AutoProcessor, + "default": { + // TODO: replace with original + // "model": "facebook/detr-resnet-50-panoptic", + "model": "Xenova/detr-resnet-50-panoptic" + }, + "type": "multimodal" + }, + "zero-shot-image-classification": { + "tokenizer": _tokenizers_js__WEBPACK_IMPORTED_MODULE_0__.AutoTokenizer, + "pipeline": ZeroShotImageClassificationPipeline, + "model": _models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModel, + "processor": _processors_js__WEBPACK_IMPORTED_MODULE_2__.AutoProcessor, + "default": { + // TODO: replace with original + // "model": "openai/clip-vit-base-patch32", + "model": "Xenova/clip-vit-base-patch32" + }, + "type": "multimodal" + }, + "object-detection": { + // no tokenizer + "pipeline": ObjectDetectionPipeline, + "model": _models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModelForObjectDetection, + "processor": _processors_js__WEBPACK_IMPORTED_MODULE_2__.AutoProcessor, + "default": { + // TODO: replace with original + // "model": "facebook/detr-resnet-50", + "model": "Xenova/detr-resnet-50" + }, + "type": "multimodal" + }, + "zero-shot-object-detection": { + "tokenizer": _tokenizers_js__WEBPACK_IMPORTED_MODULE_0__.AutoTokenizer, + "pipeline": ZeroShotObjectDetectionPipeline, + "model": _models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModelForZeroShotObjectDetection, + "processor": _processors_js__WEBPACK_IMPORTED_MODULE_2__.AutoProcessor, + "default": { + // TODO: replace with original + // "model": "google/owlvit-base-patch32", + "model": "Xenova/owlvit-base-patch32" + }, + "type": "multimodal" + }, + "document-question-answering": { + "tokenizer": _tokenizers_js__WEBPACK_IMPORTED_MODULE_0__.AutoTokenizer, + "pipeline": DocumentQuestionAnsweringPipeline, + "model": _models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModelForDocumentQuestionAnswering, + "processor": _processors_js__WEBPACK_IMPORTED_MODULE_2__.AutoProcessor, + "default": { + // TODO: replace with original + // "model": "naver-clova-ix/donut-base-finetuned-docvqa", + "model": "Xenova/donut-base-finetuned-docvqa" + }, + "type": "multimodal" + }, + "image-to-image": { + // no tokenizer + "pipeline": ImageToImagePipeline, + "model": _models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModelForImageToImage, + "processor": _processors_js__WEBPACK_IMPORTED_MODULE_2__.AutoProcessor, + "default": { + // TODO: replace with original + // "model": "caidas/swin2SR-classical-sr-x2-64", + "model": "Xenova/swin2SR-classical-sr-x2-64" + }, + "type": "image" + }, + "depth-estimation": { + // no tokenizer + "pipeline": DepthEstimationPipeline, + "model": _models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModelForDepthEstimation, + "processor": _processors_js__WEBPACK_IMPORTED_MODULE_2__.AutoProcessor, + "default": { + // TODO: replace with original + // "model": "Intel/dpt-large", + "model": "Xenova/dpt-large" + }, + "type": "image" + }, + // This task serves as a useful interface for dealing with sentence-transformers (https://huggingface.co/sentence-transformers). + "feature-extraction": { + "tokenizer": _tokenizers_js__WEBPACK_IMPORTED_MODULE_0__.AutoTokenizer, + "pipeline": FeatureExtractionPipeline, + "model": _models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModel, + "default": { + // TODO: replace with original + // "model": "sentence-transformers/all-MiniLM-L6-v2", + "model": "Xenova/all-MiniLM-L6-v2" + }, + "type": "text" + }, + "image-feature-extraction": { + "processor": _processors_js__WEBPACK_IMPORTED_MODULE_2__.AutoProcessor, + "pipeline": ImageFeatureExtractionPipeline, + "model": [_models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModelForImageFeatureExtraction, _models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModel], + "default": { + // TODO: replace with original + // "model": "google/vit-base-patch16-224", + "model": "Xenova/vit-base-patch16-224-in21k" + }, + "type": "image" + } + }); + const TASK_ALIASES = Object.freeze({ + "sentiment-analysis": "text-classification", + "ner": "token-classification", + // "vqa": "visual-question-answering", // TODO: Add + "asr": "automatic-speech-recognition", + "text-to-speech": "text-to-audio", + // Add for backwards compatibility + "embeddings": "feature-extraction" + }); + async function pipeline(task, model = null, { + progress_callback = null, + config = null, + cache_dir = null, + local_files_only = false, + revision = "main", + device = null, + dtype = null, + model_file_name = null, + session_options = {} + } = {}) { + task = TASK_ALIASES[task] ?? task; + const pipelineInfo = SUPPORTED_TASKS[task.split("_", 1)[0]]; + if (!pipelineInfo) { + throw Error(`Unsupported pipeline: ${task}. Must be one of [${Object.keys(SUPPORTED_TASKS)}]`); + } + if (!model) { + model = pipelineInfo.default.model; + console.log(`No model specified. Using default model: "${model}".`); + } + const pretrainedOptions = { + progress_callback, + config, + cache_dir, + local_files_only, + revision, + device, + dtype, + model_file_name, + session_options + }; + const classes = /* @__PURE__ */ new Map([ + ["tokenizer", pipelineInfo.tokenizer], + ["model", pipelineInfo.model], + ["processor", pipelineInfo.processor] + ]); + const results = await loadItems(classes, model, pretrainedOptions); + results.task = task; + (0, _utils_core_js__WEBPACK_IMPORTED_MODULE_4__.dispatchCallback)(progress_callback, { + "status": "ready", + "task": task, + "model": model + }); + const pipelineClass = pipelineInfo.pipeline; + return new pipelineClass(results); + } + async function loadItems(mapping, model, pretrainedOptions) { + const result = /* @__PURE__ */ Object.create(null); + const promises = []; + for (const [name, cls] of mapping.entries()) { + if (!cls) continue; + let promise; + if (Array.isArray(cls)) { + promise = new Promise(async (resolve, reject) => { + var _a, _b; + let e; + for (const c of cls) { + if (c === null) { + resolve(null); + return; + } + try { + resolve(await c.from_pretrained(model, pretrainedOptions)); + return; + } catch (err) { + if ((_a = err.message) == null ? void 0 : _a.includes("Unsupported model type")) { + e = err; + } else if ((_b = err.message) == null ? void 0 : _b.includes("Could not locate file")) { + e = err; + } else { + reject(err); + return; + } + } + } + reject(e); + }); + } else { + promise = cls.from_pretrained(model, pretrainedOptions); + } + result[name] = promise; + promises.push(promise); + } + await Promise.all(promises); + for (const [name, promise] of Object.entries(result)) { + result[name] = await promise; + } + return result; + } + } + ), + /***/ + "./src/processors.js": ( + /*!***************************!*\ + !*** ./src/processors.js ***! + \***************************/ + /***/ + (__unused_webpack___webpack_module__, __webpack_exports__2, __webpack_require__2) => { + __webpack_require__2.r(__webpack_exports__2); + __webpack_require__2.d(__webpack_exports__2, { + /* harmony export */ + ASTFeatureExtractor: () => ( + /* binding */ + ASTFeatureExtractor + ), + /* harmony export */ + AutoProcessor: () => ( + /* binding */ + AutoProcessor + ), + /* harmony export */ + BeitFeatureExtractor: () => ( + /* binding */ + BeitFeatureExtractor + ), + /* harmony export */ + BitImageProcessor: () => ( + /* binding */ + BitImageProcessor + ), + /* harmony export */ + CLIPFeatureExtractor: () => ( + /* binding */ + CLIPFeatureExtractor + ), + /* harmony export */ + CLIPImageProcessor: () => ( + /* binding */ + CLIPImageProcessor + ), + /* harmony export */ + ChineseCLIPFeatureExtractor: () => ( + /* binding */ + ChineseCLIPFeatureExtractor + ), + /* harmony export */ + ClapFeatureExtractor: () => ( + /* binding */ + ClapFeatureExtractor + ), + /* harmony export */ + ConvNextFeatureExtractor: () => ( + /* binding */ + ConvNextFeatureExtractor + ), + /* harmony export */ + ConvNextImageProcessor: () => ( + /* binding */ + ConvNextImageProcessor + ), + /* harmony export */ + DPTFeatureExtractor: () => ( + /* binding */ + DPTFeatureExtractor + ), + /* harmony export */ + DPTImageProcessor: () => ( + /* binding */ + DPTImageProcessor + ), + /* harmony export */ + DeiTFeatureExtractor: () => ( + /* binding */ + DeiTFeatureExtractor + ), + /* harmony export */ + DetrFeatureExtractor: () => ( + /* binding */ + DetrFeatureExtractor + ), + /* harmony export */ + DonutFeatureExtractor: () => ( + /* binding */ + DonutFeatureExtractor + ), + /* harmony export */ + EfficientNetImageProcessor: () => ( + /* binding */ + EfficientNetImageProcessor + ), + /* harmony export */ + FeatureExtractor: () => ( + /* binding */ + FeatureExtractor + ), + /* harmony export */ + Florence2Processor: () => ( + /* binding */ + Florence2Processor + ), + /* harmony export */ + GLPNFeatureExtractor: () => ( + /* binding */ + GLPNFeatureExtractor + ), + /* harmony export */ + ImageFeatureExtractor: () => ( + /* binding */ + ImageFeatureExtractor + ), + /* harmony export */ + MaskFormerFeatureExtractor: () => ( + /* binding */ + MaskFormerFeatureExtractor + ), + /* harmony export */ + MobileNetV1FeatureExtractor: () => ( + /* binding */ + MobileNetV1FeatureExtractor + ), + /* harmony export */ + MobileNetV2FeatureExtractor: () => ( + /* binding */ + MobileNetV2FeatureExtractor + ), + /* harmony export */ + MobileNetV3FeatureExtractor: () => ( + /* binding */ + MobileNetV3FeatureExtractor + ), + /* harmony export */ + MobileNetV4FeatureExtractor: () => ( + /* binding */ + MobileNetV4FeatureExtractor + ), + /* harmony export */ + MobileViTFeatureExtractor: () => ( + /* binding */ + MobileViTFeatureExtractor + ), + /* harmony export */ + MobileViTImageProcessor: () => ( + /* binding */ + MobileViTImageProcessor + ), + /* harmony export */ + NougatImageProcessor: () => ( + /* binding */ + NougatImageProcessor + ), + /* harmony export */ + OwlViTFeatureExtractor: () => ( + /* binding */ + OwlViTFeatureExtractor + ), + /* harmony export */ + OwlViTProcessor: () => ( + /* binding */ + OwlViTProcessor + ), + /* harmony export */ + Owlv2ImageProcessor: () => ( + /* binding */ + Owlv2ImageProcessor + ), + /* harmony export */ + Processor: () => ( + /* binding */ + Processor + ), + /* harmony export */ + PvtImageProcessor: () => ( + /* binding */ + PvtImageProcessor + ), + /* harmony export */ + PyAnnoteFeatureExtractor: () => ( + /* binding */ + PyAnnoteFeatureExtractor + ), + /* harmony export */ + PyAnnoteProcessor: () => ( + /* binding */ + PyAnnoteProcessor + ), + /* harmony export */ + RTDetrImageProcessor: () => ( + /* binding */ + RTDetrImageProcessor + ), + /* harmony export */ + SamImageProcessor: () => ( + /* binding */ + SamImageProcessor + ), + /* harmony export */ + SamProcessor: () => ( + /* binding */ + SamProcessor + ), + /* harmony export */ + SapiensFeatureExtractor: () => ( + /* binding */ + SapiensFeatureExtractor + ), + /* harmony export */ + SeamlessM4TFeatureExtractor: () => ( + /* binding */ + SeamlessM4TFeatureExtractor + ), + /* harmony export */ + SegformerFeatureExtractor: () => ( + /* binding */ + SegformerFeatureExtractor + ), + /* harmony export */ + SiglipImageProcessor: () => ( + /* binding */ + SiglipImageProcessor + ), + /* harmony export */ + SpeechT5FeatureExtractor: () => ( + /* binding */ + SpeechT5FeatureExtractor + ), + /* harmony export */ + SpeechT5Processor: () => ( + /* binding */ + SpeechT5Processor + ), + /* harmony export */ + Swin2SRImageProcessor: () => ( + /* binding */ + Swin2SRImageProcessor + ), + /* harmony export */ + ViTFeatureExtractor: () => ( + /* binding */ + ViTFeatureExtractor + ), + /* harmony export */ + ViTImageProcessor: () => ( + /* binding */ + ViTImageProcessor + ), + /* harmony export */ + VitMatteImageProcessor: () => ( + /* binding */ + VitMatteImageProcessor + ), + /* harmony export */ + Wav2Vec2FeatureExtractor: () => ( + /* binding */ + Wav2Vec2FeatureExtractor + ), + /* harmony export */ + Wav2Vec2ProcessorWithLM: () => ( + /* binding */ + Wav2Vec2ProcessorWithLM + ), + /* harmony export */ + WeSpeakerFeatureExtractor: () => ( + /* binding */ + WeSpeakerFeatureExtractor + ), + /* harmony export */ + WhisperFeatureExtractor: () => ( + /* binding */ + WhisperFeatureExtractor + ), + /* harmony export */ + WhisperProcessor: () => ( + /* binding */ + WhisperProcessor + ), + /* harmony export */ + YolosFeatureExtractor: () => ( + /* binding */ + YolosFeatureExtractor + ) + /* harmony export */ + }); + var _utils_generic_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__2( + /*! ./utils/generic.js */ + "./src/utils/generic.js" + ); + var _utils_core_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__2( + /*! ./utils/core.js */ + "./src/utils/core.js" + ); + var _utils_hub_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__2( + /*! ./utils/hub.js */ + "./src/utils/hub.js" + ); + var _utils_maths_js__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__2( + /*! ./utils/maths.js */ + "./src/utils/maths.js" + ); + var _utils_tensor_js__WEBPACK_IMPORTED_MODULE_4__ = __webpack_require__2( + /*! ./utils/tensor.js */ + "./src/utils/tensor.js" + ); + var _utils_image_js__WEBPACK_IMPORTED_MODULE_5__ = __webpack_require__2( + /*! ./utils/image.js */ + "./src/utils/image.js" + ); + var _utils_audio_js__WEBPACK_IMPORTED_MODULE_6__2 = __webpack_require__2( + /*! ./utils/audio.js */ + "./src/utils/audio.js" + ); + function center_to_corners_format([centerX, centerY, width, height]) { + return [ + centerX - width / 2, + centerY - height / 2, + centerX + width / 2, + centerY + height / 2 + ]; + } + function post_process_object_detection(outputs, threshold = 0.5, target_sizes = null, is_zero_shot = false) { + const out_logits = outputs.logits; + const out_bbox = outputs.pred_boxes; + const [batch_size, num_boxes, num_classes] = out_logits.dims; + if (target_sizes !== null && target_sizes.length !== batch_size) { + throw Error("Make sure that you pass in as many target sizes as the batch dimension of the logits"); + } + let toReturn = []; + for (let i = 0; i < batch_size; ++i) { + let target_size = target_sizes !== null ? target_sizes[i] : null; + let info = { + boxes: [], + classes: [], + scores: [] + }; + let logits = out_logits[i]; + let bbox = out_bbox[i]; + for (let j = 0; j < num_boxes; ++j) { + let logit = logits[j]; + let indices = []; + let probs; + if (is_zero_shot) { + probs = logit.sigmoid().data; + for (let k = 0; k < probs.length; ++k) { + if (probs[k] > threshold) { + indices.push(k); + } + } + } else { + let maxIndex = (0, _utils_maths_js__WEBPACK_IMPORTED_MODULE_3__.max)(logit.data)[1]; + if (maxIndex === num_classes - 1) { + continue; + } + probs = (0, _utils_maths_js__WEBPACK_IMPORTED_MODULE_3__.softmax)(logit.data); + if (probs[maxIndex] < threshold) { + continue; + } + indices.push(maxIndex); + } + for (const index of indices) { + let box = bbox[j].data; + box = center_to_corners_format(box); + if (target_size !== null) { + box = box.map((x, i2) => x * target_size[(i2 + 1) % 2]); + } + info.boxes.push(box); + info.classes.push(index); + info.scores.push(probs[index]); + } + } + toReturn.push(info); + } + return toReturn; + } + function post_process_semantic_segmentation(outputs, target_sizes = null) { + const logits = outputs.logits; + const batch_size = logits.dims[0]; + if (target_sizes !== null && target_sizes.length !== batch_size) { + throw Error("Make sure that you pass in as many target sizes as the batch dimension of the logits"); + } + const toReturn = []; + for (let i = 0; i < batch_size; ++i) { + const target_size = target_sizes !== null ? target_sizes[i] : null; + let data = logits[i]; + if (target_size !== null) { + data = (0, _utils_tensor_js__WEBPACK_IMPORTED_MODULE_4__.interpolate)(data, target_size, "bilinear", false); + } + const [height, width] = target_size ?? data.dims.slice(-2); + const segmentation = new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_4__.Tensor( + "int32", + new Int32Array(height * width), + [height, width] + ); + const buffer = data[0].data; + const segmentation_data = segmentation.data; + for (let j = 1; j < data.dims[0]; ++j) { + const row = data[j].data; + for (let k = 0; k < row.length; ++k) { + if (row[k] > buffer[k]) { + buffer[k] = row[k]; + segmentation_data[k] = j; + } + } + } + const hasLabel = new Array(data.dims[0]); + for (let j = 0; j < segmentation_data.length; ++j) { + const index = segmentation_data[j]; + hasLabel[index] = index; + } + const labels = hasLabel.filter((x) => x !== void 0); + toReturn.push({ segmentation, labels }); + } + return toReturn; + } + function remove_low_and_no_objects(class_logits, mask_logits, object_mask_threshold, num_labels) { + const mask_probs_item = []; + const pred_scores_item = []; + const pred_labels_item = []; + for (let j = 0; j < class_logits.dims[0]; ++j) { + const cls = class_logits[j]; + const mask = mask_logits[j]; + const pred_label = (0, _utils_maths_js__WEBPACK_IMPORTED_MODULE_3__.max)(cls.data)[1]; + if (pred_label === num_labels) { + continue; + } + const scores = (0, _utils_maths_js__WEBPACK_IMPORTED_MODULE_3__.softmax)(cls.data); + const pred_score = scores[pred_label]; + if (pred_score > object_mask_threshold) { + mask_probs_item.push(mask); + pred_scores_item.push(pred_score); + pred_labels_item.push(pred_label); + } + } + return [mask_probs_item, pred_scores_item, pred_labels_item]; + } + function check_segment_validity(mask_labels, mask_probs, k, mask_threshold = 0.5, overlap_mask_area_threshold = 0.8) { + const mask_k = []; + let mask_k_area = 0; + let original_area = 0; + const mask_probs_k_data = mask_probs[k].data; + for (let i = 0; i < mask_labels.length; ++i) { + if (mask_labels[i] === k) { + mask_k.push(i); + ++mask_k_area; + } + if (mask_probs_k_data[i] >= mask_threshold) { + ++original_area; + } + } + let mask_exists = mask_k_area > 0 && original_area > 0; + if (mask_exists) { + let area_ratio = mask_k_area / original_area; + mask_exists = area_ratio > overlap_mask_area_threshold; + } + return [mask_exists, mask_k]; + } + function compute_segments(mask_probs, pred_scores, pred_labels, mask_threshold, overlap_mask_area_threshold, label_ids_to_fuse = null, target_size = null) { + const [height, width] = target_size ?? mask_probs[0].dims; + const segmentation = new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_4__.Tensor( + "int32", + new Int32Array(height * width), + [height, width] + ); + const segments = []; + if (target_size !== null) { + for (let i = 0; i < mask_probs.length; ++i) { + mask_probs[i] = (0, _utils_tensor_js__WEBPACK_IMPORTED_MODULE_4__.interpolate)(mask_probs[i], target_size, "bilinear", false); + } + } + const mask_labels = new Int32Array(mask_probs[0].data.length); + const bestScores = new Float32Array(mask_probs[0].data.length); + for (let i = 0; i < mask_probs.length; ++i) { + let score = pred_scores[i]; + const mask_probs_i_data = mask_probs[i].data; + for (let j = 0; j < mask_probs_i_data.length; ++j) { + mask_probs_i_data[j] *= score; + if (mask_probs_i_data[j] > bestScores[j]) { + mask_labels[j] = i; + bestScores[j] = mask_probs_i_data[j]; + } + } + } + let current_segment_id = 0; + const segmentation_data = segmentation.data; + for (let k = 0; k < pred_labels.length; ++k) { + const pred_class = pred_labels[k]; + const [mask_exists, mask_k] = check_segment_validity( + mask_labels, + mask_probs, + k, + mask_threshold, + overlap_mask_area_threshold + ); + if (!mask_exists) { + continue; + } + ++current_segment_id; + for (const index of mask_k) { + segmentation_data[index] = current_segment_id; + } + segments.push({ + id: current_segment_id, + label_id: pred_class, + // was_fused: should_fuse, TODO + score: pred_scores[k] + }); + } + return [segmentation, segments]; + } + function post_process_panoptic_segmentation(outputs, threshold = 0.5, mask_threshold = 0.5, overlap_mask_area_threshold = 0.8, label_ids_to_fuse = null, target_sizes = null) { + if (label_ids_to_fuse === null) { + console.warn("`label_ids_to_fuse` unset. No instance will be fused."); + label_ids_to_fuse = /* @__PURE__ */ new Set(); + } + const class_queries_logits = outputs.class_queries_logits ?? outputs.logits; + const masks_queries_logits = outputs.masks_queries_logits ?? outputs.pred_masks; + const mask_probs = masks_queries_logits.sigmoid(); + let [batch_size, num_queries, num_labels] = class_queries_logits.dims; + num_labels -= 1; + if (target_sizes !== null && target_sizes.length !== batch_size) { + throw Error("Make sure that you pass in as many target sizes as the batch dimension of the logits"); + } + let toReturn = []; + for (let i = 0; i < batch_size; ++i) { + let target_size = target_sizes !== null ? target_sizes[i] : null; + let class_logits = class_queries_logits[i]; + let mask_logits = mask_probs[i]; + let [mask_probs_item, pred_scores_item, pred_labels_item] = remove_low_and_no_objects(class_logits, mask_logits, threshold, num_labels); + if (pred_labels_item.length === 0) { + let [height, width] = target_size ?? mask_logits.dims.slice(-2); + let segmentation2 = new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_4__.Tensor( + "int32", + new Int32Array(height * width).fill(-1), + [height, width] + ); + toReturn.push({ + segmentation: segmentation2, + segments_info: [] + }); + continue; + } + let [segmentation, segments] = compute_segments( + mask_probs_item, + pred_scores_item, + pred_labels_item, + mask_threshold, + overlap_mask_area_threshold, + label_ids_to_fuse, + target_size + ); + toReturn.push({ + segmentation, + segments_info: segments + }); + } + return toReturn; + } + function post_process_instance_segmentation(outputs, threshold = 0.5, target_sizes = null) { + throw new Error("Not implemented yet"); + return []; + } + function validate_audio_inputs(audio, feature_extractor) { + var _a; + if (!(audio instanceof Float32Array || audio instanceof Float64Array)) { + throw new Error( + `${feature_extractor} expects input to be a Float32Array or a Float64Array, but got ${((_a = audio == null ? void 0 : audio.constructor) == null ? void 0 : _a.name) ?? typeof audio} instead. If using the feature extractor directly, remember to use \`read_audio(url, sampling_rate)\` to obtain the raw audio data of the file/url.` + ); + } + } + function constraint_to_multiple_of(val, multiple, minVal = 0, maxVal = null) { + const a = val / multiple; + let x = (0, _utils_maths_js__WEBPACK_IMPORTED_MODULE_3__.bankers_round)(a) * multiple; + if (maxVal !== null && x > maxVal) { + x = Math.floor(a) * multiple; + } + if (x < minVal) { + x = Math.ceil(a) * multiple; + } + return x; + } + function enforce_size_divisibility([width, height], divisor) { + return [ + Math.max(Math.floor(width / divisor), 1) * divisor, + Math.max(Math.floor(height / divisor), 1) * divisor + ]; + } + class FeatureExtractor extends _utils_generic_js__WEBPACK_IMPORTED_MODULE_0__.Callable { + /** + * Constructs a new FeatureExtractor instance. + * + * @param {Object} config The configuration for the feature extractor. + */ + constructor(config) { + super(); + this.config = config; + } + } + class ImageFeatureExtractor extends FeatureExtractor { + /** + * Constructs a new ImageFeatureExtractor instance. + * + * @param {Object} config The configuration for the feature extractor. + * @param {number[]} config.image_mean The mean values for image normalization. + * @param {number[]} config.image_std The standard deviation values for image normalization. + * @param {boolean} config.do_rescale Whether to rescale the image pixel values to the [0,1] range. + * @param {number} config.rescale_factor The factor to use for rescaling the image pixel values. + * @param {boolean} config.do_normalize Whether to normalize the image pixel values. + * @param {boolean} config.do_resize Whether to resize the image. + * @param {number} config.resample What method to use for resampling. + * @param {number|Object} config.size The size to resize the image to. + * @param {boolean} [config.do_flip_channel_order=false] Whether to flip the color channels from RGB to BGR. + * Can be overridden by the `do_flip_channel_order` parameter in the `preprocess` method. + */ + constructor(config) { + super(config); + this.image_mean = this.config.image_mean ?? this.config.mean; + this.image_std = this.config.image_std ?? this.config.std; + this.resample = this.config.resample ?? 2; + this.do_rescale = this.config.do_rescale ?? true; + this.rescale_factor = this.config.rescale_factor ?? 1 / 255; + this.do_normalize = this.config.do_normalize; + this.do_resize = this.config.do_resize; + this.do_thumbnail = this.config.do_thumbnail; + this.size = this.config.size; + this.size_divisibility = this.config.size_divisibility ?? this.config.size_divisor; + this.do_center_crop = this.config.do_center_crop; + this.crop_size = this.config.crop_size; + this.do_convert_rgb = this.config.do_convert_rgb ?? true; + this.do_crop_margin = this.config.do_crop_margin; + this.pad_size = this.config.pad_size; + this.do_pad = this.config.do_pad; + if (this.do_pad && !this.pad_size && this.size && this.size.width !== void 0 && this.size.height !== void 0) { + this.pad_size = this.size; + } + this.do_flip_channel_order = this.config.do_flip_channel_order ?? false; + } + /** + * Resize the image to make a thumbnail. The image is resized so that no dimension is larger than any + * corresponding dimension of the specified size. + * @param {RawImage} image The image to be resized. + * @param {{height:number, width:number}} size The size `{"height": h, "width": w}` to resize the image to. + * @param {string | 0 | 1 | 2 | 3 | 4 | 5} [resample=2] The resampling filter to use. + * @returns {Promise} The resized image. + */ + async thumbnail(image, size, resample = 2) { + const input_height = image.height; + const input_width = image.width; + const output_height = size.height; + const output_width = size.width; + let height = Math.min(input_height, output_height); + let width = Math.min(input_width, output_width); + if (height === input_height && width === input_width) { + return image; + } + if (input_height > input_width) { + width = Math.floor(input_width * height / input_height); + } else if (input_width > input_height) { + height = Math.floor(input_height * width / input_width); + } + return await image.resize(width, height, { resample }); + } + /** + * Crops the margin of the image. Gray pixels are considered margin (i.e., pixels with a value below the threshold). + * @param {RawImage} image The image to be cropped. + * @param {number} gray_threshold Value below which pixels are considered to be gray. + * @returns {Promise} The cropped image. + */ + async crop_margin(image, gray_threshold = 200) { + const gray_image = image.clone().grayscale(); + const minValue = (0, _utils_maths_js__WEBPACK_IMPORTED_MODULE_3__.min)(gray_image.data)[0]; + const maxValue = (0, _utils_maths_js__WEBPACK_IMPORTED_MODULE_3__.max)(gray_image.data)[0]; + const diff = maxValue - minValue; + if (diff === 0) { + return image; + } + const threshold = gray_threshold / 255; + let x_min = gray_image.width, y_min = gray_image.height, x_max = 0, y_max = 0; + const gray_image_data = gray_image.data; + for (let j = 0; j < gray_image.height; ++j) { + const row = j * gray_image.width; + for (let i = 0; i < gray_image.width; ++i) { + if ((gray_image_data[row + i] - minValue) / diff < threshold) { + x_min = Math.min(x_min, i); + y_min = Math.min(y_min, j); + x_max = Math.max(x_max, i); + y_max = Math.max(y_max, j); + } + } + } + image = await image.crop([x_min, y_min, x_max, y_max]); + return image; + } + /** + * Pad the image by a certain amount. + * @param {Float32Array} pixelData The pixel data to pad. + * @param {number[]} imgDims The dimensions of the image (height, width, channels). + * @param {{width:number; height:number}|number} padSize The dimensions of the padded image. + * @param {Object} options The options for padding. + * @param {'constant'|'symmetric'} [options.mode='constant'] The type of padding to add. + * @param {boolean} [options.center=false] Whether to center the image. + * @param {number} [options.constant_values=0] The constant value to use for padding. + * @returns {[Float32Array, number[]]} The padded pixel data and image dimensions. + */ + pad_image(pixelData, imgDims, padSize, { + mode = "constant", + center = false, + constant_values = 0 + } = {}) { + const [imageHeight, imageWidth, imageChannels] = imgDims; + let paddedImageWidth, paddedImageHeight; + if (typeof padSize === "number") { + paddedImageWidth = padSize; + paddedImageHeight = padSize; + } else { + paddedImageWidth = padSize.width; + paddedImageHeight = padSize.height; + } + if (paddedImageWidth !== imageWidth || paddedImageHeight !== imageHeight) { + const paddedPixelData = new Float32Array(paddedImageWidth * paddedImageHeight * imageChannels); + if (Array.isArray(constant_values)) { + for (let i = 0; i < paddedPixelData.length; ++i) { + paddedPixelData[i] = constant_values[i % imageChannels]; + } + } else if (constant_values !== 0) { + paddedPixelData.fill(constant_values); + } + const [left, top] = center ? [Math.floor((paddedImageWidth - imageWidth) / 2), Math.floor((paddedImageHeight - imageHeight) / 2)] : [0, 0]; + for (let i = 0; i < imageHeight; ++i) { + const a = (i + top) * paddedImageWidth; + const b = i * imageWidth; + for (let j = 0; j < imageWidth; ++j) { + const c = (a + j + left) * imageChannels; + const d = (b + j) * imageChannels; + for (let k = 0; k < imageChannels; ++k) { + paddedPixelData[c + k] = pixelData[d + k]; + } + } + } + if (mode === "symmetric") { + if (center) { + throw new Error("`center` padding is not supported when `mode` is set to `symmetric`."); + } + const h1 = imageHeight - 1; + const w1 = imageWidth - 1; + for (let i = 0; i < paddedImageHeight; ++i) { + const a = i * paddedImageWidth; + const b = (0, _utils_core_js__WEBPACK_IMPORTED_MODULE_1__.calculateReflectOffset)(i, h1) * imageWidth; + for (let j = 0; j < paddedImageWidth; ++j) { + if (i < imageHeight && j < imageWidth) continue; + const c = (a + j) * imageChannels; + const d = (b + (0, _utils_core_js__WEBPACK_IMPORTED_MODULE_1__.calculateReflectOffset)(j, w1)) * imageChannels; + for (let k = 0; k < imageChannels; ++k) { + paddedPixelData[c + k] = pixelData[d + k]; + } + } + } + } + pixelData = paddedPixelData; + imgDims = [paddedImageHeight, paddedImageWidth, imageChannels]; + } + return [pixelData, imgDims]; + } + /** + * Rescale the image' pixel values by `this.rescale_factor`. + * @param {Float32Array} pixelData The pixel data to rescale. + * @returns {void} + */ + rescale(pixelData) { + for (let i = 0; i < pixelData.length; ++i) { + pixelData[i] = this.rescale_factor * pixelData[i]; + } + } + /** + * Find the target (width, height) dimension of the output image after + * resizing given the input image and the desired size. + * @param {RawImage} image The image to resize. + * @param {any} size The size to use for resizing the image. + * @returns {[number, number]} The target (width, height) dimension of the output image after resizing. + */ + get_resize_output_image_size(image, size) { + const [srcWidth, srcHeight] = image.size; + let shortest_edge; + let longest_edge; + if (this.do_thumbnail) { + const { height, width } = size; + shortest_edge = Math.min(height, width); + } else if (Number.isInteger(size)) { + shortest_edge = size; + longest_edge = this.config.max_size ?? shortest_edge; + } else if (size !== void 0) { + shortest_edge = size.shortest_edge; + longest_edge = size.longest_edge; + } + if (shortest_edge !== void 0 || longest_edge !== void 0) { + const shortResizeFactor = shortest_edge === void 0 ? 1 : Math.max(shortest_edge / srcWidth, shortest_edge / srcHeight); + const newWidth = srcWidth * shortResizeFactor; + const newHeight = srcHeight * shortResizeFactor; + const longResizeFactor = longest_edge === void 0 ? 1 : Math.min(longest_edge / newWidth, longest_edge / newHeight); + let finalWidth = Math.floor(Number((newWidth * longResizeFactor).toFixed(2))); + let finalHeight = Math.floor(Number((newHeight * longResizeFactor).toFixed(2))); + if (this.size_divisibility !== void 0) { + [finalWidth, finalHeight] = enforce_size_divisibility([finalWidth, finalHeight], this.size_divisibility); + } + return [finalWidth, finalHeight]; + } else if (size !== void 0 && size.width !== void 0 && size.height !== void 0) { + let newWidth = size.width; + let newHeight = size.height; + if (this.config.keep_aspect_ratio && this.config.ensure_multiple_of) { + let scale_height = newHeight / srcHeight; + let scale_width = newWidth / srcWidth; + if (Math.abs(1 - scale_width) < Math.abs(1 - scale_height)) { + scale_height = scale_width; + } else { + scale_width = scale_height; + } + newHeight = constraint_to_multiple_of(scale_height * srcHeight, this.config.ensure_multiple_of); + newWidth = constraint_to_multiple_of(scale_width * srcWidth, this.config.ensure_multiple_of); + } + return [newWidth, newHeight]; + } else if (this.size_divisibility !== void 0) { + return enforce_size_divisibility([srcWidth, srcHeight], this.size_divisibility); + } else { + throw new Error(`Could not resize image due to unsupported \`this.size\` option in config: ${JSON.stringify(size)}`); + } + } + /** + * Resizes the image. + * @param {RawImage} image The image to resize. + * @returns {Promise} The resized image. + */ + async resize(image) { + const [newWidth, newHeight] = this.get_resize_output_image_size(image, this.size); + return await image.resize(newWidth, newHeight, { + resample: this.resample + }); + } + /** + * @typedef {object} PreprocessedImage + * @property {HeightWidth} original_size The original size of the image. + * @property {HeightWidth} reshaped_input_size The reshaped input size of the image. + * @property {Tensor} pixel_values The pixel values of the preprocessed image. + */ + /** + * Preprocesses the given image. + * + * @param {RawImage} image The image to preprocess. + * @param {Object} overrides The overrides for the preprocessing options. + * @returns {Promise} The preprocessed image. + */ + async preprocess(image, { + do_normalize = null, + do_pad = null, + do_convert_rgb = null, + do_convert_grayscale = null, + do_flip_channel_order = null + } = {}) { + if (this.do_crop_margin) { + image = await this.crop_margin(image); + } + const [srcWidth, srcHeight] = image.size; + if (do_convert_rgb ?? this.do_convert_rgb) { + image = image.rgb(); + } else if (do_convert_grayscale) { + image = image.grayscale(); + } + if (this.do_resize) { + image = await this.resize(image); + } + if (this.do_thumbnail) { + image = await this.thumbnail(image, this.size, this.resample); + } + if (this.do_center_crop) { + let crop_width; + let crop_height; + if (Number.isInteger(this.crop_size)) { + crop_width = this.crop_size; + crop_height = this.crop_size; + } else { + crop_width = this.crop_size.width; + crop_height = this.crop_size.height; + } + image = await image.center_crop(crop_width, crop_height); + } + const reshaped_input_size = [image.height, image.width]; + let pixelData = Float32Array.from(image.data); + let imgDims = [image.height, image.width, image.channels]; + if (this.do_rescale) { + this.rescale(pixelData); + } + if (do_normalize ?? this.do_normalize) { + let image_mean = this.image_mean; + if (!Array.isArray(this.image_mean)) { + image_mean = new Array(image.channels).fill(image_mean); + } + let image_std = this.image_std; + if (!Array.isArray(this.image_std)) { + image_std = new Array(image.channels).fill(image_mean); + } + if (image_mean.length !== image.channels || image_std.length !== image.channels) { + throw new Error(`When set to arrays, the length of \`image_mean\` (${image_mean.length}) and \`image_std\` (${image_std.length}) must match the number of channels in the image (${image.channels}).`); + } + for (let i = 0; i < pixelData.length; i += image.channels) { + for (let j = 0; j < image.channels; ++j) { + pixelData[i + j] = (pixelData[i + j] - image_mean[j]) / image_std[j]; + } + } + } + if (do_pad ?? this.do_pad) { + if (this.pad_size) { + const padded = this.pad_image(pixelData, [image.height, image.width, image.channels], this.pad_size); + [pixelData, imgDims] = padded; + } else if (this.size_divisibility) { + const [paddedWidth, paddedHeight] = enforce_size_divisibility([imgDims[1], imgDims[0]], this.size_divisibility); + [pixelData, imgDims] = this.pad_image(pixelData, imgDims, { width: paddedWidth, height: paddedHeight }); + } + } + if (do_flip_channel_order ?? this.do_flip_channel_order) { + if (imgDims[2] !== 3) { + throw new Error("Flipping channel order is only supported for RGB images."); + } + for (let i = 0; i < pixelData.length; i += 3) { + const temp = pixelData[i]; + pixelData[i] = pixelData[i + 2]; + pixelData[i + 2] = temp; + } + } + const pixel_values = new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_4__.Tensor("float32", pixelData, imgDims).permute(2, 0, 1); + return { + original_size: [srcHeight, srcWidth], + reshaped_input_size, + pixel_values + }; + } + /** + * Calls the feature extraction process on an array of images, + * preprocesses each image, and concatenates the resulting + * features into a single Tensor. + * @param {RawImage[]} images The image(s) to extract features from. + * @param {...any} args Additional arguments. + * @returns {Promise} An object containing the concatenated pixel values (and other metadata) of the preprocessed images. + */ + async _call(images, ...args) { + if (!Array.isArray(images)) { + images = [images]; + } + const imageData = await Promise.all(images.map((x) => this.preprocess(x))); + const pixel_values = (0, _utils_tensor_js__WEBPACK_IMPORTED_MODULE_4__.stack)(imageData.map((x) => x.pixel_values), 0); + return { + pixel_values, + // Original sizes of images + original_sizes: imageData.map((x) => x.original_size), + // Reshaped sizes of images, before padding or cropping + reshaped_input_sizes: imageData.map((x) => x.reshaped_input_size) + }; + } + } + class SapiensFeatureExtractor extends ImageFeatureExtractor { + /** @type {typeof post_process_semantic_segmentation} */ + post_process_semantic_segmentation(...args) { + return post_process_semantic_segmentation(...args); + } + } + class SegformerFeatureExtractor extends ImageFeatureExtractor { + /** @type {typeof post_process_semantic_segmentation} */ + post_process_semantic_segmentation(...args) { + return post_process_semantic_segmentation(...args); + } + } + class PvtImageProcessor extends ImageFeatureExtractor { + } + class DPTFeatureExtractor extends ImageFeatureExtractor { + } + class DPTImageProcessor extends DPTFeatureExtractor { + } + class BitImageProcessor extends ImageFeatureExtractor { + } + class GLPNFeatureExtractor extends ImageFeatureExtractor { + } + class CLIPFeatureExtractor extends ImageFeatureExtractor { + } + class CLIPImageProcessor extends CLIPFeatureExtractor { + } + class ChineseCLIPFeatureExtractor extends ImageFeatureExtractor { + } + class SiglipImageProcessor extends ImageFeatureExtractor { + } + class ConvNextFeatureExtractor extends ImageFeatureExtractor { + constructor(config) { + super(config); + this.crop_pct = this.config.crop_pct ?? 224 / 256; + } + async resize(image) { + var _a; + const shortest_edge = (_a = this.size) == null ? void 0 : _a.shortest_edge; + if (shortest_edge === void 0) { + throw new Error(`Size dictionary must contain 'shortest_edge' key.`); + } + if (shortest_edge < 384) { + const resize_shortest_edge = Math.floor(shortest_edge / this.crop_pct); + const [newWidth, newHeight] = this.get_resize_output_image_size(image, { + shortest_edge: resize_shortest_edge + }); + image = await image.resize(newWidth, newHeight, { + resample: this.resample + }); + image = await image.center_crop(shortest_edge, shortest_edge); + } else { + image = await image.resize(shortest_edge, shortest_edge, { + resample: this.resample + }); + } + return image; + } + } + class ConvNextImageProcessor extends ConvNextFeatureExtractor { + } + class ViTFeatureExtractor extends ImageFeatureExtractor { + } + class ViTImageProcessor extends ImageFeatureExtractor { + } + class EfficientNetImageProcessor extends ImageFeatureExtractor { + constructor(config) { + super(config); + this.include_top = this.config.include_top ?? true; + if (this.include_top) { + this.image_std = this.image_std.map((x) => x * x); + } + } + } + class MobileNetV1FeatureExtractor extends ImageFeatureExtractor { + } + class MobileNetV2FeatureExtractor extends ImageFeatureExtractor { + } + class MobileNetV3FeatureExtractor extends ImageFeatureExtractor { + } + class MobileNetV4FeatureExtractor extends ImageFeatureExtractor { + } + class MobileViTFeatureExtractor extends ImageFeatureExtractor { + } + class MobileViTImageProcessor extends MobileViTFeatureExtractor { + } + class OwlViTFeatureExtractor extends ImageFeatureExtractor { + /** @type {typeof post_process_object_detection} */ + post_process_object_detection(...args) { + return post_process_object_detection(...args); + } + } + class Owlv2ImageProcessor extends OwlViTFeatureExtractor { + } + class RTDetrImageProcessor extends ImageFeatureExtractor { + /** @type {typeof post_process_object_detection} */ + post_process_object_detection(...args) { + return post_process_object_detection(...args); + } + } + class DeiTFeatureExtractor extends ImageFeatureExtractor { + } + class BeitFeatureExtractor extends ImageFeatureExtractor { + } + class DonutFeatureExtractor extends ImageFeatureExtractor { + pad_image(pixelData, imgDims, padSize, options = {}) { + const [imageHeight, imageWidth, imageChannels] = imgDims; + let image_mean = this.image_mean; + if (!Array.isArray(this.image_mean)) { + image_mean = new Array(imageChannels).fill(image_mean); + } + let image_std = this.image_std; + if (!Array.isArray(image_std)) { + image_std = new Array(imageChannels).fill(image_mean); + } + const constant_values = image_mean.map((x, i) => -x / image_std[i]); + return super.pad_image(pixelData, imgDims, padSize, { + center: true, + // Since normalization is done after padding, we need to use certain constant values to ensure the same behaviour is observed. + // For more information, see https://github.com/huggingface/transformers/blob/main/src/transformers/models/donut/image_processing_donut.py#L433-L451 + constant_values, + ...options + }); + } + } + class NougatImageProcessor extends DonutFeatureExtractor { + } + class DetrFeatureExtractor extends ImageFeatureExtractor { + /** + * Calls the feature extraction process on an array of images, preprocesses + * each image, and concatenates the resulting features into a single Tensor. + * @param {RawImage[]} images The image(s) to extract features from. + * @returns {Promise} An object containing the concatenated pixel values of the preprocessed images. + */ + async _call(images) { + const result = await super._call(images); + const maskSize = [result.pixel_values.dims[0], 64, 64]; + const pixel_mask = (0, _utils_tensor_js__WEBPACK_IMPORTED_MODULE_4__.full)(maskSize, 1n); + return { ...result, pixel_mask }; + } + /** @type {typeof post_process_object_detection} */ + post_process_object_detection(...args) { + return post_process_object_detection(...args); + } + /** @type {typeof post_process_panoptic_segmentation} */ + post_process_panoptic_segmentation(...args) { + return post_process_panoptic_segmentation(...args); + } + post_process_instance_segmentation() { + throw Error("Not implemented yet"); + } + } + class MaskFormerFeatureExtractor extends ImageFeatureExtractor { + /** @type {typeof post_process_panoptic_segmentation} */ + post_process_panoptic_segmentation(...args) { + return post_process_panoptic_segmentation(...args); + } + post_process_instance_segmentation() { + throw Error("Not implemented yet"); + } + } + class YolosFeatureExtractor extends ImageFeatureExtractor { + /** @type {typeof post_process_object_detection} */ + post_process_object_detection(...args) { + return post_process_object_detection(...args); + } + } + class SamImageProcessor extends ImageFeatureExtractor { + /** + * + * @param {any} input_points + * @param {HeightWidth[]} original_sizes + * @param {HeightWidth[]} reshaped_input_sizes + * @returns {Tensor} + */ + reshape_input_points(input_points, original_sizes, reshaped_input_sizes, is_bounding_box = false) { + input_points = structuredClone(input_points); + let shape = (0, _utils_core_js__WEBPACK_IMPORTED_MODULE_1__.calculateDimensions)(input_points); + if (shape.length === 3) { + if (!is_bounding_box) { + shape = [1, ...shape]; + } + input_points = [input_points]; + } else if (shape.length !== 4) { + throw Error("The input_points must be a 4D tensor of shape `batch_size`, `point_batch_size`, `nb_points_per_image`, `2`."); + } + for (let i = 0; i < input_points.length; ++i) { + let originalImageSize = original_sizes[i]; + let reshapedImageSize = reshaped_input_sizes[i]; + let resizeFactors = [ + reshapedImageSize[0] / originalImageSize[0], + reshapedImageSize[1] / originalImageSize[1] + ]; + for (let j = 0; j < input_points[i].length; ++j) { + for (let k = 0; k < input_points[i][j].length; ++k) { + for (let w = 0; w < input_points[i][j][k].length; ++w) { + input_points[i][j][k][w] *= resizeFactors[w % 2]; + } + } + } + } + return new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_4__.Tensor( + "float32", + Float32Array.from(input_points.flat(Infinity)), + shape + ); + } + /** + * + * @param {any} input_labels + * @param {Tensor} input_points + * @returns {Tensor} + */ + add_input_labels(input_labels, input_points) { + let shape = (0, _utils_core_js__WEBPACK_IMPORTED_MODULE_1__.calculateDimensions)(input_labels); + if (shape.length === 2) { + shape = [1, ...shape]; + input_labels = [input_labels]; + } else if (shape.length !== 3) { + throw Error("The input_points must be a 4D tensor of shape `batch_size`, `point_batch_size`, `nb_points_per_image`, `2`."); + } + if (shape.some((x, i) => x !== input_points.dims[i])) { + throw Error(`The first ${shape.length} dimensions of 'input_points' and 'input_labels' must be the same.`); + } + return new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_4__.Tensor( + "int64", + input_labels.flat(Infinity).map(BigInt), + shape + ); + } + /** + * @param {any[]} images The URL(s) of the image(s) to extract features from. + * @param {Object} [options] Additional options for the processor. + * @param {any} [options.input_points=null] A 3D or 4D array, representing the input points provided by the user. + * - 3D: `[point_batch_size, nb_points_per_image, 2]`. In this case, `batch_size` is assumed to be 1. + * - 4D: `[batch_size, point_batch_size, nb_points_per_image, 2]`. + * @param {any} [options.input_labels=null] A 2D or 3D array, representing the input labels for the points, used by the prompt encoder to encode the prompt. + * - 2D: `[point_batch_size, nb_points_per_image]`. In this case, `batch_size` is assumed to be 1. + * - 3D: `[batch_size, point_batch_size, nb_points_per_image]`. + * @param {number[][][]} [options.input_boxes=null] A 3D array of shape `(batch_size, num_boxes, 4)`, representing the input boxes provided by the user. + * This is used by the prompt encoder to encode the prompt. Generally yields to much better generated masks. + * The processor will generate a tensor, with each dimension corresponding respectively to the image batch size, + * the number of boxes per image and the coordinates of the top left and botton right point of the box. + * In the order (`x1`, `y1`, `x2`, `y2`): + * - `x1`: the x coordinate of the top left point of the input box + * - `y1`: the y coordinate of the top left point of the input box + * - `x2`: the x coordinate of the bottom right point of the input box + * - `y2`: the y coordinate of the bottom right point of the input box + * @returns {Promise} + */ + async _call(images, { + input_points = null, + input_labels = null, + input_boxes = null + } = {}) { + const processed = await super._call(images); + if (input_points) { + processed.input_points = this.reshape_input_points( + input_points, + processed.original_sizes, + processed.reshaped_input_sizes + ); + } + if (input_labels) { + if (!processed.input_points) { + throw Error("`input_points` must be provided if `input_labels` are provided."); + } + processed.input_labels = this.add_input_labels(input_labels, processed.input_points); + } + if (input_boxes) { + processed.input_boxes = this.reshape_input_points( + input_boxes, + processed.original_sizes, + processed.reshaped_input_sizes, + true + ); + } + return processed; + } + /** + * Remove padding and upscale masks to the original image size. + * @param {Tensor} masks Batched masks from the mask_decoder in (batch_size, num_channels, height, width) format. + * @param {[number, number][]} original_sizes The original sizes of each image before it was resized to the model's expected input shape, in (height, width) format. + * @param {[number, number][]} reshaped_input_sizes The size of each image as it is fed to the model, in (height, width) format. Used to remove padding. + * @param {Object} options Optional parameters for post-processing. + * @param {number} [options.mask_threshold] The threshold to use for binarizing the masks. + * @param {boolean} [options.binarize] Whether to binarize the masks. + * @param {Object} [options.pad_size] The target size the images were padded to before being passed to the model. If `null`, the target size is assumed to be the processor's `pad_size`. + * @param {number} [options.pad_size.height] The height the images were padded to. + * @param {number} [options.pad_size.width] The width the images were padded to. + * @returns {Promise} Batched masks in batch_size, num_channels, height, width) format, where (height, width) is given by original_size. + */ + async post_process_masks(masks, original_sizes, reshaped_input_sizes, { + mask_threshold = 0, + binarize = true, + pad_size = null + } = {}) { + const output_masks = []; + pad_size = pad_size ?? this.pad_size; + const target_image_size = [pad_size.height, pad_size.width]; + for (let i = 0; i < original_sizes.length; ++i) { + const original_size = original_sizes[i]; + const reshaped_input_size = reshaped_input_sizes[i]; + let interpolated_mask = await (0, _utils_tensor_js__WEBPACK_IMPORTED_MODULE_4__.interpolate_4d)( + masks[i], + { mode: "bilinear", size: target_image_size } + ); + interpolated_mask = interpolated_mask.slice(null, null, [0, reshaped_input_size[0]], [0, reshaped_input_size[1]]); + interpolated_mask = await (0, _utils_tensor_js__WEBPACK_IMPORTED_MODULE_4__.interpolate_4d)( + interpolated_mask, + { mode: "bilinear", size: original_size } + ); + if (binarize) { + const data = interpolated_mask.data; + const binarizedMaskData = new Uint8Array(data.length); + for (let i2 = 0; i2 < data.length; ++i2) { + if (data[i2] > mask_threshold) { + binarizedMaskData[i2] = 1; + } + } + interpolated_mask = new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_4__.Tensor( + "bool", + binarizedMaskData, + interpolated_mask.dims + ); + } + output_masks.push(interpolated_mask); + } + return output_masks; + } + /** + * Generates a list of crop boxes of different sizes. Each layer has (2**i)**2 boxes for the ith layer. + * @param {RawImage} image Input original image + * @param {number} target_size Target size of the resized image + * @param {Object} options Options for generating crop boxes + * @param {number} [options.crop_n_layers] If >0, mask prediction will be run again on crops of the image. + * Sets the number of layers to run, where each layer has 2**i_layer number of image crops. + * @param {number} [options.overlap_ratio] Sets the degree to which crops overlap. In the first crop layer, + * crops will overlap by this fraction of the image length. Later layers with more crops scale down this overlap. + * @param {number} [options.points_per_crop] Number of points to sample from each crop. + * @param {number} [options.crop_n_points_downscale_factor] The number of points-per-side sampled in layer n is + * scaled down by crop_n_points_downscale_factor**n. + * @returns {Object} An object containing the crop boxes, number of points per crop, cropped images, and input labels. + */ + generate_crop_boxes(image, target_size, { + crop_n_layers = 0, + overlap_ratio = 512 / 1500, + points_per_crop = 32, + crop_n_points_downscale_factor = 1 + } = {}) { + } + } + class Swin2SRImageProcessor extends ImageFeatureExtractor { + pad_image(pixelData, imgDims, padSize, options = {}) { + const [imageHeight, imageWidth, imageChannels] = imgDims; + return super.pad_image(pixelData, imgDims, { + // NOTE: For Swin2SR models, the original python implementation adds padding even when the image's width/height is already + // a multiple of `pad_size`. However, this is most likely a bug (PR: https://github.com/mv-lab/swin2sr/pull/19). + // For this reason, we only add padding when the image's width/height is not a multiple of `pad_size`. + width: imageWidth + (padSize - imageWidth % padSize) % padSize, + height: imageHeight + (padSize - imageHeight % padSize) % padSize + }, { + mode: "symmetric", + center: false, + constant_values: -1, + ...options + }); + } + } + class VitMatteImageProcessor extends ImageFeatureExtractor { + /** + * Calls the feature extraction process on an array of images, preprocesses + * each image, and concatenates the resulting features into a single Tensor. + * @param {RawImage[]} images The image(s) to extract features from. + * @param {RawImage[]} trimaps The trimaps(s) to extract features from. + * @returns {Promise} An object containing the concatenated pixel values of the preprocessed images. + */ + async _call(images, trimaps) { + if (!Array.isArray(images)) { + images = [images]; + } + if (!Array.isArray(trimaps)) { + trimaps = [trimaps]; + } + const imageData = await Promise.all(images.map((x) => this.preprocess(x))); + const trimapData = await Promise.all(trimaps.map((x) => this.preprocess(x, { + do_normalize: false, + do_convert_rgb: false, + do_convert_grayscale: true + }))); + const pixel_values = (0, _utils_tensor_js__WEBPACK_IMPORTED_MODULE_4__.stack)(imageData.map( + // Concatenate images and trimaps + (x, i) => (0, _utils_tensor_js__WEBPACK_IMPORTED_MODULE_4__.cat)([x.pixel_values, trimapData[i].pixel_values], 0) + ), 0); + return { + pixel_values, + // Original sizes of images + original_sizes: imageData.map((x) => x.original_size), + // Reshaped sizes of images, before padding or cropping + reshaped_input_sizes: imageData.map((x) => x.reshaped_input_size) + }; + } + } + class WhisperFeatureExtractor extends FeatureExtractor { + constructor(config) { + var _a; + super(config); + (_a = this.config).mel_filters ?? (_a.mel_filters = (0, _utils_audio_js__WEBPACK_IMPORTED_MODULE_6__2.mel_filter_bank)( + Math.floor(1 + this.config.n_fft / 2), + // num_frequency_bins + this.config.feature_size, + // num_mel_filters + 0, + // min_frequency + 8e3, + // max_frequency + this.config.sampling_rate, + // sampling_rate + "slaney", + // norm + "slaney" + // mel_scale + )); + this.window = (0, _utils_audio_js__WEBPACK_IMPORTED_MODULE_6__2.window_function)(this.config.n_fft, "hann"); + } + /** + * Computes the log-Mel spectrogram of the provided audio waveform. + * @param {Float32Array|Float64Array} waveform The audio waveform to process. + * @returns {Promise} An object containing the log-Mel spectrogram data as a Float32Array and its dimensions as an array of numbers. + */ + async _extract_fbank_features(waveform) { + const features = await (0, _utils_audio_js__WEBPACK_IMPORTED_MODULE_6__2.spectrogram)( + waveform, + this.window, + // window + this.config.n_fft, + // frame_length + this.config.hop_length, + // hop_length + { + power: 2, + mel_filters: this.config.mel_filters, + log_mel: "log10", + // Custom + max_num_frames: this.config.nb_max_frames + // 3000 + } + ); + const data = features.data; + const maxValue = (0, _utils_maths_js__WEBPACK_IMPORTED_MODULE_3__.max)(data)[0]; + for (let i = 0; i < data.length; ++i) { + data[i] = (Math.max(data[i], maxValue - 8) + 4) / 4; + } + return features; + } + /** + * Asynchronously extracts features from a given audio using the provided configuration. + * @param {Float32Array|Float64Array} audio The audio data as a Float32Array/Float64Array. + * @returns {Promise<{ input_features: Tensor }>} A Promise resolving to an object containing the extracted input features as a Tensor. + */ + async _call(audio) { + validate_audio_inputs(audio, "WhisperFeatureExtractor"); + let waveform; + if (audio.length > this.config.n_samples) { + console.warn( + "Attempting to extract features for audio longer than 30 seconds. If using a pipeline to extract transcript from a long audio clip, remember to specify `chunk_length_s` and/or `stride_length_s`." + ); + waveform = audio.slice(0, this.config.n_samples); + } else { + waveform = new Float32Array(this.config.n_samples); + waveform.set(audio); + } + const features = await this._extract_fbank_features(waveform); + return { + input_features: features.unsqueeze_(0) + }; + } + } + class Wav2Vec2FeatureExtractor extends FeatureExtractor { + /** + * @param {Float32Array} input_values + * @returns {Float32Array} + */ + _zero_mean_unit_var_norm(input_values) { + const sum = input_values.reduce((a, b) => a + b, 0); + const mean = sum / input_values.length; + const variance = input_values.reduce((a, b) => a + (b - mean) ** 2, 0) / input_values.length; + return input_values.map((x) => (x - mean) / Math.sqrt(variance + 1e-7)); + } + /** + * Asynchronously extracts features from a given audio using the provided configuration. + * @param {Float32Array|Float64Array} audio The audio data as a Float32Array/Float64Array. + * @returns {Promise<{ input_values: Tensor; attention_mask: Tensor }>} A Promise resolving to an object containing the extracted input features and attention mask as Tensors. + */ + async _call(audio) { + validate_audio_inputs(audio, "Wav2Vec2FeatureExtractor"); + if (audio instanceof Float64Array) { + audio = new Float32Array(audio); + } + let input_values = audio; + if (this.config.do_normalize) { + input_values = this._zero_mean_unit_var_norm(input_values); + } + const shape = [1, input_values.length]; + return { + input_values: new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_4__.Tensor("float32", input_values, shape), + attention_mask: new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_4__.Tensor("int64", new BigInt64Array(input_values.length).fill(1n), shape) + }; + } + } + class SeamlessM4TFeatureExtractor extends FeatureExtractor { + constructor(config) { + super(config); + const sampling_rate = this.config.sampling_rate; + const mel_filters = (0, _utils_audio_js__WEBPACK_IMPORTED_MODULE_6__2.mel_filter_bank)( + 256, + // num_frequency_bins + this.config.num_mel_bins, + // num_mel_filters + 20, + // min_frequency + Math.floor(sampling_rate / 2), + // max_frequency + sampling_rate, + // sampling_rate + null, + // norm + "kaldi", + // mel_scale + true + // triangularize_in_mel_space + ); + for (let i = 0; i < mel_filters.length; ++i) { + mel_filters[i].push(0); + } + this.mel_filters = mel_filters; + this.window = (0, _utils_audio_js__WEBPACK_IMPORTED_MODULE_6__2.window_function)(400, "povey", { + periodic: false + }); + } + /** + * Computes the log-Mel spectrogram of the provided audio waveform. + * @param {Float32Array|Float64Array} waveform The audio waveform to process. + * @param {number} max_length The maximum number of frames to return. + * @returns {Promise} An object containing the log-Mel spectrogram data as a Float32Array and its dimensions as an array of numbers. + */ + async _extract_fbank_features(waveform, max_length) { + waveform = waveform.map((x) => x * 32768); + return (0, _utils_audio_js__WEBPACK_IMPORTED_MODULE_6__2.spectrogram)( + waveform, + this.window, + // window + 400, + // frame_length + 160, + // hop_length + { + fft_length: 512, + power: 2, + center: false, + preemphasis: 0.97, + mel_filters: this.mel_filters, + log_mel: "log", + mel_floor: 1192092955078125e-22, + remove_dc_offset: true, + // Custom + max_num_frames: max_length, + transpose: true + } + ); + } + /** + * Asynchronously extracts features from a given audio using the provided configuration. + * @param {Float32Array|Float64Array} audio The audio data as a Float32Array/Float64Array. + * @param {Object} options Optional parameters for feature extraction. + * @param {boolean} [options.padding=true] Whether to pad the sequence to a multiple of `pad_to_multiple_of`. + * @param {number} [options.pad_to_multiple_of=2] The number to pad the sequence to a multiple of. + * @param {boolean} [options.do_normalize_per_mel_bins=true] Whether or not to zero-mean unit-variance normalize the input per mel-channel. + * @param {boolean} [options.return_attention_mask=true] Whether to return the attention mask. + * @returns {Promise<{ input_features: Tensor, attention_mask?: Tensor }>} A Promise resolving to an object containing the extracted input features and attention masks as Tensors. + */ + async _call(audio, { + padding = true, + pad_to_multiple_of = 2, + do_normalize_per_mel_bins = true, + return_attention_mask = true + } = {}) { + validate_audio_inputs(audio, "SeamlessM4TFeatureExtractor"); + let features = await this._extract_fbank_features(audio, this.config.max_length); + if (do_normalize_per_mel_bins) { + const [num_features, feature_size] = features.dims; + const data = features.data; + for (let i = 0; i < feature_size; ++i) { + let sum = 0; + for (let j = 0; j < num_features; ++j) { + sum += data[j * feature_size + i]; + } + const mean = sum / num_features; + let variance = 0; + for (let j = 0; j < num_features; ++j) { + variance += (data[j * feature_size + i] - mean) ** 2; + } + variance /= num_features - 1; + const std = Math.sqrt(variance + 1e-7); + for (let j = 0; j < num_features; ++j) { + const index = j * feature_size + i; + data[index] = (data[index] - mean) / std; + } + } + } + let padded_attention_mask; + if (padding) { + const [num_frames2, num_channels2] = features.dims; + const data = ( + /** @type {Float32Array} */ + features.data + ); + const pad_size = num_frames2 % pad_to_multiple_of; + if (pad_size > 0) { + const padded_data = new Float32Array(num_channels2 * (num_frames2 + pad_size)); + padded_data.set(data); + padded_data.fill(this.config.padding_value, data.length); + const numPaddedFrames = num_frames2 + pad_size; + features = new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_4__.Tensor( + features.type, + padded_data, + [numPaddedFrames, num_channels2] + ); + if (return_attention_mask) { + padded_attention_mask = new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_4__.Tensor( + "int64", + new BigInt64Array(numPaddedFrames), + [1, numPaddedFrames] + ); + padded_attention_mask.data.fill(1n, 0, num_frames2); + } + } + } + const [num_frames, num_channels] = features.dims; + const stride = this.config.stride; + const remainder = num_frames % stride; + if (remainder !== 0) { + throw new Error(`The number of frames (${num_frames}) must be a multiple of the stride (${stride}).`); + } + const input_features = features.view( + 1, + Math.floor(num_frames / stride), + num_channels * stride + ); + const result = { input_features }; + if (return_attention_mask) { + const reshapedNumFrames = input_features.dims[1]; + const attention_mask_data = new BigInt64Array(reshapedNumFrames); + if (padded_attention_mask) { + const padded_attention_mask_data = padded_attention_mask.data; + for (let i = 1, j = 0; i < num_frames; i += stride, ++j) { + attention_mask_data[j] = padded_attention_mask_data[i]; + } + } else { + attention_mask_data.fill(1n); + } + result.attention_mask = new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_4__.Tensor( + "int64", + attention_mask_data, + [1, reshapedNumFrames] + ); + } + return result; + } + } + class ASTFeatureExtractor extends FeatureExtractor { + constructor(config) { + super(config); + const sampling_rate = this.config.sampling_rate; + const mel_filters = (0, _utils_audio_js__WEBPACK_IMPORTED_MODULE_6__2.mel_filter_bank)( + 256, + // num_frequency_bins + this.config.num_mel_bins, + // num_mel_filters + 20, + // min_frequency + Math.floor(sampling_rate / 2), + // max_frequency + sampling_rate, + // sampling_rate + null, + // norm + "kaldi", + // mel_scale + true + // triangularize_in_mel_space + ); + for (let i = 0; i < mel_filters.length; ++i) { + mel_filters[i].push(0); + } + this.mel_filters = mel_filters; + this.window = (0, _utils_audio_js__WEBPACK_IMPORTED_MODULE_6__2.window_function)(400, "hann", { + periodic: false + }); + this.mean = this.config.mean; + this.std = this.config.std; + } + /** + * Computes the log-Mel spectrogram of the provided audio waveform. + * @param {Float32Array|Float64Array} waveform The audio waveform to process. + * @param {number} max_length The maximum number of frames to return. + * @returns {Promise} An object containing the log-Mel spectrogram data as a Float32Array and its dimensions as an array of numbers. + */ + async _extract_fbank_features(waveform, max_length) { + return (0, _utils_audio_js__WEBPACK_IMPORTED_MODULE_6__2.spectrogram)( + waveform, + this.window, + // window + 400, + // frame_length + 160, + // hop_length + { + fft_length: 512, + power: 2, + center: false, + preemphasis: 0.97, + mel_filters: this.mel_filters, + log_mel: "log", + mel_floor: 1192092955078125e-22, + remove_dc_offset: true, + // Custom + max_num_frames: max_length, + transpose: true + } + ); + } + /** + * Asynchronously extracts features from a given audio using the provided configuration. + * @param {Float32Array|Float64Array} audio The audio data as a Float32Array/Float64Array. + * @returns {Promise<{ input_values: Tensor }>} A Promise resolving to an object containing the extracted input features as a Tensor. + */ + async _call(audio) { + validate_audio_inputs(audio, "ASTFeatureExtractor"); + const features = await this._extract_fbank_features(audio, this.config.max_length); + if (this.config.do_normalize) { + const denom = this.std * 2; + const features_data = features.data; + for (let i = 0; i < features_data.length; ++i) { + features_data[i] = (features_data[i] - this.mean) / denom; + } + } + return { + input_values: features.unsqueeze_(0) + }; + } + } + class ClapFeatureExtractor extends FeatureExtractor { + constructor(config) { + super(config); + this.mel_filters = (0, _utils_audio_js__WEBPACK_IMPORTED_MODULE_6__2.mel_filter_bank)( + this.config.nb_frequency_bins, + // num_frequency_bins + this.config.feature_size, + // num_mel_filters + this.config.frequency_min, + // min_frequency + this.config.frequency_max, + // max_frequency + this.config.sampling_rate, + // sampling_rate + null, + // norm + "htk" + // mel_scale + ); + this.mel_filters_slaney = (0, _utils_audio_js__WEBPACK_IMPORTED_MODULE_6__2.mel_filter_bank)( + this.config.nb_frequency_bins, + // num_frequency_bins + this.config.feature_size, + // num_mel_filters + this.config.frequency_min, + // min_frequency + this.config.frequency_max, + // max_frequency + this.config.sampling_rate, + // sampling_rate + "slaney", + // norm + "slaney" + // mel_scale + ); + this.window = (0, _utils_audio_js__WEBPACK_IMPORTED_MODULE_6__2.window_function)(this.config.fft_window_size, "hann"); + } + /** + * Extracts the mel spectrogram and prepares it for the mode based on the `truncation` and `padding` arguments. + * + * Four different path are possible: + * - `truncation="fusion"` and the length of the waveform is greater than the max length: the mel spectrogram + * will be computed on the entire audio. 3 random crops and a dowsampled version of the full mel spectrogram + * are then stacked together. They will later be used for `feature_fusion`. + * - `truncation="rand_trunc"` and the length of the waveform is smaller than the max length: the audio is + * padded based on `padding`. + * - `truncation="fusion"` and the length of the waveform is smaller than the max length: the audio is padded + * based on `padding`, and is repeated `4` times. + * - `truncation="rand_trunc"` and the length of the waveform is greater than the max length: the mel + * spectrogram will be computed on a random crop of the waveform. + * + * @param {Float32Array|Float64Array} waveform The input waveform. + * @param {number} max_length The maximum length of the waveform. + * @param {string} truncation The truncation strategy to use. + * @param {string} padding The padding strategy to use. + * @returns {Promise} An object containing the mel spectrogram data as a Float32Array, its dimensions as an array of numbers, and a boolean indicating whether the waveform was longer than the max length. + * @private + */ + async _get_input_mel(waveform, max_length, truncation, padding) { + let input_mel; + let longer = false; + const diff = waveform.length - max_length; + if (diff > 0) { + if (truncation === "rand_trunc") { + longer = true; + const idx = Math.floor(Math.random() * (diff + 1)); + waveform = waveform.subarray(idx, idx + max_length); + input_mel = await this._extract_fbank_features(waveform, this.mel_filters_slaney, this.config.nb_max_samples); + } else { + throw new Error(`Truncation strategy "${truncation}" not implemented`); + } + } else { + if (diff < 0) { + let padded = new Float64Array(max_length); + padded.set(waveform); + if (padding === "repeat") { + for (let i = waveform.length; i < max_length; i += waveform.length) { + padded.set(waveform.subarray(0, Math.min(waveform.length, max_length - i)), i); + } + } else if (padding === "repeatpad") { + for (let i = waveform.length; i < -diff; i += waveform.length) { + padded.set(waveform, i); + } + } + waveform = padded; + } + if (truncation === "fusion") { + throw new Error(`Truncation strategy "${truncation}" not implemented`); + } + input_mel = await this._extract_fbank_features(waveform, this.mel_filters_slaney, this.config.nb_max_samples); + } + return input_mel.unsqueeze_(0); + } + /** + * Compute the log-mel spectrogram of the provided `waveform` using the Hann window. + * In CLAP, two different filter banks are used depending on the truncation pattern: + * - `self.mel_filters`: they correspond to the default parameters of `torchaudio` which can be obtained from + * calling `torchaudio.transforms.MelSpectrogram().mel_scale.fb`. These filters are used when `truncation` + * is set to `"fusion"`. + * - `self.mel_filteres_slaney` : they correspond to the default parameters of `librosa` which used + * `librosa.filters.mel` when computing the mel spectrogram. These filters were only used in the original + * implementation when the truncation mode is not `"fusion"`. + * + * @param {Float32Array|Float64Array} waveform The audio waveform to process. + * @param {number[][]} mel_filters The mel filters to use. + * @param {number} [max_length=null] The maximum number of frames to return. + * @returns {Promise} An object containing the log-Mel spectrogram data as a Float32Array and its dimensions as an array of numbers. + */ + async _extract_fbank_features(waveform, mel_filters, max_length = null) { + return (0, _utils_audio_js__WEBPACK_IMPORTED_MODULE_6__2.spectrogram)( + waveform, + this.window, + // window + this.config.fft_window_size, + // frame_length + this.config.hop_length, + // hop_length + { + power: 2, + mel_filters, + log_mel: "dB", + // Custom + max_num_frames: max_length, + do_pad: false, + transpose: true + } + ); + } + /** + * Asynchronously extracts features from a given audio using the provided configuration. + * @param {Float32Array|Float64Array} audio The audio data as a Float32Array/Float64Array. + * @returns {Promise<{ input_features: Tensor }>} A Promise resolving to an object containing the extracted input features as a Tensor. + */ + async _call(audio, { + max_length = null + } = {}) { + validate_audio_inputs(audio, "ClapFeatureExtractor"); + const padded_inputs = await this._get_input_mel( + audio, + max_length ?? this.config.nb_max_samples, + this.config.truncation, + this.config.padding + ); + return { + input_features: padded_inputs.unsqueeze_(0) + }; + } + } + class PyAnnoteFeatureExtractor extends FeatureExtractor { + /** + * Asynchronously extracts features from a given audio using the provided configuration. + * @param {Float32Array|Float64Array} audio The audio data as a Float32Array/Float64Array. + * @returns {Promise<{ input_values: Tensor; }>} The extracted input features. + */ + async _call(audio) { + validate_audio_inputs(audio, "PyAnnoteFeatureExtractor"); + if (audio instanceof Float64Array) { + audio = new Float32Array(audio); + } + const shape = [ + 1, + /* batch_size */ + 1, + /* num_channels */ + audio.length + /* num_samples */ + ]; + return { + input_values: new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_4__.Tensor("float32", audio, shape) + }; + } + /** + * NOTE: Can return fractional values. `Math.ceil` will ensure correct value. + * @param {number} samples The number of frames in the audio. + * @returns {number} The number of frames in the audio. + */ + samples_to_frames(samples) { + return (samples - this.config.offset) / this.config.step; + } + /** + * Post-processes the speaker diarization logits output by the model. + * @param {Tensor} logits The speaker diarization logits output by the model. + * @param {number} num_samples Number of samples in the input audio. + * @returns {Array>} The post-processed speaker diarization results. + */ + post_process_speaker_diarization(logits, num_samples) { + const ratio = num_samples / this.samples_to_frames(num_samples) / this.config.sampling_rate; + const results = []; + for (const scores of logits.tolist()) { + const accumulated_segments = []; + let current_speaker = -1; + for (let i = 0; i < scores.length; ++i) { + const probabilities = (0, _utils_maths_js__WEBPACK_IMPORTED_MODULE_3__.softmax)(scores[i]); + const [score, id] = (0, _utils_maths_js__WEBPACK_IMPORTED_MODULE_3__.max)(probabilities); + const [start, end] = [i, i + 1]; + if (id !== current_speaker) { + current_speaker = id; + accumulated_segments.push({ id, start, end, score }); + } else { + accumulated_segments.at(-1).end = end; + accumulated_segments.at(-1).score += score; + } + } + results.push(accumulated_segments.map( + // Convert frame-space to time-space + // and compute the confidence + ({ id, start, end, score }) => ({ + id, + start: start * ratio, + end: end * ratio, + confidence: score / (end - start) + }) + )); + } + return results; + } + } + class WeSpeakerFeatureExtractor extends FeatureExtractor { + constructor(config) { + super(config); + const sampling_rate = this.config.sampling_rate; + const mel_filters = (0, _utils_audio_js__WEBPACK_IMPORTED_MODULE_6__2.mel_filter_bank)( + 256, + // num_frequency_bins + this.config.num_mel_bins, + // num_mel_filters + 20, + // min_frequency + Math.floor(sampling_rate / 2), + // max_frequency + sampling_rate, + // sampling_rate + null, + // norm + "kaldi", + // mel_scale + true + // triangularize_in_mel_space + ); + for (let i = 0; i < mel_filters.length; ++i) { + mel_filters[i].push(0); + } + this.mel_filters = mel_filters; + this.window = (0, _utils_audio_js__WEBPACK_IMPORTED_MODULE_6__2.window_function)(400, "hamming", { + periodic: false + }); + this.min_num_frames = this.config.min_num_frames; + } + /** + * Computes the log-Mel spectrogram of the provided audio waveform. + * @param {Float32Array|Float64Array} waveform The audio waveform to process. + * @returns {Promise} An object containing the log-Mel spectrogram data as a Float32Array and its dimensions as an array of numbers. + */ + async _extract_fbank_features(waveform) { + waveform = waveform.map((x) => x * 32768); + return (0, _utils_audio_js__WEBPACK_IMPORTED_MODULE_6__2.spectrogram)( + waveform, + this.window, + // window + 400, + // frame_length + 160, + // hop_length + { + fft_length: 512, + power: 2, + center: false, + preemphasis: 0.97, + mel_filters: this.mel_filters, + log_mel: "log", + mel_floor: 1192092955078125e-22, + remove_dc_offset: true, + // Custom + transpose: true, + min_num_frames: this.min_num_frames + } + ); + } + /** + * Asynchronously extracts features from a given audio using the provided configuration. + * @param {Float32Array|Float64Array} audio The audio data as a Float32Array/Float64Array. + * @returns {Promise<{ input_features: Tensor }>} A Promise resolving to an object containing the extracted input features as a Tensor. + */ + async _call(audio) { + validate_audio_inputs(audio, "WeSpeakerFeatureExtractor"); + const features = (await this._extract_fbank_features(audio)).unsqueeze_(0); + if (this.config.fbank_centering_span === null) { + const meanData = ( + /** @type {Float32Array} */ + features.mean(1).data + ); + const featuresData = ( + /** @type {Float32Array} */ + features.data + ); + const [batch_size, num_frames, feature_size] = features.dims; + for (let i = 0; i < batch_size; ++i) { + const offset1 = i * num_frames * feature_size; + const offset2 = i * feature_size; + for (let j = 0; j < num_frames; ++j) { + const offset3 = offset1 + j * feature_size; + for (let k = 0; k < feature_size; ++k) { + featuresData[offset3 + k] -= meanData[offset2 + k]; + } + } + } + } + return { + input_features: features + }; + } + } + class SpeechT5FeatureExtractor extends FeatureExtractor { + } + class Processor extends _utils_generic_js__WEBPACK_IMPORTED_MODULE_0__.Callable { + /** + * Creates a new Processor with the given feature extractor. + * @param {FeatureExtractor} feature_extractor The function used to extract features from the input. + */ + constructor(feature_extractor) { + super(); + this.feature_extractor = feature_extractor; + } + /** + * Calls the feature_extractor function with the given input. + * @param {any} input The input to extract features from. + * @param {...any} args Additional arguments. + * @returns {Promise} A Promise that resolves with the extracted features. + */ + async _call(input, ...args) { + return await this.feature_extractor(input, ...args); + } + } + class SamProcessor extends Processor { + /** + * @borrows SamImageProcessor#_call as _call + */ + async _call(...args) { + return await this.feature_extractor(...args); + } + /** + * @borrows SamImageProcessor#post_process_masks as post_process_masks + */ + post_process_masks(...args) { + return this.feature_extractor.post_process_masks(...args); + } + /** + * @borrows SamImageProcessor#reshape_input_points as reshape_input_points + */ + reshape_input_points(...args) { + return this.feature_extractor.reshape_input_points(...args); + } + } + class WhisperProcessor extends Processor { + /** + * Calls the feature_extractor function with the given audio input. + * @param {any} audio The audio input to extract features from. + * @returns {Promise} A Promise that resolves with the extracted features. + */ + async _call(audio) { + return await this.feature_extractor(audio); + } + } + class Wav2Vec2ProcessorWithLM extends Processor { + /** + * Calls the feature_extractor function with the given audio input. + * @param {any} audio The audio input to extract features from. + * @returns {Promise} A Promise that resolves with the extracted features. + */ + async _call(audio) { + return await this.feature_extractor(audio); + } + } + class PyAnnoteProcessor extends Processor { + /** + * Calls the feature_extractor function with the given audio input. + * @param {any} audio The audio input to extract features from. + * @returns {Promise} A Promise that resolves with the extracted features. + */ + async _call(audio) { + return await this.feature_extractor(audio); + } + post_process_speaker_diarization(...args) { + return this.feature_extractor.post_process_speaker_diarization(...args); + } + } + class SpeechT5Processor extends Processor { + /** + * Calls the feature_extractor function with the given input. + * @param {any} input The input to extract features from. + * @returns {Promise} A Promise that resolves with the extracted features. + */ + async _call(input) { + return await this.feature_extractor(input); + } + } + class OwlViTProcessor extends Processor { + } + class Florence2Processor extends Processor { + constructor(feature_extractor) { + super(feature_extractor); + const { + tasks_answer_post_processing_type, + task_prompts_without_inputs, + task_prompts_with_input + } = feature_extractor.config; + this.tasks_answer_post_processing_type = new Map(Object.entries(tasks_answer_post_processing_type ?? {})); + this.task_prompts_without_inputs = new Map(Object.entries(task_prompts_without_inputs ?? {})); + this.task_prompts_with_input = new Map(Object.entries(task_prompts_with_input ?? {})); + this.regexes = { + quad_boxes: /(.+?)/gm, + bboxes: /([^<]+)?/gm + }; + this.size_per_bin = 1e3; + } + /** + * Helper function to construct prompts from input texts + * @param {string|string[]} text + * @returns {string[]} + */ + construct_prompts(text) { + if (typeof text === "string") { + text = [text]; + } + const prompts = []; + for (const t of text) { + if (this.task_prompts_without_inputs.has(t)) { + prompts.push(this.task_prompts_without_inputs.get(t)); + } else { + for (const [task, prompt] of this.task_prompts_with_input) { + if (t.includes(task)) { + prompts.push(prompt.replaceAll("{input}", t).replaceAll(task, "")); + break; + } + } + if (prompts.length !== text.length) { + prompts.push(t); + } + } + } + return prompts; + } + /** + * Post-process the output of the model to each of the task outputs. + * @param {string} text The text to post-process. + * @param {string} task The task to post-process the text for. + * @param {[number, number]} image_size The size of the image. height x width. + */ + post_process_generation(text, task, image_size) { + const task_answer_post_processing_type = this.tasks_answer_post_processing_type.get(task) ?? "pure_text"; + text = text.replaceAll("", "").replaceAll("", ""); + let final_answer; + switch (task_answer_post_processing_type) { + case "pure_text": + final_answer = text; + break; + case "description_with_bboxes": + case "bboxes": + case "phrase_grounding": + case "ocr": + const key = task_answer_post_processing_type === "ocr" ? "quad_boxes" : "bboxes"; + const matches = text.matchAll(this.regexes[key]); + const labels = []; + const items = []; + for (const [_, label, ...locations] of matches) { + labels.push(label ? label.trim() : labels.at(-1) ?? ""); + items.push( + locations.map((x, i) => ( + // NOTE: Add 0.5 to use the center position of the bin as the coordinate. + (Number(x) + 0.5) / this.size_per_bin * image_size[i % 2] + )) + ); + } + final_answer = { labels, [key]: items }; + break; + default: + throw new Error(`Task "${task}" (of type "${task_answer_post_processing_type}") not yet implemented.`); + } + return { [task]: final_answer }; + } + } + class AutoProcessor { + /** + * Instantiate one of the processor classes of the library from a pretrained model. + * + * The processor class to instantiate is selected based on the `feature_extractor_type` property of the config object + * (either passed as an argument or loaded from `pretrained_model_name_or_path` if possible) + * + * @param {string} pretrained_model_name_or_path The name or path of the pretrained model. Can be either: + * - A string, the *model id* of a pretrained processor hosted inside a model repo on huggingface.co. + * Valid model ids can be located at the root-level, like `bert-base-uncased`, or namespaced under a + * user or organization name, like `dbmdz/bert-base-german-cased`. + * - A path to a *directory* containing processor files, e.g., `./my_model_directory/`. + * @param {import('./utils/hub.js').PretrainedOptions} options Additional options for loading the processor. + * + * @returns {Promise} A new instance of the Processor class. + */ + static async from_pretrained(pretrained_model_name_or_path, { + progress_callback = null, + config = null, + cache_dir = null, + local_files_only = false, + revision = "main" + } = {}) { + let preprocessorConfig = config ?? await (0, _utils_hub_js__WEBPACK_IMPORTED_MODULE_2__.getModelJSON)(pretrained_model_name_or_path, "preprocessor_config.json", true, { + progress_callback, + config, + cache_dir, + local_files_only, + revision + }); + let key = preprocessorConfig.feature_extractor_type ?? preprocessorConfig.image_processor_type; + let feature_extractor_class = this.FEATURE_EXTRACTOR_CLASS_MAPPING[key]; + if (!feature_extractor_class) { + if (preprocessorConfig.size !== void 0) { + console.warn(`Feature extractor type "${key}" not found, assuming ImageFeatureExtractor due to size parameter in config.`); + feature_extractor_class = ImageFeatureExtractor; + } else { + throw new Error(`Unknown Feature Extractor type: ${key}`); + } + } + let processor_class = this.PROCESSOR_CLASS_MAPPING[preprocessorConfig.processor_class] ?? Processor; + let feature_extractor = new feature_extractor_class(preprocessorConfig); + return new processor_class(feature_extractor); + } + } + __publicField(AutoProcessor, "FEATURE_EXTRACTOR_CLASS_MAPPING", { + ImageFeatureExtractor, + WhisperFeatureExtractor, + ViTFeatureExtractor, + MobileViTFeatureExtractor, + MobileViTImageProcessor, + MobileNetV1FeatureExtractor, + MobileNetV2FeatureExtractor, + MobileNetV3FeatureExtractor, + MobileNetV4FeatureExtractor, + OwlViTFeatureExtractor, + Owlv2ImageProcessor, + CLIPFeatureExtractor, + CLIPImageProcessor, + Florence2Processor, + ChineseCLIPFeatureExtractor, + SiglipImageProcessor, + ConvNextFeatureExtractor, + ConvNextImageProcessor, + SegformerFeatureExtractor, + SapiensFeatureExtractor, + BitImageProcessor, + DPTImageProcessor, + DPTFeatureExtractor, + PvtImageProcessor, + GLPNFeatureExtractor, + BeitFeatureExtractor, + DeiTFeatureExtractor, + DetrFeatureExtractor, + RTDetrImageProcessor, + MaskFormerFeatureExtractor, + YolosFeatureExtractor, + DonutFeatureExtractor, + NougatImageProcessor, + EfficientNetImageProcessor, + ViTImageProcessor, + VitMatteImageProcessor, + SamImageProcessor, + Swin2SRImageProcessor, + Wav2Vec2FeatureExtractor, + SeamlessM4TFeatureExtractor, + SpeechT5FeatureExtractor, + ASTFeatureExtractor, + ClapFeatureExtractor, + PyAnnoteFeatureExtractor, + WeSpeakerFeatureExtractor + }); + __publicField(AutoProcessor, "PROCESSOR_CLASS_MAPPING", { + WhisperProcessor, + Wav2Vec2ProcessorWithLM, + PyAnnoteProcessor, + SamProcessor, + SpeechT5Processor, + OwlViTProcessor, + Florence2Processor + }); + } + ), + /***/ + "./src/tokenizers.js": ( + /*!***************************!*\ + !*** ./src/tokenizers.js ***! + \***************************/ + /***/ + (__unused_webpack___webpack_module__, __webpack_exports__2, __webpack_require__2) => { + __webpack_require__2.r(__webpack_exports__2); + __webpack_require__2.d(__webpack_exports__2, { + /* harmony export */ + AlbertTokenizer: () => ( + /* binding */ + AlbertTokenizer + ), + /* harmony export */ + AutoTokenizer: () => ( + /* binding */ + AutoTokenizer + ), + /* harmony export */ + BartTokenizer: () => ( + /* binding */ + BartTokenizer + ), + /* harmony export */ + BertTokenizer: () => ( + /* binding */ + BertTokenizer + ), + /* harmony export */ + BlenderbotSmallTokenizer: () => ( + /* binding */ + BlenderbotSmallTokenizer + ), + /* harmony export */ + BlenderbotTokenizer: () => ( + /* binding */ + BlenderbotTokenizer + ), + /* harmony export */ + BloomTokenizer: () => ( + /* binding */ + BloomTokenizer + ), + /* harmony export */ + CLIPTokenizer: () => ( + /* binding */ + CLIPTokenizer + ), + /* harmony export */ + CamembertTokenizer: () => ( + /* binding */ + CamembertTokenizer + ), + /* harmony export */ + CodeGenTokenizer: () => ( + /* binding */ + CodeGenTokenizer + ), + /* harmony export */ + CodeLlamaTokenizer: () => ( + /* binding */ + CodeLlamaTokenizer + ), + /* harmony export */ + CohereTokenizer: () => ( + /* binding */ + CohereTokenizer + ), + /* harmony export */ + ConvBertTokenizer: () => ( + /* binding */ + ConvBertTokenizer + ), + /* harmony export */ + DebertaTokenizer: () => ( + /* binding */ + DebertaTokenizer + ), + /* harmony export */ + DebertaV2Tokenizer: () => ( + /* binding */ + DebertaV2Tokenizer + ), + /* harmony export */ + DistilBertTokenizer: () => ( + /* binding */ + DistilBertTokenizer + ), + /* harmony export */ + ElectraTokenizer: () => ( + /* binding */ + ElectraTokenizer + ), + /* harmony export */ + EsmTokenizer: () => ( + /* binding */ + EsmTokenizer + ), + /* harmony export */ + FalconTokenizer: () => ( + /* binding */ + FalconTokenizer + ), + /* harmony export */ + GPT2Tokenizer: () => ( + /* binding */ + GPT2Tokenizer + ), + /* harmony export */ + GPTNeoXTokenizer: () => ( + /* binding */ + GPTNeoXTokenizer + ), + /* harmony export */ + GemmaTokenizer: () => ( + /* binding */ + GemmaTokenizer + ), + /* harmony export */ + Grok1Tokenizer: () => ( + /* binding */ + Grok1Tokenizer + ), + /* harmony export */ + HerbertTokenizer: () => ( + /* binding */ + HerbertTokenizer + ), + /* harmony export */ + LlamaTokenizer: () => ( + /* binding */ + LlamaTokenizer + ), + /* harmony export */ + M2M100Tokenizer: () => ( + /* binding */ + M2M100Tokenizer + ), + /* harmony export */ + MBart50Tokenizer: () => ( + /* binding */ + MBart50Tokenizer + ), + /* harmony export */ + MBartTokenizer: () => ( + /* binding */ + MBartTokenizer + ), + /* harmony export */ + MPNetTokenizer: () => ( + /* binding */ + MPNetTokenizer + ), + /* harmony export */ + MarianTokenizer: () => ( + /* binding */ + MarianTokenizer + ), + /* harmony export */ + MobileBertTokenizer: () => ( + /* binding */ + MobileBertTokenizer + ), + /* harmony export */ + NllbTokenizer: () => ( + /* binding */ + NllbTokenizer + ), + /* harmony export */ + NougatTokenizer: () => ( + /* binding */ + NougatTokenizer + ), + /* harmony export */ + PreTrainedTokenizer: () => ( + /* binding */ + PreTrainedTokenizer + ), + /* harmony export */ + Qwen2Tokenizer: () => ( + /* binding */ + Qwen2Tokenizer + ), + /* harmony export */ + RoFormerTokenizer: () => ( + /* binding */ + RoFormerTokenizer + ), + /* harmony export */ + RobertaTokenizer: () => ( + /* binding */ + RobertaTokenizer + ), + /* harmony export */ + SiglipTokenizer: () => ( + /* binding */ + SiglipTokenizer + ), + /* harmony export */ + SpeechT5Tokenizer: () => ( + /* binding */ + SpeechT5Tokenizer + ), + /* harmony export */ + SqueezeBertTokenizer: () => ( + /* binding */ + SqueezeBertTokenizer + ), + /* harmony export */ + T5Tokenizer: () => ( + /* binding */ + T5Tokenizer + ), + /* harmony export */ + TokenizerModel: () => ( + /* binding */ + TokenizerModel + ), + /* harmony export */ + VitsTokenizer: () => ( + /* binding */ + VitsTokenizer + ), + /* harmony export */ + Wav2Vec2CTCTokenizer: () => ( + /* binding */ + Wav2Vec2CTCTokenizer + ), + /* harmony export */ + WhisperTokenizer: () => ( + /* binding */ + WhisperTokenizer + ), + /* harmony export */ + XLMRobertaTokenizer: () => ( + /* binding */ + XLMRobertaTokenizer + ), + /* harmony export */ + XLMTokenizer: () => ( + /* binding */ + XLMTokenizer + ), + /* harmony export */ + is_chinese_char: () => ( + /* binding */ + is_chinese_char + ) + /* harmony export */ + }); + var _utils_generic_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__2( + /*! ./utils/generic.js */ + "./src/utils/generic.js" + ); + var _utils_core_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__2( + /*! ./utils/core.js */ + "./src/utils/core.js" + ); + var _utils_hub_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__2( + /*! ./utils/hub.js */ + "./src/utils/hub.js" + ); + var _utils_maths_js__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__2( + /*! ./utils/maths.js */ + "./src/utils/maths.js" + ); + var _utils_tensor_js__WEBPACK_IMPORTED_MODULE_4__ = __webpack_require__2( + /*! ./utils/tensor.js */ + "./src/utils/tensor.js" + ); + var _utils_data_structures_js__WEBPACK_IMPORTED_MODULE_5__ = __webpack_require__2( + /*! ./utils/data-structures.js */ + "./src/utils/data-structures.js" + ); + var _huggingface_jinja__WEBPACK_IMPORTED_MODULE_6__ = __webpack_require__2( + /*! @huggingface/jinja */ + "./node_modules/@huggingface/jinja/dist/index.js" + ); + var _models_whisper_common_whisper_js__WEBPACK_IMPORTED_MODULE_7__ = __webpack_require__2( + /*! ./models/whisper/common_whisper.js */ + "./src/models/whisper/common_whisper.js" + ); + var _utils_constants_js__WEBPACK_IMPORTED_MODULE_8__ = __webpack_require__2( + /*! ./utils/constants.js */ + "./src/utils/constants.js" + ); + async function loadTokenizer(pretrained_model_name_or_path, options) { + const info = await Promise.all([ + (0, _utils_hub_js__WEBPACK_IMPORTED_MODULE_2__.getModelJSON)(pretrained_model_name_or_path, "tokenizer.json", true, options), + (0, _utils_hub_js__WEBPACK_IMPORTED_MODULE_2__.getModelJSON)(pretrained_model_name_or_path, "tokenizer_config.json", true, options) + ]); + if (options.legacy !== null) { + info[1].legacy = options.legacy; + } + return info; + } + function regexSplit(text, regex) { + const result = []; + let prev = 0; + for (const match of text.matchAll(regex)) { + const fullMatch = match[0]; + if (prev < match.index) { + result.push(text.slice(prev, match.index)); + } + if (fullMatch.length > 0) { + result.push(fullMatch); + } + prev = match.index + fullMatch.length; + } + if (prev < text.length) { + result.push(text.slice(prev)); + } + return result; + } + function createPattern(pattern, invert = true) { + if (pattern.Regex !== void 0) { + let regex = pattern.Regex.replace(/\\([#&~])/g, "$1"); + for (const [key, value] of PROBLEMATIC_REGEX_MAP) { + regex = regex.replaceAll(key, value); + } + return new RegExp(regex, "gu"); + } else if (pattern.String !== void 0) { + const escaped = (0, _utils_core_js__WEBPACK_IMPORTED_MODULE_1__.escapeRegExp)(pattern.String); + return new RegExp(invert ? escaped : `(${escaped})`, "gu"); + } else { + console.warn("Unknown pattern type:", pattern); + return null; + } + } + function objectToMap(obj) { + return new Map(Object.entries(obj)); + } + function prepareTensorForDecode(tensor) { + const dims = tensor.dims; + switch (dims.length) { + case 1: + return tensor.tolist(); + case 2: + if (dims[0] !== 1) { + throw new Error("Unable to decode tensor with `batch size !== 1`. Use `tokenizer.batch_decode(...)` for batched inputs."); + } + return tensor.tolist()[0]; + default: + throw new Error(`Expected tensor to have 1-2 dimensions, got ${dims.length}.`); + } + } + function clean_up_tokenization(text) { + return text.replace(/ \./g, ".").replace(/ \?/g, "?").replace(/ \!/g, "!").replace(/ ,/g, ",").replace(/ \' /g, "'").replace(/ n\'t/g, "n't").replace(/ \'m/g, "'m").replace(/ \'s/g, "'s").replace(/ \'ve/g, "'ve").replace(/ \'re/g, "'re"); + } + function remove_accents(text) { + return text.replace(new RegExp("\\p{M}", "gu"), ""); + } + function lowercase_and_remove_accent(text) { + return remove_accents(text.toLowerCase()); + } + function is_chinese_char(cp) { + return cp >= 19968 && cp <= 40959 || cp >= 13312 && cp <= 19903 || cp >= 131072 && cp <= 173791 || cp >= 173824 && cp <= 177983 || cp >= 177984 && cp <= 178207 || cp >= 178208 && cp <= 183983 || cp >= 63744 && cp <= 64255 || cp >= 194560 && cp <= 195103; + } + function fuse_unk(arr, tokens_to_ids, unk_token_id) { + const fused = []; + let i = 0; + while (i < arr.length) { + fused.push(arr[i]); + if ((tokens_to_ids.get(arr[i]) ?? unk_token_id) !== unk_token_id) { + ++i; + continue; + } + while (++i < arr.length && (tokens_to_ids.get(arr[i]) ?? unk_token_id) === unk_token_id) { + if (tokens_to_ids.get(fused.at(-1)) !== unk_token_id) { + fused[fused.length - 1] += arr[i]; + } + } + } + return fused; + } + function whitespace_split(text) { + return text.match(/\S+/g) || []; + } + const PUNCTUATION_REGEX = "\\u0021-\\u002F\\u003A-\\u0040\\u005B-\\u0060\\u007B-\\u007E"; + const PUNCTUATION_ONLY_REGEX = new RegExp(`^[${PUNCTUATION_REGEX}]+$`, "gu"); + const BLOOM_SPLIT_CHARS = ".,!?…。,、।۔،"; + const PROBLEMATIC_REGEX_MAP = /* @__PURE__ */ new Map([ + // This uses the case insensitive group modifier, which is not supported in JavaScript. + // When parsing the regex, an "Invalid group" error is thrown. + ["(?i:'s|'t|'re|'ve|'m|'ll|'d)", "(?:'([sS]|[tT]|[rR][eE]|[vV][eE]|[mM]|[lL][lL]|[dD]))"], + // Used to override the default (invalid) regex of the bloom pretokenizer. + // For more information, see https://github.com/huggingface/transformers.js/issues/94 + [` ?[^(\\s|[${BLOOM_SPLIT_CHARS}])]+`, ` ?[^\\s${BLOOM_SPLIT_CHARS}]+`] + ]); + class AddedToken { + /** + * Creates a new instance of AddedToken. + * @param {Object} config Added token configuration object. + * @param {string} config.content The content of the added token. + * @param {number} config.id The id of the added token. + * @param {boolean} [config.single_word=false] Whether this token must be a single word or can break words. + * @param {boolean} [config.lstrip=false] Whether this token should strip whitespaces on its left. + * @param {boolean} [config.rstrip=false] Whether this token should strip whitespaces on its right. + * @param {boolean} [config.normalized=false] Whether this token should be normalized. + * @param {boolean} [config.special=false] Whether this token is special. + */ + constructor(config) { + this.content = config.content; + this.id = config.id; + this.single_word = config.single_word ?? false; + this.lstrip = config.lstrip ?? false; + this.rstrip = config.rstrip ?? false; + this.special = config.special ?? false; + this.normalized = config.normalized ?? null; + } + } + class TokenizerModel extends _utils_generic_js__WEBPACK_IMPORTED_MODULE_0__.Callable { + /** + * Creates a new instance of TokenizerModel. + * @param {Object} config The configuration object for the TokenizerModel. + */ + constructor(config) { + super(); + this.config = config; + this.vocab = []; + this.tokens_to_ids = /* @__PURE__ */ new Map(); + this.unk_token_id = void 0; + this.unk_token = void 0; + this.end_of_word_suffix = void 0; + this.fuse_unk = this.config.fuse_unk ?? false; + } + /** + * Instantiates a new TokenizerModel instance based on the configuration object provided. + * @param {Object} config The configuration object for the TokenizerModel. + * @param {...*} args Optional arguments to pass to the specific TokenizerModel constructor. + * @returns {TokenizerModel} A new instance of a TokenizerModel. + * @throws Will throw an error if the TokenizerModel type in the config is not recognized. + */ + static fromConfig(config, ...args) { + switch (config.type) { + case "WordPiece": + return new WordPieceTokenizer(config); + case "Unigram": + return new Unigram(config, ...args); + case "BPE": + return new BPE(config); + default: + if (config.vocab) { + if (Array.isArray(config.vocab)) { + return new Unigram(config, ...args); + } else { + return new LegacyTokenizerModel(config, ...args); + } + } + throw new Error(`Unknown TokenizerModel type: ${config.type}`); + } + } + /** + * Internal function to call the TokenizerModel instance. + * @param {string[]} tokens The tokens to encode. + * @returns {string[]} The encoded tokens. + */ + _call(tokens) { + tokens = this.encode(tokens); + if (this.fuse_unk) { + tokens = fuse_unk(tokens, this.tokens_to_ids, this.unk_token_id); + } + return tokens; + } + /** + * Encodes a list of tokens into a list of token IDs. + * @param {string[]} tokens The tokens to encode. + * @returns {string[]} The encoded tokens. + * @throws Will throw an error if not implemented in a subclass. + */ + encode(tokens) { + throw Error("encode should be implemented in subclass."); + } + /** + * Converts a list of tokens into a list of token IDs. + * @param {string[]} tokens The tokens to convert. + * @returns {number[]} The converted token IDs. + */ + convert_tokens_to_ids(tokens) { + return tokens.map((t) => this.tokens_to_ids.get(t) ?? this.unk_token_id); + } + /** + * Converts a list of token IDs into a list of tokens. + * @param {number[]|bigint[]} ids The token IDs to convert. + * @returns {string[]} The converted tokens. + */ + convert_ids_to_tokens(ids) { + return ids.map((i) => this.vocab[i] ?? this.unk_token); + } + } + class WordPieceTokenizer extends TokenizerModel { + /** + * @param {Object} config The configuration object. + * @param {Object} config.vocab A mapping of tokens to ids. + * @param {string} config.unk_token The unknown token string. + * @param {string} config.continuing_subword_prefix The prefix to use for continuing subwords. + * @param {number} [config.max_input_chars_per_word=100] The maximum number of characters per word. + */ + constructor(config) { + super(config); + this.tokens_to_ids = objectToMap(config.vocab); + this.unk_token_id = this.tokens_to_ids.get(config.unk_token); + this.unk_token = config.unk_token; + this.max_input_chars_per_word = config.max_input_chars_per_word ?? 100; + this.vocab = new Array(this.tokens_to_ids.size); + for (const [key, value] of this.tokens_to_ids) { + this.vocab[value] = key; + } + } + /** + * Encodes an array of tokens using WordPiece encoding. + * @param {string[]} tokens The tokens to encode. + * @returns {string[]} An array of encoded tokens. + */ + encode(tokens) { + const outputTokens = []; + for (const token of tokens) { + const chars = [...token]; + if (chars.length > this.max_input_chars_per_word) { + outputTokens.push(this.unk_token); + continue; + } + let isUnknown = false; + let start = 0; + const subTokens = []; + while (start < chars.length) { + let end = chars.length; + let currentSubstring = null; + while (start < end) { + let substr = chars.slice(start, end).join(""); + if (start > 0) { + substr = this.config.continuing_subword_prefix + substr; + } + if (this.tokens_to_ids.has(substr)) { + currentSubstring = substr; + break; + } + --end; + } + if (currentSubstring === null) { + isUnknown = true; + break; + } + subTokens.push(currentSubstring); + start = end; + } + if (isUnknown) { + outputTokens.push(this.unk_token); + } else { + outputTokens.push(...subTokens); + } + } + return outputTokens; + } + } + class Unigram extends TokenizerModel { + /** + * Create a new Unigram tokenizer model. + * @param {Object} config The configuration object for the Unigram model. + * @param {number} config.unk_id The ID of the unknown token + * @param {any[][]} config.vocab A 2D array representing a mapping of tokens to scores. + * @param {Object} moreConfig Additional configuration object for the Unigram model. + */ + constructor(config, moreConfig) { + super(config); + const vocabSize = config.vocab.length; + this.vocab = new Array(vocabSize); + this.scores = new Array(vocabSize); + for (let i = 0; i < vocabSize; ++i) { + const piece = config.vocab[i]; + this.vocab[i] = piece[0]; + this.scores[i] = piece[1]; + } + this.unk_token_id = config.unk_id; + this.unk_token = this.vocab[config.unk_id]; + this.tokens_to_ids = new Map(this.vocab.map((x, i) => [x, i])); + this.bos_token = " "; + this.bos_token_id = this.tokens_to_ids.get(this.bos_token); + this.eos_token = moreConfig.eos_token; + this.eos_token_id = this.tokens_to_ids.get(this.eos_token); + this.unk_token = this.vocab[this.unk_token_id]; + this.minScore = (0, _utils_maths_js__WEBPACK_IMPORTED_MODULE_3__.min)(this.scores)[0]; + this.unk_score = this.minScore - 10; + this.scores[this.unk_token_id] = this.unk_score; + this.trie = new _utils_data_structures_js__WEBPACK_IMPORTED_MODULE_5__.CharTrie(); + this.trie.extend(this.vocab); + this.fuse_unk = true; + } + /** + * Populates lattice nodes. + * @param {TokenLattice} lattice The token lattice to populate with nodes. + */ + populateNodes(lattice) { + const chars = lattice.chars; + const mblen = 1; + let beginPos = 0; + while (beginPos < chars.length) { + let hasSingleNode = false; + const tokens = []; + const sliced = chars.slice(beginPos).join(""); + const prefixedTokens = this.trie.commonPrefixSearch(sliced); + for (const token of prefixedTokens) { + tokens.push(token); + const tokenId = this.tokens_to_ids.get(token); + const tokenScore = this.scores[tokenId]; + const n = (0, _utils_core_js__WEBPACK_IMPORTED_MODULE_1__.len)(token); + lattice.insert(beginPos, n, tokenScore, tokenId); + if (!hasSingleNode && n === mblen) { + hasSingleNode = true; + } + } + if (!hasSingleNode) { + lattice.insert(beginPos, mblen, this.unk_score, this.unk_token_id); + } + beginPos += mblen; + } + } + /** + * Encodes an array of tokens into an array of subtokens using the unigram model. + * + * @param {string} normalized The normalized string. + * @returns {string[]} An array of subtokens obtained by encoding the input tokens using the unigram model. + */ + tokenize(normalized) { + const lattice = new _utils_data_structures_js__WEBPACK_IMPORTED_MODULE_5__.TokenLattice(normalized, this.bos_token_id, this.eos_token_id); + this.populateNodes(lattice); + return lattice.tokens(); + } + /** + * Encodes an array of tokens using Unigram encoding. + * @param {string[]} tokens The tokens to encode. + * @returns {string[]} An array of encoded tokens. + */ + encode(tokens) { + const toReturn = []; + for (const token of tokens) { + const tokenized = this.tokenize(token); + toReturn.push(...tokenized); + } + return toReturn; + } + } + const BYTES_TO_UNICODE = (() => { + const bs = [ + ...Array.from({ length: "~".charCodeAt(0) - "!".charCodeAt(0) + 1 }, (_, i) => i + "!".charCodeAt(0)), + ...Array.from({ length: "¬".charCodeAt(0) - "¡".charCodeAt(0) + 1 }, (_, i) => i + "¡".charCodeAt(0)), + ...Array.from({ length: "ÿ".charCodeAt(0) - "®".charCodeAt(0) + 1 }, (_, i) => i + "®".charCodeAt(0)) + ]; + const cs = bs.slice(); + let n = 0; + for (let b = 0; b < 256; ++b) { + if (!bs.includes(b)) { + bs.push(b); + cs.push(256 + n); + n += 1; + } + } + const ccs = cs.map((n2) => String.fromCharCode(n2)); + return Object.fromEntries(bs.map((b, i) => [b, ccs[i]])); + })(); + const UNICODE_TO_BYTES = (0, _utils_core_js__WEBPACK_IMPORTED_MODULE_1__.reverseDictionary)(BYTES_TO_UNICODE); + class BPE extends TokenizerModel { + /** + * Create a BPE instance. + * @param {Object} config The configuration object for BPE. + * @param {Object} config.vocab A mapping of tokens to ids. + * @param {string[]|[string, string][]} config.merges An array of BPE merges as strings. + * @param {string} config.unk_token The unknown token used for out of vocabulary words. + * @param {string} config.end_of_word_suffix The suffix to place at the end of each word. + * @param {string} [config.continuing_subword_suffix] The suffix to insert between words. + * @param {boolean} [config.byte_fallback=false] Whether to use spm byte-fallback trick (defaults to False) + * @param {boolean} [config.ignore_merges=false] Whether or not to match tokens with the vocab before using merges. + */ + constructor(config) { + super(config); + this.tokens_to_ids = objectToMap(config.vocab); + this.unk_token_id = this.tokens_to_ids.get(config.unk_token); + this.unk_token = config.unk_token; + this.vocab = new Array(this.tokens_to_ids.size); + for (const [key, value] of this.tokens_to_ids) { + this.vocab[value] = key; + } + const use_new_merge_format = Array.isArray(config.merges[0]); + this.merges = use_new_merge_format ? ( + /** @type {[string, string][]} */ + config.merges + ) : ( + /** @type {string[]} */ + config.merges.map((x) => ( + /** @type {[string, string]} */ + x.split(" ", 2) + )) + ); + this.bpe_ranks = new Map(this.merges.map((x, i) => [JSON.stringify(x), i])); + this.end_of_word_suffix = config.end_of_word_suffix; + this.continuing_subword_suffix = config.continuing_subword_suffix ?? null; + this.byte_fallback = this.config.byte_fallback ?? false; + if (this.byte_fallback) { + this.text_encoder = new TextEncoder(); + } + this.ignore_merges = this.config.ignore_merges ?? false; + this.cache = /* @__PURE__ */ new Map(); + } + /** + * Apply Byte-Pair-Encoding (BPE) to a given token. Efficient heap-based priority + * queue implementation adapted from https://github.com/belladoreai/llama-tokenizer-js. + * @param {string} token The token to encode. + * @returns {string[]} The BPE encoded tokens. + */ + bpe(token) { + if (token.length === 0) { + return []; + } + const cached = this.cache.get(token); + if (cached !== void 0) { + return cached; + } + const word = Array.from(token); + if (this.end_of_word_suffix) { + word[word.length - 1] += this.end_of_word_suffix; + } + let result = []; + if (word.length > 1) { + const queue = new _utils_data_structures_js__WEBPACK_IMPORTED_MODULE_5__.PriorityQueue((a, b) => a.score < b.score); + let startingNode = { + token: word[0], + bias: 0, + prev: null, + next: null + }; + let previousNode = startingNode; + for (let i = 1; i < word.length; ++i) { + const currentNode = { + bias: i / word.length, + // Add fractional component to break ties + token: word[i], + prev: previousNode, + next: null + }; + previousNode.next = currentNode; + this._add_node(queue, previousNode); + previousNode = currentNode; + } + while (!queue.isEmpty()) { + const node = queue.pop(); + if (node.deleted || !node.next || node.next.deleted) continue; + node.deleted = true; + node.next.deleted = true; + if (node.prev) { + const newPreviousNode = { ...node.prev }; + node.prev.deleted = true; + node.prev = newPreviousNode; + if (newPreviousNode.prev) { + newPreviousNode.prev.next = newPreviousNode; + } else { + startingNode = newPreviousNode; + } + } + const merged = { + token: node.token + node.next.token, + bias: node.bias, + prev: node.prev, + next: node.next.next + }; + if (merged.prev) { + merged.prev.next = merged; + this._add_node(queue, merged.prev); + } else { + startingNode = merged; + } + if (merged.next) { + merged.next.prev = merged; + this._add_node(queue, merged); + } + } + for (let currentNode = startingNode; currentNode !== null; currentNode = currentNode.next) { + result.push(currentNode.token); + } + } else { + result = word; + } + if (this.continuing_subword_suffix) { + for (let i = 0; i < result.length - 1; ++i) { + result[i] += this.continuing_subword_suffix; + } + } + this.cache.set(token, result); + return result; + } + /** + * Helper function to add a node to the priority queue. + * @param {PriorityQueue} queue + * @param {BPENode} node + * @private + */ + _add_node(queue, node) { + const rank = this.bpe_ranks.get(JSON.stringify([node.token, node.next.token])); + if (rank !== void 0) { + node.score = rank + node.bias; + queue.push(node); + } + } + /** + * Encodes the input sequence of tokens using the BPE algorithm and returns the resulting subword tokens. + * @param {string[]} tokens The input sequence of tokens to encode. + * @returns {string[]} The resulting subword tokens after applying the BPE algorithm to the input sequence of tokens. + */ + encode(tokens) { + const outputTokens = []; + for (const token of tokens) { + if (this.ignore_merges && this.tokens_to_ids.has(token)) { + outputTokens.push(token); + continue; + } + const bpe_token_list = this.bpe(token); + for (const t of bpe_token_list) { + if (this.tokens_to_ids.has(t)) { + outputTokens.push(t); + } else if (this.byte_fallback) { + const byteTokens = Array.from(this.text_encoder.encode(t)).map((x) => `<0x${x.toString(16).toUpperCase().padStart(2, "0")}>`); + if (byteTokens.every((x) => this.tokens_to_ids.has(x))) { + outputTokens.push(...byteTokens); + } else { + outputTokens.push(this.unk_token); + } + } else { + outputTokens.push(this.unk_token); + } + } + } + return outputTokens; + } + } + class LegacyTokenizerModel extends TokenizerModel { + /** + * Create a LegacyTokenizerModel instance. + * @param {Object} config The configuration object for LegacyTokenizerModel. + * @param {Object} config.vocab A (possibly nested) mapping of tokens to ids. + * @param {Object} moreConfig Additional configuration object for the LegacyTokenizerModel model. + */ + constructor(config, moreConfig) { + super(config); + this.tokens_to_ids = objectToMap( + moreConfig.target_lang ? config.vocab[moreConfig.target_lang] : config.vocab + ); + this.bos_token = moreConfig.bos_token; + this.bos_token_id = this.tokens_to_ids.get(this.bos_token); + this.eos_token = moreConfig.eos_token; + this.eos_token_id = this.tokens_to_ids.get(this.eos_token); + this.pad_token = moreConfig.pad_token; + this.pad_token_id = this.tokens_to_ids.get(this.pad_token); + this.unk_token = moreConfig.unk_token; + this.unk_token_id = this.tokens_to_ids.get(this.unk_token); + this.vocab = new Array(this.tokens_to_ids.size); + for (const [key, value] of this.tokens_to_ids) { + this.vocab[value] = key; + } + } + encode(tokens) { + return tokens; + } + } + class Normalizer extends _utils_generic_js__WEBPACK_IMPORTED_MODULE_0__.Callable { + /** + * @param {Object} config The configuration object for the normalizer. + */ + constructor(config) { + super(); + this.config = config; + } + /** + * Factory method for creating normalizers from config objects. + * @static + * @param {Object} config The configuration object for the normalizer. + * @returns {Normalizer} A Normalizer object. + * @throws {Error} If an unknown Normalizer type is specified in the config. + */ + static fromConfig(config) { + if (config === null) return null; + switch (config.type) { + case "BertNormalizer": + return new BertNormalizer(config); + case "Precompiled": + return new Precompiled(config); + case "Sequence": + return new NormalizerSequence(config); + case "Replace": + return new Replace(config); + case "NFC": + return new NFC(config); + case "NFKC": + return new NFKC(config); + case "NFKD": + return new NFKD(config); + case "Strip": + return new StripNormalizer(config); + case "StripAccents": + return new StripAccents(config); + case "Lowercase": + return new Lowercase(config); + case "Prepend": + return new Prepend(config); + default: + throw new Error(`Unknown Normalizer type: ${config.type}`); + } + } + /** + * Normalize the input text. + * @abstract + * @param {string} text The text to normalize. + * @returns {string} The normalized text. + * @throws {Error} If this method is not implemented in a subclass. + */ + normalize(text) { + throw Error("normalize should be implemented in subclass."); + } + /** + * Alias for {@link Normalizer#normalize}. + * @param {string} text The text to normalize. + * @returns {string} The normalized text. + */ + _call(text) { + return this.normalize(text); + } + } + class Replace extends Normalizer { + /** + * Normalize the input text by replacing the pattern with the content. + * @param {string} text The input text to be normalized. + * @returns {string} The normalized text after replacing the pattern with the content. + */ + normalize(text) { + const pattern = createPattern(this.config.pattern); + return pattern === null ? text : text.replaceAll(pattern, this.config.content); + } + } + class NFC extends Normalizer { + /** + * Normalize the input text by applying Unicode normalization form C (NFC). + * @param {string} text The input text to be normalized. + * @returns {string} The normalized text. + */ + normalize(text) { + text = text.normalize("NFC"); + return text; + } + } + class NFKC extends Normalizer { + /** + * Normalize text using NFKC normalization. + * @param {string} text The text to be normalized. + * @returns {string} The normalized text. + */ + normalize(text) { + text = text.normalize("NFKC"); + return text; + } + } + class NFKD extends Normalizer { + /** + * Normalize text using NFKD normalization. + * @param {string} text The text to be normalized. + * @returns {string} The normalized text. + */ + normalize(text) { + text = text.normalize("NFKD"); + return text; + } + } + class StripNormalizer extends Normalizer { + /** + * Strip leading and/or trailing whitespace from the input text. + * @param {string} text The input text. + * @returns {string} The normalized text. + */ + normalize(text) { + if (this.config.strip_left && this.config.strip_right) { + text = text.trim(); + } else { + if (this.config.strip_left) { + text = text.trimStart(); + } + if (this.config.strip_right) { + text = text.trimEnd(); + } + } + return text; + } + } + class StripAccents extends Normalizer { + /** + * Remove all accents from the text. + * @param {string} text The input text. + * @returns {string} The normalized text without accents. + */ + normalize(text) { + text = remove_accents(text); + return text; + } + } + class Lowercase extends Normalizer { + /** + * Lowercases the input string. + * @param {string} text The text to normalize. + * @returns {string} The normalized text. + */ + normalize(text) { + text = text.toLowerCase(); + return text; + } + } + class Prepend extends Normalizer { + /** + * Prepends the input string. + * @param {string} text The text to normalize. + * @returns {string} The normalized text. + */ + normalize(text) { + text = this.config.prepend + text; + return text; + } + } + class NormalizerSequence extends Normalizer { + /** + * Create a new instance of NormalizerSequence. + * @param {Object} config The configuration object. + * @param {Object[]} config.normalizers An array of Normalizer configuration objects. + */ + constructor(config) { + super(config); + this.normalizers = config.normalizers.map((x) => Normalizer.fromConfig(x)); + } + /** + * Apply a sequence of Normalizers to the input text. + * @param {string} text The text to normalize. + * @returns {string} The normalized text. + */ + normalize(text) { + return this.normalizers.reduce((t, normalizer) => { + return normalizer.normalize(t); + }, text); + } + } + class BertNormalizer extends Normalizer { + /** + * Adds whitespace around any CJK (Chinese, Japanese, or Korean) character in the input text. + * + * @param {string} text The input text to tokenize. + * @returns {string} The tokenized text with whitespace added around CJK characters. + */ + _tokenize_chinese_chars(text) { + const output = []; + for (let i = 0; i < text.length; ++i) { + const char = text[i]; + const cp = char.charCodeAt(0); + if (is_chinese_char(cp)) { + output.push(" "); + output.push(char); + output.push(" "); + } else { + output.push(char); + } + } + return output.join(""); + } + /** + * Strips accents from the given text. + * @param {string} text The text to strip accents from. + * @returns {string} The text with accents removed. + */ + stripAccents(text) { + return text.normalize("NFD").replace(new RegExp("\\p{Mn}", "gu"), ""); + } + /** + * Checks whether `char` is a control character. + * @param {string} char The character to check. + * @returns {boolean} Whether `char` is a control character. + * @private + */ + _is_control(char) { + switch (char) { + case " ": + case "\n": + case "\r": + return false; + default: + return new RegExp("^\\p{Cc}|\\p{Cf}|\\p{Co}|\\p{Cs}$", "u").test(char); + } + } + /** + * Performs invalid character removal and whitespace cleanup on text. + * @param {string} text The text to clean. + * @returns {string} The cleaned text. + * @private + */ + _clean_text(text) { + const output = []; + for (const char of text) { + const cp = char.charCodeAt(0); + if (cp === 0 || cp === 65533 || this._is_control(char)) { + continue; + } + if (/^\s$/.test(char)) { + output.push(" "); + } else { + output.push(char); + } + } + return output.join(""); + } + /** + * Normalizes the given text based on the configuration. + * @param {string} text The text to normalize. + * @returns {string} The normalized text. + */ + normalize(text) { + if (this.config.clean_text) { + text = this._clean_text(text); + } + if (this.config.handle_chinese_chars) { + text = this._tokenize_chinese_chars(text); + } + if (this.config.lowercase) { + text = text.toLowerCase(); + if (this.config.strip_accents !== false) { + text = this.stripAccents(text); + } + } else if (this.config.strip_accents) { + text = this.stripAccents(text); + } + return text; + } + } + class PreTokenizer extends _utils_generic_js__WEBPACK_IMPORTED_MODULE_0__.Callable { + /** + * Factory method that returns an instance of a subclass of `PreTokenizer` based on the provided configuration. + * + * @static + * @param {Object} config A configuration object for the pre-tokenizer. + * @returns {PreTokenizer} An instance of a subclass of `PreTokenizer`. + * @throws {Error} If the provided configuration object does not correspond to any known pre-tokenizer. + */ + static fromConfig(config) { + if (config === null) return null; + switch (config.type) { + case "BertPreTokenizer": + return new BertPreTokenizer(config); + case "Sequence": + return new PreTokenizerSequence(config); + case "Whitespace": + return new WhitespacePreTokenizer(config); + case "WhitespaceSplit": + return new WhitespaceSplit(config); + case "Metaspace": + return new MetaspacePreTokenizer(config); + case "ByteLevel": + return new ByteLevelPreTokenizer(config); + case "Split": + return new SplitPreTokenizer(config); + case "Punctuation": + return new PunctuationPreTokenizer(config); + case "Digits": + return new DigitsPreTokenizer(config); + case "Replace": + return new ReplacePreTokenizer(config); + default: + throw new Error(`Unknown PreTokenizer type: ${config.type}`); + } + } + /** + * Method that should be implemented by subclasses to define the specific pre-tokenization logic. + * + * @abstract + * @param {string} text The text to pre-tokenize. + * @param {Object} [options] Additional options for the pre-tokenization logic. + * @returns {string[]} The pre-tokenized text. + * @throws {Error} If the method is not implemented in the subclass. + */ + pre_tokenize_text(text, options) { + throw Error("pre_tokenize_text should be implemented in subclass."); + } + /** + * Tokenizes the given text into pre-tokens. + * @param {string|string[]} text The text or array of texts to pre-tokenize. + * @param {Object} [options] Additional options for the pre-tokenization logic. + * @returns {string[]} An array of pre-tokens. + */ + pre_tokenize(text, options) { + return (Array.isArray(text) ? text.map((x) => this.pre_tokenize_text(x, options)) : this.pre_tokenize_text(text, options)).flat(); + } + /** + * Alias for {@link PreTokenizer#pre_tokenize}. + * @param {string|string[]} text The text or array of texts to pre-tokenize. + * @param {Object} [options] Additional options for the pre-tokenization logic. + * @returns {string[]} An array of pre-tokens. + */ + _call(text, options) { + return this.pre_tokenize(text, options); + } + } + class BertPreTokenizer extends PreTokenizer { + /** + * A PreTokenizer that splits text into wordpieces using a basic tokenization scheme + * similar to that used in the original implementation of BERT. + * + * @param {Object} config The configuration object. + */ + constructor(config) { + super(); + this.pattern = new RegExp(`[^\\s${PUNCTUATION_REGEX}]+|[${PUNCTUATION_REGEX}]`, "gu"); + } + /** + * Tokenizes a single text using the BERT pre-tokenization scheme. + * + * @param {string} text The text to tokenize. + * @param {Object} [options] Additional options for the pre-tokenization logic. + * @returns {string[]} An array of tokens. + */ + pre_tokenize_text(text, options) { + return text.trim().match(this.pattern) || []; + } + } + class ByteLevelPreTokenizer extends PreTokenizer { + /** + * Creates a new instance of the `ByteLevelPreTokenizer` class. + * @param {Object} config The configuration object. + */ + constructor(config) { + super(); + this.config = config; + this.add_prefix_space = this.config.add_prefix_space; + this.trim_offsets = this.config.trim_offsets; + this.use_regex = this.config.use_regex ?? true; + this.pattern = new RegExp("'s|'t|'re|'ve|'m|'ll|'d| ?\\p{L}+| ?\\p{N}+| ?[^\\s\\p{L}\\p{N}]+|\\s+(?!\\S)|\\s+", "gu"); + this.byte_encoder = BYTES_TO_UNICODE; + this.text_encoder = new TextEncoder(); + } + /** + * Tokenizes a single piece of text using byte-level tokenization. + * @param {string} text The text to tokenize. + * @param {Object} [options] Additional options for the pre-tokenization logic. + * @returns {string[]} An array of tokens. + */ + pre_tokenize_text(text, options) { + if (this.add_prefix_space && !text.startsWith(" ")) { + text = " " + text; + } + const tokens = this.use_regex ? text.match(this.pattern) || [] : [text]; + return tokens.map( + (token) => Array.from(this.text_encoder.encode(token), (byte) => this.byte_encoder[byte]).join("") + ); + } + } + class SplitPreTokenizer extends PreTokenizer { + /** + * @param {Object} config The configuration options for the pre-tokenizer. + * @param {Object} config.pattern The pattern used to split the text. Can be a string or a regex object. + * @param {string|undefined} config.pattern.String The string to use for splitting. Only defined if the pattern is a string. + * @param {string|undefined} config.pattern.Regex The regex to use for splitting. Only defined if the pattern is a regex. + * @param {SplitDelimiterBehavior} config.behavior The behavior to use when splitting. + * @param {boolean} config.invert Whether to split (invert=false) or match (invert=true) the pattern. + */ + constructor(config) { + super(); + this.config = config; + this.pattern = createPattern(this.config.pattern, this.config.invert); + } + /** + * Tokenizes text by splitting it using the given pattern. + * @param {string} text The text to tokenize. + * @param {Object} [options] Additional options for the pre-tokenization logic. + * @returns {string[]} An array of tokens. + */ + pre_tokenize_text(text, options) { + if (this.pattern === null) { + return []; + } + if (this.config.invert) { + return text.match(this.pattern) || []; + } else { + return regexSplit(text, this.pattern); + } + } + } + class PunctuationPreTokenizer extends PreTokenizer { + /** + * @param {Object} config The configuration options for the pre-tokenizer. + * @param {SplitDelimiterBehavior} config.behavior The behavior to use when splitting. + */ + constructor(config) { + super(); + this.config = config; + this.pattern = new RegExp(`[^${PUNCTUATION_REGEX}]+|[${PUNCTUATION_REGEX}]+`, "gu"); + } + /** + * Tokenizes text by splitting it using the given pattern. + * @param {string} text The text to tokenize. + * @param {Object} [options] Additional options for the pre-tokenization logic. + * @returns {string[]} An array of tokens. + */ + pre_tokenize_text(text, options) { + return text.match(this.pattern) || []; + } + } + class DigitsPreTokenizer extends PreTokenizer { + /** + * @param {Object} config The configuration options for the pre-tokenizer. + * @param {boolean} config.individual_digits Whether to split on individual digits. + */ + constructor(config) { + super(); + this.config = config; + const digit_pattern = `[^\\d]+|\\d${this.config.individual_digits ? "" : "+"}`; + this.pattern = new RegExp(digit_pattern, "gu"); + } + /** + * Tokenizes text by splitting it using the given pattern. + * @param {string} text The text to tokenize. + * @param {Object} [options] Additional options for the pre-tokenization logic. + * @returns {string[]} An array of tokens. + */ + pre_tokenize_text(text, options) { + return text.match(this.pattern) || []; + } + } + class PostProcessor extends _utils_generic_js__WEBPACK_IMPORTED_MODULE_0__.Callable { + /** + * @param {Object} config The configuration for the post-processor. + */ + constructor(config) { + super(); + this.config = config; + } + /** + * Factory method to create a PostProcessor object from a configuration object. + * + * @param {Object} config Configuration object representing a PostProcessor. + * @returns {PostProcessor} A PostProcessor object created from the given configuration. + * @throws {Error} If an unknown PostProcessor type is encountered. + */ + static fromConfig(config) { + if (config === null) return null; + switch (config.type) { + case "TemplateProcessing": + return new TemplateProcessing(config); + case "ByteLevel": + return new ByteLevelPostProcessor(config); + case "RobertaProcessing": + return new RobertaProcessing(config); + case "BertProcessing": + return new BertProcessing(config); + case "Sequence": + return new PostProcessorSequence(config); + default: + throw new Error(`Unknown PostProcessor type: ${config.type}`); + } + } + /** + * Method to be implemented in subclass to apply post-processing on the given tokens. + * + * @param {Array} tokens The input tokens to be post-processed. + * @param {...*} args Additional arguments required by the post-processing logic. + * @returns {PostProcessedOutput} The post-processed tokens. + * @throws {Error} If the method is not implemented in subclass. + */ + post_process(tokens, ...args) { + throw Error("post_process should be implemented in subclass."); + } + /** + * Alias for {@link PostProcessor#post_process}. + * @param {Array} tokens The text or array of texts to post-process. + * @param {...*} args Additional arguments required by the post-processing logic. + * @returns {PostProcessedOutput} The post-processed tokens. + */ + _call(tokens, ...args) { + return this.post_process(tokens, ...args); + } + } + class BertProcessing extends PostProcessor { + /** + * @param {Object} config The configuration for the post-processor. + * @param {string[]} config.cls The special tokens to add to the beginning of the input. + * @param {string[]} config.sep The special tokens to add to the end of the input. + */ + constructor(config) { + super(config); + this.cls = config.cls[0]; + this.sep = config.sep[0]; + } + /** + * Adds the special tokens to the beginning and end of the input. + * @param {string[]} tokens The input tokens. + * @param {string[]} [tokens_pair=null] An optional second set of input tokens. + * @returns {PostProcessedOutput} The post-processed tokens with the special tokens added to the beginning and end. + */ + post_process(tokens, tokens_pair = null, { + add_special_tokens = true + } = {}) { + if (add_special_tokens) { + tokens = (0, _utils_core_js__WEBPACK_IMPORTED_MODULE_1__.mergeArrays)([this.cls], tokens, [this.sep]); + } + let token_type_ids = new Array(tokens.length).fill(0); + if (tokens_pair !== null) { + const middle = add_special_tokens && this instanceof RobertaProcessing ? [this.sep] : []; + const after = add_special_tokens ? [this.sep] : []; + tokens = (0, _utils_core_js__WEBPACK_IMPORTED_MODULE_1__.mergeArrays)(tokens, middle, tokens_pair, after); + token_type_ids = (0, _utils_core_js__WEBPACK_IMPORTED_MODULE_1__.mergeArrays)(token_type_ids, new Array(tokens_pair.length + middle.length + after.length).fill(1)); + } + return { tokens, token_type_ids }; + } + } + class RobertaProcessing extends BertProcessing { + } + class TemplateProcessing extends PostProcessor { + /** + * Creates a new instance of `TemplateProcessing`. + * @param {Object} config The configuration options for the post processor. + * @param {Array} config.single The template for a single sequence of tokens. + * @param {Array} config.pair The template for a pair of sequences of tokens. + */ + constructor(config) { + super(config); + this.single = config.single; + this.pair = config.pair; + } + /** + * Replaces special tokens in the template with actual tokens. + * @param {string[]} tokens The list of tokens for the first sequence. + * @param {string[]} [tokens_pair=null] The list of tokens for the second sequence (optional). + * @returns {PostProcessedOutput} An object containing the list of tokens with the special tokens replaced with actual tokens. + */ + post_process(tokens, tokens_pair = null, { + add_special_tokens = true + } = {}) { + const type = tokens_pair === null ? this.single : this.pair; + let processedTokens = []; + let types = []; + for (const item of type) { + if ("SpecialToken" in item) { + if (add_special_tokens) { + processedTokens.push(item.SpecialToken.id); + types.push(item.SpecialToken.type_id); + } + } else if ("Sequence" in item) { + if (item.Sequence.id === "A") { + processedTokens = (0, _utils_core_js__WEBPACK_IMPORTED_MODULE_1__.mergeArrays)(processedTokens, tokens); + types = (0, _utils_core_js__WEBPACK_IMPORTED_MODULE_1__.mergeArrays)(types, new Array(tokens.length).fill(item.Sequence.type_id)); + } else if (item.Sequence.id === "B") { + processedTokens = (0, _utils_core_js__WEBPACK_IMPORTED_MODULE_1__.mergeArrays)(processedTokens, tokens_pair); + types = (0, _utils_core_js__WEBPACK_IMPORTED_MODULE_1__.mergeArrays)(types, new Array(tokens_pair.length).fill(item.Sequence.type_id)); + } + } + } + return { tokens: processedTokens, token_type_ids: types }; + } + } + class ByteLevelPostProcessor extends PostProcessor { + /** + * Post process the given tokens. + * @param {string[]} tokens The list of tokens for the first sequence. + * @param {string[]} [tokens_pair=null] The list of tokens for the second sequence (optional). + * @returns {PostProcessedOutput} An object containing the post-processed tokens. + */ + post_process(tokens, tokens_pair = null) { + if (tokens_pair) { + tokens = (0, _utils_core_js__WEBPACK_IMPORTED_MODULE_1__.mergeArrays)(tokens, tokens_pair); + } + return { tokens }; + } + } + class PostProcessorSequence extends PostProcessor { + /** + * Creates a new instance of PostProcessorSequence. + * @param {Object} config The configuration object. + * @param {Object[]} config.processors The list of post-processors to apply. + */ + constructor(config) { + super(config); + this.processors = config.processors.map((x) => PostProcessor.fromConfig(x)); + } + /** + * Post process the given tokens. + * @param {string[]} tokens The list of tokens for the first sequence. + * @param {string[]} [tokens_pair=null] The list of tokens for the second sequence (optional). + * @returns {PostProcessedOutput} An object containing the post-processed tokens. + */ + post_process(tokens, tokens_pair = null, options = {}) { + let token_type_ids; + for (const processor of this.processors) { + if (processor instanceof ByteLevelPostProcessor) { + const output = processor.post_process(tokens); + tokens = output.tokens; + if (tokens_pair) { + const pair_output = processor.post_process(tokens_pair); + tokens_pair = pair_output.tokens; + } + } else { + const output = processor.post_process(tokens, tokens_pair, options); + tokens = output.tokens; + token_type_ids = output.token_type_ids; + } + } + return { tokens, token_type_ids }; + } + } + class Decoder extends _utils_generic_js__WEBPACK_IMPORTED_MODULE_0__.Callable { + /** + * Creates an instance of `Decoder`. + * + * @param {Object} config The configuration object. + */ + constructor(config) { + super(); + this.config = config; + this.added_tokens = []; + this.end_of_word_suffix = null; + this.trim_offsets = config.trim_offsets; + } + /** + * Creates a decoder instance based on the provided configuration. + * + * @param {Object} config The configuration object. + * @returns {Decoder} A decoder instance. + * @throws {Error} If an unknown decoder type is provided. + */ + static fromConfig(config) { + if (config === null) return null; + switch (config.type) { + case "WordPiece": + return new WordPieceDecoder(config); + case "Metaspace": + return new MetaspaceDecoder(config); + case "ByteLevel": + return new ByteLevelDecoder(config); + case "Replace": + return new ReplaceDecoder(config); + case "ByteFallback": + return new ByteFallback(config); + case "Fuse": + return new FuseDecoder(config); + case "Strip": + return new StripDecoder(config); + case "Sequence": + return new DecoderSequence(config); + case "CTC": + return new CTCDecoder(config); + case "BPEDecoder": + return new BPEDecoder(config); + default: + throw new Error(`Unknown Decoder type: ${config.type}`); + } + } + /** + * Calls the `decode` method. + * + * @param {string[]} tokens The list of tokens. + * @returns {string} The decoded string. + */ + _call(tokens) { + return this.decode(tokens); + } + /** + * Decodes a list of tokens. + * @param {string[]} tokens The list of tokens. + * @returns {string} The decoded string. + */ + decode(tokens) { + return this.decode_chain(tokens).join(""); + } + /** + * Apply the decoder to a list of tokens. + * + * @param {string[]} tokens The list of tokens. + * @returns {string[]} The decoded list of tokens. + * @throws {Error} If the `decode_chain` method is not implemented in the subclass. + */ + decode_chain(tokens) { + throw Error("`decode_chain` should be implemented in subclass."); + } + } + class ReplaceDecoder extends Decoder { + /** @type {Decoder['decode_chain']} */ + decode_chain(tokens) { + const pattern = createPattern(this.config.pattern); + return pattern === null ? tokens : tokens.map((token) => token.replaceAll(pattern, this.config.content)); + } + } + class ByteFallback extends Decoder { + constructor(config) { + super(config); + this.text_decoder = new TextDecoder(); + } + /** @type {Decoder['decode_chain']} */ + decode_chain(tokens) { + const new_tokens = []; + let previous_byte_tokens = []; + for (const token of tokens) { + let bytes = null; + if (token.length === 6 && token.startsWith("<0x") && token.endsWith(">")) { + const byte = parseInt(token.slice(3, 5), 16); + if (!isNaN(byte)) { + bytes = byte; + } + } + if (bytes !== null) { + previous_byte_tokens.push(bytes); + } else { + if (previous_byte_tokens.length > 0) { + const string = this.text_decoder.decode(Uint8Array.from(previous_byte_tokens)); + new_tokens.push(string); + previous_byte_tokens = []; + } + new_tokens.push(token); + } + } + if (previous_byte_tokens.length > 0) { + const string = this.text_decoder.decode(Uint8Array.from(previous_byte_tokens)); + new_tokens.push(string); + previous_byte_tokens = []; + } + return new_tokens; + } + } + class FuseDecoder extends Decoder { + /** @type {Decoder['decode_chain']} */ + decode_chain(tokens) { + return [tokens.join("")]; + } + } + class StripDecoder extends Decoder { + constructor(config) { + super(config); + this.content = this.config.content; + this.start = this.config.start; + this.stop = this.config.stop; + } + /** @type {Decoder['decode_chain']} */ + decode_chain(tokens) { + return tokens.map((token) => { + let start_cut = 0; + for (let i = 0; i < this.start; ++i) { + if (token[i] === this.content) { + start_cut = i + 1; + continue; + } else { + break; + } + } + let stop_cut = token.length; + for (let i = 0; i < this.stop; ++i) { + const index = token.length - i - 1; + if (token[index] === this.content) { + stop_cut = index; + continue; + } else { + break; + } + } + return token.slice(start_cut, stop_cut); + }); + } + } + class WordPieceDecoder extends Decoder { + /** + * Creates a new instance of WordPieceDecoder. + * @param {Object} config The configuration object. + * @param {string} config.prefix The prefix used for WordPiece encoding. + * @param {boolean} config.cleanup Whether to cleanup the decoded string. + */ + constructor(config) { + super(config); + this.cleanup = config.cleanup; + } + /** @type {Decoder['decode_chain']} */ + decode_chain(tokens) { + return tokens.map((token, i) => { + if (i !== 0) { + if (token.startsWith(this.config.prefix)) { + token = token.replace(this.config.prefix, ""); + } else { + token = " " + token; + } + } + if (this.cleanup) { + token = clean_up_tokenization(token); + } + return token; + }); + } + } + class ByteLevelDecoder extends Decoder { + /** + * Create a `ByteLevelDecoder` object. + * @param {Object} config Configuration object. + */ + constructor(config) { + super(config); + this.byte_decoder = UNICODE_TO_BYTES; + this.text_decoder = new TextDecoder("utf-8", { + fatal: false, + ignoreBOM: true + }); + this.end_of_word_suffix = null; + } + /** + * Convert an array of tokens to string by decoding each byte. + * @param {string[]} tokens Array of tokens to be decoded. + * @returns {string} The decoded string. + */ + convert_tokens_to_string(tokens) { + const text = tokens.join(""); + const byteArray = new Uint8Array([...text].map((c) => this.byte_decoder[c])); + const decoded_text = this.text_decoder.decode(byteArray); + return decoded_text; + } + /** @type {Decoder['decode_chain']} */ + decode_chain(tokens) { + const sub_texts = []; + let current_sub_text = []; + for (const token of tokens) { + if (this.added_tokens.find((x) => x.content === token) !== void 0) { + if (current_sub_text.length > 0) { + sub_texts.push(this.convert_tokens_to_string(current_sub_text)); + current_sub_text = []; + } + sub_texts.push(token); + } else { + current_sub_text.push(token); + } + } + if (current_sub_text.length > 0) { + sub_texts.push(this.convert_tokens_to_string(current_sub_text)); + } + return sub_texts; + } + } + class CTCDecoder extends Decoder { + constructor(config) { + super(config); + this.pad_token = this.config.pad_token; + this.word_delimiter_token = this.config.word_delimiter_token; + this.cleanup = this.config.cleanup; + } + /** + * Converts a connectionist-temporal-classification (CTC) output tokens into a single string. + * @param {string[]} tokens Array of tokens to be decoded. + * @returns {string} The decoded string. + */ + convert_tokens_to_string(tokens) { + if (tokens.length === 0) return ""; + const grouped_tokens = [tokens[0]]; + for (let i = 1; i < tokens.length; ++i) { + if (tokens[i] !== grouped_tokens.at(-1)) { + grouped_tokens.push(tokens[i]); + } + } + const filtered_tokens = grouped_tokens.filter((token) => token !== this.pad_token); + let text = filtered_tokens.join(""); + if (this.cleanup) { + text = clean_up_tokenization(text).replaceAll(this.word_delimiter_token, " ").trim(); + } + return text; + } + /** @type {Decoder['decode_chain']} */ + decode_chain(tokens) { + return [this.convert_tokens_to_string(tokens)]; + } + } + class DecoderSequence extends Decoder { + /** + * Creates a new instance of DecoderSequence. + * @param {Object} config The configuration object. + * @param {Object[]} config.decoders The list of decoders to apply. + */ + constructor(config) { + super(config); + this.decoders = config.decoders.map((x) => Decoder.fromConfig(x)); + } + /** @type {Decoder['decode_chain']} */ + decode_chain(tokens) { + return this.decoders.reduce((toks, decoder) => { + return decoder.decode_chain(toks); + }, tokens); + } + } + class BPEDecoder extends Decoder { + constructor(config) { + super(config); + this.suffix = this.config.suffix; + } + /** @type {Decoder['decode_chain']} */ + decode_chain(tokens) { + return tokens.map((token, i) => { + return token.replaceAll(this.suffix, i === tokens.length - 1 ? "" : " "); + }); + } + } + class VitsDecoder extends Decoder { + /** @type {Decoder['decode_chain']} */ + decode_chain(tokens) { + let decoded = ""; + for (let i = 1; i < tokens.length; i += 2) { + decoded += tokens[i]; + } + return [decoded]; + } + } + class MetaspacePreTokenizer extends PreTokenizer { + /** + * @param {Object} config The configuration object for the MetaspacePreTokenizer. + * @param {boolean} config.add_prefix_space Whether to add a prefix space to the first token. + * @param {string} config.replacement The character to replace spaces with. + * @param {string} [config.str_rep=config.replacement] An optional string representation of the replacement character. + * @param {'first'|'never'|'always'} [config.prepend_scheme='always'] The metaspace prepending scheme. + */ + constructor(config) { + super(); + this.addPrefixSpace = config.add_prefix_space; + this.replacement = config.replacement; + this.strRep = config.str_rep || this.replacement; + this.prepend_scheme = config.prepend_scheme ?? "always"; + } + /** + * This method takes a string, replaces spaces with the replacement character, + * adds a prefix space if requested, and returns a new list of tokens. + * @param {string} text The text to pre-tokenize. + * @param {Object} [options] The options for the pre-tokenization. + * @param {number} [options.section_index] The index of the section to pre-tokenize. + * @returns {string[]} A new list of pre-tokenized tokens. + */ + pre_tokenize_text(text, { + section_index = void 0 + } = {}) { + let normalized = text.replaceAll(" ", this.strRep); + if ( + // We add a prefix space if: + // (1) The addPrefixSpace option is enabled and the normalized + // token does not already start with the replacement character. + this.addPrefixSpace && !normalized.startsWith(this.replacement) && (this.prepend_scheme === "always" || this.prepend_scheme === "first" && section_index === 0) + ) { + normalized = this.strRep + normalized; + } + return [normalized]; + } + } + class MetaspaceDecoder extends Decoder { + /** + * Constructs a new MetaspaceDecoder object. + * @param {Object} config The configuration object for the MetaspaceDecoder. + * @param {boolean} config.add_prefix_space Whether to add a prefix space to the decoded string. + * @param {string} config.replacement The string to replace spaces with. + */ + constructor(config) { + super(config); + this.addPrefixSpace = config.add_prefix_space; + this.replacement = config.replacement; + } + /** @type {Decoder['decode_chain']} */ + decode_chain(tokens) { + const result = []; + for (let i = 0; i < tokens.length; ++i) { + let normalized = tokens[i].replaceAll(this.replacement, " "); + if (this.addPrefixSpace && i == 0 && normalized.startsWith(" ")) { + normalized = normalized.substring(1); + } + result.push(normalized); + } + return result; + } + } + class Precompiled extends Normalizer { + /** + * Create a new instance of Precompiled normalizer. + * @param {Object} config The configuration object. + * @param {any} config.precompiled_charsmap Precompiled chars mapping. + */ + constructor(config) { + super(config); + this.charsmap = config.precompiled_charsmap; + } + /** + * Normalizes the given text by applying the precompiled charsmap. + * @param {string} text The text to normalize. + * @returns {string} The normalized text. + */ + normalize(text) { + text = text.replace(/[\u0001-\u0008\u000B\u000E-\u001F\u007F\u008F\u009F]/gm, ""); + text = text.replace(/[\u0009\u000A\u000C\u000D\u00A0\u1680\u2000-\u200F\u2028\u2029\u202F\u205F\u2581\u3000\uFEFF\uFFFD]/gm, " "); + if (text.includes("~")) { + const parts = text.split("~"); + text = parts.map((part) => part.normalize("NFKC")).join("~"); + } else { + text = text.normalize("NFKC"); + } + return text; + } + } + class PreTokenizerSequence extends PreTokenizer { + /** + * Creates an instance of PreTokenizerSequence. + * @param {Object} config The configuration object for the pre-tokenizer sequence. + * @param {Object[]} config.pretokenizers An array of pre-tokenizer configurations. + */ + constructor(config) { + super(); + this.tokenizers = config.pretokenizers.map((x) => PreTokenizer.fromConfig(x)); + } + /** + * Applies each pre-tokenizer in the sequence to the input text in turn. + * @param {string} text The text to pre-tokenize. + * @param {Object} [options] Additional options for the pre-tokenization logic. + * @returns {string[]} The pre-tokenized text. + */ + pre_tokenize_text(text, options) { + return this.tokenizers.reduce((preTokenizedText, tokenizer) => { + return tokenizer.pre_tokenize(preTokenizedText, options); + }, [text]); + } + } + class WhitespacePreTokenizer extends PreTokenizer { + /** + * Creates an instance of WhitespacePreTokenizer. + * @param {Object} config The configuration object for the pre-tokenizer. + */ + constructor(config) { + super(); + } + /** + * Pre-tokenizes the input text by splitting it on word boundaries. + * @param {string} text The text to be pre-tokenized. + * @param {Object} [options] Additional options for the pre-tokenization logic. + * @returns {string[]} An array of tokens produced by splitting the input text on whitespace. + */ + pre_tokenize_text(text, options) { + return text.match(/\w+|[^\w\s]+/g) || []; + } + } + class WhitespaceSplit extends PreTokenizer { + /** + * Creates an instance of WhitespaceSplit. + * @param {Object} config The configuration object for the pre-tokenizer. + */ + constructor(config) { + super(); + } + /** + * Pre-tokenizes the input text by splitting it on whitespace characters. + * @param {string} text The text to be pre-tokenized. + * @param {Object} [options] Additional options for the pre-tokenization logic. + * @returns {string[]} An array of tokens produced by splitting the input text on whitespace. + */ + pre_tokenize_text(text, options) { + return whitespace_split(text); + } + } + class ReplacePreTokenizer extends PreTokenizer { + /** + * @param {Object} config The configuration options for the pre-tokenizer. + * @param {Object} config.pattern The pattern used to split the text. Can be a string or a regex object. + * @param {string} config.content What to replace the pattern with. + */ + constructor(config) { + super(); + this.config = config; + this.pattern = createPattern(this.config.pattern); + this.content = this.config.content; + } + /** + * Pre-tokenizes the input text by replacing certain characters. + * @param {string} text The text to be pre-tokenized. + * @param {Object} [options] Additional options for the pre-tokenization logic. + * @returns {string[]} An array of tokens produced by replacing certain characters. + */ + pre_tokenize_text(text, options) { + if (this.pattern === null) { + return [text]; + } + return [text.replaceAll(this.pattern, this.config.content)]; + } + } + const SPECIAL_TOKEN_ATTRIBUTES = [ + "bos_token", + "eos_token", + "unk_token", + "sep_token", + "pad_token", + "cls_token", + "mask_token" + // additional_special_tokens (TODO) + ]; + function padHelper(item, length, value_fn, side) { + for (const key of Object.keys(item)) { + const diff = length - item[key].length; + const value = value_fn(key); + const padData = new Array(diff).fill(value); + item[key] = side === "right" ? (0, _utils_core_js__WEBPACK_IMPORTED_MODULE_1__.mergeArrays)(item[key], padData) : (0, _utils_core_js__WEBPACK_IMPORTED_MODULE_1__.mergeArrays)(padData, item[key]); + } + } + function truncateHelper(item, length) { + for (const key of Object.keys(item)) { + item[key].length = length; + } + } + class PreTrainedTokenizer extends _utils_generic_js__WEBPACK_IMPORTED_MODULE_0__.Callable { + /** + * Create a new PreTrainedTokenizer instance. + * @param {Object} tokenizerJSON The JSON of the tokenizer. + * @param {Object} tokenizerConfig The config of the tokenizer. + */ + constructor(tokenizerJSON, tokenizerConfig) { + super(); + __publicField(this, "return_token_type_ids", false); + __publicField(this, "padding_side", "right"); + this._tokenizer_config = tokenizerConfig; + this.normalizer = Normalizer.fromConfig(tokenizerJSON.normalizer); + this.pre_tokenizer = PreTokenizer.fromConfig(tokenizerJSON.pre_tokenizer); + this.model = TokenizerModel.fromConfig(tokenizerJSON.model, tokenizerConfig); + this.post_processor = PostProcessor.fromConfig(tokenizerJSON.post_processor); + this.decoder = Decoder.fromConfig(tokenizerJSON.decoder); + this.special_tokens = []; + this.all_special_ids = []; + this.added_tokens = []; + for (const addedToken of tokenizerJSON.added_tokens) { + const token = new AddedToken(addedToken); + this.added_tokens.push(token); + this.model.tokens_to_ids.set(token.content, token.id); + this.model.vocab[token.id] = token.content; + if (token.special) { + this.special_tokens.push(token.content); + this.all_special_ids.push(token.id); + } + } + this.additional_special_tokens = tokenizerConfig.additional_special_tokens ?? []; + this.special_tokens.push(...this.additional_special_tokens); + this.special_tokens = [...new Set(this.special_tokens)]; + if (this.decoder) { + this.decoder.added_tokens = this.added_tokens; + this.decoder.end_of_word_suffix = this.model.end_of_word_suffix; + } + this.added_tokens_regex = this.added_tokens.length > 0 ? new RegExp( + this.added_tokens.slice().sort((a, b) => b.content.length - a.content.length).map((x) => `${x.lstrip ? "\\s*" : ""}(${(0, _utils_core_js__WEBPACK_IMPORTED_MODULE_1__.escapeRegExp)(x.content)})${x.rstrip ? "\\s*" : ""}`).join("|") + ) : null; + this.mask_token = this.getToken("mask_token"); + this.mask_token_id = this.model.tokens_to_ids.get(this.mask_token); + this.pad_token = this.getToken("pad_token", "eos_token"); + this.pad_token_id = this.model.tokens_to_ids.get(this.pad_token); + this.sep_token = this.getToken("sep_token"); + this.sep_token_id = this.model.tokens_to_ids.get(this.sep_token); + this.unk_token = this.getToken("unk_token"); + this.unk_token_id = this.model.tokens_to_ids.get(this.unk_token); + this.model_max_length = tokenizerConfig.model_max_length; + this.remove_space = tokenizerConfig.remove_space; + this.clean_up_tokenization_spaces = tokenizerConfig.clean_up_tokenization_spaces ?? true; + this.do_lowercase_and_remove_accent = tokenizerConfig.do_lowercase_and_remove_accent ?? false; + if (tokenizerConfig.padding_side) { + this.padding_side = tokenizerConfig.padding_side; + } + this.legacy = false; + this.chat_template = tokenizerConfig.chat_template ?? null; + if (Array.isArray(this.chat_template)) { + const chat_template = /* @__PURE__ */ Object.create(null); + for (const { name, template } of this.chat_template) { + if (typeof name !== "string" || typeof template !== "string") { + throw new Error('Chat template must be a list of objects with "name" and "template" properties'); + } + chat_template[name] = template; + } + this.chat_template = chat_template; + } + this._compiled_template_cache = /* @__PURE__ */ new Map(); + } + /** + * Returns the value of the first matching key in the tokenizer config object. + * @param {...string} keys One or more keys to search for in the tokenizer config object. + * @returns {string|null} The value associated with the first matching key, or null if no match is found. + * @throws {Error} If an object is found for a matching key and its __type property is not "AddedToken". + * @private + */ + getToken(...keys) { + for (const key of keys) { + const item = this._tokenizer_config[key]; + if (!item) continue; + if (typeof item === "object") { + if (item.__type === "AddedToken") { + return item.content; + } else { + throw Error(`Unknown token: ${item}`); + } + } else { + return item; + } + } + return null; + } + /** + * Loads a pre-trained tokenizer from the given `pretrained_model_name_or_path`. + * + * @param {string} pretrained_model_name_or_path The path to the pre-trained tokenizer. + * @param {PretrainedTokenizerOptions} options Additional options for loading the tokenizer. + * + * @throws {Error} Throws an error if the tokenizer.json or tokenizer_config.json files are not found in the `pretrained_model_name_or_path`. + * @returns {Promise} A new instance of the `PreTrainedTokenizer` class. + */ + static async from_pretrained(pretrained_model_name_or_path, { + progress_callback = null, + config = null, + cache_dir = null, + local_files_only = false, + revision = "main", + legacy = null + } = {}) { + const info = await loadTokenizer(pretrained_model_name_or_path, { + progress_callback, + config, + cache_dir, + local_files_only, + revision, + legacy + }); + return new this(...info); + } + /** + * @typedef {number[]|number[][]|Tensor} BatchEncodingItem + * + * @typedef {Object} BatchEncoding Holds the output of the tokenizer's call function. + * @property {BatchEncodingItem} input_ids List of token ids to be fed to a model. + * @property {BatchEncodingItem} attention_mask List of indices specifying which tokens should be attended to by the model. + * @property {BatchEncodingItem} [token_type_ids] List of token type ids to be fed to a model. + */ + /** + * Encode/tokenize the given text(s). + * @param {string|string[]} text The text to tokenize. + * @param {Object} options An optional object containing the following properties: + * @param {string|string[]} [options.text_pair=null] Optional second sequence to be encoded. If set, must be the same type as text. + * @param {boolean|'max_length'} [options.padding=false] Whether to pad the input sequences. + * @param {boolean} [options.add_special_tokens=true] Whether or not to add the special tokens associated with the corresponding model. + * @param {boolean} [options.truncation=null] Whether to truncate the input sequences. + * @param {number} [options.max_length=null] Maximum length of the returned list and optionally padding length. + * @param {boolean} [options.return_tensor=true] Whether to return the results as Tensors or arrays. + * @param {boolean} [options.return_token_type_ids=null] Whether to return the token type ids. + * @returns {BatchEncoding} Object to be passed to the model. + */ + _call(text, { + text_pair = null, + add_special_tokens = true, + padding = false, + truncation = null, + max_length = null, + return_tensor = true, + // Different to HF + return_token_type_ids = null + } = {}) { + const isBatched = Array.isArray(text); + let encodedTokens; + if (isBatched) { + if (text.length === 0) { + throw Error("text array must be non-empty"); + } + if (text_pair !== null) { + if (!Array.isArray(text_pair)) { + throw Error("text_pair must also be an array"); + } else if (text.length !== text_pair.length) { + throw Error("text and text_pair must have the same length"); + } + encodedTokens = text.map( + (t, i) => this._encode_plus(t, { text_pair: text_pair[i], add_special_tokens, return_token_type_ids }) + ); + } else { + encodedTokens = text.map((x) => this._encode_plus(x, { add_special_tokens, return_token_type_ids })); + } + } else { + if (text === null || text === void 0) { + throw Error("text may not be null or undefined"); + } + if (Array.isArray(text_pair)) { + throw Error("When specifying `text_pair`, since `text` is a string, `text_pair` must also be a string (i.e., not an array)."); + } + encodedTokens = [this._encode_plus(text, { text_pair, add_special_tokens, return_token_type_ids })]; + } + if (max_length === null) { + if (padding === "max_length") { + max_length = this.model_max_length; + } else { + max_length = (0, _utils_maths_js__WEBPACK_IMPORTED_MODULE_3__.max)(encodedTokens.map((x) => x.input_ids.length))[0]; + } + } else { + if (!truncation) { + console.warn(`Truncation was not explicitly activated but \`max_length\` is provided a specific value, please use \`truncation=true\` to explicitly truncate examples to max length.`); + } + } + max_length = Math.min(max_length, this.model_max_length ?? Infinity); + if (padding || truncation) { + for (let i = 0; i < encodedTokens.length; ++i) { + if (encodedTokens[i].input_ids.length === max_length) { + continue; + } else if (encodedTokens[i].input_ids.length > max_length) { + if (truncation) { + truncateHelper(encodedTokens[i], max_length); + } + } else { + if (padding) { + padHelper( + encodedTokens[i], + max_length, + (key) => key === "input_ids" ? this.pad_token_id : 0, + this.padding_side + ); + } + } + } + } + const result = {}; + if (return_tensor) { + if (!(padding && truncation)) { + if (encodedTokens.some((x) => { + var _a; + for (const key of Object.keys(x)) { + if (x[key].length !== ((_a = encodedTokens[0][key]) == null ? void 0 : _a.length)) { + return true; + } + } + return false; + })) { + throw Error( + "Unable to create tensor, you should probably activate truncation and/or padding with 'padding=true' and 'truncation=true' to have batched tensors with the same length." + ); + } + } + const dims = [encodedTokens.length, encodedTokens[0].input_ids.length]; + for (const key of Object.keys(encodedTokens[0])) { + result[key] = new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_4__.Tensor( + "int64", + BigInt64Array.from(encodedTokens.flatMap((x) => x[key]).map(BigInt)), + dims + ); + } + } else { + for (const key of Object.keys(encodedTokens[0])) { + result[key] = encodedTokens.map((x) => x[key]); + } + if (!isBatched) { + for (const key of Object.keys(result)) { + result[key] = result[key][0]; + } + } + } + return ( + /** @type {BatchEncoding} */ + result + ); + } + /** + * Encodes a single text using the preprocessor pipeline of the tokenizer. + * + * @param {string|null} text The text to encode. + * @returns {string[]|null} The encoded tokens. + */ + _encode_text(text) { + if (text === null) return null; + const sections = this.added_tokens_regex ? text.split(this.added_tokens_regex).filter((x) => x) : [text]; + const tokens = sections.map((x, section_index) => { + const addedToken = this.added_tokens.find((t) => t.content === x); + if (addedToken !== void 0) { + return x; + } else { + if (this.remove_space === true) { + x = x.trim().split(/\s+/).join(" "); + } + if (this.do_lowercase_and_remove_accent) { + x = lowercase_and_remove_accent(x); + } + if (this.normalizer !== null) { + x = this.normalizer(x); + } + if (x.length === 0) { + return []; + } + const sectionTokens = this.pre_tokenizer !== null ? this.pre_tokenizer(x, { + section_index + }) : [x]; + const tokens2 = this.model(sectionTokens); + return tokens2; + } + }).flat(); + return tokens; + } + /** + * Encodes a single text or a pair of texts using the model's tokenizer. + * + * @param {string} text The text to encode. + * @param {Object} options An optional object containing the following properties: + * @param {string} [options.text_pair=null] The optional second text to encode. + * @param {boolean} [options.add_special_tokens=true] Whether or not to add the special tokens associated with the corresponding model. + * @param {boolean} [options.return_token_type_ids=null] Whether to return token_type_ids. + * @returns {EncodingSingle} An object containing the encoded text. + * @private + */ + _encode_plus(text, { + text_pair = null, + add_special_tokens = true, + return_token_type_ids = null + } = {}) { + const { tokens, token_type_ids } = this._tokenize_helper(text, { pair: text_pair, add_special_tokens }); + const input_ids = this.model.convert_tokens_to_ids(tokens); + const result = { + input_ids, + attention_mask: new Array(input_ids.length).fill(1) + }; + if ((return_token_type_ids ?? this.return_token_type_ids) && token_type_ids) { + result.token_type_ids = token_type_ids; + } + return result; + } + /** + * Internal helper function to tokenize a text, and optionally a pair of texts. + * @param {string} text The text to tokenize. + * @param {Object} options An optional object containing the following properties: + * @param {string} [options.pair=null] The optional second text to tokenize. + * @param {boolean} [options.add_special_tokens=false] Whether or not to add the special tokens associated with the corresponding model. + * @returns {{tokens: string[], token_type_ids?: number[]}} An object containing the tokens and optionally the token type IDs. + */ + _tokenize_helper(text, { + pair = null, + add_special_tokens = false + } = {}) { + const tokens = this._encode_text(text); + const tokens2 = this._encode_text(pair); + return this.post_processor ? this.post_processor(tokens, tokens2, { add_special_tokens }) : { tokens: (0, _utils_core_js__WEBPACK_IMPORTED_MODULE_1__.mergeArrays)(tokens ?? [], tokens2 ?? []) }; + } + /** + * Converts a string into a sequence of tokens. + * @param {string} text The sequence to be encoded. + * @param {Object} options An optional object containing the following properties: + * @param {string} [options.pair] A second sequence to be encoded with the first. + * @param {boolean} [options.add_special_tokens=false] Whether or not to add the special tokens associated with the corresponding model. + * @returns {string[]} The list of tokens. + */ + tokenize(text, { + pair = null, + add_special_tokens = false + } = {}) { + return this._tokenize_helper(text, { pair, add_special_tokens }).tokens; + } + /** + * Encodes a single text or a pair of texts using the model's tokenizer. + * + * @param {string} text The text to encode. + * @param {Object} options An optional object containing the following properties: + * @param {string} [options.text_pair=null] The optional second text to encode. + * @param {boolean} [options.add_special_tokens=true] Whether or not to add the special tokens associated with the corresponding model. + * @param {boolean} [options.return_token_type_ids=null] Whether to return token_type_ids. + * @returns {number[]} An array of token IDs representing the encoded text(s). + */ + encode(text, { + text_pair = null, + add_special_tokens = true, + return_token_type_ids = null + } = {}) { + return this._encode_plus(text, { + text_pair, + add_special_tokens, + return_token_type_ids + }).input_ids; + } + /** + * Decode a batch of tokenized sequences. + * @param {number[][]|Tensor} batch List/Tensor of tokenized input sequences. + * @param {Object} decode_args (Optional) Object with decoding arguments. + * @returns {string[]} List of decoded sequences. + */ + batch_decode(batch, decode_args = {}) { + if (batch instanceof _utils_tensor_js__WEBPACK_IMPORTED_MODULE_4__.Tensor) { + batch = batch.tolist(); + } + return batch.map((x) => this.decode(x, decode_args)); + } + /** + * Decodes a sequence of token IDs back to a string. + * + * @param {number[]|bigint[]|Tensor} token_ids List/Tensor of token IDs to decode. + * @param {Object} [decode_args={}] + * @param {boolean} [decode_args.skip_special_tokens=false] If true, special tokens are removed from the output string. + * @param {boolean} [decode_args.clean_up_tokenization_spaces=true] If true, spaces before punctuations and abbreviated forms are removed. + * + * @returns {string} The decoded string. + * @throws {Error} If `token_ids` is not a non-empty array of integers. + */ + decode(token_ids, decode_args = {}) { + if (token_ids instanceof _utils_tensor_js__WEBPACK_IMPORTED_MODULE_4__.Tensor) { + token_ids = prepareTensorForDecode(token_ids); + } + if (!Array.isArray(token_ids) || token_ids.length === 0 || !(0, _utils_core_js__WEBPACK_IMPORTED_MODULE_1__.isIntegralNumber)(token_ids[0])) { + throw Error("token_ids must be a non-empty array of integers."); + } + return this.decode_single(token_ids, decode_args); + } + /** + * Decode a single list of token ids to a string. + * @param {number[]|bigint[]} token_ids List of token ids to decode + * @param {Object} decode_args Optional arguments for decoding + * @param {boolean} [decode_args.skip_special_tokens=false] Whether to skip special tokens during decoding + * @param {boolean} [decode_args.clean_up_tokenization_spaces=null] Whether to clean up tokenization spaces during decoding. + * If null, the value is set to `this.decoder.cleanup` if it exists, falling back to `this.clean_up_tokenization_spaces` if it exists, falling back to `true`. + * @returns {string} The decoded string + */ + decode_single(token_ids, { + skip_special_tokens = false, + clean_up_tokenization_spaces = null + }) { + let tokens = this.model.convert_ids_to_tokens(token_ids); + if (skip_special_tokens) { + tokens = tokens.filter((x) => !this.special_tokens.includes(x)); + } + let decoded = this.decoder ? this.decoder(tokens) : tokens.join(" "); + if (this.decoder && this.decoder.end_of_word_suffix) { + decoded = decoded.replaceAll(this.decoder.end_of_word_suffix, " "); + if (skip_special_tokens) { + decoded = decoded.trim(); + } + } + if (clean_up_tokenization_spaces ?? this.clean_up_tokenization_spaces) { + decoded = clean_up_tokenization(decoded); + } + return decoded; + } + /** + * Retrieve the chat template string used for tokenizing chat messages. This template is used + * internally by the `apply_chat_template` method and can also be used externally to retrieve the model's chat + * template for better generation tracking. + * + * @param {Object} options An optional object containing the following properties: + * @param {string} [options.chat_template=null] + * A Jinja template or the name of a template to use for this conversion. + * It is usually not necessary to pass anything to this argument, + * as the model's template will be used by default. + * @param {Object[]} [options.tools=null] + * A list of tools (callable functions) that will be accessible to the model. If the template does not + * support function calling, this argument will have no effect. Each tool should be passed as a JSON Schema, + * giving the name, description and argument types for the tool. See our + * [chat templating guide](https://huggingface.co/docs/transformers/main/en/chat_templating#automated-function-conversion-for-tool-use) + * for more information. + * @returns {string} The chat template string. + */ + get_chat_template({ + chat_template = null, + tools = null + } = {}) { + if (this.chat_template && typeof this.chat_template === "object") { + const template_dict = this.chat_template; + if (chat_template !== null && Object.hasOwn(template_dict, chat_template)) { + chat_template = template_dict[chat_template]; + } else if (chat_template === null) { + if (tools !== null && "tool_use" in template_dict) { + chat_template = template_dict["tool_use"]; + } else if ("default" in template_dict) { + chat_template = template_dict["default"]; + } else { + throw Error( + `This model has multiple chat templates with no default specified! Please either pass a chat template or the name of the template you wish to use to the 'chat_template' argument. Available template names are ${Object.keys(template_dict).sort()}.` + ); + } + } + } else if (chat_template === null) { + if (this.chat_template) { + chat_template = this.chat_template; + } else { + throw Error( + "Cannot use apply_chat_template() because tokenizer.chat_template is not set and no template argument was passed! For information about writing templates and setting the tokenizer.chat_template attribute, please see the documentation at https://huggingface.co/docs/transformers/main/en/chat_templating" + ); + } + } + return chat_template; + } + /** + * Converts a list of message objects with `"role"` and `"content"` keys to a list of token + * ids. This method is intended for use with chat models, and will read the tokenizer's chat_template attribute to + * determine the format and control tokens to use when converting. + * + * See [here](https://huggingface.co/docs/transformers/chat_templating) for more information. + * + * **Example:** Applying a chat template to a conversation. + * + * ```javascript + * import { AutoTokenizer } from "@huggingface/transformers"; + * + * const tokenizer = await AutoTokenizer.from_pretrained("Xenova/mistral-tokenizer-v1"); + * + * const chat = [ + * { "role": "user", "content": "Hello, how are you?" }, + * { "role": "assistant", "content": "I'm doing great. How can I help you today?" }, + * { "role": "user", "content": "I'd like to show off how chat templating works!" }, + * ] + * + * const text = tokenizer.apply_chat_template(chat, { tokenize: false }); + * // "[INST] Hello, how are you? [/INST]I'm doing great. How can I help you today? [INST] I'd like to show off how chat templating works! [/INST]" + * + * const input_ids = tokenizer.apply_chat_template(chat, { tokenize: true, return_tensor: false }); + * // [1, 733, 16289, 28793, 22557, 28725, 910, 460, 368, 28804, 733, 28748, 16289, 28793, 28737, 28742, 28719, 2548, 1598, 28723, 1602, 541, 315, 1316, 368, 3154, 28804, 2, 28705, 733, 16289, 28793, 315, 28742, 28715, 737, 298, 1347, 805, 910, 10706, 5752, 1077, 3791, 28808, 733, 28748, 16289, 28793] + * ``` + * + * @param {Message[]} conversation A list of message objects with `"role"` and `"content"` keys, + * representing the chat history so far. + * @param {Object} options An optional object containing the following properties: + * @param {string} [options.chat_template=null] A Jinja template to use for this conversion. If + * this is not passed, the model's chat template will be used instead. + * @param {Object[]} [options.tools=null] + * A list of tools (callable functions) that will be accessible to the model. If the template does not + * support function calling, this argument will have no effect. Each tool should be passed as a JSON Schema, + * giving the name, description and argument types for the tool. See our + * [chat templating guide](https://huggingface.co/docs/transformers/main/en/chat_templating#automated-function-conversion-for-tool-use) + * for more information. + * @param {Record[]} [options.documents=null] + * A list of dicts representing documents that will be accessible to the model if it is performing RAG + * (retrieval-augmented generation). If the template does not support RAG, this argument will have no + * effect. We recommend that each document should be a dict containing "title" and "text" keys. Please + * see the RAG section of the [chat templating guide](https://huggingface.co/docs/transformers/main/en/chat_templating#arguments-for-RAG) + * for examples of passing documents with chat templates. + * @param {boolean} [options.add_generation_prompt=false] Whether to end the prompt with the token(s) that indicate + * the start of an assistant message. This is useful when you want to generate a response from the model. + * Note that this argument will be passed to the chat template, and so it must be supported in the + * template for this argument to have any effect. + * @param {boolean} [options.tokenize=true] Whether to tokenize the output. If false, the output will be a string. + * @param {boolean} [options.padding=false] Whether to pad sequences to the maximum length. Has no effect if tokenize is false. + * @param {boolean} [options.truncation=false] Whether to truncate sequences to the maximum length. Has no effect if tokenize is false. + * @param {number} [options.max_length=null] Maximum length (in tokens) to use for padding or truncation. Has no effect if tokenize is false. + * If not specified, the tokenizer's `max_length` attribute will be used as a default. + * @param {boolean} [options.return_tensor=true] Whether to return the output as a Tensor or an Array. Has no effect if tokenize is false. + * @param {boolean} [options.return_dict=true] Whether to return a dictionary with named outputs. Has no effect if tokenize is false. + * @param {Object} [options.tokenizer_kwargs={}] Additional options to pass to the tokenizer. + * @returns {string | Tensor | number[]| number[][]|BatchEncoding} The tokenized output. + */ + apply_chat_template(conversation, { + tools = null, + documents = null, + chat_template = null, + add_generation_prompt = false, + tokenize = true, + padding = false, + truncation = false, + max_length = null, + return_tensor = true, + return_dict = false, + tokenizer_kwargs = {}, + ...kwargs + } = {}) { + chat_template = this.get_chat_template({ chat_template, tools }); + if (typeof chat_template !== "string") { + throw Error(`chat_template must be a string, but got ${typeof chat_template}`); + } + let compiledTemplate = this._compiled_template_cache.get(chat_template); + if (compiledTemplate === void 0) { + compiledTemplate = new _huggingface_jinja__WEBPACK_IMPORTED_MODULE_6__.Template(chat_template); + this._compiled_template_cache.set(chat_template, compiledTemplate); + } + const special_tokens_map = /* @__PURE__ */ Object.create(null); + for (const key of SPECIAL_TOKEN_ATTRIBUTES) { + const value = this.getToken(key); + if (value) { + special_tokens_map[key] = value; + } + } + const rendered = compiledTemplate.render({ + messages: conversation, + add_generation_prompt, + tools, + documents, + ...special_tokens_map, + ...kwargs + }); + if (tokenize) { + const out = this._call(rendered, { + add_special_tokens: false, + padding, + truncation, + max_length, + return_tensor, + ...tokenizer_kwargs + }); + return return_dict ? out : out.input_ids; + } + return rendered; + } + } + class BertTokenizer extends PreTrainedTokenizer { + constructor() { + super(...arguments); + __publicField(this, "return_token_type_ids", true); + } + } + class AlbertTokenizer extends PreTrainedTokenizer { + constructor() { + super(...arguments); + __publicField(this, "return_token_type_ids", true); + } + } + class MobileBertTokenizer extends PreTrainedTokenizer { + constructor() { + super(...arguments); + __publicField(this, "return_token_type_ids", true); + } + } + class SqueezeBertTokenizer extends PreTrainedTokenizer { + constructor() { + super(...arguments); + __publicField(this, "return_token_type_ids", true); + } + } + class DebertaTokenizer extends PreTrainedTokenizer { + constructor() { + super(...arguments); + __publicField(this, "return_token_type_ids", true); + } + } + class DebertaV2Tokenizer extends PreTrainedTokenizer { + constructor() { + super(...arguments); + __publicField(this, "return_token_type_ids", true); + } + } + class HerbertTokenizer extends PreTrainedTokenizer { + constructor() { + super(...arguments); + __publicField(this, "return_token_type_ids", true); + } + } + class ConvBertTokenizer extends PreTrainedTokenizer { + constructor() { + super(...arguments); + __publicField(this, "return_token_type_ids", true); + } + } + class RoFormerTokenizer extends PreTrainedTokenizer { + constructor() { + super(...arguments); + __publicField(this, "return_token_type_ids", true); + } + } + class DistilBertTokenizer extends PreTrainedTokenizer { + } + class CamembertTokenizer extends PreTrainedTokenizer { + } + class XLMTokenizer extends PreTrainedTokenizer { + constructor(tokenizerJSON, tokenizerConfig) { + super(tokenizerJSON, tokenizerConfig); + __publicField(this, "return_token_type_ids", true); + console.warn('WARNING: `XLMTokenizer` is not yet supported by Hugging Face\'s "fast" tokenizers library. Therefore, you may experience slightly inaccurate results.'); + } + } + class ElectraTokenizer extends PreTrainedTokenizer { + constructor() { + super(...arguments); + __publicField(this, "return_token_type_ids", true); + } + } + class T5Tokenizer extends PreTrainedTokenizer { + } + class GPT2Tokenizer extends PreTrainedTokenizer { + } + class BartTokenizer extends PreTrainedTokenizer { + } + class MBartTokenizer extends PreTrainedTokenizer { + constructor(tokenizerJSON, tokenizerConfig) { + super(tokenizerJSON, tokenizerConfig); + this.languageRegex = /^[a-z]{2}_[A-Z]{2}$/; + this.language_codes = this.special_tokens.filter((x) => this.languageRegex.test(x)); + this.lang_to_token = (x) => x; + } + /** + * Helper function to build translation inputs for an `MBartTokenizer`. + * @param {string|string[]} raw_inputs The text to tokenize. + * @param {Object} tokenizer_options Options to be sent to the tokenizer + * @param {Object} generate_kwargs Generation options. + * @returns {Object} Object to be passed to the model. + */ + _build_translation_inputs(raw_inputs, tokenizer_options, generate_kwargs) { + return _build_translation_inputs(this, raw_inputs, tokenizer_options, generate_kwargs); + } + } + class MBart50Tokenizer extends MBartTokenizer { + } + class RobertaTokenizer extends PreTrainedTokenizer { + } + class BloomTokenizer extends PreTrainedTokenizer { + } + const SPIECE_UNDERLINE = "▁"; + class LlamaTokenizer extends PreTrainedTokenizer { + constructor(tokenizerJSON, tokenizerConfig) { + super(tokenizerJSON, tokenizerConfig); + __publicField(this, "padding_side", "left"); + this.legacy = tokenizerConfig.legacy ?? true; + if (!this.legacy) { + this.normalizer = null; + this.pre_tokenizer = new MetaspacePreTokenizer({ + replacement: SPIECE_UNDERLINE, + add_prefix_space: true, + prepend_scheme: "first" + }); + } + } + /** + * Helper function to handle legacy encoding of SPM tokenizers. + * Adapted from https://github.com/huggingface/transformers/blob/e6dcf8abd6f65bb4b6dfc1831b20d9ba49ce00e2/src/transformers/models/t5/tokenization_t5.py#L374-L387 + * @param {string} text The text to encode. + * @returns {string[]} The encoded tokens. + */ + _encode_text(text) { + if (text === null) return null; + if (this.legacy || text.length === 0) { + return super._encode_text(text); + } + let tokens = super._encode_text(SPIECE_UNDERLINE + text.replaceAll(SPIECE_UNDERLINE, " ")); + if (tokens.length > 1 && tokens[0] === SPIECE_UNDERLINE && this.special_tokens.includes(tokens[1])) { + tokens = tokens.slice(1); + } + return tokens; + } + } + class CodeLlamaTokenizer extends PreTrainedTokenizer { + } + class XLMRobertaTokenizer extends PreTrainedTokenizer { + } + class MPNetTokenizer extends PreTrainedTokenizer { + } + class FalconTokenizer extends PreTrainedTokenizer { + } + class GPTNeoXTokenizer extends PreTrainedTokenizer { + } + class EsmTokenizer extends PreTrainedTokenizer { + } + class Qwen2Tokenizer extends PreTrainedTokenizer { + } + class GemmaTokenizer extends PreTrainedTokenizer { + } + class Grok1Tokenizer extends PreTrainedTokenizer { + } + function _build_translation_inputs(self2, raw_inputs, tokenizer_options, generate_kwargs) { + if (!("language_codes" in self2) || !Array.isArray(self2.language_codes)) { + throw new Error("Tokenizer must have `language_codes` attribute set and it should be an array of language ids."); + } + if (!("languageRegex" in self2) || !(self2.languageRegex instanceof RegExp)) { + throw new Error("Tokenizer must have `languageRegex` attribute set and it should be a regular expression."); + } + if (!("lang_to_token" in self2) || typeof self2.lang_to_token !== "function") { + throw new Error("Tokenizer must have `lang_to_token` attribute set and it should be a function."); + } + const src_lang_token = generate_kwargs.src_lang; + const tgt_lang_token = generate_kwargs.tgt_lang; + if (!self2.language_codes.includes(tgt_lang_token)) { + throw new Error(`Target language code "${tgt_lang_token}" is not valid. Must be one of: {${self2.language_codes.join(", ")}}`); + } + if (src_lang_token !== void 0) { + if (!self2.language_codes.includes(src_lang_token)) { + throw new Error(`Source language code "${src_lang_token}" is not valid. Must be one of: {${self2.language_codes.join(", ")}}`); + } + for (const item of self2.post_processor.config.single) { + if ("SpecialToken" in item && self2.languageRegex.test(item.SpecialToken.id)) { + item.SpecialToken.id = self2.lang_to_token(src_lang_token); + break; + } + } + } + generate_kwargs.forced_bos_token_id = self2.model.convert_tokens_to_ids([self2.lang_to_token(tgt_lang_token)])[0]; + return self2._call(raw_inputs, tokenizer_options); + } + class NllbTokenizer extends PreTrainedTokenizer { + constructor(tokenizerJSON, tokenizerConfig) { + super(tokenizerJSON, tokenizerConfig); + this.languageRegex = /^[a-z]{3}_[A-Z][a-z]{3}$/; + this.language_codes = this.special_tokens.filter((x) => this.languageRegex.test(x)); + this.lang_to_token = (x) => x; + } + /** + * Helper function to build translation inputs for an `NllbTokenizer`. + * @param {string|string[]} raw_inputs The text to tokenize. + * @param {Object} tokenizer_options Options to be sent to the tokenizer + * @param {Object} generate_kwargs Generation options. + * @returns {Object} Object to be passed to the model. + */ + _build_translation_inputs(raw_inputs, tokenizer_options, generate_kwargs) { + return _build_translation_inputs(this, raw_inputs, tokenizer_options, generate_kwargs); + } + } + class M2M100Tokenizer extends PreTrainedTokenizer { + constructor(tokenizerJSON, tokenizerConfig) { + super(tokenizerJSON, tokenizerConfig); + this.languageRegex = /^__[a-z]{2,3}__$/; + this.language_codes = this.special_tokens.filter((x) => this.languageRegex.test(x)).map((x) => x.slice(2, -2)); + this.lang_to_token = (x) => `__${x}__`; + } + /** + * Helper function to build translation inputs for an `M2M100Tokenizer`. + * @param {string|string[]} raw_inputs The text to tokenize. + * @param {Object} tokenizer_options Options to be sent to the tokenizer + * @param {Object} generate_kwargs Generation options. + * @returns {Object} Object to be passed to the model. + */ + _build_translation_inputs(raw_inputs, tokenizer_options, generate_kwargs) { + return _build_translation_inputs(this, raw_inputs, tokenizer_options, generate_kwargs); + } + } + class WhisperTokenizer extends PreTrainedTokenizer { + get timestamp_begin() { + return this.model.convert_tokens_to_ids(["<|notimestamps|>"])[0] + 1; + } + /** + * Decodes automatic speech recognition (ASR) sequences. + * @param {Array<{tokens: bigint[], token_timestamps?: number[], stride: number[]}>} sequences The sequences to decode. + * @param {Object} options The options to use for decoding. + * @returns {Array, text: string}>}>} The decoded sequences. + */ + _decode_asr(sequences, { + return_timestamps = false, + return_language = false, + time_precision = null, + force_full_sequences = true + } = {}) { + if (time_precision === null) { + throw Error("Must specify time_precision"); + } + let last_language = null; + const returnWordTimestamps = return_timestamps === "word"; + function new_chunk() { + return { "language": last_language, "timestamp": [null, null], "text": "" }; + } + const chunks = []; + let chunk = new_chunk(); + let time_offset = 0; + const timestamp_begin = this.timestamp_begin; + let previous_tokens = []; + let previous_token_timestamps = []; + let skip = false; + let right_stride_start = null; + const all_special_ids = new Set(this.all_special_ids); + for (const output of sequences) { + const token_ids = output.tokens; + const token_timestamps = returnWordTimestamps ? output.token_timestamps : null; + let last_timestamp = null; + let first_timestamp = timestamp_begin; + if ("stride" in output) { + const [chunk_len, stride_left, stride_right] = output.stride; + time_offset -= stride_left; + right_stride_start = chunk_len - stride_right; + if (stride_left) { + first_timestamp = stride_left / time_precision + timestamp_begin; + } + if (stride_right) { + for (let i = token_ids.length - 1; i >= 0; --i) { + const token = Number(token_ids[i]); + if (token >= timestamp_begin) { + if (last_timestamp !== null && (token - timestamp_begin) * time_precision < right_stride_start) { + break; + } + last_timestamp = token; + } + } + } + } + let current_tokens = []; + let current_token_timestamps = []; + for (let i = 0; i < token_ids.length; ++i) { + const token = Number(token_ids[i]); + if (all_special_ids.has(token)) { + const text = this.decode([token]); + const language = _models_whisper_common_whisper_js__WEBPACK_IMPORTED_MODULE_7__.WHISPER_LANGUAGE_MAPPING.get(text.slice(2, -2)); + if (language !== void 0) { + if (last_language !== null && language !== last_language && !return_timestamps) { + previous_tokens.push(current_tokens); + const resolved_tokens = this.findLongestCommonSequence(previous_tokens)[0]; + const resolved_text = this.decode(resolved_tokens); + chunk.text = resolved_text; + chunks.push(chunk); + previous_tokens = []; + current_tokens = []; + chunk = new_chunk(); + } + last_language = chunk.language = language; + } else { + } + } else if (token >= timestamp_begin) { + const time = (token - timestamp_begin) * time_precision + time_offset; + const rounded_time = (0, _utils_maths_js__WEBPACK_IMPORTED_MODULE_3__.round)(time, 2); + if (last_timestamp !== null && token >= last_timestamp) { + skip = true; + } else if (skip || previous_tokens.length > 0 && token < first_timestamp) { + skip = false; + } else if (chunk.timestamp[0] === null) { + chunk.timestamp[0] = rounded_time; + } else { + if (rounded_time === chunk.timestamp[0]) { + } else { + chunk.timestamp[1] = rounded_time; + previous_tokens.push(current_tokens); + if (returnWordTimestamps) { + previous_token_timestamps.push(current_token_timestamps); + } + const [resolved_tokens, resolved_token_timestamps] = this.findLongestCommonSequence( + previous_tokens, + previous_token_timestamps + ); + const resolved_text = this.decode(resolved_tokens); + chunk.text = resolved_text; + if (returnWordTimestamps) { + chunk.words = this.collateWordTimestamps( + resolved_tokens, + resolved_token_timestamps, + last_language + ); + } + chunks.push(chunk); + previous_tokens = []; + current_tokens = []; + previous_token_timestamps = []; + current_token_timestamps = []; + chunk = new_chunk(); + } + } + } else { + current_tokens.push(token); + if (returnWordTimestamps) { + let start_time = (0, _utils_maths_js__WEBPACK_IMPORTED_MODULE_3__.round)(token_timestamps[i] + time_offset, 2); + let end_time; + if (i + 1 < token_timestamps.length) { + end_time = (0, _utils_maths_js__WEBPACK_IMPORTED_MODULE_3__.round)(token_timestamps[i + 1] + time_offset, 2); + const decoded_text = this.decode([token]); + if (PUNCTUATION_ONLY_REGEX.test(decoded_text)) { + end_time = (0, _utils_maths_js__WEBPACK_IMPORTED_MODULE_3__.round)(Math.min(start_time + time_precision, end_time), 2); + } + } else { + end_time = null; + } + current_token_timestamps.push([start_time, end_time]); + } + } + } + if ("stride" in output) { + const [chunk_len, stride_left, stride_right] = output.stride; + time_offset += chunk_len - stride_right; + } + if (current_tokens.length > 0) { + previous_tokens.push(current_tokens); + if (returnWordTimestamps) { + previous_token_timestamps.push(current_token_timestamps); + } + } else if (previous_tokens.every((p) => p.length === 0)) { + chunk = new_chunk(); + previous_tokens = []; + current_tokens = []; + previous_token_timestamps = []; + current_token_timestamps = []; + } + } + if (previous_tokens.length > 0) { + if (force_full_sequences && return_timestamps) { + throw new Error( + "Whisper did not predict an ending timestamp, which can happen if audio is cut off in the middle of a word. Also make sure WhisperTimeStampLogitsProcessor was used during generation." + ); + } + const [resolved_tokens, resolved_token_timestamps] = this.findLongestCommonSequence(previous_tokens, previous_token_timestamps); + const resolved_text = this.decode(resolved_tokens); + chunk.text = resolved_text; + if (returnWordTimestamps) { + chunk.words = this.collateWordTimestamps( + resolved_tokens, + resolved_token_timestamps, + last_language + ); + } + chunks.push(chunk); + } + let optional = /* @__PURE__ */ Object.create(null); + const full_text = chunks.map((chunk2) => chunk2.text).join(""); + if (return_timestamps || return_language) { + for (let i = 0; i < chunks.length; ++i) { + const chunk2 = chunks[i]; + if (!return_timestamps) { + delete chunk2["timestamp"]; + } + if (!return_language) { + delete chunk2["language"]; + } + } + if (returnWordTimestamps) { + const new_chunks = []; + for (const chunk2 of chunks) { + for (const word of chunk2.words) { + new_chunks.push(word); + } + } + optional = { "chunks": new_chunks }; + } else { + optional = { "chunks": chunks }; + } + } + return [full_text, optional]; + } + /** + * Finds the longest common sequence among the provided sequences. + * @param {number[][]} sequences An array of sequences of token ids to compare. + * @returns {number[][]} The longest common sequence found. + * @throws {Error} If there is a bug within the function. + * @private + */ + findLongestCommonSequence(sequences, token_timestamp_sequences = null) { + let leftSequence = sequences[0]; + let leftLength = leftSequence.length; + let totalSequence = []; + const use_token_timestamp_sequences = Array.isArray(token_timestamp_sequences) && token_timestamp_sequences.length > 0; + let total_token_timestamp_sequence = use_token_timestamp_sequences ? [] : null; + let left_token_timestamp_sequence = use_token_timestamp_sequences ? token_timestamp_sequences[0] : null; + for (let i = 1; i < sequences.length; ++i) { + const rightSequence = sequences[i]; + let max = 0; + let maxIndices = [leftLength, leftLength, 0, 0]; + const rightLength = rightSequence.length; + for (let j = 1; j < leftLength + rightLength; ++j) { + const leftStart2 = Math.max(0, leftLength - j); + const leftStop2 = Math.min(leftLength, leftLength + rightLength - j); + const left = leftSequence.slice(leftStart2, leftStop2); + const rightStart2 = Math.max(0, j - leftLength); + const rightStop2 = Math.min(rightLength, j); + const right = rightSequence.slice(rightStart2, rightStop2); + if (left.length !== right.length) { + throw new Error("There is a bug within whisper `decode_asr` function, please report it. Dropping to prevent bad inference."); + } + let matches; + if (use_token_timestamp_sequences) { + matches = left.filter((elem, idx) => elem === right[idx] && left_token_timestamp_sequence[leftStart2 + idx] <= token_timestamp_sequences[i][rightStart2 + idx]).length; + } else { + matches = left.filter((elem, idx) => elem === right[idx]).length; + } + const eps = j / 1e4; + const matching = matches / j + eps; + if (matches > 1 && matching > max) { + max = matching; + maxIndices = [leftStart2, leftStop2, rightStart2, rightStop2]; + } + } + const [leftStart, leftStop, rightStart, rightStop] = maxIndices; + const leftMid = Math.floor((leftStop + leftStart) / 2); + const rightMid = Math.floor((rightStop + rightStart) / 2); + totalSequence.push(...leftSequence.slice(0, leftMid)); + leftSequence = rightSequence.slice(rightMid); + leftLength = leftSequence.length; + if (use_token_timestamp_sequences) { + total_token_timestamp_sequence.push(...left_token_timestamp_sequence.slice(0, leftMid)); + left_token_timestamp_sequence = token_timestamp_sequences[i].slice(rightMid); + } + } + totalSequence.push(...leftSequence); + if (use_token_timestamp_sequences) { + total_token_timestamp_sequence.push(...left_token_timestamp_sequence); + return [totalSequence, total_token_timestamp_sequence]; + } else { + return [totalSequence, []]; + } + } + /** @private */ + collateWordTimestamps(tokens, token_timestamps, language) { + const [words, _, token_indices] = this.combineTokensIntoWords(tokens, language); + const timings = []; + for (let i = 0; i < words.length; ++i) { + const indices = token_indices[i]; + timings.push({ + text: words[i], + timestamp: [ + token_timestamps[indices.at(0)][0], + token_timestamps[indices.at(-1)][1] + ] + }); + } + return timings; + } + /** + * Groups tokens by word. Returns a tuple containing a list of strings with the words, + * and a list of `token_id` sequences with the tokens making up each word. + * @param {number[]} tokens + * @param {string} [language] + * @param {string} prepend_punctionations + * @param {string} append_punctuations + * + * @private + */ + combineTokensIntoWords(tokens, language, prepend_punctionations = `"'“¡¿([{-`, append_punctuations = `"'.。,,!!??::”)]}、`) { + language = language ?? "english"; + let words, word_tokens, token_indices; + if (["chinese", "japanese", "thai", "lao", "myanmar"].includes(language)) { + [words, word_tokens, token_indices] = this.splitTokensOnUnicode(tokens); + } else { + [words, word_tokens, token_indices] = this.splitTokensOnSpaces(tokens); + } + return this.mergePunctuations(words, word_tokens, token_indices, prepend_punctionations, append_punctuations); + } + /** @type {PreTrainedTokenizer['decode']} */ + decode(token_ids, decode_args) { + let text; + if (decode_args == null ? void 0 : decode_args.decode_with_timestamps) { + if (token_ids instanceof _utils_tensor_js__WEBPACK_IMPORTED_MODULE_4__.Tensor) { + token_ids = prepareTensorForDecode(token_ids); + } + text = this.decodeWithTimestamps(token_ids, decode_args); + } else { + text = super.decode(token_ids, decode_args); + } + return text; + } + /** + * @param {number[]|bigint[]} token_ids List of token IDs to decode. + * @param {Object} decode_args Optional arguments for decoding + * @private + */ + decodeWithTimestamps(token_ids, decode_args) { + const time_precision = (decode_args == null ? void 0 : decode_args.time_precision) ?? 0.02; + const timestamp_begin = Array.from(this.all_special_ids).at(-1) + 1; + let outputs = [[]]; + for (let token of token_ids) { + token = Number(token); + if (token >= timestamp_begin) { + const timestamp = ((token - timestamp_begin) * time_precision).toFixed(2); + outputs.push(`<|${timestamp}|>`); + outputs.push([]); + } else { + outputs[outputs.length - 1].push(token); + } + } + outputs = outputs.map( + (s) => typeof s === "string" ? s : super.decode(s, decode_args) + ); + return outputs.join(""); + } + /** + * Combine tokens into words by splitting at any position where the tokens are decoded as valid unicode points. + * @param {number[]} tokens + * @returns {*} + * @private + */ + splitTokensOnUnicode(tokens) { + const decoded_full = this.decode(tokens, { + // @ts-ignore + decode_with_timestamps: true + }); + const replacement_char = "�"; + const words = []; + const word_tokens = []; + const token_indices = []; + let current_tokens = []; + let current_indices = []; + let unicode_offset = 0; + for (let token_idx = 0; token_idx < tokens.length; ++token_idx) { + const token = tokens[token_idx]; + current_tokens.push(token); + current_indices.push(token_idx); + const decoded = this.decode(current_tokens, { + // @ts-ignore + decode_with_timestamps: true + }); + if (!decoded.includes(replacement_char) || decoded_full[unicode_offset + decoded.indexOf(replacement_char)] === replacement_char) { + words.push(decoded); + word_tokens.push(current_tokens); + token_indices.push(current_indices); + current_tokens = []; + current_indices = []; + unicode_offset += decoded.length; + } + } + return [words, word_tokens, token_indices]; + } + /** + * Combine tokens into words by splitting at whitespace and punctuation tokens. + * @param {number[]} tokens + * @private + */ + splitTokensOnSpaces(tokens) { + const [subwords, subword_tokens_list, subword_indices_list] = this.splitTokensOnUnicode(tokens); + const words = []; + const word_tokens = []; + const token_indices = []; + const punctuationRegex = new RegExp(`^[${PUNCTUATION_REGEX}]$`, "gu"); + for (let i = 0; i < subwords.length; ++i) { + const subword = subwords[i]; + const subword_tokens = subword_tokens_list[i]; + const subword_indices = subword_indices_list[i]; + const special = subword_tokens[0] >= this.model.tokens_to_ids.get("<|endoftext|>"); + const with_space = subword.startsWith(" "); + const trimmed = subword.trim(); + const punctuation = punctuationRegex.test(trimmed); + if (special || with_space || punctuation || words.length === 0) { + words.push(subword); + word_tokens.push(subword_tokens); + token_indices.push(subword_indices); + } else { + const ix = words.length - 1; + words[ix] += subword; + word_tokens[ix].push(...subword_tokens); + token_indices[ix].push(...subword_indices); + } + } + return [words, word_tokens, token_indices]; + } + /** + * Merges punctuation tokens with neighboring words. + * @param {string[]} words + * @param {number[][]} tokens + * @param {number[][]} indices + * @param {string} prepended + * @param {string} appended + * @private + */ + mergePunctuations(words, tokens, indices, prepended, appended) { + const newWords = structuredClone(words); + const newTokens = structuredClone(tokens); + const newIndices = structuredClone(indices); + let i = newWords.length - 2; + let j = newWords.length - 1; + while (i >= 0) { + if (newWords[i].startsWith(" ") && prepended.includes(newWords[i].trim())) { + newWords[j] = newWords[i] + newWords[j]; + newTokens[j] = (0, _utils_core_js__WEBPACK_IMPORTED_MODULE_1__.mergeArrays)(newTokens[i], newTokens[j]); + newIndices[j] = (0, _utils_core_js__WEBPACK_IMPORTED_MODULE_1__.mergeArrays)(newIndices[i], newIndices[j]); + newWords[i] = ""; + newTokens[i] = []; + newIndices[i] = []; + } else { + j = i; + } + --i; + } + i = 0; + j = 1; + while (j < newWords.length) { + if (!newWords[i].endsWith(" ") && appended.includes(newWords[j])) { + newWords[i] += newWords[j]; + newTokens[i] = (0, _utils_core_js__WEBPACK_IMPORTED_MODULE_1__.mergeArrays)(newTokens[i], newTokens[j]); + newIndices[i] = (0, _utils_core_js__WEBPACK_IMPORTED_MODULE_1__.mergeArrays)(newIndices[i], newIndices[j]); + newWords[j] = ""; + newTokens[j] = []; + newIndices[j] = []; + } else { + i = j; + } + ++j; + } + return [ + newWords.filter((x) => x), + newTokens.filter((x) => x.length > 0), + newIndices.filter((x) => x.length > 0) + ]; + } + } + class CodeGenTokenizer extends PreTrainedTokenizer { + } + class CLIPTokenizer extends PreTrainedTokenizer { + } + class SiglipTokenizer extends PreTrainedTokenizer { + } + class MarianTokenizer extends PreTrainedTokenizer { + /** + * Create a new MarianTokenizer instance. + * @param {Object} tokenizerJSON The JSON of the tokenizer. + * @param {Object} tokenizerConfig The config of the tokenizer. + */ + constructor(tokenizerJSON, tokenizerConfig) { + super(tokenizerJSON, tokenizerConfig); + this.languageRegex = /^(>>\w+<<)\s*/g; + this.supported_language_codes = this.model.vocab.filter( + (x) => this.languageRegex.test(x) + ); + console.warn('WARNING: `MarianTokenizer` is not yet supported by Hugging Face\'s "fast" tokenizers library. Therefore, you may experience slightly inaccurate results.'); + } + /** + * Encodes a single text. Overriding this method is necessary since the language codes + * must be removed before encoding with sentencepiece model. + * @see https://github.com/huggingface/transformers/blob/12d51db243a00726a548a43cc333390ebae731e3/src/transformers/models/marian/tokenization_marian.py#L204-L213 + * + * @param {string|null} text The text to encode. + * @returns {Array} The encoded tokens. + */ + _encode_text(text) { + if (text === null) return null; + const [matchInfo, ...remainder] = text.trim().split(this.languageRegex); + if (remainder.length === 0) { + return super._encode_text(matchInfo); + } else if (remainder.length === 2) { + const [language, text2] = remainder; + if (!this.supported_language_codes.includes(language)) { + console.warn(`Unsupported language code "${language}" detected, which may lead to unexpected behavior. Should be one of: ${JSON.stringify(this.supported_language_codes)}`); + } + return (0, _utils_core_js__WEBPACK_IMPORTED_MODULE_1__.mergeArrays)([language], super._encode_text(text2)); + } + } + } + class Wav2Vec2CTCTokenizer extends PreTrainedTokenizer { + } + class BlenderbotTokenizer extends PreTrainedTokenizer { + } + class BlenderbotSmallTokenizer extends PreTrainedTokenizer { + } + class SpeechT5Tokenizer extends PreTrainedTokenizer { + } + class NougatTokenizer extends PreTrainedTokenizer { + } + class VitsTokenizer extends PreTrainedTokenizer { + constructor(tokenizerJSON, tokenizerConfig) { + super(tokenizerJSON, tokenizerConfig); + this.decoder = new VitsDecoder({}); + } + } + class CohereTokenizer extends PreTrainedTokenizer { + } + class AutoTokenizer { + /** + * Instantiate one of the tokenizer classes of the library from a pretrained model. + * + * The tokenizer class to instantiate is selected based on the `tokenizer_class` property of the config object + * (either passed as an argument or loaded from `pretrained_model_name_or_path` if possible) + * + * @param {string} pretrained_model_name_or_path The name or path of the pretrained model. Can be either: + * - A string, the *model id* of a pretrained tokenizer hosted inside a model repo on huggingface.co. + * Valid model ids can be located at the root-level, like `bert-base-uncased`, or namespaced under a + * user or organization name, like `dbmdz/bert-base-german-cased`. + * - A path to a *directory* containing tokenizer files, e.g., `./my_model_directory/`. + * @param {PretrainedTokenizerOptions} options Additional options for loading the tokenizer. + * + * @returns {Promise} A new instance of the PreTrainedTokenizer class. + */ + static async from_pretrained(pretrained_model_name_or_path, { + progress_callback = null, + config = null, + cache_dir = null, + local_files_only = false, + revision = "main", + legacy = null + } = {}) { + var _a; + const [tokenizerJSON, tokenizerConfig] = await loadTokenizer(pretrained_model_name_or_path, { + progress_callback, + config, + cache_dir, + local_files_only, + revision, + legacy + }); + const tokenizerName = ((_a = tokenizerConfig.tokenizer_class) == null ? void 0 : _a.replace(/Fast$/, "")) ?? "PreTrainedTokenizer"; + let cls = this.TOKENIZER_CLASS_MAPPING[tokenizerName]; + if (!cls) { + console.warn(`Unknown tokenizer class "${tokenizerName}", attempting to construct from base class.`); + cls = PreTrainedTokenizer; + } + return new cls(tokenizerJSON, tokenizerConfig); + } + } + __publicField(AutoTokenizer, "TOKENIZER_CLASS_MAPPING", { + T5Tokenizer, + DistilBertTokenizer, + CamembertTokenizer, + DebertaTokenizer, + DebertaV2Tokenizer, + BertTokenizer, + HerbertTokenizer, + ConvBertTokenizer, + RoFormerTokenizer, + XLMTokenizer, + ElectraTokenizer, + MobileBertTokenizer, + SqueezeBertTokenizer, + AlbertTokenizer, + GPT2Tokenizer, + BartTokenizer, + MBartTokenizer, + MBart50Tokenizer, + RobertaTokenizer, + WhisperTokenizer, + CodeGenTokenizer, + CLIPTokenizer, + SiglipTokenizer, + MarianTokenizer, + BloomTokenizer, + NllbTokenizer, + M2M100Tokenizer, + LlamaTokenizer, + CodeLlamaTokenizer, + XLMRobertaTokenizer, + MPNetTokenizer, + FalconTokenizer, + GPTNeoXTokenizer, + EsmTokenizer, + Wav2Vec2CTCTokenizer, + BlenderbotTokenizer, + BlenderbotSmallTokenizer, + SpeechT5Tokenizer, + NougatTokenizer, + VitsTokenizer, + Qwen2Tokenizer, + GemmaTokenizer, + Grok1Tokenizer, + CohereTokenizer, + // Base case: + PreTrainedTokenizer + }); + } + ), + /***/ + "./src/utils/audio.js": ( + /*!****************************!*\ + !*** ./src/utils/audio.js ***! + \****************************/ + /***/ + (__unused_webpack___webpack_module__, __webpack_exports__2, __webpack_require__2) => { + __webpack_require__2.r(__webpack_exports__2); + __webpack_require__2.d(__webpack_exports__2, { + /* harmony export */ + hamming: () => ( + /* binding */ + hamming + ), + /* harmony export */ + hanning: () => ( + /* binding */ + hanning + ), + /* harmony export */ + mel_filter_bank: () => ( + /* binding */ + mel_filter_bank + ), + /* harmony export */ + read_audio: () => ( + /* binding */ + read_audio + ), + /* harmony export */ + spectrogram: () => ( + /* binding */ + spectrogram + ), + /* harmony export */ + window_function: () => ( + /* binding */ + window_function + ) + /* harmony export */ + }); + var _hub_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__2( + /*! ./hub.js */ + "./src/utils/hub.js" + ); + var _maths_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__2( + /*! ./maths.js */ + "./src/utils/maths.js" + ); + var _core_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__2( + /*! ./core.js */ + "./src/utils/core.js" + ); + var _tensor_js__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__2( + /*! ./tensor.js */ + "./src/utils/tensor.js" + ); + async function read_audio(url, sampling_rate) { + if (typeof AudioContext === "undefined") { + throw Error( + "Unable to load audio from path/URL since `AudioContext` is not available in your environment. Instead, audio data should be passed directly to the pipeline/processor. For more information and some example code, see https://huggingface.co/docs/transformers.js/guides/node-audio-processing." + ); + } + const response = await (await (0, _hub_js__WEBPACK_IMPORTED_MODULE_0__.getFile)(url)).arrayBuffer(); + const audioCTX = new AudioContext({ sampleRate: sampling_rate }); + if (typeof sampling_rate === "undefined") { + console.warn(`No sampling rate provided, using default of ${audioCTX.sampleRate}Hz.`); + } + const decoded = await audioCTX.decodeAudioData(response); + let audio; + if (decoded.numberOfChannels === 2) { + const SCALING_FACTOR = Math.sqrt(2); + const left = decoded.getChannelData(0); + const right = decoded.getChannelData(1); + audio = new Float32Array(left.length); + for (let i = 0; i < decoded.length; ++i) { + audio[i] = SCALING_FACTOR * (left[i] + right[i]) / 2; + } + } else { + audio = decoded.getChannelData(0); + } + return audio; + } + function generalized_cosine_window(M, a_0) { + if (M < 1) { + return new Float64Array(); + } + if (M === 1) { + return new Float64Array([1]); + } + const a_1 = 1 - a_0; + const factor = 2 * Math.PI / (M - 1); + const cos_vals = new Float64Array(M); + for (let i = 0; i < M; ++i) { + cos_vals[i] = a_0 - a_1 * Math.cos(i * factor); + } + return cos_vals; + } + function hanning(M) { + return generalized_cosine_window(M, 0.5); + } + function hamming(M) { + return generalized_cosine_window(M, 0.54); + } + const HERTZ_TO_MEL_MAPPING = { + "htk": (freq) => 2595 * Math.log10(1 + freq / 700), + "kaldi": (freq) => 1127 * Math.log(1 + freq / 700), + "slaney": (freq, min_log_hertz = 1e3, min_log_mel = 15, logstep = 27 / Math.log(6.4)) => freq >= min_log_hertz ? min_log_mel + Math.log(freq / min_log_hertz) * logstep : 3 * freq / 200 + }; + function hertz_to_mel(freq, mel_scale = "htk") { + const fn = HERTZ_TO_MEL_MAPPING[mel_scale]; + if (!fn) { + throw new Error('mel_scale should be one of "htk", "slaney" or "kaldi".'); + } + return typeof freq === "number" ? fn(freq) : freq.map((x) => fn(x)); + } + const MEL_TO_HERTZ_MAPPING = { + "htk": (mels) => 700 * (10 ** (mels / 2595) - 1), + "kaldi": (mels) => 700 * (Math.exp(mels / 1127) - 1), + "slaney": (mels, min_log_hertz = 1e3, min_log_mel = 15, logstep = Math.log(6.4) / 27) => mels >= min_log_mel ? min_log_hertz * Math.exp(logstep * (mels - min_log_mel)) : 200 * mels / 3 + }; + function mel_to_hertz(mels, mel_scale = "htk") { + const fn = MEL_TO_HERTZ_MAPPING[mel_scale]; + if (!fn) { + throw new Error('mel_scale should be one of "htk", "slaney" or "kaldi".'); + } + return typeof mels === "number" ? fn(mels) : mels.map((x) => fn(x)); + } + function _create_triangular_filter_bank(fft_freqs, filter_freqs) { + const filter_diff = Float64Array.from( + { length: filter_freqs.length - 1 }, + (_, i) => filter_freqs[i + 1] - filter_freqs[i] + ); + const slopes = Array.from({ + length: fft_freqs.length + }, () => new Array(filter_freqs.length)); + for (let j = 0; j < fft_freqs.length; ++j) { + const slope = slopes[j]; + for (let i = 0; i < filter_freqs.length; ++i) { + slope[i] = filter_freqs[i] - fft_freqs[j]; + } + } + const numFreqs = filter_freqs.length - 2; + const ret = Array.from({ length: numFreqs }, () => new Array(fft_freqs.length)); + for (let j = 0; j < fft_freqs.length; ++j) { + const slope = slopes[j]; + for (let i = 0; i < numFreqs; ++i) { + const down = -slope[i] / filter_diff[i]; + const up = slope[i + 2] / filter_diff[i + 1]; + ret[i][j] = Math.max(0, Math.min(down, up)); + } + } + return ret; + } + function linspace(start, end, num) { + const step = (end - start) / (num - 1); + return Float64Array.from({ length: num }, (_, i) => start + step * i); + } + function mel_filter_bank(num_frequency_bins, num_mel_filters, min_frequency, max_frequency, sampling_rate, norm = null, mel_scale = "htk", triangularize_in_mel_space = false) { + if (norm !== null && norm !== "slaney") { + throw new Error('norm must be one of null or "slaney"'); + } + const mel_min = hertz_to_mel(min_frequency, mel_scale); + const mel_max = hertz_to_mel(max_frequency, mel_scale); + const mel_freqs = linspace(mel_min, mel_max, num_mel_filters + 2); + let filter_freqs = mel_to_hertz(mel_freqs, mel_scale); + let fft_freqs; + if (triangularize_in_mel_space) { + const fft_bin_width = sampling_rate / (num_frequency_bins * 2); + fft_freqs = hertz_to_mel(Float64Array.from({ length: num_frequency_bins }, (_, i) => i * fft_bin_width), mel_scale); + filter_freqs = mel_freqs; + } else { + fft_freqs = linspace(0, Math.floor(sampling_rate / 2), num_frequency_bins); + } + const mel_filters = _create_triangular_filter_bank(fft_freqs, filter_freqs); + if (norm !== null && norm === "slaney") { + for (let i = 0; i < num_mel_filters; ++i) { + const filter = mel_filters[i]; + const enorm = 2 / (filter_freqs[i + 2] - filter_freqs[i]); + for (let j = 0; j < num_frequency_bins; ++j) { + filter[j] *= enorm; + } + } + } + return mel_filters; + } + function padReflect(array, left, right) { + const padded = new array.constructor(array.length + left + right); + const w = array.length - 1; + for (let i = 0; i < array.length; ++i) { + padded[left + i] = array[i]; + } + for (let i = 1; i <= left; ++i) { + padded[left - i] = array[(0, _core_js__WEBPACK_IMPORTED_MODULE_2__.calculateReflectOffset)(i, w)]; + } + for (let i = 1; i <= right; ++i) { + padded[w + left + i] = array[(0, _core_js__WEBPACK_IMPORTED_MODULE_2__.calculateReflectOffset)(w - i, w)]; + } + return padded; + } + function _db_conversion_helper(spectrogram2, factor, reference, min_value, db_range) { + if (reference <= 0) { + throw new Error("reference must be greater than zero"); + } + if (min_value <= 0) { + throw new Error("min_value must be greater than zero"); + } + reference = Math.max(min_value, reference); + const logReference = Math.log10(reference); + for (let i = 0; i < spectrogram2.length; ++i) { + spectrogram2[i] = factor * Math.log10(Math.max(min_value, spectrogram2[i]) - logReference); + } + if (db_range !== null) { + if (db_range <= 0) { + throw new Error("db_range must be greater than zero"); + } + const maxValue = (0, _maths_js__WEBPACK_IMPORTED_MODULE_1__.max)(spectrogram2)[0] - db_range; + for (let i = 0; i < spectrogram2.length; ++i) { + spectrogram2[i] = Math.max(spectrogram2[i], maxValue); + } + } + return spectrogram2; + } + function amplitude_to_db(spectrogram2, reference = 1, min_value = 1e-5, db_range = null) { + return _db_conversion_helper(spectrogram2, 20, reference, min_value, db_range); + } + function power_to_db(spectrogram2, reference = 1, min_value = 1e-10, db_range = null) { + return _db_conversion_helper(spectrogram2, 10, reference, min_value, db_range); + } + async function spectrogram(waveform, window2, frame_length, hop_length, { + fft_length = null, + power = 1, + center = true, + pad_mode = "reflect", + onesided = true, + preemphasis = null, + mel_filters = null, + mel_floor = 1e-10, + log_mel = null, + reference = 1, + min_value = 1e-10, + db_range = null, + remove_dc_offset = null, + // Custom parameters for efficiency reasons + min_num_frames = null, + max_num_frames = null, + do_pad = true, + transpose = false + } = {}) { + const window_length = window2.length; + if (fft_length === null) { + fft_length = frame_length; + } + if (frame_length > fft_length) { + throw Error(`frame_length (${frame_length}) may not be larger than fft_length (${fft_length})`); + } + if (window_length !== frame_length) { + throw new Error(`Length of the window (${window_length}) must equal frame_length (${frame_length})`); + } + if (hop_length <= 0) { + throw new Error("hop_length must be greater than zero"); + } + if (power === null && mel_filters !== null) { + throw new Error( + "You have provided `mel_filters` but `power` is `None`. Mel spectrogram computation is not yet supported for complex-valued spectrogram. Specify `power` to fix this issue." + ); + } + if (center) { + if (pad_mode !== "reflect") { + throw new Error(`pad_mode="${pad_mode}" not implemented yet.`); + } + const half_window = Math.floor((fft_length - 1) / 2) + 1; + waveform = padReflect(waveform, half_window, half_window); + } + let num_frames = Math.floor(1 + Math.floor((waveform.length - frame_length) / hop_length)); + if (min_num_frames !== null && num_frames < min_num_frames) { + num_frames = min_num_frames; + } + const num_frequency_bins = onesided ? Math.floor(fft_length / 2) + 1 : fft_length; + let d1 = num_frames; + let d1Max = num_frames; + if (max_num_frames !== null) { + if (max_num_frames > num_frames) { + if (do_pad) { + d1Max = max_num_frames; + } + } else { + d1Max = d1 = max_num_frames; + } + } + const fft = new _maths_js__WEBPACK_IMPORTED_MODULE_1__.FFT(fft_length); + const inputBuffer = new Float64Array(fft_length); + const outputBuffer = new Float64Array(fft.outputBufferSize); + const transposedMagnitudeData = new Float32Array(num_frequency_bins * d1Max); + for (let i = 0; i < d1; ++i) { + const offset = i * hop_length; + const buffer_size = Math.min(waveform.length - offset, frame_length); + if (buffer_size !== frame_length) { + inputBuffer.fill(0, 0, frame_length); + } + for (let j = 0; j < buffer_size; ++j) { + inputBuffer[j] = waveform[offset + j]; + } + if (remove_dc_offset) { + let sum = 0; + for (let j = 0; j < buffer_size; ++j) { + sum += inputBuffer[j]; + } + const mean = sum / buffer_size; + for (let j = 0; j < buffer_size; ++j) { + inputBuffer[j] -= mean; + } + } + if (preemphasis !== null) { + for (let j = buffer_size - 1; j >= 1; --j) { + inputBuffer[j] -= preemphasis * inputBuffer[j - 1]; + } + inputBuffer[0] *= 1 - preemphasis; + } + for (let j = 0; j < window2.length; ++j) { + inputBuffer[j] *= window2[j]; + } + fft.realTransform(outputBuffer, inputBuffer); + for (let j = 0; j < num_frequency_bins; ++j) { + const j2 = j << 1; + transposedMagnitudeData[j * d1Max + i] = outputBuffer[j2] ** 2 + outputBuffer[j2 + 1] ** 2; + } + } + if (power !== null && power !== 2) { + const pow = 2 / power; + for (let i = 0; i < transposedMagnitudeData.length; ++i) { + transposedMagnitudeData[i] **= pow; + } + } + const num_mel_filters = mel_filters.length; + let mel_spec = await (0, _tensor_js__WEBPACK_IMPORTED_MODULE_3__.matmul)( + // TODO: Make `mel_filters` a Tensor during initialization + new _tensor_js__WEBPACK_IMPORTED_MODULE_3__.Tensor("float32", mel_filters.flat(), [num_mel_filters, num_frequency_bins]), + new _tensor_js__WEBPACK_IMPORTED_MODULE_3__.Tensor("float32", transposedMagnitudeData, [num_frequency_bins, d1Max]) + ); + if (transpose) { + mel_spec = mel_spec.transpose(1, 0); + } + const mel_spec_data = ( + /** @type {Float32Array} */ + mel_spec.data + ); + for (let i = 0; i < mel_spec_data.length; ++i) { + mel_spec_data[i] = Math.max(mel_floor, mel_spec_data[i]); + } + if (power !== null && log_mel !== null) { + const o = Math.min(mel_spec_data.length, d1 * num_mel_filters); + switch (log_mel) { + case "log": + for (let i = 0; i < o; ++i) { + mel_spec_data[i] = Math.log(mel_spec_data[i]); + } + break; + case "log10": + for (let i = 0; i < o; ++i) { + mel_spec_data[i] = Math.log10(mel_spec_data[i]); + } + break; + case "dB": + if (power === 1) { + amplitude_to_db(mel_spec_data, reference, min_value, db_range); + } else if (power === 2) { + power_to_db(mel_spec_data, reference, min_value, db_range); + } else { + throw new Error(`Cannot use log_mel option '${log_mel}' with power ${power}`); + } + break; + default: + throw new Error(`log_mel must be one of null, 'log', 'log10' or 'dB'. Got '${log_mel}'`); + } + } + return mel_spec; + } + function window_function(window_length, name, { + periodic = true, + frame_length = null, + center = true + } = {}) { + const length = periodic ? window_length + 1 : window_length; + let window2; + switch (name) { + case "boxcar": + window2 = new Float64Array(length).fill(1); + break; + case "hann": + case "hann_window": + window2 = hanning(length); + break; + case "hamming": + window2 = hamming(length); + break; + case "povey": + window2 = hanning(length).map((x) => Math.pow(x, 0.85)); + break; + default: + throw new Error(`Unknown window type ${name}.`); + } + if (periodic) { + window2 = window2.subarray(0, window_length); + } + if (frame_length === null) { + return window2; + } + if (window_length > frame_length) { + throw new Error(`Length of the window (${window_length}) may not be larger than frame_length (${frame_length})`); + } + return window2; + } + } + ), + /***/ + "./src/utils/constants.js": ( + /*!********************************!*\ + !*** ./src/utils/constants.js ***! + \********************************/ + /***/ + (__unused_webpack___webpack_module__, __webpack_exports__2, __webpack_require__2) => { + __webpack_require__2.r(__webpack_exports__2); + __webpack_require__2.d(__webpack_exports__2, { + /* harmony export */ + GITHUB_ISSUE_URL: () => ( + /* binding */ + GITHUB_ISSUE_URL + ) + /* harmony export */ + }); + const GITHUB_ISSUE_URL = "https://github.com/huggingface/transformers.js/issues/new/choose"; + } + ), + /***/ + "./src/utils/core.js": ( + /*!***************************!*\ + !*** ./src/utils/core.js ***! + \***************************/ + /***/ + (__unused_webpack___webpack_module__, __webpack_exports__2, __webpack_require__2) => { + __webpack_require__2.r(__webpack_exports__2); + __webpack_require__2.d(__webpack_exports__2, { + /* harmony export */ + calculateDimensions: () => ( + /* binding */ + calculateDimensions + ), + /* harmony export */ + calculateReflectOffset: () => ( + /* binding */ + calculateReflectOffset + ), + /* harmony export */ + dispatchCallback: () => ( + /* binding */ + dispatchCallback + ), + /* harmony export */ + escapeRegExp: () => ( + /* binding */ + escapeRegExp + ), + /* harmony export */ + isIntegralNumber: () => ( + /* binding */ + isIntegralNumber + ), + /* harmony export */ + isTypedArray: () => ( + /* binding */ + isTypedArray + ), + /* harmony export */ + len: () => ( + /* binding */ + len + ), + /* harmony export */ + mergeArrays: () => ( + /* binding */ + mergeArrays + ), + /* harmony export */ + pick: () => ( + /* binding */ + pick + ), + /* harmony export */ + pop: () => ( + /* binding */ + pop + ), + /* harmony export */ + product: () => ( + /* binding */ + product + ), + /* harmony export */ + reverseDictionary: () => ( + /* binding */ + reverseDictionary + ) + /* harmony export */ + }); + function dispatchCallback(progress_callback, data) { + if (progress_callback) progress_callback(data); + } + function reverseDictionary(data) { + return Object.fromEntries(Object.entries(data).map(([key, value]) => [value, key])); + } + function escapeRegExp(string) { + return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); + } + function isTypedArray(val) { + var _a, _b, _c; + return ((_c = (_b = (_a = val == null ? void 0 : val.prototype) == null ? void 0 : _a.__proto__) == null ? void 0 : _b.constructor) == null ? void 0 : _c.name) === "TypedArray"; + } + function isIntegralNumber(x) { + return Number.isInteger(x) || typeof x === "bigint"; + } + function calculateDimensions(arr) { + const dimensions = []; + let current = arr; + while (Array.isArray(current)) { + dimensions.push(current.length); + current = current[0]; + } + return dimensions; + } + function pop(obj, key, defaultValue = void 0) { + const value = obj[key]; + if (value !== void 0) { + delete obj[key]; + return value; + } + if (defaultValue === void 0) { + throw Error(`Key ${key} does not exist in object.`); + } + return defaultValue; + } + function mergeArrays(...arrs) { + return Array.prototype.concat.apply([], arrs); + } + function product(...a) { + return a.reduce((a2, b) => a2.flatMap((d) => b.map((e) => [d, e]))); + } + function calculateReflectOffset(i, w) { + return Math.abs((i + w) % (2 * w) - w); + } + function pick(o, props) { + return Object.assign( + {}, + ...props.map((prop) => { + if (o[prop] !== void 0) { + return { [prop]: o[prop] }; + } + }) + ); + } + function len(s) { + let length = 0; + for (const c of s) ++length; + return length; + } + } + ), + /***/ + "./src/utils/data-structures.js": ( + /*!**************************************!*\ + !*** ./src/utils/data-structures.js ***! + \**************************************/ + /***/ + (__unused_webpack___webpack_module__, __webpack_exports__2, __webpack_require__2) => { + __webpack_require__2.r(__webpack_exports__2); + __webpack_require__2.d(__webpack_exports__2, { + /* harmony export */ + CharTrie: () => ( + /* binding */ + CharTrie + ), + /* harmony export */ + PriorityQueue: () => ( + /* binding */ + PriorityQueue + ), + /* harmony export */ + TokenLattice: () => ( + /* binding */ + TokenLattice + ) + /* harmony export */ + }); + class PriorityQueue { + /** + * Create a new PriorityQueue. + * @param {function(any, any): boolean} comparator Comparator function to determine priority. Defaults to a MaxHeap. + */ + constructor(comparator = (a, b) => a > b, maxSize = Infinity) { + this._heap = []; + this._comparator = comparator; + this._maxSize = maxSize; + } + /** + * The size of the queue + */ + get size() { + return this._heap.length; + } + /** + * Check if the queue is empty. + * @returns {boolean} `true` if the queue is empty, `false` otherwise. + */ + isEmpty() { + return this.size === 0; + } + /** + * Return the element with the highest priority in the queue. + * @returns {any} The highest priority element in the queue. + */ + peek() { + return this._heap[0]; + } + /** + * Add one or more elements to the queue. + * @param {...any} values The values to push into the queue. + * @returns {number} The new size of the queue. + */ + push(...values) { + return this.extend(values); + } + /** + * Add multiple elements to the queue. + * @param {any[]} values The values to push into the queue. + * @returns {number} The new size of the queue. + */ + extend(values) { + for (const value of values) { + if (this.size < this._maxSize) { + this._heap.push(value); + this._siftUp(); + } else { + const smallest = this._smallest(); + if (this._comparator(value, this._heap[smallest])) { + this._heap[smallest] = value; + this._siftUpFrom(smallest); + } + } + } + return this.size; + } + /** + * Remove and return the element with the highest priority in the queue. + * @returns {any} The element with the highest priority in the queue. + */ + pop() { + const poppedValue = this.peek(); + const bottom = this.size - 1; + if (bottom > 0) { + this._swap(0, bottom); + } + this._heap.pop(); + this._siftDown(); + return poppedValue; + } + /** + * Replace the element with the highest priority in the queue with a new value. + * @param {*} value The new value. + * @returns {*} The replaced value. + */ + replace(value) { + const replacedValue = this.peek(); + this._heap[0] = value; + this._siftDown(); + return replacedValue; + } + /** + * Compute the index for the parent of the node at index `i`. + * @param {number} i The index of the node to get the parent of. + * @returns {number} The index of the parent node. + * @private + */ + _parent(i) { + return (i + 1 >>> 1) - 1; + } + /** + * Compute the index for the left child of the node at index `i`. + * @param {number} i The index of the node to get the left child of. + * @returns {number} The index of the left child. + * @private + */ + _left(i) { + return (i << 1) + 1; + } + /** + * Compute the index for the right child of the node at index `i`. + * @param {number} i The index of the node to get the right child of. + * @returns {number} The index of the right child. + * @private + */ + _right(i) { + return i + 1 << 1; + } + /** + * Check if the element at index `i` is greater than the element at index `j`. + * @param {number} i The index of the first element to compare. + * @param {number} j The index of the second element to compare. + * @returns {boolean} `true` if the element at index `i` is greater than the element at index `j`, `false` otherwise. + * @private + */ + _greater(i, j) { + return this._comparator(this._heap[i], this._heap[j]); + } + /** + * Swap the elements at indices `i` and `j`. + * @param {number} i The index of the first element to swap. + * @param {number} j The index of the second element to swap. + * @private + */ + _swap(i, j) { + const temp = this._heap[i]; + this._heap[i] = this._heap[j]; + this._heap[j] = temp; + } + /** + * Maintain the heap property by updating positions in the heap, + * starting at the last element and moving up the heap. + * @private + */ + _siftUp() { + this._siftUpFrom(this.size - 1); + } + /** + * Helper function to sift up from a given node. + * @param {number} node The index of the node to start sifting up from. + */ + _siftUpFrom(node) { + while (node > 0 && this._greater(node, this._parent(node))) { + this._swap(node, this._parent(node)); + node = this._parent(node); + } + } + /** + * Maintain the heap property by updating positions in the heap, + * starting at the first element and moving down the heap. + * @private + */ + _siftDown() { + let node = 0; + while (this._left(node) < this.size && this._greater(this._left(node), node) || this._right(node) < this.size && this._greater(this._right(node), node)) { + const maxChild = this._right(node) < this.size && this._greater(this._right(node), this._left(node)) ? this._right(node) : this._left(node); + this._swap(node, maxChild); + node = maxChild; + } + } + /** + * Get the index of the smallest element in the heap. Since we use an array-based heap, + * the index can be computed without needing to traverse the heap. + * @private + */ + _smallest() { + return 2 ** Math.floor(Math.log2(this.size)) - 1; + } + } + class CharTrie { + constructor() { + this.root = CharTrieNode.default(); + } + /** + * Adds one or more `texts` to the trie. + * @param {string[]} texts The strings to add to the trie. + */ + extend(texts) { + for (const text of texts) { + this.push(text); + } + } + /** + * Adds text to the trie. + * @param {string} text The string to add to the trie. + */ + push(text) { + let node = this.root; + for (const ch of text) { + let child = node.children.get(ch); + if (child === void 0) { + child = CharTrieNode.default(); + node.children.set(ch, child); + } + node = child; + } + node.isLeaf = true; + } + /** + * Searches the trie for all strings with a common prefix of `text`. + * @param {string} text The common prefix to search for. + * @yields {string} Each string in the trie that has `text` as a prefix. + */ + *commonPrefixSearch(text) { + let node = this.root; + if (node === void 0) return; + let prefix = ""; + for (const ch of text) { + prefix += ch; + node = node.children.get(ch); + if (node === void 0) return; + if (node.isLeaf) { + yield prefix; + } + } + } + } + class CharTrieNode { + /** + * Create a new CharTrieNode. + * @param {boolean} isLeaf Whether the node is a leaf node or not. + * @param {Map} children A map containing the node's children, where the key is a character and the value is a `CharTrieNode`. + */ + constructor(isLeaf, children) { + this.isLeaf = isLeaf; + this.children = children; + } + /** + * Returns a new `CharTrieNode` instance with default values. + * @returns {CharTrieNode} A new `CharTrieNode` instance with `isLeaf` set to `false` and an empty `children` map. + */ + static default() { + return new CharTrieNode(false, /* @__PURE__ */ new Map()); + } + } + class TokenLattice { + /** + * Creates a new TokenLattice instance. + * + * @param {string} sentence The input sentence to be tokenized. + * @param {number} bosTokenId The beginning-of-sequence token ID. + * @param {number} eosTokenId The end-of-sequence token ID. + */ + constructor(sentence, bosTokenId, eosTokenId) { + this.chars = Array.from(sentence); + this.len = this.chars.length; + this.bosTokenId = bosTokenId; + this.eosTokenId = eosTokenId; + this.nodes = []; + this.beginNodes = Array.from({ length: this.len + 1 }, () => []); + this.endNodes = Array.from({ length: this.len + 1 }, () => []); + const bos = new TokenLatticeNode(this.bosTokenId, 0, 0, 0, 0); + const eos = new TokenLatticeNode(this.eosTokenId, 1, this.len, 0, 0); + this.nodes.push(bos.clone()); + this.nodes.push(eos.clone()); + this.beginNodes[this.len].push(eos); + this.endNodes[0].push(bos); + } + /** + * Inserts a new token node into the token lattice. + * + * @param {number} pos The starting position of the token. + * @param {number} length The length of the token. + * @param {number} score The score of the token. + * @param {number} tokenId The token ID of the token. + */ + insert(pos, length, score, tokenId) { + const nodeId = this.nodes.length; + const node = new TokenLatticeNode(tokenId, nodeId, pos, length, score); + this.beginNodes[pos].push(node); + this.endNodes[pos + length].push(node); + this.nodes.push(node); + } + /** + * Implements the Viterbi algorithm to compute the most likely sequence of tokens. + * + * @returns {TokenLatticeNode[]} The most likely sequence of tokens. + */ + viterbi() { + const len = this.len; + let pos = 0; + while (pos <= len) { + if (this.beginNodes[pos].length == 0) { + return []; + } + for (let rnode of this.beginNodes[pos]) { + rnode.prev = null; + let bestScore = 0; + let bestNode = null; + for (let lnode of this.endNodes[pos]) { + const score = lnode.backtraceScore + rnode.score; + if (bestNode === null || score > bestScore) { + bestNode = lnode.clone(); + bestScore = score; + } + } + if (bestNode !== null) { + rnode.prev = bestNode; + rnode.backtraceScore = bestScore; + } else { + return []; + } + } + ++pos; + } + const results = []; + const root = this.beginNodes[len][0]; + const prev = root.prev; + if (prev === null) { + return []; + } + let node = prev.clone(); + while (node.prev !== null) { + results.push(node.clone()); + const n = node.clone(); + node = n.prev.clone(); + } + results.reverse(); + return results; + } + /** + * @param {TokenLatticeNode} node + * @returns {string} The array of nodes representing the most likely sequence of tokens. + */ + piece(node) { + return this.chars.slice(node.pos, node.pos + node.length).join(""); + } + /** + * @returns {string[]} The most likely sequence of tokens. + */ + tokens() { + const nodes = this.viterbi(); + return nodes.map((x) => this.piece(x)); + } + /** + * @returns {number[]} The most likely sequence of token ids. + */ + tokenIds() { + const nodes = this.viterbi(); + return nodes.map((x) => x.tokenId); + } + } + class TokenLatticeNode { + /** + * Represents a node in a token lattice for a given sentence. + * @param {number} tokenId The ID of the token associated with this node. + * @param {number} nodeId The ID of this node. + * @param {number} pos The starting position of the token in the sentence. + * @param {number} length The length of the token. + * @param {number} score The score associated with the token. + */ + constructor(tokenId, nodeId, pos, length, score) { + this.tokenId = tokenId; + this.nodeId = nodeId; + this.pos = pos; + this.length = length; + this.score = score; + this.prev = null; + this.backtraceScore = 0; + } + /** + * Returns a clone of this node. + * @returns {TokenLatticeNode} A clone of this node. + */ + clone() { + const n = new TokenLatticeNode(this.tokenId, this.nodeId, this.pos, this.length, this.score); + n.prev = this.prev; + n.backtraceScore = this.backtraceScore; + return n; + } + } + } + ), + /***/ + "./src/utils/devices.js": ( + /*!******************************!*\ + !*** ./src/utils/devices.js ***! + \******************************/ + /***/ + (__unused_webpack___webpack_module__, __webpack_exports__2, __webpack_require__2) => { + __webpack_require__2.r(__webpack_exports__2); + __webpack_require__2.d(__webpack_exports__2, { + /* harmony export */ + DEVICE_TYPES: () => ( + /* binding */ + DEVICE_TYPES + ) + /* harmony export */ + }); + const DEVICE_TYPES = Object.freeze({ + auto: "auto", + // Auto-detect based on device and environment + gpu: "gpu", + // Auto-detect GPU + cpu: "cpu", + // CPU + wasm: "wasm", + // WebAssembly + webgpu: "webgpu", + // WebGPU + cuda: "cuda", + // CUDA + dml: "dml", + // DirectML + webnn: "webnn", + // WebNN (default) + "webnn-npu": "webnn-npu", + // WebNN NPU + "webnn-gpu": "webnn-gpu", + // WebNN GPU + "webnn-cpu": "webnn-cpu" + // WebNN CPU + }); + } + ), + /***/ + "./src/utils/dtypes.js": ( + /*!*****************************!*\ + !*** ./src/utils/dtypes.js ***! + \*****************************/ + /***/ + (__unused_webpack___webpack_module__, __webpack_exports__2, __webpack_require__2) => { + __webpack_require__2.r(__webpack_exports__2); + __webpack_require__2.d(__webpack_exports__2, { + /* harmony export */ + DATA_TYPES: () => ( + /* binding */ + DATA_TYPES + ), + /* harmony export */ + DEFAULT_DEVICE_DTYPE_MAPPING: () => ( + /* binding */ + DEFAULT_DEVICE_DTYPE_MAPPING + ), + /* harmony export */ + DEFAULT_DTYPE_SUFFIX_MAPPING: () => ( + /* binding */ + DEFAULT_DTYPE_SUFFIX_MAPPING + ), + /* harmony export */ + isWebGpuFp16Supported: () => ( + /* binding */ + isWebGpuFp16Supported + ) + /* harmony export */ + }); + var _env_js__WEBPACK_IMPORTED_MODULE_0__2 = __webpack_require__2( + /*! ../env.js */ + "./src/env.js" + ); + var _devices_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__2( + /*! ./devices.js */ + "./src/utils/devices.js" + ); + const isWebGpuFp16Supported = /* @__PURE__ */ function() { + let cachedResult; + return async function() { + if (cachedResult === void 0) { + if (!_env_js__WEBPACK_IMPORTED_MODULE_0__2.apis.IS_WEBGPU_AVAILABLE) { + cachedResult = false; + } else { + try { + const adapter = await navigator.gpu.requestAdapter(); + cachedResult = adapter.features.has("shader-f16"); + } catch (e) { + cachedResult = false; + } + } + } + return cachedResult; + }; + }(); + const DATA_TYPES = Object.freeze({ + fp32: "fp32", + fp16: "fp16", + q8: "q8", + int8: "int8", + uint8: "uint8", + q4: "q4", + bnb4: "bnb4", + q4f16: "q4f16" + // fp16 model with int4 block weight quantization + }); + const DEFAULT_DEVICE_DTYPE_MAPPING = Object.freeze({ + // NOTE: If not specified, will default to fp32 + [_devices_js__WEBPACK_IMPORTED_MODULE_1__.DEVICE_TYPES.wasm]: DATA_TYPES.q8 + }); + const DEFAULT_DTYPE_SUFFIX_MAPPING = Object.freeze({ + [DATA_TYPES.fp32]: "", + [DATA_TYPES.fp16]: "_fp16", + [DATA_TYPES.int8]: "_int8", + [DATA_TYPES.uint8]: "_uint8", + [DATA_TYPES.q8]: "_quantized", + [DATA_TYPES.q4]: "_q4", + [DATA_TYPES.q4f16]: "_q4f16", + [DATA_TYPES.bnb4]: "_bnb4" + }); + } + ), + /***/ + "./src/utils/generic.js": ( + /*!******************************!*\ + !*** ./src/utils/generic.js ***! + \******************************/ + /***/ + (__unused_webpack___webpack_module__, __webpack_exports__2, __webpack_require__2) => { + __webpack_require__2.r(__webpack_exports__2); + __webpack_require__2.d(__webpack_exports__2, { + /* harmony export */ + Callable: () => ( + /* binding */ + Callable + ) + /* harmony export */ + }); + const Callable = ( + /** @type {any} */ + class { + /** + * Creates a new instance of the Callable class. + */ + constructor() { + let closure = function(...args) { + return closure._call(...args); + }; + return Object.setPrototypeOf(closure, new.target.prototype); + } + /** + * This method should be implemented in subclasses to provide the + * functionality of the callable object. + * + * @param {any[]} args + * @throws {Error} If the subclass does not implement the `_call` method. + */ + _call(...args) { + throw Error("Must implement _call method in subclass"); + } + } + ); + } + ), + /***/ + "./src/utils/hub.js": ( + /*!**************************!*\ + !*** ./src/utils/hub.js ***! + \**************************/ + /***/ + (__unused_webpack___webpack_module__, __webpack_exports__2, __webpack_require__2) => { + __webpack_require__2.r(__webpack_exports__2); + __webpack_require__2.d(__webpack_exports__2, { + /* harmony export */ + getFile: () => ( + /* binding */ + getFile + ), + /* harmony export */ + getModelFile: () => ( + /* binding */ + getModelFile + ), + /* harmony export */ + getModelJSON: () => ( + /* binding */ + getModelJSON + ) + /* harmony export */ + }); + var fs__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__2( + /*! fs */ + "?7a2c" + ); + var path__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__2( + /*! path */ + "?a42a" + ); + var _env_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__2( + /*! ../env.js */ + "./src/env.js" + ); + var _core_js__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__2( + /*! ./core.js */ + "./src/utils/core.js" + ); + const CONTENT_TYPE_MAP = { + "txt": "text/plain", + "html": "text/html", + "css": "text/css", + "js": "text/javascript", + "json": "application/json", + "png": "image/png", + "jpg": "image/jpeg", + "jpeg": "image/jpeg", + "gif": "image/gif" + }; + class FileResponse { + /** + * Creates a new `FileResponse` object. + * @param {string|URL} filePath + */ + constructor(filePath) { + this.filePath = filePath; + this.headers = new Headers(); + this.exists = fs__WEBPACK_IMPORTED_MODULE_0__.existsSync(filePath); + if (this.exists) { + this.status = 200; + this.statusText = "OK"; + let stats = fs__WEBPACK_IMPORTED_MODULE_0__.statSync(filePath); + this.headers.set("content-length", stats.size.toString()); + this.updateContentType(); + let self2 = this; + this.body = new ReadableStream({ + start(controller) { + self2.arrayBuffer().then((buffer) => { + controller.enqueue(new Uint8Array(buffer)); + controller.close(); + }); + } + }); + } else { + this.status = 404; + this.statusText = "Not Found"; + this.body = null; + } + } + /** + * Updates the 'content-type' header property of the response based on the extension of + * the file specified by the filePath property of the current object. + * @returns {void} + */ + updateContentType() { + const extension = this.filePath.toString().split(".").pop().toLowerCase(); + this.headers.set("content-type", CONTENT_TYPE_MAP[extension] ?? "application/octet-stream"); + } + /** + * Clone the current FileResponse object. + * @returns {FileResponse} A new FileResponse object with the same properties as the current object. + */ + clone() { + let response = new FileResponse(this.filePath); + response.exists = this.exists; + response.status = this.status; + response.statusText = this.statusText; + response.headers = new Headers(this.headers); + return response; + } + /** + * Reads the contents of the file specified by the filePath property and returns a Promise that + * resolves with an ArrayBuffer containing the file's contents. + * @returns {Promise} A Promise that resolves with an ArrayBuffer containing the file's contents. + * @throws {Error} If the file cannot be read. + */ + async arrayBuffer() { + const data = await fs__WEBPACK_IMPORTED_MODULE_0__.promises.readFile(this.filePath); + return data.buffer; + } + /** + * Reads the contents of the file specified by the filePath property and returns a Promise that + * resolves with a Blob containing the file's contents. + * @returns {Promise} A Promise that resolves with a Blob containing the file's contents. + * @throws {Error} If the file cannot be read. + */ + async blob() { + const data = await fs__WEBPACK_IMPORTED_MODULE_0__.promises.readFile(this.filePath); + return new Blob([data], { type: this.headers.get("content-type") }); + } + /** + * Reads the contents of the file specified by the filePath property and returns a Promise that + * resolves with a string containing the file's contents. + * @returns {Promise} A Promise that resolves with a string containing the file's contents. + * @throws {Error} If the file cannot be read. + */ + async text() { + const data = await fs__WEBPACK_IMPORTED_MODULE_0__.promises.readFile(this.filePath, "utf8"); + return data; + } + /** + * Reads the contents of the file specified by the filePath property and returns a Promise that + * resolves with a parsed JavaScript object containing the file's contents. + * + * @returns {Promise} A Promise that resolves with a parsed JavaScript object containing the file's contents. + * @throws {Error} If the file cannot be read. + */ + async json() { + return JSON.parse(await this.text()); + } + } + function isValidUrl(string, protocols = null, validHosts = null) { + let url; + try { + url = new URL(string); + } catch (_) { + return false; + } + if (protocols && !protocols.includes(url.protocol)) { + return false; + } + if (validHosts && !validHosts.includes(url.hostname)) { + return false; + } + return true; + } + async function getFile(urlOrPath) { + var _a, _b, _c, _d; + if (_env_js__WEBPACK_IMPORTED_MODULE_2__.env.useFS && !isValidUrl(urlOrPath, ["http:", "https:", "blob:"])) { + return new FileResponse(urlOrPath); + } else if (typeof process !== "undefined" && ((_a = process == null ? void 0 : process.release) == null ? void 0 : _a.name) === "node") { + const IS_CI = !!((_b = process.env) == null ? void 0 : _b.TESTING_REMOTELY); + const version = _env_js__WEBPACK_IMPORTED_MODULE_2__.env.version; + const headers = new Headers(); + headers.set("User-Agent", `transformers.js/${version}; is_ci/${IS_CI};`); + const isHFURL = isValidUrl(urlOrPath, ["http:", "https:"], ["huggingface.co", "hf.co"]); + if (isHFURL) { + const token = ((_c = process.env) == null ? void 0 : _c.HF_TOKEN) ?? ((_d = process.env) == null ? void 0 : _d.HF_ACCESS_TOKEN); + if (token) { + headers.set("Authorization", `Bearer ${token}`); + } + } + return fetch(urlOrPath, { headers }); + } else { + return fetch(urlOrPath); + } + } + const ERROR_MAPPING = { + // 4xx errors (https://developer.mozilla.org/en-US/docs/Web/HTTP/Status#client_error_responses) + 400: "Bad request error occurred while trying to load file", + 401: "Unauthorized access to file", + 403: "Forbidden access to file", + 404: "Could not locate file", + 408: "Request timeout error occurred while trying to load file", + // 5xx errors (https://developer.mozilla.org/en-US/docs/Web/HTTP/Status#server_error_responses) + 500: "Internal server error error occurred while trying to load file", + 502: "Bad gateway error occurred while trying to load file", + 503: "Service unavailable error occurred while trying to load file", + 504: "Gateway timeout error occurred while trying to load file" + }; + function handleError(status, remoteURL, fatal) { + if (!fatal) { + return null; + } + const message = ERROR_MAPPING[status] ?? `Error (${status}) occurred while trying to load file`; + throw Error(`${message}: "${remoteURL}".`); + } + class FileCache { + /** + * Instantiate a `FileCache` object. + * @param {string} path + */ + constructor(path) { + this.path = path; + } + /** + * Checks whether the given request is in the cache. + * @param {string} request + * @returns {Promise} + */ + async match(request) { + let filePath = path__WEBPACK_IMPORTED_MODULE_1__.join(this.path, request); + let file = new FileResponse(filePath); + if (file.exists) { + return file; + } else { + return void 0; + } + } + /** + * Adds the given response to the cache. + * @param {string} request + * @param {Response|FileResponse} response + * @returns {Promise} + */ + async put(request, response) { + const buffer = Buffer.from(await response.arrayBuffer()); + let outputPath = path__WEBPACK_IMPORTED_MODULE_1__.join(this.path, request); + try { + await fs__WEBPACK_IMPORTED_MODULE_0__.promises.mkdir(path__WEBPACK_IMPORTED_MODULE_1__.dirname(outputPath), { recursive: true }); + await fs__WEBPACK_IMPORTED_MODULE_0__.promises.writeFile(outputPath, buffer); + } catch (err) { + console.warn("An error occurred while writing the file to cache:", err); + } + } + // TODO add the rest? + // addAll(requests: RequestInfo[]): Promise; + // delete(request: RequestInfo | URL, options?: CacheQueryOptions): Promise; + // keys(request?: RequestInfo | URL, options?: CacheQueryOptions): Promise>; + // match(request: RequestInfo | URL, options?: CacheQueryOptions): Promise; + // matchAll(request?: RequestInfo | URL, options?: CacheQueryOptions): Promise>; + } + async function tryCache(cache, ...names) { + for (let name of names) { + try { + let result = await cache.match(name); + if (result) return result; + } catch (e) { + continue; + } + } + return void 0; + } + async function getModelFile(path_or_repo_id, filename, fatal = true, options = {}) { + if (!_env_js__WEBPACK_IMPORTED_MODULE_2__.env.allowLocalModels) { + if (options.local_files_only) { + throw Error("Invalid configuration detected: local models are disabled (`env.allowLocalModels=false`) but you have requested to only use local models (`local_files_only=true`)."); + } else if (!_env_js__WEBPACK_IMPORTED_MODULE_2__.env.allowRemoteModels) { + throw Error("Invalid configuration detected: both local and remote models are disabled. Fix by setting `env.allowLocalModels` or `env.allowRemoteModels` to `true`."); + } + } + (0, _core_js__WEBPACK_IMPORTED_MODULE_3__.dispatchCallback)(options.progress_callback, { + status: "initiate", + name: path_or_repo_id, + file: filename + }); + let cache; + if (!cache && _env_js__WEBPACK_IMPORTED_MODULE_2__.env.useBrowserCache) { + if (typeof caches === "undefined") { + throw Error("Browser cache is not available in this environment."); + } + try { + cache = await caches.open("transformers-cache"); + } catch (e) { + console.warn("An error occurred while opening the browser cache:", e); + } + } + if (!cache && _env_js__WEBPACK_IMPORTED_MODULE_2__.env.useFSCache) { + cache = new FileCache(options.cache_dir ?? _env_js__WEBPACK_IMPORTED_MODULE_2__.env.cacheDir); + } + if (!cache && _env_js__WEBPACK_IMPORTED_MODULE_2__.env.useCustomCache) { + if (!_env_js__WEBPACK_IMPORTED_MODULE_2__.env.customCache) { + throw Error("`env.useCustomCache=true`, but `env.customCache` is not defined."); + } + if (!_env_js__WEBPACK_IMPORTED_MODULE_2__.env.customCache.match || !_env_js__WEBPACK_IMPORTED_MODULE_2__.env.customCache.put) { + throw new Error( + "`env.customCache` must be an object which implements the `match` and `put` functions of the Web Cache API. For more information, see https://developer.mozilla.org/en-US/docs/Web/API/Cache" + ); + } + cache = _env_js__WEBPACK_IMPORTED_MODULE_2__.env.customCache; + } + const revision = options.revision ?? "main"; + let requestURL = pathJoin(path_or_repo_id, filename); + let localPath = pathJoin(_env_js__WEBPACK_IMPORTED_MODULE_2__.env.localModelPath, requestURL); + let remoteURL = pathJoin( + _env_js__WEBPACK_IMPORTED_MODULE_2__.env.remoteHost, + _env_js__WEBPACK_IMPORTED_MODULE_2__.env.remotePathTemplate.replaceAll("{model}", path_or_repo_id).replaceAll("{revision}", encodeURIComponent(revision)), + filename + ); + let fsCacheKey = revision === "main" ? requestURL : pathJoin(path_or_repo_id, revision, filename); + let cacheKey; + let proposedCacheKey = cache instanceof FileCache ? fsCacheKey : remoteURL; + let toCacheResponse = false; + let response; + if (cache) { + response = await tryCache(cache, localPath, proposedCacheKey); + } + const cacheHit = response !== void 0; + if (response === void 0) { + if (_env_js__WEBPACK_IMPORTED_MODULE_2__.env.allowLocalModels) { + const isURL = isValidUrl(requestURL, ["http:", "https:"]); + if (!isURL) { + try { + response = await getFile(localPath); + cacheKey = localPath; + } catch (e) { + console.warn(`Unable to load from local path "${localPath}": "${e}"`); + } + } else if (options.local_files_only) { + throw new Error(`\`local_files_only=true\`, but attempted to load a remote file from: ${requestURL}.`); + } else if (!_env_js__WEBPACK_IMPORTED_MODULE_2__.env.allowRemoteModels) { + throw new Error(`\`env.allowRemoteModels=false\`, but attempted to load a remote file from: ${requestURL}.`); + } + } + if (response === void 0 || response.status === 404) { + if (options.local_files_only || !_env_js__WEBPACK_IMPORTED_MODULE_2__.env.allowRemoteModels) { + if (fatal) { + throw Error(`\`local_files_only=true\` or \`env.allowRemoteModels=false\` and file was not found locally at "${localPath}".`); + } else { + return null; + } + } + response = await getFile(remoteURL); + if (response.status !== 200) { + return handleError(response.status, remoteURL, fatal); + } + cacheKey = proposedCacheKey; + } + toCacheResponse = cache && typeof Response !== "undefined" && response instanceof Response && response.status === 200; + } + (0, _core_js__WEBPACK_IMPORTED_MODULE_3__.dispatchCallback)(options.progress_callback, { + status: "download", + name: path_or_repo_id, + file: filename + }); + const progressInfo = { + status: "progress", + name: path_or_repo_id, + file: filename + }; + let buffer; + if (!options.progress_callback) { + buffer = new Uint8Array(await response.arrayBuffer()); + } else if (cacheHit && typeof navigator !== "undefined" && /firefox/i.test(navigator.userAgent)) { + buffer = new Uint8Array(await response.arrayBuffer()); + (0, _core_js__WEBPACK_IMPORTED_MODULE_3__.dispatchCallback)(options.progress_callback, { + ...progressInfo, + progress: 100, + loaded: buffer.length, + total: buffer.length + }); + } else { + buffer = await readResponse(response, (data) => { + (0, _core_js__WEBPACK_IMPORTED_MODULE_3__.dispatchCallback)(options.progress_callback, { + ...progressInfo, + ...data + }); + }); + } + if ( + // Only cache web responses + // i.e., do not cache FileResponses (prevents duplication) + toCacheResponse && cacheKey && // Check again whether request is in cache. If not, we add the response to the cache + await cache.match(cacheKey) === void 0 + ) { + await cache.put(cacheKey, new Response(buffer, { + headers: response.headers + })).catch((err) => { + console.warn(`Unable to add response to browser cache: ${err}.`); + }); + } + (0, _core_js__WEBPACK_IMPORTED_MODULE_3__.dispatchCallback)(options.progress_callback, { + status: "done", + name: path_or_repo_id, + file: filename + }); + return buffer; + } + async function getModelJSON(modelPath, fileName, fatal = true, options = {}) { + let buffer = await getModelFile(modelPath, fileName, fatal, options); + if (buffer === null) { + return {}; + } + let decoder = new TextDecoder("utf-8"); + let jsonData = decoder.decode(buffer); + return JSON.parse(jsonData); + } + async function readResponse(response, progress_callback) { + const contentLength = response.headers.get("Content-Length"); + if (contentLength === null) { + console.warn("Unable to determine content-length from response headers. Will expand buffer when needed."); + } + const arrayBuffer = await response.arrayBuffer(); + const buffer = new Uint8Array(arrayBuffer); + const loaded = buffer.length; + const total = buffer.length; + const progress = loaded / total * 100; + + progress_callback({ + progress, + loaded, + total + }); + + return buffer; + } + function pathJoin(...parts) { + parts = parts.map((part, index) => { + if (index) { + part = part.replace(new RegExp("^/"), ""); + } + if (index !== parts.length - 1) { + part = part.replace(new RegExp("/$"), ""); + } + return part; + }); + return parts.join("/"); + } + } + ), + /***/ + "./src/utils/image.js": ( + /*!****************************!*\ + !*** ./src/utils/image.js ***! + \****************************/ + /***/ + (__unused_webpack___webpack_module__, __webpack_exports__2, __webpack_require__2) => { + __webpack_require__2.r(__webpack_exports__2); + __webpack_require__2.d(__webpack_exports__2, { + /* harmony export */ + RawImage: () => ( + /* binding */ + RawImage + ) + /* harmony export */ + }); + var _hub_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__2( + /*! ./hub.js */ + "./src/utils/hub.js" + ); + var _env_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__2( + /*! ../env.js */ + "./src/env.js" + ); + var _tensor_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__2( + /*! ./tensor.js */ + "./src/utils/tensor.js" + ); + var sharp__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__2( + /*! sharp */ + "?2b25" + ); + const BROWSER_ENV = typeof self !== "undefined"; + const WEBWORKER_ENV = BROWSER_ENV && self.constructor.name === "DedicatedWorkerGlobalScope"; + let createCanvasFunction; + let ImageDataClass; + let loadImageFunction; + if (BROWSER_ENV) { + createCanvasFunction = (width, height) => { + if (!self.OffscreenCanvas) { + throw new Error("OffscreenCanvas not supported by this browser."); + } + return new self.OffscreenCanvas(width, height); + }; + loadImageFunction = self.createImageBitmap; + ImageDataClass = self.ImageData; + } else if (sharp__WEBPACK_IMPORTED_MODULE_3__) { + loadImageFunction = async (img) => { + const metadata = await img.metadata(); + const rawChannels = metadata.channels; + const { data, info } = await img.rotate().raw().toBuffer({ resolveWithObject: true }); + const newImage = new RawImage(new Uint8ClampedArray(data), info.width, info.height, info.channels); + if (rawChannels !== void 0 && rawChannels !== info.channels) { + newImage.convert(rawChannels); + } + return newImage; + }; + } else { + throw new Error("Unable to load image processing library."); + } + const RESAMPLING_MAPPING = { + 0: "nearest", + 1: "lanczos", + 2: "bilinear", + 3: "bicubic", + 4: "box", + 5: "hamming" + }; + const CONTENT_TYPE_MAP = /* @__PURE__ */ new Map([ + ["png", "image/png"], + ["jpg", "image/jpeg"], + ["jpeg", "image/jpeg"], + ["gif", "image/gif"] + ]); + class RawImage { + /** + * Create a new `RawImage` object. + * @param {Uint8ClampedArray|Uint8Array} data The pixel data. + * @param {number} width The width of the image. + * @param {number} height The height of the image. + * @param {1|2|3|4} channels The number of channels. + */ + constructor(data, width, height, channels) { + this.data = data; + this.width = width; + this.height = height; + this.channels = channels; + } + /** + * Returns the size of the image (width, height). + * @returns {[number, number]} The size of the image (width, height). + */ + get size() { + return [this.width, this.height]; + } + /** + * Helper method for reading an image from a variety of input types. + * @param {RawImage|string|URL} input + * @returns The image object. + * + * **Example:** Read image from a URL. + * ```javascript + * let image = await RawImage.read('https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/football-match.jpg'); + * // RawImage { + * // "data": Uint8ClampedArray [ 25, 25, 25, 19, 19, 19, ... ], + * // "width": 800, + * // "height": 533, + * // "channels": 3 + * // } + * ``` + */ + static async read(input) { + if (input instanceof RawImage) { + return input; + } else if (typeof input === "string" || input instanceof URL) { + return await this.fromURL(input); + } else { + throw new Error(`Unsupported input type: ${typeof input}`); + } + } + /** + * Read an image from a canvas. + * @param {HTMLCanvasElement|OffscreenCanvas} canvas The canvas to read the image from. + * @returns {RawImage} The image object. + */ + static fromCanvas(canvas) { + if (!BROWSER_ENV) { + throw new Error("fromCanvas() is only supported in browser environments."); + } + const ctx = canvas.getContext("2d"); + const data = ctx.getImageData(0, 0, canvas.width, canvas.height).data; + return new RawImage(data, canvas.width, canvas.height, 4); + } + /** + * Read an image from a URL or file path. + * @param {string|URL} url The URL or file path to read the image from. + * @returns {Promise} The image object. + */ + static async fromURL(url) { + const response = await (0, _hub_js__WEBPACK_IMPORTED_MODULE_0__.getFile)(url); + if (response.status !== 200) { + throw new Error(`Unable to read image from "${url}" (${response.status} ${response.statusText})`); + } + const blob = await response.blob(); + return this.fromBlob(blob); + } + /** + * Helper method to create a new Image from a blob. + * @param {Blob} blob The blob to read the image from. + * @returns {Promise} The image object. + */ + static async fromBlob(blob) { + if (BROWSER_ENV) { + const img = await loadImageFunction(blob); + const ctx = createCanvasFunction(img.width, img.height).getContext("2d"); + ctx.drawImage(img, 0, 0); + return new this(ctx.getImageData(0, 0, img.width, img.height).data, img.width, img.height, 4); + } else { + const img = sharp__WEBPACK_IMPORTED_MODULE_3__(await blob.arrayBuffer()); + return await loadImageFunction(img); + } + } + /** + * Helper method to create a new Image from a tensor + * @param {Tensor} tensor + */ + static fromTensor(tensor, channel_format = "CHW") { + if (tensor.dims.length !== 3) { + throw new Error(`Tensor should have 3 dimensions, but has ${tensor.dims.length} dimensions.`); + } + if (channel_format === "CHW") { + tensor = tensor.transpose(1, 2, 0); + } else if (channel_format === "HWC") { + } else { + throw new Error(`Unsupported channel format: ${channel_format}`); + } + if (!(tensor.data instanceof Uint8ClampedArray || tensor.data instanceof Uint8Array)) { + throw new Error(`Unsupported tensor type: ${tensor.type}`); + } + switch (tensor.dims[2]) { + case 1: + case 2: + case 3: + case 4: + return new RawImage(tensor.data, tensor.dims[1], tensor.dims[0], tensor.dims[2]); + default: + throw new Error(`Unsupported number of channels: ${tensor.dims[2]}`); + } + } + /** + * Convert the image to grayscale format. + * @returns {RawImage} `this` to support chaining. + */ + grayscale() { + if (this.channels === 1) { + return this; + } + const newData = new Uint8ClampedArray(this.width * this.height * 1); + switch (this.channels) { + case 3: + case 4: + for (let i = 0, offset = 0; i < this.data.length; i += this.channels) { + const red = this.data[i]; + const green = this.data[i + 1]; + const blue = this.data[i + 2]; + newData[offset++] = Math.round(0.2989 * red + 0.587 * green + 0.114 * blue); + } + break; + default: + throw new Error(`Conversion failed due to unsupported number of channels: ${this.channels}`); + } + return this._update(newData, this.width, this.height, 1); + } + /** + * Convert the image to RGB format. + * @returns {RawImage} `this` to support chaining. + */ + rgb() { + if (this.channels === 3) { + return this; + } + const newData = new Uint8ClampedArray(this.width * this.height * 3); + switch (this.channels) { + case 1: + for (let i = 0, offset = 0; i < this.data.length; ++i) { + newData[offset++] = this.data[i]; + newData[offset++] = this.data[i]; + newData[offset++] = this.data[i]; + } + break; + case 4: + for (let i = 0, offset = 0; i < this.data.length; i += 4) { + newData[offset++] = this.data[i]; + newData[offset++] = this.data[i + 1]; + newData[offset++] = this.data[i + 2]; + } + break; + default: + throw new Error(`Conversion failed due to unsupported number of channels: ${this.channels}`); + } + return this._update(newData, this.width, this.height, 3); + } + /** + * Convert the image to RGBA format. + * @returns {RawImage} `this` to support chaining. + */ + rgba() { + if (this.channels === 4) { + return this; + } + const newData = new Uint8ClampedArray(this.width * this.height * 4); + switch (this.channels) { + case 1: + for (let i = 0, offset = 0; i < this.data.length; ++i) { + newData[offset++] = this.data[i]; + newData[offset++] = this.data[i]; + newData[offset++] = this.data[i]; + newData[offset++] = 255; + } + break; + case 3: + for (let i = 0, offset = 0; i < this.data.length; i += 3) { + newData[offset++] = this.data[i]; + newData[offset++] = this.data[i + 1]; + newData[offset++] = this.data[i + 2]; + newData[offset++] = 255; + } + break; + default: + throw new Error(`Conversion failed due to unsupported number of channels: ${this.channels}`); + } + return this._update(newData, this.width, this.height, 4); + } + /** + * Resize the image to the given dimensions. This method uses the canvas API to perform the resizing. + * @param {number} width The width of the new image. + * @param {number} height The height of the new image. + * @param {Object} options Additional options for resizing. + * @param {0|1|2|3|4|5|string} [options.resample] The resampling method to use. + * @returns {Promise} `this` to support chaining. + */ + async resize(width, height, { + resample = 2 + } = {}) { + let resampleMethod = RESAMPLING_MAPPING[resample] ?? resample; + if (BROWSER_ENV) { + const numChannels = this.channels; + const canvas = this.toCanvas(); + const ctx = createCanvasFunction(width, height).getContext("2d"); + ctx.drawImage(canvas, 0, 0, width, height); + const resizedImage = new RawImage(ctx.getImageData(0, 0, width, height).data, width, height, 4); + return resizedImage.convert(numChannels); + } else { + let img = this.toSharp(); + switch (resampleMethod) { + case "box": + case "hamming": + if (resampleMethod === "box" || resampleMethod === "hamming") { + console.warn(`Resampling method ${resampleMethod} is not yet supported. Using bilinear instead.`); + resampleMethod = "bilinear"; + } + case "nearest": + case "bilinear": + case "bicubic": + img = img.affine([width / this.width, 0, 0, height / this.height], { + interpolator: resampleMethod + }); + break; + case "lanczos": + img = img.resize({ + width, + height, + fit: "fill", + kernel: "lanczos3" + // PIL Lanczos uses a kernel size of 3 + }); + break; + default: + throw new Error(`Resampling method ${resampleMethod} is not supported.`); + } + return await loadImageFunction(img); + } + } + async pad([left, right, top, bottom]) { + left = Math.max(left, 0); + right = Math.max(right, 0); + top = Math.max(top, 0); + bottom = Math.max(bottom, 0); + if (left === 0 && right === 0 && top === 0 && bottom === 0) { + return this; + } + if (BROWSER_ENV) { + const numChannels = this.channels; + const canvas = this.toCanvas(); + const newWidth = this.width + left + right; + const newHeight = this.height + top + bottom; + const ctx = createCanvasFunction(newWidth, newHeight).getContext("2d"); + ctx.drawImage( + canvas, + 0, + 0, + this.width, + this.height, + left, + top, + newWidth, + newHeight + ); + const paddedImage = new RawImage( + ctx.getImageData(0, 0, newWidth, newHeight).data, + newWidth, + newHeight, + 4 + ); + return paddedImage.convert(numChannels); + } else { + const img = this.toSharp().extend({ left, right, top, bottom }); + return await loadImageFunction(img); + } + } + async crop([x_min, y_min, x_max, y_max]) { + x_min = Math.max(x_min, 0); + y_min = Math.max(y_min, 0); + x_max = Math.min(x_max, this.width - 1); + y_max = Math.min(y_max, this.height - 1); + if (x_min === 0 && y_min === 0 && x_max === this.width - 1 && y_max === this.height - 1) { + return this; + } + const crop_width = x_max - x_min + 1; + const crop_height = y_max - y_min + 1; + if (BROWSER_ENV) { + const numChannels = this.channels; + const canvas = this.toCanvas(); + const ctx = createCanvasFunction(crop_width, crop_height).getContext("2d"); + ctx.drawImage( + canvas, + x_min, + y_min, + crop_width, + crop_height, + 0, + 0, + crop_width, + crop_height + ); + const resizedImage = new RawImage(ctx.getImageData(0, 0, crop_width, crop_height).data, crop_width, crop_height, 4); + return resizedImage.convert(numChannels); + } else { + const img = this.toSharp().extract({ + left: x_min, + top: y_min, + width: crop_width, + height: crop_height + }); + return await loadImageFunction(img); + } + } + async center_crop(crop_width, crop_height) { + if (this.width === crop_width && this.height === crop_height) { + return this; + } + const width_offset = (this.width - crop_width) / 2; + const height_offset = (this.height - crop_height) / 2; + if (BROWSER_ENV) { + const numChannels = this.channels; + const canvas = this.toCanvas(); + const ctx = createCanvasFunction(crop_width, crop_height).getContext("2d"); + let sourceX = 0; + let sourceY = 0; + let destX = 0; + let destY = 0; + if (width_offset >= 0) { + sourceX = width_offset; + } else { + destX = -width_offset; + } + if (height_offset >= 0) { + sourceY = height_offset; + } else { + destY = -height_offset; + } + ctx.drawImage( + canvas, + sourceX, + sourceY, + crop_width, + crop_height, + destX, + destY, + crop_width, + crop_height + ); + const resizedImage = new RawImage(ctx.getImageData(0, 0, crop_width, crop_height).data, crop_width, crop_height, 4); + return resizedImage.convert(numChannels); + } else { + let img = this.toSharp(); + if (width_offset >= 0 && height_offset >= 0) { + img = img.extract({ + left: Math.floor(width_offset), + top: Math.floor(height_offset), + width: crop_width, + height: crop_height + }); + } else if (width_offset <= 0 && height_offset <= 0) { + const top = Math.floor(-height_offset); + const left = Math.floor(-width_offset); + img = img.extend({ + top, + left, + // Ensures the resulting image has the desired dimensions + right: crop_width - this.width - left, + bottom: crop_height - this.height - top + }); + } else { + let y_padding = [0, 0]; + let y_extract = 0; + if (height_offset < 0) { + y_padding[0] = Math.floor(-height_offset); + y_padding[1] = crop_height - this.height - y_padding[0]; + } else { + y_extract = Math.floor(height_offset); + } + let x_padding = [0, 0]; + let x_extract = 0; + if (width_offset < 0) { + x_padding[0] = Math.floor(-width_offset); + x_padding[1] = crop_width - this.width - x_padding[0]; + } else { + x_extract = Math.floor(width_offset); + } + img = img.extend({ + top: y_padding[0], + bottom: y_padding[1], + left: x_padding[0], + right: x_padding[1] + }).extract({ + left: x_extract, + top: y_extract, + width: crop_width, + height: crop_height + }); + } + return await loadImageFunction(img); + } + } + async toBlob(type = "image/png", quality = 1) { + if (!BROWSER_ENV) { + throw new Error("toBlob() is only supported in browser environments."); + } + const canvas = this.toCanvas(); + return await canvas.convertToBlob({ type, quality }); + } + toTensor(channel_format = "CHW") { + let tensor = new _tensor_js__WEBPACK_IMPORTED_MODULE_2__.Tensor( + "uint8", + new Uint8Array(this.data), + [this.height, this.width, this.channels] + ); + if (channel_format === "HWC") { + } else if (channel_format === "CHW") { + tensor = tensor.permute(2, 0, 1); + } else { + throw new Error(`Unsupported channel format: ${channel_format}`); + } + return tensor; + } + toCanvas() { + if (!BROWSER_ENV) { + throw new Error("toCanvas() is only supported in browser environments."); + } + const cloned = this.clone().rgba(); + const clonedCanvas = createCanvasFunction(cloned.width, cloned.height); + const data = new ImageDataClass(cloned.data, cloned.width, cloned.height); + clonedCanvas.getContext("2d").putImageData(data, 0, 0); + return clonedCanvas; + } + /** + * Helper method to update the image data. + * @param {Uint8ClampedArray} data The new image data. + * @param {number} width The new width of the image. + * @param {number} height The new height of the image. + * @param {1|2|3|4|null} [channels] The new number of channels of the image. + * @private + */ + _update(data, width, height, channels = null) { + this.data = data; + this.width = width; + this.height = height; + if (channels !== null) { + this.channels = channels; + } + return this; + } + /** + * Clone the image + * @returns {RawImage} The cloned image + */ + clone() { + return new RawImage(this.data.slice(), this.width, this.height, this.channels); + } + /** + * Helper method for converting image to have a certain number of channels + * @param {number} numChannels The number of channels. Must be 1, 3, or 4. + * @returns {RawImage} `this` to support chaining. + */ + convert(numChannels) { + if (this.channels === numChannels) return this; + switch (numChannels) { + case 1: + this.grayscale(); + break; + case 3: + this.rgb(); + break; + case 4: + this.rgba(); + break; + default: + throw new Error(`Conversion failed due to unsupported number of channels: ${this.channels}`); + } + return this; + } + /** + * Save the image to the given path. + * @param {string} path The path to save the image to. + */ + async save(path) { + if (BROWSER_ENV) { + if (WEBWORKER_ENV) { + throw new Error("Unable to save an image from a Web Worker."); + } + const extension = path.split(".").pop().toLowerCase(); + const mime = CONTENT_TYPE_MAP.get(extension) ?? "image/png"; + const blob = await this.toBlob(mime); + const dataURL = URL.createObjectURL(blob); + const downloadLink = document.createElement("a"); + downloadLink.href = dataURL; + downloadLink.download = path; + downloadLink.click(); + downloadLink.remove(); + } else if (!_env_js__WEBPACK_IMPORTED_MODULE_1__.env.useFS) { + throw new Error("Unable to save the image because filesystem is disabled in this environment."); + } else { + const img = this.toSharp(); + return await img.toFile(path); + } + } + toSharp() { + if (BROWSER_ENV) { + throw new Error("toSharp() is only supported in server-side environments."); + } + return sharp__WEBPACK_IMPORTED_MODULE_3__(this.data, { + raw: { + width: this.width, + height: this.height, + channels: this.channels + } + }); + } + } + } + ), + /***/ + "./src/utils/maths.js": ( + /*!****************************!*\ + !*** ./src/utils/maths.js ***! + \****************************/ + /***/ + (__unused_webpack___webpack_module__, __webpack_exports__2, __webpack_require__2) => { + __webpack_require__2.r(__webpack_exports__2); + __webpack_require__2.d(__webpack_exports__2, { + /* harmony export */ + FFT: () => ( + /* binding */ + FFT + ), + /* harmony export */ + bankers_round: () => ( + /* binding */ + bankers_round + ), + /* harmony export */ + cos_sim: () => ( + /* binding */ + cos_sim + ), + /* harmony export */ + dot: () => ( + /* binding */ + dot + ), + /* harmony export */ + dynamic_time_warping: () => ( + /* binding */ + dynamic_time_warping + ), + /* harmony export */ + interpolate_data: () => ( + /* binding */ + interpolate_data + ), + /* harmony export */ + log_softmax: () => ( + /* binding */ + log_softmax + ), + /* harmony export */ + magnitude: () => ( + /* binding */ + magnitude + ), + /* harmony export */ + max: () => ( + /* binding */ + max + ), + /* harmony export */ + medianFilter: () => ( + /* binding */ + medianFilter + ), + /* harmony export */ + min: () => ( + /* binding */ + min + ), + /* harmony export */ + permute_data: () => ( + /* binding */ + permute_data + ), + /* harmony export */ + round: () => ( + /* binding */ + round + ), + /* harmony export */ + softmax: () => ( + /* binding */ + softmax + ) + /* harmony export */ + }); + function interpolate_data(input, [in_channels, in_height, in_width], [out_height, out_width], mode = "bilinear", align_corners = false) { + const x_scale = out_width / in_width; + const y_scale = out_height / in_height; + const out_img = new input.constructor(out_height * out_width * in_channels); + const inStride = in_height * in_width; + const outStride = out_height * out_width; + for (let i = 0; i < out_height; ++i) { + for (let j = 0; j < out_width; ++j) { + const outOffset = i * out_width + j; + const x = (j + 0.5) / x_scale - 0.5; + const y = (i + 0.5) / y_scale - 0.5; + let x1 = Math.floor(x); + let y1 = Math.floor(y); + const x2 = Math.min(x1 + 1, in_width - 1); + const y2 = Math.min(y1 + 1, in_height - 1); + x1 = Math.max(x1, 0); + y1 = Math.max(y1, 0); + const s = x - x1; + const t = y - y1; + const w1 = (1 - s) * (1 - t); + const w2 = s * (1 - t); + const w3 = (1 - s) * t; + const w4 = s * t; + const yStride = y1 * in_width; + const xStride = y2 * in_width; + const idx1 = yStride + x1; + const idx2 = yStride + x2; + const idx3 = xStride + x1; + const idx4 = xStride + x2; + for (let k = 0; k < in_channels; ++k) { + const cOffset = k * inStride; + out_img[k * outStride + outOffset] = w1 * input[cOffset + idx1] + w2 * input[cOffset + idx2] + w3 * input[cOffset + idx3] + w4 * input[cOffset + idx4]; + } + } + } + return out_img; + } + function permute_data(array, dims, axes) { + const shape = new Array(axes.length); + const stride = new Array(axes.length); + for (let i = axes.length - 1, s = 1; i >= 0; --i) { + stride[i] = s; + shape[i] = dims[axes[i]]; + s *= shape[i]; + } + const invStride = axes.map((_, i) => stride[axes.indexOf(i)]); + const permutedData = new array.constructor(array.length); + for (let i = 0; i < array.length; ++i) { + let newIndex = 0; + for (let j = dims.length - 1, k = i; j >= 0; --j) { + newIndex += k % dims[j] * invStride[j]; + k = Math.floor(k / dims[j]); + } + permutedData[newIndex] = array[i]; + } + return [permutedData, shape]; + } + function softmax(arr) { + const maxVal = max(arr)[0]; + const exps = arr.map((x) => Math.exp(x - maxVal)); + const sumExps = exps.reduce((acc, val) => acc + val, 0); + const softmaxArr = exps.map((x) => x / sumExps); + return ( + /** @type {T} */ + softmaxArr + ); + } + function log_softmax(arr) { + const maxVal = max(arr)[0]; + let sumExps = 0; + for (let i = 0; i < arr.length; ++i) { + sumExps += Math.exp(arr[i] - maxVal); + } + const logSum = Math.log(sumExps); + const logSoftmaxArr = arr.map((x) => x - maxVal - logSum); + return ( + /** @type {T} */ + logSoftmaxArr + ); + } + function dot(arr1, arr2) { + let result = 0; + for (let i = 0; i < arr1.length; ++i) { + result += arr1[i] * arr2[i]; + } + return result; + } + function cos_sim(arr1, arr2) { + const dotProduct = dot(arr1, arr2); + const magnitudeA = magnitude(arr1); + const magnitudeB = magnitude(arr2); + const cosineSimilarity = dotProduct / (magnitudeA * magnitudeB); + return cosineSimilarity; + } + function magnitude(arr) { + return Math.sqrt(arr.reduce((acc, val) => acc + val * val, 0)); + } + function min(arr) { + if (arr.length === 0) throw Error("Array must not be empty"); + let min2 = arr[0]; + let indexOfMin = 0; + for (let i = 1; i < arr.length; ++i) { + if (arr[i] < min2) { + min2 = arr[i]; + indexOfMin = i; + } + } + return [min2, indexOfMin]; + } + function max(arr) { + if (arr.length === 0) throw Error("Array must not be empty"); + let max2 = arr[0]; + let indexOfMax = 0; + for (let i = 1; i < arr.length; ++i) { + if (arr[i] > max2) { + max2 = arr[i]; + indexOfMax = i; + } + } + return [Number(max2), indexOfMax]; + } + function isPowerOfTwo(number) { + return number > 0 && (number & number - 1) === 0; + } + class P2FFT { + /** + * @param {number} size The size of the input array. Must be a power of two larger than 1. + * @throws {Error} FFT size must be a power of two larger than 1. + */ + constructor(size) { + this.size = size | 0; + if (this.size <= 1 || !isPowerOfTwo(this.size)) + throw new Error("FFT size must be a power of two larger than 1"); + this._csize = size << 1; + this.table = new Float64Array(this.size * 2); + for (let i = 0; i < this.table.length; i += 2) { + const angle = Math.PI * i / this.size; + this.table[i] = Math.cos(angle); + this.table[i + 1] = -Math.sin(angle); + } + let power = 0; + for (let t = 1; this.size > t; t <<= 1) + ++power; + this._width = power % 2 === 0 ? power - 1 : power; + this._bitrev = new Int32Array(1 << this._width); + for (let j = 0; j < this._bitrev.length; ++j) { + this._bitrev[j] = 0; + for (let shift = 0; shift < this._width; shift += 2) { + const revShift = this._width - shift - 2; + this._bitrev[j] |= (j >>> shift & 3) << revShift; + } + } + } + /** + * Create a complex number array with size `2 * size` + * + * @returns {Float64Array} A complex number array with size `2 * size` + */ + createComplexArray() { + return new Float64Array(this._csize); + } + /** + * Converts a complex number representation stored in a Float64Array to an array of real numbers. + * + * @param {Float64Array} complex The complex number representation to be converted. + * @param {number[]} [storage] An optional array to store the result in. + * @returns {number[]} An array of real numbers representing the input complex number representation. + */ + fromComplexArray(complex, storage) { + const res = storage || new Array(complex.length >>> 1); + for (let i = 0; i < complex.length; i += 2) + res[i >>> 1] = complex[i]; + return res; + } + /** + * Convert a real-valued input array to a complex-valued output array. + * @param {Float64Array} input The real-valued input array. + * @param {Float64Array} [storage] Optional buffer to store the output array. + * @returns {Float64Array} The complex-valued output array. + */ + toComplexArray(input, storage) { + const res = storage || this.createComplexArray(); + for (let i = 0; i < res.length; i += 2) { + res[i] = input[i >>> 1]; + res[i + 1] = 0; + } + return res; + } + /** + * Performs a Fast Fourier Transform (FFT) on the given input data and stores the result in the output buffer. + * + * @param {Float64Array} out The output buffer to store the result. + * @param {Float64Array} data The input data to transform. + * + * @throws {Error} Input and output buffers must be different. + * + * @returns {void} + */ + transform(out, data) { + if (out === data) + throw new Error("Input and output buffers must be different"); + this._transform4( + out, + data, + 1 + /* DONE */ + ); + } + /** + * Performs a real-valued forward FFT on the given input buffer and stores the result in the given output buffer. + * The input buffer must contain real values only, while the output buffer will contain complex values. The input and + * output buffers must be different. + * + * @param {Float64Array} out The output buffer. + * @param {Float64Array} data The input buffer containing real values. + * + * @throws {Error} If the input and output buffers are the same. + */ + realTransform(out, data) { + if (out === data) + throw new Error("Input and output buffers must be different"); + this._realTransform4( + out, + data, + 1 + /* DONE */ + ); + } + /** + * Performs an inverse FFT transformation on the given `data` array, and stores the result in `out`. + * The `out` array must be a different buffer than the `data` array. The `out` array will contain the + * result of the transformation. The `data` array will not be modified. + * + * @param {Float64Array} out The output buffer for the transformed data. + * @param {Float64Array} data The input data to transform. + * @throws {Error} If `out` and `data` refer to the same buffer. + * @returns {void} + */ + inverseTransform(out, data) { + if (out === data) + throw new Error("Input and output buffers must be different"); + this._transform4( + out, + data, + -1 + /* DONE */ + ); + for (let i = 0; i < out.length; ++i) + out[i] /= this.size; + } + /** + * Performs a radix-4 implementation of a discrete Fourier transform on a given set of data. + * + * @param {Float64Array} out The output buffer for the transformed data. + * @param {Float64Array} data The input buffer of data to be transformed. + * @param {number} inv A scaling factor to apply to the transform. + * @returns {void} + */ + _transform4(out, data, inv) { + const size = this._csize; + const width = this._width; + let step = 1 << width; + let len = size / step << 1; + let outOff; + let t; + const bitrev = this._bitrev; + if (len === 4) { + for (outOff = 0, t = 0; outOff < size; outOff += len, ++t) { + const off = bitrev[t]; + this._singleTransform2(data, out, outOff, off, step); + } + } else { + for (outOff = 0, t = 0; outOff < size; outOff += len, ++t) { + const off = bitrev[t]; + this._singleTransform4(data, out, outOff, off, step, inv); + } + } + const table = this.table; + for (step >>= 2; step >= 2; step >>= 2) { + len = size / step << 1; + const quarterLen = len >>> 2; + for (outOff = 0; outOff < size; outOff += len) { + const limit = outOff + quarterLen - 1; + for (let i = outOff, k = 0; i < limit; i += 2, k += step) { + const A = i; + const B = A + quarterLen; + const C = B + quarterLen; + const D = C + quarterLen; + const Ar = out[A]; + const Ai = out[A + 1]; + const Br = out[B]; + const Bi = out[B + 1]; + const Cr = out[C]; + const Ci = out[C + 1]; + const Dr = out[D]; + const Di = out[D + 1]; + const tableBr = table[k]; + const tableBi = inv * table[k + 1]; + const MBr = Br * tableBr - Bi * tableBi; + const MBi = Br * tableBi + Bi * tableBr; + const tableCr = table[2 * k]; + const tableCi = inv * table[2 * k + 1]; + const MCr = Cr * tableCr - Ci * tableCi; + const MCi = Cr * tableCi + Ci * tableCr; + const tableDr = table[3 * k]; + const tableDi = inv * table[3 * k + 1]; + const MDr = Dr * tableDr - Di * tableDi; + const MDi = Dr * tableDi + Di * tableDr; + const T0r = Ar + MCr; + const T0i = Ai + MCi; + const T1r = Ar - MCr; + const T1i = Ai - MCi; + const T2r = MBr + MDr; + const T2i = MBi + MDi; + const T3r = inv * (MBr - MDr); + const T3i = inv * (MBi - MDi); + out[A] = T0r + T2r; + out[A + 1] = T0i + T2i; + out[B] = T1r + T3i; + out[B + 1] = T1i - T3r; + out[C] = T0r - T2r; + out[C + 1] = T0i - T2i; + out[D] = T1r - T3i; + out[D + 1] = T1i + T3r; + } + } + } + } + /** + * Performs a radix-2 implementation of a discrete Fourier transform on a given set of data. + * + * @param {Float64Array} data The input buffer of data to be transformed. + * @param {Float64Array} out The output buffer for the transformed data. + * @param {number} outOff The offset at which to write the output data. + * @param {number} off The offset at which to begin reading the input data. + * @param {number} step The step size for indexing the input data. + * @returns {void} + */ + _singleTransform2(data, out, outOff, off, step) { + const evenR = data[off]; + const evenI = data[off + 1]; + const oddR = data[off + step]; + const oddI = data[off + step + 1]; + out[outOff] = evenR + oddR; + out[outOff + 1] = evenI + oddI; + out[outOff + 2] = evenR - oddR; + out[outOff + 3] = evenI - oddI; + } + /** + * Performs radix-4 transformation on input data of length 8 + * + * @param {Float64Array} data Input data array of length 8 + * @param {Float64Array} out Output data array of length 8 + * @param {number} outOff Index of output array to start writing from + * @param {number} off Index of input array to start reading from + * @param {number} step Step size between elements in input array + * @param {number} inv Scaling factor for inverse transform + * + * @returns {void} + */ + _singleTransform4(data, out, outOff, off, step, inv) { + const step2 = step * 2; + const step3 = step * 3; + const Ar = data[off]; + const Ai = data[off + 1]; + const Br = data[off + step]; + const Bi = data[off + step + 1]; + const Cr = data[off + step2]; + const Ci = data[off + step2 + 1]; + const Dr = data[off + step3]; + const Di = data[off + step3 + 1]; + const T0r = Ar + Cr; + const T0i = Ai + Ci; + const T1r = Ar - Cr; + const T1i = Ai - Ci; + const T2r = Br + Dr; + const T2i = Bi + Di; + const T3r = inv * (Br - Dr); + const T3i = inv * (Bi - Di); + out[outOff] = T0r + T2r; + out[outOff + 1] = T0i + T2i; + out[outOff + 2] = T1r + T3i; + out[outOff + 3] = T1i - T3r; + out[outOff + 4] = T0r - T2r; + out[outOff + 5] = T0i - T2i; + out[outOff + 6] = T1r - T3i; + out[outOff + 7] = T1i + T3r; + } + /** + * Real input radix-4 implementation + * @param {Float64Array} out Output array for the transformed data + * @param {Float64Array} data Input array of real data to be transformed + * @param {number} inv The scale factor used to normalize the inverse transform + */ + _realTransform4(out, data, inv) { + const size = this._csize; + const width = this._width; + let step = 1 << width; + let len = size / step << 1; + let outOff; + let t; + const bitrev = this._bitrev; + if (len === 4) { + for (outOff = 0, t = 0; outOff < size; outOff += len, ++t) { + const off = bitrev[t]; + this._singleRealTransform2(data, out, outOff, off >>> 1, step >>> 1); + } + } else { + for (outOff = 0, t = 0; outOff < size; outOff += len, ++t) { + const off = bitrev[t]; + this._singleRealTransform4(data, out, outOff, off >>> 1, step >>> 1, inv); + } + } + const table = this.table; + for (step >>= 2; step >= 2; step >>= 2) { + len = size / step << 1; + const halfLen = len >>> 1; + const quarterLen = halfLen >>> 1; + const hquarterLen = quarterLen >>> 1; + for (outOff = 0; outOff < size; outOff += len) { + for (let i = 0, k = 0; i <= hquarterLen; i += 2, k += step) { + const A = outOff + i; + const B = A + quarterLen; + const C = B + quarterLen; + const D = C + quarterLen; + const Ar = out[A]; + const Ai = out[A + 1]; + const Br = out[B]; + const Bi = out[B + 1]; + const Cr = out[C]; + const Ci = out[C + 1]; + const Dr = out[D]; + const Di = out[D + 1]; + const MAr = Ar; + const MAi = Ai; + const tableBr = table[k]; + const tableBi = inv * table[k + 1]; + const MBr = Br * tableBr - Bi * tableBi; + const MBi = Br * tableBi + Bi * tableBr; + const tableCr = table[2 * k]; + const tableCi = inv * table[2 * k + 1]; + const MCr = Cr * tableCr - Ci * tableCi; + const MCi = Cr * tableCi + Ci * tableCr; + const tableDr = table[3 * k]; + const tableDi = inv * table[3 * k + 1]; + const MDr = Dr * tableDr - Di * tableDi; + const MDi = Dr * tableDi + Di * tableDr; + const T0r = MAr + MCr; + const T0i = MAi + MCi; + const T1r = MAr - MCr; + const T1i = MAi - MCi; + const T2r = MBr + MDr; + const T2i = MBi + MDi; + const T3r = inv * (MBr - MDr); + const T3i = inv * (MBi - MDi); + out[A] = T0r + T2r; + out[A + 1] = T0i + T2i; + out[B] = T1r + T3i; + out[B + 1] = T1i - T3r; + if (i === 0) { + out[C] = T0r - T2r; + out[C + 1] = T0i - T2i; + continue; + } + if (i === hquarterLen) + continue; + const SA = outOff + quarterLen - i; + const SB = outOff + halfLen - i; + out[SA] = T1r - inv * T3i; + out[SA + 1] = -T1i - inv * T3r; + out[SB] = T0r - inv * T2r; + out[SB + 1] = -T0i + inv * T2i; + } + } + } + const half = size >>> 1; + for (let i = 2; i < half; i += 2) { + out[size - i] = out[i]; + out[size - i + 1] = -out[i + 1]; + } + } + /** + * Performs a single real input radix-2 transformation on the provided data + * + * @param {Float64Array} data The input data array + * @param {Float64Array} out The output data array + * @param {number} outOff The output offset + * @param {number} off The input offset + * @param {number} step The step + * + * @returns {void} + */ + _singleRealTransform2(data, out, outOff, off, step) { + const evenR = data[off]; + const oddR = data[off + step]; + out[outOff] = evenR + oddR; + out[outOff + 1] = 0; + out[outOff + 2] = evenR - oddR; + out[outOff + 3] = 0; + } + /** + * Computes a single real-valued transform using radix-4 algorithm. + * This method is only called for len=8. + * + * @param {Float64Array} data The input data array. + * @param {Float64Array} out The output data array. + * @param {number} outOff The offset into the output array. + * @param {number} off The offset into the input array. + * @param {number} step The step size for the input array. + * @param {number} inv The value of inverse. + */ + _singleRealTransform4(data, out, outOff, off, step, inv) { + const step2 = step * 2; + const step3 = step * 3; + const Ar = data[off]; + const Br = data[off + step]; + const Cr = data[off + step2]; + const Dr = data[off + step3]; + const T0r = Ar + Cr; + const T1r = Ar - Cr; + const T2r = Br + Dr; + const T3r = inv * (Br - Dr); + out[outOff] = T0r + T2r; + out[outOff + 1] = 0; + out[outOff + 2] = T1r; + out[outOff + 3] = -T3r; + out[outOff + 4] = T0r - T2r; + out[outOff + 5] = 0; + out[outOff + 6] = T1r; + out[outOff + 7] = T3r; + } + } + class NP2FFT { + /** + * Constructs a new NP2FFT object. + * @param {number} fft_length The length of the FFT + */ + constructor(fft_length) { + const a = 2 * (fft_length - 1); + const b = 2 * (2 * fft_length - 1); + const nextP2 = 2 ** Math.ceil(Math.log2(b)); + this.bufferSize = nextP2; + this._a = a; + const chirp = new Float64Array(b); + const ichirp = new Float64Array(nextP2); + this._chirpBuffer = new Float64Array(nextP2); + this._buffer1 = new Float64Array(nextP2); + this._buffer2 = new Float64Array(nextP2); + this._outBuffer1 = new Float64Array(nextP2); + this._outBuffer2 = new Float64Array(nextP2); + const theta = -2 * Math.PI / fft_length; + const baseR = Math.cos(theta); + const baseI = Math.sin(theta); + for (let i = 0; i < b >> 1; ++i) { + const e = (i + 1 - fft_length) ** 2 / 2; + const result_mod = Math.sqrt(baseR ** 2 + baseI ** 2) ** e; + const result_arg = e * Math.atan2(baseI, baseR); + const i2 = 2 * i; + chirp[i2] = result_mod * Math.cos(result_arg); + chirp[i2 + 1] = result_mod * Math.sin(result_arg); + ichirp[i2] = chirp[i2]; + ichirp[i2 + 1] = -chirp[i2 + 1]; + } + this._slicedChirpBuffer = chirp.subarray(a, b); + this._f = new P2FFT(nextP2 >> 1); + this._f.transform(this._chirpBuffer, ichirp); + } + _transform(output, input, real) { + const ib1 = this._buffer1; + const ib2 = this._buffer2; + const ob2 = this._outBuffer1; + const ob3 = this._outBuffer2; + const cb = this._chirpBuffer; + const sb = this._slicedChirpBuffer; + const a = this._a; + if (real) { + for (let j = 0; j < sb.length; j += 2) { + const j2 = j + 1; + const j3 = j >> 1; + const a_real = input[j3]; + ib1[j] = a_real * sb[j]; + ib1[j2] = a_real * sb[j2]; + } + } else { + for (let j = 0; j < sb.length; j += 2) { + const j2 = j + 1; + ib1[j] = input[j] * sb[j] - input[j2] * sb[j2]; + ib1[j2] = input[j] * sb[j2] + input[j2] * sb[j]; + } + } + this._f.transform(ob2, ib1); + for (let j = 0; j < cb.length; j += 2) { + const j2 = j + 1; + ib2[j] = ob2[j] * cb[j] - ob2[j2] * cb[j2]; + ib2[j2] = ob2[j] * cb[j2] + ob2[j2] * cb[j]; + } + this._f.inverseTransform(ob3, ib2); + for (let j = 0; j < ob3.length; j += 2) { + const a_real = ob3[j + a]; + const a_imag = ob3[j + a + 1]; + const b_real = sb[j]; + const b_imag = sb[j + 1]; + output[j] = a_real * b_real - a_imag * b_imag; + output[j + 1] = a_real * b_imag + a_imag * b_real; + } + } + transform(output, input) { + this._transform(output, input, false); + } + realTransform(output, input) { + this._transform(output, input, true); + } + } + class FFT { + constructor(fft_length) { + this.fft_length = fft_length; + this.isPowerOfTwo = isPowerOfTwo(fft_length); + if (this.isPowerOfTwo) { + this.fft = new P2FFT(fft_length); + this.outputBufferSize = 2 * fft_length; + } else { + this.fft = new NP2FFT(fft_length); + this.outputBufferSize = this.fft.bufferSize; + } + } + realTransform(out, input) { + this.fft.realTransform(out, input); + } + transform(out, input) { + this.fft.transform(out, input); + } + } + function medianFilter(data, windowSize) { + if (windowSize % 2 === 0 || windowSize <= 0) { + throw new Error("Window size must be a positive odd number"); + } + const outputArray = new data.constructor(data.length); + const buffer = new data.constructor(windowSize); + const halfWindowSize = Math.floor(windowSize / 2); + for (let i = 0; i < data.length; ++i) { + let valuesIndex = 0; + for (let j = -halfWindowSize; j <= halfWindowSize; ++j) { + let index = i + j; + if (index < 0) { + index = Math.abs(index); + } else if (index >= data.length) { + index = 2 * (data.length - 1) - index; + } + buffer[valuesIndex++] = data[index]; + } + buffer.sort(); + outputArray[i] = buffer[halfWindowSize]; + } + return outputArray; + } + function round(num, decimals) { + const pow = Math.pow(10, decimals); + return Math.round(num * pow) / pow; + } + function bankers_round(x) { + const r = Math.round(x); + const br = Math.abs(x) % 1 === 0.5 ? r % 2 === 0 ? r : r - 1 : r; + return br; + } + function dynamic_time_warping(matrix) { + const output_length = matrix.length; + const input_length = matrix[0].length; + const outputShape = [output_length + 1, input_length + 1]; + const cost = Array.from( + { length: outputShape[0] }, + () => Array(outputShape[1]).fill(Infinity) + ); + cost[0][0] = 0; + const trace = Array.from( + { length: outputShape[0] }, + () => Array(outputShape[1]).fill(-1) + ); + for (let j2 = 1; j2 < outputShape[1]; ++j2) { + for (let i2 = 1; i2 < outputShape[0]; ++i2) { + const c0 = cost[i2 - 1][j2 - 1]; + const c1 = cost[i2 - 1][j2]; + const c2 = cost[i2][j2 - 1]; + let c, t; + if (c0 < c1 && c0 < c2) { + c = c0; + t = 0; + } else if (c1 < c0 && c1 < c2) { + c = c1; + t = 1; + } else { + c = c2; + t = 2; + } + cost[i2][j2] = matrix[i2 - 1][j2 - 1] + c; + trace[i2][j2] = t; + } + } + for (let i2 = 0; i2 < outputShape[1]; ++i2) { + trace[0][i2] = 2; + } + for (let i2 = 0; i2 < outputShape[0]; ++i2) { + trace[i2][0] = 1; + } + let i = output_length; + let j = input_length; + let text_indices = []; + let time_indices = []; + while (i > 0 || j > 0) { + text_indices.push(i - 1); + time_indices.push(j - 1); + switch (trace[i][j]) { + case 0: + --i; + --j; + break; + case 1: + --i; + break; + case 2: + --j; + break; + default: + throw new Error( + `Internal error in dynamic time warping. Unexpected trace[${i}, ${j}]. Please file a bug report.` + ); + } + } + text_indices.reverse(); + time_indices.reverse(); + return [text_indices, time_indices]; + } + } + ), + /***/ + "./src/utils/tensor.js": ( + /*!*****************************!*\ + !*** ./src/utils/tensor.js ***! + \*****************************/ + /***/ + (__unused_webpack___webpack_module__, __webpack_exports__2, __webpack_require__2) => { + __webpack_require__2.r(__webpack_exports__2); + __webpack_require__2.d(__webpack_exports__2, { + /* harmony export */ + Tensor: () => ( + /* binding */ + Tensor + ), + /* harmony export */ + cat: () => ( + /* binding */ + cat + ), + /* harmony export */ + full: () => ( + /* binding */ + full + ), + /* harmony export */ + full_like: () => ( + /* binding */ + full_like + ), + /* harmony export */ + interpolate: () => ( + /* binding */ + interpolate + ), + /* harmony export */ + interpolate_4d: () => ( + /* binding */ + interpolate_4d + ), + /* harmony export */ + layer_norm: () => ( + /* binding */ + layer_norm + ), + /* harmony export */ + matmul: () => ( + /* binding */ + matmul + ), + /* harmony export */ + mean: () => ( + /* binding */ + mean + ), + /* harmony export */ + mean_pooling: () => ( + /* binding */ + mean_pooling + ), + /* harmony export */ + ones: () => ( + /* binding */ + ones + ), + /* harmony export */ + ones_like: () => ( + /* binding */ + ones_like + ), + /* harmony export */ + permute: () => ( + /* binding */ + permute + ), + /* harmony export */ + quantize_embeddings: () => ( + /* binding */ + quantize_embeddings + ), + /* harmony export */ + rfft: () => ( + /* binding */ + rfft + ), + /* harmony export */ + stack: () => ( + /* binding */ + stack + ), + /* harmony export */ + std_mean: () => ( + /* binding */ + std_mean + ), + /* harmony export */ + topk: () => ( + /* binding */ + topk + ), + /* harmony export */ + zeros: () => ( + /* binding */ + zeros + ), + /* harmony export */ + zeros_like: () => ( + /* binding */ + zeros_like + ) + /* harmony export */ + }); + var _maths_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__2( + /*! ./maths.js */ + "./src/utils/maths.js" + ); + var _backends_onnx_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__2( + /*! ../backends/onnx.js */ + "./src/backends/onnx.js" + ); + var _ops_registry_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__2( + /*! ../ops/registry.js */ + "./src/ops/registry.js" + ); + const DataTypeMap = Object.freeze({ + float32: Float32Array, + float16: Uint16Array, + float64: Float64Array, + string: Array, + // string[] + int8: Int8Array, + uint8: Uint8Array, + int16: Int16Array, + uint16: Uint16Array, + int32: Int32Array, + uint32: Uint32Array, + int64: BigInt64Array, + uint64: BigUint64Array, + bool: Uint8Array + }); + class Tensor { + /** + * Create a new Tensor or copy an existing Tensor. + * @param {[DataType, DataArray, number[]]|[ONNXTensor]} args + */ + constructor(...args) { + __publicField(this, "ort_tensor"); + if ((0, _backends_onnx_js__WEBPACK_IMPORTED_MODULE_1__.isONNXTensor)(args[0])) { + this.ort_tensor = /** @type {ONNXTensor} */ + args[0]; + } else { + this.ort_tensor = new _backends_onnx_js__WEBPACK_IMPORTED_MODULE_1__.Tensor( + /** @type {DataType} */ + args[0], + /** @type {Exclude} */ + args[1], + args[2] + ); + } + return new Proxy(this, { + get: (obj, key) => { + if (typeof key === "string") { + let index = Number(key); + if (Number.isInteger(index)) { + return obj._getitem(index); + } + } + return obj[key]; + }, + set: (obj, key, value) => { + return obj[key] = value; + } + }); + } + /** @type {number[]} Dimensions of the tensor. */ + get dims() { + return this.ort_tensor.dims; + } + set dims(value) { + this.ort_tensor.dims = value; + } + /** @type {DataType} Type of the tensor. */ + get type() { + return this.ort_tensor.type; + } + /** @type {DataArray} The data stored in the tensor. */ + get data() { + return this.ort_tensor.data; + } + /** @type {number} The number of elements in the tensor. */ + get size() { + return this.ort_tensor.size; + } + /** @type {string} The location of the tensor data. */ + get location() { + return this.ort_tensor.location; + } + dispose() { + this.ort_tensor.dispose(); + } + /** + * Returns an iterator object for iterating over the tensor data in row-major order. + * If the tensor has more than one dimension, the iterator will yield subarrays. + * @returns {Iterator} An iterator object for iterating over the tensor data in row-major order. + */ + *[Symbol.iterator]() { + const [iterLength, ...iterDims] = this.dims; + if (iterDims.length > 0) { + const iterSize = iterDims.reduce((a, b) => a * b); + for (let i = 0; i < iterLength; ++i) { + yield this._subarray(i, iterSize, iterDims); + } + } else { + yield* this.data; + } + } + /** + * Index into a Tensor object. + * @param {number} index The index to access. + * @returns {Tensor} The data at the specified index. + */ + _getitem(index) { + const [iterLength, ...iterDims] = this.dims; + index = safeIndex(index, iterLength); + if (iterDims.length > 0) { + const iterSize = iterDims.reduce((a, b) => a * b); + return this._subarray(index, iterSize, iterDims); + } else { + return new Tensor(this.type, [this.data[index]], iterDims); + } + } + /** + * @param {number|bigint} item The item to search for in the tensor + * @returns {number} The index of the first occurrence of item in the tensor data. + */ + indexOf(item) { + const this_data = this.data; + for (let index = 0; index < this_data.length; ++index) { + if (this_data[index] == item) { + return index; + } + } + return -1; + } + /** + * @param {number} index + * @param {number} iterSize + * @param {any} iterDims + * @returns {Tensor} + */ + _subarray(index, iterSize, iterDims) { + const o1 = index * iterSize; + const o2 = (index + 1) * iterSize; + const data = "subarray" in this.data ? this.data.subarray(o1, o2) : this.data.slice(o1, o2); + return new Tensor(this.type, data, iterDims); + } + /** + * Returns the value of this tensor as a standard JavaScript Number. This only works + * for tensors with one element. For other cases, see `Tensor.tolist()`. + * @returns {number|bigint} The value of this tensor as a standard JavaScript Number. + * @throws {Error} If the tensor has more than one element. + */ + item() { + const this_data = this.data; + if (this_data.length !== 1) { + throw new Error(`a Tensor with ${this_data.length} elements cannot be converted to Scalar`); + } + return this_data[0]; + } + /** + * Convert tensor data to a n-dimensional JS list + * @returns {Array} + */ + tolist() { + return reshape(this.data, this.dims); + } + /** + * Return a new Tensor with the sigmoid function applied to each element. + * @returns {Tensor} The tensor with the sigmoid function applied. + */ + sigmoid() { + return this.clone().sigmoid_(); + } + /** + * Applies the sigmoid function to the tensor in place. + * @returns {Tensor} Returns `this`. + */ + sigmoid_() { + const this_data = this.data; + for (let i = 0; i < this_data.length; ++i) { + this_data[i] = 1 / (1 + Math.exp(-this_data[i])); + } + return this; + } + /** + * Return a new Tensor with a callback function applied to each element. + * @param {Function} callback - The function to apply to each element. It should take three arguments: + * the current element, its index, and the tensor's data array. + * @returns {Tensor} A new Tensor with the callback function applied to each element. + */ + map(callback) { + return this.clone().map_(callback); + } + /** + * Apply a callback function to each element of the tensor in place. + * @param {Function} callback - The function to apply to each element. It should take three arguments: + * the current element, its index, and the tensor's data array. + * @returns {Tensor} Returns `this`. + */ + map_(callback) { + const this_data = this.data; + for (let i = 0; i < this_data.length; ++i) { + this_data[i] = callback(this_data[i], i, this_data); + } + return this; + } + /** + * Return a new Tensor with every element multiplied by a constant. + * @param {number} val The value to multiply by. + * @returns {Tensor} The new tensor. + */ + mul(val) { + return this.clone().mul_(val); + } + /** + * Multiply the tensor by a constant in place. + * @param {number} val The value to multiply by. + * @returns {Tensor} Returns `this`. + */ + mul_(val) { + const this_data = this.data; + for (let i = 0; i < this_data.length; ++i) { + this_data[i] *= val; + } + return this; + } + /** + * Return a new Tensor with every element divided by a constant. + * @param {number} val The value to divide by. + * @returns {Tensor} The new tensor. + */ + div(val) { + return this.clone().div_(val); + } + /** + * Divide the tensor by a constant in place. + * @param {number} val The value to divide by. + * @returns {Tensor} Returns `this`. + */ + div_(val) { + const this_data = this.data; + for (let i = 0; i < this_data.length; ++i) { + this_data[i] /= val; + } + return this; + } + /** + * Return a new Tensor with every element added by a constant. + * @param {number} val The value to add by. + * @returns {Tensor} The new tensor. + */ + add(val) { + return this.clone().add_(val); + } + /** + * Add the tensor by a constant in place. + * @param {number} val The value to add by. + * @returns {Tensor} Returns `this`. + */ + add_(val) { + const this_data = this.data; + for (let i = 0; i < this_data.length; ++i) { + this_data[i] += val; + } + return this; + } + /** + * Return a new Tensor with every element subtracted by a constant. + * @param {number} val The value to subtract by. + * @returns {Tensor} The new tensor. + */ + sub(val) { + return this.clone().sub_(val); + } + /** + * Subtract the tensor by a constant in place. + * @param {number} val The value to subtract by. + * @returns {Tensor} Returns `this`. + */ + sub_(val) { + const this_data = this.data; + for (let i = 0; i < this_data.length; ++i) { + this_data[i] -= val; + } + return this; + } + clone() { + return new Tensor(this.type, this.data.slice(), this.dims.slice()); + } + slice(...slices) { + const newTensorDims = []; + const newOffsets = []; + for (let sliceIndex = 0; sliceIndex < this.dims.length; ++sliceIndex) { + let slice = slices[sliceIndex]; + if (slice === null || slice === void 0) { + newOffsets.push([0, this.dims[sliceIndex]]); + newTensorDims.push(this.dims[sliceIndex]); + } else if (typeof slice === "number") { + slice = safeIndex(slice, this.dims[sliceIndex], sliceIndex); + newOffsets.push([slice, slice + 1]); + } else if (Array.isArray(slice) && slice.length === 2) { + let [start, end] = slice; + start = start === null ? 0 : safeIndex(start, this.dims[sliceIndex], sliceIndex, false); + end = end === null ? this.dims[sliceIndex] : safeIndex(end, this.dims[sliceIndex], sliceIndex, false); + if (start > end) { + throw new Error(`Invalid slice: ${slice}`); + } + const offsets = [ + Math.max(start, 0), + Math.min(end, this.dims[sliceIndex]) + ]; + newOffsets.push(offsets); + newTensorDims.push(offsets[1] - offsets[0]); + } else { + throw new Error(`Invalid slice: ${slice}`); + } + } + const newDims = newOffsets.map(([start, end]) => end - start); + const newBufferSize = newDims.reduce((a, b) => a * b); + const this_data = this.data; + const data = new this_data.constructor(newBufferSize); + const stride = this.stride(); + for (let i = 0; i < newBufferSize; ++i) { + let originalIndex = 0; + for (let j = newDims.length - 1, num = i; j >= 0; --j) { + const size = newDims[j]; + originalIndex += (num % size + newOffsets[j][0]) * stride[j]; + num = Math.floor(num / size); + } + data[i] = this_data[originalIndex]; + } + return new Tensor(this.type, data, newTensorDims); + } + /** + * Return a permuted version of this Tensor, according to the provided dimensions. + * @param {...number} dims Dimensions to permute. + * @returns {Tensor} The permuted tensor. + */ + permute(...dims) { + return permute(this, dims); + } + // TODO: implement transpose. For now (backwards compatibility), it's just an alias for permute() + transpose(...dims) { + return this.permute(...dims); + } + // TODO add .max() and .min() methods + /** + * Returns the sum of each row of the input tensor in the given dimension dim. + * + * @param {number} [dim=null] The dimension or dimensions to reduce. If `null`, all dimensions are reduced. + * @param {boolean} keepdim Whether the output tensor has `dim` retained or not. + * @returns The summed tensor + */ + sum(dim = null, keepdim = false) { + return this.norm(1, dim, keepdim); + } + /** + * Returns the matrix norm or vector norm of a given tensor. + * @param {number|string} [p='fro'] The order of norm + * @param {number} [dim=null] Specifies which dimension of the tensor to calculate the norm across. + * If dim is None, the norm will be calculated across all dimensions of input. + * @param {boolean} [keepdim=false] Whether the output tensors have dim retained or not. + * @returns {Tensor} The norm of the tensor. + */ + norm(p = "fro", dim = null, keepdim = false) { + if (p === "fro") { + p = 2; + } else if (typeof p === "string") { + throw Error(`Unsupported norm: ${p}`); + } + const this_data = this.data; + if (dim === null) { + let val = this_data.reduce((a, b) => a + b ** p, 0) ** (1 / p); + return new Tensor(this.type, [val], []); + } + dim = safeIndex(dim, this.dims.length); + const resultDims = this.dims.slice(); + resultDims[dim] = 1; + const result = new this_data.constructor(this_data.length / this.dims[dim]); + for (let i = 0; i < this_data.length; ++i) { + let resultIndex = 0; + for (let j = this.dims.length - 1, num = i, resultMultiplier = 1; j >= 0; --j) { + const size = this.dims[j]; + if (j !== dim) { + const index = num % size; + resultIndex += index * resultMultiplier; + resultMultiplier *= resultDims[j]; + } + num = Math.floor(num / size); + } + result[resultIndex] += this_data[i] ** p; + } + if (p !== 1) { + for (let i = 0; i < result.length; ++i) { + result[i] = result[i] ** (1 / p); + } + } + if (!keepdim) { + resultDims.splice(dim, 1); + } + return new Tensor(this.type, result, resultDims); + } + /** + * Performs `L_p` normalization of inputs over specified dimension. Operates in place. + * @param {number} [p=2] The exponent value in the norm formulation + * @param {number} [dim=1] The dimension to reduce + * @returns {Tensor} `this` for operation chaining. + */ + normalize_(p = 2, dim = 1) { + dim = safeIndex(dim, this.dims.length); + const norm = this.norm(p, dim, true); + const this_data = this.data; + const norm_data = norm.data; + for (let i = 0; i < this_data.length; ++i) { + let resultIndex = 0; + for (let j = this.dims.length - 1, num = i, resultMultiplier = 1; j >= 0; --j) { + const size = this.dims[j]; + if (j !== dim) { + const index = num % size; + resultIndex += index * resultMultiplier; + resultMultiplier *= this.dims[j]; + } + num = Math.floor(num / size); + } + this_data[i] /= norm_data[resultIndex]; + } + return this; + } + /** + * Performs `L_p` normalization of inputs over specified dimension. + * @param {number} [p=2] The exponent value in the norm formulation + * @param {number} [dim=1] The dimension to reduce + * @returns {Tensor} The normalized tensor. + */ + normalize(p = 2, dim = 1) { + return this.clone().normalize_(p, dim); + } + /** + * Compute and return the stride of this tensor. + * Stride is the jump necessary to go from one element to the next one in the specified dimension dim. + * @returns {number[]} The stride of this tensor. + */ + stride() { + return dimsToStride(this.dims); + } + /** + * Returns a tensor with all specified dimensions of input of size 1 removed. + * + * NOTE: The returned tensor shares the storage with the input tensor, so changing the contents of one will change the contents of the other. + * If you would like a copy, use `tensor.clone()` before squeezing. + * + * @param {number} [dim=null] If given, the input will be squeezed only in the specified dimensions. + * @returns {Tensor} The squeezed tensor + */ + squeeze(dim = null) { + return new Tensor( + this.type, + this.data, + calc_squeeze_dims(this.dims, dim) + ); + } + /** + * In-place version of @see {@link Tensor.squeeze} + */ + squeeze_(dim = null) { + this.dims = calc_squeeze_dims(this.dims, dim); + return this; + } + /** + * Returns a new tensor with a dimension of size one inserted at the specified position. + * + * NOTE: The returned tensor shares the same underlying data with this tensor. + * + * @param {number} dim The index at which to insert the singleton dimension + * @returns {Tensor} The unsqueezed tensor + */ + unsqueeze(dim = null) { + return new Tensor( + this.type, + this.data, + calc_unsqueeze_dims(this.dims, dim) + ); + } + /** + * In-place version of @see {@link Tensor.unsqueeze} + */ + unsqueeze_(dim = null) { + this.dims = calc_unsqueeze_dims(this.dims, dim); + return this; + } + /** + * In-place version of @see {@link Tensor.flatten} + */ + flatten_(start_dim = 0, end_dim = -1) { + end_dim = (end_dim + this.dims.length) % this.dims.length; + let dimsToKeepBefore = this.dims.slice(0, start_dim); + let dimsToFlatten = this.dims.slice(start_dim, end_dim + 1); + let dimsToKeepAfter = this.dims.slice(end_dim + 1); + this.dims = [...dimsToKeepBefore, dimsToFlatten.reduce((a, b) => a * b, 1), ...dimsToKeepAfter]; + return this; + } + /** + * Flattens input by reshaping it into a one-dimensional tensor. + * If `start_dim` or `end_dim` are passed, only dimensions starting with `start_dim` + * and ending with `end_dim` are flattened. The order of elements in input is unchanged. + * @param {number} start_dim the first dim to flatten + * @param {number} end_dim the last dim to flatten + * @returns {Tensor} The flattened tensor. + */ + flatten(start_dim = 0, end_dim = -1) { + return this.clone().flatten_(start_dim, end_dim); + } + /** + * Returns a new tensor with the same data as the `self` tensor but of a different `shape`. + * @param {...number} dims the desired size + * @returns {Tensor} The tensor with the same data but different shape + */ + view(...dims) { + let inferredIndex = -1; + for (let i = 0; i < dims.length; ++i) { + if (dims[i] === -1) { + if (inferredIndex !== -1) { + throw new Error("Only one dimension can be inferred"); + } + inferredIndex = i; + } + } + const this_data = this.data; + if (inferredIndex !== -1) { + const productOther = dims.reduce((product, curr, index) => { + return index !== inferredIndex ? product * curr : product; + }, 1); + dims[inferredIndex] = this_data.length / productOther; + } + return new Tensor(this.type, this_data, dims); + } + neg_() { + const this_data = this.data; + for (let i = 0; i < this_data.length; ++i) { + this_data[i] = -this_data[i]; + } + return this; + } + neg() { + return this.clone().neg_(); + } + /** + * In-place version of @see {@link Tensor.clamp} + */ + clamp_(min, max) { + const this_data = this.data; + for (let i = 0; i < this_data.length; ++i) { + this_data[i] = Math.min(Math.max(this_data[i], min), max); + } + return this; + } + /** + * Clamps all elements in input into the range [ min, max ] + * @param {number} min lower-bound of the range to be clamped to + * @param {number} max upper-bound of the range to be clamped to + * @returns {Tensor} the output tensor. + */ + clamp(min, max) { + return this.clone().clamp_(min, max); + } + /** + * In-place version of @see {@link Tensor.round} + */ + round_() { + const this_data = this.data; + for (let i = 0; i < this_data.length; ++i) { + this_data[i] = Math.round(this_data[i]); + } + return this; + } + /** + * Rounds elements of input to the nearest integer. + * @returns {Tensor} the output tensor. + */ + round() { + return this.clone().round_(); + } + mean(dim = null, keepdim = false) { + return mean(this, dim, keepdim); + } + /** + * Performs Tensor dtype conversion. + * @param {DataType} type The desired data type. + * @returns {Tensor} The converted tensor. + */ + to(type) { + if (this.type === type) return this; + if (!DataTypeMap.hasOwnProperty(type)) { + throw new Error(`Unsupported type: ${type}`); + } + return new Tensor(type, DataTypeMap[type].from(this.data), this.dims); + } + } + function reshape(data, dimensions) { + const totalElements = data.length; + const dimensionSize = dimensions.reduce((a, b) => a * b); + if (totalElements !== dimensionSize) { + throw Error(`cannot reshape array of size ${totalElements} into shape (${dimensions})`); + } + let reshapedArray = data; + for (let i = dimensions.length - 1; i >= 0; i--) { + reshapedArray = reshapedArray.reduce((acc, val) => { + let lastArray = acc[acc.length - 1]; + if (lastArray.length < dimensions[i]) { + lastArray.push(val); + } else { + acc.push([val]); + } + return acc; + }, [[]]); + } + return reshapedArray[0]; + } + function permute(tensor, axes) { + const [permutedData, shape] = (0, _maths_js__WEBPACK_IMPORTED_MODULE_0__.permute_data)(tensor.data, tensor.dims, axes); + return new Tensor(tensor.type, permutedData, shape); + } + function interpolate(input, [out_height, out_width], mode = "bilinear", align_corners = false) { + const in_channels = input.dims.at(-3) ?? 1; + const in_height = input.dims.at(-2); + const in_width = input.dims.at(-1); + let output = (0, _maths_js__WEBPACK_IMPORTED_MODULE_0__.interpolate_data)( + /** @type {import('./maths.js').TypedArray}*/ + input.data, + [in_channels, in_height, in_width], + [out_height, out_width], + mode, + align_corners + ); + return new Tensor(input.type, output, [in_channels, out_height, out_width]); + } + async function interpolate_4d(input, { + size = null, + mode = "bilinear" + } = {}) { + if (input.dims.length !== 4) { + throw new Error("`interpolate_4d` currently only supports 4D input."); + } + if (!size) { + throw new Error("`interpolate_4d` requires a `size` argument."); + } + let targetDims; + if (size.length === 2) { + targetDims = [...input.dims.slice(0, 2), ...size]; + } else if (size.length === 3) { + targetDims = [input.dims[0], ...size]; + } else if (size.length === 4) { + targetDims = size; + } else { + throw new Error("`size` must be of length 2, 3, or 4."); + } + let op; + if (mode === "bilinear") { + op = await _ops_registry_js__WEBPACK_IMPORTED_MODULE_2__.TensorOpRegistry.bilinear_interpolate_4d; + } else if (mode === "bicubic") { + op = await _ops_registry_js__WEBPACK_IMPORTED_MODULE_2__.TensorOpRegistry.bicubic_interpolate_4d; + } else { + throw new Error(`Unsupported mode: ${mode}`); + } + const sizeTensor = new Tensor("int64", new BigInt64Array(targetDims.map(BigInt)), [targetDims.length]); + return await op({ x: input, s: sizeTensor }); + } + async function matmul(a, b) { + const op = await _ops_registry_js__WEBPACK_IMPORTED_MODULE_2__.TensorOpRegistry.matmul; + return await op({ a, b }); + } + async function rfft(x, a) { + const op = await _ops_registry_js__WEBPACK_IMPORTED_MODULE_2__.TensorOpRegistry.rfft; + return await op({ x, a }); + } + async function topk(x, k) { + const op = await _ops_registry_js__WEBPACK_IMPORTED_MODULE_2__.TensorOpRegistry.top_k; + if (k === null) { + k = x.dims.at(-1); + } else { + k = Math.min(k, x.dims.at(-1)); + } + return await op({ + x, + k: new Tensor( + "int64", + [BigInt(k)], + [1] + ) + }); + } + function mean_pooling(last_hidden_state, attention_mask) { + const lastHiddenStateData = last_hidden_state.data; + const attentionMaskData = attention_mask.data; + const shape = [last_hidden_state.dims[0], last_hidden_state.dims[2]]; + const returnedData = new lastHiddenStateData.constructor(shape[0] * shape[1]); + const [batchSize, seqLength, embedDim] = last_hidden_state.dims; + let outIndex = 0; + for (let i = 0; i < batchSize; ++i) { + const offset = i * embedDim * seqLength; + for (let k = 0; k < embedDim; ++k) { + let sum = 0; + let count = 0; + const attnMaskOffset = i * seqLength; + const offset2 = offset + k; + for (let j = 0; j < seqLength; ++j) { + const attn = Number(attentionMaskData[attnMaskOffset + j]); + count += attn; + sum += lastHiddenStateData[offset2 + j * embedDim] * attn; + } + const avg = sum / count; + returnedData[outIndex++] = avg; + } + } + return new Tensor( + last_hidden_state.type, + returnedData, + shape + ); + } + function layer_norm(input, normalized_shape, { + eps = 1e-5 + } = {}) { + if (input.dims.length !== 2) { + throw new Error("`layer_norm` currently only supports 2D input."); + } + const [batchSize, featureDim] = input.dims; + if (normalized_shape.length !== 1 && normalized_shape[0] !== featureDim) { + throw new Error("`normalized_shape` must be a 1D array with shape `[input.dims[1]]`."); + } + const [std, mean2] = std_mean(input, 1, 0, true); + const stdData = ( + /** @type {Float32Array} */ + std.data + ); + const meanData = ( + /** @type {Float32Array} */ + mean2.data + ); + const inputData = ( + /** @type {Float32Array} */ + input.data + ); + const returnedData = new inputData.constructor(inputData.length); + for (let i = 0; i < batchSize; ++i) { + const offset = i * featureDim; + for (let j = 0; j < featureDim; ++j) { + const offset2 = offset + j; + returnedData[offset2] = (inputData[offset2] - meanData[i]) / (stdData[i] + eps); + } + } + return new Tensor(input.type, returnedData, input.dims); + } + function calc_squeeze_dims(dims, dim) { + dims = dims.slice(); + if (dim === null) { + dims = dims.filter((d) => d !== 1); + } else if (typeof dim === "number") { + if (dims[dim] === 1) { + dims.splice(dim, 1); + } + } else if (Array.isArray(dim)) { + dims = dims.filter((x, i) => { + return x !== 1 || !dim.includes(i); + }); + } + return dims; + } + function calc_unsqueeze_dims(dims, dim) { + dim = safeIndex(dim, dims.length + 1); + dims = dims.slice(); + dims.splice(dim, 0, 1); + return dims; + } + function safeIndex(index, size, dimension = null, boundsCheck = true) { + if (boundsCheck && (index < -size || index >= size)) { + throw new Error(`IndexError: index ${index} is out of bounds for dimension${dimension === null ? "" : " " + dimension} with size ${size}`); + } + if (index < 0) { + index = (index % size + size) % size; + } + return index; + } + function cat(tensors, dim = 0) { + dim = safeIndex(dim, tensors[0].dims.length); + const resultDims = tensors[0].dims.slice(); + resultDims[dim] = tensors.reduce((a, b) => a + b.dims[dim], 0); + const resultSize = resultDims.reduce((a, b) => a * b, 1); + const result = new tensors[0].data.constructor(resultSize); + const resultType = tensors[0].type; + if (dim === 0) { + let offset = 0; + for (const tensor of tensors) { + const tensorData = tensor.data; + result.set(tensorData, offset); + offset += tensorData.length; + } + } else { + let currentDim = 0; + for (let t = 0; t < tensors.length; ++t) { + const { data, dims } = tensors[t]; + for (let i = 0; i < data.length; ++i) { + let resultIndex = 0; + for (let j = dims.length - 1, num = i, resultMultiplier = 1; j >= 0; --j) { + const size = dims[j]; + let index = num % size; + if (j === dim) { + index += currentDim; + } + resultIndex += index * resultMultiplier; + resultMultiplier *= resultDims[j]; + num = Math.floor(num / size); + } + result[resultIndex] = data[i]; + } + currentDim += dims[dim]; + } + } + return new Tensor(resultType, result, resultDims); + } + function stack(tensors, dim = 0) { + return cat(tensors.map((t) => t.unsqueeze(dim)), dim); + } + function std_mean(input, dim = null, correction = 1, keepdim = false) { + const inputData = ( + /** @type {Float32Array} */ + input.data + ); + const inputDims = input.dims; + if (dim === null) { + const sum = inputData.reduce((a, b) => a + b, 0); + const mean2 = sum / inputData.length; + const std = Math.sqrt(inputData.reduce((a, b) => a + (b - mean2) ** 2, 0) / (inputData.length - correction)); + const meanTensor2 = new Tensor(input.type, [mean2], [ + /* scalar */ + ]); + const stdTensor2 = new Tensor(input.type, [std], [ + /* scalar */ + ]); + return [stdTensor2, meanTensor2]; + } + dim = safeIndex(dim, inputDims.length); + const meanTensor = mean(input, dim, keepdim); + const meanTensorData = meanTensor.data; + const resultDims = inputDims.slice(); + resultDims[dim] = 1; + const result = new inputData.constructor(inputData.length / inputDims[dim]); + for (let i = 0; i < inputData.length; ++i) { + let resultIndex = 0; + for (let j = inputDims.length - 1, num = i, resultMultiplier = 1; j >= 0; --j) { + const size = inputDims[j]; + if (j !== dim) { + const index = num % size; + resultIndex += index * resultMultiplier; + resultMultiplier *= resultDims[j]; + } + num = Math.floor(num / size); + } + result[resultIndex] += (inputData[i] - meanTensorData[resultIndex]) ** 2; + } + for (let i = 0; i < result.length; ++i) { + result[i] = Math.sqrt(result[i] / (inputDims[dim] - correction)); + } + if (!keepdim) { + resultDims.splice(dim, 1); + } + const stdTensor = new Tensor(input.type, result, resultDims); + return [stdTensor, meanTensor]; + } + function mean(input, dim = null, keepdim = false) { + const inputData = ( + /** @type {Float32Array} */ + input.data + ); + if (dim === null) { + const val = inputData.reduce((a, b) => a + b, 0); + return new Tensor(input.type, [val / inputData.length], [ + /* scalar */ + ]); + } + const inputDims = input.dims; + dim = safeIndex(dim, inputDims.length); + const resultDims = inputDims.slice(); + resultDims[dim] = 1; + const result = new inputData.constructor(inputData.length / inputDims[dim]); + for (let i = 0; i < inputData.length; ++i) { + let resultIndex = 0; + for (let j = inputDims.length - 1, num = i, resultMultiplier = 1; j >= 0; --j) { + const size = inputDims[j]; + if (j !== dim) { + const index = num % size; + resultIndex += index * resultMultiplier; + resultMultiplier *= resultDims[j]; + } + num = Math.floor(num / size); + } + result[resultIndex] += inputData[i]; + } + if (inputDims[dim] !== 1) { + for (let i = 0; i < result.length; ++i) { + result[i] = result[i] / inputDims[dim]; + } + } + if (!keepdim) { + resultDims.splice(dim, 1); + } + return new Tensor(input.type, result, resultDims); + } + function dimsToStride(dims) { + const stride = new Array(dims.length); + for (let i = dims.length - 1, s2 = 1; i >= 0; --i) { + stride[i] = s2; + s2 *= dims[i]; + } + return stride; + } + function fullHelper(size, fill_value, dtype, cls) { + const numElements = size.reduce((a, b) => a * b, 1); + return new Tensor( + dtype, + new cls(numElements).fill(fill_value), + size + ); + } + function full(size, fill_value) { + let dtype; + let typedArrayCls; + if (typeof fill_value === "number") { + dtype = "float32"; + typedArrayCls = Float32Array; + } else if (typeof fill_value === "bigint") { + dtype = "int64"; + typedArrayCls = BigInt64Array; + } else { + throw new Error(`Unsupported data type: ${typeof fill_value}`); + } + return fullHelper(size, fill_value, dtype, typedArrayCls); + } + function full_like(tensor, fill_value) { + return full(tensor.dims, fill_value); + } + function ones(size) { + return fullHelper(size, 1n, "int64", BigInt64Array); + } + function ones_like(tensor) { + return ones(tensor.dims); + } + function zeros(size) { + return fullHelper(size, 0n, "int64", BigInt64Array); + } + function zeros_like(tensor) { + return zeros(tensor.dims); + } + function quantize_embeddings(tensor, precision) { + if (tensor.dims.length !== 2) { + throw new Error("The tensor must have 2 dimensions"); + } + if (tensor.dims.at(-1) % 8 !== 0) { + throw new Error("The last dimension of the tensor must be a multiple of 8"); + } + if (!["binary", "ubinary"].includes(precision)) { + throw new Error("The precision must be either 'binary' or 'ubinary'"); + } + const signed = precision === "binary"; + const dtype = signed ? "int8" : "uint8"; + const cls = signed ? Int8Array : Uint8Array; + const inputData = tensor.data; + const outputData = new cls(inputData.length / 8); + for (let i = 0; i < inputData.length; ++i) { + const bit = inputData[i] > 0 ? 1 : 0; + const arrayIndex = Math.floor(i / 8); + const bitPosition = i % 8; + outputData[arrayIndex] |= bit << 7 - bitPosition; + if (signed && bitPosition === 0) { + outputData[arrayIndex] -= 128; + } + } + ; + return new Tensor(dtype, outputData, [tensor.dims[0], tensor.dims[1] / 8]); + } + } + ) + /******/ + }; + var __webpack_module_cache__ = {}; + function __webpack_require__(moduleId) { + var cachedModule = __webpack_module_cache__[moduleId]; + if (cachedModule !== void 0) { + return cachedModule.exports; + } + var module = __webpack_module_cache__[moduleId] = { + /******/ + // no module.id needed + /******/ + // no module.loaded needed + /******/ + exports: {} + /******/ + }; + __webpack_modules__[moduleId](module, module.exports, __webpack_require__); + return module.exports; + } + __webpack_require__.m = __webpack_modules__; + (() => { + var getProto = Object.getPrototypeOf ? (obj) => Object.getPrototypeOf(obj) : (obj) => obj.__proto__; + var leafPrototypes; + __webpack_require__.t = function(value, mode) { + if (mode & 1) value = this(value); + if (mode & 8) return value; + if (typeof value === "object" && value) { + if (mode & 4 && value.__esModule) return value; + if (mode & 16 && typeof value.then === "function") return value; + } + var ns = /* @__PURE__ */ Object.create(null); + __webpack_require__.r(ns); + var def = {}; + leafPrototypes = leafPrototypes || [null, getProto({}), getProto([]), getProto(getProto)]; + for (var current = mode & 2 && value; typeof current == "object" && !~leafPrototypes.indexOf(current); current = getProto(current)) { + Object.getOwnPropertyNames(current).forEach((key) => def[key] = () => value[key]); + } + def["default"] = () => value; + __webpack_require__.d(ns, def); + return ns; + }; + })(); + (() => { + __webpack_require__.d = (exports, definition) => { + for (var key in definition) { + if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) { + Object.defineProperty(exports, key, { enumerable: true, get: definition[key] }); + } + } + }; + })(); + (() => { + __webpack_require__.o = (obj, prop) => Object.prototype.hasOwnProperty.call(obj, prop); + })(); + (() => { + __webpack_require__.r = (exports) => { + if (typeof Symbol !== "undefined" && Symbol.toStringTag) { + Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" }); + } + Object.defineProperty(exports, "__esModule", { value: true }); + }; + })(); + (() => { + var scriptUrl; + if (typeof META_URL === "string") scriptUrl = META_URL; + if (!scriptUrl) throw new Error("Automatic publicPath is not supported in this browser"); + scriptUrl = scriptUrl.replace(/#.*$/, "").replace(/\?.*$/, "").replace(/\/[^\/]+$/, "/"); + __webpack_require__.p = scriptUrl; + })(); + (() => { + __webpack_require__.b = new URL("/node_modules/.vite/deps", META_URL); + var installedChunks = { + /******/ + "transformers": 0 + /******/ + }; + })(); + var __webpack_exports__ = {}; + __webpack_require__.r(__webpack_exports__); + __webpack_require__.d(__webpack_exports__, { + /* harmony export */ + ASTFeatureExtractor: () => ( + /* reexport safe */ + _processors_js__WEBPACK_IMPORTED_MODULE_4__.ASTFeatureExtractor + ), + /* harmony export */ + ASTForAudioClassification: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.ASTForAudioClassification + ), + /* harmony export */ + ASTModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.ASTModel + ), + /* harmony export */ + ASTPreTrainedModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.ASTPreTrainedModel + ), + /* harmony export */ + AlbertForMaskedLM: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.AlbertForMaskedLM + ), + /* harmony export */ + AlbertForQuestionAnswering: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.AlbertForQuestionAnswering + ), + /* harmony export */ + AlbertForSequenceClassification: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.AlbertForSequenceClassification + ), + /* harmony export */ + AlbertModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.AlbertModel + ), + /* harmony export */ + AlbertPreTrainedModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.AlbertPreTrainedModel + ), + /* harmony export */ + AlbertTokenizer: () => ( + /* reexport safe */ + _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.AlbertTokenizer + ), + /* harmony export */ + AudioClassificationPipeline: () => ( + /* reexport safe */ + _pipelines_js__WEBPACK_IMPORTED_MODULE_1__.AudioClassificationPipeline + ), + /* harmony export */ + AutoConfig: () => ( + /* reexport safe */ + _configs_js__WEBPACK_IMPORTED_MODULE_5__.AutoConfig + ), + /* harmony export */ + AutoModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModel + ), + /* harmony export */ + AutoModelForAudioClassification: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModelForAudioClassification + ), + /* harmony export */ + AutoModelForAudioFrameClassification: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModelForAudioFrameClassification + ), + /* harmony export */ + AutoModelForCTC: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModelForCTC + ), + /* harmony export */ + AutoModelForCausalLM: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModelForCausalLM + ), + /* harmony export */ + AutoModelForDepthEstimation: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModelForDepthEstimation + ), + /* harmony export */ + AutoModelForDocumentQuestionAnswering: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModelForDocumentQuestionAnswering + ), + /* harmony export */ + AutoModelForImageClassification: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModelForImageClassification + ), + /* harmony export */ + AutoModelForImageFeatureExtraction: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModelForImageFeatureExtraction + ), + /* harmony export */ + AutoModelForImageMatting: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModelForImageMatting + ), + /* harmony export */ + AutoModelForImageSegmentation: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModelForImageSegmentation + ), + /* harmony export */ + AutoModelForImageToImage: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModelForImageToImage + ), + /* harmony export */ + AutoModelForMaskGeneration: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModelForMaskGeneration + ), + /* harmony export */ + AutoModelForMaskedLM: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModelForMaskedLM + ), + /* harmony export */ + AutoModelForNormalEstimation: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModelForNormalEstimation + ), + /* harmony export */ + AutoModelForObjectDetection: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModelForObjectDetection + ), + /* harmony export */ + AutoModelForQuestionAnswering: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModelForQuestionAnswering + ), + /* harmony export */ + AutoModelForSemanticSegmentation: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModelForSemanticSegmentation + ), + /* harmony export */ + AutoModelForSeq2SeqLM: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModelForSeq2SeqLM + ), + /* harmony export */ + AutoModelForSequenceClassification: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModelForSequenceClassification + ), + /* harmony export */ + AutoModelForSpeechSeq2Seq: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModelForSpeechSeq2Seq + ), + /* harmony export */ + AutoModelForTextToSpectrogram: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModelForTextToSpectrogram + ), + /* harmony export */ + AutoModelForTextToWaveform: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModelForTextToWaveform + ), + /* harmony export */ + AutoModelForTokenClassification: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModelForTokenClassification + ), + /* harmony export */ + AutoModelForUniversalSegmentation: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModelForUniversalSegmentation + ), + /* harmony export */ + AutoModelForVision2Seq: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModelForVision2Seq + ), + /* harmony export */ + AutoModelForXVector: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModelForXVector + ), + /* harmony export */ + AutoModelForZeroShotObjectDetection: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModelForZeroShotObjectDetection + ), + /* harmony export */ + AutoProcessor: () => ( + /* reexport safe */ + _processors_js__WEBPACK_IMPORTED_MODULE_4__.AutoProcessor + ), + /* harmony export */ + AutoTokenizer: () => ( + /* reexport safe */ + _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.AutoTokenizer + ), + /* harmony export */ + AutomaticSpeechRecognitionPipeline: () => ( + /* reexport safe */ + _pipelines_js__WEBPACK_IMPORTED_MODULE_1__.AutomaticSpeechRecognitionPipeline + ), + /* harmony export */ + BartForConditionalGeneration: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.BartForConditionalGeneration + ), + /* harmony export */ + BartForSequenceClassification: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.BartForSequenceClassification + ), + /* harmony export */ + BartModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.BartModel + ), + /* harmony export */ + BartPretrainedModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.BartPretrainedModel + ), + /* harmony export */ + BartTokenizer: () => ( + /* reexport safe */ + _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.BartTokenizer + ), + /* harmony export */ + BaseModelOutput: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.BaseModelOutput + ), + /* harmony export */ + BaseStreamer: () => ( + /* reexport safe */ + _generation_streamers_js__WEBPACK_IMPORTED_MODULE_10__.BaseStreamer + ), + /* harmony export */ + BeitFeatureExtractor: () => ( + /* reexport safe */ + _processors_js__WEBPACK_IMPORTED_MODULE_4__.BeitFeatureExtractor + ), + /* harmony export */ + BeitForImageClassification: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.BeitForImageClassification + ), + /* harmony export */ + BeitModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.BeitModel + ), + /* harmony export */ + BeitPreTrainedModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.BeitPreTrainedModel + ), + /* harmony export */ + BertForMaskedLM: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.BertForMaskedLM + ), + /* harmony export */ + BertForQuestionAnswering: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.BertForQuestionAnswering + ), + /* harmony export */ + BertForSequenceClassification: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.BertForSequenceClassification + ), + /* harmony export */ + BertForTokenClassification: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.BertForTokenClassification + ), + /* harmony export */ + BertModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.BertModel + ), + /* harmony export */ + BertPreTrainedModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.BertPreTrainedModel + ), + /* harmony export */ + BertTokenizer: () => ( + /* reexport safe */ + _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.BertTokenizer + ), + /* harmony export */ + BitImageProcessor: () => ( + /* reexport safe */ + _processors_js__WEBPACK_IMPORTED_MODULE_4__.BitImageProcessor + ), + /* harmony export */ + BlenderbotForConditionalGeneration: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.BlenderbotForConditionalGeneration + ), + /* harmony export */ + BlenderbotModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.BlenderbotModel + ), + /* harmony export */ + BlenderbotPreTrainedModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.BlenderbotPreTrainedModel + ), + /* harmony export */ + BlenderbotSmallForConditionalGeneration: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.BlenderbotSmallForConditionalGeneration + ), + /* harmony export */ + BlenderbotSmallModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.BlenderbotSmallModel + ), + /* harmony export */ + BlenderbotSmallPreTrainedModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.BlenderbotSmallPreTrainedModel + ), + /* harmony export */ + BlenderbotSmallTokenizer: () => ( + /* reexport safe */ + _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.BlenderbotSmallTokenizer + ), + /* harmony export */ + BlenderbotTokenizer: () => ( + /* reexport safe */ + _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.BlenderbotTokenizer + ), + /* harmony export */ + BloomForCausalLM: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.BloomForCausalLM + ), + /* harmony export */ + BloomModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.BloomModel + ), + /* harmony export */ + BloomPreTrainedModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.BloomPreTrainedModel + ), + /* harmony export */ + BloomTokenizer: () => ( + /* reexport safe */ + _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.BloomTokenizer + ), + /* harmony export */ + CLIPFeatureExtractor: () => ( + /* reexport safe */ + _processors_js__WEBPACK_IMPORTED_MODULE_4__.CLIPFeatureExtractor + ), + /* harmony export */ + CLIPImageProcessor: () => ( + /* reexport safe */ + _processors_js__WEBPACK_IMPORTED_MODULE_4__.CLIPImageProcessor + ), + /* harmony export */ + CLIPModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.CLIPModel + ), + /* harmony export */ + CLIPPreTrainedModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.CLIPPreTrainedModel + ), + /* harmony export */ + CLIPSegForImageSegmentation: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.CLIPSegForImageSegmentation + ), + /* harmony export */ + CLIPSegModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.CLIPSegModel + ), + /* harmony export */ + CLIPSegPreTrainedModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.CLIPSegPreTrainedModel + ), + /* harmony export */ + CLIPTextModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.CLIPTextModel + ), + /* harmony export */ + CLIPTextModelWithProjection: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.CLIPTextModelWithProjection + ), + /* harmony export */ + CLIPTokenizer: () => ( + /* reexport safe */ + _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.CLIPTokenizer + ), + /* harmony export */ + CLIPVisionModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.CLIPVisionModel + ), + /* harmony export */ + CLIPVisionModelWithProjection: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.CLIPVisionModelWithProjection + ), + /* harmony export */ + CamembertForMaskedLM: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.CamembertForMaskedLM + ), + /* harmony export */ + CamembertForQuestionAnswering: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.CamembertForQuestionAnswering + ), + /* harmony export */ + CamembertForSequenceClassification: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.CamembertForSequenceClassification + ), + /* harmony export */ + CamembertForTokenClassification: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.CamembertForTokenClassification + ), + /* harmony export */ + CamembertModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.CamembertModel + ), + /* harmony export */ + CamembertPreTrainedModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.CamembertPreTrainedModel + ), + /* harmony export */ + CamembertTokenizer: () => ( + /* reexport safe */ + _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.CamembertTokenizer + ), + /* harmony export */ + CausalLMOutput: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.CausalLMOutput + ), + /* harmony export */ + CausalLMOutputWithPast: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.CausalLMOutputWithPast + ), + /* harmony export */ + ChineseCLIPFeatureExtractor: () => ( + /* reexport safe */ + _processors_js__WEBPACK_IMPORTED_MODULE_4__.ChineseCLIPFeatureExtractor + ), + /* harmony export */ + ChineseCLIPModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.ChineseCLIPModel + ), + /* harmony export */ + ChineseCLIPPreTrainedModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.ChineseCLIPPreTrainedModel + ), + /* harmony export */ + ClapAudioModelWithProjection: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.ClapAudioModelWithProjection + ), + /* harmony export */ + ClapFeatureExtractor: () => ( + /* reexport safe */ + _processors_js__WEBPACK_IMPORTED_MODULE_4__.ClapFeatureExtractor + ), + /* harmony export */ + ClapModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.ClapModel + ), + /* harmony export */ + ClapPreTrainedModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.ClapPreTrainedModel + ), + /* harmony export */ + ClapTextModelWithProjection: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.ClapTextModelWithProjection + ), + /* harmony export */ + CodeGenForCausalLM: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.CodeGenForCausalLM + ), + /* harmony export */ + CodeGenModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.CodeGenModel + ), + /* harmony export */ + CodeGenPreTrainedModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.CodeGenPreTrainedModel + ), + /* harmony export */ + CodeGenTokenizer: () => ( + /* reexport safe */ + _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.CodeGenTokenizer + ), + /* harmony export */ + CodeLlamaTokenizer: () => ( + /* reexport safe */ + _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.CodeLlamaTokenizer + ), + /* harmony export */ + CohereForCausalLM: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.CohereForCausalLM + ), + /* harmony export */ + CohereModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.CohereModel + ), + /* harmony export */ + CoherePreTrainedModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.CoherePreTrainedModel + ), + /* harmony export */ + CohereTokenizer: () => ( + /* reexport safe */ + _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.CohereTokenizer + ), + /* harmony export */ + ConvBertForMaskedLM: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.ConvBertForMaskedLM + ), + /* harmony export */ + ConvBertForQuestionAnswering: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.ConvBertForQuestionAnswering + ), + /* harmony export */ + ConvBertForSequenceClassification: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.ConvBertForSequenceClassification + ), + /* harmony export */ + ConvBertForTokenClassification: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.ConvBertForTokenClassification + ), + /* harmony export */ + ConvBertModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.ConvBertModel + ), + /* harmony export */ + ConvBertPreTrainedModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.ConvBertPreTrainedModel + ), + /* harmony export */ + ConvBertTokenizer: () => ( + /* reexport safe */ + _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.ConvBertTokenizer + ), + /* harmony export */ + ConvNextFeatureExtractor: () => ( + /* reexport safe */ + _processors_js__WEBPACK_IMPORTED_MODULE_4__.ConvNextFeatureExtractor + ), + /* harmony export */ + ConvNextForImageClassification: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.ConvNextForImageClassification + ), + /* harmony export */ + ConvNextImageProcessor: () => ( + /* reexport safe */ + _processors_js__WEBPACK_IMPORTED_MODULE_4__.ConvNextImageProcessor + ), + /* harmony export */ + ConvNextModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.ConvNextModel + ), + /* harmony export */ + ConvNextPreTrainedModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.ConvNextPreTrainedModel + ), + /* harmony export */ + ConvNextV2ForImageClassification: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.ConvNextV2ForImageClassification + ), + /* harmony export */ + ConvNextV2Model: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.ConvNextV2Model + ), + /* harmony export */ + ConvNextV2PreTrainedModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.ConvNextV2PreTrainedModel + ), + /* harmony export */ + DPTFeatureExtractor: () => ( + /* reexport safe */ + _processors_js__WEBPACK_IMPORTED_MODULE_4__.DPTFeatureExtractor + ), + /* harmony export */ + DPTForDepthEstimation: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.DPTForDepthEstimation + ), + /* harmony export */ + DPTImageProcessor: () => ( + /* reexport safe */ + _processors_js__WEBPACK_IMPORTED_MODULE_4__.DPTImageProcessor + ), + /* harmony export */ + DPTModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.DPTModel + ), + /* harmony export */ + DPTPreTrainedModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.DPTPreTrainedModel + ), + /* harmony export */ + DebertaForMaskedLM: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.DebertaForMaskedLM + ), + /* harmony export */ + DebertaForQuestionAnswering: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.DebertaForQuestionAnswering + ), + /* harmony export */ + DebertaForSequenceClassification: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.DebertaForSequenceClassification + ), + /* harmony export */ + DebertaForTokenClassification: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.DebertaForTokenClassification + ), + /* harmony export */ + DebertaModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.DebertaModel + ), + /* harmony export */ + DebertaPreTrainedModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.DebertaPreTrainedModel + ), + /* harmony export */ + DebertaTokenizer: () => ( + /* reexport safe */ + _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.DebertaTokenizer + ), + /* harmony export */ + DebertaV2ForMaskedLM: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.DebertaV2ForMaskedLM + ), + /* harmony export */ + DebertaV2ForQuestionAnswering: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.DebertaV2ForQuestionAnswering + ), + /* harmony export */ + DebertaV2ForSequenceClassification: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.DebertaV2ForSequenceClassification + ), + /* harmony export */ + DebertaV2ForTokenClassification: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.DebertaV2ForTokenClassification + ), + /* harmony export */ + DebertaV2Model: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.DebertaV2Model + ), + /* harmony export */ + DebertaV2PreTrainedModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.DebertaV2PreTrainedModel + ), + /* harmony export */ + DebertaV2Tokenizer: () => ( + /* reexport safe */ + _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.DebertaV2Tokenizer + ), + /* harmony export */ + DecisionTransformerModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.DecisionTransformerModel + ), + /* harmony export */ + DecisionTransformerPreTrainedModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.DecisionTransformerPreTrainedModel + ), + /* harmony export */ + DeiTFeatureExtractor: () => ( + /* reexport safe */ + _processors_js__WEBPACK_IMPORTED_MODULE_4__.DeiTFeatureExtractor + ), + /* harmony export */ + DeiTForImageClassification: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.DeiTForImageClassification + ), + /* harmony export */ + DeiTModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.DeiTModel + ), + /* harmony export */ + DeiTPreTrainedModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.DeiTPreTrainedModel + ), + /* harmony export */ + DepthAnythingForDepthEstimation: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.DepthAnythingForDepthEstimation + ), + /* harmony export */ + DepthAnythingPreTrainedModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.DepthAnythingPreTrainedModel + ), + /* harmony export */ + DepthEstimationPipeline: () => ( + /* reexport safe */ + _pipelines_js__WEBPACK_IMPORTED_MODULE_1__.DepthEstimationPipeline + ), + /* harmony export */ + DepthProForDepthEstimation: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.DepthProForDepthEstimation + ), + /* harmony export */ + DepthProPreTrainedModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.DepthProPreTrainedModel + ), + /* harmony export */ + DetrFeatureExtractor: () => ( + /* reexport safe */ + _processors_js__WEBPACK_IMPORTED_MODULE_4__.DetrFeatureExtractor + ), + /* harmony export */ + DetrForObjectDetection: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.DetrForObjectDetection + ), + /* harmony export */ + DetrForSegmentation: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.DetrForSegmentation + ), + /* harmony export */ + DetrModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.DetrModel + ), + /* harmony export */ + DetrObjectDetectionOutput: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.DetrObjectDetectionOutput + ), + /* harmony export */ + DetrPreTrainedModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.DetrPreTrainedModel + ), + /* harmony export */ + DetrSegmentationOutput: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.DetrSegmentationOutput + ), + /* harmony export */ + Dinov2ForImageClassification: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.Dinov2ForImageClassification + ), + /* harmony export */ + Dinov2Model: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.Dinov2Model + ), + /* harmony export */ + Dinov2PreTrainedModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.Dinov2PreTrainedModel + ), + /* harmony export */ + DistilBertForMaskedLM: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.DistilBertForMaskedLM + ), + /* harmony export */ + DistilBertForQuestionAnswering: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.DistilBertForQuestionAnswering + ), + /* harmony export */ + DistilBertForSequenceClassification: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.DistilBertForSequenceClassification + ), + /* harmony export */ + DistilBertForTokenClassification: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.DistilBertForTokenClassification + ), + /* harmony export */ + DistilBertModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.DistilBertModel + ), + /* harmony export */ + DistilBertPreTrainedModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.DistilBertPreTrainedModel + ), + /* harmony export */ + DistilBertTokenizer: () => ( + /* reexport safe */ + _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.DistilBertTokenizer + ), + /* harmony export */ + DocumentQuestionAnsweringPipeline: () => ( + /* reexport safe */ + _pipelines_js__WEBPACK_IMPORTED_MODULE_1__.DocumentQuestionAnsweringPipeline + ), + /* harmony export */ + DonutFeatureExtractor: () => ( + /* reexport safe */ + _processors_js__WEBPACK_IMPORTED_MODULE_4__.DonutFeatureExtractor + ), + /* harmony export */ + DonutSwinModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.DonutSwinModel + ), + /* harmony export */ + DonutSwinPreTrainedModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.DonutSwinPreTrainedModel + ), + /* harmony export */ + EfficientNetForImageClassification: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.EfficientNetForImageClassification + ), + /* harmony export */ + EfficientNetImageProcessor: () => ( + /* reexport safe */ + _processors_js__WEBPACK_IMPORTED_MODULE_4__.EfficientNetImageProcessor + ), + /* harmony export */ + EfficientNetModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.EfficientNetModel + ), + /* harmony export */ + EfficientNetPreTrainedModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.EfficientNetPreTrainedModel + ), + /* harmony export */ + ElectraForMaskedLM: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.ElectraForMaskedLM + ), + /* harmony export */ + ElectraForQuestionAnswering: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.ElectraForQuestionAnswering + ), + /* harmony export */ + ElectraForSequenceClassification: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.ElectraForSequenceClassification + ), + /* harmony export */ + ElectraForTokenClassification: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.ElectraForTokenClassification + ), + /* harmony export */ + ElectraModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.ElectraModel + ), + /* harmony export */ + ElectraPreTrainedModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.ElectraPreTrainedModel + ), + /* harmony export */ + ElectraTokenizer: () => ( + /* reexport safe */ + _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.ElectraTokenizer + ), + /* harmony export */ + EosTokenCriteria: () => ( + /* reexport safe */ + _generation_stopping_criteria_js__WEBPACK_IMPORTED_MODULE_11__.EosTokenCriteria + ), + /* harmony export */ + EsmForMaskedLM: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.EsmForMaskedLM + ), + /* harmony export */ + EsmForSequenceClassification: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.EsmForSequenceClassification + ), + /* harmony export */ + EsmForTokenClassification: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.EsmForTokenClassification + ), + /* harmony export */ + EsmModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.EsmModel + ), + /* harmony export */ + EsmPreTrainedModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.EsmPreTrainedModel + ), + /* harmony export */ + EsmTokenizer: () => ( + /* reexport safe */ + _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.EsmTokenizer + ), + /* harmony export */ + FFT: () => ( + /* reexport safe */ + _utils_maths_js__WEBPACK_IMPORTED_MODULE_9__.FFT + ), + /* harmony export */ + FalconForCausalLM: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.FalconForCausalLM + ), + /* harmony export */ + FalconModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.FalconModel + ), + /* harmony export */ + FalconPreTrainedModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.FalconPreTrainedModel + ), + /* harmony export */ + FalconTokenizer: () => ( + /* reexport safe */ + _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.FalconTokenizer + ), + /* harmony export */ + FastViTForImageClassification: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.FastViTForImageClassification + ), + /* harmony export */ + FastViTModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.FastViTModel + ), + /* harmony export */ + FastViTPreTrainedModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.FastViTPreTrainedModel + ), + /* harmony export */ + FeatureExtractionPipeline: () => ( + /* reexport safe */ + _pipelines_js__WEBPACK_IMPORTED_MODULE_1__.FeatureExtractionPipeline + ), + /* harmony export */ + FeatureExtractor: () => ( + /* reexport safe */ + _processors_js__WEBPACK_IMPORTED_MODULE_4__.FeatureExtractor + ), + /* harmony export */ + FillMaskPipeline: () => ( + /* reexport safe */ + _pipelines_js__WEBPACK_IMPORTED_MODULE_1__.FillMaskPipeline + ), + /* harmony export */ + Florence2ForConditionalGeneration: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.Florence2ForConditionalGeneration + ), + /* harmony export */ + Florence2PreTrainedModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.Florence2PreTrainedModel + ), + /* harmony export */ + Florence2Processor: () => ( + /* reexport safe */ + _processors_js__WEBPACK_IMPORTED_MODULE_4__.Florence2Processor + ), + /* harmony export */ + GLPNFeatureExtractor: () => ( + /* reexport safe */ + _processors_js__WEBPACK_IMPORTED_MODULE_4__.GLPNFeatureExtractor + ), + /* harmony export */ + GLPNForDepthEstimation: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.GLPNForDepthEstimation + ), + /* harmony export */ + GLPNModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.GLPNModel + ), + /* harmony export */ + GLPNPreTrainedModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.GLPNPreTrainedModel + ), + /* harmony export */ + GPT2LMHeadModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.GPT2LMHeadModel + ), + /* harmony export */ + GPT2Model: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.GPT2Model + ), + /* harmony export */ + GPT2PreTrainedModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.GPT2PreTrainedModel + ), + /* harmony export */ + GPT2Tokenizer: () => ( + /* reexport safe */ + _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.GPT2Tokenizer + ), + /* harmony export */ + GPTBigCodeForCausalLM: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.GPTBigCodeForCausalLM + ), + /* harmony export */ + GPTBigCodeModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.GPTBigCodeModel + ), + /* harmony export */ + GPTBigCodePreTrainedModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.GPTBigCodePreTrainedModel + ), + /* harmony export */ + GPTJForCausalLM: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.GPTJForCausalLM + ), + /* harmony export */ + GPTJModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.GPTJModel + ), + /* harmony export */ + GPTJPreTrainedModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.GPTJPreTrainedModel + ), + /* harmony export */ + GPTNeoForCausalLM: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.GPTNeoForCausalLM + ), + /* harmony export */ + GPTNeoModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.GPTNeoModel + ), + /* harmony export */ + GPTNeoPreTrainedModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.GPTNeoPreTrainedModel + ), + /* harmony export */ + GPTNeoXForCausalLM: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.GPTNeoXForCausalLM + ), + /* harmony export */ + GPTNeoXModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.GPTNeoXModel + ), + /* harmony export */ + GPTNeoXPreTrainedModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.GPTNeoXPreTrainedModel + ), + /* harmony export */ + GPTNeoXTokenizer: () => ( + /* reexport safe */ + _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.GPTNeoXTokenizer + ), + /* harmony export */ + Gemma2ForCausalLM: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.Gemma2ForCausalLM + ), + /* harmony export */ + Gemma2Model: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.Gemma2Model + ), + /* harmony export */ + Gemma2PreTrainedModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.Gemma2PreTrainedModel + ), + /* harmony export */ + GemmaForCausalLM: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.GemmaForCausalLM + ), + /* harmony export */ + GemmaModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.GemmaModel + ), + /* harmony export */ + GemmaPreTrainedModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.GemmaPreTrainedModel + ), + /* harmony export */ + GemmaTokenizer: () => ( + /* reexport safe */ + _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.GemmaTokenizer + ), + /* harmony export */ + GraniteForCausalLM: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.GraniteForCausalLM + ), + /* harmony export */ + GraniteModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.GraniteModel + ), + /* harmony export */ + GranitePreTrainedModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.GranitePreTrainedModel + ), + /* harmony export */ + Grok1Tokenizer: () => ( + /* reexport safe */ + _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.Grok1Tokenizer + ), + /* harmony export */ + GroupViTModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.GroupViTModel + ), + /* harmony export */ + GroupViTPreTrainedModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.GroupViTPreTrainedModel + ), + /* harmony export */ + HerbertTokenizer: () => ( + /* reexport safe */ + _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.HerbertTokenizer + ), + /* harmony export */ + HieraForImageClassification: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.HieraForImageClassification + ), + /* harmony export */ + HieraModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.HieraModel + ), + /* harmony export */ + HieraPreTrainedModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.HieraPreTrainedModel + ), + /* harmony export */ + HubertForCTC: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.HubertForCTC + ), + /* harmony export */ + HubertForSequenceClassification: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.HubertForSequenceClassification + ), + /* harmony export */ + HubertModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.HubertModel + ), + /* harmony export */ + HubertPreTrainedModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.HubertPreTrainedModel + ), + /* harmony export */ + ImageClassificationPipeline: () => ( + /* reexport safe */ + _pipelines_js__WEBPACK_IMPORTED_MODULE_1__.ImageClassificationPipeline + ), + /* harmony export */ + ImageFeatureExtractionPipeline: () => ( + /* reexport safe */ + _pipelines_js__WEBPACK_IMPORTED_MODULE_1__.ImageFeatureExtractionPipeline + ), + /* harmony export */ + ImageFeatureExtractor: () => ( + /* reexport safe */ + _processors_js__WEBPACK_IMPORTED_MODULE_4__.ImageFeatureExtractor + ), + /* harmony export */ + ImageMattingOutput: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.ImageMattingOutput + ), + /* harmony export */ + ImageSegmentationPipeline: () => ( + /* reexport safe */ + _pipelines_js__WEBPACK_IMPORTED_MODULE_1__.ImageSegmentationPipeline + ), + /* harmony export */ + ImageToImagePipeline: () => ( + /* reexport safe */ + _pipelines_js__WEBPACK_IMPORTED_MODULE_1__.ImageToImagePipeline + ), + /* harmony export */ + ImageToTextPipeline: () => ( + /* reexport safe */ + _pipelines_js__WEBPACK_IMPORTED_MODULE_1__.ImageToTextPipeline + ), + /* harmony export */ + InterruptableStoppingCriteria: () => ( + /* reexport safe */ + _generation_stopping_criteria_js__WEBPACK_IMPORTED_MODULE_11__.InterruptableStoppingCriteria + ), + /* harmony export */ + JAISLMHeadModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.JAISLMHeadModel + ), + /* harmony export */ + JAISModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.JAISModel + ), + /* harmony export */ + JAISPreTrainedModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.JAISPreTrainedModel + ), + /* harmony export */ + LlamaForCausalLM: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.LlamaForCausalLM + ), + /* harmony export */ + LlamaModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.LlamaModel + ), + /* harmony export */ + LlamaPreTrainedModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.LlamaPreTrainedModel + ), + /* harmony export */ + LlamaTokenizer: () => ( + /* reexport safe */ + _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.LlamaTokenizer + ), + /* harmony export */ + LlavaForConditionalGeneration: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.LlavaForConditionalGeneration + ), + /* harmony export */ + LlavaPreTrainedModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.LlavaPreTrainedModel + ), + /* harmony export */ + LongT5ForConditionalGeneration: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.LongT5ForConditionalGeneration + ), + /* harmony export */ + LongT5Model: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.LongT5Model + ), + /* harmony export */ + LongT5PreTrainedModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.LongT5PreTrainedModel + ), + /* harmony export */ + M2M100ForConditionalGeneration: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.M2M100ForConditionalGeneration + ), + /* harmony export */ + M2M100Model: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.M2M100Model + ), + /* harmony export */ + M2M100PreTrainedModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.M2M100PreTrainedModel + ), + /* harmony export */ + M2M100Tokenizer: () => ( + /* reexport safe */ + _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.M2M100Tokenizer + ), + /* harmony export */ + MBart50Tokenizer: () => ( + /* reexport safe */ + _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.MBart50Tokenizer + ), + /* harmony export */ + MBartForCausalLM: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.MBartForCausalLM + ), + /* harmony export */ + MBartForConditionalGeneration: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.MBartForConditionalGeneration + ), + /* harmony export */ + MBartForSequenceClassification: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.MBartForSequenceClassification + ), + /* harmony export */ + MBartModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.MBartModel + ), + /* harmony export */ + MBartPreTrainedModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.MBartPreTrainedModel + ), + /* harmony export */ + MBartTokenizer: () => ( + /* reexport safe */ + _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.MBartTokenizer + ), + /* harmony export */ + MPNetForMaskedLM: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.MPNetForMaskedLM + ), + /* harmony export */ + MPNetForQuestionAnswering: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.MPNetForQuestionAnswering + ), + /* harmony export */ + MPNetForSequenceClassification: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.MPNetForSequenceClassification + ), + /* harmony export */ + MPNetForTokenClassification: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.MPNetForTokenClassification + ), + /* harmony export */ + MPNetModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.MPNetModel + ), + /* harmony export */ + MPNetPreTrainedModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.MPNetPreTrainedModel + ), + /* harmony export */ + MPNetTokenizer: () => ( + /* reexport safe */ + _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.MPNetTokenizer + ), + /* harmony export */ + MT5ForConditionalGeneration: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.MT5ForConditionalGeneration + ), + /* harmony export */ + MT5Model: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.MT5Model + ), + /* harmony export */ + MT5PreTrainedModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.MT5PreTrainedModel + ), + /* harmony export */ + MarianMTModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.MarianMTModel + ), + /* harmony export */ + MarianModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.MarianModel + ), + /* harmony export */ + MarianPreTrainedModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.MarianPreTrainedModel + ), + /* harmony export */ + MarianTokenizer: () => ( + /* reexport safe */ + _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.MarianTokenizer + ), + /* harmony export */ + MaskFormerFeatureExtractor: () => ( + /* reexport safe */ + _processors_js__WEBPACK_IMPORTED_MODULE_4__.MaskFormerFeatureExtractor + ), + /* harmony export */ + MaskFormerForInstanceSegmentation: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.MaskFormerForInstanceSegmentation + ), + /* harmony export */ + MaskFormerModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.MaskFormerModel + ), + /* harmony export */ + MaskFormerPreTrainedModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.MaskFormerPreTrainedModel + ), + /* harmony export */ + MaskedLMOutput: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.MaskedLMOutput + ), + /* harmony export */ + MaxLengthCriteria: () => ( + /* reexport safe */ + _generation_stopping_criteria_js__WEBPACK_IMPORTED_MODULE_11__.MaxLengthCriteria + ), + /* harmony export */ + MistralForCausalLM: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.MistralForCausalLM + ), + /* harmony export */ + MistralModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.MistralModel + ), + /* harmony export */ + MistralPreTrainedModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.MistralPreTrainedModel + ), + /* harmony export */ + MobileBertForMaskedLM: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.MobileBertForMaskedLM + ), + /* harmony export */ + MobileBertForQuestionAnswering: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.MobileBertForQuestionAnswering + ), + /* harmony export */ + MobileBertForSequenceClassification: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.MobileBertForSequenceClassification + ), + /* harmony export */ + MobileBertModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.MobileBertModel + ), + /* harmony export */ + MobileBertPreTrainedModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.MobileBertPreTrainedModel + ), + /* harmony export */ + MobileBertTokenizer: () => ( + /* reexport safe */ + _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.MobileBertTokenizer + ), + /* harmony export */ + MobileNetV1FeatureExtractor: () => ( + /* reexport safe */ + _processors_js__WEBPACK_IMPORTED_MODULE_4__.MobileNetV1FeatureExtractor + ), + /* harmony export */ + MobileNetV1ForImageClassification: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.MobileNetV1ForImageClassification + ), + /* harmony export */ + MobileNetV1Model: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.MobileNetV1Model + ), + /* harmony export */ + MobileNetV1PreTrainedModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.MobileNetV1PreTrainedModel + ), + /* harmony export */ + MobileNetV2FeatureExtractor: () => ( + /* reexport safe */ + _processors_js__WEBPACK_IMPORTED_MODULE_4__.MobileNetV2FeatureExtractor + ), + /* harmony export */ + MobileNetV2ForImageClassification: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.MobileNetV2ForImageClassification + ), + /* harmony export */ + MobileNetV2Model: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.MobileNetV2Model + ), + /* harmony export */ + MobileNetV2PreTrainedModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.MobileNetV2PreTrainedModel + ), + /* harmony export */ + MobileNetV3FeatureExtractor: () => ( + /* reexport safe */ + _processors_js__WEBPACK_IMPORTED_MODULE_4__.MobileNetV3FeatureExtractor + ), + /* harmony export */ + MobileNetV3ForImageClassification: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.MobileNetV3ForImageClassification + ), + /* harmony export */ + MobileNetV3Model: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.MobileNetV3Model + ), + /* harmony export */ + MobileNetV3PreTrainedModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.MobileNetV3PreTrainedModel + ), + /* harmony export */ + MobileNetV4FeatureExtractor: () => ( + /* reexport safe */ + _processors_js__WEBPACK_IMPORTED_MODULE_4__.MobileNetV4FeatureExtractor + ), + /* harmony export */ + MobileNetV4ForImageClassification: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.MobileNetV4ForImageClassification + ), + /* harmony export */ + MobileNetV4Model: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.MobileNetV4Model + ), + /* harmony export */ + MobileNetV4PreTrainedModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.MobileNetV4PreTrainedModel + ), + /* harmony export */ + MobileViTFeatureExtractor: () => ( + /* reexport safe */ + _processors_js__WEBPACK_IMPORTED_MODULE_4__.MobileViTFeatureExtractor + ), + /* harmony export */ + MobileViTForImageClassification: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.MobileViTForImageClassification + ), + /* harmony export */ + MobileViTImageProcessor: () => ( + /* reexport safe */ + _processors_js__WEBPACK_IMPORTED_MODULE_4__.MobileViTImageProcessor + ), + /* harmony export */ + MobileViTModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.MobileViTModel + ), + /* harmony export */ + MobileViTPreTrainedModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.MobileViTPreTrainedModel + ), + /* harmony export */ + MobileViTV2ForImageClassification: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.MobileViTV2ForImageClassification + ), + /* harmony export */ + MobileViTV2Model: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.MobileViTV2Model + ), + /* harmony export */ + MobileViTV2PreTrainedModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.MobileViTV2PreTrainedModel + ), + /* harmony export */ + ModelOutput: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.ModelOutput + ), + /* harmony export */ + Moondream1ForConditionalGeneration: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.Moondream1ForConditionalGeneration + ), + /* harmony export */ + MptForCausalLM: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.MptForCausalLM + ), + /* harmony export */ + MptModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.MptModel + ), + /* harmony export */ + MptPreTrainedModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.MptPreTrainedModel + ), + /* harmony export */ + MusicgenForCausalLM: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.MusicgenForCausalLM + ), + /* harmony export */ + MusicgenForConditionalGeneration: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.MusicgenForConditionalGeneration + ), + /* harmony export */ + MusicgenModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.MusicgenModel + ), + /* harmony export */ + MusicgenPreTrainedModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.MusicgenPreTrainedModel + ), + /* harmony export */ + NllbTokenizer: () => ( + /* reexport safe */ + _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.NllbTokenizer + ), + /* harmony export */ + NomicBertModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.NomicBertModel + ), + /* harmony export */ + NomicBertPreTrainedModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.NomicBertPreTrainedModel + ), + /* harmony export */ + NougatImageProcessor: () => ( + /* reexport safe */ + _processors_js__WEBPACK_IMPORTED_MODULE_4__.NougatImageProcessor + ), + /* harmony export */ + NougatTokenizer: () => ( + /* reexport safe */ + _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.NougatTokenizer + ), + /* harmony export */ + OPTForCausalLM: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.OPTForCausalLM + ), + /* harmony export */ + OPTModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.OPTModel + ), + /* harmony export */ + OPTPreTrainedModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.OPTPreTrainedModel + ), + /* harmony export */ + ObjectDetectionPipeline: () => ( + /* reexport safe */ + _pipelines_js__WEBPACK_IMPORTED_MODULE_1__.ObjectDetectionPipeline + ), + /* harmony export */ + OpenELMForCausalLM: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.OpenELMForCausalLM + ), + /* harmony export */ + OpenELMModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.OpenELMModel + ), + /* harmony export */ + OpenELMPreTrainedModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.OpenELMPreTrainedModel + ), + /* harmony export */ + OwlViTFeatureExtractor: () => ( + /* reexport safe */ + _processors_js__WEBPACK_IMPORTED_MODULE_4__.OwlViTFeatureExtractor + ), + /* harmony export */ + OwlViTForObjectDetection: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.OwlViTForObjectDetection + ), + /* harmony export */ + OwlViTModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.OwlViTModel + ), + /* harmony export */ + OwlViTPreTrainedModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.OwlViTPreTrainedModel + ), + /* harmony export */ + OwlViTProcessor: () => ( + /* reexport safe */ + _processors_js__WEBPACK_IMPORTED_MODULE_4__.OwlViTProcessor + ), + /* harmony export */ + Owlv2ForObjectDetection: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.Owlv2ForObjectDetection + ), + /* harmony export */ + Owlv2ImageProcessor: () => ( + /* reexport safe */ + _processors_js__WEBPACK_IMPORTED_MODULE_4__.Owlv2ImageProcessor + ), + /* harmony export */ + Owlv2Model: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.Owlv2Model + ), + /* harmony export */ + Owlv2PreTrainedModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.Owlv2PreTrainedModel + ), + /* harmony export */ + Phi3ForCausalLM: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.Phi3ForCausalLM + ), + /* harmony export */ + Phi3Model: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.Phi3Model + ), + /* harmony export */ + Phi3PreTrainedModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.Phi3PreTrainedModel + ), + /* harmony export */ + PhiForCausalLM: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.PhiForCausalLM + ), + /* harmony export */ + PhiModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.PhiModel + ), + /* harmony export */ + PhiPreTrainedModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.PhiPreTrainedModel + ), + /* harmony export */ + Pipeline: () => ( + /* reexport safe */ + _pipelines_js__WEBPACK_IMPORTED_MODULE_1__.Pipeline + ), + /* harmony export */ + PreTrainedModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.PreTrainedModel + ), + /* harmony export */ + PreTrainedTokenizer: () => ( + /* reexport safe */ + _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.PreTrainedTokenizer + ), + /* harmony export */ + PretrainedConfig: () => ( + /* reexport safe */ + _configs_js__WEBPACK_IMPORTED_MODULE_5__.PretrainedConfig + ), + /* harmony export */ + PretrainedMixin: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.PretrainedMixin + ), + /* harmony export */ + Processor: () => ( + /* reexport safe */ + _processors_js__WEBPACK_IMPORTED_MODULE_4__.Processor + ), + /* harmony export */ + PvtForImageClassification: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.PvtForImageClassification + ), + /* harmony export */ + PvtImageProcessor: () => ( + /* reexport safe */ + _processors_js__WEBPACK_IMPORTED_MODULE_4__.PvtImageProcessor + ), + /* harmony export */ + PvtModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.PvtModel + ), + /* harmony export */ + PvtPreTrainedModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.PvtPreTrainedModel + ), + /* harmony export */ + PyAnnoteFeatureExtractor: () => ( + /* reexport safe */ + _processors_js__WEBPACK_IMPORTED_MODULE_4__.PyAnnoteFeatureExtractor + ), + /* harmony export */ + PyAnnoteForAudioFrameClassification: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.PyAnnoteForAudioFrameClassification + ), + /* harmony export */ + PyAnnoteModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.PyAnnoteModel + ), + /* harmony export */ + PyAnnotePreTrainedModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.PyAnnotePreTrainedModel + ), + /* harmony export */ + PyAnnoteProcessor: () => ( + /* reexport safe */ + _processors_js__WEBPACK_IMPORTED_MODULE_4__.PyAnnoteProcessor + ), + /* harmony export */ + QuestionAnsweringModelOutput: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.QuestionAnsweringModelOutput + ), + /* harmony export */ + QuestionAnsweringPipeline: () => ( + /* reexport safe */ + _pipelines_js__WEBPACK_IMPORTED_MODULE_1__.QuestionAnsweringPipeline + ), + /* harmony export */ + Qwen2ForCausalLM: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.Qwen2ForCausalLM + ), + /* harmony export */ + Qwen2Model: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.Qwen2Model + ), + /* harmony export */ + Qwen2PreTrainedModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.Qwen2PreTrainedModel + ), + /* harmony export */ + Qwen2Tokenizer: () => ( + /* reexport safe */ + _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.Qwen2Tokenizer + ), + /* harmony export */ + RTDetrForObjectDetection: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.RTDetrForObjectDetection + ), + /* harmony export */ + RTDetrImageProcessor: () => ( + /* reexport safe */ + _processors_js__WEBPACK_IMPORTED_MODULE_4__.RTDetrImageProcessor + ), + /* harmony export */ + RTDetrModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.RTDetrModel + ), + /* harmony export */ + RTDetrObjectDetectionOutput: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.RTDetrObjectDetectionOutput + ), + /* harmony export */ + RTDetrPreTrainedModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.RTDetrPreTrainedModel + ), + /* harmony export */ + RawImage: () => ( + /* reexport safe */ + _utils_image_js__WEBPACK_IMPORTED_MODULE_7__.RawImage + ), + /* harmony export */ + ResNetForImageClassification: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.ResNetForImageClassification + ), + /* harmony export */ + ResNetModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.ResNetModel + ), + /* harmony export */ + ResNetPreTrainedModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.ResNetPreTrainedModel + ), + /* harmony export */ + RoFormerForMaskedLM: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.RoFormerForMaskedLM + ), + /* harmony export */ + RoFormerForQuestionAnswering: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.RoFormerForQuestionAnswering + ), + /* harmony export */ + RoFormerForSequenceClassification: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.RoFormerForSequenceClassification + ), + /* harmony export */ + RoFormerForTokenClassification: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.RoFormerForTokenClassification + ), + /* harmony export */ + RoFormerModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.RoFormerModel + ), + /* harmony export */ + RoFormerPreTrainedModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.RoFormerPreTrainedModel + ), + /* harmony export */ + RoFormerTokenizer: () => ( + /* reexport safe */ + _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.RoFormerTokenizer + ), + /* harmony export */ + RobertaForMaskedLM: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.RobertaForMaskedLM + ), + /* harmony export */ + RobertaForQuestionAnswering: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.RobertaForQuestionAnswering + ), + /* harmony export */ + RobertaForSequenceClassification: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.RobertaForSequenceClassification + ), + /* harmony export */ + RobertaForTokenClassification: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.RobertaForTokenClassification + ), + /* harmony export */ + RobertaModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.RobertaModel + ), + /* harmony export */ + RobertaPreTrainedModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.RobertaPreTrainedModel + ), + /* harmony export */ + RobertaTokenizer: () => ( + /* reexport safe */ + _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.RobertaTokenizer + ), + /* harmony export */ + SamImageProcessor: () => ( + /* reexport safe */ + _processors_js__WEBPACK_IMPORTED_MODULE_4__.SamImageProcessor + ), + /* harmony export */ + SamImageSegmentationOutput: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.SamImageSegmentationOutput + ), + /* harmony export */ + SamModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.SamModel + ), + /* harmony export */ + SamPreTrainedModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.SamPreTrainedModel + ), + /* harmony export */ + SamProcessor: () => ( + /* reexport safe */ + _processors_js__WEBPACK_IMPORTED_MODULE_4__.SamProcessor + ), + /* harmony export */ + SapiensFeatureExtractor: () => ( + /* reexport safe */ + _processors_js__WEBPACK_IMPORTED_MODULE_4__.SapiensFeatureExtractor + ), + /* harmony export */ + SapiensForDepthEstimation: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.SapiensForDepthEstimation + ), + /* harmony export */ + SapiensForNormalEstimation: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.SapiensForNormalEstimation + ), + /* harmony export */ + SapiensForSemanticSegmentation: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.SapiensForSemanticSegmentation + ), + /* harmony export */ + SapiensPreTrainedModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.SapiensPreTrainedModel + ), + /* harmony export */ + SeamlessM4TFeatureExtractor: () => ( + /* reexport safe */ + _processors_js__WEBPACK_IMPORTED_MODULE_4__.SeamlessM4TFeatureExtractor + ), + /* harmony export */ + SegformerFeatureExtractor: () => ( + /* reexport safe */ + _processors_js__WEBPACK_IMPORTED_MODULE_4__.SegformerFeatureExtractor + ), + /* harmony export */ + SegformerForImageClassification: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.SegformerForImageClassification + ), + /* harmony export */ + SegformerForSemanticSegmentation: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.SegformerForSemanticSegmentation + ), + /* harmony export */ + SegformerModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.SegformerModel + ), + /* harmony export */ + SegformerPreTrainedModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.SegformerPreTrainedModel + ), + /* harmony export */ + Seq2SeqLMOutput: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.Seq2SeqLMOutput + ), + /* harmony export */ + SequenceClassifierOutput: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.SequenceClassifierOutput + ), + /* harmony export */ + SiglipImageProcessor: () => ( + /* reexport safe */ + _processors_js__WEBPACK_IMPORTED_MODULE_4__.SiglipImageProcessor + ), + /* harmony export */ + SiglipModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.SiglipModel + ), + /* harmony export */ + SiglipPreTrainedModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.SiglipPreTrainedModel + ), + /* harmony export */ + SiglipTextModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.SiglipTextModel + ), + /* harmony export */ + SiglipTokenizer: () => ( + /* reexport safe */ + _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.SiglipTokenizer + ), + /* harmony export */ + SiglipVisionModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.SiglipVisionModel + ), + /* harmony export */ + SpeechT5FeatureExtractor: () => ( + /* reexport safe */ + _processors_js__WEBPACK_IMPORTED_MODULE_4__.SpeechT5FeatureExtractor + ), + /* harmony export */ + SpeechT5ForSpeechToText: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.SpeechT5ForSpeechToText + ), + /* harmony export */ + SpeechT5ForTextToSpeech: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.SpeechT5ForTextToSpeech + ), + /* harmony export */ + SpeechT5HifiGan: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.SpeechT5HifiGan + ), + /* harmony export */ + SpeechT5Model: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.SpeechT5Model + ), + /* harmony export */ + SpeechT5PreTrainedModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.SpeechT5PreTrainedModel + ), + /* harmony export */ + SpeechT5Processor: () => ( + /* reexport safe */ + _processors_js__WEBPACK_IMPORTED_MODULE_4__.SpeechT5Processor + ), + /* harmony export */ + SpeechT5Tokenizer: () => ( + /* reexport safe */ + _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.SpeechT5Tokenizer + ), + /* harmony export */ + SqueezeBertForMaskedLM: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.SqueezeBertForMaskedLM + ), + /* harmony export */ + SqueezeBertForQuestionAnswering: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.SqueezeBertForQuestionAnswering + ), + /* harmony export */ + SqueezeBertForSequenceClassification: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.SqueezeBertForSequenceClassification + ), + /* harmony export */ + SqueezeBertModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.SqueezeBertModel + ), + /* harmony export */ + SqueezeBertPreTrainedModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.SqueezeBertPreTrainedModel + ), + /* harmony export */ + SqueezeBertTokenizer: () => ( + /* reexport safe */ + _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.SqueezeBertTokenizer + ), + /* harmony export */ + StableLmForCausalLM: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.StableLmForCausalLM + ), + /* harmony export */ + StableLmModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.StableLmModel + ), + /* harmony export */ + StableLmPreTrainedModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.StableLmPreTrainedModel + ), + /* harmony export */ + Starcoder2ForCausalLM: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.Starcoder2ForCausalLM + ), + /* harmony export */ + Starcoder2Model: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.Starcoder2Model + ), + /* harmony export */ + Starcoder2PreTrainedModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.Starcoder2PreTrainedModel + ), + /* harmony export */ + StoppingCriteria: () => ( + /* reexport safe */ + _generation_stopping_criteria_js__WEBPACK_IMPORTED_MODULE_11__.StoppingCriteria + ), + /* harmony export */ + StoppingCriteriaList: () => ( + /* reexport safe */ + _generation_stopping_criteria_js__WEBPACK_IMPORTED_MODULE_11__.StoppingCriteriaList + ), + /* harmony export */ + SummarizationPipeline: () => ( + /* reexport safe */ + _pipelines_js__WEBPACK_IMPORTED_MODULE_1__.SummarizationPipeline + ), + /* harmony export */ + Swin2SRForImageSuperResolution: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.Swin2SRForImageSuperResolution + ), + /* harmony export */ + Swin2SRImageProcessor: () => ( + /* reexport safe */ + _processors_js__WEBPACK_IMPORTED_MODULE_4__.Swin2SRImageProcessor + ), + /* harmony export */ + Swin2SRModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.Swin2SRModel + ), + /* harmony export */ + Swin2SRPreTrainedModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.Swin2SRPreTrainedModel + ), + /* harmony export */ + SwinForImageClassification: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.SwinForImageClassification + ), + /* harmony export */ + SwinModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.SwinModel + ), + /* harmony export */ + SwinPreTrainedModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.SwinPreTrainedModel + ), + /* harmony export */ + T5ForConditionalGeneration: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.T5ForConditionalGeneration + ), + /* harmony export */ + T5Model: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.T5Model + ), + /* harmony export */ + T5PreTrainedModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.T5PreTrainedModel + ), + /* harmony export */ + T5Tokenizer: () => ( + /* reexport safe */ + _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.T5Tokenizer + ), + /* harmony export */ + TableTransformerForObjectDetection: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.TableTransformerForObjectDetection + ), + /* harmony export */ + TableTransformerModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.TableTransformerModel + ), + /* harmony export */ + TableTransformerObjectDetectionOutput: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.TableTransformerObjectDetectionOutput + ), + /* harmony export */ + TableTransformerPreTrainedModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.TableTransformerPreTrainedModel + ), + /* harmony export */ + Tensor: () => ( + /* reexport safe */ + _utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.Tensor + ), + /* harmony export */ + Text2TextGenerationPipeline: () => ( + /* reexport safe */ + _pipelines_js__WEBPACK_IMPORTED_MODULE_1__.Text2TextGenerationPipeline + ), + /* harmony export */ + TextClassificationPipeline: () => ( + /* reexport safe */ + _pipelines_js__WEBPACK_IMPORTED_MODULE_1__.TextClassificationPipeline + ), + /* harmony export */ + TextGenerationPipeline: () => ( + /* reexport safe */ + _pipelines_js__WEBPACK_IMPORTED_MODULE_1__.TextGenerationPipeline + ), + /* harmony export */ + TextStreamer: () => ( + /* reexport safe */ + _generation_streamers_js__WEBPACK_IMPORTED_MODULE_10__.TextStreamer + ), + /* harmony export */ + TextToAudioPipeline: () => ( + /* reexport safe */ + _pipelines_js__WEBPACK_IMPORTED_MODULE_1__.TextToAudioPipeline + ), + /* harmony export */ + TokenClassificationPipeline: () => ( + /* reexport safe */ + _pipelines_js__WEBPACK_IMPORTED_MODULE_1__.TokenClassificationPipeline + ), + /* harmony export */ + TokenClassifierOutput: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.TokenClassifierOutput + ), + /* harmony export */ + TokenizerModel: () => ( + /* reexport safe */ + _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.TokenizerModel + ), + /* harmony export */ + TrOCRForCausalLM: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.TrOCRForCausalLM + ), + /* harmony export */ + TrOCRPreTrainedModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.TrOCRPreTrainedModel + ), + /* harmony export */ + TranslationPipeline: () => ( + /* reexport safe */ + _pipelines_js__WEBPACK_IMPORTED_MODULE_1__.TranslationPipeline + ), + /* harmony export */ + UniSpeechForCTC: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.UniSpeechForCTC + ), + /* harmony export */ + UniSpeechForSequenceClassification: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.UniSpeechForSequenceClassification + ), + /* harmony export */ + UniSpeechModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.UniSpeechModel + ), + /* harmony export */ + UniSpeechPreTrainedModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.UniSpeechPreTrainedModel + ), + /* harmony export */ + UniSpeechSatForAudioFrameClassification: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.UniSpeechSatForAudioFrameClassification + ), + /* harmony export */ + UniSpeechSatForCTC: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.UniSpeechSatForCTC + ), + /* harmony export */ + UniSpeechSatForSequenceClassification: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.UniSpeechSatForSequenceClassification + ), + /* harmony export */ + UniSpeechSatModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.UniSpeechSatModel + ), + /* harmony export */ + UniSpeechSatPreTrainedModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.UniSpeechSatPreTrainedModel + ), + /* harmony export */ + ViTFeatureExtractor: () => ( + /* reexport safe */ + _processors_js__WEBPACK_IMPORTED_MODULE_4__.ViTFeatureExtractor + ), + /* harmony export */ + ViTForImageClassification: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.ViTForImageClassification + ), + /* harmony export */ + ViTImageProcessor: () => ( + /* reexport safe */ + _processors_js__WEBPACK_IMPORTED_MODULE_4__.ViTImageProcessor + ), + /* harmony export */ + ViTMAEModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.ViTMAEModel + ), + /* harmony export */ + ViTMAEPreTrainedModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.ViTMAEPreTrainedModel + ), + /* harmony export */ + ViTMSNForImageClassification: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.ViTMSNForImageClassification + ), + /* harmony export */ + ViTMSNModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.ViTMSNModel + ), + /* harmony export */ + ViTMSNPreTrainedModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.ViTMSNPreTrainedModel + ), + /* harmony export */ + ViTModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.ViTModel + ), + /* harmony export */ + ViTPreTrainedModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.ViTPreTrainedModel + ), + /* harmony export */ + VisionEncoderDecoderModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.VisionEncoderDecoderModel + ), + /* harmony export */ + VitMatteForImageMatting: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.VitMatteForImageMatting + ), + /* harmony export */ + VitMatteImageProcessor: () => ( + /* reexport safe */ + _processors_js__WEBPACK_IMPORTED_MODULE_4__.VitMatteImageProcessor + ), + /* harmony export */ + VitMattePreTrainedModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.VitMattePreTrainedModel + ), + /* harmony export */ + VitsModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.VitsModel + ), + /* harmony export */ + VitsModelOutput: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.VitsModelOutput + ), + /* harmony export */ + VitsPreTrainedModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.VitsPreTrainedModel + ), + /* harmony export */ + VitsTokenizer: () => ( + /* reexport safe */ + _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.VitsTokenizer + ), + /* harmony export */ + Wav2Vec2BertForCTC: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.Wav2Vec2BertForCTC + ), + /* harmony export */ + Wav2Vec2BertForSequenceClassification: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.Wav2Vec2BertForSequenceClassification + ), + /* harmony export */ + Wav2Vec2BertModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.Wav2Vec2BertModel + ), + /* harmony export */ + Wav2Vec2BertPreTrainedModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.Wav2Vec2BertPreTrainedModel + ), + /* harmony export */ + Wav2Vec2CTCTokenizer: () => ( + /* reexport safe */ + _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.Wav2Vec2CTCTokenizer + ), + /* harmony export */ + Wav2Vec2FeatureExtractor: () => ( + /* reexport safe */ + _processors_js__WEBPACK_IMPORTED_MODULE_4__.Wav2Vec2FeatureExtractor + ), + /* harmony export */ + Wav2Vec2ForAudioFrameClassification: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.Wav2Vec2ForAudioFrameClassification + ), + /* harmony export */ + Wav2Vec2ForCTC: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.Wav2Vec2ForCTC + ), + /* harmony export */ + Wav2Vec2ForSequenceClassification: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.Wav2Vec2ForSequenceClassification + ), + /* harmony export */ + Wav2Vec2Model: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.Wav2Vec2Model + ), + /* harmony export */ + Wav2Vec2PreTrainedModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.Wav2Vec2PreTrainedModel + ), + /* harmony export */ + Wav2Vec2ProcessorWithLM: () => ( + /* reexport safe */ + _processors_js__WEBPACK_IMPORTED_MODULE_4__.Wav2Vec2ProcessorWithLM + ), + /* harmony export */ + WavLMForAudioFrameClassification: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.WavLMForAudioFrameClassification + ), + /* harmony export */ + WavLMForCTC: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.WavLMForCTC + ), + /* harmony export */ + WavLMForSequenceClassification: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.WavLMForSequenceClassification + ), + /* harmony export */ + WavLMForXVector: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.WavLMForXVector + ), + /* harmony export */ + WavLMModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.WavLMModel + ), + /* harmony export */ + WavLMPreTrainedModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.WavLMPreTrainedModel + ), + /* harmony export */ + WeSpeakerFeatureExtractor: () => ( + /* reexport safe */ + _processors_js__WEBPACK_IMPORTED_MODULE_4__.WeSpeakerFeatureExtractor + ), + /* harmony export */ + WeSpeakerResNetModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.WeSpeakerResNetModel + ), + /* harmony export */ + WeSpeakerResNetPreTrainedModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.WeSpeakerResNetPreTrainedModel + ), + /* harmony export */ + WhisperFeatureExtractor: () => ( + /* reexport safe */ + _processors_js__WEBPACK_IMPORTED_MODULE_4__.WhisperFeatureExtractor + ), + /* harmony export */ + WhisperForConditionalGeneration: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.WhisperForConditionalGeneration + ), + /* harmony export */ + WhisperModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.WhisperModel + ), + /* harmony export */ + WhisperPreTrainedModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.WhisperPreTrainedModel + ), + /* harmony export */ + WhisperProcessor: () => ( + /* reexport safe */ + _processors_js__WEBPACK_IMPORTED_MODULE_4__.WhisperProcessor + ), + /* harmony export */ + WhisperTextStreamer: () => ( + /* reexport safe */ + _generation_streamers_js__WEBPACK_IMPORTED_MODULE_10__.WhisperTextStreamer + ), + /* harmony export */ + WhisperTokenizer: () => ( + /* reexport safe */ + _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.WhisperTokenizer + ), + /* harmony export */ + XLMForQuestionAnswering: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.XLMForQuestionAnswering + ), + /* harmony export */ + XLMForSequenceClassification: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.XLMForSequenceClassification + ), + /* harmony export */ + XLMForTokenClassification: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.XLMForTokenClassification + ), + /* harmony export */ + XLMModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.XLMModel + ), + /* harmony export */ + XLMPreTrainedModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.XLMPreTrainedModel + ), + /* harmony export */ + XLMRobertaForMaskedLM: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.XLMRobertaForMaskedLM + ), + /* harmony export */ + XLMRobertaForQuestionAnswering: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.XLMRobertaForQuestionAnswering + ), + /* harmony export */ + XLMRobertaForSequenceClassification: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.XLMRobertaForSequenceClassification + ), + /* harmony export */ + XLMRobertaForTokenClassification: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.XLMRobertaForTokenClassification + ), + /* harmony export */ + XLMRobertaModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.XLMRobertaModel + ), + /* harmony export */ + XLMRobertaPreTrainedModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.XLMRobertaPreTrainedModel + ), + /* harmony export */ + XLMRobertaTokenizer: () => ( + /* reexport safe */ + _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.XLMRobertaTokenizer + ), + /* harmony export */ + XLMTokenizer: () => ( + /* reexport safe */ + _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.XLMTokenizer + ), + /* harmony export */ + XLMWithLMHeadModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.XLMWithLMHeadModel + ), + /* harmony export */ + XVectorOutput: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.XVectorOutput + ), + /* harmony export */ + YolosFeatureExtractor: () => ( + /* reexport safe */ + _processors_js__WEBPACK_IMPORTED_MODULE_4__.YolosFeatureExtractor + ), + /* harmony export */ + YolosForObjectDetection: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.YolosForObjectDetection + ), + /* harmony export */ + YolosModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.YolosModel + ), + /* harmony export */ + YolosObjectDetectionOutput: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.YolosObjectDetectionOutput + ), + /* harmony export */ + YolosPreTrainedModel: () => ( + /* reexport safe */ + _models_js__WEBPACK_IMPORTED_MODULE_2__.YolosPreTrainedModel + ), + /* harmony export */ + ZeroShotAudioClassificationPipeline: () => ( + /* reexport safe */ + _pipelines_js__WEBPACK_IMPORTED_MODULE_1__.ZeroShotAudioClassificationPipeline + ), + /* harmony export */ + ZeroShotClassificationPipeline: () => ( + /* reexport safe */ + _pipelines_js__WEBPACK_IMPORTED_MODULE_1__.ZeroShotClassificationPipeline + ), + /* harmony export */ + ZeroShotImageClassificationPipeline: () => ( + /* reexport safe */ + _pipelines_js__WEBPACK_IMPORTED_MODULE_1__.ZeroShotImageClassificationPipeline + ), + /* harmony export */ + ZeroShotObjectDetectionPipeline: () => ( + /* reexport safe */ + _pipelines_js__WEBPACK_IMPORTED_MODULE_1__.ZeroShotObjectDetectionPipeline + ), + /* harmony export */ + bankers_round: () => ( + /* reexport safe */ + _utils_maths_js__WEBPACK_IMPORTED_MODULE_9__.bankers_round + ), + /* harmony export */ + cat: () => ( + /* reexport safe */ + _utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.cat + ), + /* harmony export */ + cos_sim: () => ( + /* reexport safe */ + _utils_maths_js__WEBPACK_IMPORTED_MODULE_9__.cos_sim + ), + /* harmony export */ + dot: () => ( + /* reexport safe */ + _utils_maths_js__WEBPACK_IMPORTED_MODULE_9__.dot + ), + /* harmony export */ + dynamic_time_warping: () => ( + /* reexport safe */ + _utils_maths_js__WEBPACK_IMPORTED_MODULE_9__.dynamic_time_warping + ), + /* harmony export */ + env: () => ( + /* reexport safe */ + _env_js__WEBPACK_IMPORTED_MODULE_0__.env + ), + /* harmony export */ + full: () => ( + /* reexport safe */ + _utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.full + ), + /* harmony export */ + full_like: () => ( + /* reexport safe */ + _utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.full_like + ), + /* harmony export */ + getKeyValueShapes: () => ( + /* reexport safe */ + _configs_js__WEBPACK_IMPORTED_MODULE_5__.getKeyValueShapes + ), + /* harmony export */ + hamming: () => ( + /* reexport safe */ + _utils_audio_js__WEBPACK_IMPORTED_MODULE_6__.hamming + ), + /* harmony export */ + hanning: () => ( + /* reexport safe */ + _utils_audio_js__WEBPACK_IMPORTED_MODULE_6__.hanning + ), + /* harmony export */ + interpolate: () => ( + /* reexport safe */ + _utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.interpolate + ), + /* harmony export */ + interpolate_4d: () => ( + /* reexport safe */ + _utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.interpolate_4d + ), + /* harmony export */ + interpolate_data: () => ( + /* reexport safe */ + _utils_maths_js__WEBPACK_IMPORTED_MODULE_9__.interpolate_data + ), + /* harmony export */ + is_chinese_char: () => ( + /* reexport safe */ + _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.is_chinese_char + ), + /* harmony export */ + layer_norm: () => ( + /* reexport safe */ + _utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.layer_norm + ), + /* harmony export */ + log_softmax: () => ( + /* reexport safe */ + _utils_maths_js__WEBPACK_IMPORTED_MODULE_9__.log_softmax + ), + /* harmony export */ + magnitude: () => ( + /* reexport safe */ + _utils_maths_js__WEBPACK_IMPORTED_MODULE_9__.magnitude + ), + /* harmony export */ + matmul: () => ( + /* reexport safe */ + _utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.matmul + ), + /* harmony export */ + max: () => ( + /* reexport safe */ + _utils_maths_js__WEBPACK_IMPORTED_MODULE_9__.max + ), + /* harmony export */ + mean: () => ( + /* reexport safe */ + _utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.mean + ), + /* harmony export */ + mean_pooling: () => ( + /* reexport safe */ + _utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.mean_pooling + ), + /* harmony export */ + medianFilter: () => ( + /* reexport safe */ + _utils_maths_js__WEBPACK_IMPORTED_MODULE_9__.medianFilter + ), + /* harmony export */ + mel_filter_bank: () => ( + /* reexport safe */ + _utils_audio_js__WEBPACK_IMPORTED_MODULE_6__.mel_filter_bank + ), + /* harmony export */ + min: () => ( + /* reexport safe */ + _utils_maths_js__WEBPACK_IMPORTED_MODULE_9__.min + ), + /* harmony export */ + ones: () => ( + /* reexport safe */ + _utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.ones + ), + /* harmony export */ + ones_like: () => ( + /* reexport safe */ + _utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.ones_like + ), + /* harmony export */ + permute: () => ( + /* reexport safe */ + _utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.permute + ), + /* harmony export */ + permute_data: () => ( + /* reexport safe */ + _utils_maths_js__WEBPACK_IMPORTED_MODULE_9__.permute_data + ), + /* harmony export */ + pipeline: () => ( + /* reexport safe */ + _pipelines_js__WEBPACK_IMPORTED_MODULE_1__.pipeline + ), + /* harmony export */ + quantize_embeddings: () => ( + /* reexport safe */ + _utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.quantize_embeddings + ), + /* harmony export */ + read_audio: () => ( + /* reexport safe */ + _utils_audio_js__WEBPACK_IMPORTED_MODULE_6__.read_audio + ), + /* harmony export */ + rfft: () => ( + /* reexport safe */ + _utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.rfft + ), + /* harmony export */ + round: () => ( + /* reexport safe */ + _utils_maths_js__WEBPACK_IMPORTED_MODULE_9__.round + ), + /* harmony export */ + softmax: () => ( + /* reexport safe */ + _utils_maths_js__WEBPACK_IMPORTED_MODULE_9__.softmax + ), + /* harmony export */ + spectrogram: () => ( + /* reexport safe */ + _utils_audio_js__WEBPACK_IMPORTED_MODULE_6__.spectrogram + ), + /* harmony export */ + stack: () => ( + /* reexport safe */ + _utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.stack + ), + /* harmony export */ + std_mean: () => ( + /* reexport safe */ + _utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.std_mean + ), + /* harmony export */ + topk: () => ( + /* reexport safe */ + _utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.topk + ), + /* harmony export */ + window_function: () => ( + /* reexport safe */ + _utils_audio_js__WEBPACK_IMPORTED_MODULE_6__.window_function + ), + /* harmony export */ + zeros: () => ( + /* reexport safe */ + _utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.zeros + ), + /* harmony export */ + zeros_like: () => ( + /* reexport safe */ + _utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.zeros_like + ) + /* harmony export */ + }); + var _env_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__( + /*! ./env.js */ + "./src/env.js" + ); + var _pipelines_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__( + /*! ./pipelines.js */ + "./src/pipelines.js" + ); + var _models_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__( + /*! ./models.js */ + "./src/models.js" + ); + var _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__( + /*! ./tokenizers.js */ + "./src/tokenizers.js" + ); + var _processors_js__WEBPACK_IMPORTED_MODULE_4__ = __webpack_require__( + /*! ./processors.js */ + "./src/processors.js" + ); + var _configs_js__WEBPACK_IMPORTED_MODULE_5__ = __webpack_require__( + /*! ./configs.js */ + "./src/configs.js" + ); + var _utils_audio_js__WEBPACK_IMPORTED_MODULE_6__ = __webpack_require__( + /*! ./utils/audio.js */ + "./src/utils/audio.js" + ); + var _utils_image_js__WEBPACK_IMPORTED_MODULE_7__ = __webpack_require__( + /*! ./utils/image.js */ + "./src/utils/image.js" + ); + var _utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__ = __webpack_require__( + /*! ./utils/tensor.js */ + "./src/utils/tensor.js" + ); + var _utils_maths_js__WEBPACK_IMPORTED_MODULE_9__ = __webpack_require__( + /*! ./utils/maths.js */ + "./src/utils/maths.js" + ); + var _generation_streamers_js__WEBPACK_IMPORTED_MODULE_10__ = __webpack_require__( + /*! ./generation/streamers.js */ + "./src/generation/streamers.js" + ); + var _generation_stopping_criteria_js__WEBPACK_IMPORTED_MODULE_11__ = __webpack_require__( + /*! ./generation/stopping_criteria.js */ + "./src/generation/stopping_criteria.js" + ); + var __webpack_exports__ASTFeatureExtractor = __webpack_exports__.ASTFeatureExtractor; + var __webpack_exports__ASTForAudioClassification = __webpack_exports__.ASTForAudioClassification; + var __webpack_exports__ASTModel = __webpack_exports__.ASTModel; + var __webpack_exports__ASTPreTrainedModel = __webpack_exports__.ASTPreTrainedModel; + var __webpack_exports__AlbertForMaskedLM = __webpack_exports__.AlbertForMaskedLM; + var __webpack_exports__AlbertForQuestionAnswering = __webpack_exports__.AlbertForQuestionAnswering; + var __webpack_exports__AlbertForSequenceClassification = __webpack_exports__.AlbertForSequenceClassification; + var __webpack_exports__AlbertModel = __webpack_exports__.AlbertModel; + var __webpack_exports__AlbertPreTrainedModel = __webpack_exports__.AlbertPreTrainedModel; + var __webpack_exports__AlbertTokenizer = __webpack_exports__.AlbertTokenizer; + var __webpack_exports__AudioClassificationPipeline = __webpack_exports__.AudioClassificationPipeline; + var __webpack_exports__AutoConfig = __webpack_exports__.AutoConfig; + var __webpack_exports__AutoModel = __webpack_exports__.AutoModel; + var __webpack_exports__AutoModelForAudioClassification = __webpack_exports__.AutoModelForAudioClassification; + var __webpack_exports__AutoModelForAudioFrameClassification = __webpack_exports__.AutoModelForAudioFrameClassification; + var __webpack_exports__AutoModelForCTC = __webpack_exports__.AutoModelForCTC; + var __webpack_exports__AutoModelForCausalLM = __webpack_exports__.AutoModelForCausalLM; + var __webpack_exports__AutoModelForDepthEstimation = __webpack_exports__.AutoModelForDepthEstimation; + var __webpack_exports__AutoModelForDocumentQuestionAnswering = __webpack_exports__.AutoModelForDocumentQuestionAnswering; + var __webpack_exports__AutoModelForImageClassification = __webpack_exports__.AutoModelForImageClassification; + var __webpack_exports__AutoModelForImageFeatureExtraction = __webpack_exports__.AutoModelForImageFeatureExtraction; + var __webpack_exports__AutoModelForImageMatting = __webpack_exports__.AutoModelForImageMatting; + var __webpack_exports__AutoModelForImageSegmentation = __webpack_exports__.AutoModelForImageSegmentation; + var __webpack_exports__AutoModelForImageToImage = __webpack_exports__.AutoModelForImageToImage; + var __webpack_exports__AutoModelForMaskGeneration = __webpack_exports__.AutoModelForMaskGeneration; + var __webpack_exports__AutoModelForMaskedLM = __webpack_exports__.AutoModelForMaskedLM; + var __webpack_exports__AutoModelForNormalEstimation = __webpack_exports__.AutoModelForNormalEstimation; + var __webpack_exports__AutoModelForObjectDetection = __webpack_exports__.AutoModelForObjectDetection; + var __webpack_exports__AutoModelForQuestionAnswering = __webpack_exports__.AutoModelForQuestionAnswering; + var __webpack_exports__AutoModelForSemanticSegmentation = __webpack_exports__.AutoModelForSemanticSegmentation; + var __webpack_exports__AutoModelForSeq2SeqLM = __webpack_exports__.AutoModelForSeq2SeqLM; + var __webpack_exports__AutoModelForSequenceClassification = __webpack_exports__.AutoModelForSequenceClassification; + var __webpack_exports__AutoModelForSpeechSeq2Seq = __webpack_exports__.AutoModelForSpeechSeq2Seq; + var __webpack_exports__AutoModelForTextToSpectrogram = __webpack_exports__.AutoModelForTextToSpectrogram; + var __webpack_exports__AutoModelForTextToWaveform = __webpack_exports__.AutoModelForTextToWaveform; + var __webpack_exports__AutoModelForTokenClassification = __webpack_exports__.AutoModelForTokenClassification; + var __webpack_exports__AutoModelForUniversalSegmentation = __webpack_exports__.AutoModelForUniversalSegmentation; + var __webpack_exports__AutoModelForVision2Seq = __webpack_exports__.AutoModelForVision2Seq; + var __webpack_exports__AutoModelForXVector = __webpack_exports__.AutoModelForXVector; + var __webpack_exports__AutoModelForZeroShotObjectDetection = __webpack_exports__.AutoModelForZeroShotObjectDetection; + var __webpack_exports__AutoProcessor = __webpack_exports__.AutoProcessor; + var __webpack_exports__AutoTokenizer = __webpack_exports__.AutoTokenizer; + var __webpack_exports__AutomaticSpeechRecognitionPipeline = __webpack_exports__.AutomaticSpeechRecognitionPipeline; + var __webpack_exports__BartForConditionalGeneration = __webpack_exports__.BartForConditionalGeneration; + var __webpack_exports__BartForSequenceClassification = __webpack_exports__.BartForSequenceClassification; + var __webpack_exports__BartModel = __webpack_exports__.BartModel; + var __webpack_exports__BartPretrainedModel = __webpack_exports__.BartPretrainedModel; + var __webpack_exports__BartTokenizer = __webpack_exports__.BartTokenizer; + var __webpack_exports__BaseModelOutput = __webpack_exports__.BaseModelOutput; + var __webpack_exports__BaseStreamer = __webpack_exports__.BaseStreamer; + var __webpack_exports__BeitFeatureExtractor = __webpack_exports__.BeitFeatureExtractor; + var __webpack_exports__BeitForImageClassification = __webpack_exports__.BeitForImageClassification; + var __webpack_exports__BeitModel = __webpack_exports__.BeitModel; + var __webpack_exports__BeitPreTrainedModel = __webpack_exports__.BeitPreTrainedModel; + var __webpack_exports__BertForMaskedLM = __webpack_exports__.BertForMaskedLM; + var __webpack_exports__BertForQuestionAnswering = __webpack_exports__.BertForQuestionAnswering; + var __webpack_exports__BertForSequenceClassification = __webpack_exports__.BertForSequenceClassification; + var __webpack_exports__BertForTokenClassification = __webpack_exports__.BertForTokenClassification; + var __webpack_exports__BertModel = __webpack_exports__.BertModel; + var __webpack_exports__BertPreTrainedModel = __webpack_exports__.BertPreTrainedModel; + var __webpack_exports__BertTokenizer = __webpack_exports__.BertTokenizer; + var __webpack_exports__BitImageProcessor = __webpack_exports__.BitImageProcessor; + var __webpack_exports__BlenderbotForConditionalGeneration = __webpack_exports__.BlenderbotForConditionalGeneration; + var __webpack_exports__BlenderbotModel = __webpack_exports__.BlenderbotModel; + var __webpack_exports__BlenderbotPreTrainedModel = __webpack_exports__.BlenderbotPreTrainedModel; + var __webpack_exports__BlenderbotSmallForConditionalGeneration = __webpack_exports__.BlenderbotSmallForConditionalGeneration; + var __webpack_exports__BlenderbotSmallModel = __webpack_exports__.BlenderbotSmallModel; + var __webpack_exports__BlenderbotSmallPreTrainedModel = __webpack_exports__.BlenderbotSmallPreTrainedModel; + var __webpack_exports__BlenderbotSmallTokenizer = __webpack_exports__.BlenderbotSmallTokenizer; + var __webpack_exports__BlenderbotTokenizer = __webpack_exports__.BlenderbotTokenizer; + var __webpack_exports__BloomForCausalLM = __webpack_exports__.BloomForCausalLM; + var __webpack_exports__BloomModel = __webpack_exports__.BloomModel; + var __webpack_exports__BloomPreTrainedModel = __webpack_exports__.BloomPreTrainedModel; + var __webpack_exports__BloomTokenizer = __webpack_exports__.BloomTokenizer; + var __webpack_exports__CLIPFeatureExtractor = __webpack_exports__.CLIPFeatureExtractor; + var __webpack_exports__CLIPImageProcessor = __webpack_exports__.CLIPImageProcessor; + var __webpack_exports__CLIPModel = __webpack_exports__.CLIPModel; + var __webpack_exports__CLIPPreTrainedModel = __webpack_exports__.CLIPPreTrainedModel; + var __webpack_exports__CLIPSegForImageSegmentation = __webpack_exports__.CLIPSegForImageSegmentation; + var __webpack_exports__CLIPSegModel = __webpack_exports__.CLIPSegModel; + var __webpack_exports__CLIPSegPreTrainedModel = __webpack_exports__.CLIPSegPreTrainedModel; + var __webpack_exports__CLIPTextModel = __webpack_exports__.CLIPTextModel; + var __webpack_exports__CLIPTextModelWithProjection = __webpack_exports__.CLIPTextModelWithProjection; + var __webpack_exports__CLIPTokenizer = __webpack_exports__.CLIPTokenizer; + var __webpack_exports__CLIPVisionModel = __webpack_exports__.CLIPVisionModel; + var __webpack_exports__CLIPVisionModelWithProjection = __webpack_exports__.CLIPVisionModelWithProjection; + var __webpack_exports__CamembertForMaskedLM = __webpack_exports__.CamembertForMaskedLM; + var __webpack_exports__CamembertForQuestionAnswering = __webpack_exports__.CamembertForQuestionAnswering; + var __webpack_exports__CamembertForSequenceClassification = __webpack_exports__.CamembertForSequenceClassification; + var __webpack_exports__CamembertForTokenClassification = __webpack_exports__.CamembertForTokenClassification; + var __webpack_exports__CamembertModel = __webpack_exports__.CamembertModel; + var __webpack_exports__CamembertPreTrainedModel = __webpack_exports__.CamembertPreTrainedModel; + var __webpack_exports__CamembertTokenizer = __webpack_exports__.CamembertTokenizer; + var __webpack_exports__CausalLMOutput = __webpack_exports__.CausalLMOutput; + var __webpack_exports__CausalLMOutputWithPast = __webpack_exports__.CausalLMOutputWithPast; + var __webpack_exports__ChineseCLIPFeatureExtractor = __webpack_exports__.ChineseCLIPFeatureExtractor; + var __webpack_exports__ChineseCLIPModel = __webpack_exports__.ChineseCLIPModel; + var __webpack_exports__ChineseCLIPPreTrainedModel = __webpack_exports__.ChineseCLIPPreTrainedModel; + var __webpack_exports__ClapAudioModelWithProjection = __webpack_exports__.ClapAudioModelWithProjection; + var __webpack_exports__ClapFeatureExtractor = __webpack_exports__.ClapFeatureExtractor; + var __webpack_exports__ClapModel = __webpack_exports__.ClapModel; + var __webpack_exports__ClapPreTrainedModel = __webpack_exports__.ClapPreTrainedModel; + var __webpack_exports__ClapTextModelWithProjection = __webpack_exports__.ClapTextModelWithProjection; + var __webpack_exports__CodeGenForCausalLM = __webpack_exports__.CodeGenForCausalLM; + var __webpack_exports__CodeGenModel = __webpack_exports__.CodeGenModel; + var __webpack_exports__CodeGenPreTrainedModel = __webpack_exports__.CodeGenPreTrainedModel; + var __webpack_exports__CodeGenTokenizer = __webpack_exports__.CodeGenTokenizer; + var __webpack_exports__CodeLlamaTokenizer = __webpack_exports__.CodeLlamaTokenizer; + var __webpack_exports__CohereForCausalLM = __webpack_exports__.CohereForCausalLM; + var __webpack_exports__CohereModel = __webpack_exports__.CohereModel; + var __webpack_exports__CoherePreTrainedModel = __webpack_exports__.CoherePreTrainedModel; + var __webpack_exports__CohereTokenizer = __webpack_exports__.CohereTokenizer; + var __webpack_exports__ConvBertForMaskedLM = __webpack_exports__.ConvBertForMaskedLM; + var __webpack_exports__ConvBertForQuestionAnswering = __webpack_exports__.ConvBertForQuestionAnswering; + var __webpack_exports__ConvBertForSequenceClassification = __webpack_exports__.ConvBertForSequenceClassification; + var __webpack_exports__ConvBertForTokenClassification = __webpack_exports__.ConvBertForTokenClassification; + var __webpack_exports__ConvBertModel = __webpack_exports__.ConvBertModel; + var __webpack_exports__ConvBertPreTrainedModel = __webpack_exports__.ConvBertPreTrainedModel; + var __webpack_exports__ConvBertTokenizer = __webpack_exports__.ConvBertTokenizer; + var __webpack_exports__ConvNextFeatureExtractor = __webpack_exports__.ConvNextFeatureExtractor; + var __webpack_exports__ConvNextForImageClassification = __webpack_exports__.ConvNextForImageClassification; + var __webpack_exports__ConvNextImageProcessor = __webpack_exports__.ConvNextImageProcessor; + var __webpack_exports__ConvNextModel = __webpack_exports__.ConvNextModel; + var __webpack_exports__ConvNextPreTrainedModel = __webpack_exports__.ConvNextPreTrainedModel; + var __webpack_exports__ConvNextV2ForImageClassification = __webpack_exports__.ConvNextV2ForImageClassification; + var __webpack_exports__ConvNextV2Model = __webpack_exports__.ConvNextV2Model; + var __webpack_exports__ConvNextV2PreTrainedModel = __webpack_exports__.ConvNextV2PreTrainedModel; + var __webpack_exports__DPTFeatureExtractor = __webpack_exports__.DPTFeatureExtractor; + var __webpack_exports__DPTForDepthEstimation = __webpack_exports__.DPTForDepthEstimation; + var __webpack_exports__DPTImageProcessor = __webpack_exports__.DPTImageProcessor; + var __webpack_exports__DPTModel = __webpack_exports__.DPTModel; + var __webpack_exports__DPTPreTrainedModel = __webpack_exports__.DPTPreTrainedModel; + var __webpack_exports__DebertaForMaskedLM = __webpack_exports__.DebertaForMaskedLM; + var __webpack_exports__DebertaForQuestionAnswering = __webpack_exports__.DebertaForQuestionAnswering; + var __webpack_exports__DebertaForSequenceClassification = __webpack_exports__.DebertaForSequenceClassification; + var __webpack_exports__DebertaForTokenClassification = __webpack_exports__.DebertaForTokenClassification; + var __webpack_exports__DebertaModel = __webpack_exports__.DebertaModel; + var __webpack_exports__DebertaPreTrainedModel = __webpack_exports__.DebertaPreTrainedModel; + var __webpack_exports__DebertaTokenizer = __webpack_exports__.DebertaTokenizer; + var __webpack_exports__DebertaV2ForMaskedLM = __webpack_exports__.DebertaV2ForMaskedLM; + var __webpack_exports__DebertaV2ForQuestionAnswering = __webpack_exports__.DebertaV2ForQuestionAnswering; + var __webpack_exports__DebertaV2ForSequenceClassification = __webpack_exports__.DebertaV2ForSequenceClassification; + var __webpack_exports__DebertaV2ForTokenClassification = __webpack_exports__.DebertaV2ForTokenClassification; + var __webpack_exports__DebertaV2Model = __webpack_exports__.DebertaV2Model; + var __webpack_exports__DebertaV2PreTrainedModel = __webpack_exports__.DebertaV2PreTrainedModel; + var __webpack_exports__DebertaV2Tokenizer = __webpack_exports__.DebertaV2Tokenizer; + var __webpack_exports__DecisionTransformerModel = __webpack_exports__.DecisionTransformerModel; + var __webpack_exports__DecisionTransformerPreTrainedModel = __webpack_exports__.DecisionTransformerPreTrainedModel; + var __webpack_exports__DeiTFeatureExtractor = __webpack_exports__.DeiTFeatureExtractor; + var __webpack_exports__DeiTForImageClassification = __webpack_exports__.DeiTForImageClassification; + var __webpack_exports__DeiTModel = __webpack_exports__.DeiTModel; + var __webpack_exports__DeiTPreTrainedModel = __webpack_exports__.DeiTPreTrainedModel; + var __webpack_exports__DepthAnythingForDepthEstimation = __webpack_exports__.DepthAnythingForDepthEstimation; + var __webpack_exports__DepthAnythingPreTrainedModel = __webpack_exports__.DepthAnythingPreTrainedModel; + var __webpack_exports__DepthEstimationPipeline = __webpack_exports__.DepthEstimationPipeline; + var __webpack_exports__DepthProForDepthEstimation = __webpack_exports__.DepthProForDepthEstimation; + var __webpack_exports__DepthProPreTrainedModel = __webpack_exports__.DepthProPreTrainedModel; + var __webpack_exports__DetrFeatureExtractor = __webpack_exports__.DetrFeatureExtractor; + var __webpack_exports__DetrForObjectDetection = __webpack_exports__.DetrForObjectDetection; + var __webpack_exports__DetrForSegmentation = __webpack_exports__.DetrForSegmentation; + var __webpack_exports__DetrModel = __webpack_exports__.DetrModel; + var __webpack_exports__DetrObjectDetectionOutput = __webpack_exports__.DetrObjectDetectionOutput; + var __webpack_exports__DetrPreTrainedModel = __webpack_exports__.DetrPreTrainedModel; + var __webpack_exports__DetrSegmentationOutput = __webpack_exports__.DetrSegmentationOutput; + var __webpack_exports__Dinov2ForImageClassification = __webpack_exports__.Dinov2ForImageClassification; + var __webpack_exports__Dinov2Model = __webpack_exports__.Dinov2Model; + var __webpack_exports__Dinov2PreTrainedModel = __webpack_exports__.Dinov2PreTrainedModel; + var __webpack_exports__DistilBertForMaskedLM = __webpack_exports__.DistilBertForMaskedLM; + var __webpack_exports__DistilBertForQuestionAnswering = __webpack_exports__.DistilBertForQuestionAnswering; + var __webpack_exports__DistilBertForSequenceClassification = __webpack_exports__.DistilBertForSequenceClassification; + var __webpack_exports__DistilBertForTokenClassification = __webpack_exports__.DistilBertForTokenClassification; + var __webpack_exports__DistilBertModel = __webpack_exports__.DistilBertModel; + var __webpack_exports__DistilBertPreTrainedModel = __webpack_exports__.DistilBertPreTrainedModel; + var __webpack_exports__DistilBertTokenizer = __webpack_exports__.DistilBertTokenizer; + var __webpack_exports__DocumentQuestionAnsweringPipeline = __webpack_exports__.DocumentQuestionAnsweringPipeline; + var __webpack_exports__DonutFeatureExtractor = __webpack_exports__.DonutFeatureExtractor; + var __webpack_exports__DonutSwinModel = __webpack_exports__.DonutSwinModel; + var __webpack_exports__DonutSwinPreTrainedModel = __webpack_exports__.DonutSwinPreTrainedModel; + var __webpack_exports__EfficientNetForImageClassification = __webpack_exports__.EfficientNetForImageClassification; + var __webpack_exports__EfficientNetImageProcessor = __webpack_exports__.EfficientNetImageProcessor; + var __webpack_exports__EfficientNetModel = __webpack_exports__.EfficientNetModel; + var __webpack_exports__EfficientNetPreTrainedModel = __webpack_exports__.EfficientNetPreTrainedModel; + var __webpack_exports__ElectraForMaskedLM = __webpack_exports__.ElectraForMaskedLM; + var __webpack_exports__ElectraForQuestionAnswering = __webpack_exports__.ElectraForQuestionAnswering; + var __webpack_exports__ElectraForSequenceClassification = __webpack_exports__.ElectraForSequenceClassification; + var __webpack_exports__ElectraForTokenClassification = __webpack_exports__.ElectraForTokenClassification; + var __webpack_exports__ElectraModel = __webpack_exports__.ElectraModel; + var __webpack_exports__ElectraPreTrainedModel = __webpack_exports__.ElectraPreTrainedModel; + var __webpack_exports__ElectraTokenizer = __webpack_exports__.ElectraTokenizer; + var __webpack_exports__EosTokenCriteria = __webpack_exports__.EosTokenCriteria; + var __webpack_exports__EsmForMaskedLM = __webpack_exports__.EsmForMaskedLM; + var __webpack_exports__EsmForSequenceClassification = __webpack_exports__.EsmForSequenceClassification; + var __webpack_exports__EsmForTokenClassification = __webpack_exports__.EsmForTokenClassification; + var __webpack_exports__EsmModel = __webpack_exports__.EsmModel; + var __webpack_exports__EsmPreTrainedModel = __webpack_exports__.EsmPreTrainedModel; + var __webpack_exports__EsmTokenizer = __webpack_exports__.EsmTokenizer; + var __webpack_exports__FFT = __webpack_exports__.FFT; + var __webpack_exports__FalconForCausalLM = __webpack_exports__.FalconForCausalLM; + var __webpack_exports__FalconModel = __webpack_exports__.FalconModel; + var __webpack_exports__FalconPreTrainedModel = __webpack_exports__.FalconPreTrainedModel; + var __webpack_exports__FalconTokenizer = __webpack_exports__.FalconTokenizer; + var __webpack_exports__FastViTForImageClassification = __webpack_exports__.FastViTForImageClassification; + var __webpack_exports__FastViTModel = __webpack_exports__.FastViTModel; + var __webpack_exports__FastViTPreTrainedModel = __webpack_exports__.FastViTPreTrainedModel; + var __webpack_exports__FeatureExtractionPipeline = __webpack_exports__.FeatureExtractionPipeline; + var __webpack_exports__FeatureExtractor = __webpack_exports__.FeatureExtractor; + var __webpack_exports__FillMaskPipeline = __webpack_exports__.FillMaskPipeline; + var __webpack_exports__Florence2ForConditionalGeneration = __webpack_exports__.Florence2ForConditionalGeneration; + var __webpack_exports__Florence2PreTrainedModel = __webpack_exports__.Florence2PreTrainedModel; + var __webpack_exports__Florence2Processor = __webpack_exports__.Florence2Processor; + var __webpack_exports__GLPNFeatureExtractor = __webpack_exports__.GLPNFeatureExtractor; + var __webpack_exports__GLPNForDepthEstimation = __webpack_exports__.GLPNForDepthEstimation; + var __webpack_exports__GLPNModel = __webpack_exports__.GLPNModel; + var __webpack_exports__GLPNPreTrainedModel = __webpack_exports__.GLPNPreTrainedModel; + var __webpack_exports__GPT2LMHeadModel = __webpack_exports__.GPT2LMHeadModel; + var __webpack_exports__GPT2Model = __webpack_exports__.GPT2Model; + var __webpack_exports__GPT2PreTrainedModel = __webpack_exports__.GPT2PreTrainedModel; + var __webpack_exports__GPT2Tokenizer = __webpack_exports__.GPT2Tokenizer; + var __webpack_exports__GPTBigCodeForCausalLM = __webpack_exports__.GPTBigCodeForCausalLM; + var __webpack_exports__GPTBigCodeModel = __webpack_exports__.GPTBigCodeModel; + var __webpack_exports__GPTBigCodePreTrainedModel = __webpack_exports__.GPTBigCodePreTrainedModel; + var __webpack_exports__GPTJForCausalLM = __webpack_exports__.GPTJForCausalLM; + var __webpack_exports__GPTJModel = __webpack_exports__.GPTJModel; + var __webpack_exports__GPTJPreTrainedModel = __webpack_exports__.GPTJPreTrainedModel; + var __webpack_exports__GPTNeoForCausalLM = __webpack_exports__.GPTNeoForCausalLM; + var __webpack_exports__GPTNeoModel = __webpack_exports__.GPTNeoModel; + var __webpack_exports__GPTNeoPreTrainedModel = __webpack_exports__.GPTNeoPreTrainedModel; + var __webpack_exports__GPTNeoXForCausalLM = __webpack_exports__.GPTNeoXForCausalLM; + var __webpack_exports__GPTNeoXModel = __webpack_exports__.GPTNeoXModel; + var __webpack_exports__GPTNeoXPreTrainedModel = __webpack_exports__.GPTNeoXPreTrainedModel; + var __webpack_exports__GPTNeoXTokenizer = __webpack_exports__.GPTNeoXTokenizer; + var __webpack_exports__Gemma2ForCausalLM = __webpack_exports__.Gemma2ForCausalLM; + var __webpack_exports__Gemma2Model = __webpack_exports__.Gemma2Model; + var __webpack_exports__Gemma2PreTrainedModel = __webpack_exports__.Gemma2PreTrainedModel; + var __webpack_exports__GemmaForCausalLM = __webpack_exports__.GemmaForCausalLM; + var __webpack_exports__GemmaModel = __webpack_exports__.GemmaModel; + var __webpack_exports__GemmaPreTrainedModel = __webpack_exports__.GemmaPreTrainedModel; + var __webpack_exports__GemmaTokenizer = __webpack_exports__.GemmaTokenizer; + var __webpack_exports__GraniteForCausalLM = __webpack_exports__.GraniteForCausalLM; + var __webpack_exports__GraniteModel = __webpack_exports__.GraniteModel; + var __webpack_exports__GranitePreTrainedModel = __webpack_exports__.GranitePreTrainedModel; + var __webpack_exports__Grok1Tokenizer = __webpack_exports__.Grok1Tokenizer; + var __webpack_exports__GroupViTModel = __webpack_exports__.GroupViTModel; + var __webpack_exports__GroupViTPreTrainedModel = __webpack_exports__.GroupViTPreTrainedModel; + var __webpack_exports__HerbertTokenizer = __webpack_exports__.HerbertTokenizer; + var __webpack_exports__HieraForImageClassification = __webpack_exports__.HieraForImageClassification; + var __webpack_exports__HieraModel = __webpack_exports__.HieraModel; + var __webpack_exports__HieraPreTrainedModel = __webpack_exports__.HieraPreTrainedModel; + var __webpack_exports__HubertForCTC = __webpack_exports__.HubertForCTC; + var __webpack_exports__HubertForSequenceClassification = __webpack_exports__.HubertForSequenceClassification; + var __webpack_exports__HubertModel = __webpack_exports__.HubertModel; + var __webpack_exports__HubertPreTrainedModel = __webpack_exports__.HubertPreTrainedModel; + var __webpack_exports__ImageClassificationPipeline = __webpack_exports__.ImageClassificationPipeline; + var __webpack_exports__ImageFeatureExtractionPipeline = __webpack_exports__.ImageFeatureExtractionPipeline; + var __webpack_exports__ImageFeatureExtractor = __webpack_exports__.ImageFeatureExtractor; + var __webpack_exports__ImageMattingOutput = __webpack_exports__.ImageMattingOutput; + var __webpack_exports__ImageSegmentationPipeline = __webpack_exports__.ImageSegmentationPipeline; + var __webpack_exports__ImageToImagePipeline = __webpack_exports__.ImageToImagePipeline; + var __webpack_exports__ImageToTextPipeline = __webpack_exports__.ImageToTextPipeline; + var __webpack_exports__InterruptableStoppingCriteria = __webpack_exports__.InterruptableStoppingCriteria; + var __webpack_exports__JAISLMHeadModel = __webpack_exports__.JAISLMHeadModel; + var __webpack_exports__JAISModel = __webpack_exports__.JAISModel; + var __webpack_exports__JAISPreTrainedModel = __webpack_exports__.JAISPreTrainedModel; + var __webpack_exports__LlamaForCausalLM = __webpack_exports__.LlamaForCausalLM; + var __webpack_exports__LlamaModel = __webpack_exports__.LlamaModel; + var __webpack_exports__LlamaPreTrainedModel = __webpack_exports__.LlamaPreTrainedModel; + var __webpack_exports__LlamaTokenizer = __webpack_exports__.LlamaTokenizer; + var __webpack_exports__LlavaForConditionalGeneration = __webpack_exports__.LlavaForConditionalGeneration; + var __webpack_exports__LlavaPreTrainedModel = __webpack_exports__.LlavaPreTrainedModel; + var __webpack_exports__LongT5ForConditionalGeneration = __webpack_exports__.LongT5ForConditionalGeneration; + var __webpack_exports__LongT5Model = __webpack_exports__.LongT5Model; + var __webpack_exports__LongT5PreTrainedModel = __webpack_exports__.LongT5PreTrainedModel; + var __webpack_exports__M2M100ForConditionalGeneration = __webpack_exports__.M2M100ForConditionalGeneration; + var __webpack_exports__M2M100Model = __webpack_exports__.M2M100Model; + var __webpack_exports__M2M100PreTrainedModel = __webpack_exports__.M2M100PreTrainedModel; + var __webpack_exports__M2M100Tokenizer = __webpack_exports__.M2M100Tokenizer; + var __webpack_exports__MBart50Tokenizer = __webpack_exports__.MBart50Tokenizer; + var __webpack_exports__MBartForCausalLM = __webpack_exports__.MBartForCausalLM; + var __webpack_exports__MBartForConditionalGeneration = __webpack_exports__.MBartForConditionalGeneration; + var __webpack_exports__MBartForSequenceClassification = __webpack_exports__.MBartForSequenceClassification; + var __webpack_exports__MBartModel = __webpack_exports__.MBartModel; + var __webpack_exports__MBartPreTrainedModel = __webpack_exports__.MBartPreTrainedModel; + var __webpack_exports__MBartTokenizer = __webpack_exports__.MBartTokenizer; + var __webpack_exports__MPNetForMaskedLM = __webpack_exports__.MPNetForMaskedLM; + var __webpack_exports__MPNetForQuestionAnswering = __webpack_exports__.MPNetForQuestionAnswering; + var __webpack_exports__MPNetForSequenceClassification = __webpack_exports__.MPNetForSequenceClassification; + var __webpack_exports__MPNetForTokenClassification = __webpack_exports__.MPNetForTokenClassification; + var __webpack_exports__MPNetModel = __webpack_exports__.MPNetModel; + var __webpack_exports__MPNetPreTrainedModel = __webpack_exports__.MPNetPreTrainedModel; + var __webpack_exports__MPNetTokenizer = __webpack_exports__.MPNetTokenizer; + var __webpack_exports__MT5ForConditionalGeneration = __webpack_exports__.MT5ForConditionalGeneration; + var __webpack_exports__MT5Model = __webpack_exports__.MT5Model; + var __webpack_exports__MT5PreTrainedModel = __webpack_exports__.MT5PreTrainedModel; + var __webpack_exports__MarianMTModel = __webpack_exports__.MarianMTModel; + var __webpack_exports__MarianModel = __webpack_exports__.MarianModel; + var __webpack_exports__MarianPreTrainedModel = __webpack_exports__.MarianPreTrainedModel; + var __webpack_exports__MarianTokenizer = __webpack_exports__.MarianTokenizer; + var __webpack_exports__MaskFormerFeatureExtractor = __webpack_exports__.MaskFormerFeatureExtractor; + var __webpack_exports__MaskFormerForInstanceSegmentation = __webpack_exports__.MaskFormerForInstanceSegmentation; + var __webpack_exports__MaskFormerModel = __webpack_exports__.MaskFormerModel; + var __webpack_exports__MaskFormerPreTrainedModel = __webpack_exports__.MaskFormerPreTrainedModel; + var __webpack_exports__MaskedLMOutput = __webpack_exports__.MaskedLMOutput; + var __webpack_exports__MaxLengthCriteria = __webpack_exports__.MaxLengthCriteria; + var __webpack_exports__MistralForCausalLM = __webpack_exports__.MistralForCausalLM; + var __webpack_exports__MistralModel = __webpack_exports__.MistralModel; + var __webpack_exports__MistralPreTrainedModel = __webpack_exports__.MistralPreTrainedModel; + var __webpack_exports__MobileBertForMaskedLM = __webpack_exports__.MobileBertForMaskedLM; + var __webpack_exports__MobileBertForQuestionAnswering = __webpack_exports__.MobileBertForQuestionAnswering; + var __webpack_exports__MobileBertForSequenceClassification = __webpack_exports__.MobileBertForSequenceClassification; + var __webpack_exports__MobileBertModel = __webpack_exports__.MobileBertModel; + var __webpack_exports__MobileBertPreTrainedModel = __webpack_exports__.MobileBertPreTrainedModel; + var __webpack_exports__MobileBertTokenizer = __webpack_exports__.MobileBertTokenizer; + var __webpack_exports__MobileNetV1FeatureExtractor = __webpack_exports__.MobileNetV1FeatureExtractor; + var __webpack_exports__MobileNetV1ForImageClassification = __webpack_exports__.MobileNetV1ForImageClassification; + var __webpack_exports__MobileNetV1Model = __webpack_exports__.MobileNetV1Model; + var __webpack_exports__MobileNetV1PreTrainedModel = __webpack_exports__.MobileNetV1PreTrainedModel; + var __webpack_exports__MobileNetV2FeatureExtractor = __webpack_exports__.MobileNetV2FeatureExtractor; + var __webpack_exports__MobileNetV2ForImageClassification = __webpack_exports__.MobileNetV2ForImageClassification; + var __webpack_exports__MobileNetV2Model = __webpack_exports__.MobileNetV2Model; + var __webpack_exports__MobileNetV2PreTrainedModel = __webpack_exports__.MobileNetV2PreTrainedModel; + var __webpack_exports__MobileNetV3FeatureExtractor = __webpack_exports__.MobileNetV3FeatureExtractor; + var __webpack_exports__MobileNetV3ForImageClassification = __webpack_exports__.MobileNetV3ForImageClassification; + var __webpack_exports__MobileNetV3Model = __webpack_exports__.MobileNetV3Model; + var __webpack_exports__MobileNetV3PreTrainedModel = __webpack_exports__.MobileNetV3PreTrainedModel; + var __webpack_exports__MobileNetV4FeatureExtractor = __webpack_exports__.MobileNetV4FeatureExtractor; + var __webpack_exports__MobileNetV4ForImageClassification = __webpack_exports__.MobileNetV4ForImageClassification; + var __webpack_exports__MobileNetV4Model = __webpack_exports__.MobileNetV4Model; + var __webpack_exports__MobileNetV4PreTrainedModel = __webpack_exports__.MobileNetV4PreTrainedModel; + var __webpack_exports__MobileViTFeatureExtractor = __webpack_exports__.MobileViTFeatureExtractor; + var __webpack_exports__MobileViTForImageClassification = __webpack_exports__.MobileViTForImageClassification; + var __webpack_exports__MobileViTImageProcessor = __webpack_exports__.MobileViTImageProcessor; + var __webpack_exports__MobileViTModel = __webpack_exports__.MobileViTModel; + var __webpack_exports__MobileViTPreTrainedModel = __webpack_exports__.MobileViTPreTrainedModel; + var __webpack_exports__MobileViTV2ForImageClassification = __webpack_exports__.MobileViTV2ForImageClassification; + var __webpack_exports__MobileViTV2Model = __webpack_exports__.MobileViTV2Model; + var __webpack_exports__MobileViTV2PreTrainedModel = __webpack_exports__.MobileViTV2PreTrainedModel; + var __webpack_exports__ModelOutput = __webpack_exports__.ModelOutput; + var __webpack_exports__Moondream1ForConditionalGeneration = __webpack_exports__.Moondream1ForConditionalGeneration; + var __webpack_exports__MptForCausalLM = __webpack_exports__.MptForCausalLM; + var __webpack_exports__MptModel = __webpack_exports__.MptModel; + var __webpack_exports__MptPreTrainedModel = __webpack_exports__.MptPreTrainedModel; + var __webpack_exports__MusicgenForCausalLM = __webpack_exports__.MusicgenForCausalLM; + var __webpack_exports__MusicgenForConditionalGeneration = __webpack_exports__.MusicgenForConditionalGeneration; + var __webpack_exports__MusicgenModel = __webpack_exports__.MusicgenModel; + var __webpack_exports__MusicgenPreTrainedModel = __webpack_exports__.MusicgenPreTrainedModel; + var __webpack_exports__NllbTokenizer = __webpack_exports__.NllbTokenizer; + var __webpack_exports__NomicBertModel = __webpack_exports__.NomicBertModel; + var __webpack_exports__NomicBertPreTrainedModel = __webpack_exports__.NomicBertPreTrainedModel; + var __webpack_exports__NougatImageProcessor = __webpack_exports__.NougatImageProcessor; + var __webpack_exports__NougatTokenizer = __webpack_exports__.NougatTokenizer; + var __webpack_exports__OPTForCausalLM = __webpack_exports__.OPTForCausalLM; + var __webpack_exports__OPTModel = __webpack_exports__.OPTModel; + var __webpack_exports__OPTPreTrainedModel = __webpack_exports__.OPTPreTrainedModel; + var __webpack_exports__ObjectDetectionPipeline = __webpack_exports__.ObjectDetectionPipeline; + var __webpack_exports__OpenELMForCausalLM = __webpack_exports__.OpenELMForCausalLM; + var __webpack_exports__OpenELMModel = __webpack_exports__.OpenELMModel; + var __webpack_exports__OpenELMPreTrainedModel = __webpack_exports__.OpenELMPreTrainedModel; + var __webpack_exports__OwlViTFeatureExtractor = __webpack_exports__.OwlViTFeatureExtractor; + var __webpack_exports__OwlViTForObjectDetection = __webpack_exports__.OwlViTForObjectDetection; + var __webpack_exports__OwlViTModel = __webpack_exports__.OwlViTModel; + var __webpack_exports__OwlViTPreTrainedModel = __webpack_exports__.OwlViTPreTrainedModel; + var __webpack_exports__OwlViTProcessor = __webpack_exports__.OwlViTProcessor; + var __webpack_exports__Owlv2ForObjectDetection = __webpack_exports__.Owlv2ForObjectDetection; + var __webpack_exports__Owlv2ImageProcessor = __webpack_exports__.Owlv2ImageProcessor; + var __webpack_exports__Owlv2Model = __webpack_exports__.Owlv2Model; + var __webpack_exports__Owlv2PreTrainedModel = __webpack_exports__.Owlv2PreTrainedModel; + var __webpack_exports__Phi3ForCausalLM = __webpack_exports__.Phi3ForCausalLM; + var __webpack_exports__Phi3Model = __webpack_exports__.Phi3Model; + var __webpack_exports__Phi3PreTrainedModel = __webpack_exports__.Phi3PreTrainedModel; + var __webpack_exports__PhiForCausalLM = __webpack_exports__.PhiForCausalLM; + var __webpack_exports__PhiModel = __webpack_exports__.PhiModel; + var __webpack_exports__PhiPreTrainedModel = __webpack_exports__.PhiPreTrainedModel; + var __webpack_exports__Pipeline = __webpack_exports__.Pipeline; + var __webpack_exports__PreTrainedModel = __webpack_exports__.PreTrainedModel; + var __webpack_exports__PreTrainedTokenizer = __webpack_exports__.PreTrainedTokenizer; + var __webpack_exports__PretrainedConfig = __webpack_exports__.PretrainedConfig; + var __webpack_exports__PretrainedMixin = __webpack_exports__.PretrainedMixin; + var __webpack_exports__Processor = __webpack_exports__.Processor; + var __webpack_exports__PvtForImageClassification = __webpack_exports__.PvtForImageClassification; + var __webpack_exports__PvtImageProcessor = __webpack_exports__.PvtImageProcessor; + var __webpack_exports__PvtModel = __webpack_exports__.PvtModel; + var __webpack_exports__PvtPreTrainedModel = __webpack_exports__.PvtPreTrainedModel; + var __webpack_exports__PyAnnoteFeatureExtractor = __webpack_exports__.PyAnnoteFeatureExtractor; + var __webpack_exports__PyAnnoteForAudioFrameClassification = __webpack_exports__.PyAnnoteForAudioFrameClassification; + var __webpack_exports__PyAnnoteModel = __webpack_exports__.PyAnnoteModel; + var __webpack_exports__PyAnnotePreTrainedModel = __webpack_exports__.PyAnnotePreTrainedModel; + var __webpack_exports__PyAnnoteProcessor = __webpack_exports__.PyAnnoteProcessor; + var __webpack_exports__QuestionAnsweringModelOutput = __webpack_exports__.QuestionAnsweringModelOutput; + var __webpack_exports__QuestionAnsweringPipeline = __webpack_exports__.QuestionAnsweringPipeline; + var __webpack_exports__Qwen2ForCausalLM = __webpack_exports__.Qwen2ForCausalLM; + var __webpack_exports__Qwen2Model = __webpack_exports__.Qwen2Model; + var __webpack_exports__Qwen2PreTrainedModel = __webpack_exports__.Qwen2PreTrainedModel; + var __webpack_exports__Qwen2Tokenizer = __webpack_exports__.Qwen2Tokenizer; + var __webpack_exports__RTDetrForObjectDetection = __webpack_exports__.RTDetrForObjectDetection; + var __webpack_exports__RTDetrImageProcessor = __webpack_exports__.RTDetrImageProcessor; + var __webpack_exports__RTDetrModel = __webpack_exports__.RTDetrModel; + var __webpack_exports__RTDetrObjectDetectionOutput = __webpack_exports__.RTDetrObjectDetectionOutput; + var __webpack_exports__RTDetrPreTrainedModel = __webpack_exports__.RTDetrPreTrainedModel; + var __webpack_exports__RawImage = __webpack_exports__.RawImage; + var __webpack_exports__ResNetForImageClassification = __webpack_exports__.ResNetForImageClassification; + var __webpack_exports__ResNetModel = __webpack_exports__.ResNetModel; + var __webpack_exports__ResNetPreTrainedModel = __webpack_exports__.ResNetPreTrainedModel; + var __webpack_exports__RoFormerForMaskedLM = __webpack_exports__.RoFormerForMaskedLM; + var __webpack_exports__RoFormerForQuestionAnswering = __webpack_exports__.RoFormerForQuestionAnswering; + var __webpack_exports__RoFormerForSequenceClassification = __webpack_exports__.RoFormerForSequenceClassification; + var __webpack_exports__RoFormerForTokenClassification = __webpack_exports__.RoFormerForTokenClassification; + var __webpack_exports__RoFormerModel = __webpack_exports__.RoFormerModel; + var __webpack_exports__RoFormerPreTrainedModel = __webpack_exports__.RoFormerPreTrainedModel; + var __webpack_exports__RoFormerTokenizer = __webpack_exports__.RoFormerTokenizer; + var __webpack_exports__RobertaForMaskedLM = __webpack_exports__.RobertaForMaskedLM; + var __webpack_exports__RobertaForQuestionAnswering = __webpack_exports__.RobertaForQuestionAnswering; + var __webpack_exports__RobertaForSequenceClassification = __webpack_exports__.RobertaForSequenceClassification; + var __webpack_exports__RobertaForTokenClassification = __webpack_exports__.RobertaForTokenClassification; + var __webpack_exports__RobertaModel = __webpack_exports__.RobertaModel; + var __webpack_exports__RobertaPreTrainedModel = __webpack_exports__.RobertaPreTrainedModel; + var __webpack_exports__RobertaTokenizer = __webpack_exports__.RobertaTokenizer; + var __webpack_exports__SamImageProcessor = __webpack_exports__.SamImageProcessor; + var __webpack_exports__SamImageSegmentationOutput = __webpack_exports__.SamImageSegmentationOutput; + var __webpack_exports__SamModel = __webpack_exports__.SamModel; + var __webpack_exports__SamPreTrainedModel = __webpack_exports__.SamPreTrainedModel; + var __webpack_exports__SamProcessor = __webpack_exports__.SamProcessor; + var __webpack_exports__SapiensFeatureExtractor = __webpack_exports__.SapiensFeatureExtractor; + var __webpack_exports__SapiensForDepthEstimation = __webpack_exports__.SapiensForDepthEstimation; + var __webpack_exports__SapiensForNormalEstimation = __webpack_exports__.SapiensForNormalEstimation; + var __webpack_exports__SapiensForSemanticSegmentation = __webpack_exports__.SapiensForSemanticSegmentation; + var __webpack_exports__SapiensPreTrainedModel = __webpack_exports__.SapiensPreTrainedModel; + var __webpack_exports__SeamlessM4TFeatureExtractor = __webpack_exports__.SeamlessM4TFeatureExtractor; + var __webpack_exports__SegformerFeatureExtractor = __webpack_exports__.SegformerFeatureExtractor; + var __webpack_exports__SegformerForImageClassification = __webpack_exports__.SegformerForImageClassification; + var __webpack_exports__SegformerForSemanticSegmentation = __webpack_exports__.SegformerForSemanticSegmentation; + var __webpack_exports__SegformerModel = __webpack_exports__.SegformerModel; + var __webpack_exports__SegformerPreTrainedModel = __webpack_exports__.SegformerPreTrainedModel; + var __webpack_exports__Seq2SeqLMOutput = __webpack_exports__.Seq2SeqLMOutput; + var __webpack_exports__SequenceClassifierOutput = __webpack_exports__.SequenceClassifierOutput; + var __webpack_exports__SiglipImageProcessor = __webpack_exports__.SiglipImageProcessor; + var __webpack_exports__SiglipModel = __webpack_exports__.SiglipModel; + var __webpack_exports__SiglipPreTrainedModel = __webpack_exports__.SiglipPreTrainedModel; + var __webpack_exports__SiglipTextModel = __webpack_exports__.SiglipTextModel; + var __webpack_exports__SiglipTokenizer = __webpack_exports__.SiglipTokenizer; + var __webpack_exports__SiglipVisionModel = __webpack_exports__.SiglipVisionModel; + var __webpack_exports__SpeechT5FeatureExtractor = __webpack_exports__.SpeechT5FeatureExtractor; + var __webpack_exports__SpeechT5ForSpeechToText = __webpack_exports__.SpeechT5ForSpeechToText; + var __webpack_exports__SpeechT5ForTextToSpeech = __webpack_exports__.SpeechT5ForTextToSpeech; + var __webpack_exports__SpeechT5HifiGan = __webpack_exports__.SpeechT5HifiGan; + var __webpack_exports__SpeechT5Model = __webpack_exports__.SpeechT5Model; + var __webpack_exports__SpeechT5PreTrainedModel = __webpack_exports__.SpeechT5PreTrainedModel; + var __webpack_exports__SpeechT5Processor = __webpack_exports__.SpeechT5Processor; + var __webpack_exports__SpeechT5Tokenizer = __webpack_exports__.SpeechT5Tokenizer; + var __webpack_exports__SqueezeBertForMaskedLM = __webpack_exports__.SqueezeBertForMaskedLM; + var __webpack_exports__SqueezeBertForQuestionAnswering = __webpack_exports__.SqueezeBertForQuestionAnswering; + var __webpack_exports__SqueezeBertForSequenceClassification = __webpack_exports__.SqueezeBertForSequenceClassification; + var __webpack_exports__SqueezeBertModel = __webpack_exports__.SqueezeBertModel; + var __webpack_exports__SqueezeBertPreTrainedModel = __webpack_exports__.SqueezeBertPreTrainedModel; + var __webpack_exports__SqueezeBertTokenizer = __webpack_exports__.SqueezeBertTokenizer; + var __webpack_exports__StableLmForCausalLM = __webpack_exports__.StableLmForCausalLM; + var __webpack_exports__StableLmModel = __webpack_exports__.StableLmModel; + var __webpack_exports__StableLmPreTrainedModel = __webpack_exports__.StableLmPreTrainedModel; + var __webpack_exports__Starcoder2ForCausalLM = __webpack_exports__.Starcoder2ForCausalLM; + var __webpack_exports__Starcoder2Model = __webpack_exports__.Starcoder2Model; + var __webpack_exports__Starcoder2PreTrainedModel = __webpack_exports__.Starcoder2PreTrainedModel; + var __webpack_exports__StoppingCriteria = __webpack_exports__.StoppingCriteria; + var __webpack_exports__StoppingCriteriaList = __webpack_exports__.StoppingCriteriaList; + var __webpack_exports__SummarizationPipeline = __webpack_exports__.SummarizationPipeline; + var __webpack_exports__Swin2SRForImageSuperResolution = __webpack_exports__.Swin2SRForImageSuperResolution; + var __webpack_exports__Swin2SRImageProcessor = __webpack_exports__.Swin2SRImageProcessor; + var __webpack_exports__Swin2SRModel = __webpack_exports__.Swin2SRModel; + var __webpack_exports__Swin2SRPreTrainedModel = __webpack_exports__.Swin2SRPreTrainedModel; + var __webpack_exports__SwinForImageClassification = __webpack_exports__.SwinForImageClassification; + var __webpack_exports__SwinModel = __webpack_exports__.SwinModel; + var __webpack_exports__SwinPreTrainedModel = __webpack_exports__.SwinPreTrainedModel; + var __webpack_exports__T5ForConditionalGeneration = __webpack_exports__.T5ForConditionalGeneration; + var __webpack_exports__T5Model = __webpack_exports__.T5Model; + var __webpack_exports__T5PreTrainedModel = __webpack_exports__.T5PreTrainedModel; + var __webpack_exports__T5Tokenizer = __webpack_exports__.T5Tokenizer; + var __webpack_exports__TableTransformerForObjectDetection = __webpack_exports__.TableTransformerForObjectDetection; + var __webpack_exports__TableTransformerModel = __webpack_exports__.TableTransformerModel; + var __webpack_exports__TableTransformerObjectDetectionOutput = __webpack_exports__.TableTransformerObjectDetectionOutput; + var __webpack_exports__TableTransformerPreTrainedModel = __webpack_exports__.TableTransformerPreTrainedModel; + var __webpack_exports__Tensor = __webpack_exports__.Tensor; + var __webpack_exports__Text2TextGenerationPipeline = __webpack_exports__.Text2TextGenerationPipeline; + var __webpack_exports__TextClassificationPipeline = __webpack_exports__.TextClassificationPipeline; + var __webpack_exports__TextGenerationPipeline = __webpack_exports__.TextGenerationPipeline; + var __webpack_exports__TextStreamer = __webpack_exports__.TextStreamer; + var __webpack_exports__TextToAudioPipeline = __webpack_exports__.TextToAudioPipeline; + var __webpack_exports__TokenClassificationPipeline = __webpack_exports__.TokenClassificationPipeline; + var __webpack_exports__TokenClassifierOutput = __webpack_exports__.TokenClassifierOutput; + var __webpack_exports__TokenizerModel = __webpack_exports__.TokenizerModel; + var __webpack_exports__TrOCRForCausalLM = __webpack_exports__.TrOCRForCausalLM; + var __webpack_exports__TrOCRPreTrainedModel = __webpack_exports__.TrOCRPreTrainedModel; + var __webpack_exports__TranslationPipeline = __webpack_exports__.TranslationPipeline; + var __webpack_exports__UniSpeechForCTC = __webpack_exports__.UniSpeechForCTC; + var __webpack_exports__UniSpeechForSequenceClassification = __webpack_exports__.UniSpeechForSequenceClassification; + var __webpack_exports__UniSpeechModel = __webpack_exports__.UniSpeechModel; + var __webpack_exports__UniSpeechPreTrainedModel = __webpack_exports__.UniSpeechPreTrainedModel; + var __webpack_exports__UniSpeechSatForAudioFrameClassification = __webpack_exports__.UniSpeechSatForAudioFrameClassification; + var __webpack_exports__UniSpeechSatForCTC = __webpack_exports__.UniSpeechSatForCTC; + var __webpack_exports__UniSpeechSatForSequenceClassification = __webpack_exports__.UniSpeechSatForSequenceClassification; + var __webpack_exports__UniSpeechSatModel = __webpack_exports__.UniSpeechSatModel; + var __webpack_exports__UniSpeechSatPreTrainedModel = __webpack_exports__.UniSpeechSatPreTrainedModel; + var __webpack_exports__ViTFeatureExtractor = __webpack_exports__.ViTFeatureExtractor; + var __webpack_exports__ViTForImageClassification = __webpack_exports__.ViTForImageClassification; + var __webpack_exports__ViTImageProcessor = __webpack_exports__.ViTImageProcessor; + var __webpack_exports__ViTMAEModel = __webpack_exports__.ViTMAEModel; + var __webpack_exports__ViTMAEPreTrainedModel = __webpack_exports__.ViTMAEPreTrainedModel; + var __webpack_exports__ViTMSNForImageClassification = __webpack_exports__.ViTMSNForImageClassification; + var __webpack_exports__ViTMSNModel = __webpack_exports__.ViTMSNModel; + var __webpack_exports__ViTMSNPreTrainedModel = __webpack_exports__.ViTMSNPreTrainedModel; + var __webpack_exports__ViTModel = __webpack_exports__.ViTModel; + var __webpack_exports__ViTPreTrainedModel = __webpack_exports__.ViTPreTrainedModel; + var __webpack_exports__VisionEncoderDecoderModel = __webpack_exports__.VisionEncoderDecoderModel; + var __webpack_exports__VitMatteForImageMatting = __webpack_exports__.VitMatteForImageMatting; + var __webpack_exports__VitMatteImageProcessor = __webpack_exports__.VitMatteImageProcessor; + var __webpack_exports__VitMattePreTrainedModel = __webpack_exports__.VitMattePreTrainedModel; + var __webpack_exports__VitsModel = __webpack_exports__.VitsModel; + var __webpack_exports__VitsModelOutput = __webpack_exports__.VitsModelOutput; + var __webpack_exports__VitsPreTrainedModel = __webpack_exports__.VitsPreTrainedModel; + var __webpack_exports__VitsTokenizer = __webpack_exports__.VitsTokenizer; + var __webpack_exports__Wav2Vec2BertForCTC = __webpack_exports__.Wav2Vec2BertForCTC; + var __webpack_exports__Wav2Vec2BertForSequenceClassification = __webpack_exports__.Wav2Vec2BertForSequenceClassification; + var __webpack_exports__Wav2Vec2BertModel = __webpack_exports__.Wav2Vec2BertModel; + var __webpack_exports__Wav2Vec2BertPreTrainedModel = __webpack_exports__.Wav2Vec2BertPreTrainedModel; + var __webpack_exports__Wav2Vec2CTCTokenizer = __webpack_exports__.Wav2Vec2CTCTokenizer; + var __webpack_exports__Wav2Vec2FeatureExtractor = __webpack_exports__.Wav2Vec2FeatureExtractor; + var __webpack_exports__Wav2Vec2ForAudioFrameClassification = __webpack_exports__.Wav2Vec2ForAudioFrameClassification; + var __webpack_exports__Wav2Vec2ForCTC = __webpack_exports__.Wav2Vec2ForCTC; + var __webpack_exports__Wav2Vec2ForSequenceClassification = __webpack_exports__.Wav2Vec2ForSequenceClassification; + var __webpack_exports__Wav2Vec2Model = __webpack_exports__.Wav2Vec2Model; + var __webpack_exports__Wav2Vec2PreTrainedModel = __webpack_exports__.Wav2Vec2PreTrainedModel; + var __webpack_exports__Wav2Vec2ProcessorWithLM = __webpack_exports__.Wav2Vec2ProcessorWithLM; + var __webpack_exports__WavLMForAudioFrameClassification = __webpack_exports__.WavLMForAudioFrameClassification; + var __webpack_exports__WavLMForCTC = __webpack_exports__.WavLMForCTC; + var __webpack_exports__WavLMForSequenceClassification = __webpack_exports__.WavLMForSequenceClassification; + var __webpack_exports__WavLMForXVector = __webpack_exports__.WavLMForXVector; + var __webpack_exports__WavLMModel = __webpack_exports__.WavLMModel; + var __webpack_exports__WavLMPreTrainedModel = __webpack_exports__.WavLMPreTrainedModel; + var __webpack_exports__WeSpeakerFeatureExtractor = __webpack_exports__.WeSpeakerFeatureExtractor; + var __webpack_exports__WeSpeakerResNetModel = __webpack_exports__.WeSpeakerResNetModel; + var __webpack_exports__WeSpeakerResNetPreTrainedModel = __webpack_exports__.WeSpeakerResNetPreTrainedModel; + var __webpack_exports__WhisperFeatureExtractor = __webpack_exports__.WhisperFeatureExtractor; + var __webpack_exports__WhisperForConditionalGeneration = __webpack_exports__.WhisperForConditionalGeneration; + var __webpack_exports__WhisperModel = __webpack_exports__.WhisperModel; + var __webpack_exports__WhisperPreTrainedModel = __webpack_exports__.WhisperPreTrainedModel; + var __webpack_exports__WhisperProcessor = __webpack_exports__.WhisperProcessor; + var __webpack_exports__WhisperTextStreamer = __webpack_exports__.WhisperTextStreamer; + var __webpack_exports__WhisperTokenizer = __webpack_exports__.WhisperTokenizer; + var __webpack_exports__XLMForQuestionAnswering = __webpack_exports__.XLMForQuestionAnswering; + var __webpack_exports__XLMForSequenceClassification = __webpack_exports__.XLMForSequenceClassification; + var __webpack_exports__XLMForTokenClassification = __webpack_exports__.XLMForTokenClassification; + var __webpack_exports__XLMModel = __webpack_exports__.XLMModel; + var __webpack_exports__XLMPreTrainedModel = __webpack_exports__.XLMPreTrainedModel; + var __webpack_exports__XLMRobertaForMaskedLM = __webpack_exports__.XLMRobertaForMaskedLM; + var __webpack_exports__XLMRobertaForQuestionAnswering = __webpack_exports__.XLMRobertaForQuestionAnswering; + var __webpack_exports__XLMRobertaForSequenceClassification = __webpack_exports__.XLMRobertaForSequenceClassification; + var __webpack_exports__XLMRobertaForTokenClassification = __webpack_exports__.XLMRobertaForTokenClassification; + var __webpack_exports__XLMRobertaModel = __webpack_exports__.XLMRobertaModel; + var __webpack_exports__XLMRobertaPreTrainedModel = __webpack_exports__.XLMRobertaPreTrainedModel; + var __webpack_exports__XLMRobertaTokenizer = __webpack_exports__.XLMRobertaTokenizer; + var __webpack_exports__XLMTokenizer = __webpack_exports__.XLMTokenizer; + var __webpack_exports__XLMWithLMHeadModel = __webpack_exports__.XLMWithLMHeadModel; + var __webpack_exports__XVectorOutput = __webpack_exports__.XVectorOutput; + var __webpack_exports__YolosFeatureExtractor = __webpack_exports__.YolosFeatureExtractor; + var __webpack_exports__YolosForObjectDetection = __webpack_exports__.YolosForObjectDetection; + var __webpack_exports__YolosModel = __webpack_exports__.YolosModel; + var __webpack_exports__YolosObjectDetectionOutput = __webpack_exports__.YolosObjectDetectionOutput; + var __webpack_exports__YolosPreTrainedModel = __webpack_exports__.YolosPreTrainedModel; + var __webpack_exports__ZeroShotAudioClassificationPipeline = __webpack_exports__.ZeroShotAudioClassificationPipeline; + var __webpack_exports__ZeroShotClassificationPipeline = __webpack_exports__.ZeroShotClassificationPipeline; + var __webpack_exports__ZeroShotImageClassificationPipeline = __webpack_exports__.ZeroShotImageClassificationPipeline; + var __webpack_exports__ZeroShotObjectDetectionPipeline = __webpack_exports__.ZeroShotObjectDetectionPipeline; + var __webpack_exports__bankers_round = __webpack_exports__.bankers_round; + var __webpack_exports__cat = __webpack_exports__.cat; + var __webpack_exports__cos_sim = __webpack_exports__.cos_sim; + var __webpack_exports__dot = __webpack_exports__.dot; + var __webpack_exports__dynamic_time_warping = __webpack_exports__.dynamic_time_warping; + var __webpack_exports__env = __webpack_exports__.env; + var __webpack_exports__full = __webpack_exports__.full; + var __webpack_exports__full_like = __webpack_exports__.full_like; + var __webpack_exports__getKeyValueShapes = __webpack_exports__.getKeyValueShapes; + var __webpack_exports__hamming = __webpack_exports__.hamming; + var __webpack_exports__hanning = __webpack_exports__.hanning; + var __webpack_exports__interpolate = __webpack_exports__.interpolate; + var __webpack_exports__interpolate_4d = __webpack_exports__.interpolate_4d; + var __webpack_exports__interpolate_data = __webpack_exports__.interpolate_data; + var __webpack_exports__is_chinese_char = __webpack_exports__.is_chinese_char; + var __webpack_exports__layer_norm = __webpack_exports__.layer_norm; + var __webpack_exports__log_softmax = __webpack_exports__.log_softmax; + var __webpack_exports__magnitude = __webpack_exports__.magnitude; + var __webpack_exports__matmul = __webpack_exports__.matmul; + var __webpack_exports__max = __webpack_exports__.max; + var __webpack_exports__mean = __webpack_exports__.mean; + var __webpack_exports__mean_pooling = __webpack_exports__.mean_pooling; + var __webpack_exports__medianFilter = __webpack_exports__.medianFilter; + var __webpack_exports__mel_filter_bank = __webpack_exports__.mel_filter_bank; + var __webpack_exports__min = __webpack_exports__.min; + var __webpack_exports__ones = __webpack_exports__.ones; + var __webpack_exports__ones_like = __webpack_exports__.ones_like; + var __webpack_exports__permute = __webpack_exports__.permute; + var __webpack_exports__permute_data = __webpack_exports__.permute_data; + var __webpack_exports__pipeline = __webpack_exports__.pipeline; + var __webpack_exports__quantize_embeddings = __webpack_exports__.quantize_embeddings; + var __webpack_exports__read_audio = __webpack_exports__.read_audio; + var __webpack_exports__rfft = __webpack_exports__.rfft; + var __webpack_exports__round = __webpack_exports__.round; + var __webpack_exports__softmax = __webpack_exports__.softmax; + var __webpack_exports__spectrogram = __webpack_exports__.spectrogram; + var __webpack_exports__stack = __webpack_exports__.stack; + var __webpack_exports__std_mean = __webpack_exports__.std_mean; + var __webpack_exports__topk = __webpack_exports__.topk; + var __webpack_exports__window_function = __webpack_exports__.window_function; + var __webpack_exports__zeros = __webpack_exports__.zeros; + var __webpack_exports__zeros_like = __webpack_exports__.zeros_like; + export { + __webpack_exports__ASTFeatureExtractor as ASTFeatureExtractor, + __webpack_exports__ASTForAudioClassification as ASTForAudioClassification, + __webpack_exports__ASTModel as ASTModel, + __webpack_exports__ASTPreTrainedModel as ASTPreTrainedModel, + __webpack_exports__AlbertForMaskedLM as AlbertForMaskedLM, + __webpack_exports__AlbertForQuestionAnswering as AlbertForQuestionAnswering, + __webpack_exports__AlbertForSequenceClassification as AlbertForSequenceClassification, + __webpack_exports__AlbertModel as AlbertModel, + __webpack_exports__AlbertPreTrainedModel as AlbertPreTrainedModel, + __webpack_exports__AlbertTokenizer as AlbertTokenizer, + __webpack_exports__AudioClassificationPipeline as AudioClassificationPipeline, + __webpack_exports__AutoConfig as AutoConfig, + __webpack_exports__AutoModel as AutoModel, + __webpack_exports__AutoModelForAudioClassification as AutoModelForAudioClassification, + __webpack_exports__AutoModelForAudioFrameClassification as AutoModelForAudioFrameClassification, + __webpack_exports__AutoModelForCTC as AutoModelForCTC, + __webpack_exports__AutoModelForCausalLM as AutoModelForCausalLM, + __webpack_exports__AutoModelForDepthEstimation as AutoModelForDepthEstimation, + __webpack_exports__AutoModelForDocumentQuestionAnswering as AutoModelForDocumentQuestionAnswering, + __webpack_exports__AutoModelForImageClassification as AutoModelForImageClassification, + __webpack_exports__AutoModelForImageFeatureExtraction as AutoModelForImageFeatureExtraction, + __webpack_exports__AutoModelForImageMatting as AutoModelForImageMatting, + __webpack_exports__AutoModelForImageSegmentation as AutoModelForImageSegmentation, + __webpack_exports__AutoModelForImageToImage as AutoModelForImageToImage, + __webpack_exports__AutoModelForMaskGeneration as AutoModelForMaskGeneration, + __webpack_exports__AutoModelForMaskedLM as AutoModelForMaskedLM, + __webpack_exports__AutoModelForNormalEstimation as AutoModelForNormalEstimation, + __webpack_exports__AutoModelForObjectDetection as AutoModelForObjectDetection, + __webpack_exports__AutoModelForQuestionAnswering as AutoModelForQuestionAnswering, + __webpack_exports__AutoModelForSemanticSegmentation as AutoModelForSemanticSegmentation, + __webpack_exports__AutoModelForSeq2SeqLM as AutoModelForSeq2SeqLM, + __webpack_exports__AutoModelForSequenceClassification as AutoModelForSequenceClassification, + __webpack_exports__AutoModelForSpeechSeq2Seq as AutoModelForSpeechSeq2Seq, + __webpack_exports__AutoModelForTextToSpectrogram as AutoModelForTextToSpectrogram, + __webpack_exports__AutoModelForTextToWaveform as AutoModelForTextToWaveform, + __webpack_exports__AutoModelForTokenClassification as AutoModelForTokenClassification, + __webpack_exports__AutoModelForUniversalSegmentation as AutoModelForUniversalSegmentation, + __webpack_exports__AutoModelForVision2Seq as AutoModelForVision2Seq, + __webpack_exports__AutoModelForXVector as AutoModelForXVector, + __webpack_exports__AutoModelForZeroShotObjectDetection as AutoModelForZeroShotObjectDetection, + __webpack_exports__AutoProcessor as AutoProcessor, + __webpack_exports__AutoTokenizer as AutoTokenizer, + __webpack_exports__AutomaticSpeechRecognitionPipeline as AutomaticSpeechRecognitionPipeline, + __webpack_exports__BartForConditionalGeneration as BartForConditionalGeneration, + __webpack_exports__BartForSequenceClassification as BartForSequenceClassification, + __webpack_exports__BartModel as BartModel, + __webpack_exports__BartPretrainedModel as BartPretrainedModel, + __webpack_exports__BartTokenizer as BartTokenizer, + __webpack_exports__BaseModelOutput as BaseModelOutput, + __webpack_exports__BaseStreamer as BaseStreamer, + __webpack_exports__BeitFeatureExtractor as BeitFeatureExtractor, + __webpack_exports__BeitForImageClassification as BeitForImageClassification, + __webpack_exports__BeitModel as BeitModel, + __webpack_exports__BeitPreTrainedModel as BeitPreTrainedModel, + __webpack_exports__BertForMaskedLM as BertForMaskedLM, + __webpack_exports__BertForQuestionAnswering as BertForQuestionAnswering, + __webpack_exports__BertForSequenceClassification as BertForSequenceClassification, + __webpack_exports__BertForTokenClassification as BertForTokenClassification, + __webpack_exports__BertModel as BertModel, + __webpack_exports__BertPreTrainedModel as BertPreTrainedModel, + __webpack_exports__BertTokenizer as BertTokenizer, + __webpack_exports__BitImageProcessor as BitImageProcessor, + __webpack_exports__BlenderbotForConditionalGeneration as BlenderbotForConditionalGeneration, + __webpack_exports__BlenderbotModel as BlenderbotModel, + __webpack_exports__BlenderbotPreTrainedModel as BlenderbotPreTrainedModel, + __webpack_exports__BlenderbotSmallForConditionalGeneration as BlenderbotSmallForConditionalGeneration, + __webpack_exports__BlenderbotSmallModel as BlenderbotSmallModel, + __webpack_exports__BlenderbotSmallPreTrainedModel as BlenderbotSmallPreTrainedModel, + __webpack_exports__BlenderbotSmallTokenizer as BlenderbotSmallTokenizer, + __webpack_exports__BlenderbotTokenizer as BlenderbotTokenizer, + __webpack_exports__BloomForCausalLM as BloomForCausalLM, + __webpack_exports__BloomModel as BloomModel, + __webpack_exports__BloomPreTrainedModel as BloomPreTrainedModel, + __webpack_exports__BloomTokenizer as BloomTokenizer, + __webpack_exports__CLIPFeatureExtractor as CLIPFeatureExtractor, + __webpack_exports__CLIPImageProcessor as CLIPImageProcessor, + __webpack_exports__CLIPModel as CLIPModel, + __webpack_exports__CLIPPreTrainedModel as CLIPPreTrainedModel, + __webpack_exports__CLIPSegForImageSegmentation as CLIPSegForImageSegmentation, + __webpack_exports__CLIPSegModel as CLIPSegModel, + __webpack_exports__CLIPSegPreTrainedModel as CLIPSegPreTrainedModel, + __webpack_exports__CLIPTextModel as CLIPTextModel, + __webpack_exports__CLIPTextModelWithProjection as CLIPTextModelWithProjection, + __webpack_exports__CLIPTokenizer as CLIPTokenizer, + __webpack_exports__CLIPVisionModel as CLIPVisionModel, + __webpack_exports__CLIPVisionModelWithProjection as CLIPVisionModelWithProjection, + __webpack_exports__CamembertForMaskedLM as CamembertForMaskedLM, + __webpack_exports__CamembertForQuestionAnswering as CamembertForQuestionAnswering, + __webpack_exports__CamembertForSequenceClassification as CamembertForSequenceClassification, + __webpack_exports__CamembertForTokenClassification as CamembertForTokenClassification, + __webpack_exports__CamembertModel as CamembertModel, + __webpack_exports__CamembertPreTrainedModel as CamembertPreTrainedModel, + __webpack_exports__CamembertTokenizer as CamembertTokenizer, + __webpack_exports__CausalLMOutput as CausalLMOutput, + __webpack_exports__CausalLMOutputWithPast as CausalLMOutputWithPast, + __webpack_exports__ChineseCLIPFeatureExtractor as ChineseCLIPFeatureExtractor, + __webpack_exports__ChineseCLIPModel as ChineseCLIPModel, + __webpack_exports__ChineseCLIPPreTrainedModel as ChineseCLIPPreTrainedModel, + __webpack_exports__ClapAudioModelWithProjection as ClapAudioModelWithProjection, + __webpack_exports__ClapFeatureExtractor as ClapFeatureExtractor, + __webpack_exports__ClapModel as ClapModel, + __webpack_exports__ClapPreTrainedModel as ClapPreTrainedModel, + __webpack_exports__ClapTextModelWithProjection as ClapTextModelWithProjection, + __webpack_exports__CodeGenForCausalLM as CodeGenForCausalLM, + __webpack_exports__CodeGenModel as CodeGenModel, + __webpack_exports__CodeGenPreTrainedModel as CodeGenPreTrainedModel, + __webpack_exports__CodeGenTokenizer as CodeGenTokenizer, + __webpack_exports__CodeLlamaTokenizer as CodeLlamaTokenizer, + __webpack_exports__CohereForCausalLM as CohereForCausalLM, + __webpack_exports__CohereModel as CohereModel, + __webpack_exports__CoherePreTrainedModel as CoherePreTrainedModel, + __webpack_exports__CohereTokenizer as CohereTokenizer, + __webpack_exports__ConvBertForMaskedLM as ConvBertForMaskedLM, + __webpack_exports__ConvBertForQuestionAnswering as ConvBertForQuestionAnswering, + __webpack_exports__ConvBertForSequenceClassification as ConvBertForSequenceClassification, + __webpack_exports__ConvBertForTokenClassification as ConvBertForTokenClassification, + __webpack_exports__ConvBertModel as ConvBertModel, + __webpack_exports__ConvBertPreTrainedModel as ConvBertPreTrainedModel, + __webpack_exports__ConvBertTokenizer as ConvBertTokenizer, + __webpack_exports__ConvNextFeatureExtractor as ConvNextFeatureExtractor, + __webpack_exports__ConvNextForImageClassification as ConvNextForImageClassification, + __webpack_exports__ConvNextImageProcessor as ConvNextImageProcessor, + __webpack_exports__ConvNextModel as ConvNextModel, + __webpack_exports__ConvNextPreTrainedModel as ConvNextPreTrainedModel, + __webpack_exports__ConvNextV2ForImageClassification as ConvNextV2ForImageClassification, + __webpack_exports__ConvNextV2Model as ConvNextV2Model, + __webpack_exports__ConvNextV2PreTrainedModel as ConvNextV2PreTrainedModel, + __webpack_exports__DPTFeatureExtractor as DPTFeatureExtractor, + __webpack_exports__DPTForDepthEstimation as DPTForDepthEstimation, + __webpack_exports__DPTImageProcessor as DPTImageProcessor, + __webpack_exports__DPTModel as DPTModel, + __webpack_exports__DPTPreTrainedModel as DPTPreTrainedModel, + __webpack_exports__DebertaForMaskedLM as DebertaForMaskedLM, + __webpack_exports__DebertaForQuestionAnswering as DebertaForQuestionAnswering, + __webpack_exports__DebertaForSequenceClassification as DebertaForSequenceClassification, + __webpack_exports__DebertaForTokenClassification as DebertaForTokenClassification, + __webpack_exports__DebertaModel as DebertaModel, + __webpack_exports__DebertaPreTrainedModel as DebertaPreTrainedModel, + __webpack_exports__DebertaTokenizer as DebertaTokenizer, + __webpack_exports__DebertaV2ForMaskedLM as DebertaV2ForMaskedLM, + __webpack_exports__DebertaV2ForQuestionAnswering as DebertaV2ForQuestionAnswering, + __webpack_exports__DebertaV2ForSequenceClassification as DebertaV2ForSequenceClassification, + __webpack_exports__DebertaV2ForTokenClassification as DebertaV2ForTokenClassification, + __webpack_exports__DebertaV2Model as DebertaV2Model, + __webpack_exports__DebertaV2PreTrainedModel as DebertaV2PreTrainedModel, + __webpack_exports__DebertaV2Tokenizer as DebertaV2Tokenizer, + __webpack_exports__DecisionTransformerModel as DecisionTransformerModel, + __webpack_exports__DecisionTransformerPreTrainedModel as DecisionTransformerPreTrainedModel, + __webpack_exports__DeiTFeatureExtractor as DeiTFeatureExtractor, + __webpack_exports__DeiTForImageClassification as DeiTForImageClassification, + __webpack_exports__DeiTModel as DeiTModel, + __webpack_exports__DeiTPreTrainedModel as DeiTPreTrainedModel, + __webpack_exports__DepthAnythingForDepthEstimation as DepthAnythingForDepthEstimation, + __webpack_exports__DepthAnythingPreTrainedModel as DepthAnythingPreTrainedModel, + __webpack_exports__DepthEstimationPipeline as DepthEstimationPipeline, + __webpack_exports__DepthProForDepthEstimation as DepthProForDepthEstimation, + __webpack_exports__DepthProPreTrainedModel as DepthProPreTrainedModel, + __webpack_exports__DetrFeatureExtractor as DetrFeatureExtractor, + __webpack_exports__DetrForObjectDetection as DetrForObjectDetection, + __webpack_exports__DetrForSegmentation as DetrForSegmentation, + __webpack_exports__DetrModel as DetrModel, + __webpack_exports__DetrObjectDetectionOutput as DetrObjectDetectionOutput, + __webpack_exports__DetrPreTrainedModel as DetrPreTrainedModel, + __webpack_exports__DetrSegmentationOutput as DetrSegmentationOutput, + __webpack_exports__Dinov2ForImageClassification as Dinov2ForImageClassification, + __webpack_exports__Dinov2Model as Dinov2Model, + __webpack_exports__Dinov2PreTrainedModel as Dinov2PreTrainedModel, + __webpack_exports__DistilBertForMaskedLM as DistilBertForMaskedLM, + __webpack_exports__DistilBertForQuestionAnswering as DistilBertForQuestionAnswering, + __webpack_exports__DistilBertForSequenceClassification as DistilBertForSequenceClassification, + __webpack_exports__DistilBertForTokenClassification as DistilBertForTokenClassification, + __webpack_exports__DistilBertModel as DistilBertModel, + __webpack_exports__DistilBertPreTrainedModel as DistilBertPreTrainedModel, + __webpack_exports__DistilBertTokenizer as DistilBertTokenizer, + __webpack_exports__DocumentQuestionAnsweringPipeline as DocumentQuestionAnsweringPipeline, + __webpack_exports__DonutFeatureExtractor as DonutFeatureExtractor, + __webpack_exports__DonutSwinModel as DonutSwinModel, + __webpack_exports__DonutSwinPreTrainedModel as DonutSwinPreTrainedModel, + __webpack_exports__EfficientNetForImageClassification as EfficientNetForImageClassification, + __webpack_exports__EfficientNetImageProcessor as EfficientNetImageProcessor, + __webpack_exports__EfficientNetModel as EfficientNetModel, + __webpack_exports__EfficientNetPreTrainedModel as EfficientNetPreTrainedModel, + __webpack_exports__ElectraForMaskedLM as ElectraForMaskedLM, + __webpack_exports__ElectraForQuestionAnswering as ElectraForQuestionAnswering, + __webpack_exports__ElectraForSequenceClassification as ElectraForSequenceClassification, + __webpack_exports__ElectraForTokenClassification as ElectraForTokenClassification, + __webpack_exports__ElectraModel as ElectraModel, + __webpack_exports__ElectraPreTrainedModel as ElectraPreTrainedModel, + __webpack_exports__ElectraTokenizer as ElectraTokenizer, + __webpack_exports__EosTokenCriteria as EosTokenCriteria, + __webpack_exports__EsmForMaskedLM as EsmForMaskedLM, + __webpack_exports__EsmForSequenceClassification as EsmForSequenceClassification, + __webpack_exports__EsmForTokenClassification as EsmForTokenClassification, + __webpack_exports__EsmModel as EsmModel, + __webpack_exports__EsmPreTrainedModel as EsmPreTrainedModel, + __webpack_exports__EsmTokenizer as EsmTokenizer, + __webpack_exports__FFT as FFT, + __webpack_exports__FalconForCausalLM as FalconForCausalLM, + __webpack_exports__FalconModel as FalconModel, + __webpack_exports__FalconPreTrainedModel as FalconPreTrainedModel, + __webpack_exports__FalconTokenizer as FalconTokenizer, + __webpack_exports__FastViTForImageClassification as FastViTForImageClassification, + __webpack_exports__FastViTModel as FastViTModel, + __webpack_exports__FastViTPreTrainedModel as FastViTPreTrainedModel, + __webpack_exports__FeatureExtractionPipeline as FeatureExtractionPipeline, + __webpack_exports__FeatureExtractor as FeatureExtractor, + __webpack_exports__FillMaskPipeline as FillMaskPipeline, + __webpack_exports__Florence2ForConditionalGeneration as Florence2ForConditionalGeneration, + __webpack_exports__Florence2PreTrainedModel as Florence2PreTrainedModel, + __webpack_exports__Florence2Processor as Florence2Processor, + __webpack_exports__GLPNFeatureExtractor as GLPNFeatureExtractor, + __webpack_exports__GLPNForDepthEstimation as GLPNForDepthEstimation, + __webpack_exports__GLPNModel as GLPNModel, + __webpack_exports__GLPNPreTrainedModel as GLPNPreTrainedModel, + __webpack_exports__GPT2LMHeadModel as GPT2LMHeadModel, + __webpack_exports__GPT2Model as GPT2Model, + __webpack_exports__GPT2PreTrainedModel as GPT2PreTrainedModel, + __webpack_exports__GPT2Tokenizer as GPT2Tokenizer, + __webpack_exports__GPTBigCodeForCausalLM as GPTBigCodeForCausalLM, + __webpack_exports__GPTBigCodeModel as GPTBigCodeModel, + __webpack_exports__GPTBigCodePreTrainedModel as GPTBigCodePreTrainedModel, + __webpack_exports__GPTJForCausalLM as GPTJForCausalLM, + __webpack_exports__GPTJModel as GPTJModel, + __webpack_exports__GPTJPreTrainedModel as GPTJPreTrainedModel, + __webpack_exports__GPTNeoForCausalLM as GPTNeoForCausalLM, + __webpack_exports__GPTNeoModel as GPTNeoModel, + __webpack_exports__GPTNeoPreTrainedModel as GPTNeoPreTrainedModel, + __webpack_exports__GPTNeoXForCausalLM as GPTNeoXForCausalLM, + __webpack_exports__GPTNeoXModel as GPTNeoXModel, + __webpack_exports__GPTNeoXPreTrainedModel as GPTNeoXPreTrainedModel, + __webpack_exports__GPTNeoXTokenizer as GPTNeoXTokenizer, + __webpack_exports__Gemma2ForCausalLM as Gemma2ForCausalLM, + __webpack_exports__Gemma2Model as Gemma2Model, + __webpack_exports__Gemma2PreTrainedModel as Gemma2PreTrainedModel, + __webpack_exports__GemmaForCausalLM as GemmaForCausalLM, + __webpack_exports__GemmaModel as GemmaModel, + __webpack_exports__GemmaPreTrainedModel as GemmaPreTrainedModel, + __webpack_exports__GemmaTokenizer as GemmaTokenizer, + __webpack_exports__GraniteForCausalLM as GraniteForCausalLM, + __webpack_exports__GraniteModel as GraniteModel, + __webpack_exports__GranitePreTrainedModel as GranitePreTrainedModel, + __webpack_exports__Grok1Tokenizer as Grok1Tokenizer, + __webpack_exports__GroupViTModel as GroupViTModel, + __webpack_exports__GroupViTPreTrainedModel as GroupViTPreTrainedModel, + __webpack_exports__HerbertTokenizer as HerbertTokenizer, + __webpack_exports__HieraForImageClassification as HieraForImageClassification, + __webpack_exports__HieraModel as HieraModel, + __webpack_exports__HieraPreTrainedModel as HieraPreTrainedModel, + __webpack_exports__HubertForCTC as HubertForCTC, + __webpack_exports__HubertForSequenceClassification as HubertForSequenceClassification, + __webpack_exports__HubertModel as HubertModel, + __webpack_exports__HubertPreTrainedModel as HubertPreTrainedModel, + __webpack_exports__ImageClassificationPipeline as ImageClassificationPipeline, + __webpack_exports__ImageFeatureExtractionPipeline as ImageFeatureExtractionPipeline, + __webpack_exports__ImageFeatureExtractor as ImageFeatureExtractor, + __webpack_exports__ImageMattingOutput as ImageMattingOutput, + __webpack_exports__ImageSegmentationPipeline as ImageSegmentationPipeline, + __webpack_exports__ImageToImagePipeline as ImageToImagePipeline, + __webpack_exports__ImageToTextPipeline as ImageToTextPipeline, + __webpack_exports__InterruptableStoppingCriteria as InterruptableStoppingCriteria, + __webpack_exports__JAISLMHeadModel as JAISLMHeadModel, + __webpack_exports__JAISModel as JAISModel, + __webpack_exports__JAISPreTrainedModel as JAISPreTrainedModel, + __webpack_exports__LlamaForCausalLM as LlamaForCausalLM, + __webpack_exports__LlamaModel as LlamaModel, + __webpack_exports__LlamaPreTrainedModel as LlamaPreTrainedModel, + __webpack_exports__LlamaTokenizer as LlamaTokenizer, + __webpack_exports__LlavaForConditionalGeneration as LlavaForConditionalGeneration, + __webpack_exports__LlavaPreTrainedModel as LlavaPreTrainedModel, + __webpack_exports__LongT5ForConditionalGeneration as LongT5ForConditionalGeneration, + __webpack_exports__LongT5Model as LongT5Model, + __webpack_exports__LongT5PreTrainedModel as LongT5PreTrainedModel, + __webpack_exports__M2M100ForConditionalGeneration as M2M100ForConditionalGeneration, + __webpack_exports__M2M100Model as M2M100Model, + __webpack_exports__M2M100PreTrainedModel as M2M100PreTrainedModel, + __webpack_exports__M2M100Tokenizer as M2M100Tokenizer, + __webpack_exports__MBart50Tokenizer as MBart50Tokenizer, + __webpack_exports__MBartForCausalLM as MBartForCausalLM, + __webpack_exports__MBartForConditionalGeneration as MBartForConditionalGeneration, + __webpack_exports__MBartForSequenceClassification as MBartForSequenceClassification, + __webpack_exports__MBartModel as MBartModel, + __webpack_exports__MBartPreTrainedModel as MBartPreTrainedModel, + __webpack_exports__MBartTokenizer as MBartTokenizer, + __webpack_exports__MPNetForMaskedLM as MPNetForMaskedLM, + __webpack_exports__MPNetForQuestionAnswering as MPNetForQuestionAnswering, + __webpack_exports__MPNetForSequenceClassification as MPNetForSequenceClassification, + __webpack_exports__MPNetForTokenClassification as MPNetForTokenClassification, + __webpack_exports__MPNetModel as MPNetModel, + __webpack_exports__MPNetPreTrainedModel as MPNetPreTrainedModel, + __webpack_exports__MPNetTokenizer as MPNetTokenizer, + __webpack_exports__MT5ForConditionalGeneration as MT5ForConditionalGeneration, + __webpack_exports__MT5Model as MT5Model, + __webpack_exports__MT5PreTrainedModel as MT5PreTrainedModel, + __webpack_exports__MarianMTModel as MarianMTModel, + __webpack_exports__MarianModel as MarianModel, + __webpack_exports__MarianPreTrainedModel as MarianPreTrainedModel, + __webpack_exports__MarianTokenizer as MarianTokenizer, + __webpack_exports__MaskFormerFeatureExtractor as MaskFormerFeatureExtractor, + __webpack_exports__MaskFormerForInstanceSegmentation as MaskFormerForInstanceSegmentation, + __webpack_exports__MaskFormerModel as MaskFormerModel, + __webpack_exports__MaskFormerPreTrainedModel as MaskFormerPreTrainedModel, + __webpack_exports__MaskedLMOutput as MaskedLMOutput, + __webpack_exports__MaxLengthCriteria as MaxLengthCriteria, + __webpack_exports__MistralForCausalLM as MistralForCausalLM, + __webpack_exports__MistralModel as MistralModel, + __webpack_exports__MistralPreTrainedModel as MistralPreTrainedModel, + __webpack_exports__MobileBertForMaskedLM as MobileBertForMaskedLM, + __webpack_exports__MobileBertForQuestionAnswering as MobileBertForQuestionAnswering, + __webpack_exports__MobileBertForSequenceClassification as MobileBertForSequenceClassification, + __webpack_exports__MobileBertModel as MobileBertModel, + __webpack_exports__MobileBertPreTrainedModel as MobileBertPreTrainedModel, + __webpack_exports__MobileBertTokenizer as MobileBertTokenizer, + __webpack_exports__MobileNetV1FeatureExtractor as MobileNetV1FeatureExtractor, + __webpack_exports__MobileNetV1ForImageClassification as MobileNetV1ForImageClassification, + __webpack_exports__MobileNetV1Model as MobileNetV1Model, + __webpack_exports__MobileNetV1PreTrainedModel as MobileNetV1PreTrainedModel, + __webpack_exports__MobileNetV2FeatureExtractor as MobileNetV2FeatureExtractor, + __webpack_exports__MobileNetV2ForImageClassification as MobileNetV2ForImageClassification, + __webpack_exports__MobileNetV2Model as MobileNetV2Model, + __webpack_exports__MobileNetV2PreTrainedModel as MobileNetV2PreTrainedModel, + __webpack_exports__MobileNetV3FeatureExtractor as MobileNetV3FeatureExtractor, + __webpack_exports__MobileNetV3ForImageClassification as MobileNetV3ForImageClassification, + __webpack_exports__MobileNetV3Model as MobileNetV3Model, + __webpack_exports__MobileNetV3PreTrainedModel as MobileNetV3PreTrainedModel, + __webpack_exports__MobileNetV4FeatureExtractor as MobileNetV4FeatureExtractor, + __webpack_exports__MobileNetV4ForImageClassification as MobileNetV4ForImageClassification, + __webpack_exports__MobileNetV4Model as MobileNetV4Model, + __webpack_exports__MobileNetV4PreTrainedModel as MobileNetV4PreTrainedModel, + __webpack_exports__MobileViTFeatureExtractor as MobileViTFeatureExtractor, + __webpack_exports__MobileViTForImageClassification as MobileViTForImageClassification, + __webpack_exports__MobileViTImageProcessor as MobileViTImageProcessor, + __webpack_exports__MobileViTModel as MobileViTModel, + __webpack_exports__MobileViTPreTrainedModel as MobileViTPreTrainedModel, + __webpack_exports__MobileViTV2ForImageClassification as MobileViTV2ForImageClassification, + __webpack_exports__MobileViTV2Model as MobileViTV2Model, + __webpack_exports__MobileViTV2PreTrainedModel as MobileViTV2PreTrainedModel, + __webpack_exports__ModelOutput as ModelOutput, + __webpack_exports__Moondream1ForConditionalGeneration as Moondream1ForConditionalGeneration, + __webpack_exports__MptForCausalLM as MptForCausalLM, + __webpack_exports__MptModel as MptModel, + __webpack_exports__MptPreTrainedModel as MptPreTrainedModel, + __webpack_exports__MusicgenForCausalLM as MusicgenForCausalLM, + __webpack_exports__MusicgenForConditionalGeneration as MusicgenForConditionalGeneration, + __webpack_exports__MusicgenModel as MusicgenModel, + __webpack_exports__MusicgenPreTrainedModel as MusicgenPreTrainedModel, + __webpack_exports__NllbTokenizer as NllbTokenizer, + __webpack_exports__NomicBertModel as NomicBertModel, + __webpack_exports__NomicBertPreTrainedModel as NomicBertPreTrainedModel, + __webpack_exports__NougatImageProcessor as NougatImageProcessor, + __webpack_exports__NougatTokenizer as NougatTokenizer, + __webpack_exports__OPTForCausalLM as OPTForCausalLM, + __webpack_exports__OPTModel as OPTModel, + __webpack_exports__OPTPreTrainedModel as OPTPreTrainedModel, + __webpack_exports__ObjectDetectionPipeline as ObjectDetectionPipeline, + __webpack_exports__OpenELMForCausalLM as OpenELMForCausalLM, + __webpack_exports__OpenELMModel as OpenELMModel, + __webpack_exports__OpenELMPreTrainedModel as OpenELMPreTrainedModel, + __webpack_exports__OwlViTFeatureExtractor as OwlViTFeatureExtractor, + __webpack_exports__OwlViTForObjectDetection as OwlViTForObjectDetection, + __webpack_exports__OwlViTModel as OwlViTModel, + __webpack_exports__OwlViTPreTrainedModel as OwlViTPreTrainedModel, + __webpack_exports__OwlViTProcessor as OwlViTProcessor, + __webpack_exports__Owlv2ForObjectDetection as Owlv2ForObjectDetection, + __webpack_exports__Owlv2ImageProcessor as Owlv2ImageProcessor, + __webpack_exports__Owlv2Model as Owlv2Model, + __webpack_exports__Owlv2PreTrainedModel as Owlv2PreTrainedModel, + __webpack_exports__Phi3ForCausalLM as Phi3ForCausalLM, + __webpack_exports__Phi3Model as Phi3Model, + __webpack_exports__Phi3PreTrainedModel as Phi3PreTrainedModel, + __webpack_exports__PhiForCausalLM as PhiForCausalLM, + __webpack_exports__PhiModel as PhiModel, + __webpack_exports__PhiPreTrainedModel as PhiPreTrainedModel, + __webpack_exports__Pipeline as Pipeline, + __webpack_exports__PreTrainedModel as PreTrainedModel, + __webpack_exports__PreTrainedTokenizer as PreTrainedTokenizer, + __webpack_exports__PretrainedConfig as PretrainedConfig, + __webpack_exports__PretrainedMixin as PretrainedMixin, + __webpack_exports__Processor as Processor, + __webpack_exports__PvtForImageClassification as PvtForImageClassification, + __webpack_exports__PvtImageProcessor as PvtImageProcessor, + __webpack_exports__PvtModel as PvtModel, + __webpack_exports__PvtPreTrainedModel as PvtPreTrainedModel, + __webpack_exports__PyAnnoteFeatureExtractor as PyAnnoteFeatureExtractor, + __webpack_exports__PyAnnoteForAudioFrameClassification as PyAnnoteForAudioFrameClassification, + __webpack_exports__PyAnnoteModel as PyAnnoteModel, + __webpack_exports__PyAnnotePreTrainedModel as PyAnnotePreTrainedModel, + __webpack_exports__PyAnnoteProcessor as PyAnnoteProcessor, + __webpack_exports__QuestionAnsweringModelOutput as QuestionAnsweringModelOutput, + __webpack_exports__QuestionAnsweringPipeline as QuestionAnsweringPipeline, + __webpack_exports__Qwen2ForCausalLM as Qwen2ForCausalLM, + __webpack_exports__Qwen2Model as Qwen2Model, + __webpack_exports__Qwen2PreTrainedModel as Qwen2PreTrainedModel, + __webpack_exports__Qwen2Tokenizer as Qwen2Tokenizer, + __webpack_exports__RTDetrForObjectDetection as RTDetrForObjectDetection, + __webpack_exports__RTDetrImageProcessor as RTDetrImageProcessor, + __webpack_exports__RTDetrModel as RTDetrModel, + __webpack_exports__RTDetrObjectDetectionOutput as RTDetrObjectDetectionOutput, + __webpack_exports__RTDetrPreTrainedModel as RTDetrPreTrainedModel, + __webpack_exports__RawImage as RawImage, + __webpack_exports__ResNetForImageClassification as ResNetForImageClassification, + __webpack_exports__ResNetModel as ResNetModel, + __webpack_exports__ResNetPreTrainedModel as ResNetPreTrainedModel, + __webpack_exports__RoFormerForMaskedLM as RoFormerForMaskedLM, + __webpack_exports__RoFormerForQuestionAnswering as RoFormerForQuestionAnswering, + __webpack_exports__RoFormerForSequenceClassification as RoFormerForSequenceClassification, + __webpack_exports__RoFormerForTokenClassification as RoFormerForTokenClassification, + __webpack_exports__RoFormerModel as RoFormerModel, + __webpack_exports__RoFormerPreTrainedModel as RoFormerPreTrainedModel, + __webpack_exports__RoFormerTokenizer as RoFormerTokenizer, + __webpack_exports__RobertaForMaskedLM as RobertaForMaskedLM, + __webpack_exports__RobertaForQuestionAnswering as RobertaForQuestionAnswering, + __webpack_exports__RobertaForSequenceClassification as RobertaForSequenceClassification, + __webpack_exports__RobertaForTokenClassification as RobertaForTokenClassification, + __webpack_exports__RobertaModel as RobertaModel, + __webpack_exports__RobertaPreTrainedModel as RobertaPreTrainedModel, + __webpack_exports__RobertaTokenizer as RobertaTokenizer, + __webpack_exports__SamImageProcessor as SamImageProcessor, + __webpack_exports__SamImageSegmentationOutput as SamImageSegmentationOutput, + __webpack_exports__SamModel as SamModel, + __webpack_exports__SamPreTrainedModel as SamPreTrainedModel, + __webpack_exports__SamProcessor as SamProcessor, + __webpack_exports__SapiensFeatureExtractor as SapiensFeatureExtractor, + __webpack_exports__SapiensForDepthEstimation as SapiensForDepthEstimation, + __webpack_exports__SapiensForNormalEstimation as SapiensForNormalEstimation, + __webpack_exports__SapiensForSemanticSegmentation as SapiensForSemanticSegmentation, + __webpack_exports__SapiensPreTrainedModel as SapiensPreTrainedModel, + __webpack_exports__SeamlessM4TFeatureExtractor as SeamlessM4TFeatureExtractor, + __webpack_exports__SegformerFeatureExtractor as SegformerFeatureExtractor, + __webpack_exports__SegformerForImageClassification as SegformerForImageClassification, + __webpack_exports__SegformerForSemanticSegmentation as SegformerForSemanticSegmentation, + __webpack_exports__SegformerModel as SegformerModel, + __webpack_exports__SegformerPreTrainedModel as SegformerPreTrainedModel, + __webpack_exports__Seq2SeqLMOutput as Seq2SeqLMOutput, + __webpack_exports__SequenceClassifierOutput as SequenceClassifierOutput, + __webpack_exports__SiglipImageProcessor as SiglipImageProcessor, + __webpack_exports__SiglipModel as SiglipModel, + __webpack_exports__SiglipPreTrainedModel as SiglipPreTrainedModel, + __webpack_exports__SiglipTextModel as SiglipTextModel, + __webpack_exports__SiglipTokenizer as SiglipTokenizer, + __webpack_exports__SiglipVisionModel as SiglipVisionModel, + __webpack_exports__SpeechT5FeatureExtractor as SpeechT5FeatureExtractor, + __webpack_exports__SpeechT5ForSpeechToText as SpeechT5ForSpeechToText, + __webpack_exports__SpeechT5ForTextToSpeech as SpeechT5ForTextToSpeech, + __webpack_exports__SpeechT5HifiGan as SpeechT5HifiGan, + __webpack_exports__SpeechT5Model as SpeechT5Model, + __webpack_exports__SpeechT5PreTrainedModel as SpeechT5PreTrainedModel, + __webpack_exports__SpeechT5Processor as SpeechT5Processor, + __webpack_exports__SpeechT5Tokenizer as SpeechT5Tokenizer, + __webpack_exports__SqueezeBertForMaskedLM as SqueezeBertForMaskedLM, + __webpack_exports__SqueezeBertForQuestionAnswering as SqueezeBertForQuestionAnswering, + __webpack_exports__SqueezeBertForSequenceClassification as SqueezeBertForSequenceClassification, + __webpack_exports__SqueezeBertModel as SqueezeBertModel, + __webpack_exports__SqueezeBertPreTrainedModel as SqueezeBertPreTrainedModel, + __webpack_exports__SqueezeBertTokenizer as SqueezeBertTokenizer, + __webpack_exports__StableLmForCausalLM as StableLmForCausalLM, + __webpack_exports__StableLmModel as StableLmModel, + __webpack_exports__StableLmPreTrainedModel as StableLmPreTrainedModel, + __webpack_exports__Starcoder2ForCausalLM as Starcoder2ForCausalLM, + __webpack_exports__Starcoder2Model as Starcoder2Model, + __webpack_exports__Starcoder2PreTrainedModel as Starcoder2PreTrainedModel, + __webpack_exports__StoppingCriteria as StoppingCriteria, + __webpack_exports__StoppingCriteriaList as StoppingCriteriaList, + __webpack_exports__SummarizationPipeline as SummarizationPipeline, + __webpack_exports__Swin2SRForImageSuperResolution as Swin2SRForImageSuperResolution, + __webpack_exports__Swin2SRImageProcessor as Swin2SRImageProcessor, + __webpack_exports__Swin2SRModel as Swin2SRModel, + __webpack_exports__Swin2SRPreTrainedModel as Swin2SRPreTrainedModel, + __webpack_exports__SwinForImageClassification as SwinForImageClassification, + __webpack_exports__SwinModel as SwinModel, + __webpack_exports__SwinPreTrainedModel as SwinPreTrainedModel, + __webpack_exports__T5ForConditionalGeneration as T5ForConditionalGeneration, + __webpack_exports__T5Model as T5Model, + __webpack_exports__T5PreTrainedModel as T5PreTrainedModel, + __webpack_exports__T5Tokenizer as T5Tokenizer, + __webpack_exports__TableTransformerForObjectDetection as TableTransformerForObjectDetection, + __webpack_exports__TableTransformerModel as TableTransformerModel, + __webpack_exports__TableTransformerObjectDetectionOutput as TableTransformerObjectDetectionOutput, + __webpack_exports__TableTransformerPreTrainedModel as TableTransformerPreTrainedModel, + __webpack_exports__Tensor as Tensor, + __webpack_exports__Text2TextGenerationPipeline as Text2TextGenerationPipeline, + __webpack_exports__TextClassificationPipeline as TextClassificationPipeline, + __webpack_exports__TextGenerationPipeline as TextGenerationPipeline, + __webpack_exports__TextStreamer as TextStreamer, + __webpack_exports__TextToAudioPipeline as TextToAudioPipeline, + __webpack_exports__TokenClassificationPipeline as TokenClassificationPipeline, + __webpack_exports__TokenClassifierOutput as TokenClassifierOutput, + __webpack_exports__TokenizerModel as TokenizerModel, + __webpack_exports__TrOCRForCausalLM as TrOCRForCausalLM, + __webpack_exports__TrOCRPreTrainedModel as TrOCRPreTrainedModel, + __webpack_exports__TranslationPipeline as TranslationPipeline, + __webpack_exports__UniSpeechForCTC as UniSpeechForCTC, + __webpack_exports__UniSpeechForSequenceClassification as UniSpeechForSequenceClassification, + __webpack_exports__UniSpeechModel as UniSpeechModel, + __webpack_exports__UniSpeechPreTrainedModel as UniSpeechPreTrainedModel, + __webpack_exports__UniSpeechSatForAudioFrameClassification as UniSpeechSatForAudioFrameClassification, + __webpack_exports__UniSpeechSatForCTC as UniSpeechSatForCTC, + __webpack_exports__UniSpeechSatForSequenceClassification as UniSpeechSatForSequenceClassification, + __webpack_exports__UniSpeechSatModel as UniSpeechSatModel, + __webpack_exports__UniSpeechSatPreTrainedModel as UniSpeechSatPreTrainedModel, + __webpack_exports__ViTFeatureExtractor as ViTFeatureExtractor, + __webpack_exports__ViTForImageClassification as ViTForImageClassification, + __webpack_exports__ViTImageProcessor as ViTImageProcessor, + __webpack_exports__ViTMAEModel as ViTMAEModel, + __webpack_exports__ViTMAEPreTrainedModel as ViTMAEPreTrainedModel, + __webpack_exports__ViTMSNForImageClassification as ViTMSNForImageClassification, + __webpack_exports__ViTMSNModel as ViTMSNModel, + __webpack_exports__ViTMSNPreTrainedModel as ViTMSNPreTrainedModel, + __webpack_exports__ViTModel as ViTModel, + __webpack_exports__ViTPreTrainedModel as ViTPreTrainedModel, + __webpack_exports__VisionEncoderDecoderModel as VisionEncoderDecoderModel, + __webpack_exports__VitMatteForImageMatting as VitMatteForImageMatting, + __webpack_exports__VitMatteImageProcessor as VitMatteImageProcessor, + __webpack_exports__VitMattePreTrainedModel as VitMattePreTrainedModel, + __webpack_exports__VitsModel as VitsModel, + __webpack_exports__VitsModelOutput as VitsModelOutput, + __webpack_exports__VitsPreTrainedModel as VitsPreTrainedModel, + __webpack_exports__VitsTokenizer as VitsTokenizer, + __webpack_exports__Wav2Vec2BertForCTC as Wav2Vec2BertForCTC, + __webpack_exports__Wav2Vec2BertForSequenceClassification as Wav2Vec2BertForSequenceClassification, + __webpack_exports__Wav2Vec2BertModel as Wav2Vec2BertModel, + __webpack_exports__Wav2Vec2BertPreTrainedModel as Wav2Vec2BertPreTrainedModel, + __webpack_exports__Wav2Vec2CTCTokenizer as Wav2Vec2CTCTokenizer, + __webpack_exports__Wav2Vec2FeatureExtractor as Wav2Vec2FeatureExtractor, + __webpack_exports__Wav2Vec2ForAudioFrameClassification as Wav2Vec2ForAudioFrameClassification, + __webpack_exports__Wav2Vec2ForCTC as Wav2Vec2ForCTC, + __webpack_exports__Wav2Vec2ForSequenceClassification as Wav2Vec2ForSequenceClassification, + __webpack_exports__Wav2Vec2Model as Wav2Vec2Model, + __webpack_exports__Wav2Vec2PreTrainedModel as Wav2Vec2PreTrainedModel, + __webpack_exports__Wav2Vec2ProcessorWithLM as Wav2Vec2ProcessorWithLM, + __webpack_exports__WavLMForAudioFrameClassification as WavLMForAudioFrameClassification, + __webpack_exports__WavLMForCTC as WavLMForCTC, + __webpack_exports__WavLMForSequenceClassification as WavLMForSequenceClassification, + __webpack_exports__WavLMForXVector as WavLMForXVector, + __webpack_exports__WavLMModel as WavLMModel, + __webpack_exports__WavLMPreTrainedModel as WavLMPreTrainedModel, + __webpack_exports__WeSpeakerFeatureExtractor as WeSpeakerFeatureExtractor, + __webpack_exports__WeSpeakerResNetModel as WeSpeakerResNetModel, + __webpack_exports__WeSpeakerResNetPreTrainedModel as WeSpeakerResNetPreTrainedModel, + __webpack_exports__WhisperFeatureExtractor as WhisperFeatureExtractor, + __webpack_exports__WhisperForConditionalGeneration as WhisperForConditionalGeneration, + __webpack_exports__WhisperModel as WhisperModel, + __webpack_exports__WhisperPreTrainedModel as WhisperPreTrainedModel, + __webpack_exports__WhisperProcessor as WhisperProcessor, + __webpack_exports__WhisperTextStreamer as WhisperTextStreamer, + __webpack_exports__WhisperTokenizer as WhisperTokenizer, + __webpack_exports__XLMForQuestionAnswering as XLMForQuestionAnswering, + __webpack_exports__XLMForSequenceClassification as XLMForSequenceClassification, + __webpack_exports__XLMForTokenClassification as XLMForTokenClassification, + __webpack_exports__XLMModel as XLMModel, + __webpack_exports__XLMPreTrainedModel as XLMPreTrainedModel, + __webpack_exports__XLMRobertaForMaskedLM as XLMRobertaForMaskedLM, + __webpack_exports__XLMRobertaForQuestionAnswering as XLMRobertaForQuestionAnswering, + __webpack_exports__XLMRobertaForSequenceClassification as XLMRobertaForSequenceClassification, + __webpack_exports__XLMRobertaForTokenClassification as XLMRobertaForTokenClassification, + __webpack_exports__XLMRobertaModel as XLMRobertaModel, + __webpack_exports__XLMRobertaPreTrainedModel as XLMRobertaPreTrainedModel, + __webpack_exports__XLMRobertaTokenizer as XLMRobertaTokenizer, + __webpack_exports__XLMTokenizer as XLMTokenizer, + __webpack_exports__XLMWithLMHeadModel as XLMWithLMHeadModel, + __webpack_exports__XVectorOutput as XVectorOutput, + __webpack_exports__YolosFeatureExtractor as YolosFeatureExtractor, + __webpack_exports__YolosForObjectDetection as YolosForObjectDetection, + __webpack_exports__YolosModel as YolosModel, + __webpack_exports__YolosObjectDetectionOutput as YolosObjectDetectionOutput, + __webpack_exports__YolosPreTrainedModel as YolosPreTrainedModel, + __webpack_exports__ZeroShotAudioClassificationPipeline as ZeroShotAudioClassificationPipeline, + __webpack_exports__ZeroShotClassificationPipeline as ZeroShotClassificationPipeline, + __webpack_exports__ZeroShotImageClassificationPipeline as ZeroShotImageClassificationPipeline, + __webpack_exports__ZeroShotObjectDetectionPipeline as ZeroShotObjectDetectionPipeline, + __webpack_exports__bankers_round as bankers_round, + __webpack_exports__cat as cat, + __webpack_exports__cos_sim as cos_sim, + __webpack_exports__dot as dot, + __webpack_exports__dynamic_time_warping as dynamic_time_warping, + __webpack_exports__env as env, + __webpack_exports__full as full, + __webpack_exports__full_like as full_like, + __webpack_exports__getKeyValueShapes as getKeyValueShapes, + __webpack_exports__hamming as hamming, + __webpack_exports__hanning as hanning, + __webpack_exports__interpolate as interpolate, + __webpack_exports__interpolate_4d as interpolate_4d, + __webpack_exports__interpolate_data as interpolate_data, + __webpack_exports__is_chinese_char as is_chinese_char, + __webpack_exports__layer_norm as layer_norm, + __webpack_exports__log_softmax as log_softmax, + __webpack_exports__magnitude as magnitude, + __webpack_exports__matmul as matmul, + __webpack_exports__max as max, + __webpack_exports__mean as mean, + __webpack_exports__mean_pooling as mean_pooling, + __webpack_exports__medianFilter as medianFilter, + __webpack_exports__mel_filter_bank as mel_filter_bank, + __webpack_exports__min as min, + __webpack_exports__ones as ones, + __webpack_exports__ones_like as ones_like, + __webpack_exports__permute as permute, + __webpack_exports__permute_data as permute_data, + __webpack_exports__pipeline as pipeline, + __webpack_exports__quantize_embeddings as quantize_embeddings, + __webpack_exports__read_audio as read_audio, + __webpack_exports__rfft as rfft, + __webpack_exports__round as round, + __webpack_exports__softmax as softmax, + __webpack_exports__spectrogram as spectrogram, + __webpack_exports__stack as stack, + __webpack_exports__std_mean as std_mean, + __webpack_exports__topk as topk, + __webpack_exports__window_function as window_function, + __webpack_exports__zeros as zeros, + __webpack_exports__zeros_like as zeros_like + }; + /*! Bundled license information: + + @huggingface/transformers/dist/transformers.js: + (*! + * ONNX Runtime Web v1.20.0-dev.20241016-2b8fc5529b + * Copyright (c) Microsoft Corporation. All rights reserved. + * Licensed under the MIT License. + *) + (** + * @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. + * ============================================================================= + *) + (*!*****************************!*\ + !*** ./src/transformers.js ***! + \*****************************) + */ + //# sourceMappingURL=@huggingface_transformers.js.map + \ No newline at end of file diff --git a/yarn.lock b/yarn.lock index 2b9586f84..1118d412a 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1830,15 +1830,6 @@ __metadata: languageName: node linkType: hard -"@emnapi/runtime@npm:^1.2.0": - version: 1.3.1 - resolution: "@emnapi/runtime@npm:1.3.1" - dependencies: - tslib: ^2.4.0 - checksum: 9a16ae7905a9c0e8956cf1854ef74e5087fbf36739abdba7aa6b308485aafdc993da07c19d7af104cd5f8e425121120852851bb3a0f78e2160e420a36d47f42f - languageName: node - linkType: hard - "@eslint-community/eslint-utils@npm:^4.2.0, @eslint-community/eslint-utils@npm:^4.4.0": version: 4.4.0 resolution: "@eslint-community/eslint-utils@npm:4.4.0" @@ -1897,25 +1888,6 @@ __metadata: languageName: node linkType: hard -"@huggingface/jinja@npm:^0.3.2": - version: 0.3.2 - resolution: "@huggingface/jinja@npm:0.3.2" - checksum: 4bc7d00b6f8655a0032c2d89e38a095d0a87ef81a1c12fb6fd0404e1319e1ef6eef87734502689c1df39db4e77a7bb5996e7b6c1b4d6a768ecfa5a48f2a939a7 - languageName: node - linkType: hard - -"@huggingface/transformers@npm:^3.1.2": - version: 3.1.2 - resolution: "@huggingface/transformers@npm:3.1.2" - dependencies: - "@huggingface/jinja": ^0.3.2 - onnxruntime-node: 1.20.1 - onnxruntime-web: 1.21.0-dev.20241205-d27fecd3d3 - sharp: ^0.33.5 - checksum: f87517d7e77a46a61610336120df35e6091e0d67ef549f1eab39501bbe27aeff95769b042536c558de768479ee1d5a253d628e964ad22c2fa70d206dfea15d92 - languageName: node - linkType: hard - "@humanwhocodes/config-array@npm:^0.11.14": version: 0.11.14 resolution: "@humanwhocodes/config-array@npm:0.11.14" @@ -1941,181 +1913,6 @@ __metadata: languageName: node linkType: hard -"@img/sharp-darwin-arm64@npm:0.33.5": - version: 0.33.5 - resolution: "@img/sharp-darwin-arm64@npm:0.33.5" - dependencies: - "@img/sharp-libvips-darwin-arm64": 1.0.4 - dependenciesMeta: - "@img/sharp-libvips-darwin-arm64": - optional: true - conditions: os=darwin & cpu=arm64 - languageName: node - linkType: hard - -"@img/sharp-darwin-x64@npm:0.33.5": - version: 0.33.5 - resolution: "@img/sharp-darwin-x64@npm:0.33.5" - dependencies: - "@img/sharp-libvips-darwin-x64": 1.0.4 - dependenciesMeta: - "@img/sharp-libvips-darwin-x64": - optional: true - conditions: os=darwin & cpu=x64 - languageName: node - linkType: hard - -"@img/sharp-libvips-darwin-arm64@npm:1.0.4": - version: 1.0.4 - resolution: "@img/sharp-libvips-darwin-arm64@npm:1.0.4" - conditions: os=darwin & cpu=arm64 - languageName: node - linkType: hard - -"@img/sharp-libvips-darwin-x64@npm:1.0.4": - version: 1.0.4 - resolution: "@img/sharp-libvips-darwin-x64@npm:1.0.4" - conditions: os=darwin & cpu=x64 - languageName: node - linkType: hard - -"@img/sharp-libvips-linux-arm64@npm:1.0.4": - version: 1.0.4 - resolution: "@img/sharp-libvips-linux-arm64@npm:1.0.4" - conditions: os=linux & cpu=arm64 & libc=glibc - languageName: node - linkType: hard - -"@img/sharp-libvips-linux-arm@npm:1.0.5": - version: 1.0.5 - resolution: "@img/sharp-libvips-linux-arm@npm:1.0.5" - conditions: os=linux & cpu=arm & libc=glibc - languageName: node - linkType: hard - -"@img/sharp-libvips-linux-s390x@npm:1.0.4": - version: 1.0.4 - resolution: "@img/sharp-libvips-linux-s390x@npm:1.0.4" - conditions: os=linux & cpu=s390x & libc=glibc - languageName: node - linkType: hard - -"@img/sharp-libvips-linux-x64@npm:1.0.4": - version: 1.0.4 - resolution: "@img/sharp-libvips-linux-x64@npm:1.0.4" - conditions: os=linux & cpu=x64 & libc=glibc - languageName: node - linkType: hard - -"@img/sharp-libvips-linuxmusl-arm64@npm:1.0.4": - version: 1.0.4 - resolution: "@img/sharp-libvips-linuxmusl-arm64@npm:1.0.4" - conditions: os=linux & cpu=arm64 & libc=musl - languageName: node - linkType: hard - -"@img/sharp-libvips-linuxmusl-x64@npm:1.0.4": - version: 1.0.4 - resolution: "@img/sharp-libvips-linuxmusl-x64@npm:1.0.4" - conditions: os=linux & cpu=x64 & libc=musl - languageName: node - linkType: hard - -"@img/sharp-linux-arm64@npm:0.33.5": - version: 0.33.5 - resolution: "@img/sharp-linux-arm64@npm:0.33.5" - dependencies: - "@img/sharp-libvips-linux-arm64": 1.0.4 - dependenciesMeta: - "@img/sharp-libvips-linux-arm64": - optional: true - conditions: os=linux & cpu=arm64 & libc=glibc - languageName: node - linkType: hard - -"@img/sharp-linux-arm@npm:0.33.5": - version: 0.33.5 - resolution: "@img/sharp-linux-arm@npm:0.33.5" - dependencies: - "@img/sharp-libvips-linux-arm": 1.0.5 - dependenciesMeta: - "@img/sharp-libvips-linux-arm": - optional: true - conditions: os=linux & cpu=arm & libc=glibc - languageName: node - linkType: hard - -"@img/sharp-linux-s390x@npm:0.33.5": - version: 0.33.5 - resolution: "@img/sharp-linux-s390x@npm:0.33.5" - dependencies: - "@img/sharp-libvips-linux-s390x": 1.0.4 - dependenciesMeta: - "@img/sharp-libvips-linux-s390x": - optional: true - conditions: os=linux & cpu=s390x & libc=glibc - languageName: node - linkType: hard - -"@img/sharp-linux-x64@npm:0.33.5": - version: 0.33.5 - resolution: "@img/sharp-linux-x64@npm:0.33.5" - dependencies: - "@img/sharp-libvips-linux-x64": 1.0.4 - dependenciesMeta: - "@img/sharp-libvips-linux-x64": - optional: true - conditions: os=linux & cpu=x64 & libc=glibc - languageName: node - linkType: hard - -"@img/sharp-linuxmusl-arm64@npm:0.33.5": - version: 0.33.5 - resolution: "@img/sharp-linuxmusl-arm64@npm:0.33.5" - dependencies: - "@img/sharp-libvips-linuxmusl-arm64": 1.0.4 - dependenciesMeta: - "@img/sharp-libvips-linuxmusl-arm64": - optional: true - conditions: os=linux & cpu=arm64 & libc=musl - languageName: node - linkType: hard - -"@img/sharp-linuxmusl-x64@npm:0.33.5": - version: 0.33.5 - resolution: "@img/sharp-linuxmusl-x64@npm:0.33.5" - dependencies: - "@img/sharp-libvips-linuxmusl-x64": 1.0.4 - dependenciesMeta: - "@img/sharp-libvips-linuxmusl-x64": - optional: true - conditions: os=linux & cpu=x64 & libc=musl - languageName: node - linkType: hard - -"@img/sharp-wasm32@npm:0.33.5": - version: 0.33.5 - resolution: "@img/sharp-wasm32@npm:0.33.5" - dependencies: - "@emnapi/runtime": ^1.2.0 - conditions: cpu=wasm32 - languageName: node - linkType: hard - -"@img/sharp-win32-ia32@npm:0.33.5": - version: 0.33.5 - resolution: "@img/sharp-win32-ia32@npm:0.33.5" - conditions: os=win32 & cpu=ia32 - languageName: node - linkType: hard - -"@img/sharp-win32-x64@npm:0.33.5": - version: 0.33.5 - resolution: "@img/sharp-win32-x64@npm:0.33.5" - conditions: os=win32 & cpu=x64 - languageName: node - linkType: hard - "@isaacs/cliui@npm:^8.0.2": version: 8.0.2 resolution: "@isaacs/cliui@npm:8.0.2" @@ -2130,15 +1927,6 @@ __metadata: languageName: node linkType: hard -"@isaacs/fs-minipass@npm:^4.0.0": - version: 4.0.1 - resolution: "@isaacs/fs-minipass@npm:4.0.1" - dependencies: - minipass: ^7.0.4 - checksum: 5d36d289960e886484362d9eb6a51d1ea28baed5f5d0140bbe62b99bac52eaf06cc01c2bc0d3575977962f84f6b2c4387b043ee632216643d4787b0999465bf2 - languageName: node - linkType: hard - "@isaacs/ttlcache@npm:^1.4.1": version: 1.4.1 resolution: "@isaacs/ttlcache@npm:1.4.1" @@ -2552,79 +2340,6 @@ __metadata: languageName: node linkType: hard -"@protobufjs/aspromise@npm:^1.1.1, @protobufjs/aspromise@npm:^1.1.2": - version: 1.1.2 - resolution: "@protobufjs/aspromise@npm:1.1.2" - checksum: 011fe7ef0826b0fd1a95935a033a3c0fd08483903e1aa8f8b4e0704e3233406abb9ee25350ec0c20bbecb2aad8da0dcea58b392bbd77d6690736f02c143865d2 - languageName: node - linkType: hard - -"@protobufjs/base64@npm:^1.1.2": - version: 1.1.2 - resolution: "@protobufjs/base64@npm:1.1.2" - checksum: 67173ac34de1e242c55da52c2f5bdc65505d82453893f9b51dc74af9fe4c065cf4a657a4538e91b0d4a1a1e0a0642215e31894c31650ff6e3831471061e1ee9e - languageName: node - linkType: hard - -"@protobufjs/codegen@npm:^2.0.4": - version: 2.0.4 - resolution: "@protobufjs/codegen@npm:2.0.4" - checksum: 59240c850b1d3d0b56d8f8098dd04787dcaec5c5bd8de186fa548de86b86076e1c50e80144b90335e705a044edf5bc8b0998548474c2a10a98c7e004a1547e4b - languageName: node - linkType: hard - -"@protobufjs/eventemitter@npm:^1.1.0": - version: 1.1.0 - resolution: "@protobufjs/eventemitter@npm:1.1.0" - checksum: 0369163a3d226851682f855f81413cbf166cd98f131edb94a0f67f79e75342d86e89df9d7a1df08ac28be2bc77e0a7f0200526bb6c2a407abbfee1f0262d5fd7 - languageName: node - linkType: hard - -"@protobufjs/fetch@npm:^1.1.0": - version: 1.1.0 - resolution: "@protobufjs/fetch@npm:1.1.0" - dependencies: - "@protobufjs/aspromise": ^1.1.1 - "@protobufjs/inquire": ^1.1.0 - checksum: 3fce7e09eb3f1171dd55a192066450f65324fd5f7cc01a431df01bb00d0a895e6bfb5b0c5561ce157ee1d886349c90703d10a4e11a1a256418ff591b969b3477 - languageName: node - linkType: hard - -"@protobufjs/float@npm:^1.0.2": - version: 1.0.2 - resolution: "@protobufjs/float@npm:1.0.2" - checksum: 5781e1241270b8bd1591d324ca9e3a3128d2f768077a446187a049e36505e91bc4156ed5ac3159c3ce3d2ba3743dbc757b051b2d723eea9cd367bfd54ab29b2f - languageName: node - linkType: hard - -"@protobufjs/inquire@npm:^1.1.0": - version: 1.1.0 - resolution: "@protobufjs/inquire@npm:1.1.0" - checksum: ca06f02eaf65ca36fb7498fc3492b7fc087bfcc85c702bac5b86fad34b692bdce4990e0ef444c1e2aea8c034227bd1f0484be02810d5d7e931c55445555646f4 - languageName: node - linkType: hard - -"@protobufjs/path@npm:^1.1.2": - version: 1.1.2 - resolution: "@protobufjs/path@npm:1.1.2" - checksum: 856eeb532b16a7aac071cacde5c5620df800db4c80cee6dbc56380524736205aae21e5ae47739114bf669ab5e8ba0e767a282ad894f3b5e124197cb9224445ee - languageName: node - linkType: hard - -"@protobufjs/pool@npm:^1.1.0": - version: 1.1.0 - resolution: "@protobufjs/pool@npm:1.1.0" - checksum: d6a34fbbd24f729e2a10ee915b74e1d77d52214de626b921b2d77288bd8f2386808da2315080f2905761527cceffe7ec34c7647bd21a5ae41a25e8212ff79451 - languageName: node - linkType: hard - -"@protobufjs/utf8@npm:^1.1.0": - version: 1.1.0 - resolution: "@protobufjs/utf8@npm:1.1.0" - checksum: f9bf3163d13aaa3b6f5e6fbf37a116e094ea021c0e1f2a7ccd0e12a29e2ce08dafba4e8b36e13f8ed7397e1591610ce880ed1289af4d66cf4ace8a36a9557278 - languageName: node - linkType: hard - "@puppeteer/browsers@npm:2.3.0": version: 2.3.0 resolution: "@puppeteer/browsers@npm:2.3.0" @@ -3928,15 +3643,6 @@ __metadata: languageName: node linkType: hard -"@types/node@npm:>=13.7.0": - version: 22.10.2 - resolution: "@types/node@npm:22.10.2" - dependencies: - undici-types: ~6.20.0 - checksum: b22401e6e7d1484e437d802c72f5560e18100b1257b9ad0574d6fe05bebe4dbcb620ea68627d1f1406775070d29ace8b6b51f57e7b1c7b8bafafe6da7f29c843 - languageName: node - linkType: hard - "@types/node@npm:^18.0.0": version: 18.19.47 resolution: "@types/node@npm:18.19.47" @@ -4622,7 +4328,6 @@ __metadata: "@babel/preset-env": ^7.20.0 "@babel/runtime": ^7.20.0 "@callstack/react-native-visionos": ^0.74.0 - "@huggingface/transformers": ^3.1.2 "@react-native/babel-preset": 0.74.84 "@react-native/eslint-config": 0.74.84 "@react-native/metro-config": 0.74.84 @@ -5498,13 +5203,6 @@ __metadata: languageName: node linkType: hard -"chownr@npm:^3.0.0": - version: 3.0.0 - resolution: "chownr@npm:3.0.0" - checksum: fd73a4bab48b79e66903fe1cafbdc208956f41ea4f856df883d0c7277b7ab29fd33ee65f93b2ec9192fc0169238f2f8307b7735d27c155821d886b84aa97aa8d - languageName: node - linkType: hard - "chrome-launcher@npm:^0.15.2": version: 0.15.2 resolution: "chrome-launcher@npm:0.15.2" @@ -6154,13 +5852,6 @@ __metadata: languageName: node linkType: hard -"detect-libc@npm:^2.0.3": - version: 2.0.3 - resolution: "detect-libc@npm:2.0.3" - checksum: 2ba6a939ae55f189aea996ac67afceb650413c7a34726ee92c40fb0deb2400d57ef94631a8a3f052055eea7efb0f99a9b5e6ce923415daa3e68221f963cfc27d - languageName: node - linkType: hard - "detect-newline@npm:^3.0.0": version: 3.1.0 resolution: "detect-newline@npm:3.1.0" @@ -7210,13 +6901,6 @@ __metadata: languageName: node linkType: hard -"flatbuffers@npm:^1.12.0": - version: 1.12.0 - resolution: "flatbuffers@npm:1.12.0" - checksum: 8a6461ec80a8f850c623439fbc3d031bac52dfd7dee27fbadf1d850e96fd92cbd782c28bf2a08f2d852a3ac329cc31e2ad21e133ab68993cf0df69d3dd32fd12 - languageName: node - linkType: hard - "flatted@npm:^3.2.9": version: 3.3.1 resolution: "flatted@npm:3.3.1" @@ -7577,13 +7261,6 @@ __metadata: languageName: node linkType: hard -"guid-typescript@npm:^1.0.9": - version: 1.0.9 - resolution: "guid-typescript@npm:1.0.9" - checksum: 829dd87866800a5138aafa0873994028bbc446eb20ff4cae6452d471a2a3d26f7025bed3eb980692c0f022fd22f95ea7396122b46b45a4b5084958505a4fc50c - languageName: node - linkType: hard - "hard-rejection@npm:^2.1.0": version: 2.1.0 resolution: "hard-rejection@npm:2.1.0" @@ -9254,13 +8931,6 @@ __metadata: languageName: node linkType: hard -"long@npm:^5.0.0, long@npm:^5.2.3": - version: 5.2.3 - resolution: "long@npm:5.2.3" - checksum: 885ede7c3de4facccbd2cacc6168bae3a02c3e836159ea4252c87b6e34d40af819824b2d4edce330bfb5c4d6e8ce3ec5864bdcf9473fa1f53a4f8225860e5897 - languageName: node - linkType: hard - "loose-envify@npm:^1.0.0, loose-envify@npm:^1.1.0, loose-envify@npm:^1.4.0": version: 1.4.0 resolution: "loose-envify@npm:1.4.0" @@ -9838,7 +9508,7 @@ __metadata: languageName: node linkType: hard -"minipass@npm:^5.0.0 || ^6.0.2 || ^7.0.0, minipass@npm:^7.0.2, minipass@npm:^7.0.3, minipass@npm:^7.0.4, minipass@npm:^7.1.2": +"minipass@npm:^5.0.0 || ^6.0.2 || ^7.0.0, minipass@npm:^7.0.2, minipass@npm:^7.0.3, minipass@npm:^7.1.2": version: 7.1.2 resolution: "minipass@npm:7.1.2" checksum: 2bfd325b95c555f2b4d2814d49325691c7bee937d753814861b0b49d5edcda55cbbf22b6b6a60bb91eddac8668771f03c5ff647dcd9d0f798e9548b9cdc46ee3 @@ -9855,16 +9525,6 @@ __metadata: languageName: node linkType: hard -"minizlib@npm:^3.0.1": - version: 3.0.1 - resolution: "minizlib@npm:3.0.1" - dependencies: - minipass: ^7.0.4 - rimraf: ^5.0.5 - checksum: da0a53899252380475240c587e52c824f8998d9720982ba5c4693c68e89230718884a209858c156c6e08d51aad35700a3589987e540593c36f6713fe30cd7338 - languageName: node - linkType: hard - "mitt@npm:3.0.1": version: 3.0.1 resolution: "mitt@npm:3.0.1" @@ -10216,45 +9876,6 @@ __metadata: languageName: node linkType: hard -"onnxruntime-common@npm:1.20.1": - version: 1.20.1 - resolution: "onnxruntime-common@npm:1.20.1" - checksum: 5cde8fae546c9a4a2d8f13e18cc4c346d77e733d08d1f6b95f4958fb09618592113d232db64049fafadbd18913ec8564e6c06c47dadc4c2aac8df4ed18b2956c - languageName: node - linkType: hard - -"onnxruntime-common@npm:1.21.0-dev.20241205-6ed77cc374": - version: 1.21.0-dev.20241205-6ed77cc374 - resolution: "onnxruntime-common@npm:1.21.0-dev.20241205-6ed77cc374" - checksum: f490d6b1a8c059ce5665a468ac1c38de4c3729ead0bae173a0c9334c32a67fb2899972b6e185cc6c42f05e61f2c3da2738a814dbc89b5577206a7b17e29f4190 - languageName: node - linkType: hard - -"onnxruntime-node@npm:1.20.1": - version: 1.20.1 - resolution: "onnxruntime-node@npm:1.20.1" - dependencies: - onnxruntime-common: 1.20.1 - tar: ^7.0.1 - checksum: 6b5467eb1d08e1f5931ed1bff77e180f8600be917b690bad5edcfad61fcb797d29f74c5cff5eeb1f8bc95a36d261647d68ca88e149b0aa88412d8dea90901042 - conditions: (os=win32 | os=darwin | os=linux) - languageName: node - linkType: hard - -"onnxruntime-web@npm:1.21.0-dev.20241205-d27fecd3d3": - version: 1.21.0-dev.20241205-d27fecd3d3 - resolution: "onnxruntime-web@npm:1.21.0-dev.20241205-d27fecd3d3" - dependencies: - flatbuffers: ^1.12.0 - guid-typescript: ^1.0.9 - long: ^5.2.3 - onnxruntime-common: 1.21.0-dev.20241205-6ed77cc374 - platform: ^1.3.6 - protobufjs: ^7.2.4 - checksum: f668b638440dc8122209ce04c9e06b449bd2d7d0ce05be0d0618468d98746310e4a4d1a15afea30c86e98cea0053496d1c0fef5e6785153f16be8530f24018b8 - languageName: node - linkType: hard - "open@npm:^6.2.0": version: 6.4.0 resolution: "open@npm:6.4.0" @@ -10569,13 +10190,6 @@ __metadata: languageName: node linkType: hard -"platform@npm:^1.3.6": - version: 1.3.6 - resolution: "platform@npm:1.3.6" - checksum: 6f472a09c61d418c7e26c1c16d0bdc029549d512dbec6526216a1e59ec68100d07007d0097dcba69dddad883d6f2a83361b4bdfe0094a3d9a2af24158643d85e - languageName: node - linkType: hard - "pngjs@npm:^6.0.0": version: 6.0.0 resolution: "pngjs@npm:6.0.0" @@ -10724,26 +10338,6 @@ __metadata: languageName: node linkType: hard -"protobufjs@npm:^7.2.4": - version: 7.4.0 - resolution: "protobufjs@npm:7.4.0" - dependencies: - "@protobufjs/aspromise": ^1.1.2 - "@protobufjs/base64": ^1.1.2 - "@protobufjs/codegen": ^2.0.4 - "@protobufjs/eventemitter": ^1.1.0 - "@protobufjs/fetch": ^1.1.0 - "@protobufjs/float": ^1.0.2 - "@protobufjs/inquire": ^1.1.0 - "@protobufjs/path": ^1.1.2 - "@protobufjs/pool": ^1.1.0 - "@protobufjs/utf8": ^1.1.0 - "@types/node": ">=13.7.0" - long: ^5.0.0 - checksum: ba0e6b60541bbf818bb148e90f5eb68bd99004e29a6034ad9895a381cbd352be8dce5376e47ae21b2e05559f2505b4a5f4a3c8fa62402822c6ab4dcdfb89ffb3 - languageName: node - linkType: hard - "proxy-agent@npm:^6.4.0": version: 6.4.0 resolution: "proxy-agent@npm:6.4.0" @@ -11556,7 +11150,7 @@ __metadata: languageName: node linkType: hard -"rimraf@npm:^5.0.5, rimraf@npm:^5.0.7": +"rimraf@npm:^5.0.7": version: 5.0.10 resolution: "rimraf@npm:5.0.10" dependencies: @@ -11791,75 +11385,6 @@ __metadata: languageName: node linkType: hard -"sharp@npm:^0.33.5": - version: 0.33.5 - resolution: "sharp@npm:0.33.5" - dependencies: - "@img/sharp-darwin-arm64": 0.33.5 - "@img/sharp-darwin-x64": 0.33.5 - "@img/sharp-libvips-darwin-arm64": 1.0.4 - "@img/sharp-libvips-darwin-x64": 1.0.4 - "@img/sharp-libvips-linux-arm": 1.0.5 - "@img/sharp-libvips-linux-arm64": 1.0.4 - "@img/sharp-libvips-linux-s390x": 1.0.4 - "@img/sharp-libvips-linux-x64": 1.0.4 - "@img/sharp-libvips-linuxmusl-arm64": 1.0.4 - "@img/sharp-libvips-linuxmusl-x64": 1.0.4 - "@img/sharp-linux-arm": 0.33.5 - "@img/sharp-linux-arm64": 0.33.5 - "@img/sharp-linux-s390x": 0.33.5 - "@img/sharp-linux-x64": 0.33.5 - "@img/sharp-linuxmusl-arm64": 0.33.5 - "@img/sharp-linuxmusl-x64": 0.33.5 - "@img/sharp-wasm32": 0.33.5 - "@img/sharp-win32-ia32": 0.33.5 - "@img/sharp-win32-x64": 0.33.5 - color: ^4.2.3 - detect-libc: ^2.0.3 - semver: ^7.6.3 - dependenciesMeta: - "@img/sharp-darwin-arm64": - optional: true - "@img/sharp-darwin-x64": - optional: true - "@img/sharp-libvips-darwin-arm64": - optional: true - "@img/sharp-libvips-darwin-x64": - optional: true - "@img/sharp-libvips-linux-arm": - optional: true - "@img/sharp-libvips-linux-arm64": - optional: true - "@img/sharp-libvips-linux-s390x": - optional: true - "@img/sharp-libvips-linux-x64": - optional: true - "@img/sharp-libvips-linuxmusl-arm64": - optional: true - "@img/sharp-libvips-linuxmusl-x64": - optional: true - "@img/sharp-linux-arm": - optional: true - "@img/sharp-linux-arm64": - optional: true - "@img/sharp-linux-s390x": - optional: true - "@img/sharp-linux-x64": - optional: true - "@img/sharp-linuxmusl-arm64": - optional: true - "@img/sharp-linuxmusl-x64": - optional: true - "@img/sharp-wasm32": - optional: true - "@img/sharp-win32-ia32": - optional: true - "@img/sharp-win32-x64": - optional: true - checksum: 04beae89910ac65c5f145f88de162e8466bec67705f497ace128de849c24d168993e016f33a343a1f3c30b25d2a90c3e62b017a9a0d25452371556f6cd2471e4 - languageName: node - linkType: hard - "shebang-command@npm:^2.0.0": version: 2.0.0 resolution: "shebang-command@npm:2.0.0" @@ -12438,20 +11963,6 @@ __metadata: languageName: node linkType: hard -"tar@npm:^7.0.1": - version: 7.4.3 - resolution: "tar@npm:7.4.3" - dependencies: - "@isaacs/fs-minipass": ^4.0.0 - chownr: ^3.0.0 - minipass: ^7.1.2 - minizlib: ^3.0.1 - mkdirp: ^3.0.1 - yallist: ^5.0.0 - checksum: 8485350c0688331c94493031f417df069b778aadb25598abdad51862e007c39d1dd5310702c7be4a6784731a174799d8885d2fde0484269aea205b724d7b2ffa - languageName: node - linkType: hard - "teapot@npm:^1.0.0": version: 1.0.0 resolution: "teapot@npm:1.0.0" @@ -12978,13 +12489,6 @@ __metadata: languageName: node linkType: hard -"undici-types@npm:~6.20.0": - version: 6.20.0 - resolution: "undici-types@npm:6.20.0" - checksum: b7bc50f012dc6afbcce56c9fd62d7e86b20a62ff21f12b7b5cbf1973b9578d90f22a9c7fe50e638e96905d33893bf2f9f16d98929c4673c2480de05c6c96ea8b - languageName: node - linkType: hard - "unicode-canonical-property-names-ecmascript@npm:^2.0.0": version: 2.0.0 resolution: "unicode-canonical-property-names-ecmascript@npm:2.0.0" @@ -13444,13 +12948,6 @@ __metadata: languageName: node linkType: hard -"yallist@npm:^5.0.0": - version: 5.0.0 - resolution: "yallist@npm:5.0.0" - checksum: eba51182400b9f35b017daa7f419f434424410691bbc5de4f4240cc830fdef906b504424992700dc047f16b4d99100a6f8b8b11175c193f38008e9c96322b6a5 - languageName: node - linkType: hard - "yaml@npm:^1.10.0": version: 1.10.2 resolution: "yaml@npm:1.10.2" From e3947301acbb3c257dd93423c8535b7f7f2f1d41 Mon Sep 17 00:00:00 2001 From: William Candillon Date: Thu, 12 Dec 2024 16:54:02 +0100 Subject: [PATCH 13/35] :wrench: --- apps/example/ios/Podfile | 2 +- apps/example/ios/Podfile.lock | 226 +++++++---------- apps/example/package.json | 1 + .../src/TransformerJS/TransformerJS.tsx | 3 +- .../example/src/TransformerJS/transformers.js | 8 +- .../webgpu/cpp/rnwgpu/api/GPUAdapterInfo.h | 18 +- packages/webgpu/cpp/rnwgpu/api/GPUDevice.h | 5 + yarn.lock | 231 +++++++++++++++++- 8 files changed, 333 insertions(+), 161 deletions(-) diff --git a/apps/example/ios/Podfile b/apps/example/ios/Podfile index 24eedffca..3a4a7b64c 100644 --- a/apps/example/ios/Podfile +++ b/apps/example/ios/Podfile @@ -7,7 +7,7 @@ require "#{ws_dir}/node_modules/react-native-test-app/test_app.rb" workspace 'Example.xcworkspace' options = { - :hermes_enabled => true + :hermes_enabled => false } use_test_app! options diff --git a/apps/example/ios/Podfile.lock b/apps/example/ios/Podfile.lock index e29fb8c7c..445be89bc 100644 --- a/apps/example/ios/Podfile.lock +++ b/apps/example/ios/Podfile.lock @@ -4,9 +4,6 @@ PODS: - FBLazyVector (0.74.2) - fmt (9.1.0) - glog (0.3.5) - - hermes-engine (0.74.2): - - hermes-engine/Pre-built (= 0.74.2) - - hermes-engine/Pre-built (0.74.2) - RCT-Folly (2024.01.01.00): - boost - DoubleConversion @@ -46,7 +43,6 @@ PODS: - React-Codegen (0.74.2): - DoubleConversion - glog - - hermes-engine - RCT-Folly - RCTRequired - RCTTypeSafety @@ -56,6 +52,7 @@ PODS: - React-FabricImage - React-featureflags - React-graphics + - React-jsc - React-jsi - React-jsiexecutor - React-NativeModulesApple @@ -65,13 +62,12 @@ PODS: - ReactCommon/turbomodule/core - React-Core (0.74.2): - glog - - hermes-engine - RCT-Folly (= 2024.01.01.00) - RCTDeprecation - React-Core/Default (= 0.74.2) - React-cxxreact - React-featureflags - - React-hermes + - React-jsc - React-jsi - React-jsiexecutor - React-jsinspector @@ -82,13 +78,12 @@ PODS: - Yoga - React-Core/CoreModulesHeaders (0.74.2): - glog - - hermes-engine - RCT-Folly (= 2024.01.01.00) - RCTDeprecation - React-Core/Default - React-cxxreact - React-featureflags - - React-hermes + - React-jsc - React-jsi - React-jsiexecutor - React-jsinspector @@ -99,12 +94,11 @@ PODS: - Yoga - React-Core/Default (0.74.2): - glog - - hermes-engine - RCT-Folly (= 2024.01.01.00) - RCTDeprecation - React-cxxreact - React-featureflags - - React-hermes + - React-jsc - React-jsi - React-jsiexecutor - React-jsinspector @@ -115,14 +109,13 @@ PODS: - Yoga - React-Core/DevSupport (0.74.2): - glog - - hermes-engine - RCT-Folly (= 2024.01.01.00) - RCTDeprecation - React-Core/Default (= 0.74.2) - React-Core/RCTWebSocket (= 0.74.2) - React-cxxreact - React-featureflags - - React-hermes + - React-jsc - React-jsi - React-jsiexecutor - React-jsinspector @@ -133,13 +126,12 @@ PODS: - Yoga - React-Core/RCTActionSheetHeaders (0.74.2): - glog - - hermes-engine - RCT-Folly (= 2024.01.01.00) - RCTDeprecation - React-Core/Default - React-cxxreact - React-featureflags - - React-hermes + - React-jsc - React-jsi - React-jsiexecutor - React-jsinspector @@ -150,13 +142,12 @@ PODS: - Yoga - React-Core/RCTAnimationHeaders (0.74.2): - glog - - hermes-engine - RCT-Folly (= 2024.01.01.00) - RCTDeprecation - React-Core/Default - React-cxxreact - React-featureflags - - React-hermes + - React-jsc - React-jsi - React-jsiexecutor - React-jsinspector @@ -167,13 +158,12 @@ PODS: - Yoga - React-Core/RCTBlobHeaders (0.74.2): - glog - - hermes-engine - RCT-Folly (= 2024.01.01.00) - RCTDeprecation - React-Core/Default - React-cxxreact - React-featureflags - - React-hermes + - React-jsc - React-jsi - React-jsiexecutor - React-jsinspector @@ -184,13 +174,12 @@ PODS: - Yoga - React-Core/RCTImageHeaders (0.74.2): - glog - - hermes-engine - RCT-Folly (= 2024.01.01.00) - RCTDeprecation - React-Core/Default - React-cxxreact - React-featureflags - - React-hermes + - React-jsc - React-jsi - React-jsiexecutor - React-jsinspector @@ -201,13 +190,12 @@ PODS: - Yoga - React-Core/RCTLinkingHeaders (0.74.2): - glog - - hermes-engine - RCT-Folly (= 2024.01.01.00) - RCTDeprecation - React-Core/Default - React-cxxreact - React-featureflags - - React-hermes + - React-jsc - React-jsi - React-jsiexecutor - React-jsinspector @@ -218,13 +206,12 @@ PODS: - Yoga - React-Core/RCTNetworkHeaders (0.74.2): - glog - - hermes-engine - RCT-Folly (= 2024.01.01.00) - RCTDeprecation - React-Core/Default - React-cxxreact - React-featureflags - - React-hermes + - React-jsc - React-jsi - React-jsiexecutor - React-jsinspector @@ -235,13 +222,12 @@ PODS: - Yoga - React-Core/RCTSettingsHeaders (0.74.2): - glog - - hermes-engine - RCT-Folly (= 2024.01.01.00) - RCTDeprecation - React-Core/Default - React-cxxreact - React-featureflags - - React-hermes + - React-jsc - React-jsi - React-jsiexecutor - React-jsinspector @@ -252,13 +238,12 @@ PODS: - Yoga - React-Core/RCTTextHeaders (0.74.2): - glog - - hermes-engine - RCT-Folly (= 2024.01.01.00) - RCTDeprecation - React-Core/Default - React-cxxreact - React-featureflags - - React-hermes + - React-jsc - React-jsi - React-jsiexecutor - React-jsinspector @@ -269,13 +254,12 @@ PODS: - Yoga - React-Core/RCTVibrationHeaders (0.74.2): - glog - - hermes-engine - RCT-Folly (= 2024.01.01.00) - RCTDeprecation - React-Core/Default - React-cxxreact - React-featureflags - - React-hermes + - React-jsc - React-jsi - React-jsiexecutor - React-jsinspector @@ -286,13 +270,12 @@ PODS: - Yoga - React-Core/RCTWebSocket (0.74.2): - glog - - hermes-engine - RCT-Folly (= 2024.01.01.00) - RCTDeprecation - React-Core/Default (= 0.74.2) - React-cxxreact - React-featureflags - - React-hermes + - React-jsc - React-jsi - React-jsiexecutor - React-jsinspector @@ -320,7 +303,6 @@ PODS: - DoubleConversion - fmt (= 9.1.0) - glog - - hermes-engine - RCT-Folly (= 2024.01.01.00) - React-callinvoker (= 0.74.2) - React-debug (= 0.74.2) @@ -334,7 +316,6 @@ PODS: - DoubleConversion - fmt (= 9.1.0) - glog - - hermes-engine - RCT-Folly/Fabric (= 2024.01.01.00) - RCTRequired - RCTTypeSafety @@ -356,6 +337,7 @@ PODS: - React-Fabric/textlayoutmanager (= 0.74.2) - React-Fabric/uimanager (= 0.74.2) - React-graphics + - React-jsc - React-jsi - React-jsiexecutor - React-logger @@ -367,7 +349,6 @@ PODS: - DoubleConversion - fmt (= 9.1.0) - glog - - hermes-engine - RCT-Folly/Fabric (= 2024.01.01.00) - RCTRequired - RCTTypeSafety @@ -375,6 +356,7 @@ PODS: - React-cxxreact - React-debug - React-graphics + - React-jsc - React-jsi - React-jsiexecutor - React-logger @@ -386,7 +368,6 @@ PODS: - DoubleConversion - fmt (= 9.1.0) - glog - - hermes-engine - RCT-Folly/Fabric (= 2024.01.01.00) - RCTRequired - RCTTypeSafety @@ -394,6 +375,7 @@ PODS: - React-cxxreact - React-debug - React-graphics + - React-jsc - React-jsi - React-jsiexecutor - React-logger @@ -405,7 +387,6 @@ PODS: - DoubleConversion - fmt (= 9.1.0) - glog - - hermes-engine - RCT-Folly/Fabric (= 2024.01.01.00) - RCTRequired - RCTTypeSafety @@ -413,6 +394,7 @@ PODS: - React-cxxreact - React-debug - React-graphics + - React-jsc - React-jsi - React-jsiexecutor - React-logger @@ -424,7 +406,6 @@ PODS: - DoubleConversion - fmt (= 9.1.0) - glog - - hermes-engine - RCT-Folly/Fabric (= 2024.01.01.00) - RCTRequired - RCTTypeSafety @@ -432,6 +413,7 @@ PODS: - React-cxxreact - React-debug - React-graphics + - React-jsc - React-jsi - React-jsiexecutor - React-logger @@ -443,7 +425,6 @@ PODS: - DoubleConversion - fmt (= 9.1.0) - glog - - hermes-engine - RCT-Folly/Fabric (= 2024.01.01.00) - RCTRequired - RCTTypeSafety @@ -462,6 +443,7 @@ PODS: - React-Fabric/components/unimplementedview (= 0.74.2) - React-Fabric/components/view (= 0.74.2) - React-graphics + - React-jsc - React-jsi - React-jsiexecutor - React-logger @@ -473,7 +455,6 @@ PODS: - DoubleConversion - fmt (= 9.1.0) - glog - - hermes-engine - RCT-Folly/Fabric (= 2024.01.01.00) - RCTRequired - RCTTypeSafety @@ -481,6 +462,7 @@ PODS: - React-cxxreact - React-debug - React-graphics + - React-jsc - React-jsi - React-jsiexecutor - React-logger @@ -492,7 +474,6 @@ PODS: - DoubleConversion - fmt (= 9.1.0) - glog - - hermes-engine - RCT-Folly/Fabric (= 2024.01.01.00) - RCTRequired - RCTTypeSafety @@ -500,6 +481,7 @@ PODS: - React-cxxreact - React-debug - React-graphics + - React-jsc - React-jsi - React-jsiexecutor - React-logger @@ -511,7 +493,6 @@ PODS: - DoubleConversion - fmt (= 9.1.0) - glog - - hermes-engine - RCT-Folly/Fabric (= 2024.01.01.00) - RCTRequired - RCTTypeSafety @@ -519,6 +500,7 @@ PODS: - React-cxxreact - React-debug - React-graphics + - React-jsc - React-jsi - React-jsiexecutor - React-logger @@ -530,7 +512,6 @@ PODS: - DoubleConversion - fmt (= 9.1.0) - glog - - hermes-engine - RCT-Folly/Fabric (= 2024.01.01.00) - RCTRequired - RCTTypeSafety @@ -538,6 +519,7 @@ PODS: - React-cxxreact - React-debug - React-graphics + - React-jsc - React-jsi - React-jsiexecutor - React-logger @@ -549,7 +531,6 @@ PODS: - DoubleConversion - fmt (= 9.1.0) - glog - - hermes-engine - RCT-Folly/Fabric (= 2024.01.01.00) - RCTRequired - RCTTypeSafety @@ -557,6 +538,7 @@ PODS: - React-cxxreact - React-debug - React-graphics + - React-jsc - React-jsi - React-jsiexecutor - React-logger @@ -568,7 +550,6 @@ PODS: - DoubleConversion - fmt (= 9.1.0) - glog - - hermes-engine - RCT-Folly/Fabric (= 2024.01.01.00) - RCTRequired - RCTTypeSafety @@ -576,6 +557,7 @@ PODS: - React-cxxreact - React-debug - React-graphics + - React-jsc - React-jsi - React-jsiexecutor - React-logger @@ -587,7 +569,6 @@ PODS: - DoubleConversion - fmt (= 9.1.0) - glog - - hermes-engine - RCT-Folly/Fabric (= 2024.01.01.00) - RCTRequired - RCTTypeSafety @@ -595,6 +576,7 @@ PODS: - React-cxxreact - React-debug - React-graphics + - React-jsc - React-jsi - React-jsiexecutor - React-logger @@ -606,7 +588,6 @@ PODS: - DoubleConversion - fmt (= 9.1.0) - glog - - hermes-engine - RCT-Folly/Fabric (= 2024.01.01.00) - RCTRequired - RCTTypeSafety @@ -614,6 +595,7 @@ PODS: - React-cxxreact - React-debug - React-graphics + - React-jsc - React-jsi - React-jsiexecutor - React-logger @@ -625,7 +607,6 @@ PODS: - DoubleConversion - fmt (= 9.1.0) - glog - - hermes-engine - RCT-Folly/Fabric (= 2024.01.01.00) - RCTRequired - RCTTypeSafety @@ -633,6 +614,7 @@ PODS: - React-cxxreact - React-debug - React-graphics + - React-jsc - React-jsi - React-jsiexecutor - React-logger @@ -644,7 +626,6 @@ PODS: - DoubleConversion - fmt (= 9.1.0) - glog - - hermes-engine - RCT-Folly/Fabric (= 2024.01.01.00) - RCTRequired - RCTTypeSafety @@ -652,6 +633,7 @@ PODS: - React-cxxreact - React-debug - React-graphics + - React-jsc - React-jsi - React-jsiexecutor - React-logger @@ -663,7 +645,6 @@ PODS: - DoubleConversion - fmt (= 9.1.0) - glog - - hermes-engine - RCT-Folly/Fabric (= 2024.01.01.00) - RCTRequired - RCTTypeSafety @@ -671,6 +652,7 @@ PODS: - React-cxxreact - React-debug - React-graphics + - React-jsc - React-jsi - React-jsiexecutor - React-logger @@ -683,7 +665,6 @@ PODS: - DoubleConversion - fmt (= 9.1.0) - glog - - hermes-engine - RCT-Folly/Fabric (= 2024.01.01.00) - RCTRequired - RCTTypeSafety @@ -691,6 +672,7 @@ PODS: - React-cxxreact - React-debug - React-graphics + - React-jsc - React-jsi - React-jsiexecutor - React-logger @@ -702,7 +684,6 @@ PODS: - DoubleConversion - fmt (= 9.1.0) - glog - - hermes-engine - RCT-Folly/Fabric (= 2024.01.01.00) - RCTRequired - RCTTypeSafety @@ -710,6 +691,7 @@ PODS: - React-cxxreact - React-debug - React-graphics + - React-jsc - React-jsi - React-jsiexecutor - React-logger @@ -721,7 +703,6 @@ PODS: - DoubleConversion - fmt (= 9.1.0) - glog - - hermes-engine - RCT-Folly/Fabric (= 2024.01.01.00) - RCTRequired - RCTTypeSafety @@ -729,6 +710,7 @@ PODS: - React-cxxreact - React-debug - React-graphics + - React-jsc - React-jsi - React-jsiexecutor - React-logger @@ -740,7 +722,6 @@ PODS: - DoubleConversion - fmt (= 9.1.0) - glog - - hermes-engine - RCT-Folly/Fabric (= 2024.01.01.00) - RCTRequired - RCTTypeSafety @@ -748,6 +729,7 @@ PODS: - React-cxxreact - React-debug - React-graphics + - React-jsc - React-jsi - React-jsiexecutor - React-logger @@ -759,7 +741,6 @@ PODS: - DoubleConversion - fmt (= 9.1.0) - glog - - hermes-engine - RCT-Folly/Fabric (= 2024.01.01.00) - RCTRequired - RCTTypeSafety @@ -767,6 +748,7 @@ PODS: - React-cxxreact - React-debug - React-graphics + - React-jsc - React-jsi - React-jsiexecutor - React-logger @@ -778,7 +760,6 @@ PODS: - DoubleConversion - fmt (= 9.1.0) - glog - - hermes-engine - RCT-Folly/Fabric (= 2024.01.01.00) - RCTRequired - RCTTypeSafety @@ -786,6 +767,7 @@ PODS: - React-cxxreact - React-debug - React-graphics + - React-jsc - React-jsi - React-jsiexecutor - React-logger @@ -797,7 +779,6 @@ PODS: - DoubleConversion - fmt (= 9.1.0) - glog - - hermes-engine - RCT-Folly/Fabric (= 2024.01.01.00) - RCTRequired - RCTTypeSafety @@ -805,6 +786,7 @@ PODS: - React-cxxreact - React-debug - React-graphics + - React-jsc - React-jsi - React-jsiexecutor - React-logger @@ -816,7 +798,6 @@ PODS: - DoubleConversion - fmt (= 9.1.0) - glog - - hermes-engine - RCT-Folly/Fabric (= 2024.01.01.00) - RCTRequired - RCTTypeSafety @@ -825,6 +806,7 @@ PODS: - React-debug - React-Fabric/uimanager - React-graphics + - React-jsc - React-jsi - React-jsiexecutor - React-logger @@ -836,7 +818,6 @@ PODS: - DoubleConversion - fmt (= 9.1.0) - glog - - hermes-engine - RCT-Folly/Fabric (= 2024.01.01.00) - RCTRequired - RCTTypeSafety @@ -844,6 +825,7 @@ PODS: - React-cxxreact - React-debug - React-graphics + - React-jsc - React-jsi - React-jsiexecutor - React-logger @@ -855,13 +837,13 @@ PODS: - DoubleConversion - fmt (= 9.1.0) - glog - - hermes-engine - RCT-Folly/Fabric (= 2024.01.01.00) - RCTRequired (= 0.74.2) - RCTTypeSafety (= 0.74.2) - React-Fabric - React-graphics - React-ImageManager + - React-jsc - React-jsi - React-jsiexecutor (= 0.74.2) - React-logger @@ -877,18 +859,6 @@ PODS: - RCT-Folly/Fabric (= 2024.01.01.00) - React-Core/Default (= 0.74.2) - React-utils - - React-hermes (0.74.2): - - DoubleConversion - - fmt (= 9.1.0) - - glog - - hermes-engine - - RCT-Folly (= 2024.01.01.00) - - React-cxxreact (= 0.74.2) - - React-jsi - - React-jsiexecutor (= 0.74.2) - - React-jsinspector - - React-perflogger (= 0.74.2) - - React-runtimeexecutor - React-ImageManager (0.74.2): - glog - RCT-Folly/Fabric @@ -898,6 +868,11 @@ PODS: - React-graphics - React-rendererdebug - React-utils + - React-jsc (0.74.2): + - React-jsc/Fabric (= 0.74.2) + - React-jsi (= 0.74.2) + - React-jsc/Fabric (0.74.2): + - React-jsi (= 0.74.2) - React-jserrorhandler (0.74.2): - RCT-Folly/Fabric (= 2024.01.01.00) - React-debug @@ -908,13 +883,11 @@ PODS: - DoubleConversion - fmt (= 9.1.0) - glog - - hermes-engine - RCT-Folly (= 2024.01.01.00) - React-jsiexecutor (0.74.2): - DoubleConversion - fmt (= 9.1.0) - glog - - hermes-engine - RCT-Folly (= 2024.01.01.00) - React-cxxreact (= 0.74.2) - React-jsi (= 0.74.2) @@ -923,7 +896,6 @@ PODS: - React-jsinspector (0.74.2): - DoubleConversion - glog - - hermes-engine - RCT-Folly (= 2024.01.01.00) - React-featureflags - React-jsi @@ -940,7 +912,6 @@ PODS: - react-native-wgpu (0.1.20): - DoubleConversion - glog - - hermes-engine - RCT-Folly (= 2024.01.01.00) - RCTRequired - RCTTypeSafety @@ -951,6 +922,7 @@ PODS: - React-featureflags - React-graphics - React-ImageManager + - React-jsi - React-NativeModulesApple - React-RCTFabric - React-rendererdebug @@ -961,10 +933,10 @@ PODS: - React-nativeconfig (0.74.2) - React-NativeModulesApple (0.74.2): - glog - - hermes-engine - React-callinvoker - React-Core - React-cxxreact + - React-jsc - React-jsi - React-jsinspector - React-runtimeexecutor @@ -992,7 +964,7 @@ PODS: - React-Fabric - React-featureflags - React-graphics - - React-hermes + - React-jsc - React-nativeconfig - React-NativeModulesApple - React-RCTFabric @@ -1001,14 +973,12 @@ PODS: - React-rendererdebug - React-RuntimeApple - React-RuntimeCore - - React-RuntimeHermes - React-runtimescheduler - React-utils - ReactCommon - React-RCTBlob (0.74.2): - DoubleConversion - fmt (= 9.1.0) - - hermes-engine - RCT-Folly (= 2024.01.01.00) - React-Codegen - React-Core/RCTBlobHeaders @@ -1020,7 +990,6 @@ PODS: - ReactCommon - React-RCTFabric (0.74.2): - glog - - hermes-engine - RCT-Folly/Fabric (= 2024.01.01.00) - React-Core - React-debug @@ -1029,6 +998,7 @@ PODS: - React-featureflags - React-graphics - React-ImageManager + - React-jsc - React-jsi - React-jsinspector - React-nativeconfig @@ -1087,12 +1057,12 @@ PODS: - React-debug - React-rncore (0.74.2) - React-RuntimeApple (0.74.2): - - hermes-engine - RCT-Folly/Fabric (= 2024.01.01.00) - React-callinvoker - React-Core/Default - React-CoreModules - React-cxxreact + - React-jsc - React-jserrorhandler - React-jsi - React-jsiexecutor @@ -1102,14 +1072,13 @@ PODS: - React-RCTFabric - React-RuntimeCore - React-runtimeexecutor - - React-RuntimeHermes - React-utils - React-RuntimeCore (0.74.2): - glog - - hermes-engine - RCT-Folly/Fabric (= 2024.01.01.00) - React-cxxreact - React-featureflags + - React-jsc - React-jserrorhandler - React-jsi - React-jsiexecutor @@ -1119,34 +1088,23 @@ PODS: - React-utils - React-runtimeexecutor (0.74.2): - React-jsi (= 0.74.2) - - React-RuntimeHermes (0.74.2): - - hermes-engine - - RCT-Folly/Fabric (= 2024.01.01.00) - - React-featureflags - - React-hermes - - React-jsi - - React-jsinspector - - React-jsitracing - - React-nativeconfig - - React-RuntimeCore - - React-utils - React-runtimescheduler (0.74.2): - glog - - hermes-engine - RCT-Folly (= 2024.01.01.00) - React-callinvoker - React-cxxreact - React-debug - React-featureflags + - React-jsc - React-jsi - React-rendererdebug - React-runtimeexecutor - React-utils - React-utils (0.74.2): - glog - - hermes-engine - RCT-Folly (= 2024.01.01.00) - React-debug + - React-jsc - React-jsi (= 0.74.2) - ReactCommon (0.74.2): - ReactCommon/turbomodule (= 0.74.2) @@ -1154,7 +1112,6 @@ PODS: - DoubleConversion - fmt (= 9.1.0) - glog - - hermes-engine - RCT-Folly (= 2024.01.01.00) - React-callinvoker (= 0.74.2) - React-cxxreact (= 0.74.2) @@ -1167,7 +1124,6 @@ PODS: - DoubleConversion - fmt (= 9.1.0) - glog - - hermes-engine - RCT-Folly (= 2024.01.01.00) - React-callinvoker (= 0.74.2) - React-cxxreact (= 0.74.2) @@ -1178,7 +1134,6 @@ PODS: - DoubleConversion - fmt (= 9.1.0) - glog - - hermes-engine - RCT-Folly (= 2024.01.01.00) - React-callinvoker (= 0.74.2) - React-cxxreact (= 0.74.2) @@ -1190,7 +1145,6 @@ PODS: - ReactNativeHost (0.5.0): - DoubleConversion - glog - - hermes-engine - RCT-Folly (= 2024.01.01.00) - RCTRequired - RCTTypeSafety @@ -1202,6 +1156,7 @@ PODS: - React-featureflags - React-graphics - React-ImageManager + - React-jsi - React-NativeModulesApple - React-RCTFabric - React-rendererdebug @@ -1216,7 +1171,6 @@ PODS: - RNGestureHandler (2.19.0): - DoubleConversion - glog - - hermes-engine - RCT-Folly (= 2024.01.01.00) - RCTRequired - RCTTypeSafety @@ -1227,6 +1181,7 @@ PODS: - React-featureflags - React-graphics - React-ImageManager + - React-jsi - React-NativeModulesApple - React-RCTFabric - React-rendererdebug @@ -1237,7 +1192,6 @@ PODS: - RNReanimated (3.15.2): - DoubleConversion - glog - - hermes-engine - RCT-Folly (= 2024.01.01.00) - RCTRequired - RCTTypeSafety @@ -1248,6 +1202,7 @@ PODS: - React-featureflags - React-graphics - React-ImageManager + - React-jsi - React-NativeModulesApple - React-RCTFabric - React-rendererdebug @@ -1260,7 +1215,6 @@ PODS: - RNReanimated/reanimated (3.15.2): - DoubleConversion - glog - - hermes-engine - RCT-Folly (= 2024.01.01.00) - RCTRequired - RCTTypeSafety @@ -1271,6 +1225,7 @@ PODS: - React-featureflags - React-graphics - React-ImageManager + - React-jsi - React-NativeModulesApple - React-RCTFabric - React-rendererdebug @@ -1281,7 +1236,6 @@ PODS: - RNReanimated/worklets (3.15.2): - DoubleConversion - glog - - hermes-engine - RCT-Folly (= 2024.01.01.00) - RCTRequired - RCTTypeSafety @@ -1292,6 +1246,7 @@ PODS: - React-featureflags - React-graphics - React-ImageManager + - React-jsi - React-NativeModulesApple - React-RCTFabric - React-rendererdebug @@ -1308,7 +1263,6 @@ DEPENDENCIES: - FBLazyVector (from `../../../node_modules/react-native/Libraries/FBLazyVector`) - fmt (from `../../../node_modules/react-native/third-party-podspecs/fmt.podspec`) - glog (from `../../../node_modules/react-native/third-party-podspecs/glog.podspec`) - - hermes-engine (from `../../../node_modules/react-native/sdks/hermes-engine/hermes-engine.podspec`) - RCT-Folly (from `../../../node_modules/react-native/third-party-podspecs/RCT-Folly.podspec`) - RCT-Folly/Fabric (from `../../../node_modules/react-native/third-party-podspecs/RCT-Folly.podspec`) - RCTDeprecation (from `../../../node_modules/react-native/ReactApple/Libraries/RCTFoundation/RCTDeprecation`) @@ -1326,8 +1280,8 @@ DEPENDENCIES: - React-FabricImage (from `../../../node_modules/react-native/ReactCommon`) - React-featureflags (from `../../../node_modules/react-native/ReactCommon/react/featureflags`) - React-graphics (from `../../../node_modules/react-native/ReactCommon/react/renderer/graphics`) - - React-hermes (from `../../../node_modules/react-native/ReactCommon/hermes`) - React-ImageManager (from `../../../node_modules/react-native/ReactCommon/react/renderer/imagemanager/platform/ios`) + - React-jsc (from `../../../node_modules/react-native/ReactCommon/jsc`) - React-jserrorhandler (from `../../../node_modules/react-native/ReactCommon/jserrorhandler`) - React-jsi (from `../../../node_modules/react-native/ReactCommon/jsi`) - React-jsiexecutor (from `../../../node_modules/react-native/ReactCommon/jsiexecutor`) @@ -1356,7 +1310,6 @@ DEPENDENCIES: - React-RuntimeApple (from `../../../node_modules/react-native/ReactCommon/react/runtime/platform/ios`) - React-RuntimeCore (from `../../../node_modules/react-native/ReactCommon/react/runtime`) - React-runtimeexecutor (from `../../../node_modules/react-native/ReactCommon/runtimeexecutor`) - - React-RuntimeHermes (from `../../../node_modules/react-native/ReactCommon/react/runtime`) - React-runtimescheduler (from `../../../node_modules/react-native/ReactCommon/react/renderer/runtimescheduler`) - React-utils (from `../../../node_modules/react-native/ReactCommon/react/utils`) - ReactCommon/turbomodule/core (from `../../../node_modules/react-native/ReactCommon`) @@ -1382,9 +1335,6 @@ EXTERNAL SOURCES: :podspec: "../../../node_modules/react-native/third-party-podspecs/fmt.podspec" glog: :podspec: "../../../node_modules/react-native/third-party-podspecs/glog.podspec" - hermes-engine: - :podspec: "../../../node_modules/react-native/sdks/hermes-engine/hermes-engine.podspec" - :tag: hermes-2024-06-03-RNv0.74.2-bb1e74fe1e95c2b5a2f4f9311152da052badc2bc RCT-Folly: :podspec: "../../../node_modules/react-native/third-party-podspecs/RCT-Folly.podspec" RCTDeprecation: @@ -1415,10 +1365,10 @@ EXTERNAL SOURCES: :path: "../../../node_modules/react-native/ReactCommon/react/featureflags" React-graphics: :path: "../../../node_modules/react-native/ReactCommon/react/renderer/graphics" - React-hermes: - :path: "../../../node_modules/react-native/ReactCommon/hermes" React-ImageManager: :path: "../../../node_modules/react-native/ReactCommon/react/renderer/imagemanager/platform/ios" + React-jsc: + :path: "../../../node_modules/react-native/ReactCommon/jsc" React-jserrorhandler: :path: "../../../node_modules/react-native/ReactCommon/jserrorhandler" React-jsi: @@ -1475,8 +1425,6 @@ EXTERNAL SOURCES: :path: "../../../node_modules/react-native/ReactCommon/react/runtime" React-runtimeexecutor: :path: "../../../node_modules/react-native/ReactCommon/runtimeexecutor" - React-RuntimeHermes: - :path: "../../../node_modules/react-native/ReactCommon/react/runtime" React-runtimescheduler: :path: "../../../node_modules/react-native/ReactCommon/react/renderer/runtimescheduler" React-utils: @@ -1502,41 +1450,40 @@ SPEC CHECKSUMS: FBLazyVector: 4bc164e5b5e6cfc288d2b5ff28643ea15fa1a589 fmt: 4c2741a687cc09f0634a2e2c72a838b99f1ff120 glog: fdfdfe5479092de0c4bdbebedd9056951f092c4f - hermes-engine: 01d3e052018c2a13937aca1860fbedbccd4a41b7 RCT-Folly: 02617c592a293bd6d418e0a88ff4ee1f88329b47 RCTDeprecation: b03c35057846b685b3ccadc9bfe43e349989cdb2 RCTRequired: 194626909cfa8d39ca6663138c417bc6c431648c RCTTypeSafety: 552aff5b8e8341660594db00e53ac889682bc120 React: a57fe42044fe6ed3e828f8867ce070a6c5872754 React-callinvoker: 6bedefb354a8848b534752417954caa3a5cf34f9 - React-Codegen: ed6f073979a0c97091bbff1e02f0fb7d03b7d370 - React-Core: 289ee3dfc1639bb9058c1e77427bb48169c26d7a + React-Codegen: 6d86d54bbe76e2bb648ed9a55f7cef62d4ef001a + React-Core: 5c1d9ccec3980f94f22d9d5d1cd185d87c89cfcf React-CoreModules: eda5ce541a1f552158317abd90a5a0f6a4f8d6f7 - React-cxxreact: 56bd17ccc6d4248116f7f95884ddb8c528379fb6 + React-cxxreact: e7b005ccfe69a961ef2c6f8890409e5525844074 React-debug: 164b8e302404d92d4bec39778a5e03bcb1b6eb08 - React-Fabric: 05620c36074e3ab397dd8f9db0deb6d3c38b4efa - React-FabricImage: 2a8a7f5729f5c44e32e6f58f7225ee1017ed0704 + React-Fabric: 813eb5fd4710af23d5c0448e30390e6fd19a7441 + React-FabricImage: 4592a0dede0b033c3c19d313d770c09c6d5481fa React-featureflags: d97a6393993052e951e16a3b81206e22110be8d2 React-graphics: ef07d701f4eb72ae6fca6ed0a7260a04f2a58dec - React-hermes: 6ccc301ababfa17a9aad25a7e33faf325fd024b4 React-ImageManager: 00404bfe122626bc6493621f2a31ce802115a9b3 + React-jsc: e70a1bb9f4336280fc7b225a01f3d43435713d4f React-jserrorhandler: 5e2632590a84363855b2083e6b3d501e93bc3f04 - React-jsi: 828703c235f4eea1647897ee8030efdc6e8e9f14 - React-jsiexecutor: 713d7bbef0a410cee5b3b78f73ed1fc16e177ba7 - React-jsinspector: e1fa5325a47f34645195c63e3312ddb6a2efef5d + React-jsi: d254f10d6109d5a63610c71fc87fbf5d8460729e + React-jsiexecutor: f8a1e251674b9e71dfeb290680a02cd12f7ed1bf + React-jsinspector: 38e2c05ab66e647ac752cb5604f39b5c31171e84 React-jsitracing: 0fa7f78d8fdda794667cb2e6f19c874c1cf31d7e React-logger: 29fa3e048f5f67fe396bc08af7606426d9bd7b5d React-Mapbuffer: bf56147c9775491e53122a94c423ac201417e326 react-native-safe-area-context: 851c62c48dce80ccaa5637b6aa5991a1bc36eca9 - react-native-wgpu: 078c84b159de97704f8926011b0f9bc96591d478 + react-native-wgpu: fc6eeb8096c12fd0f9034e5d190764d5f5f62ecd React-nativeconfig: 9f223cd321823afdecf59ed00861ab2d69ee0fc1 - React-NativeModulesApple: ff7efaff7098639db5631236cfd91d60abff04c0 + React-NativeModulesApple: aa472b3b953e3aceeb45d82e86e03dee776d01de React-perflogger: 32ed45d9cee02cf6639acae34251590dccd30994 React-RCTActionSheet: 19f967ddaea258182b56ef11437133b056ba2adf React-RCTAnimation: d7f4137fc44a08bba465267ea7cb1dbdb7c4ec87 - React-RCTAppDelegate: 2b3f4d8009796af209a0d496e73276b743acee08 - React-RCTBlob: c6c3e1e0251700b7bea036b893913f22e2b9cb47 - React-RCTFabric: 93a3ea55169d19294f07092013c1c9ea7a015c9b + React-RCTAppDelegate: 7ec8c3fad90193071b31e0f52278f0339624924e + React-RCTBlob: 1f9cb31b2331c7ac97f0d5ce3ab626f990edfd43 + React-RCTFabric: 04765e952b8520461f16dfda2fb4a713e04c7670 React-RCTImage: 40528ab74a4fef0f0e2ee797a074b26d120b6cc6 React-RCTLinking: 385b5beb96749aae9ae1606746e883e1c9f8a6a7 React-RCTNetwork: ffc9f05bd8fa5b3bce562199ba41235ad0af645c @@ -1545,21 +1492,20 @@ SPEC CHECKSUMS: React-RCTVibration: e4ccf673579d0d94a96b3a0b64492db08f8324d5 React-rendererdebug: ac70f40de137ce7bdbc55eaee60c467a215d9923 React-rncore: edfff7a3f7f82ca1e0ba26978c6d84c7a8970dac - React-RuntimeApple: a0c98b75571aa5f44ddc7c6e9fd55803fa4db00f - React-RuntimeCore: 4b8db1fe2f3f4a3a5ecb22e1a419824e3e2cd7ef + React-RuntimeApple: 5e2eaa6dff2ba15a0a5ceff8c15ab692e303d68b + React-RuntimeCore: f30a5d38677d9fb38875f5efe4b9bbd79f6797ac React-runtimeexecutor: 5961acc7a77b69f964e1645a5d6069e124ce6b37 - React-RuntimeHermes: c5825bfae4815fdf4e9e639340c3a986a491884c - React-runtimescheduler: 56b642bf605ba5afa500d35790928fc1d51565ad - React-utils: 4476b7fcbbd95cfd002f3e778616155241d86e31 - ReactCommon: ecad995f26e0d1e24061f60f4e5d74782f003f12 - ReactNativeHost: 76fb17eac13a9a2200f659deffc91c054731a7e2 + React-runtimescheduler: d2f61c6fd47401f52d220cff22791db1ac7053e0 + React-utils: a4de2ebe8d96dfafd27ffd7c131e7ddca3a386e4 + ReactCommon: f75776d715a088ecb67041eaf50ccdb7e94b245c + ReactNativeHost: dce645b2e9026bb18485762c2599509ead34447b ReactTestApp-DevSupport: 690d06567b7ecae4f2f98dff5e4881c8d25be8e2 ReactTestApp-Resources: 857244f3a23f2b3157b364fa06cf3e8866deff9c - RNGestureHandler: 67e78f16895947f7e57ab91e75e914d3e9ef7239 - RNReanimated: 4c72fc2c0f4c6a9c36932e653cd68e4521b6c4ac + RNGestureHandler: 7f8d8a2c7361ab9326f28ad42e9d9843cba98778 + RNReanimated: 2dc2ba731bf4a3fd3bba2e3c598951c5aa4c432f SocketRocket: abac6f5de4d4d62d24e11868d7a2f427e0ef940d Yoga: ae3c32c514802d30f687a04a6a35b348506d411f -PODFILE CHECKSUM: 21851af42ab33f67696cf6469097d79f1cc68181 +PODFILE CHECKSUM: e27e9a1602b03e96ffc2d6e45e4fa7daec1a754e COCOAPODS: 1.15.2 diff --git a/apps/example/package.json b/apps/example/package.json index 859a494d4..751e1feba 100644 --- a/apps/example/package.json +++ b/apps/example/package.json @@ -20,6 +20,7 @@ "@react-navigation/native": "^6.1.17", "@react-navigation/stack": "^6.4.0", "@react-three/fiber": "^8.17.6", + "@tensorflow/tfjs": "^4.22.0", "babel-plugin-transform-import-meta": "^2.2.1", "fast-text-encoding": "^1.0.6", "react": "18.2.0", diff --git a/apps/example/src/TransformerJS/TransformerJS.tsx b/apps/example/src/TransformerJS/TransformerJS.tsx index 993cf5b19..9bf7f9310 100644 --- a/apps/example/src/TransformerJS/TransformerJS.tsx +++ b/apps/example/src/TransformerJS/TransformerJS.tsx @@ -147,9 +147,8 @@ async function load() { } load().then((r) => { - console.log(r); - check(); + console.log({ done: r }); }); // Listen for messages from the main thread diff --git a/apps/example/src/TransformerJS/transformers.js b/apps/example/src/TransformerJS/transformers.js index aa6d8658a..9e5f4586a 100644 --- a/apps/example/src/TransformerJS/transformers.js +++ b/apps/example/src/TransformerJS/transformers.js @@ -2054,6 +2054,7 @@ const META_URL = "http://localhost:5173/"; const errors = []; const availableBackendNames = /* @__PURE__ */ new Set(); for (const backendName of backendNames) { + console.log({ backendName }); const resolveResult = await tryResolveAndInitializeBackend(backendName); if (typeof resolveResult === "string") { errors.push({ name: backendName, err: resolveResult }); @@ -5248,7 +5249,7 @@ const META_URL = "http://localhost:5173/"; tt(b); } }); } - var ci = typeof TextDecoder < "u" ? new TextDecoder("utf-16le") : void 0, _c = (s, p) => { + var ci = typeof TextDecoder < "u" ? new TextDecoder("utf-8") : void 0, _c = (s, p) => { for (var f = s >> 1, b = f + p / 2; !(f >= b) && o()[f >>> 0]; ) ++f; if (32 < (f <<= 1) - s && ci) return ci.decode(r().slice(s, f)); for (f = "", b = 0; !(b >= p / 2); ++b) { @@ -6215,6 +6216,7 @@ const META_URL = "http://localhost:5173/"; let n = r.buffer, o = r.byteOffset, i = r.byteLength, a = Qn(i), d = this.storageCache.get(t); if (!d) throw new Error("gpu data for uploading does not exist"); if (d.originalSize !== i) throw new Error(`inconsistent data size. gpu data size=${d.originalSize}, data size=${i}`); + console.log({ backend: this.backend, device: this.backend.device }); let l = this.backend.device.createBuffer({ mappedAtCreation: true, size: a, usage: GPUBufferUsage.MAP_WRITE | GPUBufferUsage.COPY_SRC }), c = l.getMappedRange(); new Uint8Array(c).set(new Uint8Array(n, o, i)), l.unmap(); let m = this.backend.getCommandEncoder(); @@ -11828,9 +11830,7 @@ const META_URL = "http://localhost:5173/"; async initialize(t, r) { this.env = t; let n = [], o = { requiredLimits: { maxComputeWorkgroupStorageSize: r.limits.maxComputeWorkgroupStorageSize, maxComputeWorkgroupsPerDimension: r.limits.maxComputeWorkgroupsPerDimension, maxStorageBufferBindingSize: r.limits.maxStorageBufferBindingSize, maxBufferSize: r.limits.maxBufferSize, maxComputeInvocationsPerWorkgroup: r.limits.maxComputeInvocationsPerWorkgroup, maxComputeWorkgroupSizeX: r.limits.maxComputeWorkgroupSizeX, maxComputeWorkgroupSizeY: r.limits.maxComputeWorkgroupSizeY, maxComputeWorkgroupSizeZ: r.limits.maxComputeWorkgroupSizeZ }, requiredFeatures: n }; - r.features.has("chromium-experimental-timestamp-query-inside-passes") ? n.push("chromium-experimental-timestamp-query-inside-passes") : r.features.has("timestamp-query") && n.push("timestamp-query"), r.features.has("shader-f16") && n.push("shader-f16"), this.device = await r.requestDevice(o), this.adapterInfo = new Io(r.info || await r.requestAdapterInfo()), this.gpuDataManager = ja(this), this.programManager = new an(this), this.kernels = /* @__PURE__ */ new Map(), this.kernelPersistentData = /* @__PURE__ */ new Map(), this.kernelCustomData = /* @__PURE__ */ new Map(), Wr(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: false, enumerable: true, configurable: false }), Object.defineProperty(this.env.webgpu, "adapter", { value: r, writable: false, enumerable: true, configurable: false }), this.setQueryType(); + r.features.has("chromium-experimental-timestamp-query-inside-passes") ? n.push("chromium-experimental-timestamp-query-inside-passes") : r.features.has("timestamp-query") && n.push("timestamp-query"), r.features.has("shader-f16") && n.push("shader-f16"), this.device = await r.requestDevice(o), this.adapterInfo = new Io(r.info || await r.requestAdapterInfo()), this.gpuDataManager = ja(this), this.programManager = new an(this), this.kernels = /* @__PURE__ */ new Map(), this.kernelPersistentData = /* @__PURE__ */ new Map(), this.kernelCustomData = /* @__PURE__ */ new Map(), Wr(t.logLevel, !!t.debug), Object.defineProperty(this.env.webgpu, "device", { value: this.device, writable: false, enumerable: true, configurable: false }), Object.defineProperty(this.env.webgpu, "adapter", { value: r, writable: false, enumerable: true, configurable: false }), this.setQueryType(); } dispose() { typeof this.querySet < "u" && this.querySet.destroy(), this.gpuDataManager.dispose(); diff --git a/packages/webgpu/cpp/rnwgpu/api/GPUAdapterInfo.h b/packages/webgpu/cpp/rnwgpu/api/GPUAdapterInfo.h index 640b1b6a0..327d03c93 100644 --- a/packages/webgpu/cpp/rnwgpu/api/GPUAdapterInfo.h +++ b/packages/webgpu/cpp/rnwgpu/api/GPUAdapterInfo.h @@ -31,21 +31,21 @@ class GPUAdapterInfo : public m::HybridObject { return ""; } std::string getArchitecture() { - if (_instance.architecture.length) { - return _instance.architecture.data; - } +// if (_instance.architecture.length) { +// return _instance.architecture.data; +// } return ""; } std::string getDevice() { - if (_instance.device.length) { - return _instance.device.data; - } +// if (_instance.device.length) { +// return _instance.device.data; +// } return ""; } std::string getDescription() { - if (_instance.device.length) { - return _instance.device.data; - } +// if (_instance.device.length) { +// return _instance.device.data; +// } return ""; } diff --git a/packages/webgpu/cpp/rnwgpu/api/GPUDevice.h b/packages/webgpu/cpp/rnwgpu/api/GPUDevice.h index f3c7d5121..561c101d7 100644 --- a/packages/webgpu/cpp/rnwgpu/api/GPUDevice.h +++ b/packages/webgpu/cpp/rnwgpu/api/GPUDevice.h @@ -148,8 +148,13 @@ class GPUDevice : public m::HybridObject { registerHybridGetter("lost", &GPUDevice::getLost, this); registerHybridGetter("label", &GPUDevice::getLabel, this); registerHybridSetter("label", &GPUDevice::setLabel, this); + // registerHybridSetter("onuncapturederror", &GPUDevice::setOnuncapturederror, this); } +// void setOnuncapturederror() { +// ; +// } + inline const wgpu::Device get() { return _instance; } private: diff --git a/yarn.lock b/yarn.lock index 1118d412a..017d8ac47 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3444,6 +3444,100 @@ __metadata: languageName: node linkType: hard +"@tensorflow/tfjs-backend-cpu@npm:4.22.0": + version: 4.22.0 + resolution: "@tensorflow/tfjs-backend-cpu@npm:4.22.0" + dependencies: + "@types/seedrandom": ^2.4.28 + seedrandom: ^3.0.5 + peerDependencies: + "@tensorflow/tfjs-core": 4.22.0 + checksum: afa375b7d752e145fee025270f2c848504987cf8e54f787736bfde968853428c0c8e4539205beee4c59443459c0a4cd150021aa613b48f40547987a94c7d7e04 + languageName: node + linkType: hard + +"@tensorflow/tfjs-backend-webgl@npm:4.22.0": + version: 4.22.0 + resolution: "@tensorflow/tfjs-backend-webgl@npm:4.22.0" + dependencies: + "@tensorflow/tfjs-backend-cpu": 4.22.0 + "@types/offscreencanvas": ~2019.3.0 + "@types/seedrandom": ^2.4.28 + seedrandom: ^3.0.5 + peerDependencies: + "@tensorflow/tfjs-core": 4.22.0 + checksum: 16da253bc53e3ac97f6da40257794f24e7831ee7e362be397c8c004084ab2db25f7803bfee0d9b57d60961ce1146f2dc9c94b26570c5f8cfd7eab2fc89e93f59 + languageName: node + linkType: hard + +"@tensorflow/tfjs-converter@npm:4.22.0": + version: 4.22.0 + resolution: "@tensorflow/tfjs-converter@npm:4.22.0" + peerDependencies: + "@tensorflow/tfjs-core": 4.22.0 + checksum: 918d3e11eed19cda41f785a3964619bfeeb40da6c3ad24a5028ac71c2abeaab7e201778bdbed94545e4b08931a08ff194bfecfde1131cf5c4837a0818e2cd508 + languageName: node + linkType: hard + +"@tensorflow/tfjs-core@npm:4.22.0": + version: 4.22.0 + resolution: "@tensorflow/tfjs-core@npm:4.22.0" + dependencies: + "@types/long": ^4.0.1 + "@types/offscreencanvas": ~2019.7.0 + "@types/seedrandom": ^2.4.28 + "@webgpu/types": 0.1.38 + long: 4.0.0 + node-fetch: ~2.6.1 + seedrandom: ^3.0.5 + checksum: 3efe6fc6020911ea06e387bc7228e73bb4bb33e2440c7a970369be8952ba3ac3f4ef5a3cd78a2e382702de0a3b8f097869e4e3a3e7dd65fa93e3f78d15d6b334 + languageName: node + linkType: hard + +"@tensorflow/tfjs-data@npm:4.22.0": + version: 4.22.0 + resolution: "@tensorflow/tfjs-data@npm:4.22.0" + dependencies: + "@types/node-fetch": ^2.1.2 + node-fetch: ~2.6.1 + string_decoder: ^1.3.0 + peerDependencies: + "@tensorflow/tfjs-core": 4.22.0 + seedrandom: ^3.0.5 + checksum: b20770a79222688f7a1349c0c19fc913424ea095c6d1d329625a4cd4c80a1ff3d988e3a845ab2f2823c72a7990f6b070cbce4e638a42fe4d2385b420477aa36b + languageName: node + linkType: hard + +"@tensorflow/tfjs-layers@npm:4.22.0": + version: 4.22.0 + resolution: "@tensorflow/tfjs-layers@npm:4.22.0" + peerDependencies: + "@tensorflow/tfjs-core": 4.22.0 + checksum: 88253be2b91dadb9264303d2260fe6160e753fabf4642c290d26c451385363ce8d0ad150d5a3070e52645d4ba061c2e63b6dda67d3bf3257a36e65ab8fd3d69a + languageName: node + linkType: hard + +"@tensorflow/tfjs@npm:^4.22.0": + version: 4.22.0 + resolution: "@tensorflow/tfjs@npm:4.22.0" + dependencies: + "@tensorflow/tfjs-backend-cpu": 4.22.0 + "@tensorflow/tfjs-backend-webgl": 4.22.0 + "@tensorflow/tfjs-converter": 4.22.0 + "@tensorflow/tfjs-core": 4.22.0 + "@tensorflow/tfjs-data": 4.22.0 + "@tensorflow/tfjs-layers": 4.22.0 + argparse: ^1.0.10 + chalk: ^4.1.0 + core-js: 3.29.1 + regenerator-runtime: ^0.13.5 + yargs: ^16.0.3 + bin: + tfjs-custom-module: dist/tools/custom_module/cli.js + checksum: 725afe0b4099ac3aef0d508019b195f4f2bcd3ed6a0384b39ced6113d65698d4f251b7d417ac751a5e5db2b275efccfbd53b495fff04a82459fa548f5085dfac + languageName: node + linkType: hard + "@tootallnate/quickjs-emscripten@npm:^0.23.0": version: 0.23.0 resolution: "@tootallnate/quickjs-emscripten@npm:0.23.0" @@ -3618,6 +3712,13 @@ __metadata: languageName: node linkType: hard +"@types/long@npm:^4.0.1": + version: 4.0.2 + resolution: "@types/long@npm:4.0.2" + checksum: d16cde7240d834cf44ba1eaec49e78ae3180e724cd667052b194a372f350d024cba8dd3f37b0864931683dab09ca935d52f0c4c1687178af5ada9fc85b0635f4 + languageName: node + linkType: hard + "@types/minimist@npm:^1.2.2": version: 1.2.5 resolution: "@types/minimist@npm:1.2.5" @@ -3625,6 +3726,16 @@ __metadata: languageName: node linkType: hard +"@types/node-fetch@npm:^2.1.2": + version: 2.6.12 + resolution: "@types/node-fetch@npm:2.6.12" + dependencies: + "@types/node": "*" + form-data: ^4.0.0 + checksum: 9647e68f9a125a090220c38d77b3c8e669c488658ae7506f1b4f9568214beba087624b1705bba1dc76649a65281ce3fd5b400e15266cbef8088027fb88777557 + languageName: node + linkType: hard + "@types/node-forge@npm:^1.3.0": version: 1.3.11 resolution: "@types/node-forge@npm:1.3.11" @@ -3668,6 +3779,20 @@ __metadata: languageName: node linkType: hard +"@types/offscreencanvas@npm:~2019.3.0": + version: 2019.3.0 + resolution: "@types/offscreencanvas@npm:2019.3.0" + checksum: 006361d170aac80925b16a8c0421d5ee7bfe6acb5cd6b22407e2c28be091becc7de5ba1fd21293a1fdff70648918d9c035001ffafd4d6c49d1c5a22dab88ed0d + languageName: node + linkType: hard + +"@types/offscreencanvas@npm:~2019.7.0": + version: 2019.7.3 + resolution: "@types/offscreencanvas@npm:2019.7.3" + checksum: 53a394a65ae08eddff6e0a2a8db72abecc94f41fc8fee166e8900075d3c1ca32540ddf5b4836c37357d53a0253a03fea4d781b2db543e3f08bc1cdc2dc0fefb5 + languageName: node + linkType: hard + "@types/parse-json@npm:^4.0.0": version: 4.0.2 resolution: "@types/parse-json@npm:4.0.2" @@ -3737,6 +3862,13 @@ __metadata: languageName: node linkType: hard +"@types/seedrandom@npm:^2.4.28": + version: 2.4.34 + resolution: "@types/seedrandom@npm:2.4.34" + checksum: d9bcf58dbdbf7f3c708e6d252e15a452ee9a289905d1dedec80d18d5c57cc6ab777687da189ebc4ad8840bd2d89a6b8812c878c90f014ed5a598f09157a9ad3e + languageName: node + linkType: hard + "@types/seedrandom@npm:^3.0.8": version: 3.0.8 resolution: "@types/seedrandom@npm:3.0.8" @@ -4336,6 +4468,7 @@ __metadata: "@react-navigation/stack": ^6.4.0 "@react-three/fiber": ^8.17.6 "@rnx-kit/metro-config": ^1.3.17 + "@tensorflow/tfjs": ^4.22.0 "@types/node": ^20.14.7 "@types/react": ^18.2.6 "@types/react-test-renderer": ^18.0.0 @@ -4575,7 +4708,7 @@ __metadata: languageName: node linkType: hard -"argparse@npm:^1.0.7": +"argparse@npm:^1.0.10, argparse@npm:^1.0.7": version: 1.0.10 resolution: "argparse@npm:1.0.10" dependencies: @@ -4742,6 +4875,13 @@ __metadata: languageName: node linkType: hard +"asynckit@npm:^0.4.0": + version: 0.4.0 + resolution: "asynckit@npm:0.4.0" + checksum: 7b78c451df768adba04e2d02e63e2d0bf3b07adcd6e42b4cf665cb7ce899bedd344c69a1dcbce355b5f972d597b25aaa1c1742b52cffd9caccb22f348114f6be + languageName: node + linkType: hard + "available-typed-arrays@npm:^1.0.7": version: 1.0.7 resolution: "available-typed-arrays@npm:1.0.7" @@ -5309,6 +5449,17 @@ __metadata: languageName: node linkType: hard +"cliui@npm:^7.0.2": + version: 7.0.4 + resolution: "cliui@npm:7.0.4" + dependencies: + string-width: ^4.2.0 + strip-ansi: ^6.0.0 + wrap-ansi: ^7.0.0 + checksum: ce2e8f578a4813806788ac399b9e866297740eecd4ad1823c27fd344d78b22c5f8597d548adbcc46f0573e43e21e751f39446c5a5e804a12aace402b7a315d7f + languageName: node + linkType: hard + "cliui@npm:^8.0.0, cliui@npm:^8.0.1": version: 8.0.1 resolution: "cliui@npm:8.0.1" @@ -5418,6 +5569,15 @@ __metadata: languageName: node linkType: hard +"combined-stream@npm:^1.0.8": + version: 1.0.8 + resolution: "combined-stream@npm:1.0.8" + dependencies: + delayed-stream: ~1.0.0 + checksum: 49fa4aeb4916567e33ea81d088f6584749fc90c7abec76fd516bf1c5aa5c79f3584b5ba3de6b86d26ddd64bae5329c4c7479343250cfe71c75bb366eae53bb7c + languageName: node + linkType: hard + "command-exists@npm:^1.2.8": version: 1.2.9 resolution: "command-exists@npm:1.2.9" @@ -5505,6 +5665,13 @@ __metadata: languageName: node linkType: hard +"core-js@npm:3.29.1": + version: 3.29.1 + resolution: "core-js@npm:3.29.1" + checksum: b38446dbfcfd3887b3d4922990da487e2c95044cb4c5717aaf95e786a4c6b218f05c056c7ed6c699169b9794a49fec890e402659d54661fc56965a0eb717e7bd + languageName: node + linkType: hard + "core-util-is@npm:~1.0.0": version: 1.0.3 resolution: "core-util-is@npm:1.0.3" @@ -5831,6 +5998,13 @@ __metadata: languageName: node linkType: hard +"delayed-stream@npm:~1.0.0": + version: 1.0.0 + resolution: "delayed-stream@npm:1.0.0" + checksum: 46fe6e83e2cb1d85ba50bd52803c68be9bd953282fa7096f51fc29edd5d67ff84ff753c51966061e5ba7cb5e47ef6d36a91924eddb7f3f3483b1c560f77a0020 + languageName: node + linkType: hard + "denodeify@npm:^1.2.1": version: 1.2.1 resolution: "denodeify@npm:1.2.1" @@ -6941,6 +7115,17 @@ __metadata: languageName: node linkType: hard +"form-data@npm:^4.0.0": + version: 4.0.1 + resolution: "form-data@npm:4.0.1" + dependencies: + asynckit: ^0.4.0 + combined-stream: ^1.0.8 + mime-types: ^2.1.12 + checksum: ccee458cd5baf234d6b57f349fe9cc5f9a2ea8fd1af5ecda501a18fd1572a6dd3bf08a49f00568afd995b6a65af34cb8dec083cf9d582c4e621836499498dd84 + languageName: node + linkType: hard + "fresh@npm:0.5.2": version: 0.5.2 resolution: "fresh@npm:0.5.2" @@ -8931,6 +9116,13 @@ __metadata: languageName: node linkType: hard +"long@npm:4.0.0": + version: 4.0.0 + resolution: "long@npm:4.0.0" + checksum: 16afbe8f749c7c849db1f4de4e2e6a31ac6e617cead3bdc4f9605cb703cd20e1e9fc1a7baba674ffcca57d660a6e5b53a9e236d7b25a295d3855cca79cc06744 + languageName: node + linkType: hard + "loose-envify@npm:^1.0.0, loose-envify@npm:^1.1.0, loose-envify@npm:^1.4.0": version: 1.4.0 resolution: "loose-envify@npm:1.4.0" @@ -9355,7 +9547,7 @@ __metadata: languageName: node linkType: hard -"mime-types@npm:^2.1.27, mime-types@npm:~2.1.34": +"mime-types@npm:^2.1.12, mime-types@npm:^2.1.27, mime-types@npm:~2.1.34": version: 2.1.35 resolution: "mime-types@npm:2.1.35" dependencies: @@ -9663,6 +9855,20 @@ __metadata: languageName: node linkType: hard +"node-fetch@npm:~2.6.1": + version: 2.6.13 + resolution: "node-fetch@npm:2.6.13" + dependencies: + whatwg-url: ^5.0.0 + peerDependencies: + encoding: ^0.1.0 + peerDependenciesMeta: + encoding: + optional: true + checksum: 055845ae5b4796c78c7053564745345025cf959563b3568b43c385f67d311779e6b00e5fef6ed1b79f86ba4048e4b4b722e1aa948305521b9353eb7e788912c9 + languageName: node + linkType: hard + "node-forge@npm:^1": version: 1.3.1 resolution: "node-forge@npm:1.3.1" @@ -10945,7 +11151,7 @@ __metadata: languageName: node linkType: hard -"regenerator-runtime@npm:^0.13.2": +"regenerator-runtime@npm:^0.13.2, regenerator-runtime@npm:^0.13.5": version: 0.13.11 resolution: "regenerator-runtime@npm:0.13.11" checksum: 27481628d22a1c4e3ff551096a683b424242a216fee44685467307f14d58020af1e19660bf2e26064de946bad7eff28950eae9f8209d55723e2d9351e632bbb4 @@ -11772,7 +11978,7 @@ __metadata: languageName: node linkType: hard -"string_decoder@npm:^1.1.1": +"string_decoder@npm:^1.1.1, string_decoder@npm:^1.3.0": version: 1.3.0 resolution: "string_decoder@npm:1.3.0" dependencies: @@ -12974,7 +13180,7 @@ __metadata: languageName: node linkType: hard -"yargs-parser@npm:^20.2.9": +"yargs-parser@npm:^20.2.2, yargs-parser@npm:^20.2.9": version: 20.2.9 resolution: "yargs-parser@npm:20.2.9" checksum: 8bb69015f2b0ff9e17b2c8e6bfe224ab463dd00ca211eece72a4cd8a906224d2703fb8a326d36fdd0e68701e201b2a60ed7cf81ce0fd9b3799f9fe7745977ae3 @@ -13007,6 +13213,21 @@ __metadata: languageName: node linkType: hard +"yargs@npm:^16.0.3": + version: 16.2.0 + resolution: "yargs@npm:16.2.0" + dependencies: + cliui: ^7.0.2 + escalade: ^3.1.1 + get-caller-file: ^2.0.5 + require-directory: ^2.1.1 + string-width: ^4.2.0 + y18n: ^5.0.5 + yargs-parser: ^20.2.2 + checksum: b14afbb51e3251a204d81937c86a7e9d4bdbf9a2bcee38226c900d00f522969ab675703bee2a6f99f8e20103f608382936034e64d921b74df82b63c07c5e8f59 + languageName: node + linkType: hard + "yargs@npm:^17.3.1, yargs@npm:^17.5.1, yargs@npm:^17.6.2, yargs@npm:^17.7.2": version: 17.7.2 resolution: "yargs@npm:17.7.2" From 3067a18300804592fdc0f824fd1f75918f40ba3e Mon Sep 17 00:00:00 2001 From: William Candillon Date: Thu, 12 Dec 2024 17:06:34 +0100 Subject: [PATCH 14/35] :wrench: --- apps/example/package.json | 1 + apps/example/src/App.tsx | 1 + .../src/TransformerJS/TransformerJS.tsx | 155 +----------------- yarn.lock | 12 ++ 4 files changed, 20 insertions(+), 149 deletions(-) diff --git a/apps/example/package.json b/apps/example/package.json index 751e1feba..81d042f17 100644 --- a/apps/example/package.json +++ b/apps/example/package.json @@ -21,6 +21,7 @@ "@react-navigation/stack": "^6.4.0", "@react-three/fiber": "^8.17.6", "@tensorflow/tfjs": "^4.22.0", + "@tensorflow/tfjs-backend-webgpu": "^4.22.0", "babel-plugin-transform-import-meta": "^2.2.1", "fast-text-encoding": "^1.0.6", "react": "18.2.0", diff --git a/apps/example/src/App.tsx b/apps/example/src/App.tsx index e094a56ad..2c68a2ac2 100644 --- a/apps/example/src/App.tsx +++ b/apps/example/src/App.tsx @@ -1,3 +1,4 @@ +import "react-native-wgpu"; import { NavigationContainer } from "@react-navigation/native"; import { createStackNavigator } from "@react-navigation/stack"; import { GestureHandlerRootView } from "react-native-gesture-handler"; diff --git a/apps/example/src/TransformerJS/TransformerJS.tsx b/apps/example/src/TransformerJS/TransformerJS.tsx index 9bf7f9310..9707a8448 100644 --- a/apps/example/src/TransformerJS/TransformerJS.tsx +++ b/apps/example/src/TransformerJS/TransformerJS.tsx @@ -1,156 +1,13 @@ // @ts-nocheck -/* eslint-disable camelcase */ import { View } from "react-native"; -import { - AutoTokenizer, - AutoModelForCausalLM, - TextStreamer, - InterruptableStoppingCriteria, -} from "./transformers"; - -class Self { - constructor() {} - - postMessage(data) { - console.log(data); - } -} - -const self = new Self(); - -/** - * This class uses the Singleton pattern to enable lazy-loading of the pipeline - */ -class TextGenerationPipeline { - static model_id = "onnx-community/Llama-3.2-1B-Instruct-q4f16"; - - static async getInstance(progress_callback = null) { - this.tokenizer ??= AutoTokenizer.from_pretrained(this.model_id, { - progress_callback, - }); - - this.model ??= AutoModelForCausalLM.from_pretrained(this.model_id, { - dtype: "q4f16", - device: "webgpu", - progress_callback, - }); - - return Promise.all([this.tokenizer, this.model]); - } -} - -const stopping_criteria = new InterruptableStoppingCriteria(); - -const past_key_values_cache = null; -async function generate(messages) { - // Retrieve the text-generation pipeline. - const [tokenizer, model] = await TextGenerationPipeline.getInstance(); - - const inputs = tokenizer.apply_chat_template(messages, { - add_generation_prompt: true, - return_dict: true, - }); - - let startTime; - let numTokens = 0; - let tps; - const token_callback_function = () => { - startTime ??= performance.now(); - - if (numTokens++ > 0) { - tps = (numTokens / (performance.now() - startTime)) * 1000; - } - }; - const callback_function = (output) => { - self.postMessage({ - status: "update", - output, - tps, - numTokens, - }); - }; - - const streamer = new TextStreamer(tokenizer, { - skip_prompt: true, - skip_special_tokens: true, - callback_function, - token_callback_function, - }); - - // Tell the main thread we are starting - self.postMessage({ status: "start" }); - - const { past_key_values, sequences } = await model.generate({ - ...inputs, - // TODO: Add when model is fixed - // past_key_values: past_key_values_cache, - - // Sampling - do_sample: false, - - max_new_tokens: 1024, - streamer, - stopping_criteria, - return_dict_in_generate: true, - }); - // past_key_values_cache = past_key_values; - - const decoded = tokenizer.batch_decode(sequences, { - skip_special_tokens: true, - }); - - // Send the output back to the main thread - self.postMessage({ - status: "complete", - output: decoded, - }); -} - -async function check() { - try { - const adapter = await navigator.gpu.requestAdapter(); - if (!adapter) { - throw new Error("WebGPU is not supported (no adapter found)"); - } - } catch (e) { - self.postMessage({ - status: "error", - data: e.toString(), - }); - } -} - -async function load() { - self.postMessage({ - status: "loading", - data: "Loading model...", - }); - - // Load the pipeline and save it for future use. - const [tokenizer, model] = await TextGenerationPipeline.getInstance((x) => { - // We also add a progress callback to the pipeline so that we can - // track model loading. - self.postMessage(x); - }); - - self.postMessage({ - status: "loading", - data: "Compiling shaders and warming up model...", - }); - - // Run model with dummy input to compile shaders - const inputs = tokenizer("a"); - await model.generate({ ...inputs, max_new_tokens: 1 }); - self.postMessage({ status: "ready" }); -} - -load().then((r) => { - check(); - console.log({ done: r }); -}); - +// Import @tensorflow/tfjs or @tensorflow/tfjs-core +import * as tf from "@tensorflow/tfjs"; +// Add the WebGPU backend to the global backend registry. +import "@tensorflow/tfjs-backend-webgpu"; +// Set the backend to WebGPU and wait for the module to be ready. +tf.setBackend("webgpu").then(() => console.log("WebGPU backend is ready")); // Listen for messages from the main thread // self.addEventListener("message", async (e) => { // const { type, data } = e.data; diff --git a/yarn.lock b/yarn.lock index 017d8ac47..fe75086fa 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3470,6 +3470,17 @@ __metadata: languageName: node linkType: hard +"@tensorflow/tfjs-backend-webgpu@npm:^4.22.0": + version: 4.22.0 + resolution: "@tensorflow/tfjs-backend-webgpu@npm:4.22.0" + dependencies: + "@tensorflow/tfjs-backend-cpu": 4.22.0 + peerDependencies: + "@tensorflow/tfjs-core": 4.22.0 + checksum: da9f3c3db9a78d5e6c3c332cd33716bc6b7dbc6fdc0237bf899587936c07a1f483da9ffc43ffad86ee9f7af8619cab7c421c2de9bd2f9c128a185e462b6ae9da + languageName: node + linkType: hard + "@tensorflow/tfjs-converter@npm:4.22.0": version: 4.22.0 resolution: "@tensorflow/tfjs-converter@npm:4.22.0" @@ -4469,6 +4480,7 @@ __metadata: "@react-three/fiber": ^8.17.6 "@rnx-kit/metro-config": ^1.3.17 "@tensorflow/tfjs": ^4.22.0 + "@tensorflow/tfjs-backend-webgpu": ^4.22.0 "@types/node": ^20.14.7 "@types/react": ^18.2.6 "@types/react-test-renderer": ^18.0.0 From 9f2994b5f26cb0f0088179488cade3ce88b6f4a5 Mon Sep 17 00:00:00 2001 From: William Candillon Date: Fri, 13 Dec 2024 11:33:17 +0100 Subject: [PATCH 15/35] :wrench: --- apps/example/ios/Podfile | 2 +- apps/example/ios/Podfile.lock | 226 +- apps/example/package.json | 1 + .../src/TransformerJS/TransformerJS.tsx | 125 +- apps/example/src/TransformerJS/runtime.js | 28 - .../example/src/TransformerJS/transformers.js | 37540 ---------------- .../webgpu/cpp/rnwgpu/api/GPUAdapterInfo.h | 18 +- .../api/descriptors/GPUDeviceDescriptor.h | 28 +- yarn.lock | 1233 +- 9 files changed, 1470 insertions(+), 37731 deletions(-) delete mode 100644 apps/example/src/TransformerJS/runtime.js delete mode 100644 apps/example/src/TransformerJS/transformers.js diff --git a/apps/example/ios/Podfile b/apps/example/ios/Podfile index 3a4a7b64c..24eedffca 100644 --- a/apps/example/ios/Podfile +++ b/apps/example/ios/Podfile @@ -7,7 +7,7 @@ require "#{ws_dir}/node_modules/react-native-test-app/test_app.rb" workspace 'Example.xcworkspace' options = { - :hermes_enabled => false + :hermes_enabled => true } use_test_app! options diff --git a/apps/example/ios/Podfile.lock b/apps/example/ios/Podfile.lock index 445be89bc..e29fb8c7c 100644 --- a/apps/example/ios/Podfile.lock +++ b/apps/example/ios/Podfile.lock @@ -4,6 +4,9 @@ PODS: - FBLazyVector (0.74.2) - fmt (9.1.0) - glog (0.3.5) + - hermes-engine (0.74.2): + - hermes-engine/Pre-built (= 0.74.2) + - hermes-engine/Pre-built (0.74.2) - RCT-Folly (2024.01.01.00): - boost - DoubleConversion @@ -43,6 +46,7 @@ PODS: - React-Codegen (0.74.2): - DoubleConversion - glog + - hermes-engine - RCT-Folly - RCTRequired - RCTTypeSafety @@ -52,7 +56,6 @@ PODS: - React-FabricImage - React-featureflags - React-graphics - - React-jsc - React-jsi - React-jsiexecutor - React-NativeModulesApple @@ -62,12 +65,13 @@ PODS: - ReactCommon/turbomodule/core - React-Core (0.74.2): - glog + - hermes-engine - RCT-Folly (= 2024.01.01.00) - RCTDeprecation - React-Core/Default (= 0.74.2) - React-cxxreact - React-featureflags - - React-jsc + - React-hermes - React-jsi - React-jsiexecutor - React-jsinspector @@ -78,12 +82,13 @@ PODS: - Yoga - React-Core/CoreModulesHeaders (0.74.2): - glog + - hermes-engine - RCT-Folly (= 2024.01.01.00) - RCTDeprecation - React-Core/Default - React-cxxreact - React-featureflags - - React-jsc + - React-hermes - React-jsi - React-jsiexecutor - React-jsinspector @@ -94,11 +99,12 @@ PODS: - Yoga - React-Core/Default (0.74.2): - glog + - hermes-engine - RCT-Folly (= 2024.01.01.00) - RCTDeprecation - React-cxxreact - React-featureflags - - React-jsc + - React-hermes - React-jsi - React-jsiexecutor - React-jsinspector @@ -109,13 +115,14 @@ PODS: - Yoga - React-Core/DevSupport (0.74.2): - glog + - hermes-engine - RCT-Folly (= 2024.01.01.00) - RCTDeprecation - React-Core/Default (= 0.74.2) - React-Core/RCTWebSocket (= 0.74.2) - React-cxxreact - React-featureflags - - React-jsc + - React-hermes - React-jsi - React-jsiexecutor - React-jsinspector @@ -126,12 +133,13 @@ PODS: - Yoga - React-Core/RCTActionSheetHeaders (0.74.2): - glog + - hermes-engine - RCT-Folly (= 2024.01.01.00) - RCTDeprecation - React-Core/Default - React-cxxreact - React-featureflags - - React-jsc + - React-hermes - React-jsi - React-jsiexecutor - React-jsinspector @@ -142,12 +150,13 @@ PODS: - Yoga - React-Core/RCTAnimationHeaders (0.74.2): - glog + - hermes-engine - RCT-Folly (= 2024.01.01.00) - RCTDeprecation - React-Core/Default - React-cxxreact - React-featureflags - - React-jsc + - React-hermes - React-jsi - React-jsiexecutor - React-jsinspector @@ -158,12 +167,13 @@ PODS: - Yoga - React-Core/RCTBlobHeaders (0.74.2): - glog + - hermes-engine - RCT-Folly (= 2024.01.01.00) - RCTDeprecation - React-Core/Default - React-cxxreact - React-featureflags - - React-jsc + - React-hermes - React-jsi - React-jsiexecutor - React-jsinspector @@ -174,12 +184,13 @@ PODS: - Yoga - React-Core/RCTImageHeaders (0.74.2): - glog + - hermes-engine - RCT-Folly (= 2024.01.01.00) - RCTDeprecation - React-Core/Default - React-cxxreact - React-featureflags - - React-jsc + - React-hermes - React-jsi - React-jsiexecutor - React-jsinspector @@ -190,12 +201,13 @@ PODS: - Yoga - React-Core/RCTLinkingHeaders (0.74.2): - glog + - hermes-engine - RCT-Folly (= 2024.01.01.00) - RCTDeprecation - React-Core/Default - React-cxxreact - React-featureflags - - React-jsc + - React-hermes - React-jsi - React-jsiexecutor - React-jsinspector @@ -206,12 +218,13 @@ PODS: - Yoga - React-Core/RCTNetworkHeaders (0.74.2): - glog + - hermes-engine - RCT-Folly (= 2024.01.01.00) - RCTDeprecation - React-Core/Default - React-cxxreact - React-featureflags - - React-jsc + - React-hermes - React-jsi - React-jsiexecutor - React-jsinspector @@ -222,12 +235,13 @@ PODS: - Yoga - React-Core/RCTSettingsHeaders (0.74.2): - glog + - hermes-engine - RCT-Folly (= 2024.01.01.00) - RCTDeprecation - React-Core/Default - React-cxxreact - React-featureflags - - React-jsc + - React-hermes - React-jsi - React-jsiexecutor - React-jsinspector @@ -238,12 +252,13 @@ PODS: - Yoga - React-Core/RCTTextHeaders (0.74.2): - glog + - hermes-engine - RCT-Folly (= 2024.01.01.00) - RCTDeprecation - React-Core/Default - React-cxxreact - React-featureflags - - React-jsc + - React-hermes - React-jsi - React-jsiexecutor - React-jsinspector @@ -254,12 +269,13 @@ PODS: - Yoga - React-Core/RCTVibrationHeaders (0.74.2): - glog + - hermes-engine - RCT-Folly (= 2024.01.01.00) - RCTDeprecation - React-Core/Default - React-cxxreact - React-featureflags - - React-jsc + - React-hermes - React-jsi - React-jsiexecutor - React-jsinspector @@ -270,12 +286,13 @@ PODS: - Yoga - React-Core/RCTWebSocket (0.74.2): - glog + - hermes-engine - RCT-Folly (= 2024.01.01.00) - RCTDeprecation - React-Core/Default (= 0.74.2) - React-cxxreact - React-featureflags - - React-jsc + - React-hermes - React-jsi - React-jsiexecutor - React-jsinspector @@ -303,6 +320,7 @@ PODS: - DoubleConversion - fmt (= 9.1.0) - glog + - hermes-engine - RCT-Folly (= 2024.01.01.00) - React-callinvoker (= 0.74.2) - React-debug (= 0.74.2) @@ -316,6 +334,7 @@ PODS: - DoubleConversion - fmt (= 9.1.0) - glog + - hermes-engine - RCT-Folly/Fabric (= 2024.01.01.00) - RCTRequired - RCTTypeSafety @@ -337,7 +356,6 @@ PODS: - React-Fabric/textlayoutmanager (= 0.74.2) - React-Fabric/uimanager (= 0.74.2) - React-graphics - - React-jsc - React-jsi - React-jsiexecutor - React-logger @@ -349,6 +367,7 @@ PODS: - DoubleConversion - fmt (= 9.1.0) - glog + - hermes-engine - RCT-Folly/Fabric (= 2024.01.01.00) - RCTRequired - RCTTypeSafety @@ -356,7 +375,6 @@ PODS: - React-cxxreact - React-debug - React-graphics - - React-jsc - React-jsi - React-jsiexecutor - React-logger @@ -368,6 +386,7 @@ PODS: - DoubleConversion - fmt (= 9.1.0) - glog + - hermes-engine - RCT-Folly/Fabric (= 2024.01.01.00) - RCTRequired - RCTTypeSafety @@ -375,7 +394,6 @@ PODS: - React-cxxreact - React-debug - React-graphics - - React-jsc - React-jsi - React-jsiexecutor - React-logger @@ -387,6 +405,7 @@ PODS: - DoubleConversion - fmt (= 9.1.0) - glog + - hermes-engine - RCT-Folly/Fabric (= 2024.01.01.00) - RCTRequired - RCTTypeSafety @@ -394,7 +413,6 @@ PODS: - React-cxxreact - React-debug - React-graphics - - React-jsc - React-jsi - React-jsiexecutor - React-logger @@ -406,6 +424,7 @@ PODS: - DoubleConversion - fmt (= 9.1.0) - glog + - hermes-engine - RCT-Folly/Fabric (= 2024.01.01.00) - RCTRequired - RCTTypeSafety @@ -413,7 +432,6 @@ PODS: - React-cxxreact - React-debug - React-graphics - - React-jsc - React-jsi - React-jsiexecutor - React-logger @@ -425,6 +443,7 @@ PODS: - DoubleConversion - fmt (= 9.1.0) - glog + - hermes-engine - RCT-Folly/Fabric (= 2024.01.01.00) - RCTRequired - RCTTypeSafety @@ -443,7 +462,6 @@ PODS: - React-Fabric/components/unimplementedview (= 0.74.2) - React-Fabric/components/view (= 0.74.2) - React-graphics - - React-jsc - React-jsi - React-jsiexecutor - React-logger @@ -455,6 +473,7 @@ PODS: - DoubleConversion - fmt (= 9.1.0) - glog + - hermes-engine - RCT-Folly/Fabric (= 2024.01.01.00) - RCTRequired - RCTTypeSafety @@ -462,7 +481,6 @@ PODS: - React-cxxreact - React-debug - React-graphics - - React-jsc - React-jsi - React-jsiexecutor - React-logger @@ -474,6 +492,7 @@ PODS: - DoubleConversion - fmt (= 9.1.0) - glog + - hermes-engine - RCT-Folly/Fabric (= 2024.01.01.00) - RCTRequired - RCTTypeSafety @@ -481,7 +500,6 @@ PODS: - React-cxxreact - React-debug - React-graphics - - React-jsc - React-jsi - React-jsiexecutor - React-logger @@ -493,6 +511,7 @@ PODS: - DoubleConversion - fmt (= 9.1.0) - glog + - hermes-engine - RCT-Folly/Fabric (= 2024.01.01.00) - RCTRequired - RCTTypeSafety @@ -500,7 +519,6 @@ PODS: - React-cxxreact - React-debug - React-graphics - - React-jsc - React-jsi - React-jsiexecutor - React-logger @@ -512,6 +530,7 @@ PODS: - DoubleConversion - fmt (= 9.1.0) - glog + - hermes-engine - RCT-Folly/Fabric (= 2024.01.01.00) - RCTRequired - RCTTypeSafety @@ -519,7 +538,6 @@ PODS: - React-cxxreact - React-debug - React-graphics - - React-jsc - React-jsi - React-jsiexecutor - React-logger @@ -531,6 +549,7 @@ PODS: - DoubleConversion - fmt (= 9.1.0) - glog + - hermes-engine - RCT-Folly/Fabric (= 2024.01.01.00) - RCTRequired - RCTTypeSafety @@ -538,7 +557,6 @@ PODS: - React-cxxreact - React-debug - React-graphics - - React-jsc - React-jsi - React-jsiexecutor - React-logger @@ -550,6 +568,7 @@ PODS: - DoubleConversion - fmt (= 9.1.0) - glog + - hermes-engine - RCT-Folly/Fabric (= 2024.01.01.00) - RCTRequired - RCTTypeSafety @@ -557,7 +576,6 @@ PODS: - React-cxxreact - React-debug - React-graphics - - React-jsc - React-jsi - React-jsiexecutor - React-logger @@ -569,6 +587,7 @@ PODS: - DoubleConversion - fmt (= 9.1.0) - glog + - hermes-engine - RCT-Folly/Fabric (= 2024.01.01.00) - RCTRequired - RCTTypeSafety @@ -576,7 +595,6 @@ PODS: - React-cxxreact - React-debug - React-graphics - - React-jsc - React-jsi - React-jsiexecutor - React-logger @@ -588,6 +606,7 @@ PODS: - DoubleConversion - fmt (= 9.1.0) - glog + - hermes-engine - RCT-Folly/Fabric (= 2024.01.01.00) - RCTRequired - RCTTypeSafety @@ -595,7 +614,6 @@ PODS: - React-cxxreact - React-debug - React-graphics - - React-jsc - React-jsi - React-jsiexecutor - React-logger @@ -607,6 +625,7 @@ PODS: - DoubleConversion - fmt (= 9.1.0) - glog + - hermes-engine - RCT-Folly/Fabric (= 2024.01.01.00) - RCTRequired - RCTTypeSafety @@ -614,7 +633,6 @@ PODS: - React-cxxreact - React-debug - React-graphics - - React-jsc - React-jsi - React-jsiexecutor - React-logger @@ -626,6 +644,7 @@ PODS: - DoubleConversion - fmt (= 9.1.0) - glog + - hermes-engine - RCT-Folly/Fabric (= 2024.01.01.00) - RCTRequired - RCTTypeSafety @@ -633,7 +652,6 @@ PODS: - React-cxxreact - React-debug - React-graphics - - React-jsc - React-jsi - React-jsiexecutor - React-logger @@ -645,6 +663,7 @@ PODS: - DoubleConversion - fmt (= 9.1.0) - glog + - hermes-engine - RCT-Folly/Fabric (= 2024.01.01.00) - RCTRequired - RCTTypeSafety @@ -652,7 +671,6 @@ PODS: - React-cxxreact - React-debug - React-graphics - - React-jsc - React-jsi - React-jsiexecutor - React-logger @@ -665,6 +683,7 @@ PODS: - DoubleConversion - fmt (= 9.1.0) - glog + - hermes-engine - RCT-Folly/Fabric (= 2024.01.01.00) - RCTRequired - RCTTypeSafety @@ -672,7 +691,6 @@ PODS: - React-cxxreact - React-debug - React-graphics - - React-jsc - React-jsi - React-jsiexecutor - React-logger @@ -684,6 +702,7 @@ PODS: - DoubleConversion - fmt (= 9.1.0) - glog + - hermes-engine - RCT-Folly/Fabric (= 2024.01.01.00) - RCTRequired - RCTTypeSafety @@ -691,7 +710,6 @@ PODS: - React-cxxreact - React-debug - React-graphics - - React-jsc - React-jsi - React-jsiexecutor - React-logger @@ -703,6 +721,7 @@ PODS: - DoubleConversion - fmt (= 9.1.0) - glog + - hermes-engine - RCT-Folly/Fabric (= 2024.01.01.00) - RCTRequired - RCTTypeSafety @@ -710,7 +729,6 @@ PODS: - React-cxxreact - React-debug - React-graphics - - React-jsc - React-jsi - React-jsiexecutor - React-logger @@ -722,6 +740,7 @@ PODS: - DoubleConversion - fmt (= 9.1.0) - glog + - hermes-engine - RCT-Folly/Fabric (= 2024.01.01.00) - RCTRequired - RCTTypeSafety @@ -729,7 +748,6 @@ PODS: - React-cxxreact - React-debug - React-graphics - - React-jsc - React-jsi - React-jsiexecutor - React-logger @@ -741,6 +759,7 @@ PODS: - DoubleConversion - fmt (= 9.1.0) - glog + - hermes-engine - RCT-Folly/Fabric (= 2024.01.01.00) - RCTRequired - RCTTypeSafety @@ -748,7 +767,6 @@ PODS: - React-cxxreact - React-debug - React-graphics - - React-jsc - React-jsi - React-jsiexecutor - React-logger @@ -760,6 +778,7 @@ PODS: - DoubleConversion - fmt (= 9.1.0) - glog + - hermes-engine - RCT-Folly/Fabric (= 2024.01.01.00) - RCTRequired - RCTTypeSafety @@ -767,7 +786,6 @@ PODS: - React-cxxreact - React-debug - React-graphics - - React-jsc - React-jsi - React-jsiexecutor - React-logger @@ -779,6 +797,7 @@ PODS: - DoubleConversion - fmt (= 9.1.0) - glog + - hermes-engine - RCT-Folly/Fabric (= 2024.01.01.00) - RCTRequired - RCTTypeSafety @@ -786,7 +805,6 @@ PODS: - React-cxxreact - React-debug - React-graphics - - React-jsc - React-jsi - React-jsiexecutor - React-logger @@ -798,6 +816,7 @@ PODS: - DoubleConversion - fmt (= 9.1.0) - glog + - hermes-engine - RCT-Folly/Fabric (= 2024.01.01.00) - RCTRequired - RCTTypeSafety @@ -806,7 +825,6 @@ PODS: - React-debug - React-Fabric/uimanager - React-graphics - - React-jsc - React-jsi - React-jsiexecutor - React-logger @@ -818,6 +836,7 @@ PODS: - DoubleConversion - fmt (= 9.1.0) - glog + - hermes-engine - RCT-Folly/Fabric (= 2024.01.01.00) - RCTRequired - RCTTypeSafety @@ -825,7 +844,6 @@ PODS: - React-cxxreact - React-debug - React-graphics - - React-jsc - React-jsi - React-jsiexecutor - React-logger @@ -837,13 +855,13 @@ PODS: - DoubleConversion - fmt (= 9.1.0) - glog + - hermes-engine - RCT-Folly/Fabric (= 2024.01.01.00) - RCTRequired (= 0.74.2) - RCTTypeSafety (= 0.74.2) - React-Fabric - React-graphics - React-ImageManager - - React-jsc - React-jsi - React-jsiexecutor (= 0.74.2) - React-logger @@ -859,6 +877,18 @@ PODS: - RCT-Folly/Fabric (= 2024.01.01.00) - React-Core/Default (= 0.74.2) - React-utils + - React-hermes (0.74.2): + - DoubleConversion + - fmt (= 9.1.0) + - glog + - hermes-engine + - RCT-Folly (= 2024.01.01.00) + - React-cxxreact (= 0.74.2) + - React-jsi + - React-jsiexecutor (= 0.74.2) + - React-jsinspector + - React-perflogger (= 0.74.2) + - React-runtimeexecutor - React-ImageManager (0.74.2): - glog - RCT-Folly/Fabric @@ -868,11 +898,6 @@ PODS: - React-graphics - React-rendererdebug - React-utils - - React-jsc (0.74.2): - - React-jsc/Fabric (= 0.74.2) - - React-jsi (= 0.74.2) - - React-jsc/Fabric (0.74.2): - - React-jsi (= 0.74.2) - React-jserrorhandler (0.74.2): - RCT-Folly/Fabric (= 2024.01.01.00) - React-debug @@ -883,11 +908,13 @@ PODS: - DoubleConversion - fmt (= 9.1.0) - glog + - hermes-engine - RCT-Folly (= 2024.01.01.00) - React-jsiexecutor (0.74.2): - DoubleConversion - fmt (= 9.1.0) - glog + - hermes-engine - RCT-Folly (= 2024.01.01.00) - React-cxxreact (= 0.74.2) - React-jsi (= 0.74.2) @@ -896,6 +923,7 @@ PODS: - React-jsinspector (0.74.2): - DoubleConversion - glog + - hermes-engine - RCT-Folly (= 2024.01.01.00) - React-featureflags - React-jsi @@ -912,6 +940,7 @@ PODS: - react-native-wgpu (0.1.20): - DoubleConversion - glog + - hermes-engine - RCT-Folly (= 2024.01.01.00) - RCTRequired - RCTTypeSafety @@ -922,7 +951,6 @@ PODS: - React-featureflags - React-graphics - React-ImageManager - - React-jsi - React-NativeModulesApple - React-RCTFabric - React-rendererdebug @@ -933,10 +961,10 @@ PODS: - React-nativeconfig (0.74.2) - React-NativeModulesApple (0.74.2): - glog + - hermes-engine - React-callinvoker - React-Core - React-cxxreact - - React-jsc - React-jsi - React-jsinspector - React-runtimeexecutor @@ -964,7 +992,7 @@ PODS: - React-Fabric - React-featureflags - React-graphics - - React-jsc + - React-hermes - React-nativeconfig - React-NativeModulesApple - React-RCTFabric @@ -973,12 +1001,14 @@ PODS: - React-rendererdebug - React-RuntimeApple - React-RuntimeCore + - React-RuntimeHermes - React-runtimescheduler - React-utils - ReactCommon - React-RCTBlob (0.74.2): - DoubleConversion - fmt (= 9.1.0) + - hermes-engine - RCT-Folly (= 2024.01.01.00) - React-Codegen - React-Core/RCTBlobHeaders @@ -990,6 +1020,7 @@ PODS: - ReactCommon - React-RCTFabric (0.74.2): - glog + - hermes-engine - RCT-Folly/Fabric (= 2024.01.01.00) - React-Core - React-debug @@ -998,7 +1029,6 @@ PODS: - React-featureflags - React-graphics - React-ImageManager - - React-jsc - React-jsi - React-jsinspector - React-nativeconfig @@ -1057,12 +1087,12 @@ PODS: - React-debug - React-rncore (0.74.2) - React-RuntimeApple (0.74.2): + - hermes-engine - RCT-Folly/Fabric (= 2024.01.01.00) - React-callinvoker - React-Core/Default - React-CoreModules - React-cxxreact - - React-jsc - React-jserrorhandler - React-jsi - React-jsiexecutor @@ -1072,13 +1102,14 @@ PODS: - React-RCTFabric - React-RuntimeCore - React-runtimeexecutor + - React-RuntimeHermes - React-utils - React-RuntimeCore (0.74.2): - glog + - hermes-engine - RCT-Folly/Fabric (= 2024.01.01.00) - React-cxxreact - React-featureflags - - React-jsc - React-jserrorhandler - React-jsi - React-jsiexecutor @@ -1088,23 +1119,34 @@ PODS: - React-utils - React-runtimeexecutor (0.74.2): - React-jsi (= 0.74.2) + - React-RuntimeHermes (0.74.2): + - hermes-engine + - RCT-Folly/Fabric (= 2024.01.01.00) + - React-featureflags + - React-hermes + - React-jsi + - React-jsinspector + - React-jsitracing + - React-nativeconfig + - React-RuntimeCore + - React-utils - React-runtimescheduler (0.74.2): - glog + - hermes-engine - RCT-Folly (= 2024.01.01.00) - React-callinvoker - React-cxxreact - React-debug - React-featureflags - - React-jsc - React-jsi - React-rendererdebug - React-runtimeexecutor - React-utils - React-utils (0.74.2): - glog + - hermes-engine - RCT-Folly (= 2024.01.01.00) - React-debug - - React-jsc - React-jsi (= 0.74.2) - ReactCommon (0.74.2): - ReactCommon/turbomodule (= 0.74.2) @@ -1112,6 +1154,7 @@ PODS: - DoubleConversion - fmt (= 9.1.0) - glog + - hermes-engine - RCT-Folly (= 2024.01.01.00) - React-callinvoker (= 0.74.2) - React-cxxreact (= 0.74.2) @@ -1124,6 +1167,7 @@ PODS: - DoubleConversion - fmt (= 9.1.0) - glog + - hermes-engine - RCT-Folly (= 2024.01.01.00) - React-callinvoker (= 0.74.2) - React-cxxreact (= 0.74.2) @@ -1134,6 +1178,7 @@ PODS: - DoubleConversion - fmt (= 9.1.0) - glog + - hermes-engine - RCT-Folly (= 2024.01.01.00) - React-callinvoker (= 0.74.2) - React-cxxreact (= 0.74.2) @@ -1145,6 +1190,7 @@ PODS: - ReactNativeHost (0.5.0): - DoubleConversion - glog + - hermes-engine - RCT-Folly (= 2024.01.01.00) - RCTRequired - RCTTypeSafety @@ -1156,7 +1202,6 @@ PODS: - React-featureflags - React-graphics - React-ImageManager - - React-jsi - React-NativeModulesApple - React-RCTFabric - React-rendererdebug @@ -1171,6 +1216,7 @@ PODS: - RNGestureHandler (2.19.0): - DoubleConversion - glog + - hermes-engine - RCT-Folly (= 2024.01.01.00) - RCTRequired - RCTTypeSafety @@ -1181,7 +1227,6 @@ PODS: - React-featureflags - React-graphics - React-ImageManager - - React-jsi - React-NativeModulesApple - React-RCTFabric - React-rendererdebug @@ -1192,6 +1237,7 @@ PODS: - RNReanimated (3.15.2): - DoubleConversion - glog + - hermes-engine - RCT-Folly (= 2024.01.01.00) - RCTRequired - RCTTypeSafety @@ -1202,7 +1248,6 @@ PODS: - React-featureflags - React-graphics - React-ImageManager - - React-jsi - React-NativeModulesApple - React-RCTFabric - React-rendererdebug @@ -1215,6 +1260,7 @@ PODS: - RNReanimated/reanimated (3.15.2): - DoubleConversion - glog + - hermes-engine - RCT-Folly (= 2024.01.01.00) - RCTRequired - RCTTypeSafety @@ -1225,7 +1271,6 @@ PODS: - React-featureflags - React-graphics - React-ImageManager - - React-jsi - React-NativeModulesApple - React-RCTFabric - React-rendererdebug @@ -1236,6 +1281,7 @@ PODS: - RNReanimated/worklets (3.15.2): - DoubleConversion - glog + - hermes-engine - RCT-Folly (= 2024.01.01.00) - RCTRequired - RCTTypeSafety @@ -1246,7 +1292,6 @@ PODS: - React-featureflags - React-graphics - React-ImageManager - - React-jsi - React-NativeModulesApple - React-RCTFabric - React-rendererdebug @@ -1263,6 +1308,7 @@ DEPENDENCIES: - FBLazyVector (from `../../../node_modules/react-native/Libraries/FBLazyVector`) - fmt (from `../../../node_modules/react-native/third-party-podspecs/fmt.podspec`) - glog (from `../../../node_modules/react-native/third-party-podspecs/glog.podspec`) + - hermes-engine (from `../../../node_modules/react-native/sdks/hermes-engine/hermes-engine.podspec`) - RCT-Folly (from `../../../node_modules/react-native/third-party-podspecs/RCT-Folly.podspec`) - RCT-Folly/Fabric (from `../../../node_modules/react-native/third-party-podspecs/RCT-Folly.podspec`) - RCTDeprecation (from `../../../node_modules/react-native/ReactApple/Libraries/RCTFoundation/RCTDeprecation`) @@ -1280,8 +1326,8 @@ DEPENDENCIES: - React-FabricImage (from `../../../node_modules/react-native/ReactCommon`) - React-featureflags (from `../../../node_modules/react-native/ReactCommon/react/featureflags`) - React-graphics (from `../../../node_modules/react-native/ReactCommon/react/renderer/graphics`) + - React-hermes (from `../../../node_modules/react-native/ReactCommon/hermes`) - React-ImageManager (from `../../../node_modules/react-native/ReactCommon/react/renderer/imagemanager/platform/ios`) - - React-jsc (from `../../../node_modules/react-native/ReactCommon/jsc`) - React-jserrorhandler (from `../../../node_modules/react-native/ReactCommon/jserrorhandler`) - React-jsi (from `../../../node_modules/react-native/ReactCommon/jsi`) - React-jsiexecutor (from `../../../node_modules/react-native/ReactCommon/jsiexecutor`) @@ -1310,6 +1356,7 @@ DEPENDENCIES: - React-RuntimeApple (from `../../../node_modules/react-native/ReactCommon/react/runtime/platform/ios`) - React-RuntimeCore (from `../../../node_modules/react-native/ReactCommon/react/runtime`) - React-runtimeexecutor (from `../../../node_modules/react-native/ReactCommon/runtimeexecutor`) + - React-RuntimeHermes (from `../../../node_modules/react-native/ReactCommon/react/runtime`) - React-runtimescheduler (from `../../../node_modules/react-native/ReactCommon/react/renderer/runtimescheduler`) - React-utils (from `../../../node_modules/react-native/ReactCommon/react/utils`) - ReactCommon/turbomodule/core (from `../../../node_modules/react-native/ReactCommon`) @@ -1335,6 +1382,9 @@ EXTERNAL SOURCES: :podspec: "../../../node_modules/react-native/third-party-podspecs/fmt.podspec" glog: :podspec: "../../../node_modules/react-native/third-party-podspecs/glog.podspec" + hermes-engine: + :podspec: "../../../node_modules/react-native/sdks/hermes-engine/hermes-engine.podspec" + :tag: hermes-2024-06-03-RNv0.74.2-bb1e74fe1e95c2b5a2f4f9311152da052badc2bc RCT-Folly: :podspec: "../../../node_modules/react-native/third-party-podspecs/RCT-Folly.podspec" RCTDeprecation: @@ -1365,10 +1415,10 @@ EXTERNAL SOURCES: :path: "../../../node_modules/react-native/ReactCommon/react/featureflags" React-graphics: :path: "../../../node_modules/react-native/ReactCommon/react/renderer/graphics" + React-hermes: + :path: "../../../node_modules/react-native/ReactCommon/hermes" React-ImageManager: :path: "../../../node_modules/react-native/ReactCommon/react/renderer/imagemanager/platform/ios" - React-jsc: - :path: "../../../node_modules/react-native/ReactCommon/jsc" React-jserrorhandler: :path: "../../../node_modules/react-native/ReactCommon/jserrorhandler" React-jsi: @@ -1425,6 +1475,8 @@ EXTERNAL SOURCES: :path: "../../../node_modules/react-native/ReactCommon/react/runtime" React-runtimeexecutor: :path: "../../../node_modules/react-native/ReactCommon/runtimeexecutor" + React-RuntimeHermes: + :path: "../../../node_modules/react-native/ReactCommon/react/runtime" React-runtimescheduler: :path: "../../../node_modules/react-native/ReactCommon/react/renderer/runtimescheduler" React-utils: @@ -1450,40 +1502,41 @@ SPEC CHECKSUMS: FBLazyVector: 4bc164e5b5e6cfc288d2b5ff28643ea15fa1a589 fmt: 4c2741a687cc09f0634a2e2c72a838b99f1ff120 glog: fdfdfe5479092de0c4bdbebedd9056951f092c4f + hermes-engine: 01d3e052018c2a13937aca1860fbedbccd4a41b7 RCT-Folly: 02617c592a293bd6d418e0a88ff4ee1f88329b47 RCTDeprecation: b03c35057846b685b3ccadc9bfe43e349989cdb2 RCTRequired: 194626909cfa8d39ca6663138c417bc6c431648c RCTTypeSafety: 552aff5b8e8341660594db00e53ac889682bc120 React: a57fe42044fe6ed3e828f8867ce070a6c5872754 React-callinvoker: 6bedefb354a8848b534752417954caa3a5cf34f9 - React-Codegen: 6d86d54bbe76e2bb648ed9a55f7cef62d4ef001a - React-Core: 5c1d9ccec3980f94f22d9d5d1cd185d87c89cfcf + React-Codegen: ed6f073979a0c97091bbff1e02f0fb7d03b7d370 + React-Core: 289ee3dfc1639bb9058c1e77427bb48169c26d7a React-CoreModules: eda5ce541a1f552158317abd90a5a0f6a4f8d6f7 - React-cxxreact: e7b005ccfe69a961ef2c6f8890409e5525844074 + React-cxxreact: 56bd17ccc6d4248116f7f95884ddb8c528379fb6 React-debug: 164b8e302404d92d4bec39778a5e03bcb1b6eb08 - React-Fabric: 813eb5fd4710af23d5c0448e30390e6fd19a7441 - React-FabricImage: 4592a0dede0b033c3c19d313d770c09c6d5481fa + React-Fabric: 05620c36074e3ab397dd8f9db0deb6d3c38b4efa + React-FabricImage: 2a8a7f5729f5c44e32e6f58f7225ee1017ed0704 React-featureflags: d97a6393993052e951e16a3b81206e22110be8d2 React-graphics: ef07d701f4eb72ae6fca6ed0a7260a04f2a58dec + React-hermes: 6ccc301ababfa17a9aad25a7e33faf325fd024b4 React-ImageManager: 00404bfe122626bc6493621f2a31ce802115a9b3 - React-jsc: e70a1bb9f4336280fc7b225a01f3d43435713d4f React-jserrorhandler: 5e2632590a84363855b2083e6b3d501e93bc3f04 - React-jsi: d254f10d6109d5a63610c71fc87fbf5d8460729e - React-jsiexecutor: f8a1e251674b9e71dfeb290680a02cd12f7ed1bf - React-jsinspector: 38e2c05ab66e647ac752cb5604f39b5c31171e84 + React-jsi: 828703c235f4eea1647897ee8030efdc6e8e9f14 + React-jsiexecutor: 713d7bbef0a410cee5b3b78f73ed1fc16e177ba7 + React-jsinspector: e1fa5325a47f34645195c63e3312ddb6a2efef5d React-jsitracing: 0fa7f78d8fdda794667cb2e6f19c874c1cf31d7e React-logger: 29fa3e048f5f67fe396bc08af7606426d9bd7b5d React-Mapbuffer: bf56147c9775491e53122a94c423ac201417e326 react-native-safe-area-context: 851c62c48dce80ccaa5637b6aa5991a1bc36eca9 - react-native-wgpu: fc6eeb8096c12fd0f9034e5d190764d5f5f62ecd + react-native-wgpu: 078c84b159de97704f8926011b0f9bc96591d478 React-nativeconfig: 9f223cd321823afdecf59ed00861ab2d69ee0fc1 - React-NativeModulesApple: aa472b3b953e3aceeb45d82e86e03dee776d01de + React-NativeModulesApple: ff7efaff7098639db5631236cfd91d60abff04c0 React-perflogger: 32ed45d9cee02cf6639acae34251590dccd30994 React-RCTActionSheet: 19f967ddaea258182b56ef11437133b056ba2adf React-RCTAnimation: d7f4137fc44a08bba465267ea7cb1dbdb7c4ec87 - React-RCTAppDelegate: 7ec8c3fad90193071b31e0f52278f0339624924e - React-RCTBlob: 1f9cb31b2331c7ac97f0d5ce3ab626f990edfd43 - React-RCTFabric: 04765e952b8520461f16dfda2fb4a713e04c7670 + React-RCTAppDelegate: 2b3f4d8009796af209a0d496e73276b743acee08 + React-RCTBlob: c6c3e1e0251700b7bea036b893913f22e2b9cb47 + React-RCTFabric: 93a3ea55169d19294f07092013c1c9ea7a015c9b React-RCTImage: 40528ab74a4fef0f0e2ee797a074b26d120b6cc6 React-RCTLinking: 385b5beb96749aae9ae1606746e883e1c9f8a6a7 React-RCTNetwork: ffc9f05bd8fa5b3bce562199ba41235ad0af645c @@ -1492,20 +1545,21 @@ SPEC CHECKSUMS: React-RCTVibration: e4ccf673579d0d94a96b3a0b64492db08f8324d5 React-rendererdebug: ac70f40de137ce7bdbc55eaee60c467a215d9923 React-rncore: edfff7a3f7f82ca1e0ba26978c6d84c7a8970dac - React-RuntimeApple: 5e2eaa6dff2ba15a0a5ceff8c15ab692e303d68b - React-RuntimeCore: f30a5d38677d9fb38875f5efe4b9bbd79f6797ac + React-RuntimeApple: a0c98b75571aa5f44ddc7c6e9fd55803fa4db00f + React-RuntimeCore: 4b8db1fe2f3f4a3a5ecb22e1a419824e3e2cd7ef React-runtimeexecutor: 5961acc7a77b69f964e1645a5d6069e124ce6b37 - React-runtimescheduler: d2f61c6fd47401f52d220cff22791db1ac7053e0 - React-utils: a4de2ebe8d96dfafd27ffd7c131e7ddca3a386e4 - ReactCommon: f75776d715a088ecb67041eaf50ccdb7e94b245c - ReactNativeHost: dce645b2e9026bb18485762c2599509ead34447b + React-RuntimeHermes: c5825bfae4815fdf4e9e639340c3a986a491884c + React-runtimescheduler: 56b642bf605ba5afa500d35790928fc1d51565ad + React-utils: 4476b7fcbbd95cfd002f3e778616155241d86e31 + ReactCommon: ecad995f26e0d1e24061f60f4e5d74782f003f12 + ReactNativeHost: 76fb17eac13a9a2200f659deffc91c054731a7e2 ReactTestApp-DevSupport: 690d06567b7ecae4f2f98dff5e4881c8d25be8e2 ReactTestApp-Resources: 857244f3a23f2b3157b364fa06cf3e8866deff9c - RNGestureHandler: 7f8d8a2c7361ab9326f28ad42e9d9843cba98778 - RNReanimated: 2dc2ba731bf4a3fd3bba2e3c598951c5aa4c432f + RNGestureHandler: 67e78f16895947f7e57ab91e75e914d3e9ef7239 + RNReanimated: 4c72fc2c0f4c6a9c36932e653cd68e4521b6c4ac SocketRocket: abac6f5de4d4d62d24e11868d7a2f427e0ef940d Yoga: ae3c32c514802d30f687a04a6a35b348506d411f -PODFILE CHECKSUM: e27e9a1602b03e96ffc2d6e45e4fa7daec1a754e +PODFILE CHECKSUM: 21851af42ab33f67696cf6469097d79f1cc68181 COCOAPODS: 1.15.2 diff --git a/apps/example/package.json b/apps/example/package.json index 81d042f17..e6887e291 100644 --- a/apps/example/package.json +++ b/apps/example/package.json @@ -22,6 +22,7 @@ "@react-three/fiber": "^8.17.6", "@tensorflow/tfjs": "^4.22.0", "@tensorflow/tfjs-backend-webgpu": "^4.22.0", + "@tensorflow/tfjs-vis": "^1.5.1", "babel-plugin-transform-import-meta": "^2.2.1", "fast-text-encoding": "^1.0.6", "react": "18.2.0", diff --git a/apps/example/src/TransformerJS/TransformerJS.tsx b/apps/example/src/TransformerJS/TransformerJS.tsx index 9707a8448..cdcb0de3d 100644 --- a/apps/example/src/TransformerJS/TransformerJS.tsx +++ b/apps/example/src/TransformerJS/TransformerJS.tsx @@ -1,41 +1,98 @@ -// @ts-nocheck - import { View } from "react-native"; -// Import @tensorflow/tfjs or @tensorflow/tfjs-core +// Import TensorFlow.js and required modules import * as tf from "@tensorflow/tfjs"; -// Add the WebGPU backend to the global backend registry. import "@tensorflow/tfjs-backend-webgpu"; -// Set the backend to WebGPU and wait for the module to be ready. -tf.setBackend("webgpu").then(() => console.log("WebGPU backend is ready")); -// Listen for messages from the main thread -// self.addEventListener("message", async (e) => { -// const { type, data } = e.data; - -// switch (type) { -// case "check": -// check(); -// break; - -// case "load": -// load(); -// break; - -// case "generate": -// stopping_criteria.reset(); -// generate(data); -// break; - -// case "interrupt": -// stopping_criteria.interrupt(); -// break; - -// case "reset": -// // past_key_values_cache = null; -// stopping_criteria.reset(); -// break; -// } -// }); +//import * as tfvis from "@tensorflow/tfjs-vis"; + +(async () => { + // Set the backend to WebGPU + const result = await tf.setBackend("webgpu"); + console.log("WebGPU backend is ready: " + result); + + // // Load and preprocess the Fashion MNIST dataset + // const loadFashionMnist = async () => { + // const mnist = await tf.data.csv( + // "https://storage.googleapis.com/tfjs-examples/mnist-data/mnist_test.csv", + // { columnConfigs: { label: { isLabel: true } } }, + // ); + + // const NUM_CLASSES = 10; + // const DATA_SIZE = 70000; + + // // Split features and labels, normalize feature values + // const processedData = mnist.map(({ xs, ys }) => { + // const labels = tf.oneHot(tf.tensor1d([ys.label], "int32"), NUM_CLASSES); + // const images = tf.tensor2d(Object.values(xs), [1, 28 * 28]).div(255); + // return { xs: images, ys: labels }; + // }); + + // const split = DATA_SIZE * 0.8; + // return { + // trainData: processedData.take(split), + // testData: processedData.skip(split), + // }; + // }; + + // // Define a simple CNN model + // const createModel = () => { + // const model = tf.sequential(); + + // // Add layers + // model.add( + // tf.layers.conv2d({ + // inputShape: [28, 28, 1], + // kernelSize: 3, + // filters: 16, + // activation: "relu", + // }), + // ); + // model.add(tf.layers.maxPooling2d({ poolSize: [2, 2] })); + // model.add(tf.layers.flatten()); + // model.add(tf.layers.dense({ units: 128, activation: "relu" })); + // model.add(tf.layers.dense({ units: 10, activation: "softmax" })); + + // model.compile({ + // optimizer: tf.train.adam(), + // loss: "categoricalCrossentropy", + // metrics: ["accuracy"], + // }); + + // return model; + // }; + + // // Train and evaluate the model + // const trainModel = async (model, trainData, testData) => { + // const BATCH_SIZE = 32; + // const EPOCHS = 5; + + // const trainXs = trainData.map(({ xs }) => xs); + // const trainYs = trainData.map(({ ys }) => ys); + + // const testXs = testData.map(({ xs }) => xs); + // const testYs = testData.map(({ ys }) => ys); + + // await model.fit(trainXs, trainYs, { + // batchSize: BATCH_SIZE, + // epochs: EPOCHS, + // validationData: [testXs, testYs], + // callbacks: tfvis.show.fitCallbacks( + // { name: "Training Performance" }, + // ["loss", "val_loss", "accuracy", "val_accuracy"], + // { height: 200, callbacks: ["onEpochEnd"] }, + // ), + // }); + + // const testResult = model.evaluate(testXs, testYs); + // console.log("Test Accuracy:", testResult[1].dataSync()[0]); + // }; + + // (async () => { + // const { trainData, testData } = await loadFashionMnist(); + // const model = createModel(); + // await trainModel(model, trainData, testData); + // })(); +})(); export const TransformerJS = () => { return ; diff --git a/apps/example/src/TransformerJS/runtime.js b/apps/example/src/TransformerJS/runtime.js deleted file mode 100644 index 84fb9e48a..000000000 --- a/apps/example/src/TransformerJS/runtime.js +++ /dev/null @@ -1,28 +0,0 @@ -var __defProp = Object.defineProperty; -var __getOwnPropNames = Object.getOwnPropertyNames; -var __typeError = (msg) => { - throw TypeError(msg); -}; -var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; -var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, { - get: (a, b) => (typeof require !== "undefined" ? require : a)[b] -}) : x)(function(x) { - if (typeof require !== "undefined") return require.apply(this, arguments); - throw Error('Dynamic require of "' + x + '" is not supported'); -}); -var __commonJS = (cb, mod) => function __require2() { - return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports; -}; -var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value); -var __accessCheck = (obj, member, msg) => member.has(obj) || __typeError("Cannot " + msg); -var __privateAdd = (obj, member, value) => member.has(obj) ? __typeError("Cannot add the same private member more than once") : member instanceof WeakSet ? member.add(obj) : member.set(obj, value); -var __privateMethod = (obj, member, method) => (__accessCheck(obj, member, "access private method"), method); - -export { - __require, - __commonJS, - __publicField, - __privateAdd, - __privateMethod -}; -//# sourceMappingURL=chunk-KZF2VKC5.js.map diff --git a/apps/example/src/TransformerJS/transformers.js b/apps/example/src/TransformerJS/transformers.js deleted file mode 100644 index 9e5f4586a..000000000 --- a/apps/example/src/TransformerJS/transformers.js +++ /dev/null @@ -1,37540 +0,0 @@ -import { - __publicField, - __require - } from "./runtime"; - -const META_URL = "http://localhost:5173/"; - - // node_modules/@huggingface/transformers/dist/transformers.js - var __webpack_modules__ = { - /***/ - "./node_modules/onnxruntime-web/dist/ort-wasm-simd-threaded.jsep.wasm": ( - /*!****************************************************************************!*\ - !*** ./node_modules/onnxruntime-web/dist/ort-wasm-simd-threaded.jsep.wasm ***! - \****************************************************************************/ - /***/ - (module, __unused_webpack_exports, __webpack_require__2) => { - module.exports = __webpack_require__2.p + "ort-wasm-simd-threaded.jsep.wasm"; - } - ), - /***/ - "?2ce3": ( - /*!**********************************!*\ - !*** onnxruntime-node (ignored) ***! - \**********************************/ - /***/ - () => { - } - ), - /***/ - "?7a2c": ( - /*!********************!*\ - !*** fs (ignored) ***! - \********************/ - /***/ - () => { - } - ), - /***/ - "?a42a": ( - /*!**********************!*\ - !*** path (ignored) ***! - \**********************/ - /***/ - () => { - } - ), - /***/ - "?2b25": ( - /*!***********************!*\ - !*** sharp (ignored) ***! - \***********************/ - /***/ - () => { - } - ), - /***/ - "?569f": ( - /*!********************!*\ - !*** fs (ignored) ***! - \********************/ - /***/ - () => { - } - ), - /***/ - "?3f59": ( - /*!**********************!*\ - !*** path (ignored) ***! - \**********************/ - /***/ - () => { - } - ), - /***/ - "?154a": ( - /*!*********************!*\ - !*** url (ignored) ***! - \*********************/ - /***/ - () => { - } - ), - /***/ - "./node_modules/@huggingface/jinja/dist/index.js": ( - /*!*******************************************************!*\ - !*** ./node_modules/@huggingface/jinja/dist/index.js ***! - \*******************************************************/ - /***/ - (__unused_webpack___webpack_module__, __webpack_exports__2, __webpack_require__2) => { - __webpack_require__2.r(__webpack_exports__2); - __webpack_require__2.d(__webpack_exports__2, { - /* harmony export */ - Environment: () => ( - /* binding */ - Environment - ), - /* harmony export */ - Interpreter: () => ( - /* binding */ - Interpreter - ), - /* harmony export */ - Template: () => ( - /* binding */ - Template - ), - /* harmony export */ - parse: () => ( - /* binding */ - parse - ), - /* harmony export */ - tokenize: () => ( - /* binding */ - tokenize - ) - /* harmony export */ - }); - var TOKEN_TYPES = Object.freeze({ - Text: "Text", - // The text between Jinja statements or expressions - NumericLiteral: "NumericLiteral", - // e.g., 123 - BooleanLiteral: "BooleanLiteral", - // true or false - StringLiteral: "StringLiteral", - // 'string' - Identifier: "Identifier", - // Variables, functions, etc. - Equals: "Equals", - // = - OpenParen: "OpenParen", - // ( - CloseParen: "CloseParen", - // ) - OpenStatement: "OpenStatement", - // {% - CloseStatement: "CloseStatement", - // %} - OpenExpression: "OpenExpression", - // {{ - CloseExpression: "CloseExpression", - // }} - OpenSquareBracket: "OpenSquareBracket", - // [ - CloseSquareBracket: "CloseSquareBracket", - // ] - OpenCurlyBracket: "OpenCurlyBracket", - // { - CloseCurlyBracket: "CloseCurlyBracket", - // } - Comma: "Comma", - // , - Dot: "Dot", - // . - Colon: "Colon", - // : - Pipe: "Pipe", - // | - CallOperator: "CallOperator", - // () - AdditiveBinaryOperator: "AdditiveBinaryOperator", - // + - - MultiplicativeBinaryOperator: "MultiplicativeBinaryOperator", - // * / % - ComparisonBinaryOperator: "ComparisonBinaryOperator", - // < > <= >= == != - UnaryOperator: "UnaryOperator", - // ! - + - // Keywords - Set: "Set", - If: "If", - For: "For", - In: "In", - Is: "Is", - NotIn: "NotIn", - Else: "Else", - EndIf: "EndIf", - ElseIf: "ElseIf", - EndFor: "EndFor", - And: "And", - Or: "Or", - Not: "UnaryOperator", - Macro: "Macro", - EndMacro: "EndMacro" - }); - var KEYWORDS = Object.freeze({ - set: TOKEN_TYPES.Set, - for: TOKEN_TYPES.For, - in: TOKEN_TYPES.In, - is: TOKEN_TYPES.Is, - if: TOKEN_TYPES.If, - else: TOKEN_TYPES.Else, - endif: TOKEN_TYPES.EndIf, - elif: TOKEN_TYPES.ElseIf, - endfor: TOKEN_TYPES.EndFor, - and: TOKEN_TYPES.And, - or: TOKEN_TYPES.Or, - not: TOKEN_TYPES.Not, - "not in": TOKEN_TYPES.NotIn, - macro: TOKEN_TYPES.Macro, - endmacro: TOKEN_TYPES.EndMacro, - // Literals - true: TOKEN_TYPES.BooleanLiteral, - false: TOKEN_TYPES.BooleanLiteral, - // NOTE: According to the Jinja docs: The special constants true, false, and none are indeed lowercase. - // Because that caused confusion in the past, (True used to expand to an undefined variable that was considered false), - // all three can now also be written in title case (True, False, and None). However, for consistency, (all Jinja identifiers are lowercase) - // you should use the lowercase versions. - True: TOKEN_TYPES.BooleanLiteral, - False: TOKEN_TYPES.BooleanLiteral - }); - var Token = class { - /** - * Constructs a new Token. - * @param {string} value The raw value as seen inside the source code. - * @param {TokenType} type The type of token. - */ - constructor(value, type) { - this.value = value; - this.type = type; - } - }; - function isWord(char) { - return /\w/.test(char); - } - function isInteger(char) { - return /[0-9]/.test(char); - } - var ORDERED_MAPPING_TABLE = [ - // Control sequences - ["{%", TOKEN_TYPES.OpenStatement], - ["%}", TOKEN_TYPES.CloseStatement], - ["{{", TOKEN_TYPES.OpenExpression], - ["}}", TOKEN_TYPES.CloseExpression], - // Single character tokens - ["(", TOKEN_TYPES.OpenParen], - [")", TOKEN_TYPES.CloseParen], - ["{", TOKEN_TYPES.OpenCurlyBracket], - ["}", TOKEN_TYPES.CloseCurlyBracket], - ["[", TOKEN_TYPES.OpenSquareBracket], - ["]", TOKEN_TYPES.CloseSquareBracket], - [",", TOKEN_TYPES.Comma], - [".", TOKEN_TYPES.Dot], - [":", TOKEN_TYPES.Colon], - ["|", TOKEN_TYPES.Pipe], - // Comparison operators - ["<=", TOKEN_TYPES.ComparisonBinaryOperator], - [">=", TOKEN_TYPES.ComparisonBinaryOperator], - ["==", TOKEN_TYPES.ComparisonBinaryOperator], - ["!=", TOKEN_TYPES.ComparisonBinaryOperator], - ["<", TOKEN_TYPES.ComparisonBinaryOperator], - [">", TOKEN_TYPES.ComparisonBinaryOperator], - // Arithmetic operators - ["+", TOKEN_TYPES.AdditiveBinaryOperator], - ["-", TOKEN_TYPES.AdditiveBinaryOperator], - ["*", TOKEN_TYPES.MultiplicativeBinaryOperator], - ["/", TOKEN_TYPES.MultiplicativeBinaryOperator], - ["%", TOKEN_TYPES.MultiplicativeBinaryOperator], - // Assignment operator - ["=", TOKEN_TYPES.Equals] - ]; - var ESCAPE_CHARACTERS = /* @__PURE__ */ new Map([ - ["n", "\n"], - // New line - ["t", " "], - // Horizontal tab - ["r", "\r"], - // Carriage return - ["b", "\b"], - // Backspace - ["f", "\f"], - // Form feed - ["v", "\v"], - // Vertical tab - ["'", "'"], - // Single quote - ['"', '"'], - // Double quote - ["\\", "\\"] - // Backslash - ]); - function preprocess(template, options = {}) { - if (template.endsWith("\n")) { - template = template.slice(0, -1); - } - template = template.replace(/{#.*?#}/gs, "{##}"); - if (options.lstrip_blocks) { - template = template.replace(/^[ \t]*({[#%])/gm, "$1"); - } - if (options.trim_blocks) { - template = template.replace(/([#%]})\n/g, "$1"); - } - return template.replace(/{##}/g, "").replace(/-%}\s*/g, "%}").replace(/\s*{%-/g, "{%").replace(/-}}\s*/g, "}}").replace(/\s*{{-/g, "{{"); - } - function tokenize(source, options = {}) { - var _a, _b, _c; - const tokens = []; - const src = preprocess(source, options); - let cursorPosition = 0; - const consumeWhile = (predicate) => { - let str = ""; - while (predicate(src[cursorPosition])) { - if (src[cursorPosition] === "\\") { - ++cursorPosition; - if (cursorPosition >= src.length) - throw new SyntaxError("Unexpected end of input"); - const escaped = src[cursorPosition++]; - const unescaped = ESCAPE_CHARACTERS.get(escaped); - if (unescaped === void 0) { - throw new SyntaxError(`Unexpected escaped character: ${escaped}`); - } - str += unescaped; - continue; - } - str += src[cursorPosition++]; - if (cursorPosition >= src.length) - throw new SyntaxError("Unexpected end of input"); - } - return str; - }; - main: - while (cursorPosition < src.length) { - const lastTokenType = (_a = tokens.at(-1)) == null ? void 0 : _a.type; - if (lastTokenType === void 0 || lastTokenType === TOKEN_TYPES.CloseStatement || lastTokenType === TOKEN_TYPES.CloseExpression) { - let text = ""; - while (cursorPosition < src.length && // Keep going until we hit the next Jinja statement or expression - !(src[cursorPosition] === "{" && (src[cursorPosition + 1] === "%" || src[cursorPosition + 1] === "{"))) { - text += src[cursorPosition++]; - } - if (text.length > 0) { - tokens.push(new Token(text, TOKEN_TYPES.Text)); - continue; - } - } - consumeWhile((char2) => /\s/.test(char2)); - const char = src[cursorPosition]; - if (char === "-" || char === "+") { - const lastTokenType2 = (_b = tokens.at(-1)) == null ? void 0 : _b.type; - if (lastTokenType2 === TOKEN_TYPES.Text || lastTokenType2 === void 0) { - throw new SyntaxError(`Unexpected character: ${char}`); - } - switch (lastTokenType2) { - case TOKEN_TYPES.Identifier: - case TOKEN_TYPES.NumericLiteral: - case TOKEN_TYPES.BooleanLiteral: - case TOKEN_TYPES.StringLiteral: - case TOKEN_TYPES.CloseParen: - case TOKEN_TYPES.CloseSquareBracket: - break; - default: { - ++cursorPosition; - const num = consumeWhile(isInteger); - tokens.push( - new Token(`${char}${num}`, num.length > 0 ? TOKEN_TYPES.NumericLiteral : TOKEN_TYPES.UnaryOperator) - ); - continue; - } - } - } - for (const [char2, token] of ORDERED_MAPPING_TABLE) { - const slice2 = src.slice(cursorPosition, cursorPosition + char2.length); - if (slice2 === char2) { - tokens.push(new Token(char2, token)); - cursorPosition += char2.length; - continue main; - } - } - if (char === "'" || char === '"') { - ++cursorPosition; - const str = consumeWhile((c) => c !== char); - tokens.push(new Token(str, TOKEN_TYPES.StringLiteral)); - ++cursorPosition; - continue; - } - if (isInteger(char)) { - const num = consumeWhile(isInteger); - tokens.push(new Token(num, TOKEN_TYPES.NumericLiteral)); - continue; - } - if (isWord(char)) { - const word = consumeWhile(isWord); - const type = Object.hasOwn(KEYWORDS, word) ? KEYWORDS[word] : TOKEN_TYPES.Identifier; - if (type === TOKEN_TYPES.In && ((_c = tokens.at(-1)) == null ? void 0 : _c.type) === TOKEN_TYPES.Not) { - tokens.pop(); - tokens.push(new Token("not in", TOKEN_TYPES.NotIn)); - } else { - tokens.push(new Token(word, type)); - } - continue; - } - throw new SyntaxError(`Unexpected character: ${char}`); - } - return tokens; - } - var Statement = class { - constructor() { - __publicField(this, "type", "Statement"); - } - }; - var Program = class extends Statement { - constructor(body) { - super(); - __publicField(this, "type", "Program"); - this.body = body; - } - }; - var If = class extends Statement { - constructor(test, body, alternate) { - super(); - __publicField(this, "type", "If"); - this.test = test; - this.body = body; - this.alternate = alternate; - } - }; - var For = class extends Statement { - constructor(loopvar, iterable, body, defaultBlock) { - super(); - __publicField(this, "type", "For"); - this.loopvar = loopvar; - this.iterable = iterable; - this.body = body; - this.defaultBlock = defaultBlock; - } - }; - var SetStatement = class extends Statement { - constructor(assignee, value) { - super(); - __publicField(this, "type", "Set"); - this.assignee = assignee; - this.value = value; - } - }; - var Macro = class extends Statement { - constructor(name, args, body) { - super(); - __publicField(this, "type", "Macro"); - this.name = name; - this.args = args; - this.body = body; - } - }; - var Expression = class extends Statement { - constructor() { - super(...arguments); - __publicField(this, "type", "Expression"); - } - }; - var MemberExpression = class extends Expression { - constructor(object, property, computed) { - super(); - __publicField(this, "type", "MemberExpression"); - this.object = object; - this.property = property; - this.computed = computed; - } - }; - var CallExpression = class extends Expression { - constructor(callee, args) { - super(); - __publicField(this, "type", "CallExpression"); - this.callee = callee; - this.args = args; - } - }; - var Identifier = class extends Expression { - /** - * @param {string} value The name of the identifier - */ - constructor(value) { - super(); - __publicField(this, "type", "Identifier"); - this.value = value; - } - }; - var Literal = class extends Expression { - constructor(value) { - super(); - __publicField(this, "type", "Literal"); - this.value = value; - } - }; - var NumericLiteral = class extends Literal { - constructor() { - super(...arguments); - __publicField(this, "type", "NumericLiteral"); - } - }; - var StringLiteral = class extends Literal { - constructor() { - super(...arguments); - __publicField(this, "type", "StringLiteral"); - } - }; - var BooleanLiteral = class extends Literal { - constructor() { - super(...arguments); - __publicField(this, "type", "BooleanLiteral"); - } - }; - var ArrayLiteral = class extends Literal { - constructor() { - super(...arguments); - __publicField(this, "type", "ArrayLiteral"); - } - }; - var TupleLiteral = class extends Literal { - constructor() { - super(...arguments); - __publicField(this, "type", "TupleLiteral"); - } - }; - var ObjectLiteral = class extends Literal { - constructor() { - super(...arguments); - __publicField(this, "type", "ObjectLiteral"); - } - }; - var BinaryExpression = class extends Expression { - constructor(operator, left, right) { - super(); - __publicField(this, "type", "BinaryExpression"); - this.operator = operator; - this.left = left; - this.right = right; - } - }; - var FilterExpression = class extends Expression { - constructor(operand, filter) { - super(); - __publicField(this, "type", "FilterExpression"); - this.operand = operand; - this.filter = filter; - } - }; - var SelectExpression = class extends Expression { - constructor(iterable, test) { - super(); - __publicField(this, "type", "SelectExpression"); - this.iterable = iterable; - this.test = test; - } - }; - var TestExpression = class extends Expression { - constructor(operand, negate, test) { - super(); - __publicField(this, "type", "TestExpression"); - this.operand = operand; - this.negate = negate; - this.test = test; - } - }; - var UnaryExpression = class extends Expression { - constructor(operator, argument) { - super(); - __publicField(this, "type", "UnaryExpression"); - this.operator = operator; - this.argument = argument; - } - }; - var SliceExpression = class extends Expression { - constructor(start = void 0, stop = void 0, step = void 0) { - super(); - __publicField(this, "type", "SliceExpression"); - this.start = start; - this.stop = stop; - this.step = step; - } - }; - var KeywordArgumentExpression = class extends Expression { - constructor(key, value) { - super(); - __publicField(this, "type", "KeywordArgumentExpression"); - this.key = key; - this.value = value; - } - }; - function parse(tokens) { - const program = new Program([]); - let current = 0; - function expect(type, error) { - const prev = tokens[current++]; - if (!prev || prev.type !== type) { - throw new Error(`Parser Error: ${error}. ${prev.type} !== ${type}.`); - } - return prev; - } - function parseAny() { - switch (tokens[current].type) { - case TOKEN_TYPES.Text: - return parseText(); - case TOKEN_TYPES.OpenStatement: - return parseJinjaStatement(); - case TOKEN_TYPES.OpenExpression: - return parseJinjaExpression(); - default: - throw new SyntaxError(`Unexpected token type: ${tokens[current].type}`); - } - } - function not(...types) { - return current + types.length <= tokens.length && types.some((type, i) => type !== tokens[current + i].type); - } - function is(...types) { - return current + types.length <= tokens.length && types.every((type, i) => type === tokens[current + i].type); - } - function parseText() { - return new StringLiteral(expect(TOKEN_TYPES.Text, "Expected text token").value); - } - function parseJinjaStatement() { - expect(TOKEN_TYPES.OpenStatement, "Expected opening statement token"); - let result; - switch (tokens[current].type) { - case TOKEN_TYPES.Set: - ++current; - result = parseSetStatement(); - expect(TOKEN_TYPES.CloseStatement, "Expected closing statement token"); - break; - case TOKEN_TYPES.If: - ++current; - result = parseIfStatement(); - expect(TOKEN_TYPES.OpenStatement, "Expected {% token"); - expect(TOKEN_TYPES.EndIf, "Expected endif token"); - expect(TOKEN_TYPES.CloseStatement, "Expected %} token"); - break; - case TOKEN_TYPES.Macro: - ++current; - result = parseMacroStatement(); - expect(TOKEN_TYPES.OpenStatement, "Expected {% token"); - expect(TOKEN_TYPES.EndMacro, "Expected endmacro token"); - expect(TOKEN_TYPES.CloseStatement, "Expected %} token"); - break; - case TOKEN_TYPES.For: - ++current; - result = parseForStatement(); - expect(TOKEN_TYPES.OpenStatement, "Expected {% token"); - expect(TOKEN_TYPES.EndFor, "Expected endfor token"); - expect(TOKEN_TYPES.CloseStatement, "Expected %} token"); - break; - default: - throw new SyntaxError(`Unknown statement type: ${tokens[current].type}`); - } - return result; - } - function parseJinjaExpression() { - expect(TOKEN_TYPES.OpenExpression, "Expected opening expression token"); - const result = parseExpression(); - expect(TOKEN_TYPES.CloseExpression, "Expected closing expression token"); - return result; - } - function parseSetStatement() { - const left = parseExpression(); - if (is(TOKEN_TYPES.Equals)) { - ++current; - const value = parseSetStatement(); - return new SetStatement(left, value); - } - return left; - } - function parseIfStatement() { - var _a, _b, _c, _d, _e, _f, _g, _h; - const test = parseExpression(); - expect(TOKEN_TYPES.CloseStatement, "Expected closing statement token"); - const body = []; - const alternate = []; - while (!(((_a = tokens[current]) == null ? void 0 : _a.type) === TOKEN_TYPES.OpenStatement && (((_b = tokens[current + 1]) == null ? void 0 : _b.type) === TOKEN_TYPES.ElseIf || ((_c = tokens[current + 1]) == null ? void 0 : _c.type) === TOKEN_TYPES.Else || ((_d = tokens[current + 1]) == null ? void 0 : _d.type) === TOKEN_TYPES.EndIf))) { - body.push(parseAny()); - } - if (((_e = tokens[current]) == null ? void 0 : _e.type) === TOKEN_TYPES.OpenStatement && ((_f = tokens[current + 1]) == null ? void 0 : _f.type) !== TOKEN_TYPES.EndIf) { - ++current; - if (is(TOKEN_TYPES.ElseIf)) { - expect(TOKEN_TYPES.ElseIf, "Expected elseif token"); - alternate.push(parseIfStatement()); - } else { - expect(TOKEN_TYPES.Else, "Expected else token"); - expect(TOKEN_TYPES.CloseStatement, "Expected closing statement token"); - while (!(((_g = tokens[current]) == null ? void 0 : _g.type) === TOKEN_TYPES.OpenStatement && ((_h = tokens[current + 1]) == null ? void 0 : _h.type) === TOKEN_TYPES.EndIf)) { - alternate.push(parseAny()); - } - } - } - return new If(test, body, alternate); - } - function parseMacroStatement() { - const name = parsePrimaryExpression(); - if (name.type !== "Identifier") { - throw new SyntaxError(`Expected identifier following macro statement`); - } - const args = parseArgs(); - expect(TOKEN_TYPES.CloseStatement, "Expected closing statement token"); - const body = []; - while (not(TOKEN_TYPES.OpenStatement, TOKEN_TYPES.EndMacro)) { - body.push(parseAny()); - } - return new Macro(name, args, body); - } - function parseExpressionSequence(primary = false) { - const fn = primary ? parsePrimaryExpression : parseExpression; - const expressions = [fn()]; - const isTuple = is(TOKEN_TYPES.Comma); - while (isTuple) { - ++current; - expressions.push(fn()); - if (!is(TOKEN_TYPES.Comma)) { - break; - } - } - return isTuple ? new TupleLiteral(expressions) : expressions[0]; - } - function parseForStatement() { - const loopVariable = parseExpressionSequence(true); - if (!(loopVariable instanceof Identifier || loopVariable instanceof TupleLiteral)) { - throw new SyntaxError(`Expected identifier/tuple for the loop variable, got ${loopVariable.type} instead`); - } - expect(TOKEN_TYPES.In, "Expected `in` keyword following loop variable"); - const iterable = parseExpression(); - expect(TOKEN_TYPES.CloseStatement, "Expected closing statement token"); - const body = []; - while (not(TOKEN_TYPES.OpenStatement, TOKEN_TYPES.EndFor) && not(TOKEN_TYPES.OpenStatement, TOKEN_TYPES.Else)) { - body.push(parseAny()); - } - const alternative = []; - if (is(TOKEN_TYPES.OpenStatement, TOKEN_TYPES.Else)) { - ++current; - ++current; - expect(TOKEN_TYPES.CloseStatement, "Expected closing statement token"); - while (not(TOKEN_TYPES.OpenStatement, TOKEN_TYPES.EndFor)) { - alternative.push(parseAny()); - } - } - return new For(loopVariable, iterable, body, alternative); - } - function parseExpression() { - return parseIfExpression(); - } - function parseIfExpression() { - const a = parseLogicalOrExpression(); - if (is(TOKEN_TYPES.If)) { - ++current; - const predicate = parseLogicalOrExpression(); - if (is(TOKEN_TYPES.Else)) { - ++current; - const b = parseLogicalOrExpression(); - return new If(predicate, [a], [b]); - } else { - return new SelectExpression(a, predicate); - } - } - return a; - } - function parseLogicalOrExpression() { - let left = parseLogicalAndExpression(); - while (is(TOKEN_TYPES.Or)) { - const operator = tokens[current]; - ++current; - const right = parseLogicalAndExpression(); - left = new BinaryExpression(operator, left, right); - } - return left; - } - function parseLogicalAndExpression() { - let left = parseLogicalNegationExpression(); - while (is(TOKEN_TYPES.And)) { - const operator = tokens[current]; - ++current; - const right = parseLogicalNegationExpression(); - left = new BinaryExpression(operator, left, right); - } - return left; - } - function parseLogicalNegationExpression() { - let right; - while (is(TOKEN_TYPES.Not)) { - const operator = tokens[current]; - ++current; - const arg = parseLogicalNegationExpression(); - right = new UnaryExpression(operator, arg); - } - return right ?? parseComparisonExpression(); - } - function parseComparisonExpression() { - let left = parseAdditiveExpression(); - while (is(TOKEN_TYPES.ComparisonBinaryOperator) || is(TOKEN_TYPES.In) || is(TOKEN_TYPES.NotIn)) { - const operator = tokens[current]; - ++current; - const right = parseAdditiveExpression(); - left = new BinaryExpression(operator, left, right); - } - return left; - } - function parseAdditiveExpression() { - let left = parseMultiplicativeExpression(); - while (is(TOKEN_TYPES.AdditiveBinaryOperator)) { - const operator = tokens[current]; - ++current; - const right = parseMultiplicativeExpression(); - left = new BinaryExpression(operator, left, right); - } - return left; - } - function parseCallMemberExpression() { - const member = parseMemberExpression(); - if (is(TOKEN_TYPES.OpenParen)) { - return parseCallExpression(member); - } - return member; - } - function parseCallExpression(callee) { - let callExpression = new CallExpression(callee, parseArgs()); - if (is(TOKEN_TYPES.OpenParen)) { - callExpression = parseCallExpression(callExpression); - } - return callExpression; - } - function parseArgs() { - expect(TOKEN_TYPES.OpenParen, "Expected opening parenthesis for arguments list"); - const args = parseArgumentsList(); - expect(TOKEN_TYPES.CloseParen, "Expected closing parenthesis for arguments list"); - return args; - } - function parseArgumentsList() { - const args = []; - while (!is(TOKEN_TYPES.CloseParen)) { - let argument = parseExpression(); - if (is(TOKEN_TYPES.Equals)) { - ++current; - if (!(argument instanceof Identifier)) { - throw new SyntaxError(`Expected identifier for keyword argument`); - } - const value = parseExpression(); - argument = new KeywordArgumentExpression(argument, value); - } - args.push(argument); - if (is(TOKEN_TYPES.Comma)) { - ++current; - } - } - return args; - } - function parseMemberExpressionArgumentsList() { - const slices = []; - let isSlice = false; - while (!is(TOKEN_TYPES.CloseSquareBracket)) { - if (is(TOKEN_TYPES.Colon)) { - slices.push(void 0); - ++current; - isSlice = true; - } else { - slices.push(parseExpression()); - if (is(TOKEN_TYPES.Colon)) { - ++current; - isSlice = true; - } - } - } - if (slices.length === 0) { - throw new SyntaxError(`Expected at least one argument for member/slice expression`); - } - if (isSlice) { - if (slices.length > 3) { - throw new SyntaxError(`Expected 0-3 arguments for slice expression`); - } - return new SliceExpression(...slices); - } - return slices[0]; - } - function parseMemberExpression() { - let object = parsePrimaryExpression(); - while (is(TOKEN_TYPES.Dot) || is(TOKEN_TYPES.OpenSquareBracket)) { - const operator = tokens[current]; - ++current; - let property; - const computed = operator.type !== TOKEN_TYPES.Dot; - if (computed) { - property = parseMemberExpressionArgumentsList(); - expect(TOKEN_TYPES.CloseSquareBracket, "Expected closing square bracket"); - } else { - property = parsePrimaryExpression(); - if (property.type !== "Identifier") { - throw new SyntaxError(`Expected identifier following dot operator`); - } - } - object = new MemberExpression(object, property, computed); - } - return object; - } - function parseMultiplicativeExpression() { - let left = parseTestExpression(); - while (is(TOKEN_TYPES.MultiplicativeBinaryOperator)) { - const operator = tokens[current]; - ++current; - const right = parseTestExpression(); - left = new BinaryExpression(operator, left, right); - } - return left; - } - function parseTestExpression() { - let operand = parseFilterExpression(); - while (is(TOKEN_TYPES.Is)) { - ++current; - const negate = is(TOKEN_TYPES.Not); - if (negate) { - ++current; - } - let filter = parsePrimaryExpression(); - if (filter instanceof BooleanLiteral) { - filter = new Identifier(filter.value.toString()); - } - if (!(filter instanceof Identifier)) { - throw new SyntaxError(`Expected identifier for the test`); - } - operand = new TestExpression(operand, negate, filter); - } - return operand; - } - function parseFilterExpression() { - let operand = parseCallMemberExpression(); - while (is(TOKEN_TYPES.Pipe)) { - ++current; - let filter = parsePrimaryExpression(); - if (!(filter instanceof Identifier)) { - throw new SyntaxError(`Expected identifier for the filter`); - } - if (is(TOKEN_TYPES.OpenParen)) { - filter = parseCallExpression(filter); - } - operand = new FilterExpression(operand, filter); - } - return operand; - } - function parsePrimaryExpression() { - const token = tokens[current]; - switch (token.type) { - case TOKEN_TYPES.NumericLiteral: - ++current; - return new NumericLiteral(Number(token.value)); - case TOKEN_TYPES.StringLiteral: - ++current; - return new StringLiteral(token.value); - case TOKEN_TYPES.BooleanLiteral: - ++current; - return new BooleanLiteral(token.value.toLowerCase() === "true"); - case TOKEN_TYPES.Identifier: - ++current; - return new Identifier(token.value); - case TOKEN_TYPES.OpenParen: { - ++current; - const expression = parseExpressionSequence(); - if (tokens[current].type !== TOKEN_TYPES.CloseParen) { - throw new SyntaxError(`Expected closing parenthesis, got ${tokens[current].type} instead`); - } - ++current; - return expression; - } - case TOKEN_TYPES.OpenSquareBracket: { - ++current; - const values = []; - while (!is(TOKEN_TYPES.CloseSquareBracket)) { - values.push(parseExpression()); - if (is(TOKEN_TYPES.Comma)) { - ++current; - } - } - ++current; - return new ArrayLiteral(values); - } - case TOKEN_TYPES.OpenCurlyBracket: { - ++current; - const values = /* @__PURE__ */ new Map(); - while (!is(TOKEN_TYPES.CloseCurlyBracket)) { - const key = parseExpression(); - expect(TOKEN_TYPES.Colon, "Expected colon between key and value in object literal"); - const value = parseExpression(); - values.set(key, value); - if (is(TOKEN_TYPES.Comma)) { - ++current; - } - } - ++current; - return new ObjectLiteral(values); - } - default: - throw new SyntaxError(`Unexpected token: ${token.type}`); - } - } - while (current < tokens.length) { - program.body.push(parseAny()); - } - return program; - } - function range(start, stop, step = 1) { - if (stop === void 0) { - stop = start; - start = 0; - } - const result = []; - for (let i = start; i < stop; i += step) { - result.push(i); - } - return result; - } - function slice(array, start, stop, step = 1) { - const direction = Math.sign(step); - if (direction >= 0) { - start = (start ?? (start = 0)) < 0 ? Math.max(array.length + start, 0) : Math.min(start, array.length); - stop = (stop ?? (stop = array.length)) < 0 ? Math.max(array.length + stop, 0) : Math.min(stop, array.length); - } else { - start = (start ?? (start = array.length - 1)) < 0 ? Math.max(array.length + start, -1) : Math.min(start, array.length - 1); - stop = (stop ?? (stop = -1)) < -1 ? Math.max(array.length + stop, -1) : Math.min(stop, array.length - 1); - } - const result = []; - for (let i = start; direction * i < direction * stop; i += step) { - result.push(array[i]); - } - return result; - } - function titleCase(value) { - return value.replace(/\b\w/g, (c) => c.toUpperCase()); - } - var RuntimeValue = class { - /** - * Creates a new RuntimeValue. - */ - constructor(value = void 0) { - __publicField(this, "type", "RuntimeValue"); - __publicField(this, "value"); - /** - * A collection of built-in functions for this type. - */ - __publicField(this, "builtins", /* @__PURE__ */ new Map()); - this.value = value; - } - /** - * Determines truthiness or falsiness of the runtime value. - * This function should be overridden by subclasses if it has custom truthiness criteria. - * @returns {BooleanValue} BooleanValue(true) if the value is truthy, BooleanValue(false) otherwise. - */ - __bool__() { - return new BooleanValue(!!this.value); - } - }; - var NumericValue = class extends RuntimeValue { - constructor() { - super(...arguments); - __publicField(this, "type", "NumericValue"); - } - }; - var StringValue = class extends RuntimeValue { - constructor() { - super(...arguments); - __publicField(this, "type", "StringValue"); - __publicField(this, "builtins", /* @__PURE__ */ new Map([ - [ - "upper", - new FunctionValue(() => { - return new StringValue(this.value.toUpperCase()); - }) - ], - [ - "lower", - new FunctionValue(() => { - return new StringValue(this.value.toLowerCase()); - }) - ], - [ - "strip", - new FunctionValue(() => { - return new StringValue(this.value.trim()); - }) - ], - [ - "title", - new FunctionValue(() => { - return new StringValue(titleCase(this.value)); - }) - ], - ["length", new NumericValue(this.value.length)] - ])); - } - }; - var BooleanValue = class extends RuntimeValue { - constructor() { - super(...arguments); - __publicField(this, "type", "BooleanValue"); - } - }; - var ObjectValue = class extends RuntimeValue { - constructor() { - super(...arguments); - __publicField(this, "type", "ObjectValue"); - __publicField(this, "builtins", /* @__PURE__ */ new Map([ - [ - "get", - new FunctionValue(([key, defaultValue]) => { - if (!(key instanceof StringValue)) { - throw new Error(`Object key must be a string: got ${key.type}`); - } - return this.value.get(key.value) ?? defaultValue ?? new NullValue(); - }) - ], - [ - "items", - new FunctionValue(() => { - return new ArrayValue( - Array.from(this.value.entries()).map(([key, value]) => new ArrayValue([new StringValue(key), value])) - ); - }) - ] - ])); - } - /** - * NOTE: necessary to override since all JavaScript arrays are considered truthy, - * while only non-empty Python arrays are consider truthy. - * - * e.g., - * - JavaScript: {} && 5 -> 5 - * - Python: {} and 5 -> {} - */ - __bool__() { - return new BooleanValue(this.value.size > 0); - } - }; - var KeywordArgumentsValue = class extends ObjectValue { - constructor() { - super(...arguments); - __publicField(this, "type", "KeywordArgumentsValue"); - } - }; - var ArrayValue = class extends RuntimeValue { - constructor() { - super(...arguments); - __publicField(this, "type", "ArrayValue"); - __publicField(this, "builtins", /* @__PURE__ */ new Map([["length", new NumericValue(this.value.length)]])); - } - /** - * NOTE: necessary to override since all JavaScript arrays are considered truthy, - * while only non-empty Python arrays are consider truthy. - * - * e.g., - * - JavaScript: [] && 5 -> 5 - * - Python: [] and 5 -> [] - */ - __bool__() { - return new BooleanValue(this.value.length > 0); - } - }; - var TupleValue = class extends ArrayValue { - constructor() { - super(...arguments); - __publicField(this, "type", "TupleValue"); - } - }; - var FunctionValue = class extends RuntimeValue { - constructor() { - super(...arguments); - __publicField(this, "type", "FunctionValue"); - } - }; - var NullValue = class extends RuntimeValue { - constructor() { - super(...arguments); - __publicField(this, "type", "NullValue"); - } - }; - var UndefinedValue = class extends RuntimeValue { - constructor() { - super(...arguments); - __publicField(this, "type", "UndefinedValue"); - } - }; - var Environment = class { - constructor(parent) { - /** - * The variables declared in this environment. - */ - __publicField(this, "variables", /* @__PURE__ */ new Map([ - [ - "namespace", - new FunctionValue((args) => { - if (args.length === 0) { - return new ObjectValue(/* @__PURE__ */ new Map()); - } - if (args.length !== 1 || !(args[0] instanceof ObjectValue)) { - throw new Error("`namespace` expects either zero arguments or a single object argument"); - } - return args[0]; - }) - ] - ])); - /** - * The tests available in this environment. - */ - __publicField(this, "tests", /* @__PURE__ */ new Map([ - ["boolean", (operand) => operand.type === "BooleanValue"], - ["callable", (operand) => operand instanceof FunctionValue], - [ - "odd", - (operand) => { - if (operand.type !== "NumericValue") { - throw new Error(`Cannot apply test "odd" to type: ${operand.type}`); - } - return operand.value % 2 !== 0; - } - ], - [ - "even", - (operand) => { - if (operand.type !== "NumericValue") { - throw new Error(`Cannot apply test "even" to type: ${operand.type}`); - } - return operand.value % 2 === 0; - } - ], - ["false", (operand) => operand.type === "BooleanValue" && !operand.value], - ["true", (operand) => operand.type === "BooleanValue" && operand.value], - ["string", (operand) => operand.type === "StringValue"], - ["number", (operand) => operand.type === "NumericValue"], - ["integer", (operand) => operand.type === "NumericValue" && Number.isInteger(operand.value)], - ["iterable", (operand) => operand instanceof ArrayValue || operand instanceof StringValue], - [ - "lower", - (operand) => { - const str = operand.value; - return operand.type === "StringValue" && str === str.toLowerCase(); - } - ], - [ - "upper", - (operand) => { - const str = operand.value; - return operand.type === "StringValue" && str === str.toUpperCase(); - } - ], - ["none", (operand) => operand.type === "NullValue"], - ["defined", (operand) => operand.type !== "UndefinedValue"], - ["undefined", (operand) => operand.type === "UndefinedValue"], - ["equalto", (a, b) => a.value === b.value], - ["eq", (a, b) => a.value === b.value] - ])); - this.parent = parent; - } - /** - * Set the value of a variable in the current environment. - */ - set(name, value) { - return this.declareVariable(name, convertToRuntimeValues(value)); - } - declareVariable(name, value) { - if (this.variables.has(name)) { - throw new SyntaxError(`Variable already declared: ${name}`); - } - this.variables.set(name, value); - return value; - } - // private assignVariable(name: string, value: AnyRuntimeValue): AnyRuntimeValue { - // const env = this.resolve(name); - // env.variables.set(name, value); - // return value; - // } - /** - * Set variable in the current scope. - * See https://jinja.palletsprojects.com/en/3.0.x/templates/#assignments for more information. - */ - setVariable(name, value) { - this.variables.set(name, value); - return value; - } - /** - * Resolve the environment in which the variable is declared. - * @param {string} name The name of the variable. - * @returns {Environment} The environment in which the variable is declared. - */ - resolve(name) { - if (this.variables.has(name)) { - return this; - } - if (this.parent) { - return this.parent.resolve(name); - } - throw new Error(`Unknown variable: ${name}`); - } - lookupVariable(name) { - try { - return this.resolve(name).variables.get(name) ?? new UndefinedValue(); - } catch { - return new UndefinedValue(); - } - } - }; - var Interpreter = class { - constructor(env) { - __publicField(this, "global"); - this.global = env ?? new Environment(); - } - /** - * Run the program. - */ - run(program) { - return this.evaluate(program, this.global); - } - /** - * Evaluates expressions following the binary operation type. - */ - evaluateBinaryExpression(node, environment) { - const left = this.evaluate(node.left, environment); - switch (node.operator.value) { - case "and": - return left.__bool__().value ? this.evaluate(node.right, environment) : left; - case "or": - return left.__bool__().value ? left : this.evaluate(node.right, environment); - } - const right = this.evaluate(node.right, environment); - switch (node.operator.value) { - case "==": - return new BooleanValue(left.value == right.value); - case "!=": - return new BooleanValue(left.value != right.value); - } - if (left instanceof UndefinedValue || right instanceof UndefinedValue) { - throw new Error("Cannot perform operation on undefined values"); - } else if (left instanceof NullValue || right instanceof NullValue) { - throw new Error("Cannot perform operation on null values"); - } else if (left instanceof NumericValue && right instanceof NumericValue) { - switch (node.operator.value) { - case "+": - return new NumericValue(left.value + right.value); - case "-": - return new NumericValue(left.value - right.value); - case "*": - return new NumericValue(left.value * right.value); - case "/": - return new NumericValue(left.value / right.value); - case "%": - return new NumericValue(left.value % right.value); - case "<": - return new BooleanValue(left.value < right.value); - case ">": - return new BooleanValue(left.value > right.value); - case ">=": - return new BooleanValue(left.value >= right.value); - case "<=": - return new BooleanValue(left.value <= right.value); - } - } else if (left instanceof ArrayValue && right instanceof ArrayValue) { - switch (node.operator.value) { - case "+": - return new ArrayValue(left.value.concat(right.value)); - } - } else if (right instanceof ArrayValue) { - const member = right.value.find((x) => x.value === left.value) !== void 0; - switch (node.operator.value) { - case "in": - return new BooleanValue(member); - case "not in": - return new BooleanValue(!member); - } - } - if (left instanceof StringValue || right instanceof StringValue) { - switch (node.operator.value) { - case "+": - return new StringValue(left.value.toString() + right.value.toString()); - } - } - if (left instanceof StringValue && right instanceof StringValue) { - switch (node.operator.value) { - case "in": - return new BooleanValue(right.value.includes(left.value)); - case "not in": - return new BooleanValue(!right.value.includes(left.value)); - } - } - if (left instanceof StringValue && right instanceof ObjectValue) { - switch (node.operator.value) { - case "in": - return new BooleanValue(right.value.has(left.value)); - case "not in": - return new BooleanValue(!right.value.has(left.value)); - } - } - throw new SyntaxError(`Unknown operator "${node.operator.value}" between ${left.type} and ${right.type}`); - } - evaluateArguments(args, environment) { - const positionalArguments = []; - const keywordArguments = /* @__PURE__ */ new Map(); - for (const argument of args) { - if (argument.type === "KeywordArgumentExpression") { - const kwarg = argument; - keywordArguments.set(kwarg.key.value, this.evaluate(kwarg.value, environment)); - } else { - if (keywordArguments.size > 0) { - throw new Error("Positional arguments must come before keyword arguments"); - } - positionalArguments.push(this.evaluate(argument, environment)); - } - } - return [positionalArguments, keywordArguments]; - } - /** - * Evaluates expressions following the filter operation type. - */ - evaluateFilterExpression(node, environment) { - const operand = this.evaluate(node.operand, environment); - if (node.filter.type === "Identifier") { - const filter = node.filter; - if (filter.value === "tojson") { - return new StringValue(toJSON(operand)); - } - if (operand instanceof ArrayValue) { - switch (filter.value) { - case "list": - return operand; - case "first": - return operand.value[0]; - case "last": - return operand.value[operand.value.length - 1]; - case "length": - return new NumericValue(operand.value.length); - case "reverse": - return new ArrayValue(operand.value.reverse()); - case "sort": - return new ArrayValue( - operand.value.sort((a, b) => { - if (a.type !== b.type) { - throw new Error(`Cannot compare different types: ${a.type} and ${b.type}`); - } - switch (a.type) { - case "NumericValue": - return a.value - b.value; - case "StringValue": - return a.value.localeCompare(b.value); - default: - throw new Error(`Cannot compare type: ${a.type}`); - } - }) - ); - default: - throw new Error(`Unknown ArrayValue filter: ${filter.value}`); - } - } else if (operand instanceof StringValue) { - switch (filter.value) { - case "length": - return new NumericValue(operand.value.length); - case "upper": - return new StringValue(operand.value.toUpperCase()); - case "lower": - return new StringValue(operand.value.toLowerCase()); - case "title": - return new StringValue(titleCase(operand.value)); - case "capitalize": - return new StringValue(operand.value.charAt(0).toUpperCase() + operand.value.slice(1)); - case "trim": - return new StringValue(operand.value.trim()); - case "indent": - return new StringValue( - operand.value.split("\n").map( - (x, i) => ( - // By default, don't indent the first line or empty lines - i === 0 || x.length === 0 ? x : " " + x - ) - ).join("\n") - ); - case "string": - return operand; - default: - throw new Error(`Unknown StringValue filter: ${filter.value}`); - } - } else if (operand instanceof NumericValue) { - switch (filter.value) { - case "abs": - return new NumericValue(Math.abs(operand.value)); - default: - throw new Error(`Unknown NumericValue filter: ${filter.value}`); - } - } else if (operand instanceof ObjectValue) { - switch (filter.value) { - case "items": - return new ArrayValue( - Array.from(operand.value.entries()).map(([key, value]) => new ArrayValue([new StringValue(key), value])) - ); - case "length": - return new NumericValue(operand.value.size); - default: - throw new Error(`Unknown ObjectValue filter: ${filter.value}`); - } - } - throw new Error(`Cannot apply filter "${filter.value}" to type: ${operand.type}`); - } else if (node.filter.type === "CallExpression") { - const filter = node.filter; - if (filter.callee.type !== "Identifier") { - throw new Error(`Unknown filter: ${filter.callee.type}`); - } - const filterName = filter.callee.value; - if (filterName === "tojson") { - const [, kwargs] = this.evaluateArguments(filter.args, environment); - const indent = kwargs.get("indent") ?? new NullValue(); - if (!(indent instanceof NumericValue || indent instanceof NullValue)) { - throw new Error("If set, indent must be a number"); - } - return new StringValue(toJSON(operand, indent.value)); - } - if (operand instanceof ArrayValue) { - switch (filterName) { - case "selectattr": { - if (operand.value.some((x) => !(x instanceof ObjectValue))) { - throw new Error("`selectattr` can only be applied to array of objects"); - } - if (filter.args.some((x) => x.type !== "StringLiteral")) { - throw new Error("arguments of `selectattr` must be strings"); - } - const [attr, testName, value] = filter.args.map((x) => this.evaluate(x, environment)); - let testFunction; - if (testName) { - const test = environment.tests.get(testName.value); - if (!test) { - throw new Error(`Unknown test: ${testName.value}`); - } - testFunction = test; - } else { - testFunction = (...x) => x[0].__bool__().value; - } - const filtered = operand.value.filter((item) => { - const a = item.value.get(attr.value); - if (a) { - return testFunction(a, value); - } - return false; - }); - return new ArrayValue(filtered); - } - case "map": { - const [, kwargs] = this.evaluateArguments(filter.args, environment); - if (kwargs.has("attribute")) { - const attr = kwargs.get("attribute"); - if (!(attr instanceof StringValue)) { - throw new Error("attribute must be a string"); - } - const defaultValue = kwargs.get("default"); - const mapped = operand.value.map((item) => { - if (!(item instanceof ObjectValue)) { - throw new Error("items in map must be an object"); - } - return item.value.get(attr.value) ?? defaultValue ?? new UndefinedValue(); - }); - return new ArrayValue(mapped); - } else { - throw new Error("`map` expressions without `attribute` set are not currently supported."); - } - } - } - throw new Error(`Unknown ArrayValue filter: ${filterName}`); - } else if (operand instanceof StringValue) { - switch (filterName) { - case "indent": { - const [args, kwargs] = this.evaluateArguments(filter.args, environment); - const width = args.at(0) ?? kwargs.get("width") ?? new NumericValue(4); - if (!(width instanceof NumericValue)) { - throw new Error("width must be a number"); - } - const first = args.at(1) ?? kwargs.get("first") ?? new BooleanValue(false); - const blank = args.at(2) ?? kwargs.get("blank") ?? new BooleanValue(false); - const lines = operand.value.split("\n"); - const indent = " ".repeat(width.value); - const indented = lines.map( - (x, i) => !first.value && i === 0 || !blank.value && x.length === 0 ? x : indent + x - ); - return new StringValue(indented.join("\n")); - } - } - throw new Error(`Unknown StringValue filter: ${filterName}`); - } else { - throw new Error(`Cannot apply filter "${filterName}" to type: ${operand.type}`); - } - } - throw new Error(`Unknown filter: ${node.filter.type}`); - } - /** - * Evaluates expressions following the test operation type. - */ - evaluateTestExpression(node, environment) { - const operand = this.evaluate(node.operand, environment); - const test = environment.tests.get(node.test.value); - if (!test) { - throw new Error(`Unknown test: ${node.test.value}`); - } - const result = test(operand); - return new BooleanValue(node.negate ? !result : result); - } - /** - * Evaluates expressions following the unary operation type. - */ - evaluateUnaryExpression(node, environment) { - const argument = this.evaluate(node.argument, environment); - switch (node.operator.value) { - case "not": - return new BooleanValue(!argument.value); - default: - throw new SyntaxError(`Unknown operator: ${node.operator.value}`); - } - } - evalProgram(program, environment) { - return this.evaluateBlock(program.body, environment); - } - evaluateBlock(statements, environment) { - let result = ""; - for (const statement of statements) { - const lastEvaluated = this.evaluate(statement, environment); - if (lastEvaluated.type !== "NullValue" && lastEvaluated.type !== "UndefinedValue") { - result += lastEvaluated.value; - } - } - return new StringValue(result); - } - evaluateIdentifier(node, environment) { - return environment.lookupVariable(node.value); - } - evaluateCallExpression(expr, environment) { - const [args, kwargs] = this.evaluateArguments(expr.args, environment); - if (kwargs.size > 0) { - args.push(new KeywordArgumentsValue(kwargs)); - } - const fn = this.evaluate(expr.callee, environment); - if (fn.type !== "FunctionValue") { - throw new Error(`Cannot call something that is not a function: got ${fn.type}`); - } - return fn.value(args, environment); - } - evaluateSliceExpression(object, expr, environment) { - if (!(object instanceof ArrayValue || object instanceof StringValue)) { - throw new Error("Slice object must be an array or string"); - } - const start = this.evaluate(expr.start, environment); - const stop = this.evaluate(expr.stop, environment); - const step = this.evaluate(expr.step, environment); - if (!(start instanceof NumericValue || start instanceof UndefinedValue)) { - throw new Error("Slice start must be numeric or undefined"); - } - if (!(stop instanceof NumericValue || stop instanceof UndefinedValue)) { - throw new Error("Slice stop must be numeric or undefined"); - } - if (!(step instanceof NumericValue || step instanceof UndefinedValue)) { - throw new Error("Slice step must be numeric or undefined"); - } - if (object instanceof ArrayValue) { - return new ArrayValue(slice(object.value, start.value, stop.value, step.value)); - } else { - return new StringValue(slice(Array.from(object.value), start.value, stop.value, step.value).join("")); - } - } - evaluateMemberExpression(expr, environment) { - const object = this.evaluate(expr.object, environment); - let property; - if (expr.computed) { - if (expr.property.type === "SliceExpression") { - return this.evaluateSliceExpression(object, expr.property, environment); - } else { - property = this.evaluate(expr.property, environment); - } - } else { - property = new StringValue(expr.property.value); - } - let value; - if (object instanceof ObjectValue) { - if (!(property instanceof StringValue)) { - throw new Error(`Cannot access property with non-string: got ${property.type}`); - } - value = object.value.get(property.value) ?? object.builtins.get(property.value); - } else if (object instanceof ArrayValue || object instanceof StringValue) { - if (property instanceof NumericValue) { - value = object.value.at(property.value); - if (object instanceof StringValue) { - value = new StringValue(object.value.at(property.value)); - } - } else if (property instanceof StringValue) { - value = object.builtins.get(property.value); - } else { - throw new Error(`Cannot access property with non-string/non-number: got ${property.type}`); - } - } else { - if (!(property instanceof StringValue)) { - throw new Error(`Cannot access property with non-string: got ${property.type}`); - } - value = object.builtins.get(property.value); - } - return value instanceof RuntimeValue ? value : new UndefinedValue(); - } - evaluateSet(node, environment) { - const rhs = this.evaluate(node.value, environment); - if (node.assignee.type === "Identifier") { - const variableName = node.assignee.value; - environment.setVariable(variableName, rhs); - } else if (node.assignee.type === "MemberExpression") { - const member = node.assignee; - const object = this.evaluate(member.object, environment); - if (!(object instanceof ObjectValue)) { - throw new Error("Cannot assign to member of non-object"); - } - if (member.property.type !== "Identifier") { - throw new Error("Cannot assign to member with non-identifier property"); - } - object.value.set(member.property.value, rhs); - } else { - throw new Error(`Invalid LHS inside assignment expression: ${JSON.stringify(node.assignee)}`); - } - return new NullValue(); - } - evaluateIf(node, environment) { - const test = this.evaluate(node.test, environment); - return this.evaluateBlock(test.__bool__().value ? node.body : node.alternate, environment); - } - evaluateFor(node, environment) { - const scope = new Environment(environment); - let test, iterable; - if (node.iterable.type === "SelectExpression") { - const select = node.iterable; - iterable = this.evaluate(select.iterable, scope); - test = select.test; - } else { - iterable = this.evaluate(node.iterable, scope); - } - if (!(iterable instanceof ArrayValue)) { - throw new Error(`Expected iterable type in for loop: got ${iterable.type}`); - } - const items = []; - const scopeUpdateFunctions = []; - for (let i = 0; i < iterable.value.length; ++i) { - const loopScope = new Environment(scope); - const current = iterable.value[i]; - let scopeUpdateFunction; - if (node.loopvar.type === "Identifier") { - scopeUpdateFunction = (scope2) => scope2.setVariable(node.loopvar.value, current); - } else if (node.loopvar.type === "TupleLiteral") { - const loopvar = node.loopvar; - if (current.type !== "ArrayValue") { - throw new Error(`Cannot unpack non-iterable type: ${current.type}`); - } - const c = current; - if (loopvar.value.length !== c.value.length) { - throw new Error(`Too ${loopvar.value.length > c.value.length ? "few" : "many"} items to unpack`); - } - scopeUpdateFunction = (scope2) => { - for (let j = 0; j < loopvar.value.length; ++j) { - if (loopvar.value[j].type !== "Identifier") { - throw new Error(`Cannot unpack non-identifier type: ${loopvar.value[j].type}`); - } - scope2.setVariable(loopvar.value[j].value, c.value[j]); - } - }; - } else { - throw new Error(`Invalid loop variable(s): ${node.loopvar.type}`); - } - if (test) { - scopeUpdateFunction(loopScope); - const testValue = this.evaluate(test, loopScope); - if (!testValue.__bool__().value) { - continue; - } - } - items.push(current); - scopeUpdateFunctions.push(scopeUpdateFunction); - } - let result = ""; - let noIteration = true; - for (let i = 0; i < items.length; ++i) { - const loop = /* @__PURE__ */ new Map([ - ["index", new NumericValue(i + 1)], - ["index0", new NumericValue(i)], - ["revindex", new NumericValue(items.length - i)], - ["revindex0", new NumericValue(items.length - i - 1)], - ["first", new BooleanValue(i === 0)], - ["last", new BooleanValue(i === items.length - 1)], - ["length", new NumericValue(items.length)], - ["previtem", i > 0 ? items[i - 1] : new UndefinedValue()], - ["nextitem", i < items.length - 1 ? items[i + 1] : new UndefinedValue()] - ]); - scope.setVariable("loop", new ObjectValue(loop)); - scopeUpdateFunctions[i](scope); - const evaluated = this.evaluateBlock(node.body, scope); - result += evaluated.value; - noIteration = false; - } - if (noIteration) { - const defaultEvaluated = this.evaluateBlock(node.defaultBlock, scope); - result += defaultEvaluated.value; - } - return new StringValue(result); - } - /** - * See https://jinja.palletsprojects.com/en/3.1.x/templates/#macros for more information. - */ - evaluateMacro(node, environment) { - environment.setVariable( - node.name.value, - new FunctionValue((args, scope) => { - var _a; - const macroScope = new Environment(scope); - args = args.slice(); - let kwargs; - if (((_a = args.at(-1)) == null ? void 0 : _a.type) === "KeywordArgumentsValue") { - kwargs = args.pop(); - } - for (let i = 0; i < node.args.length; ++i) { - const nodeArg = node.args[i]; - const passedArg = args[i]; - if (nodeArg.type === "Identifier") { - const identifier = nodeArg; - if (!passedArg) { - throw new Error(`Missing positional argument: ${identifier.value}`); - } - macroScope.setVariable(identifier.value, passedArg); - } else if (nodeArg.type === "KeywordArgumentExpression") { - const kwarg = nodeArg; - const value = passedArg ?? // Try positional arguments first - (kwargs == null ? void 0 : kwargs.value.get(kwarg.key.value)) ?? // Look in user-passed kwargs - this.evaluate(kwarg.value, macroScope); - macroScope.setVariable(kwarg.key.value, value); - } else { - throw new Error(`Unknown argument type: ${nodeArg.type}`); - } - } - return this.evaluateBlock(node.body, macroScope); - }) - ); - return new NullValue(); - } - evaluate(statement, environment) { - if (statement === void 0) - return new UndefinedValue(); - switch (statement.type) { - case "Program": - return this.evalProgram(statement, environment); - case "Set": - return this.evaluateSet(statement, environment); - case "If": - return this.evaluateIf(statement, environment); - case "For": - return this.evaluateFor(statement, environment); - case "Macro": - return this.evaluateMacro(statement, environment); - case "NumericLiteral": - return new NumericValue(Number(statement.value)); - case "StringLiteral": - return new StringValue(statement.value); - case "BooleanLiteral": - return new BooleanValue(statement.value); - case "ArrayLiteral": - return new ArrayValue(statement.value.map((x) => this.evaluate(x, environment))); - case "TupleLiteral": - return new TupleValue(statement.value.map((x) => this.evaluate(x, environment))); - case "ObjectLiteral": { - const mapping = /* @__PURE__ */ new Map(); - for (const [key, value] of statement.value) { - const evaluatedKey = this.evaluate(key, environment); - if (!(evaluatedKey instanceof StringValue)) { - throw new Error(`Object keys must be strings: got ${evaluatedKey.type}`); - } - mapping.set(evaluatedKey.value, this.evaluate(value, environment)); - } - return new ObjectValue(mapping); - } - case "Identifier": - return this.evaluateIdentifier(statement, environment); - case "CallExpression": - return this.evaluateCallExpression(statement, environment); - case "MemberExpression": - return this.evaluateMemberExpression(statement, environment); - case "UnaryExpression": - return this.evaluateUnaryExpression(statement, environment); - case "BinaryExpression": - return this.evaluateBinaryExpression(statement, environment); - case "FilterExpression": - return this.evaluateFilterExpression(statement, environment); - case "TestExpression": - return this.evaluateTestExpression(statement, environment); - default: - throw new SyntaxError(`Unknown node type: ${statement.type}`); - } - } - }; - function convertToRuntimeValues(input) { - switch (typeof input) { - case "number": - return new NumericValue(input); - case "string": - return new StringValue(input); - case "boolean": - return new BooleanValue(input); - case "undefined": - return new UndefinedValue(); - case "object": - if (input === null) { - return new NullValue(); - } else if (Array.isArray(input)) { - return new ArrayValue(input.map(convertToRuntimeValues)); - } else { - return new ObjectValue( - new Map(Object.entries(input).map(([key, value]) => [key, convertToRuntimeValues(value)])) - ); - } - case "function": - return new FunctionValue((args, _scope) => { - const result = input(...args.map((x) => x.value)) ?? null; - return convertToRuntimeValues(result); - }); - default: - throw new Error(`Cannot convert to runtime value: ${input}`); - } - } - function toJSON(input, indent, depth) { - const currentDepth = depth ?? 0; - switch (input.type) { - case "NullValue": - case "UndefinedValue": - return "null"; - case "NumericValue": - case "StringValue": - case "BooleanValue": - return JSON.stringify(input.value); - case "ArrayValue": - case "ObjectValue": { - const indentValue = indent ? " ".repeat(indent) : ""; - const basePadding = "\n" + indentValue.repeat(currentDepth); - const childrenPadding = basePadding + indentValue; - if (input.type === "ArrayValue") { - const core = input.value.map((x) => toJSON(x, indent, currentDepth + 1)); - return indent ? `[${childrenPadding}${core.join(`,${childrenPadding}`)}${basePadding}]` : `[${core.join(", ")}]`; - } else { - const core = Array.from(input.value.entries()).map(([key, value]) => { - const v = `"${key}": ${toJSON(value, indent, currentDepth + 1)}`; - return indent ? `${childrenPadding}${v}` : v; - }); - return indent ? `{${core.join(",")}${basePadding}}` : `{${core.join(", ")}}`; - } - } - default: - throw new Error(`Cannot convert to JSON: ${input.type}`); - } - } - var Template = class { - /** - * @param {string} template The template string - */ - constructor(template) { - __publicField(this, "parsed"); - const tokens = tokenize(template, { - lstrip_blocks: true, - trim_blocks: true - }); - this.parsed = parse(tokens); - } - render(items) { - const env = new Environment(); - env.set("false", false); - env.set("true", true); - env.set("raise_exception", (args) => { - throw new Error(args); - }); - env.set("range", range); - for (const [key, value] of Object.entries(items)) { - env.set(key, value); - } - const interpreter = new Interpreter(env); - const result = interpreter.run(this.parsed); - return result.value; - } - }; - } - ), - /***/ - "./node_modules/onnxruntime-common/dist/esm/backend-impl.js": ( - /*!******************************************************************!*\ - !*** ./node_modules/onnxruntime-common/dist/esm/backend-impl.js ***! - \******************************************************************/ - /***/ - (__unused_webpack___webpack_module__, __webpack_exports__2, __webpack_require__2) => { - __webpack_require__2.r(__webpack_exports__2); - __webpack_require__2.d(__webpack_exports__2, { - /* harmony export */ - registerBackend: () => ( - /* binding */ - registerBackend - ), - /* harmony export */ - resolveBackendAndExecutionProviders: () => ( - /* binding */ - resolveBackendAndExecutionProviders - ) - /* harmony export */ - }); - const backends = /* @__PURE__ */ new Map(); - const backendsSortedByPriority = []; - const registerBackend = (name, backend, priority) => { - if (backend && typeof backend.init === "function" && typeof backend.createInferenceSessionHandler === "function") { - const currentBackend = backends.get(name); - if (currentBackend === void 0) { - backends.set(name, { backend, priority }); - } else if (currentBackend.priority > priority) { - return; - } else if (currentBackend.priority === priority) { - if (currentBackend.backend !== backend) { - throw new Error(`cannot register backend "${name}" using priority ${priority}`); - } - } - if (priority >= 0) { - const i = backendsSortedByPriority.indexOf(name); - if (i !== -1) { - backendsSortedByPriority.splice(i, 1); - } - for (let i2 = 0; i2 < backendsSortedByPriority.length; i2++) { - if (backends.get(backendsSortedByPriority[i2]).priority <= priority) { - backendsSortedByPriority.splice(i2, 0, name); - return; - } - } - backendsSortedByPriority.push(name); - } - return; - } - throw new TypeError("not a valid backend"); - }; - const tryResolveAndInitializeBackend = async (backendName) => { - const backendInfo = backends.get(backendName); - if (!backendInfo) { - return "backend not found."; - } - if (backendInfo.initialized) { - return backendInfo.backend; - } else if (backendInfo.aborted) { - return backendInfo.error; - } else { - const isInitializing = !!backendInfo.initPromise; - try { - if (!isInitializing) { - backendInfo.initPromise = backendInfo.backend.init(backendName); - } - await backendInfo.initPromise; - backendInfo.initialized = true; - return backendInfo.backend; - } catch (e) { - if (!isInitializing) { - backendInfo.error = `${e}`; - backendInfo.aborted = true; - } - return backendInfo.error; - } finally { - delete backendInfo.initPromise; - } - } - }; - const resolveBackendAndExecutionProviders = async (options) => { - const eps = options.executionProviders || []; - const backendHints = eps.map((i) => typeof i === "string" ? i : i.name); - const backendNames = backendHints.length === 0 ? backendsSortedByPriority : backendHints; - let backend; - const errors = []; - const availableBackendNames = /* @__PURE__ */ new Set(); - for (const backendName of backendNames) { - console.log({ backendName }); - const resolveResult = await tryResolveAndInitializeBackend(backendName); - if (typeof resolveResult === "string") { - errors.push({ name: backendName, err: resolveResult }); - } else { - if (!backend) { - backend = resolveResult; - } - if (backend === resolveResult) { - availableBackendNames.add(backendName); - } - } - } - if (!backend) { - throw new Error(`no available backend found. ERR: ${errors.map((e) => `[${e.name}] ${e.err}`).join(", ")}`); - } - for (const { name, err } of errors) { - if (backendHints.includes(name)) { - console.warn(`removing requested execution provider "${name}" from session options because it is not available: ${err}`); - } - } - const filteredEps = eps.filter((i) => availableBackendNames.has(typeof i === "string" ? i : i.name)); - return [ - backend, - new Proxy(options, { - get: (target, prop) => { - if (prop === "executionProviders") { - return filteredEps; - } - return Reflect.get(target, prop); - } - }) - ]; - }; - } - ), - /***/ - "./node_modules/onnxruntime-common/dist/esm/backend.js": ( - /*!*************************************************************!*\ - !*** ./node_modules/onnxruntime-common/dist/esm/backend.js ***! - \*************************************************************/ - /***/ - (__unused_webpack___webpack_module__, __webpack_exports__2, __webpack_require__2) => { - __webpack_require__2.r(__webpack_exports__2); - __webpack_require__2.d(__webpack_exports__2, { - /* harmony export */ - registerBackend: () => ( - /* reexport safe */ - _backend_impl_js__WEBPACK_IMPORTED_MODULE_0__.registerBackend - ) - /* harmony export */ - }); - var _backend_impl_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__2( - /*! ./backend-impl.js */ - "./node_modules/onnxruntime-common/dist/esm/backend-impl.js" - ); - } - ), - /***/ - "./node_modules/onnxruntime-common/dist/esm/env-impl.js": ( - /*!**************************************************************!*\ - !*** ./node_modules/onnxruntime-common/dist/esm/env-impl.js ***! - \**************************************************************/ - /***/ - (__unused_webpack___webpack_module__, __webpack_exports__2, __webpack_require__2) => { - __webpack_require__2.r(__webpack_exports__2); - __webpack_require__2.d(__webpack_exports__2, { - /* harmony export */ - env: () => ( - /* binding */ - env - ) - /* harmony export */ - }); - var _version_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__2( - /*! ./version.js */ - "./node_modules/onnxruntime-common/dist/esm/version.js" - ); - let logLevelValue = "warning"; - const env = { - wasm: {}, - webgl: {}, - webgpu: {}, - versions: { common: _version_js__WEBPACK_IMPORTED_MODULE_0__.version }, - set logLevel(value) { - if (value === void 0) { - return; - } - if (typeof value !== "string" || ["verbose", "info", "warning", "error", "fatal"].indexOf(value) === -1) { - throw new Error(`Unsupported logging level: ${value}`); - } - logLevelValue = value; - }, - get logLevel() { - return logLevelValue; - } - }; - Object.defineProperty(env, "logLevel", { enumerable: true }); - } - ), - /***/ - "./node_modules/onnxruntime-common/dist/esm/env.js": ( - /*!*********************************************************!*\ - !*** ./node_modules/onnxruntime-common/dist/esm/env.js ***! - \*********************************************************/ - /***/ - (__unused_webpack___webpack_module__, __webpack_exports__2, __webpack_require__2) => { - __webpack_require__2.r(__webpack_exports__2); - __webpack_require__2.d(__webpack_exports__2, { - /* harmony export */ - env: () => ( - /* binding */ - env - ) - /* harmony export */ - }); - var _env_impl_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__2( - /*! ./env-impl.js */ - "./node_modules/onnxruntime-common/dist/esm/env-impl.js" - ); - const env = _env_impl_js__WEBPACK_IMPORTED_MODULE_0__.env; - } - ), - /***/ - "./node_modules/onnxruntime-common/dist/esm/index.js": ( - /*!***********************************************************!*\ - !*** ./node_modules/onnxruntime-common/dist/esm/index.js ***! - \***********************************************************/ - /***/ - (__unused_webpack___webpack_module__, __webpack_exports__2, __webpack_require__2) => { - __webpack_require__2.r(__webpack_exports__2); - __webpack_require__2.d(__webpack_exports__2, { - /* harmony export */ - InferenceSession: () => ( - /* reexport safe */ - _inference_session_js__WEBPACK_IMPORTED_MODULE_2__.InferenceSession - ), - /* harmony export */ - TRACE: () => ( - /* reexport safe */ - _trace_js__WEBPACK_IMPORTED_MODULE_6__.TRACE - ), - /* harmony export */ - TRACE_FUNC_BEGIN: () => ( - /* reexport safe */ - _trace_js__WEBPACK_IMPORTED_MODULE_6__.TRACE_FUNC_BEGIN - ), - /* harmony export */ - TRACE_FUNC_END: () => ( - /* reexport safe */ - _trace_js__WEBPACK_IMPORTED_MODULE_6__.TRACE_FUNC_END - ), - /* harmony export */ - Tensor: () => ( - /* reexport safe */ - _tensor_js__WEBPACK_IMPORTED_MODULE_3__.Tensor - ), - /* harmony export */ - TrainingSession: () => ( - /* reexport safe */ - _training_session_js__WEBPACK_IMPORTED_MODULE_9__.TrainingSession - ), - /* harmony export */ - env: () => ( - /* reexport safe */ - _env_js__WEBPACK_IMPORTED_MODULE_1__.env - ), - /* harmony export */ - registerBackend: () => ( - /* reexport safe */ - _backend_js__WEBPACK_IMPORTED_MODULE_0__.registerBackend - ) - /* harmony export */ - }); - var _backend_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__2( - /*! ./backend.js */ - "./node_modules/onnxruntime-common/dist/esm/backend.js" - ); - var _env_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__2( - /*! ./env.js */ - "./node_modules/onnxruntime-common/dist/esm/env.js" - ); - var _inference_session_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__2( - /*! ./inference-session.js */ - "./node_modules/onnxruntime-common/dist/esm/inference-session.js" - ); - var _tensor_js__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__2( - /*! ./tensor.js */ - "./node_modules/onnxruntime-common/dist/esm/tensor.js" - ); - var _tensor_conversion_js__WEBPACK_IMPORTED_MODULE_4__ = __webpack_require__2( - /*! ./tensor-conversion.js */ - "./node_modules/onnxruntime-common/dist/esm/tensor-conversion.js" - ); - var _tensor_factory_js__WEBPACK_IMPORTED_MODULE_5__ = __webpack_require__2( - /*! ./tensor-factory.js */ - "./node_modules/onnxruntime-common/dist/esm/tensor-factory.js" - ); - var _trace_js__WEBPACK_IMPORTED_MODULE_6__ = __webpack_require__2( - /*! ./trace.js */ - "./node_modules/onnxruntime-common/dist/esm/trace.js" - ); - var _onnx_model_js__WEBPACK_IMPORTED_MODULE_7__ = __webpack_require__2( - /*! ./onnx-model.js */ - "./node_modules/onnxruntime-common/dist/esm/onnx-model.js" - ); - var _onnx_value_js__WEBPACK_IMPORTED_MODULE_8__ = __webpack_require__2( - /*! ./onnx-value.js */ - "./node_modules/onnxruntime-common/dist/esm/onnx-value.js" - ); - var _training_session_js__WEBPACK_IMPORTED_MODULE_9__ = __webpack_require__2( - /*! ./training-session.js */ - "./node_modules/onnxruntime-common/dist/esm/training-session.js" - ); - } - ), - /***/ - "./node_modules/onnxruntime-common/dist/esm/inference-session-impl.js": ( - /*!****************************************************************************!*\ - !*** ./node_modules/onnxruntime-common/dist/esm/inference-session-impl.js ***! - \****************************************************************************/ - /***/ - (__unused_webpack___webpack_module__, __webpack_exports__2, __webpack_require__2) => { - __webpack_require__2.r(__webpack_exports__2); - __webpack_require__2.d(__webpack_exports__2, { - /* harmony export */ - InferenceSession: () => ( - /* binding */ - InferenceSession - ) - /* harmony export */ - }); - var _backend_impl_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__2( - /*! ./backend-impl.js */ - "./node_modules/onnxruntime-common/dist/esm/backend-impl.js" - ); - var _tensor_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__2( - /*! ./tensor.js */ - "./node_modules/onnxruntime-common/dist/esm/tensor.js" - ); - var _trace_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__2( - /*! ./trace.js */ - "./node_modules/onnxruntime-common/dist/esm/trace.js" - ); - class InferenceSession { - constructor(handler) { - this.handler = handler; - } - async run(feeds, arg1, arg2) { - (0, _trace_js__WEBPACK_IMPORTED_MODULE_2__.TRACE_FUNC_BEGIN)(); - const fetches = {}; - let options = {}; - if (typeof feeds !== "object" || feeds === null || feeds instanceof _tensor_js__WEBPACK_IMPORTED_MODULE_1__.Tensor || Array.isArray(feeds)) { - throw new TypeError("'feeds' must be an object that use input names as keys and OnnxValue as corresponding values."); - } - let isFetchesEmpty = true; - if (typeof arg1 === "object") { - if (arg1 === null) { - throw new TypeError("Unexpected argument[1]: cannot be null."); - } - if (arg1 instanceof _tensor_js__WEBPACK_IMPORTED_MODULE_1__.Tensor) { - throw new TypeError("'fetches' cannot be a Tensor"); - } - if (Array.isArray(arg1)) { - if (arg1.length === 0) { - throw new TypeError("'fetches' cannot be an empty array."); - } - isFetchesEmpty = false; - for (const name of arg1) { - if (typeof name !== "string") { - throw new TypeError("'fetches' must be a string array or an object."); - } - if (this.outputNames.indexOf(name) === -1) { - throw new RangeError(`'fetches' contains invalid output name: ${name}.`); - } - fetches[name] = null; - } - if (typeof arg2 === "object" && arg2 !== null) { - options = arg2; - } else if (typeof arg2 !== "undefined") { - throw new TypeError("'options' must be an object."); - } - } else { - let isFetches = false; - const arg1Keys = Object.getOwnPropertyNames(arg1); - for (const name of this.outputNames) { - if (arg1Keys.indexOf(name) !== -1) { - const v = arg1[name]; - if (v === null || v instanceof _tensor_js__WEBPACK_IMPORTED_MODULE_1__.Tensor) { - isFetches = true; - isFetchesEmpty = false; - fetches[name] = v; - } - } - } - if (isFetches) { - if (typeof arg2 === "object" && arg2 !== null) { - options = arg2; - } else if (typeof arg2 !== "undefined") { - throw new TypeError("'options' must be an object."); - } - } else { - options = arg1; - } - } - } else if (typeof arg1 !== "undefined") { - throw new TypeError("Unexpected argument[1]: must be 'fetches' or 'options'."); - } - for (const name of this.inputNames) { - if (typeof feeds[name] === "undefined") { - throw new Error(`input '${name}' is missing in 'feeds'.`); - } - } - if (isFetchesEmpty) { - for (const name of this.outputNames) { - fetches[name] = null; - } - } - const results = await this.handler.run(feeds, fetches, options); - const returnValue = {}; - for (const key in results) { - if (Object.hasOwnProperty.call(results, key)) { - const result = results[key]; - if (result instanceof _tensor_js__WEBPACK_IMPORTED_MODULE_1__.Tensor) { - returnValue[key] = result; - } else { - returnValue[key] = new _tensor_js__WEBPACK_IMPORTED_MODULE_1__.Tensor(result.type, result.data, result.dims); - } - } - } - (0, _trace_js__WEBPACK_IMPORTED_MODULE_2__.TRACE_FUNC_END)(); - return returnValue; - } - async release() { - return this.handler.dispose(); - } - static async create(arg0, arg1, arg2, arg3) { - (0, _trace_js__WEBPACK_IMPORTED_MODULE_2__.TRACE_FUNC_BEGIN)(); - let filePathOrUint8Array; - let options = {}; - if (typeof arg0 === "string") { - filePathOrUint8Array = arg0; - if (typeof arg1 === "object" && arg1 !== null) { - options = arg1; - } else if (typeof arg1 !== "undefined") { - throw new TypeError("'options' must be an object."); - } - } else if (arg0 instanceof Uint8Array) { - filePathOrUint8Array = arg0; - if (typeof arg1 === "object" && arg1 !== null) { - options = arg1; - } else if (typeof arg1 !== "undefined") { - throw new TypeError("'options' must be an object."); - } - } else if (arg0 instanceof ArrayBuffer || typeof SharedArrayBuffer !== "undefined" && arg0 instanceof SharedArrayBuffer) { - const buffer = arg0; - let byteOffset = 0; - let byteLength = arg0.byteLength; - if (typeof arg1 === "object" && arg1 !== null) { - options = arg1; - } else if (typeof arg1 === "number") { - byteOffset = arg1; - if (!Number.isSafeInteger(byteOffset)) { - throw new RangeError("'byteOffset' must be an integer."); - } - if (byteOffset < 0 || byteOffset >= buffer.byteLength) { - throw new RangeError(`'byteOffset' is out of range [0, ${buffer.byteLength}).`); - } - byteLength = arg0.byteLength - byteOffset; - if (typeof arg2 === "number") { - byteLength = arg2; - if (!Number.isSafeInteger(byteLength)) { - throw new RangeError("'byteLength' must be an integer."); - } - if (byteLength <= 0 || byteOffset + byteLength > buffer.byteLength) { - throw new RangeError(`'byteLength' is out of range (0, ${buffer.byteLength - byteOffset}].`); - } - if (typeof arg3 === "object" && arg3 !== null) { - options = arg3; - } else if (typeof arg3 !== "undefined") { - throw new TypeError("'options' must be an object."); - } - } else if (typeof arg2 !== "undefined") { - throw new TypeError("'byteLength' must be a number."); - } - } else if (typeof arg1 !== "undefined") { - throw new TypeError("'options' must be an object."); - } - filePathOrUint8Array = new Uint8Array(buffer, byteOffset, byteLength); - } else { - throw new TypeError("Unexpected argument[0]: must be 'path' or 'buffer'."); - } - const [backend, optionsWithValidatedEPs] = await (0, _backend_impl_js__WEBPACK_IMPORTED_MODULE_0__.resolveBackendAndExecutionProviders)(options); - const handler = await backend.createInferenceSessionHandler(filePathOrUint8Array, optionsWithValidatedEPs); - (0, _trace_js__WEBPACK_IMPORTED_MODULE_2__.TRACE_FUNC_END)(); - return new InferenceSession(handler); - } - startProfiling() { - this.handler.startProfiling(); - } - endProfiling() { - this.handler.endProfiling(); - } - get inputNames() { - return this.handler.inputNames; - } - get outputNames() { - return this.handler.outputNames; - } - } - } - ), - /***/ - "./node_modules/onnxruntime-common/dist/esm/inference-session.js": ( - /*!***********************************************************************!*\ - !*** ./node_modules/onnxruntime-common/dist/esm/inference-session.js ***! - \***********************************************************************/ - /***/ - (__unused_webpack___webpack_module__, __webpack_exports__2, __webpack_require__2) => { - __webpack_require__2.r(__webpack_exports__2); - __webpack_require__2.d(__webpack_exports__2, { - /* harmony export */ - InferenceSession: () => ( - /* binding */ - InferenceSession - ) - /* harmony export */ - }); - var _inference_session_impl_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__2( - /*! ./inference-session-impl.js */ - "./node_modules/onnxruntime-common/dist/esm/inference-session-impl.js" - ); - const InferenceSession = _inference_session_impl_js__WEBPACK_IMPORTED_MODULE_0__.InferenceSession; - } - ), - /***/ - "./node_modules/onnxruntime-common/dist/esm/onnx-model.js": ( - /*!****************************************************************!*\ - !*** ./node_modules/onnxruntime-common/dist/esm/onnx-model.js ***! - \****************************************************************/ - /***/ - (__unused_webpack___webpack_module__, __webpack_exports__2, __webpack_require__2) => { - __webpack_require__2.r(__webpack_exports__2); - } - ), - /***/ - "./node_modules/onnxruntime-common/dist/esm/onnx-value.js": ( - /*!****************************************************************!*\ - !*** ./node_modules/onnxruntime-common/dist/esm/onnx-value.js ***! - \****************************************************************/ - /***/ - (__unused_webpack___webpack_module__, __webpack_exports__2, __webpack_require__2) => { - __webpack_require__2.r(__webpack_exports__2); - } - ), - /***/ - "./node_modules/onnxruntime-common/dist/esm/tensor-conversion-impl.js": ( - /*!****************************************************************************!*\ - !*** ./node_modules/onnxruntime-common/dist/esm/tensor-conversion-impl.js ***! - \****************************************************************************/ - /***/ - (__unused_webpack___webpack_module__, __webpack_exports__2, __webpack_require__2) => { - __webpack_require__2.r(__webpack_exports__2); - __webpack_require__2.d(__webpack_exports__2, { - /* harmony export */ - tensorToDataURL: () => ( - /* binding */ - tensorToDataURL - ), - /* harmony export */ - tensorToImageData: () => ( - /* binding */ - tensorToImageData - ) - /* harmony export */ - }); - const tensorToDataURL = (tensor, options) => { - const canvas = typeof document !== "undefined" ? document.createElement("canvas") : new OffscreenCanvas(1, 1); - canvas.width = tensor.dims[3]; - canvas.height = tensor.dims[2]; - const pixels2DContext = canvas.getContext("2d"); - if (pixels2DContext != null) { - let width; - let height; - if ((options == null ? void 0 : options.tensorLayout) !== void 0 && options.tensorLayout === "NHWC") { - width = tensor.dims[2]; - height = tensor.dims[3]; - } else { - width = tensor.dims[3]; - height = tensor.dims[2]; - } - const inputformat = (options == null ? void 0 : options.format) !== void 0 ? options.format : "RGB"; - const norm = options == null ? void 0 : options.norm; - let normMean; - let normBias; - if (norm === void 0 || norm.mean === void 0) { - normMean = [255, 255, 255, 255]; - } else { - if (typeof norm.mean === "number") { - normMean = [norm.mean, norm.mean, norm.mean, norm.mean]; - } else { - normMean = [norm.mean[0], norm.mean[1], norm.mean[2], 0]; - if (norm.mean[3] !== void 0) { - normMean[3] = norm.mean[3]; - } - } - } - if (norm === void 0 || norm.bias === void 0) { - normBias = [0, 0, 0, 0]; - } else { - if (typeof norm.bias === "number") { - normBias = [norm.bias, norm.bias, norm.bias, norm.bias]; - } else { - normBias = [norm.bias[0], norm.bias[1], norm.bias[2], 0]; - if (norm.bias[3] !== void 0) { - normBias[3] = norm.bias[3]; - } - } - } - const stride = height * width; - let rTensorPointer = 0, gTensorPointer = stride, bTensorPointer = stride * 2, aTensorPointer = -1; - if (inputformat === "RGBA") { - rTensorPointer = 0; - gTensorPointer = stride; - bTensorPointer = stride * 2; - aTensorPointer = stride * 3; - } else if (inputformat === "RGB") { - rTensorPointer = 0; - gTensorPointer = stride; - bTensorPointer = stride * 2; - } else if (inputformat === "RBG") { - rTensorPointer = 0; - bTensorPointer = stride; - gTensorPointer = stride * 2; - } - for (let i = 0; i < height; i++) { - for (let j = 0; j < width; j++) { - const R = (tensor.data[rTensorPointer++] - normBias[0]) * normMean[0]; - const G = (tensor.data[gTensorPointer++] - normBias[1]) * normMean[1]; - const B = (tensor.data[bTensorPointer++] - normBias[2]) * normMean[2]; - const A = aTensorPointer === -1 ? 255 : (tensor.data[aTensorPointer++] - normBias[3]) * normMean[3]; - pixels2DContext.fillStyle = "rgba(" + R + "," + G + "," + B + "," + A + ")"; - pixels2DContext.fillRect(j, i, 1, 1); - } - } - if ("toDataURL" in canvas) { - return canvas.toDataURL(); - } else { - throw new Error("toDataURL is not supported"); - } - } else { - throw new Error("Can not access image data"); - } - }; - const tensorToImageData = (tensor, options) => { - const pixels2DContext = typeof document !== "undefined" ? document.createElement("canvas").getContext("2d") : new OffscreenCanvas(1, 1).getContext("2d"); - let image; - if (pixels2DContext != null) { - let width; - let height; - let channels; - if ((options == null ? void 0 : options.tensorLayout) !== void 0 && options.tensorLayout === "NHWC") { - width = tensor.dims[2]; - height = tensor.dims[1]; - channels = tensor.dims[3]; - } else { - width = tensor.dims[3]; - height = tensor.dims[2]; - channels = tensor.dims[1]; - } - const inputformat = options !== void 0 ? options.format !== void 0 ? options.format : "RGB" : "RGB"; - const norm = options == null ? void 0 : options.norm; - let normMean; - let normBias; - if (norm === void 0 || norm.mean === void 0) { - normMean = [255, 255, 255, 255]; - } else { - if (typeof norm.mean === "number") { - normMean = [norm.mean, norm.mean, norm.mean, norm.mean]; - } else { - normMean = [norm.mean[0], norm.mean[1], norm.mean[2], 255]; - if (norm.mean[3] !== void 0) { - normMean[3] = norm.mean[3]; - } - } - } - if (norm === void 0 || norm.bias === void 0) { - normBias = [0, 0, 0, 0]; - } else { - if (typeof norm.bias === "number") { - normBias = [norm.bias, norm.bias, norm.bias, norm.bias]; - } else { - normBias = [norm.bias[0], norm.bias[1], norm.bias[2], 0]; - if (norm.bias[3] !== void 0) { - normBias[3] = norm.bias[3]; - } - } - } - const stride = height * width; - if (options !== void 0) { - if (options.format !== void 0 && (channels === 4 && options.format !== "RGBA") || channels === 3 && (options.format !== "RGB" && options.format !== "BGR")) { - throw new Error("Tensor format doesn't match input tensor dims"); - } - } - const step = 4; - let rImagePointer = 0, gImagePointer = 1, bImagePointer = 2, aImagePointer = 3; - let rTensorPointer = 0, gTensorPointer = stride, bTensorPointer = stride * 2, aTensorPointer = -1; - if (inputformat === "RGBA") { - rTensorPointer = 0; - gTensorPointer = stride; - bTensorPointer = stride * 2; - aTensorPointer = stride * 3; - } else if (inputformat === "RGB") { - rTensorPointer = 0; - gTensorPointer = stride; - bTensorPointer = stride * 2; - } else if (inputformat === "RBG") { - rTensorPointer = 0; - bTensorPointer = stride; - gTensorPointer = stride * 2; - } - image = pixels2DContext.createImageData(width, height); - for (let i = 0; i < height * width; rImagePointer += step, gImagePointer += step, bImagePointer += step, aImagePointer += step, i++) { - image.data[rImagePointer] = (tensor.data[rTensorPointer++] - normBias[0]) * normMean[0]; - image.data[gImagePointer] = (tensor.data[gTensorPointer++] - normBias[1]) * normMean[1]; - image.data[bImagePointer] = (tensor.data[bTensorPointer++] - normBias[2]) * normMean[2]; - image.data[aImagePointer] = aTensorPointer === -1 ? 255 : (tensor.data[aTensorPointer++] - normBias[3]) * normMean[3]; - } - } else { - throw new Error("Can not access image data"); - } - return image; - }; - } - ), - /***/ - "./node_modules/onnxruntime-common/dist/esm/tensor-conversion.js": ( - /*!***********************************************************************!*\ - !*** ./node_modules/onnxruntime-common/dist/esm/tensor-conversion.js ***! - \***********************************************************************/ - /***/ - (__unused_webpack___webpack_module__, __webpack_exports__2, __webpack_require__2) => { - __webpack_require__2.r(__webpack_exports__2); - } - ), - /***/ - "./node_modules/onnxruntime-common/dist/esm/tensor-factory-impl.js": ( - /*!*************************************************************************!*\ - !*** ./node_modules/onnxruntime-common/dist/esm/tensor-factory-impl.js ***! - \*************************************************************************/ - /***/ - (__unused_webpack___webpack_module__, __webpack_exports__2, __webpack_require__2) => { - __webpack_require__2.r(__webpack_exports__2); - __webpack_require__2.d(__webpack_exports__2, { - /* harmony export */ - bufferToTensor: () => ( - /* binding */ - bufferToTensor - ), - /* harmony export */ - tensorFromGpuBuffer: () => ( - /* binding */ - tensorFromGpuBuffer - ), - /* harmony export */ - tensorFromImage: () => ( - /* binding */ - tensorFromImage - ), - /* harmony export */ - tensorFromPinnedBuffer: () => ( - /* binding */ - tensorFromPinnedBuffer - ), - /* harmony export */ - tensorFromTexture: () => ( - /* binding */ - tensorFromTexture - ) - /* harmony export */ - }); - var _tensor_impl_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__2( - /*! ./tensor-impl.js */ - "./node_modules/onnxruntime-common/dist/esm/tensor-impl.js" - ); - const bufferToTensor = (buffer, options) => { - if (buffer === void 0) { - throw new Error("Image buffer must be defined"); - } - if (options.height === void 0 || options.width === void 0) { - throw new Error("Image height and width must be defined"); - } - if (options.tensorLayout === "NHWC") { - throw new Error("NHWC Tensor layout is not supported yet"); - } - const { height, width } = options; - const norm = options.norm ?? { mean: 255, bias: 0 }; - let normMean; - let normBias; - if (typeof norm.mean === "number") { - normMean = [norm.mean, norm.mean, norm.mean, norm.mean]; - } else { - normMean = [norm.mean[0], norm.mean[1], norm.mean[2], norm.mean[3] ?? 255]; - } - if (typeof norm.bias === "number") { - normBias = [norm.bias, norm.bias, norm.bias, norm.bias]; - } else { - normBias = [norm.bias[0], norm.bias[1], norm.bias[2], norm.bias[3] ?? 0]; - } - const inputformat = options.format !== void 0 ? options.format : "RGBA"; - const outputformat = options.tensorFormat !== void 0 ? options.tensorFormat !== void 0 ? options.tensorFormat : "RGB" : "RGB"; - const stride = height * width; - const float32Data = outputformat === "RGBA" ? new Float32Array(stride * 4) : new Float32Array(stride * 3); - let step = 4, rImagePointer = 0, gImagePointer = 1, bImagePointer = 2, aImagePointer = 3; - let rTensorPointer = 0, gTensorPointer = stride, bTensorPointer = stride * 2, aTensorPointer = -1; - if (inputformat === "RGB") { - step = 3; - rImagePointer = 0; - gImagePointer = 1; - bImagePointer = 2; - aImagePointer = -1; - } - if (outputformat === "RGBA") { - aTensorPointer = stride * 3; - } else if (outputformat === "RBG") { - rTensorPointer = 0; - bTensorPointer = stride; - gTensorPointer = stride * 2; - } else if (outputformat === "BGR") { - bTensorPointer = 0; - gTensorPointer = stride; - rTensorPointer = stride * 2; - } - for (let i = 0; i < stride; i++, rImagePointer += step, bImagePointer += step, gImagePointer += step, aImagePointer += step) { - float32Data[rTensorPointer++] = (buffer[rImagePointer] + normBias[0]) / normMean[0]; - float32Data[gTensorPointer++] = (buffer[gImagePointer] + normBias[1]) / normMean[1]; - float32Data[bTensorPointer++] = (buffer[bImagePointer] + normBias[2]) / normMean[2]; - if (aTensorPointer !== -1 && aImagePointer !== -1) { - float32Data[aTensorPointer++] = (buffer[aImagePointer] + normBias[3]) / normMean[3]; - } - } - const outputTensor = outputformat === "RGBA" ? new _tensor_impl_js__WEBPACK_IMPORTED_MODULE_0__.Tensor("float32", float32Data, [1, 4, height, width]) : new _tensor_impl_js__WEBPACK_IMPORTED_MODULE_0__.Tensor("float32", float32Data, [1, 3, height, width]); - return outputTensor; - }; - const tensorFromImage = async (image, options) => { - const isHTMLImageEle = typeof HTMLImageElement !== "undefined" && image instanceof HTMLImageElement; - const isImageDataEle = typeof ImageData !== "undefined" && image instanceof ImageData; - const isImageBitmap = typeof ImageBitmap !== "undefined" && image instanceof ImageBitmap; - const isString = typeof image === "string"; - let data; - let bufferToTensorOptions = options ?? {}; - const createCanvas = () => { - if (typeof document !== "undefined") { - return document.createElement("canvas"); - } else if (typeof OffscreenCanvas !== "undefined") { - return new OffscreenCanvas(1, 1); - } else { - throw new Error("Canvas is not supported"); - } - }; - const createCanvasContext = (canvas) => { - if (canvas instanceof HTMLCanvasElement) { - return canvas.getContext("2d"); - } else if (canvas instanceof OffscreenCanvas) { - return canvas.getContext("2d"); - } else { - return null; - } - }; - if (isHTMLImageEle) { - const canvas = createCanvas(); - canvas.width = image.width; - canvas.height = image.height; - const pixels2DContext = createCanvasContext(canvas); - if (pixels2DContext != null) { - let height = image.height; - let width = image.width; - if (options !== void 0 && options.resizedHeight !== void 0 && options.resizedWidth !== void 0) { - height = options.resizedHeight; - width = options.resizedWidth; - } - if (options !== void 0) { - bufferToTensorOptions = options; - if (options.tensorFormat !== void 0) { - throw new Error("Image input config format must be RGBA for HTMLImageElement"); - } else { - bufferToTensorOptions.tensorFormat = "RGBA"; - } - bufferToTensorOptions.height = height; - bufferToTensorOptions.width = width; - } else { - bufferToTensorOptions.tensorFormat = "RGBA"; - bufferToTensorOptions.height = height; - bufferToTensorOptions.width = width; - } - pixels2DContext.drawImage(image, 0, 0); - data = pixels2DContext.getImageData(0, 0, width, height).data; - } else { - throw new Error("Can not access image data"); - } - } else if (isImageDataEle) { - let height; - let width; - if (options !== void 0 && options.resizedWidth !== void 0 && options.resizedHeight !== void 0) { - height = options.resizedHeight; - width = options.resizedWidth; - } else { - height = image.height; - width = image.width; - } - if (options !== void 0) { - bufferToTensorOptions = options; - } - bufferToTensorOptions.format = "RGBA"; - bufferToTensorOptions.height = height; - bufferToTensorOptions.width = width; - if (options !== void 0) { - const tempCanvas = createCanvas(); - tempCanvas.width = width; - tempCanvas.height = height; - const pixels2DContext = createCanvasContext(tempCanvas); - if (pixels2DContext != null) { - pixels2DContext.putImageData(image, 0, 0); - data = pixels2DContext.getImageData(0, 0, width, height).data; - } else { - throw new Error("Can not access image data"); - } - } else { - data = image.data; - } - } else if (isImageBitmap) { - if (options === void 0) { - throw new Error("Please provide image config with format for Imagebitmap"); - } - const canvas = createCanvas(); - canvas.width = image.width; - canvas.height = image.height; - const pixels2DContext = createCanvasContext(canvas); - if (pixels2DContext != null) { - const height = image.height; - const width = image.width; - pixels2DContext.drawImage(image, 0, 0, width, height); - data = pixels2DContext.getImageData(0, 0, width, height).data; - bufferToTensorOptions.height = height; - bufferToTensorOptions.width = width; - return bufferToTensor(data, bufferToTensorOptions); - } else { - throw new Error("Can not access image data"); - } - } else if (isString) { - return new Promise((resolve, reject) => { - const canvas = createCanvas(); - const context = createCanvasContext(canvas); - if (!image || !context) { - return reject(); - } - const newImage = new Image(); - newImage.crossOrigin = "Anonymous"; - newImage.src = image; - newImage.onload = () => { - canvas.width = newImage.width; - canvas.height = newImage.height; - context.drawImage(newImage, 0, 0, canvas.width, canvas.height); - const img = context.getImageData(0, 0, canvas.width, canvas.height); - bufferToTensorOptions.height = canvas.height; - bufferToTensorOptions.width = canvas.width; - resolve(bufferToTensor(img.data, bufferToTensorOptions)); - }; - }); - } else { - throw new Error("Input data provided is not supported - aborted tensor creation"); - } - if (data !== void 0) { - return bufferToTensor(data, bufferToTensorOptions); - } else { - throw new Error("Input data provided is not supported - aborted tensor creation"); - } - }; - const tensorFromTexture = (texture, options) => { - const { width, height, download, dispose } = options; - const dims = [1, height, width, 4]; - return new _tensor_impl_js__WEBPACK_IMPORTED_MODULE_0__.Tensor({ location: "texture", type: "float32", texture, dims, download, dispose }); - }; - const tensorFromGpuBuffer = (gpuBuffer, options) => { - const { dataType, dims, download, dispose } = options; - return new _tensor_impl_js__WEBPACK_IMPORTED_MODULE_0__.Tensor({ location: "gpu-buffer", type: dataType ?? "float32", gpuBuffer, dims, download, dispose }); - }; - const tensorFromPinnedBuffer = (type, buffer, dims) => new _tensor_impl_js__WEBPACK_IMPORTED_MODULE_0__.Tensor({ location: "cpu-pinned", type, data: buffer, dims: dims ?? [buffer.length] }); - } - ), - /***/ - "./node_modules/onnxruntime-common/dist/esm/tensor-factory.js": ( - /*!********************************************************************!*\ - !*** ./node_modules/onnxruntime-common/dist/esm/tensor-factory.js ***! - \********************************************************************/ - /***/ - (__unused_webpack___webpack_module__, __webpack_exports__2, __webpack_require__2) => { - __webpack_require__2.r(__webpack_exports__2); - } - ), - /***/ - "./node_modules/onnxruntime-common/dist/esm/tensor-impl-type-mapping.js": ( - /*!******************************************************************************!*\ - !*** ./node_modules/onnxruntime-common/dist/esm/tensor-impl-type-mapping.js ***! - \******************************************************************************/ - /***/ - (__unused_webpack___webpack_module__, __webpack_exports__2, __webpack_require__2) => { - __webpack_require__2.r(__webpack_exports__2); - __webpack_require__2.d(__webpack_exports__2, { - /* harmony export */ - NUMERIC_TENSOR_TYPEDARRAY_TO_TYPE_MAP: () => ( - /* binding */ - NUMERIC_TENSOR_TYPEDARRAY_TO_TYPE_MAP - ), - /* harmony export */ - NUMERIC_TENSOR_TYPE_TO_TYPEDARRAY_MAP: () => ( - /* binding */ - NUMERIC_TENSOR_TYPE_TO_TYPEDARRAY_MAP - ), - /* harmony export */ - checkTypedArray: () => ( - /* binding */ - checkTypedArray - ) - /* harmony export */ - }); - const NUMERIC_TENSOR_TYPE_TO_TYPEDARRAY_MAP = /* @__PURE__ */ new Map([ - ["float32", Float32Array], - ["uint8", Uint8Array], - ["int8", Int8Array], - ["uint16", Uint16Array], - ["int16", Int16Array], - ["int32", Int32Array], - ["bool", Uint8Array], - ["float64", Float64Array], - ["uint32", Uint32Array] - ]); - const NUMERIC_TENSOR_TYPEDARRAY_TO_TYPE_MAP = /* @__PURE__ */ new Map([ - [Float32Array, "float32"], - [Uint8Array, "uint8"], - [Int8Array, "int8"], - [Uint16Array, "uint16"], - [Int16Array, "int16"], - [Int32Array, "int32"], - [Float64Array, "float64"], - [Uint32Array, "uint32"] - ]); - let isTypedArrayChecked = false; - const checkTypedArray = () => { - if (!isTypedArrayChecked) { - isTypedArrayChecked = true; - const isBigInt64ArrayAvailable = typeof BigInt64Array !== "undefined" && BigInt64Array.from; - const isBigUint64ArrayAvailable = typeof BigUint64Array !== "undefined" && BigUint64Array.from; - const isFloat16ArrayAvailable = typeof Float16Array !== "undefined" && Float16Array.from; - if (isBigInt64ArrayAvailable) { - NUMERIC_TENSOR_TYPE_TO_TYPEDARRAY_MAP.set("int64", BigInt64Array); - NUMERIC_TENSOR_TYPEDARRAY_TO_TYPE_MAP.set(BigInt64Array, "int64"); - } - if (isBigUint64ArrayAvailable) { - NUMERIC_TENSOR_TYPE_TO_TYPEDARRAY_MAP.set("uint64", BigUint64Array); - NUMERIC_TENSOR_TYPEDARRAY_TO_TYPE_MAP.set(BigUint64Array, "uint64"); - } - if (isFloat16ArrayAvailable) { - NUMERIC_TENSOR_TYPE_TO_TYPEDARRAY_MAP.set("float16", Float16Array); - NUMERIC_TENSOR_TYPEDARRAY_TO_TYPE_MAP.set(Float16Array, "float16"); - } else { - NUMERIC_TENSOR_TYPE_TO_TYPEDARRAY_MAP.set("float16", Uint16Array); - } - } - }; - } - ), - /***/ - "./node_modules/onnxruntime-common/dist/esm/tensor-impl.js": ( - /*!*****************************************************************!*\ - !*** ./node_modules/onnxruntime-common/dist/esm/tensor-impl.js ***! - \*****************************************************************/ - /***/ - (__unused_webpack___webpack_module__, __webpack_exports__2, __webpack_require__2) => { - __webpack_require__2.r(__webpack_exports__2); - __webpack_require__2.d(__webpack_exports__2, { - /* harmony export */ - Tensor: () => ( - /* binding */ - Tensor - ) - /* harmony export */ - }); - var _tensor_conversion_impl_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__2( - /*! ./tensor-conversion-impl.js */ - "./node_modules/onnxruntime-common/dist/esm/tensor-conversion-impl.js" - ); - var _tensor_factory_impl_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__2( - /*! ./tensor-factory-impl.js */ - "./node_modules/onnxruntime-common/dist/esm/tensor-factory-impl.js" - ); - var _tensor_impl_type_mapping_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__2( - /*! ./tensor-impl-type-mapping.js */ - "./node_modules/onnxruntime-common/dist/esm/tensor-impl-type-mapping.js" - ); - var _tensor_utils_impl_js__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__2( - /*! ./tensor-utils-impl.js */ - "./node_modules/onnxruntime-common/dist/esm/tensor-utils-impl.js" - ); - class Tensor { - /** - * implementation. - */ - constructor(arg0, arg1, arg2) { - (0, _tensor_impl_type_mapping_js__WEBPACK_IMPORTED_MODULE_2__.checkTypedArray)(); - let type; - let dims; - if (typeof arg0 === "object" && "location" in arg0) { - this.dataLocation = arg0.location; - type = arg0.type; - dims = arg0.dims; - switch (arg0.location) { - case "cpu-pinned": { - const expectedTypedArrayConstructor = _tensor_impl_type_mapping_js__WEBPACK_IMPORTED_MODULE_2__.NUMERIC_TENSOR_TYPE_TO_TYPEDARRAY_MAP.get(type); - if (!expectedTypedArrayConstructor) { - throw new TypeError(`unsupported type "${type}" to create tensor from pinned buffer`); - } - if (!(arg0.data instanceof expectedTypedArrayConstructor)) { - throw new TypeError(`buffer should be of type ${expectedTypedArrayConstructor.name}`); - } - this.cpuData = arg0.data; - break; - } - case "texture": { - if (type !== "float32") { - throw new TypeError(`unsupported type "${type}" to create tensor from texture`); - } - this.gpuTextureData = arg0.texture; - this.downloader = arg0.download; - this.disposer = arg0.dispose; - break; - } - case "gpu-buffer": { - if (type !== "float32" && type !== "float16" && type !== "int32" && type !== "int64" && type !== "uint32" && type !== "uint8" && type !== "bool") { - throw new TypeError(`unsupported type "${type}" to create tensor from gpu buffer`); - } - this.gpuBufferData = arg0.gpuBuffer; - this.downloader = arg0.download; - this.disposer = arg0.dispose; - break; - } - default: - throw new Error(`Tensor constructor: unsupported location '${this.dataLocation}'`); - } - } else { - let data; - let maybeDims; - if (typeof arg0 === "string") { - type = arg0; - maybeDims = arg2; - if (arg0 === "string") { - if (!Array.isArray(arg1)) { - throw new TypeError("A string tensor's data must be a string array."); - } - data = arg1; - } else { - const typedArrayConstructor = _tensor_impl_type_mapping_js__WEBPACK_IMPORTED_MODULE_2__.NUMERIC_TENSOR_TYPE_TO_TYPEDARRAY_MAP.get(arg0); - if (typedArrayConstructor === void 0) { - throw new TypeError(`Unsupported tensor type: ${arg0}.`); - } - if (Array.isArray(arg1)) { - if (arg0 === "float16" && typedArrayConstructor === Uint16Array) { - throw new TypeError("Creating a float16 tensor from number array is not supported. Please use Uint16Array as data."); - } else if (arg0 === "uint64" || arg0 === "int64") { - data = typedArrayConstructor.from(arg1, BigInt); - } else { - data = typedArrayConstructor.from(arg1); - } - } else if (arg1 instanceof typedArrayConstructor) { - data = arg1; - } else { - throw new TypeError(`A ${type} tensor's data must be type of ${typedArrayConstructor}`); - } - } - } else { - maybeDims = arg1; - if (Array.isArray(arg0)) { - if (arg0.length === 0) { - throw new TypeError("Tensor type cannot be inferred from an empty array."); - } - const firstElementType = typeof arg0[0]; - if (firstElementType === "string") { - type = "string"; - data = arg0; - } else if (firstElementType === "boolean") { - type = "bool"; - data = Uint8Array.from(arg0); - } else { - throw new TypeError(`Invalid element type of data array: ${firstElementType}.`); - } - } else { - const mappedType = _tensor_impl_type_mapping_js__WEBPACK_IMPORTED_MODULE_2__.NUMERIC_TENSOR_TYPEDARRAY_TO_TYPE_MAP.get(arg0.constructor); - if (mappedType === void 0) { - throw new TypeError(`Unsupported type for tensor data: ${arg0.constructor}.`); - } - type = mappedType; - data = arg0; - } - } - if (maybeDims === void 0) { - maybeDims = [data.length]; - } else if (!Array.isArray(maybeDims)) { - throw new TypeError("A tensor's dims must be a number array"); - } - dims = maybeDims; - this.cpuData = data; - this.dataLocation = "cpu"; - } - const size = (0, _tensor_utils_impl_js__WEBPACK_IMPORTED_MODULE_3__.calculateSize)(dims); - if (this.cpuData && size !== this.cpuData.length) { - throw new Error(`Tensor's size(${size}) does not match data length(${this.cpuData.length}).`); - } - this.type = type; - this.dims = dims; - this.size = size; - } - // #endregion - // #region factory - static async fromImage(image, options) { - return (0, _tensor_factory_impl_js__WEBPACK_IMPORTED_MODULE_1__.tensorFromImage)(image, options); - } - static fromTexture(texture, options) { - return (0, _tensor_factory_impl_js__WEBPACK_IMPORTED_MODULE_1__.tensorFromTexture)(texture, options); - } - static fromGpuBuffer(gpuBuffer, options) { - return (0, _tensor_factory_impl_js__WEBPACK_IMPORTED_MODULE_1__.tensorFromGpuBuffer)(gpuBuffer, options); - } - static fromPinnedBuffer(type, buffer, dims) { - return (0, _tensor_factory_impl_js__WEBPACK_IMPORTED_MODULE_1__.tensorFromPinnedBuffer)(type, buffer, dims); - } - // #endregion - // #region conversions - toDataURL(options) { - return (0, _tensor_conversion_impl_js__WEBPACK_IMPORTED_MODULE_0__.tensorToDataURL)(this, options); - } - toImageData(options) { - return (0, _tensor_conversion_impl_js__WEBPACK_IMPORTED_MODULE_0__.tensorToImageData)(this, options); - } - // #endregion - // #region properties - get data() { - this.ensureValid(); - if (!this.cpuData) { - throw new Error("The data is not on CPU. Use `getData()` to download GPU data to CPU, or use `texture` or `gpuBuffer` property to access the GPU data directly."); - } - return this.cpuData; - } - get location() { - return this.dataLocation; - } - get texture() { - this.ensureValid(); - if (!this.gpuTextureData) { - throw new Error("The data is not stored as a WebGL texture."); - } - return this.gpuTextureData; - } - get gpuBuffer() { - this.ensureValid(); - if (!this.gpuBufferData) { - throw new Error("The data is not stored as a WebGPU buffer."); - } - return this.gpuBufferData; - } - // #endregion - // #region methods - async getData(releaseData) { - this.ensureValid(); - switch (this.dataLocation) { - case "cpu": - case "cpu-pinned": - return this.data; - case "texture": - case "gpu-buffer": { - if (!this.downloader) { - throw new Error("The current tensor is not created with a specified data downloader."); - } - if (this.isDownloading) { - throw new Error("The current tensor is being downloaded."); - } - try { - this.isDownloading = true; - const data = await this.downloader(); - this.downloader = void 0; - this.dataLocation = "cpu"; - this.cpuData = data; - if (releaseData && this.disposer) { - this.disposer(); - this.disposer = void 0; - } - return data; - } finally { - this.isDownloading = false; - } - } - default: - throw new Error(`cannot get data from location: ${this.dataLocation}`); - } - } - dispose() { - if (this.isDownloading) { - throw new Error("The current tensor is being downloaded."); - } - if (this.disposer) { - this.disposer(); - this.disposer = void 0; - } - this.cpuData = void 0; - this.gpuTextureData = void 0; - this.gpuBufferData = void 0; - this.downloader = void 0; - this.isDownloading = void 0; - this.dataLocation = "none"; - } - // #endregion - // #region tensor utilities - ensureValid() { - if (this.dataLocation === "none") { - throw new Error("The tensor is disposed."); - } - } - reshape(dims) { - this.ensureValid(); - if (this.downloader || this.disposer) { - throw new Error("Cannot reshape a tensor that owns GPU resource."); - } - return (0, _tensor_utils_impl_js__WEBPACK_IMPORTED_MODULE_3__.tensorReshape)(this, dims); - } - } - } - ), - /***/ - "./node_modules/onnxruntime-common/dist/esm/tensor-utils-impl.js": ( - /*!***********************************************************************!*\ - !*** ./node_modules/onnxruntime-common/dist/esm/tensor-utils-impl.js ***! - \***********************************************************************/ - /***/ - (__unused_webpack___webpack_module__, __webpack_exports__2, __webpack_require__2) => { - __webpack_require__2.r(__webpack_exports__2); - __webpack_require__2.d(__webpack_exports__2, { - /* harmony export */ - calculateSize: () => ( - /* binding */ - calculateSize - ), - /* harmony export */ - tensorReshape: () => ( - /* binding */ - tensorReshape - ) - /* harmony export */ - }); - var _tensor_impl_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__2( - /*! ./tensor-impl.js */ - "./node_modules/onnxruntime-common/dist/esm/tensor-impl.js" - ); - const calculateSize = (dims) => { - let size = 1; - for (let i = 0; i < dims.length; i++) { - const dim = dims[i]; - if (typeof dim !== "number" || !Number.isSafeInteger(dim)) { - throw new TypeError(`dims[${i}] must be an integer, got: ${dim}`); - } - if (dim < 0) { - throw new RangeError(`dims[${i}] must be a non-negative integer, got: ${dim}`); - } - size *= dim; - } - return size; - }; - const tensorReshape = (tensor, dims) => { - switch (tensor.location) { - case "cpu": - return new _tensor_impl_js__WEBPACK_IMPORTED_MODULE_0__.Tensor(tensor.type, tensor.data, dims); - case "cpu-pinned": - return new _tensor_impl_js__WEBPACK_IMPORTED_MODULE_0__.Tensor({ - location: "cpu-pinned", - data: tensor.data, - type: tensor.type, - dims - }); - case "texture": - return new _tensor_impl_js__WEBPACK_IMPORTED_MODULE_0__.Tensor({ - location: "texture", - texture: tensor.texture, - type: tensor.type, - dims - }); - case "gpu-buffer": - return new _tensor_impl_js__WEBPACK_IMPORTED_MODULE_0__.Tensor({ - location: "gpu-buffer", - gpuBuffer: tensor.gpuBuffer, - type: tensor.type, - dims - }); - default: - throw new Error(`tensorReshape: tensor location ${tensor.location} is not supported`); - } - }; - } - ), - /***/ - "./node_modules/onnxruntime-common/dist/esm/tensor.js": ( - /*!************************************************************!*\ - !*** ./node_modules/onnxruntime-common/dist/esm/tensor.js ***! - \************************************************************/ - /***/ - (__unused_webpack___webpack_module__, __webpack_exports__2, __webpack_require__2) => { - __webpack_require__2.r(__webpack_exports__2); - __webpack_require__2.d(__webpack_exports__2, { - /* harmony export */ - Tensor: () => ( - /* binding */ - Tensor - ) - /* harmony export */ - }); - var _tensor_impl_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__2( - /*! ./tensor-impl.js */ - "./node_modules/onnxruntime-common/dist/esm/tensor-impl.js" - ); - const Tensor = _tensor_impl_js__WEBPACK_IMPORTED_MODULE_0__.Tensor; - } - ), - /***/ - "./node_modules/onnxruntime-common/dist/esm/trace.js": ( - /*!***********************************************************!*\ - !*** ./node_modules/onnxruntime-common/dist/esm/trace.js ***! - \***********************************************************/ - /***/ - (__unused_webpack___webpack_module__, __webpack_exports__2, __webpack_require__2) => { - __webpack_require__2.r(__webpack_exports__2); - __webpack_require__2.d(__webpack_exports__2, { - /* harmony export */ - TRACE: () => ( - /* binding */ - TRACE - ), - /* harmony export */ - TRACE_FUNC_BEGIN: () => ( - /* binding */ - TRACE_FUNC_BEGIN - ), - /* harmony export */ - TRACE_FUNC_END: () => ( - /* binding */ - TRACE_FUNC_END - ) - /* harmony export */ - }); - var _env_impl_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__2( - /*! ./env-impl.js */ - "./node_modules/onnxruntime-common/dist/esm/env-impl.js" - ); - const TRACE = (deviceType, label) => { - if (typeof _env_impl_js__WEBPACK_IMPORTED_MODULE_0__.env.trace === "undefined" ? !_env_impl_js__WEBPACK_IMPORTED_MODULE_0__.env.wasm.trace : !_env_impl_js__WEBPACK_IMPORTED_MODULE_0__.env.trace) { - return; - } - console.timeStamp(`${deviceType}::ORT::${label}`); - }; - const TRACE_FUNC = (msg, extraMsg) => { - var _a; - const stack = ((_a = new Error().stack) == null ? void 0 : _a.split(/\r\n|\r|\n/g)) || []; - let hasTraceFunc = false; - for (let i = 0; i < stack.length; i++) { - if (hasTraceFunc && !stack[i].includes("TRACE_FUNC")) { - let label = `FUNC_${msg}::${stack[i].trim().split(" ")[1]}`; - if (extraMsg) { - label += `::${extraMsg}`; - } - TRACE("CPU", label); - return; - } - if (stack[i].includes("TRACE_FUNC")) { - hasTraceFunc = true; - } - } - }; - const TRACE_FUNC_BEGIN = (extraMsg) => { - if (typeof _env_impl_js__WEBPACK_IMPORTED_MODULE_0__.env.trace === "undefined" ? !_env_impl_js__WEBPACK_IMPORTED_MODULE_0__.env.wasm.trace : !_env_impl_js__WEBPACK_IMPORTED_MODULE_0__.env.trace) { - return; - } - TRACE_FUNC("BEGIN", extraMsg); - }; - const TRACE_FUNC_END = (extraMsg) => { - if (typeof _env_impl_js__WEBPACK_IMPORTED_MODULE_0__.env.trace === "undefined" ? !_env_impl_js__WEBPACK_IMPORTED_MODULE_0__.env.wasm.trace : !_env_impl_js__WEBPACK_IMPORTED_MODULE_0__.env.trace) { - return; - } - TRACE_FUNC("END", extraMsg); - }; - } - ), - /***/ - "./node_modules/onnxruntime-common/dist/esm/training-session-impl.js": ( - /*!***************************************************************************!*\ - !*** ./node_modules/onnxruntime-common/dist/esm/training-session-impl.js ***! - \***************************************************************************/ - /***/ - (__unused_webpack___webpack_module__, __webpack_exports__2, __webpack_require__2) => { - __webpack_require__2.r(__webpack_exports__2); - __webpack_require__2.d(__webpack_exports__2, { - /* harmony export */ - TrainingSession: () => ( - /* binding */ - TrainingSession - ) - /* harmony export */ - }); - var _backend_impl_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__2( - /*! ./backend-impl.js */ - "./node_modules/onnxruntime-common/dist/esm/backend-impl.js" - ); - var _tensor_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__2( - /*! ./tensor.js */ - "./node_modules/onnxruntime-common/dist/esm/tensor.js" - ); - const noBackendErrMsg = "Training backend could not be resolved. Make sure you're using the correct configuration & WebAssembly files."; - class TrainingSession { - constructor(handler, hasOptimizerModel, hasEvalModel) { - this.handler = handler; - this.hasOptimizerModel = hasOptimizerModel; - this.hasEvalModel = hasEvalModel; - } - get trainingInputNames() { - return this.handler.inputNames; - } - get trainingOutputNames() { - return this.handler.outputNames; - } - get evalInputNames() { - if (this.hasEvalModel) { - return this.handler.evalInputNames; - } else { - throw new Error("This training session has no evalModel loaded."); - } - } - get evalOutputNames() { - if (this.hasEvalModel) { - return this.handler.evalOutputNames; - } else { - throw new Error("This training session has no evalModel loaded."); - } - } - static async create(trainingOptions, sessionOptions) { - const evalModel = trainingOptions.evalModel || ""; - const optimizerModel = trainingOptions.optimizerModel || ""; - const options = sessionOptions || {}; - const [backend, optionsWithValidatedEPs] = await (0, _backend_impl_js__WEBPACK_IMPORTED_MODULE_0__.resolveBackendAndExecutionProviders)(options); - if (backend.createTrainingSessionHandler) { - const handler = await backend.createTrainingSessionHandler(trainingOptions.checkpointState, trainingOptions.trainModel, evalModel, optimizerModel, optionsWithValidatedEPs); - return new TrainingSession(handler, !!trainingOptions.optimizerModel, !!trainingOptions.evalModel); - } else { - throw new Error(noBackendErrMsg); - } - } - /** - * Helper function for runTrainStep and future runStep methods that handles the type-narrowing conversion from - * the given parameters to SessionHandler.FetchesType and RunOptions. - * - * @param inputNames the feeds object is checked that they contain all input names in the provided list of input - * names. - * @param outputNames the fetches object is checked that their keys match up with valid names in the list of output - * names. - * @param feeds the required input - * @param arg1 narrowed & converted into the SessionHandler.FetchesType or RunOptions object - * @param arg2 optional RunOptions object. - * @returns - */ - typeNarrowingForRunStep(inputNames, outputNames, feeds, arg1, arg2) { - const fetches = {}; - let options = {}; - if (typeof feeds !== "object" || feeds === null || feeds instanceof _tensor_js__WEBPACK_IMPORTED_MODULE_1__.Tensor || Array.isArray(feeds)) { - throw new TypeError("'feeds' must be an object that use input names as keys and OnnxValue as corresponding values."); - } - let isFetchesEmpty = true; - if (typeof arg1 === "object") { - if (arg1 === null) { - throw new TypeError("Unexpected argument[1]: cannot be null."); - } - if (arg1 instanceof _tensor_js__WEBPACK_IMPORTED_MODULE_1__.Tensor) { - throw new TypeError("'fetches' cannot be a Tensor"); - } - if (Array.isArray(arg1)) { - if (arg1.length === 0) { - throw new TypeError("'fetches' cannot be an empty array."); - } - isFetchesEmpty = false; - for (const name of arg1) { - if (typeof name !== "string") { - throw new TypeError("'fetches' must be a string array or an object."); - } - if (outputNames.indexOf(name) === -1) { - throw new RangeError(`'fetches' contains invalid output name: ${name}.`); - } - fetches[name] = null; - } - if (typeof arg2 === "object" && arg2 !== null) { - options = arg2; - } else if (typeof arg2 !== "undefined") { - throw new TypeError("'options' must be an object."); - } - } else { - let isFetches = false; - const arg1Keys = Object.getOwnPropertyNames(arg1); - for (const name of outputNames) { - if (arg1Keys.indexOf(name) !== -1) { - const v = arg1[name]; - if (v === null || v instanceof _tensor_js__WEBPACK_IMPORTED_MODULE_1__.Tensor) { - isFetches = true; - isFetchesEmpty = false; - fetches[name] = v; - } - } - } - if (isFetches) { - if (typeof arg2 === "object" && arg2 !== null) { - options = arg2; - } else if (typeof arg2 !== "undefined") { - throw new TypeError("'options' must be an object."); - } - } else { - options = arg1; - } - } - } else if (typeof arg1 !== "undefined") { - throw new TypeError("Unexpected argument[1]: must be 'fetches' or 'options'."); - } - for (const name of inputNames) { - if (typeof feeds[name] === "undefined") { - throw new Error(`input '${name}' is missing in 'feeds'.`); - } - } - if (isFetchesEmpty) { - for (const name of outputNames) { - fetches[name] = null; - } - } - return [fetches, options]; - } - /** - * Helper method for runTrainStep and any other runStep methods. Takes the ReturnType result from the SessionHandler - * and changes it into a map of Tensors. - * - * @param results - * @returns - */ - convertHandlerReturnTypeToMapOfTensors(results) { - const returnValue = {}; - for (const key in results) { - if (Object.hasOwnProperty.call(results, key)) { - const result = results[key]; - if (result instanceof _tensor_js__WEBPACK_IMPORTED_MODULE_1__.Tensor) { - returnValue[key] = result; - } else { - returnValue[key] = new _tensor_js__WEBPACK_IMPORTED_MODULE_1__.Tensor(result.type, result.data, result.dims); - } - } - } - return returnValue; - } - async lazyResetGrad() { - await this.handler.lazyResetGrad(); - } - async runTrainStep(feeds, arg1, arg2) { - const [fetches, options] = this.typeNarrowingForRunStep(this.trainingInputNames, this.trainingOutputNames, feeds, arg1, arg2); - const results = await this.handler.runTrainStep(feeds, fetches, options); - return this.convertHandlerReturnTypeToMapOfTensors(results); - } - async runOptimizerStep(options) { - if (this.hasOptimizerModel) { - await this.handler.runOptimizerStep(options || {}); - } else { - throw new Error("This TrainingSession has no OptimizerModel loaded."); - } - } - async runEvalStep(feeds, arg1, arg2) { - if (this.hasEvalModel) { - const [fetches, options] = this.typeNarrowingForRunStep(this.evalInputNames, this.evalOutputNames, feeds, arg1, arg2); - const results = await this.handler.runEvalStep(feeds, fetches, options); - return this.convertHandlerReturnTypeToMapOfTensors(results); - } else { - throw new Error("This TrainingSession has no EvalModel loaded."); - } - } - async getParametersSize(trainableOnly = true) { - return this.handler.getParametersSize(trainableOnly); - } - async loadParametersBuffer(array, trainableOnly = true) { - const paramsSize = await this.getParametersSize(trainableOnly); - if (array.length !== 4 * paramsSize) { - throw new Error("Size of the buffer passed into loadParametersBuffer must match the number of parameters in the model. Please use getParametersSize method to check."); - } - return this.handler.loadParametersBuffer(array, trainableOnly); - } - async getContiguousParameters(trainableOnly = true) { - return this.handler.getContiguousParameters(trainableOnly); - } - async release() { - return this.handler.dispose(); - } - } - } - ), - /***/ - "./node_modules/onnxruntime-common/dist/esm/training-session.js": ( - /*!**********************************************************************!*\ - !*** ./node_modules/onnxruntime-common/dist/esm/training-session.js ***! - \**********************************************************************/ - /***/ - (__unused_webpack___webpack_module__, __webpack_exports__2, __webpack_require__2) => { - __webpack_require__2.r(__webpack_exports__2); - __webpack_require__2.d(__webpack_exports__2, { - /* harmony export */ - TrainingSession: () => ( - /* binding */ - TrainingSession - ) - /* harmony export */ - }); - var _training_session_impl_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__2( - /*! ./training-session-impl.js */ - "./node_modules/onnxruntime-common/dist/esm/training-session-impl.js" - ); - const TrainingSession = _training_session_impl_js__WEBPACK_IMPORTED_MODULE_0__.TrainingSession; - } - ), - /***/ - "./node_modules/onnxruntime-common/dist/esm/version.js": ( - /*!*************************************************************!*\ - !*** ./node_modules/onnxruntime-common/dist/esm/version.js ***! - \*************************************************************/ - /***/ - (__unused_webpack___webpack_module__, __webpack_exports__2, __webpack_require__2) => { - __webpack_require__2.r(__webpack_exports__2); - __webpack_require__2.d(__webpack_exports__2, { - /* harmony export */ - version: () => ( - /* binding */ - version - ) - /* harmony export */ - }); - const version = "1.19.2"; - } - ), - /***/ - "./node_modules/onnxruntime-web/dist/ort.webgpu.bundle.min.mjs": ( - /*!*********************************************************************!*\ - !*** ./node_modules/onnxruntime-web/dist/ort.webgpu.bundle.min.mjs ***! - \*********************************************************************/ - /***/ - (__unused_webpack___webpack_module__, __webpack_exports__2, __webpack_require__2) => { - __webpack_require__2.r(__webpack_exports__2); - __webpack_require__2.d(__webpack_exports__2, { - /* harmony export */ - InferenceSession: () => ( - /* binding */ - Ip - ), - /* harmony export */ - TRACE: () => ( - /* binding */ - Sr - ), - /* harmony export */ - TRACE_FUNC_BEGIN: () => ( - /* binding */ - Le - ), - /* harmony export */ - TRACE_FUNC_END: () => ( - /* binding */ - Ve - ), - /* harmony export */ - Tensor: () => ( - /* binding */ - De - ), - /* harmony export */ - TrainingSession: () => ( - /* binding */ - Ap - ), - /* harmony export */ - "default": () => ( - /* binding */ - Hx - ), - /* harmony export */ - env: () => ( - /* binding */ - _e - ), - /* harmony export */ - registerBackend: () => ( - /* binding */ - St - ) - /* harmony export */ - }); - var Un = Object.defineProperty; - var vp = Object.getOwnPropertyDescriptor; - var $p = Object.getOwnPropertyNames; - var xp = Object.prototype.hasOwnProperty; - var Vn = ((e) => typeof __require < "u" ? __require : typeof Proxy < "u" ? new Proxy(e, { get: (t, r) => (typeof __require < "u" ? __require : t)[r] }) : e)(function(e) { - if (typeof __require < "u") return __require.apply(this, arguments); - throw Error('Dynamic require of "' + e + '" is not supported'); - }); - var V = (e, t) => () => (e && (t = e(e = 0)), t); - var Gt = (e, t) => { - for (var r in t) Un(e, r, { get: t[r], enumerable: true }); - }, Sp = (e, t, r, n) => { - if (t && typeof t == "object" || typeof t == "function") for (let o of $p(t)) !xp.call(e, o) && o !== r && Un(e, o, { get: () => t[o], enumerable: !(n = vp(t, o)) || n.enumerable }); - return e; - }; - var br = (e) => Sp(Un({}, "__esModule", { value: true }), e); - var wr, xt, St, Tp, _r, vr = V(() => { - "use strict"; - wr = /* @__PURE__ */ new Map(), xt = [], St = (e, t, r) => { - if (t && typeof t.init == "function" && typeof t.createInferenceSessionHandler == "function") { - let n = wr.get(e); - if (n === void 0) wr.set(e, { backend: t, priority: r }); - else { - if (n.priority > r) return; - if (n.priority === r && n.backend !== t) throw new Error(`cannot register backend "${e}" using priority ${r}`); - } - if (r >= 0) { - let o = xt.indexOf(e); - o !== -1 && xt.splice(o, 1); - for (let i = 0; i < xt.length; i++) if (wr.get(xt[i]).priority <= r) { - xt.splice(i, 0, e); - return; - } - xt.push(e); - } - return; - } - throw new TypeError("not a valid backend"); - }, Tp = async (e) => { - let t = wr.get(e); - if (!t) return "backend not found."; - if (t.initialized) return t.backend; - if (t.aborted) return t.error; - { - let r = !!t.initPromise; - try { - return r || (t.initPromise = t.backend.init(e)), await t.initPromise, t.initialized = true, t.backend; - } catch (n) { - return r || (t.error = `${n}`, t.aborted = true), t.error; - } finally { - delete t.initPromise; - } - } - }, _r = async (e) => { - let t = e.executionProviders || [], r = t.map((l) => typeof l == "string" ? l : l.name), n = r.length === 0 ? xt : r, o, i = [], a = /* @__PURE__ */ new Set(); - for (let l of n) { - let c = await Tp(l); - typeof c == "string" ? i.push({ name: l, err: c }) : (o || (o = c), o === c && a.add(l)); - } - if (!o) throw new Error(`no available backend found. ERR: ${i.map((l) => `[${l.name}] ${l.err}`).join(", ")}`); - for (let { name: l, err: c } of i) r.includes(l) && console.warn(`removing requested execution provider "${l}" from session options because it is not available: ${c}`); - let d = t.filter((l) => a.has(typeof l == "string" ? l : l.name)); - return [o, new Proxy(e, { get: (l, c) => c === "executionProviders" ? d : Reflect.get(l, c) })]; - }; - }); - var Zi = V(() => { - "use strict"; - vr(); - }); - var Qi, Ji = V(() => { - "use strict"; - Qi = "1.20.0-dev.20241016-2b8fc5529b"; - }); - var ea, We, Nn = V(() => { - "use strict"; - Ji(); - ea = "warning", We = { wasm: {}, webgl: {}, webgpu: {}, versions: { common: Qi }, 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}`); - ea = e; - } - }, get logLevel() { - return ea; - } }; - Object.defineProperty(We, "logLevel", { enumerable: true }); - }); - var _e, ta = V(() => { - "use strict"; - Nn(); - _e = We; - }); - var ra, na, oa = V(() => { - "use strict"; - ra = (e, t) => { - let r = typeof document < "u" ? document.createElement("canvas") : new OffscreenCanvas(1, 1); - r.width = e.dims[3], r.height = e.dims[2]; - let n = r.getContext("2d"); - if (n != null) { - let o, i; - (t == null ? void 0 : t.tensorLayout) !== void 0 && t.tensorLayout === "NHWC" ? (o = e.dims[2], i = e.dims[3]) : (o = e.dims[3], i = e.dims[2]); - let a = (t == null ? void 0 : t.format) !== void 0 ? t.format : "RGB", d = t == null ? void 0 : t.norm, l, c; - d === void 0 || d.mean === void 0 ? l = [255, 255, 255, 255] : typeof d.mean == "number" ? l = [d.mean, d.mean, d.mean, d.mean] : (l = [d.mean[0], d.mean[1], d.mean[2], 0], d.mean[3] !== void 0 && (l[3] = d.mean[3])), d === void 0 || d.bias === void 0 ? c = [0, 0, 0, 0] : typeof d.bias == "number" ? c = [d.bias, d.bias, d.bias, d.bias] : (c = [d.bias[0], d.bias[1], d.bias[2], 0], d.bias[3] !== void 0 && (c[3] = d.bias[3])); - let m = i * o, u = 0, h = m, w = m * 2, g = -1; - a === "RGBA" ? (u = 0, h = m, w = m * 2, g = m * 3) : a === "RGB" ? (u = 0, h = m, w = m * 2) : a === "RBG" && (u = 0, w = m, h = m * 2); - for (let y = 0; y < i; y++) for (let S = 0; S < o; S++) { - let $ = (e.data[u++] - c[0]) * l[0], v = (e.data[h++] - c[1]) * l[1], x = (e.data[w++] - c[2]) * l[2], T = g === -1 ? 255 : (e.data[g++] - c[3]) * l[3]; - n.fillStyle = "rgba(" + $ + "," + v + "," + x + "," + T + ")", n.fillRect(S, y, 1, 1); - } - if ("toDataURL" in r) return r.toDataURL(); - throw new Error("toDataURL is not supported"); - } else throw new Error("Can not access image data"); - }, na = (e, t) => { - let r = typeof document < "u" ? document.createElement("canvas").getContext("2d") : new OffscreenCanvas(1, 1).getContext("2d"), n; - if (r != null) { - let o, i, a; - (t == null ? void 0 : t.tensorLayout) !== void 0 && t.tensorLayout === "NHWC" ? (o = e.dims[2], i = e.dims[1], a = e.dims[3]) : (o = e.dims[3], i = e.dims[2], a = e.dims[1]); - let d = t !== void 0 && t.format !== void 0 ? t.format : "RGB", l = t == null ? void 0 : t.norm, c, m; - l === void 0 || l.mean === void 0 ? c = [255, 255, 255, 255] : typeof l.mean == "number" ? c = [l.mean, l.mean, l.mean, l.mean] : (c = [l.mean[0], l.mean[1], l.mean[2], 255], l.mean[3] !== void 0 && (c[3] = l.mean[3])), l === void 0 || l.bias === void 0 ? m = [0, 0, 0, 0] : typeof l.bias == "number" ? m = [l.bias, l.bias, l.bias, l.bias] : (m = [l.bias[0], l.bias[1], l.bias[2], 0], l.bias[3] !== void 0 && (m[3] = l.bias[3])); - let u = i * o; - if (t !== void 0 && (t.format !== void 0 && a === 4 && t.format !== "RGBA" || a === 3 && t.format !== "RGB" && t.format !== "BGR")) throw new Error("Tensor format doesn't match input tensor dims"); - let h = 4, w = 0, g = 1, y = 2, S = 3, $ = 0, v = u, x = u * 2, T = -1; - d === "RGBA" ? ($ = 0, v = u, x = u * 2, T = u * 3) : d === "RGB" ? ($ = 0, v = u, x = u * 2) : d === "RBG" && ($ = 0, x = u, v = u * 2), n = r.createImageData(o, i); - for (let C = 0; C < i * o; w += h, g += h, y += h, S += h, C++) n.data[w] = (e.data[$++] - m[0]) * c[0], n.data[g] = (e.data[v++] - m[1]) * c[1], n.data[y] = (e.data[x++] - m[2]) * c[2], n.data[S] = T === -1 ? 255 : (e.data[T++] - m[3]) * c[3]; - } else throw new Error("Can not access image data"); - return n; - }; - }); - var Wn, ia, aa, sa, ua, da, la = V(() => { - "use strict"; - $r(); - 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: r, width: n } = t, o = t.norm ?? { mean: 255, bias: 0 }, i, a; - typeof o.mean == "number" ? i = [o.mean, o.mean, o.mean, o.mean] : i = [o.mean[0], o.mean[1], o.mean[2], o.mean[3] ?? 255], typeof o.bias == "number" ? a = [o.bias, o.bias, o.bias, o.bias] : a = [o.bias[0], o.bias[1], o.bias[2], o.bias[3] ?? 0]; - let d = t.format !== void 0 ? t.format : "RGBA", l = t.tensorFormat !== void 0 && t.tensorFormat !== void 0 ? t.tensorFormat : "RGB", c = r * n, m = l === "RGBA" ? new Float32Array(c * 4) : new Float32Array(c * 3), u = 4, h = 0, w = 1, g = 2, y = 3, S = 0, $ = c, v = c * 2, x = -1; - d === "RGB" && (u = 3, h = 0, w = 1, g = 2, y = -1), l === "RGBA" ? x = c * 3 : l === "RBG" ? (S = 0, v = c, $ = c * 2) : l === "BGR" && (v = 0, $ = c, S = c * 2); - for (let C = 0; C < c; C++, h += u, g += u, w += u, y += u) m[S++] = (e[h] + a[0]) / i[0], m[$++] = (e[w] + a[1]) / i[1], m[v++] = (e[g] + a[2]) / i[2], x !== -1 && y !== -1 && (m[x++] = (e[y] + a[3]) / i[3]); - return l === "RGBA" ? new Be("float32", m, [1, 4, r, n]) : new Be("float32", m, [1, 3, r, n]); - }, ia = async (e, t) => { - let r = typeof HTMLImageElement < "u" && e instanceof HTMLImageElement, n = typeof ImageData < "u" && e instanceof ImageData, o = typeof ImageBitmap < "u" && e instanceof ImageBitmap, i = typeof e == "string", a, d = t ?? {}, l = () => { - if (typeof document < "u") return document.createElement("canvas"); - if (typeof OffscreenCanvas < "u") return new OffscreenCanvas(1, 1); - throw new Error("Canvas is not supported"); - }, c = (m) => typeof HTMLCanvasElement < "u" && m instanceof HTMLCanvasElement || m instanceof OffscreenCanvas ? m.getContext("2d") : null; - if (r) { - let m = l(); - m.width = e.width, m.height = e.height; - let u = c(m); - if (u != null) { - let h = e.height, w = e.width; - if (t !== void 0 && t.resizedHeight !== void 0 && t.resizedWidth !== void 0 && (h = t.resizedHeight, w = t.resizedWidth), t !== void 0) { - if (d = t, t.tensorFormat !== void 0) throw new Error("Image input config format must be RGBA for HTMLImageElement"); - d.tensorFormat = "RGBA", d.height = h, d.width = w; - } else d.tensorFormat = "RGBA", d.height = h, d.width = w; - u.drawImage(e, 0, 0), a = u.getImageData(0, 0, w, h).data; - } else throw new Error("Can not access image data"); - } else if (n) { - let m, u; - if (t !== void 0 && t.resizedWidth !== void 0 && t.resizedHeight !== void 0 ? (m = t.resizedHeight, u = t.resizedWidth) : (m = e.height, u = e.width), t !== void 0 && (d = t), d.format = "RGBA", d.height = m, d.width = u, t !== void 0) { - let h = l(); - h.width = u, h.height = m; - let w = c(h); - if (w != null) w.putImageData(e, 0, 0), a = w.getImageData(0, 0, u, m).data; - else throw new Error("Can not access image data"); - } else a = e.data; - } else if (o) { - if (t === void 0) throw new Error("Please provide image config with format for Imagebitmap"); - let m = l(); - m.width = e.width, m.height = e.height; - let u = c(m); - if (u != null) { - let h = e.height, w = e.width; - return u.drawImage(e, 0, 0, w, h), a = u.getImageData(0, 0, w, h).data, d.height = h, d.width = w, Wn(a, d); - } else throw new Error("Can not access image data"); - } else { - if (i) return new Promise((m, u) => { - let h = l(), w = c(h); - if (!e || !w) return u(); - let g = new Image(); - g.crossOrigin = "Anonymous", g.src = e, g.onload = () => { - h.width = g.width, h.height = g.height, w.drawImage(g, 0, 0, h.width, h.height); - let y = w.getImageData(0, 0, h.width, h.height); - d.height = h.height, d.width = h.width, m(Wn(y.data, d)); - }; - }); - throw new Error("Input data provided is not supported - aborted tensor creation"); - } - if (a !== void 0) return Wn(a, d); - throw new Error("Input data provided is not supported - aborted tensor creation"); - }, aa = (e, t) => { - let { width: r, height: n, download: o, dispose: i } = t, a = [1, n, r, 4]; - return new Be({ location: "texture", type: "float32", texture: e, dims: a, download: o, dispose: i }); - }, sa = (e, t) => { - let { dataType: r, dims: n, download: o, dispose: i } = t; - return new Be({ location: "gpu-buffer", type: r ?? "float32", gpuBuffer: e, dims: n, download: o, dispose: i }); - }, ua = (e, t) => { - let { dataType: r, dims: n, download: o, dispose: i } = t; - return new Be({ location: "ml-tensor", type: r ?? "float32", mlTensor: e, dims: n, download: o, dispose: i }); - }, da = (e, t, r) => new Be({ location: "cpu-pinned", type: e, data: t, dims: r ?? [t.length] }); - }); - var Tt, Ft, ca, pa, ma = V(() => { - "use strict"; - Tt = /* @__PURE__ */ new Map([["float32", Float32Array], ["uint8", Uint8Array], ["int8", Int8Array], ["uint16", Uint16Array], ["int16", Int16Array], ["int32", Int32Array], ["bool", Uint8Array], ["float64", Float64Array], ["uint32", Uint32Array], ["int4", Uint8Array], ["uint4", Uint8Array]]), Ft = /* @__PURE__ */ new Map([[Float32Array, "float32"], [Uint8Array, "uint8"], [Int8Array, "int8"], [Uint16Array, "uint16"], [Int16Array, "int16"], [Int32Array, "int32"], [Float64Array, "float64"], [Uint32Array, "uint32"]]), ca = false, pa = () => { - if (!ca) { - ca = true; - let e = typeof BigInt64Array < "u" && BigInt64Array.from, t = typeof BigUint64Array < "u" && BigUint64Array.from, r = typeof Float16Array < "u" && Float16Array.from; - e && (Tt.set("int64", BigInt64Array), Ft.set(BigInt64Array, "int64")), t && (Tt.set("uint64", BigUint64Array), Ft.set(BigUint64Array, "uint64")), r ? (Tt.set("float16", Float16Array), Ft.set(Float16Array, "float16")) : Tt.set("float16", Uint16Array); - } - }; - }); - var fa, ha, ga = V(() => { - "use strict"; - $r(); - fa = (e) => { - let t = 1; - for (let r = 0; r < e.length; r++) { - let n = e[r]; - if (typeof n != "number" || !Number.isSafeInteger(n)) throw new TypeError(`dims[${r}] must be an integer, got: ${n}`); - if (n < 0) throw new RangeError(`dims[${r}] must be a non-negative integer, got: ${n}`); - t *= n; - } - return t; - }, ha = (e, t) => { - switch (e.location) { - case "cpu": - return new Be(e.type, e.data, t); - case "cpu-pinned": - return new Be({ location: "cpu-pinned", data: e.data, type: e.type, dims: t }); - case "texture": - return new Be({ location: "texture", texture: e.texture, type: e.type, dims: t }); - case "gpu-buffer": - return new Be({ location: "gpu-buffer", gpuBuffer: e.gpuBuffer, type: e.type, dims: t }); - case "ml-tensor": - return new Be({ location: "ml-tensor", mlTensor: e.mlTensor, type: e.type, dims: t }); - default: - throw new Error(`tensorReshape: tensor location ${e.location} is not supported`); - } - }; - }); - var Be, $r = V(() => { - "use strict"; - oa(); - la(); - ma(); - ga(); - Be = class { - constructor(t, r, n) { - pa(); - let o, i; - if (typeof t == "object" && "location" in t) switch (this.dataLocation = t.location, o = t.type, i = t.dims, t.location) { - case "cpu-pinned": { - let d = Tt.get(o); - if (!d) throw new TypeError(`unsupported type "${o}" to create tensor from pinned buffer`); - if (!(t.data instanceof d)) throw new TypeError(`buffer should be of type ${d.name}`); - this.cpuData = t.data; - break; - } - case "texture": { - if (o !== "float32") throw new TypeError(`unsupported type "${o}" to create tensor from texture`); - this.gpuTextureData = t.texture, this.downloader = t.download, this.disposer = t.dispose; - break; - } - case "gpu-buffer": { - if (o !== "float32" && o !== "float16" && o !== "int32" && o !== "int64" && o !== "uint32" && o !== "uint8" && o !== "bool" && o !== "uint4" && o !== "int4") throw new TypeError(`unsupported type "${o}" to create tensor from gpu buffer`); - this.gpuBufferData = t.gpuBuffer, this.downloader = t.download, this.disposer = t.dispose; - break; - } - case "ml-tensor": { - if (o !== "float32" && o !== "float16" && o !== "int32" && o !== "int64" && o !== "uint32" && o !== "uint64" && o !== "int8" && o !== "uint8" && o !== "bool") throw new TypeError(`unsupported type "${o}" to create tensor from MLTensor`); - this.mlTensorData = t.mlTensor, this.downloader = t.download, this.disposer = t.dispose; - break; - } - default: - throw new Error(`Tensor constructor: unsupported location '${this.dataLocation}'`); - } - else { - let d, l; - if (typeof t == "string") if (o = t, l = n, t === "string") { - if (!Array.isArray(r)) throw new TypeError("A string tensor's data must be a string array."); - d = r; - } else { - let c = Tt.get(t); - if (c === void 0) throw new TypeError(`Unsupported tensor type: ${t}.`); - if (Array.isArray(r)) { - if (t === "float16" && c === Uint16Array || t === "uint4" || t === "int4") throw new TypeError(`Creating a ${t} tensor from number array is not supported. Please use ${c.name} as data.`); - t === "uint64" || t === "int64" ? d = c.from(r, BigInt) : d = c.from(r); - } else if (r instanceof c) d = r; - else if (r instanceof Uint8ClampedArray) if (t === "uint8") d = Uint8Array.from(r); - else throw new TypeError("A Uint8ClampedArray tensor's data must be type of uint8"); - else throw new TypeError(`A ${o} tensor's data must be type of ${c}`); - } - else if (l = r, Array.isArray(t)) { - if (t.length === 0) throw new TypeError("Tensor type cannot be inferred from an empty array."); - let c = typeof t[0]; - if (c === "string") o = "string", d = t; - else if (c === "boolean") o = "bool", d = Uint8Array.from(t); - else throw new TypeError(`Invalid element type of data array: ${c}.`); - } else if (t instanceof Uint8ClampedArray) o = "uint8", d = Uint8Array.from(t); - else { - let c = Ft.get(t.constructor); - if (c === void 0) throw new TypeError(`Unsupported type for tensor data: ${t.constructor}.`); - o = c, d = t; - } - if (l === void 0) l = [d.length]; - else if (!Array.isArray(l)) throw new TypeError("A tensor's dims must be a number array"); - i = l, this.cpuData = d, this.dataLocation = "cpu"; - } - let a = fa(i); - if (this.cpuData && a !== this.cpuData.length && !((o === "uint4" || o === "int4") && Math.ceil(a / 2) === this.cpuData.length)) throw new Error(`Tensor's size(${a}) does not match data length(${this.cpuData.length}).`); - this.type = o, this.dims = i, this.size = a; - } - static async fromImage(t, r) { - return ia(t, r); - } - static fromTexture(t, r) { - return aa(t, r); - } - static fromGpuBuffer(t, r) { - return sa(t, r); - } - static fromMLTensor(t, r) { - return ua(t, r); - } - static fromPinnedBuffer(t, r, n) { - return da(t, r, n); - } - toDataURL(t) { - return ra(this, t); - } - toImageData(t) { - return na(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 = true; - let r = await this.downloader(); - return this.downloader = void 0, this.dataLocation = "cpu", this.cpuData = r, t && this.disposer && (this.disposer(), this.disposer = void 0), r; - } finally { - this.isDownloading = false; - } - } - default: - throw new Error(`cannot get data from location: ${this.dataLocation}`); - } - } - dispose() { - if (this.isDownloading) throw new Error("The current tensor is being downloaded."); - 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 ha(this, t); - } - }; - }); - var De, xr = V(() => { - "use strict"; - $r(); - De = Be; - }); - var Sr, ya, Le, Ve, Ln = V(() => { - "use strict"; - Nn(); - Sr = (e, t) => { - (typeof We.trace > "u" ? !We.wasm.trace : !We.trace) || console.timeStamp(`${e}::ORT::${t}`); - }, ya = (e, t) => { - var _a2; - let r = ((_a2 = new Error().stack) == null ? void 0 : _a2.split(/\r\n|\r|\n/g)) || [], n = false; - for (let o = 0; o < r.length; o++) { - if (n && !r[o].includes("TRACE_FUNC")) { - let i = `FUNC_${e}::${r[o].trim().split(" ")[1]}`; - t && (i += `::${t}`), Sr("CPU", i); - return; - } - r[o].includes("TRACE_FUNC") && (n = true); - } - }, Le = (e) => { - (typeof We.trace > "u" ? !We.wasm.trace : !We.trace) || ya("BEGIN", e); - }, Ve = (e) => { - (typeof We.trace > "u" ? !We.wasm.trace : !We.trace) || ya("END", e); - }; - }); - var Tr, ba = V(() => { - "use strict"; - vr(); - xr(); - Ln(); - Tr = class e { - constructor(t) { - this.handler = t; - } - async run(t, r, n) { - Le(); - let o = {}, i = {}; - if (typeof t != "object" || t === null || t instanceof De || Array.isArray(t)) throw new TypeError("'feeds' must be an object that use input names as keys and OnnxValue as corresponding values."); - let a = true; - if (typeof r == "object") { - if (r === null) throw new TypeError("Unexpected argument[1]: cannot be null."); - if (r instanceof De) throw new TypeError("'fetches' cannot be a Tensor"); - if (Array.isArray(r)) { - if (r.length === 0) throw new TypeError("'fetches' cannot be an empty array."); - a = false; - for (let c of r) { - if (typeof c != "string") throw new TypeError("'fetches' must be a string array or an object."); - if (this.outputNames.indexOf(c) === -1) throw new RangeError(`'fetches' contains invalid output name: ${c}.`); - o[c] = null; - } - if (typeof n == "object" && n !== null) i = n; - else if (typeof n < "u") throw new TypeError("'options' must be an object."); - } else { - let c = false, m = Object.getOwnPropertyNames(r); - for (let u of this.outputNames) if (m.indexOf(u) !== -1) { - let h = r[u]; - (h === null || h instanceof De) && (c = true, a = false, o[u] = h); - } - if (c) { - if (typeof n == "object" && n !== null) i = n; - else if (typeof n < "u") throw new TypeError("'options' must be an object."); - } else i = r; - } - } else if (typeof r < "u") throw new TypeError("Unexpected argument[1]: must be 'fetches' or 'options'."); - for (let c of this.inputNames) if (typeof t[c] > "u") throw new Error(`input '${c}' is missing in 'feeds'.`); - if (a) for (let c of this.outputNames) o[c] = null; - let d = await this.handler.run(t, o, i), l = {}; - for (let c in d) if (Object.hasOwnProperty.call(d, c)) { - let m = d[c]; - m instanceof De ? l[c] = m : l[c] = new De(m.type, m.data, m.dims); - } - return Ve(), l; - } - async release() { - return this.handler.dispose(); - } - static async create(t, r, n, o) { - Le(); - let i, a = {}; - if (typeof t == "string") { - if (i = t, typeof r == "object" && r !== null) a = r; - else if (typeof r < "u") throw new TypeError("'options' must be an object."); - } else if (t instanceof Uint8Array) { - if (i = t, typeof r == "object" && r !== null) a = r; - else if (typeof r < "u") throw new TypeError("'options' must be an object."); - } else if (t instanceof ArrayBuffer || typeof SharedArrayBuffer < "u" && t instanceof SharedArrayBuffer) { - let m = t, u = 0, h = t.byteLength; - if (typeof r == "object" && r !== null) a = r; - else if (typeof r == "number") { - if (u = r, !Number.isSafeInteger(u)) throw new RangeError("'byteOffset' must be an integer."); - if (u < 0 || u >= m.byteLength) throw new RangeError(`'byteOffset' is out of range [0, ${m.byteLength}).`); - if (h = t.byteLength - u, typeof n == "number") { - if (h = n, !Number.isSafeInteger(h)) throw new RangeError("'byteLength' must be an integer."); - if (h <= 0 || u + h > m.byteLength) throw new RangeError(`'byteLength' is out of range (0, ${m.byteLength - u}].`); - if (typeof o == "object" && o !== null) a = o; - else if (typeof o < "u") throw new TypeError("'options' must be an object."); - } else if (typeof n < "u") throw new TypeError("'byteLength' must be a number."); - } else if (typeof r < "u") throw new TypeError("'options' must be an object."); - i = new Uint8Array(m, u, h); - } else throw new TypeError("Unexpected argument[0]: must be 'path' or 'buffer'."); - let [d, l] = await _r(a), c = await d.createInferenceSessionHandler(i, l); - return Ve(), new e(c); - } - startProfiling() { - this.handler.startProfiling(); - } - endProfiling() { - this.handler.endProfiling(); - } - get inputNames() { - return this.handler.inputNames; - } - get outputNames() { - return this.handler.outputNames; - } - }; - }); - var Ip, wa = V(() => { - "use strict"; - ba(); - Ip = Tr; - }); - var _a = V(() => { - "use strict"; - }); - var va = V(() => { - "use strict"; - }); - var $a = V(() => { - "use strict"; - }); - var xa = V(() => { - "use strict"; - }); - var Cp, Ir, Sa = V(() => { - "use strict"; - vr(); - xr(); - Cp = "Training backend could not be resolved. Make sure you're using the correct configuration & WebAssembly files.", Ir = class e { - constructor(t, r, n) { - this.handler = t, this.hasOptimizerModel = r, this.hasEvalModel = n; - } - get trainingInputNames() { - return this.handler.inputNames; - } - get trainingOutputNames() { - return this.handler.outputNames; - } - get evalInputNames() { - if (this.hasEvalModel) return this.handler.evalInputNames; - throw new Error("This training session has no evalModel loaded."); - } - get evalOutputNames() { - if (this.hasEvalModel) return this.handler.evalOutputNames; - throw new Error("This training session has no evalModel loaded."); - } - static async create(t, r) { - let n = t.evalModel || "", o = t.optimizerModel || "", i = r || {}, [a, d] = await _r(i); - if (a.createTrainingSessionHandler) { - let l = await a.createTrainingSessionHandler(t.checkpointState, t.trainModel, n, o, d); - return new e(l, !!t.optimizerModel, !!t.evalModel); - } else throw new Error(Cp); - } - typeNarrowingForRunStep(t, r, n, o, i) { - let a = {}, d = {}; - if (typeof n != "object" || n === null || n instanceof De || Array.isArray(n)) throw new TypeError("'feeds' must be an object that use input names as keys and OnnxValue as corresponding values."); - let l = true; - if (typeof o == "object") { - if (o === null) throw new TypeError("Unexpected argument[1]: cannot be null."); - if (o instanceof De) throw new TypeError("'fetches' cannot be a Tensor"); - if (Array.isArray(o)) { - if (o.length === 0) throw new TypeError("'fetches' cannot be an empty array."); - l = false; - for (let c of o) { - if (typeof c != "string") throw new TypeError("'fetches' must be a string array or an object."); - if (r.indexOf(c) === -1) throw new RangeError(`'fetches' contains invalid output name: ${c}.`); - a[c] = null; - } - if (typeof i == "object" && i !== null) d = i; - else if (typeof i < "u") throw new TypeError("'options' must be an object."); - } else { - let c = false, m = Object.getOwnPropertyNames(o); - for (let u of r) if (m.indexOf(u) !== -1) { - let h = o[u]; - (h === null || h instanceof De) && (c = true, l = false, a[u] = h); - } - if (c) { - if (typeof i == "object" && i !== null) d = i; - else if (typeof i < "u") throw new TypeError("'options' must be an object."); - } else d = o; - } - } else if (typeof o < "u") throw new TypeError("Unexpected argument[1]: must be 'fetches' or 'options'."); - for (let c of t) if (typeof n[c] > "u") throw new Error(`input '${c}' is missing in 'feeds'.`); - if (l) for (let c of r) a[c] = null; - return [a, d]; - } - convertHandlerReturnTypeToMapOfTensors(t) { - let r = {}; - for (let n in t) if (Object.hasOwnProperty.call(t, n)) { - let o = t[n]; - o instanceof De ? r[n] = o : r[n] = new De(o.type, o.data, o.dims); - } - return r; - } - async lazyResetGrad() { - await this.handler.lazyResetGrad(); - } - async runTrainStep(t, r, n) { - let [o, i] = this.typeNarrowingForRunStep(this.trainingInputNames, this.trainingOutputNames, t, r, n), a = await this.handler.runTrainStep(t, o, i); - return this.convertHandlerReturnTypeToMapOfTensors(a); - } - async runOptimizerStep(t) { - if (this.hasOptimizerModel) await this.handler.runOptimizerStep(t || {}); - else throw new Error("This TrainingSession has no OptimizerModel loaded."); - } - async runEvalStep(t, r, n) { - if (this.hasEvalModel) { - let [o, i] = this.typeNarrowingForRunStep(this.evalInputNames, this.evalOutputNames, t, r, n), a = await this.handler.runEvalStep(t, o, i); - return this.convertHandlerReturnTypeToMapOfTensors(a); - } else throw new Error("This TrainingSession has no EvalModel loaded."); - } - async getParametersSize(t = true) { - return this.handler.getParametersSize(t); - } - async loadParametersBuffer(t, r = true) { - let n = await this.getParametersSize(r); - if (t.length !== 4 * n) throw new Error("Size of the buffer passed into loadParametersBuffer must match the number of parameters in the model. Please use getParametersSize method to check."); - return this.handler.loadParametersBuffer(t, r); - } - async getContiguousParameters(t = true) { - return this.handler.getContiguousParameters(t); - } - async release() { - return this.handler.dispose(); - } - }; - }); - var Ap, Ta = V(() => { - "use strict"; - Sa(); - Ap = Ir; - }); - var Hn = {}; - Gt(Hn, { InferenceSession: () => Ip, TRACE: () => Sr, TRACE_FUNC_BEGIN: () => Le, TRACE_FUNC_END: () => Ve, Tensor: () => De, TrainingSession: () => Ap, env: () => _e, registerBackend: () => St }); - var Ke = V(() => { - "use strict"; - Zi(); - ta(); - wa(); - xr(); - _a(); - va(); - Ln(); - $a(); - xa(); - Ta(); - }); - var Cr = V(() => { - "use strict"; - }); - var ka = {}; - Gt(ka, { default: () => kp }); - var Ca, Aa, kp, Ea = V(() => { - "use strict"; - var _a2; - Gn(); - gt(); - qt(); - Ca = "ort-wasm-proxy-worker", Aa = ((_a2 = globalThis.self) == null ? void 0 : _a2.name) === Ca; - Aa && (self.onmessage = (e) => { - let { type: t, in: r } = e.data; - try { - switch (t) { - case "init-wasm": - Ar(r.wasm).then(() => { - kr(r).then(() => { - postMessage({ type: t }); - }, (n) => { - postMessage({ type: t, err: n }); - }); - }, (n) => { - postMessage({ type: t, err: n }); - }); - break; - case "init-ep": { - let { epName: n, env: o } = r; - Er(o, n).then(() => { - postMessage({ type: t }); - }, (i) => { - postMessage({ type: t, err: i }); - }); - break; - } - case "copy-from": { - let { buffer: n } = r, o = jt(n); - postMessage({ type: t, out: o }); - break; - } - case "create": { - let { model: n, options: o } = r; - Pr(n, o).then((i) => { - postMessage({ type: t, out: i }); - }, (i) => { - postMessage({ type: t, err: i }); - }); - break; - } - case "release": - zr(r), postMessage({ type: t }); - break; - case "run": { - let { sessionId: n, inputIndices: o, inputs: i, outputIndices: a, options: d } = r; - Or(n, o, i, a, new Array(a.length).fill(null), d).then((l) => { - l.some((c) => c[3] !== "cpu") ? postMessage({ type: t, err: "Proxy does not support non-cpu tensor location." }) : postMessage({ type: t, out: l }, Dr([...i, ...l])); - }, (l) => { - postMessage({ type: t, err: l }); - }); - break; - } - case "end-profiling": - Br(r), postMessage({ type: t }); - break; - default: - } - } catch (n) { - postMessage({ type: t, err: n }); - } - }); - kp = Aa ? null : (e) => new Worker(e ?? Ut, { type: "module", name: Ca }); - }); - var za = {}; - Gt(za, { default: () => Ep }); - var Fn, Pa, Ep, Oa = V(() => { - "use strict"; - var _a2; - Pa = (Fn = META_URL, async function(e = {}) { - function t() { - return de.buffer != ce.buffer && Ce(), ce; - } - function r() { - return de.buffer != ce.buffer && Ce(), q; - } - function n() { - return de.buffer != ce.buffer && Ce(), ue; - } - function o() { - return de.buffer != ce.buffer && Ce(), re; - } - function i() { - return de.buffer != ce.buffer && Ce(), ne; - } - function a() { - return de.buffer != ce.buffer && Ce(), oe; - } - function d() { - return de.buffer != ce.buffer && Ce(), U; - } - function l() { - return de.buffer != ce.buffer && Ce(), Re; - } - var c, m, u = Object.assign({}, e), h = new Promise((s, p) => { - c = s, m = p; - }), w = typeof window == "object", g = typeof importScripts == "function", y = g && self.name == "em-pthread"; - u.mountExternalData = (s, p) => { - s.startsWith("./") && (s = s.substring(2)), (u.Fb || (u.Fb = /* @__PURE__ */ new Map())).set(s, p); - }, u.unmountExternalData = () => { - delete u.Fb; - }; - var S = globalThis.SharedArrayBuffer ?? new WebAssembly.Memory({ initial: 0, maximum: 0, shared: true }).buffer.constructor; - let $ = () => { - let s = (f, b, _) => (...I) => { - let O = et, D = b == null ? void 0 : b(); - I = f(...I); - let L = b == null ? void 0 : b(); - return D !== L && (f = L, _(D), b = _ = null), et != O ? new Promise((H, X) => { - En = { resolve: H, reject: X }; - }) : I; - }, p = (f) => async (...b) => { - var _a3; - try { - if (u.Eb) throw Error("Session already started"); - let _ = u.Eb = { fc: b[0], errors: [] }, I = await f(...b); - if (u.Eb !== _) throw Error("Session mismatch"); - (_a3 = u.Gb) == null ? void 0 : _a3.flush(); - let O = _.errors; - if (0 < O.length) { - let D = await Promise.all(O); - if (D = D.filter((L) => L), 0 < D.length) throw Error(D.join(` - `)); - } - return I; - } finally { - u.Eb = null; - } - }; - u._OrtCreateSession = s(u._OrtCreateSession, () => u._OrtCreateSession, (f) => u._OrtCreateSession = f), u._OrtRun = p(s(u._OrtRun, () => u._OrtRun, (f) => u._OrtRun = f)), u._OrtRunWithBinding = p(s(u._OrtRunWithBinding, () => u._OrtRunWithBinding, (f) => u._OrtRunWithBinding = f)), u._OrtBindInput = s(u._OrtBindInput, () => u._OrtBindInput, (f) => u._OrtBindInput = f), $ = void 0; - }; - u.jsepInit = (s, p) => { - if ($ == null ? void 0 : $(), s === "webgpu") { - [u.Gb, u.Ub, u.Yb, u.Nb, u.Xb, u.jb, u.Zb, u.bc, u.Vb, u.Wb, u.$b] = p; - let f = u.Gb; - u.jsepRegisterBuffer = (b, _, I, O) => f.registerBuffer(b, _, I, O), u.jsepGetBuffer = (b) => f.getBuffer(b), u.jsepCreateDownloader = (b, _, I) => f.createDownloader(b, _, I), u.jsepOnReleaseSession = (b) => { - f.onReleaseSession(b); - }, u.jsepOnRunStart = (b) => f.onRunStart(b), u.cc = (b, _) => { - f.upload(b, _); - }; - } else if (s === "webnn") { - [u.Gb, u.ac, u.Ob, u.jsepEnsureTensor, u.dc, u.jsepDownloadTensor] = p, u.jsepReleaseTensorId = u.Ob; - let f = u.Gb; - u.jsepOnRunStart = (b) => f.onRunStart(b), u.jsepRegisterMLContext = (b, _) => { - f.registerMLContext(b, _); - }, u.jsepOnReleaseSession = (b) => { - f.onReleaseSession(b); - }, u.jsepCreateMLTensorDownloader = (b, _) => f.createMLTensorDownloader(b, _), u.jsepRegisterMLTensor = (b, _, I) => f.registerMLTensor(b, _, I); - } - }; - var v, x, T = Object.assign({}, u), C = "./this.program", A = (s, p) => { - throw p; - }, P = ""; - (w || g) && (g ? P = self.location.href : typeof document < "u" && document.currentScript && (P = document.currentScript.src), Fn && (P = Fn), P = P.startsWith("blob:") ? "" : P.substr(0, P.replace(/[?#].*/, "").lastIndexOf("/") + 1), g && (x = (s) => { - var p = new XMLHttpRequest(); - return p.open("GET", s, false), p.responseType = "arraybuffer", p.send(null), new Uint8Array(p.response); - }), v = (s, p, f) => { - var b = new XMLHttpRequest(); - b.open("GET", s, true), b.responseType = "arraybuffer", b.onload = () => { - b.status == 200 || b.status == 0 && b.response ? p(b.response) : f(); - }, b.onerror = f, b.send(null); - }); - var B, N = console.log.bind(console), W = console.error.bind(console), K = N, Z = W; - if (Object.assign(u, T), T = null, y) { - let s = function(p) { - try { - var f = p.data, b = f.cmd; - if (b === "load") { - let _ = []; - self.onmessage = (I) => _.push(I), self.startWorker = () => { - postMessage({ cmd: "loaded" }); - for (let I of _) s(I); - self.onmessage = s; - }; - for (let I of f.handlers) u[I] && !u[I].proxy || (u[I] = (...O) => { - postMessage({ Mb: "callHandler", oc: I, args: O }); - }, I == "print" && (K = u[I]), I == "printErr" && (Z = u[I])); - de = f.wasmMemory, Ce(), ee(f.wasmModule); - } else if (b === "run") { - Bn(f.pthread_ptr, 0, 0, 1, 0, 0), Cn(f.pthread_ptr), ic(), Lo(), se || (Vi(), se = true); - try { - ac(f.start_routine, f.arg); - } catch (_) { - if (_ != "unwind") throw _; - } - } else b === "cancel" ? Rt() && gr(-1) : f.target !== "setimmediate" && (b === "checkMailbox" ? se && sr() : b && (Z(`worker: received unknown command ${b}`), Z(f))); - } catch (_) { - throw Ni(), _; - } - }; - var jh = s, ee, se = false; - Z = function(...p) { - p = p.join(" "), console.error(p); - }, self.alert = function(...p) { - postMessage({ Mb: "alert", text: p.join(" "), qc: Rt() }); - }, u.instantiateWasm = (p, f) => new Promise((b) => { - ee = (_) => { - _ = new WebAssembly.Instance(_, Ro()), f(_), b(); - }; - }), self.onunhandledrejection = (p) => { - throw p.reason || p; - }, self.onmessage = s; - } - u.wasmBinary && (B = u.wasmBinary); - var de, Y, le, ce, q, ue, re, ne, oe, U, G, ye, Re, $e = false; - function Ce() { - var s = de.buffer; - u.HEAP8 = ce = new Int8Array(s), u.HEAP16 = ue = new Int16Array(s), u.HEAPU8 = q = new Uint8Array(s), u.HEAPU16 = re = new Uint16Array(s), u.HEAP32 = ne = new Int32Array(s), u.HEAPU32 = oe = new Uint32Array(s), u.HEAPF32 = U = new Float32Array(s), u.HEAPF64 = Re = new Float64Array(s), u.HEAP64 = G = new BigInt64Array(s), u.HEAPU64 = ye = new BigUint64Array(s); - } - if (!y) { - if (!((de = new WebAssembly.Memory({ initial: 256, maximum: 65536, shared: true })).buffer instanceof S)) throw Z("requested a shared WebAssembly.Memory but the returned buffer is not a SharedArrayBuffer, indicating that while the browser has SharedArrayBuffer it does not have WebAssembly threads support - you may need to set a flag"), Error("bad memory"); - Ce(); - } - var bt = [], Ae = [], Me = [], Ue = 0, zt = null, wt = null; - function zo() { - if (--Ue == 0 && (zt !== null && (clearInterval(zt), zt = null), wt)) { - var s = wt; - wt = null, s(); - } - } - function Ot(s) { - throw Z(s = "Aborted(" + s + ")"), $e = true, le = 1, s = new WebAssembly.RuntimeError(s + ". Build with -sASSERTIONS for more info."), m(s), s; - } - var hn, Oo = (s) => s.startsWith("data:application/octet-stream;base64,"), Bo = (s) => s.startsWith("file://"); - function Do(s) { - if (s == hn && B) return new Uint8Array(B); - if (x) return x(s); - throw "both async and sync fetching of the wasm failed"; - } - function Mo(s, p, f) { - return function(b) { - if (!B && (w || g)) { - if (typeof fetch == "function" && !Bo(b)) return fetch(b, { credentials: "same-origin" }).then((_) => { - if (!_.ok) throw `failed to load wasm binary file at '${b}'`; - return _.arrayBuffer(); - }).catch(() => Do(b)); - if (v) return new Promise((_, I) => { - v(b, (O) => _(new Uint8Array(O)), I); - }); - } - return Promise.resolve().then(() => Do(b)); - }(s).then((b) => WebAssembly.instantiate(b, p)).then(f, (b) => { - Z(`failed to asynchronously prepare wasm: ${b}`), Ot(b); - }); - } - function Ro() { - return { a: { O: oc, Aa: nc, b: uc, aa: qo, B: Yo, qa: Xo, Y: Qo, _: Jo, ra: ei, oa: ti, ha: ri, na: ni, L: oi, Z: ii, W: ai, pa: si, X: ui, wa: dc, F: cc, Q: pc, P: fc, E: gc, u: yc, q: bc, G: wc, A: Ic, R: Cc, ua: Ac, ka: kc, U: Ec, ba: Pc, H: zc, ja: Cn, ta: Oc, t: Bc, x: Rc, o: Uc, l: Nc, c: Tn, n: Wc, j: Gc, w: Fc, p: qc, g: jc, s: Kc, m: Yc, e: Xc, k: Zc, i: Qc, h: Jc, d: ep, ea: tp, fa: rp, ga: np, ca: $i, da: xi, T: op, f: ip, D: ap, I: sp, M: up, y: dp, sa: lp, V: cp, v: Ti, z: pp, N: mp, S: fp, za: hp, ya: gp, la: Ai, ma: ki, $: _n, C: Ei, K: Pi, ia: zi, J: Oi, a: de, xa: wn, va: Mi, r: wp } }; - } - var gn = { 867620: (s, p, f, b, _) => { - if (u === void 0 || !u.Fb) return 1; - if ((s = ze(s >>> 0)).startsWith("./") && (s = s.substring(2)), !(s = u.Fb.get(s))) return 2; - if (b >>>= 0, (p >>>= 0) + (f >>>= 0) > s.byteLength) return 3; - try { - let I = s.subarray(p, p + f); - switch (_) { - case 0: - r().set(I, b >>> 0); - break; - case 1: - u.cc(b, I); - break; - default: - return 4; - } - return 0; - } catch { - return 4; - } - }, 868303: (s, p, f) => { - u.dc(s, r().subarray(p >>> 0, p + f >>> 0)); - }, 868366: () => u.ac(), 868407: (s) => { - u.Ob(s); - }, 868443: () => { - u.Vb(); - }, 868474: () => { - u.Wb(); - }, 868503: () => { - u.$b(); - }, 868528: (s) => u.Ub(s), 868561: (s) => u.Yb(s), 868593: (s, p, f) => { - u.Nb(s, p, f, true); - }, 868632: (s, p, f) => { - u.Nb(s, p, f); - }, 868665: () => typeof wasmOffsetConverter < "u", 868722: (s) => { - u.jb("Abs", s, void 0); - }, 868773: (s) => { - u.jb("Neg", s, void 0); - }, 868824: (s) => { - u.jb("Floor", s, void 0); - }, 868877: (s) => { - u.jb("Ceil", s, void 0); - }, 868929: (s) => { - u.jb("Reciprocal", s, void 0); - }, 868987: (s) => { - u.jb("Sqrt", s, void 0); - }, 869039: (s) => { - u.jb("Exp", s, void 0); - }, 869090: (s) => { - u.jb("Erf", s, void 0); - }, 869141: (s) => { - u.jb("Sigmoid", s, void 0); - }, 869196: (s, p, f) => { - u.jb("HardSigmoid", s, { alpha: p, beta: f }); - }, 869275: (s) => { - u.jb("Log", s, void 0); - }, 869326: (s) => { - u.jb("Sin", s, void 0); - }, 869377: (s) => { - u.jb("Cos", s, void 0); - }, 869428: (s) => { - u.jb("Tan", s, void 0); - }, 869479: (s) => { - u.jb("Asin", s, void 0); - }, 869531: (s) => { - u.jb("Acos", s, void 0); - }, 869583: (s) => { - u.jb("Atan", s, void 0); - }, 869635: (s) => { - u.jb("Sinh", s, void 0); - }, 869687: (s) => { - u.jb("Cosh", s, void 0); - }, 869739: (s) => { - u.jb("Asinh", s, void 0); - }, 869792: (s) => { - u.jb("Acosh", s, void 0); - }, 869845: (s) => { - u.jb("Atanh", s, void 0); - }, 869898: (s) => { - u.jb("Tanh", s, void 0); - }, 869950: (s) => { - u.jb("Not", s, void 0); - }, 870001: (s, p, f) => { - u.jb("Clip", s, { min: p, max: f }); - }, 870070: (s) => { - u.jb("Clip", s, void 0); - }, 870122: (s, p) => { - u.jb("Elu", s, { alpha: p }); - }, 870180: (s) => { - u.jb("Gelu", s, void 0); - }, 870232: (s) => { - u.jb("Relu", s, void 0); - }, 870284: (s, p) => { - u.jb("LeakyRelu", s, { alpha: p }); - }, 870348: (s, p) => { - u.jb("ThresholdedRelu", s, { alpha: p }); - }, 870418: (s, p) => { - u.jb("Cast", s, { to: p }); - }, 870476: (s) => { - u.jb("Add", s, void 0); - }, 870527: (s) => { - u.jb("Sub", s, void 0); - }, 870578: (s) => { - u.jb("Mul", s, void 0); - }, 870629: (s) => { - u.jb("Div", s, void 0); - }, 870680: (s) => { - u.jb("Pow", s, void 0); - }, 870731: (s) => { - u.jb("Equal", s, void 0); - }, 870784: (s) => { - u.jb("Greater", s, void 0); - }, 870839: (s) => { - u.jb("GreaterOrEqual", s, void 0); - }, 870901: (s) => { - u.jb("Less", s, void 0); - }, 870953: (s) => { - u.jb("LessOrEqual", s, void 0); - }, 871012: (s, p, f, b, _) => { - u.jb("ReduceMean", s, { keepDims: !!p, noopWithEmptyAxes: !!f, axes: b ? Array.from(i().subarray(b >>> 0, _ >>> 0)) : [] }); - }, 871171: (s, p, f, b, _) => { - u.jb("ReduceMax", s, { keepDims: !!p, noopWithEmptyAxes: !!f, axes: b ? Array.from(i().subarray(b >>> 0, _ >>> 0)) : [] }); - }, 871329: (s, p, f, b, _) => { - u.jb("ReduceMin", s, { keepDims: !!p, noopWithEmptyAxes: !!f, axes: b ? Array.from(i().subarray(b >>> 0, _ >>> 0)) : [] }); - }, 871487: (s, p, f, b, _) => { - u.jb("ReduceProd", s, { keepDims: !!p, noopWithEmptyAxes: !!f, axes: b ? Array.from(i().subarray(b >>> 0, _ >>> 0)) : [] }); - }, 871646: (s, p, f, b, _) => { - u.jb("ReduceSum", s, { keepDims: !!p, noopWithEmptyAxes: !!f, axes: b ? Array.from(i().subarray(b >>> 0, _ >>> 0)) : [] }); - }, 871804: (s, p, f, b, _) => { - u.jb("ReduceL1", s, { keepDims: !!p, noopWithEmptyAxes: !!f, axes: b ? Array.from(i().subarray(b >>> 0, _ >>> 0)) : [] }); - }, 871961: (s, p, f, b, _) => { - u.jb("ReduceL2", s, { keepDims: !!p, noopWithEmptyAxes: !!f, axes: b ? Array.from(i().subarray(b >>> 0, _ >>> 0)) : [] }); - }, 872118: (s, p, f, b, _) => { - u.jb("ReduceLogSum", s, { keepDims: !!p, noopWithEmptyAxes: !!f, axes: b ? Array.from(i().subarray(b >>> 0, _ >>> 0)) : [] }); - }, 872279: (s, p, f, b, _) => { - u.jb("ReduceSumSquare", s, { keepDims: !!p, noopWithEmptyAxes: !!f, axes: b ? Array.from(i().subarray(b >>> 0, _ >>> 0)) : [] }); - }, 872443: (s, p, f, b, _) => { - u.jb("ReduceLogSumExp", s, { keepDims: !!p, noopWithEmptyAxes: !!f, axes: b ? Array.from(i().subarray(b >>> 0, _ >>> 0)) : [] }); - }, 872607: (s) => { - u.jb("Where", s, void 0); - }, 872660: (s, p, f) => { - u.jb("Transpose", s, { perm: p ? Array.from(i().subarray(p >>> 0, f >>> 0)) : [] }); - }, 872768: (s, p, f, b) => { - u.jb("DepthToSpace", s, { blocksize: p, mode: ze(f), format: b ? "NHWC" : "NCHW" }); - }, 872901: (s, p, f, b) => { - u.jb("DepthToSpace", s, { blocksize: p, mode: ze(f), format: b ? "NHWC" : "NCHW" }); - }, 873034: (s, p, f, b, _, I, O, D, L, H, X, fe, be, z, pe) => { - u.jb("ConvTranspose", s, { format: L ? "NHWC" : "NCHW", autoPad: p, dilations: [f], group: b, kernelShape: [_], pads: [I, O], strides: [D], wIsConst: () => !!t()[H >>> 0], outputPadding: X ? Array.from(i().subarray(X >>> 0, fe >>> 0)) : [], outputShape: be ? Array.from(i().subarray(be >>> 0, z >>> 0)) : [], activation: ze(pe) }); - }, 873435: (s, p, f, b, _, I, O, D, L, H, X, fe, be, z) => { - u.jb("ConvTranspose", s, { format: D ? "NHWC" : "NCHW", autoPad: p, dilations: Array.from(i().subarray(f >>> 0, 2 + (f >>> 0) >>> 0)), group: b, kernelShape: Array.from(i().subarray(_ >>> 0, 2 + (_ >>> 0) >>> 0)), pads: Array.from(i().subarray(I >>> 0, 4 + (I >>> 0) >>> 0)), strides: Array.from(i().subarray(O >>> 0, 2 + (O >>> 0) >>> 0)), wIsConst: () => !!t()[L >>> 0], outputPadding: H ? Array.from(i().subarray(H >>> 0, X >>> 0)) : [], outputShape: fe ? Array.from(i().subarray(fe >>> 0, be >>> 0)) : [], activation: ze(z) }); - }, 874e3: (s, p, f, b, _, I, O, D, L, H, X, fe, be, z, pe) => { - u.jb("ConvTranspose", s, { format: L ? "NHWC" : "NCHW", autoPad: p, dilations: [f], group: b, kernelShape: [_], pads: [I, O], strides: [D], wIsConst: () => !!t()[H >>> 0], outputPadding: X ? Array.from(i().subarray(X >>> 0, fe >>> 0)) : [], outputShape: be ? Array.from(i().subarray(be >>> 0, z >>> 0)) : [], activation: ze(pe) }); - }, 874401: (s, p, f, b, _, I, O, D, L, H, X, fe, be, z) => { - u.jb("ConvTranspose", s, { format: D ? "NHWC" : "NCHW", autoPad: p, dilations: Array.from(i().subarray(f >>> 0, 2 + (f >>> 0) >>> 0)), group: b, kernelShape: Array.from(i().subarray(_ >>> 0, 2 + (_ >>> 0) >>> 0)), pads: Array.from(i().subarray(I >>> 0, 4 + (I >>> 0) >>> 0)), strides: Array.from(i().subarray(O >>> 0, 2 + (O >>> 0) >>> 0)), wIsConst: () => !!t()[L >>> 0], outputPadding: H ? Array.from(i().subarray(H >>> 0, X >>> 0)) : [], outputShape: fe ? Array.from(i().subarray(fe >>> 0, be >>> 0)) : [], activation: ze(z) }); - }, 874966: (s, p) => { - u.jb("GlobalAveragePool", s, { format: p ? "NHWC" : "NCHW" }); - }, 875057: (s, p, f, b, _, I, O, D, L, H, X, fe, be, z) => { - u.jb("AveragePool", s, { format: z ? "NHWC" : "NCHW", auto_pad: p, ceil_mode: f, count_include_pad: b, storage_order: _, dilations: I ? Array.from(i().subarray(I >>> 0, O >>> 0)) : [], kernel_shape: D ? Array.from(i().subarray(D >>> 0, L >>> 0)) : [], pads: H ? Array.from(i().subarray(H >>> 0, X >>> 0)) : [], strides: fe ? Array.from(i().subarray(fe >>> 0, be >>> 0)) : [] }); - }, 875472: (s, p) => { - u.jb("GlobalAveragePool", s, { format: p ? "NHWC" : "NCHW" }); - }, 875563: (s, p, f, b, _, I, O, D, L, H, X, fe, be, z) => { - u.jb("AveragePool", s, { format: z ? "NHWC" : "NCHW", auto_pad: p, ceil_mode: f, count_include_pad: b, storage_order: _, dilations: I ? Array.from(i().subarray(I >>> 0, O >>> 0)) : [], kernel_shape: D ? Array.from(i().subarray(D >>> 0, L >>> 0)) : [], pads: H ? Array.from(i().subarray(H >>> 0, X >>> 0)) : [], strides: fe ? Array.from(i().subarray(fe >>> 0, be >>> 0)) : [] }); - }, 875978: (s, p) => { - u.jb("GlobalMaxPool", s, { format: p ? "NHWC" : "NCHW" }); - }, 876065: (s, p, f, b, _, I, O, D, L, H, X, fe, be, z) => { - u.jb("MaxPool", s, { format: z ? "NHWC" : "NCHW", auto_pad: p, ceil_mode: f, count_include_pad: b, storage_order: _, dilations: I ? Array.from(i().subarray(I >>> 0, O >>> 0)) : [], kernel_shape: D ? Array.from(i().subarray(D >>> 0, L >>> 0)) : [], pads: H ? Array.from(i().subarray(H >>> 0, X >>> 0)) : [], strides: fe ? Array.from(i().subarray(fe >>> 0, be >>> 0)) : [] }); - }, 876476: (s, p) => { - u.jb("GlobalMaxPool", s, { format: p ? "NHWC" : "NCHW" }); - }, 876563: (s, p, f, b, _, I, O, D, L, H, X, fe, be, z) => { - u.jb("MaxPool", s, { format: z ? "NHWC" : "NCHW", auto_pad: p, ceil_mode: f, count_include_pad: b, storage_order: _, dilations: I ? Array.from(i().subarray(I >>> 0, O >>> 0)) : [], kernel_shape: D ? Array.from(i().subarray(D >>> 0, L >>> 0)) : [], pads: H ? Array.from(i().subarray(H >>> 0, X >>> 0)) : [], strides: fe ? Array.from(i().subarray(fe >>> 0, be >>> 0)) : [] }); - }, 876974: (s, p, f, b, _) => { - u.jb("Gemm", s, { alpha: p, beta: f, transA: b, transB: _ }); - }, 877078: (s) => { - u.jb("MatMul", s, void 0); - }, 877132: (s, p, f, b) => { - u.jb("ArgMax", s, { keepDims: !!p, selectLastIndex: !!f, axis: b }); - }, 877240: (s, p, f, b) => { - u.jb("ArgMin", s, { keepDims: !!p, selectLastIndex: !!f, axis: b }); - }, 877348: (s, p) => { - u.jb("Softmax", s, { axis: p }); - }, 877411: (s, p) => { - u.jb("Concat", s, { axis: p }); - }, 877471: (s, p, f, b, _) => { - u.jb("Split", s, { axis: p, numOutputs: f, splitSizes: b ? Array.from(i().subarray(b >>> 0, _ >>> 0)) : [] }); - }, 877611: (s) => { - u.jb("Expand", s, void 0); - }, 877665: (s, p) => { - u.jb("Gather", s, { axis: Number(p) }); - }, 877736: (s, p) => { - u.jb("GatherElements", s, { axis: Number(p) }); - }, 877815: (s, p, f, b, _, I, O, D, L, H, X) => { - u.jb("Resize", s, { antialias: p, axes: f ? Array.from(i().subarray(f >>> 0, b >>> 0)) : [], coordinateTransformMode: ze(_), cubicCoeffA: I, excludeOutside: O, extrapolationValue: D, keepAspectRatioPolicy: ze(L), mode: ze(H), nearestMode: ze(X) }); - }, 878161: (s, p, f, b, _, I, O) => { - u.jb("Slice", s, { starts: p ? Array.from(i().subarray(p >>> 0, f >>> 0)) : [], ends: b ? Array.from(i().subarray(b >>> 0, _ >>> 0)) : [], axes: I ? Array.from(i().subarray(I >>> 0, O >>> 0)) : [] }); - }, 878377: (s) => { - u.jb("Tile", s, void 0); - }, 878429: (s, p, f) => { - u.jb("InstanceNormalization", s, { epsilon: p, format: f ? "NHWC" : "NCHW" }); - }, 878543: (s, p, f) => { - u.jb("InstanceNormalization", s, { epsilon: p, format: f ? "NHWC" : "NCHW" }); - }, 878657: (s) => { - u.jb("Range", s, void 0); - }, 878710: (s, p) => { - u.jb("Einsum", s, { equation: ze(p) }); - }, 878791: (s, p, f, b, _) => { - u.jb("Pad", s, { mode: p, value: f, pads: b ? Array.from(i().subarray(b >>> 0, _ >>> 0)) : [] }); - }, 878918: (s, p, f, b, _, I) => { - u.jb("BatchNormalization", s, { epsilon: p, momentum: f, spatial: !!_, trainingMode: !!b, format: I ? "NHWC" : "NCHW" }); - }, 879087: (s, p, f, b, _, I) => { - u.jb("BatchNormalization", s, { epsilon: p, momentum: f, spatial: !!_, trainingMode: !!b, format: I ? "NHWC" : "NCHW" }); - }, 879256: (s, p, f) => { - u.jb("CumSum", s, { exclusive: Number(p), reverse: Number(f) }); - }, 879353: (s, p, f) => { - u.jb("DequantizeLinear", s, { axis: p, blockSize: f }); - }, 879443: (s, p, f, b, _, I, O, D, L) => { - u.jb("Attention", s, { numHeads: p, isUnidirectional: f, maskFilterValue: b, scale: _, doRotary: I, qkvHiddenSizes: O ? Array.from(i().subarray(Number(D) >>> 0, Number(D) + O >>> 0)) : [], pastPresentShareBuffer: !!L }); - }, 879715: (s) => { - u.jb("BiasAdd", s, void 0); - }, 879770: (s) => { - u.jb("BiasSplitGelu", s, void 0); - }, 879831: (s) => { - u.jb("FastGelu", s, void 0); - }, 879887: (s, p, f, b, _, I, O, D, L, H, X, fe, be, z, pe, Se) => { - u.jb("Conv", s, { format: fe ? "NHWC" : "NCHW", auto_pad: p, dilations: f ? Array.from(i().subarray(f >>> 0, b >>> 0)) : [], group: _, kernel_shape: I ? Array.from(i().subarray(I >>> 0, O >>> 0)) : [], pads: D ? Array.from(i().subarray(D >>> 0, L >>> 0)) : [], strides: H ? Array.from(i().subarray(H >>> 0, X >>> 0)) : [], w_is_const: () => !!t()[be >>> 0], activation: ze(z), activation_params: pe ? Array.from(d().subarray(pe >>> 0, Se >>> 0)) : [] }); - }, 880383: (s) => { - u.jb("Gelu", s, void 0); - }, 880435: (s, p, f, b) => { - u.jb("GroupQueryAttention", s, { numHeads: p, kvNumHeads: f, scale: b }); - }, 880548: (s, p, f, b) => { - u.jb("LayerNormalization", s, { axis: p, epsilon: f, simplified: !!b }); - }, 880659: (s, p, f, b) => { - u.jb("LayerNormalization", s, { axis: p, epsilon: f, simplified: !!b }); - }, 880770: (s, p, f, b, _, I) => { - u.jb("MatMulNBits", s, { k: p, n: f, accuracyLevel: b, bits: _, blockSize: I }); - }, 880897: (s, p, f, b, _, I) => { - u.jb("MultiHeadAttention", s, { numHeads: p, isUnidirectional: f, maskFilterValue: b, scale: _, doRotary: I }); - }, 881056: (s, p) => { - u.jb("QuickGelu", s, { alpha: p }); - }, 881120: (s, p, f, b, _) => { - u.jb("RotaryEmbedding", s, { interleaved: !!p, numHeads: f, rotaryEmbeddingDim: b, scale: _ }); - }, 881259: (s, p, f) => { - u.jb("SkipLayerNormalization", s, { epsilon: p, simplified: !!f }); - }, 881361: (s, p, f) => { - u.jb("SkipLayerNormalization", s, { epsilon: p, simplified: !!f }); - }, 881463: (s, p, f, b) => { - u.jb("GatherBlockQuantized", s, { gatherAxis: p, quantizeAxis: f, blockSize: b }); - }, 881584: (s) => { - u.Zb(s); - }, 881618: (s, p) => u.bc(s, p, u.Eb.fc, u.Eb.errors) }; - function nc(s, p, f) { - return yi(async () => { - await u.Xb(s, p, f); - }); - } - function oc() { - return typeof wasmOffsetConverter < "u"; - } - function yn(s) { - this.name = "ExitStatus", this.message = `Program terminated with exit(${s})`, this.status = s; - } - var bn = (s) => { - s.terminate(), s.onmessage = () => { - }; - }, Uo = (s) => { - pt.length == 0 && (Go(), Ho(pt[0])); - var p = pt.pop(); - if (!p) return 6; - vt.push(p), Qe[s.Ab] = p, p.Ab = s.Ab; - var f = { cmd: "run", start_routine: s.hc, arg: s.Qb, pthread_ptr: s.Ab }; - return p.postMessage(f, s.mc), 0; - }, _t = 0, xe = (s, p, ...f) => { - for (var b = 2 * f.length, _ = Rn(), I = Mn(8 * b), O = I >>> 3, D = 0; D < f.length; D++) { - var L = f[D]; - typeof L == "bigint" ? (G[O + 2 * D] = 1n, G[O + 2 * D + 1] = L) : (G[O + 2 * D] = 0n, l()[O + 2 * D + 1 >>> 0] = L); - } - return s = Wi(s, 0, b, I, p), yr(_), s; - }; - function wn(s) { - if (y) return xe(0, 1, s); - if (le = s, !(0 < _t)) { - for (var p of vt) bn(p); - for (p of pt) bn(p); - pt = [], vt = [], Qe = [], $e = true; - } - A(s, new yn(s)); - } - function Vo(s) { - if (y) return xe(1, 0, s); - _n(s); - } - var _n = (s) => { - if (le = s, y) throw Vo(s), "unwind"; - wn(s); - }, pt = [], vt = [], No = [], Qe = {}, Wo = (s) => { - var p = s.Ab; - delete Qe[p], pt.push(s), vt.splice(vt.indexOf(s), 1), s.Ab = 0, Dn(p); - }; - function Lo() { - No.forEach((s) => s()); - } - var Ho = (s) => new Promise((p) => { - s.onmessage = (_) => { - var I = (_ = _.data).cmd; - if (_.targetThread && _.targetThread != Rt()) { - var O = Qe[_.targetThread]; - O ? O.postMessage(_, _.transferList) : Z(`Internal error! Worker sent a message "${I}" to target pthread ${_.targetThread}, but that thread no longer exists!`); - } else I === "checkMailbox" ? sr() : I === "spawnThread" ? Uo(_) : I === "cleanupThread" ? Wo(Qe[_.thread]) : I === "killThread" ? (_ = _.thread, I = Qe[_], delete Qe[_], bn(I), Dn(_), vt.splice(vt.indexOf(I), 1), I.Ab = 0) : I === "cancelThread" ? Qe[_.thread].postMessage({ cmd: "cancel" }) : I === "loaded" ? (s.loaded = true, p(s)) : I === "alert" ? alert(`Thread ${_.threadId}: ${_.text}`) : _.target === "setimmediate" ? s.postMessage(_) : I === "callHandler" ? u[_.handler](..._.args) : I && Z(`worker sent an unknown command ${I}`); - }, s.onerror = (_) => { - throw Z(`worker sent an error! ${_.filename}:${_.lineno}: ${_.message}`), _; - }; - var f, b = []; - for (f of []) u.hasOwnProperty(f) && b.push(f); - s.postMessage({ cmd: "load", handlers: b, wasmMemory: de, wasmModule: Y }); - }); - function Go() { - var s = new Worker(new URL(META_URL), { type: "module", workerData: "em-pthread", name: "em-pthread" }); - pt.push(s); - } - var ar = (s) => { - for (; 0 < s.length; ) s.shift()(u); - }, ic = () => { - var s = Rt(), p = a()[s + 52 >>> 2 >>> 0]; - s = a()[s + 56 >>> 2 >>> 0], Hi(p, p - s), yr(p); - }, ac = (s, p) => { - _t = 0, s = Gi(s, p), 0 < _t ? le = s : gr(s); - }; - class sc { - constructor(p) { - this.Jb = p - 24; - } - } - function uc(s, p, f) { - var b = new sc(s >>>= 0); - throw p >>>= 0, f >>>= 0, a()[b.Jb + 16 >>> 2 >>> 0] = 0, a()[b.Jb + 4 >>> 2 >>> 0] = p, a()[b.Jb + 8 >>> 2 >>> 0] = f, s; - } - function Fo(s, p, f, b) { - return y ? xe(2, 1, s, p, f, b) : qo(s, p, f, b); - } - function qo(s, p, f, b) { - if (s >>>= 0, p >>>= 0, f >>>= 0, b >>>= 0, S === void 0) return Z("Current environment does not support SharedArrayBuffer, pthreads are not available!"), 6; - var _ = []; - return y && _.length === 0 ? Fo(s, p, f, b) : (s = { hc: f, Ab: s, Qb: b, mc: _ }, y ? (s.Mb = "spawnThread", postMessage(s, _), 0) : Uo(s)); - } - var jo = typeof TextDecoder < "u" ? new TextDecoder("utf8") : void 0, Ko = (s, p, f) => { - var b = (p >>>= 0) + f; - for (f = p; s[f] && !(f >= b); ) ++f; - if (16 < f - p && s.buffer && jo) return jo.decode(s.buffer instanceof S ? s.slice(p, f) : s.subarray(p, f)); - for (b = ""; p < f; ) { - var _ = s[p++]; - if (128 & _) { - var I = 63 & s[p++]; - if ((224 & _) == 192) b += String.fromCharCode((31 & _) << 6 | I); - else { - var O = 63 & s[p++]; - 65536 > (_ = (240 & _) == 224 ? (15 & _) << 12 | I << 6 | O : (7 & _) << 18 | I << 12 | O << 6 | 63 & s[p++]) ? b += String.fromCharCode(_) : (_ -= 65536, b += String.fromCharCode(55296 | _ >> 10, 56320 | 1023 & _)); - } - } else b += String.fromCharCode(_); - } - return b; - }, ze = (s, p) => (s >>>= 0) ? Ko(r(), s, p) : ""; - function Yo(s, p, f) { - return y ? xe(3, 1, s, p, f) : 0; - } - function Xo(s, p) { - if (y) return xe(4, 1, s, p); - } - var vn = (s) => { - for (var p = 0, f = 0; f < s.length; ++f) { - var b = s.charCodeAt(f); - 127 >= b ? p++ : 2047 >= b ? p += 2 : 55296 <= b && 57343 >= b ? (p += 4, ++f) : p += 3; - } - return p; - }, Zo = (s, p, f, b) => { - if (!(0 < b)) return 0; - var _ = f >>>= 0; - b = f + b - 1; - for (var I = 0; I < s.length; ++I) { - var O = s.charCodeAt(I); - if (55296 <= O && 57343 >= O && (O = 65536 + ((1023 & O) << 10) | 1023 & s.charCodeAt(++I)), 127 >= O) { - if (f >= b) break; - p[f++ >>> 0] = O; - } else { - if (2047 >= O) { - if (f + 1 >= b) break; - p[f++ >>> 0] = 192 | O >> 6; - } else { - if (65535 >= O) { - if (f + 2 >= b) break; - p[f++ >>> 0] = 224 | O >> 12; - } else { - if (f + 3 >= b) break; - p[f++ >>> 0] = 240 | O >> 18, p[f++ >>> 0] = 128 | O >> 12 & 63; - } - p[f++ >>> 0] = 128 | O >> 6 & 63; - } - p[f++ >>> 0] = 128 | 63 & O; - } - } - return p[f >>> 0] = 0, f - _; - }, Bt = (s, p, f) => Zo(s, r(), p, f); - function Qo(s, p) { - if (y) return xe(5, 1, s, p); - } - function Jo(s, p, f) { - if (y) return xe(6, 1, s, p, f); - } - function ei(s, p, f) { - return y ? xe(7, 1, s, p, f) : 0; - } - function ti(s, p) { - if (y) return xe(8, 1, s, p); - } - function ri(s, p, f) { - if (y) return xe(9, 1, s, p, f); - } - function ni(s, p, f, b) { - if (y) return xe(10, 1, s, p, f, b); - } - function oi(s, p, f, b) { - if (y) return xe(11, 1, s, p, f, b); - } - function ii(s, p, f, b) { - if (y) return xe(12, 1, s, p, f, b); - } - function ai(s) { - if (y) return xe(13, 1, s); - } - function si(s, p) { - if (y) return xe(14, 1, s, p); - } - function ui(s, p, f) { - if (y) return xe(15, 1, s, p, f); - } - var di, mt, dc = () => { - Ot(""); - }, Je = (s) => { - for (var p = ""; r()[s >>> 0]; ) p += di[r()[s++ >>> 0]]; - return p; - }, $n = {}, xn = {}, lc = {}; - function ut(s, p, f = {}) { - if (!("argPackAdvance" in p)) throw new TypeError("registerType registeredInstance requires argPackAdvance"); - return function(b, _, I = {}) { - var O = _.name; - if (!b) throw new mt(`type "${O}" must have a positive integer typeid pointer`); - if (xn.hasOwnProperty(b)) { - if (I.Sb) return; - throw new mt(`Cannot register type '${O}' twice`); - } - xn[b] = _, delete lc[b], $n.hasOwnProperty(b) && (_ = $n[b], delete $n[b], _.forEach((D) => D())); - }(s, p, f); - } - var li = (s, p, f) => { - switch (p) { - case 1: - return f ? (b) => t()[b >>> 0] : (b) => r()[b >>> 0]; - case 2: - return f ? (b) => n()[b >>> 1 >>> 0] : (b) => o()[b >>> 1 >>> 0]; - case 4: - return f ? (b) => i()[b >>> 2 >>> 0] : (b) => a()[b >>> 2 >>> 0]; - case 8: - return f ? (b) => G[b >>> 3] : (b) => ye[b >>> 3]; - default: - throw new TypeError(`invalid integer width (${p}): ${s}`); - } - }; - function cc(s, p, f) { - f >>>= 0, ut(s >>>= 0, { name: p = Je(p >>> 0), fromWireType: (b) => b, toWireType: function(b, _) { - if (typeof _ != "bigint" && typeof _ != "number") throw _ = _ === null ? "null" : (b = typeof _) == "object" || b === "array" || b === "function" ? _.toString() : "" + _, new TypeError(`Cannot convert "${_}" to ${this.name}`); - return typeof _ == "number" && (_ = BigInt(_)), _; - }, argPackAdvance: ft, readValueFromPointer: li(p, f, p.indexOf("u") == -1), Db: null }); - } - var ft = 8; - function pc(s, p, f, b) { - ut(s >>>= 0, { name: p = Je(p >>> 0), fromWireType: function(_) { - return !!_; - }, toWireType: function(_, I) { - return I ? f : b; - }, argPackAdvance: ft, readValueFromPointer: function(_) { - return this.fromWireType(r()[_ >>> 0]); - }, Db: null }); - } - var Sn = [], dt = []; - function Tn(s) { - 9 < (s >>>= 0) && --dt[s + 1] == 0 && (dt[s] = void 0, Sn.push(s)); - } - var qe = (s) => { - if (!s) throw new mt("Cannot use deleted val. handle = " + s); - return dt[s]; - }, je = (s) => { - switch (s) { - case void 0: - return 2; - case null: - return 4; - case true: - return 6; - case false: - return 8; - default: - let p = Sn.pop() || dt.length; - return dt[p] = s, dt[p + 1] = 1, p; - } - }; - function In(s) { - return this.fromWireType(a()[s >>> 2 >>> 0]); - } - var mc = { name: "emscripten::val", fromWireType: (s) => { - var p = qe(s); - return Tn(s), p; - }, toWireType: (s, p) => je(p), argPackAdvance: ft, readValueFromPointer: In, Db: null }; - function fc(s) { - return ut(s >>> 0, mc); - } - var hc = (s, p) => { - switch (p) { - case 4: - return function(f) { - return this.fromWireType(d()[f >>> 2 >>> 0]); - }; - case 8: - return function(f) { - return this.fromWireType(l()[f >>> 3 >>> 0]); - }; - default: - throw new TypeError(`invalid float width (${p}): ${s}`); - } - }; - function gc(s, p, f) { - f >>>= 0, ut(s >>>= 0, { name: p = Je(p >>> 0), fromWireType: (b) => b, toWireType: (b, _) => _, argPackAdvance: ft, readValueFromPointer: hc(p, f), Db: null }); - } - function yc(s, p, f, b, _) { - if (s >>>= 0, f >>>= 0, p = Je(p >>> 0), _ === -1 && (_ = 4294967295), _ = (D) => D, b === 0) { - var I = 32 - 8 * f; - _ = (D) => D << I >>> I; - } - var O = p.includes("unsigned") ? function(D, L) { - return L >>> 0; - } : function(D, L) { - return L; - }; - ut(s, { name: p, fromWireType: _, toWireType: O, argPackAdvance: ft, readValueFromPointer: li(p, f, b !== 0), Db: null }); - } - function bc(s, p, f) { - function b(I) { - var O = a()[I >>> 2 >>> 0]; - return I = a()[I + 4 >>> 2 >>> 0], new _(t().buffer, I, O); - } - var _ = [Int8Array, Uint8Array, Int16Array, Uint16Array, Int32Array, Uint32Array, Float32Array, Float64Array, BigInt64Array, BigUint64Array][p]; - ut(s >>>= 0, { name: f = Je(f >>> 0), fromWireType: b, argPackAdvance: ft, readValueFromPointer: b }, { Sb: true }); - } - function wc(s, p) { - s >>>= 0; - var f = (p = Je(p >>> 0)) === "std::string"; - ut(s, { name: p, fromWireType: function(b) { - var _ = a()[b >>> 2 >>> 0], I = b + 4; - if (f) for (var O = I, D = 0; D <= _; ++D) { - var L = I + D; - if (D == _ || r()[L >>> 0] == 0) { - if (O = ze(O, L - O), H === void 0) var H = O; - else H += String.fromCharCode(0), H += O; - O = L + 1; - } - } - else { - for (H = Array(_), D = 0; D < _; ++D) H[D] = String.fromCharCode(r()[I + D >>> 0]); - H = H.join(""); - } - return tt(b), H; - }, toWireType: function(b, _) { - _ instanceof ArrayBuffer && (_ = new Uint8Array(_)); - var I = typeof _ == "string"; - if (!(I || _ instanceof Uint8Array || _ instanceof Uint8ClampedArray || _ instanceof Int8Array)) throw new mt("Cannot pass non-string to std::string"); - var O = f && I ? vn(_) : _.length, D = hr(4 + O + 1), L = D + 4; - if (a()[D >>> 2 >>> 0] = O, f && I) Bt(_, L, O + 1); - else if (I) for (I = 0; I < O; ++I) { - var H = _.charCodeAt(I); - if (255 < H) throw tt(L), new mt("String has UTF-16 code units that do not fit in 8 bits"); - r()[L + I >>> 0] = H; - } - else for (I = 0; I < O; ++I) r()[L + I >>> 0] = _[I]; - return b !== null && b.push(tt, D), D; - }, argPackAdvance: ft, readValueFromPointer: In, Db(b) { - tt(b); - } }); - } - var ci = typeof TextDecoder < "u" ? new TextDecoder("utf-8") : void 0, _c = (s, p) => { - for (var f = s >> 1, b = f + p / 2; !(f >= b) && o()[f >>> 0]; ) ++f; - if (32 < (f <<= 1) - s && ci) return ci.decode(r().slice(s, f)); - for (f = "", b = 0; !(b >= p / 2); ++b) { - var _ = n()[s + 2 * b >>> 1 >>> 0]; - if (_ == 0) break; - f += String.fromCharCode(_); - } - return f; - }, vc = (s, p, f) => { - if (f ?? (f = 2147483647), 2 > f) return 0; - var b = p; - f = (f -= 2) < 2 * s.length ? f / 2 : s.length; - for (var _ = 0; _ < f; ++_) { - var I = s.charCodeAt(_); - n()[p >>> 1 >>> 0] = I, p += 2; - } - return n()[p >>> 1 >>> 0] = 0, p - b; - }, $c = (s) => 2 * s.length, xc = (s, p) => { - for (var f = 0, b = ""; !(f >= p / 4); ) { - var _ = i()[s + 4 * f >>> 2 >>> 0]; - if (_ == 0) break; - ++f, 65536 <= _ ? (_ -= 65536, b += String.fromCharCode(55296 | _ >> 10, 56320 | 1023 & _)) : b += String.fromCharCode(_); - } - return b; - }, Sc = (s, p, f) => { - if (p >>>= 0, f ?? (f = 2147483647), 4 > f) return 0; - var b = p; - f = b + f - 4; - for (var _ = 0; _ < s.length; ++_) { - var I = s.charCodeAt(_); - if (55296 <= I && 57343 >= I && (I = 65536 + ((1023 & I) << 10) | 1023 & s.charCodeAt(++_)), i()[p >>> 2 >>> 0] = I, (p += 4) + 4 > f) break; - } - return i()[p >>> 2 >>> 0] = 0, p - b; - }, Tc = (s) => { - for (var p = 0, f = 0; f < s.length; ++f) { - var b = s.charCodeAt(f); - 55296 <= b && 57343 >= b && ++f, p += 4; - } - return p; - }; - function Ic(s, p, f) { - if (s >>>= 0, p >>>= 0, f = Je(f >>>= 0), p === 2) var b = _c, _ = vc, I = $c, O = (D) => o()[D >>> 1 >>> 0]; - else p === 4 && (b = xc, _ = Sc, I = Tc, O = (D) => a()[D >>> 2 >>> 0]); - ut(s, { name: f, fromWireType: (D) => { - for (var L, H = a()[D >>> 2 >>> 0], X = D + 4, fe = 0; fe <= H; ++fe) { - var be = D + 4 + fe * p; - fe != H && O(be) != 0 || (X = b(X, be - X), L === void 0 ? L = X : (L += String.fromCharCode(0), L += X), X = be + p); - } - return tt(D), L; - }, toWireType: (D, L) => { - if (typeof L != "string") throw new mt(`Cannot pass non-string to C++ string type ${f}`); - var H = I(L), X = hr(4 + H + p); - return a()[X >>> 2 >>> 0] = H / p, _(L, X + 4, H + p), D !== null && D.push(tt, X), X; - }, argPackAdvance: ft, readValueFromPointer: In, Db(D) { - tt(D); - } }); - } - function Cc(s, p) { - ut(s >>>= 0, { Tb: true, name: p = Je(p >>> 0), argPackAdvance: 0, fromWireType: () => { - }, toWireType: () => { - } }); - } - var Ac = () => 1; - function kc(s) { - Bn(s >>> 0, !g, 1, !w, 131072, false), Lo(); - } - var pi = (s) => { - if (!$e) try { - if (s(), !(0 < _t)) try { - y ? gr(le) : _n(le); - } catch (p) { - p instanceof yn || p == "unwind" || A(1, p); - } - } catch (p) { - p instanceof yn || p == "unwind" || A(1, p); - } - }; - function Cn(s) { - s >>>= 0, typeof Atomics.nc == "function" && (Atomics.nc(i(), s >>> 2, s).value.then(sr), s += 128, Atomics.store(i(), s >>> 2, 1)); - } - var sr = () => { - var s = Rt(); - s && (Cn(s), pi(Li)); - }; - function Ec(s, p) { - (s >>>= 0) == p >>> 0 ? setTimeout(sr) : y ? postMessage({ targetThread: s, cmd: "checkMailbox" }) : (s = Qe[s]) && s.postMessage({ cmd: "checkMailbox" }); - } - var An = []; - function Pc(s, p, f, b, _) { - for (p >>>= 0, b /= 2, An.length = b, f = _ >>> 0 >>> 3, _ = 0; _ < b; _++) An[_] = G[f + 2 * _] ? G[f + 2 * _ + 1] : l()[f + 2 * _ + 1 >>> 0]; - return (p ? gn[p] : _p[s])(...An); - } - function zc(s) { - s >>>= 0, y ? postMessage({ cmd: "cleanupThread", thread: s }) : Wo(Qe[s]); - } - function Oc(s) { - } - var kn = (s, p) => { - var f = xn[s]; - if (f === void 0) throw s = Ui(s), f = Je(s), tt(s), new mt(`${p} has unknown type ${f}`); - return f; - }, mi = (s, p, f) => { - var b = []; - return s = s.toWireType(b, f), b.length && (a()[p >>> 2 >>> 0] = je(b)), s; - }; - function Bc(s, p, f) { - return p >>>= 0, f >>>= 0, s = qe(s >>> 0), p = kn(p, "emval::as"), mi(p, f, s); - } - var ur = (s) => { - try { - s(); - } catch (p) { - Ot(p); - } - }, ht = 0, et = null, fi = 0, dr = [], hi = {}, gi = {}, Dc = 0, En = null, Mc = []; - function yi(s) { - return function(p) { - if (!$e) { - if (ht === 0) { - var f = false, b = false; - p((_ = 0) => { - if (!$e && (fi = _, f = true, b)) { - ht = 2, ur(() => ji(et)), typeof Browser < "u" && Browser.Kb.Rb && Browser.Kb.resume(), _ = false; - try { - var I = function() { - var L = i()[et + 8 >>> 2 >>> 0]; - return L = j[gi[L]], --_t, L(); - }(); - } catch (L) { - I = L, _ = true; - } - var O = false; - if (!et) { - var D = En; - D && (En = null, (_ ? D.reject : D.resolve)(I), O = true); - } - if (_ && !O) throw I; - } - }), b = true, f || (ht = 1, et = function() { - var _ = hr(65548), I = _ + 12; - a()[_ >>> 2 >>> 0] = I, a()[_ + 4 >>> 2 >>> 0] = I + 65536, I = dr[0]; - var O = hi[I]; - return O === void 0 && (O = Dc++, hi[I] = O, gi[O] = I), I = O, i()[_ + 8 >>> 2 >>> 0] = I, _; - }(), typeof Browser < "u" && Browser.Kb.Rb && Browser.Kb.pause(), ur(() => Fi(et))); - } else ht === 2 ? (ht = 0, ur(Ki), tt(et), et = null, Mc.forEach(pi)) : Ot(`invalid state: ${ht}`); - return fi; - } - }((p) => { - s().then(p); - }); - } - function Rc(s) { - return s >>>= 0, yi(() => (s = qe(s)).then(je)); - } - var lr = []; - function Uc(s, p, f, b) { - return f >>>= 0, b >>>= 0, (s = lr[s >>> 0])(null, p = qe(p >>> 0), f, b); - } - var Vc = {}, cr = (s) => { - var p = Vc[s]; - return p === void 0 ? Je(s) : p; - }; - function Nc(s, p, f, b, _) { - return f >>>= 0, b >>>= 0, _ >>>= 0, (s = lr[s >>> 0])(p = qe(p >>> 0), p[f = cr(f)], b, _); - } - var bi = () => typeof globalThis == "object" ? globalThis : Function("return this")(); - function Wc(s) { - return (s >>>= 0) == 0 ? je(bi()) : (s = cr(s), je(bi()[s])); - } - var Lc = (s) => { - var p = lr.length; - return lr.push(s), p; - }, Hc = (s, p) => { - for (var f = Array(s), b = 0; b < s; ++b) f[b] = kn(a()[p + 4 * b >>> 2 >>> 0], "parameter " + b); - return f; - }, wi = (s, p) => Object.defineProperty(p, "name", { value: s }); - function Gc(s, p, f) { - var b = (p = Hc(s, p >>> 0)).shift(); - s--; - var _ = `return function (obj, func, destructorsRef, args) { - `, I = 0, O = []; - f === 0 && O.push("obj"); - for (var D = ["retType"], L = [b], H = 0; H < s; ++H) O.push("arg" + H), D.push("argType" + H), L.push(p[H]), _ += ` var arg${H} = argType${H}.readValueFromPointer(args${I ? "+" + I : ""}); - `, I += p[H].argPackAdvance; - return _ += ` var rv = ${f === 1 ? "new func" : "func.call"}(${O.join(", ")}); - `, b.Tb || (D.push("emval_returnValue"), L.push(mi), _ += ` return emval_returnValue(retType, destructorsRef, rv); - `), D.push(_ + `}; - `), s = function(X) { - var fe = Function; - if (!(fe instanceof Function)) throw new TypeError(`new_ called with constructor type ${typeof fe} which is not a function`); - var be = wi(fe.name || "unknownFunctionName", function() { - }); - return be.prototype = fe.prototype, be = new be(), (X = fe.apply(be, X)) instanceof Object ? X : be; - }(D)(...L), f = `methodCaller<(${p.map((X) => X.name).join(", ")}) => ${b.name}>`, Lc(wi(f, s)); - } - function Fc(s) { - return s = cr(s >>> 0), je(u[s]); - } - function qc(s, p) { - return p >>>= 0, s = qe(s >>> 0), p = qe(p), je(s[p]); - } - function jc(s) { - 9 < (s >>>= 0) && (dt[s + 1] += 1); - } - function Kc() { - return je([]); - } - function Yc(s) { - s = qe(s >>> 0); - for (var p = Array(s.length), f = 0; f < s.length; f++) p[f] = s[f]; - return je(p); - } - function Xc(s) { - return je(cr(s >>> 0)); - } - function Zc() { - return je({}); - } - function Qc(s) { - for (var p = qe(s >>>= 0); p.length; ) { - var f = p.pop(); - p.pop()(f); - } - Tn(s); - } - function Jc(s, p, f) { - p >>>= 0, f >>>= 0, s = qe(s >>> 0), p = qe(p), f = qe(f), s[p] = f; - } - function ep(s, p) { - return p >>>= 0, s = (s = kn(s >>> 0, "_emval_take_value")).readValueFromPointer(p), je(s); - } - function tp(s, p) { - s = -9007199254740992 > s || 9007199254740992 < s ? NaN : Number(s), p >>>= 0, s = new Date(1e3 * s), i()[p >>> 2 >>> 0] = s.getUTCSeconds(), i()[p + 4 >>> 2 >>> 0] = s.getUTCMinutes(), i()[p + 8 >>> 2 >>> 0] = s.getUTCHours(), i()[p + 12 >>> 2 >>> 0] = s.getUTCDate(), i()[p + 16 >>> 2 >>> 0] = s.getUTCMonth(), i()[p + 20 >>> 2 >>> 0] = s.getUTCFullYear() - 1900, i()[p + 24 >>> 2 >>> 0] = s.getUTCDay(), s = (s.getTime() - Date.UTC(s.getUTCFullYear(), 0, 1, 0, 0, 0, 0)) / 864e5 | 0, i()[p + 28 >>> 2 >>> 0] = s; - } - var Dt = (s) => s % 4 == 0 && (s % 100 != 0 || s % 400 == 0), _i = [0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335], vi = [0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334]; - function rp(s, p) { - s = -9007199254740992 > s || 9007199254740992 < s ? NaN : Number(s), p >>>= 0, s = new Date(1e3 * s), i()[p >>> 2 >>> 0] = s.getSeconds(), i()[p + 4 >>> 2 >>> 0] = s.getMinutes(), i()[p + 8 >>> 2 >>> 0] = s.getHours(), i()[p + 12 >>> 2 >>> 0] = s.getDate(), i()[p + 16 >>> 2 >>> 0] = s.getMonth(), i()[p + 20 >>> 2 >>> 0] = s.getFullYear() - 1900, i()[p + 24 >>> 2 >>> 0] = s.getDay(); - var f = (Dt(s.getFullYear()) ? _i : vi)[s.getMonth()] + s.getDate() - 1 | 0; - i()[p + 28 >>> 2 >>> 0] = f, i()[p + 36 >>> 2 >>> 0] = -60 * s.getTimezoneOffset(), f = new Date(s.getFullYear(), 6, 1).getTimezoneOffset(); - var b = new Date(s.getFullYear(), 0, 1).getTimezoneOffset(); - s = 0 | (f != b && s.getTimezoneOffset() == Math.min(b, f)), i()[p + 32 >>> 2 >>> 0] = s; - } - function np(s) { - s >>>= 0; - var p = new Date(i()[s + 20 >>> 2 >>> 0] + 1900, i()[s + 16 >>> 2 >>> 0], i()[s + 12 >>> 2 >>> 0], i()[s + 8 >>> 2 >>> 0], i()[s + 4 >>> 2 >>> 0], i()[s >>> 2 >>> 0], 0), f = i()[s + 32 >>> 2 >>> 0], b = p.getTimezoneOffset(), _ = new Date(p.getFullYear(), 6, 1).getTimezoneOffset(), I = new Date(p.getFullYear(), 0, 1).getTimezoneOffset(), O = Math.min(I, _); - return 0 > f ? i()[s + 32 >>> 2 >>> 0] = +(_ != I && O == b) : 0 < f != (O == b) && (_ = Math.max(I, _), p.setTime(p.getTime() + 6e4 * ((0 < f ? O : _) - b))), i()[s + 24 >>> 2 >>> 0] = p.getDay(), f = (Dt(p.getFullYear()) ? _i : vi)[p.getMonth()] + p.getDate() - 1 | 0, i()[s + 28 >>> 2 >>> 0] = f, i()[s >>> 2 >>> 0] = p.getSeconds(), i()[s + 4 >>> 2 >>> 0] = p.getMinutes(), i()[s + 8 >>> 2 >>> 0] = p.getHours(), i()[s + 12 >>> 2 >>> 0] = p.getDate(), i()[s + 16 >>> 2 >>> 0] = p.getMonth(), i()[s + 20 >>> 2 >>> 0] = p.getYear(), s = p.getTime(), BigInt(isNaN(s) ? -1 : s / 1e3); - } - function $i(s, p, f, b, _, I, O) { - return y ? xe(16, 1, s, p, f, b, _, I, O) : -52; - } - function xi(s, p, f, b, _, I) { - if (y) return xe(17, 1, s, p, f, b, _, I); - } - function op(s, p, f, b) { - s >>>= 0, p >>>= 0, f >>>= 0, b >>>= 0; - var _ = (/* @__PURE__ */ new Date()).getFullYear(), I = new Date(_, 0, 1), O = new Date(_, 6, 1); - _ = I.getTimezoneOffset(); - var D = O.getTimezoneOffset(), L = Math.max(_, D); - a()[s >>> 2 >>> 0] = 60 * L, i()[p >>> 2 >>> 0] = +(_ != D), I = (s = (H) => H.toLocaleTimeString(void 0, { hour12: false, timeZoneName: "short" }).split(" ")[1])(I), O = s(O), D < _ ? (Bt(I, f, 17), Bt(O, b, 17)) : (Bt(I, b, 17), Bt(O, f, 17)); - } - var Pn = [], Si = (s, p) => { - Pn.length = 0; - for (var f; f = r()[s++ >>> 0]; ) { - var b = f != 105; - p += (b &= f != 112) && p % 8 ? 4 : 0, Pn.push(f == 112 ? a()[p >>> 2 >>> 0] : f == 106 ? G[p >>> 3] : f == 105 ? i()[p >>> 2 >>> 0] : l()[p >>> 3 >>> 0]), p += b ? 8 : 4; - } - return Pn; - }; - function ip(s, p, f) { - return s >>>= 0, p = Si(p >>> 0, f >>> 0), gn[s](...p); - } - function ap(s, p, f) { - return s >>>= 0, p = Si(p >>> 0, f >>> 0), gn[s](...p); - } - var sp = () => { - }, up = () => Date.now(); - function dp(s, p) { - return Z(ze(s >>> 0, p >>> 0)); - } - var Ti, lp = () => { - throw _t += 1, "unwind"; - }; - function cp() { - return 4294901760; - } - Ti = () => performance.timeOrigin + performance.now(); - var pp = () => navigator.hardwareConcurrency; - function mp() { - return Ot("Cannot use emscripten_pc_get_function without -sUSE_OFFSET_CONVERTER"), 0; - } - function fp(s) { - s >>>= 0; - var p = r().length; - if (s <= p || 4294901760 < s) return false; - for (var f = 1; 4 >= f; f *= 2) { - var b = p * (1 + 0.2 / f); - b = Math.min(b, s + 100663296); - var _ = Math; - b = Math.max(s, b); - e: { - _ = (_.min.call(_, 4294901760, b + (65536 - b % 65536) % 65536) - de.buffer.byteLength + 65535) / 65536; - try { - de.grow(_), Ce(); - var I = 1; - break e; - } catch { - } - I = void 0; - } - if (I) return true; - } - return false; - } - var pr = () => (Ot("Cannot use convertFrameToPC (needed by __builtin_return_address) without -sUSE_OFFSET_CONVERTER"), 0), Mt = {}, Ii = (s) => { - s.forEach((p) => { - var f = pr(); - f && (Mt[f] = p); - }); - }; - function hp() { - var s = Error().stack.toString().split(` - `); - return s[0] == "Error" && s.shift(), Ii(s), Mt.Pb = pr(), Mt.ec = s, Mt.Pb; - } - function gp(s, p, f) { - if (s >>>= 0, p >>>= 0, Mt.Pb == s) var b = Mt.ec; - else (b = Error().stack.toString().split(` - `))[0] == "Error" && b.shift(), Ii(b); - for (var _ = 3; b[_] && pr() != s; ) ++_; - for (s = 0; s < f && b[s + _]; ++s) i()[p + 4 * s >>> 2 >>> 0] = pr(); - return s; - } - var zn, On = {}, Ci = () => { - if (!zn) { - var s, p = { USER: "web_user", LOGNAME: "web_user", PATH: "/", PWD: "/", HOME: "/home/web_user", LANG: (typeof navigator == "object" && navigator.languages && navigator.languages[0] || "C").replace("-", "_") + ".UTF-8", _: C || "./this.program" }; - for (s in On) On[s] === void 0 ? delete p[s] : p[s] = On[s]; - var f = []; - for (s in p) f.push(`${s}=${p[s]}`); - zn = f; - } - return zn; - }; - function Ai(s, p) { - if (y) return xe(18, 1, s, p); - s >>>= 0, p >>>= 0; - var f = 0; - return Ci().forEach((b, _) => { - var I = p + f; - for (_ = a()[s + 4 * _ >>> 2 >>> 0] = I, I = 0; I < b.length; ++I) t()[_++ >>> 0] = b.charCodeAt(I); - t()[_ >>> 0] = 0, f += b.length + 1; - }), 0; - } - function ki(s, p) { - if (y) return xe(19, 1, s, p); - s >>>= 0, p >>>= 0; - var f = Ci(); - a()[s >>> 2 >>> 0] = f.length; - var b = 0; - return f.forEach((_) => b += _.length + 1), a()[p >>> 2 >>> 0] = b, 0; - } - function Ei(s) { - return y ? xe(20, 1, s) : 52; - } - function Pi(s, p, f, b) { - return y ? xe(21, 1, s, p, f, b) : 52; - } - function zi(s, p, f, b) { - return y ? xe(22, 1, s, p, f, b) : 70; - } - var yp = [null, [], []]; - function Oi(s, p, f, b) { - if (y) return xe(23, 1, s, p, f, b); - p >>>= 0, f >>>= 0, b >>>= 0; - for (var _ = 0, I = 0; I < f; I++) { - var O = a()[p >>> 2 >>> 0], D = a()[p + 4 >>> 2 >>> 0]; - p += 8; - for (var L = 0; L < D; L++) { - var H = r()[O + L >>> 0], X = yp[s]; - H === 0 || H === 10 ? ((s === 1 ? K : Z)(Ko(X, 0)), X.length = 0) : X.push(H); - } - _ += D; - } - return a()[b >>> 2 >>> 0] = _, 0; - } - var Bi = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31], Di = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31], bp = (s, p) => { - t().set(s, p >>> 0); - }; - function Mi(s, p, f, b) { - function _(z, pe, Se) { - for (z = typeof z == "number" ? z.toString() : z || ""; z.length < pe; ) z = Se[0] + z; - return z; - } - function I(z, pe) { - return _(z, pe, "0"); - } - function O(z, pe) { - function Se(Xi) { - return 0 > Xi ? -1 : 0 < Xi ? 1 : 0; - } - var $t; - return ($t = Se(z.getFullYear() - pe.getFullYear())) === 0 && ($t = Se(z.getMonth() - pe.getMonth())) === 0 && ($t = Se(z.getDate() - pe.getDate())), $t; - } - function D(z) { - switch (z.getDay()) { - case 0: - return new Date(z.getFullYear() - 1, 11, 29); - case 1: - return z; - case 2: - return new Date(z.getFullYear(), 0, 3); - case 3: - return new Date(z.getFullYear(), 0, 2); - case 4: - return new Date(z.getFullYear(), 0, 1); - case 5: - return new Date(z.getFullYear() - 1, 11, 31); - case 6: - return new Date(z.getFullYear() - 1, 11, 30); - } - } - function L(z) { - var pe = z.Bb; - for (z = new Date(new Date(z.Cb + 1900, 0, 1).getTime()); 0 < pe; ) { - var Se = z.getMonth(), $t = (Dt(z.getFullYear()) ? Bi : Di)[Se]; - if (!(pe > $t - z.getDate())) { - z.setDate(z.getDate() + pe); - break; - } - pe -= $t - z.getDate() + 1, z.setDate(1), 11 > Se ? z.setMonth(Se + 1) : (z.setMonth(0), z.setFullYear(z.getFullYear() + 1)); - } - return Se = new Date(z.getFullYear() + 1, 0, 4), pe = D(new Date(z.getFullYear(), 0, 4)), Se = D(Se), 0 >= O(pe, z) ? 0 >= O(Se, z) ? z.getFullYear() + 1 : z.getFullYear() : z.getFullYear() - 1; - } - s >>>= 0, p >>>= 0, f >>>= 0, b >>>= 0; - var H = a()[b + 40 >>> 2 >>> 0]; - for (var X in b = { kc: i()[b >>> 2 >>> 0], jc: i()[b + 4 >>> 2 >>> 0], Hb: i()[b + 8 >>> 2 >>> 0], Lb: i()[b + 12 >>> 2 >>> 0], Ib: i()[b + 16 >>> 2 >>> 0], Cb: i()[b + 20 >>> 2 >>> 0], ub: i()[b + 24 >>> 2 >>> 0], Bb: i()[b + 28 >>> 2 >>> 0], rc: i()[b + 32 >>> 2 >>> 0], ic: i()[b + 36 >>> 2 >>> 0], lc: H ? ze(H) : "" }, f = ze(f), H = { "%c": "%a %b %d %H:%M:%S %Y", "%D": "%m/%d/%y", "%F": "%Y-%m-%d", "%h": "%b", "%r": "%I:%M:%S %p", "%R": "%H:%M", "%T": "%H:%M:%S", "%x": "%m/%d/%y", "%X": "%H:%M:%S", "%Ec": "%c", "%EC": "%C", "%Ex": "%m/%d/%y", "%EX": "%H:%M:%S", "%Ey": "%y", "%EY": "%Y", "%Od": "%d", "%Oe": "%e", "%OH": "%H", "%OI": "%I", "%Om": "%m", "%OM": "%M", "%OS": "%S", "%Ou": "%u", "%OU": "%U", "%OV": "%V", "%Ow": "%w", "%OW": "%W", "%Oy": "%y" }) f = f.replace(new RegExp(X, "g"), H[X]); - var fe = "Sunday Monday Tuesday Wednesday Thursday Friday Saturday".split(" "), be = "January February March April May June July August September October November December".split(" "); - for (X in H = { "%a": (z) => fe[z.ub].substring(0, 3), "%A": (z) => fe[z.ub], "%b": (z) => be[z.Ib].substring(0, 3), "%B": (z) => be[z.Ib], "%C": (z) => I((z.Cb + 1900) / 100 | 0, 2), "%d": (z) => I(z.Lb, 2), "%e": (z) => _(z.Lb, 2, " "), "%g": (z) => L(z).toString().substring(2), "%G": L, "%H": (z) => I(z.Hb, 2), "%I": (z) => ((z = z.Hb) == 0 ? z = 12 : 12 < z && (z -= 12), I(z, 2)), "%j": (z) => { - for (var pe = 0, Se = 0; Se <= z.Ib - 1; pe += (Dt(z.Cb + 1900) ? Bi : Di)[Se++]) ; - return I(z.Lb + pe, 3); - }, "%m": (z) => I(z.Ib + 1, 2), "%M": (z) => I(z.jc, 2), "%n": () => ` - `, "%p": (z) => 0 <= z.Hb && 12 > z.Hb ? "AM" : "PM", "%S": (z) => I(z.kc, 2), "%t": () => " ", "%u": (z) => z.ub || 7, "%U": (z) => I(Math.floor((z.Bb + 7 - z.ub) / 7), 2), "%V": (z) => { - var pe = Math.floor((z.Bb + 7 - (z.ub + 6) % 7) / 7); - if (2 >= (z.ub + 371 - z.Bb - 2) % 7 && pe++, pe) pe == 53 && ((Se = (z.ub + 371 - z.Bb) % 7) == 4 || Se == 3 && Dt(z.Cb) || (pe = 1)); - else { - pe = 52; - var Se = (z.ub + 7 - z.Bb - 1) % 7; - (Se == 4 || Se == 5 && Dt(z.Cb % 400 - 1)) && pe++; - } - return I(pe, 2); - }, "%w": (z) => z.ub, "%W": (z) => I(Math.floor((z.Bb + 7 - (z.ub + 6) % 7) / 7), 2), "%y": (z) => (z.Cb + 1900).toString().substring(2), "%Y": (z) => z.Cb + 1900, "%z": (z) => { - var pe = 0 <= (z = z.ic); - return z = Math.abs(z) / 60, (pe ? "+" : "-") + ("0000" + (z / 60 * 100 + z % 60)).slice(-4); - }, "%Z": (z) => z.lc, "%%": () => "%" }, f = f.replace(/%%/g, "\0\0"), H) f.includes(X) && (f = f.replace(new RegExp(X, "g"), H[X](b))); - return X = function(z) { - var pe = Array(vn(z) + 1); - return Zo(z, pe, 0, pe.length), pe; - }(f = f.replace(/\0\0/g, "%")), X.length > p ? 0 : (bp(X, s), X.length - 1); - } - function wp(s, p, f, b) { - return Mi(s >>> 0, p >>> 0, f >>> 0, b >>> 0); - } - y || function() { - for (var s = u.numThreads - 1; s--; ) Go(); - bt.unshift(() => { - Ue++, function(p) { - y ? p() : Promise.all(pt.map(Ho)).then(p); - }(() => zo()); - }); - }(); - for (var Ri = Array(256), mr = 0; 256 > mr; ++mr) Ri[mr] = String.fromCharCode(mr); - di = Ri, mt = u.BindingError = class extends Error { - constructor(s) { - super(s), this.name = "BindingError"; - } - }, u.InternalError = class extends Error { - constructor(s) { - super(s), this.name = "InternalError"; - } - }, dt.push(0, 1, void 0, 1, null, 1, true, 1, false, 1), u.count_emval_handles = () => dt.length / 2 - 5 - Sn.length; - var _p = [wn, Vo, Fo, Yo, Xo, Qo, Jo, ei, ti, ri, ni, oi, ii, ai, si, ui, $i, xi, Ai, ki, Ei, Pi, zi, Oi], j = function() { - function s(f, b) { - return j = f.exports, j = function() { - var _ = j, I = {}; - for (let [O, D] of Object.entries(_)) I[O] = typeof D == "function" ? (...L) => { - dr.push(O); - try { - return D(...L); - } finally { - $e || (dr.pop(), et && ht === 1 && dr.length === 0 && (ht = 0, _t += 1, ur(qi), typeof Fibers < "u" && Fibers.sc())); - } - } : D; - return I; - }(), j = function() { - var _ = j, I = (D) => (L) => D(L) >>> 0, O = (D) => () => D() >>> 0; - return (_ = Object.assign({}, _)).Ca = I(_.Ca), _.fb = O(_.fb), _.gb = I(_.gb), _.emscripten_main_runtime_thread_id = O(_.emscripten_main_runtime_thread_id), _.sb = I(_.sb), _.tb = O(_.tb), _; - }(), No.push(j.ib), Ae.unshift(j.Ba), Y = b, zo(), j; - } - var p = Ro(); - if (Ue++, u.instantiateWasm) try { - return u.instantiateWasm(p, s); - } catch (f) { - Z(`Module.instantiateWasm callback failed with error: ${f}`), m(f); - } - return hn || (hn = u.locateFile ? Oo("ort-wasm-simd-threaded.jsep.wasm") ? "ort-wasm-simd-threaded.jsep.wasm" : u.locateFile ? u.locateFile("ort-wasm-simd-threaded.jsep.wasm", P) : P + "ort-wasm-simd-threaded.jsep.wasm" : new URL( - /* asset import */ - __webpack_require__2( - /*! ort-wasm-simd-threaded.jsep.wasm */ - "./node_modules/onnxruntime-web/dist/ort-wasm-simd-threaded.jsep.wasm" - ), - __webpack_require__2.b - ).href), function(f, b) { - var _ = hn; - return B || typeof WebAssembly.instantiateStreaming != "function" || Oo(_) || Bo(_) || typeof fetch != "function" ? Mo(_, f, b) : fetch(_, { credentials: "same-origin" }).then((I) => WebAssembly.instantiateStreaming(I, f).then(b, function(O) { - return Z(`wasm streaming compile failed: ${O}`), Z("falling back to ArrayBuffer instantiation"), Mo(_, f, b); - })); - }(p, function(f) { - s(f.instance, f.module); - }).catch(m), {}; - }(), Ui = (s) => (Ui = j.Ca)(s), Vi = () => (Vi = j.Da)(); - u._OrtInit = (s, p) => (u._OrtInit = j.Ea)(s, p), u._OrtGetLastError = (s, p) => (u._OrtGetLastError = j.Fa)(s, p), u._OrtCreateSessionOptions = (s, p, f, b, _, I, O, D, L, H) => (u._OrtCreateSessionOptions = j.Ga)(s, p, f, b, _, I, O, D, L, H), u._OrtAppendExecutionProvider = (s, p) => (u._OrtAppendExecutionProvider = j.Ha)(s, p), u._OrtAddFreeDimensionOverride = (s, p, f) => (u._OrtAddFreeDimensionOverride = j.Ia)(s, p, f), u._OrtAddSessionConfigEntry = (s, p, f) => (u._OrtAddSessionConfigEntry = j.Ja)(s, p, f), u._OrtReleaseSessionOptions = (s) => (u._OrtReleaseSessionOptions = j.Ka)(s), u._OrtCreateSession = (s, p, f) => (u._OrtCreateSession = j.La)(s, p, f), u._OrtReleaseSession = (s) => (u._OrtReleaseSession = j.Ma)(s), u._OrtGetInputOutputCount = (s, p, f) => (u._OrtGetInputOutputCount = j.Na)(s, p, f), u._OrtGetInputName = (s, p) => (u._OrtGetInputName = j.Oa)(s, p), u._OrtGetOutputName = (s, p) => (u._OrtGetOutputName = j.Pa)(s, p), u._OrtFree = (s) => (u._OrtFree = j.Qa)(s), u._OrtCreateTensor = (s, p, f, b, _, I) => (u._OrtCreateTensor = j.Ra)(s, p, f, b, _, I), u._OrtGetTensorData = (s, p, f, b, _) => (u._OrtGetTensorData = j.Sa)(s, p, f, b, _), u._OrtReleaseTensor = (s) => (u._OrtReleaseTensor = j.Ta)(s), u._OrtCreateRunOptions = (s, p, f, b) => (u._OrtCreateRunOptions = j.Ua)(s, p, f, b), u._OrtAddRunConfigEntry = (s, p, f) => (u._OrtAddRunConfigEntry = j.Va)(s, p, f), u._OrtReleaseRunOptions = (s) => (u._OrtReleaseRunOptions = j.Wa)(s), u._OrtCreateBinding = (s) => (u._OrtCreateBinding = j.Xa)(s), u._OrtBindInput = (s, p, f) => (u._OrtBindInput = j.Ya)(s, p, f), u._OrtBindOutput = (s, p, f, b) => (u._OrtBindOutput = j.Za)(s, p, f, b), u._OrtClearBoundOutputs = (s) => (u._OrtClearBoundOutputs = j._a)(s), u._OrtReleaseBinding = (s) => (u._OrtReleaseBinding = j.$a)(s), u._OrtRunWithBinding = (s, p, f, b, _) => (u._OrtRunWithBinding = j.ab)(s, p, f, b, _), u._OrtRun = (s, p, f, b, _, I, O, D) => (u._OrtRun = j.bb)(s, p, f, b, _, I, O, D), u._OrtEndProfiling = (s) => (u._OrtEndProfiling = j.cb)(s), u._JsepOutput = (s, p, f) => (u._JsepOutput = j.db)(s, p, f), u._JsepGetNodeName = (s) => (u._JsepGetNodeName = j.eb)(s); - var fr, Rt = () => (Rt = j.fb)(), hr = u._malloc = (s) => (hr = u._malloc = j.gb)(s), tt = u._free = (s) => (tt = u._free = j.hb)(s), Bn = (s, p, f, b, _, I) => (Bn = j.kb)(s, p, f, b, _, I), Ni = () => (Ni = j.lb)(), Wi = (s, p, f, b, _) => (Wi = j.mb)(s, p, f, b, _), Dn = (s) => (Dn = j.nb)(s), gr = (s) => (gr = j.ob)(s), Li = () => (Li = j.pb)(), Hi = (s, p) => (Hi = j.qb)(s, p), yr = (s) => (yr = j.rb)(s), Mn = (s) => (Mn = j.sb)(s), Rn = () => (Rn = j.tb)(), Gi = u.dynCall_ii = (s, p) => (Gi = u.dynCall_ii = j.vb)(s, p), Fi = (s) => (Fi = j.wb)(s), qi = () => (qi = j.xb)(), ji = (s) => (ji = j.yb)(s), Ki = () => (Ki = j.zb)(); - function Yi() { - 0 < Ue || (y ? (c(u), y || ar(Ae), startWorker(u)) : (ar(bt), 0 < Ue || fr || (fr = true, u.calledRun = true, $e || (y || ar(Ae), c(u), y || ar(Me))))); - } - return u.___start_em_js = 881730, u.___stop_em_js = 881952, u.stackSave = () => Rn(), u.stackRestore = (s) => yr(s), u.stackAlloc = (s) => Mn(s), u.UTF8ToString = ze, u.stringToUTF8 = Bt, u.lengthBytesUTF8 = vn, wt = function s() { - fr || Yi(), fr || (wt = s); - }, Yi(), h; - }), Ep = Pa; - ((_a2 = globalThis.self) == null ? void 0 : _a2.name) === "em-pthread" && Pa(); - }); - var Ut, Pp, zp, Op, Ba, Da, Bp, Ma, qt = V(() => { - "use strict"; - var _a2, _b; - Cr(); - Ut = false ? 0 : META_URL ?? (typeof document < "u" ? (_a2 = document.currentScript) == null ? void 0 : _a2.src : typeof self < "u" ? (_b = self.location) == null ? void 0 : _b.href : void 0), Pp = typeof location > "u" ? void 0 : location.origin, zp = (e, t) => { - try { - let r = t ?? Ut; - return (r ? new URL(e, r) : new URL(e)).origin === Pp; - } catch { - return false; - } - }, Op = async (e) => { - let r = await (await fetch(e, { credentials: "same-origin" })).blob(); - return URL.createObjectURL(r); - }, Ba = (Ea(), br(ka)).default, Da = async () => { - if (!Ut) throw new Error("Failed to load proxy worker: cannot determine the script source URL."); - if (zp(Ut)) return [void 0, Ba()]; - let e = await Op(Ut); - return [e, Ba(e)]; - }, Bp = (Oa(), br(za)).default, Ma = async (e, t, r) => [void 0, Bp]; - }); - var qn, jn, Mr, Ra, Dp, Mp, Ar, Te, gt = V(() => { - "use strict"; - qt(); - jn = false, Mr = false, Ra = false, Dp = () => { - if (typeof SharedArrayBuffer > "u") return false; - 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 false; - } - }, Mp = () => { - try { - return WebAssembly.validate(new Uint8Array([0, 97, 115, 109, 1, 0, 0, 0, 1, 4, 1, 96, 0, 0, 3, 2, 1, 0, 10, 30, 1, 28, 0, 65, 0, 253, 15, 253, 12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 253, 186, 1, 26, 11])); - } catch { - return false; - } - }, Ar = async (e) => { - if (jn) return Promise.resolve(); - if (Mr) throw new Error("multiple calls to 'initializeWebAssembly()' detected."); - if (Ra) throw new Error("previous call to 'initializeWebAssembly()' failed."); - Mr = true; - let t = e.initTimeout, r = e.numThreads; - if (!Mp()) throw new Error("WebAssembly SIMD is not supported in the current environment."); - let n = Dp(); - r > 1 && !n && (typeof self < "u" && !self.crossOriginIsolated && console.warn("env.wasm.numThreads is set to " + r + ", but this will not work unless you enable crossOriginIsolated mode. See https://web.dev/cross-origin-isolation-guide/ for more info."), console.warn("WebAssembly multi-threading is not supported in the current environment. Falling back to single-threading."), e.numThreads = r = 1); - let o = e.wasmPaths, i = typeof o == "string" ? o : void 0, a = o == null ? void 0 : o.mjs, d = (a == null ? void 0 : a.href) ?? a, l = o == null ? void 0 : o.wasm, c = (l == null ? void 0 : l.href) ?? l, m = e.wasmBinary, [u, h] = await Ma(d, i, r > 1), w = false, g = []; - if (t > 0 && g.push(new Promise((y) => { - setTimeout(() => { - w = true, y(); - }, t); - })), g.push(new Promise((y, S) => { - let $ = { numThreads: r }; - m ? $.wasmBinary = m : (c || i) && ($.locateFile = (v, x) => c ?? (i ?? x) + v), h($).then((v) => { - Mr = false, jn = true, qn = v, y(), u && URL.revokeObjectURL(u); - }, (v) => { - Mr = false, Ra = true, S(v); - }); - })), await Promise.race(g), w) throw new Error(`WebAssembly backend initializing failed due to timeout: ${t}ms`); - }, Te = () => { - if (jn && qn) return qn; - throw new Error("WebAssembly is not initialized yet."); - }; - }); - var ke, Kt, ve, Rr = V(() => { - "use strict"; - gt(); - ke = (e, t) => { - let r = Te(), n = r.lengthBytesUTF8(e) + 1, o = r._malloc(n); - return r.stringToUTF8(e, o, n), t.push(o), o; - }, Kt = (e, t, r, n) => { - if (typeof e == "object" && e !== null) { - if (r.has(e)) throw new Error("Circular reference in options"); - r.add(e); - } - Object.entries(e).forEach(([o, i]) => { - let a = t ? t + o : o; - if (typeof i == "object") Kt(i, a + ".", r, n); - else if (typeof i == "string" || typeof i == "number") n(a, i.toString()); - else if (typeof i == "boolean") n(a, i ? "1" : "0"); - else throw new Error(`Can't handle extra config type: ${typeof i}`); - }); - }, ve = (e) => { - let t = Te(), r = t.stackSave(); - try { - let n = t.stackAlloc(8); - t._OrtGetLastError(n, n + 4); - let o = t.HEAP32[n / 4], i = t.HEAPU32[n / 4 + 1], a = i ? t.UTF8ToString(i) : ""; - throw new Error(`${e} ERROR_CODE: ${o}, ERROR_MESSAGE: ${a}`); - } finally { - t.stackRestore(r); - } - }; - }); - var Ua, Va = V(() => { - "use strict"; - gt(); - Rr(); - Ua = (e) => { - let t = Te(), r = 0, n = [], o = e || {}; - try { - if ((e == null ? void 0 : 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 == null ? void 0 : 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 == null ? void 0 : e.terminate) === void 0 && (o.terminate = false); - let i = 0; - return (e == null ? void 0 : e.tag) !== void 0 && (i = ke(e.tag, n)), r = t._OrtCreateRunOptions(o.logSeverityLevel, o.logVerbosityLevel, !!o.terminate, i), r === 0 && ve("Can't create run options."), (e == null ? void 0 : e.extra) !== void 0 && Kt(e.extra, "", /* @__PURE__ */ new WeakSet(), (a, d) => { - let l = ke(a, n), c = ke(d, n); - t._OrtAddRunConfigEntry(r, l, c) !== 0 && ve(`Can't set a run config entry: ${a} - ${d}.`); - }), [r, n]; - } catch (i) { - throw r !== 0 && t._OrtReleaseRunOptions(r), n.forEach((a) => t._free(a)), i; - } - }; - }); - var Rp, Up, Vp, Np, Na, Wa = V(() => { - "use strict"; - gt(); - Rr(); - Rp = (e) => { - switch (e) { - case "disabled": - return 0; - case "basic": - return 1; - case "extended": - return 2; - case "all": - return 99; - default: - throw new Error(`unsupported graph optimization level: ${e}`); - } - }, Up = (e) => { - switch (e) { - case "sequential": - return 0; - case "parallel": - return 1; - default: - throw new Error(`unsupported execution mode: ${e}`); - } - }, Vp = (e) => { - e.extra || (e.extra = {}), e.extra.session || (e.extra.session = {}); - let t = e.extra.session; - t.use_ort_model_bytes_directly || (t.use_ort_model_bytes_directly = "1"), e.executionProviders && e.executionProviders.some((r) => (typeof r == "string" ? r : r.name) === "webgpu") && (e.enableMemPattern = false); - }, Np = (e, t, r) => { - for (let n of t) { - let o = typeof n == "string" ? n : n.name; - switch (o) { - case "webnn": - if (o = "WEBNN", typeof n != "string") { - let d = n == null ? void 0 : n.deviceType; - if (d) { - let l = ke("deviceType", r), c = ke(d, r); - Te()._OrtAddSessionConfigEntry(e, l, c) !== 0 && ve(`Can't set a session config entry: 'deviceType' - ${d}.`); - } - } - break; - case "webgpu": - if (o = "JS", typeof n != "string") { - let a = n; - if (a == null ? void 0 : a.preferredLayout) { - if (a.preferredLayout !== "NCHW" && a.preferredLayout !== "NHWC") throw new Error(`preferredLayout must be either 'NCHW' or 'NHWC': ${a.preferredLayout}`); - let d = ke("preferredLayout", r), l = ke(a.preferredLayout, r); - Te()._OrtAddSessionConfigEntry(e, d, l) !== 0 && ve(`Can't set a session config entry: 'preferredLayout' - ${a.preferredLayout}.`); - } - } - break; - case "wasm": - case "cpu": - continue; - default: - throw new Error(`not supported execution provider: ${o}`); - } - let i = ke(o, r); - Te()._OrtAppendExecutionProvider(e, i) !== 0 && ve(`Can't append execution provider: ${o}.`); - } - }, Na = (e) => { - let t = Te(), r = 0, n = [], o = e || {}; - Vp(o); - try { - let i = Rp(o.graphOptimizationLevel ?? "all"), a = Up(o.executionMode ?? "sequential"), d = typeof o.logId == "string" ? ke(o.logId, n) : 0, l = o.logSeverityLevel ?? 2; - if (!Number.isInteger(l) || l < 0 || l > 4) throw new Error(`log serverity level is not valid: ${l}`); - let c = o.logVerbosityLevel ?? 0; - if (!Number.isInteger(c) || c < 0 || c > 4) throw new Error(`log verbosity level is not valid: ${c}`); - let m = typeof o.optimizedModelFilePath == "string" ? ke(o.optimizedModelFilePath, n) : 0; - if (r = t._OrtCreateSessionOptions(i, !!o.enableCpuMemArena, !!o.enableMemPattern, a, !!o.enableProfiling, 0, d, l, c, m), r === 0 && ve("Can't create session options."), o.executionProviders && Np(r, o.executionProviders, n), o.enableGraphCapture !== void 0) { - if (typeof o.enableGraphCapture != "boolean") throw new Error(`enableGraphCapture must be a boolean value: ${o.enableGraphCapture}`); - let u = ke("enableGraphCapture", n), h = ke(o.enableGraphCapture.toString(), n); - t._OrtAddSessionConfigEntry(r, u, h) !== 0 && ve(`Can't set a session config entry: 'enableGraphCapture' - ${o.enableGraphCapture}.`); - } - if (o.freeDimensionOverrides) for (let [u, h] of Object.entries(o.freeDimensionOverrides)) { - if (typeof u != "string") throw new Error(`free dimension override name must be a string: ${u}`); - if (typeof h != "number" || !Number.isInteger(h) || h < 0) throw new Error(`free dimension override value must be a non-negative integer: ${h}`); - let w = ke(u, n); - t._OrtAddFreeDimensionOverride(r, w, h) !== 0 && ve(`Can't set a free dimension override: ${u} - ${h}.`); - } - return o.extra !== void 0 && Kt(o.extra, "", /* @__PURE__ */ new WeakSet(), (u, h) => { - let w = ke(u, n), g = ke(h, n); - t._OrtAddSessionConfigEntry(r, w, g) !== 0 && ve(`Can't set a session config entry: ${u} - ${h}.`); - }), [r, n]; - } catch (i) { - throw r !== 0 && t._OrtReleaseSessionOptions(r), n.forEach((a) => t._free(a)), i; - } - }; - }); - var Yt, yt, It, Ur, Xt, Vr, Nr, Kn, Q = V(() => { - "use strict"; - Yt = (e) => { - switch (e) { - case "int8": - return 3; - case "uint8": - return 2; - case "bool": - return 9; - case "int16": - return 5; - case "uint16": - return 4; - case "int32": - return 6; - case "uint32": - return 12; - case "float16": - return 10; - case "float32": - return 1; - case "float64": - return 11; - case "string": - return 8; - case "int64": - return 7; - case "uint64": - return 13; - case "int4": - return 22; - case "uint4": - return 21; - default: - throw new Error(`unsupported data type: ${e}`); - } - }, yt = (e) => { - switch (e) { - case 3: - return "int8"; - case 2: - return "uint8"; - case 9: - return "bool"; - case 5: - return "int16"; - case 4: - return "uint16"; - case 6: - return "int32"; - case 12: - return "uint32"; - case 10: - return "float16"; - case 1: - return "float32"; - case 11: - return "float64"; - case 8: - return "string"; - case 7: - return "int64"; - case 13: - return "uint64"; - case 22: - return "int4"; - case 21: - return "uint4"; - default: - throw new Error(`unsupported data type: ${e}`); - } - }, It = (e, t) => { - let r = [-1, 4, 1, 1, 2, 2, 4, 8, -1, 1, 2, 8, 4, 8, -1, -1, -1, -1, -1, -1, -1, 0.5, 0.5][e], n = typeof t == "number" ? t : t.reduce((o, i) => o * i, 1); - return r > 0 ? Math.ceil(n * r) : void 0; - }, Ur = (e) => { - switch (e) { - case "float16": - return typeof Float16Array < "u" && Float16Array.from ? Float16Array : Uint16Array; - case "float32": - return Float32Array; - case "uint8": - return Uint8Array; - case "int8": - return Int8Array; - case "uint16": - return Uint16Array; - case "int16": - return Int16Array; - case "int32": - return Int32Array; - case "bool": - return Uint8Array; - case "float64": - return Float64Array; - case "uint32": - return Uint32Array; - case "int64": - return BigInt64Array; - case "uint64": - return BigUint64Array; - default: - throw new Error(`unsupported type: ${e}`); - } - }, Xt = (e) => { - switch (e) { - case "verbose": - return 0; - case "info": - return 1; - case "warning": - return 2; - case "error": - return 3; - case "fatal": - return 4; - default: - throw new Error(`unsupported logging level: ${e}`); - } - }, Vr = (e) => e === "float32" || e === "float16" || e === "int32" || e === "int64" || e === "uint32" || e === "uint8" || e === "bool" || e === "uint4" || e === "int4", Nr = (e) => e === "float32" || e === "float16" || e === "int32" || e === "int64" || e === "uint32" || e === "uint64" || e === "int8" || e === "uint8" || e === "bool", Kn = (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 Zt, Yn = V(() => { - "use strict"; - Cr(); - Zt = async (e) => { - if (typeof e == "string") if (false) { - } else { - let t = await fetch(e); - if (!t.ok) throw new Error(`failed to load external data file: ${e}`); - let r = t.headers.get("Content-Length"), n = r ? parseInt(r, 10) : 0; - if (n < 1073741824) return new Uint8Array(await t.arrayBuffer()); - { - if (!t.body) throw new Error(`failed to load external data file: ${e}, no response body.`); - - //console.log(t); - let o = t.body.getReader(), i; - try { - i = new ArrayBuffer(n); - } catch (d) { - if (d instanceof RangeError) { - let l = Math.ceil(n / 65536); - i = new WebAssembly.Memory({ initial: l, maximum: l }).buffer; - } else throw d; - } - let a = 0; - for (; ; ) { - let { done: d, value: l } = await o.read(); - if (d) break; - let c = l.byteLength; - new Uint8Array(i, a, c).set(l), a += c; - } - return new Uint8Array(i, 0, n); - } - } - else return e instanceof Blob ? new Uint8Array(await e.arrayBuffer()) : e instanceof Uint8Array ? e : new Uint8Array(e); - }; - }); - var Wp, Lp, La, Ha, Wr, Hp, me, Xe = V(() => { - "use strict"; - Q(); - Wp = ["V", "I", "W", "E", "F"], Lp = (e, t) => { - console.log(`[${Wp[e]},${(/* @__PURE__ */ new Date()).toISOString()}]${t}`); - }, Wr = (e, t) => { - La = e, Ha = t; - }, Hp = (e, t) => { - let r = Xt(e), n = Xt(La); - r >= n && Lp(r, typeof t == "function" ? t() : t); - }, me = (...e) => { - Ha && Hp(...e); - }; - }); - var Lr, Xn = V(() => { - "use strict"; - Q(); - Lr = (e, t) => new (Ur(t))(e); - }); - var Hr = V(() => { - "use strict"; - }); - var Ga, Zn, Qn, Gp, Fp, Fa, eo, Jn, ja, Ka = V(() => { - "use strict"; - Xe(); - Hr(); - Ga = /* @__PURE__ */ new Map([[64, 250], [128, 200], [256, 200], [512, 200], [2048, 230], [4096, 200], [8192, 50], [16384, 50], [32768, 50], [65536, 50], [131072, 50], [262144, 50], [524288, 50], [1048576, 50], [2097152, 30], [4194304, 20], [8388608, 10], [12582912, 10], [16777216, 10], [26214400, 15], [33554432, 22], [44236800, 2], [58982400, 6], [67108864, 6], [134217728, 6], [167772160, 6]]), Zn = [], Qn = (e) => Math.ceil(e / 16) * 16, Gp = (e) => { - for (let t = 0; t < Zn.length; t++) { - let r = Zn[t]; - if (e <= r) return r; - } - return Math.ceil(e / 16) * 16; - }, Fp = 1, Fa = () => Fp++, eo = async (e, t, r, n) => { - let o = Qn(r), i = e.device.createBuffer({ size: o, usage: GPUBufferUsage.COPY_DST | GPUBufferUsage.MAP_READ }); - try { - let a = e.getCommandEncoder(); - e.endComputePass(), a.copyBufferToBuffer(t, 0, i, 0, o), e.flush(), await i.mapAsync(GPUMapMode.READ); - let d = i.getMappedRange(); - if (n) { - let l = n(); - return l.set(new Uint8Array(d, 0, r)), l; - } else return new Uint8Array(d.slice(0, r)); - } finally { - i.destroy(); - } - }, Jn = class { - constructor(t) { - this.backend = t; - this.storageCache = /* @__PURE__ */ new Map(), this.freeBuffers = /* @__PURE__ */ new Map(), this.freeUniformBuffers = /* @__PURE__ */ new Map(), this.buffersForUploadingPending = [], this.buffersPending = [], this.capturedPendingBuffers = /* @__PURE__ */ new Map(); - for (let [r] of Ga) Zn.push(r), this.freeBuffers.set(r, []), this.freeUniformBuffers.set(r, []); - } - upload(t, r) { - let n = r.buffer, o = r.byteOffset, i = r.byteLength, a = Qn(i), d = this.storageCache.get(t); - if (!d) throw new Error("gpu data for uploading does not exist"); - if (d.originalSize !== i) throw new Error(`inconsistent data size. gpu data size=${d.originalSize}, data size=${i}`); - console.log({ backend: this.backend, device: this.backend.device }); - let l = this.backend.device.createBuffer({ mappedAtCreation: true, size: a, usage: GPUBufferUsage.MAP_WRITE | GPUBufferUsage.COPY_SRC }), c = l.getMappedRange(); - new Uint8Array(c).set(new Uint8Array(n, o, i)), l.unmap(); - let m = this.backend.getCommandEncoder(); - this.backend.endComputePass(), m.copyBufferToBuffer(l, 0, d.gpuData.buffer, 0, a), me("verbose", () => `[WebGPU] GpuDataManager.upload(id=${t})`), this.buffersForUploadingPending.push(l); - } - memcpy(t, r) { - let n = this.storageCache.get(t); - if (!n) throw new Error("source gpu data for memcpy does not exist"); - let o = this.storageCache.get(r); - if (!o) throw new Error("destination gpu data for memcpy does not exist"); - if (n.originalSize !== o.originalSize) throw new Error("inconsistent source and destination gpu data size"); - let i = Qn(n.originalSize), a = this.backend.getCommandEncoder(); - this.backend.endComputePass(), a.copyBufferToBuffer(n.gpuData.buffer, 0, o.gpuData.buffer, 0, i); - } - registerExternalBuffer(t, r, n) { - let o; - if (n) { - if (o = n[0], t === n[1]) return me("verbose", () => `[WebGPU] GpuDataManager.registerExternalBuffer(size=${r}) => id=${o}, buffer is the same, skip.`), o; - if (this.backend.capturedCommandList.has(this.backend.currentSessionId)) throw new Error(`Registering a different external buffer under graph capture mode is not supported yet. - Please use the previous external buffer!`); - } else o = Fa(); - return this.storageCache.set(o, { gpuData: { id: o, type: 0, buffer: t }, originalSize: r }), me("verbose", () => `[WebGPU] GpuDataManager.registerExternalBuffer(size=${r}) => id=${o}, registered.`), o; - } - unregisterExternalBuffer(t) { - t !== void 0 && (this.storageCache.delete(t), me("verbose", () => `[WebGPU] GpuDataManager.unregisterExternalBuffer() => id=${t}`)); - } - create(t, r = GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_SRC | GPUBufferUsage.COPY_DST) { - let n = Gp(t), o, i = (r & GPUBufferUsage.STORAGE) === GPUBufferUsage.STORAGE, a = (r & GPUBufferUsage.UNIFORM) === GPUBufferUsage.UNIFORM; - if (i || a) { - let c = (i ? this.freeBuffers : this.freeUniformBuffers).get(n); - c ? c.length > 0 ? o = c.pop() : o = this.backend.device.createBuffer({ size: n, usage: r }) : o = this.backend.device.createBuffer({ size: n, usage: r }); - } else o = this.backend.device.createBuffer({ size: n, usage: r }); - let d = { id: Fa(), type: 0, buffer: o }; - return this.storageCache.set(d.id, { gpuData: d, originalSize: t }), me("verbose", () => `[WebGPU] GpuDataManager.create(size=${t}) => id=${d.id}`), d; - } - get(t) { - var _a2; - return (_a2 = this.storageCache.get(t)) == null ? void 0 : _a2.gpuData; - } - release(t) { - let r = this.storageCache.get(t); - if (!r) throw new Error("releasing data does not exist"); - return me("verbose", () => `[WebGPU] GpuDataManager.release(id=${t}), gpuDataId=${r.gpuData.id}`), this.storageCache.delete(t), this.buffersPending.push(r.gpuData.buffer), r.originalSize; - } - async download(t, r) { - let n = this.storageCache.get(t); - if (!n) throw new Error("data does not exist"); - await eo(this.backend, n.gpuData.buffer, n.originalSize, r); - } - refreshPendingBuffers() { - for (let t of this.buffersForUploadingPending) t.destroy(); - if (this.buffersForUploadingPending = [], this.buffersPending.length !== 0) if (this.backend.sessionStatus === "default") { - for (let t of this.buffersPending) { - let r = Ga.get(t.size); - if ((t.usage & GPUBufferUsage.STORAGE) === GPUBufferUsage.STORAGE) { - let n = this.freeBuffers.get(t.size) || []; - r === void 0 || n.length >= r ? t.destroy() : n.push(t); - } else if ((t.usage & GPUBufferUsage.UNIFORM) === GPUBufferUsage.UNIFORM) { - let n = this.freeUniformBuffers.get(t.size) || []; - r === void 0 || n.length >= r ? t.destroy() : n.push(t); - } else t.destroy(); - } - this.buffersPending = []; - } else { - let t = this.capturedPendingBuffers.get(this.backend.currentSessionId); - t || (t = [], this.capturedPendingBuffers.set(this.backend.currentSessionId, t)); - for (let r of this.buffersPending) t.push(r); - this.buffersPending = []; - } - } - dispose() { - this.freeBuffers.forEach((t) => { - t.forEach((r) => { - r.destroy(); - }); - }), this.freeUniformBuffers.forEach((t) => { - t.forEach((r) => { - r.destroy(); - }); - }), this.storageCache.forEach((t) => { - t.gpuData.buffer.destroy(); - }), this.capturedPendingBuffers.forEach((t) => { - t.forEach((r) => { - r.destroy(); - }); - }), this.storageCache = /* @__PURE__ */ new Map(), this.freeBuffers = /* @__PURE__ */ new Map(), this.freeUniformBuffers = /* @__PURE__ */ new Map(), this.capturedPendingBuffers = /* @__PURE__ */ new Map(); - } - onReleaseSession(t) { - let r = this.capturedPendingBuffers.get(t); - r && (r.forEach((n) => { - n.destroy(); - }), this.capturedPendingBuffers.delete(t)); - } - }, ja = (...e) => new Jn(...e); - }); - var to, J, Ie = V(() => { - "use strict"; - to = class { - constructor(t) { - Object.assign(this, t); - } - get cacheKey() { - return this.key || (this.key = Object.getOwnPropertyNames(this).sort().map((t) => `${this[t]}`).join(";")), this.key; - } - }, J = (e) => new to(e); - }); - var ro, rt, k, Ct, Gr, Ya, Xa, ie = V(() => { - "use strict"; - ro = class { - static calcMatMulShape(t, r) { - return t[1] !== r[0] ? void 0 : [t[0], r[1]]; - } - }, rt = class { - static calcShape(t, r, n = false) { - let o = t.length, i = r.length; - if (o === 0) return r; - if (i === 0) return t; - let a = Math.max(t.length, r.length), d = new Array(a); - if (n) { - if (o < 2 || i < 2) return; - let l = ro.calcMatMulShape([t[o - 2], t[o - 1]], [r[i - 2], r[i - 1]]); - if (l === void 0) return; - [d[a - 2], d[a - 1]] = l; - } - for (let l = n ? 3 : 1; l <= a; l++) { - let c = o - l < 0 ? 1 : t[o - l], m = i - l < 0 ? 1 : r[i - l]; - if (c !== m && c > 1 && m > 1) return; - let u = Math.max(c, m); - if (c && m) d[a - l] = Math.max(c, m); - else { - if (u > 1) return; - d[a - l] = 0; - } - } - return d; - } - static isValidBroadcast(t, r) { - let n = t.length, o = r.length; - if (n > o) return false; - for (let i = 1; i <= n; i++) if (t[n - i] !== 1 && t[n - i] !== r[o - i]) return false; - return true; - } - }, k = class e { - static size(t) { - return e.getSizeFromDimensionRange(t, 0, t.length); - } - static convertShape(t, r = 4) { - let n = t.length; - if (n === 0) return []; - let o = new Array(n), i = n - 1; - for (; i >= 0; ) { - if (t[i] % r === 0) { - o[i] = t[i] / r; - break; - } - if (r % t[i] !== 0) throw new Error("cannot convert shape"); - o[i] = 1, r /= t[i], i--; - } - for (i--; i >= 0; i--) o[i] = t[i]; - return o; - } - static sizeFromDimension(t, r) { - if (r < 0 || r > t.length) throw new Error(`invalid dimension of ${r} for sizeFromDimension as Tensor has ${t.length} dimensions.`); - return e.getSizeFromDimensionRange(t, r, t.length); - } - static sizeToDimension(t, r) { - if (r < 0 || r > t.length) throw new Error(`invalid dimension of ${r} for sizeToDimension as Tensor has ${t.length} dimensions.`); - return e.getSizeFromDimensionRange(t, 0, r); - } - static getSizeFromDimensionRange(t, r, n) { - let o = 1; - for (let i = r; i < n; i++) { - if (t[i] < 0) throw new Error("cannot get valid size from specified dimension range. Most likely the range contains negative values in them."); - o *= t[i]; - } - return o; - } - static computeStrides(t) { - let r = t.length; - if (r === 0) return []; - if (r === 1) return [1]; - let n = new Array(r); - n[r - 1] = 1, n[r - 2] = t[r - 1]; - for (let o = r - 3; o >= 0; --o) n[o] = n[o + 1] * t[o + 1]; - return n; - } - static normalizeAxis(t, r) { - if (t < -r && t >= r) throw new Error("unsupported axis for this operation."); - return t < 0 ? t + r : t; - } - static normalizeAxes(t, r) { - return t.map((n) => this.normalizeAxis(n, r ?? t.length)); - } - static sortBasedOnPerm(t, r) { - return r ? r.map((n) => t[n]) : t.slice().reverse(); - } - static padShape(t, r) { - let n = t.length; - return t.map((o, i) => o + r[i] + r[i + n]); - } - static areEqual(t, r) { - return t.length !== r.length ? false : t.every((n, o) => n === r[o]); - } - }, Ct = class e { - static adjustPoolAttributes(t, r, n, o, i, a) { - if (!t && n.length !== r.length - 2) throw new Error("length of specified kernel shapes should be 2 less than length of input dimensions"); - if (t) for (let d = 0; d < r.length - 2; d++) d >= n.length ? n.push(r[d + 2]) : n[d] = r[d + 2]; - for (let d = 0; d < n.length; d++) if (d < o.length) { - if (o[d] < 0) throw new Error("strides should be greater than or equal to 1"); - } else o.push(1); - for (let d = 0; d < n.length; d++) if (d < i.length) { - if (i[d] < 0) throw new Error("dilations should be greater than or equal to 1"); - } else i.push(1); - for (let d = 0; d < n.length * 2; d++) if (d < a.length) { - if (a[d] < 0) throw new Error("pad should be greater than or equal to 1"); - } else a.push(0); - for (let d = 0; d < n.length; d++) { - if (n[d] <= 0) throw new Error("kernel shapes need to be greater than 0"); - if (a[d] >= n[d] || a[d + n.length] >= n[d]) throw new Error("pads should be smaller than kernel"); - } - } - static adjustPadsBasedOnAutoPad(t, r, n, o, i, a, d) { - if (d) { - if (i.length !== 2 * (t.length - 2)) throw new Error("length of pads should be twice the length of data dimensions"); - if (r.length !== t.length - 2) throw new Error("length of strides should be the length of data dimensions"); - if (o.length !== t.length - 2) throw new Error("length of kernel shapes should be the length of data dimensions"); - for (let l = 0; l < t.length - 2; l++) e.adjustPadAndReturnShape(t[l + (a ? 1 : 2)], r[l], n[l], o[l], i, l, l + t.length - 2, d); - } - } - static computePoolOutputShape(t, r, n, o, i, a, d) { - if (r.length <= 0) throw new Error("input shape must be of size greater than 0"); - let l = [r[0], r[1]]; - return e.computeShapeHelper(t, r, l, n, o, i, a, d), l; - } - static computeConvOutputShape(t, r, n, o, i, a, d) { - if (t.length <= 0 || r.length <= 0) throw new Error("invalid input tensor dims or invalid filter tensor dims"); - let l = [t[0], r[0]]; - return e.computeShapeHelper(false, t, l, n, o, i, a, d), l; - } - static computeShapeHelper(t, r, n, o, i, a, d, l) { - if (t) for (let c = 0; c < r.length - 2; c++) n.push(1); - else for (let c = 0; c < r.length - 2; c++) n.push(e.adjustPadAndReturnShape(r[c + 2], o[c], i[c], a[c], d, c, c + r.length - 2, l)); - } - static adjustPadAndReturnShape(t, r, n, o, i, a, d, l) { - let c = n * (o - 1) + 1; - if (l && l !== "NOTSET") switch (l) { - case "VALID": - return i[a] = 0, i[d] = 0, Math.floor((t - c) / r + 1); - case "SAME_LOWER": - case "SAME_UPPER": - if (n !== 1) throw new Error("Dilation not supported for SAME_UPPER or SAME_LOWER"); - { - let u = ((t + r - 1) / r - 1) * r + o - t; - return i[a] = Math.floor(l === "SAME_LOWER" ? (u + 1) / 2 : u / 2), i[d] = u - i[a], Math.floor((t + u - o) / r + 1); - } - default: - throw new Error("Unsupported AutoPad type"); - } - else return Math.floor((t + i[a] + i[d] - c) / r + 1); - } - }, Gr = class { - static getShapeOfGemmResult(t, r, n, o, i) { - if (t.length !== 2 || n.length !== 2) throw new Error("shape need to be of size 2"); - let a, d, l; - r ? (a = t[1], d = t[0]) : (a = t[0], d = t[1]); - let c = -1; - if (o ? (l = n[0], c = 1) : (l = n[1], c = 0), n[c] !== d) throw new Error("dimension mismatch"); - if (a <= 0 || l <= 0 || d <= 0) throw new Error("invalid shape specified"); - if (i && !rt.isValidBroadcast(i, [a, l])) throw new Error("gemm: invalid bias shape for broadcast"); - return [a, l, d]; - } - }, Ya = -34028234663852886e22, Xa = 34028234663852886e22; - }); - var At, oo, he, Ee, R, we, io, kt, Ze, F, ao, E, M, Fr, no, Za, Nt, ae = V(() => { - "use strict"; - Q(); - ie(); - At = 64, oo = (e, t) => { - if (t === 3) throw new Error("vec3 has same alignment as vec4, use vec4 instead"); - switch (e) { - case 10: - return t > 1 ? `vec${t}` : "f16"; - case 1: - return t > 1 ? `vec${t}` : "f32"; - case 6: - return t > 1 ? `vec${t}` : "i32"; - case 12: - return t > 1 ? `vec${t}` : "u32"; - case 7: - if (t > 1) throw new Error("currently not supported vecX of uint64 yet"); - return ["vec2", "i32"]; - case 13: - if (t > 1) throw new Error("currently not supported vecX of uint64 yet"); - return ["vec2", "u32"]; - case 9: - if (t !== 4) throw new Error("bool must be vec4"); - return ["u32", "vec4"]; - case 22: - return "i32"; - case 21: - return "u32"; - default: - throw new Error(`Unknown data type: ${e}`); - } - }, he = (e, t = 1) => { - let r = oo(e, t); - return typeof r == "string" ? r : r[0]; - }, Ee = (e, t = 1) => { - let r = oo(e, t); - return typeof r == "string" ? r : r[1]; - }, R = (...e) => { - let t = []; - return e.forEach((r) => { - r.length !== 0 && t.push({ type: 12, data: r }, { type: 12, data: k.computeStrides(r) }); - }), t; - }, we = (e) => e % 4 === 0 ? 4 : e % 2 === 0 ? 2 : 1, io = (e = "f32", t, r = "0") => !t || t === 1 ? `${e}(${r})` : `vec${t}<${e}>(${r})`, kt = (e, t, r) => e === "f32" ? r : t === 1 ? `f32(${r})` : `vec${t}(${r})`, Ze = (e, t) => t === 4 ? `(${e}.x + ${e}.y + ${e}.z + ${e}.w)` : t === 2 ? `(${e}.x + ${e}.y)` : t === 3 ? `(${e}.x + ${e}.y + ${e}.z)` : e, F = (e, t, r, n) => e.startsWith("uniforms.") && r > 4 ? typeof t == "string" ? n === "f16" ? `${e}[(${t}) / 8][(${t}) % 8 / 4][(${t}) % 8 % 4]` : `${e}[(${t}) / 4][(${t}) % 4]` : n === "f16" ? `${e}[${Math.floor(t / 8)}][${Math.floor(t % 8 / 4)}][${t % 8 % 4}]` : `${e}[${Math.floor(t / 4)}][${t % 4}]` : r > 1 ? `${e}[${t}]` : e, ao = (e, t, r, n, o) => { - let i = typeof r == "number", a = i ? r : r.length, d = [...new Array(a).keys()], l = a < 2 ? "u32" : a <= 4 ? `vec${a}` : `array`, c = oo(t, o), m = typeof c == "string" ? c : c[1], u = typeof c == "string" ? c : c[0], h = { indices: l, value: m, storage: u, tensor: t }, w = (U) => typeof U == "string" ? U : `${U}u`, g = { offsetToIndices: false, indicesToOffset: false, broadcastedIndicesToOffset: false, set: false, setByIndices: false, get: false, getByIndices: false }, y = i ? "uniforms." : "", S = `${y}${e}_shape`, $ = `${y}${e}_strides`, v = ""; - for (let U = 0; U < a - 1; U++) v += ` - let dim${U} = current / ${F($, U, a)}; - let rest${U} = current % ${F($, U, a)}; - indices[${U}] = dim${U}; - current = rest${U}; - `; - v += `indices[${a - 1}] = current;`; - let x = a < 2 ? "" : ` - fn o2i_${e}(offset: u32) -> ${h.indices} { - var indices: ${h.indices}; - var current = offset; - ${v} - return indices; - }`, T = (U) => (g.offsetToIndices = true, a < 2 ? U : `o2i_${e}(${U})`), C = []; - if (a >= 2) for (let U = a - 1; U >= 0; U--) C.push(`${F($, U, a)} * (indices[${U}])`); - let A = a < 2 ? "" : ` - fn i2o_${e}(indices: ${h.indices}) -> u32 { - return ${C.join("+")}; - }`, P = (U) => (g.indicesToOffset = true, a < 2 ? U : `i2o_${e}(${U})`), B = (...U) => a === 0 ? "0u" : `${h.indices}(${U.map(w).join(",")})`, N = (U, G) => a < 2 ? `${U}` : `${F(U, G, a)}`, W = (U, G, ye) => a < 2 ? `${U}=${ye};` : `${F(U, G, a)}=${ye};`, K = {}, Z = (U, G) => { - g.broadcastedIndicesToOffset = true; - let ye = `${G.name}broadcastedIndicesTo${e}Offset`; - if (ye in K) return `${ye}(${U})`; - let Re = []; - for (let $e = a - 1; $e >= 0; $e--) { - let Ce = G.indicesGet("outputIndices", $e + G.rank - a); - Re.push(`${N($, $e)} * (${Ce} % ${N(S, $e)})`); - } - return K[ye] = `fn ${ye}(outputIndices: ${G.type.indices}) -> u32 { - return ${Re.length > 0 ? Re.join("+") : "0u"}; - }`, `${ye}(${U})`; - }, ee = (U, G) => (() => { - if (h.storage === h.value) return `${e}[${U}]=${G};`; - if (h.storage === "vec2" && h.value === "i32") return `${e}[${U}]=vec2(u32(${G}), select(0u, 0xFFFFFFFFu, ${G} < 0));`; - if (h.storage === "vec2" && h.value === "u32") return `${e}[${U}]=vec2(u32(${G}), 0u);`; - if (h.storage === "u32" && h.value === "vec4") return `${e}[${U}]=dot(vec4(0x1, 0x100, 0x10000, 0x1000000), vec4(${G}));`; - throw new Error(`not supported combination of storage type ${h.storage} and value type ${h.value} yet`); - })(), se = (U) => (() => { - if (h.storage === h.value) return `${e}[${U}]`; - if (h.storage === "vec2" && h.value === "i32") return `i32(${e}[${U}].x)`; - if (h.storage === "vec2" && h.value === "u32") return `u32(${e}[${U}].x)`; - if (h.storage === "u32" && h.value === "vec4") return `vec4(bool(${e}[${U}] & 0xFFu), bool(${e}[${U}] & 0xFF00u), bool(${e}[${U}] & 0xFF0000u), bool(${e}[${U}] & 0xFF000000u))`; - throw new Error(`not supported combination of storage type ${h.storage} and value type ${h.value} yet`); - })(), de = a < 2 ? "" : ` - fn get_${e}ByIndices(indices: ${h.indices}) -> ${m} { - return ${se(`i2o_${e}(indices)`)}; - }`, Y = a < 2 ? "" : (() => { - let U = d.map((ye) => `d${ye}: u32`).join(", "), G = d.map((ye) => `d${ye}`).join(", "); - return ` - fn get_${e}(${U}) -> ${m} { - return get_${e}ByIndices(${B(G)}); - }`; - })(), le = (...U) => { - if (U.length !== a) throw new Error(`indices length must be ${a}`); - let G = U.map(w).join(","); - return a === 0 ? se("0u") : a === 1 ? se(G[0]) : (g.get = true, g.getByIndices = true, g.indicesToOffset = true, `get_${e}(${G})`); - }, ce = (U) => a < 2 ? se(U) : (g.getByIndices = true, g.indicesToOffset = true, `get_${e}ByIndices(${U})`), q = a < 2 ? "" : ` - fn set_${e}ByIndices(indices: ${h.indices}, value: ${m}) { - ${ee(`i2o_${e}(indices)`, "value")} - }`, ue = a < 2 ? "" : (() => { - let U = d.map((ye) => `d${ye}: u32`).join(", "), G = d.map((ye) => `d${ye}`).join(", "); - return ` - fn set_${e}(${U}, value: ${m}) { - set_${e}ByIndices(${B(G)}, value); - }`; - })(); - return { impl: () => { - let U = [], G = false; - return g.offsetToIndices && (U.push(x), G = true), g.indicesToOffset && (U.push(A), G = true), g.broadcastedIndicesToOffset && (Object.values(K).forEach((ye) => U.push(ye)), G = true), g.set && (U.push(ue), G = true), g.setByIndices && (U.push(q), G = true), g.get && (U.push(Y), G = true), g.getByIndices && (U.push(de), G = true), !i && G && U.unshift(`const ${S} = ${h.indices}(${r.join(",")});`, `const ${$} = ${h.indices}(${k.computeStrides(r).join(",")});`), U.join(` - `); - }, type: h, offsetToIndices: T, indicesToOffset: P, broadcastedIndicesToOffset: Z, indices: B, indicesGet: N, indicesSet: W, set: (...U) => { - if (U.length !== a + 1) throw new Error(`indices length must be ${a}`); - let G = U[a]; - if (typeof G != "string") throw new Error("value must be string"); - let ye = U.slice(0, a).map(w).join(","); - return a === 0 ? ee("0u", G) : a === 1 ? ee(ye[0], G) : (g.set = true, g.setByIndices = true, g.indicesToOffset = true, `set_${e}(${ye}, ${G})`); - }, setByOffset: ee, setByIndices: (U, G) => a < 2 ? ee(U, G) : (g.setByIndices = true, g.indicesToOffset = true, `set_${e}ByIndices(${U}, ${G});`), get: le, getByOffset: se, getByIndices: ce, usage: n, name: e, strides: $, shape: S, rank: a }; - }, E = (e, t, r, n = 1) => ao(e, t, r, "input", n), M = (e, t, r, n = 1) => ao(e, t, r, "output", n), Fr = (e, t, r, n = 1) => ao(e, t, r, "internal", n), no = class { - constructor(t, r) { - this.normalizedDispatchGroup = t; - this.limits = r; - this.internalVariables = []; - this.variables = []; - this.uniforms = []; - this.variableIndex = 0; - } - guardAgainstOutOfBoundsWorkgroupSizes(t) { - return `if (global_idx >= ${typeof t == "number" ? `${t}u` : t}) { return; }`; - } - mainStart(t = At) { - let r = typeof t == "number" ? t : t[0], n = typeof t == "number" ? 1 : t[1], o = typeof t == "number" ? 1 : t[2]; - if (r > this.limits.maxComputeWorkgroupSizeX || n > this.limits.maxComputeWorkgroupSizeY || o > this.limits.maxComputeWorkgroupSizeZ) throw new Error(`workgroup size [${r}, ${n}, ${o}] exceeds the maximum workgroup size [${this.limits.maxComputeWorkgroupSizeX}, ${this.limits.maxComputeWorkgroupSizeY}, ${this.limits.maxComputeWorkgroupSizeZ}].`); - if (r * n * o > this.limits.maxComputeInvocationsPerWorkgroup) throw new Error(`workgroup size [${r}, ${n}, ${o}] exceeds the maximum workgroup invocations ${this.limits.maxComputeInvocationsPerWorkgroup}.`); - let i = this.normalizedDispatchGroup[1] === 1 && this.normalizedDispatchGroup[2] === 1, a = i ? `@builtin(global_invocation_id) global_id : vec3, - @builtin(workgroup_id) workgroup_id : vec3, - @builtin(local_invocation_index) local_idx : u32, - @builtin(local_invocation_id) local_id : vec3` : `@builtin(global_invocation_id) global_id : vec3, - @builtin(local_invocation_id) local_id : vec3, - @builtin(local_invocation_index) local_idx : u32, - @builtin(workgroup_id) workgroup_id : vec3, - @builtin(num_workgroups) num_workgroups : vec3`, d = i ? `let global_idx = global_id.x; - let workgroup_index = workgroup_id.x;` : `let workgroup_index = workgroup_id.z * num_workgroups[0] * num_workgroups[1] + - workgroup_id.y * num_workgroups[0] + workgroup_id.x; - let global_idx = workgroup_index * ${r * n * o}u + local_idx;`; - return `@compute @workgroup_size(${r}, ${n}, ${o}) - fn main(${a}) { - ${d} - `; - } - appendVariableUniforms(t) { - t.rank !== 0 && (t.shape.startsWith("uniforms.") && this.uniforms.push({ name: t.shape.replace("uniforms.", ""), type: "u32", length: t.rank }), t.strides.startsWith("uniforms.") && this.uniforms.push({ name: t.strides.replace("uniforms.", ""), type: "u32", length: t.rank })); - } - declareVariable(t, r) { - if (t.usage === "internal") throw new Error("cannot use internal variable with declareVariable(). use registerInternalVariables() instead."); - this.variables.push(t), this.appendVariableUniforms(t); - let n = t.usage === "input" ? "read" : "read_write", o = t.type.storage; - return `@group(0) @binding(${r}) var ${t.name}: array<${o}>;`; - } - declareVariables(...t) { - return t.map((r) => this.declareVariable(r, this.variableIndex++)).join(` - `); - } - registerInternalVariable(t) { - if (t.usage !== "internal") throw new Error("cannot use input or output variable with registerInternalVariable(). use declareVariables() instead."); - this.internalVariables.push(t), this.appendVariableUniforms(t); - } - registerInternalVariables(...t) { - return t.forEach((r) => this.registerInternalVariable(r)), this; - } - registerUniform(t, r, n = 1) { - return this.uniforms.push({ name: t, type: r, length: n }), this; - } - registerUniforms(t) { - return this.uniforms = this.uniforms.concat(t), this; - } - uniformDeclaration() { - if (this.uniforms.length === 0) return ""; - let t = []; - for (let { name: r, type: n, length: o } of this.uniforms) if (o && o > 4) n === "f16" ? t.push(`@align(16) ${r}:array, ${Math.ceil(o / 8)}>`) : t.push(`${r}:array, ${Math.ceil(o / 4)}>`); - else { - let i = o == null || o === 1 ? n : `vec${o}<${n}>`; - t.push(`${r}:${i}`); - } - return ` - struct Uniforms { ${t.join(", ")} }; - @group(0) @binding(${this.variableIndex}) var uniforms: Uniforms;`; - } - get additionalImplementations() { - return this.uniformDeclaration() + this.variables.map((t) => t.impl()).join(` - `) + this.internalVariables.map((t) => t.impl()).join(` - `); - } - get variablesInfo() { - if (this.uniforms.length === 0) return; - let t = (r) => [12, 10, 1, 6][["u32", "f16", "f32", "i32"].indexOf(r)]; - return this.uniforms.map((r) => [t(r.type), r.length ?? 1]); - } - }, Za = (e, t) => new no(e, t), Nt = (e, t) => { - let r = e.length, n = []; - for (let o = 0; o < r; o++) { - let i = r - 1 - o, a = e[i] || 1; - (t[t.length - 1 - o] || 1) > 1 && a === 1 && n.unshift(i); - } - return n; - }; - }); - var qp, Qa, jp, Kp, Yp, Pe, Ja, es, lt = V(() => { - "use strict"; - Q(); - ie(); - Ie(); - ae(); - qp = (e) => { - if (!e || e.length !== 1) throw new Error("Transpose requires 1 input."); - }, Qa = (e, t) => t && t.length !== e ? [...new Array(e).keys()].reverse() : t, jp = (e, t) => k.sortBasedOnPerm(e, Qa(e.length, t)), Kp = (e, t, r, n) => { - let o = `fn perm(i: ${n.type.indices}) -> ${r.type.indices} { - var a: ${r.type.indices};`; - for (let i = 0; i < t; ++i) o += r.indicesSet("a", e[i], `i[${i}]`); - return o += "return a;}"; - }, Yp = (e, t) => { - let r = [], n = []; - for (let o = 0; o < e.length; ++o) e[o] !== 1 && r.push(e[o]), e[t[o]] !== 1 && n.push(t[o]); - return { newShape: r, newPerm: n }; - }, Pe = (e, t) => { - let r = e.dataType, n = e.dims.length, o = Qa(n, t), i = jp(e.dims, o), { newShape: a, newPerm: d } = Yp(e.dims, o), l = k.areEqual(d, [2, 3, 1]), c = k.areEqual(d, [3, 1, 2]), m = a.length === 2 && d[0] > d[1] || l || c, u = m ? a : e.dims, h = i; - m && (u = l ? [a[0], a[1] * a[2]] : c ? [a[0] * a[1], a[2]] : a, h = [u[1], u[0]]); - let w = E("a", r, u.length), g = M("output", r, h.length), y = 16, S; - return m ? S = ($) => ` - ${$.registerUniform("output_size", "u32").declareVariables(w, g)} - var tile : array, ${y}>; - ${$.mainStart([y, y, 1])} - let stride = (uniforms.output_shape[1] - 1) / ${y} + 1; - let workgroup_id_x = workgroup_index % stride; - let workgroup_id_y = workgroup_index / stride; - let input_col = workgroup_id_y * ${y}u + local_id.x; - let input_row = workgroup_id_x * ${y}u + local_id.y; - if (input_row < uniforms.a_shape[0] && input_col < uniforms.a_shape[1]) { - tile[local_id.y][local_id.x] = ${w.getByIndices(`${w.type.indices}(input_row, input_col)`)}; - } - workgroupBarrier(); - - let output_col = workgroup_id_x * ${y}u + local_id.x; - let output_row = workgroup_id_y * ${y}u + local_id.y; - if (output_row < uniforms.output_shape[0] && output_col < uniforms.output_shape[1]) { - ${g.setByIndices(`${g.type.indices}(output_row, output_col)`, "tile[local_id.x][local_id.y]")} - } - }` : S = ($) => ` - ${$.registerUniform("output_size", "u32").declareVariables(w, g)} - - ${Kp(o, n, w, g)} - - ${$.mainStart()} - ${$.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} - - let indices = ${g.offsetToIndices("global_idx")}; - let aIndices = perm(indices); - - ${g.setByOffset("global_idx", w.getByIndices("aIndices"))} - }`, { name: m ? "TransposeShared" : "Transpose", shaderCache: { hint: `${t}`, inputDependencies: ["rank"] }, getRunData: () => { - let $ = k.size(i); - return { outputs: [{ dims: i, dataType: e.dataType }], dispatchGroup: m ? { x: Math.ceil(h[1] / y), y: Math.ceil(h[0] / y) } : { x: Math.ceil($ / 64) }, programUniforms: [{ type: 12, data: $ }, ...R(u, h)] }; - }, getShaderSource: S }; - }, Ja = (e, t) => { - qp(e.inputs), e.compute(Pe(e.inputs[0], t.perm)); - }, es = (e) => J({ perm: e.perm }); - }); - var Xp, Zp, Qp, Jp, em, tm, rm, nm, om, im, nt, ts, rs, ns, os, is, as, ss, us, ds, ls, cs = V(() => { - "use strict"; - Q(); - ie(); - ae(); - qr(); - lt(); - Xp = { max: "select(bestValue, candidate, candidate > bestValue)", min: "select(bestValue, candidate, candidate < bestValue)", mean: "bestValue + candidate", sum: "bestValue + candidate", prod: "bestValue * candidate", sumSquare: "bestValue + candidate * candidate", logSumExp: "bestValue + exp(candidate)", l1: "bestValue + abs(candidate)", l2: "bestValue + candidate * candidate", logSum: "bestValue + candidate" }, Zp = { 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" }, Qp = { max: "_A[offset]", min: "_A[offset]", mean: "0", sum: "0", prod: "1", sumSquare: "0", logSumExp: "0", l1: "0", l2: "0", logSum: "0" }, Jp = { max: "bestValue", min: "bestValue", sum: "bestValue", prod: "bestValue", sumSquare: "bestValue", logSumExp: "log(bestValue)", l1: "bestValue", l2: "sqrt(bestValue)", logSum: "log(bestValue)" }, em = (e, t) => { - let r = []; - for (let n = t - e; n < t; ++n) r.push(n); - return r; - }, tm = (e, t) => { - let r = [], n = e.length; - for (let i = 0; i < n; i++) t.indexOf(i) === -1 && r.push(e[i]); - let o = t.map((i) => e[i]); - return [r, o]; - }, rm = (e, t) => { - let r = e.length + t.length, n = [], o = 0; - for (let i = 0; i < r; i++) t.indexOf(i) === -1 ? n.push(e[o++]) : n.push(1); - return n; - }, nm = (e, t) => { - for (let r = 0; r < e.length; ++r) if (e[e.length - r - 1] !== t - 1 - r) return false; - return true; - }, om = (e, t) => { - let r = []; - if (!nm(e, t)) { - for (let n = 0; n < t; ++n) e.indexOf(n) === -1 && r.push(n); - e.forEach((n) => r.push(n)); - } - return r; - }, im = (e, t, r, n, o, i, a) => { - let d = r[0].dims, l = k.size(i), c = k.size(a), m = E("_A", r[0].dataType, d), u = M("output", o, i), h = 32, w = ` - var aBestValues : array; - `; - return { name: e, shaderCache: t, getShaderSource: (y) => ` - ${y.registerUniform("reduceSize", "u32").declareVariables(m, u)} - ${w} - fn DIV_CEIL(a : u32, b : u32) -> u32 { - return ((a - 1u) / b + 1u); - } - ${y.mainStart(h)} - - let outputIndex = global_idx / ${h}; - let offset = outputIndex * uniforms.reduceSize; - - var bestValue = f32(${Qp[n]}); - let Length = uniforms.reduceSize; - for (var k = local_idx; k < Length; k = k + ${h}) { - let candidate = f32(${m.getByOffset("offset + k")}); - bestValue = ${Xp[n]}; - } - aBestValues[local_idx] = bestValue; - workgroupBarrier(); - - var reduceSize = min(Length, ${h}u); - for (var currentSize = reduceSize / 2u; reduceSize > 1u; - currentSize = reduceSize / 2u) { - let interval = DIV_CEIL(reduceSize, 2u); - if (local_idx < currentSize) { - let candidate = aBestValues[local_idx + interval]; - bestValue = ${Zp[n]}; - aBestValues[local_idx] = bestValue; - } - reduceSize = interval; - workgroupBarrier(); - } - - if (local_idx == 0u) { - ${u.setByOffset("outputIndex", `${n === "mean" ? `${u.type.storage}(bestValue / f32(uniforms.reduceSize))` : `${u.type.storage}(${Jp[n]})`}`)}; - } - }`, getRunData: () => ({ outputs: [{ dims: i, dataType: o }], dispatchGroup: { x: l }, programUniforms: [{ type: 12, data: c }] }) }; - }, nt = (e, t, r, n) => { - let o = e.inputs.length === 1 ? r : so(e.inputs, r), i = o.axes; - i.length === 0 && !o.noopWithEmptyAxes && (i = e.inputs[0].dims.map((w, g) => g)); - let a = k.normalizeAxes(i, e.inputs[0].dims.length), d = a, l = e.inputs[0], c = om(d, e.inputs[0].dims.length); - c.length > 0 && (l = e.compute(Pe(e.inputs[0], c), { inputs: [0], outputs: [-1] })[0], d = em(d.length, l.dims.length)); - let [m, u] = tm(l.dims, d), h = m; - o.keepDims && (h = rm(m, a)), e.compute(im(t, { hint: o.cacheKey, inputDependencies: ["type"] }, [l], n, e.inputs[0].dataType, h, u), { inputs: [l] }); - }, ts = (e, t) => { - nt(e, "ReduceMeanShared", t, "mean"); - }, rs = (e, t) => { - nt(e, "ReduceL1Shared", t, "l1"); - }, ns = (e, t) => { - nt(e, "ReduceL2Shared", t, "l2"); - }, os = (e, t) => { - nt(e, "ReduceLogSumExpShared", t, "logSumExp"); - }, is = (e, t) => { - nt(e, "ReduceMaxShared", t, "max"); - }, as = (e, t) => { - nt(e, "ReduceMinShared", t, "min"); - }, ss = (e, t) => { - nt(e, "ReduceProdShared", t, "prod"); - }, us = (e, t) => { - nt(e, "ReduceSumShared", t, "sum"); - }, ds = (e, t) => { - nt(e, "ReduceSumSquareShared", t, "sumSquare"); - }, ls = (e, t) => { - nt(e, "ReduceLogSumShared", t, "logSum"); - }; - }); - var ot, am, jr, so, it, sm, um, dm, lm, cm, pm, mm, fm, hm, gm, at, ps, ms, fs, hs, gs, ys, bs, ws, _s, vs, qr = V(() => { - "use strict"; - Q(); - ie(); - Ie(); - ae(); - cs(); - ot = (e) => { - if (!e || e.length === 0 || e.length > 2) throw new Error("Reduce op requires 1 or 2 inputs."); - if (e.length === 2 && e[1].dims.length !== 1) throw new Error("Invalid axes input dims."); - }, am = (e) => ["", "", `var value = ${e.getByIndices("input_indices")};`, ""], jr = (e, t, r, n, o, i, a = false, d = false) => { - let l = [], c = r[0].dims, m = c.length, u = k.normalizeAxes(o, m), h = !d && u.length === 0; - c.forEach((S, $) => { - h || u.indexOf($) >= 0 ? a && l.push(1) : l.push(S); - }); - let w = l.length, g = k.size(l); - return { name: e, shaderCache: t, getShaderSource: (S) => { - let $ = [], v = E("_A", r[0].dataType, m), x = M("output", i, w), T = n(v, x, u), C = T[2]; - for (let A = 0, P = 0; A < m; A++) h || u.indexOf(A) >= 0 ? (a && P++, C = `for(var j${A}: u32 = 0; j${A} < ${c[A]}; j${A}++) { - ${T[2].includes("last_index") ? `let last_index = j${A};` : ""} - ${v.indicesSet("input_indices", A, `j${A}`)} - ${C} - }`) : ($.push(`${v.indicesSet("input_indices", A, x.indicesGet("output_indices", P))};`), P++); - return ` - - ${S.registerUniform("output_size", "u32").declareVariables(v, x)} - - ${S.mainStart()} - ${S.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} - var input_indices: ${v.type.indices}; - let output_indices = ${x.offsetToIndices("global_idx")}; - - ${$.join(` - `)} - ${T[0]} // init ops for reduce max/min - ${T[1]} - ${C} - ${T[3]} - ${T.length === 4 ? x.setByOffset("global_idx", "value") : T.slice(4).join(` - `)} - }`; - }, getRunData: () => ({ outputs: [{ dims: l, dataType: i }], dispatchGroup: { x: Math.ceil(g / 64) }, programUniforms: [{ type: 12, data: g }, ...R(c, l)] }) }; - }, so = (e, t) => { - let r = []; - return e[1].dims[0] > 0 && e[1].getBigInt64Array().forEach((n) => r.push(Number(n))), J({ axes: r, keepDims: t.keepDims, noopWithEmptyAxes: t.noopWithEmptyAxes }); - }, it = (e, t, r, n) => { - let o = e.inputs, i = o.length === 1 ? r : so(o, r); - e.compute(jr(t, { hint: i.cacheKey, inputDependencies: ["rank"] }, [o[0]], i.noopWithEmptyAxes && i.axes.length === 0 ? am : n, i.axes, o[0].dataType, i.keepDims, i.noopWithEmptyAxes), { inputs: [0] }); - }, sm = (e, t) => { - ot(e.inputs), it(e, "ReduceLogSum", t, (n, o) => [`var value = ${o.type.storage}(0);`, "", `value += ${n.getByIndices("input_indices")};`, "value = log(value);"]); - }, um = (e, t) => { - ot(e.inputs), it(e, "ReduceL1", t, (n, o) => [`var value = ${o.type.storage}(0);`, "", `value += abs(${n.getByIndices("input_indices")});`, ""]); - }, dm = (e, t) => { - ot(e.inputs), it(e, "ReduceL2", t, (n, o) => [`var t = ${o.type.value}(0); var value = ${o.type.value}(0);`, "", `t = ${n.getByIndices("input_indices")}; value += (t * t);`, "value = sqrt(value);"]); - }, lm = (e, t) => { - ot(e.inputs), it(e, "ReduceLogSumExp", t, (n, o) => [`var value = ${o.type.storage}(0);`, "", `value += exp(${n.getByIndices("input_indices")});`, "value = log(value);"]); - }, cm = (e, t) => { - ot(e.inputs), it(e, "ReduceMax", t, (n, o, i) => { - let a = []; - for (let d = 0; d < n.rank; d++) (i.indexOf(d) >= 0 || i.length === 0) && a.push(n.indicesSet("input_indices", d, 0)); - return [`${a.join(` - `)}`, `var value = ${n.getByIndices("input_indices")};`, `value = max(value, ${n.getByIndices("input_indices")});`, ""]; - }); - }, pm = (e, t) => { - ot(e.inputs), it(e, "ReduceMean", t, (n, o, i) => { - let a = 1; - for (let d = 0; d < n.rank; d++) (i.indexOf(d) >= 0 || i.length === 0) && (a *= e.inputs[0].dims[d]); - return ["var sum = f32(0);", "", `sum += f32(${n.getByIndices("input_indices")});`, `let value = ${o.type.value}(sum / ${a});`]; - }); - }, mm = (e, t) => { - ot(e.inputs), it(e, "ReduceMin", t, (n, o, i) => { - let a = []; - for (let d = 0; d < n.rank; d++) (i.indexOf(d) >= 0 || i.length === 0) && a.push(`input_indices[${d}] = 0;`); - return [`${a.join(` - `)}`, `var value = ${n.getByIndices("input_indices")};`, `value = min(value, ${n.getByIndices("input_indices")});`, ""]; - }); - }, fm = (e, t) => { - ot(e.inputs), it(e, "ReduceProd", t, (n, o) => [`var value = ${o.type.storage}(1);`, "", `value *= ${n.getByIndices("input_indices")};`, ""]); - }, hm = (e, t) => { - ot(e.inputs), it(e, "ReduceSum", t, (n, o) => [`var value = ${o.type.storage}(0);`, "", `value += ${n.getByIndices("input_indices")};`, ""]); - }, gm = (e, t) => { - ot(e.inputs), it(e, "ReduceSumSquare", t, (n, o) => [`var t = ${o.type.value}(0); var value = ${o.type.value}(0);`, "", `t = ${n.getByIndices("input_indices")}; value += t * t;`, ""]); - }, at = (e, t, r) => { - if (t.length === 0) return r; - let n = 1, o = 1; - for (let i = 0; i < t.length; i++) t.indexOf(i) === -1 ? n *= e[i] : o *= e[i]; - return o < 32 && n > 1024; - }, ps = (e, t) => { - at(e.inputs[0].dims, t.axes, t.noopWithEmptyAxes) ? pm(e, t) : ts(e, t); - }, ms = (e, t) => { - at(e.inputs[0].dims, t.axes, t.noopWithEmptyAxes) ? um(e, t) : rs(e, t); - }, fs = (e, t) => { - at(e.inputs[0].dims, t.axes, t.noopWithEmptyAxes) ? dm(e, t) : ns(e, t); - }, hs = (e, t) => { - at(e.inputs[0].dims, t.axes, t.noopWithEmptyAxes) ? lm(e, t) : os(e, t); - }, gs = (e, t) => { - at(e.inputs[0].dims, t.axes, t.noopWithEmptyAxes) ? cm(e, t) : is(e, t); - }, ys = (e, t) => { - at(e.inputs[0].dims, t.axes, t.noopWithEmptyAxes) ? mm(e, t) : as(e, t); - }, bs = (e, t) => { - at(e.inputs[0].dims, t.axes, t.noopWithEmptyAxes) ? fm(e, t) : ss(e, t); - }, ws = (e, t) => { - at(e.inputs[0].dims, t.axes, t.noopWithEmptyAxes) ? hm(e, t) : us(e, t); - }, _s = (e, t) => { - at(e.inputs[0].dims, t.axes, t.noopWithEmptyAxes) ? gm(e, t) : ds(e, t); - }, vs = (e, t) => { - at(e.inputs[0].dims, t.axes, t.noopWithEmptyAxes) ? sm(e, t) : ls(e, t); - }; - }); - var $s, xs, Ss, uo, Ts = V(() => { - "use strict"; - Q(); - Ie(); - qr(); - $s = (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."); - }, xs = (e, t) => { - $s(e.inputs); - let r = (n, o, i) => { - let a = []; - for (let d = 0; d < n.rank; d++) (i.indexOf(d) >= 0 || i.length === 0) && a.push(`input_indices[${d}] = 0;`); - return [`${a.join(` - `)}`, `var value = ${n.getByIndices("input_indices")}; - var best_index : i32 = 0;`, `if (${n.getByIndices("input_indices")} ${t.selectLastIndex > 0 ? "<=" : "<"} value) { - value = ${n.getByIndices("input_indices")}; - best_index = i32(last_index); - }`, "", o.setByOffset("global_idx", "best_index")]; - }; - e.compute(jr("ArgMin", { hint: t.cacheKey, inputDependencies: ["rank"] }, [e.inputs[0]], r, [t.axis], 7, t.keepDims), { inputs: [0] }); - }, Ss = (e, t) => { - $s(e.inputs); - let r = (n, o, i) => { - let a = []; - for (let d = 0; d < n.rank; d++) (i.indexOf(d) >= 0 || i.length === 0) && a.push(`input_indices[${d}] = 0;`); - return [`${a.join(` - `)}`, `var value = ${n.getByIndices("input_indices")}; - var best_index : i32 = 0;`, `if (${n.getByIndices("input_indices")} ${t.selectLastIndex > 0 ? ">=" : ">"} value) { - value = ${n.getByIndices("input_indices")}; - best_index = i32(last_index); - }`, "", o.setByOffset("global_idx", "best_index")]; - }; - e.compute(jr("argMax", { hint: t.cacheKey, inputDependencies: ["rank"] }, [e.inputs[0]], r, [t.axis], 7, t.keepDims), { inputs: [0] }); - }, uo = (e) => J(e); - }); - var ym, bm, wm, _m, Wt, vm, Is, Kr = V(() => { - "use strict"; - Q(); - ie(); - Hr(); - ae(); - ym = (e, t) => { - let r = e[0], n = e[1], o = e[2], i = e[3], a = e[4], d = e[5]; - if (a && d) throw new Error("Attention cannot have both past and attention_bias"); - if (r.dims.length !== 3) throw new Error('Input "input" must have 3 dimensions'); - let l = r.dims[0], c = r.dims[1], m = r.dims[2]; - if (o.dims.length !== 1) throw new Error('Input "bias" is expected to have 1 dimensions'); - if (n.dims.length !== 2) throw new Error('Input "weights" is expected to have 2 dimensions'); - if (n.dims[0] !== m) throw new Error("Input 1 dimension 0 should have same length as dimension 2 of input 0"); - if (o.dims[0] !== n.dims[1]) throw new Error('Input "bias" dimension 0 should have same length as dimension 1 of input "weights"'); - let u = o.dims[0] / 3, h = u, w = h; - if (t.qkvHiddenSizes.length > 0) { - if (t.qkvHiddenSizes.length !== 3) throw new Error("qkv_hidden_sizes attribute should have 3 elements"); - for (let x of t.qkvHiddenSizes) if (x % t.numHeads !== 0) throw new Error("qkv_hidden_sizes should be divisible by num_heads"); - u = t.qkvHiddenSizes[0], h = t.qkvHiddenSizes[1], w = t.qkvHiddenSizes[2]; - } - let g = c; - if (u !== h) throw new Error("qkv_hidden_sizes first element should be same as the second"); - if (o.dims[0] !== u + h + w) throw new Error('Input "bias" dimension 0 should have same length as sum of Q/K/V hidden sizes'); - let y = 0; - if (a) { - if (h !== w) throw new Error('Input "past" expect k_hidden_size == v_hidden_size'); - if (a.dims.length !== 5) throw new Error('Input "past" must have 5 dimensions'); - if (a.dims[0] !== 2) throw new Error('Input "past" first dimension must be 2'); - if (a.dims[1] !== l) throw new Error('Input "past" second dimension must be batch_size'); - if (a.dims[2] !== t.numHeads) throw new Error('Input "past" third dimension must be num_heads'); - if (a.dims[4] !== h / t.numHeads) throw new Error('Input "past" fifth dimension must be k_hidden_size / num_heads'); - t.pastPresentShareBuffer || (y = a.dims[3]); - } - let S = g + y, $ = -1, v = 0; - if (i) throw new Error("Mask not supported"); - if (a) throw new Error("past is not supported"); - if (d) { - if (d.dims.length !== 4) throw new Error('Input "attention_bias" must have 4 dimensions'); - if (d.dims[0] !== l || d.dims[1] !== t.numHeads || d.dims[2] !== c || d.dims[3] !== S) throw new Error('Expect "attention_bias" shape (batch_size, num_heads, sequence_length, total_sequence_length)'); - } - return { batchSize: l, sequenceLength: c, pastSequenceLength: y, kvSequenceLength: g, totalSequenceLength: S, maxSequenceLength: $, inputHiddenSize: m, hiddenSize: u, vHiddenSize: w, headSize: Math.floor(u / t.numHeads), vHeadSize: Math.floor(w / t.numHeads), numHeads: t.numHeads, isUnidirectional: false, pastPresentShareBuffer: false, maskFilterValue: t.maskFilterValue, maskType: v, scale: t.scale, broadcastResPosBias: false, passPastInKv: false, qkvFormat: 1 }; - }, bm = (e, t, r) => { - let n = we(r), o = 64, i = r / n; - i < o && (o = 32); - let a = Math.ceil(r / n / o), d = [{ type: 1, data: 1 / r }, { type: 12, data: i }, { type: 12, data: a }], l = he(e.dataType, n), c = Ee(1, n), m = ["type"], u = (h) => { - let w = M("x", e.dataType, e.dims, n), g = Ee(e.dataType), y = [{ 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; - ${h.registerUniforms(y).declareVariables(w)} - ${h.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 = ${c}(-3.402823e+38f); - for (var i: u32 = 0; i < uniforms.elements_per_thread && i + local_offset < uniforms.d_comp; i++) { - thread_max_vector = max(${c}(x[offset + i]), thread_max_vector); - } - thread_max[local_idx] = ${(() => { - switch (n) { - 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: ${n}`); - } - })()}; - 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 = ${c}(0); - for (var i: u32 = 0; i < uniforms.elements_per_thread && i + local_offset < uniforms.d_comp; i++) { - sum_vector += exp(${c}(x[offset + i]) - max_value); - } - thread_sum[local_idx] = ${(() => { - switch (n) { - 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: ${n}`); - } - })()}; - 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] = ${w.type.value}(${g}(uniforms.d_inv)); - } - } else { - for (var i: u32 = 0; i < uniforms.elements_per_thread && i + local_offset < uniforms.d_comp; i++) { - var f32input = ${c}(x[offset + i]); - x[offset + i] = ${w.type.value}(exp(f32input - max_value) / sum); - } - } - }`; - }; - return { name: "AttentionProbsSoftmax", shaderCache: { hint: `${o};${l};${n}`, inputDependencies: m }, getShaderSource: u, getRunData: () => ({ outputs: [], dispatchGroup: { x: t }, programUniforms: d }) }; - }, wm = (e, t, r, n, o, i, a, d) => { - let l = d + i.kvSequenceLength, c = [i.batchSize, i.numHeads, i.sequenceLength, l], m = i.kvNumHeads === void 0 && e > 1 && n, u = m ? [i.batchSize, i.numHeads, l, i.headSize] : void 0, h = a.scale === 0 ? 1 / Math.sqrt(i.headSize) : a.scale, w = we(i.headSize), g = i.headSize / w, y = 12, S = { x: Math.ceil(l / y), y: Math.ceil(i.sequenceLength / y), z: i.batchSize * i.numHeads }, $ = [{ type: 12, data: i.sequenceLength }, { type: 12, data: g }, { type: 12, data: l }, { type: 12, data: i.numHeads }, { type: 1, data: h }, { type: 12, data: d }, { type: 12, data: i.kvSequenceLength }], v = m && n && k.size(n.dims) > 0, x = ["type", "type"]; - v && x.push("type"), o && x.push("type"); - let T = [{ dims: c, dataType: t.dataType, gpuDataType: 0 }]; - m && T.push({ dims: u, dataType: t.dataType, gpuDataType: 0 }); - let C = (A) => { - let P = E("q", t.dataType, t.dims, w), B = E("key", r.dataType, r.dims, w), N = [P, B]; - if (v) { - let se = E("past_key", n.dataType, n.dims, w); - N.push(se); - } - o && N.push(E("attention_bias", o.dataType, o.dims)); - let W = M("output", t.dataType, c), K = [W]; - m && K.push(M("present_key", t.dataType, u, w)); - let Z = Ee(1, w), ee = [{ 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 = ${y}u; - - var tileQ: array<${P.type.storage}, ${y * y}>; - var tileK: array<${P.type.storage}, ${y * y}>; - ${A.registerUniforms(ee).declareVariables(...N, ...K)} - ${A.mainStart([y, y, 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; - ${/* @__PURE__ */ (() => v && m ? ` - 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;`)()} - ${m ? "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; - ${/* @__PURE__ */ (() => v && m ? ` - 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];")()} - ${m ? "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 (w) { - 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: ${w}`); - } - })()}; - output[outputIdx] = ${W.type.value} (sum * uniforms.alpha) + ${o ? "attention_bias[outputIdx]" : "0.0"}; - } - }`; - }; - return { name: "AttentionProbs", shaderCache: { hint: `${w};${o !== void 0};${n !== void 0};${e}`, inputDependencies: x }, getRunData: () => ({ outputs: T, dispatchGroup: S, programUniforms: $ }), getShaderSource: C }; - }, _m = (e, t, r, n, o, i) => { - let a = i + o.kvSequenceLength, d = o.nReps ? o.nReps : 1, l = o.vHiddenSize * d, c = o.kvNumHeads == null && e > 1 && n, m = c ? [o.batchSize, o.numHeads, a, o.headSize] : void 0, u = [o.batchSize, o.sequenceLength, l], h = 12, w = { x: Math.ceil(o.vHeadSize / h), y: Math.ceil(o.sequenceLength / h), z: o.batchSize * o.numHeads }, g = [{ type: 12, data: o.sequenceLength }, { type: 12, data: a }, { type: 12, data: o.vHeadSize }, { type: 12, data: o.numHeads }, { type: 12, data: l }, { type: 12, data: i }, { type: 12, data: o.kvSequenceLength }], y = c && n && k.size(n.dims) > 0, S = ["type", "type"]; - y && S.push("type"); - let $ = [{ dims: u, dataType: t.dataType, gpuDataType: 0 }]; - c && $.push({ dims: m, dataType: t.dataType, gpuDataType: 0 }); - let v = (x) => { - let T = E("probs", t.dataType, t.dims), C = E("v", r.dataType, r.dims), A = [T, C]; - y && A.push(E("past_value", n.dataType, n.dims)); - let B = [M("output", t.dataType, u)]; - c && B.push(M("present_value", t.dataType, m)); - let N = [{ 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 = ${h}u; - var tileQ: array<${T.type.value}, ${h * h}>; - var tileK: array<${T.type.value}, ${h * h}>; - ${x.registerUniforms(N).declareVariables(...A, ...B)} - ${x.mainStart([h, h, 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; - ${/* @__PURE__ */ (() => y && c ? ` - 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; - `)()} - ${c ? "let presentValueOffset = headIdx * uniforms.N * uniforms.K + n;" : ""} - var value = ${T.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; - ${/* @__PURE__ */ (() => y && c ? ` - 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]; - `)()} - ${c ? "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: `${n !== void 0};${e}`, inputDependencies: S }, getRunData: () => ({ outputs: $, dispatchGroup: w, programUniforms: g }), getShaderSource: v }; - }, Wt = (e, t, r, n, o, i, a, d, l, c, m) => { - let u = Math.min(e.outputCount, 1 + (a ? 1 : 0) + (d ? 1 : 0)), h = c.kvNumHeads !== void 0 || u > 1 ? c.pastSequenceLength : 0, w = h + c.kvSequenceLength, g = l && k.size(l.dims) > 0 ? l : void 0, y = [t, r]; - c.kvNumHeads === void 0 && u > 1 && a && k.size(a.dims) > 0 && y.push(a), g && y.push(g); - let S = e.compute(wm(u, t, r, a, g, c, m, h), { inputs: y, outputs: c.kvNumHeads === void 0 && u > 1 ? [-1, 1] : [-1] })[0]; - e.compute(bm(S, c.batchSize * c.numHeads * c.sequenceLength, w), { inputs: [S], outputs: [] }); - let $ = [S, n]; - c.kvNumHeads === void 0 && u > 1 && d && k.size(d.dims) > 0 && $.push(d), e.compute(_m(u, S, n, d, c, h), { inputs: $, outputs: c.kvNumHeads === void 0 && u > 1 ? [0, 2] : [0] }); - }, vm = (e, t) => { - let r = [t.batchSize, t.numHeads, t.sequenceLength, t.headSize], n = t.sequenceLength, o = t.inputHiddenSize, i = t.headSize, a = 12, d = { x: Math.ceil(t.headSize / a), y: Math.ceil(t.sequenceLength / a), z: t.batchSize * t.numHeads }, l = [e.inputs[0], e.inputs[1], e.inputs[2]], c = [{ type: 12, data: n }, { type: 12, data: o }, { type: 12, data: i }, { type: 12, data: t.numHeads }, { type: 12, data: t.headSize }, { type: 12, data: t.hiddenSize }, { type: 12, data: t.hiddenSize + t.hiddenSize + t.vHiddenSize }], m = (u) => { - let h = M("output_q", l[0].dataType, r), w = M("output_k", l[0].dataType, r), g = M("output_v", l[0].dataType, r), y = E("input", l[0].dataType, l[0].dims), S = E("weight", l[1].dataType, l[1].dims), $ = E("bias", l[2].dataType, l[2].dims), v = y.type.storage, x = [{ name: "M", type: "u32" }, { name: "K", type: "u32" }, { name: "N", type: "u32" }, { name: "num_heads", type: "u32" }, { name: "head_size", type: "u32" }, { name: "hidden_size", type: "u32" }, { name: "ldb", type: "u32" }]; - return ` - const TILE_SIZE = ${a}u; - var tileInput: array<${v}, ${a * a}>; - var tileWeightQ: array<${v}, ${a * a}>; - var tileWeightK: array<${v}, ${a * a}>; - var tileWeightV: array<${v}, ${a * a}>; - ${u.registerUniforms(x).declareVariables(y, S, $, h, w, g)} - ${u.mainStart([a, a, 1])} - let batchIndex = workgroup_id.z / uniforms.num_heads; - let headNumber = workgroup_id.z % uniforms.num_heads; - let m = global_id.y; - let n = global_id.x; - - let inputOffset = batchIndex * (uniforms.M * uniforms.K) + m * uniforms.K; - let biasOffsetQ = headNumber * uniforms.head_size; - let biasOffsetK = uniforms.hidden_size + biasOffsetQ; - let biasOffsetV = uniforms.hidden_size + biasOffsetK; - - var valueQ = ${v}(0); - var valueK = ${v}(0); - var valueV = ${v}(0); - for (var w: u32 = 0u; w < uniforms.K; w += TILE_SIZE) { - if (m < uniforms.M && w + local_id.x < uniforms.K) { - tileInput[TILE_SIZE * local_id.y + local_id.x] = input[inputOffset + w + local_id.x]; - } - if (n < uniforms.N && w + local_id.y < uniforms.K) { - let offset = n + (w + local_id.y) * uniforms.ldb; - tileWeightQ[TILE_SIZE * local_id.y + local_id.x] = weight[biasOffsetQ + offset]; - tileWeightK[TILE_SIZE * local_id.y + local_id.x] = weight[biasOffsetK + offset]; - tileWeightV[TILE_SIZE * local_id.y + local_id.x] = weight[biasOffsetV + offset]; - } - workgroupBarrier(); - for (var k: u32 = 0u; k ({ outputs: [{ dims: r, dataType: e.inputs[0].dataType, gpuDataType: 0 }, { dims: r, dataType: e.inputs[0].dataType, gpuDataType: 0 }, { dims: r, dataType: e.inputs[0].dataType, gpuDataType: 0 }], dispatchGroup: d, programUniforms: c }), getShaderSource: m }, { inputs: l, outputs: [-1, -1, -1] }); - }, Is = (e, t) => { - let r = ym(e.inputs, t), [n, o, i] = vm(e, r); - return Wt(e, n, o, i, e.inputs[4], void 0, void 0, void 0, e.inputs[5], r, t); - }; - }); - var $m, xm, Sm, Cs, As = V(() => { - "use strict"; - Ke(); - Q(); - ie(); - Ie(); - ae(); - $m = (e, t) => { - if (!e || e.length !== 5) throw new Error("BatchNormalization requires 5 inputs"); - let r = (n, o, i) => { - let a = o.length; - if (a !== n.length) throw new Error(`${i}: num dimensions != ${a}`); - o.forEach((d, l) => { - if (d !== n[l]) throw new Error(`${i}: dim[${l}] do not match`); - }); - }; - if (e[0].dims.length > 1) { - let n = t.format === "NHWC" ? t.spatial ? e[0].dims.slice(-1) : e[0].dims.slice(-1).concat(e[0].dims.slice(1, e[0].dims.length - 1)) : e[0].dims.slice(1, t.spatial ? 2 : void 0); - r(e[1].dims, n, "Invalid input scale"), r(e[2].dims, n, "Invalid input B"), r(e[3].dims, n, "Invalid input mean"), r(e[4].dims, n, "Invalid input var"); - } else r(e[1].dims, [1], "Invalid input scale"), r(e[2].dims, [1], "Invalid input B"), r(e[3].dims, [1], "Invalid input mean"), r(e[4].dims, [1], "Invalid input var"); - }, xm = (e, t) => { - let { epsilon: r, spatial: n, format: o } = t, i = e[0].dims, a = n ? we(i[i.length - 1]) : 1, d = o === "NHWC" && i.length > 1 ? a : 1, l = k.size(i) / a, c = n, m = c ? i.length : i, u = E("x", e[0].dataType, e[0].dims, a), h = E("scale", e[1].dataType, e[1].dims, d), w = E("bias", e[2].dataType, e[2].dims, d), g = E("inputMean", e[3].dataType, e[3].dims, d), y = E("inputVar", e[4].dataType, e[4].dims, d), S = M("y", e[0].dataType, m, a), $ = () => { - let x = ""; - if (n) x = `let cOffset = ${i.length === 1 ? "0u" : o === "NHWC" ? `outputIndices[${i.length - 1}] / ${a}` : "outputIndices[1]"};`; - else if (o === "NCHW") x = ` - ${S.indicesSet("outputIndices", "0", "0")} - let cOffset = ${S.indicesToOffset("outputIndices")};`; - else { - x = `var cIndices = ${h.type.indices}(0); - cIndices[0] = outputIndices[${i.length - 1}];`; - for (let T = 1; T < h.rank; T++) x += `cIndices[${T}] = outputIndices[${T}];`; - x += `let cOffset = ${h.indicesToOffset("cIndices")};`; - } - return x; - }, v = (x) => ` - const epsilon = ${r}; - ${x.registerUniform("outputSize", "u32").declareVariables(u, h, w, g, y, S)} - ${x.mainStart()} - ${x.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.outputSize")} - var outputIndices = ${S.offsetToIndices(`global_idx * ${a}`)}; - ${$()} - let scale = ${h.getByOffset("cOffset")}; - let bias = ${w.getByOffset("cOffset")}; - let inputMean = ${g.getByOffset("cOffset")}; - let inputVar = ${y.getByOffset("cOffset")}; - let x = ${u.getByOffset("global_idx")}; - let value = (x - inputMean) * inverseSqrt(inputVar + epsilon) * scale + bias; - ${S.setByOffset("global_idx", "value")} - }`; - return { name: "BatchNormalization", shaderCache: { hint: `${t.epsilon}_${t.format}_${n}_${a}`, inputDependencies: c ? ["rank", "type", "type", "type", "type"] : void 0 }, getShaderSource: v, getRunData: () => ({ outputs: [{ dims: e[0].dims, dataType: e[0].dataType }], dispatchGroup: { x: Math.ceil(l / 64) }, programUniforms: c ? [{ type: 12, data: l }, ...R(i)] : [{ type: 12, data: l }] }) }; - }, Sm = (e) => J(e), Cs = (e, t) => { - let { inputs: r, outputCount: n } = e, o = Sm({ ...t, outputCount: n }); - if (_e.webgpu.validateInputContent && $m(r, o), t.trainingMode) throw new Error("BatchNormalization trainingMode is not supported yet."); - e.compute(xm(r, o)); - }; - }); - var Tm, Im, ks, Es = V(() => { - "use strict"; - ie(); - ae(); - Tm = (e) => { - if (e[0].dims.length !== 3) throw new Error("input should have 3 dimensions"); - if (![320, 640, 1280].includes(e[0].dims[2])) throw new Error("number of channels should be 320, 640 or 1280"); - if (e[1].dims.length !== 1) throw new Error("bias is expected to have 1 dimensions"); - if (e[0].dims[2] !== e[1].dims[0]) throw new Error("last dimension of input and bias are not the same"); - }, Im = (e) => { - let t = e[0].dims, r = e[0].dims[2], n = k.size(t) / 4, o = e[0].dataType, i = E("input", o, t, 4), a = E("bias", o, [r], 4), d = E("residual", o, t, 4), l = M("output", o, t, 4); - return { name: "BiasAdd", getRunData: () => ({ outputs: [{ dims: t, dataType: e[0].dataType }], dispatchGroup: { x: Math.ceil(n / 64) } }), getShaderSource: (m) => ` - const channels = ${r}u / 4; - ${m.declareVariables(i, a, d, l)} - - ${m.mainStart()} - ${m.guardAgainstOutOfBoundsWorkgroupSizes(n)} - let value = ${i.getByOffset("global_idx")} - + ${a.getByOffset("global_idx % channels")} + ${d.getByOffset("global_idx")}; - ${l.setByOffset("global_idx", "value")} - }` }; - }, ks = (e) => { - Tm(e.inputs), e.compute(Im(e.inputs)); - }; - }); - var Cm, ge, Ps, zs, Os, Bs, Ds, Ms, Rs, Us, Vs, Am, Ns, Ws, Ls, Hs, Qt, Gs, Yr, Fs, qs, js, Ks, Ys, Xs, Zs, Qs, Js, eu, tu, ru, nu, ou, iu, au, su, uu, lo, co, du, lu, cu, km, Em, pu, Xr = V(() => { - "use strict"; - Q(); - ie(); - Ie(); - ae(); - Cm = (e, t, r, n, o, i, a) => { - let d = Math.ceil(t / 4), l = ""; - typeof o == "string" ? l = `${o}(a)` : l = o("a"); - let c = E("inputData", r, [d], 4), m = M("outputData", n, [d], 4), u = [{ name: "vec_size", type: "u32" }]; - return a && u.push(...a), ` - ${e.registerUniforms(u).declareVariables(c, m)} - - ${i ?? ""} - - ${e.mainStart()} - ${e.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.vec_size")} - - let a = ${c.getByOffset("global_idx")}; - ${m.setByOffset("global_idx", l)} - }`; - }, ge = (e, t, r, n, o, i = e.dataType, a, d) => { - let l = [{ type: 12, data: Math.ceil(k.size(e.dims) / 4) }]; - return a && l.push(...a), { name: t, shaderCache: { hint: o, inputDependencies: ["type"] }, getShaderSource: (c) => Cm(c, k.size(e.dims), e.dataType, i, r, n, d), getRunData: (c) => ({ outputs: [{ dims: e.dims, dataType: i }], dispatchGroup: { x: Math.ceil(k.size(c[0].dims) / 64 / 4) }, programUniforms: l }) }; - }, Ps = (e) => { - e.compute(ge(e.inputs[0], "Abs", "abs")); - }, zs = (e) => { - e.compute(ge(e.inputs[0], "Acos", "acos")); - }, Os = (e) => { - e.compute(ge(e.inputs[0], "Acosh", "acosh")); - }, Bs = (e) => { - e.compute(ge(e.inputs[0], "Asin", "asin")); - }, Ds = (e) => { - e.compute(ge(e.inputs[0], "Asinh", "asinh")); - }, Ms = (e) => { - e.compute(ge(e.inputs[0], "Atan", "atan")); - }, Rs = (e) => { - e.compute(ge(e.inputs[0], "Atanh", "atanh")); - }, Us = (e) => J(e), Vs = (e, t) => { - let r; - switch (t.to) { - case 10: - r = "vec4"; - break; - case 1: - r = "vec4"; - break; - case 12: - r = "vec4"; - break; - case 6: - r = "vec4"; - break; - case 9: - r = "vec4"; - break; - default: - throw new RangeError(`not supported type (specified in attribute 'to' from 'Cast' operator): ${t.to}`); - } - e.compute(ge(e.inputs[0], "Cast", r, void 0, t.cacheKey, t.to)); - }, Am = (e) => { - let t, r, n = e.length >= 2 && e[1].data !== 0, o = e.length >= 3 && e[2].data !== 0; - switch (e[0].dataType) { - case 1: - t = n ? e[1].getFloat32Array()[0] : -34028234663852886e22, r = o ? e[2].getFloat32Array()[0] : 34028234663852886e22; - break; - case 10: - t = n ? e[1].getUint16Array()[0] : 64511, r = o ? e[2].getUint16Array()[0] : 31743; - break; - default: - throw new Error("Unsupport data type"); - } - return J({ min: t, max: r }); - }, Ns = (e, t) => { - let r = t || Am(e.inputs), n = Ee(e.inputs[0].dataType); - e.compute(ge(e.inputs[0], "Clip", (o) => `clamp(${o}, vec4<${n}>(uniforms.min), vec4<${n}>(uniforms.max))`, void 0, r.cacheKey, void 0, [{ type: e.inputs[0].dataType, data: r.min }, { type: e.inputs[0].dataType, data: r.max }], [{ name: "min", type: n }, { name: "max", type: n }]), { inputs: [0] }); - }, Ws = (e) => { - e.compute(ge(e.inputs[0], "Ceil", "ceil")); - }, Ls = (e) => { - e.compute(ge(e.inputs[0], "Cos", "cos")); - }, Hs = (e) => { - e.compute(ge(e.inputs[0], "Cosh", "cosh")); - }, Qt = (e) => J(e), Gs = (e, t) => { - let r = Ee(e.inputs[0].dataType); - e.compute(ge(e.inputs[0], "Elu", (n) => `elu_vf32(${n})`, ` - const elu_alpha_ = ${r}(${t.alpha}); - - fn elu_f32(a: ${r}) -> ${r} { - return select((exp(a) - 1.0) * elu_alpha_, a, a >= 0.0); - } - - fn elu_vf32(v: vec4<${r}>) -> vec4<${r}> { - return vec4(elu_f32(v.x), elu_f32(v.y), elu_f32(v.z), elu_f32(v.w)); - }`, t.cacheKey)); - }, Yr = (e = "f32") => ` - const r0: ${e} = 0.3275911; - const r1: ${e} = 0.254829592; - const r2: ${e} = -0.284496736; - const r3: ${e} = 1.421413741; - const r4: ${e} = -1.453152027; - const r5: ${e} = 1.061405429; - - fn erf_vf32(v: vec4<${e}>) -> vec4<${e}> { - let absv = abs(v); - let x = 1.0 / (1.0 + r0 * absv); - return sign(v) * (1.0 - ((((r5 * x + r4) * x + r3) * x + r2) * x + r1) * x * exp(-absv * absv)); - }`, Fs = (e) => { - let t = Ee(e.inputs[0].dataType); - e.compute(ge(e.inputs[0], "Erf", (r) => `erf_vf32(${r})`, Yr(t))); - }, qs = (e) => { - e.compute(ge(e.inputs[0], "Exp", "exp")); - }, js = (e) => { - e.compute(ge(e.inputs[0], "Floor", "floor")); - }, Ks = (e) => { - let t = Ee(e.inputs[0].dataType); - e.compute(ge(e.inputs[0], "Gelu", (r) => `0.5 * ${r} * (1.0 + erf_vf32(${r} * 0.7071067811865475))`, Yr(t))); - }, Ys = (e, t) => { - let r = Ee(e.inputs[0].dataType); - e.compute(ge(e.inputs[0], "LeakyRelu", (n) => `select(leaky_relu_alpha_ * ${n}, ${n}, ${n} >= vec4<${r}>(0.0))`, `const leaky_relu_alpha_ = ${r}(${t.alpha});`, t.cacheKey)); - }, Xs = (e) => { - e.compute(ge(e.inputs[0], "Not", (t) => `!${t}`)); - }, Zs = (e) => { - e.compute(ge(e.inputs[0], "Neg", (t) => `-${t}`)); - }, Qs = (e) => { - e.compute(ge(e.inputs[0], "Reciprocal", (t) => `1.0/${t}`)); - }, Js = (e) => { - let t = Ee(e.inputs[0].dataType); - e.compute(ge(e.inputs[0], "Relu", (r) => `select(vec4<${t}>(0.0), ${r}, ${r} > vec4<${t}>(0.0))`)); - }, eu = (e) => { - e.compute(ge(e.inputs[0], "Sigmoid", (t) => `(1.0 / (1.0 + exp(-${t})))`)); - }, tu = (e) => J(e), ru = (e, t) => { - let r = Ee(e.inputs[0].dataType); - e.compute(ge(e.inputs[0], "HardSigmoid", (n) => `max(vec4<${r}>(0.0), min(vec4<${r}>(1.0), ${t.alpha} * ${n} + vec4<${r}>(${t.beta})))`, void 0, t.cacheKey)); - }, nu = (e) => { - e.compute(ge(e.inputs[0], "Sin", "sin")); - }, ou = (e) => { - e.compute(ge(e.inputs[0], "Sinh", "sinh")); - }, iu = (e) => { - e.compute(ge(e.inputs[0], "Sqrt", "sqrt")); - }, au = (e) => { - e.compute(ge(e.inputs[0], "Tan", "tan")); - }, su = (e) => `sign(${e}) * (1 - exp(-2 * abs(${e}))) / (1 + exp(-2 * abs(${e})))`, uu = (e) => { - e.compute(ge(e.inputs[0], "Tanh", su)); - }, lo = (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 ${su("v")}; - } - `, co = (e) => `(fast_gelu_a + fast_gelu_a * tanh_v(${e} * (fast_gelu_c * ${e} * ${e} + fast_gelu_b))) * ${e}`, du = (e) => { - let t = Ee(e.inputs[0].dataType); - e.compute(ge(e.inputs[0], "FastGelu", co, lo(t), void 0, e.inputs[0].dataType)); - }, lu = (e, t) => { - let r = Ee(e.inputs[0].dataType); - return e.compute(ge(e.inputs[0], "ThresholdedRelu", (n) => `select(vec4<${r}>(0.0), ${n}, ${n} > thresholded_relu_alpha_)`, `const thresholded_relu_alpha_ = vec4<${r}>(${t.alpha});`, t.cacheKey)), 0; - }, cu = (e) => { - e.compute(ge(e.inputs[0], "Log", "log")); - }, km = (e, t) => ` - const alpha = vec4<${e}>(${t}); - const one = ${e}(1.0); - const zero = ${e}(0.0); - - fn quick_gelu_impl(x: vec4<${e}>) -> vec4<${e}> { - let v = x *alpha; - var x1 : vec4<${e}>; - for (var i = 0; i < 4; i = i + 1) { - if (v[i] >= zero) { - x1[i] = one / (one + exp(-v[i])); - } else { - x1[i] = one - one / (one + exp(v[i])); - } - } - return x * x1; - } - `, Em = (e) => `quick_gelu_impl(${e})`, pu = (e, t) => { - let r = Ee(e.inputs[0].dataType); - e.compute(ge(e.inputs[0], "QuickGelu", Em, km(r, t.alpha), t.cacheKey, e.inputs[0].dataType)); - }; - }); - var Pm, zm, fu, hu = V(() => { - "use strict"; - ie(); - ae(); - Xr(); - Pm = (e) => { - if (e[0].dims.length !== 3) throw new Error("input should have 3 dimensions"); - if (![2560, 5120, 10240].includes(e[0].dims[2])) throw new Error("hidden state should be 2560, 5120 or 10240"); - if (e[1].dims.length !== 1) throw new Error("bias is expected to have 1 dimensions"); - if (e[0].dims[2] !== e[1].dims[0]) throw new Error("last dimension of input and bias are not the same"); - }, zm = (e) => { - let t = e[0].dims.slice(); - t[2] = t[2] / 2; - let r = E("input", e[0].dataType, e[0].dims, 4), n = E("bias", e[0].dataType, [e[0].dims[2]], 4), o = M("output", e[0].dataType, t, 4), i = k.size(t) / 4, a = he(e[0].dataType); - return { name: "BiasSplitGelu", getRunData: () => ({ outputs: [{ dims: t, dataType: e[0].dataType }], dispatchGroup: { x: Math.ceil(i / 64) } }), getShaderSource: (l) => ` - const M_SQRT2 = sqrt(2.0); - const halfChannels = ${e[0].dims[2] / 4 / 2}u; - - ${l.declareVariables(r, n, o)} - - ${Yr(a)} - - ${l.mainStart()} - ${l.guardAgainstOutOfBoundsWorkgroupSizes(i)} - let biasIdx = global_idx % halfChannels; - let batchIndex = global_idx / halfChannels; - let inputOffset = biasIdx + batchIndex * halfChannels * 2; - let valueLeft = input[inputOffset] + bias[biasIdx]; - let valueRight = input[inputOffset + halfChannels] + bias[biasIdx + halfChannels]; - let geluRight = valueRight * 0.5 * (erf_vf32(valueRight / M_SQRT2) + 1); - - ${o.setByOffset("global_idx", "valueLeft * geluRight")} - }` }; - }, fu = (e) => { - Pm(e.inputs), e.compute(zm(e.inputs)); - }; - }); - var Om, Bm, st, gu, yu, bu, wu, _u, vu, $u, xu, Su, Tu, Iu = V(() => { - "use strict"; - Q(); - ie(); - ae(); - Om = (e, t, r, n, o, i, a, d, l, c, m, u) => { - let h, w; - typeof d == "string" ? h = w = (v, x) => `${d}((${v}),(${x}))` : typeof d == "function" ? h = w = d : (h = d.scalar, w = d.vector); - let g = M("outputData", m, n.length, 4), y = E("aData", l, t.length, 4), S = E("bData", c, r.length, 4), $; - if (o) if (i) { - let v = k.size(t) === 1, x = k.size(r) === 1, T = t.length > 0 && t[t.length - 1] % 4 === 0, C = r.length > 0 && r[r.length - 1] % 4 === 0; - v || x ? $ = g.setByOffset("global_idx", w(v ? `${y.type.value}(${y.getByOffset("0")}.x)` : y.getByOffset("global_idx"), x ? `${S.type.value}(${S.getByOffset("0")}.x)` : S.getByOffset("global_idx"))) : $ = ` - let outputIndices = ${g.offsetToIndices("global_idx * 4u")}; - let offsetA = ${y.broadcastedIndicesToOffset("outputIndices", g)}; - let offsetB = ${S.broadcastedIndicesToOffset("outputIndices", g)}; - ${g.setByOffset("global_idx", w(a || T ? y.getByOffset("offsetA / 4u") : `${y.type.value}(${y.getByOffset("offsetA / 4u")}[offsetA % 4u])`, a || C ? S.getByOffset("offsetB / 4u") : `${S.type.value}(${S.getByOffset("offsetB / 4u")}[offsetB % 4u])`))} - `; - } else $ = g.setByOffset("global_idx", w(y.getByOffset("global_idx"), S.getByOffset("global_idx"))); - else { - if (!i) throw new Error("no necessary to use scalar implementation for element-wise binary op implementation."); - let v = (x, T, C = "") => { - let A = `aData[indexA${T}][componentA${T}]`, P = `bData[indexB${T}][componentB${T}]`; - return ` - let outputIndices${T} = ${g.offsetToIndices(`global_idx * 4u + ${T}u`)}; - let offsetA${T} = ${y.broadcastedIndicesToOffset(`outputIndices${T}`, g)}; - let offsetB${T} = ${S.broadcastedIndicesToOffset(`outputIndices${T}`, g)}; - let indexA${T} = offsetA${T} / 4u; - let indexB${T} = offsetB${T} / 4u; - let componentA${T} = offsetA${T} % 4u; - let componentB${T} = offsetB${T} % 4u; - ${x}[${T}] = ${C}(${h(A, P)}); - `; - }; - m === 9 ? $ = ` - var data = vec4(0); - ${v("data", 0, "u32")} - ${v("data", 1, "u32")} - ${v("data", 2, "u32")} - ${v("data", 3, "u32")} - outputData[global_idx] = dot(vec4(0x1, 0x100, 0x10000, 0x1000000), vec4(data));` : $ = ` - ${v("outputData[global_idx]", 0)} - ${v("outputData[global_idx]", 1)} - ${v("outputData[global_idx]", 2)} - ${v("outputData[global_idx]", 3)} - `; - } - return ` - ${e.registerUniform("vec_size", "u32").declareVariables(y, S, g)} - - ${u ?? ""} - - ${e.mainStart()} - ${e.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.vec_size")} - ${$} - }`; - }, Bm = (e, t, r, n, o, i, a = r.dataType) => { - let d = !k.areEqual(r.dims, n.dims), l = r.dims, c = k.size(r.dims), m = false, u = false, h = [d]; - if (d) { - let w = rt.calcShape(r.dims, n.dims, false); - if (!w) throw new Error("Can't perform binary op on the given tensors"); - l = w, c = k.size(l); - let g = k.size(r.dims) === 1, y = k.size(n.dims) === 1, S = r.dims.length > 0 && r.dims[r.dims.length - 1] % 4 === 0, $ = n.dims.length > 0 && n.dims[n.dims.length - 1] % 4 === 0; - h.push(g), h.push(y), h.push(S), h.push($); - let v = 1; - for (let x = 1; x < l.length; x++) { - let T = r.dims[r.dims.length - x] ?? 1, C = n.dims[n.dims.length - x] ?? 1; - if (T === C) v *= T; - else break; - } - v % 4 === 0 ? (u = true, m = true) : (g || y || S || $) && (m = true); - } else m = true; - return h.push(m), { name: e, shaderCache: { hint: t + h.map((w) => w.toString()).join("_"), inputDependencies: ["rank", "rank"] }, getShaderSource: (w) => Om(w, r.dims, n.dims, l, m, d, u, o, r.dataType, n.dataType, a, i), getRunData: () => ({ outputs: [{ dims: l, dataType: a }], dispatchGroup: { x: Math.ceil(c / 64 / 4) }, programUniforms: [{ type: 12, data: Math.ceil(k.size(l) / 4) }, ...R(r.dims, n.dims, l)] }) }; - }, st = (e, t, r, n, o, i) => { - e.compute(Bm(t, o ?? "", e.inputs[0], e.inputs[1], r, n, i)); - }, gu = (e) => { - st(e, "Add", (t, r) => `${t}+${r}`); - }, yu = (e) => { - st(e, "Div", (t, r) => `${t}/${r}`); - }, bu = (e) => { - st(e, "Equal", { scalar: (t, r) => `u32(${t}==${r})`, vector: (t, r) => `vec4(${t}==${r})` }, void 0, void 0, 9); - }, wu = (e) => { - st(e, "Mul", (t, r) => `${t}*${r}`); - }, _u = (e) => { - let t = E("input", e.inputs[0].dataType, e.inputs[0].dims).type.value; - st(e, "Pow", { scalar: (n, o) => `pow_custom(${n},${o})`, vector: (n, o) => `pow_vector_custom(${n},${o})` }, ` - fn pow_custom(a : ${t}, b : ${t}) -> ${t} { - if (b == ${t}(0.0)) { - return ${t}(1.0); - } else if (a < ${t}(0.0) && f32(b) != floor(f32(b))) { - return ${t}(pow(f32(a), f32(b))); // NaN - } - return select(sign(a), ${t}(1.0), round(f32(abs(b) % ${t}(2.0))) != 1.0) * ${t}(${t === "i32" ? "round" : ""}(pow(f32(abs(a)), f32(b)))); - } - fn pow_vector_custom(a : vec4<${t}>, b : vec4<${t}>) -> vec4<${t}> { - // TODO: implement vectorized pow - return vec4<${t}>(pow_custom(a.x, b.x), pow_custom(a.y, b.y), pow_custom(a.z, b.z), pow_custom(a.w, b.w)); - } - `); - }, vu = (e) => { - st(e, "Sub", (t, r) => `${t}-${r}`); - }, $u = (e) => { - st(e, "Greater", { scalar: (t, r) => `u32(${t}>${r})`, vector: (t, r) => `vec4(${t}>${r})` }, void 0, void 0, 9); - }, xu = (e) => { - st(e, "Less", { scalar: (t, r) => `u32(${t}<${r})`, vector: (t, r) => `vec4(${t}<${r})` }, void 0, void 0, 9); - }, Su = (e) => { - st(e, "GreaterOrEqual", { scalar: (t, r) => `u32(${t}>=${r})`, vector: (t, r) => `vec4(${t}>=${r})` }, void 0, void 0, 9); - }, Tu = (e) => { - st(e, "LessOrEqual", { scalar: (t, r) => `u32(${t}<=${r})`, vector: (t, r) => `vec4(${t}<=${r})` }, void 0, void 0, 9); - }; - }); - var Mm, Rm, Um, Vm, Cu, Au, ku = V(() => { - "use strict"; - Q(); - ie(); - Ie(); - ae(); - Mm = (e, t) => { - if (!e || e.length < 1) throw new Error("too few inputs"); - let r = 0, n = e[r], o = n.dataType, i = n.dims.length; - e.forEach((a, d) => { - if (d !== r) { - if (a.dataType !== o) throw new Error("input tensors should be one type"); - if (a.dims.length !== i) throw new Error("input tensors should have the same shape"); - a.dims.forEach((l, c) => { - if (c !== t && l !== n.dims[c]) throw new Error("non concat dimensions must match"); - }); - } - }); - }, Rm = (e, t) => ` - fn calculateInputIndex(index: u32) -> u32 { - let sizeInConcatAxis = array(${t}); - for (var i: u32 = 0u; i < ${e}; i += 1u ) { - if (index < sizeInConcatAxis[i]) { - return i; - } - } - return ${e}u; - }`, Um = (e, t) => { - let r = e.length, n = []; - for (let o = 0; o < r; ++o) { - let i = t.setByOffset("global_idx", e[o].getByIndices("indices")); - r === 1 ? n.push(i) : o === 0 ? n.push(`if (inputIndex == ${o}u) { ${i} }`) : o === r - 1 ? n.push(`else { ${i} }`) : n.push(`else if (inputIndex == ${o}) { ${i} }`); - } - return n.join(` - `); - }, Vm = (e, t, r, n) => { - let o = k.size(r), i = new Array(e.length), a = new Array(e.length), d = 0, l = [], c = [], m = [{ type: 12, data: o }]; - for (let y = 0; y < e.length; ++y) d += e[y].dims[t], i[y] = d, c.push(e[y].dims.length), a[y] = E(`input${y}`, n, c[y]), l.push("rank"), m.push({ type: 12, data: i[y] }); - for (let y = 0; y < e.length; ++y) m.push(...R(e[y].dims)); - m.push(...R(r)); - let u = M("output", n, r.length), h = u.indicesGet("indices", t), w = Array.from(Array(i.length).keys()).map((y) => `uniforms.sizeInConcatAxis${y}`).join(","), g = (y) => ` - - ${(() => { - y.registerUniform("outputSize", "u32"); - for (let S = 0; S < e.length; S++) y.registerUniform(`sizeInConcatAxis${S}`, "u32"); - return y.declareVariables(...a, u); - })()} - - ${Rm(i.length, w)} - - ${y.mainStart()} - ${y.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.outputSize")} - - var indices = ${u.offsetToIndices("global_idx")}; - - let inputIndex = calculateInputIndex(${h}); - if (inputIndex != 0u) { - let sizeInConcatAxis = array(${w}); - ${h} -= sizeInConcatAxis[inputIndex - 1u]; - } - - ${Um(a, u)} - }`; - return { name: "Concat", shaderCache: { hint: `${t}`, inputDependencies: l }, getRunData: () => ({ outputs: [{ dims: r, dataType: n }], dispatchGroup: { x: Math.ceil(o / 64) }, programUniforms: m }), getShaderSource: g }; - }, Cu = (e, t) => { - let r = e.inputs, n = r[0].dims, o = k.normalizeAxis(t.axis, n.length); - Mm(r, o); - let i = n.slice(); - i[o] = r.reduce((d, l) => d + (l.dims.length > o ? l.dims[o] : 0), 0); - let a = r.filter((d) => k.size(d.dims) > 0); - e.compute(Vm(a, o, i, r[0].dataType), { inputs: a }); - }, Au = (e) => J({ axis: e.axis }); - }); - var He, Ge, Fe, Zr, ct = V(() => { - "use strict"; - Q(); - ie(); - He = (e, t, r = "f32") => { - switch (e.activation) { - case "Relu": - return `value = max(value, ${t}(0.0));`; - case "Sigmoid": - return `value = (${t}(1.0) / (${t}(1.0) + exp(-value)));`; - case "Clip": - return `value = clamp(value, ${t}(${r}(uniforms.clip_min)), ${t}(${r}(uniforms.clip_max)));`; - case "HardSigmoid": - return `value = max(${t}(0.0), min(${t}(1.0), ${r}(uniforms.alpha) * value + ${r}(uniforms.beta)));`; - case "LeakyRelu": - return `value = select(${r}(uniforms.alpha) * value, value, value >= ${t}(0.0));`; - case "Tanh": - return `let e2x = exp(-2.0 * abs(value)); - value = sign(value) * (1.0 - e2x) / (1.0 + e2x); - `; - case "": - return ""; - default: - throw new Error(`Unsupported activation ${e.activation}`); - } - }, Ge = (e, t) => { - e.activation === "Clip" ? t.push({ type: 1, data: e.clipMax }, { type: 1, data: e.clipMin }) : e.activation === "HardSigmoid" ? t.push({ type: 1, data: e.alpha }, { type: 1, data: e.beta }) : e.activation === "LeakyRelu" && t.push({ type: 1, data: e.alpha }); - }, Fe = (e, t) => { - e.activation === "Clip" ? t.push({ name: "clip_max", type: "f32" }, { name: "clip_min", type: "f32" }) : e.activation === "HardSigmoid" ? t.push({ name: "alpha", type: "f32" }, { name: "beta", type: "f32" }) : e.activation === "LeakyRelu" && t.push({ name: "alpha", type: "f32" }); - }, Zr = (e) => { - let t = (e == null ? void 0 : e.activation) || ""; - if (t === "HardSigmoid") { - let [r, n] = (e == null ? void 0 : e.activation_params) || [0.2, 0.5]; - return { activation: t, alpha: r, beta: n }; - } else if (t === "Clip") { - let [r, n] = (e == null ? void 0 : e.activation_params) || [Ya, Xa]; - return { activation: t, clipMax: n, clipMin: r }; - } else if (t === "LeakyRelu") { - let [r] = (e == null ? void 0 : e.activation_params) || [0.01]; - return { activation: t, alpha: r }; - } - return { activation: t }; - }; - }); - var Oe, Qr, Jt = V(() => { - "use strict"; - Oe = (e, t) => { - switch (e) { - case 1: - return t; - case 2: - return `vec2<${t}>`; - case 3: - return `vec3<${t}>`; - case 4: - return `vec4<${t}>`; - default: - throw new Error(`${e}-component is not supported.`); - } - }, Qr = (e) => ` - ${e ? "value = value + getBiasByOutputCoords(coords);" : ""} - `; - }); - var Jr, po = V(() => { - "use strict"; - Jr = (e) => ` - fn getIndexFromCoords4D(coords : vec4, shape : vec4) -> i32 { - return dot(coords, vec4( - shape.y * shape.z * shape.w, shape.z * shape.w, shape.w, 1)); - } - fn getOutputIndexFromCoords(coords : vec4) -> i32 { - return dot(coords, vec4( - i32(${e}.x), i32(${e}.y), i32(${e}.z), 1)); - } - `; - }); - var Nm, Wm, er, Eu, Lm, tr, Hm, en, rr = V(() => { - "use strict"; - Q(); - ie(); - ae(); - ct(); - Jt(); - Nm = (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" : ""}); - `, Wm = (e, t) => e ? ` - let ACached0 = mm_Asub[k * innerElementSize][localRow]; - let ACached1 = mm_Asub[k * innerElementSize + 1][localRow]; - let ACached2 = mm_Asub[k * innerElementSize + 2][localRow]; - ${t === 3 ? "" : "let ACached3 = mm_Asub[k * innerElementSize + 3][localRow];"} - for (var i = 0; i < rowPerThread; i = i + 1) { - acc[i] = BCached0 * ACached0[i] + acc[i]; - acc[i] = BCached1 * ACached1[i] + acc[i]; - acc[i] = BCached2 * ACached2[i] + acc[i]; - ${t === 3 ? "" : "acc[i] = BCached3 * ACached3[i] + acc[i];"} - }` : ` - for (var i = 0; i < rowPerThread; i = i + 1) { - let ACached = mm_Asub[tileRow + i][k]; - acc[i] = BCached0 * ACached.x + acc[i]; - acc[i] = BCached1 * ACached.y + acc[i]; - acc[i] = BCached2 * ACached.z + acc[i]; - ${t === 3 ? "" : "acc[i] = BCached3 * ACached.w + acc[i];"} - }`, er = (e, t, r = "f32", n, o = false, i = 32, a = false, d = 32) => { - let l = t[1] * e[1], c = t[0] * e[0], m = o ? l : i, u = o ? i : l, h = m / t[0], w = i / t[1]; - if (!((o && h === 4 && e[1] === 4 || !o && (h === 3 || h === 4)) && m % t[0] === 0 && i % t[1] === 0 && e[0] === 4)) throw new Error(`If transposeA ${o} is true, innerElementSize ${h} and workPerThread[1] ${e[1]} must be 4. - Otherwise, innerElementSize ${h} must be 3 or 4. - tileAWidth ${m} must be divisible by workgroupSize[0]${t[0]}. tileInner ${i} must be divisible by workgroupSize[1] ${t[1]}. colPerThread ${e[0]} must be 4.`); - return ` - var mm_Asub: array, ${m / h}>, ${u}>; - var mm_Bsub: array, ${c / e[0]}>, ${i}>; - - const rowPerThread = ${e[1]}; - const colPerThread = ${e[0]}; - const innerElementSize = ${h}; - const tileInner = ${i}; - - @compute @workgroup_size(${t[0]}, ${t[1]}, ${t[2]}) - fn main(@builtin(local_invocation_id) localId : vec3, - @builtin(global_invocation_id) globalId : vec3, - @builtin(workgroup_id) workgroupId : vec3) { - let localRow = i32(localId.y); - let tileRow = localRow * rowPerThread; - let tileCol = i32(localId.x); - - let globalRow =i32(globalId.y) * rowPerThread; - let globalCol = i32(globalId.x); - let batch = ${a ? "0" : "i32(globalId.z)"}; - ${n ? `let batchIndices = ${n.offsetToIndices("u32(batch)")};` : ""} - let globalRowStart = i32(workgroupId.y) * ${l}; - - let num_tiles = ${a ? `${Math.ceil(d / i)}` : "(uniforms.dim_inner - 1) / tileInner + 1"}; - var kStart = ${a ? `i32(globalId.z) * ${d}` : "0"}; - - var acc: array, rowPerThread>; - - // Loop over shared dimension. - let tileRowB = localRow * ${w}; - for (var t = 0; t < num_tiles; t = t + 1) { - // Load one tile of A into local memory. - for (var innerRow = 0; innerRow < rowPerThread; innerRow = innerRow + 1) { - let inputRow = tileRow + innerRow; - let inputCol = tileCol; - ${Nm(o, n)} - } - - // Load one tile of B into local memory. - for (var innerRow = 0; innerRow < ${w}; innerRow = innerRow + 1) { - let inputRow = tileRowB + innerRow; - let inputCol = tileCol; - mm_Bsub[inputRow][inputCol] = mm_readB(batch, kStart + inputRow, globalCol${n ? ", batchIndices" : ""}); - } - kStart = kStart + tileInner; - workgroupBarrier(); - - // Compute acc values for a single thread. - for (var k = 0; k < tileInner / innerElementSize; k = k + 1) { - let BCached0 = mm_Bsub[k * innerElementSize][tileCol]; - let BCached1 = mm_Bsub[k * innerElementSize + 1][tileCol]; - let BCached2 = mm_Bsub[k * innerElementSize + 2][tileCol]; - ${h === 3 ? "" : "let BCached3 = mm_Bsub[k * innerElementSize + 3][tileCol];"} - - ${Wm(o, h)} - } - - workgroupBarrier(); - } - - for (var innerRow = 0; innerRow < rowPerThread; innerRow = innerRow + 1) { - mm_write(batch, globalRow + innerRow, globalCol, acc[innerRow]); - } - }`; - }, Eu = (e, t) => e ? ` - mm_Asub[inputRow][inputCol] = mm_readA(batch, - kStart + inputRow, - globalRowStart + inputCol${t ? ", batchIndices" : ""}); - ` : ` - mm_Asub[inputRow][inputCol] = mm_readA(batch, - globalRowStart + inputRow, - kStart + inputCol${t ? ", batchIndices" : ""}); - `, Lm = (e) => e ? "let ACached = mm_Asub[k][tileRow + innerRow];" : "let ACached = mm_Asub[tileRow + innerRow][k];", tr = (e, t, r = "f32", n, o = false, i = 32, a = false, d = 32, l = false) => { - let c = e[1] * t[1], m = e[0] * t[0], u = o ? c : i, h = o ? i : c; - if (!(h % t[1] === 0 && u % t[0] === 0 && i % t[1] === 0)) throw new Error(`tileAHight ${h} must be divisible by workgroupSize[1]${t[1]}, tileAWidth ${u} must be divisible by workgroupSize[0]${t[0]}, tileInner ${i} must be divisible by workgroupSize[1]${t[1]}`); - let w = h / t[1], g = u / t[0], y = i / t[1], S = l ? ` - let localRow = i32(localId.y); - let localCol = i32(localId.x); - let globalRowStart = i32(workgroupId.y) * ${c}; - let globalColStart = i32(workgroupId.x) * ${m}; - - // Loop over shared dimension. - for (var t = 0; t < num_tiles; t = t + 1) { - // Load one tile of A into local memory. - for (var inputRow = localRow; inputRow < ${h}; inputRow = inputRow + ${t[1]}) { - for (var inputCol = localCol; inputCol < ${u}; inputCol = inputCol + ${t[0]}) { - ${Eu(o, n)} - } - } - // Load one tile of B into local memory. - for (var inputRow = localRow; inputRow < ${i}; inputRow = inputRow + ${t[1]}) { - for (var inputCol = localCol; inputCol < ${m}; inputCol = inputCol + ${t[0]}) { - mm_Bsub[inputRow][inputCol] = mm_readB(batch, - kStart + inputRow, - globalColStart + inputCol${n ? ", batchIndices" : ""}); - } - } - kStart = kStart + tileInner; - workgroupBarrier(); - - // Compute acc values for a single thread. - var BCached : array<${r}, colPerThread>; - for (var k = 0; k < tileInner; k = k + 1) { - for (var inner = 0; inner < colPerThread; inner = inner + 1) { - BCached[inner] = mm_Bsub[k][localCol + inner * ${t[0]}]; - } - for (var innerRow = 0; innerRow < rowPerThread; innerRow = innerRow + 1) { - let ACached = ${o ? `mm_Asub[k][localRow + innerRow * ${t[1]}];` : `mm_Asub[localRow + innerRow * ${t[1]}][k];`} - for (var innerCol = 0; innerCol < colPerThread; innerCol = innerCol + 1) { - acc[innerRow][innerCol] = acc[innerRow][innerCol] + - ACached * BCached[innerCol]; - } - } - } - workgroupBarrier(); - } - for (var innerRow = 0; innerRow < rowPerThread; innerRow = innerRow + 1) { - let gRow = globalRowStart + localRow + innerRow * ${t[1]}; - for (var innerCol = 0; innerCol < colPerThread; innerCol = innerCol + 1) { - let gCol = globalColStart + localCol + innerCol * ${t[0]}; - mm_write(batch, gRow, gCol, acc[innerRow][innerCol]); - } - } - ` : ` - let tileRow = i32(localId.y) * rowPerThread; - let tileCol = i32(localId.x) * colPerThread; - - let globalRow = i32(globalId.y) * rowPerThread; - let globalCol = i32(globalId.x) * colPerThread; - let globalRowStart = i32(workgroupId.y) * ${c}; - - let tileRowA = i32(localId.y) * ${w}; - let tileColA = i32(localId.x) * ${g}; - let tileRowB = i32(localId.y) * ${y}; - // Loop over shared dimension. - for (var t = 0; t < num_tiles; t = t + 1) { - // Load one tile of A into local memory. - for (var innerRow = 0; innerRow < ${w}; innerRow = innerRow + 1) { - for (var innerCol = 0; innerCol < ${g}; innerCol = innerCol + 1) { - let inputRow = tileRowA + innerRow; - let inputCol = tileColA + innerCol; - ${Eu(o, n)} - } - } - - // Load one tile of B into local memory. - for (var innerRow = 0; innerRow < ${y}; innerRow = innerRow + 1) { - for (var innerCol = 0; innerCol < colPerThread; innerCol = innerCol + 1) { - let inputRow = tileRowB + innerRow; - let inputCol = tileCol + innerCol; - mm_Bsub[inputRow][inputCol] = mm_readB(batch, - kStart + inputRow, - globalCol + innerCol${n ? ", batchIndices" : ""}); - } - } - kStart = kStart + tileInner; - workgroupBarrier(); - - // Compute acc values for a single thread. - var BCached : array<${r}, colPerThread>; - for (var k = 0; k < tileInner; k = k + 1) { - for (var inner = 0; inner < colPerThread; inner = inner + 1) { - BCached[inner] = mm_Bsub[k][tileCol + inner]; - } - - for (var innerRow = 0; innerRow < rowPerThread; innerRow = innerRow + 1) { - ${Lm(o)} - for (var innerCol = 0; innerCol < colPerThread; innerCol = innerCol + 1) { - acc[innerRow][innerCol] = acc[innerRow][innerCol] + ACached * BCached[innerCol]; - } - } - } - - workgroupBarrier(); - } - - for (var innerRow = 0; innerRow < rowPerThread; innerRow = innerRow + 1) { - for (var innerCol = 0; innerCol < colPerThread; innerCol = innerCol + 1) { - mm_write(batch, globalRow + innerRow, globalCol + innerCol, - acc[innerRow][innerCol]); - } - } - `; - return ` - var mm_Asub : array, ${h}>; - var mm_Bsub : array, ${i}>; - const rowPerThread = ${e[1]}; - const colPerThread = ${e[0]}; - const tileInner = ${i}; - - @compute @workgroup_size(${t[0]}, ${t[1]}, ${t[2]}) - fn main(@builtin(local_invocation_id) localId : vec3, - @builtin(global_invocation_id) globalId : vec3, - @builtin(workgroup_id) workgroupId : vec3) { - let batch = ${a ? "0" : "i32(globalId.z)"}; - ${n ? `let batchIndices = ${n.offsetToIndices("u32(batch)")};` : ""} - let num_tiles = ${a ? `${Math.ceil(d / i)}` : "(uniforms.dim_inner - 1) / tileInner + 1"}; - var kStart = ${a ? `i32(globalId.z) * ${d}` : "0"}; - - var acc : array, rowPerThread>; - ${S} - } - `; - }, Hm = (e, t, r, n, o, i = false) => { - let [a, d, l] = o, [c, m, u, h] = n, w = Nt(a, l), g = Nt(d, l), y = he(n[0].type.tensor), S = () => { - let x = m.rank, T = c.rank, C = `var aIndices: ${m.type.indices};`; - for (let A = x - 2 - 1, P = T - 1; A >= 0; A--, P--) C += ` - aIndices[${A}] = ${T > 1 ? `batchIndices[${P}]` : "batchIndices"};`; - return w.forEach((A) => { - C += ` - aIndices[${A}] = 0;`; - }), C += ` - aIndices[${x - 2}] = u32(row); - aIndices[${x - 1}] = u32(colIn);`, C; - }, $ = () => { - let x = u.rank, T = c.rank, C = `var bIndices: ${u.type.indices};`; - for (let A = x - 2 - 1, P = T - 1; A >= 0; A--, P--) C += ` - bIndices[${A}] = ${T > 1 ? `batchIndices[${P}]` : "batchIndices"};`; - return g.forEach((A) => { - C += ` - bIndices[${A}] = 0;`; - }), C += ` - bIndices[${x - 2}] = u32(row); - bIndices[${x - 1}] = u32(colIn);`, C; - }; - return ` - fn mm_readA(batch: i32, row: i32, colIn: i32, batchIndices: ${c.type.indices}) -> ${Oe(e, y)} { - var value = ${Oe(e, y)}(0.0); - let col = colIn * ${e}; - if(row < uniforms.dim_a_outer && col < uniforms.dim_inner) - { - ${S()} - value = ${m.getByIndices("aIndices")}; - } - return value; - } - - fn mm_readB(batch: i32, row: i32, colIn: i32, batchIndices: ${c.type.indices}) -> ${Oe(e, y)} { - var value = ${Oe(e, y)}(0.0); - let col = colIn * ${e}; - if(row < uniforms.dim_inner && col < uniforms.dim_b_outer) - { - ${$()} - value = ${u.getByIndices("bIndices")}; - } - return value; - } - - fn mm_write(batch: i32, row: i32, colIn: i32, valueIn: ${Oe(e, y)}) { - let col = colIn * ${e}; - if (row < uniforms.dim_a_outer && col < uniforms.dim_b_outer) { - var value = valueIn; - let coords = vec3(batch, row, colIn); - ${t ? `value = value + ${i ? "bias[colIn]" : `${Oe(e, y)}(bias[row])`};` : ""} - ${r} - ${h.setByIndices("vec3(coords)", "value")} - } - } - `; - }, en = (e, t, r, n, o = false, i) => { - let a = e[0].dims, d = e[1].dims, l = a.slice(0, -2), c = d.slice(0, -2), m = n ? n.slice(0, -2) : r.slice(0, -2), u = k.size(m), h = a[a.length - 2], w = a[a.length - 1], g = d[d.length - 1], y = w % 4 === 0 && g % 4 === 0, S = h <= 8 ? [4, 1, 1] : [4, 4, 1], $ = [8, 8, 1], v = [Math.ceil(g / $[0] / S[0]), Math.ceil(h / $[1] / S[1]), Math.ceil(u / $[2] / S[2])], x = y ? 4 : 1, T = [...l, h, w / x], C = T.length, A = [...c, w, g / x], P = A.length, B = [u, h, g / x], N = [{ type: 6, data: h }, { type: 6, data: g }, { type: 6, data: w }]; - Ge(t, N), N.push(...R(m, T, A)); - let W = ["rank", "rank"], K = e.length > 2; - K && (N.push(...R(e[2].dims)), W.push("rank")), N.push(...R(B)); - let Z = (ee) => { - let se = m.length, de = Fr("batchDims", e[0].dataType, se, 1), Y = he(e[0].dataType), le = E("a", e[0].dataType, C, x), ce = E("b", e[1].dataType, P, x), q = M("result", e[0].dataType, B.length, x), ue = [le, ce]; - if (K) { - let G = o ? x : 1; - ue.push(E("bias", e[2].dataType, e[2].dims.length, G)); - } - let re = [{ name: "dim_a_outer", type: "i32" }, { name: "dim_b_outer", type: "i32" }, { name: "dim_inner", type: "i32" }]; - Fe(t, re); - let ne = he(q.type.tensor), oe = He(t, q.type.value, ne), U = Hm(x, K, oe, [de, le, ce, q], [l, c, m], o); - return ` - ${ee.registerUniforms(re).registerInternalVariables(de).declareVariables(...ue, q)} - ${U} - ${y ? er(S, $, Y, de) : tr(S, $, Y, de)} - `; - }; - return { name: "MatMul", shaderCache: { hint: `${S};${t.activation};${y};${o}`, inputDependencies: W }, getRunData: () => ({ outputs: [{ dims: i ? i(r) : r, dataType: e[0].dataType }], dispatchGroup: { x: v[0], y: v[1], z: v[2] }, programUniforms: N }), getShaderSource: Z }; - }; - }); - var Gm, Pu, zu = V(() => { - "use strict"; - Q(); - Xe(); - ae(); - ct(); - Jt(); - po(); - rr(); - Gm = (e, t, r, n, o = false, i, a = 4, d = 4, l = 4, c = "f32") => { - let m = (W) => { - switch (W) { - case 1: - return "resData = x[xIndex];"; - case 3: - return `resData = vec3<${c}>(x[xIndex], x[xIndex + 1], x[xIndex + 2]);`; - case 4: - return "resData = x[xIndex / 4];"; - default: - throw new Error(`innerElementSize ${W} is not supported.`); - } - }, u = (W) => { - switch (W) { - 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 ${W} is not supported.`); - } - }, h = e ? ` - let coord = vec4(batch, xRow, xCol, xCh); - ` : ` - let coord = vec4(batch, xCh, xRow, xCol); - `, w = e ? ` - let coords = vec4( - batch, - row / outWidth, - row % outWidth, - col); - ` : ` - let coords = vec4( - batch, - row, - col / outWidth, - col % outWidth); - `, g = e ? "i32(uniforms.x_shape[1])" : "i32(uniforms.x_shape[2])", y = e ? "i32(uniforms.x_shape[2])" : "i32(uniforms.x_shape[3])", S = e ? "row" : "col", $ = e ? "col" : "row", v = ` - let inChannels = i32(uniforms.w_shape[2]); - let outWidth = ${e ? "i32(uniforms.result_shape[2])" : "i32(uniforms.result_shape[3])"}; - let outRow = ${S} / outWidth; - let outCol = ${S} % outWidth; - - let WRow = ${$} / (i32(uniforms.w_shape[1]) * inChannels); - let WCol = ${$} / inChannels % i32(uniforms.w_shape[1]); - let xRow = outRow * uniforms.stride[0] + uniforms.dilation[0] * WRow - uniforms.pad[0]; - let xCol = outCol * uniforms.stride[1] + uniforms.dilation[1] * WCol - uniforms.pad[1]; - let xCh = ${$} % inChannels; - var resData = ${Oe(a, c)}(0.0); - // The bounds checking is always needed since we use it to pad zero for - // the 'same' padding type. - if (xRow >= 0 && xRow < ${g} && xCol >= 0 && xCol < ${y}) { - ${h} - let xIndex = getIndexFromCoords4D(coord, vec4(uniforms.x_shape)); - ${m(a)} - } - return resData;`, x = e ? t && n ? ` - let col = colIn * ${a}; - ${v}` : ` - let col = colIn * ${a}; - if (row < uniforms.dim_a_outer && col < uniforms.dim_inner) { - ${v} - } - return ${Oe(a, c)}(0.0);` : n && r ? ` - let col = colIn * ${a}; - ${v}` : ` - let col = colIn * ${a}; - if (row < uniforms.dim_inner && col < uniforms.dim_b_outer) { - ${v} - } - return ${Oe(a, c)}(0.0);`, T = `${u(d)}`, C = Oe(l, c), A = e ? Oe(a, c) : Oe(d, c), P = e ? Oe(d, c) : Oe(a, c), B = He(i, C, c); - return ` - fn mm_readA(batch: i32, row : i32, colIn : i32) -> ${A} { - ${e ? x : T} - } - - fn mm_readB(batch: i32, row : i32, colIn : i32) -> ${P} { - ${e ? T : x} - } - - fn mm_write(batch: i32, row : i32, colIn : i32, valueIn : ${C}) { - let col = colIn * ${l}; - if (row < uniforms.dim_a_outer && col < uniforms.dim_b_outer) - { - var value = valueIn; - let outWidth = ${e ? "i32(uniforms.result_shape[2])" : "i32(uniforms.result_shape[3])"}; - ${w} - ${Qr(o)} - ${B} - setOutputAtCoords(coords[0], coords[1], coords[2], coords[3], value); - } - }`; - }, Pu = (e, t, r, n, o, i, a, d, l) => { - let c = t.format === "NHWC", m = c ? e[0].dims[3] : e[0].dims[1], u = r[0], h = c ? r[2] : r[3], w = c ? r[1] : r[2], g = c ? r[3] : r[1], y = c && (m % 4 === 0 || m % 3 === 0) && g % 4 === 0, S = c ? g : h * w, $ = c ? h * w : g, v = [8, 8, 1], x = n <= 8 ? [4, 1, 1] : [4, 4, 1], T = [Math.ceil(S / v[0] / x[0]), Math.ceil($ / v[1] / x[1]), Math.ceil(u / v[2] / x[2])]; - me("verbose", () => `[conv2d_mm_webgpu] dispatch = ${T}`); - let C = y ? c && m % 4 !== 0 ? 3 : 4 : 1, A = v[1] * x[1], P = v[0] * x[0], B = Math.max(v[0] * C, v[1]), N = n % A === 0, W = o % P === 0, K = i % B === 0, Z = y ? [C, 4, 4] : [1, 1, 1], ee = [{ type: 6, data: n }, { type: 6, data: o }, { type: 6, data: i }, { type: 6, data: [t.pads[0], t.pads[1]] }, { type: 6, data: t.strides }, { type: 6, data: t.dilations }]; - Ge(t, ee), ee.push(...R(e[0].dims, e[1].dims)); - let se = ["rank", "rank"]; - a && (ee.push(...R(e[2].dims)), se.push("rank")), ee.push(...R(r)); - let de = (Y) => { - let le = [{ name: "dim_a_outer", type: "i32" }, { name: "dim_b_outer", type: "i32" }, { name: "dim_inner", type: "i32" }, { name: "pad", type: "i32", length: 2 }, { name: "stride", type: "i32", length: 2 }, { name: "dilation", type: "i32", length: 2 }]; - Fe(t, le); - let ce = y ? 4 : 1, q = he(e[0].dataType), ue = ` - fn setOutputAtIndex(flatIndex : i32, value : ${y ? `vec4<${q}>` : q}) { - result[flatIndex] = ${y ? `vec4<${q}>` : q}(value); - } - fn setOutputAtCoords(d0 : i32, d1 : i32, d2 : i32, d3 : i32, value : ${y ? `vec4<${q}>` : q}) { - let flatIndex = getOutputIndexFromCoords(vec4(d0, d1, d2, d3)); - setOutputAtIndex(flatIndex ${y ? "/ 4" : ""}, value); - }`, re = E("x", e[0].dataType, e[0].dims.length, C === 3 ? 1 : C), ne = E("w", e[1].dataType, e[1].dims.length, ce), oe = [re, ne], U = M("result", e[0].dataType, r.length, ce); - if (a) { - let G = E("bias", e[2].dataType, e[2].dims.length, ce); - oe.push(G), ue += ` - fn getBiasByOutputCoords(coords : vec4) -> ${y ? `vec4<${q}>` : q} { - return bias[coords.${c ? "w" : "y"}${y ? "/ 4" : ""}]; - }`; - } - return ` - ${Jr("uniforms.result_strides")} - //struct Uniforms { xShape : vec4, wShape : vec4, outShape : vec4, - // outShapeStrides: vec3, filterDims : vec2, pad : vec2, stride : vec2, - // dilation : vec2, dimAOuter : i32, dimBOuter : i32, dimInner : i32 }; - ${Y.registerUniforms(le).declareVariables(...oe, U)} - ${ue} - ${Gm(c, N, W, K, a, t, Z[0], Z[1], Z[2], q)} - ${y ? er(x, v, q, void 0, !c, B) : tr(x, v, q, void 0, !c, B, false, void 0, d)}`; - }; - return { name: "Conv2DMatMul", shaderCache: { hint: `${t.cacheKey};${C};${y};${N};${W};${K};${A};${P};${B}`, inputDependencies: se }, getRunData: () => ({ outputs: [{ dims: l ? l(r) : r, dataType: e[0].dataType }], dispatchGroup: { x: T[0], y: T[1], z: T[2] }, programUniforms: ee }), getShaderSource: de }; - }; - }); - var Fm, Ou, tn, qm, Bu, jm, Du, Mu, Ru = V(() => { - "use strict"; - Q(); - Xe(); - ie(); - ae(); - ct(); - Jt(); - Fm = (e) => { - let t = 1; - for (let r = 0; r < e.length; r++) t *= e[r]; - return t; - }, Ou = (e) => typeof e == "number" ? [e, e, e] : e, tn = (e, t) => t <= 1 ? e : e + (e - 1) * (t - 1), qm = (e, t, r, n = 1) => { - let o = tn(t, n); - return Math.floor((e[0] * (r - 1) - r + o) / 2); - }, Bu = (e, t, r, n, o) => { - o == null && (o = qm(e, t[0], n[0])); - let i = [0, 0, 0, r]; - for (let a = 0; a < 3; a++) e[a] + 2 * o >= t[a] && (i[a] = Math.trunc((e[a] - t[a] + 2 * o) / n[a] + 1)); - return i; - }, jm = (e, t, r, n, o, i, a, d, l, c) => { - let m, u, h, w; - if (e === "VALID" && (e = 0), typeof e == "number") { - m = { top: e, bottom: e, left: e, right: e, front: e, back: e }; - let g = Bu([t, r, n, 1], [d, l, c], 1, [o, i, a], e); - u = g[0], h = g[1], w = g[2]; - } else if (Array.isArray(e)) { - if (!e.every((y, S, $) => y === $[0])) throw Error(`Unsupported padding parameter: ${e}`); - m = { top: e[0], bottom: e[1], left: e[2], right: e[3], front: e[4], back: e[5] }; - let g = Bu([t, r, n, 1], [d, l, c], 1, [o, i, a], e[0]); - u = g[0], h = g[1], w = g[2]; - } else if (e === "SAME_UPPER") { - u = Math.ceil(t / o), h = Math.ceil(r / i), w = Math.ceil(n / a); - let g = (u - 1) * o + d - t, y = (h - 1) * i + l - r, S = (w - 1) * a + c - n, $ = Math.floor(g / 2), v = g - $, x = Math.floor(y / 2), T = y - x, C = Math.floor(S / 2), A = S - C; - m = { top: x, bottom: T, left: C, right: A, front: $, back: v }; - } else throw Error(`Unknown padding parameter: ${e}`); - return { padInfo: m, outDepth: u, outHeight: h, outWidth: w }; - }, Du = (e, t, r, n, o, i = false, a = "channelsLast") => { - let d, l, c, m, u; - if (a === "channelsLast") [d, l, c, m, u] = e; - else if (a === "channelsFirst") [d, u, l, c, m] = e; - else throw new Error(`Unknown dataFormat ${a}`); - let [h, , w, g, y] = t, [S, $, v] = Ou(r), [x, T, C] = Ou(n), A = tn(w, x), P = tn(g, T), B = tn(y, C), { padInfo: N, outDepth: W, outHeight: K, outWidth: Z } = jm(o, l, c, m, S, $, v, A, P, B), ee = i ? h * u : h, se = [0, 0, 0, 0, 0]; - return a === "channelsFirst" ? se = [d, ee, W, K, Z] : a === "channelsLast" && (se = [d, W, K, Z, ee]), { batchSize: d, dataFormat: a, inDepth: l, inHeight: c, inWidth: m, inChannels: u, outDepth: W, outHeight: K, outWidth: Z, outChannels: ee, padInfo: N, strideDepth: S, strideHeight: $, strideWidth: v, filterDepth: w, filterHeight: g, filterWidth: y, effectiveFilterDepth: A, effectiveFilterHeight: P, effectiveFilterWidth: B, dilationDepth: x, dilationHeight: T, dilationWidth: C, inShape: e, outShape: se, filterShape: t }; - }, Mu = (e, t, r, n, o, i) => { - let a = i === "channelsLast", d = a ? e[0].dims[3] : e[0].dims[1], l = false, c = [64, 1, 1], m = { x: r.map((v, x) => x) }, u = [Math.ceil(Fm(m.x.map((v) => r[v])) / c[0]), 1, 1]; - me("verbose", () => `[conv3d_naive_webgpu] dispatch = ${u}`); - let h = l ? a && d % 4 !== 0 ? 3 : 4 : 1, w = k.size(r), g = [{ type: 12, data: w }, { type: 12, data: n }, { type: 12, data: o }, { type: 12, data: t.strides }, { type: 12, data: t.dilations }]; - Ge(t, g), g.push(...R(e[0].dims, e[1].dims)); - let y = ["rank", "rank"], S = e.length === 3; - S && (g.push(...R(e[2].dims)), y.push("rank")), g.push(...R(r)); - let $ = (v) => { - let x = [{ name: "output_size", type: "u32" }, { name: "filter_dims", type: "u32", length: n.length }, { name: "pads", type: "u32", length: o.length }, { name: "strides", type: "u32", length: t.strides.length }, { name: "dilations", type: "u32", length: t.dilations.length }]; - Fe(t, x); - let T = l ? 4 : 1, C = he(e[0].dataType), A = E("x", e[0].dataType, e[0].dims.length, h === 3 ? 1 : h), P = E("W", e[1].dataType, e[1].dims.length, T), B = [A, P], N = M("result", e[0].dataType, r.length, T), W = ""; - if (S) { - let ee = E("bias", e[2].dataType, e[2].dims.length, T); - B.push(ee), W += ` - fn getBiasByOutputCoords(coords : array) -> ${l ? `vec4<${C}>` : C} { - return bias[${a ? F("coords", 4, 5) : F("coords", 1, 5)}${l ? "/ 4" : ""}]; - }`; - } - let K = Oe(h, C), Z = He(t, K, C); - return ` - ${W} - fn getX(d0 : u32, d1 : u32, d2 : u32, d3 : u32, d4 : u32) -> f32 { - let aIndices = array(d0, d1, d2, d3, d4); - return ${A.getByIndices("aIndices")}; - } - fn getW(d0 : u32, d1 : u32, d2 : u32, d3 : u32, d4 : u32) -> f32 { - let aIndices = array(d0, d1, d2, d3, d4); - return ${P.getByIndices("aIndices")}; - } - ${v.registerUniforms(x).declareVariables(...B, N)} - ${v.mainStart()} - ${v.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} - let coords = ${N.offsetToIndices("global_idx")}; - let batch = ${F("coords", 0, A.rank)}; - let d2 = ${a ? F("coords", A.rank - 1, A.rank) : F("coords", 1, A.rank)}; - let xFRCCorner = vec3(${a ? F("coords", 1, A.rank) : F("coords", 2, A.rank)}, - ${a ? F("coords", 2, A.rank) : F("coords", 3, A.rank)}, - ${a ? F("coords", 3, A.rank) : F("coords", 4, A.rank)}) * uniforms.strides - uniforms.pads; - let xFCorner = xFRCCorner.x; - let xRCorner = xFRCCorner.y; - let xCCorner = xFRCCorner.z; - let xShapeY = ${a ? F("uniforms.x_shape", 1, A.rank) : F("uniforms.x_shape", 2, A.rank)}; - let xShapeZ = ${a ? F("uniforms.x_shape", 2, A.rank) : F("uniforms.x_shape", 3, A.rank)}; - let xShapeW = ${a ? F("uniforms.x_shape", 3, A.rank) : F("uniforms.x_shape", 4, A.rank)}; - let xShapeU = ${a ? F("uniforms.x_shape", 4, A.rank) : F("uniforms.x_shape", 1, A.rank)}; - let inputDepthNearestVec4 = (xShapeU / 4) * 4; - let inputDepthVec4Remainder = xShapeU % 4; - - var value = 0.0; - for (var wF = 0u; wF < uniforms.filter_dims[0]; wF++) { - let xF = xFCorner + wF * uniforms.dilations[0]; - if (xF < 0 || xF >= xShapeY) { - continue; - } - - for (var wR = 0u; wR < uniforms.filter_dims[1]; wR++) { - let xR = xRCorner + wR * uniforms.dilations[1]; - if (xR < 0 || xR >= xShapeZ) { - continue; - } - - for (var wC = 0u; wC < uniforms.filter_dims[2]; wC++) { - let xC = xCCorner + wC * uniforms.dilations[2]; - if (xC < 0 || xC >= xShapeW) { - continue; - } - - for (var d1 = 0u; d1 < inputDepthNearestVec4; d1 += 4) { - ${a ? `let xValues = vec4( - getX(batch, xF, xR, xC, d1), - getX(batch, xF, xR, xC, d1 + 1), - getX(batch, xF, xR, xC, d1 + 2), - getX(batch, xF, xR, xC, d1 + 3)); - ` : `let xValues = vec4( - getX(batch, d1, xF, xR, xC), - getX(batch, d1 + 1, xF, xR, xC), - getX(batch, d1 + 2, xF, xR, xC), - getX(batch, d1 + 3, xF, xR, xC)); - `} - let wValues = vec4( - getW(d2, d1, wF, wR, wC), - getW(d2, d1 + 1, wF, wR, wC), - getW(d2, d1 + 2, wF, wR, wC), - getW(d2, d1 + 3, wF, wR, wC)); - value += dot(xValues, wValues); - } - if (inputDepthVec4Remainder == 1) { - ${a ? `value += getX(batch, xF, xR, xC, inputDepthNearestVec4) - * getW(d2, inputDepthNearestVec4, wF, wR, wC);` : `value += getX(batch, inputDepthNearestVec4, xF, xR, xC) - * getW(d2, inputDepthNearestVec4, wF, wR, wC);`} - } else if (inputDepthVec4Remainder == 2) { - ${a ? `let xValues = vec2( - getX(batch, xF, xR, xC, inputDepthNearestVec4), - getX(batch, xF, xR, xC, inputDepthNearestVec4 + 1)); - ` : `let xValues = vec2( - getX(batch, inputDepthNearestVec4, xF, xR, xC), - getX(batch, inputDepthNearestVec4 + 1, xF, xR, xC)); - `} - let wValues = vec2( - getW(d2, inputDepthNearestVec4, wF, wR, wC), - getW(d2, inputDepthNearestVec4 + 1, wF, wR, wC)); - value += dot(xValues, wValues); - } else if (inputDepthVec4Remainder == 3) { - ${a ? `let xValues = vec3( - getX(batch, xF, xR, xC, inputDepthNearestVec4), - getX(batch, xF, xR, xC, inputDepthNearestVec4 + 1), - getX(batch, xF, xR, xC, inputDepthNearestVec4 + 2)); - ` : `let xValues = vec3( - getX(batch, inputDepthNearestVec4, xF, xR, xC), - getX(batch, inputDepthNearestVec4 + 1, xF, xR, xC), - getX(batch, inputDepthNearestVec4 + 2, xF, xR, xC)); - `} - let wValues = vec3( - getW(d2, inputDepthNearestVec4, wF, wR, wC), - getW(d2, inputDepthNearestVec4 + 1, wF, wR, wC), - getW(d2, inputDepthNearestVec4 + 2, wF, wR, wC)); - value += dot(xValues, wValues); - } - } - } - } - ${S ? "value = value + getBiasByOutputCoords(coords)" : ""}; - ${Z} - result[global_idx] = f32(value); - }`; - }; - return { name: "Conv3DNaive", shaderCache: { hint: `${t.cacheKey};${a};${h};${S}`, inputDependencies: y }, getRunData: () => ({ outputs: [{ dims: r, dataType: e[0].dataType }], dispatchGroup: { x: u[0], y: u[1], z: u[2] }, programUniforms: g }), getShaderSource: $ }; - }; - }); - var Uu, Vu, Nu = V(() => { - "use strict"; - Q(); - ie(); - ae(); - ct(); - Uu = (e, t, r, n) => { - let o = e.length > 2, i = o ? "value += b[output_channel];" : "", a = e[0].dims, d = e[1].dims, l = t.format === "NHWC", c = l ? r[3] : r[1], m = c / t.group, u = l && m >= 4 ? we(c) : 1, h = k.size(r) / u, w = [{ type: 12, data: h }, { type: 12, data: t.dilations }, { type: 12, data: [t.strides[0], t.strides[1]] }, { type: 12, data: [t.pads[0], t.pads[1]] }, { type: 12, data: m }]; - Ge(t, w), w.push(...R(a, [d[0], d[1], d[2], d[3] / u])); - let g = o ? ["rank", "rank", "rank"] : ["rank", "rank"]; - w.push(...R([r[0], r[1], r[2], r[3] / u])); - let y = (S) => { - let $ = M("output", e[0].dataType, r.length, u), v = he($.type.tensor), x = He(t, $.type.value, v), T = E("x", e[0].dataType, a.length), C = E("w", e[1].dataType, d.length, u), A = [T, C]; - o && A.push(E("b", e[2].dataType, e[2].dims, u)); - let P = [{ name: "output_size", type: "u32" }, { name: "dilations", type: "u32", length: t.dilations.length }, { name: "strides", type: "u32", length: 2 }, { name: "pads", type: "u32", length: 2 }, { name: "output_channels_per_group", type: "u32" }]; - Fe(t, P); - let B = l ? ` - for (var wHeight: u32 = 0u; wHeight < uniforms.w_shape[0]; wHeight++) { - let xHeight = xRCCorner.x + wHeight * uniforms.dilations[0]; - - if (xHeight < 0u || xHeight >= uniforms.x_shape[1]) { - continue; - } - - for (var wWidth: u32 = 0u; wWidth < uniforms.w_shape[1]; wWidth++) { - let xWidth = xRCCorner.y + wWidth * uniforms.dilations[1]; - if (xWidth < 0u || xWidth >= uniforms.x_shape[2]) { - continue; - } - - for (var wInChannel: u32 = 0u; wInChannel < uniforms.w_shape[2]; wInChannel++) { - let input_channel = in_channel_offset + wInChannel; - let xVal = ${T.get("batch", "xHeight", "xWidth", "input_channel")}; - let wVal = ${C.get("wHeight", "wWidth", "wInChannel", "output_channel")}; - value += xVal * wVal; - } - } - } - ` : ` - for (var wInChannel: u32 = 0u; wInChannel < uniforms.w_shape[1]; wInChannel++) { - let input_channel = in_channel_offset + wInChannel; - for (var wHeight: u32 = 0u; wHeight < uniforms.w_shape[2]; wHeight++) { - let xHeight = xRCCorner.x + wHeight * uniforms.dilations[0]; - - if (xHeight < 0u || xHeight >= uniforms.x_shape[2]) { - continue; - } - - for (var wWidth: u32 = 0u; wWidth < uniforms.w_shape[3]; wWidth++) { - let xWidth = xRCCorner.y + wWidth * uniforms.dilations[1]; - if (xWidth < 0u || xWidth >= uniforms.x_shape[3]) { - continue; - } - - let xVal = ${T.get("batch", "input_channel", "xHeight", "xWidth")}; - let wVal = ${C.get("output_channel", "wInChannel", "wHeight", "wWidth")}; - value += xVal * wVal; - } - } - } - `; - return ` - ${S.registerUniforms(P).declareVariables(...A, $)} - - ${S.mainStart()} - ${S.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} - - let outputIndices = ${$.offsetToIndices("global_idx")}; - let batch: u32 = outputIndices[0]; - let output_channel: u32 = outputIndices[${l ? 3 : 1}]; - let xRCCorner: vec2 = vec2(outputIndices[${l ? 1 : 2}], outputIndices[${l ? 2 : 3}]) * uniforms.strides - uniforms.pads; - let group_id: u32 = output_channel * ${u} / uniforms.output_channels_per_group; - var in_channel_offset = group_id * uniforms.w_shape[${l ? 2 : 1}]; - - var value: ${$.type.value} = ${$.type.value}(0); - ${B} - ${i} - ${x} - ${$.setByOffset("global_idx", "value")} - }`; - }; - return { name: "GroupedConv", shaderCache: { hint: `${t.cacheKey}_${u}`, inputDependencies: g }, getRunData: () => ({ outputs: [{ dims: n ? n(r) : r, dataType: e[0].dataType }], dispatchGroup: { x: Math.ceil(h / 64) }, programUniforms: w }), getShaderSource: y }; - }, Vu = (e, t, r, n) => { - let o = e.length > 2, i = we(r[3]), a = we(r[2]), d = k.size(r) / i / a, l = [e[0].dims[0], e[0].dims[1], e[0].dims[2], e[0].dims[3] / i], c = [e[1].dims[0], e[1].dims[1], e[1].dims[2], e[1].dims[3] / i], m = [r[0], r[1], r[2], r[3] / i], u = [{ type: 12, data: d }, { type: 6, data: [t.strides[0], t.strides[1]] }, { type: 6, data: [t.pads[0], t.pads[1]] }]; - Ge(t, u), u.push(...R(l, c, m)); - let h = (a - 1) * t.strides[1] + c[1], w = (g) => { - let y = M("output", e[0].dataType, m.length, i), S = he(y.type.tensor), $ = He(t, y.type.value, S), v = E("x", e[0].dataType, l.length, i), x = E("w", e[1].dataType, c.length, i), T = [v, x]; - o && T.push(E("b", e[2].dataType, e[2].dims, i)); - let C = o ? "value += b[output_channel];" : "", A = [{ name: "output_size", type: "u32" }, { name: "strides", type: "i32", length: 2 }, { name: "pads", type: "i32", length: 2 }]; - return Fe(t, A), ` - ${g.registerUniforms(A).declareVariables(...T, y)} - ${g.mainStart()} - ${g.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} - let width0 = uniforms.output_shape[3]; - let output_channel = global_idx % width0; - var index1 = global_idx / width0; - let width1 = uniforms.output_shape[2] / ${a}u; - let col = (index1 % width1) * ${a}u; - index1 = index1 / width1; - let row = index1 % uniforms.output_shape[1]; - let batch = index1 / uniforms.output_shape[1]; - - let x_corner = vec2(i32(row), i32(col)) * uniforms.strides - uniforms.pads; - - var x_vals: array<${v.type.value}, ${h}>; - var values: array<${y.type.value}, ${a}>; - let input_channel = output_channel; - // Use constant instead of uniform can give better performance for w's height/width. - for (var w_height: u32 = 0u; w_height < ${c[0]}; w_height++) { - let x_height = x_corner.x + i32(w_height); - if (x_height >= 0 && u32(x_height) < uniforms.x_shape[1]) { - for (var i = 0; i < ${h}; i++) { - let x_width = x_corner.y + i; - if (x_width >= 0 && u32(x_width) < uniforms.x_shape[2]) { - x_vals[i] = ${v.get("batch", "u32(x_height)", "u32(x_width)", "input_channel")}; - } else { - x_vals[i] = ${v.type.value}(0); - } - } - for (var w_width: u32 = 0u; w_width < ${c[1]}; w_width++) { - let w_val = ${x.get("w_height", "w_width", "0", "output_channel")}; - for (var i = 0u; i < ${a}u; i++) { - values[i] = fma(x_vals[i * u32(uniforms.strides[1]) + w_width], w_val, values[i]); - } - } - } - } - - for (var i = 0u; i < ${a}u; i++) { - var value = values[i]; - ${C} - ${$} - ${y.set("batch", "row", "col + i", "output_channel", "value")}; - } - }`; - }; - return { name: "GroupedConv-Vectorize", shaderCache: { hint: `${t.cacheKey};${i};${a};${h};${c[0]};${c[1]}`, inputDependencies: o ? ["rank", "rank", "type"] : ["rank", "rank"] }, getRunData: () => ({ outputs: [{ dims: n ? n(r) : r, dataType: e[0].dataType }], dispatchGroup: { x: Math.ceil(d / 64) }, programUniforms: u }), getShaderSource: w }; - }; - }); - var mo, Km, Wu, fo = V(() => { - "use strict"; - Q(); - ie(); - rr(); - ae(); - ct(); - mo = (e, t, r, n, o = false, i) => { - let a = e[0].dims, d = e[1].dims, l = a[a.length - 2], c = d[d.length - 1], m = a[a.length - 1], u = we(c), h = we(m), w = we(l), g = k.size(r) / u / w, y = e.length > 2, S = n ? n.slice(0, -2) : r.slice(0, -2), v = [k.size(S), l, c], x = [{ type: 12, data: g }, { type: 12, data: l }, { type: 12, data: c }, { type: 12, data: m }]; - Ge(t, x), x.push(...R(S, a, d)), y && x.push(...R(e[2].dims)), x.push(...R(v)); - let T = (C) => { - let A = Fr("batch_dims", e[0].dataType, S.length), P = E("a", e[0].dataType, a.length, h), B = E("b", e[1].dataType, d.length, u), N = M("output", e[0].dataType, v.length, u), W = he(N.type.tensor), K = He(t, N.type.value, W), Z = [P, B], ee = ""; - if (y) { - let re = o ? u : 1; - Z.push(E("bias", e[2].dataType, e[2].dims.length, re)), ee = `${o ? `value += bias[col / ${re}];` : `value += ${N.type.value}(bias[row + i]);`}`; - } - let se = a.slice(0, -2), de = d.slice(0, -2), Y = Nt(se, S), le = Nt(de, S), ce = [{ name: "output_size", type: "u32" }, { name: "M", type: "u32" }, { name: "N", type: "u32" }, { name: "K", type: "u32" }]; - Fe(t, ce); - let q = (re, ne) => { - let oe = re.rank, U = re.name; - if (oe === 2) return `var ${U}_indices = ${re.type.indices}(0u, 0u);`; - let G = A.rank, ye = `var ${U}_indices: ${re.type.indices};`; - for (let Re = oe - 2 - 1, $e = G - 1; Re >= 0; Re--, $e--) ye += ` - ${U}_indices[${Re}] = ${G > 1 ? `batch_indices[${$e}]` : "batch_indices"};`; - return ne.forEach((Re) => { - ye += ` - ${U}_indices[${Re}] = 0;`; - }), ye += `${U}_indices[${oe - 2}] = 0u; - ${U}_indices[${oe - 1}] = 0u;`, ye; - }, ue = () => { - let re = `var a_data: ${P.type.value};`; - for (let ne = 0; ne < h; ne++) re += ` - let b_data${ne} = b[(b_offset + (k + ${ne}) * uniforms.N + col) / ${u}];`; - for (let ne = 0; ne < w; ne++) { - re += `a_data = a[(a_offset + (row + ${ne}) * uniforms.K + k) / ${h}];`; - for (let oe = 0; oe < h; oe++) re += ` - values[${ne}] = fma(${B.type.value}(a_data${h === 1 ? "" : `[${oe}]`}), b_data${oe}, values[${ne}]); - `; - } - return re; - }; - return ` - ${C.registerUniforms(ce).registerInternalVariables(A).declareVariables(...Z, N)} - ${C.mainStart()} - ${C.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} - let col = (global_idx % (uniforms.N / ${u})) * ${u}; - var index1 = global_idx / (uniforms.N / ${u}); - let stride1 = uniforms.M / ${w}; - let row = (index1 % stride1) * ${w}; - let batch = index1 / stride1; - - ${r.length === 2 ? "" : `let batch_indices = ${A.offsetToIndices("batch")};`} - ${q(P, Y)} - let a_offset = ${P.indicesToOffset("a_indices")}; - ${q(B, le)} - let b_offset = ${B.indicesToOffset("b_indices")}; - var values: array<${N.type.value}, ${w}>; - for (var k: u32 = 0u; k < uniforms.K; k = k + ${h}) { - ${ue()} - } - for (var i = 0u; i < ${w}u; i++) { - var value = values[i]; - ${ee} - ${K} - let cur_indices = ${N.type.indices}(batch, row + i, col); - let offset = ${N.indicesToOffset("cur_indices")}; - ${N.setByOffset(`offset / ${u}`, "value")}; - } - } - `; - }; - return { name: "MatMulNaive", shaderCache: { hint: `${t.activation};${u};${h};${w};${o}`, inputDependencies: y ? ["rank", "rank", "rank"] : ["rank", "rank"] }, getRunData: () => ({ outputs: [{ dims: i ? i(r) : r, dataType: e[0].dataType }], dispatchGroup: { x: Math.ceil(g / 64) }, programUniforms: x }), getShaderSource: T }; - }, Km = (e) => { - if (!e || e.length !== 2) throw new Error("MatMul requires 2 inputs."); - if (e[0].dims[e[0].dims.length - 1] !== e[1].dims[e[1].dims.length - 2]) throw new Error("shared dimension does not match."); - }, Wu = (e) => { - Km(e.inputs); - let t = rt.calcShape(e.inputs[0].dims, e.inputs[1].dims, true); - if (!t) throw new Error("Can't use matmul on the given tensors"); - let r = t[t.length - 1], n = e.inputs[0].dims[e.inputs[0].dims.length - 1]; - r < 8 && n < 8 ? e.compute(mo(e.inputs, { activation: "" }, t)) : e.compute(en(e.inputs, { activation: "" }, t)); - }; - }); - var Ym, ho, Xm, go, yo, Lu, Zm, Qm, bo, Hu = V(() => { - "use strict"; - ie(); - zu(); - Ru(); - rr(); - Nu(); - ct(); - fo(); - lt(); - Ym = (e, t, r, n, o, i) => { - let a = e[0], d = e.slice(i ? 1 : 2, i ? 3 : 4), l = d.length, c = t[0], u = t.slice(2).map((g, y) => g + (g - 1) * (r[y] - 1)), w = d.map((g, y) => g + n[y] + n[y + l]).map((g, y) => Math.floor((g - u[y] + o[y]) / o[y])); - return w.splice(0, 0, a), w.splice(i ? 3 : 1, 0, c), w; - }, ho = [2, 3, 1, 0], Xm = (e, t) => { - if (!e || e.length !== 2 && e.length !== 3) throw new Error("Conv requires 2 or 3 inputs"); - if (e[0].dims.length > 5) throw new Error("greater than 5D is not supported"); - if (e[0].dims.length !== e[1].dims.length) throw new Error("filter does not have same dimension as input"); - let r = e[0].dims[t.format === "NHWC" ? e[0].dims.length - 1 : 1], n = e[1].dims[1] * t.group; - if (r !== n) throw new Error("FILTER_IN_CHANNEL should be equal to DATA_CHANNEL"); - if (e.length === 3 && (e[2].dims.length !== 1 || e[1].dims[0] !== e[2].dims[0])) throw new Error("invalid bias"); - let o = e[0].dims.length - 2; - if (t.dilations.length !== o) throw new Error(`dilations should be ${o}D`); - if (t.strides.length !== o) throw new Error(`strides should be ${o}D`); - if (t.pads.length !== o * 2) throw new Error(`pads should be ${o * 2}D`); - if (t.kernelShape.length !== 0 && t.kernelShape.length !== e[1].dims.length - 2) throw new Error("invalid kernel shape"); - }, go = (e, t) => { - let r = e.kernelShape.slice(); - r.length < t[1].dims.length - 2 && r.push(...Array(t[1].dims.length - 2 - r.length).fill(0)); - for (let i = 2; i < t[1].dims.length; ++i) r[i - 2] === 0 && (r[i - 2] = t[1].dims[i]); - let n = e.pads.slice(); - Ct.adjustPadsBasedOnAutoPad(t[0].dims, e.strides, e.dilations, r, n, e.format === "NHWC", e.autoPad); - let o = Object.assign({}, e); - return Object.assign(o, { kernelShape: r, pads: n }), o; - }, yo = (e) => { - let t = Zr(e), r = e.format, n = ["NOTSET", "VALID", "SAME_UPPER", "SAME_LOWER"][e.auto_pad], o = e.dilations, i = e.group, a = e.kernel_shape, d = e.pads, l = e.strides, c = e.w_is_const(); - return { autoPad: n, format: r, dilations: o, group: i, kernelShape: a, pads: d, strides: l, wIsConst: c, ...t, cacheKey: `${e.format};${t.activation};` }; - }, Lu = (e, t, r, n) => { - let o = r.format === "NHWC", i = Ym(t[0].dims, t[1].dims, r.dilations, r.pads, r.strides, o); - if (r.group !== 1) { - let A = [t[0]]; - if (o) { - let B = e.kernelCustomData.wT ?? e.compute(Pe(t[1], ho), { inputs: [1], outputs: [r.wIsConst ? -2 : -1] })[0]; - r.wIsConst && !e.kernelCustomData.wT && (e.kernelCustomData.wT = B), A.push(B); - } else A.push(t[1]); - t.length === 3 && A.push(t[2]), !e.adapterInfo.isArchitecture("ampere") && o && t[1].dims[0] === r.group && t[1].dims[1] === 1 && r.dilations[0] === 1 && r.dilations[1] === 1 ? e.compute(Vu(A, r, i, n), { inputs: A }) : e.compute(Uu(A, r, i, n), { inputs: A }); - return; - } - let a = t.length === 3, d = t[0].dims[o ? 1 : 2], l = t[0].dims[o ? 2 : 3], c = t[0].dims[o ? 3 : 1], m = t[1].dims[2], u = t[1].dims[3], h = i[o ? 1 : 2], w = i[o ? 2 : 3], g = i[o ? 3 : 1], y = o && m === d && u === l && r.pads[0] === 0 && r.pads[1] === 0; - if (y || m === 1 && u === 1 && r.dilations[0] === 1 && r.dilations[1] === 1 && r.strides[0] === 1 && r.strides[1] === 1 && r.pads[0] === 0 && r.pads[1] === 0) { - let A = i[0], P, B, N, W = []; - if (o) { - let ee = e.kernelCustomData.wT ?? e.compute(Pe(t[1], ho), { inputs: [1], outputs: [r.wIsConst ? -2 : -1] })[0]; - if (r.wIsConst && !e.kernelCustomData.wT && (e.kernelCustomData.wT = ee), y) { - let se = d * l * c; - P = t[0].reshape([1, A, se]), B = ee.reshape([1, se, g]), N = [1, A, g]; - } else P = t[0].reshape([A, d * l, c]), B = ee.reshape([1, c, g]), N = [A, h * w, g]; - W.push(P), W.push(B); - } else P = t[0].reshape([A, c, d * l]), B = t[1].reshape([1, g, c]), N = [A, g, h * w], W.push(B), W.push(P); - a && W.push(t[2]); - let K = N[2], Z = W[0].dims[W[0].dims.length - 1]; - K < 8 && Z < 8 ? e.compute(mo(W, r, i, N, o, n), { inputs: W }) : e.compute(en(W, r, i, N, o, n), { inputs: W }); - return; - } - let S = true, $ = e.kernelCustomData.wT ?? e.compute(Pe(t[1], ho), { inputs: [1], outputs: [r.wIsConst ? -2 : -1] })[0]; - r.wIsConst && !e.kernelCustomData.wT && (e.kernelCustomData.wT = $); - let v = [t[0], $]; - a && v.push(t[2]); - let x = o ? h * w : g, T = o ? g : h * w, C = m * u * c; - e.compute(Pu(v, r, i, x, T, C, a, S, n), { inputs: v }); - }, Zm = (e, t) => { - let r = t.format === "NHWC", n = [e.inputs[0].reshape(r ? [e.inputs[0].dims[0], 1, e.inputs[0].dims[1], e.inputs[0].dims[2]] : [e.inputs[0].dims[0], e.inputs[0].dims[1], 1, e.inputs[0].dims[2]]), e.inputs[1].reshape([e.inputs[1].dims[0], e.inputs[1].dims[1], 1, e.inputs[1].dims[2]])]; - e.inputs.length === 3 && n.push(e.inputs[2]); - let o = [0, t.pads[0], 0, t.pads[1]], i = [1].concat(t.strides), a = [1].concat(t.dilations), d = [1].concat(t.kernelShape), l = go({ ...t, pads: o, strides: i, dilations: a, kernelShape: d }, n); - Lu(e, n, l, (c) => r ? [c[0], c[2], c[3]] : [c[0], c[1], c[3]]); - }, Qm = (e, t, r) => { - let n = r.format === "NHWC" ? "channelsLast" : "channelsFirst", o = go(r, t), i = r.autoPad === "NOTSET" ? r.pads : r.autoPad, a = Du(t[0].dims, t[1].dims, r.strides, r.dilations, i, false, n); - e.compute(Mu(t, o, a.outShape, [a.filterDepth, a.filterHeight, a.filterWidth], [a.padInfo.front, a.padInfo.top, a.padInfo.left], n)); - }, bo = (e, t) => { - if (Xm(e.inputs, t), e.inputs[0].dims.length === 3) Zm(e, t); - else if (e.inputs[0].dims.length === 5) Qm(e, e.inputs, t); - else { - let r = go(t, e.inputs); - Lu(e, e.inputs, r); - } - }; - }); - var Jm, Gu, Fu = V(() => { - "use strict"; - Q(); - Xe(); - ae(); - ct(); - Jt(); - po(); - rr(); - Jm = (e, t = false, r, n, o = 4) => { - let i = ($) => { - switch ($) { - case 1: - return "return w[getIndexFromCoords4D(coord, vec4(uniforms.w_shape))];"; - case 4: - return ` - let coord1 = vec4(coordX, coordY, col + 1, rowInner); - let coord2 = vec4(coordX, coordY, col + 2, rowInner); - let coord3 = vec4(coordX, coordY, col + 3, rowInner); - let v0 = w[getIndexFromCoords4D(coord, vec4(uniforms.w_shape))]; - let v1 = w[getIndexFromCoords4D(coord1, vec4(uniforms.w_shape))]; - let v2 = w[getIndexFromCoords4D(coord2, vec4(uniforms.w_shape))]; - let v3 = w[getIndexFromCoords4D(coord3, vec4(uniforms.w_shape))]; - return ${n}(v0, v1, v2, v3); - `; - default: - throw new Error(`innerElementSize ${$} is not supported.`); - } - }, a = e ? ` - let coord = vec4(batch, iXR, iXC, xCh); - ` : ` - let coord = vec4(batch, xCh, iXR, iXC); - `, d = e ? ` - let coords = vec4( - batch, - row / outWidth, - row % outWidth, - col); - ` : ` - let coords = vec4( - batch, - row, - col / outWidth, - col % outWidth); - `, l = e ? "i32(uniforms.x_shape[1])" : "i32(uniforms.x_shape[2])", c = e ? "i32(uniforms.x_shape[2])" : "i32(uniforms.x_shape[3])", m = e ? "row" : "col", u = e ? "col" : "row", h = ` - let inChannels = ${e ? "i32(uniforms.x_shape[3])" : "i32(uniforms.x_shape[1])"}; - let outWidth = ${e ? "i32(uniforms.result_shape[2])" : "i32(uniforms.result_shape[3])"}; - let outRow = ${m} / outWidth; - let outCol = ${m} % outWidth; - - let WRow = ${u} / (uniforms.filter_dims[1] * inChannels); - let WCol = ${u} / inChannels % uniforms.filter_dims[1]; - let xR = f32(outRow - uniforms.pads[0] + uniforms.dilations[0] * WRow) / f32(uniforms.strides[0]); - let xC = f32(outCol - uniforms.pads[1] + uniforms.dilations[1] * WCol) / f32(uniforms.strides[1]); - if (xR < 0.0 || xR >= f32(${l}) || fract(xR) > 0.0) { - return ${n}(0.0); - } - if (xC < 0.0 || xC >= f32(${c}) || fract(xC) > 0.0) { - return ${n}(0.0); - } - let iXR = i32(xR); - let iXC = i32(xC); - let xCh = ${u} % inChannels; - ${a} - return x[getIndexFromCoords4D(coord, vec4(uniforms.x_shape))/${o}];`, w = e ? ` - let col = colIn * ${o}; - if (row < uniforms.dim_a_outer && col < uniforms.dim_inner) { - ${h} - } - return ${n}(0.0);` : ` - let col = colIn * ${o}; - if (row < uniforms.dim_inner && col < uniforms.dim_b_outer) { - ${h} - } - return ${n}(0.0);`, g = ` - let col = colIn * ${o}; - let inChannels = ${e ? "i32(uniforms.x_shape[3])" : "i32(uniforms.x_shape[1])"}; - let coordX = uniforms.filter_dims[0] - 1 - row / (uniforms.filter_dims[1] * inChannels); - let coordY = uniforms.filter_dims[1] - 1 - (row / inChannels) % uniforms.filter_dims[1]; - if (${e ? "row < uniforms.dim_inner && col < uniforms.dim_b_outer" : "row < uniforms.dim_inner && col < uniforms.dim_a_outer"} && coordX >= 0 && coordY >= 0) { - let rowInner = row % inChannels; - let coord = vec4(coordX, coordY, col, rowInner); - ${i(o)} - } - return ${n}(0.0); - `, y = He(r, n); - return ` - fn mm_readA(batch: i32, row : i32, colIn : i32) -> ${n} { - ${e ? w : g} - } - - fn mm_readB(batch: i32, row : i32, colIn : i32) -> ${n} { - ${e ? g : w} - } - - fn mm_write(batch: i32, row : i32, colIn : i32, valueInput : ${n}) { - let col = colIn * ${o}; - if (row < uniforms.dim_a_outer && col < uniforms.dim_b_outer) { - var value = valueInput; - let outWidth = ${e ? "i32(uniforms.result_shape[2])" : "i32(uniforms.result_shape[3])"}; - ${d} - ${Qr(t)} - ${y} - result[getIndexFromCoords4D(coords, vec4(uniforms.result_shape))/${o}] = value; - } - }`; - }, Gu = (e, t, r, n, o, i, a, d) => { - let l = t.format === "NHWC", c = l ? e[0].dims[3] : e[0].dims[1], m = r[0], u = l ? r[2] : r[3], h = l ? r[1] : r[2], w = l ? r[3] : r[1], g = l && c % 4 === 0 && c % 3 && w % 4 === 0, y = l ? w : u * h, S = l ? u * h : w, $ = [8, 8, 1], v = n <= 8 ? [4, 1, 1] : [4, 4, 1], x = [Math.ceil(y / $[0] / v[0]), Math.ceil(S / $[1] / v[1]), Math.ceil(m / $[2] / v[2])]; - me("verbose", () => `[conv_backprop_mm_webgpu] dispatch = ${x}`); - let T = g ? 4 : 1, C = Math.max($[0] * T, $[1]), A = g ? 4 : 1, P = [t.kernelShape[l ? 1 : 2], t.kernelShape[l ? 2 : 3]], B = [P[0] + (t.dilations[0] <= 1 ? 0 : (P[0] - 1) * (t.dilations[0] - 1)), P[1] + (t.dilations[1] <= 1 ? 0 : (P[1] - 1) * (t.dilations[1] - 1))], N = [B[0] - 1 - Math.floor((t.pads[0] + t.pads[2]) / 2), B[1] - 1 - Math.floor((t.pads[1] + t.pads[3]) / 2)], W = [{ type: 6, data: n }, { type: 6, data: o }, { type: 6, data: i }, { type: 6, data: t.strides }, { type: 6, data: t.dilations }, { type: 6, data: P }, { type: 6, data: N }]; - Ge(t, W), W.push(...R(e[0].dims, e[1].dims)); - let K = ["rank", "rank"]; - a && (W.push(...R(e[2].dims)), K.push("rank")), W.push(...R(r)); - let Z = (ee) => { - let se = E("x", e[0].dataType, e[0].dims.length, A), de = E("w", e[1].dataType, e[1].dims.length, 1), Y = M("result", e[0].dataType, r.length, A), le = [se, de], ce = ""; - if (a) { - let re = E("bias", e[2].dataType, e[2].dims.length, A); - le.push(re), ce += ` - fn getBiasByOutputCoords(coords : vec4) -> ${re.type.value} { - return bias[coords.${l ? "w" : "y"}${g ? "/ 4" : ""}]; - }`; - } - let q = [{ name: "dim_a_outer", type: "i32" }, { name: "dim_b_outer", type: "i32" }, { name: "dim_inner", type: "i32" }, { name: "strides", type: "i32", length: 2 }, { name: "dilations", type: "i32", length: 2 }, { name: "filter_dims", type: "i32", length: P.length }, { name: "pads", type: "i32", length: N.length }]; - Fe(t, q); - let ue = he(e[0].dataType, 1); - if (ue !== "f16" && ue !== "f32") throw new Error(`elemType ${ue} is not supported.`); - return ` - ${Jr("uniforms.result_strides")} - ${ee.registerUniforms(q).declareVariables(...le, Y)}; - ${ce} - ${Jm(l, a, t, se.type.value, T)} - ${g ? er(v, $, ue, void 0, !l, C) : tr(v, $, ue, void 0, !l, C, false, void 0, d)}`; - }; - return { name: "Conv2DTransposeMatMul", shaderCache: { hint: `${t.cacheKey};${v};${$};${g}`, inputDependencies: K }, getRunData: () => ({ outputs: [{ dims: r, dataType: e[0].dataType }], dispatchGroup: { x: x[0], y: x[1], z: x[2] }, programUniforms: W }), getShaderSource: Z }; - }; - }); - var ef, wo, qu = V(() => { - "use strict"; - Q(); - Xe(); - ie(); - ae(); - ef = (e, t, r, n, o, i = false, a, d, l = false) => { - let c = l ? 1 : 2, m = l ? 2 : 3, u = l ? 3 : 1, h = i ? 2 : 1, w = ` - fn setOutputAtIndex(flatIndex : u32, value : ${i ? `vec4<${a}>` : a}) { - result[flatIndex] = ${i ? `vec4<${a}>` : a}(value); - }`; - n && (w += ` - fn getBiasByOutputCoords(coords : vec4) -> ${i ? `vec4<${a}>` : a} { - return bias[coords.${l ? "w" : "y"}${i ? "/ 4" : ""}]; - }`); - let g = i ? 4 : 1, y = E("W", t[1].dataType, t[1].dims.length, g), S = E("Dy", t[0].dataType, t[0].dims.length, g), $ = [S, y]; - n && $.push(E("bias", t[2].dataType, [r[u]].length, g)); - let v = M("result", t[0].dataType, r.length, g), x = `{ - let batch: u32 = ${o ? "global_id.z" : "workgroup_id.z"} / uniforms.result_shape[1]; - let r = ${o ? "global_id.z" : "workgroup_id.z"} % uniforms.result_shape[1]; - let c = ${o ? "global_id.y" : "workgroup_id.y"} * ${h}; - let d1: u32 = ${o ? "global_id.x" : "workgroup_id.x"} * 4; - - let dyCorner = vec2(i32(r), i32(c)) - vec2(uniforms.pads); - - // Convolve dy(?, ?, d2) with w(:, :, d1, d2) to compute dx(xR, xC, d1). - // ? = to be determined. : = across all values in that axis. - var dotProd: array, ${h}>; - for (var i = 0; i < ${h}; i++) { - dotProd[i] = vec4<${a}>(0.0); - } - for (var wR: u32 = 0; wR < uniforms.filter_dims[0]; wR = wR + 1) { - var dyR = (${a}(dyCorner.x) + ${a}(wR)) / ${a}(uniforms.strides.x); - let wRPerm = uniforms.filter_dims[0] - 1 - wR; - if (dyR < 0.0 || dyR >= ${a}(uniforms.Dy_shape[1]) || - fract(dyR) > 0.0 || wRPerm < 0) { - continue; - } - let idyR: u32 = u32(dyR); - - for (var wC: u32 = 0; wC < uniforms.filter_dims[1]; wC = wC + 1) { - let dyC = (${a}(dyCorner.y) + ${a}(wC)) / ${a}(uniforms.strides.y); - let dyC2 = (${a}(dyCorner.y) + 1.0 + ${a}(wC)) / ${a}(uniforms.strides.y); - let wCPerm = uniforms.filter_dims[1] - 1 - wC; - if (wCPerm < 0) { - continue; - } - var bDyCVal = true; - var bDyCVal2 = true; - if (dyC < 0.0 || dyC >= ${a}(uniforms.Dy_shape[2]) || - fract(dyC) > 0.0) { - bDyCVal = false; - } - if (dyC2 < 0.0 || dyC2 >= ${a}(uniforms.Dy_shape[2]) || - fract(dyC2) > 0.0) { - bDyCVal2 = false; - } - - let idyC: u32 = u32(dyC); - let idyC2: u32 = u32(dyC2); - if (bDyCVal && bDyCVal2) { - let d2Length = uniforms.Dy_shape[3]; - for (var d2 :u32 = 0; d2 < d2Length; d2 = d2 + 4) { - let wValue0 = ${y.get("u32(wRPerm)", "u32(wCPerm)", "d1", "d2")}; - let wValue1 = ${y.get("u32(wRPerm)", "u32(wCPerm)", "d1 + 1", "d2")}; - let wValue2 = ${y.get("u32(wRPerm)", "u32(wCPerm)", "d1 + 2", "d2")}; - let wValue3 = ${y.get("u32(wRPerm)", "u32(wCPerm)", "d1 + 3", "d2")}; - - var xValue = ${S.get("batch", "idyR", "idyC", "d2")}; - let tmpval = vec4<${a}>(dot(xValue, wValue0), - dot(xValue, wValue1), - dot(xValue, wValue2), - dot(xValue, wValue3)); - dotProd[0] = dotProd[0] + tmpval; - - xValue = ${S.get("batch", "idyR", "idyC2", "d2")}; - - dotProd[1] = dotProd[1] + vec4<${a}>(dot(xValue, wValue0), - dot(xValue, wValue1), - dot(xValue, wValue2), - dot(xValue, wValue3)); - } - } else if (bDyCVal) { - let d2Length = uniforms.Dy_shape[${u}]; - for (var d2: u32 = 0; d2 < d2Length; d2 = d2 + 4) { - let wValue0 = ${y.get("u32(wRPerm)", "u32(wCPerm)", "d1", "d2")}; - let wValue1 = ${y.get("u32(wRPerm)", "u32(wCPerm)", "d1 + 1", "d2")}; - let wValue2 = ${y.get("u32(wRPerm)", "u32(wCPerm)", "d1 + 2", "d2")}; - let wValue3 = ${y.get("u32(wRPerm)", "u32(wCPerm)", "d1 + 3", "d2")}; - - var xValue = ${S.get("batch", "idyR", "idyC", "d2")}; - let tmpval = vec4<${a}>(dot(xValue, wValue0), - dot(xValue, wValue1), - dot(xValue, wValue2), - dot(xValue, wValue3)); - dotProd[0] = dotProd[0] + tmpval; - } - } else if (bDyCVal2) { - let d2Length = uniforms.Dy_shape[3]; - for (var d2: u32 = 0; d2 < d2Length; d2 = d2 + 4) { - let wValue0 = ${y.get("u32(wRPerm)", "u32(wCPerm)", "d1", "d2")}; - let wValue1 = ${y.get("u32(wRPerm)", "u32(wCPerm)", "d1 + 1", "d2")}; - let wValue2 = ${y.get("u32(wRPerm)", "u32(wCPerm)", "d1 + 2", "d2")}; - let wValue3 = ${y.get("u32(wRPerm)", "u32(wCPerm)", "d1 + 3", "d2")}; - - var xValue = ${S.get("batch", "idyR", "idyC2", "d2")}; - let tmpval = vec4<${a}>(dot(xValue, wValue0), - dot(xValue, wValue1), - dot(xValue, wValue2), - dot(xValue, wValue3)); - dotProd[1] = dotProd[1] + tmpval; - } - } - } - } - - for (var i: u32 = 0; i < ${h}; i = i + 1) { - let value = dotProd[i] + ${n ? "bias[c+i]" : `vec4<${a}>(0.0)`}; - ${v.set("batch", "r", "c + i", "d1", "value")}; - } - }`, T = ` - let outputIndices = ${v.offsetToIndices("global_idx")}; - let batch = ${v.indicesGet("outputIndices", 0)}; - let d1 = ${v.indicesGet("outputIndices", u)}; - let r = ${v.indicesGet("outputIndices", c)}; - let c = ${v.indicesGet("outputIndices", m)}; - let dyCorner = vec2(i32(r), i32(c)) - uniforms.pads; - let dyRCorner = dyCorner.x; - let dyCCorner = dyCorner.y; - let groupId = d1 / uniforms.output_channels_per_group; - let wOutChannel = d1 - groupId * uniforms.output_channels_per_group; - // Convolve dy(?, ?, d2) with w(:, :, d1, d2) to compute dx(xR, xC, d1). - // ? = to be determined. : = across all values in that axis. - var dotProd = ${a}(0.0); - for (var wR: u32 = 0; wR < uniforms.effective_filter_dims.x; wR = wR + 1) { - if (wR % uniforms.dilations.x != 0) { - continue; - } - let dyR = (${a}(dyRCorner) + ${a}(wR)) / ${a}(uniforms.strides[0]); - let wRPerm = uniforms.filter_dims.x - 1 - wR / uniforms.dilations.x; - if (dyR < 0.0 || dyR >= ${a}(uniforms.Dy_shape[${c}]) || fract(dyR) > 0.0 || - wRPerm < 0) { - continue; - } - let idyR: u32 = u32(dyR); - - for (var wC: u32 = 0; wC < uniforms.effective_filter_dims.y; wC = wC + 1) { - if (wC % uniforms.dilations.y != 0) { - continue; - } - let dyC = (${a}(dyCCorner) + ${a}(wC)) / ${a}(uniforms.strides.y); - let wCPerm = uniforms.filter_dims.y - 1 - wC / uniforms.dilations.y; - if (dyC < 0.0 || dyC >= ${a}(uniforms.Dy_shape[${m}]) || - fract(dyC) > 0.0 || wCPerm < 0) { - continue; - } - let idyC: u32 = u32(dyC); - var inputChannel = groupId * uniforms.input_channels_per_group; - for (var d2: u32 = 0; d2 < uniforms.input_channels_per_group; d2 = d2 + 1) { - let xValue = ${l ? S.get("batch", "idyR", "idyC", "inputChannel") : S.get("batch", "inputChannel", "idyR", "idyC")}; - let wValue = ${y.get("inputChannel", "wOutChannel", "u32(wRPerm)", "u32(wCPerm)")}; - dotProd = dotProd + xValue * wValue; - inputChannel = inputChannel + 1; - } - } - } - let value = dotProd + ${n ? "bias[d1]" : `${a}(0.0)`}; - ${v.setByOffset("global_idx", "value")}; - `; - return ` - ${e.registerUniforms(d).declareVariables(...$, v)} - ${w} - - ${e.mainStart()} - ${e.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")}; - ${i ? x : T}}`; - }, wo = (e, t, r) => { - let n = e.length > 2, o = t.outputShape, i = k.size(o), a = [Math.ceil(i / 64), 1, 1]; - me("verbose", () => `[conv2d_backprop_webgpu] dispatch = ${a}`); - let d = t.format === "NHWC", l = ["rank", "rank"], c = [t.strides[0], t.strides[1]], m = [t.kernelShape[d ? 1 : 2], t.kernelShape[d ? 2 : 3]], u = [t.dilations[0], t.dilations[1]], h = [m[0] + (t.dilations[0] <= 1 ? 0 : (t.kernelShape[d ? 1 : 2] - 1) * (t.dilations[0] - 1)), m[1] + (t.dilations[1] <= 1 ? 0 : (t.kernelShape[d ? 2 : 3] - 1) * (t.dilations[1] - 1))], w = [h[0] - 1 - Math.floor((t.pads[0] + t.pads[2]) / 2), h[1] - 1 - Math.floor(t.pads[1] + t.pads[3]) / 2], g = false, y = t.group, S = e[1].dims, $ = S[0] / y, v = S[1], x = [{ type: 12, data: i }, { type: 12, data: c }, { type: 12, data: m }, { type: 12, data: u }, { type: 12, data: h }, { type: 6, data: w }, { type: 12, data: $ }, { type: 12, data: v }, ...R(e[0].dims, e[1].dims)]; - n && (x.push(...R(e[2].dims)), l.push("rank")), x.push(...R(o)); - let T = a[1] === 1 && a[2] === 1, C = (A) => { - let P = [{ name: "output_size", type: "u32" }, { name: "strides", type: "u32", length: c.length }, { name: "filter_dims", type: "u32", length: m.length }, { name: "dilations", type: "u32", length: m.length }, { name: "effective_filter_dims", type: "u32", length: h.length }, { name: "pads", type: "i32", length: w.length }, { name: "input_channels_per_group", type: "u32" }, { name: "output_channels_per_group", type: "u32" }], B = he(e[0].dataType); - return `${ef(A, e, o, n, T, g, B, P, d)}`; - }; - return { name: "ConvTranspose2D", shaderCache: { hint: `${t.cacheKey};`, inputDependencies: l }, getRunData: () => ({ dispatchGroup: { x: a[0], y: a[1], z: a[2] }, outputs: [{ dims: r ? r(o) : o, dataType: e[0].dataType }], programUniforms: x }), getShaderSource: C }; - }; - }); - var tf, rf, nf, ju, Ku, of, af, sf, uf, Yu, Xu = V(() => { - "use strict"; - Fu(); - qu(); - ct(); - lt(); - tf = (e, t, r, n, o, i) => (e - 1) * t + r + (n - 1) * o + 1 - i, rf = (e, t, r, n, o) => { - let i = Math.floor(e / 2); - t === "SAME_UPPER" ? (r[n] = i, r[o] = e - i) : t === "SAME_LOWER" && (r[n] = e - i, r[o] = i); - }, nf = (e, t, r, n, o, i, a, d, l, c) => { - let m = e.length - 2, u = c.length === 0; - l.length < m && l.push(...Array(m - l.length).fill(0)); - let h = e[0], w = t[d ? 3 : 1] * o; - for (let g = 0, y = e.length - m - (d ? 1 : 0); g < m; ++g, ++y) { - let S = e[y], $ = u ? S * a[g] : c[g], v = tf(S, a[g], i[g], t[y], r[g], $); - rf(v, n, i, g, g + m), u && c.push(a[g] * (S - 1) + l[g] + (t[y] - 1) * r[g] + 1 - i[g] - i[g + m]); - } - c.splice(0, 0, h), c.splice(d ? 3 : 1, 0, w); - }, ju = (e, t) => { - let r = e.kernelShape.slice(); - if (e.kernelShape.length === 0 || e.kernelShape.reduce((u, h) => u * h, 1) === 0) { - r.length = 0; - for (let u = 2; u < t[1].dims.length; ++u) r.push(t[1].dims[u]); - } - let n = e.format === "NHWC"; - r.splice(0, 0, t[1].dims[0]), r.splice(n ? 3 : 1, 0, t[1].dims[1]); - let o = e.pads.slice(), i = e.outputShape.slice(), a = e.outputPadding.slice(), d = t[0].dims, l = e.dilations.slice(); - if (l.reduce((u, h) => u + h, 0) === 0) { - let u = t[0].dims.length - 2; - l = new Array(u).fill(1); - } - let c = e.strides.slice(); - if (c.reduce((u, h) => u + h, 0) === 0) { - let u = t[0].dims.length - 2; - c = new Array(u).fill(1); - } - nf(d, r, l, e.autoPad, e.group, o, c, n, a, i); - let m = Object.assign({}, e); - return Object.assign(m, { kernelShape: r, pads: o, outputPadding: a, outputShape: i, dilations: l, strides: c }), m; - }, Ku = (e) => { - let t = Zr(e), r = e.format, n = ["NOTSET", "VALID", "SAME_UPPER", "SAME_LOWER"][typeof e.autoPad > "u" ? 0 : e.autoPad], o = e.dilations, i = e.group, a = e.kernelShape, d = e.pads, l = e.strides, c = e.wIsConst(), m = e.outputPadding, u = e.outputShape; - return { autoPad: n, format: r, dilations: o, group: i, kernelShape: a, outputPadding: m, outputShape: u, pads: d, strides: l, wIsConst: c, ...t, cacheKey: `${e.format};${t.activation};` }; - }, of = (e, t) => { - if (!e || e.length !== 2 && e.length !== 3) throw new Error("Conv requires 2 or 3 inputs"); - if (e[0].dims.length !== 4 && e[0].dims.length !== 3) throw new Error("currently only support 2-dimensional conv"); - if (e[0].dims.length !== e[1].dims.length) throw new Error("filter does not have same dimension as input"); - let r = e[0].dims[t.format === "NHWC" ? e[0].dims.length - 1 : 1], n = e[1].dims[0]; - if (r !== n) throw new Error("FILTER_IN_CHANNEL should be equal to DATA_CHANNEL"); - let o = e[1].dims[1] * t.group; - if (e.length === 3 && (e[2].dims.length !== 1 || e[2].dims[0] !== o)) throw new Error("invalid bias"); - let i = e[0].dims.length - 2; - if (t.dilations.reduce((m, u) => m + u, 0) > 0 && t.dilations.length !== i) throw new Error(`dilations should be ${i}D`); - if (t.strides.reduce((m, u) => m + u, 0) > 0 && t.strides.length !== i) throw new Error(`strides should be ${i}D`); - if (t.pads.reduce((m, u) => m + u, 0) > 0 && t.pads.length !== i * 2) throw new Error(`pads should be ${i * 2}D`); - if (t.outputPadding.length !== i && t.outputPadding.length !== 0) throw new Error(`output_padding should be ${i}D`); - if (t.kernelShape.reduce((m, u) => m + u, 0) > 0 && t.kernelShape.length !== 0 && t.kernelShape.length !== e[1].dims.length - 2) throw new Error("invalid kernel shape"); - if (t.outputShape.length !== 0 && t.outputShape.length !== e[0].dims.length - 2) throw new Error("invalid output shape"); - }, af = [2, 3, 1, 0], sf = (e, t, r) => { - let n = ju(r, t), o = r.format === "NHWC", i = n.outputShape, a = i[o ? 3 : 1], d = t[0].dims[o ? 3 : 1]; - if (n.group !== 1 || a === 1 && d === 1) { - e.compute(wo(t, n)); - return; - } - let l = i[o ? 1 : 2], c = i[o ? 2 : 3], m = t[1].dims[2], u = t[1].dims[3], h = o ? l * c : a, w = o ? a : l * c, g = m * u * d, y = true, S = e.kernelCustomData.wT ?? e.compute(Pe(t[1], af), { inputs: [1], outputs: [r.wIsConst ? -2 : -1] })[0]; - r.wIsConst && !e.kernelCustomData.wT && (e.kernelCustomData.wT = S); - let $ = [t[0], S], v = t.length === 3; - v && (!o && t[2].dims.length === 1 ? $.push(t[2].reshape([t[2].dims[0], 1, 1])) : $.push(t[2])), e.compute(Gu($, n, i, h, w, g, v, y), { inputs: $ }); - }, uf = (e, t) => { - let r = t.format === "NHWC", n = [e.inputs[0].reshape(r ? [e.inputs[0].dims[0], 1, e.inputs[0].dims[1], e.inputs[0].dims[2]] : [e.inputs[0].dims[0], e.inputs[0].dims[1], 1, e.inputs[0].dims[2]]), e.inputs[1].reshape([e.inputs[1].dims[0], e.inputs[1].dims[1], 1, e.inputs[1].dims[2]])]; - e.inputs.length === 3 && n.push(e.inputs[2]); - let o = t.kernelShape; - (o.length === 0 || o[0] === 0) && (o = [e.inputs[1].dims[2]]); - let i = t.dilations; - (i.length === 0 || i[0] === 0) && (i = [1]); - let a = t.strides; - (a.length === 0 || a[0] === 0) && (a = [1]); - let d = t.pads; - d.length === 0 && (d = [0, 0]), d = [0, d[0], 0, d[1]], a = [1].concat(a), i = [1].concat(i), o = [1].concat(o); - let l = ju({ ...t, pads: d, strides: a, dilations: i, kernelShape: o }, n); - e.compute(wo(n, l, (c) => r ? [c[0], c[2], c[3]] : [c[0], c[1], c[3]])); - }, Yu = (e, t) => { - of(e.inputs, t), e.inputs[0].dims.length === 3 ? uf(e, t) : sf(e, e.inputs, t); - }; - }); - var df, Zu, Qu, Ju = V(() => { - "use strict"; - Q(); - ie(); - Ie(); - ae(); - df = (e, t, r, n) => { - let o = k.size(t), i = t.length, a = E("input", e, i), d = M("output", e, i), l = r.dataType === 6 ? r.getInt32Array()[0] : Number(r.getBigInt64Array()[0]), c = k.normalizeAxis(l, i), m = (u) => { - let h = ` i32(${a.indicesGet("inputIndices", "uniforms.axis")}) `, w = F("uniforms.input_shape", "uniforms.axis", i), g = n.reverse ? h + (n.exclusive ? " + 1" : "") : "0", y = n.reverse ? w : h + (n.exclusive ? "" : " + 1"); - return ` - ${u.registerUniform("outputSize", "u32").registerUniform("axis", "u32").declareVariables(a, d)} - ${u.mainStart()} - ${u.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.outputSize")} - var inputIndices = ${d.offsetToIndices("global_idx")}; - var sum = ${d.type.value}(0); - let first : i32 = ${g}; - let last : i32 = ${y}; - for (var i : i32 = first; i < last; i++) { - ${a.indicesSet("inputIndices", "uniforms.axis", "u32(i)")}; - sum = sum + ${a.getByIndices("inputIndices")}; - } - ${d.setByOffset("global_idx", "sum")}; - }`; - }; - return { name: "CumSum", shaderCache: { hint: n.cacheKey, inputDependencies: ["rank"] }, getRunData: () => ({ outputs: [{ dims: t, dataType: e }], dispatchGroup: { x: Math.ceil(o / 64) }, programUniforms: [{ type: 12, data: o }, { type: 12, data: c }, ...R(t, t)] }), getShaderSource: m }; - }, Zu = (e, t) => { - let r = e.inputs[0].dims, n = e.inputs[0].dataType, o = e.inputs[1]; - e.compute(df(n, r, o, t), { inputs: [0] }); - }, Qu = (e) => { - let t = e.exclusive === 1, r = e.reverse === 1; - return J({ exclusive: t, reverse: r }); - }; - }); - var lf, cf, pf, ed, td, rd = V(() => { - "use strict"; - Q(); - ie(); - Ie(); - ae(); - lf = (e) => { - if (!e || e.length !== 1) throw new Error("DepthToSpace requires 1 input."); - if (e[0].dims.length !== 4) throw new Error("DepthToSpace requires 4D input."); - }, cf = (e, t, r, n) => { - let o = []; - o.push(`fn perm(i: ${n.type.indices}) -> ${r.type.indices} { - var a: ${r.type.indices};`); - for (let i = 0; i < t; ++i) o.push(r.indicesSet("a", e[i], `i[${i}]`)); - return o.push("return a;}"), o.join(` - `); - }, pf = (e, t) => { - let r, n, o, i, a, d, l = t.format === "NHWC", c = t.blocksize, m = t.mode === "DCR"; - l ? ([r, n, o, i] = e.dims, a = m ? [r, n, o, c, c, i / c ** 2] : [r, n, o, i / c ** 2, c, c], d = m ? [0, 1, 3, 2, 4, 5] : [0, 1, 4, 2, 5, 3]) : ([r, n, o, i] = [e.dims[0], e.dims[2], e.dims[3], e.dims[1]], a = m ? [r, c, c, i / c ** 2, n, o] : [r, i / c ** 2, c, c, n, o], d = m ? [0, 3, 4, 1, 5, 2] : [0, 1, 4, 2, 5, 3]); - let u = e.reshape(a), h = u.dims.length, w = e.dataType, g = E("a", w, h), y = M("output", w, h), S = ($) => ` - ${$.registerUniform("output_size", "u32").declareVariables(g, y)} - - ${cf(d, h, g, y)} - - ${$.mainStart()} - ${$.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} - - let indices = ${y.offsetToIndices("global_idx")}; - let aIndices = perm(indices); - - ${y.setByOffset("global_idx", g.getByIndices("aIndices"))} - }`; - return { name: "DepthToSpace", shaderCache: { hint: `${e.dims};${t.blocksize};${t.mode}`, inputDependencies: ["rank"] }, getRunData: ($) => { - let v = l ? [r, n * c, o * c, i / c ** 2] : [r, i / c ** 2, n * c, o * c], x = k.size(v), T = u.dims, C = k.sortBasedOnPerm(T, d); - return { outputs: [{ dims: v, dataType: $[0].dataType }], dispatchGroup: { x: Math.ceil(x / 64) }, programUniforms: [{ type: 12, data: x }, ...R(T, C)] }; - }, getShaderSource: S }; - }, ed = (e, t) => { - lf(e.inputs), e.compute(pf(e.inputs[0], t)); - }, td = (e) => J({ blocksize: e.blocksize, mode: e.mode, format: e.format }); - }); - var _o, rn, nd, mf, ff, vo, $o, od, hf, id, ad, sd = V(() => { - "use strict"; - Q(); - ie(); - Ie(); - ae(); - _o = "[a-zA-Z]|\\.\\.\\.", rn = "(" + _o + ")+", nd = "^" + rn + "$", mf = "(" + rn + ",)*" + rn, ff = "^" + mf + "$", vo = class { - constructor(t = -1) { - this.symbolToIndices = /* @__PURE__ */ new Map(), this.inputIndex = t; - } - addSymbol(t, r) { - let n = this.symbolToIndices.get(t); - n === void 0 ? n = [r] : n.push(r), this.symbolToIndices.set(t, n); - } - }, $o = class { - constructor(t, r) { - var _a2; - this.equation = r; - this.hasEllipsis = false, this.symbolToInfo = /* @__PURE__ */ new Map(), this.lhs = new Array(), this.outputDims = []; - let [n, o] = r.includes("->") ? r.split("->", 2) : [r, ""]; - if (!n.match(RegExp(ff))) throw new Error("Invalid LHS term"); - if (n.split(",").forEach((d, l) => { - let c = t[l].dims.slice(); - if (!d.match(RegExp(nd))) throw new Error("Invalid LHS term"); - let m = this.processTerm(d, true, c, l); - this.lhs.push(m); - }), o === "") o += [...this.symbolToInfo.entries()].filter(([d, l]) => l.count === 1 || d === "...").map(([d]) => d).join(""); - else if (!o.match(RegExp(rn))) throw new Error("Invalid RHS"); - (_a2 = o.match(RegExp(_o, "g"))) == null ? void 0 : _a2.forEach((d) => { - if (d === "...") this.outputDims = this.outputDims.concat(this.ellipsisDims); - else { - let l = this.symbolToInfo.get(d); - if (l === void 0) throw new Error("Invalid RHS symbol"); - this.outputDims.push(l.dimValue); - } - }), this.rhs = this.processTerm(o, false, this.outputDims); - } - addSymbol(t, r, n) { - let o = this.symbolToInfo.get(t); - if (o !== void 0) { - if (o.dimValue !== r && o.count !== 1) throw new Error("Dimension mismatch"); - o.count++, o.inputIndices.push(n); - } else o = { count: 1, dimValue: r, inputIndices: [n] }; - this.symbolToInfo.set(t, o); - } - processTerm(t, r, n, o = -1) { - let i = n.length, a = false, d = [], l = 0; - if (!t.match(RegExp(nd)) && !r && t !== "") throw new Error("Invalid LHS term"); - let c = t.match(RegExp(_o, "g")), m = new vo(o); - return c == null ? void 0 : c.forEach((u, h) => { - if (u === "...") { - if (a) throw new Error("Only one ellipsis is allowed per input term"); - a = true; - let w = i - c.length + 1; - if (w < 0) throw new Error("Ellipsis out of bounds"); - if (d = n.slice(l, l + w), this.hasEllipsis) { - if (this.ellipsisDims.length !== d.length || this.ellipsisDims.toString() !== d.toString()) throw new Error("Ellipsis dimensions mismatch"); - } else if (r) this.hasEllipsis = true, this.ellipsisDims = d; - else throw new Error("Ellipsis must be specified in the LHS"); - for (let g = 0; g < d.length; g++) { - let y = String.fromCharCode("0".charCodeAt(0) + g); - m.addSymbol(y, h + g), this.addSymbol(y, n[l++], o); - } - } else m.addSymbol(u, h + (this.hasEllipsis ? this.ellipsisDims.length - 1 : 0)), this.addSymbol(u, n[l++], o); - }), m; - } - }, od = (e) => e + "_max", hf = (e, t, r, n) => { - let i = e.map((m) => m.length).map((m, u) => E(`input${u}`, t, m)), a = k.size(n), d = M("output", t, n.length), l = [...r.symbolToInfo.keys()].filter((m) => !r.rhs.symbolToIndices.has(m)), c = (m) => { - let u = [], h = "var prod = 1.0;", w = "var sum = 0.0;", g = "sum += prod;", y = [], S = [], $ = [], v = [], x = r.symbolToInfo.size === r.rhs.symbolToIndices.size; - r.symbolToInfo.forEach((C, A) => { - var _a2; - if (r.rhs.symbolToIndices.has(A)) { - let P = (_a2 = r.rhs.symbolToIndices.get(A)) == null ? void 0 : _a2[0]; - P !== void 0 && r.lhs.forEach((B, N) => { - if (C.inputIndices.includes(N)) { - let W = B.symbolToIndices.get(A); - if (W === void 0) throw new Error("Invalid symbol error"); - W.forEach((K) => { - u.push(`${i[N].indicesSet(`input${N}Indices`, K, d.indicesGet("outputIndices", P))}`); - }); - } - }); - } else r.lhs.forEach((P, B) => { - if (C.inputIndices.includes(B)) { - let N = P.symbolToIndices.get(A); - if (N === void 0) throw new Error("Invalid symbol error"); - N.forEach((W) => { - y.push(`${i[B].indicesSet(`input${B}Indices`, W, `${A}`)}`); - }), v.push(`prod *= ${i[B].getByIndices(`input${B}Indices`)};`); - } - }), S.push(`for(var ${A}: u32 = 0; ${A} < uniforms.${od(A)}; ${A}++) {`), $.push("}"); - }); - let T = x ? [...u, `let sum = ${i.map((C, A) => C.getByIndices(`input${A}Indices`)).join(" * ")};`] : [...u, w, ...S, ...y, h, ...v, g, ...$]; - return ` - ${m.registerUniforms(l.map((C) => ({ name: `${od(C)}`, type: "u32" }))).registerUniform("outputSize", "u32").declareVariables(...i, d)} - - ${m.mainStart()} - ${m.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.outputSize")} - var outputIndices = ${d.offsetToIndices("global_idx")}; - ${i.map((C, A) => `var input${A}Indices: ${i[A].type.indices};`).join(` - `)} - ${T.join(` - `)}; - ${d.setByOffset("global_idx", "sum")}; - }`; - }; - return { name: "Einsum", shaderCache: { hint: r.equation, inputDependencies: e.map(() => "rank") }, getRunData: () => { - let m = l.filter((h) => r.symbolToInfo.has(h)).map((h) => { - var _a2; - return { type: 12, data: ((_a2 = r.symbolToInfo.get(h)) == null ? void 0 : _a2.dimValue) || 0 }; - }); - m.push({ type: 12, data: a }); - let u = e.map((h, w) => [...R(h)]).reduce((h, w) => h.concat(w), m); - return u.push(...R(n)), { outputs: [{ dims: n, dataType: t }], dispatchGroup: { x: Math.ceil(a / 64) }, programUniforms: u }; - }, getShaderSource: c }; - }, id = (e, t) => { - let r = new $o(e.inputs, t.equation), n = r.outputDims, o = e.inputs.map((i, a) => i.dims); - e.compute(hf(o, e.inputs[0].dataType, r, n)); - }, ad = (e) => { - let t = e.equation.replace(/\s+/g, ""); - return J({ equation: t }); - }; - }); - var gf, ud, yf, bf, dd, ld = V(() => { - "use strict"; - Q(); - ie(); - ae(); - gf = (e) => { - if (!e || e.length !== 2) throw new Error("Expand requires 2 input."); - let t = e[0].dims, r = Array.from(e[1].getBigInt64Array(), Number), n = r.length < t.length ? 0 : r.length - t.length, o = t.length < r.length ? 0 : t.length - r.length; - for (; n < r.length && o < t.length; ++n, ++o) if (r[n] !== t[o] && r[n] !== 1 && t[o] !== 1) throw new Error("Expand requires shape to be broadcastable to input"); - }, ud = (e, t) => { - let r = e.length - t.length, n = []; - for (let o = 0; o < r; ++o) n.push(e[o]); - for (let o = 0; o < t.length; ++o) n.push(t[o] === 1 ? e[o + r] : t[o]); - return n; - }, yf = (e, t) => e.length > t.length ? ud(e, t) : ud(t, e), bf = (e) => { - let t = e[0].dims, r = Array.from(e[1].getBigInt64Array(), Number), n = yf(t, r), o = e[0].dataType, i = o === 9 ? 4 : 1, a = Math.ceil(k.size(n) / i), d = (c) => { - let m = E("input", o, t.length, i), u = M("output", o, n.length, i), h; - if (o === 9) { - let w = (g, y, S = "") => ` - let outputIndices${y} = ${u.offsetToIndices(`outputOffset + ${y}u`)}; - let offset${y} = ${m.broadcastedIndicesToOffset(`outputIndices${y}`, u)}; - let index${y} = offset${y} / 4u; - let component${y} = offset${y} % 4u; - ${g}[${y}] = ${S}(${m.getByOffset(`index${y}`)}[component${y}]); - `; - h = ` - let outputOffset = global_idx * ${i}; - var data = vec4(0); - ${w("data", 0, "u32")} - ${w("data", 1, "u32")} - ${w("data", 2, "u32")} - ${w("data", 3, "u32")} - ${u.setByOffset("global_idx", "data")} - }`; - } else h = ` - let outputIndices = ${u.offsetToIndices("global_idx")}; - let inputOffset = ${m.broadcastedIndicesToOffset("outputIndices", u)}; - ${u.setByOffset("global_idx", m.getByOffset("inputOffset"))} - }`; - return ` - ${c.registerUniform("vec_size", "u32").declareVariables(m, u)} - ${c.mainStart()} - ${c.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.vec_size")} - ${h}`; - }, l = [{ type: 12, data: a }, ...R(t, n)]; - return { name: "Expand", shaderCache: { hint: `${n.length}`, inputDependencies: ["rank"] }, getShaderSource: d, getRunData: () => ({ outputs: [{ dims: n, dataType: e[0].dataType }], dispatchGroup: { x: Math.ceil(a / 64) }, programUniforms: l }) }; - }, dd = (e) => { - gf(e.inputs), e.compute(bf(e.inputs), { inputs: [0] }); - }; - }); - var wf, cd, pd = V(() => { - "use strict"; - Q(); - ie(); - ae(); - Xr(); - wf = (e) => { - let t = e[0].dataType, r = k.size(e[0].dims), n = k.size(e[1].dims), o = n % 4 === 0, i = (a) => { - let d = E("x", t, [1], 4), l = E("bias", t, [1], 4), c = M("y", t, [1], 4), m = [{ name: "output_vec_size", type: "u32" }, { name: "bias_size", type: "u32" }], u = (w) => ` - let bias${w}_offset: u32 = (global_idx * 4 + ${w}) % uniforms.bias_size; - let bias${w} = ${l.getByOffset(`bias${w}_offset / 4`)}[bias${w}_offset % 4];`, h = o ? ` - let bias = ${l.getByOffset("global_idx % (uniforms.bias_size / 4)")};` : `${u(0)}${u(1)}${u(2)}${u(3)} - let bias = ${d.type.value}(bias0, bias1, bias2, bias3);`; - return `${a.registerUniforms(m).declareVariables(d, l, c)} - - ${lo(Ee(t))} - - ${a.mainStart(At)} - ${a.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_vec_size")} - - let x = ${d.getByOffset("global_idx")}; - ${h} - let x_in = x + bias; - ${c.setByOffset("global_idx", co("x_in"))} - }`; - }; - return { name: "FastGeluWithBias", shaderCache: { hint: `${o}`, inputDependencies: ["type", "type"] }, getShaderSource: i, getRunData: (a) => ({ outputs: [{ dims: a[0].dims, dataType: a[0].dataType }], programUniforms: [{ type: 12, data: Math.ceil(r / 4) }, { type: 12, data: n }], dispatchGroup: { x: Math.ceil(r / At / 4) } }) }; - }, cd = (e) => { - e.inputs.length < 2 || k.size(e.inputs[1].dims) === 0 ? du(e) : e.compute(wf(e.inputs)); - }; - }); - var _f, vf, md, fd, hd = V(() => { - "use strict"; - Q(); - ie(); - Ie(); - ae(); - _f = (e) => { - if (!e || e.length !== 2) throw new Error("Gather requires 2 inputs."); - }, vf = (e, t) => { - let r = e[0].dims, n = e[1].dims, o = r.length, i = k.normalizeAxis(t.axis, o), a = r.slice(0); - a.splice(i, 1, ...n); - let d = r[i], l = e[0].dataType === 9 ? 4 : 1, c = Math.ceil(k.size(a) / l), m = [{ type: 12, data: c }, { type: 6, data: d }, { type: 12, data: i }, ...R(e[0].dims, e[1].dims, a)], u = (h) => { - let w = E("data", e[0].dataType, e[0].dims.length, l), g = E("inputIndices", e[1].dataType, e[1].dims.length), y = M("output", e[0].dataType, a.length, l), S = (v) => { - let x = n.length, T = `var indicesIndices${v} = ${g.type.indices}(0);`; - for (let C = 0; C < x; C++) T += `${x > 1 ? `indicesIndices${v}[${C}]` : `indicesIndices${v}`} = ${a.length > 1 ? `outputIndices${v}[uniforms.axis + ${C}]` : `outputIndices${v}`};`; - T += ` - var idx${v} = ${g.getByIndices(`indicesIndices${v}`)}; - if (idx${v} < 0) { - idx${v} = idx${v} + uniforms.axisDimLimit; - } - var dataIndices${v} : ${w.type.indices}; - `; - for (let C = 0, A = 0; C < o; C++) C === i ? (T += `${o > 1 ? `dataIndices${v}[${C}]` : `dataIndices${v}`} = u32(idx${v});`, A += x) : (T += `${o > 1 ? `dataIndices${v}[${C}]` : `dataIndices${v}`} = ${a.length > 1 ? `outputIndices${v}[${A}]` : `outputIndices${v}`};`, A++); - return T; - }, $; - if (e[0].dataType === 9) { - let v = (x, T, C = "") => ` - let outputIndices${T} = ${y.offsetToIndices(`outputOffset + ${T}u`)}; - ${S(T)}; - let offset${T} = ${w.indicesToOffset(`dataIndices${T}`)}; - let index${T} = offset${T} / 4u; - let component${T} = offset${T} % 4u; - ${x}[${T}] = ${C}(${w.getByOffset(`index${T}`)}[component${T}]); - `; - $ = ` - let outputOffset = global_idx * ${l}; - var value = vec4(0); - ${v("value", 0, "u32")} - ${v("value", 1, "u32")} - ${v("value", 2, "u32")} - ${v("value", 3, "u32")} - ${y.setByOffset("global_idx", "value")} - `; - } else $ = ` - let outputIndices = ${y.offsetToIndices("global_idx")}; - ${S("")}; - let value = ${w.getByIndices("dataIndices")}; - ${y.setByOffset("global_idx", "value")}; - `; - return ` - ${h.registerUniform("outputSize", "u32").registerUniform("axisDimLimit", "i32").registerUniform("axis", "u32").declareVariables(w, g, y)} - ${h.mainStart()} - ${h.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.outputSize")} - ${$} - }`; - }; - return { name: "Gather", shaderCache: { hint: t.cacheKey, inputDependencies: ["rank", "rank"] }, getRunData: () => ({ outputs: [{ dims: a, dataType: e[0].dataType }], dispatchGroup: { x: Math.ceil(c / 64) }, programUniforms: m }), getShaderSource: u }; - }, md = (e) => J({ axis: e.axis }), fd = (e, t) => { - let r = e.inputs; - _f(r), e.compute(vf(e.inputs, t)); - }; - }); - var $f, xf, gd, yd, bd = V(() => { - "use strict"; - Q(); - ie(); - Ie(); - ae(); - $f = (e, t) => { - if (e.length < 3 || e.length > 4) throw new Error("GatherBlockQuantized requires 3 or 4 inputs."); - let r = k.normalizeAxis(t.quantizeAxis, e[0].dims.length), n = t.blockSize, o = e[0], i = e[2], a = e.length === 4 ? e[3] : void 0; - if (i.dims.length !== o.dims.length || !o.dims.map((d, l) => l === r ? Math.ceil(d / n) === i.dims[l] : d === i.dims[l]).reduce((d, l) => d && l, true)) throw new Error("Scales must have the same rank as the input tensor and the dims should match except on gatherAxis."); - if (a) { - if (a.dataType !== o.dataType) throw new Error("Zero point must have the same data type as the input tensor."); - if (a.dims.length !== i.dims.length || !a.dims.map((d, l) => d === i.dims[l]).reduce((d, l) => d && l, true)) throw new Error("Zero point must have the same rank as the input tensor and the dims should match except on quantizeAxis."); - } - }, xf = (e, t) => { - let r = e[0].dims, n = e[1].dims, o = r.length, i = k.normalizeAxis(t.gatherAxis, o), a = k.normalizeAxis(t.quantizeAxis, o), d = r.slice(0); - d.splice(i, 1, ...n); - let l = k.size(d), c = e[2].dataType, u = e[0].dataType === 22, h = [{ type: 12, data: l }, { type: 12, data: a }, { type: 12, data: i }, { type: 12, data: t.blockSize }, ...R(...e.map((g, y) => g.dims), d)], w = (g) => { - let y = E("data", e[0].dataType, e[0].dims.length), S = E("inputIndices", e[1].dataType, e[1].dims.length), $ = E("scales", e[2].dataType, e[2].dims.length), v = e.length > 3 ? E("zeroPoint", e[3].dataType, e[3].dims.length) : void 0, x = M("output", c, d.length), T = [y, S, $]; - v && T.push(v); - let C = [{ name: "output_size", type: "u32" }, { name: "quantize_axis", type: "u32" }, { name: "gather_axis", type: "u32" }, { name: "block_size", type: "u32" }]; - return ` - ${g.registerUniforms(C).declareVariables(...T, x)} - ${g.mainStart()} - let output_indices = ${x.offsetToIndices("global_idx")}; - var indices_indices = ${S.type.indices}(0); - ${(() => n.length > 1 ? ` - for (var i: u32 = 0; i < ${n.length}; i++) { - let index = ${x.indicesGet("output_indices", "uniforms.gather_axis + i")}; - ${S.indicesSet("indices_indices", "i", "index")}; - }` : `indices_indices = ${x.indicesGet("output_indices", "uniforms.gather_axis")};`)()}; - var data_indices = ${y.type.indices}(0); - for (var i: u32 = 0; i < uniforms.gather_axis; i++) { - let index = ${x.indicesGet("output_indices", "i")}; - ${y.indicesSet("data_indices", "i", "index")}; - } - var index_from_indices = ${S.getByIndices("indices_indices")}; - if (index_from_indices < 0) { - index_from_indices += ${r[i]}; - } - ${y.indicesSet("data_indices", "uniforms.gather_axis", "u32(index_from_indices)")}; - for (var i = uniforms.gather_axis + 1; i < ${d.length}; i++) { - let index = ${x.indicesGet("output_indices", `i + ${n.length} - 1`)}; - ${y.indicesSet("data_indices", "i", "index")}; - } - let data_offset = ${y.indicesToOffset("data_indices")}; - let data_index = data_offset % 8; - // Convert 4-bit packed data to 8-bit packed data. - let packed_4bit_quantized_data = ${y.getByOffset("data_offset / 8")}; - let packed_8bit_quantized_data = (packed_4bit_quantized_data >> (4 * (data_index % 2))) & 0x0f0f0f0f; - let quantized_data_vec = ${u ? "unpack4xI8" : "unpack4xU8"}(u32(packed_8bit_quantized_data)); - let quantized_data = quantized_data_vec[data_index / 2]; - var scale_indices = data_indices; - let quantize_axis_index = ${$.indicesGet("data_indices", "uniforms.quantize_axis")} / uniforms.block_size; - ${$.indicesSet("scale_indices", "uniforms.quantize_axis", "quantize_axis_index")}; - var scale = ${$.getByIndices("scale_indices")}; - ${(() => v ? ` - let zero_point_indices = scale_indices; - let zero_point_offset = ${v.indicesToOffset("zero_point_indices")}; - let zero_point_index = zero_point_offset % 8; - let packed_4bit_zero_points = ${v.getByOffset("zero_point_offset / 8")}; - let packed_8bit_zero_points = (packed_4bit_zero_points >> (4 * (zero_point_index % 2))) & 0x0f0f0f0f; - let zero_point_vec = ${u ? "unpack4xI8" : "unpack4xU8"}(u32(packed_8bit_zero_points)); - let zero_point = zero_point_vec[zero_point_index / 2];` : "var zero_point = 0")()}; - let dequantized_data = ${Ee(c)}(quantized_data - zero_point) * scale; - ${x.setByOffset("global_idx", "dequantized_data")}; - }`; - }; - return { name: "GatherBlockQuantized", shaderCache: { hint: `${t.cacheKey};${e.filter((g, y) => y !== 1).map((g) => g.dims.join("_")).join(";")}`, inputDependencies: Array.from({ length: e.length }, (g, y) => "rank") }, getRunData: () => ({ outputs: [{ dims: d, dataType: c }], dispatchGroup: { x: Math.ceil(l / 64) }, programUniforms: h }), getShaderSource: w }; - }, gd = (e, t) => { - let r = e.inputs; - $f(r, t), e.compute(xf(e.inputs, t)); - }, yd = (e) => J({ blockSize: e.blockSize, gatherAxis: e.gatherAxis, quantizeAxis: e.quantizeAxis }); - }); - var Sf, Tf, wd, _d, vd = V(() => { - "use strict"; - Q(); - ie(); - Ie(); - ae(); - Sf = (e) => { - if (!e || e.length !== 2) throw new Error("GatherElements requires 2 inputs."); - if (e[0].dims.length < 1) throw new Error("GatherElements requires that the data input be rank >= 1."); - if (e[0].dims.length !== e[1].dims.length) throw new Error(`GatherElements requires that the data input and - indices input tensors be of same rank.`); - }, Tf = (e, t) => { - let r = e[0].dims, n = e[0].dataType, o = r.length, i = e[1].dims, a = e[1].dataType, d = k.normalizeAxis(t.axis, o), l = r[d], c = i.slice(0), m = k.size(c), u = E("input", n, o), h = E("indicesInput", a, i.length), w = M("output", n, c.length), g = [{ type: 12, data: m }, { type: 6, data: l }, { type: 12, data: d }]; - return g.push(...R(r, i, c)), { name: "GatherElements", shaderCache: { inputDependencies: ["rank", "rank"] }, getRunData: () => ({ outputs: [{ dims: c, dataType: e[0].dataType }], dispatchGroup: { x: Math.ceil(m / 64) }, programUniforms: g }), getShaderSource: ($) => ` - ${$.registerUniform("outputSize", "u32").registerUniform("axisDimLimit", "i32").registerUniform("axis", "u32").declareVariables(u, h, w)} - ${$.mainStart()} - ${$.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.outputSize")} - - let outputIndices = ${w.offsetToIndices("global_idx")}; - - var idx = ${h.getByOffset("global_idx")}; - if (idx < 0) { - idx = idx + uniforms.axisDimLimit; - } - var inputIndices = ${u.type.indices}(outputIndices); - ${u.indicesSet("inputIndices", "uniforms.axis", "u32(idx)")}; - let value = ${u.getByIndices("inputIndices")}; - - ${w.setByOffset("global_idx", "value")}; - }` }; - }, wd = (e) => J({ axis: e.axis }), _d = (e, t) => { - let r = e.inputs; - Sf(r), e.compute(Tf(e.inputs, t)); - }; - }); - var If, Cf, $d, xd, Sd = V(() => { - "use strict"; - Q(); - ie(); - ae(); - If = (e) => { - if (!e) throw new Error("Input is missing"); - if (e.length < 2 || e.length > 3) throw new Error("Invaid input number."); - if (e.length === 3 && e[2].dims.length > 2) throw new Error("Invalid input shape of C"); - if (e[0].dataType !== e[1].dataType || e.length === 3 && e[0].dataType !== e[2].dataType) throw new Error("Input types are mismatched"); - }, Cf = (e, t) => { - let r = e[0].dims.slice(), n = e[1].dims.slice(), [o, i, a] = Gr.getShapeOfGemmResult(r, t.transA, n, t.transB, e.length === 3 ? e[2].dims : void 0), d = [o, i]; - if (!d) throw new Error("Can't use gemm on the given tensors"); - let l = k.size(d), c = [{ type: 12, data: l }, { type: 12, data: o }, { type: 12, data: i }, { type: 12, data: a }, { type: 1, data: t.alpha }, { type: 1, data: t.beta }], m = ["type", "type"]; - e.length === 3 && (c.push(...R(e[2].dims)), m.push("rank")), c.push(...R(d)); - let u = (h) => { - let w = ""; - t.transA && t.transB ? w = "value += a[k * uniforms.M + m] * b[n * uniforms.K + k];" : t.transA && !t.transB ? w = "value += a[k * uniforms.M + m] * b[k * uniforms.N + n];" : !t.transA && t.transB ? w = "value += a[m * uniforms.K + k] * b[n * uniforms.K + k];" : !t.transA && !t.transB && (w = "value += a[m * uniforms.K + k] * b[k * uniforms.N + n];"); - let g = t.alpha === 1 ? "" : "value *= uniforms.alpha;", y = E("a", e[0].dataType, e[0].dims), S = E("b", e[1].dataType, e[1].dims), $ = y.type.value, v = null, x = [y, S]; - e.length === 3 && (v = E("c", e[2].dataType, e[2].dims.length), x.push(v)); - let T = M("output", e[0].dataType, d.length); - x.push(T); - let C = [{ name: "output_size", type: "u32" }, { name: "M", type: "u32" }, { name: "N", type: "u32" }, { name: "K", type: "u32" }, { name: "alpha", type: "f32" }, { name: "beta", type: "f32" }]; - return ` - ${h.registerUniforms(C).declareVariables(...x)} - - ${h.mainStart()} - ${h.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} - - let m = global_idx / uniforms.N; - let n = global_idx % uniforms.N; - - var value = ${$}(0); - for (var k: u32 = 0u; k < uniforms.K; k++) { - ${w} - } - - ${g} - ${(() => v != null ? `let cOffset = ${v.broadcastedIndicesToOffset("vec2(m, n)", T)}; value += ${$}(uniforms.beta) * ${v.getByOffset("cOffset")};` : "")()} - output[global_idx] = value; - }`; - }; - return { name: "Gemm", shaderCache: { hint: `${t.cacheKey}`, inputDependencies: m }, getRunData: () => ({ outputs: [{ dims: d, dataType: e[0].dataType }], dispatchGroup: { x: Math.ceil(l / 64) }, programUniforms: c }), getShaderSource: u }; - }, $d = (e) => { - let t = e.transA, r = e.transB, n = e.alpha, o = e.beta; - return { transA: t, transB: r, alpha: n, beta: o, cacheKey: `${e.transA};${e.transB};${e.alpha === 1}` }; - }, xd = (e, t) => { - If(e.inputs), e.compute(Cf(e.inputs, t)); - }; - }); - var Ne, Ef, Id, Td, Pf, nr, Cd, xo = V(() => { - "use strict"; - Q(); - ie(); - Ie(); - Hr(); - Kr(); - ae(); - lt(); - Ne = (e, t) => e.length > t && e[t].dims.length > 0 ? e[t] : void 0, Ef = (e, t) => { - let r = e[0], n = Ne(e, 1), o = Ne(e, 2), i = Ne(e, 3), a = Ne(e, 4), d = Ne(e, 5), l = Ne(e, 6), c = Ne(e, 7); - if (r.dims.length !== 3 && r.dims.length !== 5) throw new Error("Input query is expected to have 3 or 5 dimensions"); - let m = r.dims[0], u = r.dims[1], h = r.dims.length === 3 ? r.dims[2] : t.numHeads * r.dims[4], w = u, g = 0, y = 0, S = Math.floor(h / t.numHeads); - if (l && c && k.size(l.dims) && k.size(c.dims)) { - if (l.dims.length !== 4) throw new Error('Input "past_key" is expected to have 4 dimensions'); - if (l.dims[0] !== m || l.dims[1] !== t.numHeads || l.dims[3] !== S) throw new Error('Input "past_key" shape (batch_size, num_heads, past_sequence_length, head_size)'); - if (c.dims[0] !== m || c.dims[1] !== t.numHeads || c.dims[3] !== S) throw new Error('Input "past_value" shape (batch_size, num_heads, past_sequence_length, head_size)'); - if (l.dims[2] !== c.dims[2]) throw new Error('Input "past_key" and "past_value" shall have same dim 2 (past_sequence_length)'); - if (c.dims.length !== 4) throw new Error('Input "past_value" is expected to have 4 dimensions'); - g = l.dims[2], y = l.dims[2]; - } else if (l && k.size(l.dims) || c && k.size(c.dims)) throw new Error('Input "past_key" and "past_value" shall be both present or both absent'); - let $; - if (n && k.size(n.dims) > 0) { - if (r.dims.length !== 3) throw new Error('Input "query" is expected to have 3 dimensions when key is given'); - if (n.dims.length < 3 || n.dims.length > 5) throw new Error('Input "key" is expected to have 3, 4, or 5 dimensions'); - if (r.dims[0] !== n.dims[0]) throw new Error('Input "query" and "key" shall have same dim 0 (batch size)'); - if (n.dims.length === 3) { - if (n.dims[2] !== r.dims[2]) throw new Error('Input "query" and "key" shall have same dim 2 (hidden_size)'); - $ = 2, w = n.dims[1]; - } else if (n.dims.length === 5) { - if (n.dims[2] !== t.numHeads || n.dims[3] !== 2 || n.dims[4] !== S) throw new Error('Expect "key" shape (batch_size, kv_sequence_length, num_heads, 2, head_size) for packed kv'); - if (o) throw new Error('Expect "value" be none when "key" has packed kv format.'); - $ = 5, w = n.dims[1]; - } else { - if (n.dims[1] !== t.numHeads || n.dims[3] !== S) throw new Error('Expect "key" shape (batch_size, num_heads, kv_sequence_length, head_size) for past_key'); - $ = 0, w = n.dims[2]; - } - } else { - if (r.dims.length !== 5) throw new Error('Input "query" is expected to have 5 dimensions when key is empty'); - if (r.dims[2] !== t.numHeads || r.dims[3] !== 3) throw new Error('Expect "query" shape (batch_size, kv_sequence_length, num_heads, 3, head_size) for packed kv'); - $ = 3; - } - if (i && k.size(i.dims) > 0) { - if (i.dims.length !== 1) throw new Error('Input "bias" is expected to have 1 dimension'); - if (n && n.dims.length === 5 && n.dims[3] === 2) throw new Error("bias is not allowed for packed kv."); - } - let v = g + w, x = 0; - if (a && k.size(a.dims) > 0) { - x = 8; - let P = a.dims; - throw P.length === 1 ? P[0] === m ? x = 1 : P[0] === 3 * m + 2 && (x = 3) : P.length === 2 && P[0] === m && P[1] === v && (x = 5), x === 8 ? new Error('Input "key_padding_mask" shape shall be (batch_size) or (batch_size, total_sequence_length)') : new Error("Mask not supported"); - } - let T = false, C = h; - if (o && k.size(o.dims) > 0) { - if (o.dims.length !== 3 && o.dims.length !== 4) throw new Error('Input "value" is expected to have 3 or 4 dimensions'); - if (r.dims[0] !== o.dims[0]) throw new Error('Input "query" and "value" shall have same dim 0 (batch_size)'); - if (o.dims.length === 3) { - if (w !== o.dims[1]) throw new Error('Input "key" and "value" shall have the same dim 1 (kv_sequence_length)'); - C = o.dims[2]; - } else { - if (w !== o.dims[2]) throw new Error('Input "key" and "value" shall have the same dim 2 (kv_sequence_length)'); - C = o.dims[1] * o.dims[3], T = true; - } - } - let A = false; - if (a && k.size(a.dims) > 0) throw new Error("Key padding mask is not supported"); - if (d && k.size(d.dims) > 0) { - if (d.dims.length !== 4) throw new Error('Input "attention_bias" is expected to have 4 dimensions'); - if (d.dims[0] !== m || d.dims[1] !== t.numHeads || d.dims[2] !== u || d.dims[3] !== v) throw new Error('Expect "attention_bias" shape (batch_size, num_heads, sequence_length, total_sequence_length)'); - } - return { batchSize: m, sequenceLength: u, pastSequenceLength: g, kvSequenceLength: w, totalSequenceLength: v, maxSequenceLength: y, inputHiddenSize: 0, hiddenSize: h, vHiddenSize: C, headSize: S, vHeadSize: Math.floor(C / t.numHeads), numHeads: t.numHeads, isUnidirectional: false, pastPresentShareBuffer: false, maskFilterValue: t.maskFilterValue, maskType: x, scale: t.scale, broadcastResPosBias: A, passPastInKv: T, qkvFormat: $ }; - }, Id = (e) => J({ ...e }), Td = J({ perm: [0, 2, 1, 3] }), Pf = (e, t, r, n, o, i, a) => { - let d = [n, o, i], l = k.size(d), c = [{ type: 12, data: l }, { type: 12, data: a }, { type: 12, data: i }], m = (u) => { - let h = M("qkv_with_bias", t.dataType, d), w = E("qkv", t.dataType, d), g = E("bias", r.dataType, d), y = [{ name: "output_size", type: "u32" }, { name: "bias_offset", type: "u32" }, { name: "hidden_size", type: "u32" }]; - return ` - ${u.registerUniforms(y).declareVariables(w, g, h)} - ${u.mainStart()} - ${u.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} - let bias_offset_idx = (global_idx % uniforms.hidden_size) + uniforms.bias_offset; - - qkv_with_bias[global_idx] = qkv[global_idx] + bias[bias_offset_idx]; - }`; - }; - return e.compute({ name: "MultiHeadAttentionAddBias", shaderCache: { inputDependencies: ["type", "type"] }, getRunData: () => ({ outputs: [{ dims: d, dataType: t.dataType, gpuDataType: 0 }], dispatchGroup: { x: Math.ceil(l / 64) }, programUniforms: c }), getShaderSource: m }, { inputs: [t, r], outputs: [-1] })[0]; - }, nr = (e, t, r, n, o, i, a, d) => { - let l = i; - if (a && k.size(a.dims) > 0) { - if (n === 1) throw new Error("AddBiasReshape is not implemented. Please export your model with packed QKV or KV"); - return l = Pf(e, i, a, t, n, r * o, d), l = l.reshape([t, n, r, o]), r === 1 || n === 1 ? l : e.compute(Pe(l, Td.perm), { inputs: [l], outputs: [-1] })[0]; - } else return i.dims.length === 3 && (l = i.reshape([t, n, r, o])), r === 1 || n === 1 ? l : e.compute(Pe(l, Td.perm), { inputs: [l], outputs: [-1] })[0]; - }, Cd = (e, t) => { - let r = Ef(e.inputs, t), n = e.inputs[0], o = Ne(e.inputs, 1), i = Ne(e.inputs, 2), a = Ne(e.inputs, 3), d = Ne(e.inputs, 4), l = Ne(e.inputs, 5), c = Ne(e.inputs, 6), m = Ne(e.inputs, 7); - if (n.dims.length === 5) throw new Error("Packed QKV is not implemented"); - if ((o == null ? void 0 : o.dims.length) === 5) throw new Error("Packed KV is not implemented"); - let u = o && i && o.dims.length === 4 && i.dims.length === 4, h = nr(e, r.batchSize, r.numHeads, r.sequenceLength, r.headSize, n, a, 0); - if (u) return Wt(e, h, o, i, d, void 0, c, m, l, r, t); - if (!o || !i) throw new Error("key and value must be provided"); - let w = nr(e, r.batchSize, r.numHeads, r.kvSequenceLength, r.headSize, o, a, r.hiddenSize), g = nr(e, r.batchSize, r.numHeads, r.kvSequenceLength, r.vHeadSize, i, a, 2 * r.hiddenSize); - Wt(e, h, w, g, d, void 0, c, m, l, r, t); - }; - }); - var Ad, zf, Of, So, kd, To = V(() => { - "use strict"; - Q(); - ie(); - ae(); - Ad = (e) => Array.from(e.getBigInt64Array(), Number), zf = (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 (Ad(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"); - }, Of = (e, t) => { - let r = []; - for (let n = 0; n < e.length; ++n) r.push(e[n] * t[n]); - return r; - }, So = (e, t) => { - let r = e[0].dims, n = t ?? Ad(e[1]), o = Of(r, n), i = k.size(o), a = e[0].dataType, d = E("input", a, r.length), l = M("output", a, o.length), c = (m) => ` - const inputShape = ${d.indices(...r)}; - ${m.registerUniform("output_size", "u32").declareVariables(d, l)} - ${m.mainStart()} - ${m.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} - let output_indices = ${l.offsetToIndices("global_idx")}; - var input_indices: ${d.type.indices}; - for (var i = 0; i < ${r.length}; i++) { - let input_dim_i = ${d.indicesGet("uniforms.input_shape", "i")}; - let input_dim_value = ${l.indicesGet("output_indices", "i")} % input_dim_i; - - ${d.indicesSet("input_indices", "i", "input_dim_value")} - } - ${l.setByOffset("global_idx", d.getByIndices("input_indices"))} - }`; - return { name: "Tile", shaderCache: { hint: `${n}`, inputDependencies: ["rank"] }, getRunData: () => ({ outputs: [{ dims: o, dataType: e[0].dataType }], dispatchGroup: { x: Math.ceil(i / 64) }, programUniforms: [{ type: 12, data: i }, ...R(e[0].dims, o)] }), getShaderSource: c }; - }, kd = (e) => { - zf(e.inputs), e.compute(So(e.inputs), { inputs: [0] }); - }; - }); - var Bf, Ed, zd, Df, Pd, Od, Bd = V(() => { - "use strict"; - Q(); - ie(); - Ie(); - Kr(); - ae(); - xo(); - To(); - lt(); - Bf = (e, t) => { - let r = e[0], n = e[1], o = e[2], i = e[3], a = e[4]; - if (r.dims.length !== 3 && r.dims.length !== 5) throw new Error("Input query is expected to have 3 or 5 dimensions"); - let d = false, l = r.dims[0], c = r.dims[1], m = r.dims.length === 3 ? d ? r.dims[2] / 3 : r.dims[2] : t.numHeads * r.dims[4], u = c, h = 0, w = 0, g = Math.floor(m / t.numHeads), y = i && i.dims.length !== 0, S = a && a.dims.length !== 0, $ = true; - if (y && S) { - if (i.dims.length !== 4) throw new Error('Input "past_key" is expected to have 4 dimensions'); - if (a.dims.length !== 4) throw new Error('Input "past_value" is expected to have 4 dimensions'); - $ ? (h = i.dims[1], w = i.dims[1]) : (h = i.dims[2], w = i.dims[2]); - } else if (y || S) throw new Error('Input "past_key" and "past_value" shall be both present or both absent'); - let v; - if (n) { - if (r.dims.length !== 3) throw new Error('Input "query" is expected to have 3 dimensions when key is given'); - if (n.dims.length < 3 || n.dims.length > 5) throw new Error('Input "key" is expected to have 3, 4, or 5 dimensions'); - if (r.dims[0] !== n.dims[0]) throw new Error('Input "query" and "key" shall have same dim 0 (batch size)'); - if (n.dims.length === 3) { - if (r.dims[2] % n.dims[2] !== 0) throw new Error('Dimension 2 of "query" should be a multiple of "key"'); - v = 2, u = n.dims[1]; - } else if (n.dims.length === 5) { - if (n.dims[2] !== t.numHeads || n.dims[3] !== 2 || n.dims[4] !== g) throw new Error('Expect "key" shape (batch_size, kv_sequence_length, num_heads, 2, head_size) for packed kv'); - if (o) throw new Error('Expect "value" be none when "key" has packed kv format.'); - v = 5, u = n.dims[1]; - } else { - if (n.dims[1] !== t.numHeads || n.dims[3] !== g) throw new Error('Expect "key" shape (batch_size, num_heads, kv_sequence_length, head_size) for past_key'); - v = 0, u = n.dims[2]; - } - } else { - if (r.dims.length !== 3 && r.dims.length !== 5) throw new Error('Input "query" is expected to have 3 or 5 dimensions when key is empty'); - if (r.dims.length === 5 && (r.dims[2] !== t.numHeads || r.dims[3] !== 3)) throw new Error('Expect "query" shape (batch_size, kv_sequence_length, num_heads, 3, head_size) for packed kv'); - v = 3; - } - let x = 0, T = false, C = m; - 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 (r.dims[0] !== o.dims[0]) throw new Error('Input "query" and "value" shall have same dim 0 (batch_size)'); - if (o.dims.length === 3) { - if (u !== o.dims[1]) throw new Error('Input "key" and "value" shall have the same dim 1 (kv_sequence_length)'); - C = o.dims[2]; - } else { - if (u !== o.dims[2]) throw new Error('Input "past_key" and "past_value" shall have the same dim 2 (kv_sequence_length)'); - C = o.dims[1] * o.dims[3], T = true; - } - } - let A = h + u, P = false; - return { batchSize: l, sequenceLength: c, pastSequenceLength: h, kvSequenceLength: u, totalSequenceLength: A, maxSequenceLength: w, inputHiddenSize: 0, hiddenSize: m, vHiddenSize: C, headSize: g, vHeadSize: Math.floor(C / t.kvNumHeads), numHeads: t.numHeads, kvNumHeads: t.kvNumHeads, nReps: t.numHeads / t.kvNumHeads, pastPresentShareBuffer: false, maskType: x, scale: t.scale, broadcastResPosBias: P, passPastInKv: T, qkvFormat: v, isPastkvBSNH: $ }; - }, Ed = (e, t, r, n) => { - let o = [n.batchSize, n.totalSequenceLength, n.kvNumHeads, n.headSize], i = 4, a = k.size(o) / i, d = n.totalSequenceLength, l = M("present_kv", r, o.length, i), c = E("new_kv", e.dataType, e.dims.length, i), m = t ? E("past_kv", t.dataType, t.dims.length, i) : void 0, u = Math.ceil(n.headSize / i), h = { x: d, y: e.dims[0], z: 1 }, w = t ? ["rank", "rank"] : ["rank"], g = [{ type: 12, data: a }, { type: 12, data: n.pastSequenceLength }, { type: 12, data: n.kvSequenceLength }, { type: 12, data: n.totalSequenceLength }], y = [c]; - m ? (g.push(...R(e.dims), ...R(t.dims), ...R(o)), y.push(m)) : g.push(...R(e.dims), ...R(o)); - let S = [{ name: "output_size", type: "u32" }, { name: "past_seqlen", type: "u32" }, { name: "new_seqlen", type: "u32" }, { name: "present_seqlen", type: "u32" }], $ = ` 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];`, v = ` 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];`, x = t ? `if (s < past_seqlen) { - ${$} - } else if (s < past_seqlen + uniforms.new_seqlen) { - ${v} - }` : `if (s < past_seqlen + uniforms.new_seqlen) { - ${v} - }`, T = (C) => ` - - ${C.registerUniforms(S).declareVariables(...y, l)} - ${C.mainStart([u, n.kvNumHeads, 1])} - ${C.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} - var indices = ${l.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 = ${n.kvNumHeads}u; - let H = ${u}u; - - let present_seqlen = uniforms.present_seqlen; - let present_batch_stride = present_seqlen * num_heads * H; - var row_stride = H; - let is_bsnh = ${n.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; - ${x} - }`; - return { name: "ConcatPastNew", shaderCache: { hint: `${n.kvNumHeads}${u}${!!t}`, inputDependencies: w }, getRunData: () => ({ outputs: [{ dims: o, dataType: r }], dispatchGroup: h, programUniforms: g }), getShaderSource: T }; - }, zd = (e) => J({ ...e }), Df = J({ perm: [0, 2, 1, 3] }), Pd = (e, t, r, n, o) => { - let i = t, a = n.kvNumHeads, d = n.nReps; - return t.dims.length === 3 && n.kvSequenceLength !== 0 && (i = t.reshape([n.batchSize, n.kvSequenceLength, a, n.headSize])), r ? i = e.compute(Ed(i, r, i.dataType, n), { inputs: [i, r], outputs: [n.isPastkvBSNH ? o : -1] })[0] : i = e.compute(Ed(i, void 0, i.dataType, n), { inputs: [i], outputs: [n.isPastkvBSNH ? o : -1] })[0], d !== 1 && (i = e.compute(So([i], [1, 1, 1, d]), { inputs: [i], outputs: [-1] })[0], i = i.reshape([n.batchSize, n.totalSequenceLength, a * d, n.headSize])), e.compute(Pe(i, Df.perm), { inputs: [i], outputs: [-1] })[0]; - }, Od = (e, t) => { - var _a2; - let r = Bf(e.inputs, t); - if (e.inputs[0].dims.length === 5) throw new Error("Packed QKV is not implemented"); - if (((_a2 = e.inputs[1]) == null ? void 0 : _a2.dims.length) === 5) throw new Error("Packed KV is not implemented"); - let n = nr(e, r.batchSize, r.numHeads, r.sequenceLength, r.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, a = Pd(e, e.inputs[1], o, r, 1), d = Pd(e, e.inputs[2], i, r, 2); - Wt(e, n, a, d, void 0, void 0, void 0, void 0, void 0, r, t); - }; - }); - var Dd, Mf, Rf, Md, Rd = V(() => { - "use strict"; - Q(); - ie(); - lt(); - ae(); - Dd = (e, t, r, n, o, i, a, d) => { - let l = we(i), c = l === 1 ? "f32" : `vec${l}f`, m = l === 1 ? "vec2f" : `mat2x${l}f`, u = o * a, h = [o, a, i / l], w = [o, a, 2], g = ["rank", "type", "type"], y = []; - y.push(...R(h, w)); - let S = ($) => { - let v = E("x", t.dataType, 3, l), x = E("scale", r.dataType, r.dims), T = E("bias", n.dataType, n.dims), C = M("output", 1, 3, 2), A = [v, x, T, C], P = 64; - return ` - var workgroup_shared : array<${m}, ${P}>; - const workgroup_size = ${P}u; - ${$.declareVariables(...A)} - ${$.mainStart(P)} - let batch = workgroup_index / uniforms.x_shape[1]; - let channel = workgroup_index % uniforms.x_shape[1]; - let hight = uniforms.x_shape[2]; - // initialize workgroup memory - var sum = ${c}(0); - var squared_sum = ${c}(0); - for (var h = local_idx; h < hight; h += workgroup_size) { - let value = ${c}(${v.get("batch", "channel", "h")}); - sum += value; - squared_sum += value * value; - } - workgroup_shared[local_idx] = ${m}(sum, squared_sum); - workgroupBarrier(); - - for (var currSize = workgroup_size >> 1; currSize > 0; currSize = currSize >> 1) { - if (local_idx < currSize) { - workgroup_shared[local_idx] = workgroup_shared[local_idx] + workgroup_shared[local_idx + currSize]; - } - workgroupBarrier(); - } - if (local_idx == 0) { - let sum_final = ${Ze("workgroup_shared[0][0]", l)} / f32(hight * ${l}); - let squared_sum_final = ${Ze("workgroup_shared[0][1]", l)} / f32(hight * ${l}); - - let inv_std_dev = inverseSqrt(squared_sum_final - sum_final * sum_final + f32(${d})); - let channel_scale = inv_std_dev * f32(scale[channel]); - let channel_shift = f32(bias[channel]) - sum_final * channel_scale; - output[workgroup_index] = vec2f(channel_scale, channel_shift); - } - }`; - }; - return e.compute({ name: "InstanceNormComputeChannelScaleShift", shaderCache: { hint: `${l};${d}`, inputDependencies: g }, getRunData: () => ({ outputs: [{ dims: w, dataType: 1 }], dispatchGroup: { x: u }, programUniforms: y }), getShaderSource: S }, { inputs: [t, r, n], outputs: [-1] })[0]; - }, Mf = (e, t, r) => { - let n = t[0].dims, o = n, i = 2, a = n[0], d = n[1], l = k.sizeFromDimension(n, i), c = we(l), m = k.size(o) / c, u = Dd(e, t[0], t[1], t[2], a, l, d, r.epsilon), h = [a, d, l / c], w = [a, d], g = ["type", "none"], y = (S) => { - let $ = E("x", t[0].dataType, h.length, c), v = E("scale_shift", 1, w.length, 2), x = M("output", t[0].dataType, h.length, c), T = [$, v, x]; - return ` - ${S.registerUniform("output_size", "u32").declareVariables(...T)} - ${S.mainStart()} - ${S.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} - let outputIndices = ${x.offsetToIndices("global_idx")}; - let batch = outputIndices[0]; - let channel = outputIndices[1]; - let scale_shift = ${v.getByIndices("vec2(batch, channel)")}; - let value = ${$.getByOffset("global_idx")} * ${x.type.value}(scale_shift.x) + ${x.type.value}(scale_shift.y); - ${x.setByOffset("global_idx", "value")}; - }`; - }; - e.compute({ name: "InstanceNormalization", shaderCache: { hint: `${c}`, inputDependencies: g }, getRunData: () => ({ outputs: [{ dims: o, dataType: t[0].dataType }], dispatchGroup: { x: Math.ceil(m / 64) }, programUniforms: [{ type: 12, data: m }, ...R(h, w, h)] }), getShaderSource: y }, { inputs: [t[0], u] }); - }, Rf = (e, t, r) => { - let n = t[0].dims, o = n, i = n[0], a = n[n.length - 1], d = k.sizeFromDimension(n, 1) / a, l = we(a), c = k.size(o) / l, m = [{ type: 12, data: d }, { type: 12, data: Math.floor(a / l) }], u = ["type", "type"], h = [0, n.length - 1]; - for (let S = 0; S < n.length - 2; S++) h.push(S + 1); - let w = e.compute(Pe(e.inputs[0], h), { inputs: [e.inputs[0]], outputs: [-1] })[0], g = Dd(e, w, t[1], t[2], i, d, a, r.epsilon), y = (S) => { - let $ = he(t[0].dataType), v = l === 1 ? "vec2f" : `mat${l}x2f`, x = (A) => { - let P = A === 0 ? "x" : "y", B = l === 1 ? "f32" : `vec${l}f`; - switch (l) { - case 1: - return `${$}(${B}(scale.${P}))`; - case 2: - return `vec2<${$}>(${B}(scale[0].${P}, scale[1].${P}))`; - case 4: - return `vec4<${$}>(${B}(scale[0].${P}, scale[1].${P}, scale[2].${P}, scale[3].${P}))`; - default: - throw new Error(`Not supported compoents ${l}`); - } - }, T = E("input", t[0].dataType, t[0].dims, l), C = M("output", t[0].dataType, o, l); - return ` - @group(0) @binding(0) var input : array<${T.type.storage}>; - @group(0) @binding(1) var scale_input : array<${v}>; - @group(0) @binding(2) var output : array<${C.type.storage}>; - struct Uniforms {H: u32, C : u32}; - @group(0) @binding(3) var uniforms: Uniforms; - - ${S.mainStart()} - let current_image_number = global_idx / (uniforms.C * uniforms.H); - let current_channel_number = global_idx % uniforms.C; - - let scale_offset = current_image_number * uniforms.C + current_channel_number; - let scale = scale_input[scale_offset]; - output[global_idx] = fma(input[global_idx], ${x(0)}, ${x(1)}); - }`; - }; - e.compute({ name: "InstanceNormalizationNHWC", shaderCache: { hint: `${l}`, inputDependencies: u }, getRunData: () => ({ outputs: [{ dims: o, dataType: t[0].dataType }], dispatchGroup: { x: Math.ceil(c / 64) }, programUniforms: m }), getShaderSource: y }, { inputs: [t[0], g] }); - }, Md = (e, t) => { - t.format === "NHWC" ? Rf(e, e.inputs, t) : Mf(e, e.inputs, t); - }; - }); - var Uf, Vf, Ud, Vd = V(() => { - "use strict"; - Q(); - ie(); - ae(); - Uf = (e) => { - if (!e || e.length < 2) throw new Error("layerNorm requires at least 2 inputs."); - }, Vf = (e, t, r) => { - let n = t.simplified, o = e[0].dims, i = e[1], a = !n && e[2], d = o, l = k.normalizeAxis(t.axis, o.length), c = k.sizeToDimension(o, l), m = k.sizeFromDimension(o, l), u = k.size(i.dims), h = a ? k.size(a.dims) : 0; - if (u !== m || a && h !== m) throw new Error(`Size of X.shape()[axis:] == ${m}. - Size of scale and bias (if provided) must match this. - Got scale size of ${u} and bias size of ${h}`); - let w = []; - for (let C = 0; C < o.length; ++C) C < l ? w.push(o[C]) : w.push(1); - let g = we(m), y = ["type", "type"], S = [{ type: 12, data: c }, { type: 1, data: m }, { type: 12, data: Math.floor(m / g) }, { type: 1, data: t.epsilon }]; - a && y.push("type"); - let $ = r > 1, v = r > 2, x = (C) => { - let A = he(e[0].dataType), P = [E("x", e[0].dataType, e[0].dims, g), E("scale", i.dataType, i.dims, g)]; - a && P.push(E("bias", a.dataType, a.dims, g)), P.push(M("output", e[0].dataType, d, g)), $ && P.push(M("mean_data_output", 1, w)), v && P.push(M("inv_std_output", 1, w)); - let B = [{ name: "norm_count", type: "u32" }, { name: "norm_size", type: "f32" }, { name: "norm_size_vectorized", type: "u32" }, { name: "epsilon", type: "f32" }]; - return ` - ${C.registerUniforms(B).declareVariables(...P)} - ${C.mainStart()} - ${C.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.norm_count")} - let offset = global_idx * uniforms.norm_size_vectorized; - var mean_vector = ${io("f32", g)}; - var mean_square_vector = ${io("f32", g)}; - - for (var h: u32 = 0u; h < uniforms.norm_size_vectorized; h++) { - let value = ${kt(A, g, "x[h + offset]")}; - mean_vector += value; - mean_square_vector += value * value; - } - let mean = ${Ze("mean_vector", g)} / uniforms.norm_size; - let inv_std_dev = inverseSqrt(${Ze("mean_square_vector", g)} / uniforms.norm_size ${n ? "" : "- mean * mean"} + uniforms.epsilon); - - for (var j: u32 = 0; j < uniforms.norm_size_vectorized; j++) { - let f32input = ${kt(A, g, "x[j + offset]")}; - let f32scale = ${kt(A, g, "scale[j]")}; - output[j + offset] = ${P[0].type.value}((f32input ${n ? "" : "- mean"}) * inv_std_dev * f32scale - ${a ? `+ ${kt(A, g, "bias[j]")}` : ""} - ); - } - - ${$ ? "mean_data_output[global_idx] = mean" : ""}; - ${v ? "inv_std_output[global_idx] = inv_std_dev" : ""}; - }`; - }, T = [{ dims: d, dataType: e[0].dataType }]; - return $ && T.push({ dims: w, dataType: 1 }), v && T.push({ dims: w, dataType: 1 }), { name: "LayerNormalization", shaderCache: { hint: `${g};${r};${n}`, inputDependencies: y }, getRunData: () => ({ outputs: T, dispatchGroup: { x: Math.ceil(c / 64) }, programUniforms: S }), getShaderSource: x }; - }, Ud = (e, t) => { - Uf(e.inputs), e.compute(Vf(e.inputs, t, e.outputCount)); - }; - }); - var Nf, Wf, Lf, Nd, Wd, Ld = V(() => { - "use strict"; - Q(); - ie(); - Ie(); - ae(); - Nf = (e, t) => { - if (e.length < 3 || e.length > 4) throw new Error("MatMulNBits requires 3 or 4 inputs"); - let r = e[0], n = r.dims.length; - if (r.dims[n - 1] !== t.k) throw new Error("The last dim of input shape does not match the k value"); - let o = Math.floor((t.k + t.blockSize - 1) / t.blockSize), i = t.blockSize / 8 * t.bits, a = e[1]; - if (!k.areEqual(a.dims, [t.n, o, i])) throw new Error("The second inputs must be 3D tensor with shape N X nBlocksPerCol X blobSize"); - let l = e[2].dims; - if (k.size(l) !== t.n * o) throw new Error("scales input size error."); - if (e.length === 4) { - let m = e[3].dims, u = t.bits > 4 ? t.n * o : t.n * Math.floor((o + 1) / 2); - if (k.size(m) !== u) throw new Error("zeroPoints input size error."); - } - }, Wf = (e, t) => { - let r = e[0].dims, n = r.length, o = r[n - 2], i = t.k, a = t.n, d = r.slice(0, n - 2), l = k.size(d), m = e[1].dims[2] / 4, u = e[0].dataType, h = we(t.k), w = we(m), g = we(a), y = d.concat([o, a]), S = o > 1 && a / g % 2 === 0 ? 2 : 1, $ = k.size(y) / g / S, v = 64, x = [], T = [l, o, i / h], C = k.convertShape(e[1].dims).slice(); - C.splice(-1, 1, m / w), x.push(...R(T)), x.push(...R(C)), x.push(...R(e[2].dims)), e.length === 4 && x.push(...R(k.convertShape(e[3].dims))); - let A = [l, o, a / g]; - x.push(...R(A)); - let P = (B) => { - let N = T.length, W = E("a", e[0].dataType, N, h), K = E("b", 12, C.length, w), Z = E("scales", e[2].dataType, e[2].dims.length), ee = [W, K, Z], se = e.length === 4 ? E("zero_points", 12, e[3].dims.length) : void 0; - se && ee.push(se); - let de = A.length, Y = M("output", e[0].dataType, de, g), le = he(e[0].dataType), ce = (() => { - switch (h) { - case 1: - return `array<${le}, 8>`; - case 2: - return `mat4x2<${le}>`; - case 4: - return `mat2x4<${le}>`; - default: - throw new Error(`${h}-component is not supported.`); - } - })(), q = () => { - let ne = ` - // reuse a data - var input_offset = ${W.indicesToOffset(`${W.type.indices}(batch, row, word_offset)`)}; - var a_data: ${ce}; - for (var j: u32 = 0; j < ${8 / h}; j++) { - a_data[j] = ${W.getByOffset("input_offset")}; - input_offset++; - } - `; - for (let oe = 0; oe < g * S; oe++) ne += ` - b_value = ${w === 1 ? `b${oe}_data` : `b${oe}_data[i]`}; - b_value_lower = unpack4xU8(b_value & b_mask); - b_value_upper = unpack4xU8((b_value >> 4) & b_mask); - b_quantized_values = ${ce}(${Array.from({ length: 4 }, (U, G) => `${le}(b_value_lower[${G}]), ${le}(b_value_upper[${G}])`).join(", ")}); - b_dequantized_values = ${(() => h === 1 ? `${ce}(${Array.from({ length: 8 }, (U, G) => `(b_quantized_values[${G}] - ${se ? `zero_point${oe}` : "zero_point"}) * scale${oe}`).join(", ")});` : `(b_quantized_values - ${ce}(${Array(8).fill(`${se ? `zero_point${oe}` : "zero_point"}`).join(",")})) * scale${oe};`)()}; - workgroup_shared[local_id.x * ${S} + ${Math.floor(oe / g)}]${g > 1 ? `[${oe % g}]` : ""} += ${Array.from({ length: 8 / h }, (U, G) => `${h === 1 ? `a_data[${G}] * b_dequantized_values[${G}]` : `dot(a_data[${G}], b_dequantized_values[${G}])`}`).join(" + ")}; - `; - return ne; - }, ue = () => { - let ne = ` - var col_index = col * ${g}; - ${se ? ` - 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 = ${le}(8);`} - `; - for (let oe = 0; oe < g * S; oe++) ne += ` - let scale${oe} = ${Z.getByOffset("col_index * nBlocksPerCol + block")}; - ${se ? ` - zero_point_byte_count = col_index * zero_point_bytes_per_col + (block >> 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 = ${se.getByOffset("zero_point_word_index")} >> zero_point_bits_offset; - let zero_point${oe} = ${le}((zero_point_word) & 0xFu);` : ""} - col_index += 1;`; - return ne; - }, re = () => { - let ne = `col_index = col * ${g};`; - for (let oe = 0; oe < g * S; oe++) ne += ` - let b${oe}_data = ${K.getByIndices(`${K.type.indices}(col_index, block, word)`)}; - col_index += 1;`; - return ne += ` - var b_value: u32; - let b_mask: u32 = 0x0F0F0F0Fu; - var b_value_lower: vec4; - var b_value_upper: vec4; - var b_quantized_values: ${ce}; - var b_dequantized_values: ${ce};`, ne; - }; - return ` - var workgroup_shared: array<${Y.type.value}, ${S * v}>; - ${B.declareVariables(...ee, Y)} - ${B.mainStart([v, 1, 1])} - let output_indices = ${Y.offsetToIndices(`(global_idx / ${v}) * ${S}`)}; - let col = output_indices[2]; - let row = output_indices[1]; - let batch = output_indices[0]; - let nBlocksPerCol = uniforms.b_shape[1]; - - for (var block = local_id.x; block < nBlocksPerCol; block += ${v}) { - //process one block - var word_offset: u32 = block * ${t.blockSize / h}; - ${ue()} - for (var word: u32 = 0; word < ${m}; word += ${w}) { - ${re()} - for (var i: u32 = 0; i < ${w}; i++) { - ${q()} - word_offset += ${8 / h}; - } - } - } - workgroupBarrier(); - - if (local_id.x < ${S}) { - var output_value: ${Y.type.value} = ${Y.type.value}(0); - var workgroup_shared_offset: u32 = local_id.x; - for (var b: u32 = 0u; b < ${v}u; b++) { - output_value += workgroup_shared[workgroup_shared_offset]; - workgroup_shared_offset += ${S}; - } - ${Y.setByIndices(`${Y.type.indices}(batch, row, col + local_id.x)`, "output_value")}; - } - }`; - }; - return { name: "MatMulNBits", shaderCache: { hint: `${t.blockSize};${t.bits};${h};${w};${g};${S};${v}`, inputDependencies: Array(e.length).fill("rank") }, getRunData: () => ({ outputs: [{ dims: y, dataType: u }], dispatchGroup: { x: $ }, programUniforms: x }), getShaderSource: P }; - }, Lf = (e, t) => { - let r = e[0].dims, n = r.length, o = r[n - 2], i = t.k, a = t.n, d = r.slice(0, n - 2), l = k.size(d), m = e[1].dims[2] / 4, u = e[0].dataType, h = we(t.k), w = we(m), g = d.concat([o, a]), y = 128, S = a % 8 === 0 ? 8 : a % 4 === 0 ? 4 : 1, $ = y / S, v = $ * w * 8, x = v / h, T = v / t.blockSize, C = k.size(g) / S, A = [], P = [l, o, i / h], B = k.convertShape(e[1].dims).slice(); - B.splice(-1, 1, m / w), A.push(...R(P)), A.push(...R(B)), A.push(...R(e[2].dims)), e.length === 4 && A.push(...R(k.convertShape(e[3].dims))); - let N = [l, o, a]; - A.push(...R(N)); - let W = (K) => { - let Z = P.length, ee = E("a", e[0].dataType, Z, h), se = E("b", 12, B.length, w), de = E("scales", e[2].dataType, e[2].dims.length), Y = [ee, se, de], le = e.length === 4 ? E("zero_points", 12, e[3].dims.length) : void 0; - le && Y.push(le); - let ce = N.length, q = M("output", e[0].dataType, ce), ue = he(e[0].dataType), re = () => { - switch (h) { - case 1: - return ` - let a_data0 = vec4<${ue}>(sub_a[word_offset], sub_a[word_offset + 1], sub_a[word_offset + 2], sub_a[word_offset + 3]); - let a_data1 = vec4<${ue}>(sub_a[word_offset + 4], sub_a[word_offset + 5], sub_a[word_offset + 6], sub_a[word_offset + 7]);`; - case 2: - return ` - let a_data0 = vec4<${ue}>(sub_a[word_offset], sub_a[word_offset + 1]); - let a_data1 = vec4<${ue}>(sub_a[word_offset + 2], sub_a[word_offset + 3]);`; - case 4: - return ` - let a_data0 = sub_a[word_offset]; - let a_data1 = sub_a[word_offset + 1];`; - default: - throw new Error(`${h}-component is not supported.`); - } - }; - return ` - var sub_a: array<${ee.type.value}, ${x}>; - var inter_results: array, ${S}>; - ${K.declareVariables(...Y, q)} - ${K.mainStart([$, S, 1])} - let output_indices = ${q.offsetToIndices(`workgroup_index * ${S}`)}; - let col = output_indices[2]; - let row = output_indices[1]; - let batch = output_indices[0]; - let n_blocks_per_col = uniforms.b_shape[1]; - let num_tiles = (n_blocks_per_col - 1) / ${T} + 1; - - // Loop over shared dimension. - for (var tile: u32 = 0; tile < num_tiles; tile += 1) { - let a_col_start = tile * ${x}; - // load one tile A data into shared memory. - for (var a_offset = local_idx; a_offset < ${x}; a_offset += ${y}) - { - let a_col = a_col_start + a_offset; - if (a_col < uniforms.a_shape[2]) - { - sub_a[a_offset] = ${ee.getByIndices(`${ee.type.indices}(batch, row, a_col)`)}; - } else { - sub_a[a_offset] = ${ee.type.value}(0); - } - } - workgroupBarrier(); - - // each thread process one block - let b_row = col + local_id.y; - let block = tile * ${T} + local_id.x; - ${le ? ` - 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 = ${le.getByOffset("zero_point_word_index")} >> zero_point_bits_offset; - let zero_point = ${ue}((zero_point_word) & 0xFu);` : ` - // The default zero point is 8 for unsigned 4-bit quantization. - let zero_point = ${ue}(8);`} - let scale = ${de.getByOffset("b_row * n_blocks_per_col + block")}; - let b_data = ${se.getByIndices(`${se.type.indices}(b_row, block, 0)`)}; - var word_offset = local_id.x * ${t.blockSize / h}; - for (var i: u32 = 0; i < ${w}; i++) { - ${re()} - let b_value = ${w === 1 ? "b_data" : "b_data[i]"}; - let b_value_lower = unpack4xU8(b_value & 0x0F0F0F0Fu); - let b_value_upper = unpack4xU8((b_value >> 4) & 0x0F0F0F0Fu); - let b_quantized_values = mat2x4<${ue}>(${Array.from({ length: 4 }, (ne, oe) => `${ue}(b_value_lower[${oe}]), ${ue}(b_value_upper[${oe}])`).join(", ")}); - let b_dequantized_values = (b_quantized_values - mat2x4<${ue}>(${Array(8).fill("zero_point").join(",")})) * scale; - inter_results[local_id.y][local_id.x] += ${Array.from({ length: 2 }, (ne, oe) => `${`dot(a_data${oe}, b_dequantized_values[${oe}])`}`).join(" + ")}; - word_offset += ${8 / h}; - } - workgroupBarrier(); - } - - if (local_idx < ${S}) { - var output_value: ${q.type.value} = ${q.type.value}(0); - for (var b = 0u; b < ${$}; b++) { - output_value += inter_results[local_idx][b]; - } - if (col + local_idx < uniforms.output_shape[2]) - { - ${q.setByIndices(`${q.type.indices}(batch, row, col + local_idx)`, "output_value")} - } - } - }`; - }; - return { name: "BlockwiseMatMulNBits32", shaderCache: { hint: `${t.blockSize};${h};${w};${$};${S}`, inputDependencies: Array(e.length).fill("rank") }, getRunData: () => ({ outputs: [{ dims: g, dataType: u }], dispatchGroup: { x: C }, programUniforms: A }), getShaderSource: W }; - }, Nd = (e, t) => { - Nf(e.inputs, t), t.blockSize === 32 && e.adapterInfo.isVendor("intel") && e.adapterInfo.isArchitecture("gen-12lp") ? e.compute(Lf(e.inputs, t)) : e.compute(Wf(e.inputs, t)); - }, Wd = (e) => J(e); - }); - var Hf, Gf, Ff, qf, jf, Kf, Yf, Xf, Hd, Gd = V(() => { - "use strict"; - Q(); - ie(); - ae(); - Hf = (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]."); - } - }, Gf = (e, t, r) => { - let n = ""; - for (let o = t - 1; o >= 0; --o) n += ` - k = i32(${e.indicesGet("indices", o)}) - ${F("uniforms.pads", o, r)}; - if (k < 0) { - break; - } - if (k >= i32(${F("uniforms.x_shape", o, t)})) { - break; - } - offset += k * i32(${F("uniforms.x_strides", o, t)}); - `; - return ` - value = ${e.type.value}(uniforms.constant_value); - for (var i = 0; i < 1; i++) { - var offset = 0; - var k = 0; - ${n} - value = x[offset]; - } - `; - }, Ff = (e, t, r) => { - let n = ""; - for (let o = t - 1; o >= 0; --o) n += ` - k = i32(${e.indicesGet("indices", o)}) - ${F("uniforms.pads", o, r)}; - if (k < 0) { - k = -k; - } - { - let _2n_1 = 2 * (i32(${F("uniforms.x_shape", o, t)}) - 1); - k = k % _2n_1; - if(k >= i32(${F("uniforms.x_shape", o, t)})) { - k = _2n_1 - k; - } - } - offset += k * i32(${F("uniforms.x_strides", o, t)}); - `; - return ` - var offset = 0; - var k = 0; - ${n} - value = x[offset]; - `; - }, qf = (e, t, r) => { - let n = ""; - for (let o = t - 1; o >= 0; --o) n += ` - k = i32(${e.indicesGet("indices", o)}) - ${F("uniforms.pads", o, r)}; - if (k < 0) { - k = 0; - } - if (k >= i32(${F("uniforms.x_shape", o, t)})) { - k = i32(${F("uniforms.x_shape", o, t)}) - 1; - } - offset += k * i32(${F("uniforms.x_strides", o, t)}); - `; - return ` - var offset = 0; - var k = 0; - ${n} - value = x[offset]; - `; - }, jf = (e, t, r) => { - let n = ""; - for (let o = t - 1; o >= 0; --o) n += ` - k = i32(${e.indicesGet("indices", o)}) - ${F("uniforms.pads", o, r)}; - if (k < 0) { - k += i32(${F("uniforms.x_shape", o, t)}]); - } - if (k >= i32(${F("uniforms.x_shape", o, t)})) { - k -= i32(${F("uniforms.x_shape", o, t)}); - } - offset += k * i32(${F("uniforms.x_strides", o, t)}); - `; - return ` - var offset = 0; - var k = 0; - ${n} - value = x[offset]; - `; - }, Kf = (e, t, r) => { - switch (r.mode) { - case 0: - return Gf(e, t, r.pads.length); - case 1: - return Ff(e, t, r.pads.length); - case 2: - return qf(e, t, r.pads.length); - case 3: - return jf(e, t, r.pads.length); - default: - throw new Error("Invalid mode"); - } - }, Yf = (e, t) => { - let r = k.padShape(e[0].dims.slice(), t.pads), n = e[0].dims, o = k.size(r), i = [{ type: 12, data: o }, { type: 6, data: t.pads }], a = e.length >= 3 && e[2].data; - t.mode === 0 && i.push({ type: a ? e[2].dataType : 1, data: t.value }), i.push(...R(e[0].dims, r)); - let d = ["rank"], l = (c) => { - let m = M("output", e[0].dataType, r.length), u = E("x", e[0].dataType, n.length), h = u.type.value, w = Kf(m, n.length, t), g = [{ name: "output_size", type: "u32" }, { name: "pads", type: "i32", length: t.pads.length }]; - return t.mode === 0 && g.push({ name: "constant_value", type: a ? h : "f32" }), ` - ${c.registerUniforms(g).declareVariables(u, m)} - ${c.mainStart()} - ${c.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} - - let indices = ${m.offsetToIndices("global_idx")}; - - var value = ${h}(0); - ${w} - output[global_idx] = value; - }`; - }; - return { name: "Pad", shaderCache: { hint: `${t.mode}${a}`, inputDependencies: d }, getRunData: () => ({ outputs: [{ dims: r, dataType: e[0].dataType }], dispatchGroup: { x: Math.ceil(k.size(r) / 64) }, programUniforms: i }), getShaderSource: l }; - }, Xf = (e, t) => { - if (e.length > 1) { - let r = e[1].getBigInt64Array(), n = e.length >= 3 && e[2].data ? e[2].dataType === 10 ? e[2].getUint16Array()[0] : e[2].getFloat32Array()[0] : 0, o = e[0].dims.length, i = new Int32Array(2 * o).fill(0); - if (e.length >= 4) { - let d = e[3].getBigInt64Array(); - for (let l = 0; l < d.length; l++) i[Number(d[l])] = Number(r[l]), i[Number(d[l]) + o] = Number(r[l + d.length]); - } else r.forEach((d, l) => i[Number(l)] = Number(d)); - let a = []; - return i.forEach((d) => a.push(d)), { mode: t.mode, value: n, pads: a }; - } else return t; - }, Hd = (e, t) => { - Hf(e.inputs); - let r = Xf(e.inputs, t); - e.compute(Yf(e.inputs, r), { inputs: [0] }); - }; - }); - var nn, Fd, qd, jd, Kd, Zf, Qf, Yd, Xd, Zd, Qd, Jd, el, tl, rl, nl, ol, il, al, sl = V(() => { - "use strict"; - Ke(); - Q(); - ie(); - ae(); - nn = (e) => { - if (_e.webgpu.validateInputContent && (!e || e.length !== 1)) throw new Error("Pool ops requires 1 input."); - }, Fd = (e, t, r) => { - let n = t.format === "NHWC", o = e.dims.slice(); - n && o.splice(1, 0, o.pop()); - let i = Object.hasOwnProperty.call(t, "dilations"), a = t.kernelShape.slice(), d = t.strides.slice(), l = i ? t.dilations.slice() : [], c = t.pads.slice(); - Ct.adjustPoolAttributes(r, o, a, d, l, c); - let m = Ct.computePoolOutputShape(r, o, d, l, a, c, t.autoPad), u = Object.assign({}, t); - i ? Object.assign(u, { kernelShape: a, strides: d, pads: c, dilations: l, cacheKey: t.cacheKey }) : Object.assign(u, { kernelShape: a, strides: d, pads: c, cacheKey: t.cacheKey }); - let h = m.slice(); - return h.push(h.splice(1, 1)[0]), [u, n ? h : m]; - }, qd = (e, t) => { - let r = t.format === "NHWC", n = k.size(e), o = k.size(t.kernelShape), i = [{ type: 12, data: n }, { type: 12, data: o }], a = [{ name: "outputSize", type: "u32" }, { name: "kernelSize", type: "u32" }]; - if (t.kernelShape.length <= 2) { - let d = t.kernelShape[t.kernelShape.length - 1], l = t.strides[t.strides.length - 1], c = t.pads[t.pads.length / 2 - 1], m = t.pads[t.pads.length - 1], u = !!(c + m); - i.push({ type: 12, data: d }, { type: 12, data: l }, { type: 12, data: c }, { type: 12, data: m }), a.push({ name: "kw", type: "u32" }, { name: "sw", type: "u32" }, { name: "pwStart", type: "u32" }, { name: "pwEnd", type: "u32" }); - let h = false; - if (t.kernelShape.length === 2) { - let w = t.kernelShape[t.kernelShape.length - 2], g = t.strides[t.strides.length - 2], y = t.pads[t.pads.length / 2 - 2], S = t.pads[t.pads.length - 2]; - h = !!(y + S), i.push({ type: 12, data: w }, { type: 12, data: g }, { type: 12, data: y }, { type: 12, data: S }), a.push({ name: "kh", type: "u32" }, { name: "sh", type: "u32" }, { name: "phStart", type: "u32" }, { name: "phEnd", type: "u32" }); - } - return [i, a, true, u, h]; - } else { - if (r) throw new Error("Pooling with kernelShape.length > 2 is not supported for NHWC format."); - let d = k.computeStrides(t.kernelShape); - i.push({ type: 12, data: d }, { type: 12, data: t.pads }, { type: 12, data: t.strides }), a.push({ name: "kernelStrides", type: "u32", length: d.length }, { name: "pads", type: "u32", length: t.pads.length }, { name: "strides", type: "u32", length: t.strides.length }); - let l = t.pads.reduce((c, m) => c + m); - return [i, a, !!l, false, false]; - } - }, jd = (e, t, r, n, o, i, a, d, l, c, m, u) => { - let h = o.format === "NHWC", w = t.type.value, g = M("output", t.type.tensor, n); - if (o.kernelShape.length <= 2) { - let y = "", S = "", $ = "", v = r - (h ? 2 : 1); - if (m ? y = ` - for (var i: u32 = 0u; i < uniforms.kw; i++) { - xIndices[${v}] = indices[${v}] * uniforms.sw - uniforms.pwStart + i; - if (xIndices[${v}] < 0 || xIndices[${v}] - >= uniforms.x_shape[${v}]) { - pad++; - continue; - } - let x_val = x[${t.indicesToOffset("xIndices")}]; - ${i} - }` : y = ` - for (var i: u32 = 0u; i < uniforms.kw; i++) { - xIndices[${v}] = indices[${v}] * uniforms.sw - uniforms.pwStart + i; - let x_val = x[${t.indicesToOffset("xIndices")}]; - ${i} - }`, o.kernelShape.length === 2) { - let T = r - (h ? 3 : 2); - u ? S = ` - for (var j: u32 = 0u; j < uniforms.kh; j++) { - xIndices[${T}] = indices[${T}] * uniforms.sh - uniforms.phStart + j; - if (xIndices[${T}] < 0 || xIndices[${T}] >= uniforms.x_shape[${T}]) { - pad += i32(uniforms.kw); - continue; - } - ` : S = ` - for (var j: u32 = 0u; j < uniforms.kh; j++) { - xIndices[${T}] = indices[${T}] * uniforms.sh - uniforms.phStart + j; - `, $ = ` - } - `; - } - return ` - ${e.registerUniforms(l).declareVariables(t, g)} - - ${e.mainStart()} - ${e.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.outputSize")} - - let indices = ${g.offsetToIndices("global_idx")}; - var xIndices = ${g.offsetToIndices("global_idx")}; - - var value = ${w}(${d}); - var pad = 0; - ${S} - ${y} - ${$} - ${a} - - output[global_idx] = value; - }`; - } else { - if (h) throw new Error("Pooling with kernelShape.length > 2 is not supported for NHWC format."); - let y = o.kernelShape.length, S = o.pads.length, $ = ""; - return c ? $ = ` - if (xIndices[j] >= uniforms.x_shape[j]) { - pad++; - isPad = true; - break; - } - } - if (!isPad) { - let x_val = x[${t.indicesToOffset("xIndices")}]; - ${i} - }` : $ = ` - } - let x_val = x[${t.indicesToOffset("xIndices")}]; - ${i} - `, ` - ${e.registerUniforms(l).declareVariables(t, g)} - - ${e.mainStart()} - ${e.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.outputSize")} - let indices = ${g.offsetToIndices("global_idx")}; - var xIndices = ${g.offsetToIndices("global_idx")}; - - var offsets: array; - - var value = ${w}(${d}); - var pad = 0; - var isPad = false; - - for (var i: u32 = 0u; i < uniforms.kernelSize; i++) { - var offset = i; - for (var j = 0u; j < ${y - 1}u; j++) { - offsets[j] = offset / ${F("uniforms.kernelStrides", "j", y)}; - offset -= offsets[j] * ${F("uniforms.kernelStrides", "j", y)}; - } - offsets[${y - 1}] = offset; - - isPad = false; - for (var j = ${r - y}u; j < ${r}u; j++) { - xIndices[j] = indices[j] * ${F("uniforms.strides", `j - ${r - y}u`, y)} - + offsets[j - ${r - y}u] - ${F("uniforms.pads", "j - 2u", S)}; - ${$} - } - ${a} - - output[global_idx] = value; - }`; - } - }, Kd = (e) => `${e.format};${e.ceilMode};${e.autoPad};${e.kernelShape.length}`, Zf = (e) => `${Kd(e)};${e.countIncludePad}`, Qf = (e) => `${Kd(e)};${e.storageOrder};${e.dilations}`, Yd = (e) => ({ format: e.format, autoPad: ["NOTSET", "VALID", "SAME_UPPER", "SAME_LOWER"][e.auto_pad], ceilMode: e.ceil_mode, kernelShape: e.kernel_shape, strides: e.strides, pads: e.pads }), Xd = (e, t, r, n) => { - let [o, i] = Fd(t, n, r), a = E("x", t.dataType, t.dims.length), d = a.type.value, l = "value += x_val;", c = ""; - o.countIncludePad ? c += `value /= ${d}(uniforms.kernelSize);` : c += `value /= ${d}(i32(uniforms.kernelSize) - pad);`; - let [m, u, h, w, g] = qd(i, o); - m.push(...R(t.dims, i)); - let y = ["rank"]; - return { name: e, shaderCache: { hint: `${n.cacheKey};${h};${w};${g}`, inputDependencies: y }, getRunData: () => ({ outputs: [{ dims: i, dataType: t.dataType }], dispatchGroup: { x: Math.ceil(k.size(i) / 64) }, programUniforms: m }), getShaderSource: (S) => jd(S, a, t.dims.length, i.length, o, l, c, 0, u, h, w, g) }; - }, Zd = (e) => { - let t = e.count_include_pad !== 0, r = Yd(e); - if (r.ceilMode !== 0) throw new Error("using ceil() in shape computation is not yet supported for AveragePool"); - let n = { countIncludePad: t, ...r, cacheKey: "" }; - return { ...n, cacheKey: Zf(n) }; - }, Qd = (e, t) => { - nn(e.inputs), e.compute(Xd("AveragePool", e.inputs[0], false, t)); - }, Jd = { autoPad: "", ceilMode: 0, countIncludePad: false, kernelShape: [], strides: [], pads: [], storageOrder: 0, dilations: [] }, el = (e) => { - let t = e.format; - return { format: t, ...Jd, cacheKey: t }; - }, tl = (e, t) => { - nn(e.inputs), e.compute(Xd("GlobalAveragePool", e.inputs[0], true, t)); - }, rl = (e, t, r, n) => { - let [o, i] = Fd(t, n, r), a = ` - value = max(x_val, value); - `, d = "", l = E("x", t.dataType, t.dims.length), c = ["rank"], [m, u, h, w, g] = qd(i, o); - return m.push(...R(t.dims, i)), { name: e, shaderCache: { hint: `${n.cacheKey};${h};${w};${g}`, inputDependencies: c }, getRunData: () => ({ outputs: [{ dims: i, dataType: t.dataType }], dispatchGroup: { x: Math.ceil(k.size(i) / 64) }, programUniforms: m }), getShaderSource: (y) => jd(y, l, t.dims.length, i.length, o, a, d, t.dataType === 10 ? -65504 : -1e5, u, h, w, g) }; - }, nl = (e, t) => { - nn(e.inputs), e.compute(rl("MaxPool", e.inputs[0], false, t)); - }, ol = (e) => { - let t = e.storage_order, r = e.dilations, n = Yd(e); - if (t !== 0) throw new Error("column major storage order is not yet supported for MaxPool"); - if (n.ceilMode !== 0) throw new Error("using ceil() in shape computation is not yet supported for MaxPool"); - let o = { storageOrder: t, dilations: r, ...n, cacheKey: "" }; - return { ...o, cacheKey: Qf(o) }; - }, il = (e) => { - let t = e.format; - return { format: t, ...Jd, cacheKey: t }; - }, al = (e, t) => { - nn(e.inputs), e.compute(rl("GlobalMaxPool", e.inputs[0], true, t)); - }; - }); - var eh, th, ul, dl, ll = V(() => { - "use strict"; - Q(); - ie(); - Ie(); - ae(); - eh = (e, t) => { - if (e.length < 2 || e.length > 3) throw new Error("DequantizeLinear requires 2 or 3 inputs."); - if (e.length === 3 && e[1].dims === e[2].dims) throw new Error("x-scale and x-zero-point must have the same shape."); - if (e.length === 3 && e[0].dataType !== e[2].dataType) throw new Error("x and x-zero-point must have the same data type."); - if (e[0].dataType === 6 && e.length > 2) throw new Error("In the case of dequantizing int32 there is no zero point."); - if (e[1].dims.length !== 0 && e[1].dims.length !== 1 && e[1].dims.length !== e[0].dims.length) throw new Error("scale input must be a scalar, a 1D tensor, or have the same rank as the input tensor."); - if (e.length > 2) { - if (e[0].dataType !== e[2].dataType) throw new Error("x and x-zero-point must have the same data type."); - if (e[1].dims.length !== e[2].dims.length) throw new Error("scale and zero-point inputs must have the same rank."); - if (!e[1].dims.map((r, n) => r === e[2].dims[n]).reduce((r, n) => r && n, true)) 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, true)) throw new Error("For block qunatization, scale input shape to match the input shape except for the axis"); - if (e[1].dims.length !== e[0].dims.length) throw new Error("For block qunatization the scale input rank must be the same as the x rank."); - let r = e[0].dims[t.axis], n = e[1].dims[t.axis]; - if (t.blockSize < Math.ceil(r / n) || t.blockSize > Math.ceil(r / (n - 1) - 1)) throw new Error("blockSize must be with in the range [ceil(dI / Si), ceil(dI / (Si - 1) - 1)]."); - } - }, th = (e, t) => { - let r = k.normalizeAxis(t.axis, e[0].dims.length), n = e[0].dataType, o = n === 3, i = e[0].dims, a = e[1].dataType, d = k.size(i), l = n === 3 || n === 2, c = l ? [Math.ceil(k.size(e[0].dims) / 4)] : e[0].dims, m = e[1].dims, u = e.length > 2 ? e[2] : void 0, h = u ? l ? [Math.ceil(k.size(u.dims) / 4)] : u.dims : void 0, w = m.length === 0 || m.length === 1 && m[0] === 1, g = w === false && m.length === 1, y = we(d), S = w && (!l || y === 4), $ = S ? y : 1, v = S && !l ? y : 1, x = E("input", l ? 12 : n, c.length, v), T = E("scale", a, m.length), C = u ? E("zero_point", l ? 12 : n, h.length) : void 0, A = M("output", a, i.length, $), P = [x, T]; - C && P.push(C); - let B = [c, m]; - u && B.push(h); - let N = [{ type: 12, data: d / $ }, { type: 12, data: r }, { type: 12, data: t.blockSize }, ...R(...B, i)], W = (K) => { - let Z = [{ name: "output_size", type: "u32" }, { name: "axis", type: "u32" }, { name: "block_size", type: "u32" }]; - return ` - ${K.registerUniforms(Z).declareVariables(...P, A)} - ${K.mainStart()} - ${K.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} - let output_indices = ${A.offsetToIndices("global_idx")}; - - // Set input x - ${(() => l ? ` - let input = ${x.getByOffset("global_idx / 4")}; - let x_vec = ${o ? "unpack4xI8(input)" : "unpack4xU8(input)"}; - let x_value = ${$ === 1 ? "x_vec[global_idx % 4]" : "x_vec"};` : `let x_value = ${x.getByOffset("global_idx")};`)()}; - - // Set scale input - ${(() => w ? `let scale_value= ${T.getByOffset("0")}` : g ? ` - let scale_index = ${A.indicesGet("output_indices", "uniforms.axis")}; - let scale_value= ${T.getByOffset("scale_index")};` : ` - var scale_indices: ${T.type.indices} = output_indices; - let index = ${T.indicesGet("scale_indices", "uniforms.axis")} / uniforms.block_size; - ${T.indicesSet("scale_indices", "uniforms.axis", "index")}; - let scale_value= ${T.getByIndices("scale_indices")};`)()}; - - // Set zero-point input - ${(() => C ? w ? l ? ` - let zero_point_input = ${C.getByOffset("0")}; - let zero_point_vec = ${o ? "unpack4xI8(zero_point_input)" : "unpack4xU8(zero_point_input)"}; - let zero_point_value= zero_point_vec[0]` : `let zero_point_value = ${C.getByOffset("0")}` : g ? l ? ` - let zero_point_index = ${A.indicesGet("output_indices", "uniforms.axis")}; - let zero_point_input = ${C.getByOffset("zero_point_index / 4")}; - let zero_point_vec = ${o ? "unpack4xI8(zero_point_input)" : "unpack4xU8(zero_point_input)"}; - let zero_point_value = zero_point_vec[zero_point_index % 4]` : ` - let zero_point_index = ${A.indicesGet("output_indices", "uniforms.axis")}; - let zero_point_value = ${C.getByOffset("zero_point_index")};` : l ? ` - let zero_point_offset = ${T.indicesToOffset("scale_indices")}; - let zero_point_input = ${C.getByOffset("zero_point_offset / 4")}; - let zero_point_vec = ${o ? "unpack4xI8(zero_point_input)" : "unpack4xU8(zero_point_input)"}; - let zero_point_value = zero_point_vec[zero_point_offset % 4];` : `let zero_point_value = ${C.getByIndices("scale_indices")};` : `let zero_point_value = ${l ? o ? "i32" : "u32" : x.type.value}(0);`)()}; - // Compute and write output - ${A.setByOffset("global_idx", `${A.type.value}(x_value - zero_point_value) * scale_value`)}; - }`; - }; - return { name: "DequantizeLinear", shaderCache: { hint: t.cacheKey, inputDependencies: C ? ["rank", "rank", "rank"] : ["rank", "rank"] }, getShaderSource: W, getRunData: () => ({ outputs: [{ dims: i, dataType: a }], dispatchGroup: { x: Math.ceil(d / $ / 64), y: 1, z: 1 }, programUniforms: N }) }; - }, ul = (e, t) => { - eh(e.inputs, t), e.compute(th(e.inputs, t)); - }, dl = (e) => J({ axis: e.axis, blockSize: e.blockSize }); - }); - var rh, nh, cl, pl = V(() => { - "use strict"; - Ke(); - Q(); - ae(); - rh = (e, t, r) => { - let n = e === t, o = e < t && r < 0, i = e > t && r > 0; - if (n || o || i) throw new Error("Range these inputs' contents are invalid."); - }, nh = (e, t, r, n) => { - let o = Math.abs(Math.ceil((t - e) / r)), i = [o], a = o, d = [{ type: 12, data: a }, { type: n, data: e }, { type: n, data: r }, ...R(i)], l = (c) => { - let m = M("output", n, i.length), u = m.type.value, h = [{ name: "outputSize", type: "u32" }, { name: "start", type: u }, { name: "delta", type: u }]; - return ` - ${c.registerUniforms(h).declareVariables(m)} - ${c.mainStart()} - ${c.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.outputSize")} - output[global_idx] = uniforms.start + ${u}(global_idx) * uniforms.delta; - }`; - }; - return { name: "Range", shaderCache: { hint: `${n}` }, getShaderSource: l, getRunData: () => ({ outputs: [{ dims: i, dataType: n }], dispatchGroup: { x: Math.ceil(a / 64) }, programUniforms: d }) }; - }, cl = (e) => { - let t = 0, r = 0, n = 0; - e.inputs[0].dataType === 6 ? (t = e.inputs[0].getInt32Array()[0], r = e.inputs[1].getInt32Array()[0], n = e.inputs[2].getInt32Array()[0]) : e.inputs[0].dataType === 1 && (t = e.inputs[0].getFloat32Array()[0], r = e.inputs[1].getFloat32Array()[0], n = e.inputs[2].getFloat32Array()[0]), _e.webgpu.validateInputContent && rh(t, r, n), e.compute(nh(t, r, n, e.inputs[0].dataType), { inputs: [] }); - }; - }); - var oh, ih, ah, sh, uh, dh, lh, ch, ph, mh, fh, ml, hh, gh, yh, bh, wh, fl, hl, gl = V(() => { - "use strict"; - Q(); - ie(); - Ie(); - ae(); - oh = (e, t) => { - if (e.every((r) => r > 0 || (() => { - throw new Error("Resize requires scales input values to be positive"); - })), e.length > 0) { - if (t.mode === "linear") { - if (!(e.length === 2 || e.length === 3 || e.length === 4 && e[0] === 1 && e[1] === 1 || e.length === 4 && e[0] === 1 && e[3] === 1 || e.length === 5 && e[0] === 1 && e[1] === 1)) throw new Error(`For linear mode, Resize requires scales to be 2D, 3D, 4D with either two outermost or one innermost and - one outermost scale values equal to 1, or 5D with two outermost scale values equal to 1`); - } else if (t.mode === "cubic" && !(e.length === 2 || e.length === 4 && e[0] === 1 && e[1] === 1 || e.length === 4 && e[0] === 1 && e[3] === 1)) throw new Error("Resize requires scales input size to be 2 or 4 for cubic mode"); - } - }, ih = (e, t, r) => { - t.every((o) => o >= 0 && o < r || (() => { - throw new Error("Resize requires axes input values to be positive and less than rank"); - })); - let n = new Array(r).fill(1); - return t.forEach((o, i) => n[o] = e[i]), n; - }, ah = (e, t, r, n, o, i) => { - let [a, d, l] = r > 10 ? [1, 2, 3] : [-1, e.length > 1 ? 1 : -1, -1], c = e[0].dims.length; - if (a > 0 && e.length > a && e[a].dims.length > 0) e[a].getFloat32Array().forEach((m) => i.push(m)); - else if (t.coordinateTransformMode === "tf_crop_and_resize") throw new Error("Resize requires RoI input to be specified when coordinateTransformMode is tfCropAndResize"); - if (d > 0 && e.length > d && e[d].dims.length === 1 && e[d].dims[0] > 0) { - if (e[d].getFloat32Array().forEach((m) => n.push(m)), n.length !== 0 && n.length !== c && r >= 18 && n.length !== t.axes.length) throw new Error("Resize requires scales input size to be same as input rank or axes size for opset 18 and up"); - oh(n, t), t.axes.length > 0 && ih(n, t.axes, c).forEach((m, u) => n[u] = m); - } - if (l > 0 && e.length > l && e[l].dims.length === 1 && e[l].dims[0] > 0 && (e[l].getBigInt64Array().forEach((m) => o.push(Number(m))), o.length !== 0 && o.length !== c && r >= 18 && o.length !== t.axes.length)) throw new Error("Resize requires sizes input size to be same as input rank or axes size for opset 18 and up"); - if (t.axes.length > 0) { - if (n.length !== 0 && n.length !== t.axes.length) throw new Error('Resize requires "scales" input size to be of axes rank when axes attributes is specified'); - if (o.length !== 0 && o.length !== t.axes.length) throw new Error('Resize requires "sizes" input size to be of rank axes rank when axes attributes is specified'); - } - if (typeof n < "u" && typeof o < "u" && n.length > 0 && o.length > c) throw new Error("Resize requires only of scales or sizes to be specified"); - }, sh = (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`); - } - })() + "}", uh = (e, t, r) => `fn getNearestPixelFromOriginal(xOriginal: ${r}, isDownSample: bool) -> ${r} {` + (() => { - switch (e) { - case "round_prefer_ceil": - return "if (fract(xOriginal) == 0.5) { return ceil(xOriginal); } else { return round(xOriginal); }"; - case "floor": - return "return floor(xOriginal);"; - case "ceil": - return "return ceil(xOriginal);"; - case "round_prefer_floor": - return "if (fract(xOriginal) == 0.5) { return floor(xOriginal); } else { return round(xOriginal); }"; - case "simple": - default: - if (t < 11) return "if (isDownSample) { return ceil(xOriginal); } else { return xOriginal; }"; - throw new Error(`Nearest mode ${e} is not supported`); - } - })() + "}", dh = (e, t, r) => { - let n = new Array(r).fill(0).concat(new Array(r).fill(1)), o = e.length === 0 ? n : e.slice(); - return t.length > 0 ? (t.forEach((i, a) => { - n[i] = o[a], n[a + r] = o[t.length + a]; - }), n) : o; - }, lh = (e, t, r, n) => { - let o = []; - if (r.length > 0) if (n.length > 0) { - if (e.forEach((i) => o.push(i)), Math.max(...n) > e.length) throw new Error("axes is out of bound"); - n.forEach((i, a) => o[i] = r[a]); - } else r.forEach((i) => o.push(i)); - else { - if (t.length === 0) throw new Error("Resize requires either scales or sizes."); - o = e.map((i, a) => Math.round(i * t[a])); - } - return o; - }, ch = (e, t, r) => { - let n = (() => { - switch (r.keepAspectRatioPolicy) { - case "not_larger": - return r.axes.length > 0 ? Math.min(...r.axes.map((i) => t[i]), Number.MAX_VALUE) : Math.min(...t, Number.MAX_VALUE); - case "not_smaller": - return r.axes.length > 0 ? Math.max(...r.axes.map((i) => t[i]), Number.MIN_VALUE) : Math.max(...t, Number.MIN_VALUE); - default: - throw new Error(`Keep aspect ratio policy ${r.keepAspectRatioPolicy} is not supported`); - } - })(); - t.fill(1, 0, t.length); - let o = e.slice(); - return r.axes.length > 0 ? (r.axes.forEach((i) => t[i] = n), r.axes.forEach((i) => o[i] = Math.round(e[i] * t[i]))) : (t.fill(n, 0, t.length), o.forEach((i, a) => o[a] = Math.round(i * t[a]))), o; - }, ph = (e, t, r, n, o) => ` - fn calculateOriginalIndicesFromOutputIndices(output_indices: ${e.type.indices}) -> array<${e.type.value}, ${r.length}> { - var original_indices: array<${e.type.value}, ${r.length}>; - for (var i:u32 = 0; i < ${r.length}; i++) { - var output_index = ${e.indicesGet("output_indices", "i")}; - var scale = ${F("uniforms.scales", "i", n)}; - var roi_low = ${F("uniforms.roi", "i", o)}; - var roi_hi = ${F("uniforms.roi", `i + ${t.length}`, o)}; - if (scale == 1.0) { - original_indices[i] = ${e.type.value}(output_index); - } else { - var input_shape_i = ${F("uniforms.input_shape", "i", t.length)}; - var output_shape_i = ${F("uniforms.output_shape", "i", r.length)}; - original_indices[i] = getOriginalCoordinateFromResizedCoordinate(output_index, scale, output_shape_i, - input_shape_i, roi_low, roi_hi); - } - } - return original_indices; - }`, mh = (e, t, r, n, o, i, a) => ` - fn calculateInputIndicesFromOutputIndices(output_indices: ${t.type.indices}) -> ${e.type.indices} { - var input_indices: ${e.type.indices}; - for (var i:u32 = 0; i < ${n.length}; i++) { - var output_index = ${t.indicesGet("output_indices", "i")}; - var input_index: u32; - var scale = ${F("uniforms.scales", "i", o)}; - if (scale == 1.0) { - input_index = output_index; - } else { - var roi_low = ${F("uniforms.roi", "i", i)}; - var roi_hi = ${F("uniforms.roi", `i + ${r.length}`, i)}; - var input_shape_i = ${F("uniforms.input_shape", "i", r.length)}; - var output_shape_i = ${F("uniforms.output_shape", "i", n.length)}; - var original_idx = getOriginalCoordinateFromResizedCoordinate(output_index, scale, output_shape_i, - input_shape_i, roi_low, roi_hi); - if (!${a} || (original_idx >= 0 && original_idx < ${t.type.value}(input_shape_i))) { - if (original_idx < 0) { - input_index = 0; - } else if (original_idx > ${t.type.value}(input_shape_i - 1)) { - input_index = input_shape_i - 1; - } else { - input_index = u32(getNearestPixelFromOriginal(original_idx, scale < 1)); - } - } else { - input_index = u32(original_idx); - } - } - ${e.indicesSet("input_indices", "i", " input_index")} - } - return input_indices; - }`, fh = (e, t) => ` - fn checkInputIndices(input_indices: ${e.type.indices}) -> bool { - for (var i:u32 = 0; i < ${t.length}; i++) { - var input_index = ${e.indicesGet("input_indices", "i")}; - if (input_index < 0 || input_index >= ${F("uniforms.input_shape", "i", t.length)}) { - return false; - } - } - return true; - }`, ml = (e, t, r, n) => e.rank > n ? ` - ${e.indicesSet("input_indices", t, "channel")}; - ${e.indicesSet("input_indices", r, "batch")}; - ` : "", hh = (e, t, r, n, o) => { - let [a, d, l, c] = r.length === 2 ? [-1, 0, 1, -1] : [0, 2, 3, 1], m = e.type.value; - return ` - fn getInputValue(batch: u32, channel: u32, row: u32, col: u32) -> ${m} { - var input_indices: ${e.type.indices}; - ${e.indicesSet("input_indices", d, `max(0, min(row, ${r[d]} - 1))`)}; - ${e.indicesSet("input_indices", l, `max(0, min(col, ${r[l]} - 1))`)}; - ${ml(e, c, a, 2)} - return ${e.getByIndices("input_indices")}; - } - - fn bilinearInterpolation(output_indices: ${t.type.indices}) -> ${m} { - var originalIndices = calculateOriginalIndicesFromOutputIndices(output_indices); - var row:${m} = originalIndices[${d}]; - var col:${m} = originalIndices[${l}]; - ${n ? `if (row < 0 || row > (${r[d]} - 1) || col < 0 || col > (${r[l]} - 1)) { - return ${o}; - }` : ""}; - row = max(0, min(row, ${r[d]} - 1)); - col = max(0, min(col, ${r[l]} - 1)); - var row1: u32 = u32(row); - var col1: u32 = u32(col); - var row2: u32 = u32(row + 1); - var col2: u32 = u32(col + 1); - var channel: u32 = ${r.length > 2 ? `u32(originalIndices[${c}])` : "0"}; - var batch: u32 = ${r.length > 2 ? `u32(originalIndices[${a}])` : "0"}; - var x11: ${m} = getInputValue(batch, channel, row1, col1); - var x12: ${m} = getInputValue(batch, channel, row1, col2); - var x21: ${m} = getInputValue(batch, channel, row2, col1); - var x22: ${m} = getInputValue(batch, channel, row2, col2); - var dx1: ${m} = abs(row - ${m}(row1)); - var dx2: ${m} = abs(${m}(row2) - row); - var dy1: ${m} = abs(col - ${m}(col1)); - var dy2: ${m} = abs(${m}(col2) - col); - if (row1 == row2) { - dx1 = 0.5; - dx2 = 0.5; - } - if (col1 == col2) { - dy1 = 0.5; - dy2 = 0.5; - } - return (x11 * dx2 * dy2 + x12 * dx2 * dy1 + x21 * dx1 * dy2 + x22 * dx1 * dy1); - }`; - }, gh = (e, t, r, n, o, i, a, d, l, c) => { - let m = r.length === 2, u = true, [h, w] = m ? [0, 1] : u ? [2, 3] : [1, 2], g = e.type.value, y = (S) => { - let $ = S === h ? "row" : "col"; - return ` - fn ${$}CubicInterpolation(input_indices: ${e.type.indices}, output_indices: ${t.type.indices}) -> ${g} { - var output_index = ${t.indicesGet("output_indices", S)}; - var originalIdx: ${g} = getOriginalCoordinateFromResizedCoordinate(output_index, ${o[S]}, - ${n[S]}, ${r[S]}, ${i[S]}, ${i[S]} + ${r.length}); - var fractOriginalIdx: ${g} = originalIdx - floor(originalIdx); - var coefs = getCubicInterpolationCoefs(fractOriginalIdx); - - if (${d} && (originalIdx < 0 || originalIdx > (${r[S]} - 1))) { - return ${l}; - } - var data: array<${g}, 4> = array<${g}, 4>(0.0, 0.0, 0.0, 0.0); - for (var i: i32 = -1; i < 3; i++) { - var ${$}: ${g} = originalIdx + ${g}(i); - if (${$} < 0 || ${$} >= ${r[S]}) { - ${(() => c ? `coefs[i + 1] = 0.0; - continue;` : d ? `return ${l};` : `${$} = max(0, min(${$}, ${r[S]} - 1));`)()}; - } - var input_indices_copy: ${e.type.indices} = input_indices; - ${e.indicesSet("input_indices_copy", S, `u32(${$})`)}; - data[i + 1] = ${S === h ? e.getByIndices("input_indices_copy") : "rowCubicInterpolation(input_indices_copy, output_indices)"}; - } - return cubicInterpolation1D(data, coefs); - }`; - }; - return ` - ${y(h)}; - ${y(w)}; - fn getCubicInterpolationCoefs(s: ${g}) -> array<${g}, 4> { - var absS = abs(s); - var coeffs: array<${g}, 4> = array<${g}, 4>(0.0, 0.0, 0.0, 0.0); - var oneMinusAbsS: ${g} = 1.0 - absS; - var twoMinusAbsS: ${g} = 2.0 - absS; - var onePlusAbsS: ${g} = 1.0 + absS; - coeffs[0] = ((${a} * onePlusAbsS - 5 * ${a}) * onePlusAbsS + 8 * ${a}) * onePlusAbsS - 4 * ${a}; - coeffs[1] = ((${a} + 2) * absS - (${a} + 3)) * absS * absS + 1; - coeffs[2] = ((${a} + 2) * oneMinusAbsS - (${a} + 3)) * oneMinusAbsS * oneMinusAbsS + 1; - coeffs[3] = ((${a} * twoMinusAbsS - 5 * ${a}) * twoMinusAbsS + 8 * ${a}) * twoMinusAbsS - 4 * ${a}; - return coeffs; - } - - fn cubicInterpolation1D(x: array<${g}, 4>, coefs: array<${g}, 4>) -> ${g} { - var coefsSum: ${g} = coefs[0] + coefs[1] + coefs[2] + coefs[3]; - return (x[0] * coefs[0] + x[1] * coefs[1]+ x[2] * coefs[2]+ x[3] * coefs[3]) / coefsSum; - } - - fn bicubicInterpolation(output_indices: ${t.type.indices}) -> ${g} { - var input_indices: ${e.type.indices} = output_indices; - return colCubicInterpolation(input_indices, output_indices); - } - `; - }, yh = (e, t, r, n, o) => { - let [a, d, l, c, m] = r.length === 3 ? [-1, 0, 1, 2, -1] : [0, 2, 3, 4, 1], u = e.type.value; - return ` - fn getInputValue(batch: u32, channel: u32, depth:u32, height: u32, width: u32) -> ${u} { - var input_indices: ${e.type.indices}; - ${e.indicesSet("input_indices", d, `max(0, min(depth, ${r[d]} - 1))`)}; - ${e.indicesSet("input_indices", l, `max(0, min(height, ${r[l]} - 1))`)}; - ${e.indicesSet("input_indices", c, `max(0, min(width, ${r[c]} - 1))`)}; - ${ml(e, m, a, 3)} - return ${e.getByIndices("input_indices")}; - } - - fn trilinearInterpolation(output_indices: ${t.type.indices}) -> ${u} { - var originalIndices = calculateOriginalIndicesFromOutputIndices(output_indices); - var depth:${u} = originalIndices[${d}]; - var height:${u} = originalIndices[${l}]; - var width:${u} = originalIndices[${c}]; - ${n ? `if (depth < 0 || depth > (${r[d]} - 1) || height < 0 || height > (${r[l]} - 1) || width < 0 || (width > ${r[c]} - 1)) { - return ${o}; - }` : ""}; - - depth = max(0, min(depth, ${r[d]} - 1)); - height = max(0, min(height, ${r[l]} - 1)); - width = max(0, min(width, ${r[c]} - 1)); - var depth1: u32 = u32(depth); - var height1: u32 = u32(height); - var width1: u32 = u32(width); - var depth2: u32 = u32(depth + 1); - var height2: u32 = u32(height + 1); - var width2: u32 = u32(width + 1); - var channel: u32 = ${r.length > 3 ? `u32(originalIndices[${m}])` : "0"}; - var batch: u32 = ${r.length > 3 ? `u32(originalIndices[${a}])` : "0"}; - - var x111: ${u} = getInputValue(batch, channel, depth1, height1, width1); - var x112: ${u} = getInputValue(batch, channel, depth1, height1, width2); - var x121: ${u} = getInputValue(batch, channel, depth1, height2, width1); - var x122: ${u} = getInputValue(batch, channel, depth1, height2, width2); - var x211: ${u} = getInputValue(batch, channel, depth2, height1, width1); - var x212: ${u} = getInputValue(batch, channel, depth2, height1, width2); - var x221: ${u} = getInputValue(batch, channel, depth2, height2, width1); - var x222: ${u} = getInputValue(batch, channel, depth2, height2, width2); - var dx1: ${u} = abs(depth - ${u}(depth1)); - var dx2: ${u} = abs(${u}(depth2) - depth); - var dy1: ${u} = abs(height - ${u}(height1)); - var dy2: ${u} = abs(${u}(height2) - height); - var dz1: ${u} = abs(width - ${u}(width1)); - var dz2: ${u} = abs(${u}(width2) - width); - if (depth1 == depth2) { - dx1 = 0.5; - dx2 = 0.5; - } - if (height1 == height2) { - dy1 = 0.5; - dy2 = 0.5; - } - if (width1 == width2) { - dz1 = 0.5; - dz2 = 0.5; - } - return (x111 * dx2 * dy2 * dz2 + x112 * dx2 * dy2 * dz1 + x121 * dx2 * dy1 *dz2 + x122 * dx2 * dy1 * dz1 + - x211 * dx1 * dy2 * dz2 + x212 * dx1 * dy2 * dz1 + x221 * dx1 * dy1 *dz2 + x222 * dx1 * dy1 * dz1); - }`; - }, bh = (e, t, r, n, o, i) => { - let a = e.dims, d = dh(i, t.axes, a.length), l = lh(a, n, o, t.axes), c = n.slice(); - n.length === 0 && (c = a.map((v, x) => v === 0 ? 1 : l[x] / v), t.keepAspectRatioPolicy !== "stretch" && (l = ch(a, c, t))); - let m = M("output", e.dataType, l.length), u = E("input", e.dataType, a.length), h = k.size(l), w = a.length === l.length && a.every((v, x) => v === l[x]), g = t.coordinateTransformMode === "tf_crop_and_resize", y = t.extrapolationValue, S = u.type.value, $ = (v) => ` - ${w ? "" : ` - ${sh(t.coordinateTransformMode, S)}; - ${(() => { - switch (t.mode) { - case "nearest": - return ` - ${fh(u, a)}; - ${uh(t.nearestMode, r, S)}; - ${mh(u, m, a, l, c.length, d.length, g)}; - `; - case "linear": - return ` - ${ph(m, a, l, c.length, d.length)}; - ${(() => { - if (a.length === 2 || a.length === 4) return `${hh(u, m, a, g, y)}`; - if (a.length === 3 || a.length === 5) return `${yh(u, m, a, g, y)}`; - throw Error("Linear mode only supports input dims 2, 3, 4 and 5 are supported in linear mode."); - })()}; - `; - case "cubic": - return ` - ${(() => { - if (a.length === 2 || a.length === 4) return `${gh(u, m, a, l, c, d, t.cubicCoeffA, g, t.extrapolationValue, t.excludeOutside)}`; - throw Error("Cubic mode only supports input dims 2 and 4 are supported in linear mode."); - })()}; - `; - default: - throw Error("Invalid resize mode"); - } - })()}; - `} - ${v.registerUniform("output_size", "u32").registerUniform("scales", "f32", c.length).registerUniform("roi", "f32", d.length).declareVariables(u, m)} - ${v.mainStart()} - ${v.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} - ${w ? "output[global_idx] = input[global_idx];" : ` - let output_indices = ${m.offsetToIndices("global_idx")}; - var input_indices: ${u.type.indices}; - ${(() => { - switch (t.mode) { - case "nearest": - return `input_indices = calculateInputIndicesFromOutputIndices(output_indices); - if (checkInputIndices(input_indices)) { - output[global_idx] = ${u.getByIndices("input_indices")}; - } else { - output[global_idx] = ${t.extrapolationValue}; - }`; - case "linear": - return `output[global_idx] = ${a.length === 2 || a.length === 4 ? "bilinearInterpolation" : "trilinearInterpolation"}(output_indices);`; - case "cubic": - return "output[global_idx] = bicubicInterpolation(output_indices);"; - default: - throw Error(`Unsupported resize mode: ${t.mode}`); - } - })()}; - `} - }`; - return { name: "Resize", shaderCache: { hint: `${t.cacheKey}|${r}|${c.length > 0 ? c : ""}|${o.length > 0 ? o : ""}|${d.length > 0 ? d : ""}|${w}|${a}`, inputDependencies: ["rank"] }, getShaderSource: $, getRunData: () => ({ outputs: [{ dims: l, dataType: e.dataType }], dispatchGroup: { x: Math.ceil(h / 64) }, programUniforms: [{ type: 12, data: h }, { type: 1, data: c }, { type: 1, data: d }, ...R(a, l)] }) }; - }, wh = (e) => { - let t = e.customDataBuffer; - return new Uint32Array(t, t.byteOffset, 1)[0]; - }, fl = (e, t) => { - let r = [], n = [], o = [], i = wh(e); - if (t.antialias !== 0) throw Error("Only default value (0) for Antialias attribute is supported"); - ah(e.inputs, t, i, r, n, o), e.compute(bh(e.inputs[0], t, i, r, n, o), { inputs: [0] }); - }, hl = (e) => { - let t = e.antialias, r = e.axes, n = e.coordinateTransformMode, o = e.cubicCoeffA, i = e.excludeOutside !== 0, a = e.extrapolationValue, d = e.keepAspectRatioPolicy, l = e.mode, c = e.nearestMode === "" ? "simple" : e.nearestMode; - return J({ antialias: t, axes: r, coordinateTransformMode: n, cubicCoeffA: o, excludeOutside: i, extrapolationValue: a, keepAspectRatioPolicy: d, mode: l, nearestMode: c }); - }; - }); - var _h, vh, yl, bl = V(() => { - "use strict"; - Q(); - ie(); - Ie(); - ae(); - _h = (e, t) => { - let [r, n, o, i] = e, { numHeads: a, rotaryEmbeddingDim: d } = t; - if (r.dims.length !== 3 && r.dims.length !== 4) throw new Error(`Input 'x' is expected to have 3 or 4 dimensions, got ${r.dims.length}`); - if (!k.areEqual(n.dims, []) && !k.areEqual(n.dims, [1]) && n.dims.length !== 2) throw new Error(`Input 'position_ids' is expected to have 0, 1, or 2 dimensions, got ${n.dims.length}`); - if (o.dims.length !== 2) throw new Error(`Input 'cos_cache' is expected to have 2 dimensions, got ${o.dims.length}`); - if (i.dims.length !== 2) throw new Error(`Input 'sin_cache' is expected to have 2 dimensions, got ${i.dims.length}`); - if (!k.areEqual(o.dims, i.dims)) throw new Error("Inputs 'cos_cache' and 'sin_cache' are expected to have the same shape"); - if (d > 0 && a === 0) throw new Error("num_heads must be provided if rotary_embedding_dim is specified"); - let l = r.dims[0], c = r.dims[r.dims.length - 2], m = o.dims[0], u = k.sizeFromDimension(r.dims, 1) / c, h = d === 0 ? o.dims[1] * 2 : u / a; - if (d > h) throw new Error("rotary_embedding_dim must be less than or equal to head_size"); - if (n.dims.length === 2) { - if (l !== n.dims[0]) throw new Error(`Input 'position_ids' dimension 0 should be of size batch_size, got ${n.dims[0]}`); - if (c !== n.dims[1]) throw new Error(`Input 'position_ids' dimension 1 should be of size sequence_length, got ${n.dims[1]}`); - } - if (h / 2 !== o.dims[1] && d / 2 !== o.dims[1]) throw new Error(`Input 'cos_cache' dimension 1 should be same as head_size / 2 or rotary_embedding_dim / 2, got ${o.dims[1]}`); - if (c > m) throw new Error("Updating cos_cache and sin_cache in RotaryEmbedding is not currently supported"); - }, vh = (e, t) => { - let { interleaved: r, numHeads: n, rotaryEmbeddingDim: o, scale: i } = t, a = e[0].dims[0], d = k.sizeFromDimension(e[0].dims, 1), l = e[0].dims[e[0].dims.length - 2], c = d / l, m = e[2].dims[1], u = o === 0 ? m * 2 : c / n, h = new Array(a, l, c / u, u - m), w = k.computeStrides(h), g = [{ type: 1, data: i }, { type: 12, data: h }, { type: 12, data: w }, ...e[0].dims.length === 3 ? new Array({ type: 12, data: [d, c, u, 1] }) : [], ...e[0].dims.length === 4 ? new Array({ type: 12, data: [d, u, l * u, 1] }) : [], ...R(e[0].dims, e[1].dims, e[2].dims, e[3].dims, e[0].dims)], y = (S) => { - let $ = E("input", e[0].dataType, e[0].dims.length), v = E("position_ids", e[1].dataType, e[1].dims.length), x = E("cos_cache", e[2].dataType, e[2].dims.length), T = E("sin_cache", e[3].dataType, e[3].dims.length), C = M("output", e[0].dataType, e[0].dims.length); - return S.registerUniforms([{ name: "scale", type: "f32" }, { name: "global_shape", type: "u32", length: h.length }, { name: "global_strides", type: "u32", length: w.length }, { name: "input_output_strides", type: "u32", length: w.length }]), ` - ${S.declareVariables($, v, x, T, C)} - - ${S.mainStart(At)} - let half_rotary_emb_dim = uniforms.${x.name}_shape[1]; - let bsnh = global_idx / uniforms.global_strides % uniforms.global_shape; - let size = uniforms.global_shape[0] * uniforms.global_strides[0]; - ${S.guardAgainstOutOfBoundsWorkgroupSizes("size")} - - if (bsnh[3] < half_rotary_emb_dim) { - let position_ids_idx = - ${v.broadcastedIndicesToOffset("bsnh.xy", M("", v.type.tensor, 2))}; - let position_id = - u32(${v.getByOffset("position_ids_idx")}) + select(0, bsnh[1], position_ids_idx == 0); - let i = dot(bsnh, uniforms.input_output_strides) + select(0, bsnh[3], ${r}); - let j = i + select(half_rotary_emb_dim, 1, ${r}); - let re = ${$.getByOffset("i")} * ${x.get("position_id", "bsnh[3]")} - - ${$.getByOffset("j")} * ${T.get("position_id", "bsnh[3]")}; - ${C.setByOffset("i", "re")} - let im = ${$.getByOffset("i")} * ${T.get("position_id", "bsnh[3]")} + - ${$.getByOffset("j")} * ${x.get("position_id", "bsnh[3]")}; - ${C.setByOffset("j", "im")} - } else { - let k = dot(bsnh, uniforms.input_output_strides) + half_rotary_emb_dim; - ${C.setByOffset("k", $.getByOffset("k"))} - } - }`; - }; - return { name: "RotaryEmbedding", shaderCache: { hint: J({ interleaved: r }).cacheKey, inputDependencies: ["rank", "rank", "rank", "rank"] }, getShaderSource: y, getRunData: () => ({ outputs: [{ dims: e[0].dims, dataType: e[0].dataType }], dispatchGroup: { x: Math.ceil(k.size(h) / At) }, programUniforms: g }) }; - }, yl = (e, t) => { - _h(e.inputs, t), e.compute(vh(e.inputs, t)); - }; - }); - var $h, xh, wl, _l = V(() => { - "use strict"; - Q(); - ie(); - ae(); - $h = (e) => { - if (!e || e.length < 3) throw new Error("layerNorm requires at least 3 inputs."); - let t = e[0], r = e[1], n = e[2]; - if (t.dataType !== r.dataType || t.dataType !== n.dataType) throw new Error("All inputs must have the same data type"); - if (t.dims.length !== 3 && t.dims.length !== 2) throw new Error("Input must be 2D or 3D"); - if (r.dims.length !== 3 && r.dims.length !== 2) throw new Error("Skip must be 2D or 3D"); - let o = t.dims[t.dims.length - 1], i = t.dims[t.dims.length - 2]; - if (r.dims[r.dims.length - 1] !== o) throw new Error("Skip must have the same hidden size as input"); - if (r.dims[r.dims.length - 2] !== i) throw new Error("Skip must have the same sequence length as input"); - if (n.dims.length !== 1) throw new Error("Gamma must be 1D"); - if (n.dims[n.dims.length - 1] !== o) throw new Error("Gamma must have the same hidden size as input"); - if (e.length > 3) { - let a = e[3]; - if (a.dims.length !== 1) throw new Error("Beta must be 1D"); - if (a.dims[a.dims.length - 1] !== o) throw new Error("Beta must have the same hidden size as input"); - } - if (e.length > 4) { - let a = e[4]; - if (a.dims.length !== 1) throw new Error("Bias must be 1D"); - if (a.dims[a.dims.length - 1] !== o) throw new Error("Bias must have the same hidden size as input"); - } - }, xh = (e, t, r, n) => { - let o = t.simplified, i = e[0].dims, a = k.size(i), d = i, l = a, c = i.slice(-1)[0], m = n ? i.slice(0, -1).concat(1) : [], u = !o && e.length > 3, h = e.length > 4, w = n && r > 1, g = n && r > 2, y = r > 3, S = 64, $ = we(c), v = [{ type: 12, data: l }, { type: 12, data: $ }, { type: 12, data: c }, { type: 1, data: t.epsilon }], x = (C) => { - let A = [{ name: "output_size", type: "u32" }, { name: "components", type: "u32" }, { name: "hidden_size", type: "u32" }, { name: "epsilon", type: "f32" }], P = [E("x", e[0].dataType, e[0].dims, $), E("skip", e[1].dataType, e[1].dims, $), E("gamma", e[2].dataType, e[2].dims, $)]; - u && P.push(E("beta", e[3].dataType, e[3].dims, $)), h && P.push(E("bias", e[4].dataType, e[4].dims, $)), P.push(M("output", e[0].dataType, d, $)), w && P.push(M("mean_output", 1, m)), g && P.push(M("inv_std_output", 1, m)), y && P.push(M("input_skip_bias_sum", e[0].dataType, d, $)); - let B = he(e[0].dataType), N = he(1, $); - return ` - - ${C.registerUniforms(A).declareVariables(...P)} - var sum_shared : array<${N}, ${S}>; - var sum_squared_shared : array<${N}, ${S}>; - - ${C.mainStart([S, 1, 1])} - let ix = local_id.x; - let iy = global_id.x / ${S}; - - let hidden_size_vectorized: u32 = uniforms.hidden_size / uniforms.components; - var stride = hidden_size_vectorized / ${S}; - let offset = ix * stride + iy * hidden_size_vectorized; - let offset1d = stride * ix; - if (ix == ${S - 1}) { - stride = hidden_size_vectorized - stride * ix; - } - for (var i: u32 = 0; i < stride; i++) { - let skip_value = skip[offset + i]; - let bias_value = ${h ? "bias[offset1d + i]" : B + "(0.0)"}; - let input_value = x[offset + i]; - let value = input_value + skip_value + bias_value; - ${y ? "input_skip_bias_sum[offset + i] = value;" : ""} - output[offset + i] = value; - let f32_value = ${kt(B, $, "value")}; - sum_shared[ix] += f32_value; - sum_squared_shared[ix] += f32_value * f32_value; - } - workgroupBarrier(); - - var reduce_size : u32 = ${S}; - for (var curr_size = reduce_size >> 1; curr_size > 0; curr_size = reduce_size >> 1) { - reduce_size = curr_size + (reduce_size & 1); - if (ix < curr_size) { - sum_shared[ix] += sum_shared[ix + reduce_size]; - sum_squared_shared[ix] += sum_squared_shared[ix + reduce_size]; - } - workgroupBarrier(); - } - - let sum = sum_shared[0]; - let square_sum = sum_squared_shared[0]; - let mean = ${Ze("sum", $)} / f32(uniforms.hidden_size); - let inv_std_dev = inverseSqrt(${Ze("square_sum", $)} / f32(uniforms.hidden_size) ${o ? "" : "- mean * mean"} + uniforms.epsilon); - ${w ? "mean_output[global_idx] = mean;" : ""} - ${g ? "inv_std_output[global_idx] = inv_std_dev;" : ""} - - for (var i: u32 = 0; i < stride; i++) { - output[offset + i] = (output[offset + i] ${o ? "" : `- ${B}(mean)`}) * - ${B}(inv_std_dev) * gamma[offset1d + i] - ${u ? "+ beta[offset1d + i]" : ""}; - } - }`; - }, T = [{ dims: d, dataType: e[0].dataType }]; - return r > 1 && T.push({ dims: m, dataType: 1 }), r > 2 && T.push({ dims: m, dataType: 1 }), r > 3 && T.push({ dims: i, dataType: e[0].dataType }), { name: "SkipLayerNormalization", shaderCache: { hint: `${$};${w};${g};${y}`, inputDependencies: e.map((C, A) => "type") }, getShaderSource: x, getRunData: () => ({ outputs: T, dispatchGroup: { x: Math.ceil(l / c) }, programUniforms: v }) }; - }, wl = (e, t) => { - $h(e.inputs); - let n = [0]; - e.outputCount > 1 && n.push(-3), e.outputCount > 2 && n.push(-3), e.outputCount > 3 && n.push(3), e.compute(xh(e.inputs, t, e.outputCount, false), { outputs: n }); - }; - }); - var Sh, on, Th, vl, Ih, Ch, $l, xl, Sl = V(() => { - "use strict"; - Q(); - ie(); - Ie(); - ae(); - Sh = (e, t) => { - if (!e || e.length < 1) throw new Error("too few inputs"); - if (t.axes.length !== 0) { - if (t.axes.length !== t.starts.length || t.axes.length !== t.ends.length) throw new Error("axes, starts and ends must have the same length"); - } else if (t.starts.length !== t.ends.length) throw new Error("starts and ends must have the same length"); - e.slice(1).forEach((r, n) => { - if (e[n + 1].dataType !== 6 && e[n + 1].dataType !== 7) throw new Error(`Input ${n} must be an array of int32 or int64`); - }); - }, on = (e, t) => { - let r = []; - if (e.length > t) if (e[t].dataType === 7) e[t].getBigInt64Array().forEach((n) => r.push(Number(n))); - else if (e[t].dataType === 6) e[t].getInt32Array().forEach((n) => r.push(Number(n))); - else throw new Error(`Input ${t} must be an array of int32 or int64`); - return r; - }, Th = (e, t) => { - if (e.length > 1) { - let r = on(e, 1), n = on(e, 2), o = on(e, 3); - return o.length === 0 && (o = [...Array(e[0].dims.length).keys()]), J({ starts: r, ends: n, axes: o }); - } else return t; - }, vl = (e, t, r, n, o) => { - let i = e; - return e < 0 && (i += r[n[t]]), o[t] < 0 ? Math.max(0, Math.min(i, r[n[t]] - 1)) : Math.max(0, Math.min(i, r[n[t]])); - }, Ih = (e, t, r) => `fn calculateInputIndices(output_indices: ${t.type.indices}) -> ${e.type.indices} { - var input_indices: ${e.type.indices}; - var carry = 0u; - for (var i = ${r.length}; i >= 0; i--) { - let input_shape_i = ${F("uniforms.input_shape", "i", r.length)}; - let steps_i = ${F("uniforms.steps", "i", r.length)}; - let signs_i = ${F("uniforms.signs", "i", r.length)}; - let starts_i = ${F("uniforms.starts", "i", r.length)}; - var output_index = ${t.indicesGet("output_indices", "i")}; - var input_index = output_index * steps_i + starts_i + carry; - carry = input_index / input_shape_i; - input_index = input_index % input_shape_i; - if (signs_i < 0) { - input_index = input_shape_i - input_index - 1u + starts_i; - } - ${e.indicesSet("input_indices", "i", "input_index")}; - } - return input_indices; - }`, Ch = (e, t) => { - let r = e[0].dims, n = k.size(r), o = t.axes.length > 0 ? k.normalizeAxes(t.axes, r.length) : [...Array(r.length).keys()], i = on(e, 4); - i.forEach(($) => $ !== 0 || (() => { - throw new Error("step cannot be 0"); - })), i.length === 0 && (i = Array(o.length).fill(1)); - let a = t.starts.map(($, v) => vl($, v, r, o, i)), d = t.ends.map(($, v) => vl($, v, r, o, i)); - if (o.length !== a.length || o.length !== d.length) throw new Error("start, ends and axes should have the same number of elements"); - if (o.length !== r.length) for (let $ = 0; $ < r.length; ++$) o.includes($) || (a.splice($, 0, 0), d.splice($, 0, r[$]), i.splice($, 0, 1)); - let l = i.map(($) => Math.sign($)); - i.forEach(($, v, x) => { - if ($ < 0) { - let T = (d[v] - a[v]) / $, C = a[v], A = C + T * i[v]; - a[v] = A, d[v] = C, x[v] = -$; - } - }); - let c = r.slice(0); - o.forEach(($, v) => { - c[$] = Math.ceil((d[$] - a[$]) / i[$]); - }); - let m = { dims: c, dataType: e[0].dataType }, u = M("output", e[0].dataType, c.length), h = E("input", e[0].dataType, e[0].dims.length), w = k.size(c), g = [{ name: "outputSize", type: "u32" }, { name: "starts", type: "u32", length: a.length }, { name: "signs", type: "i32", length: l.length }, { name: "steps", type: "u32", length: i.length }], y = [{ type: 12, data: w }, { type: 12, data: a }, { type: 6, data: l }, { type: 12, data: i }, ...R(e[0].dims, c)], S = ($) => ` - ${$.registerUniforms(g).declareVariables(h, u)} - ${Ih(h, u, r)} - ${$.mainStart()} - ${$.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.outputSize")} - let output_indices = ${u.offsetToIndices("global_idx")}; - let input_indices = calculateInputIndices(output_indices); - ${u.setByOffset("global_idx", h.getByIndices("input_indices"))} - }`; - return { name: "Slice", shaderCache: { hint: `${l.length}_${a.length}_${i.length}`, inputDependencies: ["rank"] }, getShaderSource: S, getRunData: () => ({ outputs: [m], dispatchGroup: { x: Math.ceil(n / 64) }, programUniforms: y }) }; - }, $l = (e, t) => { - Sh(e.inputs, t); - let r = Th(e.inputs, t); - e.compute(Ch(e.inputs, r), { inputs: [0] }); - }, xl = (e) => { - let t = e.starts, r = e.ends, n = e.axes; - return J({ starts: t, ends: r, axes: n }); - }; - }); - var Ah, kh, Tl, Il, Cl = V(() => { - "use strict"; - Q(); - ie(); - Ie(); - lt(); - ae(); - Ah = (e) => { - if (!e || e.length !== 1) throw new Error("Softmax op requires 1 input."); - }, kh = (e, t) => { - let r = e.inputs[0], n = r.dims, o = k.size(n), i = 64, a = n.length, d = k.normalizeAxis(t.axis, a), l = d < n.length - 1, c, m = []; - l ? (m = Array.from({ length: a }, (P, B) => B), m[d] = a - 1, m[a - 1] = d, c = e.compute(Pe(r, m), { inputs: [r], outputs: [-1] })[0]) : c = r; - let u = c.dims, h = u[a - 1], w = o / h, g = we(h), y = h / g, S = (P, B) => B === 4 ? `max(max(${P}.x, ${P}.y), max(${P}.z, ${P}.w))` : B === 2 ? `max(${P}.x, ${P}.y)` : B === 3 ? `max(max(${P}.x, ${P}.y), ${P}.z)` : P, $ = E("x", c.dataType, c.dims, g), v = M("result", c.dataType, c.dims, g), x = $.type.value, T = he(c.dataType) === "f32" ? `var threadMax = ${x}(-3.402823e+38f);` : `var threadMax = ${x}(-65504.0h);`, C = (P) => ` - var rowMaxShared : ${x}; - var rowSumShared : ${x}; - var threadShared : array<${x}, ${i}>; - - fn getValue(row: i32, col: i32, row_stride: i32) -> ${x} { - let index = row * row_stride + col; - return x[index]; - } - - fn setValue(row: i32, col: i32, row_stride: i32, value: ${x}) { - let index = row * row_stride + col; - result[index] = value; - } - ${P.registerUniform("packedCols", "i32").declareVariables($, v)} - ${P.mainStart()} - let gindex = i32(global_idx); - let lindex = i32(local_idx); - const wg = ${i}; - let row = gindex / wg; - let cols = uniforms.packedCols; - let row_stride : i32 = uniforms.packedCols; - - // find the rows max - ${T} - for (var col = lindex; col < cols; col += wg) { - let value = getValue(row, col, row_stride); - threadMax = max(threadMax, value); - } - if (lindex < cols) { - threadShared[lindex] = threadMax; - } - workgroupBarrier(); - - var reduceSize = min(cols, wg); - for (var currSize = reduceSize >> 1; currSize > 0; currSize = reduceSize >> 1) { - reduceSize = currSize + (reduceSize & 1); - if (lindex < currSize) { - threadShared[lindex] = max(threadShared[lindex], threadShared[lindex + reduceSize]); - } - workgroupBarrier(); - } - if (lindex == 0) { - rowMaxShared = ${x}(${S("threadShared[0]", g)}); - } - workgroupBarrier(); - - // find the rows sum - var threadSum = ${x}(0.0); - for (var col = lindex; col < cols; col += wg) { - let subExp = exp(getValue(row, col, row_stride) - rowMaxShared); - threadSum += subExp; - } - threadShared[lindex] = threadSum; - workgroupBarrier(); - - for (var currSize = wg >> 1; currSize > 0; currSize = currSize >> 1) { - if (lindex < currSize) { - threadShared[lindex] = threadShared[lindex] + threadShared[lindex + currSize]; - } - workgroupBarrier(); - } - if (lindex == 0) { - rowSumShared = ${x}(${Ze("threadShared[0]", g)}); - } - workgroupBarrier(); - - // calculate final value for each element in the row - for (var col = lindex; col < cols; col += wg) { - let value = exp(getValue(row, col, row_stride) - rowMaxShared) / rowSumShared; - setValue(row, col, row_stride, value); - } - }`, A = e.compute({ name: "Softmax", shaderCache: { hint: `${g}`, inputDependencies: ["type"] }, getRunData: () => ({ outputs: [{ dims: u, dataType: c.dataType }], dispatchGroup: { x: w }, programUniforms: [{ type: 6, data: y }] }), getShaderSource: C }, { inputs: [c], outputs: [l ? -1 : 0] })[0]; - l && e.compute(Pe(A, m), { inputs: [A] }); - }, Tl = (e, t) => { - Ah(e.inputs), kh(e, t); - }, Il = (e) => J({ axis: e.axis }); - }); - var Eh, Ph, zh, Oh, Bh, Al, kl, El = V(() => { - "use strict"; - Q(); - ie(); - Ie(); - ae(); - Eh = (e) => { - if (!e || e.length < 1) throw new Error("too few inputs"); - }, Ph = (e, t) => { - let r = [], n = t.numOutputs; - return e[1].dims[0] > 0 && (e[1].getBigInt64Array().forEach((o) => r.push(Number(o))), n = r.length), J({ numOutputs: n, axis: t.axis, splitSizes: r }); - }, zh = (e) => ` - fn calculateOutputIndex(index: u32) -> u32 { - for (var i: u32 = 0u; i < ${e}u; i += 1u ) { - if (index < ${F("uniforms.size_in_split_axis", "i", e)}) { - return i; - } - } - return ${e}u; - }`, Oh = (e) => { - let t = e.length, r = []; - for (let n = 0; n < t; ++n) { - let o = e[n].setByIndices("indices", "input[global_idx]"); - t === 1 ? r.push(o) : n === 0 ? r.push(`if (output_number == ${n}u) { ${o} }`) : n === t - 1 ? r.push(`else { ${o} }`) : r.push(`else if (output_number == ${n}) { ${o} }`); - } - return ` - fn writeBufferData(output_number: u32, indices: ${e[0].type.indices}, global_idx: u32) { - ${r.join(` - `)} - }`; - }, Bh = (e, t) => { - let r = e[0].dims, n = k.size(r), o = e[0].dataType, i = k.normalizeAxis(t.axis, r.length), a = new Array(t.numOutputs), d = E("input", o, r.length), l = new Array(t.numOutputs), c = [], m = [], u = 0, h = [{ type: 12, data: n }]; - for (let g = 0; g < t.numOutputs; g++) { - u += t.splitSizes[g], l[g] = u; - let y = r.slice(); - y[i] = t.splitSizes[g], m.push(y), a[g] = M(`output${g}`, o, y.length), c.push({ dims: m[g], dataType: e[0].dataType }); - } - h.push({ type: 12, data: l }, ...R(r, ...m)); - let w = (g) => ` - ${g.registerUniform("input_size", "u32").registerUniform("size_in_split_axis", "u32", l.length).declareVariables(d, ...a)} - ${zh(l.length)} - ${Oh(a)} - - ${g.mainStart()} - ${g.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.input_size")} - - var indices = ${d.offsetToIndices("global_idx")}; - var index = ${d.indicesGet("indices", i)}; - let output_number = calculateOutputIndex(index); - if (output_number != 0) { - index -= ${F("uniforms.size_in_split_axis", "output_number - 1u", l.length)}; - ${d.indicesSet("indices", i, "index")}; - } - writeBufferData(output_number, indices, global_idx); - }`; - return { name: "Split", shaderCache: { hint: t.cacheKey, inputDependencies: ["rank"] }, getShaderSource: w, getRunData: () => ({ outputs: c, dispatchGroup: { x: Math.ceil(n / 64) }, programUniforms: h }) }; - }, Al = (e, t) => { - Eh(e.inputs); - let r = e.inputs.length === 1 ? t : Ph(e.inputs, t); - e.compute(Bh(e.inputs, r), { inputs: [0] }); - }, kl = (e) => { - let t = e.axis, r = e.splitSizes, n = e.numOutputs < 0 ? r.length : e.numOutputs; - if (n !== r.length) throw new Error("numOutputs and splitSizes lengh must be equal"); - return J({ axis: t, numOutputs: n, splitSizes: r }); - }; - }); - var Dh, Mh, Pl, zl = V(() => { - "use strict"; - Q(); - ie(); - ae(); - Dh = (e, t, r, n, o) => { - let i = M("output_data", o, r.length, 4), a = E("a_data", t[1].dataType, t[1].dims.length, 4), d = E("b_data", t[2].dataType, t[2].dims.length, 4), l = E("c_data", t[0].dataType, t[0].dims.length, 4), c, m = (u, h, w) => `select(${h}, ${u}, ${w})`; - if (!n) c = i.setByOffset("global_idx", m(a.getByOffset("global_idx"), d.getByOffset("global_idx"), l.getByOffset("global_idx"))); - else { - let u = (h, w, g = "") => { - let y = `a_data[index_a${w}][component_a${w}]`, S = `b_data[index_b${w}][component_b${w}]`, $ = `bool(c_data[index_c${w}] & (0xffu << (component_c${w} * 8)))`; - return ` - let output_indices${w} = ${i.offsetToIndices(`global_idx * 4u + ${w}u`)}; - let offset_a${w} = ${a.broadcastedIndicesToOffset(`output_indices${w}`, i)}; - let offset_b${w} = ${d.broadcastedIndicesToOffset(`output_indices${w}`, i)}; - let offset_c${w} = ${l.broadcastedIndicesToOffset(`output_indices${w}`, i)}; - let index_a${w} = offset_a${w} / 4u; - let index_b${w} = offset_b${w} / 4u; - let index_c${w} = offset_c${w} / 4u; - let component_a${w} = offset_a${w} % 4u; - let component_b${w} = offset_b${w} % 4u; - let component_c${w} = offset_c${w} % 4u; - ${h}[${w}] = ${g}(${m(y, S, $)}); - `; - }; - o === 9 ? c = ` - var data = vec4(0); - ${u("data", 0, "u32")} - ${u("data", 1, "u32")} - ${u("data", 2, "u32")} - ${u("data", 3, "u32")} - output_data[global_idx] = dot(vec4(0x1, 0x100, 0x10000, 0x1000000), vec4(data));` : c = ` - ${u("output_data[global_idx]", 0)} - ${u("output_data[global_idx]", 1)} - ${u("output_data[global_idx]", 2)} - ${u("output_data[global_idx]", 3)} - `; - } - return ` - ${e.registerUniform("vec_size", "u32").declareVariables(l, a, d, i)} - ${e.mainStart()} - ${e.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.vec_size")} - ${c} - }`; - }, Mh = (e) => { - let t = e[1].dims, r = e[2].dims, n = e[0].dims, o = e[1].dataType, i = !(k.areEqual(t, r) && k.areEqual(r, n)), a = t, d = k.size(t); - if (i) { - let c = rt.calcShape(rt.calcShape(t, r, false), n, false); - if (!c) throw new Error("Can't perform where op on the given tensors"); - a = c, d = k.size(a); - } - let l = Math.ceil(d / 4); - return { name: "Where", shaderCache: { inputDependencies: ["rank", "rank", "rank"] }, getShaderSource: (c) => Dh(c, e, a, i, o), getRunData: () => ({ outputs: [{ dims: a, dataType: o }], dispatchGroup: { x: Math.ceil(d / 64 / 4) }, programUniforms: [{ type: 12, data: l }, ...R(n, t, r, a)] }) }; - }, Pl = (e) => { - e.compute(Mh(e.inputs)); - }; - }); - var Ol, Bl = V(() => { - "use strict"; - Ts(); - Kr(); - As(); - Es(); - hu(); - Iu(); - ku(); - Hu(); - Xu(); - Ju(); - rd(); - sd(); - ld(); - pd(); - hd(); - bd(); - vd(); - Sd(); - Bd(); - Rd(); - Vd(); - fo(); - Ld(); - xo(); - Gd(); - sl(); - ll(); - pl(); - qr(); - gl(); - bl(); - _l(); - Sl(); - Cl(); - El(); - To(); - lt(); - Xr(); - zl(); - Ol = /* @__PURE__ */ new Map([["Abs", [Ps]], ["Acos", [zs]], ["Acosh", [Os]], ["Add", [gu]], ["ArgMax", [Ss, uo]], ["ArgMin", [xs, uo]], ["Asin", [Bs]], ["Asinh", [Ds]], ["Atan", [Ms]], ["Atanh", [Rs]], ["Attention", [Is]], ["AveragePool", [Qd, Zd]], ["BatchNormalization", [Cs]], ["BiasAdd", [ks]], ["BiasSplitGelu", [fu]], ["Cast", [Vs, Us]], ["Ceil", [Ws]], ["Clip", [Ns]], ["Concat", [Cu, Au]], ["Conv", [bo, yo]], ["ConvTranspose", [Yu, Ku]], ["Cos", [Ls]], ["Cosh", [Hs]], ["CumSum", [Zu, Qu]], ["DepthToSpace", [ed, td]], ["DequantizeLinear", [ul, dl]], ["Div", [yu]], ["Einsum", [id, ad]], ["Elu", [Gs, Qt]], ["Equal", [bu]], ["Erf", [Fs]], ["Exp", [qs]], ["Expand", [dd]], ["FastGelu", [cd]], ["Floor", [js]], ["FusedConv", [bo, yo]], ["Gather", [fd, md]], ["GatherElements", [_d, wd]], ["GatherBlockQuantized", [gd, yd]], ["Gelu", [Ks]], ["Gemm", [xd, $d]], ["GlobalAveragePool", [tl, el]], ["GlobalMaxPool", [al, il]], ["Greater", [$u]], ["GreaterOrEqual", [Su]], ["GroupQueryAttention", [Od, zd]], ["HardSigmoid", [ru, tu]], ["InstanceNormalization", [Md]], ["LayerNormalization", [Ud]], ["LeakyRelu", [Ys, Qt]], ["Less", [xu]], ["LessOrEqual", [Tu]], ["Log", [cu]], ["MatMul", [Wu]], ["MatMulNBits", [Nd, Wd]], ["MaxPool", [nl, ol]], ["Mul", [wu]], ["MultiHeadAttention", [Cd, Id]], ["Neg", [Zs]], ["Not", [Xs]], ["Pad", [Hd]], ["Pow", [_u]], ["QuickGelu", [pu, Qt]], ["Range", [cl]], ["Reciprocal", [Qs]], ["ReduceMin", [ys]], ["ReduceMean", [ps]], ["ReduceMax", [gs]], ["ReduceSum", [ws]], ["ReduceProd", [bs]], ["ReduceL1", [ms]], ["ReduceL2", [fs]], ["ReduceLogSum", [vs]], ["ReduceLogSumExp", [hs]], ["ReduceSumSquare", [_s]], ["Relu", [Js]], ["Resize", [fl, hl]], ["RotaryEmbedding", [yl]], ["Sigmoid", [eu]], ["Sin", [nu]], ["Sinh", [ou]], ["Slice", [$l, xl]], ["SkipLayerNormalization", [wl]], ["Split", [Al, kl]], ["Sqrt", [iu]], ["Softmax", [Tl, Il]], ["Sub", [vu]], ["Tan", [au]], ["Tanh", [uu]], ["ThresholdedRelu", [lu, Qt]], ["Tile", [kd]], ["Transpose", [Ja, es]], ["Where", [Pl]]]); - }); - var an, Dl = V(() => { - "use strict"; - Ke(); - Xe(); - ae(); - an = class { - constructor(t) { - this.backend = t; - this.repo = /* @__PURE__ */ new Map(), this.attributesBound = false; - } - getArtifact(t) { - return this.repo.get(t); - } - setArtifact(t, r) { - this.repo.set(t, r); - } - run(t, r, n, o, i) { - Le(t.programInfo.name); - let a = this.backend.device, d = this.backend.getComputePassEncoder(); - this.backend.writeTimestamp(this.backend.pendingDispatchNumber * 2); - let l = []; - for (let m of r) l.push({ binding: l.length, resource: { buffer: m.buffer } }); - for (let m of n) l.push({ binding: l.length, resource: { buffer: m.buffer } }); - i && l.push({ binding: l.length, resource: i }); - let c = a.createBindGroup({ layout: t.computePipeline.getBindGroupLayout(0), entries: l, label: t.programInfo.name }); - if (this.backend.sessionStatus === "capturing") { - let m = { kernelId: this.backend.currentKernelId, computePipeline: t.computePipeline, bindGroup: c, dispatchGroup: o }; - this.backend.capturedCommandList.get(this.backend.currentSessionId).push(m); - } - d.setPipeline(t.computePipeline), d.setBindGroup(0, c), d.dispatchWorkgroups(...o), this.backend.writeTimestamp(this.backend.pendingDispatchNumber * 2 + 1), this.backend.pendingDispatchNumber++, (this.backend.pendingDispatchNumber >= this.backend.maxDispatchNumber || this.backend.queryType === "at-passes") && this.backend.endComputePass(), this.backend.pendingDispatchNumber >= this.backend.maxDispatchNumber && this.backend.flush(), Ve(t.programInfo.name); - } - dispose() { - } - build(t, r) { - Le(t.name); - let n = this.backend.device, o = []; - n.features.has("shader-f16") && o.push("enable f16;"); - let i = Za(r, this.backend.device.limits), a = t.getShaderSource(i), d = `${o.join(` - `)} - ${i.additionalImplementations} - ${a}`, l = n.createShaderModule({ code: d, label: t.name }); - me("verbose", () => `[WebGPU] ${t.name} shader code: ${d}`); - let c = n.createComputePipeline({ compute: { module: l, entryPoint: "main" }, layout: "auto", label: t.name }); - return Ve(t.name), { programInfo: t, computePipeline: c, uniformVariablesInfo: i.variablesInfo }; - } - normalizeDispatchGroupSize(t) { - let r = typeof t == "number" ? t : t.x, n = typeof t == "number" ? 1 : t.y || 1, o = typeof t == "number" ? 1 : t.z || 1, i = this.backend.device.limits.maxComputeWorkgroupsPerDimension; - if (r <= i && n <= i && o <= i) return [r, n, o]; - let a = r * n * o, d = Math.ceil(Math.sqrt(a)); - if (d > i) { - if (d = Math.ceil(Math.cbrt(a)), d > i) throw new Error("Total dispatch size exceeds WebGPU maximum."); - return [d, d, d]; - } else return [d, d, 1]; - } - }; - }); - var Rh, Uh, Io, sn, Ml = V(() => { - "use strict"; - Ke(); - Q(); - Xe(); - Xn(); - Ka(); - Bl(); - Dl(); - Rh = (e, t) => { - if (t.length !== e.length) throw new Error(`inputDependencies length ${t.length} is not equal to inputTensors length ${e.length}.`); - let r = []; - for (let n = 0; n < e.length; ++n) { - let o = e[n].dataType; - switch (t[n]) { - case "none": { - r.push(""); - break; - } - case "type": { - r.push(`${o}`); - break; - } - case "rank": { - let i = e[n].dims.length; - r.push(`${o};${i}`); - break; - } - case "dims": { - let i = e[n].dims.join(","); - r.push(`${o};${i}`); - break; - } - default: - throw new Error(`unsupported input dependency: ${t[n]}`); - } - } - return r.join("|"); - }, Uh = (e, t, r) => { - var _a2, _b; - let n = e.name; - return ((_a2 = e.shaderCache) == null ? void 0 : _a2.hint) && (n += "[" + e.shaderCache.hint + "]"), n += ":" + r + `:${Rh(t, ((_b = e.shaderCache) == null ? void 0 : _b.inputDependencies) ?? new Array(t.length).fill("dims"))}`, n; - }, Io = class { - constructor(t) { - t && (this.architecture = t.architecture, this.vendor = t.vendor); - } - isArchitecture(t) { - return this.architecture === t; - } - isVendor(t) { - return this.vendor === t; - } - }, sn = class { - constructor() { - this.currentSessionId = null; - this.currentKernelId = null; - this.commandEncoder = null; - this.computePassEncoder = null; - this.maxDispatchNumber = 16; - this.pendingDispatchNumber = 0; - this.pendingKernels = []; - this.pendingQueries = /* @__PURE__ */ new Map(); - this.sessionStatus = "default"; - this.capturedCommandList = /* @__PURE__ */ new Map(); - this.capturedPendingKernels = /* @__PURE__ */ new Map(); - this.sessionExternalDataMapping = /* @__PURE__ */ new Map(); - } - get currentKernelCustomData() { - if (this.currentKernelId === null) throw new Error("currentKernelCustomData(): currentKernelId is null. (should not happen)"); - let t = this.kernelCustomData.get(this.currentKernelId); - return t || (t = {}, this.kernelCustomData.set(this.currentKernelId, t)), t; - } - async initialize(t, r) { - this.env = t; - let n = [], o = { requiredLimits: { maxComputeWorkgroupStorageSize: r.limits.maxComputeWorkgroupStorageSize, maxComputeWorkgroupsPerDimension: r.limits.maxComputeWorkgroupsPerDimension, maxStorageBufferBindingSize: r.limits.maxStorageBufferBindingSize, maxBufferSize: r.limits.maxBufferSize, maxComputeInvocationsPerWorkgroup: r.limits.maxComputeInvocationsPerWorkgroup, maxComputeWorkgroupSizeX: r.limits.maxComputeWorkgroupSizeX, maxComputeWorkgroupSizeY: r.limits.maxComputeWorkgroupSizeY, maxComputeWorkgroupSizeZ: r.limits.maxComputeWorkgroupSizeZ }, requiredFeatures: n }; - r.features.has("chromium-experimental-timestamp-query-inside-passes") ? n.push("chromium-experimental-timestamp-query-inside-passes") : r.features.has("timestamp-query") && n.push("timestamp-query"), r.features.has("shader-f16") && n.push("shader-f16"), this.device = await r.requestDevice(o), this.adapterInfo = new Io(r.info || await r.requestAdapterInfo()), this.gpuDataManager = ja(this), this.programManager = new an(this), this.kernels = /* @__PURE__ */ new Map(), this.kernelPersistentData = /* @__PURE__ */ new Map(), this.kernelCustomData = /* @__PURE__ */ new Map(), Wr(t.logLevel, !!t.debug), Object.defineProperty(this.env.webgpu, "device", { value: this.device, writable: false, enumerable: true, configurable: false }), Object.defineProperty(this.env.webgpu, "adapter", { value: r, writable: false, enumerable: true, configurable: false }), this.setQueryType(); - } - dispose() { - typeof this.querySet < "u" && this.querySet.destroy(), this.gpuDataManager.dispose(); - } - getCommandEncoder() { - return this.commandEncoder || (this.commandEncoder = this.device.createCommandEncoder()), this.commandEncoder; - } - getComputePassEncoder() { - if (!this.computePassEncoder) { - let t = this.getCommandEncoder(), r = {}; - this.queryType === "at-passes" && (r.timestampWrites = { querySet: this.querySet, beginningOfPassWriteIndex: this.pendingDispatchNumber * 2, endOfPassWriteIndex: this.pendingDispatchNumber * 2 + 1 }), this.computePassEncoder = t.beginComputePass(r); - } - return this.computePassEncoder; - } - endComputePass() { - this.computePassEncoder && (this.computePassEncoder.end(), this.computePassEncoder = null); - } - flush() { - if (!this.commandEncoder) return; - Le(), this.endComputePass(); - let t; - this.queryType !== "none" && (this.commandEncoder.resolveQuerySet(this.querySet, 0, this.pendingDispatchNumber * 2, this.queryResolveBuffer, 0), t = this.device.createBuffer({ size: this.pendingDispatchNumber * 2 * 8, usage: GPUBufferUsage.MAP_READ | GPUBufferUsage.COPY_DST }), this.pendingQueries.set(t, this.pendingKernels), this.pendingKernels = [], this.commandEncoder.copyBufferToBuffer(this.queryResolveBuffer, 0, t, 0, this.pendingDispatchNumber * 2 * 8)), this.device.queue.submit([this.commandEncoder.finish()]), this.gpuDataManager.refreshPendingBuffers(), this.commandEncoder = null, this.pendingDispatchNumber = 0, this.queryType !== "none" && t.mapAsync(GPUMapMode.READ).then(() => { - var _a2; - let r = new BigUint64Array(t.getMappedRange()), n = this.pendingQueries.get(t); - for (let o = 0; o < r.length / 2; o++) { - let i = n[o], a = i.kernelId, d = this.kernels.get(a), l = d.kernelType, c = d.kernelName, m = i.programName, u = i.inputTensorViews, h = i.outputTensorViews, w = r[o * 2], g = r[o * 2 + 1]; - typeof this.queryTimeBase > "u" && (this.queryTimeBase = w); - let y = Number(w - this.queryTimeBase), S = Number(g - this.queryTimeBase); - if (!Number.isSafeInteger(y) || !Number.isSafeInteger(S)) throw new RangeError("incorrect timestamp range"); - if ((_a2 = this.env.webgpu.profiling) == null ? void 0 : _a2.ondata) this.env.webgpu.profiling.ondata({ version: 1, inputsMetadata: u.map(($) => ({ dims: $.dims, dataType: yt($.dataType) })), outputsMetadata: h.map(($) => ({ dims: $.dims, dataType: yt($.dataType) })), kernelId: a, kernelType: l, kernelName: c, programName: m, startTime: y, endTime: S }); - else { - let $ = ""; - u.forEach((x, T) => { - $ += `input[${T}]: [${x.dims}] | ${yt(x.dataType)}, `; - }); - let v = ""; - h.forEach((x, T) => { - v += `output[${T}]: [${x.dims}] | ${yt(x.dataType)}, `; - }), console.log(`[profiling] kernel "${a}|${l}|${c}|${m}" ${$}${v}execution time: ${S - y} ns`); - } - Sr("GPU", `${m}::${w}::${g}`); - } - t.unmap(), this.pendingQueries.delete(t); - }), Ve(); - } - run(t, r, n, o, i, a) { - Le(t.name); - let d = []; - for (let x = 0; x < r.length; ++x) { - let T = r[x].data; - if (T === 0) continue; - let C = this.gpuDataManager.get(T); - if (!C) throw new Error(`no GPU data for input: ${T}`); - d.push(C); - } - let { outputs: l, dispatchGroup: c, programUniforms: m } = t.getRunData(r), u = n.length === 0 ? l.map((x, T) => T) : n; - if (u.length !== l.length) throw new Error(`Output size ${u.length} must be equal to ${l.length}.`); - let h = [], w = []; - for (let x = 0; x < l.length; ++x) { - if (!Number.isInteger(u[x]) || u[x] < -3 || u[x] >= a) throw new Error(`Invalid output index: ${u[x]}`); - if (u[x] === -3) continue; - let T = u[x] === -1, C = u[x] === -2, A = T || C ? i(l[x].dataType, l[x].dims) : o(u[x], l[x].dataType, l[x].dims); - if (h.push(A), A.data === 0) continue; - let P = this.gpuDataManager.get(A.data); - if (!P) throw new Error(`no GPU data for output: ${A.data}`); - if (T && this.temporaryData.push(P), C) { - let B = this.kernelPersistentData.get(this.currentKernelId); - B || (B = [], this.kernelPersistentData.set(this.currentKernelId, B)), B.push(P); - } - w.push(P); - } - if (d.length !== r.length || w.length !== h.length) { - if (w.length === 0) return Ve(t.name), h; - throw new Error(`Program ${t.name} has zero-sized tensor(s) in inputs or outputs. This is not supported now.`); - } - let g; - if (m) { - let x = 0, T = []; - m.forEach((B) => { - let N = typeof B.data == "number" ? [B.data] : B.data; - if (N.length === 0) return; - let W = B.type === 10 ? 2 : 4, K, Z; - B.type === 10 ? (Z = N.length > 4 ? 16 : N.length > 2 ? 8 : N.length * W, K = N.length > 4 ? 16 : W * N.length) : (Z = N.length <= 2 ? N.length * W : 16, K = 16), x = Math.ceil(x / Z) * Z, T.push(x); - let ee = B.type === 10 ? 8 : 4; - x += N.length > 4 ? Math.ceil(N.length / ee) * K : N.length * W; - }); - let C = 16; - x = Math.ceil(x / C) * C; - let A = new ArrayBuffer(x); - m.forEach((B, N) => { - let W = T[N], K = typeof B.data == "number" ? [B.data] : B.data; - if (B.type === 6) new Int32Array(A, W, K.length).set(K); - else if (B.type === 12) new Uint32Array(A, W, K.length).set(K); - else if (B.type === 10) new Uint16Array(A, W, K.length).set(K); - else if (B.type === 1) new Float32Array(A, W, K.length).set(K); - else throw new Error(`Unsupported uniform type: ${yt(B.type)}`); - }); - let P = this.gpuDataManager.create(x, GPUBufferUsage.COPY_DST | GPUBufferUsage.UNIFORM); - this.device.queue.writeBuffer(P.buffer, 0, A, 0, x), this.gpuDataManager.release(P.id), g = { offset: 0, size: x, buffer: P.buffer }; - } - let y = this.programManager.normalizeDispatchGroupSize(c), S = y[1] === 1 && y[2] === 1, $ = Uh(t, r, S), v = this.programManager.getArtifact($); - if (v || (v = this.programManager.build(t, y), this.programManager.setArtifact($, v), me("info", () => `[artifact] key: ${$}, programName: ${t.name}`)), m && v.uniformVariablesInfo) { - if (m.length !== v.uniformVariablesInfo.length) throw new Error(`Uniform variables count mismatch: expect ${v.uniformVariablesInfo.length}, got ${m.length} in program "${v.programInfo.name}".`); - for (let x = 0; x < m.length; x++) { - let T = m[x], C = T.type, A = typeof T.data == "number" ? 1 : T.data.length, [P, B] = v.uniformVariablesInfo[x]; - if (C !== P || A !== B) throw new Error(`Uniform variable ${x} mismatch: expect type ${P} with size ${B}, got type ${C} with size ${A} in program "${v.programInfo.name}".`); - } - } - if (me("info", () => `[ProgramManager] run "${t.name}" (key=${$}) with ${y[0]}x${y[1]}x${y[2]}`), this.queryType !== "none" || this.sessionStatus === "capturing") { - let x = { kernelId: this.currentKernelId, programName: v.programInfo.name, inputTensorViews: r, outputTensorViews: h }; - this.pendingKernels.push(x), this.sessionStatus === "capturing" && this.capturedPendingKernels.get(this.currentSessionId).push(x); - } - return this.programManager.run(v, d, w, y, g), Ve(t.name), h; - } - upload(t, r) { - this.gpuDataManager.upload(t, r); - } - memcpy(t, r) { - this.gpuDataManager.memcpy(t, r); - } - async download(t, r) { - await this.gpuDataManager.download(t, r); - } - alloc(t) { - return this.gpuDataManager.create(t).id; - } - free(t) { - return this.gpuDataManager.release(t); - } - createKernel(t, r, n, o) { - let i = Ol.get(t); - if (!i) throw new Error(`kernel not implemented: ${t}`); - let a = { kernelType: t, kernelName: o, kernelEntry: i[0], attributes: [i[1], n] }; - this.kernels.set(r, a); - } - releaseKernel(t) { - let r = this.kernelPersistentData.get(t); - if (r) { - for (let n of r) this.gpuDataManager.release(n.id); - this.kernelPersistentData.delete(t); - } - this.kernelCustomData.delete(t), this.kernels.delete(t); - } - computeKernel(t, r, n) { - let o = this.kernels.get(t); - if (!o) throw new Error(`kernel not created: ${t}`); - let i = o.kernelType, a = o.kernelName, d = o.kernelEntry, l = o.attributes; - if (this.currentKernelId !== null) throw new Error(`kernel "[${i}] ${a}" is not allowed to be called recursively`); - this.currentKernelId = t, l[0] && (l[1] = l[0](l[1]), l[0] = void 0), me("info", () => `[WebGPU] Start to run kernel "[${i}] ${a}"...`); - let c = this.env.debug; - this.temporaryData = []; - try { - return c && this.device.pushErrorScope("validation"), d(r, l[1]), 0; - } catch (m) { - return n.push(Promise.resolve(`[WebGPU] Kernel "[${i}] ${a}" failed. ${m}`)), 1; - } finally { - c && n.push(this.device.popErrorScope().then((m) => m ? `GPU validation error for kernel "[${i}] ${a}": ${m.message}` : null)); - for (let m of this.temporaryData) this.gpuDataManager.release(m.id); - this.temporaryData = [], this.currentKernelId = null; - } - } - registerBuffer(t, r, n, o) { - let i = this.sessionExternalDataMapping.get(t); - i || (i = /* @__PURE__ */ new Map(), this.sessionExternalDataMapping.set(t, i)); - let a = i.get(r), d = this.gpuDataManager.registerExternalBuffer(n, o, a); - return i.set(r, [d, n]), d; - } - unregisterBuffers(t) { - let r = this.sessionExternalDataMapping.get(t); - r && (r.forEach((n) => this.gpuDataManager.unregisterExternalBuffer(n[0])), this.sessionExternalDataMapping.delete(t)); - } - getBuffer(t) { - let r = this.gpuDataManager.get(t); - if (!r) throw new Error(`no GPU data for buffer: ${t}`); - return r.buffer; - } - createDownloader(t, r, n) { - return async () => { - let o = await eo(this, t, r); - return Lr(o.buffer, n); - }; - } - writeTimestamp(t) { - this.queryType === "inside-passes" && this.computePassEncoder.writeTimestamp(this.querySet, t); - } - setQueryType() { - var _a2; - this.queryType = "none", (((_a2 = this.env.webgpu.profiling) == null ? void 0 : _a2.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() { - me("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() { - me("info", "captureEnd"), this.flush(), this.sessionStatus = "default"; - } - replay() { - me("info", "replay"), this.sessionStatus = "replaying"; - let t = this.capturedCommandList.get(this.currentSessionId), r = this.capturedPendingKernels.get(this.currentSessionId), n = t.length; - this.pendingKernels = []; - for (let o = 0; o < n; o++) { - let i = this.getComputePassEncoder(), a = t[o]; - this.writeTimestamp(this.pendingDispatchNumber * 2), i.setPipeline(a.computePipeline), i.setBindGroup(0, a.bindGroup), i.dispatchWorkgroups(...a.dispatchGroup), this.writeTimestamp(this.pendingDispatchNumber * 2 + 1), this.pendingDispatchNumber++, this.queryType !== "none" && this.pendingKernels.push(r[o]), (this.pendingDispatchNumber >= 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 Vh, Rl, un, Co, Ul, Vl = V(() => { - "use strict"; - Xe(); - Vh = 1, Rl = () => Vh++, un = class { - constructor(t, r) { - this.mlContext = t; - this.tensorEntry = r, this.tensorCache = r ? [r] : []; - } - get tensor() { - var _a2; - return (_a2 = this.tensorEntry) == null ? void 0 : _a2[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, r) { - for (let [n, o, i] of this.tensorCache) if (r === n) { - if (this.context !== t) throw new Error("MLTensor cannot be registered with a different MLContext."); - return this.tensorEntry = [n, o, i], true; - } - return false; - } - async ensureTensor(t, r, n) { - var _a2; - if (this.tensorEntry) { - let [a, d, l] = this.tensorEntry; - if (d === t && l.every((c, m) => c === r[m])) return a; - } - for (let [a, d, l] of this.tensorCache) if (d === t && l.every((c, m) => c === r[m])) { - if (n && this.tensorEntry) { - me("verbose", () => `[WebNN] Slowdown may occur, having to copy existing tensor {dataType: ${t}, shape: ${r}}`); - let c = await this.context.readTensor(this.tensorEntry[0]); - this.context.writeTensor(a, c); - } - return this.tensorEntry = [a, d, l], a; - } - me("verbose", () => `[WebNN] MLContext.createTensor {dataType: ${t}, shape: ${r}}`); - let o = MLTensorUsage.READ | MLTensorUsage.WRITE, i = await this.context.createTensor({ dataType: t, shape: r, dimensions: r, usage: o }); - return this.tensorEntry = [i, t, r], this.tensorCache.push(this.tensorEntry), this.activeUpload && ((_a2 = this.mlContext) == null ? void 0 : _a2.writeTensor(i, this.activeUpload), this.activeUpload = void 0), i; - } - upload(t) { - var _a2; - if (!this.tensorEntry) { - this.activeUpload = new Uint8Array(t); - return; - } - (_a2 = this.mlContext) == null ? void 0 : _a2.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]); - } - }, Co = class { - constructor(t) { - this.backend = t; - this.tensorsById = /* @__PURE__ */ new Map(); - this.tensorIdsByContext = /* @__PURE__ */ new Map(); - } - reserveTensorId() { - let t = Rl(); - return this.tensorsById.set(t, new un()), t; - } - releaseTensorId(t) { - let r = this.tensorsById.get(t); - if (r) { - r.destroy(), this.tensorsById.delete(t); - for (let [n, o] of this.tensorIdsByContext) if (o.has(t)) { - o.delete(t), o.size === 0 && this.tensorIdsByContext.delete(n); - break; - } - } - } - async ensureTensor(t, r, n, o) { - var _a2; - me("verbose", () => `[WebNN] TensorManager.ensureTensor {tensorId: ${t}, dataType: ${r}, shape: ${n}, 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, /* @__PURE__ */ new Set()), (_a2 = this.tensorIdsByContext.get(this.backend.currentContext)) == null ? void 0 : _a2.add(t), i.ensureTensor(r, n, o); - } - upload(t, r) { - this.tensorsById.get(t).upload(r); - } - async download(t, r) { - return me("verbose", () => `[WebNN] TensorManager.download {tensorId: ${t}, dstBuffer: ${r == null ? void 0 : r.byteLength}}`), this.tensorsById.get(t).download(r); - } - releaseTensorsForContext(t) { - let r = this.tensorIdsByContext.get(t); - if (r) { - for (let n of r) this.tensorsById.get(n).destroy(), this.tensorsById.delete(n); - this.tensorIdsByContext.delete(t); - } - } - registerTensor(t, r, n, o) { - for (let [d, l] of this.tensorsById) if (l.trySelectTensor(t, r)) return d; - let i = Rl(); - this.tensorsById.set(i, new un(t, [r, n, o])); - let a = this.tensorIdsByContext.get(t); - return a || (a = /* @__PURE__ */ new Set(), this.tensorIdsByContext.set(t, a)), a.add(i), i; - } - }, Ul = (...e) => new Co(...e); - }); - var Nl, dn, Wl = V(() => { - "use strict"; - Q(); - gt(); - Xn(); - Vl(); - Xe(); - Nl = /* @__PURE__ */ new Map([[1, "float32"], [10, "float16"], [6, "int32"], [12, "uint32"], [7, "int64"], [13, "uint64"], [3, "int8"], [2, "uint8"], [9, "uint8"]]), dn = class { - constructor(t) { - this.tensorManager = Ul(this); - this.mlContextBySessionId = /* @__PURE__ */ new Map(); - this.sessionIdsByMLContext = /* @__PURE__ */ new Map(); - Wr(t.logLevel, !!t.debug); - } - get currentSessionId() { - if (this.activeSessionId === void 0) throw new Error("No active session"); - return this.activeSessionId; - } - onRunStart(t) { - this.activeSessionId = t; - } - get currentContext() { - let t = this.getMLContext(this.currentSessionId); - if (!t) throw new Error(`No MLContext found for session ${this.currentSessionId}`); - return t; - } - registerMLContext(t, r) { - this.mlContextBySessionId.set(t, r); - let n = this.sessionIdsByMLContext.get(r); - n || (n = /* @__PURE__ */ new Set(), this.sessionIdsByMLContext.set(r, n)), n.add(t); - } - onReleaseSession(t) { - let r = this.mlContextBySessionId.get(t); - if (!r) return; - this.mlContextBySessionId.delete(t); - let n = this.sessionIdsByMLContext.get(r); - n.delete(t), n.size === 0 && (this.sessionIdsByMLContext.delete(r), this.tensorManager.releaseTensorsForContext(r)); - } - getMLContext(t) { - return this.mlContextBySessionId.get(t); - } - reserveTensorId() { - return this.tensorManager.reserveTensorId(); - } - releaseTensorId(t) { - me("verbose", () => `[WebNN] releaseTensorId {tensorId: ${t}}`), this.tensorManager.releaseTensorId(t); - } - async ensureTensor(t, r, n, o) { - let i = Nl.get(r); - if (!i) throw new Error(`Unsupported ONNX data type: ${r}`); - return this.tensorManager.ensureTensor(t, i, n, o); - } - uploadTensor(t, r) { - if (!Te().shouldTransferToMLTensor) throw new Error("Trying to upload to a MLTensor while shouldTransferToMLTensor is false"); - me("verbose", () => `[WebNN] uploadTensor {tensorId: ${t}, data: ${r.byteLength}}`), this.tensorManager.upload(t, r); - } - async downloadTensor(t, r) { - return this.tensorManager.download(t, r); - } - createMLTensorDownloader(t, r) { - return async () => { - let n = await this.tensorManager.download(t); - return Lr(n, r); - }; - } - registerMLTensor(t, r, n) { - let o = Nl.get(r); - if (!o) throw new Error(`Unsupported ONNX data type: ${r}`); - let i = this.tensorManager.registerTensor(this.currentContext, t, o, n); - return me("verbose", () => `[WebNN] registerMLTensor {tensor: ${t}, dataType: ${o}, dimensions: ${n}} -> {tensorId: ${i}}`), i; - } - flush() { - } - }; - }); - var Ll = {}; - Gt(Ll, { init: () => Nh }); - var or, Ao, Nh, Hl = V(() => { - "use strict"; - Q(); - Ml(); - Xe(); - ie(); - Wl(); - or = class e { - constructor(t, r, n, o) { - this.module = t; - this.dataType = r; - this.data = n; - this.dims = o; - } - getFloat32Array() { - if (this.dataType !== 1) throw new Error("Invalid data type"); - let t = k.size(this.dims); - return t === 0 ? new Float32Array() : new Float32Array(this.module.HEAP8.buffer, this.data, t); - } - getBigInt64Array() { - if (this.dataType !== 7) throw new Error("Invalid data type"); - let t = k.size(this.dims); - return t === 0 ? new BigInt64Array() : new BigInt64Array(this.module.HEAP8.buffer, this.data, t); - } - getInt32Array() { - if (this.dataType !== 6) throw new Error("Invalid data type"); - let t = k.size(this.dims); - return t === 0 ? new Int32Array() : new Int32Array(this.module.HEAP8.buffer, this.data, t); - } - getUint16Array() { - if (this.dataType !== 10 && this.dataType !== 4) throw new Error("Invalid data type"); - let t = k.size(this.dims); - return t === 0 ? new Uint16Array() : new Uint16Array(this.module.HEAP8.buffer, this.data, t); - } - reshape(t) { - if (k.size(t) !== k.size(this.dims)) throw new Error("Invalid new shape"); - return new e(this.module, this.dataType, this.data, t); - } - }, Ao = class { - constructor(t, r, n) { - this.module = t; - this.backend = r; - this.customDataOffset = 0; - this.customDataSize = 0; - this.adapterInfo = r.adapterInfo; - let o = t.HEAPU32, i = n >>> 2; - this.opKernelContext = o[i++]; - let a = o[i++]; - this.outputCount = o[i++], this.customDataOffset = o[i++], this.customDataSize = o[i++]; - let d = []; - for (let l = 0; l < a; l++) { - let c = o[i++], m = o[i++], u = o[i++], h = []; - for (let w = 0; w < u; w++) h.push(o[i++]); - d.push(new or(t, c, m, h)); - } - this.inputs = d; - } - get kernelCustomData() { - return this.backend.currentKernelCustomData; - } - get customDataBuffer() { - return this.module.HEAPU8.subarray(this.customDataOffset, this.customDataOffset + this.customDataSize); - } - getMaxComputeWorkgroupSizes() { - return [this.backend.device.limits.maxComputeWorkgroupSizeX, this.backend.device.limits.maxComputeWorkgroupSizeY, this.backend.device.limits.maxComputeWorkgroupSizeZ]; - } - getMaxComputeWorkgroupStoragesize() { - return this.backend.device.limits.maxComputeWorkgroupStorageSize; - } - compute(t, r) { - var _a2; - let n = ((_a2 = r == null ? void 0 : r.inputs) == null ? void 0 : _a2.map((d) => typeof d == "number" ? this.inputs[d] : d)) ?? this.inputs, o = (r == null ? void 0 : r.outputs) ?? [], i = (d, l, c) => new or(this.module, l, this.output(d, c), c), a = (d, l) => { - let c = It(d, l); - if (!c) throw new Error(`Unsupported data type: ${d}`); - let m = c > 0 ? this.backend.gpuDataManager.create(c).id : 0; - return new or(this.module, d, m, l); - }; - return this.backend.run(t, n, o, i, a, this.outputCount); - } - output(t, r) { - let n = this.module.stackSave(); - try { - let o = this.module.stackAlloc((1 + r.length) * 4), i = o >> 2; - this.module.HEAPU32[i++] = r.length; - for (let a = 0; a < r.length; a++) this.module.HEAPU32[i++] = r[a]; - return this.module._JsepOutput(this.opKernelContext, t, o); - } catch (o) { - throw new Error(`Failed to generate kernel's output[${t}] with dims [${r}]. If you are running with pre-allocated output, please make sure the output type/dims are correct. Error: ${o}`); - } finally { - this.module.stackRestore(n); - } - } - }, Nh = async (e, t, r, n) => { - let o = t.jsepInit; - if (!o) throw new Error("Failed to initialize JSEP. The WebAssembly module is not built with JSEP support."); - if (e === "webgpu") { - let i = new sn(); - await i.initialize(r, n), o("webgpu", [i, (a) => i.alloc(a), (a) => i.free(a), (a, d, l, c = false) => { - if (c) me("verbose", () => `[WebGPU] jsepCopyGpuToGpu: src=${a}, dst=${d}, size=${l}`), i.memcpy(a, d); - else { - me("verbose", () => `[WebGPU] jsepCopyCpuToGpu: dataOffset=${a}, gpuDataId=${d}, size=${l}`); - let m = t.HEAPU8.subarray(a >>> 0, (a >>> 0) + l); - i.upload(d, m); - } - }, async (a, d, l) => { - me("verbose", () => `[WebGPU] jsepCopyGpuToCpu: gpuDataId=${a}, dataOffset=${d}, size=${l}`), await i.download(a, () => t.HEAPU8.subarray(d >>> 0, (d >>> 0) + l)); - }, (a, d, l) => i.createKernel(a, d, l, t.UTF8ToString(t._JsepGetNodeName(d))), (a) => i.releaseKernel(a), (a, d, l, c) => { - me("verbose", () => `[WebGPU] jsepRun: sessionHandle=${l}, kernel=${a}, contextDataOffset=${d}`); - let m = new Ao(t, i, d); - return i.computeKernel(a, m, c); - }, () => i.captureBegin(), () => i.captureEnd(), () => i.replay()]); - } else { - let i = new dn(r); - o("webnn", [i, () => i.reserveTensorId(), (a) => i.releaseTensorId(a), async (a, d, l, c) => i.ensureTensor(a, d, l, c), (a, d) => { - i.uploadTensor(a, d); - }, async (a, d) => i.downloadTensor(a, d)]); - } - }; - }); - var Wh, kr, Er, Et, Lh, jt, Pr, zr, Gl, Or, Br, Dr, Gn = V(() => { - "use strict"; - Va(); - Wa(); - Q(); - gt(); - Rr(); - Yn(); - Wh = (e, t) => { - Te()._OrtInit(e, t) !== 0 && ve("Can't initialize onnxruntime."); - }, kr = async (e) => { - Wh(e.wasm.numThreads, Xt(e.logLevel)); - }, Er = async (e, t) => { - { - let r = (Hl(), br(Ll)).init; - if (t === "webgpu") { - if (typeof navigator > "u" || !navigator.gpu) throw new Error("WebGPU is not supported in current environment"); - let n = e.webgpu.adapter; - if (n) { - if (typeof n.limits != "object" || typeof n.features != "object" || typeof n.requestDevice != "function") throw new Error("Invalid GPU adapter set in `env.webgpu.adapter`. It must be a GPUAdapter object."); - } else { - let o = e.webgpu.powerPreference; - if (o !== void 0 && o !== "low-power" && o !== "high-performance") throw new Error(`Invalid powerPreference setting: "${o}"`); - let i = e.webgpu.forceFallbackAdapter; - if (i !== void 0 && typeof i != "boolean") throw new Error(`Invalid forceFallbackAdapter setting: "${i}"`); - if (n = await navigator.gpu.requestAdapter({ powerPreference: o, forceFallbackAdapter: i }), !n) throw new Error('Failed to get GPU adapter. You may need to enable flag "--enable-unsafe-webgpu" if you are using Chrome.'); - } - await r("webgpu", Te(), e, n); - } - if (t === "webnn") { - if (typeof navigator > "u" || !navigator.ml) throw new Error("WebNN is not supported in current environment"); - await r("webnn", Te(), e); - } - } - }, Et = /* @__PURE__ */ new Map(), Lh = (e) => { - let t = Te(), r = t.stackSave(); - try { - let n = t.stackAlloc(8); - return t._OrtGetInputOutputCount(e, n, n + 4) !== 0 && ve("Can't get session input/output count."), [t.HEAP32[n / 4], t.HEAP32[n / 4 + 1]]; - } finally { - t.stackRestore(r); - } - }, jt = (e) => { - let t = Te(), r = t._malloc(e.byteLength); - if (r === 0) throw new Error(`Can't create a session. failed to allocate a buffer of size ${e.byteLength}.`); - return t.HEAPU8.set(e, r), [r, e.byteLength]; - }, Pr = async (e, t) => { - var _a2, _b; - let r, n, o = Te(); - Array.isArray(e) ? [r, n] = e : e.buffer === o.HEAPU8.buffer ? [r, n] = [e.byteOffset, e.byteLength] : [r, n] = jt(e); - let i = 0, a = 0, d = 0, l = [], c = [], m = []; - try { - if ([a, l] = Na(t), (t == null ? void 0 : t.externalData) && o.mountExternalData) { - let v = []; - for (let x of t.externalData) { - let T = typeof x == "string" ? x : x.path; - v.push(Zt(typeof x == "string" ? x : x.data).then((C) => { - o.mountExternalData(T, C); - })); - } - await Promise.all(v); - } - for (let v of (t == null ? void 0 : t.executionProviders) ?? []) if ((typeof v == "string" ? v : v.name) === "webnn") { - if (o.shouldTransferToMLTensor = false, o.currentContext) throw new Error("WebNN execution provider is already set."); - if (typeof v != "string") { - let T = v, C = T == null ? void 0 : T.context, A = T == null ? void 0 : T.gpuDevice, P = T == null ? void 0 : T.deviceType, B = T == null ? void 0 : T.numThreads, N = T == null ? void 0 : T.powerPreference; - C ? o.currentContext = C : A ? o.currentContext = await navigator.ml.createContext(A) : o.currentContext = await navigator.ml.createContext({ deviceType: P, numThreads: B, powerPreference: N }); - } else o.currentContext = await navigator.ml.createContext(); - break; - } - i = await o._OrtCreateSession(r, n, a), i === 0 && ve("Can't create a session."), o.currentContext && (o.jsepRegisterMLContext(i, o.currentContext), o.currentContext = void 0, o.shouldTransferToMLTensor = true); - let [u, h] = Lh(i), w = !!(t == null ? void 0 : t.enableGraphCapture), g = [], y = [], S = []; - for (let v = 0; v < u; v++) { - let x = o._OrtGetInputName(i, v); - x === 0 && ve("Can't get an input name."), c.push(x), g.push(o.UTF8ToString(x)); - } - for (let v = 0; v < h; v++) { - let x = o._OrtGetOutputName(i, v); - x === 0 && ve("Can't get an output name."), m.push(x); - let T = o.UTF8ToString(x); - y.push(T); - { - if (w && (t == null ? void 0 : t.preferredOutputLocation) === void 0) { - S.push("gpu-buffer"); - continue; - } - let C = typeof (t == null ? void 0 : t.preferredOutputLocation) == "string" ? t.preferredOutputLocation : ((_a2 = t == null ? void 0 : t.preferredOutputLocation) == null ? void 0 : _a2[T]) ?? "cpu"; - if (C !== "cpu" && C !== "cpu-pinned" && C !== "gpu-buffer" && C !== "ml-tensor") throw new Error(`Not supported preferred output location: ${C}.`); - if (w && C !== "gpu-buffer") throw new Error(`Not supported preferred output location: ${C}. Only 'gpu-buffer' location is supported when enableGraphCapture is true.`); - S.push(C); - } - } - let $ = null; - return S.some((v) => v === "gpu-buffer" || v === "ml-tensor") && (d = o._OrtCreateBinding(i), d === 0 && ve("Can't create IO binding."), $ = { handle: d, outputPreferredLocations: S, outputPreferredLocationsEncoded: S.map((v) => Kn(v)) }), Et.set(i, [i, c, m, $, w, false]), [i, g, y]; - } catch (u) { - throw c.forEach((h) => o._OrtFree(h)), m.forEach((h) => o._OrtFree(h)), d !== 0 && o._OrtReleaseBinding(d), i !== 0 && o._OrtReleaseSession(i), u; - } finally { - o._free(r), a !== 0 && o._OrtReleaseSessionOptions(a), l.forEach((u) => o._free(u)), (_b = o.unmountExternalData) == null ? void 0 : _b.call(o); - } - }, zr = (e) => { - var _a2; - let t = Te(), r = Et.get(e); - if (!r) throw new Error(`cannot release session. invalid session id: ${e}`); - let [n, o, i, a, d] = r; - a && (d && t._OrtClearBoundOutputs(a.handle), t._OrtReleaseBinding(a.handle)), (_a2 = t.jsepOnReleaseSession) == null ? void 0 : _a2.call(t, e), o.forEach((l) => t._OrtFree(l)), i.forEach((l) => t._OrtFree(l)), t._OrtReleaseSession(n), Et.delete(e); - }, Gl = (e, t, r, n, o, i = false) => { - if (!e) { - t.push(0); - return; - } - let a = Te(), d = e[0], l = e[1], c = e[3], m, u; - if (d === "string" && (c === "gpu-buffer" || c === "ml-tensor")) throw new Error("String tensor is not supported on GPU."); - if (i && c !== "gpu-buffer") throw new Error(`External buffer must be provided for input/output index ${o} when enableGraphCapture is true.`); - if (c === "gpu-buffer") { - let g = e[2].gpuBuffer; - u = It(Yt(d), l); - let y = a.jsepRegisterBuffer; - if (!y) throw new Error('Tensor location "gpu-buffer" is not supported without using WebGPU.'); - m = y(n, o, g, u); - } else if (c === "ml-tensor") { - let g = e[2].mlTensor; - u = It(Yt(d), l); - let y = a.jsepRegisterMLTensor; - if (!y) throw new Error('Tensor location "ml-tensor" is not supported without using WebNN.'); - m = y(g, Yt(d), l); - } else { - let g = e[2]; - if (Array.isArray(g)) { - u = 4 * g.length, m = a._malloc(u), r.push(m); - let y = m / 4; - for (let S = 0; S < g.length; S++) { - if (typeof g[S] != "string") throw new TypeError(`tensor data at index ${S} is not a string`); - a.HEAPU32[y++] = ke(g[S], r); - } - } else u = g.byteLength, m = a._malloc(u), r.push(m), a.HEAPU8.set(new Uint8Array(g.buffer, g.byteOffset, u), m); - } - let h = a.stackSave(), w = a.stackAlloc(4 * l.length); - try { - let g = w / 4; - l.forEach((S) => a.HEAP32[g++] = S); - let y = a._OrtCreateTensor(Yt(d), m, u, w, l.length, Kn(c)); - y === 0 && ve(`Can't create tensor for input/output. session=${n}, index=${o}.`), t.push(y); - } finally { - a.stackRestore(h); - } - }, Or = async (e, t, r, n, o, i) => { - var _a2, _b; - let a = Te(), d = Et.get(e); - if (!d) throw new Error(`cannot run inference. invalid session id: ${e}`); - let l = d[0], c = d[1], m = d[2], u = d[3], h = d[4], w = d[5], g = t.length, y = n.length, S = 0, $ = [], v = [], x = [], T = [], C = a.stackSave(), A = a.stackAlloc(g * 4), P = a.stackAlloc(g * 4), B = a.stackAlloc(y * 4), N = a.stackAlloc(y * 4); - try { - (_a2 = a.jsepOnRunStart) == null ? void 0 : _a2.call(a, l), [S, $] = Ua(i); - for (let Y = 0; Y < g; Y++) Gl(r[Y], v, T, e, t[Y], h); - for (let Y = 0; Y < y; Y++) Gl(o[Y], x, T, e, g + n[Y], h); - let W = A / 4, K = P / 4, Z = B / 4, ee = N / 4; - for (let Y = 0; Y < g; Y++) a.HEAPU32[W++] = v[Y], a.HEAPU32[K++] = c[t[Y]]; - for (let Y = 0; Y < y; Y++) a.HEAPU32[Z++] = x[Y], a.HEAPU32[ee++] = m[n[Y]]; - if (u && !w) { - let { handle: Y, outputPreferredLocations: le, outputPreferredLocationsEncoded: ce } = u; - if (c.length !== g) throw new Error(`input count from feeds (${g}) is expected to be always equal to model's input count (${c.length}).`); - for (let q = 0; q < g; q++) { - let ue = t[q]; - await a._OrtBindInput(Y, c[ue], v[q]) !== 0 && ve(`Can't bind input[${q}] for session=${e}.`); - } - for (let q = 0; q < y; q++) { - let ue = n[q]; - ((_b = o[q]) == null ? void 0 : _b[3]) ? a._OrtBindOutput(Y, m[ue], x[q], 0) !== 0 && ve(`Can't bind pre-allocated output[${q}] for session=${e}.`) : a._OrtBindOutput(Y, m[ue], 0, ce[ue]) !== 0 && ve(`Can't bind output[${q}] to ${le[q]} for session=${e}.`); - } - Et.set(e, [l, c, m, u, h, true]); - } - let se; - u ? se = await a._OrtRunWithBinding(l, u.handle, y, B, S) : se = await a._OrtRun(l, P, A, g, N, y, B, S), se !== 0 && ve("failed to call OrtRun()."); - let de = []; - for (let Y = 0; Y < y; Y++) { - let le = a.HEAPU32[B / 4 + Y]; - if (le === x[Y]) { - de.push(o[Y]); - continue; - } - let ce = a.stackSave(), q = a.stackAlloc(4 * 4), ue = false, re, ne = 0; - try { - a._OrtGetTensorData(le, q, q + 4, q + 8, q + 12) !== 0 && ve(`Can't access output tensor data on index ${Y}.`); - let U = q / 4, G = a.HEAPU32[U++]; - ne = a.HEAPU32[U++]; - let ye = a.HEAPU32[U++], Re = a.HEAPU32[U++], $e = []; - for (let Ae = 0; Ae < Re; Ae++) $e.push(a.HEAPU32[ye / 4 + Ae]); - a._OrtFree(ye); - let Ce = $e.reduce((Ae, Me) => Ae * Me, 1); - re = yt(G); - let bt = u == null ? void 0 : u.outputPreferredLocations[n[Y]]; - if (re === "string") { - if (bt === "gpu-buffer" || bt === "ml-tensor") throw new Error("String tensor is not supported on GPU."); - let Ae = [], Me = ne / 4; - for (let Ue = 0; Ue < Ce; Ue++) { - let zt = a.HEAPU32[Me++], wt = Ue === Ce - 1 ? void 0 : a.HEAPU32[Me] - zt; - Ae.push(a.UTF8ToString(zt, wt)); - } - de.push([re, $e, Ae, "cpu"]); - } else if (bt === "gpu-buffer" && Ce > 0) { - let Ae = a.jsepGetBuffer; - if (!Ae) throw new Error('preferredLocation "gpu-buffer" is not supported without using WebGPU.'); - let Me = Ae(ne), Ue = It(G, Ce); - if (Ue === void 0 || !Vr(re)) throw new Error(`Unsupported data type: ${re}`); - ue = true, de.push([re, $e, { gpuBuffer: Me, download: a.jsepCreateDownloader(Me, Ue, re), dispose: () => { - a._OrtReleaseTensor(le); - } }, "gpu-buffer"]); - } else if (bt === "ml-tensor" && Ce > 0) { - let Ae = a.jsepEnsureTensor; - if (!Ae) throw new Error('preferredLocation "ml-tensor" is not supported without using WebNN.'); - if (It(G, Ce) === void 0 || !Nr(re)) throw new Error(`Unsupported data type: ${re}`); - let Ue = await Ae(ne, G, $e, false); - ue = true, de.push([re, $e, { mlTensor: Ue, download: a.jsepCreateMLTensorDownloader(ne, re), dispose: () => { - a.jsepReleaseTensorId(ne), a._OrtReleaseTensor(le); - } }, "ml-tensor"]); - } else { - let Ae = Ur(re), Me = new Ae(Ce); - new Uint8Array(Me.buffer, Me.byteOffset, Me.byteLength).set(a.HEAPU8.subarray(ne, ne + Me.byteLength)), de.push([re, $e, Me, "cpu"]); - } - } finally { - a.stackRestore(ce), re === "string" && ne && a._free(ne), ue || a._OrtReleaseTensor(le); - } - } - return u && !h && (a._OrtClearBoundOutputs(u.handle), Et.set(e, [l, c, m, u, h, false])), de; - } finally { - a.stackRestore(C), v.forEach((W) => a._OrtReleaseTensor(W)), x.forEach((W) => a._OrtReleaseTensor(W)), T.forEach((W) => a._free(W)), S !== 0 && a._OrtReleaseRunOptions(S), $.forEach((W) => a._free(W)); - } - }, Br = (e) => { - let t = Te(), r = Et.get(e); - if (!r) throw new Error("invalid session id"); - let n = r[0], o = t._OrtEndProfiling(n); - o === 0 && ve("Can't get an profile file name."), t._OrtFree(o); - }, Dr = (e) => { - let t = []; - for (let r of e) { - let n = r[2]; - !Array.isArray(n) && "buffer" in n && t.push(n.buffer); - } - return t; - }; - }); - var Pt, Ye, ir, cn, pn, ln, ko, Eo, Lt, Ht, Gh, Fl, ql, jl, Kl, Yl, Xl, Zl, Po = V(() => { - "use strict"; - Ke(); - Gn(); - gt(); - qt(); - Pt = () => !!_e.wasm.proxy && typeof document < "u", ir = false, cn = false, pn = false, Eo = /* @__PURE__ */ new Map(), Lt = (e, t) => { - let r = Eo.get(e); - r ? r.push(t) : Eo.set(e, [t]); - }, Ht = () => { - if (ir || !cn || pn || !Ye) throw new Error("worker not ready"); - }, Gh = (e) => { - switch (e.data.type) { - case "init-wasm": - ir = false, e.data.err ? (pn = true, ko[1](e.data.err)) : (cn = true, ko[0]()), ln && (URL.revokeObjectURL(ln), ln = void 0); - break; - case "init-ep": - case "copy-from": - case "create": - case "release": - case "run": - case "end-profiling": { - let t = Eo.get(e.data.type); - e.data.err ? t.shift()[1](e.data.err) : t.shift()[0](e.data.out); - break; - } - default: - } - }, Fl = async () => { - if (!cn) { - if (ir) throw new Error("multiple calls to 'initWasm()' detected."); - if (pn) throw new Error("previous call to 'initWasm()' failed."); - if (ir = true, Pt()) return new Promise((e, t) => { - Ye == null ? void 0 : Ye.terminate(), Da().then(([r, n]) => { - try { - Ye = n, Ye.onerror = (i) => t(i), Ye.onmessage = Gh, ko = [e, t]; - let o = { type: "init-wasm", in: _e }; - Ye.postMessage(o), ln = r; - } catch (o) { - t(o); - } - }, t); - }); - try { - await Ar(_e.wasm), await kr(_e), cn = true; - } catch (e) { - throw pn = true, e; - } finally { - ir = false; - } - } - }, ql = async (e) => { - if (Pt()) return Ht(), new Promise((t, r) => { - Lt("init-ep", [t, r]); - let n = { type: "init-ep", in: { epName: e, env: _e } }; - Ye.postMessage(n); - }); - await Er(_e, e); - }, jl = async (e) => Pt() ? (Ht(), new Promise((t, r) => { - Lt("copy-from", [t, r]); - let n = { type: "copy-from", in: { buffer: e } }; - Ye.postMessage(n, [e.buffer]); - })) : jt(e), Kl = async (e, t) => { - if (Pt()) { - if (t == null ? void 0 : t.preferredOutputLocation) throw new Error('session option "preferredOutputLocation" is not supported for proxy.'); - return Ht(), new Promise((r, n) => { - Lt("create", [r, n]); - let o = { type: "create", in: { model: e, options: { ...t } } }, i = []; - e instanceof Uint8Array && i.push(e.buffer), Ye.postMessage(o, i); - }); - } else return Pr(e, t); - }, Yl = async (e) => { - if (Pt()) return Ht(), new Promise((t, r) => { - Lt("release", [t, r]); - let n = { type: "release", in: e }; - Ye.postMessage(n); - }); - zr(e); - }, Xl = async (e, t, r, n, o, i) => { - if (Pt()) { - if (r.some((a) => a[3] !== "cpu")) throw new Error("input tensor on GPU is not supported for proxy."); - if (o.some((a) => a)) throw new Error("pre-allocated output tensor is not supported for proxy."); - return Ht(), new Promise((a, d) => { - Lt("run", [a, d]); - let l = r, c = { type: "run", in: { sessionId: e, inputIndices: t, inputs: l, outputIndices: n, options: i } }; - Ye.postMessage(c, Dr(l)); - }); - } else return Or(e, t, r, n, o, i); - }, Zl = async (e) => { - if (Pt()) return Ht(), new Promise((t, r) => { - Lt("end-profiling", [t, r]); - let n = { type: "end-profiling", in: e }; - Ye.postMessage(n); - }); - Br(e); - }; - }); - var Ql, Fh, mn, Jl = V(() => { - "use strict"; - Ke(); - Po(); - Q(); - Cr(); - Yn(); - Ql = (e, t) => { - switch (e.location) { - case "cpu": - return [e.type, e.dims, e.data, "cpu"]; - case "gpu-buffer": - return [e.type, e.dims, { gpuBuffer: e.gpuBuffer }, "gpu-buffer"]; - case "ml-tensor": - return [e.type, e.dims, { mlTensor: e.mlTensor }, "ml-tensor"]; - default: - throw new Error(`invalid data location: ${e.location} for ${t()}`); - } - }, Fh = (e) => { - switch (e[3]) { - case "cpu": - return new De(e[0], e[2], e[1]); - case "gpu-buffer": { - let t = e[0]; - if (!Vr(t)) throw new Error(`not supported data type: ${t} for deserializing GPU tensor`); - let { gpuBuffer: r, download: n, dispose: o } = e[2]; - return De.fromGpuBuffer(r, { dataType: t, dims: e[1], download: n, dispose: o }); - } - case "ml-tensor": { - let t = e[0]; - if (!Nr(t)) throw new Error(`not supported data type: ${t} for deserializing MLTensor tensor`); - let { mlTensor: r, download: n, dispose: o } = e[2]; - return De.fromMLTensor(r, { dataType: t, dims: e[1], download: n, dispose: o }); - } - default: - throw new Error(`invalid data location: ${e[3]}`); - } - }, mn = class { - async fetchModelAndCopyToWasmMemory(t) { - return jl(await Zt(t)); - } - async loadModel(t, r) { - Le(); - let n; - typeof t == "string" ? false ? 0 : n = await this.fetchModelAndCopyToWasmMemory(t) : n = t, [this.sessionId, this.inputNames, this.outputNames] = await Kl(n, r), Ve(); - } - async dispose() { - return Yl(this.sessionId); - } - async run(t, r, n) { - Le(); - let o = [], i = []; - Object.entries(t).forEach((h) => { - let w = h[0], g = h[1], y = this.inputNames.indexOf(w); - if (y === -1) throw new Error(`invalid input '${w}'`); - o.push(g), i.push(y); - }); - let a = [], d = []; - Object.entries(r).forEach((h) => { - let w = h[0], g = h[1], y = this.outputNames.indexOf(w); - if (y === -1) throw new Error(`invalid output '${w}'`); - a.push(g), d.push(y); - }); - let l = o.map((h, w) => Ql(h, () => `input "${this.inputNames[i[w]]}"`)), c = a.map((h, w) => h ? Ql(h, () => `output "${this.outputNames[d[w]]}"`) : null), m = await Xl(this.sessionId, i, l, d, c, n), u = {}; - for (let h = 0; h < m.length; h++) u[this.outputNames[d[h]]] = a[h] ?? Fh(m[h]); - return Ve(), u; - } - startProfiling() { - } - endProfiling() { - Zl(this.sessionId); - } - }; - }); - var tc = {}; - Gt(tc, { OnnxruntimeWebAssemblyBackend: () => fn, initializeFlags: () => ec, wasmBackend: () => qh }); - var ec, fn, qh, rc = V(() => { - "use strict"; - Ke(); - Po(); - Jl(); - qt(); - ec = () => { - if ((typeof _e.wasm.initTimeout != "number" || _e.wasm.initTimeout < 0) && (_e.wasm.initTimeout = 0), _e.wasm.simd === false && console.warn('Deprecated property "env.wasm.simd" is set to false. non-SIMD build is no longer provided, and this setting will be ignored.'), typeof _e.wasm.proxy != "boolean" && (_e.wasm.proxy = false), typeof _e.wasm.trace != "boolean" && (_e.wasm.trace = false), typeof _e.wasm.numThreads != "number" || !Number.isInteger(_e.wasm.numThreads) || _e.wasm.numThreads <= 0) if (typeof self < "u" && !self.crossOriginIsolated) _e.wasm.numThreads = 1; - else { - let e = typeof navigator > "u" ? Vn("node:os").cpus().length : navigator.hardwareConcurrency; - _e.wasm.numThreads = Math.min(4, Math.ceil((e || 1) / 2)); - } - }, fn = class { - async init(t) { - ec(), await Fl(), await ql(t); - } - async createInferenceSessionHandler(t, r) { - let n = new mn(); - return await n.loadModel(t, r), Promise.resolve(n); - } - }, qh = new fn(); - }); - Ke(); - Ke(); - Ke(); - var Ia = "1.20.0-dev.20241016-2b8fc5529b"; - var Hx = Hn; - { - let e = (rc(), br(tc)).wasmBackend; - St("webgpu", e, 5), St("webnn", e, 5), St("cpu", e, 10), St("wasm", e, 10); - } - Object.defineProperty(_e.versions, "web", { value: Ia, enumerable: true }); - } - ), - /***/ - "./src/backends/onnx.js": ( - /*!******************************!*\ - !*** ./src/backends/onnx.js ***! - \******************************/ - /***/ - (__unused_webpack___webpack_module__, __webpack_exports__2, __webpack_require__2) => { - var onnxruntime_node__WEBPACK_IMPORTED_MODULE_1___namespace_cache; - __webpack_require__2.r(__webpack_exports__2); - __webpack_require__2.d(__webpack_exports__2, { - /* harmony export */ - Tensor: () => ( - /* reexport safe */ - onnxruntime_common__WEBPACK_IMPORTED_MODULE_3__.Tensor - ), - /* harmony export */ - createInferenceSession: () => ( - /* binding */ - createInferenceSession - ), - /* harmony export */ - deviceToExecutionProviders: () => ( - /* binding */ - deviceToExecutionProviders - ), - /* harmony export */ - isONNXProxy: () => ( - /* binding */ - isONNXProxy - ), - /* harmony export */ - isONNXTensor: () => ( - /* binding */ - isONNXTensor - ) - /* harmony export */ - }); - var _env_js__WEBPACK_IMPORTED_MODULE_0__2 = __webpack_require__2( - /*! ../env.js */ - "./src/env.js" - ); - var onnxruntime_node__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__2( - /*! onnxruntime-node */ - "?2ce3" - ); - var _onnxruntime_webgpu__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__2( - /*! #onnxruntime-webgpu */ - "./node_modules/onnxruntime-web/dist/ort.webgpu.bundle.min.mjs" - ); - var onnxruntime_common__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__2( - /*! onnxruntime-common */ - "./node_modules/onnxruntime-common/dist/esm/index.js" - ); - const DEVICE_TO_EXECUTION_PROVIDER_MAPPING = Object.freeze({ - auto: null, - // Auto-detect based on device and environment - gpu: null, - // Auto-detect GPU - cpu: "cpu", - // CPU - wasm: "wasm", - // WebAssembly - webgpu: "webgpu", - // WebGPU - cuda: "cuda", - // CUDA - dml: "dml", - // DirectML - webnn: { name: "webnn", deviceType: "cpu" }, - // WebNN (default) - "webnn-npu": { name: "webnn", deviceType: "npu" }, - // WebNN NPU - "webnn-gpu": { name: "webnn", deviceType: "gpu" }, - // WebNN GPU - "webnn-cpu": { name: "webnn", deviceType: "cpu" } - // WebNN CPU - }); - const supportedDevices = []; - let defaultDevices; - let ONNX; - const ORT_SYMBOL = Symbol.for("onnxruntime"); - if (ORT_SYMBOL in globalThis) { - ONNX = globalThis[ORT_SYMBOL]; - } else if (_env_js__WEBPACK_IMPORTED_MODULE_0__2.apis.IS_NODE_ENV) { - ONNX = onnxruntime_node__WEBPACK_IMPORTED_MODULE_1__ ?? (onnxruntime_node__WEBPACK_IMPORTED_MODULE_1___namespace_cache || (onnxruntime_node__WEBPACK_IMPORTED_MODULE_1___namespace_cache = __webpack_require__2.t(onnxruntime_node__WEBPACK_IMPORTED_MODULE_1__, 2))); - switch (process.platform) { - case "win32": - supportedDevices.push("dml"); - break; - case "linux": - if (process.arch === "x64") { - supportedDevices.push("cuda"); - } - break; - case "darwin": - break; - } - supportedDevices.push("cpu"); - defaultDevices = ["cpu"]; - } else { - ONNX = _onnxruntime_webgpu__WEBPACK_IMPORTED_MODULE_2__; - if (_env_js__WEBPACK_IMPORTED_MODULE_0__2.apis.IS_WEBNN_AVAILABLE) { - supportedDevices.push("webnn-npu", "webnn-gpu", "webnn-cpu", "webnn"); - } - if (_env_js__WEBPACK_IMPORTED_MODULE_0__2.apis.IS_WEBGPU_AVAILABLE) { - supportedDevices.push("webgpu"); - } - supportedDevices.push("wasm"); - defaultDevices = ["wasm"]; - } - const InferenceSession = ONNX.InferenceSession; - function deviceToExecutionProviders(device = null) { - if (!device) return defaultDevices; - switch (device) { - case "auto": - return supportedDevices; - case "gpu": - return supportedDevices.filter( - (x) => ["webgpu", "cuda", "dml", "webnn-gpu"].includes(x) - ); - } - if (supportedDevices.includes(device)) { - return [DEVICE_TO_EXECUTION_PROVIDER_MAPPING[device] ?? device]; - } - throw new Error(`Unsupported device: "${device}". Should be one of: ${supportedDevices.join(", ")}.`); - } - let wasmInitPromise = null; - async function createInferenceSession(buffer, session_options, session_config) { - if (wasmInitPromise) { - await wasmInitPromise; - } - const sessionPromise = InferenceSession.create(buffer, session_options); - wasmInitPromise ?? (wasmInitPromise = sessionPromise); - const session = await sessionPromise; - session.config = session_config; - return session; - } - function isONNXTensor(x) { - return x instanceof ONNX.Tensor; - } - const ONNX_ENV = ONNX == null ? void 0 : ONNX.env; - if (ONNX_ENV == null ? void 0 : ONNX_ENV.wasm) { - ONNX_ENV.wasm.wasmPaths = `https://cdn.jsdelivr.net/npm/@huggingface/transformers@${_env_js__WEBPACK_IMPORTED_MODULE_0__2.env.version}/dist/`; - ONNX_ENV.wasm.proxy = false; - if (typeof crossOriginIsolated === "undefined" || !crossOriginIsolated) { - ONNX_ENV.wasm.numThreads = 1; - } - } - if (ONNX_ENV == null ? void 0 : ONNX_ENV.webgpu) { - ONNX_ENV.webgpu.powerPreference = "high-performance"; - } - function isONNXProxy() { - var _a; - return (_a = ONNX_ENV == null ? void 0 : ONNX_ENV.wasm) == null ? void 0 : _a.proxy; - } - _env_js__WEBPACK_IMPORTED_MODULE_0__2.env.backends.onnx = ONNX_ENV; - } - ), - /***/ - "./src/configs.js": ( - /*!************************!*\ - !*** ./src/configs.js ***! - \************************/ - /***/ - (__unused_webpack___webpack_module__, __webpack_exports__2, __webpack_require__2) => { - __webpack_require__2.r(__webpack_exports__2); - __webpack_require__2.d(__webpack_exports__2, { - /* harmony export */ - AutoConfig: () => ( - /* binding */ - AutoConfig - ), - /* harmony export */ - PretrainedConfig: () => ( - /* binding */ - PretrainedConfig - ), - /* harmony export */ - getKeyValueShapes: () => ( - /* binding */ - getKeyValueShapes - ) - /* harmony export */ - }); - var _utils_core_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__2( - /*! ./utils/core.js */ - "./src/utils/core.js" - ); - var _utils_hub_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__2( - /*! ./utils/hub.js */ - "./src/utils/hub.js" - ); - async function loadConfig(pretrained_model_name_or_path, options) { - return await (0, _utils_hub_js__WEBPACK_IMPORTED_MODULE_1__.getModelJSON)(pretrained_model_name_or_path, "config.json", true, options); - } - function getNormalizedConfig(config) { - const mapping = {}; - let init_normalized_config = {}; - switch (config.model_type) { - case "llava": - case "paligemma": - case "florence2": - init_normalized_config = getNormalizedConfig(config.text_config); - break; - case "moondream1": - init_normalized_config = getNormalizedConfig(config.phi_config); - break; - case "musicgen": - init_normalized_config = getNormalizedConfig(config.decoder); - break; - case "gpt2": - case "gptj": - case "jais": - case "codegen": - case "gpt_bigcode": - mapping["num_heads"] = "n_head"; - mapping["num_layers"] = "n_layer"; - mapping["hidden_size"] = "n_embd"; - break; - case "gpt_neox": - case "stablelm": - case "opt": - case "phi": - case "phi3": - case "falcon": - mapping["num_heads"] = "num_attention_heads"; - mapping["num_layers"] = "num_hidden_layers"; - mapping["hidden_size"] = "hidden_size"; - break; - case "llama": - case "granite": - case "cohere": - case "mistral": - case "starcoder2": - case "qwen2": - mapping["num_heads"] = "num_key_value_heads"; - mapping["num_layers"] = "num_hidden_layers"; - mapping["hidden_size"] = "hidden_size"; - mapping["num_attention_heads"] = "num_attention_heads"; - break; - case "gemma": - case "gemma2": - mapping["num_heads"] = "num_key_value_heads"; - mapping["num_layers"] = "num_hidden_layers"; - mapping["dim_kv"] = "head_dim"; - break; - case "openelm": - mapping["num_heads"] = "num_kv_heads"; - mapping["num_layers"] = "num_transformer_layers"; - mapping["dim_kv"] = "head_dim"; - break; - case "gpt_neo": - case "donut-swin": - mapping["num_heads"] = "num_heads"; - mapping["num_layers"] = "num_layers"; - mapping["hidden_size"] = "hidden_size"; - break; - case "bloom": - mapping["num_heads"] = "n_head"; - mapping["num_layers"] = "n_layer"; - mapping["hidden_size"] = "hidden_size"; - break; - case "mpt": - mapping["num_heads"] = "n_heads"; - mapping["num_layers"] = "n_layers"; - mapping["hidden_size"] = "d_model"; - break; - case "t5": - case "mt5": - case "longt5": - mapping["num_decoder_layers"] = "num_decoder_layers"; - mapping["num_decoder_heads"] = "num_heads"; - mapping["decoder_dim_kv"] = "d_kv"; - mapping["num_encoder_layers"] = "num_layers"; - mapping["num_encoder_heads"] = "num_heads"; - mapping["encoder_dim_kv"] = "d_kv"; - break; - case "bart": - case "mbart": - case "marian": - case "whisper": - case "m2m_100": - case "blenderbot": - case "blenderbot-small": - case "florence2_language": - mapping["num_decoder_layers"] = "decoder_layers"; - mapping["num_decoder_heads"] = "decoder_attention_heads"; - mapping["decoder_hidden_size"] = "d_model"; - mapping["num_encoder_layers"] = "encoder_layers"; - mapping["num_encoder_heads"] = "encoder_attention_heads"; - mapping["encoder_hidden_size"] = "d_model"; - break; - case "speecht5": - mapping["num_decoder_layers"] = "decoder_layers"; - mapping["num_decoder_heads"] = "decoder_attention_heads"; - mapping["decoder_hidden_size"] = "hidden_size"; - mapping["num_encoder_layers"] = "encoder_layers"; - mapping["num_encoder_heads"] = "encoder_attention_heads"; - mapping["encoder_hidden_size"] = "hidden_size"; - break; - case "trocr": - mapping["num_encoder_layers"] = mapping["num_decoder_layers"] = "decoder_layers"; - mapping["num_encoder_heads"] = mapping["num_decoder_heads"] = "decoder_attention_heads"; - mapping["encoder_hidden_size"] = mapping["decoder_hidden_size"] = "d_model"; - break; - case "musicgen_decoder": - mapping["num_encoder_layers"] = mapping["num_decoder_layers"] = "num_hidden_layers"; - mapping["num_encoder_heads"] = mapping["num_decoder_heads"] = "num_attention_heads"; - mapping["encoder_hidden_size"] = mapping["decoder_hidden_size"] = "hidden_size"; - break; - case "vision-encoder-decoder": - const decoderConfig = getNormalizedConfig(config.decoder); - const add_encoder_pkv = "num_decoder_layers" in decoderConfig; - const result = (0, _utils_core_js__WEBPACK_IMPORTED_MODULE_0__.pick)(config, ["model_type", "is_encoder_decoder"]); - if (add_encoder_pkv) { - result.num_decoder_layers = decoderConfig.num_decoder_layers; - result.num_decoder_heads = decoderConfig.num_decoder_heads; - result.decoder_hidden_size = decoderConfig.decoder_hidden_size; - result.num_encoder_layers = decoderConfig.num_encoder_layers; - result.num_encoder_heads = decoderConfig.num_encoder_heads; - result.encoder_hidden_size = decoderConfig.encoder_hidden_size; - } else { - result.num_layers = decoderConfig.num_layers; - result.num_heads = decoderConfig.num_heads; - result.hidden_size = decoderConfig.hidden_size; - } - return result; - } - const normalized_config = { - ...init_normalized_config, - ...(0, _utils_core_js__WEBPACK_IMPORTED_MODULE_0__.pick)(config, ["model_type", "multi_query", "is_encoder_decoder"]) - }; - for (const key in mapping) { - normalized_config[key] = config[mapping[key]]; - } - return normalized_config; - } - function getKeyValueShapes(config, { - prefix = "past_key_values" - } = {}) { - const decoderFeeds = {}; - const normalized_config = config.normalized_config; - const batch_size = 1; - if (normalized_config.is_encoder_decoder && ("num_encoder_heads" in normalized_config && "num_decoder_heads" in normalized_config)) { - const encoder_dim_kv = normalized_config.encoder_dim_kv ?? normalized_config.encoder_hidden_size / normalized_config.num_encoder_heads; - const decoder_dim_kv = normalized_config.decoder_dim_kv ?? normalized_config.decoder_hidden_size / normalized_config.num_decoder_heads; - const encoder_dims = [batch_size, normalized_config.num_encoder_heads, 0, encoder_dim_kv]; - const decoder_dims = [batch_size, normalized_config.num_decoder_heads, 0, decoder_dim_kv]; - for (let i = 0; i < normalized_config.num_decoder_layers; ++i) { - decoderFeeds[`${prefix}.${i}.encoder.key`] = encoder_dims; - decoderFeeds[`${prefix}.${i}.encoder.value`] = encoder_dims; - decoderFeeds[`${prefix}.${i}.decoder.key`] = decoder_dims; - decoderFeeds[`${prefix}.${i}.decoder.value`] = decoder_dims; - } - } else { - const num_heads = normalized_config.num_heads; - const num_layers = normalized_config.num_layers; - const dim_kv = normalized_config.dim_kv ?? normalized_config.hidden_size / (normalized_config.num_attention_heads ?? num_heads); - if (normalized_config.model_type === "falcon") { - const dims = [batch_size * num_heads, 0, dim_kv]; - for (let i = 0; i < num_layers; ++i) { - decoderFeeds[`${prefix}.${i}.key`] = dims; - decoderFeeds[`${prefix}.${i}.value`] = dims; - } - } else if (normalized_config.multi_query) { - const dims = [batch_size * num_heads, 0, 2 * dim_kv]; - for (let i = 0; i < num_layers; ++i) { - decoderFeeds[`${prefix}.${i}.key_value`] = dims; - } - } else if (normalized_config.model_type === "bloom") { - const keyDims = [batch_size * num_heads, dim_kv, 0]; - const valueDims = [batch_size * num_heads, 0, dim_kv]; - for (let i = 0; i < num_layers; ++i) { - decoderFeeds[`${prefix}.${i}.key`] = keyDims; - decoderFeeds[`${prefix}.${i}.value`] = valueDims; - } - } else if (normalized_config.model_type === "openelm") { - for (let i = 0; i < num_layers; ++i) { - const dims = [batch_size, num_heads[i], 0, dim_kv]; - decoderFeeds[`${prefix}.${i}.key`] = dims; - decoderFeeds[`${prefix}.${i}.value`] = dims; - } - } else { - const dims = [batch_size, num_heads, 0, dim_kv]; - for (let i = 0; i < num_layers; ++i) { - decoderFeeds[`${prefix}.${i}.key`] = dims; - decoderFeeds[`${prefix}.${i}.value`] = dims; - } - } - } - return decoderFeeds; - } - class PretrainedConfig { - /** - * Create a new PreTrainedTokenizer instance. - * @param {Object} configJSON The JSON of the config. - */ - constructor(configJSON) { - // NOTE: Typo in original - /** @type {string|null} */ - __publicField(this, "model_type", null); - /** @type {boolean} */ - __publicField(this, "is_encoder_decoder", false); - /** @type {number} */ - __publicField(this, "max_position_embeddings"); - /** @type {TransformersJSConfig} */ - __publicField(this, "transformers.js_config"); - Object.assign(this, configJSON); - this.normalized_config = getNormalizedConfig(this); - } - /** - * Loads a pre-trained config from the given `pretrained_model_name_or_path`. - * - * @param {string} pretrained_model_name_or_path The path to the pre-trained config. - * @param {PretrainedOptions} options Additional options for loading the config. - * @throws {Error} Throws an error if the config.json is not found in the `pretrained_model_name_or_path`. - * - * @returns {Promise} A new instance of the `PretrainedConfig` class. - */ - static async from_pretrained(pretrained_model_name_or_path, { - progress_callback = null, - config = null, - cache_dir = null, - local_files_only = false, - revision = "main" - } = {}) { - if (config && !(config instanceof PretrainedConfig)) { - config = new PretrainedConfig(config); - } - const data = config ?? await loadConfig(pretrained_model_name_or_path, { - progress_callback, - config, - cache_dir, - local_files_only, - revision - }); - return new this(data); - } - } - class AutoConfig { - /** @type {typeof PretrainedConfig.from_pretrained} */ - static async from_pretrained(...args) { - return PretrainedConfig.from_pretrained(...args); - } - } - } - ), - /***/ - "./src/env.js": ( - /*!********************!*\ - !*** ./src/env.js ***! - \********************/ - /***/ - (__unused_webpack___webpack_module__, __webpack_exports__2, __webpack_require__2) => { - var _a; - __webpack_require__2.r(__webpack_exports__2); - __webpack_require__2.d(__webpack_exports__2, { - /* harmony export */ - apis: () => ( - /* binding */ - apis - ), - /* harmony export */ - env: () => ( - /* binding */ - env - ) - /* harmony export */ - }); - var fs__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__2( - /*! fs */ - "?569f" - ); - var path__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__2( - /*! path */ - "?3f59" - ); - var url__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__2( - /*! url */ - "?154a" - ); - const VERSION = "3.0.0"; - const IS_BROWSER_ENV = typeof self !== "undefined"; - const IS_WEBWORKER_ENV = IS_BROWSER_ENV && self.constructor.name === "DedicatedWorkerGlobalScope"; - const IS_WEB_CACHE_AVAILABLE = IS_BROWSER_ENV && "caches" in self; - const IS_WEBGPU_AVAILABLE = typeof navigator !== "undefined" && "gpu" in navigator; - const IS_WEBNN_AVAILABLE = typeof navigator !== "undefined" && "ml" in navigator; - const IS_PROCESS_AVAILABLE = typeof process !== "undefined"; - const IS_NODE_ENV = IS_PROCESS_AVAILABLE && ((_a = process == null ? void 0 : process.release) == null ? void 0 : _a.name) === "node"; - const IS_FS_AVAILABLE = !isEmpty(fs__WEBPACK_IMPORTED_MODULE_0__); - const IS_PATH_AVAILABLE = !isEmpty(path__WEBPACK_IMPORTED_MODULE_1__); - const apis = Object.freeze({ - /** Whether we are running in a browser environment */ - IS_BROWSER_ENV, - /** Whether we are running in a web worker environment */ - IS_WEBWORKER_ENV, - /** Whether the Cache API is available */ - IS_WEB_CACHE_AVAILABLE, - /** Whether the WebGPU API is available */ - IS_WEBGPU_AVAILABLE, - /** Whether the WebNN API is available */ - IS_WEBNN_AVAILABLE, - /** Whether the Node.js process API is available */ - IS_PROCESS_AVAILABLE, - /** Whether we are running in a Node.js environment */ - IS_NODE_ENV, - /** Whether the filesystem API is available */ - IS_FS_AVAILABLE, - /** Whether the path API is available */ - IS_PATH_AVAILABLE - }); - const RUNNING_LOCALLY = IS_FS_AVAILABLE && IS_PATH_AVAILABLE; - const dirname__ = RUNNING_LOCALLY ? path__WEBPACK_IMPORTED_MODULE_1__.dirname(path__WEBPACK_IMPORTED_MODULE_1__.dirname(url__WEBPACK_IMPORTED_MODULE_2__.fileURLToPath(META_URL))) : "./"; - const DEFAULT_CACHE_DIR = RUNNING_LOCALLY ? path__WEBPACK_IMPORTED_MODULE_1__.join(dirname__, "/.cache/") : null; - const DEFAULT_LOCAL_MODEL_PATH = "/models/"; - const localModelPath = RUNNING_LOCALLY ? path__WEBPACK_IMPORTED_MODULE_1__.join(dirname__, DEFAULT_LOCAL_MODEL_PATH) : DEFAULT_LOCAL_MODEL_PATH; - const env = { - version: VERSION, - /////////////////// Backends settings /////////////////// - // NOTE: These will be populated later by the backends themselves. - backends: { - // onnxruntime-web/onnxruntime-node - onnx: {} - }, - /////////////////// Model settings /////////////////// - allowRemoteModels: true, - remoteHost: "https://huggingface.co/", - remotePathTemplate: "{model}/resolve/{revision}/", - allowLocalModels: !IS_BROWSER_ENV, - localModelPath, - useFS: IS_FS_AVAILABLE, - /////////////////// Cache settings /////////////////// - useBrowserCache: IS_WEB_CACHE_AVAILABLE, - useFSCache: IS_FS_AVAILABLE, - cacheDir: DEFAULT_CACHE_DIR, - useCustomCache: false, - customCache: null - ////////////////////////////////////////////////////// - }; - function isEmpty(obj) { - return Object.keys(obj).length === 0; - } - } - ), - /***/ - "./src/generation/configuration_utils.js": ( - /*!***********************************************!*\ - !*** ./src/generation/configuration_utils.js ***! - \***********************************************/ - /***/ - (__unused_webpack___webpack_module__, __webpack_exports__2, __webpack_require__2) => { - __webpack_require__2.r(__webpack_exports__2); - __webpack_require__2.d(__webpack_exports__2, { - /* harmony export */ - GenerationConfig: () => ( - /* binding */ - GenerationConfig - ) - /* harmony export */ - }); - var _utils_core_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__2( - /*! ../utils/core.js */ - "./src/utils/core.js" - ); - class GenerationConfig { - /** - * - * @param {GenerationConfig|import('../configs.js').PretrainedConfig} config - */ - constructor(config) { - // Parameters that control the length of the output - /** - * The maximum length the generated tokens can have. - * Corresponds to the length of the input prompt + `max_new_tokens`. - * Its effect is overridden by `max_new_tokens`, if also set. - * @type {number} - * @default 20 - */ - __publicField(this, "max_length", 20); - /** - * The maximum numbers of tokens to generate, ignoring the number of tokens in the prompt. - * @type {number} - * @default null - */ - __publicField(this, "max_new_tokens", null); - /** - * The minimum length of the sequence to be generated. - * Corresponds to the length of the input prompt + `min_new_tokens`. - * Its effect is overridden by `min_new_tokens`, if also set. - * @type {number} - * @default 0 - */ - __publicField(this, "min_length", 0); - /** - * The minimum numbers of tokens to generate, ignoring the number of tokens in the prompt. - * @type {number} - * @default null - */ - __publicField(this, "min_new_tokens", null); - /** - * Controls the stopping condition for beam-based methods, like beam-search. It accepts the following values: - * - `true`, where the generation stops as soon as there are `num_beams` complete candidates; - * - `false`, where an heuristic is applied and the generation stops when is it very unlikely to find better candidates; - * - `"never"`, where the beam search procedure only stops when there cannot be better candidates (canonical beam search algorithm). - * @type {boolean|"never"} - * @default false - */ - __publicField(this, "early_stopping", false); - /** - * The maximum amount of time you allow the computation to run for in seconds. - * Generation will still finish the current pass after allocated time has been passed. - * @type {number} - * @default null - */ - __publicField(this, "max_time", null); - // Parameters that control the generation strategy used - /** - * Whether or not to use sampling; use greedy decoding otherwise. - * @type {boolean} - * @default false - */ - __publicField(this, "do_sample", false); - /** - * Number of beams for beam search. 1 means no beam search. - * @type {number} - * @default 1 - */ - __publicField(this, "num_beams", 1); - /** - * Number of groups to divide `num_beams` into in order to ensure diversity among different groups of beams. - * See [this paper](https://arxiv.org/pdf/1610.02424.pdf) for more details. - * @type {number} - * @default 1 - */ - __publicField(this, "num_beam_groups", 1); - /** - * The values balance the model confidence and the degeneration penalty in contrastive search decoding. - * @type {number} - * @default null - */ - __publicField(this, "penalty_alpha", null); - /** - * Whether or not the model should use the past last key/values attentions (if applicable to the model) to speed up decoding. - * @type {boolean} - * @default true - */ - __publicField(this, "use_cache", true); - // Parameters for manipulation of the model output logits - /** - * The value used to modulate the next token probabilities. - * @type {number} - * @default 1.0 - */ - __publicField(this, "temperature", 1); - /** - * The number of highest probability vocabulary tokens to keep for top-k-filtering. - * @type {number} - * @default 50 - */ - __publicField(this, "top_k", 50); - /** - * If set to float < 1, only the smallest set of most probable tokens with probabilities that add up to `top_p` or higher are kept for generation. - * @type {number} - * @default 1.0 - */ - __publicField(this, "top_p", 1); - /** - * Local typicality measures how similar the conditional probability of predicting a target token next is to the expected conditional probability of predicting a random token next, given the partial text already generated. - * If set to float < 1, the smallest set of the most locally typical tokens with probabilities that add up to `typical_p` or higher are kept for generation. - * See [this paper](https://arxiv.org/pdf/2202.00666.pdf) for more details. - * @type {number} - * @default 1.0 - */ - __publicField(this, "typical_p", 1); - /** - * If set to float strictly between 0 and 1, only tokens with a conditional probability greater than `epsilon_cutoff` will be sampled. - * In the paper, suggested values range from 3e-4 to 9e-4, depending on the size of the model. - * See [Truncation Sampling as Language Model Desmoothing](https://arxiv.org/abs/2210.15191) for more details. - * @type {number} - * @default 0.0 - */ - __publicField(this, "epsilon_cutoff", 0); - /** - * Eta sampling is a hybrid of locally typical sampling and epsilon sampling. - * If set to float strictly between 0 and 1, a token is only considered if it is greater than either `eta_cutoff` or `sqrt(eta_cutoff) * exp(-entropy(softmax(next_token_logits)))`. - * The latter term is intuitively the expected next token probability, scaled by `sqrt(eta_cutoff)`. In the paper, suggested values range from 3e-4 to 2e-3, depending on the size of the model. - * See [Truncation Sampling as Language Model Desmoothing](https://arxiv.org/abs/2210.15191) for more details. - * @type {number} - * @default 0.0 - */ - __publicField(this, "eta_cutoff", 0); - /** - * This value is subtracted from a beam's score if it generates a token same as any beam from other group at a particular time. - * Note that `diversity_penalty` is only effective if `group beam search` is enabled. - * @type {number} - * @default 0.0 - */ - __publicField(this, "diversity_penalty", 0); - /** - * The parameter for repetition penalty. 1.0 means no penalty. - * See [this paper](https://arxiv.org/pdf/1909.05858.pdf) for more details. - * @type {number} - * @default 1.0 - */ - __publicField(this, "repetition_penalty", 1); - /** - * The paramater for encoder_repetition_penalty. - * An exponential penalty on sequences that are not in the original input. - * 1.0 means no penalty. - * @type {number} - * @default 1.0 - */ - __publicField(this, "encoder_repetition_penalty", 1); - /** - * Exponential penalty to the length that is used with beam-based generation. - * It is applied as an exponent to the sequence length, which in turn is used to divide the score of the sequence. - * Since the score is the log likelihood of the sequence (i.e. negative), `length_penalty` > 0.0 promotes longer sequences, while `length_penalty` < 0.0 encourages shorter sequences. - * @type {number} - * @default 1.0 - */ - __publicField(this, "length_penalty", 1); - /** - * If set to int > 0, all ngrams of that size can only occur once. - * @type {number} - * @default 0 - */ - __publicField(this, "no_repeat_ngram_size", 0); - /** - * List of token ids that are not allowed to be generated. - * In order to get the token ids of the words that should not appear in the generated text, use - * `tokenizer(bad_words, { add_prefix_space: true, add_special_tokens: false }).input_ids`. - * @type {number[][]} - * @default null - */ - __publicField(this, "bad_words_ids", null); - /** - * List of token ids that must be generated. - * If given a `number[][]`, this is treated as a simple list of words that must be included, the opposite to `bad_words_ids`. - * If given `number[][][]`, this triggers a [disjunctive constraint](https://github.com/huggingface/transformers/issues/14081), where one can allow different forms of each word. - * @type {number[][]|number[][][]} - * @default null - */ - __publicField(this, "force_words_ids", null); - /** - * Whether to renormalize the logits after applying all the logits processors or warpers (including the custom ones). - * It's highly recommended to set this flag to `true` as the search algorithms suppose the score logits are normalized but some logit processors or warpers break the normalization. - * @type {boolean} - * @default false - */ - __publicField(this, "renormalize_logits", false); - /** - * Custom constraints that can be added to the generation to ensure that the output will contain the use of certain tokens as defined by `Constraint` objects, in the most sensible way possible. - * @type {Object[]} - * @default null - */ - __publicField(this, "constraints", null); - /** - * The id of the token to force as the first generated token after the `decoder_start_token_id`. - * Useful for multilingual models like mBART where the first generated token needs to be the target language token. - * @type {number} - * @default null - */ - __publicField(this, "forced_bos_token_id", null); - /** - * The id of the token to force as the last generated token when `max_length` is reached. - * Optionally, use a list to set multiple *end-of-sequence* tokens. - * @type {number|number[]} - * @default null - */ - __publicField(this, "forced_eos_token_id", null); - /** - * Whether to remove possible *nan* and *inf* outputs of the model to prevent the generation method to crash. Note that using `remove_invalid_values` can slow down generation. - * @type {boolean} - */ - __publicField(this, "remove_invalid_values", false); - /** - * This Tuple adds an exponentially increasing length penalty, after a certain amount of tokens have been generated. - * The tuple shall consist of: `(start_index, decay_factor)` where `start_index` indicates where penalty starts and `decay_factor` represents the factor of exponential decay. - * @type {[number, number]} - * @default null - */ - __publicField(this, "exponential_decay_length_penalty", null); - /** - * A list of tokens that will be suppressed at generation. - * The `SuppressTokens` logit processor will set their log probs to `-inf` so that they are not sampled. - * @type {number[]} - * @default null - */ - __publicField(this, "suppress_tokens", null); - /** - * A list of tokens that will be suppressed at the beginning of the generation. - * The `SuppressBeginTokens` logit processor will set their log probs to `-inf` so that they are not sampled. - * @type {number[]} - * @default null - */ - __publicField(this, "begin_suppress_tokens", null); - /** - * A list of pairs of integers which indicates a mapping from generation indices to token indices that will be forced before sampling. - * For example, `[[1, 123]]` means the second generated token will always be a token of index 123. - * @type {[number, number][]} - * @default null - */ - __publicField(this, "forced_decoder_ids", null); - /** - * The guidance scale for classifier free guidance (CFG). CFG is enabled by setting `guidance_scale > 1`. - * Higher guidance scale encourages the model to generate samples that are more closely linked to the input - * prompt, usually at the expense of poorer quality. - * @type {number} - * @default null - */ - __publicField(this, "guidance_scale", null); - // Parameters that define the output variables of `generate` - /** - * The number of independently computed returned sequences for each element in the batch. - * @type {number} - * @default 1 - */ - __publicField(this, "num_return_sequences", 1); - /** - * Whether or not to return the attentions tensors of all attention layers. - * See `attentions` under returned tensors for more details. - * @type {boolean} - * @default false - */ - __publicField(this, "output_attentions", false); - /** - * Whether or not to return the hidden states of all layers. - * See `hidden_states` under returned tensors for more details. - * @type {boolean} - * @default false - */ - __publicField(this, "output_hidden_states", false); - /** - * Whether or not to return the prediction scores. - * See `scores` under returned tensors for more details. - * @type {boolean} - * @default false - */ - __publicField(this, "output_scores", false); - /** - * Whether or not to return a `ModelOutput` instead of a plain tuple. - * @type {boolean} - * @default false - */ - __publicField(this, "return_dict_in_generate", false); - // Special tokens that can be used at generation time - /** - * The id of the *padding* token. - * @type {number} - * @default null - */ - __publicField(this, "pad_token_id", null); - /** - * The id of the *beginning-of-sequence* token. - * @type {number} - * @default null - */ - __publicField(this, "bos_token_id", null); - /** - * The id of the *end-of-sequence* token. - * Optionally, use a list to set multiple *end-of-sequence* tokens. - * @type {number|number[]} - * @default null - */ - __publicField(this, "eos_token_id", null); - // Generation parameters exclusive to encoder-decoder models - /** - * If set to int > 0, all ngrams of that size that occur in the `encoder_input_ids` cannot occur in the `decoder_input_ids`. - * @type {number} - * @default 0 - */ - __publicField(this, "encoder_no_repeat_ngram_size", 0); - /** - * If an encoder-decoder model starts decoding with a different token than *bos*, the id of that token. - * @type {number} - * @default null - */ - __publicField(this, "decoder_start_token_id", null); - // Wild card - /** - * Additional generation kwargs will be forwarded to the `generate` function of the model. - * Kwargs that are not present in `generate`'s signature will be used in the model forward pass. - * @type {Object} - * @default {} - */ - __publicField(this, "generation_kwargs", {}); - Object.assign(this, (0, _utils_core_js__WEBPACK_IMPORTED_MODULE_0__.pick)(config, Object.getOwnPropertyNames(this))); - } - } - } - ), - /***/ - "./src/generation/logits_process.js": ( - /*!******************************************!*\ - !*** ./src/generation/logits_process.js ***! - \******************************************/ - /***/ - (__unused_webpack___webpack_module__, __webpack_exports__2, __webpack_require__2) => { - __webpack_require__2.r(__webpack_exports__2); - __webpack_require__2.d(__webpack_exports__2, { - /* harmony export */ - ClassifierFreeGuidanceLogitsProcessor: () => ( - /* binding */ - ClassifierFreeGuidanceLogitsProcessor - ), - /* harmony export */ - ForcedBOSTokenLogitsProcessor: () => ( - /* binding */ - ForcedBOSTokenLogitsProcessor - ), - /* harmony export */ - ForcedEOSTokenLogitsProcessor: () => ( - /* binding */ - ForcedEOSTokenLogitsProcessor - ), - /* harmony export */ - LogitsProcessor: () => ( - /* binding */ - LogitsProcessor - ), - /* harmony export */ - LogitsProcessorList: () => ( - /* binding */ - LogitsProcessorList - ), - /* harmony export */ - LogitsWarper: () => ( - /* binding */ - LogitsWarper - ), - /* harmony export */ - MinLengthLogitsProcessor: () => ( - /* binding */ - MinLengthLogitsProcessor - ), - /* harmony export */ - MinNewTokensLengthLogitsProcessor: () => ( - /* binding */ - MinNewTokensLengthLogitsProcessor - ), - /* harmony export */ - NoBadWordsLogitsProcessor: () => ( - /* binding */ - NoBadWordsLogitsProcessor - ), - /* harmony export */ - NoRepeatNGramLogitsProcessor: () => ( - /* binding */ - NoRepeatNGramLogitsProcessor - ), - /* harmony export */ - RepetitionPenaltyLogitsProcessor: () => ( - /* binding */ - RepetitionPenaltyLogitsProcessor - ), - /* harmony export */ - SuppressTokensAtBeginLogitsProcessor: () => ( - /* binding */ - SuppressTokensAtBeginLogitsProcessor - ), - /* harmony export */ - TemperatureLogitsWarper: () => ( - /* binding */ - TemperatureLogitsWarper - ), - /* harmony export */ - TopKLogitsWarper: () => ( - /* binding */ - TopKLogitsWarper - ), - /* harmony export */ - TopPLogitsWarper: () => ( - /* binding */ - TopPLogitsWarper - ), - /* harmony export */ - WhisperTimeStampLogitsProcessor: () => ( - /* binding */ - WhisperTimeStampLogitsProcessor - ) - /* harmony export */ - }); - var _utils_generic_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__2( - /*! ../utils/generic.js */ - "./src/utils/generic.js" - ); - var _utils_tensor_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__2( - /*! ../utils/tensor.js */ - "./src/utils/tensor.js" - ); - var _utils_maths_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__2( - /*! ../utils/maths.js */ - "./src/utils/maths.js" - ); - class LogitsProcessor extends _utils_generic_js__WEBPACK_IMPORTED_MODULE_0__.Callable { - /** - * Apply the processor to the input logits. - * - * @abstract - * @param {bigint[][]} input_ids The input ids. - * @param {Tensor} logits The logits to process. - * @throws {Error} Throws an error if `_call` is not implemented in the subclass. - */ - _call(input_ids, logits) { - throw Error("`_call` should be implemented in a subclass"); - } - } - class LogitsWarper extends _utils_generic_js__WEBPACK_IMPORTED_MODULE_0__.Callable { - /** - * Apply the processor to the input logits. - * - * @abstract - * @param {bigint[][]} input_ids The input ids. - * @param {Tensor} logits The logits to process. - * @throws {Error} Throws an error if `_call` is not implemented in the subclass. - */ - _call(input_ids, logits) { - throw Error("`_call` should be implemented in a subclass"); - } - } - class LogitsProcessorList extends _utils_generic_js__WEBPACK_IMPORTED_MODULE_0__.Callable { - /** - * Constructs a new instance of `LogitsProcessorList`. - */ - constructor() { - super(); - this.processors = []; - } - /** - * Adds a new logits processor to the list. - * - * @param {LogitsProcessor} item The logits processor function to add. - */ - push(item) { - this.processors.push(item); - } - /** - * Adds multiple logits processors to the list. - * - * @param {LogitsProcessor[]} items The logits processor functions to add. - */ - extend(items) { - this.processors.push(...items); - } - /** - * Applies all logits processors in the list to a batch of logits, modifying them in-place. - * - * @param {bigint[][]} input_ids The input IDs for the language model. - * @param {Tensor} logits - */ - _call(input_ids, logits) { - let toReturn = logits; - for (const processor of this.processors) { - toReturn = processor(input_ids, toReturn); - } - return toReturn; - } - [Symbol.iterator]() { - return this.processors.values(); - } - } - class ForcedBOSTokenLogitsProcessor extends LogitsProcessor { - /** - * Create a ForcedBOSTokenLogitsProcessor. - * @param {number} bos_token_id The ID of the beginning-of-sequence token to be forced. - */ - constructor(bos_token_id) { - super(); - this.bos_token_id = bos_token_id; - } - /** - * Apply the BOS token forcing to the logits. - * @param {bigint[][]} input_ids The input IDs. - * @param {Tensor} logits The logits. - * @returns {Object} The logits with BOS token forcing. - */ - _call(input_ids, logits) { - for (let i = 0; i < input_ids.length; ++i) { - if (input_ids[i].length === 1) { - const batch_logits_data = ( - /** @type {Float32Array} */ - logits[i].data - ); - batch_logits_data.fill(-Infinity); - batch_logits_data[this.bos_token_id] = 0; - } - } - return logits; - } - } - class ForcedEOSTokenLogitsProcessor extends LogitsProcessor { - /** - * Create a ForcedEOSTokenLogitsProcessor. - * @param {number} max_length The maximum length of the sequence to be generated. - * @param {number|number[]} eos_token_id The id(s) of the *end-of-sequence* token. - */ - constructor(max_length, eos_token_id) { - super(); - this.max_length = max_length; - this.eos_token_id = Array.isArray(eos_token_id) ? eos_token_id : [eos_token_id]; - } - /** - * Apply the processor to input_ids and logits. - * - * @param {bigint[][]} input_ids The input ids. - * @param {Tensor} logits The logits tensor. - */ - _call(input_ids, logits) { - for (let i = 0; i < input_ids.length; ++i) { - if (input_ids[i].length === this.max_length - 1) { - const batch_logits_data = ( - /** @type {Float32Array} */ - logits[i].data - ); - batch_logits_data.fill(-Infinity); - for (const eos_token of this.eos_token_id) { - batch_logits_data[eos_token] = 0; - } - } - } - return logits; - } - } - class SuppressTokensAtBeginLogitsProcessor extends LogitsProcessor { - /** - * Create a SuppressTokensAtBeginLogitsProcessor. - * @param {number[]} begin_suppress_tokens The IDs of the tokens to suppress. - * @param {number} begin_index The number of tokens to generate before suppressing tokens. - */ - constructor(begin_suppress_tokens, begin_index) { - super(); - this.begin_suppress_tokens = begin_suppress_tokens; - this.begin_index = begin_index; - } - /** - * Apply the BOS token forcing to the logits. - * @param {bigint[][]} input_ids The input IDs. - * @param {Tensor} logits The logits. - * @returns {Object} The logits with BOS token forcing. - */ - _call(input_ids, logits) { - for (let i = 0; i < input_ids.length; ++i) { - if (input_ids[i].length === this.begin_index) { - const batch_logits_data = ( - /** @type {Float32Array} */ - logits[i].data - ); - for (const token_id of this.begin_suppress_tokens) { - batch_logits_data[token_id] = -Infinity; - } - } - } - return logits; - } - } - class WhisperTimeStampLogitsProcessor extends LogitsProcessor { - /** - * Constructs a new WhisperTimeStampLogitsProcessor. - * @param {import('../models/whisper/generation_whisper.js').WhisperGenerationConfig} generate_config The config object passed to the `generate()` method of a transformer model. - * @param {number[]} init_tokens The initial tokens of the input sequence. - */ - constructor(generate_config, init_tokens) { - super(); - this.eos_token_id = Array.isArray(generate_config.eos_token_id) ? generate_config.eos_token_id[0] : generate_config.eos_token_id; - this.no_timestamps_token_id = generate_config.no_timestamps_token_id; - this.timestamp_begin = this.no_timestamps_token_id + 1; - this.begin_index = init_tokens.length; - if (init_tokens.at(-1) === this.no_timestamps_token_id) { - this.begin_index -= 1; - } - this.max_initial_timestamp_index = generate_config.max_initial_timestamp_index; - } - /** - * Modify the logits to handle timestamp tokens. - * @param {bigint[][]} input_ids The input sequence of tokens. - * @param {Tensor} logits The logits output by the model. - * @returns {Tensor} The modified logits. - */ - _call(input_ids, logits) { - for (let i = 0; i < input_ids.length; ++i) { - const batch_logits_data = ( - /** @type {Float32Array} */ - logits[i].data - ); - batch_logits_data[this.no_timestamps_token_id] = -Infinity; - if (input_ids[i].length === this.begin_index - 1) { - batch_logits_data.fill(-Infinity); - batch_logits_data[this.timestamp_begin] = 0; - continue; - } - const seq = input_ids[i].slice(this.begin_index); - const last_was_timestamp = seq.length >= 1 && seq[seq.length - 1] >= this.timestamp_begin; - const penultimate_was_timestamp = seq.length < 2 || seq[seq.length - 2] >= this.timestamp_begin; - if (last_was_timestamp) { - if (penultimate_was_timestamp) { - batch_logits_data.subarray(this.timestamp_begin).fill(-Infinity); - } else { - batch_logits_data.subarray(0, this.eos_token_id).fill(-Infinity); - } - } - if (input_ids[i].length === this.begin_index && this.max_initial_timestamp_index !== null) { - const last_allowed = this.timestamp_begin + this.max_initial_timestamp_index; - batch_logits_data.subarray(last_allowed + 1).fill(-Infinity); - } - const logprobs = (0, _utils_maths_js__WEBPACK_IMPORTED_MODULE_2__.log_softmax)(batch_logits_data); - const timestamp_logprob = Math.log(logprobs.subarray(this.timestamp_begin).map(Math.exp).reduce((a, b) => a + b)); - const max_text_token_logprob = (0, _utils_maths_js__WEBPACK_IMPORTED_MODULE_2__.max)(logprobs.subarray(0, this.timestamp_begin))[0]; - if (timestamp_logprob > max_text_token_logprob) { - batch_logits_data.subarray(0, this.timestamp_begin).fill(-Infinity); - } - } - return logits; - } - } - class NoRepeatNGramLogitsProcessor extends LogitsProcessor { - /** - * Create a NoRepeatNGramLogitsProcessor. - * @param {number} no_repeat_ngram_size The no-repeat-ngram size. All ngrams of this size can only occur once. - */ - constructor(no_repeat_ngram_size) { - super(); - this.no_repeat_ngram_size = no_repeat_ngram_size; - } - /** - * Generate n-grams from a sequence of token ids. - * @param {bigint[]} prevInputIds List of previous input ids - * @returns {Map} Map of generated n-grams - */ - getNgrams(prevInputIds) { - const curLen = prevInputIds.length; - const ngrams = []; - for (let j = 0; j < curLen + 1 - this.no_repeat_ngram_size; ++j) { - const ngram = []; - for (let k = 0; k < this.no_repeat_ngram_size; ++k) { - ngram.push(prevInputIds[j + k]); - } - ngrams.push(ngram.map(Number)); - } - const generatedNgram = /* @__PURE__ */ new Map(); - for (const ngram of ngrams) { - const prevNgram = ngram.slice(0, ngram.length - 1); - const prevNgramKey = JSON.stringify(prevNgram); - const prevNgramValue = generatedNgram.get(prevNgramKey) ?? []; - prevNgramValue.push(ngram[ngram.length - 1]); - generatedNgram.set(prevNgramKey, prevNgramValue); - } - return generatedNgram; - } - /** - * Generate n-grams from a sequence of token ids. - * @param {Map} bannedNgrams Map of banned n-grams - * @param {bigint[]} prevInputIds List of previous input ids - * @returns {number[]} Map of generated n-grams - */ - getGeneratedNgrams(bannedNgrams, prevInputIds) { - const ngramIdx = prevInputIds.slice(prevInputIds.length + 1 - this.no_repeat_ngram_size, prevInputIds.length); - const banned = bannedNgrams.get(JSON.stringify(ngramIdx.map(Number))) ?? []; - return banned; - } - /** - * Calculate banned n-gram tokens - * @param {bigint[]} prevInputIds List of previous input ids - * @returns {number[]} Map of generated n-grams - */ - calcBannedNgramTokens(prevInputIds) { - const bannedTokens = []; - if (prevInputIds.length + 1 < this.no_repeat_ngram_size) { - return bannedTokens; - } else { - const generatedNgrams = this.getNgrams(prevInputIds); - const bannedTokens2 = this.getGeneratedNgrams(generatedNgrams, prevInputIds); - return bannedTokens2; - } - } - /** - * Apply the no-repeat-ngram processor to the logits. - * @param {bigint[][]} input_ids The input IDs. - * @param {Tensor} logits The logits. - * @returns {Object} The logits with no-repeat-ngram processing. - */ - _call(input_ids, logits) { - for (let i = 0; i < input_ids.length; ++i) { - const batch_logits_data = ( - /** @type {Float32Array} */ - logits[i].data - ); - const bannedTokens = this.calcBannedNgramTokens(input_ids[i]); - for (const token of bannedTokens) { - batch_logits_data[token] = -Infinity; - } - } - return logits; - } - } - class RepetitionPenaltyLogitsProcessor extends LogitsProcessor { - /** - * Create a RepetitionPenaltyLogitsProcessor. - * @param {number} penalty The penalty to apply for repeated tokens. - */ - constructor(penalty) { - super(); - this.penalty = penalty; - } - /** - * Apply the repetition penalty to the logits. - * @param {bigint[][]} input_ids The input IDs. - * @param {Tensor} logits The logits. - * @returns {Object} The logits with repetition penalty processing. - */ - _call(input_ids, logits) { - for (let i = 0; i < input_ids.length; ++i) { - const batch_logits_data = ( - /** @type {Float32Array} */ - logits[i].data - ); - for (const input_id of input_ids[i]) { - const token = Number(input_id); - if (batch_logits_data[token] < 0) { - batch_logits_data[token] *= this.penalty; - } else { - batch_logits_data[token] /= this.penalty; - } - } - } - return logits; - } - } - class MinLengthLogitsProcessor extends LogitsProcessor { - /** - * Create a MinLengthLogitsProcessor. - * @param {number} min_length The minimum length below which the score of `eos_token_id` is set to negative infinity. - * @param {number|number[]} eos_token_id The ID/IDs of the end-of-sequence token. - */ - constructor(min_length, eos_token_id) { - super(); - this.min_length = min_length; - this.eos_token_id = Array.isArray(eos_token_id) ? eos_token_id : [eos_token_id]; - } - /** - * Apply logit processor. - * @param {bigint[][]} input_ids The input IDs. - * @param {Tensor} logits The logits. - * @returns {Object} The processed logits. - */ - _call(input_ids, logits) { - for (let i = 0; i < input_ids.length; ++i) { - if (input_ids[i].length < this.min_length) { - const batch_logits_data = ( - /** @type {Float32Array} */ - logits[i].data - ); - for (const eos_token of this.eos_token_id) { - batch_logits_data[eos_token] = -Infinity; - } - } - } - return logits; - } - } - class MinNewTokensLengthLogitsProcessor extends LogitsProcessor { - /** - * Create a MinNewTokensLengthLogitsProcessor. - * @param {number} prompt_length_to_skip The input tokens length. - * @param {number} min_new_tokens The minimum *new* tokens length below which the score of `eos_token_id` is set to negative infinity. - * @param {number|number[]} eos_token_id The ID/IDs of the end-of-sequence token. - */ - constructor(prompt_length_to_skip, min_new_tokens, eos_token_id) { - super(); - this.prompt_length_to_skip = prompt_length_to_skip; - this.min_new_tokens = min_new_tokens; - this.eos_token_id = Array.isArray(eos_token_id) ? eos_token_id : [eos_token_id]; - } - /** - * Apply logit processor. - * @param {bigint[][]} input_ids The input IDs. - * @param {Tensor} logits The logits. - * @returns {Object} The processed logits. - */ - _call(input_ids, logits) { - for (let i = 0; i < input_ids.length; ++i) { - const new_tokens_length = input_ids[i].length - this.prompt_length_to_skip; - if (new_tokens_length < this.min_new_tokens) { - const batch_logits_data = ( - /** @type {Float32Array} */ - logits[i].data - ); - for (const eos_token of this.eos_token_id) { - batch_logits_data[eos_token] = -Infinity; - } - } - } - return logits; - } - } - class NoBadWordsLogitsProcessor extends LogitsProcessor { - /** - * Create a `NoBadWordsLogitsProcessor`. - * @param {number[][]} bad_words_ids List of list of token ids that are not allowed to be generated. - * @param {number|number[]} eos_token_id The id of the *end-of-sequence* token. Optionally, use a list to set multiple *end-of-sequence* tokens. - */ - constructor(bad_words_ids, eos_token_id) { - super(); - this.bad_words_ids = bad_words_ids; - this.eos_token_id = Array.isArray(eos_token_id) ? eos_token_id : [eos_token_id]; - } - /** - * Apply logit processor. - * @param {bigint[][]} input_ids The input IDs. - * @param {Tensor} logits The logits. - * @returns {Object} The processed logits. - */ - _call(input_ids, logits) { - for (let i = 0; i < input_ids.length; ++i) { - const batch_logits_data = ( - /** @type {Float32Array} */ - logits[i].data - ); - const ids = input_ids[i]; - for (const bad_word_ids of this.bad_words_ids) { - let mark = true; - for (let j = 1; j <= bad_word_ids.length - 1 && bad_word_ids.length < ids.length; ++j) { - if (bad_word_ids.at(-j - 1) != ids.at(-j)) { - mark = false; - break; - } - } - if (mark) { - batch_logits_data[bad_word_ids.at(-1)] = -Infinity; - } - } - } - return logits; - } - } - class ClassifierFreeGuidanceLogitsProcessor extends LogitsProcessor { - /** - * Create a `ClassifierFreeGuidanceLogitsProcessor`. - * @param {number} guidance_scale The guidance scale for classifier free guidance (CFG). CFG is enabled by setting `guidance_scale > 1`. - * Higher guidance scale encourages the model to generate samples that are more closely linked to the input - * prompt, usually at the expense of poorer quality. - */ - constructor(guidance_scale) { - super(); - if (guidance_scale <= 1) { - throw new Error( - `Require guidance scale >1 to use the classifier free guidance processor, got guidance scale ${guidance_scale}.` - ); - } - this.guidance_scale = guidance_scale; - } - /** - * Apply logit processor. - * @param {bigint[][]} input_ids The input IDs. - * @param {Tensor} logits The logits. - * @returns {Object} The processed logits. - */ - _call(input_ids, logits) { - if (logits.dims[0] !== 2 * input_ids.length) { - throw new Error( - `Logits should have twice the batch size of the input ids, the first half of batches corresponding to the conditional inputs, and the second half of batches corresponding to the unconditional inputs. Got batch size ${logits.dims[0]} for the logits and ${input_ids.length} for the input ids.` - ); - } - const unguided_bsz = input_ids.length; - const cond_logits = logits.slice([0, unguided_bsz], null); - const uncond_logits = logits.slice([unguided_bsz, logits.dims[0]], null); - for (let i = 0; i < uncond_logits.data.length; ++i) { - uncond_logits.data[i] += (cond_logits.data[i] - uncond_logits.data[i]) * this.guidance_scale; - } - return uncond_logits; - } - } - class TemperatureLogitsWarper extends LogitsWarper { - /** - * Create a `TemperatureLogitsWarper`. - * @param {number} temperature Strictly positive float value used to modulate the logits distribution. - * A value smaller than `1` decreases randomness (and vice versa), with `0` being equivalent to shifting - * all probability mass to the most likely token. - */ - constructor(temperature) { - super(); - if (typeof temperature !== "number" || temperature <= 0) { - let errorMessage = `\`temperature\` (=${temperature}) must be a strictly positive float, otherwise your next token scores will be invalid.`; - if (temperature === 0) { - errorMessage += " If you're looking for greedy decoding strategies, set `do_sample=false`."; - } - } - this.temperature = temperature; - } - /** - * Apply logit warper. - * @param {bigint[][]} input_ids The input IDs. - * @param {Tensor} logits The logits. - * @returns {Object} The processed logits. - */ - _call(input_ids, logits) { - const batch_logits_data = ( - /** @type {Float32Array} */ - logits.data - ); - for (let i = 0; i < batch_logits_data.length; ++i) { - batch_logits_data[i] /= this.temperature; - } - return logits; - } - } - class TopPLogitsWarper extends LogitsWarper { - /** - * Create a `TopPLogitsWarper`. - * @param {number} top_p If set to < 1, only the smallest set of most probable tokens with - * probabilities that add up to `top_p` or higher are kept for generation. - * @param {Object} options Additional options for the top-p sampling. - * @param {number} [options.filter_value=-Infinity] All filtered values will be set to this float value. - * @param {number} [options.min_tokens_to_keep=1] Minimum number of tokens that cannot be filtered. - */ - constructor(top_p, { - filter_value = -Infinity, - min_tokens_to_keep = 1 - } = {}) { - super(); - if (top_p < 0 || top_p > 1) { - throw new Error(`\`top_p\` must be a float > 0 and < 1, but is ${top_p}`); - } - if (!Number.isInteger(min_tokens_to_keep) || min_tokens_to_keep < 1) { - throw new Error(`\`min_tokens_to_keep\` must be a positive integer, but is ${min_tokens_to_keep}`); - } - this.top_p = top_p; - this.filter_value = filter_value; - this.min_tokens_to_keep = min_tokens_to_keep; - } - } - class TopKLogitsWarper extends LogitsWarper { - /** - * Create a `TopKLogitsWarper`. - * @param {number} top_k If set to > 0, only the top `top_k` tokens are kept for generation. - * @param {Object} options Additional options for the top-k sampling. - * @param {number} [options.filter_value=-Infinity] All filtered values will be set to this float value. - * @param {number} [options.min_tokens_to_keep=1] Minimum number of tokens that cannot be filtered. - */ - constructor(top_k, { - filter_value = -Infinity, - min_tokens_to_keep = 1 - } = {}) { - super(); - if (!Number.isInteger(top_k) || top_k < 0) { - throw new Error(`\`top_k\` must be a positive integer, but is ${top_k}`); - } - this.top_k = Math.max(top_k, min_tokens_to_keep); - this.filter_value = filter_value; - } - } - } - ), - /***/ - "./src/generation/logits_sampler.js": ( - /*!******************************************!*\ - !*** ./src/generation/logits_sampler.js ***! - \******************************************/ - /***/ - (__unused_webpack___webpack_module__, __webpack_exports__2, __webpack_require__2) => { - __webpack_require__2.r(__webpack_exports__2); - __webpack_require__2.d(__webpack_exports__2, { - /* harmony export */ - LogitsSampler: () => ( - /* binding */ - LogitsSampler - ) - /* harmony export */ - }); - var _utils_generic_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__2( - /*! ../utils/generic.js */ - "./src/utils/generic.js" - ); - var _utils_tensor_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__2( - /*! ../utils/tensor.js */ - "./src/utils/tensor.js" - ); - var _utils_maths_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__2( - /*! ../utils/maths.js */ - "./src/utils/maths.js" - ); - var _generation_configuration_utils_js__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__2( - /*! ../generation/configuration_utils.js */ - "./src/generation/configuration_utils.js" - ); - class LogitsSampler extends _utils_generic_js__WEBPACK_IMPORTED_MODULE_0__.Callable { - /** - * Creates a new Sampler object with the specified generation config. - * @param {GenerationConfig} generation_config The generation config. - */ - constructor(generation_config) { - super(); - this.generation_config = generation_config; - } - /** - * Executes the sampler, using the specified logits. - * @param {Tensor} logits - * @returns {Promise<[bigint, number][]>} - */ - async _call(logits) { - return this.sample(logits); - } - /** - * Abstract method for sampling the logits. - * @param {Tensor} logits - * @throws {Error} If not implemented in subclass. - * @returns {Promise<[bigint, number][]>} - */ - async sample(logits) { - throw Error("sample should be implemented in subclasses."); - } - /** - * Returns the specified logits as an array, with temperature applied. - * @param {Tensor} logits - * @param {number} index - * @returns {Float32Array} - */ - getLogits(logits, index) { - let vocabSize = logits.dims.at(-1); - let logs = ( - /** @type {Float32Array} */ - logits.data - ); - if (index === -1) { - logs = logs.slice(-vocabSize); - } else { - let startIndex = index * vocabSize; - logs = logs.slice(startIndex, startIndex + vocabSize); - } - return logs; - } - /** - * Selects an item randomly based on the specified probabilities. - * @param {import("../transformers.js").DataArray} probabilities An array of probabilities to use for selection. - * @returns {number} The index of the selected item. - */ - randomSelect(probabilities) { - let sumProbabilities = 0; - for (let i = 0; i < probabilities.length; ++i) { - sumProbabilities += probabilities[i]; - } - let r = Math.random() * sumProbabilities; - for (let i = 0; i < probabilities.length; ++i) { - r -= probabilities[i]; - if (r <= 0) { - return i; - } - } - return 0; - } - /** - * Returns a Sampler object based on the specified options. - * @param {GenerationConfig} generation_config An object containing options for the sampler. - * @returns {LogitsSampler} A Sampler object. - */ - static getSampler(generation_config) { - if (generation_config.do_sample) { - return new MultinomialSampler(generation_config); - } else if (generation_config.num_beams > 1) { - return new BeamSearchSampler(generation_config); - } else { - if (generation_config.num_return_sequences > 1) { - throw Error(`num_return_sequences has to be 1 when doing greedy search, but is ${generation_config.num_return_sequences}.`); - } - return new GreedySampler(generation_config); - } - } - } - class GreedySampler extends LogitsSampler { - /** - * Sample the maximum probability of a given logits tensor. - * @param {Tensor} logits - * @returns {Promise<[bigint, number][]>} An array with a single tuple, containing the index of the maximum value and a meaningless score (since this is a greedy search). - */ - async sample(logits) { - const argmax = (0, _utils_maths_js__WEBPACK_IMPORTED_MODULE_2__.max)(logits.data)[1]; - return [ - [BigInt(argmax), 0] - ]; - } - } - class MultinomialSampler extends LogitsSampler { - /** - * Sample from the logits. - * @param {Tensor} logits - * @returns {Promise<[bigint, number][]>} - */ - async sample(logits) { - let k = logits.dims.at(-1); - if (this.generation_config.top_k > 0) { - k = Math.min(this.generation_config.top_k, k); - } - const [v, i] = await (0, _utils_tensor_js__WEBPACK_IMPORTED_MODULE_1__.topk)(logits, k); - const probabilities = (0, _utils_maths_js__WEBPACK_IMPORTED_MODULE_2__.softmax)( - /** @type {Float32Array} */ - v.data - ); - return Array.from({ length: this.generation_config.num_beams }, () => { - const sampledIndex = this.randomSelect(probabilities); - return [ - i.data[sampledIndex], - // token id - Math.log(probabilities[sampledIndex]) - // score - ]; - }); - } - } - class BeamSearchSampler extends LogitsSampler { - /** - * Sample from the logits. - * @param {Tensor} logits - * @returns {Promise<[bigint, number][]>} - */ - async sample(logits) { - let k = logits.dims.at(-1); - if (this.generation_config.top_k > 0) { - k = Math.min(this.generation_config.top_k, k); - } - const [v, i] = await (0, _utils_tensor_js__WEBPACK_IMPORTED_MODULE_1__.topk)(logits, k); - const probabilities = (0, _utils_maths_js__WEBPACK_IMPORTED_MODULE_2__.softmax)( - /** @type {Float32Array} */ - v.data - ); - return Array.from({ length: this.generation_config.num_beams }, (_, x) => { - return [ - i.data[x], - // token id - Math.log(probabilities[x]) - // score - ]; - }); - } - } - } - ), - /***/ - "./src/generation/stopping_criteria.js": ( - /*!*********************************************!*\ - !*** ./src/generation/stopping_criteria.js ***! - \*********************************************/ - /***/ - (__unused_webpack___webpack_module__, __webpack_exports__2, __webpack_require__2) => { - __webpack_require__2.r(__webpack_exports__2); - __webpack_require__2.d(__webpack_exports__2, { - /* harmony export */ - EosTokenCriteria: () => ( - /* binding */ - EosTokenCriteria - ), - /* harmony export */ - InterruptableStoppingCriteria: () => ( - /* binding */ - InterruptableStoppingCriteria - ), - /* harmony export */ - MaxLengthCriteria: () => ( - /* binding */ - MaxLengthCriteria - ), - /* harmony export */ - StoppingCriteria: () => ( - /* binding */ - StoppingCriteria - ), - /* harmony export */ - StoppingCriteriaList: () => ( - /* binding */ - StoppingCriteriaList - ) - /* harmony export */ - }); - var _utils_generic_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__2( - /*! ../utils/generic.js */ - "./src/utils/generic.js" - ); - class StoppingCriteria extends _utils_generic_js__WEBPACK_IMPORTED_MODULE_0__.Callable { - /** - * - * @param {number[][]} input_ids (`number[][]` of shape `(batch_size, sequence_length)`): - * Indices of input sequence tokens in the vocabulary. - * @param {number[][]} scores scores (`number[][]` of shape `(batch_size, config.vocab_size)`): - * Prediction scores of a language modeling head. These can be scores for each vocabulary token before SoftMax - * or scores for each vocabulary token after SoftMax. - * @returns {boolean[]} A list of booleans indicating whether each sequence should be stopped. - */ - _call(input_ids, scores) { - throw Error("StoppingCriteria needs to be subclassed"); - } - } - class StoppingCriteriaList extends _utils_generic_js__WEBPACK_IMPORTED_MODULE_0__.Callable { - /** - * Constructs a new instance of `StoppingCriteriaList`. - */ - constructor() { - super(); - this.criteria = []; - } - /** - * Adds a new stopping criterion to the list. - * - * @param {StoppingCriteria} item The stopping criterion to add. - */ - push(item) { - this.criteria.push(item); - } - /** - * Adds multiple stopping criteria to the list. - * - * @param {StoppingCriteria|StoppingCriteriaList|StoppingCriteria[]} items The stopping criteria to add. - */ - extend(items) { - if (items instanceof StoppingCriteriaList) { - items = items.criteria; - } else if (items instanceof StoppingCriteria) { - items = [items]; - } - this.criteria.push(...items); - } - _call(input_ids, scores) { - const is_done = new Array(input_ids.length).fill(false); - for (const criterion of this.criteria) { - const criterion_done = criterion(input_ids, scores); - for (let i = 0; i < is_done.length; ++i) { - is_done[i] || (is_done[i] = criterion_done[i]); - } - } - return is_done; - } - [Symbol.iterator]() { - return this.criteria.values(); - } - } - class MaxLengthCriteria extends StoppingCriteria { - /** - * - * @param {number} max_length The maximum length that the output sequence can have in number of tokens. - * @param {number} [max_position_embeddings=null] The maximum model length, as defined by the model's `config.max_position_embeddings` attribute. - */ - constructor(max_length, max_position_embeddings = null) { - super(); - this.max_length = max_length; - this.max_position_embeddings = max_position_embeddings; - } - _call(input_ids) { - return input_ids.map((ids) => ids.length >= this.max_length); - } - } - class EosTokenCriteria extends StoppingCriteria { - /** - * - * @param {number|number[]} eos_token_id The id of the *end-of-sequence* token. - * Optionally, use a list to set multiple *end-of-sequence* tokens. - */ - constructor(eos_token_id) { - super(); - if (!Array.isArray(eos_token_id)) { - eos_token_id = [eos_token_id]; - } - this.eos_token_id = eos_token_id; - } - /** - * - * @param {number[][]} input_ids - * @param {number[][]} scores - * @returns {boolean[]} - */ - _call(input_ids, scores) { - return input_ids.map((ids) => { - const last = ids.at(-1); - return this.eos_token_id.some((eos_id) => last == eos_id); - }); - } - } - class InterruptableStoppingCriteria extends StoppingCriteria { - constructor() { - super(); - this.interrupted = false; - } - interrupt() { - this.interrupted = true; - } - reset() { - this.interrupted = false; - } - _call(input_ids, scores) { - return new Array(input_ids.length).fill(this.interrupted); - } - } - } - ), - /***/ - "./src/generation/streamers.js": ( - /*!*************************************!*\ - !*** ./src/generation/streamers.js ***! - \*************************************/ - /***/ - (__unused_webpack___webpack_module__, __webpack_exports__2, __webpack_require__2) => { - __webpack_require__2.r(__webpack_exports__2); - __webpack_require__2.d(__webpack_exports__2, { - /* harmony export */ - BaseStreamer: () => ( - /* binding */ - BaseStreamer - ), - /* harmony export */ - TextStreamer: () => ( - /* binding */ - TextStreamer - ), - /* harmony export */ - WhisperTextStreamer: () => ( - /* binding */ - WhisperTextStreamer - ) - /* harmony export */ - }); - var _utils_core_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__2( - /*! ../utils/core.js */ - "./src/utils/core.js" - ); - var _tokenizers_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__2( - /*! ../tokenizers.js */ - "./src/tokenizers.js" - ); - var _env_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__2( - /*! ../env.js */ - "./src/env.js" - ); - class BaseStreamer { - /** - * Function that is called by `.generate()` to push new tokens - * @param {bigint[][]} value - */ - put(value) { - throw Error("Not implemented"); - } - /** - * Function that is called by `.generate()` to signal the end of generation - */ - end() { - throw Error("Not implemented"); - } - } - const stdout_write = _env_js__WEBPACK_IMPORTED_MODULE_2__.apis.IS_PROCESS_AVAILABLE ? (x) => process.stdout.write(x) : (x) => console.log(x); - class TextStreamer extends BaseStreamer { - /** - * - * @param {import('../tokenizers.js').PreTrainedTokenizer} tokenizer - */ - constructor(tokenizer, { - skip_prompt = false, - callback_function = null, - token_callback_function = null, - decode_kwargs = {}, - ...kwargs - } = {}) { - super(); - this.tokenizer = tokenizer; - this.skip_prompt = skip_prompt; - this.callback_function = callback_function ?? stdout_write; - this.token_callback_function = token_callback_function; - this.decode_kwargs = { ...decode_kwargs, ...kwargs }; - this.token_cache = []; - this.print_len = 0; - this.next_tokens_are_prompt = true; - } - /** - * Receives tokens, decodes them, and prints them to stdout as soon as they form entire words. - * @param {bigint[][]} value - */ - put(value) { - var _a; - if (value.length > 1) { - throw Error("TextStreamer only supports batch size of 1"); - } - if (this.skip_prompt && this.next_tokens_are_prompt) { - this.next_tokens_are_prompt = false; - return; - } - const tokens = value[0]; - (_a = this.token_callback_function) == null ? void 0 : _a.call(this, tokens); - this.token_cache = (0, _utils_core_js__WEBPACK_IMPORTED_MODULE_0__.mergeArrays)(this.token_cache, tokens); - const text = this.tokenizer.decode(this.token_cache, this.decode_kwargs); - let printable_text; - if (text.endsWith("\n")) { - printable_text = text.slice(this.print_len); - this.token_cache = []; - this.print_len = 0; - } else if (text.length > 0 && (0, _tokenizers_js__WEBPACK_IMPORTED_MODULE_1__.is_chinese_char)(text.charCodeAt(text.length - 1))) { - printable_text = text.slice(this.print_len); - this.print_len += printable_text.length; - } else { - printable_text = text.slice(this.print_len, text.lastIndexOf(" ") + 1); - this.print_len += printable_text.length; - } - this.on_finalized_text(printable_text, false); - } - /** - * Flushes any remaining cache and prints a newline to stdout. - */ - end() { - let printable_text; - if (this.token_cache.length > 0) { - const text = this.tokenizer.decode(this.token_cache, this.decode_kwargs); - printable_text = text.slice(this.print_len); - this.token_cache = []; - this.print_len = 0; - } else { - printable_text = ""; - } - this.next_tokens_are_prompt = true; - this.on_finalized_text(printable_text, true); - } - /** - * Prints the new text to stdout. If the stream is ending, also prints a newline. - * @param {string} text - * @param {boolean} stream_end - */ - on_finalized_text(text, stream_end) { - var _a, _b; - if (text.length > 0) { - (_a = this.callback_function) == null ? void 0 : _a.call(this, text); - } - if (stream_end && this.callback_function === stdout_write && _env_js__WEBPACK_IMPORTED_MODULE_2__.apis.IS_PROCESS_AVAILABLE) { - (_b = this.callback_function) == null ? void 0 : _b.call(this, "\n"); - } - } - } - class WhisperTextStreamer extends TextStreamer { - /** - * @param {import('../tokenizers.js').WhisperTokenizer} tokenizer - * @param {Object} options - * @param {boolean} [options.skip_prompt=false] Whether to skip the prompt tokens - * @param {function(string): void} [options.callback_function=null] Function to call when a piece of text is ready to display - * @param {function(string): void} [options.token_callback_function=null] Function to call when a new token is generated - * @param {function(number): void} [options.on_chunk_start=null] Function to call when a new chunk starts - * @param {function(number): void} [options.on_chunk_end=null] Function to call when a chunk ends - * @param {function(): void} [options.on_finalize=null] Function to call when the stream is finalized - * @param {number} [options.time_precision=0.02] Precision of the timestamps - * @param {boolean} [options.skip_special_tokens=true] Whether to skip special tokens when decoding - * @param {Object} [options.decode_kwargs={}] Additional keyword arguments to pass to the tokenizer's decode method - */ - constructor(tokenizer, { - skip_prompt = false, - callback_function = null, - token_callback_function = null, - on_chunk_start = null, - on_chunk_end = null, - on_finalize = null, - time_precision = 0.02, - skip_special_tokens = true, - decode_kwargs = {} - } = {}) { - super(tokenizer, { - skip_prompt, - callback_function, - token_callback_function, - decode_kwargs: { skip_special_tokens, ...decode_kwargs } - }); - this.timestamp_begin = tokenizer.timestamp_begin; - this.on_chunk_start = on_chunk_start; - this.on_chunk_end = on_chunk_end; - this.on_finalize = on_finalize; - this.time_precision = time_precision; - this.waiting_for_timestamp = false; - } - /** - * @param {bigint[][]} value - */ - put(value) { - var _a, _b; - if (value.length > 1) { - throw Error("WhisperTextStreamer only supports batch size of 1"); - } - const tokens = value[0]; - if (tokens.length === 1) { - const offset = Number(tokens[0]) - this.timestamp_begin; - if (offset >= 0) { - const time = offset * this.time_precision; - if (this.waiting_for_timestamp) { - (_a = this.on_chunk_end) == null ? void 0 : _a.call(this, time); - } else { - (_b = this.on_chunk_start) == null ? void 0 : _b.call(this, time); - } - this.waiting_for_timestamp = !this.waiting_for_timestamp; - value = [[]]; - } - } - return super.put(value); - } - end() { - var _a; - super.end(); - (_a = this.on_finalize) == null ? void 0 : _a.call(this); - } - } - } - ), - /***/ - "./src/models.js": ( - /*!***********************!*\ - !*** ./src/models.js ***! - \***********************/ - /***/ - (__unused_webpack___webpack_module__, __webpack_exports__2, __webpack_require__2) => { - __webpack_require__2.r(__webpack_exports__2); - __webpack_require__2.d(__webpack_exports__2, { - /* harmony export */ - ASTForAudioClassification: () => ( - /* binding */ - ASTForAudioClassification - ), - /* harmony export */ - ASTModel: () => ( - /* binding */ - ASTModel - ), - /* harmony export */ - ASTPreTrainedModel: () => ( - /* binding */ - ASTPreTrainedModel - ), - /* harmony export */ - AlbertForMaskedLM: () => ( - /* binding */ - AlbertForMaskedLM - ), - /* harmony export */ - AlbertForQuestionAnswering: () => ( - /* binding */ - AlbertForQuestionAnswering - ), - /* harmony export */ - AlbertForSequenceClassification: () => ( - /* binding */ - AlbertForSequenceClassification - ), - /* harmony export */ - AlbertModel: () => ( - /* binding */ - AlbertModel - ), - /* harmony export */ - AlbertPreTrainedModel: () => ( - /* binding */ - AlbertPreTrainedModel - ), - /* harmony export */ - AutoModel: () => ( - /* binding */ - AutoModel - ), - /* harmony export */ - AutoModelForAudioClassification: () => ( - /* binding */ - AutoModelForAudioClassification - ), - /* harmony export */ - AutoModelForAudioFrameClassification: () => ( - /* binding */ - AutoModelForAudioFrameClassification - ), - /* harmony export */ - AutoModelForCTC: () => ( - /* binding */ - AutoModelForCTC - ), - /* harmony export */ - AutoModelForCausalLM: () => ( - /* binding */ - AutoModelForCausalLM - ), - /* harmony export */ - AutoModelForDepthEstimation: () => ( - /* binding */ - AutoModelForDepthEstimation - ), - /* harmony export */ - AutoModelForDocumentQuestionAnswering: () => ( - /* binding */ - AutoModelForDocumentQuestionAnswering - ), - /* harmony export */ - AutoModelForImageClassification: () => ( - /* binding */ - AutoModelForImageClassification - ), - /* harmony export */ - AutoModelForImageFeatureExtraction: () => ( - /* binding */ - AutoModelForImageFeatureExtraction - ), - /* harmony export */ - AutoModelForImageMatting: () => ( - /* binding */ - AutoModelForImageMatting - ), - /* harmony export */ - AutoModelForImageSegmentation: () => ( - /* binding */ - AutoModelForImageSegmentation - ), - /* harmony export */ - AutoModelForImageToImage: () => ( - /* binding */ - AutoModelForImageToImage - ), - /* harmony export */ - AutoModelForMaskGeneration: () => ( - /* binding */ - AutoModelForMaskGeneration - ), - /* harmony export */ - AutoModelForMaskedLM: () => ( - /* binding */ - AutoModelForMaskedLM - ), - /* harmony export */ - AutoModelForNormalEstimation: () => ( - /* binding */ - AutoModelForNormalEstimation - ), - /* harmony export */ - AutoModelForObjectDetection: () => ( - /* binding */ - AutoModelForObjectDetection - ), - /* harmony export */ - AutoModelForQuestionAnswering: () => ( - /* binding */ - AutoModelForQuestionAnswering - ), - /* harmony export */ - AutoModelForSemanticSegmentation: () => ( - /* binding */ - AutoModelForSemanticSegmentation - ), - /* harmony export */ - AutoModelForSeq2SeqLM: () => ( - /* binding */ - AutoModelForSeq2SeqLM - ), - /* harmony export */ - AutoModelForSequenceClassification: () => ( - /* binding */ - AutoModelForSequenceClassification - ), - /* harmony export */ - AutoModelForSpeechSeq2Seq: () => ( - /* binding */ - AutoModelForSpeechSeq2Seq - ), - /* harmony export */ - AutoModelForTextToSpectrogram: () => ( - /* binding */ - AutoModelForTextToSpectrogram - ), - /* harmony export */ - AutoModelForTextToWaveform: () => ( - /* binding */ - AutoModelForTextToWaveform - ), - /* harmony export */ - AutoModelForTokenClassification: () => ( - /* binding */ - AutoModelForTokenClassification - ), - /* harmony export */ - AutoModelForUniversalSegmentation: () => ( - /* binding */ - AutoModelForUniversalSegmentation - ), - /* harmony export */ - AutoModelForVision2Seq: () => ( - /* binding */ - AutoModelForVision2Seq - ), - /* harmony export */ - AutoModelForXVector: () => ( - /* binding */ - AutoModelForXVector - ), - /* harmony export */ - AutoModelForZeroShotObjectDetection: () => ( - /* binding */ - AutoModelForZeroShotObjectDetection - ), - /* harmony export */ - BartForConditionalGeneration: () => ( - /* binding */ - BartForConditionalGeneration - ), - /* harmony export */ - BartForSequenceClassification: () => ( - /* binding */ - BartForSequenceClassification - ), - /* harmony export */ - BartModel: () => ( - /* binding */ - BartModel - ), - /* harmony export */ - BartPretrainedModel: () => ( - /* binding */ - BartPretrainedModel - ), - /* harmony export */ - BaseModelOutput: () => ( - /* binding */ - BaseModelOutput - ), - /* harmony export */ - BeitForImageClassification: () => ( - /* binding */ - BeitForImageClassification - ), - /* harmony export */ - BeitModel: () => ( - /* binding */ - BeitModel - ), - /* harmony export */ - BeitPreTrainedModel: () => ( - /* binding */ - BeitPreTrainedModel - ), - /* harmony export */ - BertForMaskedLM: () => ( - /* binding */ - BertForMaskedLM - ), - /* harmony export */ - BertForQuestionAnswering: () => ( - /* binding */ - BertForQuestionAnswering - ), - /* harmony export */ - BertForSequenceClassification: () => ( - /* binding */ - BertForSequenceClassification - ), - /* harmony export */ - BertForTokenClassification: () => ( - /* binding */ - BertForTokenClassification - ), - /* harmony export */ - BertModel: () => ( - /* binding */ - BertModel - ), - /* harmony export */ - BertPreTrainedModel: () => ( - /* binding */ - BertPreTrainedModel - ), - /* harmony export */ - BlenderbotForConditionalGeneration: () => ( - /* binding */ - BlenderbotForConditionalGeneration - ), - /* harmony export */ - BlenderbotModel: () => ( - /* binding */ - BlenderbotModel - ), - /* harmony export */ - BlenderbotPreTrainedModel: () => ( - /* binding */ - BlenderbotPreTrainedModel - ), - /* harmony export */ - BlenderbotSmallForConditionalGeneration: () => ( - /* binding */ - BlenderbotSmallForConditionalGeneration - ), - /* harmony export */ - BlenderbotSmallModel: () => ( - /* binding */ - BlenderbotSmallModel - ), - /* harmony export */ - BlenderbotSmallPreTrainedModel: () => ( - /* binding */ - BlenderbotSmallPreTrainedModel - ), - /* harmony export */ - BloomForCausalLM: () => ( - /* binding */ - BloomForCausalLM - ), - /* harmony export */ - BloomModel: () => ( - /* binding */ - BloomModel - ), - /* harmony export */ - BloomPreTrainedModel: () => ( - /* binding */ - BloomPreTrainedModel - ), - /* harmony export */ - CLIPModel: () => ( - /* binding */ - CLIPModel - ), - /* harmony export */ - CLIPPreTrainedModel: () => ( - /* binding */ - CLIPPreTrainedModel - ), - /* harmony export */ - CLIPSegForImageSegmentation: () => ( - /* binding */ - CLIPSegForImageSegmentation - ), - /* harmony export */ - CLIPSegModel: () => ( - /* binding */ - CLIPSegModel - ), - /* harmony export */ - CLIPSegPreTrainedModel: () => ( - /* binding */ - CLIPSegPreTrainedModel - ), - /* harmony export */ - CLIPTextModel: () => ( - /* binding */ - CLIPTextModel - ), - /* harmony export */ - CLIPTextModelWithProjection: () => ( - /* binding */ - CLIPTextModelWithProjection - ), - /* harmony export */ - CLIPVisionModel: () => ( - /* binding */ - CLIPVisionModel - ), - /* harmony export */ - CLIPVisionModelWithProjection: () => ( - /* binding */ - CLIPVisionModelWithProjection - ), - /* harmony export */ - CamembertForMaskedLM: () => ( - /* binding */ - CamembertForMaskedLM - ), - /* harmony export */ - CamembertForQuestionAnswering: () => ( - /* binding */ - CamembertForQuestionAnswering - ), - /* harmony export */ - CamembertForSequenceClassification: () => ( - /* binding */ - CamembertForSequenceClassification - ), - /* harmony export */ - CamembertForTokenClassification: () => ( - /* binding */ - CamembertForTokenClassification - ), - /* harmony export */ - CamembertModel: () => ( - /* binding */ - CamembertModel - ), - /* harmony export */ - CamembertPreTrainedModel: () => ( - /* binding */ - CamembertPreTrainedModel - ), - /* harmony export */ - CausalLMOutput: () => ( - /* binding */ - CausalLMOutput - ), - /* harmony export */ - CausalLMOutputWithPast: () => ( - /* binding */ - CausalLMOutputWithPast - ), - /* harmony export */ - ChineseCLIPModel: () => ( - /* binding */ - ChineseCLIPModel - ), - /* harmony export */ - ChineseCLIPPreTrainedModel: () => ( - /* binding */ - ChineseCLIPPreTrainedModel - ), - /* harmony export */ - ClapAudioModelWithProjection: () => ( - /* binding */ - ClapAudioModelWithProjection - ), - /* harmony export */ - ClapModel: () => ( - /* binding */ - ClapModel - ), - /* harmony export */ - ClapPreTrainedModel: () => ( - /* binding */ - ClapPreTrainedModel - ), - /* harmony export */ - ClapTextModelWithProjection: () => ( - /* binding */ - ClapTextModelWithProjection - ), - /* harmony export */ - CodeGenForCausalLM: () => ( - /* binding */ - CodeGenForCausalLM - ), - /* harmony export */ - CodeGenModel: () => ( - /* binding */ - CodeGenModel - ), - /* harmony export */ - CodeGenPreTrainedModel: () => ( - /* binding */ - CodeGenPreTrainedModel - ), - /* harmony export */ - CohereForCausalLM: () => ( - /* binding */ - CohereForCausalLM - ), - /* harmony export */ - CohereModel: () => ( - /* binding */ - CohereModel - ), - /* harmony export */ - CoherePreTrainedModel: () => ( - /* binding */ - CoherePreTrainedModel - ), - /* harmony export */ - ConvBertForMaskedLM: () => ( - /* binding */ - ConvBertForMaskedLM - ), - /* harmony export */ - ConvBertForQuestionAnswering: () => ( - /* binding */ - ConvBertForQuestionAnswering - ), - /* harmony export */ - ConvBertForSequenceClassification: () => ( - /* binding */ - ConvBertForSequenceClassification - ), - /* harmony export */ - ConvBertForTokenClassification: () => ( - /* binding */ - ConvBertForTokenClassification - ), - /* harmony export */ - ConvBertModel: () => ( - /* binding */ - ConvBertModel - ), - /* harmony export */ - ConvBertPreTrainedModel: () => ( - /* binding */ - ConvBertPreTrainedModel - ), - /* harmony export */ - ConvNextForImageClassification: () => ( - /* binding */ - ConvNextForImageClassification - ), - /* harmony export */ - ConvNextModel: () => ( - /* binding */ - ConvNextModel - ), - /* harmony export */ - ConvNextPreTrainedModel: () => ( - /* binding */ - ConvNextPreTrainedModel - ), - /* harmony export */ - ConvNextV2ForImageClassification: () => ( - /* binding */ - ConvNextV2ForImageClassification - ), - /* harmony export */ - ConvNextV2Model: () => ( - /* binding */ - ConvNextV2Model - ), - /* harmony export */ - ConvNextV2PreTrainedModel: () => ( - /* binding */ - ConvNextV2PreTrainedModel - ), - /* harmony export */ - DPTForDepthEstimation: () => ( - /* binding */ - DPTForDepthEstimation - ), - /* harmony export */ - DPTModel: () => ( - /* binding */ - DPTModel - ), - /* harmony export */ - DPTPreTrainedModel: () => ( - /* binding */ - DPTPreTrainedModel - ), - /* harmony export */ - DebertaForMaskedLM: () => ( - /* binding */ - DebertaForMaskedLM - ), - /* harmony export */ - DebertaForQuestionAnswering: () => ( - /* binding */ - DebertaForQuestionAnswering - ), - /* harmony export */ - DebertaForSequenceClassification: () => ( - /* binding */ - DebertaForSequenceClassification - ), - /* harmony export */ - DebertaForTokenClassification: () => ( - /* binding */ - DebertaForTokenClassification - ), - /* harmony export */ - DebertaModel: () => ( - /* binding */ - DebertaModel - ), - /* harmony export */ - DebertaPreTrainedModel: () => ( - /* binding */ - DebertaPreTrainedModel - ), - /* harmony export */ - DebertaV2ForMaskedLM: () => ( - /* binding */ - DebertaV2ForMaskedLM - ), - /* harmony export */ - DebertaV2ForQuestionAnswering: () => ( - /* binding */ - DebertaV2ForQuestionAnswering - ), - /* harmony export */ - DebertaV2ForSequenceClassification: () => ( - /* binding */ - DebertaV2ForSequenceClassification - ), - /* harmony export */ - DebertaV2ForTokenClassification: () => ( - /* binding */ - DebertaV2ForTokenClassification - ), - /* harmony export */ - DebertaV2Model: () => ( - /* binding */ - DebertaV2Model - ), - /* harmony export */ - DebertaV2PreTrainedModel: () => ( - /* binding */ - DebertaV2PreTrainedModel - ), - /* harmony export */ - DecisionTransformerModel: () => ( - /* binding */ - DecisionTransformerModel - ), - /* harmony export */ - DecisionTransformerPreTrainedModel: () => ( - /* binding */ - DecisionTransformerPreTrainedModel - ), - /* harmony export */ - DeiTForImageClassification: () => ( - /* binding */ - DeiTForImageClassification - ), - /* harmony export */ - DeiTModel: () => ( - /* binding */ - DeiTModel - ), - /* harmony export */ - DeiTPreTrainedModel: () => ( - /* binding */ - DeiTPreTrainedModel - ), - /* harmony export */ - DepthAnythingForDepthEstimation: () => ( - /* binding */ - DepthAnythingForDepthEstimation - ), - /* harmony export */ - DepthAnythingPreTrainedModel: () => ( - /* binding */ - DepthAnythingPreTrainedModel - ), - /* harmony export */ - DepthProForDepthEstimation: () => ( - /* binding */ - DepthProForDepthEstimation - ), - /* harmony export */ - DepthProPreTrainedModel: () => ( - /* binding */ - DepthProPreTrainedModel - ), - /* harmony export */ - DetrForObjectDetection: () => ( - /* binding */ - DetrForObjectDetection - ), - /* harmony export */ - DetrForSegmentation: () => ( - /* binding */ - DetrForSegmentation - ), - /* harmony export */ - DetrModel: () => ( - /* binding */ - DetrModel - ), - /* harmony export */ - DetrObjectDetectionOutput: () => ( - /* binding */ - DetrObjectDetectionOutput - ), - /* harmony export */ - DetrPreTrainedModel: () => ( - /* binding */ - DetrPreTrainedModel - ), - /* harmony export */ - DetrSegmentationOutput: () => ( - /* binding */ - DetrSegmentationOutput - ), - /* harmony export */ - Dinov2ForImageClassification: () => ( - /* binding */ - Dinov2ForImageClassification - ), - /* harmony export */ - Dinov2Model: () => ( - /* binding */ - Dinov2Model - ), - /* harmony export */ - Dinov2PreTrainedModel: () => ( - /* binding */ - Dinov2PreTrainedModel - ), - /* harmony export */ - DistilBertForMaskedLM: () => ( - /* binding */ - DistilBertForMaskedLM - ), - /* harmony export */ - DistilBertForQuestionAnswering: () => ( - /* binding */ - DistilBertForQuestionAnswering - ), - /* harmony export */ - DistilBertForSequenceClassification: () => ( - /* binding */ - DistilBertForSequenceClassification - ), - /* harmony export */ - DistilBertForTokenClassification: () => ( - /* binding */ - DistilBertForTokenClassification - ), - /* harmony export */ - DistilBertModel: () => ( - /* binding */ - DistilBertModel - ), - /* harmony export */ - DistilBertPreTrainedModel: () => ( - /* binding */ - DistilBertPreTrainedModel - ), - /* harmony export */ - DonutSwinModel: () => ( - /* binding */ - DonutSwinModel - ), - /* harmony export */ - DonutSwinPreTrainedModel: () => ( - /* binding */ - DonutSwinPreTrainedModel - ), - /* harmony export */ - EfficientNetForImageClassification: () => ( - /* binding */ - EfficientNetForImageClassification - ), - /* harmony export */ - EfficientNetModel: () => ( - /* binding */ - EfficientNetModel - ), - /* harmony export */ - EfficientNetPreTrainedModel: () => ( - /* binding */ - EfficientNetPreTrainedModel - ), - /* harmony export */ - ElectraForMaskedLM: () => ( - /* binding */ - ElectraForMaskedLM - ), - /* harmony export */ - ElectraForQuestionAnswering: () => ( - /* binding */ - ElectraForQuestionAnswering - ), - /* harmony export */ - ElectraForSequenceClassification: () => ( - /* binding */ - ElectraForSequenceClassification - ), - /* harmony export */ - ElectraForTokenClassification: () => ( - /* binding */ - ElectraForTokenClassification - ), - /* harmony export */ - ElectraModel: () => ( - /* binding */ - ElectraModel - ), - /* harmony export */ - ElectraPreTrainedModel: () => ( - /* binding */ - ElectraPreTrainedModel - ), - /* harmony export */ - EsmForMaskedLM: () => ( - /* binding */ - EsmForMaskedLM - ), - /* harmony export */ - EsmForSequenceClassification: () => ( - /* binding */ - EsmForSequenceClassification - ), - /* harmony export */ - EsmForTokenClassification: () => ( - /* binding */ - EsmForTokenClassification - ), - /* harmony export */ - EsmModel: () => ( - /* binding */ - EsmModel - ), - /* harmony export */ - EsmPreTrainedModel: () => ( - /* binding */ - EsmPreTrainedModel - ), - /* harmony export */ - FalconForCausalLM: () => ( - /* binding */ - FalconForCausalLM - ), - /* harmony export */ - FalconModel: () => ( - /* binding */ - FalconModel - ), - /* harmony export */ - FalconPreTrainedModel: () => ( - /* binding */ - FalconPreTrainedModel - ), - /* harmony export */ - FastViTForImageClassification: () => ( - /* binding */ - FastViTForImageClassification - ), - /* harmony export */ - FastViTModel: () => ( - /* binding */ - FastViTModel - ), - /* harmony export */ - FastViTPreTrainedModel: () => ( - /* binding */ - FastViTPreTrainedModel - ), - /* harmony export */ - Florence2ForConditionalGeneration: () => ( - /* binding */ - Florence2ForConditionalGeneration - ), - /* harmony export */ - Florence2PreTrainedModel: () => ( - /* binding */ - Florence2PreTrainedModel - ), - /* harmony export */ - GLPNForDepthEstimation: () => ( - /* binding */ - GLPNForDepthEstimation - ), - /* harmony export */ - GLPNModel: () => ( - /* binding */ - GLPNModel - ), - /* harmony export */ - GLPNPreTrainedModel: () => ( - /* binding */ - GLPNPreTrainedModel - ), - /* harmony export */ - GPT2LMHeadModel: () => ( - /* binding */ - GPT2LMHeadModel - ), - /* harmony export */ - GPT2Model: () => ( - /* binding */ - GPT2Model - ), - /* harmony export */ - GPT2PreTrainedModel: () => ( - /* binding */ - GPT2PreTrainedModel - ), - /* harmony export */ - GPTBigCodeForCausalLM: () => ( - /* binding */ - GPTBigCodeForCausalLM - ), - /* harmony export */ - GPTBigCodeModel: () => ( - /* binding */ - GPTBigCodeModel - ), - /* harmony export */ - GPTBigCodePreTrainedModel: () => ( - /* binding */ - GPTBigCodePreTrainedModel - ), - /* harmony export */ - GPTJForCausalLM: () => ( - /* binding */ - GPTJForCausalLM - ), - /* harmony export */ - GPTJModel: () => ( - /* binding */ - GPTJModel - ), - /* harmony export */ - GPTJPreTrainedModel: () => ( - /* binding */ - GPTJPreTrainedModel - ), - /* harmony export */ - GPTNeoForCausalLM: () => ( - /* binding */ - GPTNeoForCausalLM - ), - /* harmony export */ - GPTNeoModel: () => ( - /* binding */ - GPTNeoModel - ), - /* harmony export */ - GPTNeoPreTrainedModel: () => ( - /* binding */ - GPTNeoPreTrainedModel - ), - /* harmony export */ - GPTNeoXForCausalLM: () => ( - /* binding */ - GPTNeoXForCausalLM - ), - /* harmony export */ - GPTNeoXModel: () => ( - /* binding */ - GPTNeoXModel - ), - /* harmony export */ - GPTNeoXPreTrainedModel: () => ( - /* binding */ - GPTNeoXPreTrainedModel - ), - /* harmony export */ - Gemma2ForCausalLM: () => ( - /* binding */ - Gemma2ForCausalLM - ), - /* harmony export */ - Gemma2Model: () => ( - /* binding */ - Gemma2Model - ), - /* harmony export */ - Gemma2PreTrainedModel: () => ( - /* binding */ - Gemma2PreTrainedModel - ), - /* harmony export */ - GemmaForCausalLM: () => ( - /* binding */ - GemmaForCausalLM - ), - /* harmony export */ - GemmaModel: () => ( - /* binding */ - GemmaModel - ), - /* harmony export */ - GemmaPreTrainedModel: () => ( - /* binding */ - GemmaPreTrainedModel - ), - /* harmony export */ - GraniteForCausalLM: () => ( - /* binding */ - GraniteForCausalLM - ), - /* harmony export */ - GraniteModel: () => ( - /* binding */ - GraniteModel - ), - /* harmony export */ - GranitePreTrainedModel: () => ( - /* binding */ - GranitePreTrainedModel - ), - /* harmony export */ - GroupViTModel: () => ( - /* binding */ - GroupViTModel - ), - /* harmony export */ - GroupViTPreTrainedModel: () => ( - /* binding */ - GroupViTPreTrainedModel - ), - /* harmony export */ - HieraForImageClassification: () => ( - /* binding */ - HieraForImageClassification - ), - /* harmony export */ - HieraModel: () => ( - /* binding */ - HieraModel - ), - /* harmony export */ - HieraPreTrainedModel: () => ( - /* binding */ - HieraPreTrainedModel - ), - /* harmony export */ - HubertForCTC: () => ( - /* binding */ - HubertForCTC - ), - /* harmony export */ - HubertForSequenceClassification: () => ( - /* binding */ - HubertForSequenceClassification - ), - /* harmony export */ - HubertModel: () => ( - /* binding */ - HubertModel - ), - /* harmony export */ - HubertPreTrainedModel: () => ( - /* binding */ - HubertPreTrainedModel - ), - /* harmony export */ - ImageMattingOutput: () => ( - /* binding */ - ImageMattingOutput - ), - /* harmony export */ - JAISLMHeadModel: () => ( - /* binding */ - JAISLMHeadModel - ), - /* harmony export */ - JAISModel: () => ( - /* binding */ - JAISModel - ), - /* harmony export */ - JAISPreTrainedModel: () => ( - /* binding */ - JAISPreTrainedModel - ), - /* harmony export */ - LlamaForCausalLM: () => ( - /* binding */ - LlamaForCausalLM - ), - /* harmony export */ - LlamaModel: () => ( - /* binding */ - LlamaModel - ), - /* harmony export */ - LlamaPreTrainedModel: () => ( - /* binding */ - LlamaPreTrainedModel - ), - /* harmony export */ - LlavaForConditionalGeneration: () => ( - /* binding */ - LlavaForConditionalGeneration - ), - /* harmony export */ - LlavaPreTrainedModel: () => ( - /* binding */ - LlavaPreTrainedModel - ), - /* harmony export */ - LongT5ForConditionalGeneration: () => ( - /* binding */ - LongT5ForConditionalGeneration - ), - /* harmony export */ - LongT5Model: () => ( - /* binding */ - LongT5Model - ), - /* harmony export */ - LongT5PreTrainedModel: () => ( - /* binding */ - LongT5PreTrainedModel - ), - /* harmony export */ - M2M100ForConditionalGeneration: () => ( - /* binding */ - M2M100ForConditionalGeneration - ), - /* harmony export */ - M2M100Model: () => ( - /* binding */ - M2M100Model - ), - /* harmony export */ - M2M100PreTrainedModel: () => ( - /* binding */ - M2M100PreTrainedModel - ), - /* harmony export */ - MBartForCausalLM: () => ( - /* binding */ - MBartForCausalLM - ), - /* harmony export */ - MBartForConditionalGeneration: () => ( - /* binding */ - MBartForConditionalGeneration - ), - /* harmony export */ - MBartForSequenceClassification: () => ( - /* binding */ - MBartForSequenceClassification - ), - /* harmony export */ - MBartModel: () => ( - /* binding */ - MBartModel - ), - /* harmony export */ - MBartPreTrainedModel: () => ( - /* binding */ - MBartPreTrainedModel - ), - /* harmony export */ - MPNetForMaskedLM: () => ( - /* binding */ - MPNetForMaskedLM - ), - /* harmony export */ - MPNetForQuestionAnswering: () => ( - /* binding */ - MPNetForQuestionAnswering - ), - /* harmony export */ - MPNetForSequenceClassification: () => ( - /* binding */ - MPNetForSequenceClassification - ), - /* harmony export */ - MPNetForTokenClassification: () => ( - /* binding */ - MPNetForTokenClassification - ), - /* harmony export */ - MPNetModel: () => ( - /* binding */ - MPNetModel - ), - /* harmony export */ - MPNetPreTrainedModel: () => ( - /* binding */ - MPNetPreTrainedModel - ), - /* harmony export */ - MT5ForConditionalGeneration: () => ( - /* binding */ - MT5ForConditionalGeneration - ), - /* harmony export */ - MT5Model: () => ( - /* binding */ - MT5Model - ), - /* harmony export */ - MT5PreTrainedModel: () => ( - /* binding */ - MT5PreTrainedModel - ), - /* harmony export */ - MarianMTModel: () => ( - /* binding */ - MarianMTModel - ), - /* harmony export */ - MarianModel: () => ( - /* binding */ - MarianModel - ), - /* harmony export */ - MarianPreTrainedModel: () => ( - /* binding */ - MarianPreTrainedModel - ), - /* harmony export */ - MaskFormerForInstanceSegmentation: () => ( - /* binding */ - MaskFormerForInstanceSegmentation - ), - /* harmony export */ - MaskFormerModel: () => ( - /* binding */ - MaskFormerModel - ), - /* harmony export */ - MaskFormerPreTrainedModel: () => ( - /* binding */ - MaskFormerPreTrainedModel - ), - /* harmony export */ - MaskedLMOutput: () => ( - /* binding */ - MaskedLMOutput - ), - /* harmony export */ - MistralForCausalLM: () => ( - /* binding */ - MistralForCausalLM - ), - /* harmony export */ - MistralModel: () => ( - /* binding */ - MistralModel - ), - /* harmony export */ - MistralPreTrainedModel: () => ( - /* binding */ - MistralPreTrainedModel - ), - /* harmony export */ - MobileBertForMaskedLM: () => ( - /* binding */ - MobileBertForMaskedLM - ), - /* harmony export */ - MobileBertForQuestionAnswering: () => ( - /* binding */ - MobileBertForQuestionAnswering - ), - /* harmony export */ - MobileBertForSequenceClassification: () => ( - /* binding */ - MobileBertForSequenceClassification - ), - /* harmony export */ - MobileBertModel: () => ( - /* binding */ - MobileBertModel - ), - /* harmony export */ - MobileBertPreTrainedModel: () => ( - /* binding */ - MobileBertPreTrainedModel - ), - /* harmony export */ - MobileNetV1ForImageClassification: () => ( - /* binding */ - MobileNetV1ForImageClassification - ), - /* harmony export */ - MobileNetV1Model: () => ( - /* binding */ - MobileNetV1Model - ), - /* harmony export */ - MobileNetV1PreTrainedModel: () => ( - /* binding */ - MobileNetV1PreTrainedModel - ), - /* harmony export */ - MobileNetV2ForImageClassification: () => ( - /* binding */ - MobileNetV2ForImageClassification - ), - /* harmony export */ - MobileNetV2Model: () => ( - /* binding */ - MobileNetV2Model - ), - /* harmony export */ - MobileNetV2PreTrainedModel: () => ( - /* binding */ - MobileNetV2PreTrainedModel - ), - /* harmony export */ - MobileNetV3ForImageClassification: () => ( - /* binding */ - MobileNetV3ForImageClassification - ), - /* harmony export */ - MobileNetV3Model: () => ( - /* binding */ - MobileNetV3Model - ), - /* harmony export */ - MobileNetV3PreTrainedModel: () => ( - /* binding */ - MobileNetV3PreTrainedModel - ), - /* harmony export */ - MobileNetV4ForImageClassification: () => ( - /* binding */ - MobileNetV4ForImageClassification - ), - /* harmony export */ - MobileNetV4Model: () => ( - /* binding */ - MobileNetV4Model - ), - /* harmony export */ - MobileNetV4PreTrainedModel: () => ( - /* binding */ - MobileNetV4PreTrainedModel - ), - /* harmony export */ - MobileViTForImageClassification: () => ( - /* binding */ - MobileViTForImageClassification - ), - /* harmony export */ - MobileViTModel: () => ( - /* binding */ - MobileViTModel - ), - /* harmony export */ - MobileViTPreTrainedModel: () => ( - /* binding */ - MobileViTPreTrainedModel - ), - /* harmony export */ - MobileViTV2ForImageClassification: () => ( - /* binding */ - MobileViTV2ForImageClassification - ), - /* harmony export */ - MobileViTV2Model: () => ( - /* binding */ - MobileViTV2Model - ), - /* harmony export */ - MobileViTV2PreTrainedModel: () => ( - /* binding */ - MobileViTV2PreTrainedModel - ), - /* harmony export */ - ModelOutput: () => ( - /* binding */ - ModelOutput - ), - /* harmony export */ - Moondream1ForConditionalGeneration: () => ( - /* binding */ - Moondream1ForConditionalGeneration - ), - /* harmony export */ - MptForCausalLM: () => ( - /* binding */ - MptForCausalLM - ), - /* harmony export */ - MptModel: () => ( - /* binding */ - MptModel - ), - /* harmony export */ - MptPreTrainedModel: () => ( - /* binding */ - MptPreTrainedModel - ), - /* harmony export */ - MusicgenForCausalLM: () => ( - /* binding */ - MusicgenForCausalLM - ), - /* harmony export */ - MusicgenForConditionalGeneration: () => ( - /* binding */ - MusicgenForConditionalGeneration - ), - /* harmony export */ - MusicgenModel: () => ( - /* binding */ - MusicgenModel - ), - /* harmony export */ - MusicgenPreTrainedModel: () => ( - /* binding */ - MusicgenPreTrainedModel - ), - /* harmony export */ - NomicBertModel: () => ( - /* binding */ - NomicBertModel - ), - /* harmony export */ - NomicBertPreTrainedModel: () => ( - /* binding */ - NomicBertPreTrainedModel - ), - /* harmony export */ - OPTForCausalLM: () => ( - /* binding */ - OPTForCausalLM - ), - /* harmony export */ - OPTModel: () => ( - /* binding */ - OPTModel - ), - /* harmony export */ - OPTPreTrainedModel: () => ( - /* binding */ - OPTPreTrainedModel - ), - /* harmony export */ - OpenELMForCausalLM: () => ( - /* binding */ - OpenELMForCausalLM - ), - /* harmony export */ - OpenELMModel: () => ( - /* binding */ - OpenELMModel - ), - /* harmony export */ - OpenELMPreTrainedModel: () => ( - /* binding */ - OpenELMPreTrainedModel - ), - /* harmony export */ - OwlViTForObjectDetection: () => ( - /* binding */ - OwlViTForObjectDetection - ), - /* harmony export */ - OwlViTModel: () => ( - /* binding */ - OwlViTModel - ), - /* harmony export */ - OwlViTPreTrainedModel: () => ( - /* binding */ - OwlViTPreTrainedModel - ), - /* harmony export */ - Owlv2ForObjectDetection: () => ( - /* binding */ - Owlv2ForObjectDetection - ), - /* harmony export */ - Owlv2Model: () => ( - /* binding */ - Owlv2Model - ), - /* harmony export */ - Owlv2PreTrainedModel: () => ( - /* binding */ - Owlv2PreTrainedModel - ), - /* harmony export */ - Phi3ForCausalLM: () => ( - /* binding */ - Phi3ForCausalLM - ), - /* harmony export */ - Phi3Model: () => ( - /* binding */ - Phi3Model - ), - /* harmony export */ - Phi3PreTrainedModel: () => ( - /* binding */ - Phi3PreTrainedModel - ), - /* harmony export */ - PhiForCausalLM: () => ( - /* binding */ - PhiForCausalLM - ), - /* harmony export */ - PhiModel: () => ( - /* binding */ - PhiModel - ), - /* harmony export */ - PhiPreTrainedModel: () => ( - /* binding */ - PhiPreTrainedModel - ), - /* harmony export */ - PreTrainedModel: () => ( - /* binding */ - PreTrainedModel - ), - /* harmony export */ - PretrainedMixin: () => ( - /* binding */ - PretrainedMixin - ), - /* harmony export */ - PvtForImageClassification: () => ( - /* binding */ - PvtForImageClassification - ), - /* harmony export */ - PvtModel: () => ( - /* binding */ - PvtModel - ), - /* harmony export */ - PvtPreTrainedModel: () => ( - /* binding */ - PvtPreTrainedModel - ), - /* harmony export */ - PyAnnoteForAudioFrameClassification: () => ( - /* binding */ - PyAnnoteForAudioFrameClassification - ), - /* harmony export */ - PyAnnoteModel: () => ( - /* binding */ - PyAnnoteModel - ), - /* harmony export */ - PyAnnotePreTrainedModel: () => ( - /* binding */ - PyAnnotePreTrainedModel - ), - /* harmony export */ - QuestionAnsweringModelOutput: () => ( - /* binding */ - QuestionAnsweringModelOutput - ), - /* harmony export */ - Qwen2ForCausalLM: () => ( - /* binding */ - Qwen2ForCausalLM - ), - /* harmony export */ - Qwen2Model: () => ( - /* binding */ - Qwen2Model - ), - /* harmony export */ - Qwen2PreTrainedModel: () => ( - /* binding */ - Qwen2PreTrainedModel - ), - /* harmony export */ - RTDetrForObjectDetection: () => ( - /* binding */ - RTDetrForObjectDetection - ), - /* harmony export */ - RTDetrModel: () => ( - /* binding */ - RTDetrModel - ), - /* harmony export */ - RTDetrObjectDetectionOutput: () => ( - /* binding */ - RTDetrObjectDetectionOutput - ), - /* harmony export */ - RTDetrPreTrainedModel: () => ( - /* binding */ - RTDetrPreTrainedModel - ), - /* harmony export */ - ResNetForImageClassification: () => ( - /* binding */ - ResNetForImageClassification - ), - /* harmony export */ - ResNetModel: () => ( - /* binding */ - ResNetModel - ), - /* harmony export */ - ResNetPreTrainedModel: () => ( - /* binding */ - ResNetPreTrainedModel - ), - /* harmony export */ - RoFormerForMaskedLM: () => ( - /* binding */ - RoFormerForMaskedLM - ), - /* harmony export */ - RoFormerForQuestionAnswering: () => ( - /* binding */ - RoFormerForQuestionAnswering - ), - /* harmony export */ - RoFormerForSequenceClassification: () => ( - /* binding */ - RoFormerForSequenceClassification - ), - /* harmony export */ - RoFormerForTokenClassification: () => ( - /* binding */ - RoFormerForTokenClassification - ), - /* harmony export */ - RoFormerModel: () => ( - /* binding */ - RoFormerModel - ), - /* harmony export */ - RoFormerPreTrainedModel: () => ( - /* binding */ - RoFormerPreTrainedModel - ), - /* harmony export */ - RobertaForMaskedLM: () => ( - /* binding */ - RobertaForMaskedLM - ), - /* harmony export */ - RobertaForQuestionAnswering: () => ( - /* binding */ - RobertaForQuestionAnswering - ), - /* harmony export */ - RobertaForSequenceClassification: () => ( - /* binding */ - RobertaForSequenceClassification - ), - /* harmony export */ - RobertaForTokenClassification: () => ( - /* binding */ - RobertaForTokenClassification - ), - /* harmony export */ - RobertaModel: () => ( - /* binding */ - RobertaModel - ), - /* harmony export */ - RobertaPreTrainedModel: () => ( - /* binding */ - RobertaPreTrainedModel - ), - /* harmony export */ - SamImageSegmentationOutput: () => ( - /* binding */ - SamImageSegmentationOutput - ), - /* harmony export */ - SamModel: () => ( - /* binding */ - SamModel - ), - /* harmony export */ - SamPreTrainedModel: () => ( - /* binding */ - SamPreTrainedModel - ), - /* harmony export */ - SapiensForDepthEstimation: () => ( - /* binding */ - SapiensForDepthEstimation - ), - /* harmony export */ - SapiensForNormalEstimation: () => ( - /* binding */ - SapiensForNormalEstimation - ), - /* harmony export */ - SapiensForSemanticSegmentation: () => ( - /* binding */ - SapiensForSemanticSegmentation - ), - /* harmony export */ - SapiensPreTrainedModel: () => ( - /* binding */ - SapiensPreTrainedModel - ), - /* harmony export */ - SegformerForImageClassification: () => ( - /* binding */ - SegformerForImageClassification - ), - /* harmony export */ - SegformerForSemanticSegmentation: () => ( - /* binding */ - SegformerForSemanticSegmentation - ), - /* harmony export */ - SegformerModel: () => ( - /* binding */ - SegformerModel - ), - /* harmony export */ - SegformerPreTrainedModel: () => ( - /* binding */ - SegformerPreTrainedModel - ), - /* harmony export */ - Seq2SeqLMOutput: () => ( - /* binding */ - Seq2SeqLMOutput - ), - /* harmony export */ - SequenceClassifierOutput: () => ( - /* binding */ - SequenceClassifierOutput - ), - /* harmony export */ - SiglipModel: () => ( - /* binding */ - SiglipModel - ), - /* harmony export */ - SiglipPreTrainedModel: () => ( - /* binding */ - SiglipPreTrainedModel - ), - /* harmony export */ - SiglipTextModel: () => ( - /* binding */ - SiglipTextModel - ), - /* harmony export */ - SiglipVisionModel: () => ( - /* binding */ - SiglipVisionModel - ), - /* harmony export */ - SpeechT5ForSpeechToText: () => ( - /* binding */ - SpeechT5ForSpeechToText - ), - /* harmony export */ - SpeechT5ForTextToSpeech: () => ( - /* binding */ - SpeechT5ForTextToSpeech - ), - /* harmony export */ - SpeechT5HifiGan: () => ( - /* binding */ - SpeechT5HifiGan - ), - /* harmony export */ - SpeechT5Model: () => ( - /* binding */ - SpeechT5Model - ), - /* harmony export */ - SpeechT5PreTrainedModel: () => ( - /* binding */ - SpeechT5PreTrainedModel - ), - /* harmony export */ - SqueezeBertForMaskedLM: () => ( - /* binding */ - SqueezeBertForMaskedLM - ), - /* harmony export */ - SqueezeBertForQuestionAnswering: () => ( - /* binding */ - SqueezeBertForQuestionAnswering - ), - /* harmony export */ - SqueezeBertForSequenceClassification: () => ( - /* binding */ - SqueezeBertForSequenceClassification - ), - /* harmony export */ - SqueezeBertModel: () => ( - /* binding */ - SqueezeBertModel - ), - /* harmony export */ - SqueezeBertPreTrainedModel: () => ( - /* binding */ - SqueezeBertPreTrainedModel - ), - /* harmony export */ - StableLmForCausalLM: () => ( - /* binding */ - StableLmForCausalLM - ), - /* harmony export */ - StableLmModel: () => ( - /* binding */ - StableLmModel - ), - /* harmony export */ - StableLmPreTrainedModel: () => ( - /* binding */ - StableLmPreTrainedModel - ), - /* harmony export */ - Starcoder2ForCausalLM: () => ( - /* binding */ - Starcoder2ForCausalLM - ), - /* harmony export */ - Starcoder2Model: () => ( - /* binding */ - Starcoder2Model - ), - /* harmony export */ - Starcoder2PreTrainedModel: () => ( - /* binding */ - Starcoder2PreTrainedModel - ), - /* harmony export */ - Swin2SRForImageSuperResolution: () => ( - /* binding */ - Swin2SRForImageSuperResolution - ), - /* harmony export */ - Swin2SRModel: () => ( - /* binding */ - Swin2SRModel - ), - /* harmony export */ - Swin2SRPreTrainedModel: () => ( - /* binding */ - Swin2SRPreTrainedModel - ), - /* harmony export */ - SwinForImageClassification: () => ( - /* binding */ - SwinForImageClassification - ), - /* harmony export */ - SwinModel: () => ( - /* binding */ - SwinModel - ), - /* harmony export */ - SwinPreTrainedModel: () => ( - /* binding */ - SwinPreTrainedModel - ), - /* harmony export */ - T5ForConditionalGeneration: () => ( - /* binding */ - T5ForConditionalGeneration - ), - /* harmony export */ - T5Model: () => ( - /* binding */ - T5Model - ), - /* harmony export */ - T5PreTrainedModel: () => ( - /* binding */ - T5PreTrainedModel - ), - /* harmony export */ - TableTransformerForObjectDetection: () => ( - /* binding */ - TableTransformerForObjectDetection - ), - /* harmony export */ - TableTransformerModel: () => ( - /* binding */ - TableTransformerModel - ), - /* harmony export */ - TableTransformerObjectDetectionOutput: () => ( - /* binding */ - TableTransformerObjectDetectionOutput - ), - /* harmony export */ - TableTransformerPreTrainedModel: () => ( - /* binding */ - TableTransformerPreTrainedModel - ), - /* harmony export */ - TokenClassifierOutput: () => ( - /* binding */ - TokenClassifierOutput - ), - /* harmony export */ - TrOCRForCausalLM: () => ( - /* binding */ - TrOCRForCausalLM - ), - /* harmony export */ - TrOCRPreTrainedModel: () => ( - /* binding */ - TrOCRPreTrainedModel - ), - /* harmony export */ - UniSpeechForCTC: () => ( - /* binding */ - UniSpeechForCTC - ), - /* harmony export */ - UniSpeechForSequenceClassification: () => ( - /* binding */ - UniSpeechForSequenceClassification - ), - /* harmony export */ - UniSpeechModel: () => ( - /* binding */ - UniSpeechModel - ), - /* harmony export */ - UniSpeechPreTrainedModel: () => ( - /* binding */ - UniSpeechPreTrainedModel - ), - /* harmony export */ - UniSpeechSatForAudioFrameClassification: () => ( - /* binding */ - UniSpeechSatForAudioFrameClassification - ), - /* harmony export */ - UniSpeechSatForCTC: () => ( - /* binding */ - UniSpeechSatForCTC - ), - /* harmony export */ - UniSpeechSatForSequenceClassification: () => ( - /* binding */ - UniSpeechSatForSequenceClassification - ), - /* harmony export */ - UniSpeechSatModel: () => ( - /* binding */ - UniSpeechSatModel - ), - /* harmony export */ - UniSpeechSatPreTrainedModel: () => ( - /* binding */ - UniSpeechSatPreTrainedModel - ), - /* harmony export */ - ViTForImageClassification: () => ( - /* binding */ - ViTForImageClassification - ), - /* harmony export */ - ViTMAEModel: () => ( - /* binding */ - ViTMAEModel - ), - /* harmony export */ - ViTMAEPreTrainedModel: () => ( - /* binding */ - ViTMAEPreTrainedModel - ), - /* harmony export */ - ViTMSNForImageClassification: () => ( - /* binding */ - ViTMSNForImageClassification - ), - /* harmony export */ - ViTMSNModel: () => ( - /* binding */ - ViTMSNModel - ), - /* harmony export */ - ViTMSNPreTrainedModel: () => ( - /* binding */ - ViTMSNPreTrainedModel - ), - /* harmony export */ - ViTModel: () => ( - /* binding */ - ViTModel - ), - /* harmony export */ - ViTPreTrainedModel: () => ( - /* binding */ - ViTPreTrainedModel - ), - /* harmony export */ - VisionEncoderDecoderModel: () => ( - /* binding */ - VisionEncoderDecoderModel - ), - /* harmony export */ - VitMatteForImageMatting: () => ( - /* binding */ - VitMatteForImageMatting - ), - /* harmony export */ - VitMattePreTrainedModel: () => ( - /* binding */ - VitMattePreTrainedModel - ), - /* harmony export */ - VitsModel: () => ( - /* binding */ - VitsModel - ), - /* harmony export */ - VitsModelOutput: () => ( - /* binding */ - VitsModelOutput - ), - /* harmony export */ - VitsPreTrainedModel: () => ( - /* binding */ - VitsPreTrainedModel - ), - /* harmony export */ - Wav2Vec2BertForCTC: () => ( - /* binding */ - Wav2Vec2BertForCTC - ), - /* harmony export */ - Wav2Vec2BertForSequenceClassification: () => ( - /* binding */ - Wav2Vec2BertForSequenceClassification - ), - /* harmony export */ - Wav2Vec2BertModel: () => ( - /* binding */ - Wav2Vec2BertModel - ), - /* harmony export */ - Wav2Vec2BertPreTrainedModel: () => ( - /* binding */ - Wav2Vec2BertPreTrainedModel - ), - /* harmony export */ - Wav2Vec2ForAudioFrameClassification: () => ( - /* binding */ - Wav2Vec2ForAudioFrameClassification - ), - /* harmony export */ - Wav2Vec2ForCTC: () => ( - /* binding */ - Wav2Vec2ForCTC - ), - /* harmony export */ - Wav2Vec2ForSequenceClassification: () => ( - /* binding */ - Wav2Vec2ForSequenceClassification - ), - /* harmony export */ - Wav2Vec2Model: () => ( - /* binding */ - Wav2Vec2Model - ), - /* harmony export */ - Wav2Vec2PreTrainedModel: () => ( - /* binding */ - Wav2Vec2PreTrainedModel - ), - /* harmony export */ - WavLMForAudioFrameClassification: () => ( - /* binding */ - WavLMForAudioFrameClassification - ), - /* harmony export */ - WavLMForCTC: () => ( - /* binding */ - WavLMForCTC - ), - /* harmony export */ - WavLMForSequenceClassification: () => ( - /* binding */ - WavLMForSequenceClassification - ), - /* harmony export */ - WavLMForXVector: () => ( - /* binding */ - WavLMForXVector - ), - /* harmony export */ - WavLMModel: () => ( - /* binding */ - WavLMModel - ), - /* harmony export */ - WavLMPreTrainedModel: () => ( - /* binding */ - WavLMPreTrainedModel - ), - /* harmony export */ - WeSpeakerResNetModel: () => ( - /* binding */ - WeSpeakerResNetModel - ), - /* harmony export */ - WeSpeakerResNetPreTrainedModel: () => ( - /* binding */ - WeSpeakerResNetPreTrainedModel - ), - /* harmony export */ - WhisperForConditionalGeneration: () => ( - /* binding */ - WhisperForConditionalGeneration - ), - /* harmony export */ - WhisperModel: () => ( - /* binding */ - WhisperModel - ), - /* harmony export */ - WhisperPreTrainedModel: () => ( - /* binding */ - WhisperPreTrainedModel - ), - /* harmony export */ - XLMForQuestionAnswering: () => ( - /* binding */ - XLMForQuestionAnswering - ), - /* harmony export */ - XLMForSequenceClassification: () => ( - /* binding */ - XLMForSequenceClassification - ), - /* harmony export */ - XLMForTokenClassification: () => ( - /* binding */ - XLMForTokenClassification - ), - /* harmony export */ - XLMModel: () => ( - /* binding */ - XLMModel - ), - /* harmony export */ - XLMPreTrainedModel: () => ( - /* binding */ - XLMPreTrainedModel - ), - /* harmony export */ - XLMRobertaForMaskedLM: () => ( - /* binding */ - XLMRobertaForMaskedLM - ), - /* harmony export */ - XLMRobertaForQuestionAnswering: () => ( - /* binding */ - XLMRobertaForQuestionAnswering - ), - /* harmony export */ - XLMRobertaForSequenceClassification: () => ( - /* binding */ - XLMRobertaForSequenceClassification - ), - /* harmony export */ - XLMRobertaForTokenClassification: () => ( - /* binding */ - XLMRobertaForTokenClassification - ), - /* harmony export */ - XLMRobertaModel: () => ( - /* binding */ - XLMRobertaModel - ), - /* harmony export */ - XLMRobertaPreTrainedModel: () => ( - /* binding */ - XLMRobertaPreTrainedModel - ), - /* harmony export */ - XLMWithLMHeadModel: () => ( - /* binding */ - XLMWithLMHeadModel - ), - /* harmony export */ - XVectorOutput: () => ( - /* binding */ - XVectorOutput - ), - /* harmony export */ - YolosForObjectDetection: () => ( - /* binding */ - YolosForObjectDetection - ), - /* harmony export */ - YolosModel: () => ( - /* binding */ - YolosModel - ), - /* harmony export */ - YolosObjectDetectionOutput: () => ( - /* binding */ - YolosObjectDetectionOutput - ), - /* harmony export */ - YolosPreTrainedModel: () => ( - /* binding */ - YolosPreTrainedModel - ) - /* harmony export */ - }); - var _configs_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__2( - /*! ./configs.js */ - "./src/configs.js" - ); - var _backends_onnx_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__2( - /*! ./backends/onnx.js */ - "./src/backends/onnx.js" - ); - var _utils_dtypes_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__2( - /*! ./utils/dtypes.js */ - "./src/utils/dtypes.js" - ); - var _utils_generic_js__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__2( - /*! ./utils/generic.js */ - "./src/utils/generic.js" - ); - var _utils_core_js__WEBPACK_IMPORTED_MODULE_4__ = __webpack_require__2( - /*! ./utils/core.js */ - "./src/utils/core.js" - ); - var _utils_hub_js__WEBPACK_IMPORTED_MODULE_5__ = __webpack_require__2( - /*! ./utils/hub.js */ - "./src/utils/hub.js" - ); - var _utils_constants_js__WEBPACK_IMPORTED_MODULE_6__ = __webpack_require__2( - /*! ./utils/constants.js */ - "./src/utils/constants.js" - ); - var _generation_logits_process_js__WEBPACK_IMPORTED_MODULE_7__ = __webpack_require__2( - /*! ./generation/logits_process.js */ - "./src/generation/logits_process.js" - ); - var _generation_configuration_utils_js__WEBPACK_IMPORTED_MODULE_8__ = __webpack_require__2( - /*! ./generation/configuration_utils.js */ - "./src/generation/configuration_utils.js" - ); - var _utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__ = __webpack_require__2( - /*! ./utils/tensor.js */ - "./src/utils/tensor.js" - ); - var _utils_maths_js__WEBPACK_IMPORTED_MODULE_10__ = __webpack_require__2( - /*! ./utils/maths.js */ - "./src/utils/maths.js" - ); - var _generation_stopping_criteria_js__WEBPACK_IMPORTED_MODULE_11__2 = __webpack_require__2( - /*! ./generation/stopping_criteria.js */ - "./src/generation/stopping_criteria.js" - ); - var _generation_logits_sampler_js__WEBPACK_IMPORTED_MODULE_12__ = __webpack_require__2( - /*! ./generation/logits_sampler.js */ - "./src/generation/logits_sampler.js" - ); - var _env_js__WEBPACK_IMPORTED_MODULE_13__ = __webpack_require__2( - /*! ./env.js */ - "./src/env.js" - ); - var _models_whisper_generation_whisper_js__WEBPACK_IMPORTED_MODULE_14__ = __webpack_require__2( - /*! ./models/whisper/generation_whisper.js */ - "./src/models/whisper/generation_whisper.js" - ); - var _models_whisper_common_whisper_js__WEBPACK_IMPORTED_MODULE_15__ = __webpack_require__2( - /*! ./models/whisper/common_whisper.js */ - "./src/models/whisper/common_whisper.js" - ); - const MODEL_TYPES = { - EncoderOnly: 0, - EncoderDecoder: 1, - Seq2Seq: 2, - Vision2Seq: 3, - DecoderOnly: 4, - MaskGeneration: 5, - ImageTextToText: 6, - Musicgen: 7 - }; - const MODEL_TYPE_MAPPING = /* @__PURE__ */ new Map(); - const MODEL_NAME_TO_CLASS_MAPPING = /* @__PURE__ */ new Map(); - const MODEL_CLASS_TO_NAME_MAPPING = /* @__PURE__ */ new Map(); - async function getSession(pretrained_model_name_or_path, fileName, options) { - var _a; - const custom_config = ((_a = options.config) == null ? void 0 : _a["transformers.js_config"]) ?? {}; - let device = options.device ?? custom_config.device; - if (device && typeof device !== "string") { - if (device.hasOwnProperty(fileName)) { - device = device[fileName]; - } else { - console.warn(`device not specified for "${fileName}". Using the default device.`); - device = null; - } - } - const selectedDevice = ( - /** @type {import("./utils/devices.js").DeviceType} */ - device ?? (_env_js__WEBPACK_IMPORTED_MODULE_13__.apis.IS_NODE_ENV ? "cpu" : "wasm") - ); - const executionProviders = (0, _backends_onnx_js__WEBPACK_IMPORTED_MODULE_1__.deviceToExecutionProviders)(selectedDevice); - let dtype = options.dtype ?? custom_config.dtype; - if (typeof dtype !== "string") { - if (dtype && dtype.hasOwnProperty(fileName)) { - dtype = dtype[fileName]; - } else { - dtype = _utils_dtypes_js__WEBPACK_IMPORTED_MODULE_2__.DEFAULT_DEVICE_DTYPE_MAPPING[selectedDevice] ?? _utils_dtypes_js__WEBPACK_IMPORTED_MODULE_2__.DATA_TYPES.fp32; - console.warn(`dtype not specified for "${fileName}". Using the default dtype (${dtype}) for this device (${selectedDevice}).`); - } - } - const selectedDtype = ( - /** @type {import("./utils/dtypes.js").DataType} */ - dtype - ); - if (!_utils_dtypes_js__WEBPACK_IMPORTED_MODULE_2__.DEFAULT_DTYPE_SUFFIX_MAPPING.hasOwnProperty(selectedDtype)) { - throw new Error(`Invalid dtype: ${selectedDtype}. Should be one of: ${Object.keys(_utils_dtypes_js__WEBPACK_IMPORTED_MODULE_2__.DATA_TYPES).join(", ")}`); - } else if (selectedDtype === _utils_dtypes_js__WEBPACK_IMPORTED_MODULE_2__.DATA_TYPES.fp16 && selectedDevice === "webgpu" && !await (0, _utils_dtypes_js__WEBPACK_IMPORTED_MODULE_2__.isWebGpuFp16Supported)()) { - throw new Error(`The device (${selectedDevice}) does not support fp16.`); - } - const kv_cache_dtype = custom_config.kv_cache_dtype ? typeof custom_config.kv_cache_dtype === "string" ? custom_config.kv_cache_dtype : custom_config.kv_cache_dtype[selectedDtype] ?? "float32" : void 0; - if (kv_cache_dtype && !["float32", "float16"].includes(kv_cache_dtype)) { - throw new Error(`Invalid kv_cache_dtype: ${kv_cache_dtype}. Should be one of: float32, float16`); - } - const session_config = { - dtype: selectedDtype, - kv_cache_dtype - }; - const suffix = _utils_dtypes_js__WEBPACK_IMPORTED_MODULE_2__.DEFAULT_DTYPE_SUFFIX_MAPPING[selectedDtype]; - const modelFileName = `${options.subfolder ?? ""}/${fileName}${suffix}.onnx`; - const session_options = { ...options.session_options }; - session_options.executionProviders ?? (session_options.executionProviders = executionProviders); - const free_dimension_overrides = custom_config.free_dimension_overrides; - if (free_dimension_overrides) { - session_options.freeDimensionOverrides ?? (session_options.freeDimensionOverrides = free_dimension_overrides); - } else if (selectedDevice.startsWith("webnn") && !session_options.freeDimensionOverrides) { - console.warn( - 'WebNN does not currently support dynamic shapes and requires `free_dimension_overrides` to be set in config.json as a field within "transformers.js_config". When `free_dimension_overrides` is not set, you may experience significant performance degradation.' - ); - } - const bufferPromise = (0, _utils_hub_js__WEBPACK_IMPORTED_MODULE_5__.getModelFile)(pretrained_model_name_or_path, modelFileName, true, options); - const use_external_data_format = options.use_external_data_format ?? custom_config.use_external_data_format; - let externalDataPromises = []; - if (use_external_data_format && (use_external_data_format === true || typeof use_external_data_format === "object" && use_external_data_format.hasOwnProperty(fileName) && use_external_data_format[fileName] === true)) { - if (_env_js__WEBPACK_IMPORTED_MODULE_13__.apis.IS_NODE_ENV) { - throw new Error("External data format is not yet supported in Node.js"); - } - const path = `${fileName}${suffix}.onnx_data`; - const fullPath = `${options.subfolder ?? ""}/${path}`; - externalDataPromises.push(new Promise(async (resolve, reject) => { - const data = await (0, _utils_hub_js__WEBPACK_IMPORTED_MODULE_5__.getModelFile)(pretrained_model_name_or_path, fullPath, true, options); - resolve({ path, data }); - })); - } else if (session_options.externalData !== void 0) { - externalDataPromises = session_options.externalData.map(async (ext) => { - if (typeof ext.data === "string") { - const ext_buffer = await (0, _utils_hub_js__WEBPACK_IMPORTED_MODULE_5__.getModelFile)(pretrained_model_name_or_path, ext.data, true, options); - return { ...ext, data: ext_buffer }; - } - return ext; - }); - } - if (externalDataPromises.length > 0) { - session_options.externalData = await Promise.all(externalDataPromises); - } - if (selectedDevice === "webgpu") { - const shapes = (0, _configs_js__WEBPACK_IMPORTED_MODULE_0__.getKeyValueShapes)(options.config, { - prefix: "present" - }); - if (Object.keys(shapes).length > 0 && !(0, _backends_onnx_js__WEBPACK_IMPORTED_MODULE_1__.isONNXProxy)()) { - const preferredOutputLocation = {}; - for (const key in shapes) { - preferredOutputLocation[key] = "gpu-buffer"; - } - session_options.preferredOutputLocation = preferredOutputLocation; - } - } - const buffer = await bufferPromise; - return { buffer, session_options, session_config }; - } - async function constructSessions(pretrained_model_name_or_path, names, options) { - return Object.fromEntries(await Promise.all( - Object.keys(names).map(async (name) => { - const { buffer, session_options, session_config } = await getSession(pretrained_model_name_or_path, names[name], options); - const session = await (0, _backends_onnx_js__WEBPACK_IMPORTED_MODULE_1__.createInferenceSession)(buffer, session_options, session_config); - return [name, session]; - }) - )); - } - async function getOptionalConfigs(pretrained_model_name_or_path, names, options) { - return Object.fromEntries(await Promise.all( - Object.keys(names).map(async (name) => { - const config = await (0, _utils_hub_js__WEBPACK_IMPORTED_MODULE_5__.getModelJSON)(pretrained_model_name_or_path, names[name], false, options); - return [name, config]; - }) - )); - } - function validateInputs(session, inputs) { - const checkedInputs = /* @__PURE__ */ Object.create(null); - const missingInputs = []; - for (const inputName of session.inputNames) { - const tensor = inputs[inputName]; - if (!(tensor instanceof _utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.Tensor)) { - missingInputs.push(inputName); - continue; - } - checkedInputs[inputName] = (0, _backends_onnx_js__WEBPACK_IMPORTED_MODULE_1__.isONNXProxy)() ? tensor.clone() : tensor; - } - if (missingInputs.length > 0) { - throw new Error( - `An error occurred during model execution: "Missing the following inputs: ${missingInputs.join(", ")}.` - ); - } - const numInputsProvided = Object.keys(inputs).length; - const numInputsNeeded = session.inputNames.length; - if (numInputsProvided > numInputsNeeded) { - let ignored = Object.keys(inputs).filter((inputName) => !session.inputNames.includes(inputName)); - console.warn(`WARNING: Too many inputs were provided (${numInputsProvided} > ${numInputsNeeded}). The following inputs will be ignored: "${ignored.join(", ")}".`); - } - return checkedInputs; - } - async function sessionRun(session, inputs) { - const checkedInputs = validateInputs(session, inputs); - try { - const ortFeed = Object.fromEntries(Object.entries(checkedInputs).map(([k, v]) => [k, v.ort_tensor])); - let output = await session.run(ortFeed); - output = replaceTensors(output); - return output; - } catch (e) { - console.error(`An error occurred during model execution: "${e}".`); - console.error("Inputs given to model:", checkedInputs); - throw e; - } - } - function replaceTensors(obj) { - for (let prop in obj) { - if ((0, _backends_onnx_js__WEBPACK_IMPORTED_MODULE_1__.isONNXTensor)(obj[prop])) { - obj[prop] = new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.Tensor(obj[prop]); - } else if (typeof obj[prop] === "object") { - replaceTensors(obj[prop]); - } - } - return obj; - } - function toI64Tensor(items) { - if (items instanceof _utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.Tensor) { - return items; - } - if (items.length === 0) { - throw Error("items must be non-empty"); - } - if (Array.isArray(items[0])) { - if (items.some((x) => x.length !== items[0].length)) { - throw Error("Unable to create tensor, you should probably activate truncation and/or padding with 'padding=True' and/or 'truncation=True' to have batched tensors with the same length."); - } - return new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.Tensor( - "int64", - BigInt64Array.from(items.flat().map((x) => BigInt(x))), - [items.length, items[0].length] - ); - } else { - return new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.Tensor( - "int64", - BigInt64Array.from(items.map((x) => BigInt(x))), - [1, items.length] - ); - } - } - function boolTensor(value) { - return new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.Tensor("bool", [value], [1]); - } - async function seq2seqForward(self2, model_inputs) { - let { encoder_outputs, input_ids, decoder_input_ids, ...other_decoder_inputs } = model_inputs; - if (!encoder_outputs) { - const encoder_inputs = (0, _utils_core_js__WEBPACK_IMPORTED_MODULE_4__.pick)(model_inputs, self2.sessions["model"].inputNames); - encoder_outputs = (await encoderForward(self2, encoder_inputs)).last_hidden_state; - } - other_decoder_inputs.input_ids = decoder_input_ids; - other_decoder_inputs.encoder_hidden_states = encoder_outputs; - if (self2.sessions["decoder_model_merged"].inputNames.includes("encoder_attention_mask")) { - other_decoder_inputs.encoder_attention_mask = model_inputs.attention_mask; - } - const decoderResults = await decoderForward(self2, other_decoder_inputs, true); - return decoderResults; - } - async function encoderForward(self2, model_inputs) { - const session = self2.sessions["model"]; - const encoderFeeds = (0, _utils_core_js__WEBPACK_IMPORTED_MODULE_4__.pick)(model_inputs, session.inputNames); - if (session.inputNames.includes("inputs_embeds") && !encoderFeeds.inputs_embeds) { - if (!model_inputs.input_ids) { - throw new Error("Both `input_ids` and `inputs_embeds` are missing in the model inputs."); - } - encoderFeeds.inputs_embeds = await self2.encode_text({ input_ids: model_inputs.input_ids }); - } - if (session.inputNames.includes("token_type_ids") && !encoderFeeds.token_type_ids) { - encoderFeeds.token_type_ids = new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.Tensor( - "int64", - new BigInt64Array(encoderFeeds.input_ids.data.length), - encoderFeeds.input_ids.dims - ); - } - return await sessionRun(session, encoderFeeds); - } - async function decoderForward(self2, model_inputs, is_encoder_decoder = false) { - const session = self2.sessions[is_encoder_decoder ? "decoder_model_merged" : "model"]; - const { past_key_values, ...new_model_inputs } = model_inputs; - if (session.inputNames.includes("use_cache_branch")) { - new_model_inputs.use_cache_branch = boolTensor(!!past_key_values); - } - if (session.inputNames.includes("position_ids") && new_model_inputs.attention_mask && !new_model_inputs.position_ids) { - new_model_inputs.position_ids = createPositionIds(new_model_inputs, past_key_values); - } - self2.addPastKeyValues(new_model_inputs, past_key_values); - const fixed = (0, _utils_core_js__WEBPACK_IMPORTED_MODULE_4__.pick)(new_model_inputs, session.inputNames); - return await sessionRun(session, fixed); - } - async function imageTextToTextForward(self2, { - // Produced by the tokenizer/processor: - input_ids = null, - attention_mask = null, - pixel_values = null, - // Used during generation: - position_ids = null, - inputs_embeds = null, - past_key_values = null, - // Generic generation parameters - generation_config = null, - logits_processor = null, - // TODO: needed? - ...kwargs - }) { - if (!inputs_embeds) { - inputs_embeds = await self2.encode_text({ input_ids }); - if (pixel_values && input_ids.dims[1] !== 1) { - const image_features = await self2.encode_image({ pixel_values }); - ({ inputs_embeds, attention_mask } = self2._merge_input_ids_with_image_features({ - image_features, - inputs_embeds, - input_ids, - attention_mask - })); - } else if (past_key_values && pixel_values && input_ids.dims[1] === 1) { - const target_length = input_ids.dims[1]; - const past_length = Object.values(past_key_values)[0].dims.at(-2); - attention_mask = (0, _utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.cat)([ - (0, _utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.ones)([input_ids.dims[0], past_length]), - attention_mask.slice(null, [attention_mask.dims[1] - target_length, attention_mask.dims[1]]) - ], 1); - } - } - const outputs = await decoderForward(self2, { - inputs_embeds, - past_key_values, - attention_mask, - position_ids, - generation_config, - logits_processor - }, true); - return outputs; - } - function createPositionIds(model_inputs, past_key_values = null) { - const { input_ids, inputs_embeds, attention_mask } = model_inputs; - const [bz, seq_len] = attention_mask.dims; - const data = new BigInt64Array(attention_mask.data.length); - for (let i = 0; i < bz; ++i) { - const start = i * seq_len; - let sum = BigInt(0); - for (let j = 0; j < seq_len; ++j) { - const index = start + j; - if (attention_mask.data[index] === 0n) { - data[index] = BigInt(1); - } else { - data[index] = sum; - sum += attention_mask.data[index]; - } - } - } - let position_ids = new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.Tensor("int64", data, attention_mask.dims); - if (past_key_values) { - const offset = -(input_ids ?? inputs_embeds).dims.at(1); - position_ids = position_ids.slice(null, [offset, null]); - } - return position_ids; - } - function decoder_prepare_inputs_for_generation(self2, input_ids, model_inputs, generation_config) { - if (model_inputs.past_key_values) { - const past_length = Object.values(model_inputs.past_key_values)[0].dims.at(-2); - const { input_ids: input_ids2, attention_mask } = model_inputs; - if (attention_mask && attention_mask.dims[1] > input_ids2.dims[1]) { - } else if (past_length < input_ids2.dims[1]) { - model_inputs.input_ids = input_ids2.slice(null, [past_length, null]); - } else { - if ( - // NOTE: Only used by VLMs (!= so that null matches undefined) - self2.config.image_token_index != null && // Equivalent to `self.config.image_token_index in input_ids` (== so that int matches bigint) - input_ids2.data.some((x) => x == self2.config.image_token_index) - ) { - const num_image_tokens = self2.config.num_image_tokens; - if (!num_image_tokens) { - throw new Error("`num_image_tokens` is missing in the model configuration."); - } - const num_new_tokens = input_ids2.dims[1] - (past_length - num_image_tokens); - model_inputs.input_ids = input_ids2.slice(null, [-num_new_tokens, null]); - model_inputs.attention_mask = (0, _utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.ones)([1, past_length + num_new_tokens]); - } - } - } - return model_inputs; - } - function encoder_decoder_prepare_inputs_for_generation(self2, input_ids, model_inputs, generation_config) { - if (model_inputs.past_key_values) { - input_ids = input_ids.map((x) => [x.at(-1)]); - } - return { - ...model_inputs, - decoder_input_ids: toI64Tensor(input_ids) - }; - } - function image_text_to_text_prepare_inputs_for_generation(self2, ...args) { - if (self2.config.is_encoder_decoder) { - return encoder_decoder_prepare_inputs_for_generation(self2, ...args); - } else { - return decoder_prepare_inputs_for_generation(self2, ...args); - } - } - class PreTrainedModel extends _utils_generic_js__WEBPACK_IMPORTED_MODULE_3__.Callable { - /** - * Creates a new instance of the `PreTrainedModel` class. - * @param {import('./configs.js').PretrainedConfig} config The model configuration. - * @param {Record} sessions The inference sessions for the model. - * @param {Record} configs Additional configuration files (e.g., generation_config.json). - */ - constructor(config, sessions, configs) { - super(); - __publicField(this, "main_input_name", "input_ids"); - __publicField(this, "forward_params", ["input_ids", "attention_mask"]); - this.config = config; - this.sessions = sessions; - this.configs = configs; - const modelName = MODEL_CLASS_TO_NAME_MAPPING.get(this.constructor); - const modelType = MODEL_TYPE_MAPPING.get(modelName); - this.can_generate = false; - this._forward = null; - this._prepare_inputs_for_generation = null; - switch (modelType) { - case MODEL_TYPES.DecoderOnly: - this.can_generate = true; - this._forward = decoderForward; - this._prepare_inputs_for_generation = decoder_prepare_inputs_for_generation; - break; - case MODEL_TYPES.Seq2Seq: - case MODEL_TYPES.Vision2Seq: - case MODEL_TYPES.Musicgen: - this.can_generate = true; - this._forward = seq2seqForward; - this._prepare_inputs_for_generation = encoder_decoder_prepare_inputs_for_generation; - break; - case MODEL_TYPES.EncoderDecoder: - this._forward = seq2seqForward; - break; - case MODEL_TYPES.ImageTextToText: - this.can_generate = true; - this._forward = imageTextToTextForward; - this._prepare_inputs_for_generation = image_text_to_text_prepare_inputs_for_generation; - break; - default: - this._forward = encoderForward; - break; - } - if (this.can_generate) { - this.forward_params.push("past_key_values"); - } - this.custom_config = this.config["transformers.js_config"] ?? {}; - } - /** - * Disposes of all the ONNX sessions that were created during inference. - * @returns {Promise} An array of promises, one for each ONNX session that is being disposed. - * @todo Use https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/FinalizationRegistry - */ - async dispose() { - var _a; - const promises = []; - for (const session of Object.values(this.sessions)) { - if ((_a = session == null ? void 0 : session.handler) == null ? void 0 : _a.dispose) { - promises.push(session.handler.dispose()); - } - } - return await Promise.all(promises); - } - /** - * Instantiate one of the model classes of the library from a pretrained model. - * - * The model class to instantiate is selected based on the `model_type` property of the config object - * (either passed as an argument or loaded from `pretrained_model_name_or_path` if possible) - * - * @param {string} pretrained_model_name_or_path The name or path of the pretrained model. Can be either: - * - A string, the *model id* of a pretrained model hosted inside a model repo on huggingface.co. - * Valid model ids can be located at the root-level, like `bert-base-uncased`, or namespaced under a - * user or organization name, like `dbmdz/bert-base-german-cased`. - * - A path to a *directory* containing model weights, e.g., `./my_model_directory/`. - * @param {import('./utils/hub.js').PretrainedModelOptions} options Additional options for loading the model. - * - * @returns {Promise} A new instance of the `PreTrainedModel` class. - */ - static async from_pretrained(pretrained_model_name_or_path, { - progress_callback = null, - config = null, - cache_dir = null, - local_files_only = false, - revision = "main", - model_file_name = null, - subfolder = "onnx", - device = null, - dtype = null, - use_external_data_format = null, - session_options = {} - } = {}) { - let options = { - progress_callback, - config, - cache_dir, - local_files_only, - revision, - model_file_name, - subfolder, - device, - dtype, - use_external_data_format, - session_options - }; - const modelName = MODEL_CLASS_TO_NAME_MAPPING.get(this); - const modelType = MODEL_TYPE_MAPPING.get(modelName); - config = options.config = await _configs_js__WEBPACK_IMPORTED_MODULE_0__.AutoConfig.from_pretrained(pretrained_model_name_or_path, options); - let info; - if (modelType === MODEL_TYPES.DecoderOnly) { - info = await Promise.all([ - constructSessions(pretrained_model_name_or_path, { - model: options.model_file_name ?? "model" - }, options), - getOptionalConfigs(pretrained_model_name_or_path, { - generation_config: "generation_config.json" - }, options) - ]); - } else if (modelType === MODEL_TYPES.Seq2Seq || modelType === MODEL_TYPES.Vision2Seq) { - info = await Promise.all([ - constructSessions(pretrained_model_name_or_path, { - model: "encoder_model", - decoder_model_merged: "decoder_model_merged" - }, options), - getOptionalConfigs(pretrained_model_name_or_path, { - generation_config: "generation_config.json" - }, options) - ]); - } else if (modelType === MODEL_TYPES.MaskGeneration) { - info = await Promise.all([ - constructSessions(pretrained_model_name_or_path, { - model: "vision_encoder", - prompt_encoder_mask_decoder: "prompt_encoder_mask_decoder" - }, options) - ]); - } else if (modelType === MODEL_TYPES.EncoderDecoder) { - info = await Promise.all([ - constructSessions(pretrained_model_name_or_path, { - model: "encoder_model", - decoder_model_merged: "decoder_model_merged" - }, options) - ]); - } else if (modelType === MODEL_TYPES.ImageTextToText) { - const sessions = { - embed_tokens: "embed_tokens", - vision_encoder: "vision_encoder", - decoder_model_merged: "decoder_model_merged" - }; - if (config.is_encoder_decoder) { - sessions["model"] = "encoder_model"; - } - info = await Promise.all([ - constructSessions(pretrained_model_name_or_path, sessions, options), - getOptionalConfigs(pretrained_model_name_or_path, { - generation_config: "generation_config.json" - }, options) - ]); - } else if (modelType === MODEL_TYPES.Musicgen) { - info = await Promise.all([ - constructSessions(pretrained_model_name_or_path, { - model: "text_encoder", - decoder_model_merged: "decoder_model_merged", - encodec_decode: "encodec_decode" - }, options), - getOptionalConfigs(pretrained_model_name_or_path, { - generation_config: "generation_config.json" - }, options) - ]); - } else { - if (modelType !== MODEL_TYPES.EncoderOnly) { - console.warn(`Model type for '${modelName ?? (config == null ? void 0 : config.model_type)}' not found, assuming encoder-only architecture. Please report this at ${_utils_constants_js__WEBPACK_IMPORTED_MODULE_6__.GITHUB_ISSUE_URL}.`); - } - info = await Promise.all([ - constructSessions(pretrained_model_name_or_path, { - model: options.model_file_name ?? "model" - }, options) - ]); - } - return new this(config, ...info); - } - /** - * Runs the model with the provided inputs - * @param {Object} model_inputs Object containing input tensors - * @returns {Promise} Object containing output tensors - */ - async _call(model_inputs) { - return await this.forward(model_inputs); - } - /** - * Forward method for a pretrained model. If not overridden by a subclass, the correct forward method - * will be chosen based on the model type. - * @param {Object} model_inputs The input data to the model in the format specified in the ONNX model. - * @returns {Promise} The output data from the model in the format specified in the ONNX model. - * @throws {Error} This method must be implemented in subclasses. - */ - async forward(model_inputs) { - return await this._forward(this, model_inputs); - } - /** - * Get the model's generation config, if it exists. - * @returns {GenerationConfig|null} The model's generation config if it exists, otherwise `null`. - */ - get generation_config() { - var _a; - return ((_a = this.configs) == null ? void 0 : _a.generation_config) ?? null; - } - /** - * This function returns a [`LogitsProcessorList`] list object that contains all relevant [`LogitsWarper`] - * instances used for multinomial sampling. - * @param {GenerationConfig} generation_config The generation config. - * @returns {LogitsProcessorList} generation_config - */ - _get_logits_warper(generation_config) { - const warpers = new _generation_logits_process_js__WEBPACK_IMPORTED_MODULE_7__.LogitsProcessorList(); - if (generation_config.temperature !== null && generation_config.temperature !== 1) { - warpers.push(new _generation_logits_process_js__WEBPACK_IMPORTED_MODULE_7__.TemperatureLogitsWarper(generation_config.temperature)); - } - if (generation_config.top_k !== null && generation_config.top_k !== 0) { - warpers.push(new _generation_logits_process_js__WEBPACK_IMPORTED_MODULE_7__.TopKLogitsWarper(generation_config.top_k)); - } - if (generation_config.top_p !== null && generation_config.top_p < 1) { - warpers.push(new _generation_logits_process_js__WEBPACK_IMPORTED_MODULE_7__.TopPLogitsWarper(generation_config.top_p)); - } - return warpers; - } - /** - * @param {GenerationConfig} generation_config - * @param {number} input_ids_seq_length The starting sequence length for the input ids. - * @returns {LogitsProcessorList} - * @private - */ - _get_logits_processor(generation_config, input_ids_seq_length, logits_processor = null) { - const processors = new _generation_logits_process_js__WEBPACK_IMPORTED_MODULE_7__.LogitsProcessorList(); - if (generation_config.repetition_penalty !== null && generation_config.repetition_penalty !== 1) { - processors.push(new _generation_logits_process_js__WEBPACK_IMPORTED_MODULE_7__.RepetitionPenaltyLogitsProcessor(generation_config.repetition_penalty)); - } - if (generation_config.no_repeat_ngram_size !== null && generation_config.no_repeat_ngram_size > 0) { - processors.push(new _generation_logits_process_js__WEBPACK_IMPORTED_MODULE_7__.NoRepeatNGramLogitsProcessor(generation_config.no_repeat_ngram_size)); - } - if (generation_config.bad_words_ids !== null) { - processors.push(new _generation_logits_process_js__WEBPACK_IMPORTED_MODULE_7__.NoBadWordsLogitsProcessor(generation_config.bad_words_ids, generation_config.eos_token_id)); - } - if (generation_config.min_length !== null && generation_config.eos_token_id !== null && generation_config.min_length > 0) { - processors.push(new _generation_logits_process_js__WEBPACK_IMPORTED_MODULE_7__.MinLengthLogitsProcessor(generation_config.min_length, generation_config.eos_token_id)); - } - if (generation_config.min_new_tokens !== null && generation_config.eos_token_id !== null && generation_config.min_new_tokens > 0) { - processors.push(new _generation_logits_process_js__WEBPACK_IMPORTED_MODULE_7__.MinNewTokensLengthLogitsProcessor( - input_ids_seq_length, - generation_config.min_new_tokens, - generation_config.eos_token_id - )); - } - if (generation_config.forced_bos_token_id !== null) { - processors.push(new _generation_logits_process_js__WEBPACK_IMPORTED_MODULE_7__.ForcedBOSTokenLogitsProcessor(generation_config.forced_bos_token_id)); - } - if (generation_config.forced_eos_token_id !== null) { - processors.push(new _generation_logits_process_js__WEBPACK_IMPORTED_MODULE_7__.ForcedEOSTokenLogitsProcessor( - generation_config.max_length, - generation_config.forced_eos_token_id - )); - } - if (generation_config.begin_suppress_tokens !== null) { - const begin_index = input_ids_seq_length > 1 || generation_config.forced_bos_token_id === null ? input_ids_seq_length : input_ids_seq_length + 1; - processors.push(new _generation_logits_process_js__WEBPACK_IMPORTED_MODULE_7__.SuppressTokensAtBeginLogitsProcessor(generation_config.begin_suppress_tokens, begin_index)); - } - if (generation_config.guidance_scale !== null && generation_config.guidance_scale > 1) { - processors.push(new _generation_logits_process_js__WEBPACK_IMPORTED_MODULE_7__.ClassifierFreeGuidanceLogitsProcessor(generation_config.guidance_scale)); - } - if (logits_processor !== null) { - processors.extend(logits_processor); - } - return processors; - } - /** - * This function merges multiple generation configs together to form a final generation config to be used by the model for text generation. - * It first creates an empty `GenerationConfig` object, then it applies the model's own `generation_config` property to it. Finally, if a `generation_config` object was passed in the arguments, it overwrites the corresponding properties in the final config with those of the passed config object. - * @param {GenerationConfig|null} generation_config A `GenerationConfig` object containing generation parameters. - * @param {Object} kwargs Additional generation parameters to be used in place of those in the `generation_config` object. - * @returns {GenerationConfig} The final generation config object to be used by the model for text generation. - */ - _prepare_generation_config(generation_config, kwargs, cls = _generation_configuration_utils_js__WEBPACK_IMPORTED_MODULE_8__.GenerationConfig) { - const config = { ...this.config }; - for (const key of ["decoder", "generator", "text_config"]) { - if (key in config) { - Object.assign(config, config[key]); - } - } - const gen_config = new cls(config); - Object.assign(gen_config, this.generation_config ?? {}); - if (generation_config) { - Object.assign(gen_config, generation_config); - } - if (kwargs) { - Object.assign(gen_config, (0, _utils_core_js__WEBPACK_IMPORTED_MODULE_4__.pick)(kwargs, Object.getOwnPropertyNames(gen_config))); - } - return gen_config; - } - /** - * - * @param {GenerationConfig} generation_config - * @param {StoppingCriteriaList} [stopping_criteria=null] - */ - _get_stopping_criteria(generation_config, stopping_criteria = null) { - const criteria = new _generation_stopping_criteria_js__WEBPACK_IMPORTED_MODULE_11__2.StoppingCriteriaList(); - if (generation_config.max_length !== null) { - criteria.push(new _generation_stopping_criteria_js__WEBPACK_IMPORTED_MODULE_11__2.MaxLengthCriteria( - generation_config.max_length, - this.config.max_position_embeddings ?? null - )); - } - if (generation_config.eos_token_id !== null) { - criteria.push(new _generation_stopping_criteria_js__WEBPACK_IMPORTED_MODULE_11__2.EosTokenCriteria(generation_config.eos_token_id)); - } - if (stopping_criteria) { - criteria.extend(stopping_criteria); - } - return criteria; - } - /** - * Confirms that the model class is compatible with generation. - * If not, raises an exception that points to the right class to use. - */ - _validate_model_class() { - if (!this.can_generate) { - const generate_compatible_mappings = [ - MODEL_FOR_CAUSAL_LM_MAPPING_NAMES, - // MODEL_FOR_CAUSAL_IMAGE_MODELING_MAPPING, // TODO - MODEL_FOR_VISION_2_SEQ_MAPPING_NAMES, - MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING_NAMES, - MODEL_FOR_SPEECH_SEQ_2_SEQ_MAPPING_NAMES - ]; - const modelName = MODEL_CLASS_TO_NAME_MAPPING.get(this.constructor); - const generate_compatible_classes = /* @__PURE__ */ new Set(); - const modelType = this.config.model_type; - for (const model_mapping of generate_compatible_mappings) { - const supported_models = model_mapping.get(modelType); - if (supported_models) { - generate_compatible_classes.add(supported_models[0]); - } - } - let errorMessage = `The current model class (${modelName}) is not compatible with \`.generate()\`, as it doesn't have a language model head.`; - if (generate_compatible_classes.size > 0) { - errorMessage += ` Please use the following class instead: ${[...generate_compatible_classes].join(", ")}`; - } - throw Error(errorMessage); - } - } - prepare_inputs_for_generation(...args) { - return this._prepare_inputs_for_generation(this, ...args); - } - /** - * - * @param {Object} inputs - * @param {bigint[][]} inputs.generated_input_ids - * @param {Object} inputs.outputs - * @param {Object} inputs.model_inputs - * @param {boolean} inputs.is_encoder_decoder - * @returns {Object} The updated model inputs for the next generation iteration. - */ - _update_model_kwargs_for_generation({ generated_input_ids, outputs, model_inputs, is_encoder_decoder }) { - model_inputs["past_key_values"] = this.getPastKeyValues(outputs, model_inputs.past_key_values); - model_inputs["input_ids"] = new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.Tensor("int64", generated_input_ids.flat(), [generated_input_ids.length, 1]); - if (!is_encoder_decoder) { - model_inputs.attention_mask = (0, _utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.cat)( - [ - model_inputs.attention_mask, - (0, _utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.ones)([model_inputs.attention_mask.dims[0], 1]) - ], - 1 - ); - } else if ("decoder_attention_mask" in model_inputs) { - } - model_inputs["position_ids"] = null; - return model_inputs; - } - /** - * This function extracts the model-specific `inputs` for generation. - * @param {Object} params - * @param {Tensor} [params.inputs=null] - * @param {number} [params.bos_token_id=null] - * @param {Record} [params.model_kwargs] - * @returns {{inputs_tensor: Tensor, model_inputs: Record, model_input_name: string}} The model-specific inputs for generation. - */ - _prepare_model_inputs({ inputs, bos_token_id, model_kwargs }) { - const model_inputs = (0, _utils_core_js__WEBPACK_IMPORTED_MODULE_4__.pick)(model_kwargs, this.forward_params); - const input_name = this.main_input_name; - if (input_name in model_inputs) { - if (inputs) { - throw new Error( - "`inputs`: {inputs}` were passed alongside {input_name} which is not allowed. Make sure to either pass {inputs} or {input_name}=..." - ); - } - } else { - model_inputs[input_name] = inputs; - } - const inputs_tensor = model_inputs[input_name]; - return { inputs_tensor, model_inputs, model_input_name: input_name }; - } - async _prepare_encoder_decoder_kwargs_for_generation({ inputs_tensor, model_inputs, model_input_name, generation_config }) { - if (this.sessions["model"].inputNames.includes("inputs_embeds") && !model_inputs.inputs_embeds && "_prepare_inputs_embeds" in this) { - const { input_ids, pixel_values, attention_mask, ...kwargs } = model_inputs; - const prepared_inputs = await this._prepare_inputs_embeds(model_inputs); - model_inputs = { - ...kwargs, - ...(0, _utils_core_js__WEBPACK_IMPORTED_MODULE_4__.pick)(prepared_inputs, ["inputs_embeds", "attention_mask"]) - }; - } - let { last_hidden_state } = await encoderForward(this, model_inputs); - if (generation_config.guidance_scale !== null && generation_config.guidance_scale > 1) { - last_hidden_state = (0, _utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.cat)([ - last_hidden_state, - (0, _utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.full_like)(last_hidden_state, 0) - ], 0); - if ("attention_mask" in model_inputs) { - model_inputs["attention_mask"] = (0, _utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.cat)([ - model_inputs["attention_mask"], - (0, _utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.zeros_like)(model_inputs["attention_mask"]) - ], 0); - } - } else if (model_inputs.decoder_input_ids) { - const decoder_input_ids_batch_size = toI64Tensor(model_inputs.decoder_input_ids).dims[0]; - if (decoder_input_ids_batch_size !== last_hidden_state.dims[0]) { - if (last_hidden_state.dims[0] !== 1) { - throw new Error( - `The encoder outputs have a different batch size (${last_hidden_state.dims[0]}) than the decoder inputs (${decoder_input_ids_batch_size}).` - ); - } - last_hidden_state = (0, _utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.cat)(Array.from({ length: decoder_input_ids_batch_size }, () => last_hidden_state), 0); - } - } - model_inputs["encoder_outputs"] = last_hidden_state; - return model_inputs; - } - /** - * Prepares `decoder_input_ids` for generation with encoder-decoder models - * @param {*} param0 - */ - _prepare_decoder_input_ids_for_generation({ batch_size, model_input_name, model_kwargs, decoder_start_token_id, bos_token_id, generation_config }) { - let { decoder_input_ids, ...model_inputs } = model_kwargs; - if (!decoder_input_ids) { - decoder_start_token_id ?? (decoder_start_token_id = bos_token_id); - if (this.config.model_type === "musicgen") { - decoder_input_ids = Array.from({ - length: batch_size * this.config.decoder.num_codebooks - }, () => [decoder_start_token_id]); - } else if (Array.isArray(decoder_start_token_id)) { - if (decoder_start_token_id.length !== batch_size) { - throw new Error( - `\`decoder_start_token_id\` expcted to have length ${batch_size} but got ${decoder_start_token_id.length}` - ); - } - decoder_input_ids = decoder_start_token_id; - } else { - decoder_input_ids = Array.from({ - length: batch_size - }, () => [decoder_start_token_id]); - } - } else if (!Array.isArray(decoder_input_ids[0])) { - decoder_input_ids = Array.from({ - length: batch_size - }, () => decoder_input_ids); - } - decoder_input_ids = toI64Tensor(decoder_input_ids); - model_kwargs["decoder_attention_mask"] = (0, _utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.ones_like)(decoder_input_ids); - return { input_ids: decoder_input_ids, model_inputs }; - } - /** - * Generates sequences of token ids for models with a language modeling head. - * @param {import('./generation/parameters.js').GenerationFunctionParameters} options - * @returns {Promise} The output of the model, which can contain the generated token ids, attentions, and scores. - */ - async generate({ - inputs = null, - generation_config = null, - logits_processor = null, - stopping_criteria = null, - streamer = null, - // inputs_attention_mask = null, - ...kwargs - }) { - this._validate_model_class(); - generation_config = this._prepare_generation_config(generation_config, kwargs); - let { inputs_tensor, model_inputs, model_input_name } = this._prepare_model_inputs({ - inputs, - model_kwargs: kwargs - }); - const is_encoder_decoder = this.config.is_encoder_decoder; - if (!is_encoder_decoder) { - } else if (!("encoder_outputs" in model_inputs)) { - model_inputs = await this._prepare_encoder_decoder_kwargs_for_generation( - { inputs_tensor, model_inputs, model_input_name, generation_config } - ); - } - let input_ids; - if (is_encoder_decoder) { - ({ input_ids, model_inputs } = this._prepare_decoder_input_ids_for_generation({ - batch_size: model_inputs[model_input_name].dims.at(0), - model_input_name, - model_kwargs: model_inputs, - decoder_start_token_id: generation_config.decoder_start_token_id, - bos_token_id: generation_config.bos_token_id, - generation_config - })); - } else { - input_ids = model_inputs[model_input_name]; - } - let input_ids_length = input_ids.dims.at(-1); - if (generation_config.max_new_tokens !== null) { - generation_config.max_length = input_ids_length + generation_config.max_new_tokens; - } - const prepared_logits_processor = this._get_logits_processor( - generation_config, - input_ids_length, - logits_processor - ); - const prepared_stopping_criteria = this._get_stopping_criteria( - generation_config, - stopping_criteria - ); - const numInputs = model_inputs[model_input_name].dims.at(0); - const sampler = _generation_logits_sampler_js__WEBPACK_IMPORTED_MODULE_12__.LogitsSampler.getSampler(generation_config); - const scores = new Array(numInputs).fill(0); - const all_input_ids = input_ids.tolist(); - if (streamer) { - streamer.put(all_input_ids); - } - let outputs; - let attentions = {}; - while (true) { - model_inputs = this.prepare_inputs_for_generation(all_input_ids, model_inputs, generation_config); - outputs = await this.forward(model_inputs); - if (generation_config.output_attentions && generation_config.return_dict_in_generate) { - const token_attentions = this.getAttentions(outputs); - for (const key in token_attentions) { - if (!(key in attentions)) { - attentions[key] = []; - } - attentions[key].push(token_attentions[key]); - } - } - const logits = outputs.logits.slice(null, -1, null); - const next_tokens_scores = prepared_logits_processor(all_input_ids, logits); - const generated_input_ids = []; - for (let batch_idx = 0; batch_idx < next_tokens_scores.dims.at(0); ++batch_idx) { - const logs = next_tokens_scores[batch_idx]; - const sampledTokens = await sampler(logs); - for (const [newTokenId, logProb] of sampledTokens) { - const bigint = BigInt(newTokenId); - scores[batch_idx] += logProb; - all_input_ids[batch_idx].push(bigint); - generated_input_ids.push([bigint]); - break; - } - } - if (streamer) { - streamer.put(generated_input_ids); - } - const stop = prepared_stopping_criteria(all_input_ids); - if (stop.every((x) => x)) { - break; - } - model_inputs = this._update_model_kwargs_for_generation({ - generated_input_ids, - outputs, - model_inputs, - is_encoder_decoder - }); - } - if (streamer) { - streamer.end(); - } - const past_key_values = this.getPastKeyValues(outputs, model_inputs.past_key_values, true); - const sequences = new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.Tensor("int64", all_input_ids.flat(), [all_input_ids.length, all_input_ids[0].length]); - if (generation_config.return_dict_in_generate) { - return { - sequences, - past_key_values, - ...attentions - // TODO: - // scores, - // logits, - }; - } else { - for (const tensor of Object.values(outputs)) { - if (tensor.location === "gpu-buffer") { - tensor.dispose(); - } - } - return sequences; - } - } - /** - * Returns an object containing past key values from the given decoder results object. - * - * @param {Object} decoderResults The decoder results object. - * @param {Object} pastKeyValues The previous past key values. - * @returns {Object} An object containing past key values. - */ - getPastKeyValues(decoderResults, pastKeyValues, disposeEncoderPKVs = false) { - const pkvs = /* @__PURE__ */ Object.create(null); - for (const name in decoderResults) { - if (name.startsWith("present")) { - const newName = name.replace("present", "past_key_values"); - const is_encoder_pkv = name.includes("encoder"); - if (is_encoder_pkv && pastKeyValues) { - pkvs[newName] = pastKeyValues[newName]; - } else { - pkvs[newName] = decoderResults[name]; - } - if (pastKeyValues && (!is_encoder_pkv || disposeEncoderPKVs)) { - const t = pastKeyValues[newName]; - if (t.location === "gpu-buffer") { - t.dispose(); - } - } - } - } - return pkvs; - } - /** - * Returns an object containing attentions from the given model output object. - * - * @param {Object} model_output The output of the model. - * @returns {{cross_attentions?: Tensor[]}} An object containing attentions. - */ - getAttentions(model_output) { - const attentions = {}; - for (const attnName of ["cross_attentions", "encoder_attentions", "decoder_attentions"]) { - for (const name in model_output) { - if (name.startsWith(attnName)) { - if (!(attnName in attentions)) { - attentions[attnName] = []; - } - attentions[attnName].push(model_output[name]); - } - } - } - return attentions; - } - /** - * Adds past key values to the decoder feeds object. If pastKeyValues is null, creates new tensors for past key values. - * - * @param {Object} decoderFeeds The decoder feeds object to add past key values to. - * @param {Object} pastKeyValues An object containing past key values. - */ - addPastKeyValues(decoderFeeds, pastKeyValues) { - var _a; - if (pastKeyValues) { - Object.assign(decoderFeeds, pastKeyValues); - } else { - const session = this.sessions["decoder_model_merged"] ?? this.sessions["model"]; - const dtype = ((_a = session == null ? void 0 : session.config) == null ? void 0 : _a.kv_cache_dtype) ?? "float32"; - const empty = dtype === "float16" ? new Uint16Array() : []; - const shapes = (0, _configs_js__WEBPACK_IMPORTED_MODULE_0__.getKeyValueShapes)(this.config); - for (const name in shapes) { - decoderFeeds[name] = new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.Tensor(dtype, empty, shapes[name]); - } - } - } - async encode_image({ pixel_values }) { - const features = (await sessionRun(this.sessions["vision_encoder"], { pixel_values })).image_features; - if (!this.config.num_image_tokens) { - console.warn( - `The number of image tokens was not set in the model configuration. Setting it to the number of features detected by the vision encoder (${features.dims[1]}).` - ); - this.config.num_image_tokens = features.dims[1]; - } - return features; - } - async encode_text({ input_ids }) { - return (await sessionRun(this.sessions["embed_tokens"], { input_ids })).inputs_embeds; - } - } - class ModelOutput { - } - class BaseModelOutput extends ModelOutput { - /** - * @param {Object} output The output of the model. - * @param {Tensor} output.last_hidden_state Sequence of hidden-states at the output of the last layer of the model. - * @param {Tensor} [output.hidden_states] Hidden-states of the model at the output of each layer plus the optional initial embedding outputs. - * @param {Tensor} [output.attentions] Attentions weights after the attention softmax, used to compute the weighted average in the self-attention heads. - */ - constructor({ last_hidden_state, hidden_states = null, attentions = null }) { - super(); - this.last_hidden_state = last_hidden_state; - this.hidden_states = hidden_states; - this.attentions = attentions; - } - } - class BertPreTrainedModel extends PreTrainedModel { - } - class BertModel extends BertPreTrainedModel { - } - class BertForMaskedLM extends BertPreTrainedModel { - /** - * Calls the model on new inputs. - * - * @param {Object} model_inputs The inputs to the model. - * @returns {Promise} An object containing the model's output logits for masked language modeling. - */ - async _call(model_inputs) { - return new MaskedLMOutput(await super._call(model_inputs)); - } - } - class BertForSequenceClassification extends BertPreTrainedModel { - /** - * Calls the model on new inputs. - * - * @param {Object} model_inputs The inputs to the model. - * @returns {Promise} An object containing the model's output logits for sequence classification. - */ - async _call(model_inputs) { - return new SequenceClassifierOutput(await super._call(model_inputs)); - } - } - class BertForTokenClassification extends BertPreTrainedModel { - /** - * Calls the model on new inputs. - * - * @param {Object} model_inputs The inputs to the model. - * @returns {Promise} An object containing the model's output logits for token classification. - */ - async _call(model_inputs) { - return new TokenClassifierOutput(await super._call(model_inputs)); - } - } - class BertForQuestionAnswering extends BertPreTrainedModel { - /** - * Calls the model on new inputs. - * - * @param {Object} model_inputs The inputs to the model. - * @returns {Promise} An object containing the model's output logits for question answering. - */ - async _call(model_inputs) { - return new QuestionAnsweringModelOutput(await super._call(model_inputs)); - } - } - class NomicBertPreTrainedModel extends PreTrainedModel { - } - class NomicBertModel extends NomicBertPreTrainedModel { - } - class RoFormerPreTrainedModel extends PreTrainedModel { - } - class RoFormerModel extends RoFormerPreTrainedModel { - } - class RoFormerForMaskedLM extends RoFormerPreTrainedModel { - /** - * Calls the model on new inputs. - * - * @param {Object} model_inputs The inputs to the model. - * @returns {Promise} An object containing the model's output logits for masked language modeling. - */ - async _call(model_inputs) { - return new MaskedLMOutput(await super._call(model_inputs)); - } - } - class RoFormerForSequenceClassification extends RoFormerPreTrainedModel { - /** - * Calls the model on new inputs. - * - * @param {Object} model_inputs The inputs to the model. - * @returns {Promise} An object containing the model's output logits for sequence classification. - */ - async _call(model_inputs) { - return new SequenceClassifierOutput(await super._call(model_inputs)); - } - } - class RoFormerForTokenClassification extends RoFormerPreTrainedModel { - /** - * Calls the model on new inputs. - * - * @param {Object} model_inputs The inputs to the model. - * @returns {Promise} An object containing the model's output logits for token classification. - */ - async _call(model_inputs) { - return new TokenClassifierOutput(await super._call(model_inputs)); - } - } - class RoFormerForQuestionAnswering extends RoFormerPreTrainedModel { - /** - * Calls the model on new inputs. - * - * @param {Object} model_inputs The inputs to the model. - * @returns {Promise} An object containing the model's output logits for question answering. - */ - async _call(model_inputs) { - return new QuestionAnsweringModelOutput(await super._call(model_inputs)); - } - } - class ConvBertPreTrainedModel extends PreTrainedModel { - } - class ConvBertModel extends ConvBertPreTrainedModel { - } - class ConvBertForMaskedLM extends ConvBertPreTrainedModel { - /** - * Calls the model on new inputs. - * - * @param {Object} model_inputs The inputs to the model. - * @returns {Promise} An object containing the model's output logits for masked language modeling. - */ - async _call(model_inputs) { - return new MaskedLMOutput(await super._call(model_inputs)); - } - } - class ConvBertForSequenceClassification extends ConvBertPreTrainedModel { - /** - * Calls the model on new inputs. - * - * @param {Object} model_inputs The inputs to the model. - * @returns {Promise} An object containing the model's output logits for sequence classification. - */ - async _call(model_inputs) { - return new SequenceClassifierOutput(await super._call(model_inputs)); - } - } - class ConvBertForTokenClassification extends ConvBertPreTrainedModel { - /** - * Calls the model on new inputs. - * - * @param {Object} model_inputs The inputs to the model. - * @returns {Promise} An object containing the model's output logits for token classification. - */ - async _call(model_inputs) { - return new TokenClassifierOutput(await super._call(model_inputs)); - } - } - class ConvBertForQuestionAnswering extends ConvBertPreTrainedModel { - /** - * Calls the model on new inputs. - * - * @param {Object} model_inputs The inputs to the model. - * @returns {Promise} An object containing the model's output logits for question answering. - */ - async _call(model_inputs) { - return new QuestionAnsweringModelOutput(await super._call(model_inputs)); - } - } - class ElectraPreTrainedModel extends PreTrainedModel { - } - class ElectraModel extends ElectraPreTrainedModel { - } - class ElectraForMaskedLM extends ElectraPreTrainedModel { - /** - * Calls the model on new inputs. - * - * @param {Object} model_inputs The inputs to the model. - * @returns {Promise} An object containing the model's output logits for masked language modeling. - */ - async _call(model_inputs) { - return new MaskedLMOutput(await super._call(model_inputs)); - } - } - class ElectraForSequenceClassification extends ElectraPreTrainedModel { - /** - * Calls the model on new inputs. - * - * @param {Object} model_inputs The inputs to the model. - * @returns {Promise} An object containing the model's output logits for sequence classification. - */ - async _call(model_inputs) { - return new SequenceClassifierOutput(await super._call(model_inputs)); - } - } - class ElectraForTokenClassification extends ElectraPreTrainedModel { - /** - * Calls the model on new inputs. - * - * @param {Object} model_inputs The inputs to the model. - * @returns {Promise} An object containing the model's output logits for token classification. - */ - async _call(model_inputs) { - return new TokenClassifierOutput(await super._call(model_inputs)); - } - } - class ElectraForQuestionAnswering extends ElectraPreTrainedModel { - /** - * Calls the model on new inputs. - * - * @param {Object} model_inputs The inputs to the model. - * @returns {Promise} An object containing the model's output logits for question answering. - */ - async _call(model_inputs) { - return new QuestionAnsweringModelOutput(await super._call(model_inputs)); - } - } - class CamembertPreTrainedModel extends PreTrainedModel { - } - class CamembertModel extends CamembertPreTrainedModel { - } - class CamembertForMaskedLM extends CamembertPreTrainedModel { - /** - * Calls the model on new inputs. - * - * @param {Object} model_inputs The inputs to the model. - * @returns {Promise} An object containing the model's output logits for masked language modeling. - */ - async _call(model_inputs) { - return new MaskedLMOutput(await super._call(model_inputs)); - } - } - class CamembertForSequenceClassification extends CamembertPreTrainedModel { - /** - * Calls the model on new inputs. - * - * @param {Object} model_inputs The inputs to the model. - * @returns {Promise} An object containing the model's output logits for sequence classification. - */ - async _call(model_inputs) { - return new SequenceClassifierOutput(await super._call(model_inputs)); - } - } - class CamembertForTokenClassification extends CamembertPreTrainedModel { - /** - * Calls the model on new inputs. - * - * @param {Object} model_inputs The inputs to the model. - * @returns {Promise} An object containing the model's output logits for token classification. - */ - async _call(model_inputs) { - return new TokenClassifierOutput(await super._call(model_inputs)); - } - } - class CamembertForQuestionAnswering extends CamembertPreTrainedModel { - /** - * Calls the model on new inputs. - * - * @param {Object} model_inputs The inputs to the model. - * @returns {Promise} An object containing the model's output logits for question answering. - */ - async _call(model_inputs) { - return new QuestionAnsweringModelOutput(await super._call(model_inputs)); - } - } - class DebertaPreTrainedModel extends PreTrainedModel { - } - class DebertaModel extends DebertaPreTrainedModel { - } - class DebertaForMaskedLM extends DebertaPreTrainedModel { - /** - * Calls the model on new inputs. - * - * @param {Object} model_inputs The inputs to the model. - * @returns {Promise} An object containing the model's output logits for masked language modeling. - */ - async _call(model_inputs) { - return new MaskedLMOutput(await super._call(model_inputs)); - } - } - class DebertaForSequenceClassification extends DebertaPreTrainedModel { - /** - * Calls the model on new inputs. - * - * @param {Object} model_inputs The inputs to the model. - * @returns {Promise} An object containing the model's output logits for sequence classification. - */ - async _call(model_inputs) { - return new SequenceClassifierOutput(await super._call(model_inputs)); - } - } - class DebertaForTokenClassification extends DebertaPreTrainedModel { - /** - * Calls the model on new inputs. - * - * @param {Object} model_inputs The inputs to the model. - * @returns {Promise} An object containing the model's output logits for token classification. - */ - async _call(model_inputs) { - return new TokenClassifierOutput(await super._call(model_inputs)); - } - } - class DebertaForQuestionAnswering extends DebertaPreTrainedModel { - /** - * Calls the model on new inputs. - * - * @param {Object} model_inputs The inputs to the model. - * @returns {Promise} An object containing the model's output logits for question answering. - */ - async _call(model_inputs) { - return new QuestionAnsweringModelOutput(await super._call(model_inputs)); - } - } - class DebertaV2PreTrainedModel extends PreTrainedModel { - } - class DebertaV2Model extends DebertaV2PreTrainedModel { - } - class DebertaV2ForMaskedLM extends DebertaV2PreTrainedModel { - /** - * Calls the model on new inputs. - * - * @param {Object} model_inputs The inputs to the model. - * @returns {Promise} An object containing the model's output logits for masked language modeling. - */ - async _call(model_inputs) { - return new MaskedLMOutput(await super._call(model_inputs)); - } - } - class DebertaV2ForSequenceClassification extends DebertaV2PreTrainedModel { - /** - * Calls the model on new inputs. - * - * @param {Object} model_inputs The inputs to the model. - * @returns {Promise} An object containing the model's output logits for sequence classification. - */ - async _call(model_inputs) { - return new SequenceClassifierOutput(await super._call(model_inputs)); - } - } - class DebertaV2ForTokenClassification extends DebertaV2PreTrainedModel { - /** - * Calls the model on new inputs. - * - * @param {Object} model_inputs The inputs to the model. - * @returns {Promise} An object containing the model's output logits for token classification. - */ - async _call(model_inputs) { - return new TokenClassifierOutput(await super._call(model_inputs)); - } - } - class DebertaV2ForQuestionAnswering extends DebertaV2PreTrainedModel { - /** - * Calls the model on new inputs. - * - * @param {Object} model_inputs The inputs to the model. - * @returns {Promise} An object containing the model's output logits for question answering. - */ - async _call(model_inputs) { - return new QuestionAnsweringModelOutput(await super._call(model_inputs)); - } - } - class DistilBertPreTrainedModel extends PreTrainedModel { - } - class DistilBertModel extends DistilBertPreTrainedModel { - } - class DistilBertForSequenceClassification extends DistilBertPreTrainedModel { - /** - * Calls the model on new inputs. - * - * @param {Object} model_inputs The inputs to the model. - * @returns {Promise} An object containing the model's output logits for sequence classification. - */ - async _call(model_inputs) { - return new SequenceClassifierOutput(await super._call(model_inputs)); - } - } - class DistilBertForTokenClassification extends DistilBertPreTrainedModel { - /** - * Calls the model on new inputs. - * - * @param {Object} model_inputs The inputs to the model. - * @returns {Promise} An object containing the model's output logits for token classification. - */ - async _call(model_inputs) { - return new TokenClassifierOutput(await super._call(model_inputs)); - } - } - class DistilBertForQuestionAnswering extends DistilBertPreTrainedModel { - /** - * Calls the model on new inputs. - * - * @param {Object} model_inputs The inputs to the model. - * @returns {Promise} An object containing the model's output logits for question answering. - */ - async _call(model_inputs) { - return new QuestionAnsweringModelOutput(await super._call(model_inputs)); - } - } - class DistilBertForMaskedLM extends DistilBertPreTrainedModel { - /** - * Calls the model on new inputs. - * - * @param {Object} model_inputs The inputs to the model. - * @returns {Promise} returned object - */ - async _call(model_inputs) { - return new MaskedLMOutput(await super._call(model_inputs)); - } - } - class EsmPreTrainedModel extends PreTrainedModel { - } - class EsmModel extends EsmPreTrainedModel { - } - class EsmForMaskedLM extends EsmPreTrainedModel { - /** - * Calls the model on new inputs. - * - * @param {Object} model_inputs The inputs to the model. - * @returns {Promise} An object containing the model's output logits for masked language modeling. - */ - async _call(model_inputs) { - return new MaskedLMOutput(await super._call(model_inputs)); - } - } - class EsmForSequenceClassification extends EsmPreTrainedModel { - /** - * Calls the model on new inputs. - * - * @param {Object} model_inputs The inputs to the model. - * @returns {Promise} An object containing the model's output logits for sequence classification. - */ - async _call(model_inputs) { - return new SequenceClassifierOutput(await super._call(model_inputs)); - } - } - class EsmForTokenClassification extends EsmPreTrainedModel { - /** - * Calls the model on new inputs. - * - * @param {Object} model_inputs The inputs to the model. - * @returns {Promise} An object containing the model's output logits for token classification. - */ - async _call(model_inputs) { - return new TokenClassifierOutput(await super._call(model_inputs)); - } - } - class MobileBertPreTrainedModel extends PreTrainedModel { - } - class MobileBertModel extends MobileBertPreTrainedModel { - } - class MobileBertForMaskedLM extends MobileBertPreTrainedModel { - /** - * Calls the model on new inputs. - * - * @param {Object} model_inputs The inputs to the model. - * @returns {Promise} returned object - */ - async _call(model_inputs) { - return new MaskedLMOutput(await super._call(model_inputs)); - } - } - class MobileBertForSequenceClassification extends MobileBertPreTrainedModel { - /** - * Calls the model on new inputs. - * - * @param {Object} model_inputs The inputs to the model. - * @returns {Promise} returned object - */ - async _call(model_inputs) { - return new SequenceClassifierOutput(await super._call(model_inputs)); - } - } - class MobileBertForQuestionAnswering extends MobileBertPreTrainedModel { - /** - * Calls the model on new inputs. - * - * @param {Object} model_inputs The inputs to the model. - * @returns {Promise} returned object - */ - async _call(model_inputs) { - return new QuestionAnsweringModelOutput(await super._call(model_inputs)); - } - } - class MPNetPreTrainedModel extends PreTrainedModel { - } - class MPNetModel extends MPNetPreTrainedModel { - } - class MPNetForMaskedLM extends MPNetPreTrainedModel { - /** - * Calls the model on new inputs. - * - * @param {Object} model_inputs The inputs to the model. - * @returns {Promise} An object containing the model's output logits for masked language modeling. - */ - async _call(model_inputs) { - return new MaskedLMOutput(await super._call(model_inputs)); - } - } - class MPNetForSequenceClassification extends MPNetPreTrainedModel { - /** - * Calls the model on new inputs. - * - * @param {Object} model_inputs The inputs to the model. - * @returns {Promise} An object containing the model's output logits for sequence classification. - */ - async _call(model_inputs) { - return new SequenceClassifierOutput(await super._call(model_inputs)); - } - } - class MPNetForTokenClassification extends MPNetPreTrainedModel { - /** - * Calls the model on new inputs. - * - * @param {Object} model_inputs The inputs to the model. - * @returns {Promise} An object containing the model's output logits for token classification. - */ - async _call(model_inputs) { - return new TokenClassifierOutput(await super._call(model_inputs)); - } - } - class MPNetForQuestionAnswering extends MPNetPreTrainedModel { - /** - * Calls the model on new inputs. - * - * @param {Object} model_inputs The inputs to the model. - * @returns {Promise} An object containing the model's output logits for question answering. - */ - async _call(model_inputs) { - return new QuestionAnsweringModelOutput(await super._call(model_inputs)); - } - } - class SqueezeBertPreTrainedModel extends PreTrainedModel { - } - class SqueezeBertModel extends SqueezeBertPreTrainedModel { - } - class SqueezeBertForMaskedLM extends SqueezeBertPreTrainedModel { - /** - * Calls the model on new inputs. - * - * @param {Object} model_inputs The inputs to the model. - * @returns {Promise} returned object - */ - async _call(model_inputs) { - return new MaskedLMOutput(await super._call(model_inputs)); - } - } - class SqueezeBertForSequenceClassification extends SqueezeBertPreTrainedModel { - /** - * Calls the model on new inputs. - * - * @param {Object} model_inputs The inputs to the model. - * @returns {Promise} returned object - */ - async _call(model_inputs) { - return new SequenceClassifierOutput(await super._call(model_inputs)); - } - } - class SqueezeBertForQuestionAnswering extends SqueezeBertPreTrainedModel { - /** - * Calls the model on new inputs. - * - * @param {Object} model_inputs The inputs to the model. - * @returns {Promise} returned object - */ - async _call(model_inputs) { - return new QuestionAnsweringModelOutput(await super._call(model_inputs)); - } - } - class AlbertPreTrainedModel extends PreTrainedModel { - } - class AlbertModel extends AlbertPreTrainedModel { - } - class AlbertForSequenceClassification extends AlbertPreTrainedModel { - /** - * Calls the model on new inputs. - * - * @param {Object} model_inputs The inputs to the model. - * @returns {Promise} returned object - */ - async _call(model_inputs) { - return new SequenceClassifierOutput(await super._call(model_inputs)); - } - } - class AlbertForQuestionAnswering extends AlbertPreTrainedModel { - /** - * Calls the model on new inputs. - * - * @param {Object} model_inputs The inputs to the model. - * @returns {Promise} returned object - */ - async _call(model_inputs) { - return new QuestionAnsweringModelOutput(await super._call(model_inputs)); - } - } - class AlbertForMaskedLM extends AlbertPreTrainedModel { - /** - * Calls the model on new inputs. - * - * @param {Object} model_inputs The inputs to the model. - * @returns {Promise} returned object - */ - async _call(model_inputs) { - return new MaskedLMOutput(await super._call(model_inputs)); - } - } - class T5PreTrainedModel extends PreTrainedModel { - constructor() { - super(...arguments); - __publicField(this, "forward_params", [ - "input_ids", - "attention_mask", - "encoder_outputs", - "decoder_input_ids", - "decoder_attention_mask", - "past_key_values" - ]); - } - } - ; - class T5Model extends T5PreTrainedModel { - } - class T5ForConditionalGeneration extends T5PreTrainedModel { - } - class LongT5PreTrainedModel extends PreTrainedModel { - } - ; - class LongT5Model extends LongT5PreTrainedModel { - } - class LongT5ForConditionalGeneration extends LongT5PreTrainedModel { - } - class MT5PreTrainedModel extends PreTrainedModel { - } - ; - class MT5Model extends MT5PreTrainedModel { - } - class MT5ForConditionalGeneration extends MT5PreTrainedModel { - } - class BartPretrainedModel extends PreTrainedModel { - } - ; - class BartModel extends BartPretrainedModel { - } - class BartForConditionalGeneration extends BartPretrainedModel { - } - class BartForSequenceClassification extends BartPretrainedModel { - /** - * Calls the model on new inputs. - * - * @param {Object} model_inputs The inputs to the model. - * @returns {Promise} An object containing the model's output logits for sequence classification. - */ - async _call(model_inputs) { - return new SequenceClassifierOutput(await super._call(model_inputs)); - } - } - class MBartPreTrainedModel extends PreTrainedModel { - } - ; - class MBartModel extends MBartPreTrainedModel { - } - class MBartForConditionalGeneration extends MBartPreTrainedModel { - } - class MBartForSequenceClassification extends MBartPreTrainedModel { - /** - * Calls the model on new inputs. - * - * @param {Object} model_inputs The inputs to the model. - * @returns {Promise} An object containing the model's output logits for sequence classification. - */ - async _call(model_inputs) { - return new SequenceClassifierOutput(await super._call(model_inputs)); - } - } - class MBartForCausalLM extends MBartPreTrainedModel { - } - class BlenderbotPreTrainedModel extends PreTrainedModel { - } - ; - class BlenderbotModel extends BlenderbotPreTrainedModel { - } - class BlenderbotForConditionalGeneration extends BlenderbotPreTrainedModel { - } - class BlenderbotSmallPreTrainedModel extends PreTrainedModel { - } - ; - class BlenderbotSmallModel extends BlenderbotSmallPreTrainedModel { - } - class BlenderbotSmallForConditionalGeneration extends BlenderbotSmallPreTrainedModel { - } - class RobertaPreTrainedModel extends PreTrainedModel { - } - class RobertaModel extends RobertaPreTrainedModel { - } - class RobertaForMaskedLM extends RobertaPreTrainedModel { - /** - * Calls the model on new inputs. - * - * @param {Object} model_inputs The inputs to the model. - * @returns {Promise} returned object - */ - async _call(model_inputs) { - return new MaskedLMOutput(await super._call(model_inputs)); - } - } - class RobertaForSequenceClassification extends RobertaPreTrainedModel { - /** - * Calls the model on new inputs. - * - * @param {Object} model_inputs The inputs to the model. - * @returns {Promise} returned object - */ - async _call(model_inputs) { - return new SequenceClassifierOutput(await super._call(model_inputs)); - } - } - class RobertaForTokenClassification extends RobertaPreTrainedModel { - /** - * Calls the model on new inputs. - * - * @param {Object} model_inputs The inputs to the model. - * @returns {Promise} An object containing the model's output logits for token classification. - */ - async _call(model_inputs) { - return new TokenClassifierOutput(await super._call(model_inputs)); - } - } - class RobertaForQuestionAnswering extends RobertaPreTrainedModel { - /** - * Calls the model on new inputs. - * - * @param {Object} model_inputs The inputs to the model. - * @returns {Promise} returned object - */ - async _call(model_inputs) { - return new QuestionAnsweringModelOutput(await super._call(model_inputs)); - } - } - class XLMPreTrainedModel extends PreTrainedModel { - } - class XLMModel extends XLMPreTrainedModel { - } - class XLMWithLMHeadModel extends XLMPreTrainedModel { - /** - * Calls the model on new inputs. - * - * @param {Object} model_inputs The inputs to the model. - * @returns {Promise} returned object - */ - async _call(model_inputs) { - return new MaskedLMOutput(await super._call(model_inputs)); - } - } - class XLMForSequenceClassification extends XLMPreTrainedModel { - /** - * Calls the model on new inputs. - * - * @param {Object} model_inputs The inputs to the model. - * @returns {Promise} returned object - */ - async _call(model_inputs) { - return new SequenceClassifierOutput(await super._call(model_inputs)); - } - } - class XLMForTokenClassification extends XLMPreTrainedModel { - /** - * Calls the model on new inputs. - * - * @param {Object} model_inputs The inputs to the model. - * @returns {Promise} An object containing the model's output logits for token classification. - */ - async _call(model_inputs) { - return new TokenClassifierOutput(await super._call(model_inputs)); - } - } - class XLMForQuestionAnswering extends XLMPreTrainedModel { - /** - * Calls the model on new inputs. - * - * @param {Object} model_inputs The inputs to the model. - * @returns {Promise} returned object - */ - async _call(model_inputs) { - return new QuestionAnsweringModelOutput(await super._call(model_inputs)); - } - } - class XLMRobertaPreTrainedModel extends PreTrainedModel { - } - class XLMRobertaModel extends XLMRobertaPreTrainedModel { - } - class XLMRobertaForMaskedLM extends XLMRobertaPreTrainedModel { - /** - * Calls the model on new inputs. - * - * @param {Object} model_inputs The inputs to the model. - * @returns {Promise} returned object - */ - async _call(model_inputs) { - return new MaskedLMOutput(await super._call(model_inputs)); - } - } - class XLMRobertaForSequenceClassification extends XLMRobertaPreTrainedModel { - /** - * Calls the model on new inputs. - * - * @param {Object} model_inputs The inputs to the model. - * @returns {Promise} returned object - */ - async _call(model_inputs) { - return new SequenceClassifierOutput(await super._call(model_inputs)); - } - } - class XLMRobertaForTokenClassification extends XLMRobertaPreTrainedModel { - /** - * Calls the model on new inputs. - * - * @param {Object} model_inputs The inputs to the model. - * @returns {Promise} An object containing the model's output logits for token classification. - */ - async _call(model_inputs) { - return new TokenClassifierOutput(await super._call(model_inputs)); - } - } - class XLMRobertaForQuestionAnswering extends XLMRobertaPreTrainedModel { - /** - * Calls the model on new inputs. - * - * @param {Object} model_inputs The inputs to the model. - * @returns {Promise} returned object - */ - async _call(model_inputs) { - return new QuestionAnsweringModelOutput(await super._call(model_inputs)); - } - } - class ASTPreTrainedModel extends PreTrainedModel { - } - ; - class ASTModel extends ASTPreTrainedModel { - } - class ASTForAudioClassification extends ASTPreTrainedModel { - } - class WhisperPreTrainedModel extends PreTrainedModel { - constructor() { - super(...arguments); - __publicField(this, "requires_attention_mask", false); - __publicField(this, "main_input_name", "input_features"); - __publicField(this, "forward_params", [ - "input_features", - "attention_mask", - "decoder_input_ids", - "decoder_attention_mask", - "past_key_values" - ]); - } - } - ; - class WhisperModel extends WhisperPreTrainedModel { - } - class WhisperForConditionalGeneration extends WhisperPreTrainedModel { - _prepare_generation_config(generation_config, kwargs) { - return ( - /** @type {WhisperGenerationConfig} */ - super._prepare_generation_config(generation_config, kwargs, _models_whisper_generation_whisper_js__WEBPACK_IMPORTED_MODULE_14__.WhisperGenerationConfig) - ); - } - /** - * - * @param {WhisperGenerationConfig} generation_config - */ - _retrieve_init_tokens(generation_config) { - const init_tokens = [generation_config.decoder_start_token_id]; - let language = generation_config.language; - const task = generation_config.task; - if (generation_config.is_multilingual) { - if (!language) { - console.warn("No language specified - defaulting to English (en)."); - language = "en"; - } - const language_code = (0, _models_whisper_common_whisper_js__WEBPACK_IMPORTED_MODULE_15__.whisper_language_to_code)(language); - const language_token = `<|${language_code}|>`; - init_tokens.push(generation_config.lang_to_id[language_token]); - init_tokens.push(generation_config.task_to_id[task ?? "transcribe"]); - } else if (language || task) { - throw new Error( - "Cannot specify `task` or `language` for an English-only model. If the model is intended to be multilingual, pass `is_multilingual=true` to generate, or update the generation config." - ); - } - if (!generation_config.return_timestamps && generation_config.no_timestamps_token_id && init_tokens.at(-1) !== generation_config.no_timestamps_token_id) { - init_tokens.push(generation_config.no_timestamps_token_id); - } else if (generation_config.return_timestamps && init_tokens.at(-1) === generation_config.no_timestamps_token_id) { - console.warn("<|notimestamps|> prompt token is removed from generation_config since `return_timestamps` is set to `true`."); - init_tokens.pop(); - } - return init_tokens.filter((token) => token != null); - } - /** - * Transcribes or translates log-mel input features to a sequence of auto-regressively generated token ids. - * @param {import('./models/whisper/generation_whisper.js').WhisperGenerationFunctionParameters} options - * @returns {Promise} The output of the model, which can contain the generated token ids, attentions, and scores. - */ - async generate({ - inputs = null, - generation_config = null, - logits_processor = null, - stopping_criteria = null, - // Whisper-specific options (passed to kwargs) - // prompt_ids = null, - // language = null, - // task = null, - ...kwargs - }) { - generation_config = this._prepare_generation_config(generation_config, kwargs); - const init_tokens = kwargs.decoder_input_ids ?? this._retrieve_init_tokens(generation_config); - if (generation_config.return_timestamps) { - logits_processor ?? (logits_processor = new _generation_logits_process_js__WEBPACK_IMPORTED_MODULE_7__.LogitsProcessorList()); - logits_processor.push( - new _generation_logits_process_js__WEBPACK_IMPORTED_MODULE_7__.WhisperTimeStampLogitsProcessor(generation_config, init_tokens) - ); - } - if (generation_config.begin_suppress_tokens) { - logits_processor ?? (logits_processor = new _generation_logits_process_js__WEBPACK_IMPORTED_MODULE_7__.LogitsProcessorList()); - logits_processor.push( - new _generation_logits_process_js__WEBPACK_IMPORTED_MODULE_7__.SuppressTokensAtBeginLogitsProcessor(generation_config.begin_suppress_tokens, init_tokens.length) - ); - } - if (generation_config.return_token_timestamps) { - if (!generation_config.alignment_heads) { - throw new Error( - "Model generation config has no `alignment_heads`, token-level timestamps not available. See https://gist.github.com/hollance/42e32852f24243b748ae6bc1f985b13a on how to add this property to the generation config." - ); - } - if (generation_config.task === "translate") { - console.warn("Token-level timestamps may not be reliable for task 'translate'."); - } - generation_config.output_attentions = true; - generation_config.return_dict_in_generate = true; - } - const outputs = await super.generate({ - inputs, - generation_config, - logits_processor, - decoder_input_ids: init_tokens, - ...kwargs - }); - if (generation_config.return_token_timestamps) { - outputs["token_timestamps"] = this._extract_token_timestamps( - outputs, - generation_config.alignment_heads, - generation_config.num_frames - ); - } - return outputs; - } - /** - * Calculates token-level timestamps using the encoder-decoder cross-attentions and - * dynamic time-warping (DTW) to map each output token to a position in the input audio. - * If `num_frames` is specified, the encoder-decoder cross-attentions will be cropped before applying DTW. - * @param {Object} generate_outputs Outputs generated by the model - * @param {Tensor[][]} generate_outputs.cross_attentions The cross attentions output by the model - * @param {Tensor} generate_outputs.sequences The sequences output by the model - * @param {number[][]} alignment_heads Alignment heads of the model - * @param {number} [num_frames=null] Number of frames in the input audio. - * @param {number} [time_precision=0.02] Precision of the timestamps in seconds - * @returns {Tensor} tensor containing the timestamps in seconds for each predicted token - */ - _extract_token_timestamps(generate_outputs, alignment_heads, num_frames = null, time_precision = 0.02) { - if (!generate_outputs.cross_attentions) { - throw new Error( - "Model outputs must contain cross attentions to extract timestamps. This is most likely because the model was not exported with `output_attentions=True`." - ); - } - if (num_frames == null) { - console.warn( - "`num_frames` has not been set, meaning the entire audio will be analyzed. This may lead to inaccurate token-level timestamps for short audios (< 30 seconds)." - ); - } - let median_filter_width = this.config.median_filter_width; - if (median_filter_width === void 0) { - console.warn("Model config has no `median_filter_width`, using default value of 7."); - median_filter_width = 7; - } - const batch = generate_outputs.cross_attentions; - const cross_attentions = Array.from( - { length: this.config.decoder_layers }, - // Concatenate the cross attentions for each layer across sequence length dimension. - (_, i) => (0, _utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.cat)(batch.map((x) => x[i]), 2) - ); - const weights = (0, _utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.stack)(alignment_heads.map(([l, h]) => { - if (l >= cross_attentions.length) { - throw new Error(`Layer index ${l} is out of bounds for cross attentions (length ${cross_attentions.length}).`); - } - return num_frames ? cross_attentions[l].slice(null, h, null, [0, num_frames]) : cross_attentions[l].slice(null, h); - })).transpose(1, 0, 2, 3); - const [std, calculatedMean] = (0, _utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.std_mean)(weights, -2, 0, true); - const smoothedWeights = weights.clone(); - for (let a = 0; a < smoothedWeights.dims[0]; ++a) { - const aTensor = smoothedWeights[a]; - for (let b = 0; b < aTensor.dims[0]; ++b) { - const bTensor = aTensor[b]; - const stdTensorData = std[a][b][0].data; - const meanTensorData = calculatedMean[a][b][0].data; - for (let c = 0; c < bTensor.dims[0]; ++c) { - let cTensorData = bTensor[c].data; - for (let d = 0; d < cTensorData.length; ++d) { - cTensorData[d] = (cTensorData[d] - meanTensorData[d]) / stdTensorData[d]; - } - cTensorData.set((0, _utils_maths_js__WEBPACK_IMPORTED_MODULE_10__.medianFilter)(cTensorData, median_filter_width)); - } - } - } - const batchedMatrices = [(0, _utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.mean)(smoothedWeights, 1)]; - const timestampsShape = generate_outputs.sequences.dims; - const timestamps = new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.Tensor( - "float32", - new Float32Array(timestampsShape[0] * timestampsShape[1]), - timestampsShape - ); - for (let batch_idx = 0; batch_idx < timestampsShape[0]; ++batch_idx) { - const matrix = batchedMatrices[batch_idx].neg().squeeze_(0); - const [text_indices, time_indices] = (0, _utils_maths_js__WEBPACK_IMPORTED_MODULE_10__.dynamic_time_warping)(matrix.tolist()); - const diffs = Array.from({ length: text_indices.length - 1 }, (v, i) => text_indices[i + 1] - text_indices[i]); - const jumps = (0, _utils_core_js__WEBPACK_IMPORTED_MODULE_4__.mergeArrays)([1], diffs).map((x) => !!x); - const jump_times = []; - for (let i = 0; i < jumps.length; ++i) { - if (jumps[i]) { - jump_times.push(time_indices[i] * time_precision); - } - } - timestamps[batch_idx].data.set(jump_times, 1); - } - return timestamps; - } - } - class VisionEncoderDecoderModel extends PreTrainedModel { - constructor() { - super(...arguments); - __publicField(this, "main_input_name", "pixel_values"); - __publicField(this, "forward_params", [ - "pixel_values", - "input_ids", - "encoder_hidden_states", - "past_key_values" - ]); - } - } - class LlavaPreTrainedModel extends PreTrainedModel { - constructor() { - super(...arguments); - __publicField(this, "forward_params", [ - "input_ids", - "pixel_values", - "attention_mask", - "position_ids", - "past_key_values" - ]); - } - } - class LlavaForConditionalGeneration extends LlavaPreTrainedModel { - _merge_input_ids_with_image_features({ - inputs_embeds, - image_features, - input_ids, - attention_mask - }) { - const image_token_index = this.config.image_token_index; - const idsList = input_ids.tolist(); - const indexOfImage = idsList.map((x) => x.findIndex((x2) => x2 == image_token_index)); - const noImages = indexOfImage.every((x) => x === -1); - const allImages = indexOfImage.every((x) => x !== -1); - if (!noImages && !allImages) { - throw new Error("Every input should contain either 0 or 1 image token."); - } - if (noImages) { - return { - inputs_embeds, - attention_mask - }; - } - const stacked = []; - const stacked_attention_mask = []; - for (let i = 0; i < indexOfImage.length; ++i) { - const index = indexOfImage[i]; - const e = inputs_embeds[i]; - const im = image_features[i]; - const am = attention_mask[i]; - stacked.push( - (0, _utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.cat)([ - e.slice([0, index]), - im, - e.slice([index + 1, e.dims[0]]) - ], 0) - ); - stacked_attention_mask.push( - (0, _utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.cat)([ - am.slice([0, index]), - (0, _utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.ones)([im.dims[0]]), - am.slice([index + 1, am.dims[0]]) - ], 0) - ); - } - return { - inputs_embeds: (0, _utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.stack)(stacked, 0), - attention_mask: (0, _utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.stack)(stacked_attention_mask, 0) - }; - } - } - class Moondream1ForConditionalGeneration extends LlavaForConditionalGeneration { - } - class Florence2PreTrainedModel extends PreTrainedModel { - constructor() { - super(...arguments); - __publicField(this, "forward_params", [ - // Encoder inputs - "input_ids", - "inputs_embeds", - "attention_mask", - "pixel_values", - // Decoder inputs - "encoder_outputs", - "decoder_input_ids", - "decoder_inputs_embeds", - "decoder_attention_mask", - "past_key_values" - ]); - __publicField(this, "main_input_name", "inputs_embeds"); - } - } - class Florence2ForConditionalGeneration extends Florence2PreTrainedModel { - _merge_input_ids_with_image_features({ - inputs_embeds, - image_features, - input_ids, - attention_mask - }) { - return { - inputs_embeds: (0, _utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.cat)([ - image_features, - // image embeds - inputs_embeds - // task prefix embeds - ], 1), - attention_mask: (0, _utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.cat)([ - (0, _utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.ones)(image_features.dims.slice(0, 2)), - // image attention mask - attention_mask - // task prefix attention mask - ], 1) - }; - } - async _prepare_inputs_embeds({ input_ids, pixel_values, inputs_embeds, attention_mask }) { - if (!input_ids && !pixel_values) { - throw new Error("Either `input_ids` or `pixel_values` should be provided."); - } - let text_features, image_features; - if (input_ids) { - text_features = await this.encode_text({ input_ids }); - } - if (pixel_values) { - image_features = await this.encode_image({ pixel_values }); - } - if (text_features && image_features) { - ({ inputs_embeds, attention_mask } = this._merge_input_ids_with_image_features({ - inputs_embeds: text_features, - image_features, - input_ids, - attention_mask - })); - } else { - inputs_embeds = text_features || image_features; - } - return { inputs_embeds, attention_mask }; - } - async forward({ - input_ids, - pixel_values, - attention_mask, - decoder_input_ids, - decoder_attention_mask, - encoder_outputs, - past_key_values, - inputs_embeds, - decoder_inputs_embeds - }) { - if (!inputs_embeds) { - ({ inputs_embeds, attention_mask } = await this._prepare_inputs_embeds({ input_ids, pixel_values, inputs_embeds, attention_mask })); - } - if (!encoder_outputs) { - let { last_hidden_state } = await encoderForward(this, { inputs_embeds, attention_mask }); - encoder_outputs = last_hidden_state; - } - if (!decoder_inputs_embeds) { - if (!decoder_input_ids) { - throw new Error("Either `decoder_input_ids` or `decoder_inputs_embeds` should be provided."); - } - decoder_inputs_embeds = await this.encode_text({ input_ids: decoder_input_ids }); - } - const decoderFeeds = { - inputs_embeds: decoder_inputs_embeds, - attention_mask: decoder_attention_mask, - encoder_attention_mask: attention_mask, - encoder_hidden_states: encoder_outputs, - past_key_values - }; - const decoder_outputs = await decoderForward(this, decoderFeeds, true); - return decoder_outputs; - } - } - class CLIPPreTrainedModel extends PreTrainedModel { - } - class CLIPModel extends CLIPPreTrainedModel { - } - class CLIPTextModel extends CLIPPreTrainedModel { - /** @type {PreTrainedModel.from_pretrained} */ - static async from_pretrained(pretrained_model_name_or_path, options = {}) { - options.model_file_name ?? (options.model_file_name = "text_model"); - return super.from_pretrained(pretrained_model_name_or_path, options); - } - } - class CLIPTextModelWithProjection extends CLIPPreTrainedModel { - /** @type {PreTrainedModel.from_pretrained} */ - static async from_pretrained(pretrained_model_name_or_path, options = {}) { - options.model_file_name ?? (options.model_file_name = "text_model"); - return super.from_pretrained(pretrained_model_name_or_path, options); - } - } - class CLIPVisionModel extends CLIPPreTrainedModel { - /** @type {PreTrainedModel.from_pretrained} */ - static async from_pretrained(pretrained_model_name_or_path, options = {}) { - options.model_file_name ?? (options.model_file_name = "vision_model"); - return super.from_pretrained(pretrained_model_name_or_path, options); - } - } - class CLIPVisionModelWithProjection extends CLIPPreTrainedModel { - /** @type {PreTrainedModel.from_pretrained} */ - static async from_pretrained(pretrained_model_name_or_path, options = {}) { - options.model_file_name ?? (options.model_file_name = "vision_model"); - return super.from_pretrained(pretrained_model_name_or_path, options); - } - } - class SiglipPreTrainedModel extends PreTrainedModel { - } - class SiglipModel extends SiglipPreTrainedModel { - } - class SiglipTextModel extends SiglipPreTrainedModel { - /** @type {PreTrainedModel.from_pretrained} */ - static async from_pretrained(pretrained_model_name_or_path, options = {}) { - options.model_file_name ?? (options.model_file_name = "text_model"); - return super.from_pretrained(pretrained_model_name_or_path, options); - } - } - class SiglipVisionModel extends CLIPPreTrainedModel { - /** @type {PreTrainedModel.from_pretrained} */ - static async from_pretrained(pretrained_model_name_or_path, options = {}) { - options.model_file_name ?? (options.model_file_name = "vision_model"); - return super.from_pretrained(pretrained_model_name_or_path, options); - } - } - class ChineseCLIPPreTrainedModel extends PreTrainedModel { - } - class ChineseCLIPModel extends ChineseCLIPPreTrainedModel { - } - class CLIPSegPreTrainedModel extends PreTrainedModel { - } - class CLIPSegModel extends CLIPSegPreTrainedModel { - } - class CLIPSegForImageSegmentation extends CLIPSegPreTrainedModel { - } - class GPT2PreTrainedModel extends PreTrainedModel { - } - class GPT2Model extends GPT2PreTrainedModel { - } - class GPT2LMHeadModel extends GPT2PreTrainedModel { - } - class JAISPreTrainedModel extends PreTrainedModel { - } - class JAISModel extends JAISPreTrainedModel { - } - class JAISLMHeadModel extends JAISPreTrainedModel { - } - class GPTNeoPreTrainedModel extends PreTrainedModel { - } - class GPTNeoModel extends GPTNeoPreTrainedModel { - } - class GPTNeoForCausalLM extends GPTNeoPreTrainedModel { - } - class GPTNeoXPreTrainedModel extends PreTrainedModel { - } - class GPTNeoXModel extends GPTNeoXPreTrainedModel { - } - class GPTNeoXForCausalLM extends GPTNeoXPreTrainedModel { - } - class GPTJPreTrainedModel extends PreTrainedModel { - } - class GPTJModel extends GPTJPreTrainedModel { - } - class GPTJForCausalLM extends GPTJPreTrainedModel { - } - class GPTBigCodePreTrainedModel extends PreTrainedModel { - } - class GPTBigCodeModel extends GPTBigCodePreTrainedModel { - } - class GPTBigCodeForCausalLM extends GPTBigCodePreTrainedModel { - } - class CodeGenPreTrainedModel extends PreTrainedModel { - } - class CodeGenModel extends CodeGenPreTrainedModel { - } - class CodeGenForCausalLM extends CodeGenPreTrainedModel { - } - class LlamaPreTrainedModel extends PreTrainedModel { - } - class LlamaModel extends LlamaPreTrainedModel { - } - class LlamaForCausalLM extends LlamaPreTrainedModel { - } - class GranitePreTrainedModel extends PreTrainedModel { - } - class GraniteModel extends GranitePreTrainedModel { - } - class GraniteForCausalLM extends GranitePreTrainedModel { - } - class CoherePreTrainedModel extends PreTrainedModel { - } - class CohereModel extends CoherePreTrainedModel { - } - class CohereForCausalLM extends CoherePreTrainedModel { - } - class GemmaPreTrainedModel extends PreTrainedModel { - } - class GemmaModel extends GemmaPreTrainedModel { - } - class GemmaForCausalLM extends GemmaPreTrainedModel { - } - class Gemma2PreTrainedModel extends PreTrainedModel { - } - class Gemma2Model extends Gemma2PreTrainedModel { - } - class Gemma2ForCausalLM extends Gemma2PreTrainedModel { - } - class OpenELMPreTrainedModel extends PreTrainedModel { - } - class OpenELMModel extends OpenELMPreTrainedModel { - } - class OpenELMForCausalLM extends OpenELMPreTrainedModel { - } - class Qwen2PreTrainedModel extends PreTrainedModel { - } - class Qwen2Model extends Qwen2PreTrainedModel { - } - class Qwen2ForCausalLM extends Qwen2PreTrainedModel { - } - class PhiPreTrainedModel extends PreTrainedModel { - } - class PhiModel extends PhiPreTrainedModel { - } - class PhiForCausalLM extends PhiPreTrainedModel { - } - class Phi3PreTrainedModel extends PreTrainedModel { - } - class Phi3Model extends Phi3PreTrainedModel { - } - class Phi3ForCausalLM extends Phi3PreTrainedModel { - } - class BloomPreTrainedModel extends PreTrainedModel { - } - class BloomModel extends BloomPreTrainedModel { - } - class BloomForCausalLM extends BloomPreTrainedModel { - } - class MptPreTrainedModel extends PreTrainedModel { - } - class MptModel extends MptPreTrainedModel { - } - class MptForCausalLM extends MptPreTrainedModel { - } - class OPTPreTrainedModel extends PreTrainedModel { - } - class OPTModel extends OPTPreTrainedModel { - } - class OPTForCausalLM extends OPTPreTrainedModel { - } - class ViTPreTrainedModel extends PreTrainedModel { - } - class ViTModel extends ViTPreTrainedModel { - } - class ViTForImageClassification extends ViTPreTrainedModel { - /** - * @param {any} model_inputs - */ - async _call(model_inputs) { - return new SequenceClassifierOutput(await super._call(model_inputs)); - } - } - class PvtPreTrainedModel extends PreTrainedModel { - } - class PvtModel extends PvtPreTrainedModel { - } - class PvtForImageClassification extends PvtPreTrainedModel { - /** - * @param {any} model_inputs - */ - async _call(model_inputs) { - return new SequenceClassifierOutput(await super._call(model_inputs)); - } - } - class ViTMAEPreTrainedModel extends PreTrainedModel { - } - class ViTMAEModel extends ViTMAEPreTrainedModel { - } - class ViTMSNPreTrainedModel extends PreTrainedModel { - } - class ViTMSNModel extends ViTMSNPreTrainedModel { - } - class ViTMSNForImageClassification extends ViTMSNPreTrainedModel { - /** - * @param {any} model_inputs - */ - async _call(model_inputs) { - return new SequenceClassifierOutput(await super._call(model_inputs)); - } - } - class GroupViTPreTrainedModel extends PreTrainedModel { - } - class GroupViTModel extends GroupViTPreTrainedModel { - } - class FastViTPreTrainedModel extends PreTrainedModel { - } - class FastViTModel extends FastViTPreTrainedModel { - } - class FastViTForImageClassification extends FastViTPreTrainedModel { - /** - * @param {any} model_inputs - */ - async _call(model_inputs) { - return new SequenceClassifierOutput(await super._call(model_inputs)); - } - } - class VitMattePreTrainedModel extends PreTrainedModel { - } - class VitMatteForImageMatting extends VitMattePreTrainedModel { - /** - * @param {any} model_inputs - */ - async _call(model_inputs) { - return new ImageMattingOutput(await super._call(model_inputs)); - } - } - class MobileViTPreTrainedModel extends PreTrainedModel { - } - class MobileViTModel extends MobileViTPreTrainedModel { - } - class MobileViTForImageClassification extends MobileViTPreTrainedModel { - /** - * @param {any} model_inputs - */ - async _call(model_inputs) { - return new SequenceClassifierOutput(await super._call(model_inputs)); - } - } - class MobileViTV2PreTrainedModel extends PreTrainedModel { - } - class MobileViTV2Model extends MobileViTV2PreTrainedModel { - } - class MobileViTV2ForImageClassification extends MobileViTV2PreTrainedModel { - /** - * @param {any} model_inputs - */ - async _call(model_inputs) { - return new SequenceClassifierOutput(await super._call(model_inputs)); - } - } - class OwlViTPreTrainedModel extends PreTrainedModel { - } - class OwlViTModel extends OwlViTPreTrainedModel { - } - class OwlViTForObjectDetection extends OwlViTPreTrainedModel { - } - class Owlv2PreTrainedModel extends PreTrainedModel { - } - class Owlv2Model extends Owlv2PreTrainedModel { - } - class Owlv2ForObjectDetection extends Owlv2PreTrainedModel { - } - class BeitPreTrainedModel extends PreTrainedModel { - } - class BeitModel extends BeitPreTrainedModel { - } - class BeitForImageClassification extends BeitPreTrainedModel { - /** - * @param {any} model_inputs - */ - async _call(model_inputs) { - return new SequenceClassifierOutput(await super._call(model_inputs)); - } - } - class DetrPreTrainedModel extends PreTrainedModel { - } - class DetrModel extends DetrPreTrainedModel { - } - class DetrForObjectDetection extends DetrPreTrainedModel { - /** - * @param {any} model_inputs - */ - async _call(model_inputs) { - return new DetrObjectDetectionOutput(await super._call(model_inputs)); - } - } - class DetrForSegmentation extends DetrPreTrainedModel { - /** - * Runs the model with the provided inputs - * @param {Object} model_inputs Model inputs - * @returns {Promise} Object containing segmentation outputs - */ - async _call(model_inputs) { - return new DetrSegmentationOutput(await super._call(model_inputs)); - } - } - class DetrObjectDetectionOutput extends ModelOutput { - /** - * @param {Object} output The output of the model. - * @param {Tensor} output.logits Classification logits (including no-object) for all queries. - * @param {Tensor} output.pred_boxes Normalized boxes coordinates for all queries, represented as (center_x, center_y, width, height). - * These values are normalized in [0, 1], relative to the size of each individual image in the batch (disregarding possible padding). - */ - constructor({ logits, pred_boxes }) { - super(); - this.logits = logits; - this.pred_boxes = pred_boxes; - } - } - class DetrSegmentationOutput extends ModelOutput { - /** - * @param {Object} output The output of the model. - * @param {Tensor} output.logits The output logits of the model. - * @param {Tensor} output.pred_boxes Predicted boxes. - * @param {Tensor} output.pred_masks Predicted masks. - */ - constructor({ logits, pred_boxes, pred_masks }) { - super(); - this.logits = logits; - this.pred_boxes = pred_boxes; - this.pred_masks = pred_masks; - } - } - class RTDetrPreTrainedModel extends PreTrainedModel { - } - class RTDetrModel extends RTDetrPreTrainedModel { - } - class RTDetrForObjectDetection extends RTDetrPreTrainedModel { - /** - * @param {any} model_inputs - */ - async _call(model_inputs) { - return new RTDetrObjectDetectionOutput(await super._call(model_inputs)); - } - } - class RTDetrObjectDetectionOutput extends ModelOutput { - /** - * @param {Object} output The output of the model. - * @param {Tensor} output.logits Classification logits (including no-object) for all queries. - * @param {Tensor} output.pred_boxes Normalized boxes coordinates for all queries, represented as (center_x, center_y, width, height). - * These values are normalized in [0, 1], relative to the size of each individual image in the batch (disregarding possible padding). - */ - constructor({ logits, pred_boxes }) { - super(); - this.logits = logits; - this.pred_boxes = pred_boxes; - } - } - class TableTransformerPreTrainedModel extends PreTrainedModel { - } - class TableTransformerModel extends TableTransformerPreTrainedModel { - } - class TableTransformerForObjectDetection extends TableTransformerPreTrainedModel { - /** - * @param {any} model_inputs - */ - async _call(model_inputs) { - return new TableTransformerObjectDetectionOutput(await super._call(model_inputs)); - } - } - class TableTransformerObjectDetectionOutput extends DetrObjectDetectionOutput { - } - class DeiTPreTrainedModel extends PreTrainedModel { - } - class DeiTModel extends DeiTPreTrainedModel { - } - class DeiTForImageClassification extends DeiTPreTrainedModel { - /** - * @param {any} model_inputs - */ - async _call(model_inputs) { - return new SequenceClassifierOutput(await super._call(model_inputs)); - } - } - class HieraPreTrainedModel extends PreTrainedModel { - } - class HieraModel extends HieraPreTrainedModel { - } - class HieraForImageClassification extends HieraPreTrainedModel { - /** - * @param {any} model_inputs - */ - async _call(model_inputs) { - return new SequenceClassifierOutput(await super._call(model_inputs)); - } - } - class ResNetPreTrainedModel extends PreTrainedModel { - } - class ResNetModel extends ResNetPreTrainedModel { - } - class ResNetForImageClassification extends ResNetPreTrainedModel { - /** - * @param {any} model_inputs - */ - async _call(model_inputs) { - return new SequenceClassifierOutput(await super._call(model_inputs)); - } - } - class SwinPreTrainedModel extends PreTrainedModel { - } - class SwinModel extends SwinPreTrainedModel { - } - class SwinForImageClassification extends SwinPreTrainedModel { - /** - * @param {any} model_inputs - */ - async _call(model_inputs) { - return new SequenceClassifierOutput(await super._call(model_inputs)); - } - } - class Swin2SRPreTrainedModel extends PreTrainedModel { - } - class Swin2SRModel extends Swin2SRPreTrainedModel { - } - class Swin2SRForImageSuperResolution extends Swin2SRPreTrainedModel { - } - class DPTPreTrainedModel extends PreTrainedModel { - } - class DPTModel extends DPTPreTrainedModel { - } - class DPTForDepthEstimation extends DPTPreTrainedModel { - } - class DepthAnythingPreTrainedModel extends PreTrainedModel { - } - class DepthAnythingForDepthEstimation extends DepthAnythingPreTrainedModel { - } - class SapiensPreTrainedModel extends PreTrainedModel { - } - class SapiensForSemanticSegmentation extends SapiensPreTrainedModel { - } - class SapiensForDepthEstimation extends SapiensPreTrainedModel { - } - class SapiensForNormalEstimation extends SapiensPreTrainedModel { - } - class DepthProPreTrainedModel extends PreTrainedModel { - } - class DepthProForDepthEstimation extends DepthProPreTrainedModel { - } - class MaskFormerPreTrainedModel extends PreTrainedModel { - } - class MaskFormerModel extends MaskFormerPreTrainedModel { - } - class MaskFormerForInstanceSegmentation extends MaskFormerPreTrainedModel { - } - class GLPNPreTrainedModel extends PreTrainedModel { - } - class GLPNModel extends GLPNPreTrainedModel { - } - class GLPNForDepthEstimation extends GLPNPreTrainedModel { - } - class DonutSwinPreTrainedModel extends PreTrainedModel { - } - class DonutSwinModel extends DonutSwinPreTrainedModel { - } - class ConvNextPreTrainedModel extends PreTrainedModel { - } - class ConvNextModel extends ConvNextPreTrainedModel { - } - class ConvNextForImageClassification extends ConvNextPreTrainedModel { - /** - * @param {any} model_inputs - */ - async _call(model_inputs) { - return new SequenceClassifierOutput(await super._call(model_inputs)); - } - } - class ConvNextV2PreTrainedModel extends PreTrainedModel { - } - class ConvNextV2Model extends ConvNextV2PreTrainedModel { - } - class ConvNextV2ForImageClassification extends ConvNextV2PreTrainedModel { - /** - * @param {any} model_inputs - */ - async _call(model_inputs) { - return new SequenceClassifierOutput(await super._call(model_inputs)); - } - } - class Dinov2PreTrainedModel extends PreTrainedModel { - } - class Dinov2Model extends Dinov2PreTrainedModel { - } - class Dinov2ForImageClassification extends Dinov2PreTrainedModel { - /** - * @param {any} model_inputs - */ - async _call(model_inputs) { - return new SequenceClassifierOutput(await super._call(model_inputs)); - } - } - class YolosPreTrainedModel extends PreTrainedModel { - } - class YolosModel extends YolosPreTrainedModel { - } - class YolosForObjectDetection extends YolosPreTrainedModel { - /** - * @param {any} model_inputs - */ - async _call(model_inputs) { - return new YolosObjectDetectionOutput(await super._call(model_inputs)); - } - } - class YolosObjectDetectionOutput extends ModelOutput { - /** - * @param {Object} output The output of the model. - * @param {Tensor} output.logits Classification logits (including no-object) for all queries. - * @param {Tensor} output.pred_boxes Normalized boxes coordinates for all queries, represented as (center_x, center_y, width, height). - * These values are normalized in [0, 1], relative to the size of each individual image in the batch (disregarding possible padding). - */ - constructor({ logits, pred_boxes }) { - super(); - this.logits = logits; - this.pred_boxes = pred_boxes; - } - } - class SamPreTrainedModel extends PreTrainedModel { - } - class SamModel extends SamPreTrainedModel { - /** - * Compute image embeddings and positional image embeddings, given the pixel values of an image. - * @param {Object} model_inputs Object containing the model inputs. - * @param {Tensor} model_inputs.pixel_values Pixel values obtained using a `SamProcessor`. - * @returns {Promise<{ image_embeddings: Tensor, image_positional_embeddings: Tensor }>} The image embeddings and positional image embeddings. - */ - async get_image_embeddings({ pixel_values }) { - return await encoderForward(this, { pixel_values }); - } - /** - * @typedef {Object} SamModelInputs Object containing the model inputs. - * @property {Tensor} pixel_values Pixel values as a Tensor with shape `(batch_size, num_channels, height, width)`. - * These can be obtained using a `SamProcessor`. - * @property {Tensor} [input_points] Input 2D spatial points with shape `(batch_size, num_points, 2)`. - * This is used by the prompt encoder to encode the prompt. - * @property {Tensor} [input_labels] Input labels for the points, as a Tensor of shape `(batch_size, point_batch_size, num_points)`. - * This is used by the prompt encoder to encode the prompt. There are 4 types of labels: - * - `1`: the point is a point that contains the object of interest - * - `0`: the point is a point that does not contain the object of interest - * - `-1`: the point corresponds to the background - * - `-10`: the point is a padding point, thus should be ignored by the prompt encoder - * @property {Tensor} [input_boxes] Input bounding boxes with shape `(batch_size, num_boxes, 4)`. - * @property {Tensor} [image_embeddings] Image embeddings used by the mask decoder. - * @property {Tensor} [image_positional_embeddings] Image positional embeddings used by the mask decoder. - */ - /** - * @param {SamModelInputs} model_inputs Object containing the model inputs. - * @returns {Promise} The output of the model. - */ - async forward(model_inputs) { - if (!model_inputs.image_embeddings || !model_inputs.image_positional_embeddings) { - model_inputs = { - ...model_inputs, - ...await this.get_image_embeddings(model_inputs) - }; - } - if (!model_inputs.input_labels && model_inputs.input_points) { - const shape = model_inputs.input_points.dims.slice(0, -1); - const numElements = shape.reduce((a, b) => a * b, 1); - model_inputs.input_labels = new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.Tensor( - "int64", - new BigInt64Array(numElements).fill(1n), - shape - ); - } - const decoder_inputs = { - image_embeddings: model_inputs.image_embeddings, - image_positional_embeddings: model_inputs.image_positional_embeddings - }; - if (model_inputs.input_points) { - decoder_inputs.input_points = model_inputs.input_points; - } - if (model_inputs.input_labels) { - decoder_inputs.input_labels = model_inputs.input_labels; - } - if (model_inputs.input_boxes) { - decoder_inputs.input_boxes = model_inputs.input_boxes; - } - return await sessionRun(this.sessions["prompt_encoder_mask_decoder"], decoder_inputs); - } - /** - * Runs the model with the provided inputs - * @param {Object} model_inputs Model inputs - * @returns {Promise} Object containing segmentation outputs - */ - async _call(model_inputs) { - return new SamImageSegmentationOutput(await super._call(model_inputs)); - } - } - class SamImageSegmentationOutput extends ModelOutput { - /** - * @param {Object} output The output of the model. - * @param {Tensor} output.iou_scores The output logits of the model. - * @param {Tensor} output.pred_masks Predicted boxes. - */ - constructor({ iou_scores, pred_masks }) { - super(); - this.iou_scores = iou_scores; - this.pred_masks = pred_masks; - } - } - class MarianPreTrainedModel extends PreTrainedModel { - } - ; - class MarianModel extends MarianPreTrainedModel { - } - class MarianMTModel extends MarianPreTrainedModel { - } - class M2M100PreTrainedModel extends PreTrainedModel { - } - ; - class M2M100Model extends M2M100PreTrainedModel { - } - class M2M100ForConditionalGeneration extends M2M100PreTrainedModel { - } - class Wav2Vec2PreTrainedModel extends PreTrainedModel { - } - ; - class Wav2Vec2Model extends Wav2Vec2PreTrainedModel { - } - class Wav2Vec2ForCTC extends Wav2Vec2PreTrainedModel { - /** - * @param {Object} model_inputs - * @param {Tensor} model_inputs.input_values Float values of input raw speech waveform. - * @param {Tensor} model_inputs.attention_mask Mask to avoid performing convolution and attention on padding token indices. Mask values selected in [0, 1] - */ - async _call(model_inputs) { - return new CausalLMOutput(await super._call(model_inputs)); - } - } - class Wav2Vec2ForSequenceClassification extends Wav2Vec2PreTrainedModel { - /** - * Calls the model on new inputs. - * @param {Object} model_inputs The inputs to the model. - * @returns {Promise} An object containing the model's output logits for sequence classification. - */ - async _call(model_inputs) { - return new SequenceClassifierOutput(await super._call(model_inputs)); - } - } - class Wav2Vec2ForAudioFrameClassification extends Wav2Vec2PreTrainedModel { - /** - * Calls the model on new inputs. - * @param {Object} model_inputs The inputs to the model. - * @returns {Promise} An object containing the model's output logits for sequence classification. - */ - async _call(model_inputs) { - return new TokenClassifierOutput(await super._call(model_inputs)); - } - } - class PyAnnotePreTrainedModel extends PreTrainedModel { - } - ; - class PyAnnoteModel extends PyAnnotePreTrainedModel { - } - class PyAnnoteForAudioFrameClassification extends PyAnnotePreTrainedModel { - /** - * Calls the model on new inputs. - * @param {Object} model_inputs The inputs to the model. - * @returns {Promise} An object containing the model's output logits for sequence classification. - */ - async _call(model_inputs) { - return new TokenClassifierOutput(await super._call(model_inputs)); - } - } - class WeSpeakerResNetPreTrainedModel extends PreTrainedModel { - } - ; - class WeSpeakerResNetModel extends WeSpeakerResNetPreTrainedModel { - } - class UniSpeechPreTrainedModel extends PreTrainedModel { - } - ; - class UniSpeechModel extends UniSpeechPreTrainedModel { - } - class UniSpeechForCTC extends UniSpeechPreTrainedModel { - /** - * @param {Object} model_inputs - * @param {Tensor} model_inputs.input_values Float values of input raw speech waveform. - * @param {Tensor} model_inputs.attention_mask Mask to avoid performing convolution and attention on padding token indices. Mask values selected in [0, 1] - */ - async _call(model_inputs) { - return new CausalLMOutput(await super._call(model_inputs)); - } - } - class UniSpeechForSequenceClassification extends UniSpeechPreTrainedModel { - /** - * Calls the model on new inputs. - * @param {Object} model_inputs The inputs to the model. - * @returns {Promise} An object containing the model's output logits for sequence classification. - */ - async _call(model_inputs) { - return new SequenceClassifierOutput(await super._call(model_inputs)); - } - } - class UniSpeechSatPreTrainedModel extends PreTrainedModel { - } - ; - class UniSpeechSatModel extends UniSpeechSatPreTrainedModel { - } - class UniSpeechSatForCTC extends UniSpeechSatPreTrainedModel { - /** - * @param {Object} model_inputs - * @param {Tensor} model_inputs.input_values Float values of input raw speech waveform. - * @param {Tensor} model_inputs.attention_mask Mask to avoid performing convolution and attention on padding token indices. Mask values selected in [0, 1] - */ - async _call(model_inputs) { - return new CausalLMOutput(await super._call(model_inputs)); - } - } - class UniSpeechSatForSequenceClassification extends UniSpeechSatPreTrainedModel { - /** - * Calls the model on new inputs. - * @param {Object} model_inputs The inputs to the model. - * @returns {Promise} An object containing the model's output logits for sequence classification. - */ - async _call(model_inputs) { - return new SequenceClassifierOutput(await super._call(model_inputs)); - } - } - class UniSpeechSatForAudioFrameClassification extends UniSpeechSatPreTrainedModel { - /** - * Calls the model on new inputs. - * @param {Object} model_inputs The inputs to the model. - * @returns {Promise} An object containing the model's output logits for sequence classification. - */ - async _call(model_inputs) { - return new TokenClassifierOutput(await super._call(model_inputs)); - } - } - class Wav2Vec2BertPreTrainedModel extends PreTrainedModel { - } - ; - class Wav2Vec2BertModel extends Wav2Vec2BertPreTrainedModel { - } - class Wav2Vec2BertForCTC extends Wav2Vec2BertPreTrainedModel { - /** - * @param {Object} model_inputs - * @param {Tensor} model_inputs.input_features Float values of input mel-spectrogram. - * @param {Tensor} model_inputs.attention_mask Mask to avoid performing convolution and attention on padding token indices. Mask values selected in [0, 1] - */ - async _call(model_inputs) { - return new CausalLMOutput(await super._call(model_inputs)); - } - } - class Wav2Vec2BertForSequenceClassification extends Wav2Vec2BertPreTrainedModel { - /** - * Calls the model on new inputs. - * @param {Object} model_inputs The inputs to the model. - * @returns {Promise} An object containing the model's output logits for sequence classification. - */ - async _call(model_inputs) { - return new SequenceClassifierOutput(await super._call(model_inputs)); - } - } - class HubertPreTrainedModel extends PreTrainedModel { - } - class HubertModel extends Wav2Vec2PreTrainedModel { - } - class HubertForCTC extends Wav2Vec2PreTrainedModel { - /** - * @param {Object} model_inputs - * @param {Tensor} model_inputs.input_values Float values of input raw speech waveform. - * @param {Tensor} model_inputs.attention_mask Mask to avoid performing convolution and attention on padding token indices. Mask values selected in [0, 1] - */ - async _call(model_inputs) { - return new CausalLMOutput(await super._call(model_inputs)); - } - } - class HubertForSequenceClassification extends Wav2Vec2PreTrainedModel { - /** - * Calls the model on new inputs. - * @param {Object} model_inputs The inputs to the model. - * @returns {Promise} An object containing the model's output logits for sequence classification. - */ - async _call(model_inputs) { - return new SequenceClassifierOutput(await super._call(model_inputs)); - } - } - class WavLMPreTrainedModel extends PreTrainedModel { - } - ; - class WavLMModel extends WavLMPreTrainedModel { - } - class WavLMForCTC extends WavLMPreTrainedModel { - /** - * @param {Object} model_inputs - * @param {Tensor} model_inputs.input_values Float values of input raw speech waveform. - * @param {Tensor} model_inputs.attention_mask Mask to avoid performing convolution and attention on padding token indices. Mask values selected in [0, 1] - */ - async _call(model_inputs) { - return new CausalLMOutput(await super._call(model_inputs)); - } - } - class WavLMForSequenceClassification extends WavLMPreTrainedModel { - /** - * Calls the model on new inputs. - * @param {Object} model_inputs The inputs to the model. - * @returns {Promise} An object containing the model's output logits for sequence classification. - */ - async _call(model_inputs) { - return new SequenceClassifierOutput(await super._call(model_inputs)); - } - } - class WavLMForXVector extends WavLMPreTrainedModel { - /** - * Calls the model on new inputs. - * @param {Object} model_inputs The inputs to the model. - * @returns {Promise} An object containing the model's output logits and speaker embeddings. - */ - async _call(model_inputs) { - return new XVectorOutput(await super._call(model_inputs)); - } - } - class WavLMForAudioFrameClassification extends WavLMPreTrainedModel { - /** - * Calls the model on new inputs. - * @param {Object} model_inputs The inputs to the model. - * @returns {Promise} An object containing the model's output logits for sequence classification. - */ - async _call(model_inputs) { - return new TokenClassifierOutput(await super._call(model_inputs)); - } - } - class SpeechT5PreTrainedModel extends PreTrainedModel { - } - ; - class SpeechT5Model extends SpeechT5PreTrainedModel { - } - ; - class SpeechT5ForSpeechToText extends SpeechT5PreTrainedModel { - } - class SpeechT5ForTextToSpeech extends SpeechT5PreTrainedModel { - /** - * @typedef {Object} SpeechOutput - * @property {Tensor} [spectrogram] The predicted log-mel spectrogram of shape - * `(output_sequence_length, config.num_mel_bins)`. Returned when no `vocoder` is provided - * @property {Tensor} [waveform] The predicted waveform of shape `(num_frames,)`. Returned when a `vocoder` is provided. - * @property {Tensor} [cross_attentions] The outputs of the decoder's cross-attention layers of shape - * `(config.decoder_layers, config.decoder_attention_heads, output_sequence_length, input_sequence_length)`. returned when `output_cross_attentions` is `true`. - */ - /** - * Converts a sequence of input tokens into a sequence of mel spectrograms, which are subsequently turned into a speech waveform using a vocoder. - * @param {Tensor} input_values Indices of input sequence tokens in the vocabulary. - * @param {Tensor} speaker_embeddings Tensor containing the speaker embeddings. - * @param {Object} options Optional parameters for generating speech. - * @param {number} [options.threshold=0.5] The generated sequence ends when the predicted stop token probability exceeds this value. - * @param {number} [options.minlenratio=0.0] Used to calculate the minimum required length for the output sequence. - * @param {number} [options.maxlenratio=20.0] Used to calculate the maximum allowed length for the output sequence. - * @param {Object} [options.vocoder=null] The vocoder that converts the mel spectrogram into a speech waveform. If `null`, the output is the mel spectrogram. - * @param {boolean} [options.output_cross_attentions=false] Whether or not to return the attentions tensors of the decoder's cross-attention layers. - * @returns {Promise} A promise which resolves to an object containing the spectrogram, waveform, and cross-attention tensors. - */ - async generate_speech(input_values, speaker_embeddings, { - threshold = 0.5, - minlenratio = 0, - maxlenratio = 20, - vocoder = null - // output_cross_attentions = false, // TODO add - } = {}) { - const model_inputs = { - input_ids: input_values - }; - const { encoder_outputs, encoder_attention_mask } = await encoderForward(this, model_inputs); - const r = encoder_outputs.dims[1] / this.config.reduction_factor; - const maxlen = Math.floor(r * maxlenratio); - const minlen = Math.floor(r * minlenratio); - const num_mel_bins = this.config.num_mel_bins; - let spectrogramParts = []; - let past_key_values = null; - let decoder_outputs = null; - let idx = 0; - while (true) { - ++idx; - const use_cache_branch = boolTensor(!!decoder_outputs); - let output_sequence; - if (decoder_outputs) { - output_sequence = decoder_outputs.output_sequence_out; - } else { - output_sequence = new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.Tensor( - "float32", - new Float32Array(num_mel_bins), - [1, 1, num_mel_bins] - ); - } - let decoderFeeds = { - use_cache_branch, - output_sequence, - encoder_attention_mask, - speaker_embeddings, - encoder_hidden_states: encoder_outputs - }; - this.addPastKeyValues(decoderFeeds, past_key_values); - decoder_outputs = await sessionRun(this.sessions["decoder_model_merged"], decoderFeeds); - past_key_values = this.getPastKeyValues(decoder_outputs, past_key_values); - const { prob, spectrum } = decoder_outputs; - spectrogramParts.push(spectrum); - if (idx >= minlen && // Finished when stop token or maximum length is reached. - (Array.from(prob.data).filter((p) => p >= threshold).length > 0 || idx >= maxlen)) { - break; - } - } - const spectrogram = (0, _utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.cat)(spectrogramParts); - const { waveform } = await sessionRun(vocoder.sessions["model"], { spectrogram }); - return { - spectrogram, - waveform - // cross_attentions: null, // TODO add - }; - } - } - class SpeechT5HifiGan extends PreTrainedModel { - constructor() { - super(...arguments); - __publicField(this, "main_input_name", "spectrogram"); - } - } - class TrOCRPreTrainedModel extends PreTrainedModel { - } - class TrOCRForCausalLM extends TrOCRPreTrainedModel { - } - class MistralPreTrainedModel extends PreTrainedModel { - } - class MistralModel extends MistralPreTrainedModel { - } - class MistralForCausalLM extends MistralPreTrainedModel { - } - class Starcoder2PreTrainedModel extends PreTrainedModel { - } - class Starcoder2Model extends Starcoder2PreTrainedModel { - } - class Starcoder2ForCausalLM extends Starcoder2PreTrainedModel { - } - class FalconPreTrainedModel extends PreTrainedModel { - } - class FalconModel extends FalconPreTrainedModel { - } - class FalconForCausalLM extends FalconPreTrainedModel { - } - class ClapPreTrainedModel extends PreTrainedModel { - } - class ClapModel extends ClapPreTrainedModel { - } - class ClapTextModelWithProjection extends ClapPreTrainedModel { - /** @type {PreTrainedModel.from_pretrained} */ - static async from_pretrained(pretrained_model_name_or_path, options = {}) { - options.model_file_name ?? (options.model_file_name = "text_model"); - return super.from_pretrained(pretrained_model_name_or_path, options); - } - } - class ClapAudioModelWithProjection extends ClapPreTrainedModel { - /** @type {PreTrainedModel.from_pretrained} */ - static async from_pretrained(pretrained_model_name_or_path, options = {}) { - options.model_file_name ?? (options.model_file_name = "audio_model"); - return super.from_pretrained(pretrained_model_name_or_path, options); - } - } - class VitsPreTrainedModel extends PreTrainedModel { - } - class VitsModel extends VitsPreTrainedModel { - /** - * Calls the model on new inputs. - * @param {Object} model_inputs The inputs to the model. - * @returns {Promise} The outputs for the VITS model. - */ - async _call(model_inputs) { - return new VitsModelOutput(await super._call(model_inputs)); - } - } - class SegformerPreTrainedModel extends PreTrainedModel { - } - class SegformerModel extends SegformerPreTrainedModel { - } - class SegformerForImageClassification extends SegformerPreTrainedModel { - } - class SegformerForSemanticSegmentation extends SegformerPreTrainedModel { - } - class StableLmPreTrainedModel extends PreTrainedModel { - } - class StableLmModel extends StableLmPreTrainedModel { - } - class StableLmForCausalLM extends StableLmPreTrainedModel { - } - class EfficientNetPreTrainedModel extends PreTrainedModel { - } - class EfficientNetModel extends EfficientNetPreTrainedModel { - } - class EfficientNetForImageClassification extends EfficientNetPreTrainedModel { - /** - * @param {any} model_inputs - */ - async _call(model_inputs) { - return new SequenceClassifierOutput(await super._call(model_inputs)); - } - } - class MusicgenPreTrainedModel extends PreTrainedModel { - } - class MusicgenModel extends MusicgenPreTrainedModel { - } - class MusicgenForCausalLM extends MusicgenPreTrainedModel { - } - class MusicgenForConditionalGeneration extends PreTrainedModel { - constructor() { - super(...arguments); - // NOTE: not MusicgenPreTrainedModel - __publicField(this, "forward_params", [ - "input_ids", - "attention_mask", - "encoder_outputs", - "decoder_input_ids", - "decoder_attention_mask", - "past_key_values" - ]); - } - /** - * Apply the pattern mask to the final ids, - * then revert the pattern delay mask by filtering the pad token id in a single step. - * @param {Tensor} outputs The output tensor from the model. - * @returns {Tensor} The filtered output tensor. - */ - _apply_and_filter_by_delay_pattern_mask(outputs) { - const [bs_x_codebooks, seqLength] = outputs.dims; - const num_codebooks = this.config.decoder.num_codebooks; - const upperBound = seqLength - num_codebooks; - let newDataSize = 0; - for (let i = 0; i < outputs.size; ++i) { - if (outputs.data[i] === this.config.decoder.pad_token_id) { - continue; - } - const row = i % seqLength; - const col = Math.floor(i / seqLength) % num_codebooks; - const diff = row - col; - if (diff > 0 && diff <= upperBound) { - outputs.data[newDataSize++] = outputs.data[i]; - } - } - const batch_size = Math.floor(bs_x_codebooks / num_codebooks); - const inferred = newDataSize / (batch_size * num_codebooks); - return new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.Tensor( - outputs.type, - outputs.data.slice(0, newDataSize), - [batch_size, num_codebooks, inferred] - ); - } - prepare_inputs_for_generation(input_ids, model_inputs, generation_config) { - let clonedInputIds = structuredClone(input_ids); - for (let i = 0; i < clonedInputIds.length; ++i) { - for (let j = 0; j < clonedInputIds[i].length; ++j) { - if (i % this.config.decoder.num_codebooks >= j) { - clonedInputIds[i][j] = BigInt(this.config.decoder.pad_token_id); - } - } - } - if (generation_config.guidance_scale !== null && generation_config.guidance_scale > 1) { - clonedInputIds = clonedInputIds.concat(clonedInputIds); - } - const prepped = super.prepare_inputs_for_generation(clonedInputIds, model_inputs, generation_config); - return prepped; - } - /** - * Generates sequences of token ids for models with a language modeling head. - * @param {import('./generation/parameters.js').GenerationFunctionParameters} options - * @returns {Promise} The output of the model, which can contain the generated token ids, attentions, and scores. - */ - async generate(options) { - const output_ids = await super.generate(options); - const audio_codes = this._apply_and_filter_by_delay_pattern_mask( - /** @type {Tensor} */ - output_ids - ).unsqueeze_(0); - const { audio_values } = await sessionRun(this.sessions["encodec_decode"], { audio_codes }); - return audio_values; - } - } - class MobileNetV1PreTrainedModel extends PreTrainedModel { - } - class MobileNetV1Model extends MobileNetV1PreTrainedModel { - } - class MobileNetV1ForImageClassification extends MobileNetV1PreTrainedModel { - /** - * @param {any} model_inputs - */ - async _call(model_inputs) { - return new SequenceClassifierOutput(await super._call(model_inputs)); - } - } - class MobileNetV2PreTrainedModel extends PreTrainedModel { - } - class MobileNetV2Model extends MobileNetV2PreTrainedModel { - } - class MobileNetV2ForImageClassification extends MobileNetV2PreTrainedModel { - /** - * @param {any} model_inputs - */ - async _call(model_inputs) { - return new SequenceClassifierOutput(await super._call(model_inputs)); - } - } - class MobileNetV3PreTrainedModel extends PreTrainedModel { - } - class MobileNetV3Model extends MobileNetV3PreTrainedModel { - } - class MobileNetV3ForImageClassification extends MobileNetV3PreTrainedModel { - /** - * @param {any} model_inputs - */ - async _call(model_inputs) { - return new SequenceClassifierOutput(await super._call(model_inputs)); - } - } - class MobileNetV4PreTrainedModel extends PreTrainedModel { - } - class MobileNetV4Model extends MobileNetV4PreTrainedModel { - } - class MobileNetV4ForImageClassification extends MobileNetV4PreTrainedModel { - /** - * @param {any} model_inputs - */ - async _call(model_inputs) { - return new SequenceClassifierOutput(await super._call(model_inputs)); - } - } - class DecisionTransformerPreTrainedModel extends PreTrainedModel { - } - class DecisionTransformerModel extends DecisionTransformerPreTrainedModel { - } - class PretrainedMixin { - /** @type {typeof PreTrainedModel.from_pretrained} */ - static async from_pretrained(pretrained_model_name_or_path, { - progress_callback = null, - config = null, - cache_dir = null, - local_files_only = false, - revision = "main", - model_file_name = null, - subfolder = "onnx", - device = null, - dtype = null, - use_external_data_format = null, - session_options = {} - } = {}) { - const options = { - progress_callback, - config, - cache_dir, - local_files_only, - revision, - model_file_name, - subfolder, - device, - dtype, - use_external_data_format, - session_options - }; - options.config = await _configs_js__WEBPACK_IMPORTED_MODULE_0__.AutoConfig.from_pretrained(pretrained_model_name_or_path, options); - if (!this.MODEL_CLASS_MAPPINGS) { - throw new Error("`MODEL_CLASS_MAPPINGS` not implemented for this type of `AutoClass`: " + this.name); - } - for (const MODEL_CLASS_MAPPING of this.MODEL_CLASS_MAPPINGS) { - const modelInfo = MODEL_CLASS_MAPPING.get(options.config.model_type); - if (!modelInfo) { - continue; - } - return await modelInfo[1].from_pretrained(pretrained_model_name_or_path, options); - } - if (this.BASE_IF_FAIL) { - console.warn(`Unknown model class "${options.config.model_type}", attempting to construct from base class.`); - return await PreTrainedModel.from_pretrained(pretrained_model_name_or_path, options); - } else { - throw Error(`Unsupported model type: ${options.config.model_type}`); - } - } - } - /** - * Mapping from model type to model class. - * @type {Map[]} - */ - __publicField(PretrainedMixin, "MODEL_CLASS_MAPPINGS", null); - /** - * Whether to attempt to instantiate the base class (`PretrainedModel`) if - * the model type is not found in the mapping. - */ - __publicField(PretrainedMixin, "BASE_IF_FAIL", false); - const MODEL_MAPPING_NAMES_ENCODER_ONLY = /* @__PURE__ */ new Map([ - ["bert", ["BertModel", BertModel]], - ["nomic_bert", ["NomicBertModel", NomicBertModel]], - ["roformer", ["RoFormerModel", RoFormerModel]], - ["electra", ["ElectraModel", ElectraModel]], - ["esm", ["EsmModel", EsmModel]], - ["convbert", ["ConvBertModel", ConvBertModel]], - ["camembert", ["CamembertModel", CamembertModel]], - ["deberta", ["DebertaModel", DebertaModel]], - ["deberta-v2", ["DebertaV2Model", DebertaV2Model]], - ["mpnet", ["MPNetModel", MPNetModel]], - ["albert", ["AlbertModel", AlbertModel]], - ["distilbert", ["DistilBertModel", DistilBertModel]], - ["roberta", ["RobertaModel", RobertaModel]], - ["xlm", ["XLMModel", XLMModel]], - ["xlm-roberta", ["XLMRobertaModel", XLMRobertaModel]], - ["clap", ["ClapModel", ClapModel]], - ["clip", ["CLIPModel", CLIPModel]], - ["clipseg", ["CLIPSegModel", CLIPSegModel]], - ["chinese_clip", ["ChineseCLIPModel", ChineseCLIPModel]], - ["siglip", ["SiglipModel", SiglipModel]], - ["mobilebert", ["MobileBertModel", MobileBertModel]], - ["squeezebert", ["SqueezeBertModel", SqueezeBertModel]], - ["wav2vec2", ["Wav2Vec2Model", Wav2Vec2Model]], - ["wav2vec2-bert", ["Wav2Vec2BertModel", Wav2Vec2BertModel]], - ["unispeech", ["UniSpeechModel", UniSpeechModel]], - ["unispeech-sat", ["UniSpeechSatModel", UniSpeechSatModel]], - ["hubert", ["HubertModel", HubertModel]], - ["wavlm", ["WavLMModel", WavLMModel]], - ["audio-spectrogram-transformer", ["ASTModel", ASTModel]], - ["vits", ["VitsModel", VitsModel]], - ["pyannote", ["PyAnnoteModel", PyAnnoteModel]], - ["wespeaker-resnet", ["WeSpeakerResNetModel", WeSpeakerResNetModel]], - ["detr", ["DetrModel", DetrModel]], - ["rt_detr", ["RTDetrModel", RTDetrModel]], - ["table-transformer", ["TableTransformerModel", TableTransformerModel]], - ["vit", ["ViTModel", ViTModel]], - ["pvt", ["PvtModel", PvtModel]], - ["vit_msn", ["ViTMSNModel", ViTMSNModel]], - ["vit_mae", ["ViTMAEModel", ViTMAEModel]], - ["groupvit", ["GroupViTModel", GroupViTModel]], - ["fastvit", ["FastViTModel", FastViTModel]], - ["mobilevit", ["MobileViTModel", MobileViTModel]], - ["mobilevitv2", ["MobileViTV2Model", MobileViTV2Model]], - ["owlvit", ["OwlViTModel", OwlViTModel]], - ["owlv2", ["Owlv2Model", Owlv2Model]], - ["beit", ["BeitModel", BeitModel]], - ["deit", ["DeiTModel", DeiTModel]], - ["hiera", ["HieraModel", HieraModel]], - ["convnext", ["ConvNextModel", ConvNextModel]], - ["convnextv2", ["ConvNextV2Model", ConvNextV2Model]], - ["dinov2", ["Dinov2Model", Dinov2Model]], - ["resnet", ["ResNetModel", ResNetModel]], - ["swin", ["SwinModel", SwinModel]], - ["swin2sr", ["Swin2SRModel", Swin2SRModel]], - ["donut-swin", ["DonutSwinModel", DonutSwinModel]], - ["yolos", ["YolosModel", YolosModel]], - ["dpt", ["DPTModel", DPTModel]], - ["glpn", ["GLPNModel", GLPNModel]], - ["hifigan", ["SpeechT5HifiGan", SpeechT5HifiGan]], - ["efficientnet", ["EfficientNetModel", EfficientNetModel]], - ["decision_transformer", ["DecisionTransformerModel", DecisionTransformerModel]], - ["mobilenet_v1", ["MobileNetV1Model", MobileNetV1Model]], - ["mobilenet_v2", ["MobileNetV2Model", MobileNetV2Model]], - ["mobilenet_v3", ["MobileNetV3Model", MobileNetV3Model]], - ["mobilenet_v4", ["MobileNetV4Model", MobileNetV4Model]], - ["maskformer", ["MaskFormerModel", MaskFormerModel]] - ]); - const MODEL_MAPPING_NAMES_ENCODER_DECODER = /* @__PURE__ */ new Map([ - ["t5", ["T5Model", T5Model]], - ["longt5", ["LongT5Model", LongT5Model]], - ["mt5", ["MT5Model", MT5Model]], - ["bart", ["BartModel", BartModel]], - ["mbart", ["MBartModel", MBartModel]], - ["marian", ["MarianModel", MarianModel]], - ["whisper", ["WhisperModel", WhisperModel]], - ["m2m_100", ["M2M100Model", M2M100Model]], - ["blenderbot", ["BlenderbotModel", BlenderbotModel]], - ["blenderbot-small", ["BlenderbotSmallModel", BlenderbotSmallModel]] - ]); - const MODEL_MAPPING_NAMES_DECODER_ONLY = /* @__PURE__ */ new Map([ - ["bloom", ["BloomModel", BloomModel]], - ["jais", ["JAISModel", JAISModel]], - ["gpt2", ["GPT2Model", GPT2Model]], - ["gptj", ["GPTJModel", GPTJModel]], - ["gpt_bigcode", ["GPTBigCodeModel", GPTBigCodeModel]], - ["gpt_neo", ["GPTNeoModel", GPTNeoModel]], - ["gpt_neox", ["GPTNeoXModel", GPTNeoXModel]], - ["codegen", ["CodeGenModel", CodeGenModel]], - ["llama", ["LlamaModel", LlamaModel]], - ["granite", ["GraniteModel", GraniteModel]], - ["cohere", ["CohereModel", CohereModel]], - ["gemma", ["GemmaModel", GemmaModel]], - ["gemma2", ["Gemma2Model", Gemma2Model]], - ["openelm", ["OpenELMModel", OpenELMModel]], - ["qwen2", ["Qwen2Model", Qwen2Model]], - ["phi", ["PhiModel", PhiModel]], - ["phi3", ["Phi3Model", Phi3Model]], - ["mpt", ["MptModel", MptModel]], - ["opt", ["OPTModel", OPTModel]], - ["mistral", ["MistralModel", MistralModel]], - ["starcoder2", ["Starcoder2Model", Starcoder2Model]], - ["falcon", ["FalconModel", FalconModel]], - ["stablelm", ["StableLmModel", StableLmModel]] - ]); - const MODEL_FOR_SPEECH_SEQ_2_SEQ_MAPPING_NAMES = /* @__PURE__ */ new Map([ - ["speecht5", ["SpeechT5ForSpeechToText", SpeechT5ForSpeechToText]], - ["whisper", ["WhisperForConditionalGeneration", WhisperForConditionalGeneration]] - ]); - const MODEL_FOR_TEXT_TO_SPECTROGRAM_MAPPING_NAMES = /* @__PURE__ */ new Map([ - ["speecht5", ["SpeechT5ForTextToSpeech", SpeechT5ForTextToSpeech]] - ]); - const MODEL_FOR_TEXT_TO_WAVEFORM_MAPPING_NAMES = /* @__PURE__ */ new Map([ - ["vits", ["VitsModel", VitsModel]], - ["musicgen", ["MusicgenForConditionalGeneration", MusicgenForConditionalGeneration]] - ]); - const MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING_NAMES = /* @__PURE__ */ new Map([ - ["bert", ["BertForSequenceClassification", BertForSequenceClassification]], - ["roformer", ["RoFormerForSequenceClassification", RoFormerForSequenceClassification]], - ["electra", ["ElectraForSequenceClassification", ElectraForSequenceClassification]], - ["esm", ["EsmForSequenceClassification", EsmForSequenceClassification]], - ["convbert", ["ConvBertForSequenceClassification", ConvBertForSequenceClassification]], - ["camembert", ["CamembertForSequenceClassification", CamembertForSequenceClassification]], - ["deberta", ["DebertaForSequenceClassification", DebertaForSequenceClassification]], - ["deberta-v2", ["DebertaV2ForSequenceClassification", DebertaV2ForSequenceClassification]], - ["mpnet", ["MPNetForSequenceClassification", MPNetForSequenceClassification]], - ["albert", ["AlbertForSequenceClassification", AlbertForSequenceClassification]], - ["distilbert", ["DistilBertForSequenceClassification", DistilBertForSequenceClassification]], - ["roberta", ["RobertaForSequenceClassification", RobertaForSequenceClassification]], - ["xlm", ["XLMForSequenceClassification", XLMForSequenceClassification]], - ["xlm-roberta", ["XLMRobertaForSequenceClassification", XLMRobertaForSequenceClassification]], - ["bart", ["BartForSequenceClassification", BartForSequenceClassification]], - ["mbart", ["MBartForSequenceClassification", MBartForSequenceClassification]], - ["mobilebert", ["MobileBertForSequenceClassification", MobileBertForSequenceClassification]], - ["squeezebert", ["SqueezeBertForSequenceClassification", SqueezeBertForSequenceClassification]] - ]); - const MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING_NAMES = /* @__PURE__ */ new Map([ - ["bert", ["BertForTokenClassification", BertForTokenClassification]], - ["roformer", ["RoFormerForTokenClassification", RoFormerForTokenClassification]], - ["electra", ["ElectraForTokenClassification", ElectraForTokenClassification]], - ["esm", ["EsmForTokenClassification", EsmForTokenClassification]], - ["convbert", ["ConvBertForTokenClassification", ConvBertForTokenClassification]], - ["camembert", ["CamembertForTokenClassification", CamembertForTokenClassification]], - ["deberta", ["DebertaForTokenClassification", DebertaForTokenClassification]], - ["deberta-v2", ["DebertaV2ForTokenClassification", DebertaV2ForTokenClassification]], - ["mpnet", ["MPNetForTokenClassification", MPNetForTokenClassification]], - ["distilbert", ["DistilBertForTokenClassification", DistilBertForTokenClassification]], - ["roberta", ["RobertaForTokenClassification", RobertaForTokenClassification]], - ["xlm", ["XLMForTokenClassification", XLMForTokenClassification]], - ["xlm-roberta", ["XLMRobertaForTokenClassification", XLMRobertaForTokenClassification]] - ]); - const MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING_NAMES = /* @__PURE__ */ new Map([ - ["t5", ["T5ForConditionalGeneration", T5ForConditionalGeneration]], - ["longt5", ["LongT5ForConditionalGeneration", LongT5ForConditionalGeneration]], - ["mt5", ["MT5ForConditionalGeneration", MT5ForConditionalGeneration]], - ["bart", ["BartForConditionalGeneration", BartForConditionalGeneration]], - ["mbart", ["MBartForConditionalGeneration", MBartForConditionalGeneration]], - ["marian", ["MarianMTModel", MarianMTModel]], - ["m2m_100", ["M2M100ForConditionalGeneration", M2M100ForConditionalGeneration]], - ["blenderbot", ["BlenderbotForConditionalGeneration", BlenderbotForConditionalGeneration]], - ["blenderbot-small", ["BlenderbotSmallForConditionalGeneration", BlenderbotSmallForConditionalGeneration]] - ]); - const MODEL_FOR_CAUSAL_LM_MAPPING_NAMES = /* @__PURE__ */ new Map([ - ["bloom", ["BloomForCausalLM", BloomForCausalLM]], - ["gpt2", ["GPT2LMHeadModel", GPT2LMHeadModel]], - ["jais", ["JAISLMHeadModel", JAISLMHeadModel]], - ["gptj", ["GPTJForCausalLM", GPTJForCausalLM]], - ["gpt_bigcode", ["GPTBigCodeForCausalLM", GPTBigCodeForCausalLM]], - ["gpt_neo", ["GPTNeoForCausalLM", GPTNeoForCausalLM]], - ["gpt_neox", ["GPTNeoXForCausalLM", GPTNeoXForCausalLM]], - ["codegen", ["CodeGenForCausalLM", CodeGenForCausalLM]], - ["llama", ["LlamaForCausalLM", LlamaForCausalLM]], - ["granite", ["GraniteForCausalLM", GraniteForCausalLM]], - ["cohere", ["CohereForCausalLM", CohereForCausalLM]], - ["gemma", ["GemmaForCausalLM", GemmaForCausalLM]], - ["gemma2", ["Gemma2ForCausalLM", Gemma2ForCausalLM]], - ["openelm", ["OpenELMForCausalLM", OpenELMForCausalLM]], - ["qwen2", ["Qwen2ForCausalLM", Qwen2ForCausalLM]], - ["phi", ["PhiForCausalLM", PhiForCausalLM]], - ["phi3", ["Phi3ForCausalLM", Phi3ForCausalLM]], - ["mpt", ["MptForCausalLM", MptForCausalLM]], - ["opt", ["OPTForCausalLM", OPTForCausalLM]], - ["mbart", ["MBartForCausalLM", MBartForCausalLM]], - ["mistral", ["MistralForCausalLM", MistralForCausalLM]], - ["starcoder2", ["Starcoder2ForCausalLM", Starcoder2ForCausalLM]], - ["falcon", ["FalconForCausalLM", FalconForCausalLM]], - ["trocr", ["TrOCRForCausalLM", TrOCRForCausalLM]], - ["stablelm", ["StableLmForCausalLM", StableLmForCausalLM]] - ]); - const MODEL_FOR_MASKED_LM_MAPPING_NAMES = /* @__PURE__ */ new Map([ - ["bert", ["BertForMaskedLM", BertForMaskedLM]], - ["roformer", ["RoFormerForMaskedLM", RoFormerForMaskedLM]], - ["electra", ["ElectraForMaskedLM", ElectraForMaskedLM]], - ["esm", ["EsmForMaskedLM", EsmForMaskedLM]], - ["convbert", ["ConvBertForMaskedLM", ConvBertForMaskedLM]], - ["camembert", ["CamembertForMaskedLM", CamembertForMaskedLM]], - ["deberta", ["DebertaForMaskedLM", DebertaForMaskedLM]], - ["deberta-v2", ["DebertaV2ForMaskedLM", DebertaV2ForMaskedLM]], - ["mpnet", ["MPNetForMaskedLM", MPNetForMaskedLM]], - ["albert", ["AlbertForMaskedLM", AlbertForMaskedLM]], - ["distilbert", ["DistilBertForMaskedLM", DistilBertForMaskedLM]], - ["roberta", ["RobertaForMaskedLM", RobertaForMaskedLM]], - ["xlm", ["XLMWithLMHeadModel", XLMWithLMHeadModel]], - ["xlm-roberta", ["XLMRobertaForMaskedLM", XLMRobertaForMaskedLM]], - ["mobilebert", ["MobileBertForMaskedLM", MobileBertForMaskedLM]], - ["squeezebert", ["SqueezeBertForMaskedLM", SqueezeBertForMaskedLM]] - ]); - const MODEL_FOR_QUESTION_ANSWERING_MAPPING_NAMES = /* @__PURE__ */ new Map([ - ["bert", ["BertForQuestionAnswering", BertForQuestionAnswering]], - ["roformer", ["RoFormerForQuestionAnswering", RoFormerForQuestionAnswering]], - ["electra", ["ElectraForQuestionAnswering", ElectraForQuestionAnswering]], - ["convbert", ["ConvBertForQuestionAnswering", ConvBertForQuestionAnswering]], - ["camembert", ["CamembertForQuestionAnswering", CamembertForQuestionAnswering]], - ["deberta", ["DebertaForQuestionAnswering", DebertaForQuestionAnswering]], - ["deberta-v2", ["DebertaV2ForQuestionAnswering", DebertaV2ForQuestionAnswering]], - ["mpnet", ["MPNetForQuestionAnswering", MPNetForQuestionAnswering]], - ["albert", ["AlbertForQuestionAnswering", AlbertForQuestionAnswering]], - ["distilbert", ["DistilBertForQuestionAnswering", DistilBertForQuestionAnswering]], - ["roberta", ["RobertaForQuestionAnswering", RobertaForQuestionAnswering]], - ["xlm", ["XLMForQuestionAnswering", XLMForQuestionAnswering]], - ["xlm-roberta", ["XLMRobertaForQuestionAnswering", XLMRobertaForQuestionAnswering]], - ["mobilebert", ["MobileBertForQuestionAnswering", MobileBertForQuestionAnswering]], - ["squeezebert", ["SqueezeBertForQuestionAnswering", SqueezeBertForQuestionAnswering]] - ]); - const MODEL_FOR_VISION_2_SEQ_MAPPING_NAMES = /* @__PURE__ */ new Map([ - ["vision-encoder-decoder", ["VisionEncoderDecoderModel", VisionEncoderDecoderModel]] - ]); - const MODEL_FOR_IMAGE_TEXT_TO_TEXT_MAPPING_NAMES = /* @__PURE__ */ new Map([ - ["llava", ["LlavaForConditionalGeneration", LlavaForConditionalGeneration]], - ["moondream1", ["Moondream1ForConditionalGeneration", Moondream1ForConditionalGeneration]], - ["florence2", ["Florence2ForConditionalGeneration", Florence2ForConditionalGeneration]] - ]); - const MODEL_FOR_DOCUMENT_QUESTION_ANSWERING_MAPPING_NAMES = /* @__PURE__ */ new Map([ - ["vision-encoder-decoder", ["VisionEncoderDecoderModel", VisionEncoderDecoderModel]] - ]); - const MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING_NAMES = /* @__PURE__ */ new Map([ - ["vit", ["ViTForImageClassification", ViTForImageClassification]], - ["pvt", ["PvtForImageClassification", PvtForImageClassification]], - ["vit_msn", ["ViTMSNForImageClassification", ViTMSNForImageClassification]], - ["fastvit", ["FastViTForImageClassification", FastViTForImageClassification]], - ["mobilevit", ["MobileViTForImageClassification", MobileViTForImageClassification]], - ["mobilevitv2", ["MobileViTV2ForImageClassification", MobileViTV2ForImageClassification]], - ["beit", ["BeitForImageClassification", BeitForImageClassification]], - ["deit", ["DeiTForImageClassification", DeiTForImageClassification]], - ["hiera", ["HieraForImageClassification", HieraForImageClassification]], - ["convnext", ["ConvNextForImageClassification", ConvNextForImageClassification]], - ["convnextv2", ["ConvNextV2ForImageClassification", ConvNextV2ForImageClassification]], - ["dinov2", ["Dinov2ForImageClassification", Dinov2ForImageClassification]], - ["resnet", ["ResNetForImageClassification", ResNetForImageClassification]], - ["swin", ["SwinForImageClassification", SwinForImageClassification]], - ["segformer", ["SegformerForImageClassification", SegformerForImageClassification]], - ["efficientnet", ["EfficientNetForImageClassification", EfficientNetForImageClassification]], - ["mobilenet_v1", ["MobileNetV1ForImageClassification", MobileNetV1ForImageClassification]], - ["mobilenet_v2", ["MobileNetV2ForImageClassification", MobileNetV2ForImageClassification]], - ["mobilenet_v3", ["MobileNetV3ForImageClassification", MobileNetV3ForImageClassification]], - ["mobilenet_v4", ["MobileNetV4ForImageClassification", MobileNetV4ForImageClassification]] - ]); - const MODEL_FOR_OBJECT_DETECTION_MAPPING_NAMES = /* @__PURE__ */ new Map([ - ["detr", ["DetrForObjectDetection", DetrForObjectDetection]], - ["rt_detr", ["RTDetrForObjectDetection", RTDetrForObjectDetection]], - ["table-transformer", ["TableTransformerForObjectDetection", TableTransformerForObjectDetection]], - ["yolos", ["YolosForObjectDetection", YolosForObjectDetection]] - ]); - const MODEL_FOR_ZERO_SHOT_OBJECT_DETECTION_MAPPING_NAMES = /* @__PURE__ */ new Map([ - ["owlvit", ["OwlViTForObjectDetection", OwlViTForObjectDetection]], - ["owlv2", ["Owlv2ForObjectDetection", Owlv2ForObjectDetection]] - ]); - const MODEL_FOR_IMAGE_SEGMENTATION_MAPPING_NAMES = /* @__PURE__ */ new Map([ - // TODO: Do not add new models here - ["detr", ["DetrForSegmentation", DetrForSegmentation]], - ["clipseg", ["CLIPSegForImageSegmentation", CLIPSegForImageSegmentation]] - ]); - const MODEL_FOR_SEMANTIC_SEGMENTATION_MAPPING_NAMES = /* @__PURE__ */ new Map([ - ["segformer", ["SegformerForSemanticSegmentation", SegformerForSemanticSegmentation]], - ["sapiens", ["SapiensForSemanticSegmentation", SapiensForSemanticSegmentation]] - ]); - const MODEL_FOR_UNIVERSAL_SEGMENTATION_MAPPING_NAMES = /* @__PURE__ */ new Map([ - ["detr", ["DetrForSegmentation", DetrForSegmentation]], - ["maskformer", ["MaskFormerForInstanceSegmentation", MaskFormerForInstanceSegmentation]] - ]); - const MODEL_FOR_MASK_GENERATION_MAPPING_NAMES = /* @__PURE__ */ new Map([ - ["sam", ["SamModel", SamModel]] - ]); - const MODEL_FOR_CTC_MAPPING_NAMES = /* @__PURE__ */ new Map([ - ["wav2vec2", ["Wav2Vec2ForCTC", Wav2Vec2ForCTC]], - ["wav2vec2-bert", ["Wav2Vec2BertForCTC", Wav2Vec2BertForCTC]], - ["unispeech", ["UniSpeechForCTC", UniSpeechForCTC]], - ["unispeech-sat", ["UniSpeechSatForCTC", UniSpeechSatForCTC]], - ["wavlm", ["WavLMForCTC", WavLMForCTC]], - ["hubert", ["HubertForCTC", HubertForCTC]] - ]); - const MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING_NAMES = /* @__PURE__ */ new Map([ - ["wav2vec2", ["Wav2Vec2ForSequenceClassification", Wav2Vec2ForSequenceClassification]], - ["wav2vec2-bert", ["Wav2Vec2BertForSequenceClassification", Wav2Vec2BertForSequenceClassification]], - ["unispeech", ["UniSpeechForSequenceClassification", UniSpeechForSequenceClassification]], - ["unispeech-sat", ["UniSpeechSatForSequenceClassification", UniSpeechSatForSequenceClassification]], - ["wavlm", ["WavLMForSequenceClassification", WavLMForSequenceClassification]], - ["hubert", ["HubertForSequenceClassification", HubertForSequenceClassification]], - ["audio-spectrogram-transformer", ["ASTForAudioClassification", ASTForAudioClassification]] - ]); - const MODEL_FOR_AUDIO_XVECTOR_MAPPING_NAMES = /* @__PURE__ */ new Map([ - ["wavlm", ["WavLMForXVector", WavLMForXVector]] - ]); - const MODEL_FOR_AUDIO_FRAME_CLASSIFICATION_MAPPING_NAMES = /* @__PURE__ */ new Map([ - ["unispeech-sat", ["UniSpeechSatForAudioFrameClassification", UniSpeechSatForAudioFrameClassification]], - ["wavlm", ["WavLMForAudioFrameClassification", WavLMForAudioFrameClassification]], - ["wav2vec2", ["Wav2Vec2ForAudioFrameClassification", Wav2Vec2ForAudioFrameClassification]], - ["pyannote", ["PyAnnoteForAudioFrameClassification", PyAnnoteForAudioFrameClassification]] - ]); - const MODEL_FOR_IMAGE_MATTING_MAPPING_NAMES = /* @__PURE__ */ new Map([ - ["vitmatte", ["VitMatteForImageMatting", VitMatteForImageMatting]] - ]); - const MODEL_FOR_IMAGE_TO_IMAGE_MAPPING_NAMES = /* @__PURE__ */ new Map([ - ["swin2sr", ["Swin2SRForImageSuperResolution", Swin2SRForImageSuperResolution]] - ]); - const MODEL_FOR_DEPTH_ESTIMATION_MAPPING_NAMES = /* @__PURE__ */ new Map([ - ["dpt", ["DPTForDepthEstimation", DPTForDepthEstimation]], - ["depth_anything", ["DepthAnythingForDepthEstimation", DepthAnythingForDepthEstimation]], - ["glpn", ["GLPNForDepthEstimation", GLPNForDepthEstimation]], - ["sapiens", ["SapiensForDepthEstimation", SapiensForDepthEstimation]], - ["depth_pro", ["DepthProForDepthEstimation", DepthProForDepthEstimation]] - ]); - const MODEL_FOR_NORMAL_ESTIMATION_MAPPING_NAMES = /* @__PURE__ */ new Map([ - ["sapiens", ["SapiensForNormalEstimation", SapiensForNormalEstimation]] - ]); - const MODEL_FOR_IMAGE_FEATURE_EXTRACTION_MAPPING_NAMES = /* @__PURE__ */ new Map([ - ["clip", ["CLIPVisionModelWithProjection", CLIPVisionModelWithProjection]], - ["siglip", ["SiglipVisionModel", SiglipVisionModel]] - ]); - const MODEL_CLASS_TYPE_MAPPING = [ - [MODEL_MAPPING_NAMES_ENCODER_ONLY, MODEL_TYPES.EncoderOnly], - [MODEL_MAPPING_NAMES_ENCODER_DECODER, MODEL_TYPES.EncoderDecoder], - [MODEL_MAPPING_NAMES_DECODER_ONLY, MODEL_TYPES.DecoderOnly], - [MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING_NAMES, MODEL_TYPES.EncoderOnly], - [MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING_NAMES, MODEL_TYPES.EncoderOnly], - [MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING_NAMES, MODEL_TYPES.Seq2Seq], - [MODEL_FOR_SPEECH_SEQ_2_SEQ_MAPPING_NAMES, MODEL_TYPES.Seq2Seq], - [MODEL_FOR_CAUSAL_LM_MAPPING_NAMES, MODEL_TYPES.DecoderOnly], - [MODEL_FOR_MASKED_LM_MAPPING_NAMES, MODEL_TYPES.EncoderOnly], - [MODEL_FOR_QUESTION_ANSWERING_MAPPING_NAMES, MODEL_TYPES.EncoderOnly], - [MODEL_FOR_VISION_2_SEQ_MAPPING_NAMES, MODEL_TYPES.Vision2Seq], - [MODEL_FOR_IMAGE_TEXT_TO_TEXT_MAPPING_NAMES, MODEL_TYPES.ImageTextToText], - [MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING_NAMES, MODEL_TYPES.EncoderOnly], - [MODEL_FOR_IMAGE_SEGMENTATION_MAPPING_NAMES, MODEL_TYPES.EncoderOnly], - [MODEL_FOR_UNIVERSAL_SEGMENTATION_MAPPING_NAMES, MODEL_TYPES.EncoderOnly], - [MODEL_FOR_SEMANTIC_SEGMENTATION_MAPPING_NAMES, MODEL_TYPES.EncoderOnly], - [MODEL_FOR_IMAGE_MATTING_MAPPING_NAMES, MODEL_TYPES.EncoderOnly], - [MODEL_FOR_IMAGE_TO_IMAGE_MAPPING_NAMES, MODEL_TYPES.EncoderOnly], - [MODEL_FOR_DEPTH_ESTIMATION_MAPPING_NAMES, MODEL_TYPES.EncoderOnly], - [MODEL_FOR_NORMAL_ESTIMATION_MAPPING_NAMES, MODEL_TYPES.EncoderOnly], - [MODEL_FOR_OBJECT_DETECTION_MAPPING_NAMES, MODEL_TYPES.EncoderOnly], - [MODEL_FOR_ZERO_SHOT_OBJECT_DETECTION_MAPPING_NAMES, MODEL_TYPES.EncoderOnly], - [MODEL_FOR_MASK_GENERATION_MAPPING_NAMES, MODEL_TYPES.MaskGeneration], - [MODEL_FOR_CTC_MAPPING_NAMES, MODEL_TYPES.EncoderOnly], - [MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING_NAMES, MODEL_TYPES.EncoderOnly], - [MODEL_FOR_TEXT_TO_SPECTROGRAM_MAPPING_NAMES, MODEL_TYPES.Seq2Seq], - [MODEL_FOR_TEXT_TO_WAVEFORM_MAPPING_NAMES, MODEL_TYPES.EncoderOnly], - [MODEL_FOR_AUDIO_XVECTOR_MAPPING_NAMES, MODEL_TYPES.EncoderOnly], - [MODEL_FOR_AUDIO_FRAME_CLASSIFICATION_MAPPING_NAMES, MODEL_TYPES.EncoderOnly], - // Custom: - [MODEL_FOR_IMAGE_FEATURE_EXTRACTION_MAPPING_NAMES, MODEL_TYPES.EncoderOnly] - ]; - for (const [mappings, type] of MODEL_CLASS_TYPE_MAPPING) { - for (const [name, model] of mappings.values()) { - MODEL_TYPE_MAPPING.set(name, type); - MODEL_CLASS_TO_NAME_MAPPING.set(model, name); - MODEL_NAME_TO_CLASS_MAPPING.set(name, model); - } - } - const CUSTOM_MAPPING = [ - // OVERRIDE: - // TODO: Refactor to allow class to specify model - ["MusicgenForConditionalGeneration", MusicgenForConditionalGeneration, MODEL_TYPES.Musicgen], - ["CLIPTextModelWithProjection", CLIPTextModelWithProjection, MODEL_TYPES.EncoderOnly], - ["SiglipTextModel", SiglipTextModel, MODEL_TYPES.EncoderOnly], - ["ClapTextModelWithProjection", ClapTextModelWithProjection, MODEL_TYPES.EncoderOnly], - ["ClapAudioModelWithProjection", ClapAudioModelWithProjection, MODEL_TYPES.EncoderOnly] - ]; - for (const [name, model, type] of CUSTOM_MAPPING) { - MODEL_TYPE_MAPPING.set(name, type); - MODEL_CLASS_TO_NAME_MAPPING.set(model, name); - MODEL_NAME_TO_CLASS_MAPPING.set(name, model); - } - class AutoModel extends PretrainedMixin { - } - /** @type {Map[]} */ - // @ts-ignore - __publicField(AutoModel, "MODEL_CLASS_MAPPINGS", MODEL_CLASS_TYPE_MAPPING.map((x) => x[0])); - __publicField(AutoModel, "BASE_IF_FAIL", true); - class AutoModelForSequenceClassification extends PretrainedMixin { - } - __publicField(AutoModelForSequenceClassification, "MODEL_CLASS_MAPPINGS", [MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING_NAMES]); - class AutoModelForTokenClassification extends PretrainedMixin { - } - __publicField(AutoModelForTokenClassification, "MODEL_CLASS_MAPPINGS", [MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING_NAMES]); - class AutoModelForSeq2SeqLM extends PretrainedMixin { - } - __publicField(AutoModelForSeq2SeqLM, "MODEL_CLASS_MAPPINGS", [MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING_NAMES]); - class AutoModelForSpeechSeq2Seq extends PretrainedMixin { - } - __publicField(AutoModelForSpeechSeq2Seq, "MODEL_CLASS_MAPPINGS", [MODEL_FOR_SPEECH_SEQ_2_SEQ_MAPPING_NAMES]); - class AutoModelForTextToSpectrogram extends PretrainedMixin { - } - __publicField(AutoModelForTextToSpectrogram, "MODEL_CLASS_MAPPINGS", [MODEL_FOR_TEXT_TO_SPECTROGRAM_MAPPING_NAMES]); - class AutoModelForTextToWaveform extends PretrainedMixin { - } - __publicField(AutoModelForTextToWaveform, "MODEL_CLASS_MAPPINGS", [MODEL_FOR_TEXT_TO_WAVEFORM_MAPPING_NAMES]); - class AutoModelForCausalLM extends PretrainedMixin { - } - __publicField(AutoModelForCausalLM, "MODEL_CLASS_MAPPINGS", [MODEL_FOR_CAUSAL_LM_MAPPING_NAMES]); - class AutoModelForMaskedLM extends PretrainedMixin { - } - __publicField(AutoModelForMaskedLM, "MODEL_CLASS_MAPPINGS", [MODEL_FOR_MASKED_LM_MAPPING_NAMES]); - class AutoModelForQuestionAnswering extends PretrainedMixin { - } - __publicField(AutoModelForQuestionAnswering, "MODEL_CLASS_MAPPINGS", [MODEL_FOR_QUESTION_ANSWERING_MAPPING_NAMES]); - class AutoModelForVision2Seq extends PretrainedMixin { - } - __publicField(AutoModelForVision2Seq, "MODEL_CLASS_MAPPINGS", [MODEL_FOR_VISION_2_SEQ_MAPPING_NAMES]); - class AutoModelForImageClassification extends PretrainedMixin { - } - __publicField(AutoModelForImageClassification, "MODEL_CLASS_MAPPINGS", [MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING_NAMES]); - class AutoModelForImageSegmentation extends PretrainedMixin { - } - __publicField(AutoModelForImageSegmentation, "MODEL_CLASS_MAPPINGS", [MODEL_FOR_IMAGE_SEGMENTATION_MAPPING_NAMES]); - class AutoModelForSemanticSegmentation extends PretrainedMixin { - } - __publicField(AutoModelForSemanticSegmentation, "MODEL_CLASS_MAPPINGS", [MODEL_FOR_SEMANTIC_SEGMENTATION_MAPPING_NAMES]); - class AutoModelForUniversalSegmentation extends PretrainedMixin { - } - __publicField(AutoModelForUniversalSegmentation, "MODEL_CLASS_MAPPINGS", [MODEL_FOR_UNIVERSAL_SEGMENTATION_MAPPING_NAMES]); - class AutoModelForObjectDetection extends PretrainedMixin { - } - __publicField(AutoModelForObjectDetection, "MODEL_CLASS_MAPPINGS", [MODEL_FOR_OBJECT_DETECTION_MAPPING_NAMES]); - class AutoModelForZeroShotObjectDetection extends PretrainedMixin { - } - __publicField(AutoModelForZeroShotObjectDetection, "MODEL_CLASS_MAPPINGS", [MODEL_FOR_ZERO_SHOT_OBJECT_DETECTION_MAPPING_NAMES]); - class AutoModelForMaskGeneration extends PretrainedMixin { - } - __publicField(AutoModelForMaskGeneration, "MODEL_CLASS_MAPPINGS", [MODEL_FOR_MASK_GENERATION_MAPPING_NAMES]); - class AutoModelForCTC extends PretrainedMixin { - } - __publicField(AutoModelForCTC, "MODEL_CLASS_MAPPINGS", [MODEL_FOR_CTC_MAPPING_NAMES]); - class AutoModelForAudioClassification extends PretrainedMixin { - } - __publicField(AutoModelForAudioClassification, "MODEL_CLASS_MAPPINGS", [MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING_NAMES]); - class AutoModelForXVector extends PretrainedMixin { - } - __publicField(AutoModelForXVector, "MODEL_CLASS_MAPPINGS", [MODEL_FOR_AUDIO_XVECTOR_MAPPING_NAMES]); - class AutoModelForAudioFrameClassification extends PretrainedMixin { - } - __publicField(AutoModelForAudioFrameClassification, "MODEL_CLASS_MAPPINGS", [MODEL_FOR_AUDIO_FRAME_CLASSIFICATION_MAPPING_NAMES]); - class AutoModelForDocumentQuestionAnswering extends PretrainedMixin { - } - __publicField(AutoModelForDocumentQuestionAnswering, "MODEL_CLASS_MAPPINGS", [MODEL_FOR_DOCUMENT_QUESTION_ANSWERING_MAPPING_NAMES]); - class AutoModelForImageMatting extends PretrainedMixin { - } - __publicField(AutoModelForImageMatting, "MODEL_CLASS_MAPPINGS", [MODEL_FOR_IMAGE_MATTING_MAPPING_NAMES]); - class AutoModelForImageToImage extends PretrainedMixin { - } - __publicField(AutoModelForImageToImage, "MODEL_CLASS_MAPPINGS", [MODEL_FOR_IMAGE_TO_IMAGE_MAPPING_NAMES]); - class AutoModelForDepthEstimation extends PretrainedMixin { - } - __publicField(AutoModelForDepthEstimation, "MODEL_CLASS_MAPPINGS", [MODEL_FOR_DEPTH_ESTIMATION_MAPPING_NAMES]); - class AutoModelForNormalEstimation extends PretrainedMixin { - } - __publicField(AutoModelForNormalEstimation, "MODEL_CLASS_MAPPINGS", [MODEL_FOR_NORMAL_ESTIMATION_MAPPING_NAMES]); - class AutoModelForImageFeatureExtraction extends PretrainedMixin { - } - __publicField(AutoModelForImageFeatureExtraction, "MODEL_CLASS_MAPPINGS", [MODEL_FOR_IMAGE_FEATURE_EXTRACTION_MAPPING_NAMES]); - class Seq2SeqLMOutput extends ModelOutput { - /** - * @param {Object} output The output of the model. - * @param {Tensor} output.logits The output logits of the model. - * @param {Tensor} output.past_key_values An tensor of key/value pairs that represent the previous state of the model. - * @param {Tensor} output.encoder_outputs The output of the encoder in a sequence-to-sequence model. - * @param {Tensor} [output.decoder_attentions] Attentions weights of the decoder, after the attention softmax, used to compute the weighted average in the self-attention heads. - * @param {Tensor} [output.cross_attentions] Attentions weights of the decoder's cross-attention layer, after the attention softmax, used to compute the weighted average in the cross-attention heads. - */ - constructor({ logits, past_key_values, encoder_outputs, decoder_attentions = null, cross_attentions = null }) { - super(); - this.logits = logits; - this.past_key_values = past_key_values; - this.encoder_outputs = encoder_outputs; - this.decoder_attentions = decoder_attentions; - this.cross_attentions = cross_attentions; - } - } - class SequenceClassifierOutput extends ModelOutput { - /** - * @param {Object} output The output of the model. - * @param {Tensor} output.logits classification (or regression if config.num_labels==1) scores (before SoftMax). - */ - constructor({ logits }) { - super(); - this.logits = logits; - } - } - class XVectorOutput extends ModelOutput { - /** - * @param {Object} output The output of the model. - * @param {Tensor} output.logits Classification hidden states before AMSoftmax, of shape `(batch_size, config.xvector_output_dim)`. - * @param {Tensor} output.embeddings Utterance embeddings used for vector similarity-based retrieval, of shape `(batch_size, config.xvector_output_dim)`. - */ - constructor({ logits, embeddings }) { - super(); - this.logits = logits; - this.embeddings = embeddings; - } - } - class TokenClassifierOutput extends ModelOutput { - /** - * @param {Object} output The output of the model. - * @param {Tensor} output.logits Classification scores (before SoftMax). - */ - constructor({ logits }) { - super(); - this.logits = logits; - } - } - class MaskedLMOutput extends ModelOutput { - /** - * @param {Object} output The output of the model. - * @param {Tensor} output.logits Prediction scores of the language modeling head (scores for each vocabulary token before SoftMax). - */ - constructor({ logits }) { - super(); - this.logits = logits; - } - } - class QuestionAnsweringModelOutput extends ModelOutput { - /** - * @param {Object} output The output of the model. - * @param {Tensor} output.start_logits Span-start scores (before SoftMax). - * @param {Tensor} output.end_logits Span-end scores (before SoftMax). - */ - constructor({ start_logits, end_logits }) { - super(); - this.start_logits = start_logits; - this.end_logits = end_logits; - } - } - class CausalLMOutput extends ModelOutput { - /** - * @param {Object} output The output of the model. - * @param {Tensor} output.logits Prediction scores of the language modeling head (scores for each vocabulary token before softmax). - */ - constructor({ logits }) { - super(); - this.logits = logits; - } - } - class CausalLMOutputWithPast extends ModelOutput { - /** - * @param {Object} output The output of the model. - * @param {Tensor} output.logits Prediction scores of the language modeling head (scores for each vocabulary token before softmax). - * @param {Tensor} output.past_key_values Contains pre-computed hidden-states (key and values in the self-attention blocks) - * that can be used (see `past_key_values` input) to speed up sequential decoding. - */ - constructor({ logits, past_key_values }) { - super(); - this.logits = logits; - this.past_key_values = past_key_values; - } - } - class ImageMattingOutput extends ModelOutput { - /** - * @param {Object} output The output of the model. - * @param {Tensor} output.alphas Estimated alpha values, of shape `(batch_size, num_channels, height, width)`. - */ - constructor({ alphas }) { - super(); - this.alphas = alphas; - } - } - class VitsModelOutput extends ModelOutput { - /** - * @param {Object} output The output of the model. - * @param {Tensor} output.waveform The final audio waveform predicted by the model, of shape `(batch_size, sequence_length)`. - * @param {Tensor} output.spectrogram The log-mel spectrogram predicted at the output of the flow model. - * This spectrogram is passed to the Hi-Fi GAN decoder model to obtain the final audio waveform. - */ - constructor({ waveform, spectrogram }) { - super(); - this.waveform = waveform; - this.spectrogram = spectrogram; - } - } - } - ), - /***/ - "./src/models/whisper/common_whisper.js": ( - /*!**********************************************!*\ - !*** ./src/models/whisper/common_whisper.js ***! - \**********************************************/ - /***/ - (__unused_webpack___webpack_module__, __webpack_exports__2, __webpack_require__2) => { - __webpack_require__2.r(__webpack_exports__2); - __webpack_require__2.d(__webpack_exports__2, { - /* harmony export */ - WHISPER_LANGUAGE_MAPPING: () => ( - /* binding */ - WHISPER_LANGUAGE_MAPPING - ), - /* harmony export */ - WHISPER_TO_LANGUAGE_CODE_MAPPING: () => ( - /* binding */ - WHISPER_TO_LANGUAGE_CODE_MAPPING - ), - /* harmony export */ - whisper_language_to_code: () => ( - /* binding */ - whisper_language_to_code - ) - /* harmony export */ - }); - const WHISPER_LANGUAGES = [ - ["en", "english"], - ["zh", "chinese"], - ["de", "german"], - ["es", "spanish"], - ["ru", "russian"], - ["ko", "korean"], - ["fr", "french"], - ["ja", "japanese"], - ["pt", "portuguese"], - ["tr", "turkish"], - ["pl", "polish"], - ["ca", "catalan"], - ["nl", "dutch"], - ["ar", "arabic"], - ["sv", "swedish"], - ["it", "italian"], - ["id", "indonesian"], - ["hi", "hindi"], - ["fi", "finnish"], - ["vi", "vietnamese"], - ["he", "hebrew"], - ["uk", "ukrainian"], - ["el", "greek"], - ["ms", "malay"], - ["cs", "czech"], - ["ro", "romanian"], - ["da", "danish"], - ["hu", "hungarian"], - ["ta", "tamil"], - ["no", "norwegian"], - ["th", "thai"], - ["ur", "urdu"], - ["hr", "croatian"], - ["bg", "bulgarian"], - ["lt", "lithuanian"], - ["la", "latin"], - ["mi", "maori"], - ["ml", "malayalam"], - ["cy", "welsh"], - ["sk", "slovak"], - ["te", "telugu"], - ["fa", "persian"], - ["lv", "latvian"], - ["bn", "bengali"], - ["sr", "serbian"], - ["az", "azerbaijani"], - ["sl", "slovenian"], - ["kn", "kannada"], - ["et", "estonian"], - ["mk", "macedonian"], - ["br", "breton"], - ["eu", "basque"], - ["is", "icelandic"], - ["hy", "armenian"], - ["ne", "nepali"], - ["mn", "mongolian"], - ["bs", "bosnian"], - ["kk", "kazakh"], - ["sq", "albanian"], - ["sw", "swahili"], - ["gl", "galician"], - ["mr", "marathi"], - ["pa", "punjabi"], - ["si", "sinhala"], - ["km", "khmer"], - ["sn", "shona"], - ["yo", "yoruba"], - ["so", "somali"], - ["af", "afrikaans"], - ["oc", "occitan"], - ["ka", "georgian"], - ["be", "belarusian"], - ["tg", "tajik"], - ["sd", "sindhi"], - ["gu", "gujarati"], - ["am", "amharic"], - ["yi", "yiddish"], - ["lo", "lao"], - ["uz", "uzbek"], - ["fo", "faroese"], - ["ht", "haitian creole"], - ["ps", "pashto"], - ["tk", "turkmen"], - ["nn", "nynorsk"], - ["mt", "maltese"], - ["sa", "sanskrit"], - ["lb", "luxembourgish"], - ["my", "myanmar"], - ["bo", "tibetan"], - ["tl", "tagalog"], - ["mg", "malagasy"], - ["as", "assamese"], - ["tt", "tatar"], - ["haw", "hawaiian"], - ["ln", "lingala"], - ["ha", "hausa"], - ["ba", "bashkir"], - ["jw", "javanese"], - ["su", "sundanese"] - ]; - const WHISPER_LANGUAGE_MAPPING = new Map(WHISPER_LANGUAGES); - const WHISPER_TO_LANGUAGE_CODE_MAPPING = new Map([ - ...WHISPER_LANGUAGES.map(([k, v]) => [v, k]), - ...[ - ["burmese", "my"], - ["valencian", "ca"], - ["flemish", "nl"], - ["haitian", "ht"], - ["letzeburgesch", "lb"], - ["pushto", "ps"], - ["panjabi", "pa"], - ["moldavian", "ro"], - ["moldovan", "ro"], - ["sinhalese", "si"], - ["castilian", "es"] - ] - ]); - function whisper_language_to_code(language) { - language = language.toLowerCase(); - let language_code = WHISPER_TO_LANGUAGE_CODE_MAPPING.get(language); - if (language_code === void 0) { - if (WHISPER_LANGUAGE_MAPPING.has(language)) { - language_code = language; - } else { - const is_language_code = language.length === 2; - const langs = is_language_code ? WHISPER_LANGUAGE_MAPPING.keys() : WHISPER_LANGUAGE_MAPPING.values(); - throw new Error(`Language "${language}" is not supported. Must be one of: ${JSON.stringify(langs)}`); - } - } - return language_code; - } - } - ), - /***/ - "./src/models/whisper/generation_whisper.js": ( - /*!**************************************************!*\ - !*** ./src/models/whisper/generation_whisper.js ***! - \**************************************************/ - /***/ - (__unused_webpack___webpack_module__, __webpack_exports__2, __webpack_require__2) => { - __webpack_require__2.r(__webpack_exports__2); - __webpack_require__2.d(__webpack_exports__2, { - /* harmony export */ - WhisperGenerationConfig: () => ( - /* binding */ - WhisperGenerationConfig - ) - /* harmony export */ - }); - var _generation_configuration_utils_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__2( - /*! ../../generation/configuration_utils.js */ - "./src/generation/configuration_utils.js" - ); - class WhisperGenerationConfig extends _generation_configuration_utils_js__WEBPACK_IMPORTED_MODULE_0__.GenerationConfig { - constructor() { - super(...arguments); - /** - * Whether to return the timestamps with the text. This enables the `WhisperTimestampsLogitsProcessor`. - * @type {boolean} - */ - __publicField(this, "return_timestamps", null); - /** - * Whether to return token-level timestamps - * with the text. This can be used with or without the `return_timestamps` option. To get word-level - * timestamps, use the tokenizer to group the tokens into words. - * @type {boolean} - */ - __publicField(this, "return_token_timestamps", null); - /** - * The number of audio frames available in this chunk. This is only used generating word-level timestamps. - * @type {number} - */ - __publicField(this, "num_frames", null); - /** - * Alignment heads to predict word-level timestamps. This is a list of [layer, head] pairs that - * select the cross-attention heads that are highly correlated to word-level timing. - * @type {[number, number][]} - */ - __publicField(this, "alignment_heads", null); - /** - * Task to use for generation, either "translate" or "transcribe". - * @type {string} - */ - __publicField(this, "task", null); - /** - * Language token to use for generation, can be either in the form of `<|en|>`, `en` or `english`. - * You can find all the possible language tokens in the `model.generation_config.lang_to_id` dictionary. - * @type {string} - */ - __publicField(this, "language", null); - /** - * The id of the `"<|notimestamps|>"` token. - * @type {number} - */ - __publicField(this, "no_timestamps_token_id", null); - /** - * Rank-1 list of token IDs created by passing text to [`~WhisperProcessor.get_prompt_ids`] that is - * provided as a prompt to each chunk. This can be used to provide or "prompt-engineer" a context for - * transcription, e.g. custom vocabularies or proper nouns to make it more likely to predict those words - * correctly. It cannot be used in conjunction with `decoder_start_token_id` as it overwrites this value. - * @type {number[]} - */ - __publicField(this, "prompt_ids", null); - /** - * Whether the model is multilingual or not. - * @type {boolean} - */ - __publicField(this, "is_multilingual", null); - /** - * (Optional) A mapping from language tokens to their corresponding IDs. - * Only required if the model is multilingual. - * @type {Record|null} - */ - __publicField(this, "lang_to_id", null); - /** - * (Optional) A mapping from task tokens to their corresponding IDs. - * @type {Record|null} - */ - __publicField(this, "task_to_id", null); - /** - * Used to set the maximum value of the initial timestamp. This is used to prevent the model from - * predicting timestamps that are too far in the future. - * @type {number} - */ - __publicField(this, "max_initial_timestamp_index", 1); - } - } - } - ), - /***/ - "./src/ops/registry.js": ( - /*!*****************************!*\ - !*** ./src/ops/registry.js ***! - \*****************************/ - /***/ - (__unused_webpack___webpack_module__, __webpack_exports__2, __webpack_require__2) => { - __webpack_require__2.r(__webpack_exports__2); - __webpack_require__2.d(__webpack_exports__2, { - /* harmony export */ - TensorOpRegistry: () => ( - /* binding */ - TensorOpRegistry - ) - /* harmony export */ - }); - var _backends_onnx_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__2( - /*! ../backends/onnx.js */ - "./src/backends/onnx.js" - ); - var _utils_tensor_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__2( - /*! ../utils/tensor.js */ - "./src/utils/tensor.js" - ); - const wrap = async (session_bytes, session_options, names) => { - const session = await (0, _backends_onnx_js__WEBPACK_IMPORTED_MODULE_0__.createInferenceSession)( - new Uint8Array(session_bytes), - session_options - ); - return ( - /** @type {any} */ - async (inputs) => { - const ortFeed = Object.fromEntries(Object.entries(inputs).map(([k, v]) => [k, v.ort_tensor])); - const outputs = await session.run(ortFeed); - if (Array.isArray(names)) { - return names.map((n) => new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_1__.Tensor(outputs[n])); - } else { - return new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_1__.Tensor(outputs[ - /** @type {string} */ - names - ]); - } - } - ); - }; - class TensorOpRegistry { - static get bilinear_interpolate_4d() { - if (!this._bilinear_interpolate_4d) { - this._bilinear_interpolate_4d = wrap( - [8, 9, 18, 0, 58, 128, 1, 10, 40, 10, 1, 120, 10, 0, 10, 0, 10, 1, 115, 18, 1, 121, 34, 6, 82, 101, 115, 105, 122, 101, 42, 17, 10, 4, 109, 111, 100, 101, 34, 6, 108, 105, 110, 101, 97, 114, 160, 1, 3, 18, 1, 114, 90, 31, 10, 1, 120, 18, 26, 10, 24, 8, 1, 18, 20, 10, 3, 18, 1, 98, 10, 3, 18, 1, 99, 10, 3, 18, 1, 104, 10, 3, 18, 1, 119, 90, 15, 10, 1, 115, 18, 10, 10, 8, 8, 7, 18, 4, 10, 2, 8, 4, 98, 31, 10, 1, 121, 18, 26, 10, 24, 8, 1, 18, 20, 10, 3, 18, 1, 98, 10, 3, 18, 1, 99, 10, 3, 18, 1, 104, 10, 3, 18, 1, 119, 66, 2, 16, 20], - this.session_options, - "y" - ); - } - return this._bilinear_interpolate_4d; - } - static get bicubic_interpolate_4d() { - if (!this._bicubic_interpolate_4d) { - this._bicubic_interpolate_4d = wrap( - [8, 9, 18, 0, 58, 127, 10, 39, 10, 1, 120, 10, 0, 10, 0, 10, 1, 115, 18, 1, 121, 34, 6, 82, 101, 115, 105, 122, 101, 42, 16, 10, 4, 109, 111, 100, 101, 34, 5, 99, 117, 98, 105, 99, 160, 1, 3, 18, 1, 114, 90, 31, 10, 1, 120, 18, 26, 10, 24, 8, 1, 18, 20, 10, 3, 18, 1, 98, 10, 3, 18, 1, 99, 10, 3, 18, 1, 104, 10, 3, 18, 1, 119, 90, 15, 10, 1, 115, 18, 10, 10, 8, 8, 7, 18, 4, 10, 2, 8, 4, 98, 31, 10, 1, 121, 18, 26, 10, 24, 8, 1, 18, 20, 10, 3, 18, 1, 98, 10, 3, 18, 1, 99, 10, 3, 18, 1, 104, 10, 3, 18, 1, 119, 66, 2, 16, 20], - this.session_options, - "y" - ); - } - return this._bicubic_interpolate_4d; - } - static get matmul() { - if (!this._matmul) { - this._matmul = wrap( - [8, 9, 18, 0, 58, 55, 10, 17, 10, 1, 97, 10, 1, 98, 18, 1, 99, 34, 6, 77, 97, 116, 77, 117, 108, 18, 1, 114, 90, 9, 10, 1, 97, 18, 4, 10, 2, 8, 1, 90, 9, 10, 1, 98, 18, 4, 10, 2, 8, 1, 98, 9, 10, 1, 99, 18, 4, 10, 2, 8, 1, 66, 2, 16, 20], - this.session_options, - "c" - ); - } - return this._matmul; - } - static get stft() { - if (!this._stft) { - this._stft = wrap( - [8, 7, 18, 0, 58, 148, 1, 10, 38, 10, 1, 115, 10, 1, 106, 10, 1, 119, 10, 1, 108, 18, 1, 111, 34, 4, 83, 84, 70, 84, 42, 15, 10, 8, 111, 110, 101, 115, 105, 100, 101, 100, 24, 1, 160, 1, 2, 18, 1, 115, 90, 26, 10, 1, 115, 18, 21, 10, 19, 8, 1, 18, 15, 10, 3, 18, 1, 98, 10, 3, 18, 1, 115, 10, 3, 18, 1, 99, 90, 11, 10, 1, 106, 18, 6, 10, 4, 8, 7, 18, 0, 90, 16, 10, 1, 119, 18, 11, 10, 9, 8, 1, 18, 5, 10, 3, 18, 1, 119, 90, 11, 10, 1, 108, 18, 6, 10, 4, 8, 7, 18, 0, 98, 31, 10, 1, 111, 18, 26, 10, 24, 8, 1, 18, 20, 10, 3, 18, 1, 98, 10, 3, 18, 1, 102, 10, 3, 18, 1, 100, 10, 3, 18, 1, 99, 66, 2, 16, 17], - this.session_options, - "o" - ); - } - return this._stft; - } - static get rfft() { - if (!this._rfft) { - this._rfft = wrap( - [8, 9, 18, 0, 58, 97, 10, 33, 10, 1, 120, 10, 0, 10, 1, 97, 18, 1, 121, 34, 3, 68, 70, 84, 42, 15, 10, 8, 111, 110, 101, 115, 105, 100, 101, 100, 24, 1, 160, 1, 2, 18, 1, 100, 90, 21, 10, 1, 120, 18, 16, 10, 14, 8, 1, 18, 10, 10, 3, 18, 1, 115, 10, 3, 18, 1, 99, 90, 11, 10, 1, 97, 18, 6, 10, 4, 8, 7, 18, 0, 98, 21, 10, 1, 121, 18, 16, 10, 14, 8, 1, 18, 10, 10, 3, 18, 1, 115, 10, 3, 18, 1, 99, 66, 2, 16, 20], - this.session_options, - "y" - ); - } - return this._rfft; - } - static get top_k() { - if (!this._top_k) { - this._top_k = wrap( - [8, 10, 18, 0, 58, 73, 10, 18, 10, 1, 120, 10, 1, 107, 18, 1, 118, 18, 1, 105, 34, 4, 84, 111, 112, 75, 18, 1, 116, 90, 9, 10, 1, 120, 18, 4, 10, 2, 8, 1, 90, 15, 10, 1, 107, 18, 10, 10, 8, 8, 7, 18, 4, 10, 2, 8, 1, 98, 9, 10, 1, 118, 18, 4, 10, 2, 8, 1, 98, 9, 10, 1, 105, 18, 4, 10, 2, 8, 7, 66, 2, 16, 21], - this.session_options, - [ - /* Values */ - "v", - /* Indices */ - "i" - ] - ); - } - return this._top_k; - } - } - __publicField(TensorOpRegistry, "session_options", { - // TODO: Allow for multiple execution providers - // executionProviders: ['webgpu'], - }); - } - ), - /***/ - "./src/pipelines.js": ( - /*!**************************!*\ - !*** ./src/pipelines.js ***! - \**************************/ - /***/ - (__unused_webpack___webpack_module__, __webpack_exports__2, __webpack_require__2) => { - __webpack_require__2.r(__webpack_exports__2); - __webpack_require__2.d(__webpack_exports__2, { - /* harmony export */ - AudioClassificationPipeline: () => ( - /* binding */ - AudioClassificationPipeline - ), - /* harmony export */ - AutomaticSpeechRecognitionPipeline: () => ( - /* binding */ - AutomaticSpeechRecognitionPipeline - ), - /* harmony export */ - DepthEstimationPipeline: () => ( - /* binding */ - DepthEstimationPipeline - ), - /* harmony export */ - DocumentQuestionAnsweringPipeline: () => ( - /* binding */ - DocumentQuestionAnsweringPipeline - ), - /* harmony export */ - FeatureExtractionPipeline: () => ( - /* binding */ - FeatureExtractionPipeline - ), - /* harmony export */ - FillMaskPipeline: () => ( - /* binding */ - FillMaskPipeline - ), - /* harmony export */ - ImageClassificationPipeline: () => ( - /* binding */ - ImageClassificationPipeline - ), - /* harmony export */ - ImageFeatureExtractionPipeline: () => ( - /* binding */ - ImageFeatureExtractionPipeline - ), - /* harmony export */ - ImageSegmentationPipeline: () => ( - /* binding */ - ImageSegmentationPipeline - ), - /* harmony export */ - ImageToImagePipeline: () => ( - /* binding */ - ImageToImagePipeline - ), - /* harmony export */ - ImageToTextPipeline: () => ( - /* binding */ - ImageToTextPipeline - ), - /* harmony export */ - ObjectDetectionPipeline: () => ( - /* binding */ - ObjectDetectionPipeline - ), - /* harmony export */ - Pipeline: () => ( - /* binding */ - Pipeline - ), - /* harmony export */ - QuestionAnsweringPipeline: () => ( - /* binding */ - QuestionAnsweringPipeline - ), - /* harmony export */ - SummarizationPipeline: () => ( - /* binding */ - SummarizationPipeline - ), - /* harmony export */ - Text2TextGenerationPipeline: () => ( - /* binding */ - Text2TextGenerationPipeline - ), - /* harmony export */ - TextClassificationPipeline: () => ( - /* binding */ - TextClassificationPipeline - ), - /* harmony export */ - TextGenerationPipeline: () => ( - /* binding */ - TextGenerationPipeline - ), - /* harmony export */ - TextToAudioPipeline: () => ( - /* binding */ - TextToAudioPipeline - ), - /* harmony export */ - TokenClassificationPipeline: () => ( - /* binding */ - TokenClassificationPipeline - ), - /* harmony export */ - TranslationPipeline: () => ( - /* binding */ - TranslationPipeline - ), - /* harmony export */ - ZeroShotAudioClassificationPipeline: () => ( - /* binding */ - ZeroShotAudioClassificationPipeline - ), - /* harmony export */ - ZeroShotClassificationPipeline: () => ( - /* binding */ - ZeroShotClassificationPipeline - ), - /* harmony export */ - ZeroShotImageClassificationPipeline: () => ( - /* binding */ - ZeroShotImageClassificationPipeline - ), - /* harmony export */ - ZeroShotObjectDetectionPipeline: () => ( - /* binding */ - ZeroShotObjectDetectionPipeline - ), - /* harmony export */ - pipeline: () => ( - /* binding */ - pipeline - ) - /* harmony export */ - }); - var _tokenizers_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__2( - /*! ./tokenizers.js */ - "./src/tokenizers.js" - ); - var _models_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__2( - /*! ./models.js */ - "./src/models.js" - ); - var _processors_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__2( - /*! ./processors.js */ - "./src/processors.js" - ); - var _utils_generic_js__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__2( - /*! ./utils/generic.js */ - "./src/utils/generic.js" - ); - var _utils_core_js__WEBPACK_IMPORTED_MODULE_4__ = __webpack_require__2( - /*! ./utils/core.js */ - "./src/utils/core.js" - ); - var _utils_maths_js__WEBPACK_IMPORTED_MODULE_5__ = __webpack_require__2( - /*! ./utils/maths.js */ - "./src/utils/maths.js" - ); - var _utils_audio_js__WEBPACK_IMPORTED_MODULE_6__2 = __webpack_require__2( - /*! ./utils/audio.js */ - "./src/utils/audio.js" - ); - var _utils_tensor_js__WEBPACK_IMPORTED_MODULE_7__ = __webpack_require__2( - /*! ./utils/tensor.js */ - "./src/utils/tensor.js" - ); - var _utils_image_js__WEBPACK_IMPORTED_MODULE_8__ = __webpack_require__2( - /*! ./utils/image.js */ - "./src/utils/image.js" - ); - async function prepareImages(images) { - if (!Array.isArray(images)) { - images = [images]; - } - return await Promise.all(images.map((x) => _utils_image_js__WEBPACK_IMPORTED_MODULE_8__.RawImage.read(x))); - } - async function prepareAudios(audios, sampling_rate) { - if (!Array.isArray(audios)) { - audios = [audios]; - } - return await Promise.all(audios.map((x) => { - if (typeof x === "string" || x instanceof URL) { - return (0, _utils_audio_js__WEBPACK_IMPORTED_MODULE_6__2.read_audio)(x, sampling_rate); - } else if (x instanceof Float64Array) { - return new Float32Array(x); - } - return x; - })); - } - function get_bounding_box(box, asInteger) { - if (asInteger) { - box = box.map((x) => x | 0); - } - const [xmin, ymin, xmax, ymax] = box; - return { xmin, ymin, xmax, ymax }; - } - class Pipeline extends _utils_generic_js__WEBPACK_IMPORTED_MODULE_3__.Callable { - /** - * Create a new Pipeline. - * @param {Object} options An object containing the following properties: - * @param {string} [options.task] The task of the pipeline. Useful for specifying subtasks. - * @param {PreTrainedModel} [options.model] The model used by the pipeline. - * @param {PreTrainedTokenizer} [options.tokenizer=null] The tokenizer used by the pipeline (if any). - * @param {Processor} [options.processor=null] The processor used by the pipeline (if any). - */ - constructor({ task, model, tokenizer = null, processor = null }) { - super(); - this.task = task; - this.model = model; - this.tokenizer = tokenizer; - this.processor = processor; - } - /** @type {DisposeType} */ - async dispose() { - await this.model.dispose(); - } - } - class TextClassificationPipeline extends /** @type {new (options: TextPipelineConstructorArgs) => TextClassificationPipelineType} */ - Pipeline { - /** - * Create a new TextClassificationPipeline. - * @param {TextPipelineConstructorArgs} options An object used to instantiate the pipeline. - */ - constructor(options) { - super(options); - } - /** @type {TextClassificationPipelineCallback} */ - async _call(texts, { - top_k = 1 - } = {}) { - const model_inputs = this.tokenizer(texts, { - padding: true, - truncation: true - }); - const outputs = await this.model(model_inputs); - const function_to_apply = this.model.config.problem_type === "multi_label_classification" ? (batch) => batch.sigmoid() : (batch) => new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_7__.Tensor( - "float32", - (0, _utils_maths_js__WEBPACK_IMPORTED_MODULE_5__.softmax)(batch.data), - batch.dims - ); - const id2label = this.model.config.id2label; - const toReturn = []; - for (const batch of outputs.logits) { - const output = function_to_apply(batch); - const scores = await (0, _utils_tensor_js__WEBPACK_IMPORTED_MODULE_7__.topk)(output, top_k); - const values = scores[0].tolist(); - const indices = scores[1].tolist(); - const vals = indices.map((x, i) => ({ - label: id2label ? id2label[x] : `LABEL_${x}`, - score: values[i] - })); - if (top_k === 1) { - toReturn.push(...vals); - } else { - toReturn.push(vals); - } - } - return Array.isArray(texts) || top_k === 1 ? ( - /** @type {TextClassificationOutput} */ - toReturn - ) : ( - /** @type {TextClassificationOutput[]} */ - toReturn[0] - ); - } - } - class TokenClassificationPipeline extends /** @type {new (options: TextPipelineConstructorArgs) => TokenClassificationPipelineType} */ - Pipeline { - /** - * Create a new TokenClassificationPipeline. - * @param {TextPipelineConstructorArgs} options An object used to instantiate the pipeline. - */ - constructor(options) { - super(options); - } - /** @type {TokenClassificationPipelineCallback} */ - async _call(texts, { - ignore_labels = ["O"] - } = {}) { - const isBatched = Array.isArray(texts); - const model_inputs = this.tokenizer(isBatched ? texts : [texts], { - padding: true, - truncation: true - }); - const outputs = await this.model(model_inputs); - const logits = outputs.logits; - const id2label = this.model.config.id2label; - const toReturn = []; - for (let i = 0; i < logits.dims[0]; ++i) { - const ids = model_inputs.input_ids[i]; - const batch = logits[i]; - const tokens = []; - for (let j = 0; j < batch.dims[0]; ++j) { - const tokenData = batch[j]; - const topScoreIndex = (0, _utils_maths_js__WEBPACK_IMPORTED_MODULE_5__.max)(tokenData.data)[1]; - const entity = id2label ? id2label[topScoreIndex] : `LABEL_${topScoreIndex}`; - if (ignore_labels.includes(entity)) { - continue; - } - const word = this.tokenizer.decode([ids[j].item()], { skip_special_tokens: true }); - if (word === "") { - continue; - } - const scores = (0, _utils_maths_js__WEBPACK_IMPORTED_MODULE_5__.softmax)(tokenData.data); - tokens.push({ - entity, - score: scores[topScoreIndex], - index: j, - word - // TODO: Add support for start and end - // start: null, - // end: null, - }); - } - toReturn.push(tokens); - } - return isBatched ? toReturn : toReturn[0]; - } - } - class QuestionAnsweringPipeline extends /** @type {new (options: TextPipelineConstructorArgs) => QuestionAnsweringPipelineType} */ - Pipeline { - /** - * Create a new QuestionAnsweringPipeline. - * @param {TextPipelineConstructorArgs} options An object used to instantiate the pipeline. - */ - constructor(options) { - super(options); - } - /** @type {QuestionAnsweringPipelineCallback} */ - async _call(question, context, { - top_k = 1 - } = {}) { - const inputs = this.tokenizer(question, { - text_pair: context, - padding: true, - truncation: true - }); - const { start_logits, end_logits } = await this.model(inputs); - const input_ids = inputs.input_ids.tolist(); - const attention_mask = inputs.attention_mask.tolist(); - const special_tokens = this.tokenizer.all_special_ids; - const toReturn = []; - for (let j = 0; j < start_logits.dims[0]; ++j) { - const ids = input_ids[j]; - const sepIndex = ids.findIndex( - (x) => ( - // We use == to match bigint with number - // @ts-ignore - x == this.tokenizer.sep_token_id - ) - ); - const valid_mask = attention_mask[j].map((y, ix) => y == 1 && (ix === 0 || ix > sepIndex && special_tokens.findIndex((x) => x == ids[ix]) === -1)); - const start = start_logits[j].tolist(); - const end = end_logits[j].tolist(); - for (let i = 1; i < start.length; ++i) { - if (attention_mask[j] == 0 || i <= sepIndex || special_tokens.findIndex((x) => x == ids[i]) !== -1) { - start[i] = -Infinity; - end[i] = -Infinity; - } - } - const start_scores = (0, _utils_maths_js__WEBPACK_IMPORTED_MODULE_5__.softmax)(start).map((x, i) => [x, i]); - const end_scores = (0, _utils_maths_js__WEBPACK_IMPORTED_MODULE_5__.softmax)(end).map((x, i) => [x, i]); - start_scores[0][0] = 0; - end_scores[0][0] = 0; - const options = (0, _utils_core_js__WEBPACK_IMPORTED_MODULE_4__.product)(start_scores, end_scores).filter((x) => x[0][1] <= x[1][1]).map((x) => [x[0][1], x[1][1], x[0][0] * x[1][0]]).sort((a, b) => b[2] - a[2]); - for (let k = 0; k < Math.min(options.length, top_k); ++k) { - const [start2, end2, score] = options[k]; - const answer_tokens = ids.slice(start2, end2 + 1); - const answer = this.tokenizer.decode(answer_tokens, { - skip_special_tokens: true - }); - toReturn.push({ - answer, - score - }); - } - } - return top_k === 1 ? toReturn[0] : toReturn; - } - } - class FillMaskPipeline extends /** @type {new (options: TextPipelineConstructorArgs) => FillMaskPipelineType} */ - Pipeline { - /** - * Create a new FillMaskPipeline. - * @param {TextPipelineConstructorArgs} options An object used to instantiate the pipeline. - */ - constructor(options) { - super(options); - } - /** @type {FillMaskPipelineCallback} */ - async _call(texts, { - top_k = 5 - } = {}) { - const model_inputs = this.tokenizer(texts, { - padding: true, - truncation: true - }); - const { logits } = await this.model(model_inputs); - const toReturn = []; - const input_ids = model_inputs.input_ids.tolist(); - for (let i = 0; i < input_ids.length; ++i) { - const ids = input_ids[i]; - const mask_token_index = ids.findIndex( - (x) => ( - // We use == to match bigint with number - // @ts-ignore - x == this.tokenizer.mask_token_id - ) - ); - if (mask_token_index === -1) { - throw Error(`Mask token (${this.tokenizer.mask_token}) not found in text.`); - } - const itemLogits = logits[i][mask_token_index]; - const scores = await (0, _utils_tensor_js__WEBPACK_IMPORTED_MODULE_7__.topk)(new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_7__.Tensor( - "float32", - (0, _utils_maths_js__WEBPACK_IMPORTED_MODULE_5__.softmax)(itemLogits.data), - itemLogits.dims - ), top_k); - const values = scores[0].tolist(); - const indices = scores[1].tolist(); - toReturn.push(indices.map((x, i2) => { - const sequence = ids.slice(); - sequence[mask_token_index] = x; - return { - score: values[i2], - token: Number(x), - token_str: this.tokenizer.model.vocab[x], - sequence: this.tokenizer.decode(sequence, { skip_special_tokens: true }) - }; - })); - } - return Array.isArray(texts) ? toReturn : toReturn[0]; - } - } - class Text2TextGenerationPipeline extends /** @type {new (options: TextPipelineConstructorArgs) => Text2TextGenerationPipelineType} */ - Pipeline { - /** - * Create a new Text2TextGenerationPipeline. - * @param {TextPipelineConstructorArgs} options An object used to instantiate the pipeline. - */ - constructor(options) { - super(options); - /** @type {'generated_text'} */ - __publicField(this, "_key", "generated_text"); - } - /** @type {Text2TextGenerationPipelineCallback} */ - async _call(texts, generate_kwargs = {}) { - if (!Array.isArray(texts)) { - texts = [texts]; - } - if (this.model.config.prefix) { - texts = texts.map((x) => this.model.config.prefix + x); - } - const task_specific_params = this.model.config.task_specific_params; - if (task_specific_params && task_specific_params[this.task]) { - if (task_specific_params[this.task].prefix) { - texts = texts.map((x) => task_specific_params[this.task].prefix + x); - } - } - const tokenizer = this.tokenizer; - const tokenizer_options = { - padding: true, - truncation: true - }; - let inputs; - if (this instanceof TranslationPipeline && "_build_translation_inputs" in tokenizer) { - inputs = tokenizer._build_translation_inputs(texts, tokenizer_options, generate_kwargs); - } else { - inputs = tokenizer(texts, tokenizer_options); - } - const outputTokenIds = await this.model.generate({ ...inputs, ...generate_kwargs }); - return tokenizer.batch_decode( - /** @type {Tensor} */ - outputTokenIds, - { - skip_special_tokens: true - } - ).map((text) => ({ [this._key]: text })); - } - } - class SummarizationPipeline extends /** @type {new (options: TextPipelineConstructorArgs) => SummarizationPipelineType} */ - /** @type {any} */ - Text2TextGenerationPipeline { - /** - * Create a new SummarizationPipeline. - * @param {TextPipelineConstructorArgs} options An object used to instantiate the pipeline. - */ - constructor(options) { - super(options); - /** @type {'summary_text'} */ - __publicField(this, "_key", "summary_text"); - } - } - class TranslationPipeline extends /** @type {new (options: TextPipelineConstructorArgs) => TranslationPipelineType} */ - /** @type {any} */ - Text2TextGenerationPipeline { - /** - * Create a new TranslationPipeline. - * @param {TextPipelineConstructorArgs} options An object used to instantiate the pipeline. - */ - constructor(options) { - super(options); - /** @type {'translation_text'} */ - __publicField(this, "_key", "translation_text"); - } - } - function isChat(x) { - return Array.isArray(x) && x.every((x2) => "role" in x2 && "content" in x2); - } - class TextGenerationPipeline extends /** @type {new (options: TextPipelineConstructorArgs) => TextGenerationPipelineType} */ - Pipeline { - /** - * Create a new TextGenerationPipeline. - * @param {TextPipelineConstructorArgs} options An object used to instantiate the pipeline. - */ - constructor(options) { - super(options); - } - /** @type {TextGenerationPipelineCallback} */ - async _call(texts, generate_kwargs = {}) { - let isBatched = false; - let isChatInput = false; - let inputs; - if (typeof texts === "string") { - inputs = texts = [texts]; - } else if (Array.isArray(texts) && texts.every((x) => typeof x === "string")) { - isBatched = true; - inputs = /** @type {string[]} */ - texts; - } else { - if (isChat(texts)) { - texts = [ - /** @type {Chat} */ - texts - ]; - } else if (Array.isArray(texts) && texts.every(isChat)) { - isBatched = true; - } else { - throw new Error("Input must be a string, an array of strings, a Chat, or an array of Chats"); - } - isChatInput = true; - inputs = /** @type {string[]} */ - /** @type {Chat[]} */ - texts.map( - (x) => this.tokenizer.apply_chat_template(x, { - tokenize: false, - add_generation_prompt: true - }) - ); - } - const add_special_tokens = generate_kwargs.add_special_tokens ?? false; - const return_full_text = isChatInput ? false : generate_kwargs.return_full_text ?? true; - this.tokenizer.padding_side = "left"; - const text_inputs = this.tokenizer(inputs, { - add_special_tokens, - padding: true, - truncation: true - }); - const outputTokenIds = ( - /** @type {Tensor} */ - await this.model.generate({ - ...text_inputs, - ...generate_kwargs - }) - ); - const decoded = this.tokenizer.batch_decode(outputTokenIds, { - skip_special_tokens: true - }); - let promptLengths; - if (!return_full_text && text_inputs.input_ids.dims.at(-1) > 0) { - promptLengths = this.tokenizer.batch_decode(text_inputs.input_ids, { - skip_special_tokens: true - }).map((x) => x.length); - } - const toReturn = Array.from({ length: texts.length }, (_) => []); - for (let i = 0; i < decoded.length; ++i) { - const textIndex = Math.floor(i / outputTokenIds.dims[0] * texts.length); - if (promptLengths) { - decoded[i] = decoded[i].slice(promptLengths[textIndex]); - } - toReturn[textIndex].push({ - generated_text: isChatInput ? [ - .../** @type {Chat[]} */ - texts[textIndex], - { role: "assistant", content: decoded[i] } - ] : decoded[i] - }); - } - return !isBatched && toReturn.length === 1 ? toReturn[0] : toReturn; - } - } - class ZeroShotClassificationPipeline extends /** @type {new (options: TextPipelineConstructorArgs) => ZeroShotClassificationPipelineType} */ - Pipeline { - /** - * Create a new ZeroShotClassificationPipeline. - * @param {TextPipelineConstructorArgs} options An object used to instantiate the pipeline. - */ - constructor(options) { - super(options); - this.label2id = Object.fromEntries( - Object.entries( - /** @type {any} */ - this.model.config.label2id - ).map( - ([k, v]) => [k.toLowerCase(), v] - ) - ); - this.entailment_id = this.label2id["entailment"]; - if (this.entailment_id === void 0) { - console.warn("Could not find 'entailment' in label2id mapping. Using 2 as entailment_id."); - this.entailment_id = 2; - } - this.contradiction_id = this.label2id["contradiction"] ?? this.label2id["not_entailment"]; - if (this.contradiction_id === void 0) { - console.warn("Could not find 'contradiction' in label2id mapping. Using 0 as contradiction_id."); - this.contradiction_id = 0; - } - } - /** @type {ZeroShotClassificationPipelineCallback} */ - async _call(texts, candidate_labels, { - hypothesis_template = "This example is {}.", - multi_label = false - } = {}) { - const isBatched = Array.isArray(texts); - if (!isBatched) { - texts = [ - /** @type {string} */ - texts - ]; - } - if (!Array.isArray(candidate_labels)) { - candidate_labels = [candidate_labels]; - } - const hypotheses = candidate_labels.map( - (x) => hypothesis_template.replace("{}", x) - ); - const softmaxEach = multi_label || candidate_labels.length === 1; - const toReturn = []; - for (const premise of texts) { - const entails_logits = []; - for (const hypothesis of hypotheses) { - const inputs = this.tokenizer(premise, { - text_pair: hypothesis, - padding: true, - truncation: true - }); - const outputs = await this.model(inputs); - if (softmaxEach) { - entails_logits.push([ - outputs.logits.data[this.contradiction_id], - outputs.logits.data[this.entailment_id] - ]); - } else { - entails_logits.push(outputs.logits.data[this.entailment_id]); - } - } - const scores = softmaxEach ? entails_logits.map((x) => (0, _utils_maths_js__WEBPACK_IMPORTED_MODULE_5__.softmax)(x)[1]) : (0, _utils_maths_js__WEBPACK_IMPORTED_MODULE_5__.softmax)(entails_logits); - const scores_sorted = scores.map((x, i) => [x, i]).sort((a, b) => b[0] - a[0]); - toReturn.push({ - sequence: premise, - labels: scores_sorted.map((x) => candidate_labels[x[1]]), - scores: scores_sorted.map((x) => x[0]) - }); - } - return isBatched ? toReturn : toReturn[0]; - } - } - class FeatureExtractionPipeline extends /** @type {new (options: TextPipelineConstructorArgs) => FeatureExtractionPipelineType} */ - Pipeline { - /** - * Create a new FeatureExtractionPipeline. - * @param {TextPipelineConstructorArgs} options An object used to instantiate the pipeline. - */ - constructor(options) { - super(options); - } - /** @type {FeatureExtractionPipelineCallback} */ - async _call(texts, { - pooling = ( - /** @type {'none'} */ - "none" - ), - normalize = false, - quantize = false, - precision = ( - /** @type {'binary'} */ - "binary" - ) - } = {}) { - const model_inputs = this.tokenizer(texts, { - padding: true, - truncation: true - }); - const outputs = await this.model(model_inputs); - let result = outputs.last_hidden_state ?? outputs.logits ?? outputs.token_embeddings; - if (pooling === "none") { - } else if (pooling === "mean") { - result = (0, _utils_tensor_js__WEBPACK_IMPORTED_MODULE_7__.mean_pooling)(result, model_inputs.attention_mask); - } else if (pooling === "cls") { - result = result.slice(null, 0); - } else { - throw Error(`Pooling method '${pooling}' not supported.`); - } - if (normalize) { - result = result.normalize(2, -1); - } - if (quantize) { - result = (0, _utils_tensor_js__WEBPACK_IMPORTED_MODULE_7__.quantize_embeddings)(result, precision); - } - return result; - } - } - class ImageFeatureExtractionPipeline extends /** @type {new (options: ImagePipelineConstructorArgs) => ImageFeatureExtractionPipelineType} */ - Pipeline { - /** - * Create a new ImageFeatureExtractionPipeline. - * @param {ImagePipelineConstructorArgs} options An object used to instantiate the pipeline. - */ - constructor(options) { - super(options); - } - /** @type {ImageFeatureExtractionPipelineCallback} */ - async _call(images, { - pool = null - } = {}) { - const preparedImages = await prepareImages(images); - const { pixel_values } = await this.processor(preparedImages); - const outputs = await this.model({ pixel_values }); - let result; - if (pool) { - if (!("pooler_output" in outputs)) { - throw Error(`No pooled output was returned. Make sure the model has a 'pooler' layer when using the 'pool' option.`); - } - result = outputs.pooler_output; - } else { - result = outputs.last_hidden_state ?? outputs.logits ?? outputs.image_embeds; - } - return result; - } - } - class AudioClassificationPipeline extends /** @type {new (options: AudioPipelineConstructorArgs) => AudioClassificationPipelineType} */ - Pipeline { - /** - * Create a new AudioClassificationPipeline. - * @param {AudioPipelineConstructorArgs} options An object used to instantiate the pipeline. - */ - constructor(options) { - super(options); - } - /** @type {AudioClassificationPipelineCallback} */ - async _call(audio, { - top_k = 5 - } = {}) { - const sampling_rate = this.processor.feature_extractor.config.sampling_rate; - const preparedAudios = await prepareAudios(audio, sampling_rate); - const id2label = this.model.config.id2label; - const toReturn = []; - for (const aud of preparedAudios) { - const inputs = await this.processor(aud); - const output = await this.model(inputs); - const logits = output.logits[0]; - const scores = await (0, _utils_tensor_js__WEBPACK_IMPORTED_MODULE_7__.topk)(new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_7__.Tensor( - "float32", - (0, _utils_maths_js__WEBPACK_IMPORTED_MODULE_5__.softmax)(logits.data), - logits.dims - ), top_k); - const values = scores[0].tolist(); - const indices = scores[1].tolist(); - const vals = indices.map((x, i) => ({ - label: ( - /** @type {string} */ - id2label ? id2label[x] : `LABEL_${x}` - ), - score: ( - /** @type {number} */ - values[i] - ) - })); - toReturn.push(vals); - } - ; - return Array.isArray(audio) ? toReturn : toReturn[0]; - } - } - class ZeroShotAudioClassificationPipeline extends /** @type {new (options: TextAudioPipelineConstructorArgs) => ZeroShotAudioClassificationPipelineType} */ - Pipeline { - /** - * Create a new ZeroShotAudioClassificationPipeline. - * @param {TextAudioPipelineConstructorArgs} options An object used to instantiate the pipeline. - */ - constructor(options) { - super(options); - } - /** @type {ZeroShotAudioClassificationPipelineCallback} */ - async _call(audio, candidate_labels, { - hypothesis_template = "This is a sound of {}." - } = {}) { - const single = !Array.isArray(audio); - if (single) { - audio = [ - /** @type {AudioInput} */ - audio - ]; - } - const texts = candidate_labels.map( - (x) => hypothesis_template.replace("{}", x) - ); - const text_inputs = this.tokenizer(texts, { - padding: true, - truncation: true - }); - const sampling_rate = this.processor.feature_extractor.config.sampling_rate; - const preparedAudios = await prepareAudios(audio, sampling_rate); - const toReturn = []; - for (const aud of preparedAudios) { - const audio_inputs = await this.processor(aud); - const output = await this.model({ ...text_inputs, ...audio_inputs }); - const probs = (0, _utils_maths_js__WEBPACK_IMPORTED_MODULE_5__.softmax)(output.logits_per_audio.data); - toReturn.push([...probs].map((x, i) => ({ - score: x, - label: candidate_labels[i] - }))); - } - return single ? toReturn[0] : toReturn; - } - } - class AutomaticSpeechRecognitionPipeline extends /** @type {new (options: TextAudioPipelineConstructorArgs) => AutomaticSpeechRecognitionPipelineType} */ - Pipeline { - /** - * Create a new AutomaticSpeechRecognitionPipeline. - * @param {TextAudioPipelineConstructorArgs} options An object used to instantiate the pipeline. - */ - constructor(options) { - super(options); - } - /** @type {AutomaticSpeechRecognitionPipelineCallback} */ - async _call(audio, kwargs = {}) { - switch (this.model.config.model_type) { - case "whisper": - return this._call_whisper(audio, kwargs); - case "wav2vec2": - case "wav2vec2-bert": - case "unispeech": - case "unispeech-sat": - case "hubert": - return this._call_wav2vec2(audio, kwargs); - default: - throw new Error(`AutomaticSpeechRecognitionPipeline does not support model type '${this.model.config.model_type}'.`); - } - } - /** - * @type {AutomaticSpeechRecognitionPipelineCallback} - * @private - */ - async _call_wav2vec2(audio, kwargs) { - if (kwargs.language) { - console.warn('`language` parameter is not yet supported for `wav2vec2` models, defaulting to "English".'); - } - if (kwargs.task) { - console.warn('`task` parameter is not yet supported for `wav2vec2` models, defaulting to "transcribe".'); - } - const single = !Array.isArray(audio); - if (single) { - audio = [ - /** @type {AudioInput} */ - audio - ]; - } - const sampling_rate = this.processor.feature_extractor.config.sampling_rate; - const preparedAudios = await prepareAudios(audio, sampling_rate); - const toReturn = []; - for (const aud of preparedAudios) { - const inputs = await this.processor(aud); - const output = await this.model(inputs); - const logits = output.logits[0]; - const predicted_ids = []; - for (const item of logits) { - predicted_ids.push((0, _utils_maths_js__WEBPACK_IMPORTED_MODULE_5__.max)(item.data)[1]); - } - const predicted_sentences = this.tokenizer.decode(predicted_ids); - toReturn.push({ text: predicted_sentences }); - } - return single ? toReturn[0] : toReturn; - } - /** - * @type {AutomaticSpeechRecognitionPipelineCallback} - * @private - */ - async _call_whisper(audio, kwargs) { - const return_timestamps = kwargs.return_timestamps ?? false; - const chunk_length_s = kwargs.chunk_length_s ?? 0; - const force_full_sequences = kwargs.force_full_sequences ?? false; - let stride_length_s = kwargs.stride_length_s ?? null; - const generation_config = { ...kwargs }; - if (return_timestamps === "word") { - generation_config["return_token_timestamps"] = true; - generation_config["return_timestamps"] = false; - } - const single = !Array.isArray(audio); - if (single) { - audio = [ - /** @type {AudioInput} */ - audio - ]; - } - const time_precision = this.processor.feature_extractor.config.chunk_length / this.model.config.max_source_positions; - const hop_length = this.processor.feature_extractor.config.hop_length; - const sampling_rate = this.processor.feature_extractor.config.sampling_rate; - const preparedAudios = await prepareAudios(audio, sampling_rate); - const toReturn = []; - for (const aud of preparedAudios) { - let chunks = []; - if (chunk_length_s > 0) { - if (stride_length_s === null) { - stride_length_s = chunk_length_s / 6; - } else if (chunk_length_s <= stride_length_s) { - throw Error("`chunk_length_s` must be larger than `stride_length_s`."); - } - const window2 = sampling_rate * chunk_length_s; - const stride = sampling_rate * stride_length_s; - const jump = window2 - 2 * stride; - let offset = 0; - while (true) { - const offset_end = offset + window2; - const subarr = aud.subarray(offset, offset_end); - const feature = await this.processor(subarr); - const is_first = offset === 0; - const is_last = offset_end >= aud.length; - chunks.push({ - stride: [ - subarr.length, - is_first ? 0 : stride, - is_last ? 0 : stride - ], - input_features: feature.input_features, - is_last - }); - if (is_last) break; - offset += jump; - } - } else { - chunks = [{ - stride: [aud.length, 0, 0], - input_features: (await this.processor(aud)).input_features, - is_last: true - }]; - } - for (const chunk of chunks) { - generation_config.num_frames = Math.floor(chunk.stride[0] / hop_length); - const data = await this.model.generate({ - inputs: chunk.input_features, - ...generation_config - }); - if (return_timestamps === "word") { - chunk.tokens = data.sequences.tolist()[0]; - chunk.token_timestamps = data.token_timestamps.tolist()[0].map( - (x) => (0, _utils_maths_js__WEBPACK_IMPORTED_MODULE_5__.round)(x, 2) - ); - } else { - chunk.tokens = /** @type {Tensor} */ - data[0].tolist(); - } - chunk.stride = chunk.stride.map((x) => x / sampling_rate); - } - const [full_text, optional] = this.tokenizer._decode_asr(chunks, { - time_precision, - return_timestamps, - force_full_sequences - }); - toReturn.push({ text: full_text, ...optional }); - } - return single ? toReturn[0] : toReturn; - } - } - class ImageToTextPipeline extends /** @type {new (options: TextImagePipelineConstructorArgs) => ImageToTextPipelineType} */ - Pipeline { - /** - * Create a new ImageToTextPipeline. - * @param {TextImagePipelineConstructorArgs} options An object used to instantiate the pipeline. - */ - constructor(options) { - super(options); - } - /** @type {ImageToTextPipelineCallback} */ - async _call(images, generate_kwargs = {}) { - const isBatched = Array.isArray(images); - const preparedImages = await prepareImages(images); - const { pixel_values } = await this.processor(preparedImages); - const toReturn = []; - for (const batch of pixel_values) { - batch.dims = [1, ...batch.dims]; - const output = await this.model.generate({ inputs: batch, ...generate_kwargs }); - const decoded = this.tokenizer.batch_decode( - /** @type {Tensor} */ - output, - { - skip_special_tokens: true - } - ).map((x) => ({ generated_text: x.trim() })); - toReturn.push(decoded); - } - return isBatched ? toReturn : toReturn[0]; - } - } - class ImageClassificationPipeline extends /** @type {new (options: ImagePipelineConstructorArgs) => ImageClassificationPipelineType} */ - Pipeline { - /** - * Create a new ImageClassificationPipeline. - * @param {ImagePipelineConstructorArgs} options An object used to instantiate the pipeline. - */ - constructor(options) { - super(options); - } - /** @type {ImageClassificationPipelineCallback} */ - async _call(images, { - top_k = 5 - } = {}) { - const preparedImages = await prepareImages(images); - const { pixel_values } = await this.processor(preparedImages); - const output = await this.model({ pixel_values }); - const id2label = this.model.config.id2label; - const toReturn = []; - for (const batch of output.logits) { - const scores = await (0, _utils_tensor_js__WEBPACK_IMPORTED_MODULE_7__.topk)(new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_7__.Tensor( - "float32", - (0, _utils_maths_js__WEBPACK_IMPORTED_MODULE_5__.softmax)(batch.data), - batch.dims - ), top_k); - const values = scores[0].tolist(); - const indices = scores[1].tolist(); - const vals = indices.map((x, i) => ({ - label: ( - /** @type {string} */ - id2label ? id2label[x] : `LABEL_${x}` - ), - score: ( - /** @type {number} */ - values[i] - ) - })); - toReturn.push(vals); - } - return Array.isArray(images) ? toReturn : toReturn[0]; - } - } - class ImageSegmentationPipeline extends /** @type {new (options: ImagePipelineConstructorArgs) => ImageSegmentationPipelineType} */ - Pipeline { - /** - * Create a new ImageSegmentationPipeline. - * @param {ImagePipelineConstructorArgs} options An object used to instantiate the pipeline. - */ - constructor(options) { - super(options); - this.subtasks_mapping = { - // Mapping of subtasks to their corresponding post-processing function names. - panoptic: "post_process_panoptic_segmentation", - instance: "post_process_instance_segmentation", - semantic: "post_process_semantic_segmentation" - }; - } - /** @type {ImageSegmentationPipelineCallback} */ - async _call(images, { - threshold = 0.5, - mask_threshold = 0.5, - overlap_mask_area_threshold = 0.8, - label_ids_to_fuse = null, - target_sizes = null, - subtask = null - } = {}) { - const isBatched = Array.isArray(images); - if (isBatched && images.length !== 1) { - throw Error("Image segmentation pipeline currently only supports a batch size of 1."); - } - const preparedImages = await prepareImages(images); - const imageSizes = preparedImages.map((x) => [x.height, x.width]); - const { pixel_values, pixel_mask } = await this.processor(preparedImages); - const output = await this.model({ pixel_values, pixel_mask }); - let fn = null; - if (subtask !== null) { - fn = this.subtasks_mapping[subtask]; - } else { - for (let [task, func] of Object.entries(this.subtasks_mapping)) { - if (func in this.processor.feature_extractor) { - fn = this.processor.feature_extractor[func].bind(this.processor.feature_extractor); - subtask = task; - break; - } - } - } - const id2label = this.model.config.id2label; - const annotation = []; - if (subtask === "panoptic" || subtask === "instance") { - const processed = fn( - output, - threshold, - mask_threshold, - overlap_mask_area_threshold, - label_ids_to_fuse, - target_sizes ?? imageSizes - // TODO FIX? - )[0]; - const segmentation = processed.segmentation; - for (const segment of processed.segments_info) { - const maskData = new Uint8ClampedArray(segmentation.data.length); - for (let i = 0; i < segmentation.data.length; ++i) { - if (segmentation.data[i] === segment.id) { - maskData[i] = 255; - } - } - const mask = new _utils_image_js__WEBPACK_IMPORTED_MODULE_8__.RawImage(maskData, segmentation.dims[1], segmentation.dims[0], 1); - annotation.push({ - score: segment.score, - label: id2label[segment.label_id], - mask - }); - } - } else if (subtask === "semantic") { - const { segmentation, labels } = fn(output, target_sizes ?? imageSizes)[0]; - for (const label of labels) { - const maskData = new Uint8ClampedArray(segmentation.data.length); - for (let i = 0; i < segmentation.data.length; ++i) { - if (segmentation.data[i] === label) { - maskData[i] = 255; - } - } - const mask = new _utils_image_js__WEBPACK_IMPORTED_MODULE_8__.RawImage(maskData, segmentation.dims[1], segmentation.dims[0], 1); - annotation.push({ - score: null, - label: id2label[label], - mask - }); - } - } else { - throw Error(`Subtask ${subtask} not supported.`); - } - return annotation; - } - } - class ZeroShotImageClassificationPipeline extends /** @type {new (options: TextImagePipelineConstructorArgs) => ZeroShotImageClassificationPipelineType} */ - Pipeline { - /** - * Create a new ZeroShotImageClassificationPipeline. - * @param {TextImagePipelineConstructorArgs} options An object used to instantiate the pipeline. - */ - constructor(options) { - super(options); - } - /** @type {ZeroShotImageClassificationPipelineCallback} */ - async _call(images, candidate_labels, { - hypothesis_template = "This is a photo of {}" - } = {}) { - const isBatched = Array.isArray(images); - const preparedImages = await prepareImages(images); - const texts = candidate_labels.map( - (x) => hypothesis_template.replace("{}", x) - ); - const text_inputs = this.tokenizer(texts, { - padding: this.model.config.model_type === "siglip" ? "max_length" : true, - truncation: true - }); - const { pixel_values } = await this.processor(preparedImages); - const output = await this.model({ ...text_inputs, pixel_values }); - const function_to_apply = this.model.config.model_type === "siglip" ? (batch) => batch.sigmoid().data : (batch) => (0, _utils_maths_js__WEBPACK_IMPORTED_MODULE_5__.softmax)(batch.data); - const toReturn = []; - for (const batch of output.logits_per_image) { - const probs = function_to_apply(batch); - const result = [...probs].map((x, i) => ({ - score: x, - label: candidate_labels[i] - })); - result.sort((a, b) => b.score - a.score); - toReturn.push(result); - } - return isBatched ? toReturn : toReturn[0]; - } - } - class ObjectDetectionPipeline extends /** @type {new (options: ImagePipelineConstructorArgs) => ObjectDetectionPipelineType} */ - Pipeline { - /** - * Create a new ObjectDetectionPipeline. - * @param {ImagePipelineConstructorArgs} options An object used to instantiate the pipeline. - */ - constructor(options) { - super(options); - } - /** @type {ObjectDetectionPipelineCallback} */ - async _call(images, { - threshold = 0.9, - percentage = false - } = {}) { - const isBatched = Array.isArray(images); - if (isBatched && images.length !== 1) { - throw Error("Object detection pipeline currently only supports a batch size of 1."); - } - const preparedImages = await prepareImages(images); - const imageSizes = percentage ? null : preparedImages.map((x) => [x.height, x.width]); - const { pixel_values, pixel_mask } = await this.processor(preparedImages); - const output = await this.model({ pixel_values, pixel_mask }); - const processed = this.processor.feature_extractor.post_process_object_detection(output, threshold, imageSizes); - const id2label = this.model.config.id2label; - const result = processed.map((batch) => batch.boxes.map((box, i) => ({ - score: batch.scores[i], - label: id2label[batch.classes[i]], - box: get_bounding_box(box, !percentage) - }))); - return isBatched ? result : result[0]; - } - } - class ZeroShotObjectDetectionPipeline extends /** @type {new (options: TextImagePipelineConstructorArgs) => ZeroShotObjectDetectionPipelineType} */ - Pipeline { - /** - * Create a new ZeroShotObjectDetectionPipeline. - * @param {TextImagePipelineConstructorArgs} options An object used to instantiate the pipeline. - */ - constructor(options) { - super(options); - } - /** @type {ZeroShotObjectDetectionPipelineCallback} */ - async _call(images, candidate_labels, { - threshold = 0.1, - top_k = null, - percentage = false - } = {}) { - const isBatched = Array.isArray(images); - const preparedImages = await prepareImages(images); - const text_inputs = this.tokenizer(candidate_labels, { - padding: true, - truncation: true - }); - const model_inputs = await this.processor(preparedImages); - const toReturn = []; - for (let i = 0; i < preparedImages.length; ++i) { - const image = preparedImages[i]; - const imageSize = percentage ? null : [[image.height, image.width]]; - const pixel_values = model_inputs.pixel_values[i].unsqueeze_(0); - const output = await this.model({ ...text_inputs, pixel_values }); - const processed = this.processor.feature_extractor.post_process_object_detection(output, threshold, imageSize, true)[0]; - let result = processed.boxes.map((box, i2) => ({ - score: processed.scores[i2], - label: candidate_labels[processed.classes[i2]], - box: get_bounding_box(box, !percentage) - })).sort((a, b) => b.score - a.score); - if (top_k !== null) { - result = result.slice(0, top_k); - } - toReturn.push(result); - } - return isBatched ? toReturn : toReturn[0]; - } - } - class DocumentQuestionAnsweringPipeline extends /** @type {new (options: TextImagePipelineConstructorArgs) => DocumentQuestionAnsweringPipelineType} */ - Pipeline { - /** - * Create a new DocumentQuestionAnsweringPipeline. - * @param {TextImagePipelineConstructorArgs} options An object used to instantiate the pipeline. - */ - constructor(options) { - super(options); - } - /** @type {DocumentQuestionAnsweringPipelineCallback} */ - async _call(image, question, generate_kwargs = {}) { - throw new Error("This pipeline is not yet supported in Transformers.js v3."); - const preparedImage = (await prepareImages(image))[0]; - const { pixel_values } = await this.processor(preparedImage); - const task_prompt = `${question}`; - const decoder_input_ids = this.tokenizer(task_prompt, { - add_special_tokens: false, - padding: true, - truncation: true - }).input_ids; - const output = await this.model.generate({ - inputs: pixel_values, - max_length: this.model.config.decoder.max_position_embeddings, - decoder_input_ids, - ...generate_kwargs - }); - const decoded = this.tokenizer.batch_decode( - /** @type {Tensor} */ - output - )[0]; - const match = decoded.match(/(.*?)<\/s_answer>/); - let answer = null; - if (match && match.length >= 2) { - answer = match[1].trim(); - } - return [{ answer }]; - } - } - class TextToAudioPipeline extends /** @type {new (options: TextToAudioPipelineConstructorArgs) => TextToAudioPipelineType} */ - Pipeline { - /** - * Create a new TextToAudioPipeline. - * @param {TextToAudioPipelineConstructorArgs} options An object used to instantiate the pipeline. - */ - constructor(options) { - super(options); - __publicField(this, "DEFAULT_VOCODER_ID", "Xenova/speecht5_hifigan"); - this.vocoder = options.vocoder ?? null; - } - /** @type {TextToAudioPipelineCallback} */ - async _call(text_inputs, { - speaker_embeddings = null - } = {}) { - if (this.processor) { - return this._call_text_to_spectrogram(text_inputs, { speaker_embeddings }); - } else { - return this._call_text_to_waveform(text_inputs); - } - } - async _call_text_to_waveform(text_inputs) { - const inputs = this.tokenizer(text_inputs, { - padding: true, - truncation: true - }); - const { waveform } = await this.model(inputs); - const sampling_rate = this.model.config.sampling_rate; - return { - audio: waveform.data, - sampling_rate - }; - } - async _call_text_to_spectrogram(text_inputs, { speaker_embeddings }) { - if (!this.vocoder) { - console.log("No vocoder specified, using default HifiGan vocoder."); - this.vocoder = await _models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModel.from_pretrained(this.DEFAULT_VOCODER_ID, { dtype: "fp32" }); - } - if (typeof speaker_embeddings === "string" || speaker_embeddings instanceof URL) { - speaker_embeddings = new Float32Array( - await (await fetch(speaker_embeddings)).arrayBuffer() - ); - } - if (speaker_embeddings instanceof Float32Array) { - speaker_embeddings = new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_7__.Tensor( - "float32", - speaker_embeddings, - [1, speaker_embeddings.length] - ); - } else if (!(speaker_embeddings instanceof _utils_tensor_js__WEBPACK_IMPORTED_MODULE_7__.Tensor)) { - throw new Error("Speaker embeddings must be a `Tensor`, `Float32Array`, `string`, or `URL`."); - } - const { input_ids } = this.tokenizer(text_inputs, { - padding: true, - truncation: true - }); - const { waveform } = await this.model.generate_speech(input_ids, speaker_embeddings, { vocoder: this.vocoder }); - const sampling_rate = this.processor.feature_extractor.config.sampling_rate; - return { - audio: waveform.data, - sampling_rate - }; - } - } - class ImageToImagePipeline extends /** @type {new (options: ImagePipelineConstructorArgs) => ImageToImagePipelineType} */ - Pipeline { - /** - * Create a new ImageToImagePipeline. - * @param {ImagePipelineConstructorArgs} options An object used to instantiate the pipeline. - */ - constructor(options) { - super(options); - } - /** @type {ImageToImagePipelineCallback} */ - async _call(images) { - const preparedImages = await prepareImages(images); - const inputs = await this.processor(preparedImages); - const outputs = await this.model(inputs); - const toReturn = []; - for (const batch of outputs.reconstruction) { - const output = batch.squeeze().clamp_(0, 1).mul_(255).round_().to("uint8"); - toReturn.push(_utils_image_js__WEBPACK_IMPORTED_MODULE_8__.RawImage.fromTensor(output)); - } - return toReturn.length > 1 ? toReturn : toReturn[0]; - } - } - class DepthEstimationPipeline extends /** @type {new (options: ImagePipelineConstructorArgs) => DepthEstimationPipelineType} */ - Pipeline { - /** - * Create a new DepthEstimationPipeline. - * @param {ImagePipelineConstructorArgs} options An object used to instantiate the pipeline. - */ - constructor(options) { - super(options); - } - /** @type {DepthEstimationPipelineCallback} */ - async _call(images) { - const preparedImages = await prepareImages(images); - const inputs = await this.processor(preparedImages); - const { predicted_depth } = await this.model(inputs); - const toReturn = []; - for (let i = 0; i < preparedImages.length; ++i) { - const prediction = (0, _utils_tensor_js__WEBPACK_IMPORTED_MODULE_7__.interpolate)(predicted_depth[i], preparedImages[i].size.reverse(), "bilinear", false); - const formatted = prediction.mul_(255 / (0, _utils_maths_js__WEBPACK_IMPORTED_MODULE_5__.max)(prediction.data)[0]).to("uint8"); - toReturn.push({ - predicted_depth: predicted_depth[i], - depth: _utils_image_js__WEBPACK_IMPORTED_MODULE_8__.RawImage.fromTensor(formatted) - }); - } - return toReturn.length > 1 ? toReturn : toReturn[0]; - } - } - const SUPPORTED_TASKS = Object.freeze({ - "text-classification": { - "tokenizer": _tokenizers_js__WEBPACK_IMPORTED_MODULE_0__.AutoTokenizer, - "pipeline": TextClassificationPipeline, - "model": _models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModelForSequenceClassification, - "default": { - // TODO: replace with original - // "model": "distilbert-base-uncased-finetuned-sst-2-english", - "model": "Xenova/distilbert-base-uncased-finetuned-sst-2-english" - }, - "type": "text" - }, - "token-classification": { - "tokenizer": _tokenizers_js__WEBPACK_IMPORTED_MODULE_0__.AutoTokenizer, - "pipeline": TokenClassificationPipeline, - "model": _models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModelForTokenClassification, - "default": { - // TODO: replace with original - // "model": "Davlan/bert-base-multilingual-cased-ner-hrl", - "model": "Xenova/bert-base-multilingual-cased-ner-hrl" - }, - "type": "text" - }, - "question-answering": { - "tokenizer": _tokenizers_js__WEBPACK_IMPORTED_MODULE_0__.AutoTokenizer, - "pipeline": QuestionAnsweringPipeline, - "model": _models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModelForQuestionAnswering, - "default": { - // TODO: replace with original - // "model": "distilbert-base-cased-distilled-squad", - "model": "Xenova/distilbert-base-cased-distilled-squad" - }, - "type": "text" - }, - "fill-mask": { - "tokenizer": _tokenizers_js__WEBPACK_IMPORTED_MODULE_0__.AutoTokenizer, - "pipeline": FillMaskPipeline, - "model": _models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModelForMaskedLM, - "default": { - // TODO: replace with original - // "model": "bert-base-uncased", - "model": "Xenova/bert-base-uncased" - }, - "type": "text" - }, - "summarization": { - "tokenizer": _tokenizers_js__WEBPACK_IMPORTED_MODULE_0__.AutoTokenizer, - "pipeline": SummarizationPipeline, - "model": _models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModelForSeq2SeqLM, - "default": { - // TODO: replace with original - // "model": "sshleifer/distilbart-cnn-6-6", - "model": "Xenova/distilbart-cnn-6-6" - }, - "type": "text" - }, - "translation": { - "tokenizer": _tokenizers_js__WEBPACK_IMPORTED_MODULE_0__.AutoTokenizer, - "pipeline": TranslationPipeline, - "model": _models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModelForSeq2SeqLM, - "default": { - // TODO: replace with original - // "model": "t5-small", - "model": "Xenova/t5-small" - }, - "type": "text" - }, - "text2text-generation": { - "tokenizer": _tokenizers_js__WEBPACK_IMPORTED_MODULE_0__.AutoTokenizer, - "pipeline": Text2TextGenerationPipeline, - "model": _models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModelForSeq2SeqLM, - "default": { - // TODO: replace with original - // "model": "google/flan-t5-small", - "model": "Xenova/flan-t5-small" - }, - "type": "text" - }, - "text-generation": { - "tokenizer": _tokenizers_js__WEBPACK_IMPORTED_MODULE_0__.AutoTokenizer, - "pipeline": TextGenerationPipeline, - "model": _models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModelForCausalLM, - "default": { - // TODO: replace with original - // "model": "gpt2", - "model": "Xenova/gpt2" - }, - "type": "text" - }, - "zero-shot-classification": { - "tokenizer": _tokenizers_js__WEBPACK_IMPORTED_MODULE_0__.AutoTokenizer, - "pipeline": ZeroShotClassificationPipeline, - "model": _models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModelForSequenceClassification, - "default": { - // TODO: replace with original - // "model": "typeform/distilbert-base-uncased-mnli", - "model": "Xenova/distilbert-base-uncased-mnli" - }, - "type": "text" - }, - "audio-classification": { - "pipeline": AudioClassificationPipeline, - "model": _models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModelForAudioClassification, - "processor": _processors_js__WEBPACK_IMPORTED_MODULE_2__.AutoProcessor, - "default": { - // TODO: replace with original - // "model": "superb/wav2vec2-base-superb-ks", - "model": "Xenova/wav2vec2-base-superb-ks" - }, - "type": "audio" - }, - "zero-shot-audio-classification": { - "tokenizer": _tokenizers_js__WEBPACK_IMPORTED_MODULE_0__.AutoTokenizer, - "pipeline": ZeroShotAudioClassificationPipeline, - "model": _models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModel, - "processor": _processors_js__WEBPACK_IMPORTED_MODULE_2__.AutoProcessor, - "default": { - // TODO: replace with original - // "model": "laion/clap-htsat-fused", - "model": "Xenova/clap-htsat-unfused" - }, - "type": "multimodal" - }, - "automatic-speech-recognition": { - "tokenizer": _tokenizers_js__WEBPACK_IMPORTED_MODULE_0__.AutoTokenizer, - "pipeline": AutomaticSpeechRecognitionPipeline, - "model": [_models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModelForSpeechSeq2Seq, _models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModelForCTC], - "processor": _processors_js__WEBPACK_IMPORTED_MODULE_2__.AutoProcessor, - "default": { - // TODO: replace with original - // "model": "openai/whisper-tiny.en", - "model": "Xenova/whisper-tiny.en" - }, - "type": "multimodal" - }, - "text-to-audio": { - "tokenizer": _tokenizers_js__WEBPACK_IMPORTED_MODULE_0__.AutoTokenizer, - "pipeline": TextToAudioPipeline, - "model": [_models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModelForTextToWaveform, _models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModelForTextToSpectrogram], - "processor": [ - _processors_js__WEBPACK_IMPORTED_MODULE_2__.AutoProcessor, - /* Some don't use a processor */ - null - ], - "default": { - // TODO: replace with original - // "model": "microsoft/speecht5_tts", - "model": "Xenova/speecht5_tts" - }, - "type": "text" - }, - "image-to-text": { - "tokenizer": _tokenizers_js__WEBPACK_IMPORTED_MODULE_0__.AutoTokenizer, - "pipeline": ImageToTextPipeline, - "model": _models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModelForVision2Seq, - "processor": _processors_js__WEBPACK_IMPORTED_MODULE_2__.AutoProcessor, - "default": { - // TODO: replace with original - // "model": "nlpconnect/vit-gpt2-image-captioning", - "model": "Xenova/vit-gpt2-image-captioning" - }, - "type": "multimodal" - }, - "image-classification": { - // no tokenizer - "pipeline": ImageClassificationPipeline, - "model": _models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModelForImageClassification, - "processor": _processors_js__WEBPACK_IMPORTED_MODULE_2__.AutoProcessor, - "default": { - // TODO: replace with original - // "model": "google/vit-base-patch16-224", - "model": "Xenova/vit-base-patch16-224" - }, - "type": "multimodal" - }, - "image-segmentation": { - // no tokenizer - "pipeline": ImageSegmentationPipeline, - "model": [_models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModelForImageSegmentation, _models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModelForSemanticSegmentation, _models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModelForUniversalSegmentation], - "processor": _processors_js__WEBPACK_IMPORTED_MODULE_2__.AutoProcessor, - "default": { - // TODO: replace with original - // "model": "facebook/detr-resnet-50-panoptic", - "model": "Xenova/detr-resnet-50-panoptic" - }, - "type": "multimodal" - }, - "zero-shot-image-classification": { - "tokenizer": _tokenizers_js__WEBPACK_IMPORTED_MODULE_0__.AutoTokenizer, - "pipeline": ZeroShotImageClassificationPipeline, - "model": _models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModel, - "processor": _processors_js__WEBPACK_IMPORTED_MODULE_2__.AutoProcessor, - "default": { - // TODO: replace with original - // "model": "openai/clip-vit-base-patch32", - "model": "Xenova/clip-vit-base-patch32" - }, - "type": "multimodal" - }, - "object-detection": { - // no tokenizer - "pipeline": ObjectDetectionPipeline, - "model": _models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModelForObjectDetection, - "processor": _processors_js__WEBPACK_IMPORTED_MODULE_2__.AutoProcessor, - "default": { - // TODO: replace with original - // "model": "facebook/detr-resnet-50", - "model": "Xenova/detr-resnet-50" - }, - "type": "multimodal" - }, - "zero-shot-object-detection": { - "tokenizer": _tokenizers_js__WEBPACK_IMPORTED_MODULE_0__.AutoTokenizer, - "pipeline": ZeroShotObjectDetectionPipeline, - "model": _models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModelForZeroShotObjectDetection, - "processor": _processors_js__WEBPACK_IMPORTED_MODULE_2__.AutoProcessor, - "default": { - // TODO: replace with original - // "model": "google/owlvit-base-patch32", - "model": "Xenova/owlvit-base-patch32" - }, - "type": "multimodal" - }, - "document-question-answering": { - "tokenizer": _tokenizers_js__WEBPACK_IMPORTED_MODULE_0__.AutoTokenizer, - "pipeline": DocumentQuestionAnsweringPipeline, - "model": _models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModelForDocumentQuestionAnswering, - "processor": _processors_js__WEBPACK_IMPORTED_MODULE_2__.AutoProcessor, - "default": { - // TODO: replace with original - // "model": "naver-clova-ix/donut-base-finetuned-docvqa", - "model": "Xenova/donut-base-finetuned-docvqa" - }, - "type": "multimodal" - }, - "image-to-image": { - // no tokenizer - "pipeline": ImageToImagePipeline, - "model": _models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModelForImageToImage, - "processor": _processors_js__WEBPACK_IMPORTED_MODULE_2__.AutoProcessor, - "default": { - // TODO: replace with original - // "model": "caidas/swin2SR-classical-sr-x2-64", - "model": "Xenova/swin2SR-classical-sr-x2-64" - }, - "type": "image" - }, - "depth-estimation": { - // no tokenizer - "pipeline": DepthEstimationPipeline, - "model": _models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModelForDepthEstimation, - "processor": _processors_js__WEBPACK_IMPORTED_MODULE_2__.AutoProcessor, - "default": { - // TODO: replace with original - // "model": "Intel/dpt-large", - "model": "Xenova/dpt-large" - }, - "type": "image" - }, - // This task serves as a useful interface for dealing with sentence-transformers (https://huggingface.co/sentence-transformers). - "feature-extraction": { - "tokenizer": _tokenizers_js__WEBPACK_IMPORTED_MODULE_0__.AutoTokenizer, - "pipeline": FeatureExtractionPipeline, - "model": _models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModel, - "default": { - // TODO: replace with original - // "model": "sentence-transformers/all-MiniLM-L6-v2", - "model": "Xenova/all-MiniLM-L6-v2" - }, - "type": "text" - }, - "image-feature-extraction": { - "processor": _processors_js__WEBPACK_IMPORTED_MODULE_2__.AutoProcessor, - "pipeline": ImageFeatureExtractionPipeline, - "model": [_models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModelForImageFeatureExtraction, _models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModel], - "default": { - // TODO: replace with original - // "model": "google/vit-base-patch16-224", - "model": "Xenova/vit-base-patch16-224-in21k" - }, - "type": "image" - } - }); - const TASK_ALIASES = Object.freeze({ - "sentiment-analysis": "text-classification", - "ner": "token-classification", - // "vqa": "visual-question-answering", // TODO: Add - "asr": "automatic-speech-recognition", - "text-to-speech": "text-to-audio", - // Add for backwards compatibility - "embeddings": "feature-extraction" - }); - async function pipeline(task, model = null, { - progress_callback = null, - config = null, - cache_dir = null, - local_files_only = false, - revision = "main", - device = null, - dtype = null, - model_file_name = null, - session_options = {} - } = {}) { - task = TASK_ALIASES[task] ?? task; - const pipelineInfo = SUPPORTED_TASKS[task.split("_", 1)[0]]; - if (!pipelineInfo) { - throw Error(`Unsupported pipeline: ${task}. Must be one of [${Object.keys(SUPPORTED_TASKS)}]`); - } - if (!model) { - model = pipelineInfo.default.model; - console.log(`No model specified. Using default model: "${model}".`); - } - const pretrainedOptions = { - progress_callback, - config, - cache_dir, - local_files_only, - revision, - device, - dtype, - model_file_name, - session_options - }; - const classes = /* @__PURE__ */ new Map([ - ["tokenizer", pipelineInfo.tokenizer], - ["model", pipelineInfo.model], - ["processor", pipelineInfo.processor] - ]); - const results = await loadItems(classes, model, pretrainedOptions); - results.task = task; - (0, _utils_core_js__WEBPACK_IMPORTED_MODULE_4__.dispatchCallback)(progress_callback, { - "status": "ready", - "task": task, - "model": model - }); - const pipelineClass = pipelineInfo.pipeline; - return new pipelineClass(results); - } - async function loadItems(mapping, model, pretrainedOptions) { - const result = /* @__PURE__ */ Object.create(null); - const promises = []; - for (const [name, cls] of mapping.entries()) { - if (!cls) continue; - let promise; - if (Array.isArray(cls)) { - promise = new Promise(async (resolve, reject) => { - var _a, _b; - let e; - for (const c of cls) { - if (c === null) { - resolve(null); - return; - } - try { - resolve(await c.from_pretrained(model, pretrainedOptions)); - return; - } catch (err) { - if ((_a = err.message) == null ? void 0 : _a.includes("Unsupported model type")) { - e = err; - } else if ((_b = err.message) == null ? void 0 : _b.includes("Could not locate file")) { - e = err; - } else { - reject(err); - return; - } - } - } - reject(e); - }); - } else { - promise = cls.from_pretrained(model, pretrainedOptions); - } - result[name] = promise; - promises.push(promise); - } - await Promise.all(promises); - for (const [name, promise] of Object.entries(result)) { - result[name] = await promise; - } - return result; - } - } - ), - /***/ - "./src/processors.js": ( - /*!***************************!*\ - !*** ./src/processors.js ***! - \***************************/ - /***/ - (__unused_webpack___webpack_module__, __webpack_exports__2, __webpack_require__2) => { - __webpack_require__2.r(__webpack_exports__2); - __webpack_require__2.d(__webpack_exports__2, { - /* harmony export */ - ASTFeatureExtractor: () => ( - /* binding */ - ASTFeatureExtractor - ), - /* harmony export */ - AutoProcessor: () => ( - /* binding */ - AutoProcessor - ), - /* harmony export */ - BeitFeatureExtractor: () => ( - /* binding */ - BeitFeatureExtractor - ), - /* harmony export */ - BitImageProcessor: () => ( - /* binding */ - BitImageProcessor - ), - /* harmony export */ - CLIPFeatureExtractor: () => ( - /* binding */ - CLIPFeatureExtractor - ), - /* harmony export */ - CLIPImageProcessor: () => ( - /* binding */ - CLIPImageProcessor - ), - /* harmony export */ - ChineseCLIPFeatureExtractor: () => ( - /* binding */ - ChineseCLIPFeatureExtractor - ), - /* harmony export */ - ClapFeatureExtractor: () => ( - /* binding */ - ClapFeatureExtractor - ), - /* harmony export */ - ConvNextFeatureExtractor: () => ( - /* binding */ - ConvNextFeatureExtractor - ), - /* harmony export */ - ConvNextImageProcessor: () => ( - /* binding */ - ConvNextImageProcessor - ), - /* harmony export */ - DPTFeatureExtractor: () => ( - /* binding */ - DPTFeatureExtractor - ), - /* harmony export */ - DPTImageProcessor: () => ( - /* binding */ - DPTImageProcessor - ), - /* harmony export */ - DeiTFeatureExtractor: () => ( - /* binding */ - DeiTFeatureExtractor - ), - /* harmony export */ - DetrFeatureExtractor: () => ( - /* binding */ - DetrFeatureExtractor - ), - /* harmony export */ - DonutFeatureExtractor: () => ( - /* binding */ - DonutFeatureExtractor - ), - /* harmony export */ - EfficientNetImageProcessor: () => ( - /* binding */ - EfficientNetImageProcessor - ), - /* harmony export */ - FeatureExtractor: () => ( - /* binding */ - FeatureExtractor - ), - /* harmony export */ - Florence2Processor: () => ( - /* binding */ - Florence2Processor - ), - /* harmony export */ - GLPNFeatureExtractor: () => ( - /* binding */ - GLPNFeatureExtractor - ), - /* harmony export */ - ImageFeatureExtractor: () => ( - /* binding */ - ImageFeatureExtractor - ), - /* harmony export */ - MaskFormerFeatureExtractor: () => ( - /* binding */ - MaskFormerFeatureExtractor - ), - /* harmony export */ - MobileNetV1FeatureExtractor: () => ( - /* binding */ - MobileNetV1FeatureExtractor - ), - /* harmony export */ - MobileNetV2FeatureExtractor: () => ( - /* binding */ - MobileNetV2FeatureExtractor - ), - /* harmony export */ - MobileNetV3FeatureExtractor: () => ( - /* binding */ - MobileNetV3FeatureExtractor - ), - /* harmony export */ - MobileNetV4FeatureExtractor: () => ( - /* binding */ - MobileNetV4FeatureExtractor - ), - /* harmony export */ - MobileViTFeatureExtractor: () => ( - /* binding */ - MobileViTFeatureExtractor - ), - /* harmony export */ - MobileViTImageProcessor: () => ( - /* binding */ - MobileViTImageProcessor - ), - /* harmony export */ - NougatImageProcessor: () => ( - /* binding */ - NougatImageProcessor - ), - /* harmony export */ - OwlViTFeatureExtractor: () => ( - /* binding */ - OwlViTFeatureExtractor - ), - /* harmony export */ - OwlViTProcessor: () => ( - /* binding */ - OwlViTProcessor - ), - /* harmony export */ - Owlv2ImageProcessor: () => ( - /* binding */ - Owlv2ImageProcessor - ), - /* harmony export */ - Processor: () => ( - /* binding */ - Processor - ), - /* harmony export */ - PvtImageProcessor: () => ( - /* binding */ - PvtImageProcessor - ), - /* harmony export */ - PyAnnoteFeatureExtractor: () => ( - /* binding */ - PyAnnoteFeatureExtractor - ), - /* harmony export */ - PyAnnoteProcessor: () => ( - /* binding */ - PyAnnoteProcessor - ), - /* harmony export */ - RTDetrImageProcessor: () => ( - /* binding */ - RTDetrImageProcessor - ), - /* harmony export */ - SamImageProcessor: () => ( - /* binding */ - SamImageProcessor - ), - /* harmony export */ - SamProcessor: () => ( - /* binding */ - SamProcessor - ), - /* harmony export */ - SapiensFeatureExtractor: () => ( - /* binding */ - SapiensFeatureExtractor - ), - /* harmony export */ - SeamlessM4TFeatureExtractor: () => ( - /* binding */ - SeamlessM4TFeatureExtractor - ), - /* harmony export */ - SegformerFeatureExtractor: () => ( - /* binding */ - SegformerFeatureExtractor - ), - /* harmony export */ - SiglipImageProcessor: () => ( - /* binding */ - SiglipImageProcessor - ), - /* harmony export */ - SpeechT5FeatureExtractor: () => ( - /* binding */ - SpeechT5FeatureExtractor - ), - /* harmony export */ - SpeechT5Processor: () => ( - /* binding */ - SpeechT5Processor - ), - /* harmony export */ - Swin2SRImageProcessor: () => ( - /* binding */ - Swin2SRImageProcessor - ), - /* harmony export */ - ViTFeatureExtractor: () => ( - /* binding */ - ViTFeatureExtractor - ), - /* harmony export */ - ViTImageProcessor: () => ( - /* binding */ - ViTImageProcessor - ), - /* harmony export */ - VitMatteImageProcessor: () => ( - /* binding */ - VitMatteImageProcessor - ), - /* harmony export */ - Wav2Vec2FeatureExtractor: () => ( - /* binding */ - Wav2Vec2FeatureExtractor - ), - /* harmony export */ - Wav2Vec2ProcessorWithLM: () => ( - /* binding */ - Wav2Vec2ProcessorWithLM - ), - /* harmony export */ - WeSpeakerFeatureExtractor: () => ( - /* binding */ - WeSpeakerFeatureExtractor - ), - /* harmony export */ - WhisperFeatureExtractor: () => ( - /* binding */ - WhisperFeatureExtractor - ), - /* harmony export */ - WhisperProcessor: () => ( - /* binding */ - WhisperProcessor - ), - /* harmony export */ - YolosFeatureExtractor: () => ( - /* binding */ - YolosFeatureExtractor - ) - /* harmony export */ - }); - var _utils_generic_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__2( - /*! ./utils/generic.js */ - "./src/utils/generic.js" - ); - var _utils_core_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__2( - /*! ./utils/core.js */ - "./src/utils/core.js" - ); - var _utils_hub_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__2( - /*! ./utils/hub.js */ - "./src/utils/hub.js" - ); - var _utils_maths_js__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__2( - /*! ./utils/maths.js */ - "./src/utils/maths.js" - ); - var _utils_tensor_js__WEBPACK_IMPORTED_MODULE_4__ = __webpack_require__2( - /*! ./utils/tensor.js */ - "./src/utils/tensor.js" - ); - var _utils_image_js__WEBPACK_IMPORTED_MODULE_5__ = __webpack_require__2( - /*! ./utils/image.js */ - "./src/utils/image.js" - ); - var _utils_audio_js__WEBPACK_IMPORTED_MODULE_6__2 = __webpack_require__2( - /*! ./utils/audio.js */ - "./src/utils/audio.js" - ); - function center_to_corners_format([centerX, centerY, width, height]) { - return [ - centerX - width / 2, - centerY - height / 2, - centerX + width / 2, - centerY + height / 2 - ]; - } - function post_process_object_detection(outputs, threshold = 0.5, target_sizes = null, is_zero_shot = false) { - const out_logits = outputs.logits; - const out_bbox = outputs.pred_boxes; - const [batch_size, num_boxes, num_classes] = out_logits.dims; - if (target_sizes !== null && target_sizes.length !== batch_size) { - throw Error("Make sure that you pass in as many target sizes as the batch dimension of the logits"); - } - let toReturn = []; - for (let i = 0; i < batch_size; ++i) { - let target_size = target_sizes !== null ? target_sizes[i] : null; - let info = { - boxes: [], - classes: [], - scores: [] - }; - let logits = out_logits[i]; - let bbox = out_bbox[i]; - for (let j = 0; j < num_boxes; ++j) { - let logit = logits[j]; - let indices = []; - let probs; - if (is_zero_shot) { - probs = logit.sigmoid().data; - for (let k = 0; k < probs.length; ++k) { - if (probs[k] > threshold) { - indices.push(k); - } - } - } else { - let maxIndex = (0, _utils_maths_js__WEBPACK_IMPORTED_MODULE_3__.max)(logit.data)[1]; - if (maxIndex === num_classes - 1) { - continue; - } - probs = (0, _utils_maths_js__WEBPACK_IMPORTED_MODULE_3__.softmax)(logit.data); - if (probs[maxIndex] < threshold) { - continue; - } - indices.push(maxIndex); - } - for (const index of indices) { - let box = bbox[j].data; - box = center_to_corners_format(box); - if (target_size !== null) { - box = box.map((x, i2) => x * target_size[(i2 + 1) % 2]); - } - info.boxes.push(box); - info.classes.push(index); - info.scores.push(probs[index]); - } - } - toReturn.push(info); - } - return toReturn; - } - function post_process_semantic_segmentation(outputs, target_sizes = null) { - const logits = outputs.logits; - const batch_size = logits.dims[0]; - if (target_sizes !== null && target_sizes.length !== batch_size) { - throw Error("Make sure that you pass in as many target sizes as the batch dimension of the logits"); - } - const toReturn = []; - for (let i = 0; i < batch_size; ++i) { - const target_size = target_sizes !== null ? target_sizes[i] : null; - let data = logits[i]; - if (target_size !== null) { - data = (0, _utils_tensor_js__WEBPACK_IMPORTED_MODULE_4__.interpolate)(data, target_size, "bilinear", false); - } - const [height, width] = target_size ?? data.dims.slice(-2); - const segmentation = new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_4__.Tensor( - "int32", - new Int32Array(height * width), - [height, width] - ); - const buffer = data[0].data; - const segmentation_data = segmentation.data; - for (let j = 1; j < data.dims[0]; ++j) { - const row = data[j].data; - for (let k = 0; k < row.length; ++k) { - if (row[k] > buffer[k]) { - buffer[k] = row[k]; - segmentation_data[k] = j; - } - } - } - const hasLabel = new Array(data.dims[0]); - for (let j = 0; j < segmentation_data.length; ++j) { - const index = segmentation_data[j]; - hasLabel[index] = index; - } - const labels = hasLabel.filter((x) => x !== void 0); - toReturn.push({ segmentation, labels }); - } - return toReturn; - } - function remove_low_and_no_objects(class_logits, mask_logits, object_mask_threshold, num_labels) { - const mask_probs_item = []; - const pred_scores_item = []; - const pred_labels_item = []; - for (let j = 0; j < class_logits.dims[0]; ++j) { - const cls = class_logits[j]; - const mask = mask_logits[j]; - const pred_label = (0, _utils_maths_js__WEBPACK_IMPORTED_MODULE_3__.max)(cls.data)[1]; - if (pred_label === num_labels) { - continue; - } - const scores = (0, _utils_maths_js__WEBPACK_IMPORTED_MODULE_3__.softmax)(cls.data); - const pred_score = scores[pred_label]; - if (pred_score > object_mask_threshold) { - mask_probs_item.push(mask); - pred_scores_item.push(pred_score); - pred_labels_item.push(pred_label); - } - } - return [mask_probs_item, pred_scores_item, pred_labels_item]; - } - function check_segment_validity(mask_labels, mask_probs, k, mask_threshold = 0.5, overlap_mask_area_threshold = 0.8) { - const mask_k = []; - let mask_k_area = 0; - let original_area = 0; - const mask_probs_k_data = mask_probs[k].data; - for (let i = 0; i < mask_labels.length; ++i) { - if (mask_labels[i] === k) { - mask_k.push(i); - ++mask_k_area; - } - if (mask_probs_k_data[i] >= mask_threshold) { - ++original_area; - } - } - let mask_exists = mask_k_area > 0 && original_area > 0; - if (mask_exists) { - let area_ratio = mask_k_area / original_area; - mask_exists = area_ratio > overlap_mask_area_threshold; - } - return [mask_exists, mask_k]; - } - function compute_segments(mask_probs, pred_scores, pred_labels, mask_threshold, overlap_mask_area_threshold, label_ids_to_fuse = null, target_size = null) { - const [height, width] = target_size ?? mask_probs[0].dims; - const segmentation = new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_4__.Tensor( - "int32", - new Int32Array(height * width), - [height, width] - ); - const segments = []; - if (target_size !== null) { - for (let i = 0; i < mask_probs.length; ++i) { - mask_probs[i] = (0, _utils_tensor_js__WEBPACK_IMPORTED_MODULE_4__.interpolate)(mask_probs[i], target_size, "bilinear", false); - } - } - const mask_labels = new Int32Array(mask_probs[0].data.length); - const bestScores = new Float32Array(mask_probs[0].data.length); - for (let i = 0; i < mask_probs.length; ++i) { - let score = pred_scores[i]; - const mask_probs_i_data = mask_probs[i].data; - for (let j = 0; j < mask_probs_i_data.length; ++j) { - mask_probs_i_data[j] *= score; - if (mask_probs_i_data[j] > bestScores[j]) { - mask_labels[j] = i; - bestScores[j] = mask_probs_i_data[j]; - } - } - } - let current_segment_id = 0; - const segmentation_data = segmentation.data; - for (let k = 0; k < pred_labels.length; ++k) { - const pred_class = pred_labels[k]; - const [mask_exists, mask_k] = check_segment_validity( - mask_labels, - mask_probs, - k, - mask_threshold, - overlap_mask_area_threshold - ); - if (!mask_exists) { - continue; - } - ++current_segment_id; - for (const index of mask_k) { - segmentation_data[index] = current_segment_id; - } - segments.push({ - id: current_segment_id, - label_id: pred_class, - // was_fused: should_fuse, TODO - score: pred_scores[k] - }); - } - return [segmentation, segments]; - } - function post_process_panoptic_segmentation(outputs, threshold = 0.5, mask_threshold = 0.5, overlap_mask_area_threshold = 0.8, label_ids_to_fuse = null, target_sizes = null) { - if (label_ids_to_fuse === null) { - console.warn("`label_ids_to_fuse` unset. No instance will be fused."); - label_ids_to_fuse = /* @__PURE__ */ new Set(); - } - const class_queries_logits = outputs.class_queries_logits ?? outputs.logits; - const masks_queries_logits = outputs.masks_queries_logits ?? outputs.pred_masks; - const mask_probs = masks_queries_logits.sigmoid(); - let [batch_size, num_queries, num_labels] = class_queries_logits.dims; - num_labels -= 1; - if (target_sizes !== null && target_sizes.length !== batch_size) { - throw Error("Make sure that you pass in as many target sizes as the batch dimension of the logits"); - } - let toReturn = []; - for (let i = 0; i < batch_size; ++i) { - let target_size = target_sizes !== null ? target_sizes[i] : null; - let class_logits = class_queries_logits[i]; - let mask_logits = mask_probs[i]; - let [mask_probs_item, pred_scores_item, pred_labels_item] = remove_low_and_no_objects(class_logits, mask_logits, threshold, num_labels); - if (pred_labels_item.length === 0) { - let [height, width] = target_size ?? mask_logits.dims.slice(-2); - let segmentation2 = new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_4__.Tensor( - "int32", - new Int32Array(height * width).fill(-1), - [height, width] - ); - toReturn.push({ - segmentation: segmentation2, - segments_info: [] - }); - continue; - } - let [segmentation, segments] = compute_segments( - mask_probs_item, - pred_scores_item, - pred_labels_item, - mask_threshold, - overlap_mask_area_threshold, - label_ids_to_fuse, - target_size - ); - toReturn.push({ - segmentation, - segments_info: segments - }); - } - return toReturn; - } - function post_process_instance_segmentation(outputs, threshold = 0.5, target_sizes = null) { - throw new Error("Not implemented yet"); - return []; - } - function validate_audio_inputs(audio, feature_extractor) { - var _a; - if (!(audio instanceof Float32Array || audio instanceof Float64Array)) { - throw new Error( - `${feature_extractor} expects input to be a Float32Array or a Float64Array, but got ${((_a = audio == null ? void 0 : audio.constructor) == null ? void 0 : _a.name) ?? typeof audio} instead. If using the feature extractor directly, remember to use \`read_audio(url, sampling_rate)\` to obtain the raw audio data of the file/url.` - ); - } - } - function constraint_to_multiple_of(val, multiple, minVal = 0, maxVal = null) { - const a = val / multiple; - let x = (0, _utils_maths_js__WEBPACK_IMPORTED_MODULE_3__.bankers_round)(a) * multiple; - if (maxVal !== null && x > maxVal) { - x = Math.floor(a) * multiple; - } - if (x < minVal) { - x = Math.ceil(a) * multiple; - } - return x; - } - function enforce_size_divisibility([width, height], divisor) { - return [ - Math.max(Math.floor(width / divisor), 1) * divisor, - Math.max(Math.floor(height / divisor), 1) * divisor - ]; - } - class FeatureExtractor extends _utils_generic_js__WEBPACK_IMPORTED_MODULE_0__.Callable { - /** - * Constructs a new FeatureExtractor instance. - * - * @param {Object} config The configuration for the feature extractor. - */ - constructor(config) { - super(); - this.config = config; - } - } - class ImageFeatureExtractor extends FeatureExtractor { - /** - * Constructs a new ImageFeatureExtractor instance. - * - * @param {Object} config The configuration for the feature extractor. - * @param {number[]} config.image_mean The mean values for image normalization. - * @param {number[]} config.image_std The standard deviation values for image normalization. - * @param {boolean} config.do_rescale Whether to rescale the image pixel values to the [0,1] range. - * @param {number} config.rescale_factor The factor to use for rescaling the image pixel values. - * @param {boolean} config.do_normalize Whether to normalize the image pixel values. - * @param {boolean} config.do_resize Whether to resize the image. - * @param {number} config.resample What method to use for resampling. - * @param {number|Object} config.size The size to resize the image to. - * @param {boolean} [config.do_flip_channel_order=false] Whether to flip the color channels from RGB to BGR. - * Can be overridden by the `do_flip_channel_order` parameter in the `preprocess` method. - */ - constructor(config) { - super(config); - this.image_mean = this.config.image_mean ?? this.config.mean; - this.image_std = this.config.image_std ?? this.config.std; - this.resample = this.config.resample ?? 2; - this.do_rescale = this.config.do_rescale ?? true; - this.rescale_factor = this.config.rescale_factor ?? 1 / 255; - this.do_normalize = this.config.do_normalize; - this.do_resize = this.config.do_resize; - this.do_thumbnail = this.config.do_thumbnail; - this.size = this.config.size; - this.size_divisibility = this.config.size_divisibility ?? this.config.size_divisor; - this.do_center_crop = this.config.do_center_crop; - this.crop_size = this.config.crop_size; - this.do_convert_rgb = this.config.do_convert_rgb ?? true; - this.do_crop_margin = this.config.do_crop_margin; - this.pad_size = this.config.pad_size; - this.do_pad = this.config.do_pad; - if (this.do_pad && !this.pad_size && this.size && this.size.width !== void 0 && this.size.height !== void 0) { - this.pad_size = this.size; - } - this.do_flip_channel_order = this.config.do_flip_channel_order ?? false; - } - /** - * Resize the image to make a thumbnail. The image is resized so that no dimension is larger than any - * corresponding dimension of the specified size. - * @param {RawImage} image The image to be resized. - * @param {{height:number, width:number}} size The size `{"height": h, "width": w}` to resize the image to. - * @param {string | 0 | 1 | 2 | 3 | 4 | 5} [resample=2] The resampling filter to use. - * @returns {Promise} The resized image. - */ - async thumbnail(image, size, resample = 2) { - const input_height = image.height; - const input_width = image.width; - const output_height = size.height; - const output_width = size.width; - let height = Math.min(input_height, output_height); - let width = Math.min(input_width, output_width); - if (height === input_height && width === input_width) { - return image; - } - if (input_height > input_width) { - width = Math.floor(input_width * height / input_height); - } else if (input_width > input_height) { - height = Math.floor(input_height * width / input_width); - } - return await image.resize(width, height, { resample }); - } - /** - * Crops the margin of the image. Gray pixels are considered margin (i.e., pixels with a value below the threshold). - * @param {RawImage} image The image to be cropped. - * @param {number} gray_threshold Value below which pixels are considered to be gray. - * @returns {Promise} The cropped image. - */ - async crop_margin(image, gray_threshold = 200) { - const gray_image = image.clone().grayscale(); - const minValue = (0, _utils_maths_js__WEBPACK_IMPORTED_MODULE_3__.min)(gray_image.data)[0]; - const maxValue = (0, _utils_maths_js__WEBPACK_IMPORTED_MODULE_3__.max)(gray_image.data)[0]; - const diff = maxValue - minValue; - if (diff === 0) { - return image; - } - const threshold = gray_threshold / 255; - let x_min = gray_image.width, y_min = gray_image.height, x_max = 0, y_max = 0; - const gray_image_data = gray_image.data; - for (let j = 0; j < gray_image.height; ++j) { - const row = j * gray_image.width; - for (let i = 0; i < gray_image.width; ++i) { - if ((gray_image_data[row + i] - minValue) / diff < threshold) { - x_min = Math.min(x_min, i); - y_min = Math.min(y_min, j); - x_max = Math.max(x_max, i); - y_max = Math.max(y_max, j); - } - } - } - image = await image.crop([x_min, y_min, x_max, y_max]); - return image; - } - /** - * Pad the image by a certain amount. - * @param {Float32Array} pixelData The pixel data to pad. - * @param {number[]} imgDims The dimensions of the image (height, width, channels). - * @param {{width:number; height:number}|number} padSize The dimensions of the padded image. - * @param {Object} options The options for padding. - * @param {'constant'|'symmetric'} [options.mode='constant'] The type of padding to add. - * @param {boolean} [options.center=false] Whether to center the image. - * @param {number} [options.constant_values=0] The constant value to use for padding. - * @returns {[Float32Array, number[]]} The padded pixel data and image dimensions. - */ - pad_image(pixelData, imgDims, padSize, { - mode = "constant", - center = false, - constant_values = 0 - } = {}) { - const [imageHeight, imageWidth, imageChannels] = imgDims; - let paddedImageWidth, paddedImageHeight; - if (typeof padSize === "number") { - paddedImageWidth = padSize; - paddedImageHeight = padSize; - } else { - paddedImageWidth = padSize.width; - paddedImageHeight = padSize.height; - } - if (paddedImageWidth !== imageWidth || paddedImageHeight !== imageHeight) { - const paddedPixelData = new Float32Array(paddedImageWidth * paddedImageHeight * imageChannels); - if (Array.isArray(constant_values)) { - for (let i = 0; i < paddedPixelData.length; ++i) { - paddedPixelData[i] = constant_values[i % imageChannels]; - } - } else if (constant_values !== 0) { - paddedPixelData.fill(constant_values); - } - const [left, top] = center ? [Math.floor((paddedImageWidth - imageWidth) / 2), Math.floor((paddedImageHeight - imageHeight) / 2)] : [0, 0]; - for (let i = 0; i < imageHeight; ++i) { - const a = (i + top) * paddedImageWidth; - const b = i * imageWidth; - for (let j = 0; j < imageWidth; ++j) { - const c = (a + j + left) * imageChannels; - const d = (b + j) * imageChannels; - for (let k = 0; k < imageChannels; ++k) { - paddedPixelData[c + k] = pixelData[d + k]; - } - } - } - if (mode === "symmetric") { - if (center) { - throw new Error("`center` padding is not supported when `mode` is set to `symmetric`."); - } - const h1 = imageHeight - 1; - const w1 = imageWidth - 1; - for (let i = 0; i < paddedImageHeight; ++i) { - const a = i * paddedImageWidth; - const b = (0, _utils_core_js__WEBPACK_IMPORTED_MODULE_1__.calculateReflectOffset)(i, h1) * imageWidth; - for (let j = 0; j < paddedImageWidth; ++j) { - if (i < imageHeight && j < imageWidth) continue; - const c = (a + j) * imageChannels; - const d = (b + (0, _utils_core_js__WEBPACK_IMPORTED_MODULE_1__.calculateReflectOffset)(j, w1)) * imageChannels; - for (let k = 0; k < imageChannels; ++k) { - paddedPixelData[c + k] = pixelData[d + k]; - } - } - } - } - pixelData = paddedPixelData; - imgDims = [paddedImageHeight, paddedImageWidth, imageChannels]; - } - return [pixelData, imgDims]; - } - /** - * Rescale the image' pixel values by `this.rescale_factor`. - * @param {Float32Array} pixelData The pixel data to rescale. - * @returns {void} - */ - rescale(pixelData) { - for (let i = 0; i < pixelData.length; ++i) { - pixelData[i] = this.rescale_factor * pixelData[i]; - } - } - /** - * Find the target (width, height) dimension of the output image after - * resizing given the input image and the desired size. - * @param {RawImage} image The image to resize. - * @param {any} size The size to use for resizing the image. - * @returns {[number, number]} The target (width, height) dimension of the output image after resizing. - */ - get_resize_output_image_size(image, size) { - const [srcWidth, srcHeight] = image.size; - let shortest_edge; - let longest_edge; - if (this.do_thumbnail) { - const { height, width } = size; - shortest_edge = Math.min(height, width); - } else if (Number.isInteger(size)) { - shortest_edge = size; - longest_edge = this.config.max_size ?? shortest_edge; - } else if (size !== void 0) { - shortest_edge = size.shortest_edge; - longest_edge = size.longest_edge; - } - if (shortest_edge !== void 0 || longest_edge !== void 0) { - const shortResizeFactor = shortest_edge === void 0 ? 1 : Math.max(shortest_edge / srcWidth, shortest_edge / srcHeight); - const newWidth = srcWidth * shortResizeFactor; - const newHeight = srcHeight * shortResizeFactor; - const longResizeFactor = longest_edge === void 0 ? 1 : Math.min(longest_edge / newWidth, longest_edge / newHeight); - let finalWidth = Math.floor(Number((newWidth * longResizeFactor).toFixed(2))); - let finalHeight = Math.floor(Number((newHeight * longResizeFactor).toFixed(2))); - if (this.size_divisibility !== void 0) { - [finalWidth, finalHeight] = enforce_size_divisibility([finalWidth, finalHeight], this.size_divisibility); - } - return [finalWidth, finalHeight]; - } else if (size !== void 0 && size.width !== void 0 && size.height !== void 0) { - let newWidth = size.width; - let newHeight = size.height; - if (this.config.keep_aspect_ratio && this.config.ensure_multiple_of) { - let scale_height = newHeight / srcHeight; - let scale_width = newWidth / srcWidth; - if (Math.abs(1 - scale_width) < Math.abs(1 - scale_height)) { - scale_height = scale_width; - } else { - scale_width = scale_height; - } - newHeight = constraint_to_multiple_of(scale_height * srcHeight, this.config.ensure_multiple_of); - newWidth = constraint_to_multiple_of(scale_width * srcWidth, this.config.ensure_multiple_of); - } - return [newWidth, newHeight]; - } else if (this.size_divisibility !== void 0) { - return enforce_size_divisibility([srcWidth, srcHeight], this.size_divisibility); - } else { - throw new Error(`Could not resize image due to unsupported \`this.size\` option in config: ${JSON.stringify(size)}`); - } - } - /** - * Resizes the image. - * @param {RawImage} image The image to resize. - * @returns {Promise} The resized image. - */ - async resize(image) { - const [newWidth, newHeight] = this.get_resize_output_image_size(image, this.size); - return await image.resize(newWidth, newHeight, { - resample: this.resample - }); - } - /** - * @typedef {object} PreprocessedImage - * @property {HeightWidth} original_size The original size of the image. - * @property {HeightWidth} reshaped_input_size The reshaped input size of the image. - * @property {Tensor} pixel_values The pixel values of the preprocessed image. - */ - /** - * Preprocesses the given image. - * - * @param {RawImage} image The image to preprocess. - * @param {Object} overrides The overrides for the preprocessing options. - * @returns {Promise} The preprocessed image. - */ - async preprocess(image, { - do_normalize = null, - do_pad = null, - do_convert_rgb = null, - do_convert_grayscale = null, - do_flip_channel_order = null - } = {}) { - if (this.do_crop_margin) { - image = await this.crop_margin(image); - } - const [srcWidth, srcHeight] = image.size; - if (do_convert_rgb ?? this.do_convert_rgb) { - image = image.rgb(); - } else if (do_convert_grayscale) { - image = image.grayscale(); - } - if (this.do_resize) { - image = await this.resize(image); - } - if (this.do_thumbnail) { - image = await this.thumbnail(image, this.size, this.resample); - } - if (this.do_center_crop) { - let crop_width; - let crop_height; - if (Number.isInteger(this.crop_size)) { - crop_width = this.crop_size; - crop_height = this.crop_size; - } else { - crop_width = this.crop_size.width; - crop_height = this.crop_size.height; - } - image = await image.center_crop(crop_width, crop_height); - } - const reshaped_input_size = [image.height, image.width]; - let pixelData = Float32Array.from(image.data); - let imgDims = [image.height, image.width, image.channels]; - if (this.do_rescale) { - this.rescale(pixelData); - } - if (do_normalize ?? this.do_normalize) { - let image_mean = this.image_mean; - if (!Array.isArray(this.image_mean)) { - image_mean = new Array(image.channels).fill(image_mean); - } - let image_std = this.image_std; - if (!Array.isArray(this.image_std)) { - image_std = new Array(image.channels).fill(image_mean); - } - if (image_mean.length !== image.channels || image_std.length !== image.channels) { - throw new Error(`When set to arrays, the length of \`image_mean\` (${image_mean.length}) and \`image_std\` (${image_std.length}) must match the number of channels in the image (${image.channels}).`); - } - for (let i = 0; i < pixelData.length; i += image.channels) { - for (let j = 0; j < image.channels; ++j) { - pixelData[i + j] = (pixelData[i + j] - image_mean[j]) / image_std[j]; - } - } - } - if (do_pad ?? this.do_pad) { - if (this.pad_size) { - const padded = this.pad_image(pixelData, [image.height, image.width, image.channels], this.pad_size); - [pixelData, imgDims] = padded; - } else if (this.size_divisibility) { - const [paddedWidth, paddedHeight] = enforce_size_divisibility([imgDims[1], imgDims[0]], this.size_divisibility); - [pixelData, imgDims] = this.pad_image(pixelData, imgDims, { width: paddedWidth, height: paddedHeight }); - } - } - if (do_flip_channel_order ?? this.do_flip_channel_order) { - if (imgDims[2] !== 3) { - throw new Error("Flipping channel order is only supported for RGB images."); - } - for (let i = 0; i < pixelData.length; i += 3) { - const temp = pixelData[i]; - pixelData[i] = pixelData[i + 2]; - pixelData[i + 2] = temp; - } - } - const pixel_values = new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_4__.Tensor("float32", pixelData, imgDims).permute(2, 0, 1); - return { - original_size: [srcHeight, srcWidth], - reshaped_input_size, - pixel_values - }; - } - /** - * Calls the feature extraction process on an array of images, - * preprocesses each image, and concatenates the resulting - * features into a single Tensor. - * @param {RawImage[]} images The image(s) to extract features from. - * @param {...any} args Additional arguments. - * @returns {Promise} An object containing the concatenated pixel values (and other metadata) of the preprocessed images. - */ - async _call(images, ...args) { - if (!Array.isArray(images)) { - images = [images]; - } - const imageData = await Promise.all(images.map((x) => this.preprocess(x))); - const pixel_values = (0, _utils_tensor_js__WEBPACK_IMPORTED_MODULE_4__.stack)(imageData.map((x) => x.pixel_values), 0); - return { - pixel_values, - // Original sizes of images - original_sizes: imageData.map((x) => x.original_size), - // Reshaped sizes of images, before padding or cropping - reshaped_input_sizes: imageData.map((x) => x.reshaped_input_size) - }; - } - } - class SapiensFeatureExtractor extends ImageFeatureExtractor { - /** @type {typeof post_process_semantic_segmentation} */ - post_process_semantic_segmentation(...args) { - return post_process_semantic_segmentation(...args); - } - } - class SegformerFeatureExtractor extends ImageFeatureExtractor { - /** @type {typeof post_process_semantic_segmentation} */ - post_process_semantic_segmentation(...args) { - return post_process_semantic_segmentation(...args); - } - } - class PvtImageProcessor extends ImageFeatureExtractor { - } - class DPTFeatureExtractor extends ImageFeatureExtractor { - } - class DPTImageProcessor extends DPTFeatureExtractor { - } - class BitImageProcessor extends ImageFeatureExtractor { - } - class GLPNFeatureExtractor extends ImageFeatureExtractor { - } - class CLIPFeatureExtractor extends ImageFeatureExtractor { - } - class CLIPImageProcessor extends CLIPFeatureExtractor { - } - class ChineseCLIPFeatureExtractor extends ImageFeatureExtractor { - } - class SiglipImageProcessor extends ImageFeatureExtractor { - } - class ConvNextFeatureExtractor extends ImageFeatureExtractor { - constructor(config) { - super(config); - this.crop_pct = this.config.crop_pct ?? 224 / 256; - } - async resize(image) { - var _a; - const shortest_edge = (_a = this.size) == null ? void 0 : _a.shortest_edge; - if (shortest_edge === void 0) { - throw new Error(`Size dictionary must contain 'shortest_edge' key.`); - } - if (shortest_edge < 384) { - const resize_shortest_edge = Math.floor(shortest_edge / this.crop_pct); - const [newWidth, newHeight] = this.get_resize_output_image_size(image, { - shortest_edge: resize_shortest_edge - }); - image = await image.resize(newWidth, newHeight, { - resample: this.resample - }); - image = await image.center_crop(shortest_edge, shortest_edge); - } else { - image = await image.resize(shortest_edge, shortest_edge, { - resample: this.resample - }); - } - return image; - } - } - class ConvNextImageProcessor extends ConvNextFeatureExtractor { - } - class ViTFeatureExtractor extends ImageFeatureExtractor { - } - class ViTImageProcessor extends ImageFeatureExtractor { - } - class EfficientNetImageProcessor extends ImageFeatureExtractor { - constructor(config) { - super(config); - this.include_top = this.config.include_top ?? true; - if (this.include_top) { - this.image_std = this.image_std.map((x) => x * x); - } - } - } - class MobileNetV1FeatureExtractor extends ImageFeatureExtractor { - } - class MobileNetV2FeatureExtractor extends ImageFeatureExtractor { - } - class MobileNetV3FeatureExtractor extends ImageFeatureExtractor { - } - class MobileNetV4FeatureExtractor extends ImageFeatureExtractor { - } - class MobileViTFeatureExtractor extends ImageFeatureExtractor { - } - class MobileViTImageProcessor extends MobileViTFeatureExtractor { - } - class OwlViTFeatureExtractor extends ImageFeatureExtractor { - /** @type {typeof post_process_object_detection} */ - post_process_object_detection(...args) { - return post_process_object_detection(...args); - } - } - class Owlv2ImageProcessor extends OwlViTFeatureExtractor { - } - class RTDetrImageProcessor extends ImageFeatureExtractor { - /** @type {typeof post_process_object_detection} */ - post_process_object_detection(...args) { - return post_process_object_detection(...args); - } - } - class DeiTFeatureExtractor extends ImageFeatureExtractor { - } - class BeitFeatureExtractor extends ImageFeatureExtractor { - } - class DonutFeatureExtractor extends ImageFeatureExtractor { - pad_image(pixelData, imgDims, padSize, options = {}) { - const [imageHeight, imageWidth, imageChannels] = imgDims; - let image_mean = this.image_mean; - if (!Array.isArray(this.image_mean)) { - image_mean = new Array(imageChannels).fill(image_mean); - } - let image_std = this.image_std; - if (!Array.isArray(image_std)) { - image_std = new Array(imageChannels).fill(image_mean); - } - const constant_values = image_mean.map((x, i) => -x / image_std[i]); - return super.pad_image(pixelData, imgDims, padSize, { - center: true, - // Since normalization is done after padding, we need to use certain constant values to ensure the same behaviour is observed. - // For more information, see https://github.com/huggingface/transformers/blob/main/src/transformers/models/donut/image_processing_donut.py#L433-L451 - constant_values, - ...options - }); - } - } - class NougatImageProcessor extends DonutFeatureExtractor { - } - class DetrFeatureExtractor extends ImageFeatureExtractor { - /** - * Calls the feature extraction process on an array of images, preprocesses - * each image, and concatenates the resulting features into a single Tensor. - * @param {RawImage[]} images The image(s) to extract features from. - * @returns {Promise} An object containing the concatenated pixel values of the preprocessed images. - */ - async _call(images) { - const result = await super._call(images); - const maskSize = [result.pixel_values.dims[0], 64, 64]; - const pixel_mask = (0, _utils_tensor_js__WEBPACK_IMPORTED_MODULE_4__.full)(maskSize, 1n); - return { ...result, pixel_mask }; - } - /** @type {typeof post_process_object_detection} */ - post_process_object_detection(...args) { - return post_process_object_detection(...args); - } - /** @type {typeof post_process_panoptic_segmentation} */ - post_process_panoptic_segmentation(...args) { - return post_process_panoptic_segmentation(...args); - } - post_process_instance_segmentation() { - throw Error("Not implemented yet"); - } - } - class MaskFormerFeatureExtractor extends ImageFeatureExtractor { - /** @type {typeof post_process_panoptic_segmentation} */ - post_process_panoptic_segmentation(...args) { - return post_process_panoptic_segmentation(...args); - } - post_process_instance_segmentation() { - throw Error("Not implemented yet"); - } - } - class YolosFeatureExtractor extends ImageFeatureExtractor { - /** @type {typeof post_process_object_detection} */ - post_process_object_detection(...args) { - return post_process_object_detection(...args); - } - } - class SamImageProcessor extends ImageFeatureExtractor { - /** - * - * @param {any} input_points - * @param {HeightWidth[]} original_sizes - * @param {HeightWidth[]} reshaped_input_sizes - * @returns {Tensor} - */ - reshape_input_points(input_points, original_sizes, reshaped_input_sizes, is_bounding_box = false) { - input_points = structuredClone(input_points); - let shape = (0, _utils_core_js__WEBPACK_IMPORTED_MODULE_1__.calculateDimensions)(input_points); - if (shape.length === 3) { - if (!is_bounding_box) { - shape = [1, ...shape]; - } - input_points = [input_points]; - } else if (shape.length !== 4) { - throw Error("The input_points must be a 4D tensor of shape `batch_size`, `point_batch_size`, `nb_points_per_image`, `2`."); - } - for (let i = 0; i < input_points.length; ++i) { - let originalImageSize = original_sizes[i]; - let reshapedImageSize = reshaped_input_sizes[i]; - let resizeFactors = [ - reshapedImageSize[0] / originalImageSize[0], - reshapedImageSize[1] / originalImageSize[1] - ]; - for (let j = 0; j < input_points[i].length; ++j) { - for (let k = 0; k < input_points[i][j].length; ++k) { - for (let w = 0; w < input_points[i][j][k].length; ++w) { - input_points[i][j][k][w] *= resizeFactors[w % 2]; - } - } - } - } - return new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_4__.Tensor( - "float32", - Float32Array.from(input_points.flat(Infinity)), - shape - ); - } - /** - * - * @param {any} input_labels - * @param {Tensor} input_points - * @returns {Tensor} - */ - add_input_labels(input_labels, input_points) { - let shape = (0, _utils_core_js__WEBPACK_IMPORTED_MODULE_1__.calculateDimensions)(input_labels); - if (shape.length === 2) { - shape = [1, ...shape]; - input_labels = [input_labels]; - } else if (shape.length !== 3) { - throw Error("The input_points must be a 4D tensor of shape `batch_size`, `point_batch_size`, `nb_points_per_image`, `2`."); - } - if (shape.some((x, i) => x !== input_points.dims[i])) { - throw Error(`The first ${shape.length} dimensions of 'input_points' and 'input_labels' must be the same.`); - } - return new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_4__.Tensor( - "int64", - input_labels.flat(Infinity).map(BigInt), - shape - ); - } - /** - * @param {any[]} images The URL(s) of the image(s) to extract features from. - * @param {Object} [options] Additional options for the processor. - * @param {any} [options.input_points=null] A 3D or 4D array, representing the input points provided by the user. - * - 3D: `[point_batch_size, nb_points_per_image, 2]`. In this case, `batch_size` is assumed to be 1. - * - 4D: `[batch_size, point_batch_size, nb_points_per_image, 2]`. - * @param {any} [options.input_labels=null] A 2D or 3D array, representing the input labels for the points, used by the prompt encoder to encode the prompt. - * - 2D: `[point_batch_size, nb_points_per_image]`. In this case, `batch_size` is assumed to be 1. - * - 3D: `[batch_size, point_batch_size, nb_points_per_image]`. - * @param {number[][][]} [options.input_boxes=null] A 3D array of shape `(batch_size, num_boxes, 4)`, representing the input boxes provided by the user. - * This is used by the prompt encoder to encode the prompt. Generally yields to much better generated masks. - * The processor will generate a tensor, with each dimension corresponding respectively to the image batch size, - * the number of boxes per image and the coordinates of the top left and botton right point of the box. - * In the order (`x1`, `y1`, `x2`, `y2`): - * - `x1`: the x coordinate of the top left point of the input box - * - `y1`: the y coordinate of the top left point of the input box - * - `x2`: the x coordinate of the bottom right point of the input box - * - `y2`: the y coordinate of the bottom right point of the input box - * @returns {Promise} - */ - async _call(images, { - input_points = null, - input_labels = null, - input_boxes = null - } = {}) { - const processed = await super._call(images); - if (input_points) { - processed.input_points = this.reshape_input_points( - input_points, - processed.original_sizes, - processed.reshaped_input_sizes - ); - } - if (input_labels) { - if (!processed.input_points) { - throw Error("`input_points` must be provided if `input_labels` are provided."); - } - processed.input_labels = this.add_input_labels(input_labels, processed.input_points); - } - if (input_boxes) { - processed.input_boxes = this.reshape_input_points( - input_boxes, - processed.original_sizes, - processed.reshaped_input_sizes, - true - ); - } - return processed; - } - /** - * Remove padding and upscale masks to the original image size. - * @param {Tensor} masks Batched masks from the mask_decoder in (batch_size, num_channels, height, width) format. - * @param {[number, number][]} original_sizes The original sizes of each image before it was resized to the model's expected input shape, in (height, width) format. - * @param {[number, number][]} reshaped_input_sizes The size of each image as it is fed to the model, in (height, width) format. Used to remove padding. - * @param {Object} options Optional parameters for post-processing. - * @param {number} [options.mask_threshold] The threshold to use for binarizing the masks. - * @param {boolean} [options.binarize] Whether to binarize the masks. - * @param {Object} [options.pad_size] The target size the images were padded to before being passed to the model. If `null`, the target size is assumed to be the processor's `pad_size`. - * @param {number} [options.pad_size.height] The height the images were padded to. - * @param {number} [options.pad_size.width] The width the images were padded to. - * @returns {Promise} Batched masks in batch_size, num_channels, height, width) format, where (height, width) is given by original_size. - */ - async post_process_masks(masks, original_sizes, reshaped_input_sizes, { - mask_threshold = 0, - binarize = true, - pad_size = null - } = {}) { - const output_masks = []; - pad_size = pad_size ?? this.pad_size; - const target_image_size = [pad_size.height, pad_size.width]; - for (let i = 0; i < original_sizes.length; ++i) { - const original_size = original_sizes[i]; - const reshaped_input_size = reshaped_input_sizes[i]; - let interpolated_mask = await (0, _utils_tensor_js__WEBPACK_IMPORTED_MODULE_4__.interpolate_4d)( - masks[i], - { mode: "bilinear", size: target_image_size } - ); - interpolated_mask = interpolated_mask.slice(null, null, [0, reshaped_input_size[0]], [0, reshaped_input_size[1]]); - interpolated_mask = await (0, _utils_tensor_js__WEBPACK_IMPORTED_MODULE_4__.interpolate_4d)( - interpolated_mask, - { mode: "bilinear", size: original_size } - ); - if (binarize) { - const data = interpolated_mask.data; - const binarizedMaskData = new Uint8Array(data.length); - for (let i2 = 0; i2 < data.length; ++i2) { - if (data[i2] > mask_threshold) { - binarizedMaskData[i2] = 1; - } - } - interpolated_mask = new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_4__.Tensor( - "bool", - binarizedMaskData, - interpolated_mask.dims - ); - } - output_masks.push(interpolated_mask); - } - return output_masks; - } - /** - * Generates a list of crop boxes of different sizes. Each layer has (2**i)**2 boxes for the ith layer. - * @param {RawImage} image Input original image - * @param {number} target_size Target size of the resized image - * @param {Object} options Options for generating crop boxes - * @param {number} [options.crop_n_layers] If >0, mask prediction will be run again on crops of the image. - * Sets the number of layers to run, where each layer has 2**i_layer number of image crops. - * @param {number} [options.overlap_ratio] Sets the degree to which crops overlap. In the first crop layer, - * crops will overlap by this fraction of the image length. Later layers with more crops scale down this overlap. - * @param {number} [options.points_per_crop] Number of points to sample from each crop. - * @param {number} [options.crop_n_points_downscale_factor] The number of points-per-side sampled in layer n is - * scaled down by crop_n_points_downscale_factor**n. - * @returns {Object} An object containing the crop boxes, number of points per crop, cropped images, and input labels. - */ - generate_crop_boxes(image, target_size, { - crop_n_layers = 0, - overlap_ratio = 512 / 1500, - points_per_crop = 32, - crop_n_points_downscale_factor = 1 - } = {}) { - } - } - class Swin2SRImageProcessor extends ImageFeatureExtractor { - pad_image(pixelData, imgDims, padSize, options = {}) { - const [imageHeight, imageWidth, imageChannels] = imgDims; - return super.pad_image(pixelData, imgDims, { - // NOTE: For Swin2SR models, the original python implementation adds padding even when the image's width/height is already - // a multiple of `pad_size`. However, this is most likely a bug (PR: https://github.com/mv-lab/swin2sr/pull/19). - // For this reason, we only add padding when the image's width/height is not a multiple of `pad_size`. - width: imageWidth + (padSize - imageWidth % padSize) % padSize, - height: imageHeight + (padSize - imageHeight % padSize) % padSize - }, { - mode: "symmetric", - center: false, - constant_values: -1, - ...options - }); - } - } - class VitMatteImageProcessor extends ImageFeatureExtractor { - /** - * Calls the feature extraction process on an array of images, preprocesses - * each image, and concatenates the resulting features into a single Tensor. - * @param {RawImage[]} images The image(s) to extract features from. - * @param {RawImage[]} trimaps The trimaps(s) to extract features from. - * @returns {Promise} An object containing the concatenated pixel values of the preprocessed images. - */ - async _call(images, trimaps) { - if (!Array.isArray(images)) { - images = [images]; - } - if (!Array.isArray(trimaps)) { - trimaps = [trimaps]; - } - const imageData = await Promise.all(images.map((x) => this.preprocess(x))); - const trimapData = await Promise.all(trimaps.map((x) => this.preprocess(x, { - do_normalize: false, - do_convert_rgb: false, - do_convert_grayscale: true - }))); - const pixel_values = (0, _utils_tensor_js__WEBPACK_IMPORTED_MODULE_4__.stack)(imageData.map( - // Concatenate images and trimaps - (x, i) => (0, _utils_tensor_js__WEBPACK_IMPORTED_MODULE_4__.cat)([x.pixel_values, trimapData[i].pixel_values], 0) - ), 0); - return { - pixel_values, - // Original sizes of images - original_sizes: imageData.map((x) => x.original_size), - // Reshaped sizes of images, before padding or cropping - reshaped_input_sizes: imageData.map((x) => x.reshaped_input_size) - }; - } - } - class WhisperFeatureExtractor extends FeatureExtractor { - constructor(config) { - var _a; - super(config); - (_a = this.config).mel_filters ?? (_a.mel_filters = (0, _utils_audio_js__WEBPACK_IMPORTED_MODULE_6__2.mel_filter_bank)( - Math.floor(1 + this.config.n_fft / 2), - // num_frequency_bins - this.config.feature_size, - // num_mel_filters - 0, - // min_frequency - 8e3, - // max_frequency - this.config.sampling_rate, - // sampling_rate - "slaney", - // norm - "slaney" - // mel_scale - )); - this.window = (0, _utils_audio_js__WEBPACK_IMPORTED_MODULE_6__2.window_function)(this.config.n_fft, "hann"); - } - /** - * Computes the log-Mel spectrogram of the provided audio waveform. - * @param {Float32Array|Float64Array} waveform The audio waveform to process. - * @returns {Promise} An object containing the log-Mel spectrogram data as a Float32Array and its dimensions as an array of numbers. - */ - async _extract_fbank_features(waveform) { - const features = await (0, _utils_audio_js__WEBPACK_IMPORTED_MODULE_6__2.spectrogram)( - waveform, - this.window, - // window - this.config.n_fft, - // frame_length - this.config.hop_length, - // hop_length - { - power: 2, - mel_filters: this.config.mel_filters, - log_mel: "log10", - // Custom - max_num_frames: this.config.nb_max_frames - // 3000 - } - ); - const data = features.data; - const maxValue = (0, _utils_maths_js__WEBPACK_IMPORTED_MODULE_3__.max)(data)[0]; - for (let i = 0; i < data.length; ++i) { - data[i] = (Math.max(data[i], maxValue - 8) + 4) / 4; - } - return features; - } - /** - * Asynchronously extracts features from a given audio using the provided configuration. - * @param {Float32Array|Float64Array} audio The audio data as a Float32Array/Float64Array. - * @returns {Promise<{ input_features: Tensor }>} A Promise resolving to an object containing the extracted input features as a Tensor. - */ - async _call(audio) { - validate_audio_inputs(audio, "WhisperFeatureExtractor"); - let waveform; - if (audio.length > this.config.n_samples) { - console.warn( - "Attempting to extract features for audio longer than 30 seconds. If using a pipeline to extract transcript from a long audio clip, remember to specify `chunk_length_s` and/or `stride_length_s`." - ); - waveform = audio.slice(0, this.config.n_samples); - } else { - waveform = new Float32Array(this.config.n_samples); - waveform.set(audio); - } - const features = await this._extract_fbank_features(waveform); - return { - input_features: features.unsqueeze_(0) - }; - } - } - class Wav2Vec2FeatureExtractor extends FeatureExtractor { - /** - * @param {Float32Array} input_values - * @returns {Float32Array} - */ - _zero_mean_unit_var_norm(input_values) { - const sum = input_values.reduce((a, b) => a + b, 0); - const mean = sum / input_values.length; - const variance = input_values.reduce((a, b) => a + (b - mean) ** 2, 0) / input_values.length; - return input_values.map((x) => (x - mean) / Math.sqrt(variance + 1e-7)); - } - /** - * Asynchronously extracts features from a given audio using the provided configuration. - * @param {Float32Array|Float64Array} audio The audio data as a Float32Array/Float64Array. - * @returns {Promise<{ input_values: Tensor; attention_mask: Tensor }>} A Promise resolving to an object containing the extracted input features and attention mask as Tensors. - */ - async _call(audio) { - validate_audio_inputs(audio, "Wav2Vec2FeatureExtractor"); - if (audio instanceof Float64Array) { - audio = new Float32Array(audio); - } - let input_values = audio; - if (this.config.do_normalize) { - input_values = this._zero_mean_unit_var_norm(input_values); - } - const shape = [1, input_values.length]; - return { - input_values: new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_4__.Tensor("float32", input_values, shape), - attention_mask: new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_4__.Tensor("int64", new BigInt64Array(input_values.length).fill(1n), shape) - }; - } - } - class SeamlessM4TFeatureExtractor extends FeatureExtractor { - constructor(config) { - super(config); - const sampling_rate = this.config.sampling_rate; - const mel_filters = (0, _utils_audio_js__WEBPACK_IMPORTED_MODULE_6__2.mel_filter_bank)( - 256, - // num_frequency_bins - this.config.num_mel_bins, - // num_mel_filters - 20, - // min_frequency - Math.floor(sampling_rate / 2), - // max_frequency - sampling_rate, - // sampling_rate - null, - // norm - "kaldi", - // mel_scale - true - // triangularize_in_mel_space - ); - for (let i = 0; i < mel_filters.length; ++i) { - mel_filters[i].push(0); - } - this.mel_filters = mel_filters; - this.window = (0, _utils_audio_js__WEBPACK_IMPORTED_MODULE_6__2.window_function)(400, "povey", { - periodic: false - }); - } - /** - * Computes the log-Mel spectrogram of the provided audio waveform. - * @param {Float32Array|Float64Array} waveform The audio waveform to process. - * @param {number} max_length The maximum number of frames to return. - * @returns {Promise} An object containing the log-Mel spectrogram data as a Float32Array and its dimensions as an array of numbers. - */ - async _extract_fbank_features(waveform, max_length) { - waveform = waveform.map((x) => x * 32768); - return (0, _utils_audio_js__WEBPACK_IMPORTED_MODULE_6__2.spectrogram)( - waveform, - this.window, - // window - 400, - // frame_length - 160, - // hop_length - { - fft_length: 512, - power: 2, - center: false, - preemphasis: 0.97, - mel_filters: this.mel_filters, - log_mel: "log", - mel_floor: 1192092955078125e-22, - remove_dc_offset: true, - // Custom - max_num_frames: max_length, - transpose: true - } - ); - } - /** - * Asynchronously extracts features from a given audio using the provided configuration. - * @param {Float32Array|Float64Array} audio The audio data as a Float32Array/Float64Array. - * @param {Object} options Optional parameters for feature extraction. - * @param {boolean} [options.padding=true] Whether to pad the sequence to a multiple of `pad_to_multiple_of`. - * @param {number} [options.pad_to_multiple_of=2] The number to pad the sequence to a multiple of. - * @param {boolean} [options.do_normalize_per_mel_bins=true] Whether or not to zero-mean unit-variance normalize the input per mel-channel. - * @param {boolean} [options.return_attention_mask=true] Whether to return the attention mask. - * @returns {Promise<{ input_features: Tensor, attention_mask?: Tensor }>} A Promise resolving to an object containing the extracted input features and attention masks as Tensors. - */ - async _call(audio, { - padding = true, - pad_to_multiple_of = 2, - do_normalize_per_mel_bins = true, - return_attention_mask = true - } = {}) { - validate_audio_inputs(audio, "SeamlessM4TFeatureExtractor"); - let features = await this._extract_fbank_features(audio, this.config.max_length); - if (do_normalize_per_mel_bins) { - const [num_features, feature_size] = features.dims; - const data = features.data; - for (let i = 0; i < feature_size; ++i) { - let sum = 0; - for (let j = 0; j < num_features; ++j) { - sum += data[j * feature_size + i]; - } - const mean = sum / num_features; - let variance = 0; - for (let j = 0; j < num_features; ++j) { - variance += (data[j * feature_size + i] - mean) ** 2; - } - variance /= num_features - 1; - const std = Math.sqrt(variance + 1e-7); - for (let j = 0; j < num_features; ++j) { - const index = j * feature_size + i; - data[index] = (data[index] - mean) / std; - } - } - } - let padded_attention_mask; - if (padding) { - const [num_frames2, num_channels2] = features.dims; - const data = ( - /** @type {Float32Array} */ - features.data - ); - const pad_size = num_frames2 % pad_to_multiple_of; - if (pad_size > 0) { - const padded_data = new Float32Array(num_channels2 * (num_frames2 + pad_size)); - padded_data.set(data); - padded_data.fill(this.config.padding_value, data.length); - const numPaddedFrames = num_frames2 + pad_size; - features = new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_4__.Tensor( - features.type, - padded_data, - [numPaddedFrames, num_channels2] - ); - if (return_attention_mask) { - padded_attention_mask = new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_4__.Tensor( - "int64", - new BigInt64Array(numPaddedFrames), - [1, numPaddedFrames] - ); - padded_attention_mask.data.fill(1n, 0, num_frames2); - } - } - } - const [num_frames, num_channels] = features.dims; - const stride = this.config.stride; - const remainder = num_frames % stride; - if (remainder !== 0) { - throw new Error(`The number of frames (${num_frames}) must be a multiple of the stride (${stride}).`); - } - const input_features = features.view( - 1, - Math.floor(num_frames / stride), - num_channels * stride - ); - const result = { input_features }; - if (return_attention_mask) { - const reshapedNumFrames = input_features.dims[1]; - const attention_mask_data = new BigInt64Array(reshapedNumFrames); - if (padded_attention_mask) { - const padded_attention_mask_data = padded_attention_mask.data; - for (let i = 1, j = 0; i < num_frames; i += stride, ++j) { - attention_mask_data[j] = padded_attention_mask_data[i]; - } - } else { - attention_mask_data.fill(1n); - } - result.attention_mask = new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_4__.Tensor( - "int64", - attention_mask_data, - [1, reshapedNumFrames] - ); - } - return result; - } - } - class ASTFeatureExtractor extends FeatureExtractor { - constructor(config) { - super(config); - const sampling_rate = this.config.sampling_rate; - const mel_filters = (0, _utils_audio_js__WEBPACK_IMPORTED_MODULE_6__2.mel_filter_bank)( - 256, - // num_frequency_bins - this.config.num_mel_bins, - // num_mel_filters - 20, - // min_frequency - Math.floor(sampling_rate / 2), - // max_frequency - sampling_rate, - // sampling_rate - null, - // norm - "kaldi", - // mel_scale - true - // triangularize_in_mel_space - ); - for (let i = 0; i < mel_filters.length; ++i) { - mel_filters[i].push(0); - } - this.mel_filters = mel_filters; - this.window = (0, _utils_audio_js__WEBPACK_IMPORTED_MODULE_6__2.window_function)(400, "hann", { - periodic: false - }); - this.mean = this.config.mean; - this.std = this.config.std; - } - /** - * Computes the log-Mel spectrogram of the provided audio waveform. - * @param {Float32Array|Float64Array} waveform The audio waveform to process. - * @param {number} max_length The maximum number of frames to return. - * @returns {Promise} An object containing the log-Mel spectrogram data as a Float32Array and its dimensions as an array of numbers. - */ - async _extract_fbank_features(waveform, max_length) { - return (0, _utils_audio_js__WEBPACK_IMPORTED_MODULE_6__2.spectrogram)( - waveform, - this.window, - // window - 400, - // frame_length - 160, - // hop_length - { - fft_length: 512, - power: 2, - center: false, - preemphasis: 0.97, - mel_filters: this.mel_filters, - log_mel: "log", - mel_floor: 1192092955078125e-22, - remove_dc_offset: true, - // Custom - max_num_frames: max_length, - transpose: true - } - ); - } - /** - * Asynchronously extracts features from a given audio using the provided configuration. - * @param {Float32Array|Float64Array} audio The audio data as a Float32Array/Float64Array. - * @returns {Promise<{ input_values: Tensor }>} A Promise resolving to an object containing the extracted input features as a Tensor. - */ - async _call(audio) { - validate_audio_inputs(audio, "ASTFeatureExtractor"); - const features = await this._extract_fbank_features(audio, this.config.max_length); - if (this.config.do_normalize) { - const denom = this.std * 2; - const features_data = features.data; - for (let i = 0; i < features_data.length; ++i) { - features_data[i] = (features_data[i] - this.mean) / denom; - } - } - return { - input_values: features.unsqueeze_(0) - }; - } - } - class ClapFeatureExtractor extends FeatureExtractor { - constructor(config) { - super(config); - this.mel_filters = (0, _utils_audio_js__WEBPACK_IMPORTED_MODULE_6__2.mel_filter_bank)( - this.config.nb_frequency_bins, - // num_frequency_bins - this.config.feature_size, - // num_mel_filters - this.config.frequency_min, - // min_frequency - this.config.frequency_max, - // max_frequency - this.config.sampling_rate, - // sampling_rate - null, - // norm - "htk" - // mel_scale - ); - this.mel_filters_slaney = (0, _utils_audio_js__WEBPACK_IMPORTED_MODULE_6__2.mel_filter_bank)( - this.config.nb_frequency_bins, - // num_frequency_bins - this.config.feature_size, - // num_mel_filters - this.config.frequency_min, - // min_frequency - this.config.frequency_max, - // max_frequency - this.config.sampling_rate, - // sampling_rate - "slaney", - // norm - "slaney" - // mel_scale - ); - this.window = (0, _utils_audio_js__WEBPACK_IMPORTED_MODULE_6__2.window_function)(this.config.fft_window_size, "hann"); - } - /** - * Extracts the mel spectrogram and prepares it for the mode based on the `truncation` and `padding` arguments. - * - * Four different path are possible: - * - `truncation="fusion"` and the length of the waveform is greater than the max length: the mel spectrogram - * will be computed on the entire audio. 3 random crops and a dowsampled version of the full mel spectrogram - * are then stacked together. They will later be used for `feature_fusion`. - * - `truncation="rand_trunc"` and the length of the waveform is smaller than the max length: the audio is - * padded based on `padding`. - * - `truncation="fusion"` and the length of the waveform is smaller than the max length: the audio is padded - * based on `padding`, and is repeated `4` times. - * - `truncation="rand_trunc"` and the length of the waveform is greater than the max length: the mel - * spectrogram will be computed on a random crop of the waveform. - * - * @param {Float32Array|Float64Array} waveform The input waveform. - * @param {number} max_length The maximum length of the waveform. - * @param {string} truncation The truncation strategy to use. - * @param {string} padding The padding strategy to use. - * @returns {Promise} An object containing the mel spectrogram data as a Float32Array, its dimensions as an array of numbers, and a boolean indicating whether the waveform was longer than the max length. - * @private - */ - async _get_input_mel(waveform, max_length, truncation, padding) { - let input_mel; - let longer = false; - const diff = waveform.length - max_length; - if (diff > 0) { - if (truncation === "rand_trunc") { - longer = true; - const idx = Math.floor(Math.random() * (diff + 1)); - waveform = waveform.subarray(idx, idx + max_length); - input_mel = await this._extract_fbank_features(waveform, this.mel_filters_slaney, this.config.nb_max_samples); - } else { - throw new Error(`Truncation strategy "${truncation}" not implemented`); - } - } else { - if (diff < 0) { - let padded = new Float64Array(max_length); - padded.set(waveform); - if (padding === "repeat") { - for (let i = waveform.length; i < max_length; i += waveform.length) { - padded.set(waveform.subarray(0, Math.min(waveform.length, max_length - i)), i); - } - } else if (padding === "repeatpad") { - for (let i = waveform.length; i < -diff; i += waveform.length) { - padded.set(waveform, i); - } - } - waveform = padded; - } - if (truncation === "fusion") { - throw new Error(`Truncation strategy "${truncation}" not implemented`); - } - input_mel = await this._extract_fbank_features(waveform, this.mel_filters_slaney, this.config.nb_max_samples); - } - return input_mel.unsqueeze_(0); - } - /** - * Compute the log-mel spectrogram of the provided `waveform` using the Hann window. - * In CLAP, two different filter banks are used depending on the truncation pattern: - * - `self.mel_filters`: they correspond to the default parameters of `torchaudio` which can be obtained from - * calling `torchaudio.transforms.MelSpectrogram().mel_scale.fb`. These filters are used when `truncation` - * is set to `"fusion"`. - * - `self.mel_filteres_slaney` : they correspond to the default parameters of `librosa` which used - * `librosa.filters.mel` when computing the mel spectrogram. These filters were only used in the original - * implementation when the truncation mode is not `"fusion"`. - * - * @param {Float32Array|Float64Array} waveform The audio waveform to process. - * @param {number[][]} mel_filters The mel filters to use. - * @param {number} [max_length=null] The maximum number of frames to return. - * @returns {Promise} An object containing the log-Mel spectrogram data as a Float32Array and its dimensions as an array of numbers. - */ - async _extract_fbank_features(waveform, mel_filters, max_length = null) { - return (0, _utils_audio_js__WEBPACK_IMPORTED_MODULE_6__2.spectrogram)( - waveform, - this.window, - // window - this.config.fft_window_size, - // frame_length - this.config.hop_length, - // hop_length - { - power: 2, - mel_filters, - log_mel: "dB", - // Custom - max_num_frames: max_length, - do_pad: false, - transpose: true - } - ); - } - /** - * Asynchronously extracts features from a given audio using the provided configuration. - * @param {Float32Array|Float64Array} audio The audio data as a Float32Array/Float64Array. - * @returns {Promise<{ input_features: Tensor }>} A Promise resolving to an object containing the extracted input features as a Tensor. - */ - async _call(audio, { - max_length = null - } = {}) { - validate_audio_inputs(audio, "ClapFeatureExtractor"); - const padded_inputs = await this._get_input_mel( - audio, - max_length ?? this.config.nb_max_samples, - this.config.truncation, - this.config.padding - ); - return { - input_features: padded_inputs.unsqueeze_(0) - }; - } - } - class PyAnnoteFeatureExtractor extends FeatureExtractor { - /** - * Asynchronously extracts features from a given audio using the provided configuration. - * @param {Float32Array|Float64Array} audio The audio data as a Float32Array/Float64Array. - * @returns {Promise<{ input_values: Tensor; }>} The extracted input features. - */ - async _call(audio) { - validate_audio_inputs(audio, "PyAnnoteFeatureExtractor"); - if (audio instanceof Float64Array) { - audio = new Float32Array(audio); - } - const shape = [ - 1, - /* batch_size */ - 1, - /* num_channels */ - audio.length - /* num_samples */ - ]; - return { - input_values: new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_4__.Tensor("float32", audio, shape) - }; - } - /** - * NOTE: Can return fractional values. `Math.ceil` will ensure correct value. - * @param {number} samples The number of frames in the audio. - * @returns {number} The number of frames in the audio. - */ - samples_to_frames(samples) { - return (samples - this.config.offset) / this.config.step; - } - /** - * Post-processes the speaker diarization logits output by the model. - * @param {Tensor} logits The speaker diarization logits output by the model. - * @param {number} num_samples Number of samples in the input audio. - * @returns {Array>} The post-processed speaker diarization results. - */ - post_process_speaker_diarization(logits, num_samples) { - const ratio = num_samples / this.samples_to_frames(num_samples) / this.config.sampling_rate; - const results = []; - for (const scores of logits.tolist()) { - const accumulated_segments = []; - let current_speaker = -1; - for (let i = 0; i < scores.length; ++i) { - const probabilities = (0, _utils_maths_js__WEBPACK_IMPORTED_MODULE_3__.softmax)(scores[i]); - const [score, id] = (0, _utils_maths_js__WEBPACK_IMPORTED_MODULE_3__.max)(probabilities); - const [start, end] = [i, i + 1]; - if (id !== current_speaker) { - current_speaker = id; - accumulated_segments.push({ id, start, end, score }); - } else { - accumulated_segments.at(-1).end = end; - accumulated_segments.at(-1).score += score; - } - } - results.push(accumulated_segments.map( - // Convert frame-space to time-space - // and compute the confidence - ({ id, start, end, score }) => ({ - id, - start: start * ratio, - end: end * ratio, - confidence: score / (end - start) - }) - )); - } - return results; - } - } - class WeSpeakerFeatureExtractor extends FeatureExtractor { - constructor(config) { - super(config); - const sampling_rate = this.config.sampling_rate; - const mel_filters = (0, _utils_audio_js__WEBPACK_IMPORTED_MODULE_6__2.mel_filter_bank)( - 256, - // num_frequency_bins - this.config.num_mel_bins, - // num_mel_filters - 20, - // min_frequency - Math.floor(sampling_rate / 2), - // max_frequency - sampling_rate, - // sampling_rate - null, - // norm - "kaldi", - // mel_scale - true - // triangularize_in_mel_space - ); - for (let i = 0; i < mel_filters.length; ++i) { - mel_filters[i].push(0); - } - this.mel_filters = mel_filters; - this.window = (0, _utils_audio_js__WEBPACK_IMPORTED_MODULE_6__2.window_function)(400, "hamming", { - periodic: false - }); - this.min_num_frames = this.config.min_num_frames; - } - /** - * Computes the log-Mel spectrogram of the provided audio waveform. - * @param {Float32Array|Float64Array} waveform The audio waveform to process. - * @returns {Promise} An object containing the log-Mel spectrogram data as a Float32Array and its dimensions as an array of numbers. - */ - async _extract_fbank_features(waveform) { - waveform = waveform.map((x) => x * 32768); - return (0, _utils_audio_js__WEBPACK_IMPORTED_MODULE_6__2.spectrogram)( - waveform, - this.window, - // window - 400, - // frame_length - 160, - // hop_length - { - fft_length: 512, - power: 2, - center: false, - preemphasis: 0.97, - mel_filters: this.mel_filters, - log_mel: "log", - mel_floor: 1192092955078125e-22, - remove_dc_offset: true, - // Custom - transpose: true, - min_num_frames: this.min_num_frames - } - ); - } - /** - * Asynchronously extracts features from a given audio using the provided configuration. - * @param {Float32Array|Float64Array} audio The audio data as a Float32Array/Float64Array. - * @returns {Promise<{ input_features: Tensor }>} A Promise resolving to an object containing the extracted input features as a Tensor. - */ - async _call(audio) { - validate_audio_inputs(audio, "WeSpeakerFeatureExtractor"); - const features = (await this._extract_fbank_features(audio)).unsqueeze_(0); - if (this.config.fbank_centering_span === null) { - const meanData = ( - /** @type {Float32Array} */ - features.mean(1).data - ); - const featuresData = ( - /** @type {Float32Array} */ - features.data - ); - const [batch_size, num_frames, feature_size] = features.dims; - for (let i = 0; i < batch_size; ++i) { - const offset1 = i * num_frames * feature_size; - const offset2 = i * feature_size; - for (let j = 0; j < num_frames; ++j) { - const offset3 = offset1 + j * feature_size; - for (let k = 0; k < feature_size; ++k) { - featuresData[offset3 + k] -= meanData[offset2 + k]; - } - } - } - } - return { - input_features: features - }; - } - } - class SpeechT5FeatureExtractor extends FeatureExtractor { - } - class Processor extends _utils_generic_js__WEBPACK_IMPORTED_MODULE_0__.Callable { - /** - * Creates a new Processor with the given feature extractor. - * @param {FeatureExtractor} feature_extractor The function used to extract features from the input. - */ - constructor(feature_extractor) { - super(); - this.feature_extractor = feature_extractor; - } - /** - * Calls the feature_extractor function with the given input. - * @param {any} input The input to extract features from. - * @param {...any} args Additional arguments. - * @returns {Promise} A Promise that resolves with the extracted features. - */ - async _call(input, ...args) { - return await this.feature_extractor(input, ...args); - } - } - class SamProcessor extends Processor { - /** - * @borrows SamImageProcessor#_call as _call - */ - async _call(...args) { - return await this.feature_extractor(...args); - } - /** - * @borrows SamImageProcessor#post_process_masks as post_process_masks - */ - post_process_masks(...args) { - return this.feature_extractor.post_process_masks(...args); - } - /** - * @borrows SamImageProcessor#reshape_input_points as reshape_input_points - */ - reshape_input_points(...args) { - return this.feature_extractor.reshape_input_points(...args); - } - } - class WhisperProcessor extends Processor { - /** - * Calls the feature_extractor function with the given audio input. - * @param {any} audio The audio input to extract features from. - * @returns {Promise} A Promise that resolves with the extracted features. - */ - async _call(audio) { - return await this.feature_extractor(audio); - } - } - class Wav2Vec2ProcessorWithLM extends Processor { - /** - * Calls the feature_extractor function with the given audio input. - * @param {any} audio The audio input to extract features from. - * @returns {Promise} A Promise that resolves with the extracted features. - */ - async _call(audio) { - return await this.feature_extractor(audio); - } - } - class PyAnnoteProcessor extends Processor { - /** - * Calls the feature_extractor function with the given audio input. - * @param {any} audio The audio input to extract features from. - * @returns {Promise} A Promise that resolves with the extracted features. - */ - async _call(audio) { - return await this.feature_extractor(audio); - } - post_process_speaker_diarization(...args) { - return this.feature_extractor.post_process_speaker_diarization(...args); - } - } - class SpeechT5Processor extends Processor { - /** - * Calls the feature_extractor function with the given input. - * @param {any} input The input to extract features from. - * @returns {Promise} A Promise that resolves with the extracted features. - */ - async _call(input) { - return await this.feature_extractor(input); - } - } - class OwlViTProcessor extends Processor { - } - class Florence2Processor extends Processor { - constructor(feature_extractor) { - super(feature_extractor); - const { - tasks_answer_post_processing_type, - task_prompts_without_inputs, - task_prompts_with_input - } = feature_extractor.config; - this.tasks_answer_post_processing_type = new Map(Object.entries(tasks_answer_post_processing_type ?? {})); - this.task_prompts_without_inputs = new Map(Object.entries(task_prompts_without_inputs ?? {})); - this.task_prompts_with_input = new Map(Object.entries(task_prompts_with_input ?? {})); - this.regexes = { - quad_boxes: /(.+?)/gm, - bboxes: /([^<]+)?/gm - }; - this.size_per_bin = 1e3; - } - /** - * Helper function to construct prompts from input texts - * @param {string|string[]} text - * @returns {string[]} - */ - construct_prompts(text) { - if (typeof text === "string") { - text = [text]; - } - const prompts = []; - for (const t of text) { - if (this.task_prompts_without_inputs.has(t)) { - prompts.push(this.task_prompts_without_inputs.get(t)); - } else { - for (const [task, prompt] of this.task_prompts_with_input) { - if (t.includes(task)) { - prompts.push(prompt.replaceAll("{input}", t).replaceAll(task, "")); - break; - } - } - if (prompts.length !== text.length) { - prompts.push(t); - } - } - } - return prompts; - } - /** - * Post-process the output of the model to each of the task outputs. - * @param {string} text The text to post-process. - * @param {string} task The task to post-process the text for. - * @param {[number, number]} image_size The size of the image. height x width. - */ - post_process_generation(text, task, image_size) { - const task_answer_post_processing_type = this.tasks_answer_post_processing_type.get(task) ?? "pure_text"; - text = text.replaceAll("", "").replaceAll("", ""); - let final_answer; - switch (task_answer_post_processing_type) { - case "pure_text": - final_answer = text; - break; - case "description_with_bboxes": - case "bboxes": - case "phrase_grounding": - case "ocr": - const key = task_answer_post_processing_type === "ocr" ? "quad_boxes" : "bboxes"; - const matches = text.matchAll(this.regexes[key]); - const labels = []; - const items = []; - for (const [_, label, ...locations] of matches) { - labels.push(label ? label.trim() : labels.at(-1) ?? ""); - items.push( - locations.map((x, i) => ( - // NOTE: Add 0.5 to use the center position of the bin as the coordinate. - (Number(x) + 0.5) / this.size_per_bin * image_size[i % 2] - )) - ); - } - final_answer = { labels, [key]: items }; - break; - default: - throw new Error(`Task "${task}" (of type "${task_answer_post_processing_type}") not yet implemented.`); - } - return { [task]: final_answer }; - } - } - class AutoProcessor { - /** - * Instantiate one of the processor classes of the library from a pretrained model. - * - * The processor class to instantiate is selected based on the `feature_extractor_type` property of the config object - * (either passed as an argument or loaded from `pretrained_model_name_or_path` if possible) - * - * @param {string} pretrained_model_name_or_path The name or path of the pretrained model. Can be either: - * - A string, the *model id* of a pretrained processor hosted inside a model repo on huggingface.co. - * Valid model ids can be located at the root-level, like `bert-base-uncased`, or namespaced under a - * user or organization name, like `dbmdz/bert-base-german-cased`. - * - A path to a *directory* containing processor files, e.g., `./my_model_directory/`. - * @param {import('./utils/hub.js').PretrainedOptions} options Additional options for loading the processor. - * - * @returns {Promise} A new instance of the Processor class. - */ - static async from_pretrained(pretrained_model_name_or_path, { - progress_callback = null, - config = null, - cache_dir = null, - local_files_only = false, - revision = "main" - } = {}) { - let preprocessorConfig = config ?? await (0, _utils_hub_js__WEBPACK_IMPORTED_MODULE_2__.getModelJSON)(pretrained_model_name_or_path, "preprocessor_config.json", true, { - progress_callback, - config, - cache_dir, - local_files_only, - revision - }); - let key = preprocessorConfig.feature_extractor_type ?? preprocessorConfig.image_processor_type; - let feature_extractor_class = this.FEATURE_EXTRACTOR_CLASS_MAPPING[key]; - if (!feature_extractor_class) { - if (preprocessorConfig.size !== void 0) { - console.warn(`Feature extractor type "${key}" not found, assuming ImageFeatureExtractor due to size parameter in config.`); - feature_extractor_class = ImageFeatureExtractor; - } else { - throw new Error(`Unknown Feature Extractor type: ${key}`); - } - } - let processor_class = this.PROCESSOR_CLASS_MAPPING[preprocessorConfig.processor_class] ?? Processor; - let feature_extractor = new feature_extractor_class(preprocessorConfig); - return new processor_class(feature_extractor); - } - } - __publicField(AutoProcessor, "FEATURE_EXTRACTOR_CLASS_MAPPING", { - ImageFeatureExtractor, - WhisperFeatureExtractor, - ViTFeatureExtractor, - MobileViTFeatureExtractor, - MobileViTImageProcessor, - MobileNetV1FeatureExtractor, - MobileNetV2FeatureExtractor, - MobileNetV3FeatureExtractor, - MobileNetV4FeatureExtractor, - OwlViTFeatureExtractor, - Owlv2ImageProcessor, - CLIPFeatureExtractor, - CLIPImageProcessor, - Florence2Processor, - ChineseCLIPFeatureExtractor, - SiglipImageProcessor, - ConvNextFeatureExtractor, - ConvNextImageProcessor, - SegformerFeatureExtractor, - SapiensFeatureExtractor, - BitImageProcessor, - DPTImageProcessor, - DPTFeatureExtractor, - PvtImageProcessor, - GLPNFeatureExtractor, - BeitFeatureExtractor, - DeiTFeatureExtractor, - DetrFeatureExtractor, - RTDetrImageProcessor, - MaskFormerFeatureExtractor, - YolosFeatureExtractor, - DonutFeatureExtractor, - NougatImageProcessor, - EfficientNetImageProcessor, - ViTImageProcessor, - VitMatteImageProcessor, - SamImageProcessor, - Swin2SRImageProcessor, - Wav2Vec2FeatureExtractor, - SeamlessM4TFeatureExtractor, - SpeechT5FeatureExtractor, - ASTFeatureExtractor, - ClapFeatureExtractor, - PyAnnoteFeatureExtractor, - WeSpeakerFeatureExtractor - }); - __publicField(AutoProcessor, "PROCESSOR_CLASS_MAPPING", { - WhisperProcessor, - Wav2Vec2ProcessorWithLM, - PyAnnoteProcessor, - SamProcessor, - SpeechT5Processor, - OwlViTProcessor, - Florence2Processor - }); - } - ), - /***/ - "./src/tokenizers.js": ( - /*!***************************!*\ - !*** ./src/tokenizers.js ***! - \***************************/ - /***/ - (__unused_webpack___webpack_module__, __webpack_exports__2, __webpack_require__2) => { - __webpack_require__2.r(__webpack_exports__2); - __webpack_require__2.d(__webpack_exports__2, { - /* harmony export */ - AlbertTokenizer: () => ( - /* binding */ - AlbertTokenizer - ), - /* harmony export */ - AutoTokenizer: () => ( - /* binding */ - AutoTokenizer - ), - /* harmony export */ - BartTokenizer: () => ( - /* binding */ - BartTokenizer - ), - /* harmony export */ - BertTokenizer: () => ( - /* binding */ - BertTokenizer - ), - /* harmony export */ - BlenderbotSmallTokenizer: () => ( - /* binding */ - BlenderbotSmallTokenizer - ), - /* harmony export */ - BlenderbotTokenizer: () => ( - /* binding */ - BlenderbotTokenizer - ), - /* harmony export */ - BloomTokenizer: () => ( - /* binding */ - BloomTokenizer - ), - /* harmony export */ - CLIPTokenizer: () => ( - /* binding */ - CLIPTokenizer - ), - /* harmony export */ - CamembertTokenizer: () => ( - /* binding */ - CamembertTokenizer - ), - /* harmony export */ - CodeGenTokenizer: () => ( - /* binding */ - CodeGenTokenizer - ), - /* harmony export */ - CodeLlamaTokenizer: () => ( - /* binding */ - CodeLlamaTokenizer - ), - /* harmony export */ - CohereTokenizer: () => ( - /* binding */ - CohereTokenizer - ), - /* harmony export */ - ConvBertTokenizer: () => ( - /* binding */ - ConvBertTokenizer - ), - /* harmony export */ - DebertaTokenizer: () => ( - /* binding */ - DebertaTokenizer - ), - /* harmony export */ - DebertaV2Tokenizer: () => ( - /* binding */ - DebertaV2Tokenizer - ), - /* harmony export */ - DistilBertTokenizer: () => ( - /* binding */ - DistilBertTokenizer - ), - /* harmony export */ - ElectraTokenizer: () => ( - /* binding */ - ElectraTokenizer - ), - /* harmony export */ - EsmTokenizer: () => ( - /* binding */ - EsmTokenizer - ), - /* harmony export */ - FalconTokenizer: () => ( - /* binding */ - FalconTokenizer - ), - /* harmony export */ - GPT2Tokenizer: () => ( - /* binding */ - GPT2Tokenizer - ), - /* harmony export */ - GPTNeoXTokenizer: () => ( - /* binding */ - GPTNeoXTokenizer - ), - /* harmony export */ - GemmaTokenizer: () => ( - /* binding */ - GemmaTokenizer - ), - /* harmony export */ - Grok1Tokenizer: () => ( - /* binding */ - Grok1Tokenizer - ), - /* harmony export */ - HerbertTokenizer: () => ( - /* binding */ - HerbertTokenizer - ), - /* harmony export */ - LlamaTokenizer: () => ( - /* binding */ - LlamaTokenizer - ), - /* harmony export */ - M2M100Tokenizer: () => ( - /* binding */ - M2M100Tokenizer - ), - /* harmony export */ - MBart50Tokenizer: () => ( - /* binding */ - MBart50Tokenizer - ), - /* harmony export */ - MBartTokenizer: () => ( - /* binding */ - MBartTokenizer - ), - /* harmony export */ - MPNetTokenizer: () => ( - /* binding */ - MPNetTokenizer - ), - /* harmony export */ - MarianTokenizer: () => ( - /* binding */ - MarianTokenizer - ), - /* harmony export */ - MobileBertTokenizer: () => ( - /* binding */ - MobileBertTokenizer - ), - /* harmony export */ - NllbTokenizer: () => ( - /* binding */ - NllbTokenizer - ), - /* harmony export */ - NougatTokenizer: () => ( - /* binding */ - NougatTokenizer - ), - /* harmony export */ - PreTrainedTokenizer: () => ( - /* binding */ - PreTrainedTokenizer - ), - /* harmony export */ - Qwen2Tokenizer: () => ( - /* binding */ - Qwen2Tokenizer - ), - /* harmony export */ - RoFormerTokenizer: () => ( - /* binding */ - RoFormerTokenizer - ), - /* harmony export */ - RobertaTokenizer: () => ( - /* binding */ - RobertaTokenizer - ), - /* harmony export */ - SiglipTokenizer: () => ( - /* binding */ - SiglipTokenizer - ), - /* harmony export */ - SpeechT5Tokenizer: () => ( - /* binding */ - SpeechT5Tokenizer - ), - /* harmony export */ - SqueezeBertTokenizer: () => ( - /* binding */ - SqueezeBertTokenizer - ), - /* harmony export */ - T5Tokenizer: () => ( - /* binding */ - T5Tokenizer - ), - /* harmony export */ - TokenizerModel: () => ( - /* binding */ - TokenizerModel - ), - /* harmony export */ - VitsTokenizer: () => ( - /* binding */ - VitsTokenizer - ), - /* harmony export */ - Wav2Vec2CTCTokenizer: () => ( - /* binding */ - Wav2Vec2CTCTokenizer - ), - /* harmony export */ - WhisperTokenizer: () => ( - /* binding */ - WhisperTokenizer - ), - /* harmony export */ - XLMRobertaTokenizer: () => ( - /* binding */ - XLMRobertaTokenizer - ), - /* harmony export */ - XLMTokenizer: () => ( - /* binding */ - XLMTokenizer - ), - /* harmony export */ - is_chinese_char: () => ( - /* binding */ - is_chinese_char - ) - /* harmony export */ - }); - var _utils_generic_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__2( - /*! ./utils/generic.js */ - "./src/utils/generic.js" - ); - var _utils_core_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__2( - /*! ./utils/core.js */ - "./src/utils/core.js" - ); - var _utils_hub_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__2( - /*! ./utils/hub.js */ - "./src/utils/hub.js" - ); - var _utils_maths_js__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__2( - /*! ./utils/maths.js */ - "./src/utils/maths.js" - ); - var _utils_tensor_js__WEBPACK_IMPORTED_MODULE_4__ = __webpack_require__2( - /*! ./utils/tensor.js */ - "./src/utils/tensor.js" - ); - var _utils_data_structures_js__WEBPACK_IMPORTED_MODULE_5__ = __webpack_require__2( - /*! ./utils/data-structures.js */ - "./src/utils/data-structures.js" - ); - var _huggingface_jinja__WEBPACK_IMPORTED_MODULE_6__ = __webpack_require__2( - /*! @huggingface/jinja */ - "./node_modules/@huggingface/jinja/dist/index.js" - ); - var _models_whisper_common_whisper_js__WEBPACK_IMPORTED_MODULE_7__ = __webpack_require__2( - /*! ./models/whisper/common_whisper.js */ - "./src/models/whisper/common_whisper.js" - ); - var _utils_constants_js__WEBPACK_IMPORTED_MODULE_8__ = __webpack_require__2( - /*! ./utils/constants.js */ - "./src/utils/constants.js" - ); - async function loadTokenizer(pretrained_model_name_or_path, options) { - const info = await Promise.all([ - (0, _utils_hub_js__WEBPACK_IMPORTED_MODULE_2__.getModelJSON)(pretrained_model_name_or_path, "tokenizer.json", true, options), - (0, _utils_hub_js__WEBPACK_IMPORTED_MODULE_2__.getModelJSON)(pretrained_model_name_or_path, "tokenizer_config.json", true, options) - ]); - if (options.legacy !== null) { - info[1].legacy = options.legacy; - } - return info; - } - function regexSplit(text, regex) { - const result = []; - let prev = 0; - for (const match of text.matchAll(regex)) { - const fullMatch = match[0]; - if (prev < match.index) { - result.push(text.slice(prev, match.index)); - } - if (fullMatch.length > 0) { - result.push(fullMatch); - } - prev = match.index + fullMatch.length; - } - if (prev < text.length) { - result.push(text.slice(prev)); - } - return result; - } - function createPattern(pattern, invert = true) { - if (pattern.Regex !== void 0) { - let regex = pattern.Regex.replace(/\\([#&~])/g, "$1"); - for (const [key, value] of PROBLEMATIC_REGEX_MAP) { - regex = regex.replaceAll(key, value); - } - return new RegExp(regex, "gu"); - } else if (pattern.String !== void 0) { - const escaped = (0, _utils_core_js__WEBPACK_IMPORTED_MODULE_1__.escapeRegExp)(pattern.String); - return new RegExp(invert ? escaped : `(${escaped})`, "gu"); - } else { - console.warn("Unknown pattern type:", pattern); - return null; - } - } - function objectToMap(obj) { - return new Map(Object.entries(obj)); - } - function prepareTensorForDecode(tensor) { - const dims = tensor.dims; - switch (dims.length) { - case 1: - return tensor.tolist(); - case 2: - if (dims[0] !== 1) { - throw new Error("Unable to decode tensor with `batch size !== 1`. Use `tokenizer.batch_decode(...)` for batched inputs."); - } - return tensor.tolist()[0]; - default: - throw new Error(`Expected tensor to have 1-2 dimensions, got ${dims.length}.`); - } - } - function clean_up_tokenization(text) { - return text.replace(/ \./g, ".").replace(/ \?/g, "?").replace(/ \!/g, "!").replace(/ ,/g, ",").replace(/ \' /g, "'").replace(/ n\'t/g, "n't").replace(/ \'m/g, "'m").replace(/ \'s/g, "'s").replace(/ \'ve/g, "'ve").replace(/ \'re/g, "'re"); - } - function remove_accents(text) { - return text.replace(new RegExp("\\p{M}", "gu"), ""); - } - function lowercase_and_remove_accent(text) { - return remove_accents(text.toLowerCase()); - } - function is_chinese_char(cp) { - return cp >= 19968 && cp <= 40959 || cp >= 13312 && cp <= 19903 || cp >= 131072 && cp <= 173791 || cp >= 173824 && cp <= 177983 || cp >= 177984 && cp <= 178207 || cp >= 178208 && cp <= 183983 || cp >= 63744 && cp <= 64255 || cp >= 194560 && cp <= 195103; - } - function fuse_unk(arr, tokens_to_ids, unk_token_id) { - const fused = []; - let i = 0; - while (i < arr.length) { - fused.push(arr[i]); - if ((tokens_to_ids.get(arr[i]) ?? unk_token_id) !== unk_token_id) { - ++i; - continue; - } - while (++i < arr.length && (tokens_to_ids.get(arr[i]) ?? unk_token_id) === unk_token_id) { - if (tokens_to_ids.get(fused.at(-1)) !== unk_token_id) { - fused[fused.length - 1] += arr[i]; - } - } - } - return fused; - } - function whitespace_split(text) { - return text.match(/\S+/g) || []; - } - const PUNCTUATION_REGEX = "\\u0021-\\u002F\\u003A-\\u0040\\u005B-\\u0060\\u007B-\\u007E"; - const PUNCTUATION_ONLY_REGEX = new RegExp(`^[${PUNCTUATION_REGEX}]+$`, "gu"); - const BLOOM_SPLIT_CHARS = ".,!?…。,、।۔،"; - const PROBLEMATIC_REGEX_MAP = /* @__PURE__ */ new Map([ - // This uses the case insensitive group modifier, which is not supported in JavaScript. - // When parsing the regex, an "Invalid group" error is thrown. - ["(?i:'s|'t|'re|'ve|'m|'ll|'d)", "(?:'([sS]|[tT]|[rR][eE]|[vV][eE]|[mM]|[lL][lL]|[dD]))"], - // Used to override the default (invalid) regex of the bloom pretokenizer. - // For more information, see https://github.com/huggingface/transformers.js/issues/94 - [` ?[^(\\s|[${BLOOM_SPLIT_CHARS}])]+`, ` ?[^\\s${BLOOM_SPLIT_CHARS}]+`] - ]); - class AddedToken { - /** - * Creates a new instance of AddedToken. - * @param {Object} config Added token configuration object. - * @param {string} config.content The content of the added token. - * @param {number} config.id The id of the added token. - * @param {boolean} [config.single_word=false] Whether this token must be a single word or can break words. - * @param {boolean} [config.lstrip=false] Whether this token should strip whitespaces on its left. - * @param {boolean} [config.rstrip=false] Whether this token should strip whitespaces on its right. - * @param {boolean} [config.normalized=false] Whether this token should be normalized. - * @param {boolean} [config.special=false] Whether this token is special. - */ - constructor(config) { - this.content = config.content; - this.id = config.id; - this.single_word = config.single_word ?? false; - this.lstrip = config.lstrip ?? false; - this.rstrip = config.rstrip ?? false; - this.special = config.special ?? false; - this.normalized = config.normalized ?? null; - } - } - class TokenizerModel extends _utils_generic_js__WEBPACK_IMPORTED_MODULE_0__.Callable { - /** - * Creates a new instance of TokenizerModel. - * @param {Object} config The configuration object for the TokenizerModel. - */ - constructor(config) { - super(); - this.config = config; - this.vocab = []; - this.tokens_to_ids = /* @__PURE__ */ new Map(); - this.unk_token_id = void 0; - this.unk_token = void 0; - this.end_of_word_suffix = void 0; - this.fuse_unk = this.config.fuse_unk ?? false; - } - /** - * Instantiates a new TokenizerModel instance based on the configuration object provided. - * @param {Object} config The configuration object for the TokenizerModel. - * @param {...*} args Optional arguments to pass to the specific TokenizerModel constructor. - * @returns {TokenizerModel} A new instance of a TokenizerModel. - * @throws Will throw an error if the TokenizerModel type in the config is not recognized. - */ - static fromConfig(config, ...args) { - switch (config.type) { - case "WordPiece": - return new WordPieceTokenizer(config); - case "Unigram": - return new Unigram(config, ...args); - case "BPE": - return new BPE(config); - default: - if (config.vocab) { - if (Array.isArray(config.vocab)) { - return new Unigram(config, ...args); - } else { - return new LegacyTokenizerModel(config, ...args); - } - } - throw new Error(`Unknown TokenizerModel type: ${config.type}`); - } - } - /** - * Internal function to call the TokenizerModel instance. - * @param {string[]} tokens The tokens to encode. - * @returns {string[]} The encoded tokens. - */ - _call(tokens) { - tokens = this.encode(tokens); - if (this.fuse_unk) { - tokens = fuse_unk(tokens, this.tokens_to_ids, this.unk_token_id); - } - return tokens; - } - /** - * Encodes a list of tokens into a list of token IDs. - * @param {string[]} tokens The tokens to encode. - * @returns {string[]} The encoded tokens. - * @throws Will throw an error if not implemented in a subclass. - */ - encode(tokens) { - throw Error("encode should be implemented in subclass."); - } - /** - * Converts a list of tokens into a list of token IDs. - * @param {string[]} tokens The tokens to convert. - * @returns {number[]} The converted token IDs. - */ - convert_tokens_to_ids(tokens) { - return tokens.map((t) => this.tokens_to_ids.get(t) ?? this.unk_token_id); - } - /** - * Converts a list of token IDs into a list of tokens. - * @param {number[]|bigint[]} ids The token IDs to convert. - * @returns {string[]} The converted tokens. - */ - convert_ids_to_tokens(ids) { - return ids.map((i) => this.vocab[i] ?? this.unk_token); - } - } - class WordPieceTokenizer extends TokenizerModel { - /** - * @param {Object} config The configuration object. - * @param {Object} config.vocab A mapping of tokens to ids. - * @param {string} config.unk_token The unknown token string. - * @param {string} config.continuing_subword_prefix The prefix to use for continuing subwords. - * @param {number} [config.max_input_chars_per_word=100] The maximum number of characters per word. - */ - constructor(config) { - super(config); - this.tokens_to_ids = objectToMap(config.vocab); - this.unk_token_id = this.tokens_to_ids.get(config.unk_token); - this.unk_token = config.unk_token; - this.max_input_chars_per_word = config.max_input_chars_per_word ?? 100; - this.vocab = new Array(this.tokens_to_ids.size); - for (const [key, value] of this.tokens_to_ids) { - this.vocab[value] = key; - } - } - /** - * Encodes an array of tokens using WordPiece encoding. - * @param {string[]} tokens The tokens to encode. - * @returns {string[]} An array of encoded tokens. - */ - encode(tokens) { - const outputTokens = []; - for (const token of tokens) { - const chars = [...token]; - if (chars.length > this.max_input_chars_per_word) { - outputTokens.push(this.unk_token); - continue; - } - let isUnknown = false; - let start = 0; - const subTokens = []; - while (start < chars.length) { - let end = chars.length; - let currentSubstring = null; - while (start < end) { - let substr = chars.slice(start, end).join(""); - if (start > 0) { - substr = this.config.continuing_subword_prefix + substr; - } - if (this.tokens_to_ids.has(substr)) { - currentSubstring = substr; - break; - } - --end; - } - if (currentSubstring === null) { - isUnknown = true; - break; - } - subTokens.push(currentSubstring); - start = end; - } - if (isUnknown) { - outputTokens.push(this.unk_token); - } else { - outputTokens.push(...subTokens); - } - } - return outputTokens; - } - } - class Unigram extends TokenizerModel { - /** - * Create a new Unigram tokenizer model. - * @param {Object} config The configuration object for the Unigram model. - * @param {number} config.unk_id The ID of the unknown token - * @param {any[][]} config.vocab A 2D array representing a mapping of tokens to scores. - * @param {Object} moreConfig Additional configuration object for the Unigram model. - */ - constructor(config, moreConfig) { - super(config); - const vocabSize = config.vocab.length; - this.vocab = new Array(vocabSize); - this.scores = new Array(vocabSize); - for (let i = 0; i < vocabSize; ++i) { - const piece = config.vocab[i]; - this.vocab[i] = piece[0]; - this.scores[i] = piece[1]; - } - this.unk_token_id = config.unk_id; - this.unk_token = this.vocab[config.unk_id]; - this.tokens_to_ids = new Map(this.vocab.map((x, i) => [x, i])); - this.bos_token = " "; - this.bos_token_id = this.tokens_to_ids.get(this.bos_token); - this.eos_token = moreConfig.eos_token; - this.eos_token_id = this.tokens_to_ids.get(this.eos_token); - this.unk_token = this.vocab[this.unk_token_id]; - this.minScore = (0, _utils_maths_js__WEBPACK_IMPORTED_MODULE_3__.min)(this.scores)[0]; - this.unk_score = this.minScore - 10; - this.scores[this.unk_token_id] = this.unk_score; - this.trie = new _utils_data_structures_js__WEBPACK_IMPORTED_MODULE_5__.CharTrie(); - this.trie.extend(this.vocab); - this.fuse_unk = true; - } - /** - * Populates lattice nodes. - * @param {TokenLattice} lattice The token lattice to populate with nodes. - */ - populateNodes(lattice) { - const chars = lattice.chars; - const mblen = 1; - let beginPos = 0; - while (beginPos < chars.length) { - let hasSingleNode = false; - const tokens = []; - const sliced = chars.slice(beginPos).join(""); - const prefixedTokens = this.trie.commonPrefixSearch(sliced); - for (const token of prefixedTokens) { - tokens.push(token); - const tokenId = this.tokens_to_ids.get(token); - const tokenScore = this.scores[tokenId]; - const n = (0, _utils_core_js__WEBPACK_IMPORTED_MODULE_1__.len)(token); - lattice.insert(beginPos, n, tokenScore, tokenId); - if (!hasSingleNode && n === mblen) { - hasSingleNode = true; - } - } - if (!hasSingleNode) { - lattice.insert(beginPos, mblen, this.unk_score, this.unk_token_id); - } - beginPos += mblen; - } - } - /** - * Encodes an array of tokens into an array of subtokens using the unigram model. - * - * @param {string} normalized The normalized string. - * @returns {string[]} An array of subtokens obtained by encoding the input tokens using the unigram model. - */ - tokenize(normalized) { - const lattice = new _utils_data_structures_js__WEBPACK_IMPORTED_MODULE_5__.TokenLattice(normalized, this.bos_token_id, this.eos_token_id); - this.populateNodes(lattice); - return lattice.tokens(); - } - /** - * Encodes an array of tokens using Unigram encoding. - * @param {string[]} tokens The tokens to encode. - * @returns {string[]} An array of encoded tokens. - */ - encode(tokens) { - const toReturn = []; - for (const token of tokens) { - const tokenized = this.tokenize(token); - toReturn.push(...tokenized); - } - return toReturn; - } - } - const BYTES_TO_UNICODE = (() => { - const bs = [ - ...Array.from({ length: "~".charCodeAt(0) - "!".charCodeAt(0) + 1 }, (_, i) => i + "!".charCodeAt(0)), - ...Array.from({ length: "¬".charCodeAt(0) - "¡".charCodeAt(0) + 1 }, (_, i) => i + "¡".charCodeAt(0)), - ...Array.from({ length: "ÿ".charCodeAt(0) - "®".charCodeAt(0) + 1 }, (_, i) => i + "®".charCodeAt(0)) - ]; - const cs = bs.slice(); - let n = 0; - for (let b = 0; b < 256; ++b) { - if (!bs.includes(b)) { - bs.push(b); - cs.push(256 + n); - n += 1; - } - } - const ccs = cs.map((n2) => String.fromCharCode(n2)); - return Object.fromEntries(bs.map((b, i) => [b, ccs[i]])); - })(); - const UNICODE_TO_BYTES = (0, _utils_core_js__WEBPACK_IMPORTED_MODULE_1__.reverseDictionary)(BYTES_TO_UNICODE); - class BPE extends TokenizerModel { - /** - * Create a BPE instance. - * @param {Object} config The configuration object for BPE. - * @param {Object} config.vocab A mapping of tokens to ids. - * @param {string[]|[string, string][]} config.merges An array of BPE merges as strings. - * @param {string} config.unk_token The unknown token used for out of vocabulary words. - * @param {string} config.end_of_word_suffix The suffix to place at the end of each word. - * @param {string} [config.continuing_subword_suffix] The suffix to insert between words. - * @param {boolean} [config.byte_fallback=false] Whether to use spm byte-fallback trick (defaults to False) - * @param {boolean} [config.ignore_merges=false] Whether or not to match tokens with the vocab before using merges. - */ - constructor(config) { - super(config); - this.tokens_to_ids = objectToMap(config.vocab); - this.unk_token_id = this.tokens_to_ids.get(config.unk_token); - this.unk_token = config.unk_token; - this.vocab = new Array(this.tokens_to_ids.size); - for (const [key, value] of this.tokens_to_ids) { - this.vocab[value] = key; - } - const use_new_merge_format = Array.isArray(config.merges[0]); - this.merges = use_new_merge_format ? ( - /** @type {[string, string][]} */ - config.merges - ) : ( - /** @type {string[]} */ - config.merges.map((x) => ( - /** @type {[string, string]} */ - x.split(" ", 2) - )) - ); - this.bpe_ranks = new Map(this.merges.map((x, i) => [JSON.stringify(x), i])); - this.end_of_word_suffix = config.end_of_word_suffix; - this.continuing_subword_suffix = config.continuing_subword_suffix ?? null; - this.byte_fallback = this.config.byte_fallback ?? false; - if (this.byte_fallback) { - this.text_encoder = new TextEncoder(); - } - this.ignore_merges = this.config.ignore_merges ?? false; - this.cache = /* @__PURE__ */ new Map(); - } - /** - * Apply Byte-Pair-Encoding (BPE) to a given token. Efficient heap-based priority - * queue implementation adapted from https://github.com/belladoreai/llama-tokenizer-js. - * @param {string} token The token to encode. - * @returns {string[]} The BPE encoded tokens. - */ - bpe(token) { - if (token.length === 0) { - return []; - } - const cached = this.cache.get(token); - if (cached !== void 0) { - return cached; - } - const word = Array.from(token); - if (this.end_of_word_suffix) { - word[word.length - 1] += this.end_of_word_suffix; - } - let result = []; - if (word.length > 1) { - const queue = new _utils_data_structures_js__WEBPACK_IMPORTED_MODULE_5__.PriorityQueue((a, b) => a.score < b.score); - let startingNode = { - token: word[0], - bias: 0, - prev: null, - next: null - }; - let previousNode = startingNode; - for (let i = 1; i < word.length; ++i) { - const currentNode = { - bias: i / word.length, - // Add fractional component to break ties - token: word[i], - prev: previousNode, - next: null - }; - previousNode.next = currentNode; - this._add_node(queue, previousNode); - previousNode = currentNode; - } - while (!queue.isEmpty()) { - const node = queue.pop(); - if (node.deleted || !node.next || node.next.deleted) continue; - node.deleted = true; - node.next.deleted = true; - if (node.prev) { - const newPreviousNode = { ...node.prev }; - node.prev.deleted = true; - node.prev = newPreviousNode; - if (newPreviousNode.prev) { - newPreviousNode.prev.next = newPreviousNode; - } else { - startingNode = newPreviousNode; - } - } - const merged = { - token: node.token + node.next.token, - bias: node.bias, - prev: node.prev, - next: node.next.next - }; - if (merged.prev) { - merged.prev.next = merged; - this._add_node(queue, merged.prev); - } else { - startingNode = merged; - } - if (merged.next) { - merged.next.prev = merged; - this._add_node(queue, merged); - } - } - for (let currentNode = startingNode; currentNode !== null; currentNode = currentNode.next) { - result.push(currentNode.token); - } - } else { - result = word; - } - if (this.continuing_subword_suffix) { - for (let i = 0; i < result.length - 1; ++i) { - result[i] += this.continuing_subword_suffix; - } - } - this.cache.set(token, result); - return result; - } - /** - * Helper function to add a node to the priority queue. - * @param {PriorityQueue} queue - * @param {BPENode} node - * @private - */ - _add_node(queue, node) { - const rank = this.bpe_ranks.get(JSON.stringify([node.token, node.next.token])); - if (rank !== void 0) { - node.score = rank + node.bias; - queue.push(node); - } - } - /** - * Encodes the input sequence of tokens using the BPE algorithm and returns the resulting subword tokens. - * @param {string[]} tokens The input sequence of tokens to encode. - * @returns {string[]} The resulting subword tokens after applying the BPE algorithm to the input sequence of tokens. - */ - encode(tokens) { - const outputTokens = []; - for (const token of tokens) { - if (this.ignore_merges && this.tokens_to_ids.has(token)) { - outputTokens.push(token); - continue; - } - const bpe_token_list = this.bpe(token); - for (const t of bpe_token_list) { - if (this.tokens_to_ids.has(t)) { - outputTokens.push(t); - } else if (this.byte_fallback) { - const byteTokens = Array.from(this.text_encoder.encode(t)).map((x) => `<0x${x.toString(16).toUpperCase().padStart(2, "0")}>`); - if (byteTokens.every((x) => this.tokens_to_ids.has(x))) { - outputTokens.push(...byteTokens); - } else { - outputTokens.push(this.unk_token); - } - } else { - outputTokens.push(this.unk_token); - } - } - } - return outputTokens; - } - } - class LegacyTokenizerModel extends TokenizerModel { - /** - * Create a LegacyTokenizerModel instance. - * @param {Object} config The configuration object for LegacyTokenizerModel. - * @param {Object} config.vocab A (possibly nested) mapping of tokens to ids. - * @param {Object} moreConfig Additional configuration object for the LegacyTokenizerModel model. - */ - constructor(config, moreConfig) { - super(config); - this.tokens_to_ids = objectToMap( - moreConfig.target_lang ? config.vocab[moreConfig.target_lang] : config.vocab - ); - this.bos_token = moreConfig.bos_token; - this.bos_token_id = this.tokens_to_ids.get(this.bos_token); - this.eos_token = moreConfig.eos_token; - this.eos_token_id = this.tokens_to_ids.get(this.eos_token); - this.pad_token = moreConfig.pad_token; - this.pad_token_id = this.tokens_to_ids.get(this.pad_token); - this.unk_token = moreConfig.unk_token; - this.unk_token_id = this.tokens_to_ids.get(this.unk_token); - this.vocab = new Array(this.tokens_to_ids.size); - for (const [key, value] of this.tokens_to_ids) { - this.vocab[value] = key; - } - } - encode(tokens) { - return tokens; - } - } - class Normalizer extends _utils_generic_js__WEBPACK_IMPORTED_MODULE_0__.Callable { - /** - * @param {Object} config The configuration object for the normalizer. - */ - constructor(config) { - super(); - this.config = config; - } - /** - * Factory method for creating normalizers from config objects. - * @static - * @param {Object} config The configuration object for the normalizer. - * @returns {Normalizer} A Normalizer object. - * @throws {Error} If an unknown Normalizer type is specified in the config. - */ - static fromConfig(config) { - if (config === null) return null; - switch (config.type) { - case "BertNormalizer": - return new BertNormalizer(config); - case "Precompiled": - return new Precompiled(config); - case "Sequence": - return new NormalizerSequence(config); - case "Replace": - return new Replace(config); - case "NFC": - return new NFC(config); - case "NFKC": - return new NFKC(config); - case "NFKD": - return new NFKD(config); - case "Strip": - return new StripNormalizer(config); - case "StripAccents": - return new StripAccents(config); - case "Lowercase": - return new Lowercase(config); - case "Prepend": - return new Prepend(config); - default: - throw new Error(`Unknown Normalizer type: ${config.type}`); - } - } - /** - * Normalize the input text. - * @abstract - * @param {string} text The text to normalize. - * @returns {string} The normalized text. - * @throws {Error} If this method is not implemented in a subclass. - */ - normalize(text) { - throw Error("normalize should be implemented in subclass."); - } - /** - * Alias for {@link Normalizer#normalize}. - * @param {string} text The text to normalize. - * @returns {string} The normalized text. - */ - _call(text) { - return this.normalize(text); - } - } - class Replace extends Normalizer { - /** - * Normalize the input text by replacing the pattern with the content. - * @param {string} text The input text to be normalized. - * @returns {string} The normalized text after replacing the pattern with the content. - */ - normalize(text) { - const pattern = createPattern(this.config.pattern); - return pattern === null ? text : text.replaceAll(pattern, this.config.content); - } - } - class NFC extends Normalizer { - /** - * Normalize the input text by applying Unicode normalization form C (NFC). - * @param {string} text The input text to be normalized. - * @returns {string} The normalized text. - */ - normalize(text) { - text = text.normalize("NFC"); - return text; - } - } - class NFKC extends Normalizer { - /** - * Normalize text using NFKC normalization. - * @param {string} text The text to be normalized. - * @returns {string} The normalized text. - */ - normalize(text) { - text = text.normalize("NFKC"); - return text; - } - } - class NFKD extends Normalizer { - /** - * Normalize text using NFKD normalization. - * @param {string} text The text to be normalized. - * @returns {string} The normalized text. - */ - normalize(text) { - text = text.normalize("NFKD"); - return text; - } - } - class StripNormalizer extends Normalizer { - /** - * Strip leading and/or trailing whitespace from the input text. - * @param {string} text The input text. - * @returns {string} The normalized text. - */ - normalize(text) { - if (this.config.strip_left && this.config.strip_right) { - text = text.trim(); - } else { - if (this.config.strip_left) { - text = text.trimStart(); - } - if (this.config.strip_right) { - text = text.trimEnd(); - } - } - return text; - } - } - class StripAccents extends Normalizer { - /** - * Remove all accents from the text. - * @param {string} text The input text. - * @returns {string} The normalized text without accents. - */ - normalize(text) { - text = remove_accents(text); - return text; - } - } - class Lowercase extends Normalizer { - /** - * Lowercases the input string. - * @param {string} text The text to normalize. - * @returns {string} The normalized text. - */ - normalize(text) { - text = text.toLowerCase(); - return text; - } - } - class Prepend extends Normalizer { - /** - * Prepends the input string. - * @param {string} text The text to normalize. - * @returns {string} The normalized text. - */ - normalize(text) { - text = this.config.prepend + text; - return text; - } - } - class NormalizerSequence extends Normalizer { - /** - * Create a new instance of NormalizerSequence. - * @param {Object} config The configuration object. - * @param {Object[]} config.normalizers An array of Normalizer configuration objects. - */ - constructor(config) { - super(config); - this.normalizers = config.normalizers.map((x) => Normalizer.fromConfig(x)); - } - /** - * Apply a sequence of Normalizers to the input text. - * @param {string} text The text to normalize. - * @returns {string} The normalized text. - */ - normalize(text) { - return this.normalizers.reduce((t, normalizer) => { - return normalizer.normalize(t); - }, text); - } - } - class BertNormalizer extends Normalizer { - /** - * Adds whitespace around any CJK (Chinese, Japanese, or Korean) character in the input text. - * - * @param {string} text The input text to tokenize. - * @returns {string} The tokenized text with whitespace added around CJK characters. - */ - _tokenize_chinese_chars(text) { - const output = []; - for (let i = 0; i < text.length; ++i) { - const char = text[i]; - const cp = char.charCodeAt(0); - if (is_chinese_char(cp)) { - output.push(" "); - output.push(char); - output.push(" "); - } else { - output.push(char); - } - } - return output.join(""); - } - /** - * Strips accents from the given text. - * @param {string} text The text to strip accents from. - * @returns {string} The text with accents removed. - */ - stripAccents(text) { - return text.normalize("NFD").replace(new RegExp("\\p{Mn}", "gu"), ""); - } - /** - * Checks whether `char` is a control character. - * @param {string} char The character to check. - * @returns {boolean} Whether `char` is a control character. - * @private - */ - _is_control(char) { - switch (char) { - case " ": - case "\n": - case "\r": - return false; - default: - return new RegExp("^\\p{Cc}|\\p{Cf}|\\p{Co}|\\p{Cs}$", "u").test(char); - } - } - /** - * Performs invalid character removal and whitespace cleanup on text. - * @param {string} text The text to clean. - * @returns {string} The cleaned text. - * @private - */ - _clean_text(text) { - const output = []; - for (const char of text) { - const cp = char.charCodeAt(0); - if (cp === 0 || cp === 65533 || this._is_control(char)) { - continue; - } - if (/^\s$/.test(char)) { - output.push(" "); - } else { - output.push(char); - } - } - return output.join(""); - } - /** - * Normalizes the given text based on the configuration. - * @param {string} text The text to normalize. - * @returns {string} The normalized text. - */ - normalize(text) { - if (this.config.clean_text) { - text = this._clean_text(text); - } - if (this.config.handle_chinese_chars) { - text = this._tokenize_chinese_chars(text); - } - if (this.config.lowercase) { - text = text.toLowerCase(); - if (this.config.strip_accents !== false) { - text = this.stripAccents(text); - } - } else if (this.config.strip_accents) { - text = this.stripAccents(text); - } - return text; - } - } - class PreTokenizer extends _utils_generic_js__WEBPACK_IMPORTED_MODULE_0__.Callable { - /** - * Factory method that returns an instance of a subclass of `PreTokenizer` based on the provided configuration. - * - * @static - * @param {Object} config A configuration object for the pre-tokenizer. - * @returns {PreTokenizer} An instance of a subclass of `PreTokenizer`. - * @throws {Error} If the provided configuration object does not correspond to any known pre-tokenizer. - */ - static fromConfig(config) { - if (config === null) return null; - switch (config.type) { - case "BertPreTokenizer": - return new BertPreTokenizer(config); - case "Sequence": - return new PreTokenizerSequence(config); - case "Whitespace": - return new WhitespacePreTokenizer(config); - case "WhitespaceSplit": - return new WhitespaceSplit(config); - case "Metaspace": - return new MetaspacePreTokenizer(config); - case "ByteLevel": - return new ByteLevelPreTokenizer(config); - case "Split": - return new SplitPreTokenizer(config); - case "Punctuation": - return new PunctuationPreTokenizer(config); - case "Digits": - return new DigitsPreTokenizer(config); - case "Replace": - return new ReplacePreTokenizer(config); - default: - throw new Error(`Unknown PreTokenizer type: ${config.type}`); - } - } - /** - * Method that should be implemented by subclasses to define the specific pre-tokenization logic. - * - * @abstract - * @param {string} text The text to pre-tokenize. - * @param {Object} [options] Additional options for the pre-tokenization logic. - * @returns {string[]} The pre-tokenized text. - * @throws {Error} If the method is not implemented in the subclass. - */ - pre_tokenize_text(text, options) { - throw Error("pre_tokenize_text should be implemented in subclass."); - } - /** - * Tokenizes the given text into pre-tokens. - * @param {string|string[]} text The text or array of texts to pre-tokenize. - * @param {Object} [options] Additional options for the pre-tokenization logic. - * @returns {string[]} An array of pre-tokens. - */ - pre_tokenize(text, options) { - return (Array.isArray(text) ? text.map((x) => this.pre_tokenize_text(x, options)) : this.pre_tokenize_text(text, options)).flat(); - } - /** - * Alias for {@link PreTokenizer#pre_tokenize}. - * @param {string|string[]} text The text or array of texts to pre-tokenize. - * @param {Object} [options] Additional options for the pre-tokenization logic. - * @returns {string[]} An array of pre-tokens. - */ - _call(text, options) { - return this.pre_tokenize(text, options); - } - } - class BertPreTokenizer extends PreTokenizer { - /** - * A PreTokenizer that splits text into wordpieces using a basic tokenization scheme - * similar to that used in the original implementation of BERT. - * - * @param {Object} config The configuration object. - */ - constructor(config) { - super(); - this.pattern = new RegExp(`[^\\s${PUNCTUATION_REGEX}]+|[${PUNCTUATION_REGEX}]`, "gu"); - } - /** - * Tokenizes a single text using the BERT pre-tokenization scheme. - * - * @param {string} text The text to tokenize. - * @param {Object} [options] Additional options for the pre-tokenization logic. - * @returns {string[]} An array of tokens. - */ - pre_tokenize_text(text, options) { - return text.trim().match(this.pattern) || []; - } - } - class ByteLevelPreTokenizer extends PreTokenizer { - /** - * Creates a new instance of the `ByteLevelPreTokenizer` class. - * @param {Object} config The configuration object. - */ - constructor(config) { - super(); - this.config = config; - this.add_prefix_space = this.config.add_prefix_space; - this.trim_offsets = this.config.trim_offsets; - this.use_regex = this.config.use_regex ?? true; - this.pattern = new RegExp("'s|'t|'re|'ve|'m|'ll|'d| ?\\p{L}+| ?\\p{N}+| ?[^\\s\\p{L}\\p{N}]+|\\s+(?!\\S)|\\s+", "gu"); - this.byte_encoder = BYTES_TO_UNICODE; - this.text_encoder = new TextEncoder(); - } - /** - * Tokenizes a single piece of text using byte-level tokenization. - * @param {string} text The text to tokenize. - * @param {Object} [options] Additional options for the pre-tokenization logic. - * @returns {string[]} An array of tokens. - */ - pre_tokenize_text(text, options) { - if (this.add_prefix_space && !text.startsWith(" ")) { - text = " " + text; - } - const tokens = this.use_regex ? text.match(this.pattern) || [] : [text]; - return tokens.map( - (token) => Array.from(this.text_encoder.encode(token), (byte) => this.byte_encoder[byte]).join("") - ); - } - } - class SplitPreTokenizer extends PreTokenizer { - /** - * @param {Object} config The configuration options for the pre-tokenizer. - * @param {Object} config.pattern The pattern used to split the text. Can be a string or a regex object. - * @param {string|undefined} config.pattern.String The string to use for splitting. Only defined if the pattern is a string. - * @param {string|undefined} config.pattern.Regex The regex to use for splitting. Only defined if the pattern is a regex. - * @param {SplitDelimiterBehavior} config.behavior The behavior to use when splitting. - * @param {boolean} config.invert Whether to split (invert=false) or match (invert=true) the pattern. - */ - constructor(config) { - super(); - this.config = config; - this.pattern = createPattern(this.config.pattern, this.config.invert); - } - /** - * Tokenizes text by splitting it using the given pattern. - * @param {string} text The text to tokenize. - * @param {Object} [options] Additional options for the pre-tokenization logic. - * @returns {string[]} An array of tokens. - */ - pre_tokenize_text(text, options) { - if (this.pattern === null) { - return []; - } - if (this.config.invert) { - return text.match(this.pattern) || []; - } else { - return regexSplit(text, this.pattern); - } - } - } - class PunctuationPreTokenizer extends PreTokenizer { - /** - * @param {Object} config The configuration options for the pre-tokenizer. - * @param {SplitDelimiterBehavior} config.behavior The behavior to use when splitting. - */ - constructor(config) { - super(); - this.config = config; - this.pattern = new RegExp(`[^${PUNCTUATION_REGEX}]+|[${PUNCTUATION_REGEX}]+`, "gu"); - } - /** - * Tokenizes text by splitting it using the given pattern. - * @param {string} text The text to tokenize. - * @param {Object} [options] Additional options for the pre-tokenization logic. - * @returns {string[]} An array of tokens. - */ - pre_tokenize_text(text, options) { - return text.match(this.pattern) || []; - } - } - class DigitsPreTokenizer extends PreTokenizer { - /** - * @param {Object} config The configuration options for the pre-tokenizer. - * @param {boolean} config.individual_digits Whether to split on individual digits. - */ - constructor(config) { - super(); - this.config = config; - const digit_pattern = `[^\\d]+|\\d${this.config.individual_digits ? "" : "+"}`; - this.pattern = new RegExp(digit_pattern, "gu"); - } - /** - * Tokenizes text by splitting it using the given pattern. - * @param {string} text The text to tokenize. - * @param {Object} [options] Additional options for the pre-tokenization logic. - * @returns {string[]} An array of tokens. - */ - pre_tokenize_text(text, options) { - return text.match(this.pattern) || []; - } - } - class PostProcessor extends _utils_generic_js__WEBPACK_IMPORTED_MODULE_0__.Callable { - /** - * @param {Object} config The configuration for the post-processor. - */ - constructor(config) { - super(); - this.config = config; - } - /** - * Factory method to create a PostProcessor object from a configuration object. - * - * @param {Object} config Configuration object representing a PostProcessor. - * @returns {PostProcessor} A PostProcessor object created from the given configuration. - * @throws {Error} If an unknown PostProcessor type is encountered. - */ - static fromConfig(config) { - if (config === null) return null; - switch (config.type) { - case "TemplateProcessing": - return new TemplateProcessing(config); - case "ByteLevel": - return new ByteLevelPostProcessor(config); - case "RobertaProcessing": - return new RobertaProcessing(config); - case "BertProcessing": - return new BertProcessing(config); - case "Sequence": - return new PostProcessorSequence(config); - default: - throw new Error(`Unknown PostProcessor type: ${config.type}`); - } - } - /** - * Method to be implemented in subclass to apply post-processing on the given tokens. - * - * @param {Array} tokens The input tokens to be post-processed. - * @param {...*} args Additional arguments required by the post-processing logic. - * @returns {PostProcessedOutput} The post-processed tokens. - * @throws {Error} If the method is not implemented in subclass. - */ - post_process(tokens, ...args) { - throw Error("post_process should be implemented in subclass."); - } - /** - * Alias for {@link PostProcessor#post_process}. - * @param {Array} tokens The text or array of texts to post-process. - * @param {...*} args Additional arguments required by the post-processing logic. - * @returns {PostProcessedOutput} The post-processed tokens. - */ - _call(tokens, ...args) { - return this.post_process(tokens, ...args); - } - } - class BertProcessing extends PostProcessor { - /** - * @param {Object} config The configuration for the post-processor. - * @param {string[]} config.cls The special tokens to add to the beginning of the input. - * @param {string[]} config.sep The special tokens to add to the end of the input. - */ - constructor(config) { - super(config); - this.cls = config.cls[0]; - this.sep = config.sep[0]; - } - /** - * Adds the special tokens to the beginning and end of the input. - * @param {string[]} tokens The input tokens. - * @param {string[]} [tokens_pair=null] An optional second set of input tokens. - * @returns {PostProcessedOutput} The post-processed tokens with the special tokens added to the beginning and end. - */ - post_process(tokens, tokens_pair = null, { - add_special_tokens = true - } = {}) { - if (add_special_tokens) { - tokens = (0, _utils_core_js__WEBPACK_IMPORTED_MODULE_1__.mergeArrays)([this.cls], tokens, [this.sep]); - } - let token_type_ids = new Array(tokens.length).fill(0); - if (tokens_pair !== null) { - const middle = add_special_tokens && this instanceof RobertaProcessing ? [this.sep] : []; - const after = add_special_tokens ? [this.sep] : []; - tokens = (0, _utils_core_js__WEBPACK_IMPORTED_MODULE_1__.mergeArrays)(tokens, middle, tokens_pair, after); - token_type_ids = (0, _utils_core_js__WEBPACK_IMPORTED_MODULE_1__.mergeArrays)(token_type_ids, new Array(tokens_pair.length + middle.length + after.length).fill(1)); - } - return { tokens, token_type_ids }; - } - } - class RobertaProcessing extends BertProcessing { - } - class TemplateProcessing extends PostProcessor { - /** - * Creates a new instance of `TemplateProcessing`. - * @param {Object} config The configuration options for the post processor. - * @param {Array} config.single The template for a single sequence of tokens. - * @param {Array} config.pair The template for a pair of sequences of tokens. - */ - constructor(config) { - super(config); - this.single = config.single; - this.pair = config.pair; - } - /** - * Replaces special tokens in the template with actual tokens. - * @param {string[]} tokens The list of tokens for the first sequence. - * @param {string[]} [tokens_pair=null] The list of tokens for the second sequence (optional). - * @returns {PostProcessedOutput} An object containing the list of tokens with the special tokens replaced with actual tokens. - */ - post_process(tokens, tokens_pair = null, { - add_special_tokens = true - } = {}) { - const type = tokens_pair === null ? this.single : this.pair; - let processedTokens = []; - let types = []; - for (const item of type) { - if ("SpecialToken" in item) { - if (add_special_tokens) { - processedTokens.push(item.SpecialToken.id); - types.push(item.SpecialToken.type_id); - } - } else if ("Sequence" in item) { - if (item.Sequence.id === "A") { - processedTokens = (0, _utils_core_js__WEBPACK_IMPORTED_MODULE_1__.mergeArrays)(processedTokens, tokens); - types = (0, _utils_core_js__WEBPACK_IMPORTED_MODULE_1__.mergeArrays)(types, new Array(tokens.length).fill(item.Sequence.type_id)); - } else if (item.Sequence.id === "B") { - processedTokens = (0, _utils_core_js__WEBPACK_IMPORTED_MODULE_1__.mergeArrays)(processedTokens, tokens_pair); - types = (0, _utils_core_js__WEBPACK_IMPORTED_MODULE_1__.mergeArrays)(types, new Array(tokens_pair.length).fill(item.Sequence.type_id)); - } - } - } - return { tokens: processedTokens, token_type_ids: types }; - } - } - class ByteLevelPostProcessor extends PostProcessor { - /** - * Post process the given tokens. - * @param {string[]} tokens The list of tokens for the first sequence. - * @param {string[]} [tokens_pair=null] The list of tokens for the second sequence (optional). - * @returns {PostProcessedOutput} An object containing the post-processed tokens. - */ - post_process(tokens, tokens_pair = null) { - if (tokens_pair) { - tokens = (0, _utils_core_js__WEBPACK_IMPORTED_MODULE_1__.mergeArrays)(tokens, tokens_pair); - } - return { tokens }; - } - } - class PostProcessorSequence extends PostProcessor { - /** - * Creates a new instance of PostProcessorSequence. - * @param {Object} config The configuration object. - * @param {Object[]} config.processors The list of post-processors to apply. - */ - constructor(config) { - super(config); - this.processors = config.processors.map((x) => PostProcessor.fromConfig(x)); - } - /** - * Post process the given tokens. - * @param {string[]} tokens The list of tokens for the first sequence. - * @param {string[]} [tokens_pair=null] The list of tokens for the second sequence (optional). - * @returns {PostProcessedOutput} An object containing the post-processed tokens. - */ - post_process(tokens, tokens_pair = null, options = {}) { - let token_type_ids; - for (const processor of this.processors) { - if (processor instanceof ByteLevelPostProcessor) { - const output = processor.post_process(tokens); - tokens = output.tokens; - if (tokens_pair) { - const pair_output = processor.post_process(tokens_pair); - tokens_pair = pair_output.tokens; - } - } else { - const output = processor.post_process(tokens, tokens_pair, options); - tokens = output.tokens; - token_type_ids = output.token_type_ids; - } - } - return { tokens, token_type_ids }; - } - } - class Decoder extends _utils_generic_js__WEBPACK_IMPORTED_MODULE_0__.Callable { - /** - * Creates an instance of `Decoder`. - * - * @param {Object} config The configuration object. - */ - constructor(config) { - super(); - this.config = config; - this.added_tokens = []; - this.end_of_word_suffix = null; - this.trim_offsets = config.trim_offsets; - } - /** - * Creates a decoder instance based on the provided configuration. - * - * @param {Object} config The configuration object. - * @returns {Decoder} A decoder instance. - * @throws {Error} If an unknown decoder type is provided. - */ - static fromConfig(config) { - if (config === null) return null; - switch (config.type) { - case "WordPiece": - return new WordPieceDecoder(config); - case "Metaspace": - return new MetaspaceDecoder(config); - case "ByteLevel": - return new ByteLevelDecoder(config); - case "Replace": - return new ReplaceDecoder(config); - case "ByteFallback": - return new ByteFallback(config); - case "Fuse": - return new FuseDecoder(config); - case "Strip": - return new StripDecoder(config); - case "Sequence": - return new DecoderSequence(config); - case "CTC": - return new CTCDecoder(config); - case "BPEDecoder": - return new BPEDecoder(config); - default: - throw new Error(`Unknown Decoder type: ${config.type}`); - } - } - /** - * Calls the `decode` method. - * - * @param {string[]} tokens The list of tokens. - * @returns {string} The decoded string. - */ - _call(tokens) { - return this.decode(tokens); - } - /** - * Decodes a list of tokens. - * @param {string[]} tokens The list of tokens. - * @returns {string} The decoded string. - */ - decode(tokens) { - return this.decode_chain(tokens).join(""); - } - /** - * Apply the decoder to a list of tokens. - * - * @param {string[]} tokens The list of tokens. - * @returns {string[]} The decoded list of tokens. - * @throws {Error} If the `decode_chain` method is not implemented in the subclass. - */ - decode_chain(tokens) { - throw Error("`decode_chain` should be implemented in subclass."); - } - } - class ReplaceDecoder extends Decoder { - /** @type {Decoder['decode_chain']} */ - decode_chain(tokens) { - const pattern = createPattern(this.config.pattern); - return pattern === null ? tokens : tokens.map((token) => token.replaceAll(pattern, this.config.content)); - } - } - class ByteFallback extends Decoder { - constructor(config) { - super(config); - this.text_decoder = new TextDecoder(); - } - /** @type {Decoder['decode_chain']} */ - decode_chain(tokens) { - const new_tokens = []; - let previous_byte_tokens = []; - for (const token of tokens) { - let bytes = null; - if (token.length === 6 && token.startsWith("<0x") && token.endsWith(">")) { - const byte = parseInt(token.slice(3, 5), 16); - if (!isNaN(byte)) { - bytes = byte; - } - } - if (bytes !== null) { - previous_byte_tokens.push(bytes); - } else { - if (previous_byte_tokens.length > 0) { - const string = this.text_decoder.decode(Uint8Array.from(previous_byte_tokens)); - new_tokens.push(string); - previous_byte_tokens = []; - } - new_tokens.push(token); - } - } - if (previous_byte_tokens.length > 0) { - const string = this.text_decoder.decode(Uint8Array.from(previous_byte_tokens)); - new_tokens.push(string); - previous_byte_tokens = []; - } - return new_tokens; - } - } - class FuseDecoder extends Decoder { - /** @type {Decoder['decode_chain']} */ - decode_chain(tokens) { - return [tokens.join("")]; - } - } - class StripDecoder extends Decoder { - constructor(config) { - super(config); - this.content = this.config.content; - this.start = this.config.start; - this.stop = this.config.stop; - } - /** @type {Decoder['decode_chain']} */ - decode_chain(tokens) { - return tokens.map((token) => { - let start_cut = 0; - for (let i = 0; i < this.start; ++i) { - if (token[i] === this.content) { - start_cut = i + 1; - continue; - } else { - break; - } - } - let stop_cut = token.length; - for (let i = 0; i < this.stop; ++i) { - const index = token.length - i - 1; - if (token[index] === this.content) { - stop_cut = index; - continue; - } else { - break; - } - } - return token.slice(start_cut, stop_cut); - }); - } - } - class WordPieceDecoder extends Decoder { - /** - * Creates a new instance of WordPieceDecoder. - * @param {Object} config The configuration object. - * @param {string} config.prefix The prefix used for WordPiece encoding. - * @param {boolean} config.cleanup Whether to cleanup the decoded string. - */ - constructor(config) { - super(config); - this.cleanup = config.cleanup; - } - /** @type {Decoder['decode_chain']} */ - decode_chain(tokens) { - return tokens.map((token, i) => { - if (i !== 0) { - if (token.startsWith(this.config.prefix)) { - token = token.replace(this.config.prefix, ""); - } else { - token = " " + token; - } - } - if (this.cleanup) { - token = clean_up_tokenization(token); - } - return token; - }); - } - } - class ByteLevelDecoder extends Decoder { - /** - * Create a `ByteLevelDecoder` object. - * @param {Object} config Configuration object. - */ - constructor(config) { - super(config); - this.byte_decoder = UNICODE_TO_BYTES; - this.text_decoder = new TextDecoder("utf-8", { - fatal: false, - ignoreBOM: true - }); - this.end_of_word_suffix = null; - } - /** - * Convert an array of tokens to string by decoding each byte. - * @param {string[]} tokens Array of tokens to be decoded. - * @returns {string} The decoded string. - */ - convert_tokens_to_string(tokens) { - const text = tokens.join(""); - const byteArray = new Uint8Array([...text].map((c) => this.byte_decoder[c])); - const decoded_text = this.text_decoder.decode(byteArray); - return decoded_text; - } - /** @type {Decoder['decode_chain']} */ - decode_chain(tokens) { - const sub_texts = []; - let current_sub_text = []; - for (const token of tokens) { - if (this.added_tokens.find((x) => x.content === token) !== void 0) { - if (current_sub_text.length > 0) { - sub_texts.push(this.convert_tokens_to_string(current_sub_text)); - current_sub_text = []; - } - sub_texts.push(token); - } else { - current_sub_text.push(token); - } - } - if (current_sub_text.length > 0) { - sub_texts.push(this.convert_tokens_to_string(current_sub_text)); - } - return sub_texts; - } - } - class CTCDecoder extends Decoder { - constructor(config) { - super(config); - this.pad_token = this.config.pad_token; - this.word_delimiter_token = this.config.word_delimiter_token; - this.cleanup = this.config.cleanup; - } - /** - * Converts a connectionist-temporal-classification (CTC) output tokens into a single string. - * @param {string[]} tokens Array of tokens to be decoded. - * @returns {string} The decoded string. - */ - convert_tokens_to_string(tokens) { - if (tokens.length === 0) return ""; - const grouped_tokens = [tokens[0]]; - for (let i = 1; i < tokens.length; ++i) { - if (tokens[i] !== grouped_tokens.at(-1)) { - grouped_tokens.push(tokens[i]); - } - } - const filtered_tokens = grouped_tokens.filter((token) => token !== this.pad_token); - let text = filtered_tokens.join(""); - if (this.cleanup) { - text = clean_up_tokenization(text).replaceAll(this.word_delimiter_token, " ").trim(); - } - return text; - } - /** @type {Decoder['decode_chain']} */ - decode_chain(tokens) { - return [this.convert_tokens_to_string(tokens)]; - } - } - class DecoderSequence extends Decoder { - /** - * Creates a new instance of DecoderSequence. - * @param {Object} config The configuration object. - * @param {Object[]} config.decoders The list of decoders to apply. - */ - constructor(config) { - super(config); - this.decoders = config.decoders.map((x) => Decoder.fromConfig(x)); - } - /** @type {Decoder['decode_chain']} */ - decode_chain(tokens) { - return this.decoders.reduce((toks, decoder) => { - return decoder.decode_chain(toks); - }, tokens); - } - } - class BPEDecoder extends Decoder { - constructor(config) { - super(config); - this.suffix = this.config.suffix; - } - /** @type {Decoder['decode_chain']} */ - decode_chain(tokens) { - return tokens.map((token, i) => { - return token.replaceAll(this.suffix, i === tokens.length - 1 ? "" : " "); - }); - } - } - class VitsDecoder extends Decoder { - /** @type {Decoder['decode_chain']} */ - decode_chain(tokens) { - let decoded = ""; - for (let i = 1; i < tokens.length; i += 2) { - decoded += tokens[i]; - } - return [decoded]; - } - } - class MetaspacePreTokenizer extends PreTokenizer { - /** - * @param {Object} config The configuration object for the MetaspacePreTokenizer. - * @param {boolean} config.add_prefix_space Whether to add a prefix space to the first token. - * @param {string} config.replacement The character to replace spaces with. - * @param {string} [config.str_rep=config.replacement] An optional string representation of the replacement character. - * @param {'first'|'never'|'always'} [config.prepend_scheme='always'] The metaspace prepending scheme. - */ - constructor(config) { - super(); - this.addPrefixSpace = config.add_prefix_space; - this.replacement = config.replacement; - this.strRep = config.str_rep || this.replacement; - this.prepend_scheme = config.prepend_scheme ?? "always"; - } - /** - * This method takes a string, replaces spaces with the replacement character, - * adds a prefix space if requested, and returns a new list of tokens. - * @param {string} text The text to pre-tokenize. - * @param {Object} [options] The options for the pre-tokenization. - * @param {number} [options.section_index] The index of the section to pre-tokenize. - * @returns {string[]} A new list of pre-tokenized tokens. - */ - pre_tokenize_text(text, { - section_index = void 0 - } = {}) { - let normalized = text.replaceAll(" ", this.strRep); - if ( - // We add a prefix space if: - // (1) The addPrefixSpace option is enabled and the normalized - // token does not already start with the replacement character. - this.addPrefixSpace && !normalized.startsWith(this.replacement) && (this.prepend_scheme === "always" || this.prepend_scheme === "first" && section_index === 0) - ) { - normalized = this.strRep + normalized; - } - return [normalized]; - } - } - class MetaspaceDecoder extends Decoder { - /** - * Constructs a new MetaspaceDecoder object. - * @param {Object} config The configuration object for the MetaspaceDecoder. - * @param {boolean} config.add_prefix_space Whether to add a prefix space to the decoded string. - * @param {string} config.replacement The string to replace spaces with. - */ - constructor(config) { - super(config); - this.addPrefixSpace = config.add_prefix_space; - this.replacement = config.replacement; - } - /** @type {Decoder['decode_chain']} */ - decode_chain(tokens) { - const result = []; - for (let i = 0; i < tokens.length; ++i) { - let normalized = tokens[i].replaceAll(this.replacement, " "); - if (this.addPrefixSpace && i == 0 && normalized.startsWith(" ")) { - normalized = normalized.substring(1); - } - result.push(normalized); - } - return result; - } - } - class Precompiled extends Normalizer { - /** - * Create a new instance of Precompiled normalizer. - * @param {Object} config The configuration object. - * @param {any} config.precompiled_charsmap Precompiled chars mapping. - */ - constructor(config) { - super(config); - this.charsmap = config.precompiled_charsmap; - } - /** - * Normalizes the given text by applying the precompiled charsmap. - * @param {string} text The text to normalize. - * @returns {string} The normalized text. - */ - normalize(text) { - text = text.replace(/[\u0001-\u0008\u000B\u000E-\u001F\u007F\u008F\u009F]/gm, ""); - text = text.replace(/[\u0009\u000A\u000C\u000D\u00A0\u1680\u2000-\u200F\u2028\u2029\u202F\u205F\u2581\u3000\uFEFF\uFFFD]/gm, " "); - if (text.includes("~")) { - const parts = text.split("~"); - text = parts.map((part) => part.normalize("NFKC")).join("~"); - } else { - text = text.normalize("NFKC"); - } - return text; - } - } - class PreTokenizerSequence extends PreTokenizer { - /** - * Creates an instance of PreTokenizerSequence. - * @param {Object} config The configuration object for the pre-tokenizer sequence. - * @param {Object[]} config.pretokenizers An array of pre-tokenizer configurations. - */ - constructor(config) { - super(); - this.tokenizers = config.pretokenizers.map((x) => PreTokenizer.fromConfig(x)); - } - /** - * Applies each pre-tokenizer in the sequence to the input text in turn. - * @param {string} text The text to pre-tokenize. - * @param {Object} [options] Additional options for the pre-tokenization logic. - * @returns {string[]} The pre-tokenized text. - */ - pre_tokenize_text(text, options) { - return this.tokenizers.reduce((preTokenizedText, tokenizer) => { - return tokenizer.pre_tokenize(preTokenizedText, options); - }, [text]); - } - } - class WhitespacePreTokenizer extends PreTokenizer { - /** - * Creates an instance of WhitespacePreTokenizer. - * @param {Object} config The configuration object for the pre-tokenizer. - */ - constructor(config) { - super(); - } - /** - * Pre-tokenizes the input text by splitting it on word boundaries. - * @param {string} text The text to be pre-tokenized. - * @param {Object} [options] Additional options for the pre-tokenization logic. - * @returns {string[]} An array of tokens produced by splitting the input text on whitespace. - */ - pre_tokenize_text(text, options) { - return text.match(/\w+|[^\w\s]+/g) || []; - } - } - class WhitespaceSplit extends PreTokenizer { - /** - * Creates an instance of WhitespaceSplit. - * @param {Object} config The configuration object for the pre-tokenizer. - */ - constructor(config) { - super(); - } - /** - * Pre-tokenizes the input text by splitting it on whitespace characters. - * @param {string} text The text to be pre-tokenized. - * @param {Object} [options] Additional options for the pre-tokenization logic. - * @returns {string[]} An array of tokens produced by splitting the input text on whitespace. - */ - pre_tokenize_text(text, options) { - return whitespace_split(text); - } - } - class ReplacePreTokenizer extends PreTokenizer { - /** - * @param {Object} config The configuration options for the pre-tokenizer. - * @param {Object} config.pattern The pattern used to split the text. Can be a string or a regex object. - * @param {string} config.content What to replace the pattern with. - */ - constructor(config) { - super(); - this.config = config; - this.pattern = createPattern(this.config.pattern); - this.content = this.config.content; - } - /** - * Pre-tokenizes the input text by replacing certain characters. - * @param {string} text The text to be pre-tokenized. - * @param {Object} [options] Additional options for the pre-tokenization logic. - * @returns {string[]} An array of tokens produced by replacing certain characters. - */ - pre_tokenize_text(text, options) { - if (this.pattern === null) { - return [text]; - } - return [text.replaceAll(this.pattern, this.config.content)]; - } - } - const SPECIAL_TOKEN_ATTRIBUTES = [ - "bos_token", - "eos_token", - "unk_token", - "sep_token", - "pad_token", - "cls_token", - "mask_token" - // additional_special_tokens (TODO) - ]; - function padHelper(item, length, value_fn, side) { - for (const key of Object.keys(item)) { - const diff = length - item[key].length; - const value = value_fn(key); - const padData = new Array(diff).fill(value); - item[key] = side === "right" ? (0, _utils_core_js__WEBPACK_IMPORTED_MODULE_1__.mergeArrays)(item[key], padData) : (0, _utils_core_js__WEBPACK_IMPORTED_MODULE_1__.mergeArrays)(padData, item[key]); - } - } - function truncateHelper(item, length) { - for (const key of Object.keys(item)) { - item[key].length = length; - } - } - class PreTrainedTokenizer extends _utils_generic_js__WEBPACK_IMPORTED_MODULE_0__.Callable { - /** - * Create a new PreTrainedTokenizer instance. - * @param {Object} tokenizerJSON The JSON of the tokenizer. - * @param {Object} tokenizerConfig The config of the tokenizer. - */ - constructor(tokenizerJSON, tokenizerConfig) { - super(); - __publicField(this, "return_token_type_ids", false); - __publicField(this, "padding_side", "right"); - this._tokenizer_config = tokenizerConfig; - this.normalizer = Normalizer.fromConfig(tokenizerJSON.normalizer); - this.pre_tokenizer = PreTokenizer.fromConfig(tokenizerJSON.pre_tokenizer); - this.model = TokenizerModel.fromConfig(tokenizerJSON.model, tokenizerConfig); - this.post_processor = PostProcessor.fromConfig(tokenizerJSON.post_processor); - this.decoder = Decoder.fromConfig(tokenizerJSON.decoder); - this.special_tokens = []; - this.all_special_ids = []; - this.added_tokens = []; - for (const addedToken of tokenizerJSON.added_tokens) { - const token = new AddedToken(addedToken); - this.added_tokens.push(token); - this.model.tokens_to_ids.set(token.content, token.id); - this.model.vocab[token.id] = token.content; - if (token.special) { - this.special_tokens.push(token.content); - this.all_special_ids.push(token.id); - } - } - this.additional_special_tokens = tokenizerConfig.additional_special_tokens ?? []; - this.special_tokens.push(...this.additional_special_tokens); - this.special_tokens = [...new Set(this.special_tokens)]; - if (this.decoder) { - this.decoder.added_tokens = this.added_tokens; - this.decoder.end_of_word_suffix = this.model.end_of_word_suffix; - } - this.added_tokens_regex = this.added_tokens.length > 0 ? new RegExp( - this.added_tokens.slice().sort((a, b) => b.content.length - a.content.length).map((x) => `${x.lstrip ? "\\s*" : ""}(${(0, _utils_core_js__WEBPACK_IMPORTED_MODULE_1__.escapeRegExp)(x.content)})${x.rstrip ? "\\s*" : ""}`).join("|") - ) : null; - this.mask_token = this.getToken("mask_token"); - this.mask_token_id = this.model.tokens_to_ids.get(this.mask_token); - this.pad_token = this.getToken("pad_token", "eos_token"); - this.pad_token_id = this.model.tokens_to_ids.get(this.pad_token); - this.sep_token = this.getToken("sep_token"); - this.sep_token_id = this.model.tokens_to_ids.get(this.sep_token); - this.unk_token = this.getToken("unk_token"); - this.unk_token_id = this.model.tokens_to_ids.get(this.unk_token); - this.model_max_length = tokenizerConfig.model_max_length; - this.remove_space = tokenizerConfig.remove_space; - this.clean_up_tokenization_spaces = tokenizerConfig.clean_up_tokenization_spaces ?? true; - this.do_lowercase_and_remove_accent = tokenizerConfig.do_lowercase_and_remove_accent ?? false; - if (tokenizerConfig.padding_side) { - this.padding_side = tokenizerConfig.padding_side; - } - this.legacy = false; - this.chat_template = tokenizerConfig.chat_template ?? null; - if (Array.isArray(this.chat_template)) { - const chat_template = /* @__PURE__ */ Object.create(null); - for (const { name, template } of this.chat_template) { - if (typeof name !== "string" || typeof template !== "string") { - throw new Error('Chat template must be a list of objects with "name" and "template" properties'); - } - chat_template[name] = template; - } - this.chat_template = chat_template; - } - this._compiled_template_cache = /* @__PURE__ */ new Map(); - } - /** - * Returns the value of the first matching key in the tokenizer config object. - * @param {...string} keys One or more keys to search for in the tokenizer config object. - * @returns {string|null} The value associated with the first matching key, or null if no match is found. - * @throws {Error} If an object is found for a matching key and its __type property is not "AddedToken". - * @private - */ - getToken(...keys) { - for (const key of keys) { - const item = this._tokenizer_config[key]; - if (!item) continue; - if (typeof item === "object") { - if (item.__type === "AddedToken") { - return item.content; - } else { - throw Error(`Unknown token: ${item}`); - } - } else { - return item; - } - } - return null; - } - /** - * Loads a pre-trained tokenizer from the given `pretrained_model_name_or_path`. - * - * @param {string} pretrained_model_name_or_path The path to the pre-trained tokenizer. - * @param {PretrainedTokenizerOptions} options Additional options for loading the tokenizer. - * - * @throws {Error} Throws an error if the tokenizer.json or tokenizer_config.json files are not found in the `pretrained_model_name_or_path`. - * @returns {Promise} A new instance of the `PreTrainedTokenizer` class. - */ - static async from_pretrained(pretrained_model_name_or_path, { - progress_callback = null, - config = null, - cache_dir = null, - local_files_only = false, - revision = "main", - legacy = null - } = {}) { - const info = await loadTokenizer(pretrained_model_name_or_path, { - progress_callback, - config, - cache_dir, - local_files_only, - revision, - legacy - }); - return new this(...info); - } - /** - * @typedef {number[]|number[][]|Tensor} BatchEncodingItem - * - * @typedef {Object} BatchEncoding Holds the output of the tokenizer's call function. - * @property {BatchEncodingItem} input_ids List of token ids to be fed to a model. - * @property {BatchEncodingItem} attention_mask List of indices specifying which tokens should be attended to by the model. - * @property {BatchEncodingItem} [token_type_ids] List of token type ids to be fed to a model. - */ - /** - * Encode/tokenize the given text(s). - * @param {string|string[]} text The text to tokenize. - * @param {Object} options An optional object containing the following properties: - * @param {string|string[]} [options.text_pair=null] Optional second sequence to be encoded. If set, must be the same type as text. - * @param {boolean|'max_length'} [options.padding=false] Whether to pad the input sequences. - * @param {boolean} [options.add_special_tokens=true] Whether or not to add the special tokens associated with the corresponding model. - * @param {boolean} [options.truncation=null] Whether to truncate the input sequences. - * @param {number} [options.max_length=null] Maximum length of the returned list and optionally padding length. - * @param {boolean} [options.return_tensor=true] Whether to return the results as Tensors or arrays. - * @param {boolean} [options.return_token_type_ids=null] Whether to return the token type ids. - * @returns {BatchEncoding} Object to be passed to the model. - */ - _call(text, { - text_pair = null, - add_special_tokens = true, - padding = false, - truncation = null, - max_length = null, - return_tensor = true, - // Different to HF - return_token_type_ids = null - } = {}) { - const isBatched = Array.isArray(text); - let encodedTokens; - if (isBatched) { - if (text.length === 0) { - throw Error("text array must be non-empty"); - } - if (text_pair !== null) { - if (!Array.isArray(text_pair)) { - throw Error("text_pair must also be an array"); - } else if (text.length !== text_pair.length) { - throw Error("text and text_pair must have the same length"); - } - encodedTokens = text.map( - (t, i) => this._encode_plus(t, { text_pair: text_pair[i], add_special_tokens, return_token_type_ids }) - ); - } else { - encodedTokens = text.map((x) => this._encode_plus(x, { add_special_tokens, return_token_type_ids })); - } - } else { - if (text === null || text === void 0) { - throw Error("text may not be null or undefined"); - } - if (Array.isArray(text_pair)) { - throw Error("When specifying `text_pair`, since `text` is a string, `text_pair` must also be a string (i.e., not an array)."); - } - encodedTokens = [this._encode_plus(text, { text_pair, add_special_tokens, return_token_type_ids })]; - } - if (max_length === null) { - if (padding === "max_length") { - max_length = this.model_max_length; - } else { - max_length = (0, _utils_maths_js__WEBPACK_IMPORTED_MODULE_3__.max)(encodedTokens.map((x) => x.input_ids.length))[0]; - } - } else { - if (!truncation) { - console.warn(`Truncation was not explicitly activated but \`max_length\` is provided a specific value, please use \`truncation=true\` to explicitly truncate examples to max length.`); - } - } - max_length = Math.min(max_length, this.model_max_length ?? Infinity); - if (padding || truncation) { - for (let i = 0; i < encodedTokens.length; ++i) { - if (encodedTokens[i].input_ids.length === max_length) { - continue; - } else if (encodedTokens[i].input_ids.length > max_length) { - if (truncation) { - truncateHelper(encodedTokens[i], max_length); - } - } else { - if (padding) { - padHelper( - encodedTokens[i], - max_length, - (key) => key === "input_ids" ? this.pad_token_id : 0, - this.padding_side - ); - } - } - } - } - const result = {}; - if (return_tensor) { - if (!(padding && truncation)) { - if (encodedTokens.some((x) => { - var _a; - for (const key of Object.keys(x)) { - if (x[key].length !== ((_a = encodedTokens[0][key]) == null ? void 0 : _a.length)) { - return true; - } - } - return false; - })) { - throw Error( - "Unable to create tensor, you should probably activate truncation and/or padding with 'padding=true' and 'truncation=true' to have batched tensors with the same length." - ); - } - } - const dims = [encodedTokens.length, encodedTokens[0].input_ids.length]; - for (const key of Object.keys(encodedTokens[0])) { - result[key] = new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_4__.Tensor( - "int64", - BigInt64Array.from(encodedTokens.flatMap((x) => x[key]).map(BigInt)), - dims - ); - } - } else { - for (const key of Object.keys(encodedTokens[0])) { - result[key] = encodedTokens.map((x) => x[key]); - } - if (!isBatched) { - for (const key of Object.keys(result)) { - result[key] = result[key][0]; - } - } - } - return ( - /** @type {BatchEncoding} */ - result - ); - } - /** - * Encodes a single text using the preprocessor pipeline of the tokenizer. - * - * @param {string|null} text The text to encode. - * @returns {string[]|null} The encoded tokens. - */ - _encode_text(text) { - if (text === null) return null; - const sections = this.added_tokens_regex ? text.split(this.added_tokens_regex).filter((x) => x) : [text]; - const tokens = sections.map((x, section_index) => { - const addedToken = this.added_tokens.find((t) => t.content === x); - if (addedToken !== void 0) { - return x; - } else { - if (this.remove_space === true) { - x = x.trim().split(/\s+/).join(" "); - } - if (this.do_lowercase_and_remove_accent) { - x = lowercase_and_remove_accent(x); - } - if (this.normalizer !== null) { - x = this.normalizer(x); - } - if (x.length === 0) { - return []; - } - const sectionTokens = this.pre_tokenizer !== null ? this.pre_tokenizer(x, { - section_index - }) : [x]; - const tokens2 = this.model(sectionTokens); - return tokens2; - } - }).flat(); - return tokens; - } - /** - * Encodes a single text or a pair of texts using the model's tokenizer. - * - * @param {string} text The text to encode. - * @param {Object} options An optional object containing the following properties: - * @param {string} [options.text_pair=null] The optional second text to encode. - * @param {boolean} [options.add_special_tokens=true] Whether or not to add the special tokens associated with the corresponding model. - * @param {boolean} [options.return_token_type_ids=null] Whether to return token_type_ids. - * @returns {EncodingSingle} An object containing the encoded text. - * @private - */ - _encode_plus(text, { - text_pair = null, - add_special_tokens = true, - return_token_type_ids = null - } = {}) { - const { tokens, token_type_ids } = this._tokenize_helper(text, { pair: text_pair, add_special_tokens }); - const input_ids = this.model.convert_tokens_to_ids(tokens); - const result = { - input_ids, - attention_mask: new Array(input_ids.length).fill(1) - }; - if ((return_token_type_ids ?? this.return_token_type_ids) && token_type_ids) { - result.token_type_ids = token_type_ids; - } - return result; - } - /** - * Internal helper function to tokenize a text, and optionally a pair of texts. - * @param {string} text The text to tokenize. - * @param {Object} options An optional object containing the following properties: - * @param {string} [options.pair=null] The optional second text to tokenize. - * @param {boolean} [options.add_special_tokens=false] Whether or not to add the special tokens associated with the corresponding model. - * @returns {{tokens: string[], token_type_ids?: number[]}} An object containing the tokens and optionally the token type IDs. - */ - _tokenize_helper(text, { - pair = null, - add_special_tokens = false - } = {}) { - const tokens = this._encode_text(text); - const tokens2 = this._encode_text(pair); - return this.post_processor ? this.post_processor(tokens, tokens2, { add_special_tokens }) : { tokens: (0, _utils_core_js__WEBPACK_IMPORTED_MODULE_1__.mergeArrays)(tokens ?? [], tokens2 ?? []) }; - } - /** - * Converts a string into a sequence of tokens. - * @param {string} text The sequence to be encoded. - * @param {Object} options An optional object containing the following properties: - * @param {string} [options.pair] A second sequence to be encoded with the first. - * @param {boolean} [options.add_special_tokens=false] Whether or not to add the special tokens associated with the corresponding model. - * @returns {string[]} The list of tokens. - */ - tokenize(text, { - pair = null, - add_special_tokens = false - } = {}) { - return this._tokenize_helper(text, { pair, add_special_tokens }).tokens; - } - /** - * Encodes a single text or a pair of texts using the model's tokenizer. - * - * @param {string} text The text to encode. - * @param {Object} options An optional object containing the following properties: - * @param {string} [options.text_pair=null] The optional second text to encode. - * @param {boolean} [options.add_special_tokens=true] Whether or not to add the special tokens associated with the corresponding model. - * @param {boolean} [options.return_token_type_ids=null] Whether to return token_type_ids. - * @returns {number[]} An array of token IDs representing the encoded text(s). - */ - encode(text, { - text_pair = null, - add_special_tokens = true, - return_token_type_ids = null - } = {}) { - return this._encode_plus(text, { - text_pair, - add_special_tokens, - return_token_type_ids - }).input_ids; - } - /** - * Decode a batch of tokenized sequences. - * @param {number[][]|Tensor} batch List/Tensor of tokenized input sequences. - * @param {Object} decode_args (Optional) Object with decoding arguments. - * @returns {string[]} List of decoded sequences. - */ - batch_decode(batch, decode_args = {}) { - if (batch instanceof _utils_tensor_js__WEBPACK_IMPORTED_MODULE_4__.Tensor) { - batch = batch.tolist(); - } - return batch.map((x) => this.decode(x, decode_args)); - } - /** - * Decodes a sequence of token IDs back to a string. - * - * @param {number[]|bigint[]|Tensor} token_ids List/Tensor of token IDs to decode. - * @param {Object} [decode_args={}] - * @param {boolean} [decode_args.skip_special_tokens=false] If true, special tokens are removed from the output string. - * @param {boolean} [decode_args.clean_up_tokenization_spaces=true] If true, spaces before punctuations and abbreviated forms are removed. - * - * @returns {string} The decoded string. - * @throws {Error} If `token_ids` is not a non-empty array of integers. - */ - decode(token_ids, decode_args = {}) { - if (token_ids instanceof _utils_tensor_js__WEBPACK_IMPORTED_MODULE_4__.Tensor) { - token_ids = prepareTensorForDecode(token_ids); - } - if (!Array.isArray(token_ids) || token_ids.length === 0 || !(0, _utils_core_js__WEBPACK_IMPORTED_MODULE_1__.isIntegralNumber)(token_ids[0])) { - throw Error("token_ids must be a non-empty array of integers."); - } - return this.decode_single(token_ids, decode_args); - } - /** - * Decode a single list of token ids to a string. - * @param {number[]|bigint[]} token_ids List of token ids to decode - * @param {Object} decode_args Optional arguments for decoding - * @param {boolean} [decode_args.skip_special_tokens=false] Whether to skip special tokens during decoding - * @param {boolean} [decode_args.clean_up_tokenization_spaces=null] Whether to clean up tokenization spaces during decoding. - * If null, the value is set to `this.decoder.cleanup` if it exists, falling back to `this.clean_up_tokenization_spaces` if it exists, falling back to `true`. - * @returns {string} The decoded string - */ - decode_single(token_ids, { - skip_special_tokens = false, - clean_up_tokenization_spaces = null - }) { - let tokens = this.model.convert_ids_to_tokens(token_ids); - if (skip_special_tokens) { - tokens = tokens.filter((x) => !this.special_tokens.includes(x)); - } - let decoded = this.decoder ? this.decoder(tokens) : tokens.join(" "); - if (this.decoder && this.decoder.end_of_word_suffix) { - decoded = decoded.replaceAll(this.decoder.end_of_word_suffix, " "); - if (skip_special_tokens) { - decoded = decoded.trim(); - } - } - if (clean_up_tokenization_spaces ?? this.clean_up_tokenization_spaces) { - decoded = clean_up_tokenization(decoded); - } - return decoded; - } - /** - * Retrieve the chat template string used for tokenizing chat messages. This template is used - * internally by the `apply_chat_template` method and can also be used externally to retrieve the model's chat - * template for better generation tracking. - * - * @param {Object} options An optional object containing the following properties: - * @param {string} [options.chat_template=null] - * A Jinja template or the name of a template to use for this conversion. - * It is usually not necessary to pass anything to this argument, - * as the model's template will be used by default. - * @param {Object[]} [options.tools=null] - * A list of tools (callable functions) that will be accessible to the model. If the template does not - * support function calling, this argument will have no effect. Each tool should be passed as a JSON Schema, - * giving the name, description and argument types for the tool. See our - * [chat templating guide](https://huggingface.co/docs/transformers/main/en/chat_templating#automated-function-conversion-for-tool-use) - * for more information. - * @returns {string} The chat template string. - */ - get_chat_template({ - chat_template = null, - tools = null - } = {}) { - if (this.chat_template && typeof this.chat_template === "object") { - const template_dict = this.chat_template; - if (chat_template !== null && Object.hasOwn(template_dict, chat_template)) { - chat_template = template_dict[chat_template]; - } else if (chat_template === null) { - if (tools !== null && "tool_use" in template_dict) { - chat_template = template_dict["tool_use"]; - } else if ("default" in template_dict) { - chat_template = template_dict["default"]; - } else { - throw Error( - `This model has multiple chat templates with no default specified! Please either pass a chat template or the name of the template you wish to use to the 'chat_template' argument. Available template names are ${Object.keys(template_dict).sort()}.` - ); - } - } - } else if (chat_template === null) { - if (this.chat_template) { - chat_template = this.chat_template; - } else { - throw Error( - "Cannot use apply_chat_template() because tokenizer.chat_template is not set and no template argument was passed! For information about writing templates and setting the tokenizer.chat_template attribute, please see the documentation at https://huggingface.co/docs/transformers/main/en/chat_templating" - ); - } - } - return chat_template; - } - /** - * Converts a list of message objects with `"role"` and `"content"` keys to a list of token - * ids. This method is intended for use with chat models, and will read the tokenizer's chat_template attribute to - * determine the format and control tokens to use when converting. - * - * See [here](https://huggingface.co/docs/transformers/chat_templating) for more information. - * - * **Example:** Applying a chat template to a conversation. - * - * ```javascript - * import { AutoTokenizer } from "@huggingface/transformers"; - * - * const tokenizer = await AutoTokenizer.from_pretrained("Xenova/mistral-tokenizer-v1"); - * - * const chat = [ - * { "role": "user", "content": "Hello, how are you?" }, - * { "role": "assistant", "content": "I'm doing great. How can I help you today?" }, - * { "role": "user", "content": "I'd like to show off how chat templating works!" }, - * ] - * - * const text = tokenizer.apply_chat_template(chat, { tokenize: false }); - * // "[INST] Hello, how are you? [/INST]I'm doing great. How can I help you today? [INST] I'd like to show off how chat templating works! [/INST]" - * - * const input_ids = tokenizer.apply_chat_template(chat, { tokenize: true, return_tensor: false }); - * // [1, 733, 16289, 28793, 22557, 28725, 910, 460, 368, 28804, 733, 28748, 16289, 28793, 28737, 28742, 28719, 2548, 1598, 28723, 1602, 541, 315, 1316, 368, 3154, 28804, 2, 28705, 733, 16289, 28793, 315, 28742, 28715, 737, 298, 1347, 805, 910, 10706, 5752, 1077, 3791, 28808, 733, 28748, 16289, 28793] - * ``` - * - * @param {Message[]} conversation A list of message objects with `"role"` and `"content"` keys, - * representing the chat history so far. - * @param {Object} options An optional object containing the following properties: - * @param {string} [options.chat_template=null] A Jinja template to use for this conversion. If - * this is not passed, the model's chat template will be used instead. - * @param {Object[]} [options.tools=null] - * A list of tools (callable functions) that will be accessible to the model. If the template does not - * support function calling, this argument will have no effect. Each tool should be passed as a JSON Schema, - * giving the name, description and argument types for the tool. See our - * [chat templating guide](https://huggingface.co/docs/transformers/main/en/chat_templating#automated-function-conversion-for-tool-use) - * for more information. - * @param {Record[]} [options.documents=null] - * A list of dicts representing documents that will be accessible to the model if it is performing RAG - * (retrieval-augmented generation). If the template does not support RAG, this argument will have no - * effect. We recommend that each document should be a dict containing "title" and "text" keys. Please - * see the RAG section of the [chat templating guide](https://huggingface.co/docs/transformers/main/en/chat_templating#arguments-for-RAG) - * for examples of passing documents with chat templates. - * @param {boolean} [options.add_generation_prompt=false] Whether to end the prompt with the token(s) that indicate - * the start of an assistant message. This is useful when you want to generate a response from the model. - * Note that this argument will be passed to the chat template, and so it must be supported in the - * template for this argument to have any effect. - * @param {boolean} [options.tokenize=true] Whether to tokenize the output. If false, the output will be a string. - * @param {boolean} [options.padding=false] Whether to pad sequences to the maximum length. Has no effect if tokenize is false. - * @param {boolean} [options.truncation=false] Whether to truncate sequences to the maximum length. Has no effect if tokenize is false. - * @param {number} [options.max_length=null] Maximum length (in tokens) to use for padding or truncation. Has no effect if tokenize is false. - * If not specified, the tokenizer's `max_length` attribute will be used as a default. - * @param {boolean} [options.return_tensor=true] Whether to return the output as a Tensor or an Array. Has no effect if tokenize is false. - * @param {boolean} [options.return_dict=true] Whether to return a dictionary with named outputs. Has no effect if tokenize is false. - * @param {Object} [options.tokenizer_kwargs={}] Additional options to pass to the tokenizer. - * @returns {string | Tensor | number[]| number[][]|BatchEncoding} The tokenized output. - */ - apply_chat_template(conversation, { - tools = null, - documents = null, - chat_template = null, - add_generation_prompt = false, - tokenize = true, - padding = false, - truncation = false, - max_length = null, - return_tensor = true, - return_dict = false, - tokenizer_kwargs = {}, - ...kwargs - } = {}) { - chat_template = this.get_chat_template({ chat_template, tools }); - if (typeof chat_template !== "string") { - throw Error(`chat_template must be a string, but got ${typeof chat_template}`); - } - let compiledTemplate = this._compiled_template_cache.get(chat_template); - if (compiledTemplate === void 0) { - compiledTemplate = new _huggingface_jinja__WEBPACK_IMPORTED_MODULE_6__.Template(chat_template); - this._compiled_template_cache.set(chat_template, compiledTemplate); - } - const special_tokens_map = /* @__PURE__ */ Object.create(null); - for (const key of SPECIAL_TOKEN_ATTRIBUTES) { - const value = this.getToken(key); - if (value) { - special_tokens_map[key] = value; - } - } - const rendered = compiledTemplate.render({ - messages: conversation, - add_generation_prompt, - tools, - documents, - ...special_tokens_map, - ...kwargs - }); - if (tokenize) { - const out = this._call(rendered, { - add_special_tokens: false, - padding, - truncation, - max_length, - return_tensor, - ...tokenizer_kwargs - }); - return return_dict ? out : out.input_ids; - } - return rendered; - } - } - class BertTokenizer extends PreTrainedTokenizer { - constructor() { - super(...arguments); - __publicField(this, "return_token_type_ids", true); - } - } - class AlbertTokenizer extends PreTrainedTokenizer { - constructor() { - super(...arguments); - __publicField(this, "return_token_type_ids", true); - } - } - class MobileBertTokenizer extends PreTrainedTokenizer { - constructor() { - super(...arguments); - __publicField(this, "return_token_type_ids", true); - } - } - class SqueezeBertTokenizer extends PreTrainedTokenizer { - constructor() { - super(...arguments); - __publicField(this, "return_token_type_ids", true); - } - } - class DebertaTokenizer extends PreTrainedTokenizer { - constructor() { - super(...arguments); - __publicField(this, "return_token_type_ids", true); - } - } - class DebertaV2Tokenizer extends PreTrainedTokenizer { - constructor() { - super(...arguments); - __publicField(this, "return_token_type_ids", true); - } - } - class HerbertTokenizer extends PreTrainedTokenizer { - constructor() { - super(...arguments); - __publicField(this, "return_token_type_ids", true); - } - } - class ConvBertTokenizer extends PreTrainedTokenizer { - constructor() { - super(...arguments); - __publicField(this, "return_token_type_ids", true); - } - } - class RoFormerTokenizer extends PreTrainedTokenizer { - constructor() { - super(...arguments); - __publicField(this, "return_token_type_ids", true); - } - } - class DistilBertTokenizer extends PreTrainedTokenizer { - } - class CamembertTokenizer extends PreTrainedTokenizer { - } - class XLMTokenizer extends PreTrainedTokenizer { - constructor(tokenizerJSON, tokenizerConfig) { - super(tokenizerJSON, tokenizerConfig); - __publicField(this, "return_token_type_ids", true); - console.warn('WARNING: `XLMTokenizer` is not yet supported by Hugging Face\'s "fast" tokenizers library. Therefore, you may experience slightly inaccurate results.'); - } - } - class ElectraTokenizer extends PreTrainedTokenizer { - constructor() { - super(...arguments); - __publicField(this, "return_token_type_ids", true); - } - } - class T5Tokenizer extends PreTrainedTokenizer { - } - class GPT2Tokenizer extends PreTrainedTokenizer { - } - class BartTokenizer extends PreTrainedTokenizer { - } - class MBartTokenizer extends PreTrainedTokenizer { - constructor(tokenizerJSON, tokenizerConfig) { - super(tokenizerJSON, tokenizerConfig); - this.languageRegex = /^[a-z]{2}_[A-Z]{2}$/; - this.language_codes = this.special_tokens.filter((x) => this.languageRegex.test(x)); - this.lang_to_token = (x) => x; - } - /** - * Helper function to build translation inputs for an `MBartTokenizer`. - * @param {string|string[]} raw_inputs The text to tokenize. - * @param {Object} tokenizer_options Options to be sent to the tokenizer - * @param {Object} generate_kwargs Generation options. - * @returns {Object} Object to be passed to the model. - */ - _build_translation_inputs(raw_inputs, tokenizer_options, generate_kwargs) { - return _build_translation_inputs(this, raw_inputs, tokenizer_options, generate_kwargs); - } - } - class MBart50Tokenizer extends MBartTokenizer { - } - class RobertaTokenizer extends PreTrainedTokenizer { - } - class BloomTokenizer extends PreTrainedTokenizer { - } - const SPIECE_UNDERLINE = "▁"; - class LlamaTokenizer extends PreTrainedTokenizer { - constructor(tokenizerJSON, tokenizerConfig) { - super(tokenizerJSON, tokenizerConfig); - __publicField(this, "padding_side", "left"); - this.legacy = tokenizerConfig.legacy ?? true; - if (!this.legacy) { - this.normalizer = null; - this.pre_tokenizer = new MetaspacePreTokenizer({ - replacement: SPIECE_UNDERLINE, - add_prefix_space: true, - prepend_scheme: "first" - }); - } - } - /** - * Helper function to handle legacy encoding of SPM tokenizers. - * Adapted from https://github.com/huggingface/transformers/blob/e6dcf8abd6f65bb4b6dfc1831b20d9ba49ce00e2/src/transformers/models/t5/tokenization_t5.py#L374-L387 - * @param {string} text The text to encode. - * @returns {string[]} The encoded tokens. - */ - _encode_text(text) { - if (text === null) return null; - if (this.legacy || text.length === 0) { - return super._encode_text(text); - } - let tokens = super._encode_text(SPIECE_UNDERLINE + text.replaceAll(SPIECE_UNDERLINE, " ")); - if (tokens.length > 1 && tokens[0] === SPIECE_UNDERLINE && this.special_tokens.includes(tokens[1])) { - tokens = tokens.slice(1); - } - return tokens; - } - } - class CodeLlamaTokenizer extends PreTrainedTokenizer { - } - class XLMRobertaTokenizer extends PreTrainedTokenizer { - } - class MPNetTokenizer extends PreTrainedTokenizer { - } - class FalconTokenizer extends PreTrainedTokenizer { - } - class GPTNeoXTokenizer extends PreTrainedTokenizer { - } - class EsmTokenizer extends PreTrainedTokenizer { - } - class Qwen2Tokenizer extends PreTrainedTokenizer { - } - class GemmaTokenizer extends PreTrainedTokenizer { - } - class Grok1Tokenizer extends PreTrainedTokenizer { - } - function _build_translation_inputs(self2, raw_inputs, tokenizer_options, generate_kwargs) { - if (!("language_codes" in self2) || !Array.isArray(self2.language_codes)) { - throw new Error("Tokenizer must have `language_codes` attribute set and it should be an array of language ids."); - } - if (!("languageRegex" in self2) || !(self2.languageRegex instanceof RegExp)) { - throw new Error("Tokenizer must have `languageRegex` attribute set and it should be a regular expression."); - } - if (!("lang_to_token" in self2) || typeof self2.lang_to_token !== "function") { - throw new Error("Tokenizer must have `lang_to_token` attribute set and it should be a function."); - } - const src_lang_token = generate_kwargs.src_lang; - const tgt_lang_token = generate_kwargs.tgt_lang; - if (!self2.language_codes.includes(tgt_lang_token)) { - throw new Error(`Target language code "${tgt_lang_token}" is not valid. Must be one of: {${self2.language_codes.join(", ")}}`); - } - if (src_lang_token !== void 0) { - if (!self2.language_codes.includes(src_lang_token)) { - throw new Error(`Source language code "${src_lang_token}" is not valid. Must be one of: {${self2.language_codes.join(", ")}}`); - } - for (const item of self2.post_processor.config.single) { - if ("SpecialToken" in item && self2.languageRegex.test(item.SpecialToken.id)) { - item.SpecialToken.id = self2.lang_to_token(src_lang_token); - break; - } - } - } - generate_kwargs.forced_bos_token_id = self2.model.convert_tokens_to_ids([self2.lang_to_token(tgt_lang_token)])[0]; - return self2._call(raw_inputs, tokenizer_options); - } - class NllbTokenizer extends PreTrainedTokenizer { - constructor(tokenizerJSON, tokenizerConfig) { - super(tokenizerJSON, tokenizerConfig); - this.languageRegex = /^[a-z]{3}_[A-Z][a-z]{3}$/; - this.language_codes = this.special_tokens.filter((x) => this.languageRegex.test(x)); - this.lang_to_token = (x) => x; - } - /** - * Helper function to build translation inputs for an `NllbTokenizer`. - * @param {string|string[]} raw_inputs The text to tokenize. - * @param {Object} tokenizer_options Options to be sent to the tokenizer - * @param {Object} generate_kwargs Generation options. - * @returns {Object} Object to be passed to the model. - */ - _build_translation_inputs(raw_inputs, tokenizer_options, generate_kwargs) { - return _build_translation_inputs(this, raw_inputs, tokenizer_options, generate_kwargs); - } - } - class M2M100Tokenizer extends PreTrainedTokenizer { - constructor(tokenizerJSON, tokenizerConfig) { - super(tokenizerJSON, tokenizerConfig); - this.languageRegex = /^__[a-z]{2,3}__$/; - this.language_codes = this.special_tokens.filter((x) => this.languageRegex.test(x)).map((x) => x.slice(2, -2)); - this.lang_to_token = (x) => `__${x}__`; - } - /** - * Helper function to build translation inputs for an `M2M100Tokenizer`. - * @param {string|string[]} raw_inputs The text to tokenize. - * @param {Object} tokenizer_options Options to be sent to the tokenizer - * @param {Object} generate_kwargs Generation options. - * @returns {Object} Object to be passed to the model. - */ - _build_translation_inputs(raw_inputs, tokenizer_options, generate_kwargs) { - return _build_translation_inputs(this, raw_inputs, tokenizer_options, generate_kwargs); - } - } - class WhisperTokenizer extends PreTrainedTokenizer { - get timestamp_begin() { - return this.model.convert_tokens_to_ids(["<|notimestamps|>"])[0] + 1; - } - /** - * Decodes automatic speech recognition (ASR) sequences. - * @param {Array<{tokens: bigint[], token_timestamps?: number[], stride: number[]}>} sequences The sequences to decode. - * @param {Object} options The options to use for decoding. - * @returns {Array, text: string}>}>} The decoded sequences. - */ - _decode_asr(sequences, { - return_timestamps = false, - return_language = false, - time_precision = null, - force_full_sequences = true - } = {}) { - if (time_precision === null) { - throw Error("Must specify time_precision"); - } - let last_language = null; - const returnWordTimestamps = return_timestamps === "word"; - function new_chunk() { - return { "language": last_language, "timestamp": [null, null], "text": "" }; - } - const chunks = []; - let chunk = new_chunk(); - let time_offset = 0; - const timestamp_begin = this.timestamp_begin; - let previous_tokens = []; - let previous_token_timestamps = []; - let skip = false; - let right_stride_start = null; - const all_special_ids = new Set(this.all_special_ids); - for (const output of sequences) { - const token_ids = output.tokens; - const token_timestamps = returnWordTimestamps ? output.token_timestamps : null; - let last_timestamp = null; - let first_timestamp = timestamp_begin; - if ("stride" in output) { - const [chunk_len, stride_left, stride_right] = output.stride; - time_offset -= stride_left; - right_stride_start = chunk_len - stride_right; - if (stride_left) { - first_timestamp = stride_left / time_precision + timestamp_begin; - } - if (stride_right) { - for (let i = token_ids.length - 1; i >= 0; --i) { - const token = Number(token_ids[i]); - if (token >= timestamp_begin) { - if (last_timestamp !== null && (token - timestamp_begin) * time_precision < right_stride_start) { - break; - } - last_timestamp = token; - } - } - } - } - let current_tokens = []; - let current_token_timestamps = []; - for (let i = 0; i < token_ids.length; ++i) { - const token = Number(token_ids[i]); - if (all_special_ids.has(token)) { - const text = this.decode([token]); - const language = _models_whisper_common_whisper_js__WEBPACK_IMPORTED_MODULE_7__.WHISPER_LANGUAGE_MAPPING.get(text.slice(2, -2)); - if (language !== void 0) { - if (last_language !== null && language !== last_language && !return_timestamps) { - previous_tokens.push(current_tokens); - const resolved_tokens = this.findLongestCommonSequence(previous_tokens)[0]; - const resolved_text = this.decode(resolved_tokens); - chunk.text = resolved_text; - chunks.push(chunk); - previous_tokens = []; - current_tokens = []; - chunk = new_chunk(); - } - last_language = chunk.language = language; - } else { - } - } else if (token >= timestamp_begin) { - const time = (token - timestamp_begin) * time_precision + time_offset; - const rounded_time = (0, _utils_maths_js__WEBPACK_IMPORTED_MODULE_3__.round)(time, 2); - if (last_timestamp !== null && token >= last_timestamp) { - skip = true; - } else if (skip || previous_tokens.length > 0 && token < first_timestamp) { - skip = false; - } else if (chunk.timestamp[0] === null) { - chunk.timestamp[0] = rounded_time; - } else { - if (rounded_time === chunk.timestamp[0]) { - } else { - chunk.timestamp[1] = rounded_time; - previous_tokens.push(current_tokens); - if (returnWordTimestamps) { - previous_token_timestamps.push(current_token_timestamps); - } - const [resolved_tokens, resolved_token_timestamps] = this.findLongestCommonSequence( - previous_tokens, - previous_token_timestamps - ); - const resolved_text = this.decode(resolved_tokens); - chunk.text = resolved_text; - if (returnWordTimestamps) { - chunk.words = this.collateWordTimestamps( - resolved_tokens, - resolved_token_timestamps, - last_language - ); - } - chunks.push(chunk); - previous_tokens = []; - current_tokens = []; - previous_token_timestamps = []; - current_token_timestamps = []; - chunk = new_chunk(); - } - } - } else { - current_tokens.push(token); - if (returnWordTimestamps) { - let start_time = (0, _utils_maths_js__WEBPACK_IMPORTED_MODULE_3__.round)(token_timestamps[i] + time_offset, 2); - let end_time; - if (i + 1 < token_timestamps.length) { - end_time = (0, _utils_maths_js__WEBPACK_IMPORTED_MODULE_3__.round)(token_timestamps[i + 1] + time_offset, 2); - const decoded_text = this.decode([token]); - if (PUNCTUATION_ONLY_REGEX.test(decoded_text)) { - end_time = (0, _utils_maths_js__WEBPACK_IMPORTED_MODULE_3__.round)(Math.min(start_time + time_precision, end_time), 2); - } - } else { - end_time = null; - } - current_token_timestamps.push([start_time, end_time]); - } - } - } - if ("stride" in output) { - const [chunk_len, stride_left, stride_right] = output.stride; - time_offset += chunk_len - stride_right; - } - if (current_tokens.length > 0) { - previous_tokens.push(current_tokens); - if (returnWordTimestamps) { - previous_token_timestamps.push(current_token_timestamps); - } - } else if (previous_tokens.every((p) => p.length === 0)) { - chunk = new_chunk(); - previous_tokens = []; - current_tokens = []; - previous_token_timestamps = []; - current_token_timestamps = []; - } - } - if (previous_tokens.length > 0) { - if (force_full_sequences && return_timestamps) { - throw new Error( - "Whisper did not predict an ending timestamp, which can happen if audio is cut off in the middle of a word. Also make sure WhisperTimeStampLogitsProcessor was used during generation." - ); - } - const [resolved_tokens, resolved_token_timestamps] = this.findLongestCommonSequence(previous_tokens, previous_token_timestamps); - const resolved_text = this.decode(resolved_tokens); - chunk.text = resolved_text; - if (returnWordTimestamps) { - chunk.words = this.collateWordTimestamps( - resolved_tokens, - resolved_token_timestamps, - last_language - ); - } - chunks.push(chunk); - } - let optional = /* @__PURE__ */ Object.create(null); - const full_text = chunks.map((chunk2) => chunk2.text).join(""); - if (return_timestamps || return_language) { - for (let i = 0; i < chunks.length; ++i) { - const chunk2 = chunks[i]; - if (!return_timestamps) { - delete chunk2["timestamp"]; - } - if (!return_language) { - delete chunk2["language"]; - } - } - if (returnWordTimestamps) { - const new_chunks = []; - for (const chunk2 of chunks) { - for (const word of chunk2.words) { - new_chunks.push(word); - } - } - optional = { "chunks": new_chunks }; - } else { - optional = { "chunks": chunks }; - } - } - return [full_text, optional]; - } - /** - * Finds the longest common sequence among the provided sequences. - * @param {number[][]} sequences An array of sequences of token ids to compare. - * @returns {number[][]} The longest common sequence found. - * @throws {Error} If there is a bug within the function. - * @private - */ - findLongestCommonSequence(sequences, token_timestamp_sequences = null) { - let leftSequence = sequences[0]; - let leftLength = leftSequence.length; - let totalSequence = []; - const use_token_timestamp_sequences = Array.isArray(token_timestamp_sequences) && token_timestamp_sequences.length > 0; - let total_token_timestamp_sequence = use_token_timestamp_sequences ? [] : null; - let left_token_timestamp_sequence = use_token_timestamp_sequences ? token_timestamp_sequences[0] : null; - for (let i = 1; i < sequences.length; ++i) { - const rightSequence = sequences[i]; - let max = 0; - let maxIndices = [leftLength, leftLength, 0, 0]; - const rightLength = rightSequence.length; - for (let j = 1; j < leftLength + rightLength; ++j) { - const leftStart2 = Math.max(0, leftLength - j); - const leftStop2 = Math.min(leftLength, leftLength + rightLength - j); - const left = leftSequence.slice(leftStart2, leftStop2); - const rightStart2 = Math.max(0, j - leftLength); - const rightStop2 = Math.min(rightLength, j); - const right = rightSequence.slice(rightStart2, rightStop2); - if (left.length !== right.length) { - throw new Error("There is a bug within whisper `decode_asr` function, please report it. Dropping to prevent bad inference."); - } - let matches; - if (use_token_timestamp_sequences) { - matches = left.filter((elem, idx) => elem === right[idx] && left_token_timestamp_sequence[leftStart2 + idx] <= token_timestamp_sequences[i][rightStart2 + idx]).length; - } else { - matches = left.filter((elem, idx) => elem === right[idx]).length; - } - const eps = j / 1e4; - const matching = matches / j + eps; - if (matches > 1 && matching > max) { - max = matching; - maxIndices = [leftStart2, leftStop2, rightStart2, rightStop2]; - } - } - const [leftStart, leftStop, rightStart, rightStop] = maxIndices; - const leftMid = Math.floor((leftStop + leftStart) / 2); - const rightMid = Math.floor((rightStop + rightStart) / 2); - totalSequence.push(...leftSequence.slice(0, leftMid)); - leftSequence = rightSequence.slice(rightMid); - leftLength = leftSequence.length; - if (use_token_timestamp_sequences) { - total_token_timestamp_sequence.push(...left_token_timestamp_sequence.slice(0, leftMid)); - left_token_timestamp_sequence = token_timestamp_sequences[i].slice(rightMid); - } - } - totalSequence.push(...leftSequence); - if (use_token_timestamp_sequences) { - total_token_timestamp_sequence.push(...left_token_timestamp_sequence); - return [totalSequence, total_token_timestamp_sequence]; - } else { - return [totalSequence, []]; - } - } - /** @private */ - collateWordTimestamps(tokens, token_timestamps, language) { - const [words, _, token_indices] = this.combineTokensIntoWords(tokens, language); - const timings = []; - for (let i = 0; i < words.length; ++i) { - const indices = token_indices[i]; - timings.push({ - text: words[i], - timestamp: [ - token_timestamps[indices.at(0)][0], - token_timestamps[indices.at(-1)][1] - ] - }); - } - return timings; - } - /** - * Groups tokens by word. Returns a tuple containing a list of strings with the words, - * and a list of `token_id` sequences with the tokens making up each word. - * @param {number[]} tokens - * @param {string} [language] - * @param {string} prepend_punctionations - * @param {string} append_punctuations - * - * @private - */ - combineTokensIntoWords(tokens, language, prepend_punctionations = `"'“¡¿([{-`, append_punctuations = `"'.。,,!!??::”)]}、`) { - language = language ?? "english"; - let words, word_tokens, token_indices; - if (["chinese", "japanese", "thai", "lao", "myanmar"].includes(language)) { - [words, word_tokens, token_indices] = this.splitTokensOnUnicode(tokens); - } else { - [words, word_tokens, token_indices] = this.splitTokensOnSpaces(tokens); - } - return this.mergePunctuations(words, word_tokens, token_indices, prepend_punctionations, append_punctuations); - } - /** @type {PreTrainedTokenizer['decode']} */ - decode(token_ids, decode_args) { - let text; - if (decode_args == null ? void 0 : decode_args.decode_with_timestamps) { - if (token_ids instanceof _utils_tensor_js__WEBPACK_IMPORTED_MODULE_4__.Tensor) { - token_ids = prepareTensorForDecode(token_ids); - } - text = this.decodeWithTimestamps(token_ids, decode_args); - } else { - text = super.decode(token_ids, decode_args); - } - return text; - } - /** - * @param {number[]|bigint[]} token_ids List of token IDs to decode. - * @param {Object} decode_args Optional arguments for decoding - * @private - */ - decodeWithTimestamps(token_ids, decode_args) { - const time_precision = (decode_args == null ? void 0 : decode_args.time_precision) ?? 0.02; - const timestamp_begin = Array.from(this.all_special_ids).at(-1) + 1; - let outputs = [[]]; - for (let token of token_ids) { - token = Number(token); - if (token >= timestamp_begin) { - const timestamp = ((token - timestamp_begin) * time_precision).toFixed(2); - outputs.push(`<|${timestamp}|>`); - outputs.push([]); - } else { - outputs[outputs.length - 1].push(token); - } - } - outputs = outputs.map( - (s) => typeof s === "string" ? s : super.decode(s, decode_args) - ); - return outputs.join(""); - } - /** - * Combine tokens into words by splitting at any position where the tokens are decoded as valid unicode points. - * @param {number[]} tokens - * @returns {*} - * @private - */ - splitTokensOnUnicode(tokens) { - const decoded_full = this.decode(tokens, { - // @ts-ignore - decode_with_timestamps: true - }); - const replacement_char = "�"; - const words = []; - const word_tokens = []; - const token_indices = []; - let current_tokens = []; - let current_indices = []; - let unicode_offset = 0; - for (let token_idx = 0; token_idx < tokens.length; ++token_idx) { - const token = tokens[token_idx]; - current_tokens.push(token); - current_indices.push(token_idx); - const decoded = this.decode(current_tokens, { - // @ts-ignore - decode_with_timestamps: true - }); - if (!decoded.includes(replacement_char) || decoded_full[unicode_offset + decoded.indexOf(replacement_char)] === replacement_char) { - words.push(decoded); - word_tokens.push(current_tokens); - token_indices.push(current_indices); - current_tokens = []; - current_indices = []; - unicode_offset += decoded.length; - } - } - return [words, word_tokens, token_indices]; - } - /** - * Combine tokens into words by splitting at whitespace and punctuation tokens. - * @param {number[]} tokens - * @private - */ - splitTokensOnSpaces(tokens) { - const [subwords, subword_tokens_list, subword_indices_list] = this.splitTokensOnUnicode(tokens); - const words = []; - const word_tokens = []; - const token_indices = []; - const punctuationRegex = new RegExp(`^[${PUNCTUATION_REGEX}]$`, "gu"); - for (let i = 0; i < subwords.length; ++i) { - const subword = subwords[i]; - const subword_tokens = subword_tokens_list[i]; - const subword_indices = subword_indices_list[i]; - const special = subword_tokens[0] >= this.model.tokens_to_ids.get("<|endoftext|>"); - const with_space = subword.startsWith(" "); - const trimmed = subword.trim(); - const punctuation = punctuationRegex.test(trimmed); - if (special || with_space || punctuation || words.length === 0) { - words.push(subword); - word_tokens.push(subword_tokens); - token_indices.push(subword_indices); - } else { - const ix = words.length - 1; - words[ix] += subword; - word_tokens[ix].push(...subword_tokens); - token_indices[ix].push(...subword_indices); - } - } - return [words, word_tokens, token_indices]; - } - /** - * Merges punctuation tokens with neighboring words. - * @param {string[]} words - * @param {number[][]} tokens - * @param {number[][]} indices - * @param {string} prepended - * @param {string} appended - * @private - */ - mergePunctuations(words, tokens, indices, prepended, appended) { - const newWords = structuredClone(words); - const newTokens = structuredClone(tokens); - const newIndices = structuredClone(indices); - let i = newWords.length - 2; - let j = newWords.length - 1; - while (i >= 0) { - if (newWords[i].startsWith(" ") && prepended.includes(newWords[i].trim())) { - newWords[j] = newWords[i] + newWords[j]; - newTokens[j] = (0, _utils_core_js__WEBPACK_IMPORTED_MODULE_1__.mergeArrays)(newTokens[i], newTokens[j]); - newIndices[j] = (0, _utils_core_js__WEBPACK_IMPORTED_MODULE_1__.mergeArrays)(newIndices[i], newIndices[j]); - newWords[i] = ""; - newTokens[i] = []; - newIndices[i] = []; - } else { - j = i; - } - --i; - } - i = 0; - j = 1; - while (j < newWords.length) { - if (!newWords[i].endsWith(" ") && appended.includes(newWords[j])) { - newWords[i] += newWords[j]; - newTokens[i] = (0, _utils_core_js__WEBPACK_IMPORTED_MODULE_1__.mergeArrays)(newTokens[i], newTokens[j]); - newIndices[i] = (0, _utils_core_js__WEBPACK_IMPORTED_MODULE_1__.mergeArrays)(newIndices[i], newIndices[j]); - newWords[j] = ""; - newTokens[j] = []; - newIndices[j] = []; - } else { - i = j; - } - ++j; - } - return [ - newWords.filter((x) => x), - newTokens.filter((x) => x.length > 0), - newIndices.filter((x) => x.length > 0) - ]; - } - } - class CodeGenTokenizer extends PreTrainedTokenizer { - } - class CLIPTokenizer extends PreTrainedTokenizer { - } - class SiglipTokenizer extends PreTrainedTokenizer { - } - class MarianTokenizer extends PreTrainedTokenizer { - /** - * Create a new MarianTokenizer instance. - * @param {Object} tokenizerJSON The JSON of the tokenizer. - * @param {Object} tokenizerConfig The config of the tokenizer. - */ - constructor(tokenizerJSON, tokenizerConfig) { - super(tokenizerJSON, tokenizerConfig); - this.languageRegex = /^(>>\w+<<)\s*/g; - this.supported_language_codes = this.model.vocab.filter( - (x) => this.languageRegex.test(x) - ); - console.warn('WARNING: `MarianTokenizer` is not yet supported by Hugging Face\'s "fast" tokenizers library. Therefore, you may experience slightly inaccurate results.'); - } - /** - * Encodes a single text. Overriding this method is necessary since the language codes - * must be removed before encoding with sentencepiece model. - * @see https://github.com/huggingface/transformers/blob/12d51db243a00726a548a43cc333390ebae731e3/src/transformers/models/marian/tokenization_marian.py#L204-L213 - * - * @param {string|null} text The text to encode. - * @returns {Array} The encoded tokens. - */ - _encode_text(text) { - if (text === null) return null; - const [matchInfo, ...remainder] = text.trim().split(this.languageRegex); - if (remainder.length === 0) { - return super._encode_text(matchInfo); - } else if (remainder.length === 2) { - const [language, text2] = remainder; - if (!this.supported_language_codes.includes(language)) { - console.warn(`Unsupported language code "${language}" detected, which may lead to unexpected behavior. Should be one of: ${JSON.stringify(this.supported_language_codes)}`); - } - return (0, _utils_core_js__WEBPACK_IMPORTED_MODULE_1__.mergeArrays)([language], super._encode_text(text2)); - } - } - } - class Wav2Vec2CTCTokenizer extends PreTrainedTokenizer { - } - class BlenderbotTokenizer extends PreTrainedTokenizer { - } - class BlenderbotSmallTokenizer extends PreTrainedTokenizer { - } - class SpeechT5Tokenizer extends PreTrainedTokenizer { - } - class NougatTokenizer extends PreTrainedTokenizer { - } - class VitsTokenizer extends PreTrainedTokenizer { - constructor(tokenizerJSON, tokenizerConfig) { - super(tokenizerJSON, tokenizerConfig); - this.decoder = new VitsDecoder({}); - } - } - class CohereTokenizer extends PreTrainedTokenizer { - } - class AutoTokenizer { - /** - * Instantiate one of the tokenizer classes of the library from a pretrained model. - * - * The tokenizer class to instantiate is selected based on the `tokenizer_class` property of the config object - * (either passed as an argument or loaded from `pretrained_model_name_or_path` if possible) - * - * @param {string} pretrained_model_name_or_path The name or path of the pretrained model. Can be either: - * - A string, the *model id* of a pretrained tokenizer hosted inside a model repo on huggingface.co. - * Valid model ids can be located at the root-level, like `bert-base-uncased`, or namespaced under a - * user or organization name, like `dbmdz/bert-base-german-cased`. - * - A path to a *directory* containing tokenizer files, e.g., `./my_model_directory/`. - * @param {PretrainedTokenizerOptions} options Additional options for loading the tokenizer. - * - * @returns {Promise} A new instance of the PreTrainedTokenizer class. - */ - static async from_pretrained(pretrained_model_name_or_path, { - progress_callback = null, - config = null, - cache_dir = null, - local_files_only = false, - revision = "main", - legacy = null - } = {}) { - var _a; - const [tokenizerJSON, tokenizerConfig] = await loadTokenizer(pretrained_model_name_or_path, { - progress_callback, - config, - cache_dir, - local_files_only, - revision, - legacy - }); - const tokenizerName = ((_a = tokenizerConfig.tokenizer_class) == null ? void 0 : _a.replace(/Fast$/, "")) ?? "PreTrainedTokenizer"; - let cls = this.TOKENIZER_CLASS_MAPPING[tokenizerName]; - if (!cls) { - console.warn(`Unknown tokenizer class "${tokenizerName}", attempting to construct from base class.`); - cls = PreTrainedTokenizer; - } - return new cls(tokenizerJSON, tokenizerConfig); - } - } - __publicField(AutoTokenizer, "TOKENIZER_CLASS_MAPPING", { - T5Tokenizer, - DistilBertTokenizer, - CamembertTokenizer, - DebertaTokenizer, - DebertaV2Tokenizer, - BertTokenizer, - HerbertTokenizer, - ConvBertTokenizer, - RoFormerTokenizer, - XLMTokenizer, - ElectraTokenizer, - MobileBertTokenizer, - SqueezeBertTokenizer, - AlbertTokenizer, - GPT2Tokenizer, - BartTokenizer, - MBartTokenizer, - MBart50Tokenizer, - RobertaTokenizer, - WhisperTokenizer, - CodeGenTokenizer, - CLIPTokenizer, - SiglipTokenizer, - MarianTokenizer, - BloomTokenizer, - NllbTokenizer, - M2M100Tokenizer, - LlamaTokenizer, - CodeLlamaTokenizer, - XLMRobertaTokenizer, - MPNetTokenizer, - FalconTokenizer, - GPTNeoXTokenizer, - EsmTokenizer, - Wav2Vec2CTCTokenizer, - BlenderbotTokenizer, - BlenderbotSmallTokenizer, - SpeechT5Tokenizer, - NougatTokenizer, - VitsTokenizer, - Qwen2Tokenizer, - GemmaTokenizer, - Grok1Tokenizer, - CohereTokenizer, - // Base case: - PreTrainedTokenizer - }); - } - ), - /***/ - "./src/utils/audio.js": ( - /*!****************************!*\ - !*** ./src/utils/audio.js ***! - \****************************/ - /***/ - (__unused_webpack___webpack_module__, __webpack_exports__2, __webpack_require__2) => { - __webpack_require__2.r(__webpack_exports__2); - __webpack_require__2.d(__webpack_exports__2, { - /* harmony export */ - hamming: () => ( - /* binding */ - hamming - ), - /* harmony export */ - hanning: () => ( - /* binding */ - hanning - ), - /* harmony export */ - mel_filter_bank: () => ( - /* binding */ - mel_filter_bank - ), - /* harmony export */ - read_audio: () => ( - /* binding */ - read_audio - ), - /* harmony export */ - spectrogram: () => ( - /* binding */ - spectrogram - ), - /* harmony export */ - window_function: () => ( - /* binding */ - window_function - ) - /* harmony export */ - }); - var _hub_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__2( - /*! ./hub.js */ - "./src/utils/hub.js" - ); - var _maths_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__2( - /*! ./maths.js */ - "./src/utils/maths.js" - ); - var _core_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__2( - /*! ./core.js */ - "./src/utils/core.js" - ); - var _tensor_js__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__2( - /*! ./tensor.js */ - "./src/utils/tensor.js" - ); - async function read_audio(url, sampling_rate) { - if (typeof AudioContext === "undefined") { - throw Error( - "Unable to load audio from path/URL since `AudioContext` is not available in your environment. Instead, audio data should be passed directly to the pipeline/processor. For more information and some example code, see https://huggingface.co/docs/transformers.js/guides/node-audio-processing." - ); - } - const response = await (await (0, _hub_js__WEBPACK_IMPORTED_MODULE_0__.getFile)(url)).arrayBuffer(); - const audioCTX = new AudioContext({ sampleRate: sampling_rate }); - if (typeof sampling_rate === "undefined") { - console.warn(`No sampling rate provided, using default of ${audioCTX.sampleRate}Hz.`); - } - const decoded = await audioCTX.decodeAudioData(response); - let audio; - if (decoded.numberOfChannels === 2) { - const SCALING_FACTOR = Math.sqrt(2); - const left = decoded.getChannelData(0); - const right = decoded.getChannelData(1); - audio = new Float32Array(left.length); - for (let i = 0; i < decoded.length; ++i) { - audio[i] = SCALING_FACTOR * (left[i] + right[i]) / 2; - } - } else { - audio = decoded.getChannelData(0); - } - return audio; - } - function generalized_cosine_window(M, a_0) { - if (M < 1) { - return new Float64Array(); - } - if (M === 1) { - return new Float64Array([1]); - } - const a_1 = 1 - a_0; - const factor = 2 * Math.PI / (M - 1); - const cos_vals = new Float64Array(M); - for (let i = 0; i < M; ++i) { - cos_vals[i] = a_0 - a_1 * Math.cos(i * factor); - } - return cos_vals; - } - function hanning(M) { - return generalized_cosine_window(M, 0.5); - } - function hamming(M) { - return generalized_cosine_window(M, 0.54); - } - const HERTZ_TO_MEL_MAPPING = { - "htk": (freq) => 2595 * Math.log10(1 + freq / 700), - "kaldi": (freq) => 1127 * Math.log(1 + freq / 700), - "slaney": (freq, min_log_hertz = 1e3, min_log_mel = 15, logstep = 27 / Math.log(6.4)) => freq >= min_log_hertz ? min_log_mel + Math.log(freq / min_log_hertz) * logstep : 3 * freq / 200 - }; - function hertz_to_mel(freq, mel_scale = "htk") { - const fn = HERTZ_TO_MEL_MAPPING[mel_scale]; - if (!fn) { - throw new Error('mel_scale should be one of "htk", "slaney" or "kaldi".'); - } - return typeof freq === "number" ? fn(freq) : freq.map((x) => fn(x)); - } - const MEL_TO_HERTZ_MAPPING = { - "htk": (mels) => 700 * (10 ** (mels / 2595) - 1), - "kaldi": (mels) => 700 * (Math.exp(mels / 1127) - 1), - "slaney": (mels, min_log_hertz = 1e3, min_log_mel = 15, logstep = Math.log(6.4) / 27) => mels >= min_log_mel ? min_log_hertz * Math.exp(logstep * (mels - min_log_mel)) : 200 * mels / 3 - }; - function mel_to_hertz(mels, mel_scale = "htk") { - const fn = MEL_TO_HERTZ_MAPPING[mel_scale]; - if (!fn) { - throw new Error('mel_scale should be one of "htk", "slaney" or "kaldi".'); - } - return typeof mels === "number" ? fn(mels) : mels.map((x) => fn(x)); - } - function _create_triangular_filter_bank(fft_freqs, filter_freqs) { - const filter_diff = Float64Array.from( - { length: filter_freqs.length - 1 }, - (_, i) => filter_freqs[i + 1] - filter_freqs[i] - ); - const slopes = Array.from({ - length: fft_freqs.length - }, () => new Array(filter_freqs.length)); - for (let j = 0; j < fft_freqs.length; ++j) { - const slope = slopes[j]; - for (let i = 0; i < filter_freqs.length; ++i) { - slope[i] = filter_freqs[i] - fft_freqs[j]; - } - } - const numFreqs = filter_freqs.length - 2; - const ret = Array.from({ length: numFreqs }, () => new Array(fft_freqs.length)); - for (let j = 0; j < fft_freqs.length; ++j) { - const slope = slopes[j]; - for (let i = 0; i < numFreqs; ++i) { - const down = -slope[i] / filter_diff[i]; - const up = slope[i + 2] / filter_diff[i + 1]; - ret[i][j] = Math.max(0, Math.min(down, up)); - } - } - return ret; - } - function linspace(start, end, num) { - const step = (end - start) / (num - 1); - return Float64Array.from({ length: num }, (_, i) => start + step * i); - } - function mel_filter_bank(num_frequency_bins, num_mel_filters, min_frequency, max_frequency, sampling_rate, norm = null, mel_scale = "htk", triangularize_in_mel_space = false) { - if (norm !== null && norm !== "slaney") { - throw new Error('norm must be one of null or "slaney"'); - } - const mel_min = hertz_to_mel(min_frequency, mel_scale); - const mel_max = hertz_to_mel(max_frequency, mel_scale); - const mel_freqs = linspace(mel_min, mel_max, num_mel_filters + 2); - let filter_freqs = mel_to_hertz(mel_freqs, mel_scale); - let fft_freqs; - if (triangularize_in_mel_space) { - const fft_bin_width = sampling_rate / (num_frequency_bins * 2); - fft_freqs = hertz_to_mel(Float64Array.from({ length: num_frequency_bins }, (_, i) => i * fft_bin_width), mel_scale); - filter_freqs = mel_freqs; - } else { - fft_freqs = linspace(0, Math.floor(sampling_rate / 2), num_frequency_bins); - } - const mel_filters = _create_triangular_filter_bank(fft_freqs, filter_freqs); - if (norm !== null && norm === "slaney") { - for (let i = 0; i < num_mel_filters; ++i) { - const filter = mel_filters[i]; - const enorm = 2 / (filter_freqs[i + 2] - filter_freqs[i]); - for (let j = 0; j < num_frequency_bins; ++j) { - filter[j] *= enorm; - } - } - } - return mel_filters; - } - function padReflect(array, left, right) { - const padded = new array.constructor(array.length + left + right); - const w = array.length - 1; - for (let i = 0; i < array.length; ++i) { - padded[left + i] = array[i]; - } - for (let i = 1; i <= left; ++i) { - padded[left - i] = array[(0, _core_js__WEBPACK_IMPORTED_MODULE_2__.calculateReflectOffset)(i, w)]; - } - for (let i = 1; i <= right; ++i) { - padded[w + left + i] = array[(0, _core_js__WEBPACK_IMPORTED_MODULE_2__.calculateReflectOffset)(w - i, w)]; - } - return padded; - } - function _db_conversion_helper(spectrogram2, factor, reference, min_value, db_range) { - if (reference <= 0) { - throw new Error("reference must be greater than zero"); - } - if (min_value <= 0) { - throw new Error("min_value must be greater than zero"); - } - reference = Math.max(min_value, reference); - const logReference = Math.log10(reference); - for (let i = 0; i < spectrogram2.length; ++i) { - spectrogram2[i] = factor * Math.log10(Math.max(min_value, spectrogram2[i]) - logReference); - } - if (db_range !== null) { - if (db_range <= 0) { - throw new Error("db_range must be greater than zero"); - } - const maxValue = (0, _maths_js__WEBPACK_IMPORTED_MODULE_1__.max)(spectrogram2)[0] - db_range; - for (let i = 0; i < spectrogram2.length; ++i) { - spectrogram2[i] = Math.max(spectrogram2[i], maxValue); - } - } - return spectrogram2; - } - function amplitude_to_db(spectrogram2, reference = 1, min_value = 1e-5, db_range = null) { - return _db_conversion_helper(spectrogram2, 20, reference, min_value, db_range); - } - function power_to_db(spectrogram2, reference = 1, min_value = 1e-10, db_range = null) { - return _db_conversion_helper(spectrogram2, 10, reference, min_value, db_range); - } - async function spectrogram(waveform, window2, frame_length, hop_length, { - fft_length = null, - power = 1, - center = true, - pad_mode = "reflect", - onesided = true, - preemphasis = null, - mel_filters = null, - mel_floor = 1e-10, - log_mel = null, - reference = 1, - min_value = 1e-10, - db_range = null, - remove_dc_offset = null, - // Custom parameters for efficiency reasons - min_num_frames = null, - max_num_frames = null, - do_pad = true, - transpose = false - } = {}) { - const window_length = window2.length; - if (fft_length === null) { - fft_length = frame_length; - } - if (frame_length > fft_length) { - throw Error(`frame_length (${frame_length}) may not be larger than fft_length (${fft_length})`); - } - if (window_length !== frame_length) { - throw new Error(`Length of the window (${window_length}) must equal frame_length (${frame_length})`); - } - if (hop_length <= 0) { - throw new Error("hop_length must be greater than zero"); - } - if (power === null && mel_filters !== null) { - throw new Error( - "You have provided `mel_filters` but `power` is `None`. Mel spectrogram computation is not yet supported for complex-valued spectrogram. Specify `power` to fix this issue." - ); - } - if (center) { - if (pad_mode !== "reflect") { - throw new Error(`pad_mode="${pad_mode}" not implemented yet.`); - } - const half_window = Math.floor((fft_length - 1) / 2) + 1; - waveform = padReflect(waveform, half_window, half_window); - } - let num_frames = Math.floor(1 + Math.floor((waveform.length - frame_length) / hop_length)); - if (min_num_frames !== null && num_frames < min_num_frames) { - num_frames = min_num_frames; - } - const num_frequency_bins = onesided ? Math.floor(fft_length / 2) + 1 : fft_length; - let d1 = num_frames; - let d1Max = num_frames; - if (max_num_frames !== null) { - if (max_num_frames > num_frames) { - if (do_pad) { - d1Max = max_num_frames; - } - } else { - d1Max = d1 = max_num_frames; - } - } - const fft = new _maths_js__WEBPACK_IMPORTED_MODULE_1__.FFT(fft_length); - const inputBuffer = new Float64Array(fft_length); - const outputBuffer = new Float64Array(fft.outputBufferSize); - const transposedMagnitudeData = new Float32Array(num_frequency_bins * d1Max); - for (let i = 0; i < d1; ++i) { - const offset = i * hop_length; - const buffer_size = Math.min(waveform.length - offset, frame_length); - if (buffer_size !== frame_length) { - inputBuffer.fill(0, 0, frame_length); - } - for (let j = 0; j < buffer_size; ++j) { - inputBuffer[j] = waveform[offset + j]; - } - if (remove_dc_offset) { - let sum = 0; - for (let j = 0; j < buffer_size; ++j) { - sum += inputBuffer[j]; - } - const mean = sum / buffer_size; - for (let j = 0; j < buffer_size; ++j) { - inputBuffer[j] -= mean; - } - } - if (preemphasis !== null) { - for (let j = buffer_size - 1; j >= 1; --j) { - inputBuffer[j] -= preemphasis * inputBuffer[j - 1]; - } - inputBuffer[0] *= 1 - preemphasis; - } - for (let j = 0; j < window2.length; ++j) { - inputBuffer[j] *= window2[j]; - } - fft.realTransform(outputBuffer, inputBuffer); - for (let j = 0; j < num_frequency_bins; ++j) { - const j2 = j << 1; - transposedMagnitudeData[j * d1Max + i] = outputBuffer[j2] ** 2 + outputBuffer[j2 + 1] ** 2; - } - } - if (power !== null && power !== 2) { - const pow = 2 / power; - for (let i = 0; i < transposedMagnitudeData.length; ++i) { - transposedMagnitudeData[i] **= pow; - } - } - const num_mel_filters = mel_filters.length; - let mel_spec = await (0, _tensor_js__WEBPACK_IMPORTED_MODULE_3__.matmul)( - // TODO: Make `mel_filters` a Tensor during initialization - new _tensor_js__WEBPACK_IMPORTED_MODULE_3__.Tensor("float32", mel_filters.flat(), [num_mel_filters, num_frequency_bins]), - new _tensor_js__WEBPACK_IMPORTED_MODULE_3__.Tensor("float32", transposedMagnitudeData, [num_frequency_bins, d1Max]) - ); - if (transpose) { - mel_spec = mel_spec.transpose(1, 0); - } - const mel_spec_data = ( - /** @type {Float32Array} */ - mel_spec.data - ); - for (let i = 0; i < mel_spec_data.length; ++i) { - mel_spec_data[i] = Math.max(mel_floor, mel_spec_data[i]); - } - if (power !== null && log_mel !== null) { - const o = Math.min(mel_spec_data.length, d1 * num_mel_filters); - switch (log_mel) { - case "log": - for (let i = 0; i < o; ++i) { - mel_spec_data[i] = Math.log(mel_spec_data[i]); - } - break; - case "log10": - for (let i = 0; i < o; ++i) { - mel_spec_data[i] = Math.log10(mel_spec_data[i]); - } - break; - case "dB": - if (power === 1) { - amplitude_to_db(mel_spec_data, reference, min_value, db_range); - } else if (power === 2) { - power_to_db(mel_spec_data, reference, min_value, db_range); - } else { - throw new Error(`Cannot use log_mel option '${log_mel}' with power ${power}`); - } - break; - default: - throw new Error(`log_mel must be one of null, 'log', 'log10' or 'dB'. Got '${log_mel}'`); - } - } - return mel_spec; - } - function window_function(window_length, name, { - periodic = true, - frame_length = null, - center = true - } = {}) { - const length = periodic ? window_length + 1 : window_length; - let window2; - switch (name) { - case "boxcar": - window2 = new Float64Array(length).fill(1); - break; - case "hann": - case "hann_window": - window2 = hanning(length); - break; - case "hamming": - window2 = hamming(length); - break; - case "povey": - window2 = hanning(length).map((x) => Math.pow(x, 0.85)); - break; - default: - throw new Error(`Unknown window type ${name}.`); - } - if (periodic) { - window2 = window2.subarray(0, window_length); - } - if (frame_length === null) { - return window2; - } - if (window_length > frame_length) { - throw new Error(`Length of the window (${window_length}) may not be larger than frame_length (${frame_length})`); - } - return window2; - } - } - ), - /***/ - "./src/utils/constants.js": ( - /*!********************************!*\ - !*** ./src/utils/constants.js ***! - \********************************/ - /***/ - (__unused_webpack___webpack_module__, __webpack_exports__2, __webpack_require__2) => { - __webpack_require__2.r(__webpack_exports__2); - __webpack_require__2.d(__webpack_exports__2, { - /* harmony export */ - GITHUB_ISSUE_URL: () => ( - /* binding */ - GITHUB_ISSUE_URL - ) - /* harmony export */ - }); - const GITHUB_ISSUE_URL = "https://github.com/huggingface/transformers.js/issues/new/choose"; - } - ), - /***/ - "./src/utils/core.js": ( - /*!***************************!*\ - !*** ./src/utils/core.js ***! - \***************************/ - /***/ - (__unused_webpack___webpack_module__, __webpack_exports__2, __webpack_require__2) => { - __webpack_require__2.r(__webpack_exports__2); - __webpack_require__2.d(__webpack_exports__2, { - /* harmony export */ - calculateDimensions: () => ( - /* binding */ - calculateDimensions - ), - /* harmony export */ - calculateReflectOffset: () => ( - /* binding */ - calculateReflectOffset - ), - /* harmony export */ - dispatchCallback: () => ( - /* binding */ - dispatchCallback - ), - /* harmony export */ - escapeRegExp: () => ( - /* binding */ - escapeRegExp - ), - /* harmony export */ - isIntegralNumber: () => ( - /* binding */ - isIntegralNumber - ), - /* harmony export */ - isTypedArray: () => ( - /* binding */ - isTypedArray - ), - /* harmony export */ - len: () => ( - /* binding */ - len - ), - /* harmony export */ - mergeArrays: () => ( - /* binding */ - mergeArrays - ), - /* harmony export */ - pick: () => ( - /* binding */ - pick - ), - /* harmony export */ - pop: () => ( - /* binding */ - pop - ), - /* harmony export */ - product: () => ( - /* binding */ - product - ), - /* harmony export */ - reverseDictionary: () => ( - /* binding */ - reverseDictionary - ) - /* harmony export */ - }); - function dispatchCallback(progress_callback, data) { - if (progress_callback) progress_callback(data); - } - function reverseDictionary(data) { - return Object.fromEntries(Object.entries(data).map(([key, value]) => [value, key])); - } - function escapeRegExp(string) { - return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); - } - function isTypedArray(val) { - var _a, _b, _c; - return ((_c = (_b = (_a = val == null ? void 0 : val.prototype) == null ? void 0 : _a.__proto__) == null ? void 0 : _b.constructor) == null ? void 0 : _c.name) === "TypedArray"; - } - function isIntegralNumber(x) { - return Number.isInteger(x) || typeof x === "bigint"; - } - function calculateDimensions(arr) { - const dimensions = []; - let current = arr; - while (Array.isArray(current)) { - dimensions.push(current.length); - current = current[0]; - } - return dimensions; - } - function pop(obj, key, defaultValue = void 0) { - const value = obj[key]; - if (value !== void 0) { - delete obj[key]; - return value; - } - if (defaultValue === void 0) { - throw Error(`Key ${key} does not exist in object.`); - } - return defaultValue; - } - function mergeArrays(...arrs) { - return Array.prototype.concat.apply([], arrs); - } - function product(...a) { - return a.reduce((a2, b) => a2.flatMap((d) => b.map((e) => [d, e]))); - } - function calculateReflectOffset(i, w) { - return Math.abs((i + w) % (2 * w) - w); - } - function pick(o, props) { - return Object.assign( - {}, - ...props.map((prop) => { - if (o[prop] !== void 0) { - return { [prop]: o[prop] }; - } - }) - ); - } - function len(s) { - let length = 0; - for (const c of s) ++length; - return length; - } - } - ), - /***/ - "./src/utils/data-structures.js": ( - /*!**************************************!*\ - !*** ./src/utils/data-structures.js ***! - \**************************************/ - /***/ - (__unused_webpack___webpack_module__, __webpack_exports__2, __webpack_require__2) => { - __webpack_require__2.r(__webpack_exports__2); - __webpack_require__2.d(__webpack_exports__2, { - /* harmony export */ - CharTrie: () => ( - /* binding */ - CharTrie - ), - /* harmony export */ - PriorityQueue: () => ( - /* binding */ - PriorityQueue - ), - /* harmony export */ - TokenLattice: () => ( - /* binding */ - TokenLattice - ) - /* harmony export */ - }); - class PriorityQueue { - /** - * Create a new PriorityQueue. - * @param {function(any, any): boolean} comparator Comparator function to determine priority. Defaults to a MaxHeap. - */ - constructor(comparator = (a, b) => a > b, maxSize = Infinity) { - this._heap = []; - this._comparator = comparator; - this._maxSize = maxSize; - } - /** - * The size of the queue - */ - get size() { - return this._heap.length; - } - /** - * Check if the queue is empty. - * @returns {boolean} `true` if the queue is empty, `false` otherwise. - */ - isEmpty() { - return this.size === 0; - } - /** - * Return the element with the highest priority in the queue. - * @returns {any} The highest priority element in the queue. - */ - peek() { - return this._heap[0]; - } - /** - * Add one or more elements to the queue. - * @param {...any} values The values to push into the queue. - * @returns {number} The new size of the queue. - */ - push(...values) { - return this.extend(values); - } - /** - * Add multiple elements to the queue. - * @param {any[]} values The values to push into the queue. - * @returns {number} The new size of the queue. - */ - extend(values) { - for (const value of values) { - if (this.size < this._maxSize) { - this._heap.push(value); - this._siftUp(); - } else { - const smallest = this._smallest(); - if (this._comparator(value, this._heap[smallest])) { - this._heap[smallest] = value; - this._siftUpFrom(smallest); - } - } - } - return this.size; - } - /** - * Remove and return the element with the highest priority in the queue. - * @returns {any} The element with the highest priority in the queue. - */ - pop() { - const poppedValue = this.peek(); - const bottom = this.size - 1; - if (bottom > 0) { - this._swap(0, bottom); - } - this._heap.pop(); - this._siftDown(); - return poppedValue; - } - /** - * Replace the element with the highest priority in the queue with a new value. - * @param {*} value The new value. - * @returns {*} The replaced value. - */ - replace(value) { - const replacedValue = this.peek(); - this._heap[0] = value; - this._siftDown(); - return replacedValue; - } - /** - * Compute the index for the parent of the node at index `i`. - * @param {number} i The index of the node to get the parent of. - * @returns {number} The index of the parent node. - * @private - */ - _parent(i) { - return (i + 1 >>> 1) - 1; - } - /** - * Compute the index for the left child of the node at index `i`. - * @param {number} i The index of the node to get the left child of. - * @returns {number} The index of the left child. - * @private - */ - _left(i) { - return (i << 1) + 1; - } - /** - * Compute the index for the right child of the node at index `i`. - * @param {number} i The index of the node to get the right child of. - * @returns {number} The index of the right child. - * @private - */ - _right(i) { - return i + 1 << 1; - } - /** - * Check if the element at index `i` is greater than the element at index `j`. - * @param {number} i The index of the first element to compare. - * @param {number} j The index of the second element to compare. - * @returns {boolean} `true` if the element at index `i` is greater than the element at index `j`, `false` otherwise. - * @private - */ - _greater(i, j) { - return this._comparator(this._heap[i], this._heap[j]); - } - /** - * Swap the elements at indices `i` and `j`. - * @param {number} i The index of the first element to swap. - * @param {number} j The index of the second element to swap. - * @private - */ - _swap(i, j) { - const temp = this._heap[i]; - this._heap[i] = this._heap[j]; - this._heap[j] = temp; - } - /** - * Maintain the heap property by updating positions in the heap, - * starting at the last element and moving up the heap. - * @private - */ - _siftUp() { - this._siftUpFrom(this.size - 1); - } - /** - * Helper function to sift up from a given node. - * @param {number} node The index of the node to start sifting up from. - */ - _siftUpFrom(node) { - while (node > 0 && this._greater(node, this._parent(node))) { - this._swap(node, this._parent(node)); - node = this._parent(node); - } - } - /** - * Maintain the heap property by updating positions in the heap, - * starting at the first element and moving down the heap. - * @private - */ - _siftDown() { - let node = 0; - while (this._left(node) < this.size && this._greater(this._left(node), node) || this._right(node) < this.size && this._greater(this._right(node), node)) { - const maxChild = this._right(node) < this.size && this._greater(this._right(node), this._left(node)) ? this._right(node) : this._left(node); - this._swap(node, maxChild); - node = maxChild; - } - } - /** - * Get the index of the smallest element in the heap. Since we use an array-based heap, - * the index can be computed without needing to traverse the heap. - * @private - */ - _smallest() { - return 2 ** Math.floor(Math.log2(this.size)) - 1; - } - } - class CharTrie { - constructor() { - this.root = CharTrieNode.default(); - } - /** - * Adds one or more `texts` to the trie. - * @param {string[]} texts The strings to add to the trie. - */ - extend(texts) { - for (const text of texts) { - this.push(text); - } - } - /** - * Adds text to the trie. - * @param {string} text The string to add to the trie. - */ - push(text) { - let node = this.root; - for (const ch of text) { - let child = node.children.get(ch); - if (child === void 0) { - child = CharTrieNode.default(); - node.children.set(ch, child); - } - node = child; - } - node.isLeaf = true; - } - /** - * Searches the trie for all strings with a common prefix of `text`. - * @param {string} text The common prefix to search for. - * @yields {string} Each string in the trie that has `text` as a prefix. - */ - *commonPrefixSearch(text) { - let node = this.root; - if (node === void 0) return; - let prefix = ""; - for (const ch of text) { - prefix += ch; - node = node.children.get(ch); - if (node === void 0) return; - if (node.isLeaf) { - yield prefix; - } - } - } - } - class CharTrieNode { - /** - * Create a new CharTrieNode. - * @param {boolean} isLeaf Whether the node is a leaf node or not. - * @param {Map} children A map containing the node's children, where the key is a character and the value is a `CharTrieNode`. - */ - constructor(isLeaf, children) { - this.isLeaf = isLeaf; - this.children = children; - } - /** - * Returns a new `CharTrieNode` instance with default values. - * @returns {CharTrieNode} A new `CharTrieNode` instance with `isLeaf` set to `false` and an empty `children` map. - */ - static default() { - return new CharTrieNode(false, /* @__PURE__ */ new Map()); - } - } - class TokenLattice { - /** - * Creates a new TokenLattice instance. - * - * @param {string} sentence The input sentence to be tokenized. - * @param {number} bosTokenId The beginning-of-sequence token ID. - * @param {number} eosTokenId The end-of-sequence token ID. - */ - constructor(sentence, bosTokenId, eosTokenId) { - this.chars = Array.from(sentence); - this.len = this.chars.length; - this.bosTokenId = bosTokenId; - this.eosTokenId = eosTokenId; - this.nodes = []; - this.beginNodes = Array.from({ length: this.len + 1 }, () => []); - this.endNodes = Array.from({ length: this.len + 1 }, () => []); - const bos = new TokenLatticeNode(this.bosTokenId, 0, 0, 0, 0); - const eos = new TokenLatticeNode(this.eosTokenId, 1, this.len, 0, 0); - this.nodes.push(bos.clone()); - this.nodes.push(eos.clone()); - this.beginNodes[this.len].push(eos); - this.endNodes[0].push(bos); - } - /** - * Inserts a new token node into the token lattice. - * - * @param {number} pos The starting position of the token. - * @param {number} length The length of the token. - * @param {number} score The score of the token. - * @param {number} tokenId The token ID of the token. - */ - insert(pos, length, score, tokenId) { - const nodeId = this.nodes.length; - const node = new TokenLatticeNode(tokenId, nodeId, pos, length, score); - this.beginNodes[pos].push(node); - this.endNodes[pos + length].push(node); - this.nodes.push(node); - } - /** - * Implements the Viterbi algorithm to compute the most likely sequence of tokens. - * - * @returns {TokenLatticeNode[]} The most likely sequence of tokens. - */ - viterbi() { - const len = this.len; - let pos = 0; - while (pos <= len) { - if (this.beginNodes[pos].length == 0) { - return []; - } - for (let rnode of this.beginNodes[pos]) { - rnode.prev = null; - let bestScore = 0; - let bestNode = null; - for (let lnode of this.endNodes[pos]) { - const score = lnode.backtraceScore + rnode.score; - if (bestNode === null || score > bestScore) { - bestNode = lnode.clone(); - bestScore = score; - } - } - if (bestNode !== null) { - rnode.prev = bestNode; - rnode.backtraceScore = bestScore; - } else { - return []; - } - } - ++pos; - } - const results = []; - const root = this.beginNodes[len][0]; - const prev = root.prev; - if (prev === null) { - return []; - } - let node = prev.clone(); - while (node.prev !== null) { - results.push(node.clone()); - const n = node.clone(); - node = n.prev.clone(); - } - results.reverse(); - return results; - } - /** - * @param {TokenLatticeNode} node - * @returns {string} The array of nodes representing the most likely sequence of tokens. - */ - piece(node) { - return this.chars.slice(node.pos, node.pos + node.length).join(""); - } - /** - * @returns {string[]} The most likely sequence of tokens. - */ - tokens() { - const nodes = this.viterbi(); - return nodes.map((x) => this.piece(x)); - } - /** - * @returns {number[]} The most likely sequence of token ids. - */ - tokenIds() { - const nodes = this.viterbi(); - return nodes.map((x) => x.tokenId); - } - } - class TokenLatticeNode { - /** - * Represents a node in a token lattice for a given sentence. - * @param {number} tokenId The ID of the token associated with this node. - * @param {number} nodeId The ID of this node. - * @param {number} pos The starting position of the token in the sentence. - * @param {number} length The length of the token. - * @param {number} score The score associated with the token. - */ - constructor(tokenId, nodeId, pos, length, score) { - this.tokenId = tokenId; - this.nodeId = nodeId; - this.pos = pos; - this.length = length; - this.score = score; - this.prev = null; - this.backtraceScore = 0; - } - /** - * Returns a clone of this node. - * @returns {TokenLatticeNode} A clone of this node. - */ - clone() { - const n = new TokenLatticeNode(this.tokenId, this.nodeId, this.pos, this.length, this.score); - n.prev = this.prev; - n.backtraceScore = this.backtraceScore; - return n; - } - } - } - ), - /***/ - "./src/utils/devices.js": ( - /*!******************************!*\ - !*** ./src/utils/devices.js ***! - \******************************/ - /***/ - (__unused_webpack___webpack_module__, __webpack_exports__2, __webpack_require__2) => { - __webpack_require__2.r(__webpack_exports__2); - __webpack_require__2.d(__webpack_exports__2, { - /* harmony export */ - DEVICE_TYPES: () => ( - /* binding */ - DEVICE_TYPES - ) - /* harmony export */ - }); - const DEVICE_TYPES = Object.freeze({ - auto: "auto", - // Auto-detect based on device and environment - gpu: "gpu", - // Auto-detect GPU - cpu: "cpu", - // CPU - wasm: "wasm", - // WebAssembly - webgpu: "webgpu", - // WebGPU - cuda: "cuda", - // CUDA - dml: "dml", - // DirectML - webnn: "webnn", - // WebNN (default) - "webnn-npu": "webnn-npu", - // WebNN NPU - "webnn-gpu": "webnn-gpu", - // WebNN GPU - "webnn-cpu": "webnn-cpu" - // WebNN CPU - }); - } - ), - /***/ - "./src/utils/dtypes.js": ( - /*!*****************************!*\ - !*** ./src/utils/dtypes.js ***! - \*****************************/ - /***/ - (__unused_webpack___webpack_module__, __webpack_exports__2, __webpack_require__2) => { - __webpack_require__2.r(__webpack_exports__2); - __webpack_require__2.d(__webpack_exports__2, { - /* harmony export */ - DATA_TYPES: () => ( - /* binding */ - DATA_TYPES - ), - /* harmony export */ - DEFAULT_DEVICE_DTYPE_MAPPING: () => ( - /* binding */ - DEFAULT_DEVICE_DTYPE_MAPPING - ), - /* harmony export */ - DEFAULT_DTYPE_SUFFIX_MAPPING: () => ( - /* binding */ - DEFAULT_DTYPE_SUFFIX_MAPPING - ), - /* harmony export */ - isWebGpuFp16Supported: () => ( - /* binding */ - isWebGpuFp16Supported - ) - /* harmony export */ - }); - var _env_js__WEBPACK_IMPORTED_MODULE_0__2 = __webpack_require__2( - /*! ../env.js */ - "./src/env.js" - ); - var _devices_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__2( - /*! ./devices.js */ - "./src/utils/devices.js" - ); - const isWebGpuFp16Supported = /* @__PURE__ */ function() { - let cachedResult; - return async function() { - if (cachedResult === void 0) { - if (!_env_js__WEBPACK_IMPORTED_MODULE_0__2.apis.IS_WEBGPU_AVAILABLE) { - cachedResult = false; - } else { - try { - const adapter = await navigator.gpu.requestAdapter(); - cachedResult = adapter.features.has("shader-f16"); - } catch (e) { - cachedResult = false; - } - } - } - return cachedResult; - }; - }(); - const DATA_TYPES = Object.freeze({ - fp32: "fp32", - fp16: "fp16", - q8: "q8", - int8: "int8", - uint8: "uint8", - q4: "q4", - bnb4: "bnb4", - q4f16: "q4f16" - // fp16 model with int4 block weight quantization - }); - const DEFAULT_DEVICE_DTYPE_MAPPING = Object.freeze({ - // NOTE: If not specified, will default to fp32 - [_devices_js__WEBPACK_IMPORTED_MODULE_1__.DEVICE_TYPES.wasm]: DATA_TYPES.q8 - }); - const DEFAULT_DTYPE_SUFFIX_MAPPING = Object.freeze({ - [DATA_TYPES.fp32]: "", - [DATA_TYPES.fp16]: "_fp16", - [DATA_TYPES.int8]: "_int8", - [DATA_TYPES.uint8]: "_uint8", - [DATA_TYPES.q8]: "_quantized", - [DATA_TYPES.q4]: "_q4", - [DATA_TYPES.q4f16]: "_q4f16", - [DATA_TYPES.bnb4]: "_bnb4" - }); - } - ), - /***/ - "./src/utils/generic.js": ( - /*!******************************!*\ - !*** ./src/utils/generic.js ***! - \******************************/ - /***/ - (__unused_webpack___webpack_module__, __webpack_exports__2, __webpack_require__2) => { - __webpack_require__2.r(__webpack_exports__2); - __webpack_require__2.d(__webpack_exports__2, { - /* harmony export */ - Callable: () => ( - /* binding */ - Callable - ) - /* harmony export */ - }); - const Callable = ( - /** @type {any} */ - class { - /** - * Creates a new instance of the Callable class. - */ - constructor() { - let closure = function(...args) { - return closure._call(...args); - }; - return Object.setPrototypeOf(closure, new.target.prototype); - } - /** - * This method should be implemented in subclasses to provide the - * functionality of the callable object. - * - * @param {any[]} args - * @throws {Error} If the subclass does not implement the `_call` method. - */ - _call(...args) { - throw Error("Must implement _call method in subclass"); - } - } - ); - } - ), - /***/ - "./src/utils/hub.js": ( - /*!**************************!*\ - !*** ./src/utils/hub.js ***! - \**************************/ - /***/ - (__unused_webpack___webpack_module__, __webpack_exports__2, __webpack_require__2) => { - __webpack_require__2.r(__webpack_exports__2); - __webpack_require__2.d(__webpack_exports__2, { - /* harmony export */ - getFile: () => ( - /* binding */ - getFile - ), - /* harmony export */ - getModelFile: () => ( - /* binding */ - getModelFile - ), - /* harmony export */ - getModelJSON: () => ( - /* binding */ - getModelJSON - ) - /* harmony export */ - }); - var fs__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__2( - /*! fs */ - "?7a2c" - ); - var path__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__2( - /*! path */ - "?a42a" - ); - var _env_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__2( - /*! ../env.js */ - "./src/env.js" - ); - var _core_js__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__2( - /*! ./core.js */ - "./src/utils/core.js" - ); - const CONTENT_TYPE_MAP = { - "txt": "text/plain", - "html": "text/html", - "css": "text/css", - "js": "text/javascript", - "json": "application/json", - "png": "image/png", - "jpg": "image/jpeg", - "jpeg": "image/jpeg", - "gif": "image/gif" - }; - class FileResponse { - /** - * Creates a new `FileResponse` object. - * @param {string|URL} filePath - */ - constructor(filePath) { - this.filePath = filePath; - this.headers = new Headers(); - this.exists = fs__WEBPACK_IMPORTED_MODULE_0__.existsSync(filePath); - if (this.exists) { - this.status = 200; - this.statusText = "OK"; - let stats = fs__WEBPACK_IMPORTED_MODULE_0__.statSync(filePath); - this.headers.set("content-length", stats.size.toString()); - this.updateContentType(); - let self2 = this; - this.body = new ReadableStream({ - start(controller) { - self2.arrayBuffer().then((buffer) => { - controller.enqueue(new Uint8Array(buffer)); - controller.close(); - }); - } - }); - } else { - this.status = 404; - this.statusText = "Not Found"; - this.body = null; - } - } - /** - * Updates the 'content-type' header property of the response based on the extension of - * the file specified by the filePath property of the current object. - * @returns {void} - */ - updateContentType() { - const extension = this.filePath.toString().split(".").pop().toLowerCase(); - this.headers.set("content-type", CONTENT_TYPE_MAP[extension] ?? "application/octet-stream"); - } - /** - * Clone the current FileResponse object. - * @returns {FileResponse} A new FileResponse object with the same properties as the current object. - */ - clone() { - let response = new FileResponse(this.filePath); - response.exists = this.exists; - response.status = this.status; - response.statusText = this.statusText; - response.headers = new Headers(this.headers); - return response; - } - /** - * Reads the contents of the file specified by the filePath property and returns a Promise that - * resolves with an ArrayBuffer containing the file's contents. - * @returns {Promise} A Promise that resolves with an ArrayBuffer containing the file's contents. - * @throws {Error} If the file cannot be read. - */ - async arrayBuffer() { - const data = await fs__WEBPACK_IMPORTED_MODULE_0__.promises.readFile(this.filePath); - return data.buffer; - } - /** - * Reads the contents of the file specified by the filePath property and returns a Promise that - * resolves with a Blob containing the file's contents. - * @returns {Promise} A Promise that resolves with a Blob containing the file's contents. - * @throws {Error} If the file cannot be read. - */ - async blob() { - const data = await fs__WEBPACK_IMPORTED_MODULE_0__.promises.readFile(this.filePath); - return new Blob([data], { type: this.headers.get("content-type") }); - } - /** - * Reads the contents of the file specified by the filePath property and returns a Promise that - * resolves with a string containing the file's contents. - * @returns {Promise} A Promise that resolves with a string containing the file's contents. - * @throws {Error} If the file cannot be read. - */ - async text() { - const data = await fs__WEBPACK_IMPORTED_MODULE_0__.promises.readFile(this.filePath, "utf8"); - return data; - } - /** - * Reads the contents of the file specified by the filePath property and returns a Promise that - * resolves with a parsed JavaScript object containing the file's contents. - * - * @returns {Promise} A Promise that resolves with a parsed JavaScript object containing the file's contents. - * @throws {Error} If the file cannot be read. - */ - async json() { - return JSON.parse(await this.text()); - } - } - function isValidUrl(string, protocols = null, validHosts = null) { - let url; - try { - url = new URL(string); - } catch (_) { - return false; - } - if (protocols && !protocols.includes(url.protocol)) { - return false; - } - if (validHosts && !validHosts.includes(url.hostname)) { - return false; - } - return true; - } - async function getFile(urlOrPath) { - var _a, _b, _c, _d; - if (_env_js__WEBPACK_IMPORTED_MODULE_2__.env.useFS && !isValidUrl(urlOrPath, ["http:", "https:", "blob:"])) { - return new FileResponse(urlOrPath); - } else if (typeof process !== "undefined" && ((_a = process == null ? void 0 : process.release) == null ? void 0 : _a.name) === "node") { - const IS_CI = !!((_b = process.env) == null ? void 0 : _b.TESTING_REMOTELY); - const version = _env_js__WEBPACK_IMPORTED_MODULE_2__.env.version; - const headers = new Headers(); - headers.set("User-Agent", `transformers.js/${version}; is_ci/${IS_CI};`); - const isHFURL = isValidUrl(urlOrPath, ["http:", "https:"], ["huggingface.co", "hf.co"]); - if (isHFURL) { - const token = ((_c = process.env) == null ? void 0 : _c.HF_TOKEN) ?? ((_d = process.env) == null ? void 0 : _d.HF_ACCESS_TOKEN); - if (token) { - headers.set("Authorization", `Bearer ${token}`); - } - } - return fetch(urlOrPath, { headers }); - } else { - return fetch(urlOrPath); - } - } - const ERROR_MAPPING = { - // 4xx errors (https://developer.mozilla.org/en-US/docs/Web/HTTP/Status#client_error_responses) - 400: "Bad request error occurred while trying to load file", - 401: "Unauthorized access to file", - 403: "Forbidden access to file", - 404: "Could not locate file", - 408: "Request timeout error occurred while trying to load file", - // 5xx errors (https://developer.mozilla.org/en-US/docs/Web/HTTP/Status#server_error_responses) - 500: "Internal server error error occurred while trying to load file", - 502: "Bad gateway error occurred while trying to load file", - 503: "Service unavailable error occurred while trying to load file", - 504: "Gateway timeout error occurred while trying to load file" - }; - function handleError(status, remoteURL, fatal) { - if (!fatal) { - return null; - } - const message = ERROR_MAPPING[status] ?? `Error (${status}) occurred while trying to load file`; - throw Error(`${message}: "${remoteURL}".`); - } - class FileCache { - /** - * Instantiate a `FileCache` object. - * @param {string} path - */ - constructor(path) { - this.path = path; - } - /** - * Checks whether the given request is in the cache. - * @param {string} request - * @returns {Promise} - */ - async match(request) { - let filePath = path__WEBPACK_IMPORTED_MODULE_1__.join(this.path, request); - let file = new FileResponse(filePath); - if (file.exists) { - return file; - } else { - return void 0; - } - } - /** - * Adds the given response to the cache. - * @param {string} request - * @param {Response|FileResponse} response - * @returns {Promise} - */ - async put(request, response) { - const buffer = Buffer.from(await response.arrayBuffer()); - let outputPath = path__WEBPACK_IMPORTED_MODULE_1__.join(this.path, request); - try { - await fs__WEBPACK_IMPORTED_MODULE_0__.promises.mkdir(path__WEBPACK_IMPORTED_MODULE_1__.dirname(outputPath), { recursive: true }); - await fs__WEBPACK_IMPORTED_MODULE_0__.promises.writeFile(outputPath, buffer); - } catch (err) { - console.warn("An error occurred while writing the file to cache:", err); - } - } - // TODO add the rest? - // addAll(requests: RequestInfo[]): Promise; - // delete(request: RequestInfo | URL, options?: CacheQueryOptions): Promise; - // keys(request?: RequestInfo | URL, options?: CacheQueryOptions): Promise>; - // match(request: RequestInfo | URL, options?: CacheQueryOptions): Promise; - // matchAll(request?: RequestInfo | URL, options?: CacheQueryOptions): Promise>; - } - async function tryCache(cache, ...names) { - for (let name of names) { - try { - let result = await cache.match(name); - if (result) return result; - } catch (e) { - continue; - } - } - return void 0; - } - async function getModelFile(path_or_repo_id, filename, fatal = true, options = {}) { - if (!_env_js__WEBPACK_IMPORTED_MODULE_2__.env.allowLocalModels) { - if (options.local_files_only) { - throw Error("Invalid configuration detected: local models are disabled (`env.allowLocalModels=false`) but you have requested to only use local models (`local_files_only=true`)."); - } else if (!_env_js__WEBPACK_IMPORTED_MODULE_2__.env.allowRemoteModels) { - throw Error("Invalid configuration detected: both local and remote models are disabled. Fix by setting `env.allowLocalModels` or `env.allowRemoteModels` to `true`."); - } - } - (0, _core_js__WEBPACK_IMPORTED_MODULE_3__.dispatchCallback)(options.progress_callback, { - status: "initiate", - name: path_or_repo_id, - file: filename - }); - let cache; - if (!cache && _env_js__WEBPACK_IMPORTED_MODULE_2__.env.useBrowserCache) { - if (typeof caches === "undefined") { - throw Error("Browser cache is not available in this environment."); - } - try { - cache = await caches.open("transformers-cache"); - } catch (e) { - console.warn("An error occurred while opening the browser cache:", e); - } - } - if (!cache && _env_js__WEBPACK_IMPORTED_MODULE_2__.env.useFSCache) { - cache = new FileCache(options.cache_dir ?? _env_js__WEBPACK_IMPORTED_MODULE_2__.env.cacheDir); - } - if (!cache && _env_js__WEBPACK_IMPORTED_MODULE_2__.env.useCustomCache) { - if (!_env_js__WEBPACK_IMPORTED_MODULE_2__.env.customCache) { - throw Error("`env.useCustomCache=true`, but `env.customCache` is not defined."); - } - if (!_env_js__WEBPACK_IMPORTED_MODULE_2__.env.customCache.match || !_env_js__WEBPACK_IMPORTED_MODULE_2__.env.customCache.put) { - throw new Error( - "`env.customCache` must be an object which implements the `match` and `put` functions of the Web Cache API. For more information, see https://developer.mozilla.org/en-US/docs/Web/API/Cache" - ); - } - cache = _env_js__WEBPACK_IMPORTED_MODULE_2__.env.customCache; - } - const revision = options.revision ?? "main"; - let requestURL = pathJoin(path_or_repo_id, filename); - let localPath = pathJoin(_env_js__WEBPACK_IMPORTED_MODULE_2__.env.localModelPath, requestURL); - let remoteURL = pathJoin( - _env_js__WEBPACK_IMPORTED_MODULE_2__.env.remoteHost, - _env_js__WEBPACK_IMPORTED_MODULE_2__.env.remotePathTemplate.replaceAll("{model}", path_or_repo_id).replaceAll("{revision}", encodeURIComponent(revision)), - filename - ); - let fsCacheKey = revision === "main" ? requestURL : pathJoin(path_or_repo_id, revision, filename); - let cacheKey; - let proposedCacheKey = cache instanceof FileCache ? fsCacheKey : remoteURL; - let toCacheResponse = false; - let response; - if (cache) { - response = await tryCache(cache, localPath, proposedCacheKey); - } - const cacheHit = response !== void 0; - if (response === void 0) { - if (_env_js__WEBPACK_IMPORTED_MODULE_2__.env.allowLocalModels) { - const isURL = isValidUrl(requestURL, ["http:", "https:"]); - if (!isURL) { - try { - response = await getFile(localPath); - cacheKey = localPath; - } catch (e) { - console.warn(`Unable to load from local path "${localPath}": "${e}"`); - } - } else if (options.local_files_only) { - throw new Error(`\`local_files_only=true\`, but attempted to load a remote file from: ${requestURL}.`); - } else if (!_env_js__WEBPACK_IMPORTED_MODULE_2__.env.allowRemoteModels) { - throw new Error(`\`env.allowRemoteModels=false\`, but attempted to load a remote file from: ${requestURL}.`); - } - } - if (response === void 0 || response.status === 404) { - if (options.local_files_only || !_env_js__WEBPACK_IMPORTED_MODULE_2__.env.allowRemoteModels) { - if (fatal) { - throw Error(`\`local_files_only=true\` or \`env.allowRemoteModels=false\` and file was not found locally at "${localPath}".`); - } else { - return null; - } - } - response = await getFile(remoteURL); - if (response.status !== 200) { - return handleError(response.status, remoteURL, fatal); - } - cacheKey = proposedCacheKey; - } - toCacheResponse = cache && typeof Response !== "undefined" && response instanceof Response && response.status === 200; - } - (0, _core_js__WEBPACK_IMPORTED_MODULE_3__.dispatchCallback)(options.progress_callback, { - status: "download", - name: path_or_repo_id, - file: filename - }); - const progressInfo = { - status: "progress", - name: path_or_repo_id, - file: filename - }; - let buffer; - if (!options.progress_callback) { - buffer = new Uint8Array(await response.arrayBuffer()); - } else if (cacheHit && typeof navigator !== "undefined" && /firefox/i.test(navigator.userAgent)) { - buffer = new Uint8Array(await response.arrayBuffer()); - (0, _core_js__WEBPACK_IMPORTED_MODULE_3__.dispatchCallback)(options.progress_callback, { - ...progressInfo, - progress: 100, - loaded: buffer.length, - total: buffer.length - }); - } else { - buffer = await readResponse(response, (data) => { - (0, _core_js__WEBPACK_IMPORTED_MODULE_3__.dispatchCallback)(options.progress_callback, { - ...progressInfo, - ...data - }); - }); - } - if ( - // Only cache web responses - // i.e., do not cache FileResponses (prevents duplication) - toCacheResponse && cacheKey && // Check again whether request is in cache. If not, we add the response to the cache - await cache.match(cacheKey) === void 0 - ) { - await cache.put(cacheKey, new Response(buffer, { - headers: response.headers - })).catch((err) => { - console.warn(`Unable to add response to browser cache: ${err}.`); - }); - } - (0, _core_js__WEBPACK_IMPORTED_MODULE_3__.dispatchCallback)(options.progress_callback, { - status: "done", - name: path_or_repo_id, - file: filename - }); - return buffer; - } - async function getModelJSON(modelPath, fileName, fatal = true, options = {}) { - let buffer = await getModelFile(modelPath, fileName, fatal, options); - if (buffer === null) { - return {}; - } - let decoder = new TextDecoder("utf-8"); - let jsonData = decoder.decode(buffer); - return JSON.parse(jsonData); - } - async function readResponse(response, progress_callback) { - const contentLength = response.headers.get("Content-Length"); - if (contentLength === null) { - console.warn("Unable to determine content-length from response headers. Will expand buffer when needed."); - } - const arrayBuffer = await response.arrayBuffer(); - const buffer = new Uint8Array(arrayBuffer); - const loaded = buffer.length; - const total = buffer.length; - const progress = loaded / total * 100; - - progress_callback({ - progress, - loaded, - total - }); - - return buffer; - } - function pathJoin(...parts) { - parts = parts.map((part, index) => { - if (index) { - part = part.replace(new RegExp("^/"), ""); - } - if (index !== parts.length - 1) { - part = part.replace(new RegExp("/$"), ""); - } - return part; - }); - return parts.join("/"); - } - } - ), - /***/ - "./src/utils/image.js": ( - /*!****************************!*\ - !*** ./src/utils/image.js ***! - \****************************/ - /***/ - (__unused_webpack___webpack_module__, __webpack_exports__2, __webpack_require__2) => { - __webpack_require__2.r(__webpack_exports__2); - __webpack_require__2.d(__webpack_exports__2, { - /* harmony export */ - RawImage: () => ( - /* binding */ - RawImage - ) - /* harmony export */ - }); - var _hub_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__2( - /*! ./hub.js */ - "./src/utils/hub.js" - ); - var _env_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__2( - /*! ../env.js */ - "./src/env.js" - ); - var _tensor_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__2( - /*! ./tensor.js */ - "./src/utils/tensor.js" - ); - var sharp__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__2( - /*! sharp */ - "?2b25" - ); - const BROWSER_ENV = typeof self !== "undefined"; - const WEBWORKER_ENV = BROWSER_ENV && self.constructor.name === "DedicatedWorkerGlobalScope"; - let createCanvasFunction; - let ImageDataClass; - let loadImageFunction; - if (BROWSER_ENV) { - createCanvasFunction = (width, height) => { - if (!self.OffscreenCanvas) { - throw new Error("OffscreenCanvas not supported by this browser."); - } - return new self.OffscreenCanvas(width, height); - }; - loadImageFunction = self.createImageBitmap; - ImageDataClass = self.ImageData; - } else if (sharp__WEBPACK_IMPORTED_MODULE_3__) { - loadImageFunction = async (img) => { - const metadata = await img.metadata(); - const rawChannels = metadata.channels; - const { data, info } = await img.rotate().raw().toBuffer({ resolveWithObject: true }); - const newImage = new RawImage(new Uint8ClampedArray(data), info.width, info.height, info.channels); - if (rawChannels !== void 0 && rawChannels !== info.channels) { - newImage.convert(rawChannels); - } - return newImage; - }; - } else { - throw new Error("Unable to load image processing library."); - } - const RESAMPLING_MAPPING = { - 0: "nearest", - 1: "lanczos", - 2: "bilinear", - 3: "bicubic", - 4: "box", - 5: "hamming" - }; - const CONTENT_TYPE_MAP = /* @__PURE__ */ new Map([ - ["png", "image/png"], - ["jpg", "image/jpeg"], - ["jpeg", "image/jpeg"], - ["gif", "image/gif"] - ]); - class RawImage { - /** - * Create a new `RawImage` object. - * @param {Uint8ClampedArray|Uint8Array} data The pixel data. - * @param {number} width The width of the image. - * @param {number} height The height of the image. - * @param {1|2|3|4} channels The number of channels. - */ - constructor(data, width, height, channels) { - this.data = data; - this.width = width; - this.height = height; - this.channels = channels; - } - /** - * Returns the size of the image (width, height). - * @returns {[number, number]} The size of the image (width, height). - */ - get size() { - return [this.width, this.height]; - } - /** - * Helper method for reading an image from a variety of input types. - * @param {RawImage|string|URL} input - * @returns The image object. - * - * **Example:** Read image from a URL. - * ```javascript - * let image = await RawImage.read('https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/football-match.jpg'); - * // RawImage { - * // "data": Uint8ClampedArray [ 25, 25, 25, 19, 19, 19, ... ], - * // "width": 800, - * // "height": 533, - * // "channels": 3 - * // } - * ``` - */ - static async read(input) { - if (input instanceof RawImage) { - return input; - } else if (typeof input === "string" || input instanceof URL) { - return await this.fromURL(input); - } else { - throw new Error(`Unsupported input type: ${typeof input}`); - } - } - /** - * Read an image from a canvas. - * @param {HTMLCanvasElement|OffscreenCanvas} canvas The canvas to read the image from. - * @returns {RawImage} The image object. - */ - static fromCanvas(canvas) { - if (!BROWSER_ENV) { - throw new Error("fromCanvas() is only supported in browser environments."); - } - const ctx = canvas.getContext("2d"); - const data = ctx.getImageData(0, 0, canvas.width, canvas.height).data; - return new RawImage(data, canvas.width, canvas.height, 4); - } - /** - * Read an image from a URL or file path. - * @param {string|URL} url The URL or file path to read the image from. - * @returns {Promise} The image object. - */ - static async fromURL(url) { - const response = await (0, _hub_js__WEBPACK_IMPORTED_MODULE_0__.getFile)(url); - if (response.status !== 200) { - throw new Error(`Unable to read image from "${url}" (${response.status} ${response.statusText})`); - } - const blob = await response.blob(); - return this.fromBlob(blob); - } - /** - * Helper method to create a new Image from a blob. - * @param {Blob} blob The blob to read the image from. - * @returns {Promise} The image object. - */ - static async fromBlob(blob) { - if (BROWSER_ENV) { - const img = await loadImageFunction(blob); - const ctx = createCanvasFunction(img.width, img.height).getContext("2d"); - ctx.drawImage(img, 0, 0); - return new this(ctx.getImageData(0, 0, img.width, img.height).data, img.width, img.height, 4); - } else { - const img = sharp__WEBPACK_IMPORTED_MODULE_3__(await blob.arrayBuffer()); - return await loadImageFunction(img); - } - } - /** - * Helper method to create a new Image from a tensor - * @param {Tensor} tensor - */ - static fromTensor(tensor, channel_format = "CHW") { - if (tensor.dims.length !== 3) { - throw new Error(`Tensor should have 3 dimensions, but has ${tensor.dims.length} dimensions.`); - } - if (channel_format === "CHW") { - tensor = tensor.transpose(1, 2, 0); - } else if (channel_format === "HWC") { - } else { - throw new Error(`Unsupported channel format: ${channel_format}`); - } - if (!(tensor.data instanceof Uint8ClampedArray || tensor.data instanceof Uint8Array)) { - throw new Error(`Unsupported tensor type: ${tensor.type}`); - } - switch (tensor.dims[2]) { - case 1: - case 2: - case 3: - case 4: - return new RawImage(tensor.data, tensor.dims[1], tensor.dims[0], tensor.dims[2]); - default: - throw new Error(`Unsupported number of channels: ${tensor.dims[2]}`); - } - } - /** - * Convert the image to grayscale format. - * @returns {RawImage} `this` to support chaining. - */ - grayscale() { - if (this.channels === 1) { - return this; - } - const newData = new Uint8ClampedArray(this.width * this.height * 1); - switch (this.channels) { - case 3: - case 4: - for (let i = 0, offset = 0; i < this.data.length; i += this.channels) { - const red = this.data[i]; - const green = this.data[i + 1]; - const blue = this.data[i + 2]; - newData[offset++] = Math.round(0.2989 * red + 0.587 * green + 0.114 * blue); - } - break; - default: - throw new Error(`Conversion failed due to unsupported number of channels: ${this.channels}`); - } - return this._update(newData, this.width, this.height, 1); - } - /** - * Convert the image to RGB format. - * @returns {RawImage} `this` to support chaining. - */ - rgb() { - if (this.channels === 3) { - return this; - } - const newData = new Uint8ClampedArray(this.width * this.height * 3); - switch (this.channels) { - case 1: - for (let i = 0, offset = 0; i < this.data.length; ++i) { - newData[offset++] = this.data[i]; - newData[offset++] = this.data[i]; - newData[offset++] = this.data[i]; - } - break; - case 4: - for (let i = 0, offset = 0; i < this.data.length; i += 4) { - newData[offset++] = this.data[i]; - newData[offset++] = this.data[i + 1]; - newData[offset++] = this.data[i + 2]; - } - break; - default: - throw new Error(`Conversion failed due to unsupported number of channels: ${this.channels}`); - } - return this._update(newData, this.width, this.height, 3); - } - /** - * Convert the image to RGBA format. - * @returns {RawImage} `this` to support chaining. - */ - rgba() { - if (this.channels === 4) { - return this; - } - const newData = new Uint8ClampedArray(this.width * this.height * 4); - switch (this.channels) { - case 1: - for (let i = 0, offset = 0; i < this.data.length; ++i) { - newData[offset++] = this.data[i]; - newData[offset++] = this.data[i]; - newData[offset++] = this.data[i]; - newData[offset++] = 255; - } - break; - case 3: - for (let i = 0, offset = 0; i < this.data.length; i += 3) { - newData[offset++] = this.data[i]; - newData[offset++] = this.data[i + 1]; - newData[offset++] = this.data[i + 2]; - newData[offset++] = 255; - } - break; - default: - throw new Error(`Conversion failed due to unsupported number of channels: ${this.channels}`); - } - return this._update(newData, this.width, this.height, 4); - } - /** - * Resize the image to the given dimensions. This method uses the canvas API to perform the resizing. - * @param {number} width The width of the new image. - * @param {number} height The height of the new image. - * @param {Object} options Additional options for resizing. - * @param {0|1|2|3|4|5|string} [options.resample] The resampling method to use. - * @returns {Promise} `this` to support chaining. - */ - async resize(width, height, { - resample = 2 - } = {}) { - let resampleMethod = RESAMPLING_MAPPING[resample] ?? resample; - if (BROWSER_ENV) { - const numChannels = this.channels; - const canvas = this.toCanvas(); - const ctx = createCanvasFunction(width, height).getContext("2d"); - ctx.drawImage(canvas, 0, 0, width, height); - const resizedImage = new RawImage(ctx.getImageData(0, 0, width, height).data, width, height, 4); - return resizedImage.convert(numChannels); - } else { - let img = this.toSharp(); - switch (resampleMethod) { - case "box": - case "hamming": - if (resampleMethod === "box" || resampleMethod === "hamming") { - console.warn(`Resampling method ${resampleMethod} is not yet supported. Using bilinear instead.`); - resampleMethod = "bilinear"; - } - case "nearest": - case "bilinear": - case "bicubic": - img = img.affine([width / this.width, 0, 0, height / this.height], { - interpolator: resampleMethod - }); - break; - case "lanczos": - img = img.resize({ - width, - height, - fit: "fill", - kernel: "lanczos3" - // PIL Lanczos uses a kernel size of 3 - }); - break; - default: - throw new Error(`Resampling method ${resampleMethod} is not supported.`); - } - return await loadImageFunction(img); - } - } - async pad([left, right, top, bottom]) { - left = Math.max(left, 0); - right = Math.max(right, 0); - top = Math.max(top, 0); - bottom = Math.max(bottom, 0); - if (left === 0 && right === 0 && top === 0 && bottom === 0) { - return this; - } - if (BROWSER_ENV) { - const numChannels = this.channels; - const canvas = this.toCanvas(); - const newWidth = this.width + left + right; - const newHeight = this.height + top + bottom; - const ctx = createCanvasFunction(newWidth, newHeight).getContext("2d"); - ctx.drawImage( - canvas, - 0, - 0, - this.width, - this.height, - left, - top, - newWidth, - newHeight - ); - const paddedImage = new RawImage( - ctx.getImageData(0, 0, newWidth, newHeight).data, - newWidth, - newHeight, - 4 - ); - return paddedImage.convert(numChannels); - } else { - const img = this.toSharp().extend({ left, right, top, bottom }); - return await loadImageFunction(img); - } - } - async crop([x_min, y_min, x_max, y_max]) { - x_min = Math.max(x_min, 0); - y_min = Math.max(y_min, 0); - x_max = Math.min(x_max, this.width - 1); - y_max = Math.min(y_max, this.height - 1); - if (x_min === 0 && y_min === 0 && x_max === this.width - 1 && y_max === this.height - 1) { - return this; - } - const crop_width = x_max - x_min + 1; - const crop_height = y_max - y_min + 1; - if (BROWSER_ENV) { - const numChannels = this.channels; - const canvas = this.toCanvas(); - const ctx = createCanvasFunction(crop_width, crop_height).getContext("2d"); - ctx.drawImage( - canvas, - x_min, - y_min, - crop_width, - crop_height, - 0, - 0, - crop_width, - crop_height - ); - const resizedImage = new RawImage(ctx.getImageData(0, 0, crop_width, crop_height).data, crop_width, crop_height, 4); - return resizedImage.convert(numChannels); - } else { - const img = this.toSharp().extract({ - left: x_min, - top: y_min, - width: crop_width, - height: crop_height - }); - return await loadImageFunction(img); - } - } - async center_crop(crop_width, crop_height) { - if (this.width === crop_width && this.height === crop_height) { - return this; - } - const width_offset = (this.width - crop_width) / 2; - const height_offset = (this.height - crop_height) / 2; - if (BROWSER_ENV) { - const numChannels = this.channels; - const canvas = this.toCanvas(); - const ctx = createCanvasFunction(crop_width, crop_height).getContext("2d"); - let sourceX = 0; - let sourceY = 0; - let destX = 0; - let destY = 0; - if (width_offset >= 0) { - sourceX = width_offset; - } else { - destX = -width_offset; - } - if (height_offset >= 0) { - sourceY = height_offset; - } else { - destY = -height_offset; - } - ctx.drawImage( - canvas, - sourceX, - sourceY, - crop_width, - crop_height, - destX, - destY, - crop_width, - crop_height - ); - const resizedImage = new RawImage(ctx.getImageData(0, 0, crop_width, crop_height).data, crop_width, crop_height, 4); - return resizedImage.convert(numChannels); - } else { - let img = this.toSharp(); - if (width_offset >= 0 && height_offset >= 0) { - img = img.extract({ - left: Math.floor(width_offset), - top: Math.floor(height_offset), - width: crop_width, - height: crop_height - }); - } else if (width_offset <= 0 && height_offset <= 0) { - const top = Math.floor(-height_offset); - const left = Math.floor(-width_offset); - img = img.extend({ - top, - left, - // Ensures the resulting image has the desired dimensions - right: crop_width - this.width - left, - bottom: crop_height - this.height - top - }); - } else { - let y_padding = [0, 0]; - let y_extract = 0; - if (height_offset < 0) { - y_padding[0] = Math.floor(-height_offset); - y_padding[1] = crop_height - this.height - y_padding[0]; - } else { - y_extract = Math.floor(height_offset); - } - let x_padding = [0, 0]; - let x_extract = 0; - if (width_offset < 0) { - x_padding[0] = Math.floor(-width_offset); - x_padding[1] = crop_width - this.width - x_padding[0]; - } else { - x_extract = Math.floor(width_offset); - } - img = img.extend({ - top: y_padding[0], - bottom: y_padding[1], - left: x_padding[0], - right: x_padding[1] - }).extract({ - left: x_extract, - top: y_extract, - width: crop_width, - height: crop_height - }); - } - return await loadImageFunction(img); - } - } - async toBlob(type = "image/png", quality = 1) { - if (!BROWSER_ENV) { - throw new Error("toBlob() is only supported in browser environments."); - } - const canvas = this.toCanvas(); - return await canvas.convertToBlob({ type, quality }); - } - toTensor(channel_format = "CHW") { - let tensor = new _tensor_js__WEBPACK_IMPORTED_MODULE_2__.Tensor( - "uint8", - new Uint8Array(this.data), - [this.height, this.width, this.channels] - ); - if (channel_format === "HWC") { - } else if (channel_format === "CHW") { - tensor = tensor.permute(2, 0, 1); - } else { - throw new Error(`Unsupported channel format: ${channel_format}`); - } - return tensor; - } - toCanvas() { - if (!BROWSER_ENV) { - throw new Error("toCanvas() is only supported in browser environments."); - } - const cloned = this.clone().rgba(); - const clonedCanvas = createCanvasFunction(cloned.width, cloned.height); - const data = new ImageDataClass(cloned.data, cloned.width, cloned.height); - clonedCanvas.getContext("2d").putImageData(data, 0, 0); - return clonedCanvas; - } - /** - * Helper method to update the image data. - * @param {Uint8ClampedArray} data The new image data. - * @param {number} width The new width of the image. - * @param {number} height The new height of the image. - * @param {1|2|3|4|null} [channels] The new number of channels of the image. - * @private - */ - _update(data, width, height, channels = null) { - this.data = data; - this.width = width; - this.height = height; - if (channels !== null) { - this.channels = channels; - } - return this; - } - /** - * Clone the image - * @returns {RawImage} The cloned image - */ - clone() { - return new RawImage(this.data.slice(), this.width, this.height, this.channels); - } - /** - * Helper method for converting image to have a certain number of channels - * @param {number} numChannels The number of channels. Must be 1, 3, or 4. - * @returns {RawImage} `this` to support chaining. - */ - convert(numChannels) { - if (this.channels === numChannels) return this; - switch (numChannels) { - case 1: - this.grayscale(); - break; - case 3: - this.rgb(); - break; - case 4: - this.rgba(); - break; - default: - throw new Error(`Conversion failed due to unsupported number of channels: ${this.channels}`); - } - return this; - } - /** - * Save the image to the given path. - * @param {string} path The path to save the image to. - */ - async save(path) { - if (BROWSER_ENV) { - if (WEBWORKER_ENV) { - throw new Error("Unable to save an image from a Web Worker."); - } - const extension = path.split(".").pop().toLowerCase(); - const mime = CONTENT_TYPE_MAP.get(extension) ?? "image/png"; - const blob = await this.toBlob(mime); - const dataURL = URL.createObjectURL(blob); - const downloadLink = document.createElement("a"); - downloadLink.href = dataURL; - downloadLink.download = path; - downloadLink.click(); - downloadLink.remove(); - } else if (!_env_js__WEBPACK_IMPORTED_MODULE_1__.env.useFS) { - throw new Error("Unable to save the image because filesystem is disabled in this environment."); - } else { - const img = this.toSharp(); - return await img.toFile(path); - } - } - toSharp() { - if (BROWSER_ENV) { - throw new Error("toSharp() is only supported in server-side environments."); - } - return sharp__WEBPACK_IMPORTED_MODULE_3__(this.data, { - raw: { - width: this.width, - height: this.height, - channels: this.channels - } - }); - } - } - } - ), - /***/ - "./src/utils/maths.js": ( - /*!****************************!*\ - !*** ./src/utils/maths.js ***! - \****************************/ - /***/ - (__unused_webpack___webpack_module__, __webpack_exports__2, __webpack_require__2) => { - __webpack_require__2.r(__webpack_exports__2); - __webpack_require__2.d(__webpack_exports__2, { - /* harmony export */ - FFT: () => ( - /* binding */ - FFT - ), - /* harmony export */ - bankers_round: () => ( - /* binding */ - bankers_round - ), - /* harmony export */ - cos_sim: () => ( - /* binding */ - cos_sim - ), - /* harmony export */ - dot: () => ( - /* binding */ - dot - ), - /* harmony export */ - dynamic_time_warping: () => ( - /* binding */ - dynamic_time_warping - ), - /* harmony export */ - interpolate_data: () => ( - /* binding */ - interpolate_data - ), - /* harmony export */ - log_softmax: () => ( - /* binding */ - log_softmax - ), - /* harmony export */ - magnitude: () => ( - /* binding */ - magnitude - ), - /* harmony export */ - max: () => ( - /* binding */ - max - ), - /* harmony export */ - medianFilter: () => ( - /* binding */ - medianFilter - ), - /* harmony export */ - min: () => ( - /* binding */ - min - ), - /* harmony export */ - permute_data: () => ( - /* binding */ - permute_data - ), - /* harmony export */ - round: () => ( - /* binding */ - round - ), - /* harmony export */ - softmax: () => ( - /* binding */ - softmax - ) - /* harmony export */ - }); - function interpolate_data(input, [in_channels, in_height, in_width], [out_height, out_width], mode = "bilinear", align_corners = false) { - const x_scale = out_width / in_width; - const y_scale = out_height / in_height; - const out_img = new input.constructor(out_height * out_width * in_channels); - const inStride = in_height * in_width; - const outStride = out_height * out_width; - for (let i = 0; i < out_height; ++i) { - for (let j = 0; j < out_width; ++j) { - const outOffset = i * out_width + j; - const x = (j + 0.5) / x_scale - 0.5; - const y = (i + 0.5) / y_scale - 0.5; - let x1 = Math.floor(x); - let y1 = Math.floor(y); - const x2 = Math.min(x1 + 1, in_width - 1); - const y2 = Math.min(y1 + 1, in_height - 1); - x1 = Math.max(x1, 0); - y1 = Math.max(y1, 0); - const s = x - x1; - const t = y - y1; - const w1 = (1 - s) * (1 - t); - const w2 = s * (1 - t); - const w3 = (1 - s) * t; - const w4 = s * t; - const yStride = y1 * in_width; - const xStride = y2 * in_width; - const idx1 = yStride + x1; - const idx2 = yStride + x2; - const idx3 = xStride + x1; - const idx4 = xStride + x2; - for (let k = 0; k < in_channels; ++k) { - const cOffset = k * inStride; - out_img[k * outStride + outOffset] = w1 * input[cOffset + idx1] + w2 * input[cOffset + idx2] + w3 * input[cOffset + idx3] + w4 * input[cOffset + idx4]; - } - } - } - return out_img; - } - function permute_data(array, dims, axes) { - const shape = new Array(axes.length); - const stride = new Array(axes.length); - for (let i = axes.length - 1, s = 1; i >= 0; --i) { - stride[i] = s; - shape[i] = dims[axes[i]]; - s *= shape[i]; - } - const invStride = axes.map((_, i) => stride[axes.indexOf(i)]); - const permutedData = new array.constructor(array.length); - for (let i = 0; i < array.length; ++i) { - let newIndex = 0; - for (let j = dims.length - 1, k = i; j >= 0; --j) { - newIndex += k % dims[j] * invStride[j]; - k = Math.floor(k / dims[j]); - } - permutedData[newIndex] = array[i]; - } - return [permutedData, shape]; - } - function softmax(arr) { - const maxVal = max(arr)[0]; - const exps = arr.map((x) => Math.exp(x - maxVal)); - const sumExps = exps.reduce((acc, val) => acc + val, 0); - const softmaxArr = exps.map((x) => x / sumExps); - return ( - /** @type {T} */ - softmaxArr - ); - } - function log_softmax(arr) { - const maxVal = max(arr)[0]; - let sumExps = 0; - for (let i = 0; i < arr.length; ++i) { - sumExps += Math.exp(arr[i] - maxVal); - } - const logSum = Math.log(sumExps); - const logSoftmaxArr = arr.map((x) => x - maxVal - logSum); - return ( - /** @type {T} */ - logSoftmaxArr - ); - } - function dot(arr1, arr2) { - let result = 0; - for (let i = 0; i < arr1.length; ++i) { - result += arr1[i] * arr2[i]; - } - return result; - } - function cos_sim(arr1, arr2) { - const dotProduct = dot(arr1, arr2); - const magnitudeA = magnitude(arr1); - const magnitudeB = magnitude(arr2); - const cosineSimilarity = dotProduct / (magnitudeA * magnitudeB); - return cosineSimilarity; - } - function magnitude(arr) { - return Math.sqrt(arr.reduce((acc, val) => acc + val * val, 0)); - } - function min(arr) { - if (arr.length === 0) throw Error("Array must not be empty"); - let min2 = arr[0]; - let indexOfMin = 0; - for (let i = 1; i < arr.length; ++i) { - if (arr[i] < min2) { - min2 = arr[i]; - indexOfMin = i; - } - } - return [min2, indexOfMin]; - } - function max(arr) { - if (arr.length === 0) throw Error("Array must not be empty"); - let max2 = arr[0]; - let indexOfMax = 0; - for (let i = 1; i < arr.length; ++i) { - if (arr[i] > max2) { - max2 = arr[i]; - indexOfMax = i; - } - } - return [Number(max2), indexOfMax]; - } - function isPowerOfTwo(number) { - return number > 0 && (number & number - 1) === 0; - } - class P2FFT { - /** - * @param {number} size The size of the input array. Must be a power of two larger than 1. - * @throws {Error} FFT size must be a power of two larger than 1. - */ - constructor(size) { - this.size = size | 0; - if (this.size <= 1 || !isPowerOfTwo(this.size)) - throw new Error("FFT size must be a power of two larger than 1"); - this._csize = size << 1; - this.table = new Float64Array(this.size * 2); - for (let i = 0; i < this.table.length; i += 2) { - const angle = Math.PI * i / this.size; - this.table[i] = Math.cos(angle); - this.table[i + 1] = -Math.sin(angle); - } - let power = 0; - for (let t = 1; this.size > t; t <<= 1) - ++power; - this._width = power % 2 === 0 ? power - 1 : power; - this._bitrev = new Int32Array(1 << this._width); - for (let j = 0; j < this._bitrev.length; ++j) { - this._bitrev[j] = 0; - for (let shift = 0; shift < this._width; shift += 2) { - const revShift = this._width - shift - 2; - this._bitrev[j] |= (j >>> shift & 3) << revShift; - } - } - } - /** - * Create a complex number array with size `2 * size` - * - * @returns {Float64Array} A complex number array with size `2 * size` - */ - createComplexArray() { - return new Float64Array(this._csize); - } - /** - * Converts a complex number representation stored in a Float64Array to an array of real numbers. - * - * @param {Float64Array} complex The complex number representation to be converted. - * @param {number[]} [storage] An optional array to store the result in. - * @returns {number[]} An array of real numbers representing the input complex number representation. - */ - fromComplexArray(complex, storage) { - const res = storage || new Array(complex.length >>> 1); - for (let i = 0; i < complex.length; i += 2) - res[i >>> 1] = complex[i]; - return res; - } - /** - * Convert a real-valued input array to a complex-valued output array. - * @param {Float64Array} input The real-valued input array. - * @param {Float64Array} [storage] Optional buffer to store the output array. - * @returns {Float64Array} The complex-valued output array. - */ - toComplexArray(input, storage) { - const res = storage || this.createComplexArray(); - for (let i = 0; i < res.length; i += 2) { - res[i] = input[i >>> 1]; - res[i + 1] = 0; - } - return res; - } - /** - * Performs a Fast Fourier Transform (FFT) on the given input data and stores the result in the output buffer. - * - * @param {Float64Array} out The output buffer to store the result. - * @param {Float64Array} data The input data to transform. - * - * @throws {Error} Input and output buffers must be different. - * - * @returns {void} - */ - transform(out, data) { - if (out === data) - throw new Error("Input and output buffers must be different"); - this._transform4( - out, - data, - 1 - /* DONE */ - ); - } - /** - * Performs a real-valued forward FFT on the given input buffer and stores the result in the given output buffer. - * The input buffer must contain real values only, while the output buffer will contain complex values. The input and - * output buffers must be different. - * - * @param {Float64Array} out The output buffer. - * @param {Float64Array} data The input buffer containing real values. - * - * @throws {Error} If the input and output buffers are the same. - */ - realTransform(out, data) { - if (out === data) - throw new Error("Input and output buffers must be different"); - this._realTransform4( - out, - data, - 1 - /* DONE */ - ); - } - /** - * Performs an inverse FFT transformation on the given `data` array, and stores the result in `out`. - * The `out` array must be a different buffer than the `data` array. The `out` array will contain the - * result of the transformation. The `data` array will not be modified. - * - * @param {Float64Array} out The output buffer for the transformed data. - * @param {Float64Array} data The input data to transform. - * @throws {Error} If `out` and `data` refer to the same buffer. - * @returns {void} - */ - inverseTransform(out, data) { - if (out === data) - throw new Error("Input and output buffers must be different"); - this._transform4( - out, - data, - -1 - /* DONE */ - ); - for (let i = 0; i < out.length; ++i) - out[i] /= this.size; - } - /** - * Performs a radix-4 implementation of a discrete Fourier transform on a given set of data. - * - * @param {Float64Array} out The output buffer for the transformed data. - * @param {Float64Array} data The input buffer of data to be transformed. - * @param {number} inv A scaling factor to apply to the transform. - * @returns {void} - */ - _transform4(out, data, inv) { - const size = this._csize; - const width = this._width; - let step = 1 << width; - let len = size / step << 1; - let outOff; - let t; - const bitrev = this._bitrev; - if (len === 4) { - for (outOff = 0, t = 0; outOff < size; outOff += len, ++t) { - const off = bitrev[t]; - this._singleTransform2(data, out, outOff, off, step); - } - } else { - for (outOff = 0, t = 0; outOff < size; outOff += len, ++t) { - const off = bitrev[t]; - this._singleTransform4(data, out, outOff, off, step, inv); - } - } - const table = this.table; - for (step >>= 2; step >= 2; step >>= 2) { - len = size / step << 1; - const quarterLen = len >>> 2; - for (outOff = 0; outOff < size; outOff += len) { - const limit = outOff + quarterLen - 1; - for (let i = outOff, k = 0; i < limit; i += 2, k += step) { - const A = i; - const B = A + quarterLen; - const C = B + quarterLen; - const D = C + quarterLen; - const Ar = out[A]; - const Ai = out[A + 1]; - const Br = out[B]; - const Bi = out[B + 1]; - const Cr = out[C]; - const Ci = out[C + 1]; - const Dr = out[D]; - const Di = out[D + 1]; - const tableBr = table[k]; - const tableBi = inv * table[k + 1]; - const MBr = Br * tableBr - Bi * tableBi; - const MBi = Br * tableBi + Bi * tableBr; - const tableCr = table[2 * k]; - const tableCi = inv * table[2 * k + 1]; - const MCr = Cr * tableCr - Ci * tableCi; - const MCi = Cr * tableCi + Ci * tableCr; - const tableDr = table[3 * k]; - const tableDi = inv * table[3 * k + 1]; - const MDr = Dr * tableDr - Di * tableDi; - const MDi = Dr * tableDi + Di * tableDr; - const T0r = Ar + MCr; - const T0i = Ai + MCi; - const T1r = Ar - MCr; - const T1i = Ai - MCi; - const T2r = MBr + MDr; - const T2i = MBi + MDi; - const T3r = inv * (MBr - MDr); - const T3i = inv * (MBi - MDi); - out[A] = T0r + T2r; - out[A + 1] = T0i + T2i; - out[B] = T1r + T3i; - out[B + 1] = T1i - T3r; - out[C] = T0r - T2r; - out[C + 1] = T0i - T2i; - out[D] = T1r - T3i; - out[D + 1] = T1i + T3r; - } - } - } - } - /** - * Performs a radix-2 implementation of a discrete Fourier transform on a given set of data. - * - * @param {Float64Array} data The input buffer of data to be transformed. - * @param {Float64Array} out The output buffer for the transformed data. - * @param {number} outOff The offset at which to write the output data. - * @param {number} off The offset at which to begin reading the input data. - * @param {number} step The step size for indexing the input data. - * @returns {void} - */ - _singleTransform2(data, out, outOff, off, step) { - const evenR = data[off]; - const evenI = data[off + 1]; - const oddR = data[off + step]; - const oddI = data[off + step + 1]; - out[outOff] = evenR + oddR; - out[outOff + 1] = evenI + oddI; - out[outOff + 2] = evenR - oddR; - out[outOff + 3] = evenI - oddI; - } - /** - * Performs radix-4 transformation on input data of length 8 - * - * @param {Float64Array} data Input data array of length 8 - * @param {Float64Array} out Output data array of length 8 - * @param {number} outOff Index of output array to start writing from - * @param {number} off Index of input array to start reading from - * @param {number} step Step size between elements in input array - * @param {number} inv Scaling factor for inverse transform - * - * @returns {void} - */ - _singleTransform4(data, out, outOff, off, step, inv) { - const step2 = step * 2; - const step3 = step * 3; - const Ar = data[off]; - const Ai = data[off + 1]; - const Br = data[off + step]; - const Bi = data[off + step + 1]; - const Cr = data[off + step2]; - const Ci = data[off + step2 + 1]; - const Dr = data[off + step3]; - const Di = data[off + step3 + 1]; - const T0r = Ar + Cr; - const T0i = Ai + Ci; - const T1r = Ar - Cr; - const T1i = Ai - Ci; - const T2r = Br + Dr; - const T2i = Bi + Di; - const T3r = inv * (Br - Dr); - const T3i = inv * (Bi - Di); - out[outOff] = T0r + T2r; - out[outOff + 1] = T0i + T2i; - out[outOff + 2] = T1r + T3i; - out[outOff + 3] = T1i - T3r; - out[outOff + 4] = T0r - T2r; - out[outOff + 5] = T0i - T2i; - out[outOff + 6] = T1r - T3i; - out[outOff + 7] = T1i + T3r; - } - /** - * Real input radix-4 implementation - * @param {Float64Array} out Output array for the transformed data - * @param {Float64Array} data Input array of real data to be transformed - * @param {number} inv The scale factor used to normalize the inverse transform - */ - _realTransform4(out, data, inv) { - const size = this._csize; - const width = this._width; - let step = 1 << width; - let len = size / step << 1; - let outOff; - let t; - const bitrev = this._bitrev; - if (len === 4) { - for (outOff = 0, t = 0; outOff < size; outOff += len, ++t) { - const off = bitrev[t]; - this._singleRealTransform2(data, out, outOff, off >>> 1, step >>> 1); - } - } else { - for (outOff = 0, t = 0; outOff < size; outOff += len, ++t) { - const off = bitrev[t]; - this._singleRealTransform4(data, out, outOff, off >>> 1, step >>> 1, inv); - } - } - const table = this.table; - for (step >>= 2; step >= 2; step >>= 2) { - len = size / step << 1; - const halfLen = len >>> 1; - const quarterLen = halfLen >>> 1; - const hquarterLen = quarterLen >>> 1; - for (outOff = 0; outOff < size; outOff += len) { - for (let i = 0, k = 0; i <= hquarterLen; i += 2, k += step) { - const A = outOff + i; - const B = A + quarterLen; - const C = B + quarterLen; - const D = C + quarterLen; - const Ar = out[A]; - const Ai = out[A + 1]; - const Br = out[B]; - const Bi = out[B + 1]; - const Cr = out[C]; - const Ci = out[C + 1]; - const Dr = out[D]; - const Di = out[D + 1]; - const MAr = Ar; - const MAi = Ai; - const tableBr = table[k]; - const tableBi = inv * table[k + 1]; - const MBr = Br * tableBr - Bi * tableBi; - const MBi = Br * tableBi + Bi * tableBr; - const tableCr = table[2 * k]; - const tableCi = inv * table[2 * k + 1]; - const MCr = Cr * tableCr - Ci * tableCi; - const MCi = Cr * tableCi + Ci * tableCr; - const tableDr = table[3 * k]; - const tableDi = inv * table[3 * k + 1]; - const MDr = Dr * tableDr - Di * tableDi; - const MDi = Dr * tableDi + Di * tableDr; - const T0r = MAr + MCr; - const T0i = MAi + MCi; - const T1r = MAr - MCr; - const T1i = MAi - MCi; - const T2r = MBr + MDr; - const T2i = MBi + MDi; - const T3r = inv * (MBr - MDr); - const T3i = inv * (MBi - MDi); - out[A] = T0r + T2r; - out[A + 1] = T0i + T2i; - out[B] = T1r + T3i; - out[B + 1] = T1i - T3r; - if (i === 0) { - out[C] = T0r - T2r; - out[C + 1] = T0i - T2i; - continue; - } - if (i === hquarterLen) - continue; - const SA = outOff + quarterLen - i; - const SB = outOff + halfLen - i; - out[SA] = T1r - inv * T3i; - out[SA + 1] = -T1i - inv * T3r; - out[SB] = T0r - inv * T2r; - out[SB + 1] = -T0i + inv * T2i; - } - } - } - const half = size >>> 1; - for (let i = 2; i < half; i += 2) { - out[size - i] = out[i]; - out[size - i + 1] = -out[i + 1]; - } - } - /** - * Performs a single real input radix-2 transformation on the provided data - * - * @param {Float64Array} data The input data array - * @param {Float64Array} out The output data array - * @param {number} outOff The output offset - * @param {number} off The input offset - * @param {number} step The step - * - * @returns {void} - */ - _singleRealTransform2(data, out, outOff, off, step) { - const evenR = data[off]; - const oddR = data[off + step]; - out[outOff] = evenR + oddR; - out[outOff + 1] = 0; - out[outOff + 2] = evenR - oddR; - out[outOff + 3] = 0; - } - /** - * Computes a single real-valued transform using radix-4 algorithm. - * This method is only called for len=8. - * - * @param {Float64Array} data The input data array. - * @param {Float64Array} out The output data array. - * @param {number} outOff The offset into the output array. - * @param {number} off The offset into the input array. - * @param {number} step The step size for the input array. - * @param {number} inv The value of inverse. - */ - _singleRealTransform4(data, out, outOff, off, step, inv) { - const step2 = step * 2; - const step3 = step * 3; - const Ar = data[off]; - const Br = data[off + step]; - const Cr = data[off + step2]; - const Dr = data[off + step3]; - const T0r = Ar + Cr; - const T1r = Ar - Cr; - const T2r = Br + Dr; - const T3r = inv * (Br - Dr); - out[outOff] = T0r + T2r; - out[outOff + 1] = 0; - out[outOff + 2] = T1r; - out[outOff + 3] = -T3r; - out[outOff + 4] = T0r - T2r; - out[outOff + 5] = 0; - out[outOff + 6] = T1r; - out[outOff + 7] = T3r; - } - } - class NP2FFT { - /** - * Constructs a new NP2FFT object. - * @param {number} fft_length The length of the FFT - */ - constructor(fft_length) { - const a = 2 * (fft_length - 1); - const b = 2 * (2 * fft_length - 1); - const nextP2 = 2 ** Math.ceil(Math.log2(b)); - this.bufferSize = nextP2; - this._a = a; - const chirp = new Float64Array(b); - const ichirp = new Float64Array(nextP2); - this._chirpBuffer = new Float64Array(nextP2); - this._buffer1 = new Float64Array(nextP2); - this._buffer2 = new Float64Array(nextP2); - this._outBuffer1 = new Float64Array(nextP2); - this._outBuffer2 = new Float64Array(nextP2); - const theta = -2 * Math.PI / fft_length; - const baseR = Math.cos(theta); - const baseI = Math.sin(theta); - for (let i = 0; i < b >> 1; ++i) { - const e = (i + 1 - fft_length) ** 2 / 2; - const result_mod = Math.sqrt(baseR ** 2 + baseI ** 2) ** e; - const result_arg = e * Math.atan2(baseI, baseR); - const i2 = 2 * i; - chirp[i2] = result_mod * Math.cos(result_arg); - chirp[i2 + 1] = result_mod * Math.sin(result_arg); - ichirp[i2] = chirp[i2]; - ichirp[i2 + 1] = -chirp[i2 + 1]; - } - this._slicedChirpBuffer = chirp.subarray(a, b); - this._f = new P2FFT(nextP2 >> 1); - this._f.transform(this._chirpBuffer, ichirp); - } - _transform(output, input, real) { - const ib1 = this._buffer1; - const ib2 = this._buffer2; - const ob2 = this._outBuffer1; - const ob3 = this._outBuffer2; - const cb = this._chirpBuffer; - const sb = this._slicedChirpBuffer; - const a = this._a; - if (real) { - for (let j = 0; j < sb.length; j += 2) { - const j2 = j + 1; - const j3 = j >> 1; - const a_real = input[j3]; - ib1[j] = a_real * sb[j]; - ib1[j2] = a_real * sb[j2]; - } - } else { - for (let j = 0; j < sb.length; j += 2) { - const j2 = j + 1; - ib1[j] = input[j] * sb[j] - input[j2] * sb[j2]; - ib1[j2] = input[j] * sb[j2] + input[j2] * sb[j]; - } - } - this._f.transform(ob2, ib1); - for (let j = 0; j < cb.length; j += 2) { - const j2 = j + 1; - ib2[j] = ob2[j] * cb[j] - ob2[j2] * cb[j2]; - ib2[j2] = ob2[j] * cb[j2] + ob2[j2] * cb[j]; - } - this._f.inverseTransform(ob3, ib2); - for (let j = 0; j < ob3.length; j += 2) { - const a_real = ob3[j + a]; - const a_imag = ob3[j + a + 1]; - const b_real = sb[j]; - const b_imag = sb[j + 1]; - output[j] = a_real * b_real - a_imag * b_imag; - output[j + 1] = a_real * b_imag + a_imag * b_real; - } - } - transform(output, input) { - this._transform(output, input, false); - } - realTransform(output, input) { - this._transform(output, input, true); - } - } - class FFT { - constructor(fft_length) { - this.fft_length = fft_length; - this.isPowerOfTwo = isPowerOfTwo(fft_length); - if (this.isPowerOfTwo) { - this.fft = new P2FFT(fft_length); - this.outputBufferSize = 2 * fft_length; - } else { - this.fft = new NP2FFT(fft_length); - this.outputBufferSize = this.fft.bufferSize; - } - } - realTransform(out, input) { - this.fft.realTransform(out, input); - } - transform(out, input) { - this.fft.transform(out, input); - } - } - function medianFilter(data, windowSize) { - if (windowSize % 2 === 0 || windowSize <= 0) { - throw new Error("Window size must be a positive odd number"); - } - const outputArray = new data.constructor(data.length); - const buffer = new data.constructor(windowSize); - const halfWindowSize = Math.floor(windowSize / 2); - for (let i = 0; i < data.length; ++i) { - let valuesIndex = 0; - for (let j = -halfWindowSize; j <= halfWindowSize; ++j) { - let index = i + j; - if (index < 0) { - index = Math.abs(index); - } else if (index >= data.length) { - index = 2 * (data.length - 1) - index; - } - buffer[valuesIndex++] = data[index]; - } - buffer.sort(); - outputArray[i] = buffer[halfWindowSize]; - } - return outputArray; - } - function round(num, decimals) { - const pow = Math.pow(10, decimals); - return Math.round(num * pow) / pow; - } - function bankers_round(x) { - const r = Math.round(x); - const br = Math.abs(x) % 1 === 0.5 ? r % 2 === 0 ? r : r - 1 : r; - return br; - } - function dynamic_time_warping(matrix) { - const output_length = matrix.length; - const input_length = matrix[0].length; - const outputShape = [output_length + 1, input_length + 1]; - const cost = Array.from( - { length: outputShape[0] }, - () => Array(outputShape[1]).fill(Infinity) - ); - cost[0][0] = 0; - const trace = Array.from( - { length: outputShape[0] }, - () => Array(outputShape[1]).fill(-1) - ); - for (let j2 = 1; j2 < outputShape[1]; ++j2) { - for (let i2 = 1; i2 < outputShape[0]; ++i2) { - const c0 = cost[i2 - 1][j2 - 1]; - const c1 = cost[i2 - 1][j2]; - const c2 = cost[i2][j2 - 1]; - let c, t; - if (c0 < c1 && c0 < c2) { - c = c0; - t = 0; - } else if (c1 < c0 && c1 < c2) { - c = c1; - t = 1; - } else { - c = c2; - t = 2; - } - cost[i2][j2] = matrix[i2 - 1][j2 - 1] + c; - trace[i2][j2] = t; - } - } - for (let i2 = 0; i2 < outputShape[1]; ++i2) { - trace[0][i2] = 2; - } - for (let i2 = 0; i2 < outputShape[0]; ++i2) { - trace[i2][0] = 1; - } - let i = output_length; - let j = input_length; - let text_indices = []; - let time_indices = []; - while (i > 0 || j > 0) { - text_indices.push(i - 1); - time_indices.push(j - 1); - switch (trace[i][j]) { - case 0: - --i; - --j; - break; - case 1: - --i; - break; - case 2: - --j; - break; - default: - throw new Error( - `Internal error in dynamic time warping. Unexpected trace[${i}, ${j}]. Please file a bug report.` - ); - } - } - text_indices.reverse(); - time_indices.reverse(); - return [text_indices, time_indices]; - } - } - ), - /***/ - "./src/utils/tensor.js": ( - /*!*****************************!*\ - !*** ./src/utils/tensor.js ***! - \*****************************/ - /***/ - (__unused_webpack___webpack_module__, __webpack_exports__2, __webpack_require__2) => { - __webpack_require__2.r(__webpack_exports__2); - __webpack_require__2.d(__webpack_exports__2, { - /* harmony export */ - Tensor: () => ( - /* binding */ - Tensor - ), - /* harmony export */ - cat: () => ( - /* binding */ - cat - ), - /* harmony export */ - full: () => ( - /* binding */ - full - ), - /* harmony export */ - full_like: () => ( - /* binding */ - full_like - ), - /* harmony export */ - interpolate: () => ( - /* binding */ - interpolate - ), - /* harmony export */ - interpolate_4d: () => ( - /* binding */ - interpolate_4d - ), - /* harmony export */ - layer_norm: () => ( - /* binding */ - layer_norm - ), - /* harmony export */ - matmul: () => ( - /* binding */ - matmul - ), - /* harmony export */ - mean: () => ( - /* binding */ - mean - ), - /* harmony export */ - mean_pooling: () => ( - /* binding */ - mean_pooling - ), - /* harmony export */ - ones: () => ( - /* binding */ - ones - ), - /* harmony export */ - ones_like: () => ( - /* binding */ - ones_like - ), - /* harmony export */ - permute: () => ( - /* binding */ - permute - ), - /* harmony export */ - quantize_embeddings: () => ( - /* binding */ - quantize_embeddings - ), - /* harmony export */ - rfft: () => ( - /* binding */ - rfft - ), - /* harmony export */ - stack: () => ( - /* binding */ - stack - ), - /* harmony export */ - std_mean: () => ( - /* binding */ - std_mean - ), - /* harmony export */ - topk: () => ( - /* binding */ - topk - ), - /* harmony export */ - zeros: () => ( - /* binding */ - zeros - ), - /* harmony export */ - zeros_like: () => ( - /* binding */ - zeros_like - ) - /* harmony export */ - }); - var _maths_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__2( - /*! ./maths.js */ - "./src/utils/maths.js" - ); - var _backends_onnx_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__2( - /*! ../backends/onnx.js */ - "./src/backends/onnx.js" - ); - var _ops_registry_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__2( - /*! ../ops/registry.js */ - "./src/ops/registry.js" - ); - const DataTypeMap = Object.freeze({ - float32: Float32Array, - float16: Uint16Array, - float64: Float64Array, - string: Array, - // string[] - int8: Int8Array, - uint8: Uint8Array, - int16: Int16Array, - uint16: Uint16Array, - int32: Int32Array, - uint32: Uint32Array, - int64: BigInt64Array, - uint64: BigUint64Array, - bool: Uint8Array - }); - class Tensor { - /** - * Create a new Tensor or copy an existing Tensor. - * @param {[DataType, DataArray, number[]]|[ONNXTensor]} args - */ - constructor(...args) { - __publicField(this, "ort_tensor"); - if ((0, _backends_onnx_js__WEBPACK_IMPORTED_MODULE_1__.isONNXTensor)(args[0])) { - this.ort_tensor = /** @type {ONNXTensor} */ - args[0]; - } else { - this.ort_tensor = new _backends_onnx_js__WEBPACK_IMPORTED_MODULE_1__.Tensor( - /** @type {DataType} */ - args[0], - /** @type {Exclude} */ - args[1], - args[2] - ); - } - return new Proxy(this, { - get: (obj, key) => { - if (typeof key === "string") { - let index = Number(key); - if (Number.isInteger(index)) { - return obj._getitem(index); - } - } - return obj[key]; - }, - set: (obj, key, value) => { - return obj[key] = value; - } - }); - } - /** @type {number[]} Dimensions of the tensor. */ - get dims() { - return this.ort_tensor.dims; - } - set dims(value) { - this.ort_tensor.dims = value; - } - /** @type {DataType} Type of the tensor. */ - get type() { - return this.ort_tensor.type; - } - /** @type {DataArray} The data stored in the tensor. */ - get data() { - return this.ort_tensor.data; - } - /** @type {number} The number of elements in the tensor. */ - get size() { - return this.ort_tensor.size; - } - /** @type {string} The location of the tensor data. */ - get location() { - return this.ort_tensor.location; - } - dispose() { - this.ort_tensor.dispose(); - } - /** - * Returns an iterator object for iterating over the tensor data in row-major order. - * If the tensor has more than one dimension, the iterator will yield subarrays. - * @returns {Iterator} An iterator object for iterating over the tensor data in row-major order. - */ - *[Symbol.iterator]() { - const [iterLength, ...iterDims] = this.dims; - if (iterDims.length > 0) { - const iterSize = iterDims.reduce((a, b) => a * b); - for (let i = 0; i < iterLength; ++i) { - yield this._subarray(i, iterSize, iterDims); - } - } else { - yield* this.data; - } - } - /** - * Index into a Tensor object. - * @param {number} index The index to access. - * @returns {Tensor} The data at the specified index. - */ - _getitem(index) { - const [iterLength, ...iterDims] = this.dims; - index = safeIndex(index, iterLength); - if (iterDims.length > 0) { - const iterSize = iterDims.reduce((a, b) => a * b); - return this._subarray(index, iterSize, iterDims); - } else { - return new Tensor(this.type, [this.data[index]], iterDims); - } - } - /** - * @param {number|bigint} item The item to search for in the tensor - * @returns {number} The index of the first occurrence of item in the tensor data. - */ - indexOf(item) { - const this_data = this.data; - for (let index = 0; index < this_data.length; ++index) { - if (this_data[index] == item) { - return index; - } - } - return -1; - } - /** - * @param {number} index - * @param {number} iterSize - * @param {any} iterDims - * @returns {Tensor} - */ - _subarray(index, iterSize, iterDims) { - const o1 = index * iterSize; - const o2 = (index + 1) * iterSize; - const data = "subarray" in this.data ? this.data.subarray(o1, o2) : this.data.slice(o1, o2); - return new Tensor(this.type, data, iterDims); - } - /** - * Returns the value of this tensor as a standard JavaScript Number. This only works - * for tensors with one element. For other cases, see `Tensor.tolist()`. - * @returns {number|bigint} The value of this tensor as a standard JavaScript Number. - * @throws {Error} If the tensor has more than one element. - */ - item() { - const this_data = this.data; - if (this_data.length !== 1) { - throw new Error(`a Tensor with ${this_data.length} elements cannot be converted to Scalar`); - } - return this_data[0]; - } - /** - * Convert tensor data to a n-dimensional JS list - * @returns {Array} - */ - tolist() { - return reshape(this.data, this.dims); - } - /** - * Return a new Tensor with the sigmoid function applied to each element. - * @returns {Tensor} The tensor with the sigmoid function applied. - */ - sigmoid() { - return this.clone().sigmoid_(); - } - /** - * Applies the sigmoid function to the tensor in place. - * @returns {Tensor} Returns `this`. - */ - sigmoid_() { - const this_data = this.data; - for (let i = 0; i < this_data.length; ++i) { - this_data[i] = 1 / (1 + Math.exp(-this_data[i])); - } - return this; - } - /** - * Return a new Tensor with a callback function applied to each element. - * @param {Function} callback - The function to apply to each element. It should take three arguments: - * the current element, its index, and the tensor's data array. - * @returns {Tensor} A new Tensor with the callback function applied to each element. - */ - map(callback) { - return this.clone().map_(callback); - } - /** - * Apply a callback function to each element of the tensor in place. - * @param {Function} callback - The function to apply to each element. It should take three arguments: - * the current element, its index, and the tensor's data array. - * @returns {Tensor} Returns `this`. - */ - map_(callback) { - const this_data = this.data; - for (let i = 0; i < this_data.length; ++i) { - this_data[i] = callback(this_data[i], i, this_data); - } - return this; - } - /** - * Return a new Tensor with every element multiplied by a constant. - * @param {number} val The value to multiply by. - * @returns {Tensor} The new tensor. - */ - mul(val) { - return this.clone().mul_(val); - } - /** - * Multiply the tensor by a constant in place. - * @param {number} val The value to multiply by. - * @returns {Tensor} Returns `this`. - */ - mul_(val) { - const this_data = this.data; - for (let i = 0; i < this_data.length; ++i) { - this_data[i] *= val; - } - return this; - } - /** - * Return a new Tensor with every element divided by a constant. - * @param {number} val The value to divide by. - * @returns {Tensor} The new tensor. - */ - div(val) { - return this.clone().div_(val); - } - /** - * Divide the tensor by a constant in place. - * @param {number} val The value to divide by. - * @returns {Tensor} Returns `this`. - */ - div_(val) { - const this_data = this.data; - for (let i = 0; i < this_data.length; ++i) { - this_data[i] /= val; - } - return this; - } - /** - * Return a new Tensor with every element added by a constant. - * @param {number} val The value to add by. - * @returns {Tensor} The new tensor. - */ - add(val) { - return this.clone().add_(val); - } - /** - * Add the tensor by a constant in place. - * @param {number} val The value to add by. - * @returns {Tensor} Returns `this`. - */ - add_(val) { - const this_data = this.data; - for (let i = 0; i < this_data.length; ++i) { - this_data[i] += val; - } - return this; - } - /** - * Return a new Tensor with every element subtracted by a constant. - * @param {number} val The value to subtract by. - * @returns {Tensor} The new tensor. - */ - sub(val) { - return this.clone().sub_(val); - } - /** - * Subtract the tensor by a constant in place. - * @param {number} val The value to subtract by. - * @returns {Tensor} Returns `this`. - */ - sub_(val) { - const this_data = this.data; - for (let i = 0; i < this_data.length; ++i) { - this_data[i] -= val; - } - return this; - } - clone() { - return new Tensor(this.type, this.data.slice(), this.dims.slice()); - } - slice(...slices) { - const newTensorDims = []; - const newOffsets = []; - for (let sliceIndex = 0; sliceIndex < this.dims.length; ++sliceIndex) { - let slice = slices[sliceIndex]; - if (slice === null || slice === void 0) { - newOffsets.push([0, this.dims[sliceIndex]]); - newTensorDims.push(this.dims[sliceIndex]); - } else if (typeof slice === "number") { - slice = safeIndex(slice, this.dims[sliceIndex], sliceIndex); - newOffsets.push([slice, slice + 1]); - } else if (Array.isArray(slice) && slice.length === 2) { - let [start, end] = slice; - start = start === null ? 0 : safeIndex(start, this.dims[sliceIndex], sliceIndex, false); - end = end === null ? this.dims[sliceIndex] : safeIndex(end, this.dims[sliceIndex], sliceIndex, false); - if (start > end) { - throw new Error(`Invalid slice: ${slice}`); - } - const offsets = [ - Math.max(start, 0), - Math.min(end, this.dims[sliceIndex]) - ]; - newOffsets.push(offsets); - newTensorDims.push(offsets[1] - offsets[0]); - } else { - throw new Error(`Invalid slice: ${slice}`); - } - } - const newDims = newOffsets.map(([start, end]) => end - start); - const newBufferSize = newDims.reduce((a, b) => a * b); - const this_data = this.data; - const data = new this_data.constructor(newBufferSize); - const stride = this.stride(); - for (let i = 0; i < newBufferSize; ++i) { - let originalIndex = 0; - for (let j = newDims.length - 1, num = i; j >= 0; --j) { - const size = newDims[j]; - originalIndex += (num % size + newOffsets[j][0]) * stride[j]; - num = Math.floor(num / size); - } - data[i] = this_data[originalIndex]; - } - return new Tensor(this.type, data, newTensorDims); - } - /** - * Return a permuted version of this Tensor, according to the provided dimensions. - * @param {...number} dims Dimensions to permute. - * @returns {Tensor} The permuted tensor. - */ - permute(...dims) { - return permute(this, dims); - } - // TODO: implement transpose. For now (backwards compatibility), it's just an alias for permute() - transpose(...dims) { - return this.permute(...dims); - } - // TODO add .max() and .min() methods - /** - * Returns the sum of each row of the input tensor in the given dimension dim. - * - * @param {number} [dim=null] The dimension or dimensions to reduce. If `null`, all dimensions are reduced. - * @param {boolean} keepdim Whether the output tensor has `dim` retained or not. - * @returns The summed tensor - */ - sum(dim = null, keepdim = false) { - return this.norm(1, dim, keepdim); - } - /** - * Returns the matrix norm or vector norm of a given tensor. - * @param {number|string} [p='fro'] The order of norm - * @param {number} [dim=null] Specifies which dimension of the tensor to calculate the norm across. - * If dim is None, the norm will be calculated across all dimensions of input. - * @param {boolean} [keepdim=false] Whether the output tensors have dim retained or not. - * @returns {Tensor} The norm of the tensor. - */ - norm(p = "fro", dim = null, keepdim = false) { - if (p === "fro") { - p = 2; - } else if (typeof p === "string") { - throw Error(`Unsupported norm: ${p}`); - } - const this_data = this.data; - if (dim === null) { - let val = this_data.reduce((a, b) => a + b ** p, 0) ** (1 / p); - return new Tensor(this.type, [val], []); - } - dim = safeIndex(dim, this.dims.length); - const resultDims = this.dims.slice(); - resultDims[dim] = 1; - const result = new this_data.constructor(this_data.length / this.dims[dim]); - for (let i = 0; i < this_data.length; ++i) { - let resultIndex = 0; - for (let j = this.dims.length - 1, num = i, resultMultiplier = 1; j >= 0; --j) { - const size = this.dims[j]; - if (j !== dim) { - const index = num % size; - resultIndex += index * resultMultiplier; - resultMultiplier *= resultDims[j]; - } - num = Math.floor(num / size); - } - result[resultIndex] += this_data[i] ** p; - } - if (p !== 1) { - for (let i = 0; i < result.length; ++i) { - result[i] = result[i] ** (1 / p); - } - } - if (!keepdim) { - resultDims.splice(dim, 1); - } - return new Tensor(this.type, result, resultDims); - } - /** - * Performs `L_p` normalization of inputs over specified dimension. Operates in place. - * @param {number} [p=2] The exponent value in the norm formulation - * @param {number} [dim=1] The dimension to reduce - * @returns {Tensor} `this` for operation chaining. - */ - normalize_(p = 2, dim = 1) { - dim = safeIndex(dim, this.dims.length); - const norm = this.norm(p, dim, true); - const this_data = this.data; - const norm_data = norm.data; - for (let i = 0; i < this_data.length; ++i) { - let resultIndex = 0; - for (let j = this.dims.length - 1, num = i, resultMultiplier = 1; j >= 0; --j) { - const size = this.dims[j]; - if (j !== dim) { - const index = num % size; - resultIndex += index * resultMultiplier; - resultMultiplier *= this.dims[j]; - } - num = Math.floor(num / size); - } - this_data[i] /= norm_data[resultIndex]; - } - return this; - } - /** - * Performs `L_p` normalization of inputs over specified dimension. - * @param {number} [p=2] The exponent value in the norm formulation - * @param {number} [dim=1] The dimension to reduce - * @returns {Tensor} The normalized tensor. - */ - normalize(p = 2, dim = 1) { - return this.clone().normalize_(p, dim); - } - /** - * Compute and return the stride of this tensor. - * Stride is the jump necessary to go from one element to the next one in the specified dimension dim. - * @returns {number[]} The stride of this tensor. - */ - stride() { - return dimsToStride(this.dims); - } - /** - * Returns a tensor with all specified dimensions of input of size 1 removed. - * - * NOTE: The returned tensor shares the storage with the input tensor, so changing the contents of one will change the contents of the other. - * If you would like a copy, use `tensor.clone()` before squeezing. - * - * @param {number} [dim=null] If given, the input will be squeezed only in the specified dimensions. - * @returns {Tensor} The squeezed tensor - */ - squeeze(dim = null) { - return new Tensor( - this.type, - this.data, - calc_squeeze_dims(this.dims, dim) - ); - } - /** - * In-place version of @see {@link Tensor.squeeze} - */ - squeeze_(dim = null) { - this.dims = calc_squeeze_dims(this.dims, dim); - return this; - } - /** - * Returns a new tensor with a dimension of size one inserted at the specified position. - * - * NOTE: The returned tensor shares the same underlying data with this tensor. - * - * @param {number} dim The index at which to insert the singleton dimension - * @returns {Tensor} The unsqueezed tensor - */ - unsqueeze(dim = null) { - return new Tensor( - this.type, - this.data, - calc_unsqueeze_dims(this.dims, dim) - ); - } - /** - * In-place version of @see {@link Tensor.unsqueeze} - */ - unsqueeze_(dim = null) { - this.dims = calc_unsqueeze_dims(this.dims, dim); - return this; - } - /** - * In-place version of @see {@link Tensor.flatten} - */ - flatten_(start_dim = 0, end_dim = -1) { - end_dim = (end_dim + this.dims.length) % this.dims.length; - let dimsToKeepBefore = this.dims.slice(0, start_dim); - let dimsToFlatten = this.dims.slice(start_dim, end_dim + 1); - let dimsToKeepAfter = this.dims.slice(end_dim + 1); - this.dims = [...dimsToKeepBefore, dimsToFlatten.reduce((a, b) => a * b, 1), ...dimsToKeepAfter]; - return this; - } - /** - * Flattens input by reshaping it into a one-dimensional tensor. - * If `start_dim` or `end_dim` are passed, only dimensions starting with `start_dim` - * and ending with `end_dim` are flattened. The order of elements in input is unchanged. - * @param {number} start_dim the first dim to flatten - * @param {number} end_dim the last dim to flatten - * @returns {Tensor} The flattened tensor. - */ - flatten(start_dim = 0, end_dim = -1) { - return this.clone().flatten_(start_dim, end_dim); - } - /** - * Returns a new tensor with the same data as the `self` tensor but of a different `shape`. - * @param {...number} dims the desired size - * @returns {Tensor} The tensor with the same data but different shape - */ - view(...dims) { - let inferredIndex = -1; - for (let i = 0; i < dims.length; ++i) { - if (dims[i] === -1) { - if (inferredIndex !== -1) { - throw new Error("Only one dimension can be inferred"); - } - inferredIndex = i; - } - } - const this_data = this.data; - if (inferredIndex !== -1) { - const productOther = dims.reduce((product, curr, index) => { - return index !== inferredIndex ? product * curr : product; - }, 1); - dims[inferredIndex] = this_data.length / productOther; - } - return new Tensor(this.type, this_data, dims); - } - neg_() { - const this_data = this.data; - for (let i = 0; i < this_data.length; ++i) { - this_data[i] = -this_data[i]; - } - return this; - } - neg() { - return this.clone().neg_(); - } - /** - * In-place version of @see {@link Tensor.clamp} - */ - clamp_(min, max) { - const this_data = this.data; - for (let i = 0; i < this_data.length; ++i) { - this_data[i] = Math.min(Math.max(this_data[i], min), max); - } - return this; - } - /** - * Clamps all elements in input into the range [ min, max ] - * @param {number} min lower-bound of the range to be clamped to - * @param {number} max upper-bound of the range to be clamped to - * @returns {Tensor} the output tensor. - */ - clamp(min, max) { - return this.clone().clamp_(min, max); - } - /** - * In-place version of @see {@link Tensor.round} - */ - round_() { - const this_data = this.data; - for (let i = 0; i < this_data.length; ++i) { - this_data[i] = Math.round(this_data[i]); - } - return this; - } - /** - * Rounds elements of input to the nearest integer. - * @returns {Tensor} the output tensor. - */ - round() { - return this.clone().round_(); - } - mean(dim = null, keepdim = false) { - return mean(this, dim, keepdim); - } - /** - * Performs Tensor dtype conversion. - * @param {DataType} type The desired data type. - * @returns {Tensor} The converted tensor. - */ - to(type) { - if (this.type === type) return this; - if (!DataTypeMap.hasOwnProperty(type)) { - throw new Error(`Unsupported type: ${type}`); - } - return new Tensor(type, DataTypeMap[type].from(this.data), this.dims); - } - } - function reshape(data, dimensions) { - const totalElements = data.length; - const dimensionSize = dimensions.reduce((a, b) => a * b); - if (totalElements !== dimensionSize) { - throw Error(`cannot reshape array of size ${totalElements} into shape (${dimensions})`); - } - let reshapedArray = data; - for (let i = dimensions.length - 1; i >= 0; i--) { - reshapedArray = reshapedArray.reduce((acc, val) => { - let lastArray = acc[acc.length - 1]; - if (lastArray.length < dimensions[i]) { - lastArray.push(val); - } else { - acc.push([val]); - } - return acc; - }, [[]]); - } - return reshapedArray[0]; - } - function permute(tensor, axes) { - const [permutedData, shape] = (0, _maths_js__WEBPACK_IMPORTED_MODULE_0__.permute_data)(tensor.data, tensor.dims, axes); - return new Tensor(tensor.type, permutedData, shape); - } - function interpolate(input, [out_height, out_width], mode = "bilinear", align_corners = false) { - const in_channels = input.dims.at(-3) ?? 1; - const in_height = input.dims.at(-2); - const in_width = input.dims.at(-1); - let output = (0, _maths_js__WEBPACK_IMPORTED_MODULE_0__.interpolate_data)( - /** @type {import('./maths.js').TypedArray}*/ - input.data, - [in_channels, in_height, in_width], - [out_height, out_width], - mode, - align_corners - ); - return new Tensor(input.type, output, [in_channels, out_height, out_width]); - } - async function interpolate_4d(input, { - size = null, - mode = "bilinear" - } = {}) { - if (input.dims.length !== 4) { - throw new Error("`interpolate_4d` currently only supports 4D input."); - } - if (!size) { - throw new Error("`interpolate_4d` requires a `size` argument."); - } - let targetDims; - if (size.length === 2) { - targetDims = [...input.dims.slice(0, 2), ...size]; - } else if (size.length === 3) { - targetDims = [input.dims[0], ...size]; - } else if (size.length === 4) { - targetDims = size; - } else { - throw new Error("`size` must be of length 2, 3, or 4."); - } - let op; - if (mode === "bilinear") { - op = await _ops_registry_js__WEBPACK_IMPORTED_MODULE_2__.TensorOpRegistry.bilinear_interpolate_4d; - } else if (mode === "bicubic") { - op = await _ops_registry_js__WEBPACK_IMPORTED_MODULE_2__.TensorOpRegistry.bicubic_interpolate_4d; - } else { - throw new Error(`Unsupported mode: ${mode}`); - } - const sizeTensor = new Tensor("int64", new BigInt64Array(targetDims.map(BigInt)), [targetDims.length]); - return await op({ x: input, s: sizeTensor }); - } - async function matmul(a, b) { - const op = await _ops_registry_js__WEBPACK_IMPORTED_MODULE_2__.TensorOpRegistry.matmul; - return await op({ a, b }); - } - async function rfft(x, a) { - const op = await _ops_registry_js__WEBPACK_IMPORTED_MODULE_2__.TensorOpRegistry.rfft; - return await op({ x, a }); - } - async function topk(x, k) { - const op = await _ops_registry_js__WEBPACK_IMPORTED_MODULE_2__.TensorOpRegistry.top_k; - if (k === null) { - k = x.dims.at(-1); - } else { - k = Math.min(k, x.dims.at(-1)); - } - return await op({ - x, - k: new Tensor( - "int64", - [BigInt(k)], - [1] - ) - }); - } - function mean_pooling(last_hidden_state, attention_mask) { - const lastHiddenStateData = last_hidden_state.data; - const attentionMaskData = attention_mask.data; - const shape = [last_hidden_state.dims[0], last_hidden_state.dims[2]]; - const returnedData = new lastHiddenStateData.constructor(shape[0] * shape[1]); - const [batchSize, seqLength, embedDim] = last_hidden_state.dims; - let outIndex = 0; - for (let i = 0; i < batchSize; ++i) { - const offset = i * embedDim * seqLength; - for (let k = 0; k < embedDim; ++k) { - let sum = 0; - let count = 0; - const attnMaskOffset = i * seqLength; - const offset2 = offset + k; - for (let j = 0; j < seqLength; ++j) { - const attn = Number(attentionMaskData[attnMaskOffset + j]); - count += attn; - sum += lastHiddenStateData[offset2 + j * embedDim] * attn; - } - const avg = sum / count; - returnedData[outIndex++] = avg; - } - } - return new Tensor( - last_hidden_state.type, - returnedData, - shape - ); - } - function layer_norm(input, normalized_shape, { - eps = 1e-5 - } = {}) { - if (input.dims.length !== 2) { - throw new Error("`layer_norm` currently only supports 2D input."); - } - const [batchSize, featureDim] = input.dims; - if (normalized_shape.length !== 1 && normalized_shape[0] !== featureDim) { - throw new Error("`normalized_shape` must be a 1D array with shape `[input.dims[1]]`."); - } - const [std, mean2] = std_mean(input, 1, 0, true); - const stdData = ( - /** @type {Float32Array} */ - std.data - ); - const meanData = ( - /** @type {Float32Array} */ - mean2.data - ); - const inputData = ( - /** @type {Float32Array} */ - input.data - ); - const returnedData = new inputData.constructor(inputData.length); - for (let i = 0; i < batchSize; ++i) { - const offset = i * featureDim; - for (let j = 0; j < featureDim; ++j) { - const offset2 = offset + j; - returnedData[offset2] = (inputData[offset2] - meanData[i]) / (stdData[i] + eps); - } - } - return new Tensor(input.type, returnedData, input.dims); - } - function calc_squeeze_dims(dims, dim) { - dims = dims.slice(); - if (dim === null) { - dims = dims.filter((d) => d !== 1); - } else if (typeof dim === "number") { - if (dims[dim] === 1) { - dims.splice(dim, 1); - } - } else if (Array.isArray(dim)) { - dims = dims.filter((x, i) => { - return x !== 1 || !dim.includes(i); - }); - } - return dims; - } - function calc_unsqueeze_dims(dims, dim) { - dim = safeIndex(dim, dims.length + 1); - dims = dims.slice(); - dims.splice(dim, 0, 1); - return dims; - } - function safeIndex(index, size, dimension = null, boundsCheck = true) { - if (boundsCheck && (index < -size || index >= size)) { - throw new Error(`IndexError: index ${index} is out of bounds for dimension${dimension === null ? "" : " " + dimension} with size ${size}`); - } - if (index < 0) { - index = (index % size + size) % size; - } - return index; - } - function cat(tensors, dim = 0) { - dim = safeIndex(dim, tensors[0].dims.length); - const resultDims = tensors[0].dims.slice(); - resultDims[dim] = tensors.reduce((a, b) => a + b.dims[dim], 0); - const resultSize = resultDims.reduce((a, b) => a * b, 1); - const result = new tensors[0].data.constructor(resultSize); - const resultType = tensors[0].type; - if (dim === 0) { - let offset = 0; - for (const tensor of tensors) { - const tensorData = tensor.data; - result.set(tensorData, offset); - offset += tensorData.length; - } - } else { - let currentDim = 0; - for (let t = 0; t < tensors.length; ++t) { - const { data, dims } = tensors[t]; - for (let i = 0; i < data.length; ++i) { - let resultIndex = 0; - for (let j = dims.length - 1, num = i, resultMultiplier = 1; j >= 0; --j) { - const size = dims[j]; - let index = num % size; - if (j === dim) { - index += currentDim; - } - resultIndex += index * resultMultiplier; - resultMultiplier *= resultDims[j]; - num = Math.floor(num / size); - } - result[resultIndex] = data[i]; - } - currentDim += dims[dim]; - } - } - return new Tensor(resultType, result, resultDims); - } - function stack(tensors, dim = 0) { - return cat(tensors.map((t) => t.unsqueeze(dim)), dim); - } - function std_mean(input, dim = null, correction = 1, keepdim = false) { - const inputData = ( - /** @type {Float32Array} */ - input.data - ); - const inputDims = input.dims; - if (dim === null) { - const sum = inputData.reduce((a, b) => a + b, 0); - const mean2 = sum / inputData.length; - const std = Math.sqrt(inputData.reduce((a, b) => a + (b - mean2) ** 2, 0) / (inputData.length - correction)); - const meanTensor2 = new Tensor(input.type, [mean2], [ - /* scalar */ - ]); - const stdTensor2 = new Tensor(input.type, [std], [ - /* scalar */ - ]); - return [stdTensor2, meanTensor2]; - } - dim = safeIndex(dim, inputDims.length); - const meanTensor = mean(input, dim, keepdim); - const meanTensorData = meanTensor.data; - const resultDims = inputDims.slice(); - resultDims[dim] = 1; - const result = new inputData.constructor(inputData.length / inputDims[dim]); - for (let i = 0; i < inputData.length; ++i) { - let resultIndex = 0; - for (let j = inputDims.length - 1, num = i, resultMultiplier = 1; j >= 0; --j) { - const size = inputDims[j]; - if (j !== dim) { - const index = num % size; - resultIndex += index * resultMultiplier; - resultMultiplier *= resultDims[j]; - } - num = Math.floor(num / size); - } - result[resultIndex] += (inputData[i] - meanTensorData[resultIndex]) ** 2; - } - for (let i = 0; i < result.length; ++i) { - result[i] = Math.sqrt(result[i] / (inputDims[dim] - correction)); - } - if (!keepdim) { - resultDims.splice(dim, 1); - } - const stdTensor = new Tensor(input.type, result, resultDims); - return [stdTensor, meanTensor]; - } - function mean(input, dim = null, keepdim = false) { - const inputData = ( - /** @type {Float32Array} */ - input.data - ); - if (dim === null) { - const val = inputData.reduce((a, b) => a + b, 0); - return new Tensor(input.type, [val / inputData.length], [ - /* scalar */ - ]); - } - const inputDims = input.dims; - dim = safeIndex(dim, inputDims.length); - const resultDims = inputDims.slice(); - resultDims[dim] = 1; - const result = new inputData.constructor(inputData.length / inputDims[dim]); - for (let i = 0; i < inputData.length; ++i) { - let resultIndex = 0; - for (let j = inputDims.length - 1, num = i, resultMultiplier = 1; j >= 0; --j) { - const size = inputDims[j]; - if (j !== dim) { - const index = num % size; - resultIndex += index * resultMultiplier; - resultMultiplier *= resultDims[j]; - } - num = Math.floor(num / size); - } - result[resultIndex] += inputData[i]; - } - if (inputDims[dim] !== 1) { - for (let i = 0; i < result.length; ++i) { - result[i] = result[i] / inputDims[dim]; - } - } - if (!keepdim) { - resultDims.splice(dim, 1); - } - return new Tensor(input.type, result, resultDims); - } - function dimsToStride(dims) { - const stride = new Array(dims.length); - for (let i = dims.length - 1, s2 = 1; i >= 0; --i) { - stride[i] = s2; - s2 *= dims[i]; - } - return stride; - } - function fullHelper(size, fill_value, dtype, cls) { - const numElements = size.reduce((a, b) => a * b, 1); - return new Tensor( - dtype, - new cls(numElements).fill(fill_value), - size - ); - } - function full(size, fill_value) { - let dtype; - let typedArrayCls; - if (typeof fill_value === "number") { - dtype = "float32"; - typedArrayCls = Float32Array; - } else if (typeof fill_value === "bigint") { - dtype = "int64"; - typedArrayCls = BigInt64Array; - } else { - throw new Error(`Unsupported data type: ${typeof fill_value}`); - } - return fullHelper(size, fill_value, dtype, typedArrayCls); - } - function full_like(tensor, fill_value) { - return full(tensor.dims, fill_value); - } - function ones(size) { - return fullHelper(size, 1n, "int64", BigInt64Array); - } - function ones_like(tensor) { - return ones(tensor.dims); - } - function zeros(size) { - return fullHelper(size, 0n, "int64", BigInt64Array); - } - function zeros_like(tensor) { - return zeros(tensor.dims); - } - function quantize_embeddings(tensor, precision) { - if (tensor.dims.length !== 2) { - throw new Error("The tensor must have 2 dimensions"); - } - if (tensor.dims.at(-1) % 8 !== 0) { - throw new Error("The last dimension of the tensor must be a multiple of 8"); - } - if (!["binary", "ubinary"].includes(precision)) { - throw new Error("The precision must be either 'binary' or 'ubinary'"); - } - const signed = precision === "binary"; - const dtype = signed ? "int8" : "uint8"; - const cls = signed ? Int8Array : Uint8Array; - const inputData = tensor.data; - const outputData = new cls(inputData.length / 8); - for (let i = 0; i < inputData.length; ++i) { - const bit = inputData[i] > 0 ? 1 : 0; - const arrayIndex = Math.floor(i / 8); - const bitPosition = i % 8; - outputData[arrayIndex] |= bit << 7 - bitPosition; - if (signed && bitPosition === 0) { - outputData[arrayIndex] -= 128; - } - } - ; - return new Tensor(dtype, outputData, [tensor.dims[0], tensor.dims[1] / 8]); - } - } - ) - /******/ - }; - var __webpack_module_cache__ = {}; - function __webpack_require__(moduleId) { - var cachedModule = __webpack_module_cache__[moduleId]; - if (cachedModule !== void 0) { - return cachedModule.exports; - } - var module = __webpack_module_cache__[moduleId] = { - /******/ - // no module.id needed - /******/ - // no module.loaded needed - /******/ - exports: {} - /******/ - }; - __webpack_modules__[moduleId](module, module.exports, __webpack_require__); - return module.exports; - } - __webpack_require__.m = __webpack_modules__; - (() => { - var getProto = Object.getPrototypeOf ? (obj) => Object.getPrototypeOf(obj) : (obj) => obj.__proto__; - var leafPrototypes; - __webpack_require__.t = function(value, mode) { - if (mode & 1) value = this(value); - if (mode & 8) return value; - if (typeof value === "object" && value) { - if (mode & 4 && value.__esModule) return value; - if (mode & 16 && typeof value.then === "function") return value; - } - var ns = /* @__PURE__ */ Object.create(null); - __webpack_require__.r(ns); - var def = {}; - leafPrototypes = leafPrototypes || [null, getProto({}), getProto([]), getProto(getProto)]; - for (var current = mode & 2 && value; typeof current == "object" && !~leafPrototypes.indexOf(current); current = getProto(current)) { - Object.getOwnPropertyNames(current).forEach((key) => def[key] = () => value[key]); - } - def["default"] = () => value; - __webpack_require__.d(ns, def); - return ns; - }; - })(); - (() => { - __webpack_require__.d = (exports, definition) => { - for (var key in definition) { - if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) { - Object.defineProperty(exports, key, { enumerable: true, get: definition[key] }); - } - } - }; - })(); - (() => { - __webpack_require__.o = (obj, prop) => Object.prototype.hasOwnProperty.call(obj, prop); - })(); - (() => { - __webpack_require__.r = (exports) => { - if (typeof Symbol !== "undefined" && Symbol.toStringTag) { - Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" }); - } - Object.defineProperty(exports, "__esModule", { value: true }); - }; - })(); - (() => { - var scriptUrl; - if (typeof META_URL === "string") scriptUrl = META_URL; - if (!scriptUrl) throw new Error("Automatic publicPath is not supported in this browser"); - scriptUrl = scriptUrl.replace(/#.*$/, "").replace(/\?.*$/, "").replace(/\/[^\/]+$/, "/"); - __webpack_require__.p = scriptUrl; - })(); - (() => { - __webpack_require__.b = new URL("/node_modules/.vite/deps", META_URL); - var installedChunks = { - /******/ - "transformers": 0 - /******/ - }; - })(); - var __webpack_exports__ = {}; - __webpack_require__.r(__webpack_exports__); - __webpack_require__.d(__webpack_exports__, { - /* harmony export */ - ASTFeatureExtractor: () => ( - /* reexport safe */ - _processors_js__WEBPACK_IMPORTED_MODULE_4__.ASTFeatureExtractor - ), - /* harmony export */ - ASTForAudioClassification: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.ASTForAudioClassification - ), - /* harmony export */ - ASTModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.ASTModel - ), - /* harmony export */ - ASTPreTrainedModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.ASTPreTrainedModel - ), - /* harmony export */ - AlbertForMaskedLM: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.AlbertForMaskedLM - ), - /* harmony export */ - AlbertForQuestionAnswering: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.AlbertForQuestionAnswering - ), - /* harmony export */ - AlbertForSequenceClassification: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.AlbertForSequenceClassification - ), - /* harmony export */ - AlbertModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.AlbertModel - ), - /* harmony export */ - AlbertPreTrainedModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.AlbertPreTrainedModel - ), - /* harmony export */ - AlbertTokenizer: () => ( - /* reexport safe */ - _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.AlbertTokenizer - ), - /* harmony export */ - AudioClassificationPipeline: () => ( - /* reexport safe */ - _pipelines_js__WEBPACK_IMPORTED_MODULE_1__.AudioClassificationPipeline - ), - /* harmony export */ - AutoConfig: () => ( - /* reexport safe */ - _configs_js__WEBPACK_IMPORTED_MODULE_5__.AutoConfig - ), - /* harmony export */ - AutoModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModel - ), - /* harmony export */ - AutoModelForAudioClassification: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModelForAudioClassification - ), - /* harmony export */ - AutoModelForAudioFrameClassification: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModelForAudioFrameClassification - ), - /* harmony export */ - AutoModelForCTC: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModelForCTC - ), - /* harmony export */ - AutoModelForCausalLM: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModelForCausalLM - ), - /* harmony export */ - AutoModelForDepthEstimation: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModelForDepthEstimation - ), - /* harmony export */ - AutoModelForDocumentQuestionAnswering: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModelForDocumentQuestionAnswering - ), - /* harmony export */ - AutoModelForImageClassification: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModelForImageClassification - ), - /* harmony export */ - AutoModelForImageFeatureExtraction: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModelForImageFeatureExtraction - ), - /* harmony export */ - AutoModelForImageMatting: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModelForImageMatting - ), - /* harmony export */ - AutoModelForImageSegmentation: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModelForImageSegmentation - ), - /* harmony export */ - AutoModelForImageToImage: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModelForImageToImage - ), - /* harmony export */ - AutoModelForMaskGeneration: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModelForMaskGeneration - ), - /* harmony export */ - AutoModelForMaskedLM: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModelForMaskedLM - ), - /* harmony export */ - AutoModelForNormalEstimation: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModelForNormalEstimation - ), - /* harmony export */ - AutoModelForObjectDetection: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModelForObjectDetection - ), - /* harmony export */ - AutoModelForQuestionAnswering: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModelForQuestionAnswering - ), - /* harmony export */ - AutoModelForSemanticSegmentation: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModelForSemanticSegmentation - ), - /* harmony export */ - AutoModelForSeq2SeqLM: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModelForSeq2SeqLM - ), - /* harmony export */ - AutoModelForSequenceClassification: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModelForSequenceClassification - ), - /* harmony export */ - AutoModelForSpeechSeq2Seq: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModelForSpeechSeq2Seq - ), - /* harmony export */ - AutoModelForTextToSpectrogram: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModelForTextToSpectrogram - ), - /* harmony export */ - AutoModelForTextToWaveform: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModelForTextToWaveform - ), - /* harmony export */ - AutoModelForTokenClassification: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModelForTokenClassification - ), - /* harmony export */ - AutoModelForUniversalSegmentation: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModelForUniversalSegmentation - ), - /* harmony export */ - AutoModelForVision2Seq: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModelForVision2Seq - ), - /* harmony export */ - AutoModelForXVector: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModelForXVector - ), - /* harmony export */ - AutoModelForZeroShotObjectDetection: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModelForZeroShotObjectDetection - ), - /* harmony export */ - AutoProcessor: () => ( - /* reexport safe */ - _processors_js__WEBPACK_IMPORTED_MODULE_4__.AutoProcessor - ), - /* harmony export */ - AutoTokenizer: () => ( - /* reexport safe */ - _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.AutoTokenizer - ), - /* harmony export */ - AutomaticSpeechRecognitionPipeline: () => ( - /* reexport safe */ - _pipelines_js__WEBPACK_IMPORTED_MODULE_1__.AutomaticSpeechRecognitionPipeline - ), - /* harmony export */ - BartForConditionalGeneration: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.BartForConditionalGeneration - ), - /* harmony export */ - BartForSequenceClassification: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.BartForSequenceClassification - ), - /* harmony export */ - BartModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.BartModel - ), - /* harmony export */ - BartPretrainedModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.BartPretrainedModel - ), - /* harmony export */ - BartTokenizer: () => ( - /* reexport safe */ - _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.BartTokenizer - ), - /* harmony export */ - BaseModelOutput: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.BaseModelOutput - ), - /* harmony export */ - BaseStreamer: () => ( - /* reexport safe */ - _generation_streamers_js__WEBPACK_IMPORTED_MODULE_10__.BaseStreamer - ), - /* harmony export */ - BeitFeatureExtractor: () => ( - /* reexport safe */ - _processors_js__WEBPACK_IMPORTED_MODULE_4__.BeitFeatureExtractor - ), - /* harmony export */ - BeitForImageClassification: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.BeitForImageClassification - ), - /* harmony export */ - BeitModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.BeitModel - ), - /* harmony export */ - BeitPreTrainedModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.BeitPreTrainedModel - ), - /* harmony export */ - BertForMaskedLM: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.BertForMaskedLM - ), - /* harmony export */ - BertForQuestionAnswering: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.BertForQuestionAnswering - ), - /* harmony export */ - BertForSequenceClassification: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.BertForSequenceClassification - ), - /* harmony export */ - BertForTokenClassification: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.BertForTokenClassification - ), - /* harmony export */ - BertModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.BertModel - ), - /* harmony export */ - BertPreTrainedModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.BertPreTrainedModel - ), - /* harmony export */ - BertTokenizer: () => ( - /* reexport safe */ - _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.BertTokenizer - ), - /* harmony export */ - BitImageProcessor: () => ( - /* reexport safe */ - _processors_js__WEBPACK_IMPORTED_MODULE_4__.BitImageProcessor - ), - /* harmony export */ - BlenderbotForConditionalGeneration: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.BlenderbotForConditionalGeneration - ), - /* harmony export */ - BlenderbotModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.BlenderbotModel - ), - /* harmony export */ - BlenderbotPreTrainedModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.BlenderbotPreTrainedModel - ), - /* harmony export */ - BlenderbotSmallForConditionalGeneration: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.BlenderbotSmallForConditionalGeneration - ), - /* harmony export */ - BlenderbotSmallModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.BlenderbotSmallModel - ), - /* harmony export */ - BlenderbotSmallPreTrainedModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.BlenderbotSmallPreTrainedModel - ), - /* harmony export */ - BlenderbotSmallTokenizer: () => ( - /* reexport safe */ - _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.BlenderbotSmallTokenizer - ), - /* harmony export */ - BlenderbotTokenizer: () => ( - /* reexport safe */ - _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.BlenderbotTokenizer - ), - /* harmony export */ - BloomForCausalLM: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.BloomForCausalLM - ), - /* harmony export */ - BloomModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.BloomModel - ), - /* harmony export */ - BloomPreTrainedModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.BloomPreTrainedModel - ), - /* harmony export */ - BloomTokenizer: () => ( - /* reexport safe */ - _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.BloomTokenizer - ), - /* harmony export */ - CLIPFeatureExtractor: () => ( - /* reexport safe */ - _processors_js__WEBPACK_IMPORTED_MODULE_4__.CLIPFeatureExtractor - ), - /* harmony export */ - CLIPImageProcessor: () => ( - /* reexport safe */ - _processors_js__WEBPACK_IMPORTED_MODULE_4__.CLIPImageProcessor - ), - /* harmony export */ - CLIPModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.CLIPModel - ), - /* harmony export */ - CLIPPreTrainedModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.CLIPPreTrainedModel - ), - /* harmony export */ - CLIPSegForImageSegmentation: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.CLIPSegForImageSegmentation - ), - /* harmony export */ - CLIPSegModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.CLIPSegModel - ), - /* harmony export */ - CLIPSegPreTrainedModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.CLIPSegPreTrainedModel - ), - /* harmony export */ - CLIPTextModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.CLIPTextModel - ), - /* harmony export */ - CLIPTextModelWithProjection: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.CLIPTextModelWithProjection - ), - /* harmony export */ - CLIPTokenizer: () => ( - /* reexport safe */ - _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.CLIPTokenizer - ), - /* harmony export */ - CLIPVisionModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.CLIPVisionModel - ), - /* harmony export */ - CLIPVisionModelWithProjection: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.CLIPVisionModelWithProjection - ), - /* harmony export */ - CamembertForMaskedLM: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.CamembertForMaskedLM - ), - /* harmony export */ - CamembertForQuestionAnswering: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.CamembertForQuestionAnswering - ), - /* harmony export */ - CamembertForSequenceClassification: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.CamembertForSequenceClassification - ), - /* harmony export */ - CamembertForTokenClassification: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.CamembertForTokenClassification - ), - /* harmony export */ - CamembertModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.CamembertModel - ), - /* harmony export */ - CamembertPreTrainedModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.CamembertPreTrainedModel - ), - /* harmony export */ - CamembertTokenizer: () => ( - /* reexport safe */ - _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.CamembertTokenizer - ), - /* harmony export */ - CausalLMOutput: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.CausalLMOutput - ), - /* harmony export */ - CausalLMOutputWithPast: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.CausalLMOutputWithPast - ), - /* harmony export */ - ChineseCLIPFeatureExtractor: () => ( - /* reexport safe */ - _processors_js__WEBPACK_IMPORTED_MODULE_4__.ChineseCLIPFeatureExtractor - ), - /* harmony export */ - ChineseCLIPModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.ChineseCLIPModel - ), - /* harmony export */ - ChineseCLIPPreTrainedModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.ChineseCLIPPreTrainedModel - ), - /* harmony export */ - ClapAudioModelWithProjection: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.ClapAudioModelWithProjection - ), - /* harmony export */ - ClapFeatureExtractor: () => ( - /* reexport safe */ - _processors_js__WEBPACK_IMPORTED_MODULE_4__.ClapFeatureExtractor - ), - /* harmony export */ - ClapModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.ClapModel - ), - /* harmony export */ - ClapPreTrainedModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.ClapPreTrainedModel - ), - /* harmony export */ - ClapTextModelWithProjection: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.ClapTextModelWithProjection - ), - /* harmony export */ - CodeGenForCausalLM: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.CodeGenForCausalLM - ), - /* harmony export */ - CodeGenModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.CodeGenModel - ), - /* harmony export */ - CodeGenPreTrainedModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.CodeGenPreTrainedModel - ), - /* harmony export */ - CodeGenTokenizer: () => ( - /* reexport safe */ - _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.CodeGenTokenizer - ), - /* harmony export */ - CodeLlamaTokenizer: () => ( - /* reexport safe */ - _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.CodeLlamaTokenizer - ), - /* harmony export */ - CohereForCausalLM: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.CohereForCausalLM - ), - /* harmony export */ - CohereModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.CohereModel - ), - /* harmony export */ - CoherePreTrainedModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.CoherePreTrainedModel - ), - /* harmony export */ - CohereTokenizer: () => ( - /* reexport safe */ - _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.CohereTokenizer - ), - /* harmony export */ - ConvBertForMaskedLM: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.ConvBertForMaskedLM - ), - /* harmony export */ - ConvBertForQuestionAnswering: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.ConvBertForQuestionAnswering - ), - /* harmony export */ - ConvBertForSequenceClassification: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.ConvBertForSequenceClassification - ), - /* harmony export */ - ConvBertForTokenClassification: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.ConvBertForTokenClassification - ), - /* harmony export */ - ConvBertModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.ConvBertModel - ), - /* harmony export */ - ConvBertPreTrainedModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.ConvBertPreTrainedModel - ), - /* harmony export */ - ConvBertTokenizer: () => ( - /* reexport safe */ - _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.ConvBertTokenizer - ), - /* harmony export */ - ConvNextFeatureExtractor: () => ( - /* reexport safe */ - _processors_js__WEBPACK_IMPORTED_MODULE_4__.ConvNextFeatureExtractor - ), - /* harmony export */ - ConvNextForImageClassification: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.ConvNextForImageClassification - ), - /* harmony export */ - ConvNextImageProcessor: () => ( - /* reexport safe */ - _processors_js__WEBPACK_IMPORTED_MODULE_4__.ConvNextImageProcessor - ), - /* harmony export */ - ConvNextModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.ConvNextModel - ), - /* harmony export */ - ConvNextPreTrainedModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.ConvNextPreTrainedModel - ), - /* harmony export */ - ConvNextV2ForImageClassification: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.ConvNextV2ForImageClassification - ), - /* harmony export */ - ConvNextV2Model: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.ConvNextV2Model - ), - /* harmony export */ - ConvNextV2PreTrainedModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.ConvNextV2PreTrainedModel - ), - /* harmony export */ - DPTFeatureExtractor: () => ( - /* reexport safe */ - _processors_js__WEBPACK_IMPORTED_MODULE_4__.DPTFeatureExtractor - ), - /* harmony export */ - DPTForDepthEstimation: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.DPTForDepthEstimation - ), - /* harmony export */ - DPTImageProcessor: () => ( - /* reexport safe */ - _processors_js__WEBPACK_IMPORTED_MODULE_4__.DPTImageProcessor - ), - /* harmony export */ - DPTModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.DPTModel - ), - /* harmony export */ - DPTPreTrainedModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.DPTPreTrainedModel - ), - /* harmony export */ - DebertaForMaskedLM: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.DebertaForMaskedLM - ), - /* harmony export */ - DebertaForQuestionAnswering: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.DebertaForQuestionAnswering - ), - /* harmony export */ - DebertaForSequenceClassification: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.DebertaForSequenceClassification - ), - /* harmony export */ - DebertaForTokenClassification: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.DebertaForTokenClassification - ), - /* harmony export */ - DebertaModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.DebertaModel - ), - /* harmony export */ - DebertaPreTrainedModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.DebertaPreTrainedModel - ), - /* harmony export */ - DebertaTokenizer: () => ( - /* reexport safe */ - _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.DebertaTokenizer - ), - /* harmony export */ - DebertaV2ForMaskedLM: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.DebertaV2ForMaskedLM - ), - /* harmony export */ - DebertaV2ForQuestionAnswering: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.DebertaV2ForQuestionAnswering - ), - /* harmony export */ - DebertaV2ForSequenceClassification: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.DebertaV2ForSequenceClassification - ), - /* harmony export */ - DebertaV2ForTokenClassification: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.DebertaV2ForTokenClassification - ), - /* harmony export */ - DebertaV2Model: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.DebertaV2Model - ), - /* harmony export */ - DebertaV2PreTrainedModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.DebertaV2PreTrainedModel - ), - /* harmony export */ - DebertaV2Tokenizer: () => ( - /* reexport safe */ - _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.DebertaV2Tokenizer - ), - /* harmony export */ - DecisionTransformerModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.DecisionTransformerModel - ), - /* harmony export */ - DecisionTransformerPreTrainedModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.DecisionTransformerPreTrainedModel - ), - /* harmony export */ - DeiTFeatureExtractor: () => ( - /* reexport safe */ - _processors_js__WEBPACK_IMPORTED_MODULE_4__.DeiTFeatureExtractor - ), - /* harmony export */ - DeiTForImageClassification: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.DeiTForImageClassification - ), - /* harmony export */ - DeiTModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.DeiTModel - ), - /* harmony export */ - DeiTPreTrainedModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.DeiTPreTrainedModel - ), - /* harmony export */ - DepthAnythingForDepthEstimation: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.DepthAnythingForDepthEstimation - ), - /* harmony export */ - DepthAnythingPreTrainedModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.DepthAnythingPreTrainedModel - ), - /* harmony export */ - DepthEstimationPipeline: () => ( - /* reexport safe */ - _pipelines_js__WEBPACK_IMPORTED_MODULE_1__.DepthEstimationPipeline - ), - /* harmony export */ - DepthProForDepthEstimation: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.DepthProForDepthEstimation - ), - /* harmony export */ - DepthProPreTrainedModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.DepthProPreTrainedModel - ), - /* harmony export */ - DetrFeatureExtractor: () => ( - /* reexport safe */ - _processors_js__WEBPACK_IMPORTED_MODULE_4__.DetrFeatureExtractor - ), - /* harmony export */ - DetrForObjectDetection: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.DetrForObjectDetection - ), - /* harmony export */ - DetrForSegmentation: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.DetrForSegmentation - ), - /* harmony export */ - DetrModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.DetrModel - ), - /* harmony export */ - DetrObjectDetectionOutput: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.DetrObjectDetectionOutput - ), - /* harmony export */ - DetrPreTrainedModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.DetrPreTrainedModel - ), - /* harmony export */ - DetrSegmentationOutput: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.DetrSegmentationOutput - ), - /* harmony export */ - Dinov2ForImageClassification: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.Dinov2ForImageClassification - ), - /* harmony export */ - Dinov2Model: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.Dinov2Model - ), - /* harmony export */ - Dinov2PreTrainedModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.Dinov2PreTrainedModel - ), - /* harmony export */ - DistilBertForMaskedLM: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.DistilBertForMaskedLM - ), - /* harmony export */ - DistilBertForQuestionAnswering: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.DistilBertForQuestionAnswering - ), - /* harmony export */ - DistilBertForSequenceClassification: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.DistilBertForSequenceClassification - ), - /* harmony export */ - DistilBertForTokenClassification: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.DistilBertForTokenClassification - ), - /* harmony export */ - DistilBertModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.DistilBertModel - ), - /* harmony export */ - DistilBertPreTrainedModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.DistilBertPreTrainedModel - ), - /* harmony export */ - DistilBertTokenizer: () => ( - /* reexport safe */ - _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.DistilBertTokenizer - ), - /* harmony export */ - DocumentQuestionAnsweringPipeline: () => ( - /* reexport safe */ - _pipelines_js__WEBPACK_IMPORTED_MODULE_1__.DocumentQuestionAnsweringPipeline - ), - /* harmony export */ - DonutFeatureExtractor: () => ( - /* reexport safe */ - _processors_js__WEBPACK_IMPORTED_MODULE_4__.DonutFeatureExtractor - ), - /* harmony export */ - DonutSwinModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.DonutSwinModel - ), - /* harmony export */ - DonutSwinPreTrainedModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.DonutSwinPreTrainedModel - ), - /* harmony export */ - EfficientNetForImageClassification: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.EfficientNetForImageClassification - ), - /* harmony export */ - EfficientNetImageProcessor: () => ( - /* reexport safe */ - _processors_js__WEBPACK_IMPORTED_MODULE_4__.EfficientNetImageProcessor - ), - /* harmony export */ - EfficientNetModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.EfficientNetModel - ), - /* harmony export */ - EfficientNetPreTrainedModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.EfficientNetPreTrainedModel - ), - /* harmony export */ - ElectraForMaskedLM: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.ElectraForMaskedLM - ), - /* harmony export */ - ElectraForQuestionAnswering: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.ElectraForQuestionAnswering - ), - /* harmony export */ - ElectraForSequenceClassification: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.ElectraForSequenceClassification - ), - /* harmony export */ - ElectraForTokenClassification: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.ElectraForTokenClassification - ), - /* harmony export */ - ElectraModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.ElectraModel - ), - /* harmony export */ - ElectraPreTrainedModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.ElectraPreTrainedModel - ), - /* harmony export */ - ElectraTokenizer: () => ( - /* reexport safe */ - _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.ElectraTokenizer - ), - /* harmony export */ - EosTokenCriteria: () => ( - /* reexport safe */ - _generation_stopping_criteria_js__WEBPACK_IMPORTED_MODULE_11__.EosTokenCriteria - ), - /* harmony export */ - EsmForMaskedLM: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.EsmForMaskedLM - ), - /* harmony export */ - EsmForSequenceClassification: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.EsmForSequenceClassification - ), - /* harmony export */ - EsmForTokenClassification: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.EsmForTokenClassification - ), - /* harmony export */ - EsmModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.EsmModel - ), - /* harmony export */ - EsmPreTrainedModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.EsmPreTrainedModel - ), - /* harmony export */ - EsmTokenizer: () => ( - /* reexport safe */ - _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.EsmTokenizer - ), - /* harmony export */ - FFT: () => ( - /* reexport safe */ - _utils_maths_js__WEBPACK_IMPORTED_MODULE_9__.FFT - ), - /* harmony export */ - FalconForCausalLM: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.FalconForCausalLM - ), - /* harmony export */ - FalconModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.FalconModel - ), - /* harmony export */ - FalconPreTrainedModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.FalconPreTrainedModel - ), - /* harmony export */ - FalconTokenizer: () => ( - /* reexport safe */ - _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.FalconTokenizer - ), - /* harmony export */ - FastViTForImageClassification: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.FastViTForImageClassification - ), - /* harmony export */ - FastViTModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.FastViTModel - ), - /* harmony export */ - FastViTPreTrainedModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.FastViTPreTrainedModel - ), - /* harmony export */ - FeatureExtractionPipeline: () => ( - /* reexport safe */ - _pipelines_js__WEBPACK_IMPORTED_MODULE_1__.FeatureExtractionPipeline - ), - /* harmony export */ - FeatureExtractor: () => ( - /* reexport safe */ - _processors_js__WEBPACK_IMPORTED_MODULE_4__.FeatureExtractor - ), - /* harmony export */ - FillMaskPipeline: () => ( - /* reexport safe */ - _pipelines_js__WEBPACK_IMPORTED_MODULE_1__.FillMaskPipeline - ), - /* harmony export */ - Florence2ForConditionalGeneration: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.Florence2ForConditionalGeneration - ), - /* harmony export */ - Florence2PreTrainedModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.Florence2PreTrainedModel - ), - /* harmony export */ - Florence2Processor: () => ( - /* reexport safe */ - _processors_js__WEBPACK_IMPORTED_MODULE_4__.Florence2Processor - ), - /* harmony export */ - GLPNFeatureExtractor: () => ( - /* reexport safe */ - _processors_js__WEBPACK_IMPORTED_MODULE_4__.GLPNFeatureExtractor - ), - /* harmony export */ - GLPNForDepthEstimation: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.GLPNForDepthEstimation - ), - /* harmony export */ - GLPNModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.GLPNModel - ), - /* harmony export */ - GLPNPreTrainedModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.GLPNPreTrainedModel - ), - /* harmony export */ - GPT2LMHeadModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.GPT2LMHeadModel - ), - /* harmony export */ - GPT2Model: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.GPT2Model - ), - /* harmony export */ - GPT2PreTrainedModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.GPT2PreTrainedModel - ), - /* harmony export */ - GPT2Tokenizer: () => ( - /* reexport safe */ - _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.GPT2Tokenizer - ), - /* harmony export */ - GPTBigCodeForCausalLM: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.GPTBigCodeForCausalLM - ), - /* harmony export */ - GPTBigCodeModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.GPTBigCodeModel - ), - /* harmony export */ - GPTBigCodePreTrainedModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.GPTBigCodePreTrainedModel - ), - /* harmony export */ - GPTJForCausalLM: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.GPTJForCausalLM - ), - /* harmony export */ - GPTJModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.GPTJModel - ), - /* harmony export */ - GPTJPreTrainedModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.GPTJPreTrainedModel - ), - /* harmony export */ - GPTNeoForCausalLM: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.GPTNeoForCausalLM - ), - /* harmony export */ - GPTNeoModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.GPTNeoModel - ), - /* harmony export */ - GPTNeoPreTrainedModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.GPTNeoPreTrainedModel - ), - /* harmony export */ - GPTNeoXForCausalLM: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.GPTNeoXForCausalLM - ), - /* harmony export */ - GPTNeoXModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.GPTNeoXModel - ), - /* harmony export */ - GPTNeoXPreTrainedModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.GPTNeoXPreTrainedModel - ), - /* harmony export */ - GPTNeoXTokenizer: () => ( - /* reexport safe */ - _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.GPTNeoXTokenizer - ), - /* harmony export */ - Gemma2ForCausalLM: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.Gemma2ForCausalLM - ), - /* harmony export */ - Gemma2Model: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.Gemma2Model - ), - /* harmony export */ - Gemma2PreTrainedModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.Gemma2PreTrainedModel - ), - /* harmony export */ - GemmaForCausalLM: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.GemmaForCausalLM - ), - /* harmony export */ - GemmaModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.GemmaModel - ), - /* harmony export */ - GemmaPreTrainedModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.GemmaPreTrainedModel - ), - /* harmony export */ - GemmaTokenizer: () => ( - /* reexport safe */ - _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.GemmaTokenizer - ), - /* harmony export */ - GraniteForCausalLM: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.GraniteForCausalLM - ), - /* harmony export */ - GraniteModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.GraniteModel - ), - /* harmony export */ - GranitePreTrainedModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.GranitePreTrainedModel - ), - /* harmony export */ - Grok1Tokenizer: () => ( - /* reexport safe */ - _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.Grok1Tokenizer - ), - /* harmony export */ - GroupViTModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.GroupViTModel - ), - /* harmony export */ - GroupViTPreTrainedModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.GroupViTPreTrainedModel - ), - /* harmony export */ - HerbertTokenizer: () => ( - /* reexport safe */ - _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.HerbertTokenizer - ), - /* harmony export */ - HieraForImageClassification: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.HieraForImageClassification - ), - /* harmony export */ - HieraModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.HieraModel - ), - /* harmony export */ - HieraPreTrainedModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.HieraPreTrainedModel - ), - /* harmony export */ - HubertForCTC: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.HubertForCTC - ), - /* harmony export */ - HubertForSequenceClassification: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.HubertForSequenceClassification - ), - /* harmony export */ - HubertModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.HubertModel - ), - /* harmony export */ - HubertPreTrainedModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.HubertPreTrainedModel - ), - /* harmony export */ - ImageClassificationPipeline: () => ( - /* reexport safe */ - _pipelines_js__WEBPACK_IMPORTED_MODULE_1__.ImageClassificationPipeline - ), - /* harmony export */ - ImageFeatureExtractionPipeline: () => ( - /* reexport safe */ - _pipelines_js__WEBPACK_IMPORTED_MODULE_1__.ImageFeatureExtractionPipeline - ), - /* harmony export */ - ImageFeatureExtractor: () => ( - /* reexport safe */ - _processors_js__WEBPACK_IMPORTED_MODULE_4__.ImageFeatureExtractor - ), - /* harmony export */ - ImageMattingOutput: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.ImageMattingOutput - ), - /* harmony export */ - ImageSegmentationPipeline: () => ( - /* reexport safe */ - _pipelines_js__WEBPACK_IMPORTED_MODULE_1__.ImageSegmentationPipeline - ), - /* harmony export */ - ImageToImagePipeline: () => ( - /* reexport safe */ - _pipelines_js__WEBPACK_IMPORTED_MODULE_1__.ImageToImagePipeline - ), - /* harmony export */ - ImageToTextPipeline: () => ( - /* reexport safe */ - _pipelines_js__WEBPACK_IMPORTED_MODULE_1__.ImageToTextPipeline - ), - /* harmony export */ - InterruptableStoppingCriteria: () => ( - /* reexport safe */ - _generation_stopping_criteria_js__WEBPACK_IMPORTED_MODULE_11__.InterruptableStoppingCriteria - ), - /* harmony export */ - JAISLMHeadModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.JAISLMHeadModel - ), - /* harmony export */ - JAISModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.JAISModel - ), - /* harmony export */ - JAISPreTrainedModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.JAISPreTrainedModel - ), - /* harmony export */ - LlamaForCausalLM: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.LlamaForCausalLM - ), - /* harmony export */ - LlamaModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.LlamaModel - ), - /* harmony export */ - LlamaPreTrainedModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.LlamaPreTrainedModel - ), - /* harmony export */ - LlamaTokenizer: () => ( - /* reexport safe */ - _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.LlamaTokenizer - ), - /* harmony export */ - LlavaForConditionalGeneration: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.LlavaForConditionalGeneration - ), - /* harmony export */ - LlavaPreTrainedModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.LlavaPreTrainedModel - ), - /* harmony export */ - LongT5ForConditionalGeneration: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.LongT5ForConditionalGeneration - ), - /* harmony export */ - LongT5Model: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.LongT5Model - ), - /* harmony export */ - LongT5PreTrainedModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.LongT5PreTrainedModel - ), - /* harmony export */ - M2M100ForConditionalGeneration: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.M2M100ForConditionalGeneration - ), - /* harmony export */ - M2M100Model: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.M2M100Model - ), - /* harmony export */ - M2M100PreTrainedModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.M2M100PreTrainedModel - ), - /* harmony export */ - M2M100Tokenizer: () => ( - /* reexport safe */ - _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.M2M100Tokenizer - ), - /* harmony export */ - MBart50Tokenizer: () => ( - /* reexport safe */ - _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.MBart50Tokenizer - ), - /* harmony export */ - MBartForCausalLM: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.MBartForCausalLM - ), - /* harmony export */ - MBartForConditionalGeneration: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.MBartForConditionalGeneration - ), - /* harmony export */ - MBartForSequenceClassification: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.MBartForSequenceClassification - ), - /* harmony export */ - MBartModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.MBartModel - ), - /* harmony export */ - MBartPreTrainedModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.MBartPreTrainedModel - ), - /* harmony export */ - MBartTokenizer: () => ( - /* reexport safe */ - _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.MBartTokenizer - ), - /* harmony export */ - MPNetForMaskedLM: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.MPNetForMaskedLM - ), - /* harmony export */ - MPNetForQuestionAnswering: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.MPNetForQuestionAnswering - ), - /* harmony export */ - MPNetForSequenceClassification: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.MPNetForSequenceClassification - ), - /* harmony export */ - MPNetForTokenClassification: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.MPNetForTokenClassification - ), - /* harmony export */ - MPNetModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.MPNetModel - ), - /* harmony export */ - MPNetPreTrainedModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.MPNetPreTrainedModel - ), - /* harmony export */ - MPNetTokenizer: () => ( - /* reexport safe */ - _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.MPNetTokenizer - ), - /* harmony export */ - MT5ForConditionalGeneration: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.MT5ForConditionalGeneration - ), - /* harmony export */ - MT5Model: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.MT5Model - ), - /* harmony export */ - MT5PreTrainedModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.MT5PreTrainedModel - ), - /* harmony export */ - MarianMTModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.MarianMTModel - ), - /* harmony export */ - MarianModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.MarianModel - ), - /* harmony export */ - MarianPreTrainedModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.MarianPreTrainedModel - ), - /* harmony export */ - MarianTokenizer: () => ( - /* reexport safe */ - _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.MarianTokenizer - ), - /* harmony export */ - MaskFormerFeatureExtractor: () => ( - /* reexport safe */ - _processors_js__WEBPACK_IMPORTED_MODULE_4__.MaskFormerFeatureExtractor - ), - /* harmony export */ - MaskFormerForInstanceSegmentation: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.MaskFormerForInstanceSegmentation - ), - /* harmony export */ - MaskFormerModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.MaskFormerModel - ), - /* harmony export */ - MaskFormerPreTrainedModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.MaskFormerPreTrainedModel - ), - /* harmony export */ - MaskedLMOutput: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.MaskedLMOutput - ), - /* harmony export */ - MaxLengthCriteria: () => ( - /* reexport safe */ - _generation_stopping_criteria_js__WEBPACK_IMPORTED_MODULE_11__.MaxLengthCriteria - ), - /* harmony export */ - MistralForCausalLM: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.MistralForCausalLM - ), - /* harmony export */ - MistralModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.MistralModel - ), - /* harmony export */ - MistralPreTrainedModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.MistralPreTrainedModel - ), - /* harmony export */ - MobileBertForMaskedLM: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.MobileBertForMaskedLM - ), - /* harmony export */ - MobileBertForQuestionAnswering: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.MobileBertForQuestionAnswering - ), - /* harmony export */ - MobileBertForSequenceClassification: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.MobileBertForSequenceClassification - ), - /* harmony export */ - MobileBertModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.MobileBertModel - ), - /* harmony export */ - MobileBertPreTrainedModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.MobileBertPreTrainedModel - ), - /* harmony export */ - MobileBertTokenizer: () => ( - /* reexport safe */ - _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.MobileBertTokenizer - ), - /* harmony export */ - MobileNetV1FeatureExtractor: () => ( - /* reexport safe */ - _processors_js__WEBPACK_IMPORTED_MODULE_4__.MobileNetV1FeatureExtractor - ), - /* harmony export */ - MobileNetV1ForImageClassification: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.MobileNetV1ForImageClassification - ), - /* harmony export */ - MobileNetV1Model: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.MobileNetV1Model - ), - /* harmony export */ - MobileNetV1PreTrainedModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.MobileNetV1PreTrainedModel - ), - /* harmony export */ - MobileNetV2FeatureExtractor: () => ( - /* reexport safe */ - _processors_js__WEBPACK_IMPORTED_MODULE_4__.MobileNetV2FeatureExtractor - ), - /* harmony export */ - MobileNetV2ForImageClassification: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.MobileNetV2ForImageClassification - ), - /* harmony export */ - MobileNetV2Model: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.MobileNetV2Model - ), - /* harmony export */ - MobileNetV2PreTrainedModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.MobileNetV2PreTrainedModel - ), - /* harmony export */ - MobileNetV3FeatureExtractor: () => ( - /* reexport safe */ - _processors_js__WEBPACK_IMPORTED_MODULE_4__.MobileNetV3FeatureExtractor - ), - /* harmony export */ - MobileNetV3ForImageClassification: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.MobileNetV3ForImageClassification - ), - /* harmony export */ - MobileNetV3Model: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.MobileNetV3Model - ), - /* harmony export */ - MobileNetV3PreTrainedModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.MobileNetV3PreTrainedModel - ), - /* harmony export */ - MobileNetV4FeatureExtractor: () => ( - /* reexport safe */ - _processors_js__WEBPACK_IMPORTED_MODULE_4__.MobileNetV4FeatureExtractor - ), - /* harmony export */ - MobileNetV4ForImageClassification: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.MobileNetV4ForImageClassification - ), - /* harmony export */ - MobileNetV4Model: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.MobileNetV4Model - ), - /* harmony export */ - MobileNetV4PreTrainedModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.MobileNetV4PreTrainedModel - ), - /* harmony export */ - MobileViTFeatureExtractor: () => ( - /* reexport safe */ - _processors_js__WEBPACK_IMPORTED_MODULE_4__.MobileViTFeatureExtractor - ), - /* harmony export */ - MobileViTForImageClassification: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.MobileViTForImageClassification - ), - /* harmony export */ - MobileViTImageProcessor: () => ( - /* reexport safe */ - _processors_js__WEBPACK_IMPORTED_MODULE_4__.MobileViTImageProcessor - ), - /* harmony export */ - MobileViTModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.MobileViTModel - ), - /* harmony export */ - MobileViTPreTrainedModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.MobileViTPreTrainedModel - ), - /* harmony export */ - MobileViTV2ForImageClassification: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.MobileViTV2ForImageClassification - ), - /* harmony export */ - MobileViTV2Model: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.MobileViTV2Model - ), - /* harmony export */ - MobileViTV2PreTrainedModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.MobileViTV2PreTrainedModel - ), - /* harmony export */ - ModelOutput: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.ModelOutput - ), - /* harmony export */ - Moondream1ForConditionalGeneration: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.Moondream1ForConditionalGeneration - ), - /* harmony export */ - MptForCausalLM: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.MptForCausalLM - ), - /* harmony export */ - MptModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.MptModel - ), - /* harmony export */ - MptPreTrainedModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.MptPreTrainedModel - ), - /* harmony export */ - MusicgenForCausalLM: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.MusicgenForCausalLM - ), - /* harmony export */ - MusicgenForConditionalGeneration: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.MusicgenForConditionalGeneration - ), - /* harmony export */ - MusicgenModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.MusicgenModel - ), - /* harmony export */ - MusicgenPreTrainedModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.MusicgenPreTrainedModel - ), - /* harmony export */ - NllbTokenizer: () => ( - /* reexport safe */ - _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.NllbTokenizer - ), - /* harmony export */ - NomicBertModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.NomicBertModel - ), - /* harmony export */ - NomicBertPreTrainedModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.NomicBertPreTrainedModel - ), - /* harmony export */ - NougatImageProcessor: () => ( - /* reexport safe */ - _processors_js__WEBPACK_IMPORTED_MODULE_4__.NougatImageProcessor - ), - /* harmony export */ - NougatTokenizer: () => ( - /* reexport safe */ - _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.NougatTokenizer - ), - /* harmony export */ - OPTForCausalLM: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.OPTForCausalLM - ), - /* harmony export */ - OPTModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.OPTModel - ), - /* harmony export */ - OPTPreTrainedModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.OPTPreTrainedModel - ), - /* harmony export */ - ObjectDetectionPipeline: () => ( - /* reexport safe */ - _pipelines_js__WEBPACK_IMPORTED_MODULE_1__.ObjectDetectionPipeline - ), - /* harmony export */ - OpenELMForCausalLM: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.OpenELMForCausalLM - ), - /* harmony export */ - OpenELMModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.OpenELMModel - ), - /* harmony export */ - OpenELMPreTrainedModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.OpenELMPreTrainedModel - ), - /* harmony export */ - OwlViTFeatureExtractor: () => ( - /* reexport safe */ - _processors_js__WEBPACK_IMPORTED_MODULE_4__.OwlViTFeatureExtractor - ), - /* harmony export */ - OwlViTForObjectDetection: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.OwlViTForObjectDetection - ), - /* harmony export */ - OwlViTModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.OwlViTModel - ), - /* harmony export */ - OwlViTPreTrainedModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.OwlViTPreTrainedModel - ), - /* harmony export */ - OwlViTProcessor: () => ( - /* reexport safe */ - _processors_js__WEBPACK_IMPORTED_MODULE_4__.OwlViTProcessor - ), - /* harmony export */ - Owlv2ForObjectDetection: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.Owlv2ForObjectDetection - ), - /* harmony export */ - Owlv2ImageProcessor: () => ( - /* reexport safe */ - _processors_js__WEBPACK_IMPORTED_MODULE_4__.Owlv2ImageProcessor - ), - /* harmony export */ - Owlv2Model: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.Owlv2Model - ), - /* harmony export */ - Owlv2PreTrainedModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.Owlv2PreTrainedModel - ), - /* harmony export */ - Phi3ForCausalLM: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.Phi3ForCausalLM - ), - /* harmony export */ - Phi3Model: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.Phi3Model - ), - /* harmony export */ - Phi3PreTrainedModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.Phi3PreTrainedModel - ), - /* harmony export */ - PhiForCausalLM: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.PhiForCausalLM - ), - /* harmony export */ - PhiModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.PhiModel - ), - /* harmony export */ - PhiPreTrainedModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.PhiPreTrainedModel - ), - /* harmony export */ - Pipeline: () => ( - /* reexport safe */ - _pipelines_js__WEBPACK_IMPORTED_MODULE_1__.Pipeline - ), - /* harmony export */ - PreTrainedModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.PreTrainedModel - ), - /* harmony export */ - PreTrainedTokenizer: () => ( - /* reexport safe */ - _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.PreTrainedTokenizer - ), - /* harmony export */ - PretrainedConfig: () => ( - /* reexport safe */ - _configs_js__WEBPACK_IMPORTED_MODULE_5__.PretrainedConfig - ), - /* harmony export */ - PretrainedMixin: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.PretrainedMixin - ), - /* harmony export */ - Processor: () => ( - /* reexport safe */ - _processors_js__WEBPACK_IMPORTED_MODULE_4__.Processor - ), - /* harmony export */ - PvtForImageClassification: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.PvtForImageClassification - ), - /* harmony export */ - PvtImageProcessor: () => ( - /* reexport safe */ - _processors_js__WEBPACK_IMPORTED_MODULE_4__.PvtImageProcessor - ), - /* harmony export */ - PvtModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.PvtModel - ), - /* harmony export */ - PvtPreTrainedModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.PvtPreTrainedModel - ), - /* harmony export */ - PyAnnoteFeatureExtractor: () => ( - /* reexport safe */ - _processors_js__WEBPACK_IMPORTED_MODULE_4__.PyAnnoteFeatureExtractor - ), - /* harmony export */ - PyAnnoteForAudioFrameClassification: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.PyAnnoteForAudioFrameClassification - ), - /* harmony export */ - PyAnnoteModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.PyAnnoteModel - ), - /* harmony export */ - PyAnnotePreTrainedModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.PyAnnotePreTrainedModel - ), - /* harmony export */ - PyAnnoteProcessor: () => ( - /* reexport safe */ - _processors_js__WEBPACK_IMPORTED_MODULE_4__.PyAnnoteProcessor - ), - /* harmony export */ - QuestionAnsweringModelOutput: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.QuestionAnsweringModelOutput - ), - /* harmony export */ - QuestionAnsweringPipeline: () => ( - /* reexport safe */ - _pipelines_js__WEBPACK_IMPORTED_MODULE_1__.QuestionAnsweringPipeline - ), - /* harmony export */ - Qwen2ForCausalLM: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.Qwen2ForCausalLM - ), - /* harmony export */ - Qwen2Model: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.Qwen2Model - ), - /* harmony export */ - Qwen2PreTrainedModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.Qwen2PreTrainedModel - ), - /* harmony export */ - Qwen2Tokenizer: () => ( - /* reexport safe */ - _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.Qwen2Tokenizer - ), - /* harmony export */ - RTDetrForObjectDetection: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.RTDetrForObjectDetection - ), - /* harmony export */ - RTDetrImageProcessor: () => ( - /* reexport safe */ - _processors_js__WEBPACK_IMPORTED_MODULE_4__.RTDetrImageProcessor - ), - /* harmony export */ - RTDetrModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.RTDetrModel - ), - /* harmony export */ - RTDetrObjectDetectionOutput: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.RTDetrObjectDetectionOutput - ), - /* harmony export */ - RTDetrPreTrainedModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.RTDetrPreTrainedModel - ), - /* harmony export */ - RawImage: () => ( - /* reexport safe */ - _utils_image_js__WEBPACK_IMPORTED_MODULE_7__.RawImage - ), - /* harmony export */ - ResNetForImageClassification: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.ResNetForImageClassification - ), - /* harmony export */ - ResNetModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.ResNetModel - ), - /* harmony export */ - ResNetPreTrainedModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.ResNetPreTrainedModel - ), - /* harmony export */ - RoFormerForMaskedLM: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.RoFormerForMaskedLM - ), - /* harmony export */ - RoFormerForQuestionAnswering: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.RoFormerForQuestionAnswering - ), - /* harmony export */ - RoFormerForSequenceClassification: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.RoFormerForSequenceClassification - ), - /* harmony export */ - RoFormerForTokenClassification: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.RoFormerForTokenClassification - ), - /* harmony export */ - RoFormerModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.RoFormerModel - ), - /* harmony export */ - RoFormerPreTrainedModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.RoFormerPreTrainedModel - ), - /* harmony export */ - RoFormerTokenizer: () => ( - /* reexport safe */ - _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.RoFormerTokenizer - ), - /* harmony export */ - RobertaForMaskedLM: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.RobertaForMaskedLM - ), - /* harmony export */ - RobertaForQuestionAnswering: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.RobertaForQuestionAnswering - ), - /* harmony export */ - RobertaForSequenceClassification: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.RobertaForSequenceClassification - ), - /* harmony export */ - RobertaForTokenClassification: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.RobertaForTokenClassification - ), - /* harmony export */ - RobertaModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.RobertaModel - ), - /* harmony export */ - RobertaPreTrainedModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.RobertaPreTrainedModel - ), - /* harmony export */ - RobertaTokenizer: () => ( - /* reexport safe */ - _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.RobertaTokenizer - ), - /* harmony export */ - SamImageProcessor: () => ( - /* reexport safe */ - _processors_js__WEBPACK_IMPORTED_MODULE_4__.SamImageProcessor - ), - /* harmony export */ - SamImageSegmentationOutput: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.SamImageSegmentationOutput - ), - /* harmony export */ - SamModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.SamModel - ), - /* harmony export */ - SamPreTrainedModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.SamPreTrainedModel - ), - /* harmony export */ - SamProcessor: () => ( - /* reexport safe */ - _processors_js__WEBPACK_IMPORTED_MODULE_4__.SamProcessor - ), - /* harmony export */ - SapiensFeatureExtractor: () => ( - /* reexport safe */ - _processors_js__WEBPACK_IMPORTED_MODULE_4__.SapiensFeatureExtractor - ), - /* harmony export */ - SapiensForDepthEstimation: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.SapiensForDepthEstimation - ), - /* harmony export */ - SapiensForNormalEstimation: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.SapiensForNormalEstimation - ), - /* harmony export */ - SapiensForSemanticSegmentation: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.SapiensForSemanticSegmentation - ), - /* harmony export */ - SapiensPreTrainedModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.SapiensPreTrainedModel - ), - /* harmony export */ - SeamlessM4TFeatureExtractor: () => ( - /* reexport safe */ - _processors_js__WEBPACK_IMPORTED_MODULE_4__.SeamlessM4TFeatureExtractor - ), - /* harmony export */ - SegformerFeatureExtractor: () => ( - /* reexport safe */ - _processors_js__WEBPACK_IMPORTED_MODULE_4__.SegformerFeatureExtractor - ), - /* harmony export */ - SegformerForImageClassification: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.SegformerForImageClassification - ), - /* harmony export */ - SegformerForSemanticSegmentation: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.SegformerForSemanticSegmentation - ), - /* harmony export */ - SegformerModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.SegformerModel - ), - /* harmony export */ - SegformerPreTrainedModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.SegformerPreTrainedModel - ), - /* harmony export */ - Seq2SeqLMOutput: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.Seq2SeqLMOutput - ), - /* harmony export */ - SequenceClassifierOutput: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.SequenceClassifierOutput - ), - /* harmony export */ - SiglipImageProcessor: () => ( - /* reexport safe */ - _processors_js__WEBPACK_IMPORTED_MODULE_4__.SiglipImageProcessor - ), - /* harmony export */ - SiglipModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.SiglipModel - ), - /* harmony export */ - SiglipPreTrainedModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.SiglipPreTrainedModel - ), - /* harmony export */ - SiglipTextModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.SiglipTextModel - ), - /* harmony export */ - SiglipTokenizer: () => ( - /* reexport safe */ - _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.SiglipTokenizer - ), - /* harmony export */ - SiglipVisionModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.SiglipVisionModel - ), - /* harmony export */ - SpeechT5FeatureExtractor: () => ( - /* reexport safe */ - _processors_js__WEBPACK_IMPORTED_MODULE_4__.SpeechT5FeatureExtractor - ), - /* harmony export */ - SpeechT5ForSpeechToText: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.SpeechT5ForSpeechToText - ), - /* harmony export */ - SpeechT5ForTextToSpeech: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.SpeechT5ForTextToSpeech - ), - /* harmony export */ - SpeechT5HifiGan: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.SpeechT5HifiGan - ), - /* harmony export */ - SpeechT5Model: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.SpeechT5Model - ), - /* harmony export */ - SpeechT5PreTrainedModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.SpeechT5PreTrainedModel - ), - /* harmony export */ - SpeechT5Processor: () => ( - /* reexport safe */ - _processors_js__WEBPACK_IMPORTED_MODULE_4__.SpeechT5Processor - ), - /* harmony export */ - SpeechT5Tokenizer: () => ( - /* reexport safe */ - _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.SpeechT5Tokenizer - ), - /* harmony export */ - SqueezeBertForMaskedLM: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.SqueezeBertForMaskedLM - ), - /* harmony export */ - SqueezeBertForQuestionAnswering: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.SqueezeBertForQuestionAnswering - ), - /* harmony export */ - SqueezeBertForSequenceClassification: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.SqueezeBertForSequenceClassification - ), - /* harmony export */ - SqueezeBertModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.SqueezeBertModel - ), - /* harmony export */ - SqueezeBertPreTrainedModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.SqueezeBertPreTrainedModel - ), - /* harmony export */ - SqueezeBertTokenizer: () => ( - /* reexport safe */ - _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.SqueezeBertTokenizer - ), - /* harmony export */ - StableLmForCausalLM: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.StableLmForCausalLM - ), - /* harmony export */ - StableLmModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.StableLmModel - ), - /* harmony export */ - StableLmPreTrainedModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.StableLmPreTrainedModel - ), - /* harmony export */ - Starcoder2ForCausalLM: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.Starcoder2ForCausalLM - ), - /* harmony export */ - Starcoder2Model: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.Starcoder2Model - ), - /* harmony export */ - Starcoder2PreTrainedModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.Starcoder2PreTrainedModel - ), - /* harmony export */ - StoppingCriteria: () => ( - /* reexport safe */ - _generation_stopping_criteria_js__WEBPACK_IMPORTED_MODULE_11__.StoppingCriteria - ), - /* harmony export */ - StoppingCriteriaList: () => ( - /* reexport safe */ - _generation_stopping_criteria_js__WEBPACK_IMPORTED_MODULE_11__.StoppingCriteriaList - ), - /* harmony export */ - SummarizationPipeline: () => ( - /* reexport safe */ - _pipelines_js__WEBPACK_IMPORTED_MODULE_1__.SummarizationPipeline - ), - /* harmony export */ - Swin2SRForImageSuperResolution: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.Swin2SRForImageSuperResolution - ), - /* harmony export */ - Swin2SRImageProcessor: () => ( - /* reexport safe */ - _processors_js__WEBPACK_IMPORTED_MODULE_4__.Swin2SRImageProcessor - ), - /* harmony export */ - Swin2SRModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.Swin2SRModel - ), - /* harmony export */ - Swin2SRPreTrainedModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.Swin2SRPreTrainedModel - ), - /* harmony export */ - SwinForImageClassification: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.SwinForImageClassification - ), - /* harmony export */ - SwinModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.SwinModel - ), - /* harmony export */ - SwinPreTrainedModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.SwinPreTrainedModel - ), - /* harmony export */ - T5ForConditionalGeneration: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.T5ForConditionalGeneration - ), - /* harmony export */ - T5Model: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.T5Model - ), - /* harmony export */ - T5PreTrainedModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.T5PreTrainedModel - ), - /* harmony export */ - T5Tokenizer: () => ( - /* reexport safe */ - _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.T5Tokenizer - ), - /* harmony export */ - TableTransformerForObjectDetection: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.TableTransformerForObjectDetection - ), - /* harmony export */ - TableTransformerModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.TableTransformerModel - ), - /* harmony export */ - TableTransformerObjectDetectionOutput: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.TableTransformerObjectDetectionOutput - ), - /* harmony export */ - TableTransformerPreTrainedModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.TableTransformerPreTrainedModel - ), - /* harmony export */ - Tensor: () => ( - /* reexport safe */ - _utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.Tensor - ), - /* harmony export */ - Text2TextGenerationPipeline: () => ( - /* reexport safe */ - _pipelines_js__WEBPACK_IMPORTED_MODULE_1__.Text2TextGenerationPipeline - ), - /* harmony export */ - TextClassificationPipeline: () => ( - /* reexport safe */ - _pipelines_js__WEBPACK_IMPORTED_MODULE_1__.TextClassificationPipeline - ), - /* harmony export */ - TextGenerationPipeline: () => ( - /* reexport safe */ - _pipelines_js__WEBPACK_IMPORTED_MODULE_1__.TextGenerationPipeline - ), - /* harmony export */ - TextStreamer: () => ( - /* reexport safe */ - _generation_streamers_js__WEBPACK_IMPORTED_MODULE_10__.TextStreamer - ), - /* harmony export */ - TextToAudioPipeline: () => ( - /* reexport safe */ - _pipelines_js__WEBPACK_IMPORTED_MODULE_1__.TextToAudioPipeline - ), - /* harmony export */ - TokenClassificationPipeline: () => ( - /* reexport safe */ - _pipelines_js__WEBPACK_IMPORTED_MODULE_1__.TokenClassificationPipeline - ), - /* harmony export */ - TokenClassifierOutput: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.TokenClassifierOutput - ), - /* harmony export */ - TokenizerModel: () => ( - /* reexport safe */ - _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.TokenizerModel - ), - /* harmony export */ - TrOCRForCausalLM: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.TrOCRForCausalLM - ), - /* harmony export */ - TrOCRPreTrainedModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.TrOCRPreTrainedModel - ), - /* harmony export */ - TranslationPipeline: () => ( - /* reexport safe */ - _pipelines_js__WEBPACK_IMPORTED_MODULE_1__.TranslationPipeline - ), - /* harmony export */ - UniSpeechForCTC: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.UniSpeechForCTC - ), - /* harmony export */ - UniSpeechForSequenceClassification: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.UniSpeechForSequenceClassification - ), - /* harmony export */ - UniSpeechModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.UniSpeechModel - ), - /* harmony export */ - UniSpeechPreTrainedModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.UniSpeechPreTrainedModel - ), - /* harmony export */ - UniSpeechSatForAudioFrameClassification: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.UniSpeechSatForAudioFrameClassification - ), - /* harmony export */ - UniSpeechSatForCTC: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.UniSpeechSatForCTC - ), - /* harmony export */ - UniSpeechSatForSequenceClassification: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.UniSpeechSatForSequenceClassification - ), - /* harmony export */ - UniSpeechSatModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.UniSpeechSatModel - ), - /* harmony export */ - UniSpeechSatPreTrainedModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.UniSpeechSatPreTrainedModel - ), - /* harmony export */ - ViTFeatureExtractor: () => ( - /* reexport safe */ - _processors_js__WEBPACK_IMPORTED_MODULE_4__.ViTFeatureExtractor - ), - /* harmony export */ - ViTForImageClassification: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.ViTForImageClassification - ), - /* harmony export */ - ViTImageProcessor: () => ( - /* reexport safe */ - _processors_js__WEBPACK_IMPORTED_MODULE_4__.ViTImageProcessor - ), - /* harmony export */ - ViTMAEModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.ViTMAEModel - ), - /* harmony export */ - ViTMAEPreTrainedModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.ViTMAEPreTrainedModel - ), - /* harmony export */ - ViTMSNForImageClassification: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.ViTMSNForImageClassification - ), - /* harmony export */ - ViTMSNModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.ViTMSNModel - ), - /* harmony export */ - ViTMSNPreTrainedModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.ViTMSNPreTrainedModel - ), - /* harmony export */ - ViTModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.ViTModel - ), - /* harmony export */ - ViTPreTrainedModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.ViTPreTrainedModel - ), - /* harmony export */ - VisionEncoderDecoderModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.VisionEncoderDecoderModel - ), - /* harmony export */ - VitMatteForImageMatting: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.VitMatteForImageMatting - ), - /* harmony export */ - VitMatteImageProcessor: () => ( - /* reexport safe */ - _processors_js__WEBPACK_IMPORTED_MODULE_4__.VitMatteImageProcessor - ), - /* harmony export */ - VitMattePreTrainedModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.VitMattePreTrainedModel - ), - /* harmony export */ - VitsModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.VitsModel - ), - /* harmony export */ - VitsModelOutput: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.VitsModelOutput - ), - /* harmony export */ - VitsPreTrainedModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.VitsPreTrainedModel - ), - /* harmony export */ - VitsTokenizer: () => ( - /* reexport safe */ - _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.VitsTokenizer - ), - /* harmony export */ - Wav2Vec2BertForCTC: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.Wav2Vec2BertForCTC - ), - /* harmony export */ - Wav2Vec2BertForSequenceClassification: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.Wav2Vec2BertForSequenceClassification - ), - /* harmony export */ - Wav2Vec2BertModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.Wav2Vec2BertModel - ), - /* harmony export */ - Wav2Vec2BertPreTrainedModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.Wav2Vec2BertPreTrainedModel - ), - /* harmony export */ - Wav2Vec2CTCTokenizer: () => ( - /* reexport safe */ - _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.Wav2Vec2CTCTokenizer - ), - /* harmony export */ - Wav2Vec2FeatureExtractor: () => ( - /* reexport safe */ - _processors_js__WEBPACK_IMPORTED_MODULE_4__.Wav2Vec2FeatureExtractor - ), - /* harmony export */ - Wav2Vec2ForAudioFrameClassification: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.Wav2Vec2ForAudioFrameClassification - ), - /* harmony export */ - Wav2Vec2ForCTC: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.Wav2Vec2ForCTC - ), - /* harmony export */ - Wav2Vec2ForSequenceClassification: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.Wav2Vec2ForSequenceClassification - ), - /* harmony export */ - Wav2Vec2Model: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.Wav2Vec2Model - ), - /* harmony export */ - Wav2Vec2PreTrainedModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.Wav2Vec2PreTrainedModel - ), - /* harmony export */ - Wav2Vec2ProcessorWithLM: () => ( - /* reexport safe */ - _processors_js__WEBPACK_IMPORTED_MODULE_4__.Wav2Vec2ProcessorWithLM - ), - /* harmony export */ - WavLMForAudioFrameClassification: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.WavLMForAudioFrameClassification - ), - /* harmony export */ - WavLMForCTC: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.WavLMForCTC - ), - /* harmony export */ - WavLMForSequenceClassification: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.WavLMForSequenceClassification - ), - /* harmony export */ - WavLMForXVector: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.WavLMForXVector - ), - /* harmony export */ - WavLMModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.WavLMModel - ), - /* harmony export */ - WavLMPreTrainedModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.WavLMPreTrainedModel - ), - /* harmony export */ - WeSpeakerFeatureExtractor: () => ( - /* reexport safe */ - _processors_js__WEBPACK_IMPORTED_MODULE_4__.WeSpeakerFeatureExtractor - ), - /* harmony export */ - WeSpeakerResNetModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.WeSpeakerResNetModel - ), - /* harmony export */ - WeSpeakerResNetPreTrainedModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.WeSpeakerResNetPreTrainedModel - ), - /* harmony export */ - WhisperFeatureExtractor: () => ( - /* reexport safe */ - _processors_js__WEBPACK_IMPORTED_MODULE_4__.WhisperFeatureExtractor - ), - /* harmony export */ - WhisperForConditionalGeneration: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.WhisperForConditionalGeneration - ), - /* harmony export */ - WhisperModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.WhisperModel - ), - /* harmony export */ - WhisperPreTrainedModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.WhisperPreTrainedModel - ), - /* harmony export */ - WhisperProcessor: () => ( - /* reexport safe */ - _processors_js__WEBPACK_IMPORTED_MODULE_4__.WhisperProcessor - ), - /* harmony export */ - WhisperTextStreamer: () => ( - /* reexport safe */ - _generation_streamers_js__WEBPACK_IMPORTED_MODULE_10__.WhisperTextStreamer - ), - /* harmony export */ - WhisperTokenizer: () => ( - /* reexport safe */ - _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.WhisperTokenizer - ), - /* harmony export */ - XLMForQuestionAnswering: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.XLMForQuestionAnswering - ), - /* harmony export */ - XLMForSequenceClassification: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.XLMForSequenceClassification - ), - /* harmony export */ - XLMForTokenClassification: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.XLMForTokenClassification - ), - /* harmony export */ - XLMModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.XLMModel - ), - /* harmony export */ - XLMPreTrainedModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.XLMPreTrainedModel - ), - /* harmony export */ - XLMRobertaForMaskedLM: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.XLMRobertaForMaskedLM - ), - /* harmony export */ - XLMRobertaForQuestionAnswering: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.XLMRobertaForQuestionAnswering - ), - /* harmony export */ - XLMRobertaForSequenceClassification: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.XLMRobertaForSequenceClassification - ), - /* harmony export */ - XLMRobertaForTokenClassification: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.XLMRobertaForTokenClassification - ), - /* harmony export */ - XLMRobertaModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.XLMRobertaModel - ), - /* harmony export */ - XLMRobertaPreTrainedModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.XLMRobertaPreTrainedModel - ), - /* harmony export */ - XLMRobertaTokenizer: () => ( - /* reexport safe */ - _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.XLMRobertaTokenizer - ), - /* harmony export */ - XLMTokenizer: () => ( - /* reexport safe */ - _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.XLMTokenizer - ), - /* harmony export */ - XLMWithLMHeadModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.XLMWithLMHeadModel - ), - /* harmony export */ - XVectorOutput: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.XVectorOutput - ), - /* harmony export */ - YolosFeatureExtractor: () => ( - /* reexport safe */ - _processors_js__WEBPACK_IMPORTED_MODULE_4__.YolosFeatureExtractor - ), - /* harmony export */ - YolosForObjectDetection: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.YolosForObjectDetection - ), - /* harmony export */ - YolosModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.YolosModel - ), - /* harmony export */ - YolosObjectDetectionOutput: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.YolosObjectDetectionOutput - ), - /* harmony export */ - YolosPreTrainedModel: () => ( - /* reexport safe */ - _models_js__WEBPACK_IMPORTED_MODULE_2__.YolosPreTrainedModel - ), - /* harmony export */ - ZeroShotAudioClassificationPipeline: () => ( - /* reexport safe */ - _pipelines_js__WEBPACK_IMPORTED_MODULE_1__.ZeroShotAudioClassificationPipeline - ), - /* harmony export */ - ZeroShotClassificationPipeline: () => ( - /* reexport safe */ - _pipelines_js__WEBPACK_IMPORTED_MODULE_1__.ZeroShotClassificationPipeline - ), - /* harmony export */ - ZeroShotImageClassificationPipeline: () => ( - /* reexport safe */ - _pipelines_js__WEBPACK_IMPORTED_MODULE_1__.ZeroShotImageClassificationPipeline - ), - /* harmony export */ - ZeroShotObjectDetectionPipeline: () => ( - /* reexport safe */ - _pipelines_js__WEBPACK_IMPORTED_MODULE_1__.ZeroShotObjectDetectionPipeline - ), - /* harmony export */ - bankers_round: () => ( - /* reexport safe */ - _utils_maths_js__WEBPACK_IMPORTED_MODULE_9__.bankers_round - ), - /* harmony export */ - cat: () => ( - /* reexport safe */ - _utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.cat - ), - /* harmony export */ - cos_sim: () => ( - /* reexport safe */ - _utils_maths_js__WEBPACK_IMPORTED_MODULE_9__.cos_sim - ), - /* harmony export */ - dot: () => ( - /* reexport safe */ - _utils_maths_js__WEBPACK_IMPORTED_MODULE_9__.dot - ), - /* harmony export */ - dynamic_time_warping: () => ( - /* reexport safe */ - _utils_maths_js__WEBPACK_IMPORTED_MODULE_9__.dynamic_time_warping - ), - /* harmony export */ - env: () => ( - /* reexport safe */ - _env_js__WEBPACK_IMPORTED_MODULE_0__.env - ), - /* harmony export */ - full: () => ( - /* reexport safe */ - _utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.full - ), - /* harmony export */ - full_like: () => ( - /* reexport safe */ - _utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.full_like - ), - /* harmony export */ - getKeyValueShapes: () => ( - /* reexport safe */ - _configs_js__WEBPACK_IMPORTED_MODULE_5__.getKeyValueShapes - ), - /* harmony export */ - hamming: () => ( - /* reexport safe */ - _utils_audio_js__WEBPACK_IMPORTED_MODULE_6__.hamming - ), - /* harmony export */ - hanning: () => ( - /* reexport safe */ - _utils_audio_js__WEBPACK_IMPORTED_MODULE_6__.hanning - ), - /* harmony export */ - interpolate: () => ( - /* reexport safe */ - _utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.interpolate - ), - /* harmony export */ - interpolate_4d: () => ( - /* reexport safe */ - _utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.interpolate_4d - ), - /* harmony export */ - interpolate_data: () => ( - /* reexport safe */ - _utils_maths_js__WEBPACK_IMPORTED_MODULE_9__.interpolate_data - ), - /* harmony export */ - is_chinese_char: () => ( - /* reexport safe */ - _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.is_chinese_char - ), - /* harmony export */ - layer_norm: () => ( - /* reexport safe */ - _utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.layer_norm - ), - /* harmony export */ - log_softmax: () => ( - /* reexport safe */ - _utils_maths_js__WEBPACK_IMPORTED_MODULE_9__.log_softmax - ), - /* harmony export */ - magnitude: () => ( - /* reexport safe */ - _utils_maths_js__WEBPACK_IMPORTED_MODULE_9__.magnitude - ), - /* harmony export */ - matmul: () => ( - /* reexport safe */ - _utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.matmul - ), - /* harmony export */ - max: () => ( - /* reexport safe */ - _utils_maths_js__WEBPACK_IMPORTED_MODULE_9__.max - ), - /* harmony export */ - mean: () => ( - /* reexport safe */ - _utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.mean - ), - /* harmony export */ - mean_pooling: () => ( - /* reexport safe */ - _utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.mean_pooling - ), - /* harmony export */ - medianFilter: () => ( - /* reexport safe */ - _utils_maths_js__WEBPACK_IMPORTED_MODULE_9__.medianFilter - ), - /* harmony export */ - mel_filter_bank: () => ( - /* reexport safe */ - _utils_audio_js__WEBPACK_IMPORTED_MODULE_6__.mel_filter_bank - ), - /* harmony export */ - min: () => ( - /* reexport safe */ - _utils_maths_js__WEBPACK_IMPORTED_MODULE_9__.min - ), - /* harmony export */ - ones: () => ( - /* reexport safe */ - _utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.ones - ), - /* harmony export */ - ones_like: () => ( - /* reexport safe */ - _utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.ones_like - ), - /* harmony export */ - permute: () => ( - /* reexport safe */ - _utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.permute - ), - /* harmony export */ - permute_data: () => ( - /* reexport safe */ - _utils_maths_js__WEBPACK_IMPORTED_MODULE_9__.permute_data - ), - /* harmony export */ - pipeline: () => ( - /* reexport safe */ - _pipelines_js__WEBPACK_IMPORTED_MODULE_1__.pipeline - ), - /* harmony export */ - quantize_embeddings: () => ( - /* reexport safe */ - _utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.quantize_embeddings - ), - /* harmony export */ - read_audio: () => ( - /* reexport safe */ - _utils_audio_js__WEBPACK_IMPORTED_MODULE_6__.read_audio - ), - /* harmony export */ - rfft: () => ( - /* reexport safe */ - _utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.rfft - ), - /* harmony export */ - round: () => ( - /* reexport safe */ - _utils_maths_js__WEBPACK_IMPORTED_MODULE_9__.round - ), - /* harmony export */ - softmax: () => ( - /* reexport safe */ - _utils_maths_js__WEBPACK_IMPORTED_MODULE_9__.softmax - ), - /* harmony export */ - spectrogram: () => ( - /* reexport safe */ - _utils_audio_js__WEBPACK_IMPORTED_MODULE_6__.spectrogram - ), - /* harmony export */ - stack: () => ( - /* reexport safe */ - _utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.stack - ), - /* harmony export */ - std_mean: () => ( - /* reexport safe */ - _utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.std_mean - ), - /* harmony export */ - topk: () => ( - /* reexport safe */ - _utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.topk - ), - /* harmony export */ - window_function: () => ( - /* reexport safe */ - _utils_audio_js__WEBPACK_IMPORTED_MODULE_6__.window_function - ), - /* harmony export */ - zeros: () => ( - /* reexport safe */ - _utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.zeros - ), - /* harmony export */ - zeros_like: () => ( - /* reexport safe */ - _utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.zeros_like - ) - /* harmony export */ - }); - var _env_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__( - /*! ./env.js */ - "./src/env.js" - ); - var _pipelines_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__( - /*! ./pipelines.js */ - "./src/pipelines.js" - ); - var _models_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__( - /*! ./models.js */ - "./src/models.js" - ); - var _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__( - /*! ./tokenizers.js */ - "./src/tokenizers.js" - ); - var _processors_js__WEBPACK_IMPORTED_MODULE_4__ = __webpack_require__( - /*! ./processors.js */ - "./src/processors.js" - ); - var _configs_js__WEBPACK_IMPORTED_MODULE_5__ = __webpack_require__( - /*! ./configs.js */ - "./src/configs.js" - ); - var _utils_audio_js__WEBPACK_IMPORTED_MODULE_6__ = __webpack_require__( - /*! ./utils/audio.js */ - "./src/utils/audio.js" - ); - var _utils_image_js__WEBPACK_IMPORTED_MODULE_7__ = __webpack_require__( - /*! ./utils/image.js */ - "./src/utils/image.js" - ); - var _utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__ = __webpack_require__( - /*! ./utils/tensor.js */ - "./src/utils/tensor.js" - ); - var _utils_maths_js__WEBPACK_IMPORTED_MODULE_9__ = __webpack_require__( - /*! ./utils/maths.js */ - "./src/utils/maths.js" - ); - var _generation_streamers_js__WEBPACK_IMPORTED_MODULE_10__ = __webpack_require__( - /*! ./generation/streamers.js */ - "./src/generation/streamers.js" - ); - var _generation_stopping_criteria_js__WEBPACK_IMPORTED_MODULE_11__ = __webpack_require__( - /*! ./generation/stopping_criteria.js */ - "./src/generation/stopping_criteria.js" - ); - var __webpack_exports__ASTFeatureExtractor = __webpack_exports__.ASTFeatureExtractor; - var __webpack_exports__ASTForAudioClassification = __webpack_exports__.ASTForAudioClassification; - var __webpack_exports__ASTModel = __webpack_exports__.ASTModel; - var __webpack_exports__ASTPreTrainedModel = __webpack_exports__.ASTPreTrainedModel; - var __webpack_exports__AlbertForMaskedLM = __webpack_exports__.AlbertForMaskedLM; - var __webpack_exports__AlbertForQuestionAnswering = __webpack_exports__.AlbertForQuestionAnswering; - var __webpack_exports__AlbertForSequenceClassification = __webpack_exports__.AlbertForSequenceClassification; - var __webpack_exports__AlbertModel = __webpack_exports__.AlbertModel; - var __webpack_exports__AlbertPreTrainedModel = __webpack_exports__.AlbertPreTrainedModel; - var __webpack_exports__AlbertTokenizer = __webpack_exports__.AlbertTokenizer; - var __webpack_exports__AudioClassificationPipeline = __webpack_exports__.AudioClassificationPipeline; - var __webpack_exports__AutoConfig = __webpack_exports__.AutoConfig; - var __webpack_exports__AutoModel = __webpack_exports__.AutoModel; - var __webpack_exports__AutoModelForAudioClassification = __webpack_exports__.AutoModelForAudioClassification; - var __webpack_exports__AutoModelForAudioFrameClassification = __webpack_exports__.AutoModelForAudioFrameClassification; - var __webpack_exports__AutoModelForCTC = __webpack_exports__.AutoModelForCTC; - var __webpack_exports__AutoModelForCausalLM = __webpack_exports__.AutoModelForCausalLM; - var __webpack_exports__AutoModelForDepthEstimation = __webpack_exports__.AutoModelForDepthEstimation; - var __webpack_exports__AutoModelForDocumentQuestionAnswering = __webpack_exports__.AutoModelForDocumentQuestionAnswering; - var __webpack_exports__AutoModelForImageClassification = __webpack_exports__.AutoModelForImageClassification; - var __webpack_exports__AutoModelForImageFeatureExtraction = __webpack_exports__.AutoModelForImageFeatureExtraction; - var __webpack_exports__AutoModelForImageMatting = __webpack_exports__.AutoModelForImageMatting; - var __webpack_exports__AutoModelForImageSegmentation = __webpack_exports__.AutoModelForImageSegmentation; - var __webpack_exports__AutoModelForImageToImage = __webpack_exports__.AutoModelForImageToImage; - var __webpack_exports__AutoModelForMaskGeneration = __webpack_exports__.AutoModelForMaskGeneration; - var __webpack_exports__AutoModelForMaskedLM = __webpack_exports__.AutoModelForMaskedLM; - var __webpack_exports__AutoModelForNormalEstimation = __webpack_exports__.AutoModelForNormalEstimation; - var __webpack_exports__AutoModelForObjectDetection = __webpack_exports__.AutoModelForObjectDetection; - var __webpack_exports__AutoModelForQuestionAnswering = __webpack_exports__.AutoModelForQuestionAnswering; - var __webpack_exports__AutoModelForSemanticSegmentation = __webpack_exports__.AutoModelForSemanticSegmentation; - var __webpack_exports__AutoModelForSeq2SeqLM = __webpack_exports__.AutoModelForSeq2SeqLM; - var __webpack_exports__AutoModelForSequenceClassification = __webpack_exports__.AutoModelForSequenceClassification; - var __webpack_exports__AutoModelForSpeechSeq2Seq = __webpack_exports__.AutoModelForSpeechSeq2Seq; - var __webpack_exports__AutoModelForTextToSpectrogram = __webpack_exports__.AutoModelForTextToSpectrogram; - var __webpack_exports__AutoModelForTextToWaveform = __webpack_exports__.AutoModelForTextToWaveform; - var __webpack_exports__AutoModelForTokenClassification = __webpack_exports__.AutoModelForTokenClassification; - var __webpack_exports__AutoModelForUniversalSegmentation = __webpack_exports__.AutoModelForUniversalSegmentation; - var __webpack_exports__AutoModelForVision2Seq = __webpack_exports__.AutoModelForVision2Seq; - var __webpack_exports__AutoModelForXVector = __webpack_exports__.AutoModelForXVector; - var __webpack_exports__AutoModelForZeroShotObjectDetection = __webpack_exports__.AutoModelForZeroShotObjectDetection; - var __webpack_exports__AutoProcessor = __webpack_exports__.AutoProcessor; - var __webpack_exports__AutoTokenizer = __webpack_exports__.AutoTokenizer; - var __webpack_exports__AutomaticSpeechRecognitionPipeline = __webpack_exports__.AutomaticSpeechRecognitionPipeline; - var __webpack_exports__BartForConditionalGeneration = __webpack_exports__.BartForConditionalGeneration; - var __webpack_exports__BartForSequenceClassification = __webpack_exports__.BartForSequenceClassification; - var __webpack_exports__BartModel = __webpack_exports__.BartModel; - var __webpack_exports__BartPretrainedModel = __webpack_exports__.BartPretrainedModel; - var __webpack_exports__BartTokenizer = __webpack_exports__.BartTokenizer; - var __webpack_exports__BaseModelOutput = __webpack_exports__.BaseModelOutput; - var __webpack_exports__BaseStreamer = __webpack_exports__.BaseStreamer; - var __webpack_exports__BeitFeatureExtractor = __webpack_exports__.BeitFeatureExtractor; - var __webpack_exports__BeitForImageClassification = __webpack_exports__.BeitForImageClassification; - var __webpack_exports__BeitModel = __webpack_exports__.BeitModel; - var __webpack_exports__BeitPreTrainedModel = __webpack_exports__.BeitPreTrainedModel; - var __webpack_exports__BertForMaskedLM = __webpack_exports__.BertForMaskedLM; - var __webpack_exports__BertForQuestionAnswering = __webpack_exports__.BertForQuestionAnswering; - var __webpack_exports__BertForSequenceClassification = __webpack_exports__.BertForSequenceClassification; - var __webpack_exports__BertForTokenClassification = __webpack_exports__.BertForTokenClassification; - var __webpack_exports__BertModel = __webpack_exports__.BertModel; - var __webpack_exports__BertPreTrainedModel = __webpack_exports__.BertPreTrainedModel; - var __webpack_exports__BertTokenizer = __webpack_exports__.BertTokenizer; - var __webpack_exports__BitImageProcessor = __webpack_exports__.BitImageProcessor; - var __webpack_exports__BlenderbotForConditionalGeneration = __webpack_exports__.BlenderbotForConditionalGeneration; - var __webpack_exports__BlenderbotModel = __webpack_exports__.BlenderbotModel; - var __webpack_exports__BlenderbotPreTrainedModel = __webpack_exports__.BlenderbotPreTrainedModel; - var __webpack_exports__BlenderbotSmallForConditionalGeneration = __webpack_exports__.BlenderbotSmallForConditionalGeneration; - var __webpack_exports__BlenderbotSmallModel = __webpack_exports__.BlenderbotSmallModel; - var __webpack_exports__BlenderbotSmallPreTrainedModel = __webpack_exports__.BlenderbotSmallPreTrainedModel; - var __webpack_exports__BlenderbotSmallTokenizer = __webpack_exports__.BlenderbotSmallTokenizer; - var __webpack_exports__BlenderbotTokenizer = __webpack_exports__.BlenderbotTokenizer; - var __webpack_exports__BloomForCausalLM = __webpack_exports__.BloomForCausalLM; - var __webpack_exports__BloomModel = __webpack_exports__.BloomModel; - var __webpack_exports__BloomPreTrainedModel = __webpack_exports__.BloomPreTrainedModel; - var __webpack_exports__BloomTokenizer = __webpack_exports__.BloomTokenizer; - var __webpack_exports__CLIPFeatureExtractor = __webpack_exports__.CLIPFeatureExtractor; - var __webpack_exports__CLIPImageProcessor = __webpack_exports__.CLIPImageProcessor; - var __webpack_exports__CLIPModel = __webpack_exports__.CLIPModel; - var __webpack_exports__CLIPPreTrainedModel = __webpack_exports__.CLIPPreTrainedModel; - var __webpack_exports__CLIPSegForImageSegmentation = __webpack_exports__.CLIPSegForImageSegmentation; - var __webpack_exports__CLIPSegModel = __webpack_exports__.CLIPSegModel; - var __webpack_exports__CLIPSegPreTrainedModel = __webpack_exports__.CLIPSegPreTrainedModel; - var __webpack_exports__CLIPTextModel = __webpack_exports__.CLIPTextModel; - var __webpack_exports__CLIPTextModelWithProjection = __webpack_exports__.CLIPTextModelWithProjection; - var __webpack_exports__CLIPTokenizer = __webpack_exports__.CLIPTokenizer; - var __webpack_exports__CLIPVisionModel = __webpack_exports__.CLIPVisionModel; - var __webpack_exports__CLIPVisionModelWithProjection = __webpack_exports__.CLIPVisionModelWithProjection; - var __webpack_exports__CamembertForMaskedLM = __webpack_exports__.CamembertForMaskedLM; - var __webpack_exports__CamembertForQuestionAnswering = __webpack_exports__.CamembertForQuestionAnswering; - var __webpack_exports__CamembertForSequenceClassification = __webpack_exports__.CamembertForSequenceClassification; - var __webpack_exports__CamembertForTokenClassification = __webpack_exports__.CamembertForTokenClassification; - var __webpack_exports__CamembertModel = __webpack_exports__.CamembertModel; - var __webpack_exports__CamembertPreTrainedModel = __webpack_exports__.CamembertPreTrainedModel; - var __webpack_exports__CamembertTokenizer = __webpack_exports__.CamembertTokenizer; - var __webpack_exports__CausalLMOutput = __webpack_exports__.CausalLMOutput; - var __webpack_exports__CausalLMOutputWithPast = __webpack_exports__.CausalLMOutputWithPast; - var __webpack_exports__ChineseCLIPFeatureExtractor = __webpack_exports__.ChineseCLIPFeatureExtractor; - var __webpack_exports__ChineseCLIPModel = __webpack_exports__.ChineseCLIPModel; - var __webpack_exports__ChineseCLIPPreTrainedModel = __webpack_exports__.ChineseCLIPPreTrainedModel; - var __webpack_exports__ClapAudioModelWithProjection = __webpack_exports__.ClapAudioModelWithProjection; - var __webpack_exports__ClapFeatureExtractor = __webpack_exports__.ClapFeatureExtractor; - var __webpack_exports__ClapModel = __webpack_exports__.ClapModel; - var __webpack_exports__ClapPreTrainedModel = __webpack_exports__.ClapPreTrainedModel; - var __webpack_exports__ClapTextModelWithProjection = __webpack_exports__.ClapTextModelWithProjection; - var __webpack_exports__CodeGenForCausalLM = __webpack_exports__.CodeGenForCausalLM; - var __webpack_exports__CodeGenModel = __webpack_exports__.CodeGenModel; - var __webpack_exports__CodeGenPreTrainedModel = __webpack_exports__.CodeGenPreTrainedModel; - var __webpack_exports__CodeGenTokenizer = __webpack_exports__.CodeGenTokenizer; - var __webpack_exports__CodeLlamaTokenizer = __webpack_exports__.CodeLlamaTokenizer; - var __webpack_exports__CohereForCausalLM = __webpack_exports__.CohereForCausalLM; - var __webpack_exports__CohereModel = __webpack_exports__.CohereModel; - var __webpack_exports__CoherePreTrainedModel = __webpack_exports__.CoherePreTrainedModel; - var __webpack_exports__CohereTokenizer = __webpack_exports__.CohereTokenizer; - var __webpack_exports__ConvBertForMaskedLM = __webpack_exports__.ConvBertForMaskedLM; - var __webpack_exports__ConvBertForQuestionAnswering = __webpack_exports__.ConvBertForQuestionAnswering; - var __webpack_exports__ConvBertForSequenceClassification = __webpack_exports__.ConvBertForSequenceClassification; - var __webpack_exports__ConvBertForTokenClassification = __webpack_exports__.ConvBertForTokenClassification; - var __webpack_exports__ConvBertModel = __webpack_exports__.ConvBertModel; - var __webpack_exports__ConvBertPreTrainedModel = __webpack_exports__.ConvBertPreTrainedModel; - var __webpack_exports__ConvBertTokenizer = __webpack_exports__.ConvBertTokenizer; - var __webpack_exports__ConvNextFeatureExtractor = __webpack_exports__.ConvNextFeatureExtractor; - var __webpack_exports__ConvNextForImageClassification = __webpack_exports__.ConvNextForImageClassification; - var __webpack_exports__ConvNextImageProcessor = __webpack_exports__.ConvNextImageProcessor; - var __webpack_exports__ConvNextModel = __webpack_exports__.ConvNextModel; - var __webpack_exports__ConvNextPreTrainedModel = __webpack_exports__.ConvNextPreTrainedModel; - var __webpack_exports__ConvNextV2ForImageClassification = __webpack_exports__.ConvNextV2ForImageClassification; - var __webpack_exports__ConvNextV2Model = __webpack_exports__.ConvNextV2Model; - var __webpack_exports__ConvNextV2PreTrainedModel = __webpack_exports__.ConvNextV2PreTrainedModel; - var __webpack_exports__DPTFeatureExtractor = __webpack_exports__.DPTFeatureExtractor; - var __webpack_exports__DPTForDepthEstimation = __webpack_exports__.DPTForDepthEstimation; - var __webpack_exports__DPTImageProcessor = __webpack_exports__.DPTImageProcessor; - var __webpack_exports__DPTModel = __webpack_exports__.DPTModel; - var __webpack_exports__DPTPreTrainedModel = __webpack_exports__.DPTPreTrainedModel; - var __webpack_exports__DebertaForMaskedLM = __webpack_exports__.DebertaForMaskedLM; - var __webpack_exports__DebertaForQuestionAnswering = __webpack_exports__.DebertaForQuestionAnswering; - var __webpack_exports__DebertaForSequenceClassification = __webpack_exports__.DebertaForSequenceClassification; - var __webpack_exports__DebertaForTokenClassification = __webpack_exports__.DebertaForTokenClassification; - var __webpack_exports__DebertaModel = __webpack_exports__.DebertaModel; - var __webpack_exports__DebertaPreTrainedModel = __webpack_exports__.DebertaPreTrainedModel; - var __webpack_exports__DebertaTokenizer = __webpack_exports__.DebertaTokenizer; - var __webpack_exports__DebertaV2ForMaskedLM = __webpack_exports__.DebertaV2ForMaskedLM; - var __webpack_exports__DebertaV2ForQuestionAnswering = __webpack_exports__.DebertaV2ForQuestionAnswering; - var __webpack_exports__DebertaV2ForSequenceClassification = __webpack_exports__.DebertaV2ForSequenceClassification; - var __webpack_exports__DebertaV2ForTokenClassification = __webpack_exports__.DebertaV2ForTokenClassification; - var __webpack_exports__DebertaV2Model = __webpack_exports__.DebertaV2Model; - var __webpack_exports__DebertaV2PreTrainedModel = __webpack_exports__.DebertaV2PreTrainedModel; - var __webpack_exports__DebertaV2Tokenizer = __webpack_exports__.DebertaV2Tokenizer; - var __webpack_exports__DecisionTransformerModel = __webpack_exports__.DecisionTransformerModel; - var __webpack_exports__DecisionTransformerPreTrainedModel = __webpack_exports__.DecisionTransformerPreTrainedModel; - var __webpack_exports__DeiTFeatureExtractor = __webpack_exports__.DeiTFeatureExtractor; - var __webpack_exports__DeiTForImageClassification = __webpack_exports__.DeiTForImageClassification; - var __webpack_exports__DeiTModel = __webpack_exports__.DeiTModel; - var __webpack_exports__DeiTPreTrainedModel = __webpack_exports__.DeiTPreTrainedModel; - var __webpack_exports__DepthAnythingForDepthEstimation = __webpack_exports__.DepthAnythingForDepthEstimation; - var __webpack_exports__DepthAnythingPreTrainedModel = __webpack_exports__.DepthAnythingPreTrainedModel; - var __webpack_exports__DepthEstimationPipeline = __webpack_exports__.DepthEstimationPipeline; - var __webpack_exports__DepthProForDepthEstimation = __webpack_exports__.DepthProForDepthEstimation; - var __webpack_exports__DepthProPreTrainedModel = __webpack_exports__.DepthProPreTrainedModel; - var __webpack_exports__DetrFeatureExtractor = __webpack_exports__.DetrFeatureExtractor; - var __webpack_exports__DetrForObjectDetection = __webpack_exports__.DetrForObjectDetection; - var __webpack_exports__DetrForSegmentation = __webpack_exports__.DetrForSegmentation; - var __webpack_exports__DetrModel = __webpack_exports__.DetrModel; - var __webpack_exports__DetrObjectDetectionOutput = __webpack_exports__.DetrObjectDetectionOutput; - var __webpack_exports__DetrPreTrainedModel = __webpack_exports__.DetrPreTrainedModel; - var __webpack_exports__DetrSegmentationOutput = __webpack_exports__.DetrSegmentationOutput; - var __webpack_exports__Dinov2ForImageClassification = __webpack_exports__.Dinov2ForImageClassification; - var __webpack_exports__Dinov2Model = __webpack_exports__.Dinov2Model; - var __webpack_exports__Dinov2PreTrainedModel = __webpack_exports__.Dinov2PreTrainedModel; - var __webpack_exports__DistilBertForMaskedLM = __webpack_exports__.DistilBertForMaskedLM; - var __webpack_exports__DistilBertForQuestionAnswering = __webpack_exports__.DistilBertForQuestionAnswering; - var __webpack_exports__DistilBertForSequenceClassification = __webpack_exports__.DistilBertForSequenceClassification; - var __webpack_exports__DistilBertForTokenClassification = __webpack_exports__.DistilBertForTokenClassification; - var __webpack_exports__DistilBertModel = __webpack_exports__.DistilBertModel; - var __webpack_exports__DistilBertPreTrainedModel = __webpack_exports__.DistilBertPreTrainedModel; - var __webpack_exports__DistilBertTokenizer = __webpack_exports__.DistilBertTokenizer; - var __webpack_exports__DocumentQuestionAnsweringPipeline = __webpack_exports__.DocumentQuestionAnsweringPipeline; - var __webpack_exports__DonutFeatureExtractor = __webpack_exports__.DonutFeatureExtractor; - var __webpack_exports__DonutSwinModel = __webpack_exports__.DonutSwinModel; - var __webpack_exports__DonutSwinPreTrainedModel = __webpack_exports__.DonutSwinPreTrainedModel; - var __webpack_exports__EfficientNetForImageClassification = __webpack_exports__.EfficientNetForImageClassification; - var __webpack_exports__EfficientNetImageProcessor = __webpack_exports__.EfficientNetImageProcessor; - var __webpack_exports__EfficientNetModel = __webpack_exports__.EfficientNetModel; - var __webpack_exports__EfficientNetPreTrainedModel = __webpack_exports__.EfficientNetPreTrainedModel; - var __webpack_exports__ElectraForMaskedLM = __webpack_exports__.ElectraForMaskedLM; - var __webpack_exports__ElectraForQuestionAnswering = __webpack_exports__.ElectraForQuestionAnswering; - var __webpack_exports__ElectraForSequenceClassification = __webpack_exports__.ElectraForSequenceClassification; - var __webpack_exports__ElectraForTokenClassification = __webpack_exports__.ElectraForTokenClassification; - var __webpack_exports__ElectraModel = __webpack_exports__.ElectraModel; - var __webpack_exports__ElectraPreTrainedModel = __webpack_exports__.ElectraPreTrainedModel; - var __webpack_exports__ElectraTokenizer = __webpack_exports__.ElectraTokenizer; - var __webpack_exports__EosTokenCriteria = __webpack_exports__.EosTokenCriteria; - var __webpack_exports__EsmForMaskedLM = __webpack_exports__.EsmForMaskedLM; - var __webpack_exports__EsmForSequenceClassification = __webpack_exports__.EsmForSequenceClassification; - var __webpack_exports__EsmForTokenClassification = __webpack_exports__.EsmForTokenClassification; - var __webpack_exports__EsmModel = __webpack_exports__.EsmModel; - var __webpack_exports__EsmPreTrainedModel = __webpack_exports__.EsmPreTrainedModel; - var __webpack_exports__EsmTokenizer = __webpack_exports__.EsmTokenizer; - var __webpack_exports__FFT = __webpack_exports__.FFT; - var __webpack_exports__FalconForCausalLM = __webpack_exports__.FalconForCausalLM; - var __webpack_exports__FalconModel = __webpack_exports__.FalconModel; - var __webpack_exports__FalconPreTrainedModel = __webpack_exports__.FalconPreTrainedModel; - var __webpack_exports__FalconTokenizer = __webpack_exports__.FalconTokenizer; - var __webpack_exports__FastViTForImageClassification = __webpack_exports__.FastViTForImageClassification; - var __webpack_exports__FastViTModel = __webpack_exports__.FastViTModel; - var __webpack_exports__FastViTPreTrainedModel = __webpack_exports__.FastViTPreTrainedModel; - var __webpack_exports__FeatureExtractionPipeline = __webpack_exports__.FeatureExtractionPipeline; - var __webpack_exports__FeatureExtractor = __webpack_exports__.FeatureExtractor; - var __webpack_exports__FillMaskPipeline = __webpack_exports__.FillMaskPipeline; - var __webpack_exports__Florence2ForConditionalGeneration = __webpack_exports__.Florence2ForConditionalGeneration; - var __webpack_exports__Florence2PreTrainedModel = __webpack_exports__.Florence2PreTrainedModel; - var __webpack_exports__Florence2Processor = __webpack_exports__.Florence2Processor; - var __webpack_exports__GLPNFeatureExtractor = __webpack_exports__.GLPNFeatureExtractor; - var __webpack_exports__GLPNForDepthEstimation = __webpack_exports__.GLPNForDepthEstimation; - var __webpack_exports__GLPNModel = __webpack_exports__.GLPNModel; - var __webpack_exports__GLPNPreTrainedModel = __webpack_exports__.GLPNPreTrainedModel; - var __webpack_exports__GPT2LMHeadModel = __webpack_exports__.GPT2LMHeadModel; - var __webpack_exports__GPT2Model = __webpack_exports__.GPT2Model; - var __webpack_exports__GPT2PreTrainedModel = __webpack_exports__.GPT2PreTrainedModel; - var __webpack_exports__GPT2Tokenizer = __webpack_exports__.GPT2Tokenizer; - var __webpack_exports__GPTBigCodeForCausalLM = __webpack_exports__.GPTBigCodeForCausalLM; - var __webpack_exports__GPTBigCodeModel = __webpack_exports__.GPTBigCodeModel; - var __webpack_exports__GPTBigCodePreTrainedModel = __webpack_exports__.GPTBigCodePreTrainedModel; - var __webpack_exports__GPTJForCausalLM = __webpack_exports__.GPTJForCausalLM; - var __webpack_exports__GPTJModel = __webpack_exports__.GPTJModel; - var __webpack_exports__GPTJPreTrainedModel = __webpack_exports__.GPTJPreTrainedModel; - var __webpack_exports__GPTNeoForCausalLM = __webpack_exports__.GPTNeoForCausalLM; - var __webpack_exports__GPTNeoModel = __webpack_exports__.GPTNeoModel; - var __webpack_exports__GPTNeoPreTrainedModel = __webpack_exports__.GPTNeoPreTrainedModel; - var __webpack_exports__GPTNeoXForCausalLM = __webpack_exports__.GPTNeoXForCausalLM; - var __webpack_exports__GPTNeoXModel = __webpack_exports__.GPTNeoXModel; - var __webpack_exports__GPTNeoXPreTrainedModel = __webpack_exports__.GPTNeoXPreTrainedModel; - var __webpack_exports__GPTNeoXTokenizer = __webpack_exports__.GPTNeoXTokenizer; - var __webpack_exports__Gemma2ForCausalLM = __webpack_exports__.Gemma2ForCausalLM; - var __webpack_exports__Gemma2Model = __webpack_exports__.Gemma2Model; - var __webpack_exports__Gemma2PreTrainedModel = __webpack_exports__.Gemma2PreTrainedModel; - var __webpack_exports__GemmaForCausalLM = __webpack_exports__.GemmaForCausalLM; - var __webpack_exports__GemmaModel = __webpack_exports__.GemmaModel; - var __webpack_exports__GemmaPreTrainedModel = __webpack_exports__.GemmaPreTrainedModel; - var __webpack_exports__GemmaTokenizer = __webpack_exports__.GemmaTokenizer; - var __webpack_exports__GraniteForCausalLM = __webpack_exports__.GraniteForCausalLM; - var __webpack_exports__GraniteModel = __webpack_exports__.GraniteModel; - var __webpack_exports__GranitePreTrainedModel = __webpack_exports__.GranitePreTrainedModel; - var __webpack_exports__Grok1Tokenizer = __webpack_exports__.Grok1Tokenizer; - var __webpack_exports__GroupViTModel = __webpack_exports__.GroupViTModel; - var __webpack_exports__GroupViTPreTrainedModel = __webpack_exports__.GroupViTPreTrainedModel; - var __webpack_exports__HerbertTokenizer = __webpack_exports__.HerbertTokenizer; - var __webpack_exports__HieraForImageClassification = __webpack_exports__.HieraForImageClassification; - var __webpack_exports__HieraModel = __webpack_exports__.HieraModel; - var __webpack_exports__HieraPreTrainedModel = __webpack_exports__.HieraPreTrainedModel; - var __webpack_exports__HubertForCTC = __webpack_exports__.HubertForCTC; - var __webpack_exports__HubertForSequenceClassification = __webpack_exports__.HubertForSequenceClassification; - var __webpack_exports__HubertModel = __webpack_exports__.HubertModel; - var __webpack_exports__HubertPreTrainedModel = __webpack_exports__.HubertPreTrainedModel; - var __webpack_exports__ImageClassificationPipeline = __webpack_exports__.ImageClassificationPipeline; - var __webpack_exports__ImageFeatureExtractionPipeline = __webpack_exports__.ImageFeatureExtractionPipeline; - var __webpack_exports__ImageFeatureExtractor = __webpack_exports__.ImageFeatureExtractor; - var __webpack_exports__ImageMattingOutput = __webpack_exports__.ImageMattingOutput; - var __webpack_exports__ImageSegmentationPipeline = __webpack_exports__.ImageSegmentationPipeline; - var __webpack_exports__ImageToImagePipeline = __webpack_exports__.ImageToImagePipeline; - var __webpack_exports__ImageToTextPipeline = __webpack_exports__.ImageToTextPipeline; - var __webpack_exports__InterruptableStoppingCriteria = __webpack_exports__.InterruptableStoppingCriteria; - var __webpack_exports__JAISLMHeadModel = __webpack_exports__.JAISLMHeadModel; - var __webpack_exports__JAISModel = __webpack_exports__.JAISModel; - var __webpack_exports__JAISPreTrainedModel = __webpack_exports__.JAISPreTrainedModel; - var __webpack_exports__LlamaForCausalLM = __webpack_exports__.LlamaForCausalLM; - var __webpack_exports__LlamaModel = __webpack_exports__.LlamaModel; - var __webpack_exports__LlamaPreTrainedModel = __webpack_exports__.LlamaPreTrainedModel; - var __webpack_exports__LlamaTokenizer = __webpack_exports__.LlamaTokenizer; - var __webpack_exports__LlavaForConditionalGeneration = __webpack_exports__.LlavaForConditionalGeneration; - var __webpack_exports__LlavaPreTrainedModel = __webpack_exports__.LlavaPreTrainedModel; - var __webpack_exports__LongT5ForConditionalGeneration = __webpack_exports__.LongT5ForConditionalGeneration; - var __webpack_exports__LongT5Model = __webpack_exports__.LongT5Model; - var __webpack_exports__LongT5PreTrainedModel = __webpack_exports__.LongT5PreTrainedModel; - var __webpack_exports__M2M100ForConditionalGeneration = __webpack_exports__.M2M100ForConditionalGeneration; - var __webpack_exports__M2M100Model = __webpack_exports__.M2M100Model; - var __webpack_exports__M2M100PreTrainedModel = __webpack_exports__.M2M100PreTrainedModel; - var __webpack_exports__M2M100Tokenizer = __webpack_exports__.M2M100Tokenizer; - var __webpack_exports__MBart50Tokenizer = __webpack_exports__.MBart50Tokenizer; - var __webpack_exports__MBartForCausalLM = __webpack_exports__.MBartForCausalLM; - var __webpack_exports__MBartForConditionalGeneration = __webpack_exports__.MBartForConditionalGeneration; - var __webpack_exports__MBartForSequenceClassification = __webpack_exports__.MBartForSequenceClassification; - var __webpack_exports__MBartModel = __webpack_exports__.MBartModel; - var __webpack_exports__MBartPreTrainedModel = __webpack_exports__.MBartPreTrainedModel; - var __webpack_exports__MBartTokenizer = __webpack_exports__.MBartTokenizer; - var __webpack_exports__MPNetForMaskedLM = __webpack_exports__.MPNetForMaskedLM; - var __webpack_exports__MPNetForQuestionAnswering = __webpack_exports__.MPNetForQuestionAnswering; - var __webpack_exports__MPNetForSequenceClassification = __webpack_exports__.MPNetForSequenceClassification; - var __webpack_exports__MPNetForTokenClassification = __webpack_exports__.MPNetForTokenClassification; - var __webpack_exports__MPNetModel = __webpack_exports__.MPNetModel; - var __webpack_exports__MPNetPreTrainedModel = __webpack_exports__.MPNetPreTrainedModel; - var __webpack_exports__MPNetTokenizer = __webpack_exports__.MPNetTokenizer; - var __webpack_exports__MT5ForConditionalGeneration = __webpack_exports__.MT5ForConditionalGeneration; - var __webpack_exports__MT5Model = __webpack_exports__.MT5Model; - var __webpack_exports__MT5PreTrainedModel = __webpack_exports__.MT5PreTrainedModel; - var __webpack_exports__MarianMTModel = __webpack_exports__.MarianMTModel; - var __webpack_exports__MarianModel = __webpack_exports__.MarianModel; - var __webpack_exports__MarianPreTrainedModel = __webpack_exports__.MarianPreTrainedModel; - var __webpack_exports__MarianTokenizer = __webpack_exports__.MarianTokenizer; - var __webpack_exports__MaskFormerFeatureExtractor = __webpack_exports__.MaskFormerFeatureExtractor; - var __webpack_exports__MaskFormerForInstanceSegmentation = __webpack_exports__.MaskFormerForInstanceSegmentation; - var __webpack_exports__MaskFormerModel = __webpack_exports__.MaskFormerModel; - var __webpack_exports__MaskFormerPreTrainedModel = __webpack_exports__.MaskFormerPreTrainedModel; - var __webpack_exports__MaskedLMOutput = __webpack_exports__.MaskedLMOutput; - var __webpack_exports__MaxLengthCriteria = __webpack_exports__.MaxLengthCriteria; - var __webpack_exports__MistralForCausalLM = __webpack_exports__.MistralForCausalLM; - var __webpack_exports__MistralModel = __webpack_exports__.MistralModel; - var __webpack_exports__MistralPreTrainedModel = __webpack_exports__.MistralPreTrainedModel; - var __webpack_exports__MobileBertForMaskedLM = __webpack_exports__.MobileBertForMaskedLM; - var __webpack_exports__MobileBertForQuestionAnswering = __webpack_exports__.MobileBertForQuestionAnswering; - var __webpack_exports__MobileBertForSequenceClassification = __webpack_exports__.MobileBertForSequenceClassification; - var __webpack_exports__MobileBertModel = __webpack_exports__.MobileBertModel; - var __webpack_exports__MobileBertPreTrainedModel = __webpack_exports__.MobileBertPreTrainedModel; - var __webpack_exports__MobileBertTokenizer = __webpack_exports__.MobileBertTokenizer; - var __webpack_exports__MobileNetV1FeatureExtractor = __webpack_exports__.MobileNetV1FeatureExtractor; - var __webpack_exports__MobileNetV1ForImageClassification = __webpack_exports__.MobileNetV1ForImageClassification; - var __webpack_exports__MobileNetV1Model = __webpack_exports__.MobileNetV1Model; - var __webpack_exports__MobileNetV1PreTrainedModel = __webpack_exports__.MobileNetV1PreTrainedModel; - var __webpack_exports__MobileNetV2FeatureExtractor = __webpack_exports__.MobileNetV2FeatureExtractor; - var __webpack_exports__MobileNetV2ForImageClassification = __webpack_exports__.MobileNetV2ForImageClassification; - var __webpack_exports__MobileNetV2Model = __webpack_exports__.MobileNetV2Model; - var __webpack_exports__MobileNetV2PreTrainedModel = __webpack_exports__.MobileNetV2PreTrainedModel; - var __webpack_exports__MobileNetV3FeatureExtractor = __webpack_exports__.MobileNetV3FeatureExtractor; - var __webpack_exports__MobileNetV3ForImageClassification = __webpack_exports__.MobileNetV3ForImageClassification; - var __webpack_exports__MobileNetV3Model = __webpack_exports__.MobileNetV3Model; - var __webpack_exports__MobileNetV3PreTrainedModel = __webpack_exports__.MobileNetV3PreTrainedModel; - var __webpack_exports__MobileNetV4FeatureExtractor = __webpack_exports__.MobileNetV4FeatureExtractor; - var __webpack_exports__MobileNetV4ForImageClassification = __webpack_exports__.MobileNetV4ForImageClassification; - var __webpack_exports__MobileNetV4Model = __webpack_exports__.MobileNetV4Model; - var __webpack_exports__MobileNetV4PreTrainedModel = __webpack_exports__.MobileNetV4PreTrainedModel; - var __webpack_exports__MobileViTFeatureExtractor = __webpack_exports__.MobileViTFeatureExtractor; - var __webpack_exports__MobileViTForImageClassification = __webpack_exports__.MobileViTForImageClassification; - var __webpack_exports__MobileViTImageProcessor = __webpack_exports__.MobileViTImageProcessor; - var __webpack_exports__MobileViTModel = __webpack_exports__.MobileViTModel; - var __webpack_exports__MobileViTPreTrainedModel = __webpack_exports__.MobileViTPreTrainedModel; - var __webpack_exports__MobileViTV2ForImageClassification = __webpack_exports__.MobileViTV2ForImageClassification; - var __webpack_exports__MobileViTV2Model = __webpack_exports__.MobileViTV2Model; - var __webpack_exports__MobileViTV2PreTrainedModel = __webpack_exports__.MobileViTV2PreTrainedModel; - var __webpack_exports__ModelOutput = __webpack_exports__.ModelOutput; - var __webpack_exports__Moondream1ForConditionalGeneration = __webpack_exports__.Moondream1ForConditionalGeneration; - var __webpack_exports__MptForCausalLM = __webpack_exports__.MptForCausalLM; - var __webpack_exports__MptModel = __webpack_exports__.MptModel; - var __webpack_exports__MptPreTrainedModel = __webpack_exports__.MptPreTrainedModel; - var __webpack_exports__MusicgenForCausalLM = __webpack_exports__.MusicgenForCausalLM; - var __webpack_exports__MusicgenForConditionalGeneration = __webpack_exports__.MusicgenForConditionalGeneration; - var __webpack_exports__MusicgenModel = __webpack_exports__.MusicgenModel; - var __webpack_exports__MusicgenPreTrainedModel = __webpack_exports__.MusicgenPreTrainedModel; - var __webpack_exports__NllbTokenizer = __webpack_exports__.NllbTokenizer; - var __webpack_exports__NomicBertModel = __webpack_exports__.NomicBertModel; - var __webpack_exports__NomicBertPreTrainedModel = __webpack_exports__.NomicBertPreTrainedModel; - var __webpack_exports__NougatImageProcessor = __webpack_exports__.NougatImageProcessor; - var __webpack_exports__NougatTokenizer = __webpack_exports__.NougatTokenizer; - var __webpack_exports__OPTForCausalLM = __webpack_exports__.OPTForCausalLM; - var __webpack_exports__OPTModel = __webpack_exports__.OPTModel; - var __webpack_exports__OPTPreTrainedModel = __webpack_exports__.OPTPreTrainedModel; - var __webpack_exports__ObjectDetectionPipeline = __webpack_exports__.ObjectDetectionPipeline; - var __webpack_exports__OpenELMForCausalLM = __webpack_exports__.OpenELMForCausalLM; - var __webpack_exports__OpenELMModel = __webpack_exports__.OpenELMModel; - var __webpack_exports__OpenELMPreTrainedModel = __webpack_exports__.OpenELMPreTrainedModel; - var __webpack_exports__OwlViTFeatureExtractor = __webpack_exports__.OwlViTFeatureExtractor; - var __webpack_exports__OwlViTForObjectDetection = __webpack_exports__.OwlViTForObjectDetection; - var __webpack_exports__OwlViTModel = __webpack_exports__.OwlViTModel; - var __webpack_exports__OwlViTPreTrainedModel = __webpack_exports__.OwlViTPreTrainedModel; - var __webpack_exports__OwlViTProcessor = __webpack_exports__.OwlViTProcessor; - var __webpack_exports__Owlv2ForObjectDetection = __webpack_exports__.Owlv2ForObjectDetection; - var __webpack_exports__Owlv2ImageProcessor = __webpack_exports__.Owlv2ImageProcessor; - var __webpack_exports__Owlv2Model = __webpack_exports__.Owlv2Model; - var __webpack_exports__Owlv2PreTrainedModel = __webpack_exports__.Owlv2PreTrainedModel; - var __webpack_exports__Phi3ForCausalLM = __webpack_exports__.Phi3ForCausalLM; - var __webpack_exports__Phi3Model = __webpack_exports__.Phi3Model; - var __webpack_exports__Phi3PreTrainedModel = __webpack_exports__.Phi3PreTrainedModel; - var __webpack_exports__PhiForCausalLM = __webpack_exports__.PhiForCausalLM; - var __webpack_exports__PhiModel = __webpack_exports__.PhiModel; - var __webpack_exports__PhiPreTrainedModel = __webpack_exports__.PhiPreTrainedModel; - var __webpack_exports__Pipeline = __webpack_exports__.Pipeline; - var __webpack_exports__PreTrainedModel = __webpack_exports__.PreTrainedModel; - var __webpack_exports__PreTrainedTokenizer = __webpack_exports__.PreTrainedTokenizer; - var __webpack_exports__PretrainedConfig = __webpack_exports__.PretrainedConfig; - var __webpack_exports__PretrainedMixin = __webpack_exports__.PretrainedMixin; - var __webpack_exports__Processor = __webpack_exports__.Processor; - var __webpack_exports__PvtForImageClassification = __webpack_exports__.PvtForImageClassification; - var __webpack_exports__PvtImageProcessor = __webpack_exports__.PvtImageProcessor; - var __webpack_exports__PvtModel = __webpack_exports__.PvtModel; - var __webpack_exports__PvtPreTrainedModel = __webpack_exports__.PvtPreTrainedModel; - var __webpack_exports__PyAnnoteFeatureExtractor = __webpack_exports__.PyAnnoteFeatureExtractor; - var __webpack_exports__PyAnnoteForAudioFrameClassification = __webpack_exports__.PyAnnoteForAudioFrameClassification; - var __webpack_exports__PyAnnoteModel = __webpack_exports__.PyAnnoteModel; - var __webpack_exports__PyAnnotePreTrainedModel = __webpack_exports__.PyAnnotePreTrainedModel; - var __webpack_exports__PyAnnoteProcessor = __webpack_exports__.PyAnnoteProcessor; - var __webpack_exports__QuestionAnsweringModelOutput = __webpack_exports__.QuestionAnsweringModelOutput; - var __webpack_exports__QuestionAnsweringPipeline = __webpack_exports__.QuestionAnsweringPipeline; - var __webpack_exports__Qwen2ForCausalLM = __webpack_exports__.Qwen2ForCausalLM; - var __webpack_exports__Qwen2Model = __webpack_exports__.Qwen2Model; - var __webpack_exports__Qwen2PreTrainedModel = __webpack_exports__.Qwen2PreTrainedModel; - var __webpack_exports__Qwen2Tokenizer = __webpack_exports__.Qwen2Tokenizer; - var __webpack_exports__RTDetrForObjectDetection = __webpack_exports__.RTDetrForObjectDetection; - var __webpack_exports__RTDetrImageProcessor = __webpack_exports__.RTDetrImageProcessor; - var __webpack_exports__RTDetrModel = __webpack_exports__.RTDetrModel; - var __webpack_exports__RTDetrObjectDetectionOutput = __webpack_exports__.RTDetrObjectDetectionOutput; - var __webpack_exports__RTDetrPreTrainedModel = __webpack_exports__.RTDetrPreTrainedModel; - var __webpack_exports__RawImage = __webpack_exports__.RawImage; - var __webpack_exports__ResNetForImageClassification = __webpack_exports__.ResNetForImageClassification; - var __webpack_exports__ResNetModel = __webpack_exports__.ResNetModel; - var __webpack_exports__ResNetPreTrainedModel = __webpack_exports__.ResNetPreTrainedModel; - var __webpack_exports__RoFormerForMaskedLM = __webpack_exports__.RoFormerForMaskedLM; - var __webpack_exports__RoFormerForQuestionAnswering = __webpack_exports__.RoFormerForQuestionAnswering; - var __webpack_exports__RoFormerForSequenceClassification = __webpack_exports__.RoFormerForSequenceClassification; - var __webpack_exports__RoFormerForTokenClassification = __webpack_exports__.RoFormerForTokenClassification; - var __webpack_exports__RoFormerModel = __webpack_exports__.RoFormerModel; - var __webpack_exports__RoFormerPreTrainedModel = __webpack_exports__.RoFormerPreTrainedModel; - var __webpack_exports__RoFormerTokenizer = __webpack_exports__.RoFormerTokenizer; - var __webpack_exports__RobertaForMaskedLM = __webpack_exports__.RobertaForMaskedLM; - var __webpack_exports__RobertaForQuestionAnswering = __webpack_exports__.RobertaForQuestionAnswering; - var __webpack_exports__RobertaForSequenceClassification = __webpack_exports__.RobertaForSequenceClassification; - var __webpack_exports__RobertaForTokenClassification = __webpack_exports__.RobertaForTokenClassification; - var __webpack_exports__RobertaModel = __webpack_exports__.RobertaModel; - var __webpack_exports__RobertaPreTrainedModel = __webpack_exports__.RobertaPreTrainedModel; - var __webpack_exports__RobertaTokenizer = __webpack_exports__.RobertaTokenizer; - var __webpack_exports__SamImageProcessor = __webpack_exports__.SamImageProcessor; - var __webpack_exports__SamImageSegmentationOutput = __webpack_exports__.SamImageSegmentationOutput; - var __webpack_exports__SamModel = __webpack_exports__.SamModel; - var __webpack_exports__SamPreTrainedModel = __webpack_exports__.SamPreTrainedModel; - var __webpack_exports__SamProcessor = __webpack_exports__.SamProcessor; - var __webpack_exports__SapiensFeatureExtractor = __webpack_exports__.SapiensFeatureExtractor; - var __webpack_exports__SapiensForDepthEstimation = __webpack_exports__.SapiensForDepthEstimation; - var __webpack_exports__SapiensForNormalEstimation = __webpack_exports__.SapiensForNormalEstimation; - var __webpack_exports__SapiensForSemanticSegmentation = __webpack_exports__.SapiensForSemanticSegmentation; - var __webpack_exports__SapiensPreTrainedModel = __webpack_exports__.SapiensPreTrainedModel; - var __webpack_exports__SeamlessM4TFeatureExtractor = __webpack_exports__.SeamlessM4TFeatureExtractor; - var __webpack_exports__SegformerFeatureExtractor = __webpack_exports__.SegformerFeatureExtractor; - var __webpack_exports__SegformerForImageClassification = __webpack_exports__.SegformerForImageClassification; - var __webpack_exports__SegformerForSemanticSegmentation = __webpack_exports__.SegformerForSemanticSegmentation; - var __webpack_exports__SegformerModel = __webpack_exports__.SegformerModel; - var __webpack_exports__SegformerPreTrainedModel = __webpack_exports__.SegformerPreTrainedModel; - var __webpack_exports__Seq2SeqLMOutput = __webpack_exports__.Seq2SeqLMOutput; - var __webpack_exports__SequenceClassifierOutput = __webpack_exports__.SequenceClassifierOutput; - var __webpack_exports__SiglipImageProcessor = __webpack_exports__.SiglipImageProcessor; - var __webpack_exports__SiglipModel = __webpack_exports__.SiglipModel; - var __webpack_exports__SiglipPreTrainedModel = __webpack_exports__.SiglipPreTrainedModel; - var __webpack_exports__SiglipTextModel = __webpack_exports__.SiglipTextModel; - var __webpack_exports__SiglipTokenizer = __webpack_exports__.SiglipTokenizer; - var __webpack_exports__SiglipVisionModel = __webpack_exports__.SiglipVisionModel; - var __webpack_exports__SpeechT5FeatureExtractor = __webpack_exports__.SpeechT5FeatureExtractor; - var __webpack_exports__SpeechT5ForSpeechToText = __webpack_exports__.SpeechT5ForSpeechToText; - var __webpack_exports__SpeechT5ForTextToSpeech = __webpack_exports__.SpeechT5ForTextToSpeech; - var __webpack_exports__SpeechT5HifiGan = __webpack_exports__.SpeechT5HifiGan; - var __webpack_exports__SpeechT5Model = __webpack_exports__.SpeechT5Model; - var __webpack_exports__SpeechT5PreTrainedModel = __webpack_exports__.SpeechT5PreTrainedModel; - var __webpack_exports__SpeechT5Processor = __webpack_exports__.SpeechT5Processor; - var __webpack_exports__SpeechT5Tokenizer = __webpack_exports__.SpeechT5Tokenizer; - var __webpack_exports__SqueezeBertForMaskedLM = __webpack_exports__.SqueezeBertForMaskedLM; - var __webpack_exports__SqueezeBertForQuestionAnswering = __webpack_exports__.SqueezeBertForQuestionAnswering; - var __webpack_exports__SqueezeBertForSequenceClassification = __webpack_exports__.SqueezeBertForSequenceClassification; - var __webpack_exports__SqueezeBertModel = __webpack_exports__.SqueezeBertModel; - var __webpack_exports__SqueezeBertPreTrainedModel = __webpack_exports__.SqueezeBertPreTrainedModel; - var __webpack_exports__SqueezeBertTokenizer = __webpack_exports__.SqueezeBertTokenizer; - var __webpack_exports__StableLmForCausalLM = __webpack_exports__.StableLmForCausalLM; - var __webpack_exports__StableLmModel = __webpack_exports__.StableLmModel; - var __webpack_exports__StableLmPreTrainedModel = __webpack_exports__.StableLmPreTrainedModel; - var __webpack_exports__Starcoder2ForCausalLM = __webpack_exports__.Starcoder2ForCausalLM; - var __webpack_exports__Starcoder2Model = __webpack_exports__.Starcoder2Model; - var __webpack_exports__Starcoder2PreTrainedModel = __webpack_exports__.Starcoder2PreTrainedModel; - var __webpack_exports__StoppingCriteria = __webpack_exports__.StoppingCriteria; - var __webpack_exports__StoppingCriteriaList = __webpack_exports__.StoppingCriteriaList; - var __webpack_exports__SummarizationPipeline = __webpack_exports__.SummarizationPipeline; - var __webpack_exports__Swin2SRForImageSuperResolution = __webpack_exports__.Swin2SRForImageSuperResolution; - var __webpack_exports__Swin2SRImageProcessor = __webpack_exports__.Swin2SRImageProcessor; - var __webpack_exports__Swin2SRModel = __webpack_exports__.Swin2SRModel; - var __webpack_exports__Swin2SRPreTrainedModel = __webpack_exports__.Swin2SRPreTrainedModel; - var __webpack_exports__SwinForImageClassification = __webpack_exports__.SwinForImageClassification; - var __webpack_exports__SwinModel = __webpack_exports__.SwinModel; - var __webpack_exports__SwinPreTrainedModel = __webpack_exports__.SwinPreTrainedModel; - var __webpack_exports__T5ForConditionalGeneration = __webpack_exports__.T5ForConditionalGeneration; - var __webpack_exports__T5Model = __webpack_exports__.T5Model; - var __webpack_exports__T5PreTrainedModel = __webpack_exports__.T5PreTrainedModel; - var __webpack_exports__T5Tokenizer = __webpack_exports__.T5Tokenizer; - var __webpack_exports__TableTransformerForObjectDetection = __webpack_exports__.TableTransformerForObjectDetection; - var __webpack_exports__TableTransformerModel = __webpack_exports__.TableTransformerModel; - var __webpack_exports__TableTransformerObjectDetectionOutput = __webpack_exports__.TableTransformerObjectDetectionOutput; - var __webpack_exports__TableTransformerPreTrainedModel = __webpack_exports__.TableTransformerPreTrainedModel; - var __webpack_exports__Tensor = __webpack_exports__.Tensor; - var __webpack_exports__Text2TextGenerationPipeline = __webpack_exports__.Text2TextGenerationPipeline; - var __webpack_exports__TextClassificationPipeline = __webpack_exports__.TextClassificationPipeline; - var __webpack_exports__TextGenerationPipeline = __webpack_exports__.TextGenerationPipeline; - var __webpack_exports__TextStreamer = __webpack_exports__.TextStreamer; - var __webpack_exports__TextToAudioPipeline = __webpack_exports__.TextToAudioPipeline; - var __webpack_exports__TokenClassificationPipeline = __webpack_exports__.TokenClassificationPipeline; - var __webpack_exports__TokenClassifierOutput = __webpack_exports__.TokenClassifierOutput; - var __webpack_exports__TokenizerModel = __webpack_exports__.TokenizerModel; - var __webpack_exports__TrOCRForCausalLM = __webpack_exports__.TrOCRForCausalLM; - var __webpack_exports__TrOCRPreTrainedModel = __webpack_exports__.TrOCRPreTrainedModel; - var __webpack_exports__TranslationPipeline = __webpack_exports__.TranslationPipeline; - var __webpack_exports__UniSpeechForCTC = __webpack_exports__.UniSpeechForCTC; - var __webpack_exports__UniSpeechForSequenceClassification = __webpack_exports__.UniSpeechForSequenceClassification; - var __webpack_exports__UniSpeechModel = __webpack_exports__.UniSpeechModel; - var __webpack_exports__UniSpeechPreTrainedModel = __webpack_exports__.UniSpeechPreTrainedModel; - var __webpack_exports__UniSpeechSatForAudioFrameClassification = __webpack_exports__.UniSpeechSatForAudioFrameClassification; - var __webpack_exports__UniSpeechSatForCTC = __webpack_exports__.UniSpeechSatForCTC; - var __webpack_exports__UniSpeechSatForSequenceClassification = __webpack_exports__.UniSpeechSatForSequenceClassification; - var __webpack_exports__UniSpeechSatModel = __webpack_exports__.UniSpeechSatModel; - var __webpack_exports__UniSpeechSatPreTrainedModel = __webpack_exports__.UniSpeechSatPreTrainedModel; - var __webpack_exports__ViTFeatureExtractor = __webpack_exports__.ViTFeatureExtractor; - var __webpack_exports__ViTForImageClassification = __webpack_exports__.ViTForImageClassification; - var __webpack_exports__ViTImageProcessor = __webpack_exports__.ViTImageProcessor; - var __webpack_exports__ViTMAEModel = __webpack_exports__.ViTMAEModel; - var __webpack_exports__ViTMAEPreTrainedModel = __webpack_exports__.ViTMAEPreTrainedModel; - var __webpack_exports__ViTMSNForImageClassification = __webpack_exports__.ViTMSNForImageClassification; - var __webpack_exports__ViTMSNModel = __webpack_exports__.ViTMSNModel; - var __webpack_exports__ViTMSNPreTrainedModel = __webpack_exports__.ViTMSNPreTrainedModel; - var __webpack_exports__ViTModel = __webpack_exports__.ViTModel; - var __webpack_exports__ViTPreTrainedModel = __webpack_exports__.ViTPreTrainedModel; - var __webpack_exports__VisionEncoderDecoderModel = __webpack_exports__.VisionEncoderDecoderModel; - var __webpack_exports__VitMatteForImageMatting = __webpack_exports__.VitMatteForImageMatting; - var __webpack_exports__VitMatteImageProcessor = __webpack_exports__.VitMatteImageProcessor; - var __webpack_exports__VitMattePreTrainedModel = __webpack_exports__.VitMattePreTrainedModel; - var __webpack_exports__VitsModel = __webpack_exports__.VitsModel; - var __webpack_exports__VitsModelOutput = __webpack_exports__.VitsModelOutput; - var __webpack_exports__VitsPreTrainedModel = __webpack_exports__.VitsPreTrainedModel; - var __webpack_exports__VitsTokenizer = __webpack_exports__.VitsTokenizer; - var __webpack_exports__Wav2Vec2BertForCTC = __webpack_exports__.Wav2Vec2BertForCTC; - var __webpack_exports__Wav2Vec2BertForSequenceClassification = __webpack_exports__.Wav2Vec2BertForSequenceClassification; - var __webpack_exports__Wav2Vec2BertModel = __webpack_exports__.Wav2Vec2BertModel; - var __webpack_exports__Wav2Vec2BertPreTrainedModel = __webpack_exports__.Wav2Vec2BertPreTrainedModel; - var __webpack_exports__Wav2Vec2CTCTokenizer = __webpack_exports__.Wav2Vec2CTCTokenizer; - var __webpack_exports__Wav2Vec2FeatureExtractor = __webpack_exports__.Wav2Vec2FeatureExtractor; - var __webpack_exports__Wav2Vec2ForAudioFrameClassification = __webpack_exports__.Wav2Vec2ForAudioFrameClassification; - var __webpack_exports__Wav2Vec2ForCTC = __webpack_exports__.Wav2Vec2ForCTC; - var __webpack_exports__Wav2Vec2ForSequenceClassification = __webpack_exports__.Wav2Vec2ForSequenceClassification; - var __webpack_exports__Wav2Vec2Model = __webpack_exports__.Wav2Vec2Model; - var __webpack_exports__Wav2Vec2PreTrainedModel = __webpack_exports__.Wav2Vec2PreTrainedModel; - var __webpack_exports__Wav2Vec2ProcessorWithLM = __webpack_exports__.Wav2Vec2ProcessorWithLM; - var __webpack_exports__WavLMForAudioFrameClassification = __webpack_exports__.WavLMForAudioFrameClassification; - var __webpack_exports__WavLMForCTC = __webpack_exports__.WavLMForCTC; - var __webpack_exports__WavLMForSequenceClassification = __webpack_exports__.WavLMForSequenceClassification; - var __webpack_exports__WavLMForXVector = __webpack_exports__.WavLMForXVector; - var __webpack_exports__WavLMModel = __webpack_exports__.WavLMModel; - var __webpack_exports__WavLMPreTrainedModel = __webpack_exports__.WavLMPreTrainedModel; - var __webpack_exports__WeSpeakerFeatureExtractor = __webpack_exports__.WeSpeakerFeatureExtractor; - var __webpack_exports__WeSpeakerResNetModel = __webpack_exports__.WeSpeakerResNetModel; - var __webpack_exports__WeSpeakerResNetPreTrainedModel = __webpack_exports__.WeSpeakerResNetPreTrainedModel; - var __webpack_exports__WhisperFeatureExtractor = __webpack_exports__.WhisperFeatureExtractor; - var __webpack_exports__WhisperForConditionalGeneration = __webpack_exports__.WhisperForConditionalGeneration; - var __webpack_exports__WhisperModel = __webpack_exports__.WhisperModel; - var __webpack_exports__WhisperPreTrainedModel = __webpack_exports__.WhisperPreTrainedModel; - var __webpack_exports__WhisperProcessor = __webpack_exports__.WhisperProcessor; - var __webpack_exports__WhisperTextStreamer = __webpack_exports__.WhisperTextStreamer; - var __webpack_exports__WhisperTokenizer = __webpack_exports__.WhisperTokenizer; - var __webpack_exports__XLMForQuestionAnswering = __webpack_exports__.XLMForQuestionAnswering; - var __webpack_exports__XLMForSequenceClassification = __webpack_exports__.XLMForSequenceClassification; - var __webpack_exports__XLMForTokenClassification = __webpack_exports__.XLMForTokenClassification; - var __webpack_exports__XLMModel = __webpack_exports__.XLMModel; - var __webpack_exports__XLMPreTrainedModel = __webpack_exports__.XLMPreTrainedModel; - var __webpack_exports__XLMRobertaForMaskedLM = __webpack_exports__.XLMRobertaForMaskedLM; - var __webpack_exports__XLMRobertaForQuestionAnswering = __webpack_exports__.XLMRobertaForQuestionAnswering; - var __webpack_exports__XLMRobertaForSequenceClassification = __webpack_exports__.XLMRobertaForSequenceClassification; - var __webpack_exports__XLMRobertaForTokenClassification = __webpack_exports__.XLMRobertaForTokenClassification; - var __webpack_exports__XLMRobertaModel = __webpack_exports__.XLMRobertaModel; - var __webpack_exports__XLMRobertaPreTrainedModel = __webpack_exports__.XLMRobertaPreTrainedModel; - var __webpack_exports__XLMRobertaTokenizer = __webpack_exports__.XLMRobertaTokenizer; - var __webpack_exports__XLMTokenizer = __webpack_exports__.XLMTokenizer; - var __webpack_exports__XLMWithLMHeadModel = __webpack_exports__.XLMWithLMHeadModel; - var __webpack_exports__XVectorOutput = __webpack_exports__.XVectorOutput; - var __webpack_exports__YolosFeatureExtractor = __webpack_exports__.YolosFeatureExtractor; - var __webpack_exports__YolosForObjectDetection = __webpack_exports__.YolosForObjectDetection; - var __webpack_exports__YolosModel = __webpack_exports__.YolosModel; - var __webpack_exports__YolosObjectDetectionOutput = __webpack_exports__.YolosObjectDetectionOutput; - var __webpack_exports__YolosPreTrainedModel = __webpack_exports__.YolosPreTrainedModel; - var __webpack_exports__ZeroShotAudioClassificationPipeline = __webpack_exports__.ZeroShotAudioClassificationPipeline; - var __webpack_exports__ZeroShotClassificationPipeline = __webpack_exports__.ZeroShotClassificationPipeline; - var __webpack_exports__ZeroShotImageClassificationPipeline = __webpack_exports__.ZeroShotImageClassificationPipeline; - var __webpack_exports__ZeroShotObjectDetectionPipeline = __webpack_exports__.ZeroShotObjectDetectionPipeline; - var __webpack_exports__bankers_round = __webpack_exports__.bankers_round; - var __webpack_exports__cat = __webpack_exports__.cat; - var __webpack_exports__cos_sim = __webpack_exports__.cos_sim; - var __webpack_exports__dot = __webpack_exports__.dot; - var __webpack_exports__dynamic_time_warping = __webpack_exports__.dynamic_time_warping; - var __webpack_exports__env = __webpack_exports__.env; - var __webpack_exports__full = __webpack_exports__.full; - var __webpack_exports__full_like = __webpack_exports__.full_like; - var __webpack_exports__getKeyValueShapes = __webpack_exports__.getKeyValueShapes; - var __webpack_exports__hamming = __webpack_exports__.hamming; - var __webpack_exports__hanning = __webpack_exports__.hanning; - var __webpack_exports__interpolate = __webpack_exports__.interpolate; - var __webpack_exports__interpolate_4d = __webpack_exports__.interpolate_4d; - var __webpack_exports__interpolate_data = __webpack_exports__.interpolate_data; - var __webpack_exports__is_chinese_char = __webpack_exports__.is_chinese_char; - var __webpack_exports__layer_norm = __webpack_exports__.layer_norm; - var __webpack_exports__log_softmax = __webpack_exports__.log_softmax; - var __webpack_exports__magnitude = __webpack_exports__.magnitude; - var __webpack_exports__matmul = __webpack_exports__.matmul; - var __webpack_exports__max = __webpack_exports__.max; - var __webpack_exports__mean = __webpack_exports__.mean; - var __webpack_exports__mean_pooling = __webpack_exports__.mean_pooling; - var __webpack_exports__medianFilter = __webpack_exports__.medianFilter; - var __webpack_exports__mel_filter_bank = __webpack_exports__.mel_filter_bank; - var __webpack_exports__min = __webpack_exports__.min; - var __webpack_exports__ones = __webpack_exports__.ones; - var __webpack_exports__ones_like = __webpack_exports__.ones_like; - var __webpack_exports__permute = __webpack_exports__.permute; - var __webpack_exports__permute_data = __webpack_exports__.permute_data; - var __webpack_exports__pipeline = __webpack_exports__.pipeline; - var __webpack_exports__quantize_embeddings = __webpack_exports__.quantize_embeddings; - var __webpack_exports__read_audio = __webpack_exports__.read_audio; - var __webpack_exports__rfft = __webpack_exports__.rfft; - var __webpack_exports__round = __webpack_exports__.round; - var __webpack_exports__softmax = __webpack_exports__.softmax; - var __webpack_exports__spectrogram = __webpack_exports__.spectrogram; - var __webpack_exports__stack = __webpack_exports__.stack; - var __webpack_exports__std_mean = __webpack_exports__.std_mean; - var __webpack_exports__topk = __webpack_exports__.topk; - var __webpack_exports__window_function = __webpack_exports__.window_function; - var __webpack_exports__zeros = __webpack_exports__.zeros; - var __webpack_exports__zeros_like = __webpack_exports__.zeros_like; - export { - __webpack_exports__ASTFeatureExtractor as ASTFeatureExtractor, - __webpack_exports__ASTForAudioClassification as ASTForAudioClassification, - __webpack_exports__ASTModel as ASTModel, - __webpack_exports__ASTPreTrainedModel as ASTPreTrainedModel, - __webpack_exports__AlbertForMaskedLM as AlbertForMaskedLM, - __webpack_exports__AlbertForQuestionAnswering as AlbertForQuestionAnswering, - __webpack_exports__AlbertForSequenceClassification as AlbertForSequenceClassification, - __webpack_exports__AlbertModel as AlbertModel, - __webpack_exports__AlbertPreTrainedModel as AlbertPreTrainedModel, - __webpack_exports__AlbertTokenizer as AlbertTokenizer, - __webpack_exports__AudioClassificationPipeline as AudioClassificationPipeline, - __webpack_exports__AutoConfig as AutoConfig, - __webpack_exports__AutoModel as AutoModel, - __webpack_exports__AutoModelForAudioClassification as AutoModelForAudioClassification, - __webpack_exports__AutoModelForAudioFrameClassification as AutoModelForAudioFrameClassification, - __webpack_exports__AutoModelForCTC as AutoModelForCTC, - __webpack_exports__AutoModelForCausalLM as AutoModelForCausalLM, - __webpack_exports__AutoModelForDepthEstimation as AutoModelForDepthEstimation, - __webpack_exports__AutoModelForDocumentQuestionAnswering as AutoModelForDocumentQuestionAnswering, - __webpack_exports__AutoModelForImageClassification as AutoModelForImageClassification, - __webpack_exports__AutoModelForImageFeatureExtraction as AutoModelForImageFeatureExtraction, - __webpack_exports__AutoModelForImageMatting as AutoModelForImageMatting, - __webpack_exports__AutoModelForImageSegmentation as AutoModelForImageSegmentation, - __webpack_exports__AutoModelForImageToImage as AutoModelForImageToImage, - __webpack_exports__AutoModelForMaskGeneration as AutoModelForMaskGeneration, - __webpack_exports__AutoModelForMaskedLM as AutoModelForMaskedLM, - __webpack_exports__AutoModelForNormalEstimation as AutoModelForNormalEstimation, - __webpack_exports__AutoModelForObjectDetection as AutoModelForObjectDetection, - __webpack_exports__AutoModelForQuestionAnswering as AutoModelForQuestionAnswering, - __webpack_exports__AutoModelForSemanticSegmentation as AutoModelForSemanticSegmentation, - __webpack_exports__AutoModelForSeq2SeqLM as AutoModelForSeq2SeqLM, - __webpack_exports__AutoModelForSequenceClassification as AutoModelForSequenceClassification, - __webpack_exports__AutoModelForSpeechSeq2Seq as AutoModelForSpeechSeq2Seq, - __webpack_exports__AutoModelForTextToSpectrogram as AutoModelForTextToSpectrogram, - __webpack_exports__AutoModelForTextToWaveform as AutoModelForTextToWaveform, - __webpack_exports__AutoModelForTokenClassification as AutoModelForTokenClassification, - __webpack_exports__AutoModelForUniversalSegmentation as AutoModelForUniversalSegmentation, - __webpack_exports__AutoModelForVision2Seq as AutoModelForVision2Seq, - __webpack_exports__AutoModelForXVector as AutoModelForXVector, - __webpack_exports__AutoModelForZeroShotObjectDetection as AutoModelForZeroShotObjectDetection, - __webpack_exports__AutoProcessor as AutoProcessor, - __webpack_exports__AutoTokenizer as AutoTokenizer, - __webpack_exports__AutomaticSpeechRecognitionPipeline as AutomaticSpeechRecognitionPipeline, - __webpack_exports__BartForConditionalGeneration as BartForConditionalGeneration, - __webpack_exports__BartForSequenceClassification as BartForSequenceClassification, - __webpack_exports__BartModel as BartModel, - __webpack_exports__BartPretrainedModel as BartPretrainedModel, - __webpack_exports__BartTokenizer as BartTokenizer, - __webpack_exports__BaseModelOutput as BaseModelOutput, - __webpack_exports__BaseStreamer as BaseStreamer, - __webpack_exports__BeitFeatureExtractor as BeitFeatureExtractor, - __webpack_exports__BeitForImageClassification as BeitForImageClassification, - __webpack_exports__BeitModel as BeitModel, - __webpack_exports__BeitPreTrainedModel as BeitPreTrainedModel, - __webpack_exports__BertForMaskedLM as BertForMaskedLM, - __webpack_exports__BertForQuestionAnswering as BertForQuestionAnswering, - __webpack_exports__BertForSequenceClassification as BertForSequenceClassification, - __webpack_exports__BertForTokenClassification as BertForTokenClassification, - __webpack_exports__BertModel as BertModel, - __webpack_exports__BertPreTrainedModel as BertPreTrainedModel, - __webpack_exports__BertTokenizer as BertTokenizer, - __webpack_exports__BitImageProcessor as BitImageProcessor, - __webpack_exports__BlenderbotForConditionalGeneration as BlenderbotForConditionalGeneration, - __webpack_exports__BlenderbotModel as BlenderbotModel, - __webpack_exports__BlenderbotPreTrainedModel as BlenderbotPreTrainedModel, - __webpack_exports__BlenderbotSmallForConditionalGeneration as BlenderbotSmallForConditionalGeneration, - __webpack_exports__BlenderbotSmallModel as BlenderbotSmallModel, - __webpack_exports__BlenderbotSmallPreTrainedModel as BlenderbotSmallPreTrainedModel, - __webpack_exports__BlenderbotSmallTokenizer as BlenderbotSmallTokenizer, - __webpack_exports__BlenderbotTokenizer as BlenderbotTokenizer, - __webpack_exports__BloomForCausalLM as BloomForCausalLM, - __webpack_exports__BloomModel as BloomModel, - __webpack_exports__BloomPreTrainedModel as BloomPreTrainedModel, - __webpack_exports__BloomTokenizer as BloomTokenizer, - __webpack_exports__CLIPFeatureExtractor as CLIPFeatureExtractor, - __webpack_exports__CLIPImageProcessor as CLIPImageProcessor, - __webpack_exports__CLIPModel as CLIPModel, - __webpack_exports__CLIPPreTrainedModel as CLIPPreTrainedModel, - __webpack_exports__CLIPSegForImageSegmentation as CLIPSegForImageSegmentation, - __webpack_exports__CLIPSegModel as CLIPSegModel, - __webpack_exports__CLIPSegPreTrainedModel as CLIPSegPreTrainedModel, - __webpack_exports__CLIPTextModel as CLIPTextModel, - __webpack_exports__CLIPTextModelWithProjection as CLIPTextModelWithProjection, - __webpack_exports__CLIPTokenizer as CLIPTokenizer, - __webpack_exports__CLIPVisionModel as CLIPVisionModel, - __webpack_exports__CLIPVisionModelWithProjection as CLIPVisionModelWithProjection, - __webpack_exports__CamembertForMaskedLM as CamembertForMaskedLM, - __webpack_exports__CamembertForQuestionAnswering as CamembertForQuestionAnswering, - __webpack_exports__CamembertForSequenceClassification as CamembertForSequenceClassification, - __webpack_exports__CamembertForTokenClassification as CamembertForTokenClassification, - __webpack_exports__CamembertModel as CamembertModel, - __webpack_exports__CamembertPreTrainedModel as CamembertPreTrainedModel, - __webpack_exports__CamembertTokenizer as CamembertTokenizer, - __webpack_exports__CausalLMOutput as CausalLMOutput, - __webpack_exports__CausalLMOutputWithPast as CausalLMOutputWithPast, - __webpack_exports__ChineseCLIPFeatureExtractor as ChineseCLIPFeatureExtractor, - __webpack_exports__ChineseCLIPModel as ChineseCLIPModel, - __webpack_exports__ChineseCLIPPreTrainedModel as ChineseCLIPPreTrainedModel, - __webpack_exports__ClapAudioModelWithProjection as ClapAudioModelWithProjection, - __webpack_exports__ClapFeatureExtractor as ClapFeatureExtractor, - __webpack_exports__ClapModel as ClapModel, - __webpack_exports__ClapPreTrainedModel as ClapPreTrainedModel, - __webpack_exports__ClapTextModelWithProjection as ClapTextModelWithProjection, - __webpack_exports__CodeGenForCausalLM as CodeGenForCausalLM, - __webpack_exports__CodeGenModel as CodeGenModel, - __webpack_exports__CodeGenPreTrainedModel as CodeGenPreTrainedModel, - __webpack_exports__CodeGenTokenizer as CodeGenTokenizer, - __webpack_exports__CodeLlamaTokenizer as CodeLlamaTokenizer, - __webpack_exports__CohereForCausalLM as CohereForCausalLM, - __webpack_exports__CohereModel as CohereModel, - __webpack_exports__CoherePreTrainedModel as CoherePreTrainedModel, - __webpack_exports__CohereTokenizer as CohereTokenizer, - __webpack_exports__ConvBertForMaskedLM as ConvBertForMaskedLM, - __webpack_exports__ConvBertForQuestionAnswering as ConvBertForQuestionAnswering, - __webpack_exports__ConvBertForSequenceClassification as ConvBertForSequenceClassification, - __webpack_exports__ConvBertForTokenClassification as ConvBertForTokenClassification, - __webpack_exports__ConvBertModel as ConvBertModel, - __webpack_exports__ConvBertPreTrainedModel as ConvBertPreTrainedModel, - __webpack_exports__ConvBertTokenizer as ConvBertTokenizer, - __webpack_exports__ConvNextFeatureExtractor as ConvNextFeatureExtractor, - __webpack_exports__ConvNextForImageClassification as ConvNextForImageClassification, - __webpack_exports__ConvNextImageProcessor as ConvNextImageProcessor, - __webpack_exports__ConvNextModel as ConvNextModel, - __webpack_exports__ConvNextPreTrainedModel as ConvNextPreTrainedModel, - __webpack_exports__ConvNextV2ForImageClassification as ConvNextV2ForImageClassification, - __webpack_exports__ConvNextV2Model as ConvNextV2Model, - __webpack_exports__ConvNextV2PreTrainedModel as ConvNextV2PreTrainedModel, - __webpack_exports__DPTFeatureExtractor as DPTFeatureExtractor, - __webpack_exports__DPTForDepthEstimation as DPTForDepthEstimation, - __webpack_exports__DPTImageProcessor as DPTImageProcessor, - __webpack_exports__DPTModel as DPTModel, - __webpack_exports__DPTPreTrainedModel as DPTPreTrainedModel, - __webpack_exports__DebertaForMaskedLM as DebertaForMaskedLM, - __webpack_exports__DebertaForQuestionAnswering as DebertaForQuestionAnswering, - __webpack_exports__DebertaForSequenceClassification as DebertaForSequenceClassification, - __webpack_exports__DebertaForTokenClassification as DebertaForTokenClassification, - __webpack_exports__DebertaModel as DebertaModel, - __webpack_exports__DebertaPreTrainedModel as DebertaPreTrainedModel, - __webpack_exports__DebertaTokenizer as DebertaTokenizer, - __webpack_exports__DebertaV2ForMaskedLM as DebertaV2ForMaskedLM, - __webpack_exports__DebertaV2ForQuestionAnswering as DebertaV2ForQuestionAnswering, - __webpack_exports__DebertaV2ForSequenceClassification as DebertaV2ForSequenceClassification, - __webpack_exports__DebertaV2ForTokenClassification as DebertaV2ForTokenClassification, - __webpack_exports__DebertaV2Model as DebertaV2Model, - __webpack_exports__DebertaV2PreTrainedModel as DebertaV2PreTrainedModel, - __webpack_exports__DebertaV2Tokenizer as DebertaV2Tokenizer, - __webpack_exports__DecisionTransformerModel as DecisionTransformerModel, - __webpack_exports__DecisionTransformerPreTrainedModel as DecisionTransformerPreTrainedModel, - __webpack_exports__DeiTFeatureExtractor as DeiTFeatureExtractor, - __webpack_exports__DeiTForImageClassification as DeiTForImageClassification, - __webpack_exports__DeiTModel as DeiTModel, - __webpack_exports__DeiTPreTrainedModel as DeiTPreTrainedModel, - __webpack_exports__DepthAnythingForDepthEstimation as DepthAnythingForDepthEstimation, - __webpack_exports__DepthAnythingPreTrainedModel as DepthAnythingPreTrainedModel, - __webpack_exports__DepthEstimationPipeline as DepthEstimationPipeline, - __webpack_exports__DepthProForDepthEstimation as DepthProForDepthEstimation, - __webpack_exports__DepthProPreTrainedModel as DepthProPreTrainedModel, - __webpack_exports__DetrFeatureExtractor as DetrFeatureExtractor, - __webpack_exports__DetrForObjectDetection as DetrForObjectDetection, - __webpack_exports__DetrForSegmentation as DetrForSegmentation, - __webpack_exports__DetrModel as DetrModel, - __webpack_exports__DetrObjectDetectionOutput as DetrObjectDetectionOutput, - __webpack_exports__DetrPreTrainedModel as DetrPreTrainedModel, - __webpack_exports__DetrSegmentationOutput as DetrSegmentationOutput, - __webpack_exports__Dinov2ForImageClassification as Dinov2ForImageClassification, - __webpack_exports__Dinov2Model as Dinov2Model, - __webpack_exports__Dinov2PreTrainedModel as Dinov2PreTrainedModel, - __webpack_exports__DistilBertForMaskedLM as DistilBertForMaskedLM, - __webpack_exports__DistilBertForQuestionAnswering as DistilBertForQuestionAnswering, - __webpack_exports__DistilBertForSequenceClassification as DistilBertForSequenceClassification, - __webpack_exports__DistilBertForTokenClassification as DistilBertForTokenClassification, - __webpack_exports__DistilBertModel as DistilBertModel, - __webpack_exports__DistilBertPreTrainedModel as DistilBertPreTrainedModel, - __webpack_exports__DistilBertTokenizer as DistilBertTokenizer, - __webpack_exports__DocumentQuestionAnsweringPipeline as DocumentQuestionAnsweringPipeline, - __webpack_exports__DonutFeatureExtractor as DonutFeatureExtractor, - __webpack_exports__DonutSwinModel as DonutSwinModel, - __webpack_exports__DonutSwinPreTrainedModel as DonutSwinPreTrainedModel, - __webpack_exports__EfficientNetForImageClassification as EfficientNetForImageClassification, - __webpack_exports__EfficientNetImageProcessor as EfficientNetImageProcessor, - __webpack_exports__EfficientNetModel as EfficientNetModel, - __webpack_exports__EfficientNetPreTrainedModel as EfficientNetPreTrainedModel, - __webpack_exports__ElectraForMaskedLM as ElectraForMaskedLM, - __webpack_exports__ElectraForQuestionAnswering as ElectraForQuestionAnswering, - __webpack_exports__ElectraForSequenceClassification as ElectraForSequenceClassification, - __webpack_exports__ElectraForTokenClassification as ElectraForTokenClassification, - __webpack_exports__ElectraModel as ElectraModel, - __webpack_exports__ElectraPreTrainedModel as ElectraPreTrainedModel, - __webpack_exports__ElectraTokenizer as ElectraTokenizer, - __webpack_exports__EosTokenCriteria as EosTokenCriteria, - __webpack_exports__EsmForMaskedLM as EsmForMaskedLM, - __webpack_exports__EsmForSequenceClassification as EsmForSequenceClassification, - __webpack_exports__EsmForTokenClassification as EsmForTokenClassification, - __webpack_exports__EsmModel as EsmModel, - __webpack_exports__EsmPreTrainedModel as EsmPreTrainedModel, - __webpack_exports__EsmTokenizer as EsmTokenizer, - __webpack_exports__FFT as FFT, - __webpack_exports__FalconForCausalLM as FalconForCausalLM, - __webpack_exports__FalconModel as FalconModel, - __webpack_exports__FalconPreTrainedModel as FalconPreTrainedModel, - __webpack_exports__FalconTokenizer as FalconTokenizer, - __webpack_exports__FastViTForImageClassification as FastViTForImageClassification, - __webpack_exports__FastViTModel as FastViTModel, - __webpack_exports__FastViTPreTrainedModel as FastViTPreTrainedModel, - __webpack_exports__FeatureExtractionPipeline as FeatureExtractionPipeline, - __webpack_exports__FeatureExtractor as FeatureExtractor, - __webpack_exports__FillMaskPipeline as FillMaskPipeline, - __webpack_exports__Florence2ForConditionalGeneration as Florence2ForConditionalGeneration, - __webpack_exports__Florence2PreTrainedModel as Florence2PreTrainedModel, - __webpack_exports__Florence2Processor as Florence2Processor, - __webpack_exports__GLPNFeatureExtractor as GLPNFeatureExtractor, - __webpack_exports__GLPNForDepthEstimation as GLPNForDepthEstimation, - __webpack_exports__GLPNModel as GLPNModel, - __webpack_exports__GLPNPreTrainedModel as GLPNPreTrainedModel, - __webpack_exports__GPT2LMHeadModel as GPT2LMHeadModel, - __webpack_exports__GPT2Model as GPT2Model, - __webpack_exports__GPT2PreTrainedModel as GPT2PreTrainedModel, - __webpack_exports__GPT2Tokenizer as GPT2Tokenizer, - __webpack_exports__GPTBigCodeForCausalLM as GPTBigCodeForCausalLM, - __webpack_exports__GPTBigCodeModel as GPTBigCodeModel, - __webpack_exports__GPTBigCodePreTrainedModel as GPTBigCodePreTrainedModel, - __webpack_exports__GPTJForCausalLM as GPTJForCausalLM, - __webpack_exports__GPTJModel as GPTJModel, - __webpack_exports__GPTJPreTrainedModel as GPTJPreTrainedModel, - __webpack_exports__GPTNeoForCausalLM as GPTNeoForCausalLM, - __webpack_exports__GPTNeoModel as GPTNeoModel, - __webpack_exports__GPTNeoPreTrainedModel as GPTNeoPreTrainedModel, - __webpack_exports__GPTNeoXForCausalLM as GPTNeoXForCausalLM, - __webpack_exports__GPTNeoXModel as GPTNeoXModel, - __webpack_exports__GPTNeoXPreTrainedModel as GPTNeoXPreTrainedModel, - __webpack_exports__GPTNeoXTokenizer as GPTNeoXTokenizer, - __webpack_exports__Gemma2ForCausalLM as Gemma2ForCausalLM, - __webpack_exports__Gemma2Model as Gemma2Model, - __webpack_exports__Gemma2PreTrainedModel as Gemma2PreTrainedModel, - __webpack_exports__GemmaForCausalLM as GemmaForCausalLM, - __webpack_exports__GemmaModel as GemmaModel, - __webpack_exports__GemmaPreTrainedModel as GemmaPreTrainedModel, - __webpack_exports__GemmaTokenizer as GemmaTokenizer, - __webpack_exports__GraniteForCausalLM as GraniteForCausalLM, - __webpack_exports__GraniteModel as GraniteModel, - __webpack_exports__GranitePreTrainedModel as GranitePreTrainedModel, - __webpack_exports__Grok1Tokenizer as Grok1Tokenizer, - __webpack_exports__GroupViTModel as GroupViTModel, - __webpack_exports__GroupViTPreTrainedModel as GroupViTPreTrainedModel, - __webpack_exports__HerbertTokenizer as HerbertTokenizer, - __webpack_exports__HieraForImageClassification as HieraForImageClassification, - __webpack_exports__HieraModel as HieraModel, - __webpack_exports__HieraPreTrainedModel as HieraPreTrainedModel, - __webpack_exports__HubertForCTC as HubertForCTC, - __webpack_exports__HubertForSequenceClassification as HubertForSequenceClassification, - __webpack_exports__HubertModel as HubertModel, - __webpack_exports__HubertPreTrainedModel as HubertPreTrainedModel, - __webpack_exports__ImageClassificationPipeline as ImageClassificationPipeline, - __webpack_exports__ImageFeatureExtractionPipeline as ImageFeatureExtractionPipeline, - __webpack_exports__ImageFeatureExtractor as ImageFeatureExtractor, - __webpack_exports__ImageMattingOutput as ImageMattingOutput, - __webpack_exports__ImageSegmentationPipeline as ImageSegmentationPipeline, - __webpack_exports__ImageToImagePipeline as ImageToImagePipeline, - __webpack_exports__ImageToTextPipeline as ImageToTextPipeline, - __webpack_exports__InterruptableStoppingCriteria as InterruptableStoppingCriteria, - __webpack_exports__JAISLMHeadModel as JAISLMHeadModel, - __webpack_exports__JAISModel as JAISModel, - __webpack_exports__JAISPreTrainedModel as JAISPreTrainedModel, - __webpack_exports__LlamaForCausalLM as LlamaForCausalLM, - __webpack_exports__LlamaModel as LlamaModel, - __webpack_exports__LlamaPreTrainedModel as LlamaPreTrainedModel, - __webpack_exports__LlamaTokenizer as LlamaTokenizer, - __webpack_exports__LlavaForConditionalGeneration as LlavaForConditionalGeneration, - __webpack_exports__LlavaPreTrainedModel as LlavaPreTrainedModel, - __webpack_exports__LongT5ForConditionalGeneration as LongT5ForConditionalGeneration, - __webpack_exports__LongT5Model as LongT5Model, - __webpack_exports__LongT5PreTrainedModel as LongT5PreTrainedModel, - __webpack_exports__M2M100ForConditionalGeneration as M2M100ForConditionalGeneration, - __webpack_exports__M2M100Model as M2M100Model, - __webpack_exports__M2M100PreTrainedModel as M2M100PreTrainedModel, - __webpack_exports__M2M100Tokenizer as M2M100Tokenizer, - __webpack_exports__MBart50Tokenizer as MBart50Tokenizer, - __webpack_exports__MBartForCausalLM as MBartForCausalLM, - __webpack_exports__MBartForConditionalGeneration as MBartForConditionalGeneration, - __webpack_exports__MBartForSequenceClassification as MBartForSequenceClassification, - __webpack_exports__MBartModel as MBartModel, - __webpack_exports__MBartPreTrainedModel as MBartPreTrainedModel, - __webpack_exports__MBartTokenizer as MBartTokenizer, - __webpack_exports__MPNetForMaskedLM as MPNetForMaskedLM, - __webpack_exports__MPNetForQuestionAnswering as MPNetForQuestionAnswering, - __webpack_exports__MPNetForSequenceClassification as MPNetForSequenceClassification, - __webpack_exports__MPNetForTokenClassification as MPNetForTokenClassification, - __webpack_exports__MPNetModel as MPNetModel, - __webpack_exports__MPNetPreTrainedModel as MPNetPreTrainedModel, - __webpack_exports__MPNetTokenizer as MPNetTokenizer, - __webpack_exports__MT5ForConditionalGeneration as MT5ForConditionalGeneration, - __webpack_exports__MT5Model as MT5Model, - __webpack_exports__MT5PreTrainedModel as MT5PreTrainedModel, - __webpack_exports__MarianMTModel as MarianMTModel, - __webpack_exports__MarianModel as MarianModel, - __webpack_exports__MarianPreTrainedModel as MarianPreTrainedModel, - __webpack_exports__MarianTokenizer as MarianTokenizer, - __webpack_exports__MaskFormerFeatureExtractor as MaskFormerFeatureExtractor, - __webpack_exports__MaskFormerForInstanceSegmentation as MaskFormerForInstanceSegmentation, - __webpack_exports__MaskFormerModel as MaskFormerModel, - __webpack_exports__MaskFormerPreTrainedModel as MaskFormerPreTrainedModel, - __webpack_exports__MaskedLMOutput as MaskedLMOutput, - __webpack_exports__MaxLengthCriteria as MaxLengthCriteria, - __webpack_exports__MistralForCausalLM as MistralForCausalLM, - __webpack_exports__MistralModel as MistralModel, - __webpack_exports__MistralPreTrainedModel as MistralPreTrainedModel, - __webpack_exports__MobileBertForMaskedLM as MobileBertForMaskedLM, - __webpack_exports__MobileBertForQuestionAnswering as MobileBertForQuestionAnswering, - __webpack_exports__MobileBertForSequenceClassification as MobileBertForSequenceClassification, - __webpack_exports__MobileBertModel as MobileBertModel, - __webpack_exports__MobileBertPreTrainedModel as MobileBertPreTrainedModel, - __webpack_exports__MobileBertTokenizer as MobileBertTokenizer, - __webpack_exports__MobileNetV1FeatureExtractor as MobileNetV1FeatureExtractor, - __webpack_exports__MobileNetV1ForImageClassification as MobileNetV1ForImageClassification, - __webpack_exports__MobileNetV1Model as MobileNetV1Model, - __webpack_exports__MobileNetV1PreTrainedModel as MobileNetV1PreTrainedModel, - __webpack_exports__MobileNetV2FeatureExtractor as MobileNetV2FeatureExtractor, - __webpack_exports__MobileNetV2ForImageClassification as MobileNetV2ForImageClassification, - __webpack_exports__MobileNetV2Model as MobileNetV2Model, - __webpack_exports__MobileNetV2PreTrainedModel as MobileNetV2PreTrainedModel, - __webpack_exports__MobileNetV3FeatureExtractor as MobileNetV3FeatureExtractor, - __webpack_exports__MobileNetV3ForImageClassification as MobileNetV3ForImageClassification, - __webpack_exports__MobileNetV3Model as MobileNetV3Model, - __webpack_exports__MobileNetV3PreTrainedModel as MobileNetV3PreTrainedModel, - __webpack_exports__MobileNetV4FeatureExtractor as MobileNetV4FeatureExtractor, - __webpack_exports__MobileNetV4ForImageClassification as MobileNetV4ForImageClassification, - __webpack_exports__MobileNetV4Model as MobileNetV4Model, - __webpack_exports__MobileNetV4PreTrainedModel as MobileNetV4PreTrainedModel, - __webpack_exports__MobileViTFeatureExtractor as MobileViTFeatureExtractor, - __webpack_exports__MobileViTForImageClassification as MobileViTForImageClassification, - __webpack_exports__MobileViTImageProcessor as MobileViTImageProcessor, - __webpack_exports__MobileViTModel as MobileViTModel, - __webpack_exports__MobileViTPreTrainedModel as MobileViTPreTrainedModel, - __webpack_exports__MobileViTV2ForImageClassification as MobileViTV2ForImageClassification, - __webpack_exports__MobileViTV2Model as MobileViTV2Model, - __webpack_exports__MobileViTV2PreTrainedModel as MobileViTV2PreTrainedModel, - __webpack_exports__ModelOutput as ModelOutput, - __webpack_exports__Moondream1ForConditionalGeneration as Moondream1ForConditionalGeneration, - __webpack_exports__MptForCausalLM as MptForCausalLM, - __webpack_exports__MptModel as MptModel, - __webpack_exports__MptPreTrainedModel as MptPreTrainedModel, - __webpack_exports__MusicgenForCausalLM as MusicgenForCausalLM, - __webpack_exports__MusicgenForConditionalGeneration as MusicgenForConditionalGeneration, - __webpack_exports__MusicgenModel as MusicgenModel, - __webpack_exports__MusicgenPreTrainedModel as MusicgenPreTrainedModel, - __webpack_exports__NllbTokenizer as NllbTokenizer, - __webpack_exports__NomicBertModel as NomicBertModel, - __webpack_exports__NomicBertPreTrainedModel as NomicBertPreTrainedModel, - __webpack_exports__NougatImageProcessor as NougatImageProcessor, - __webpack_exports__NougatTokenizer as NougatTokenizer, - __webpack_exports__OPTForCausalLM as OPTForCausalLM, - __webpack_exports__OPTModel as OPTModel, - __webpack_exports__OPTPreTrainedModel as OPTPreTrainedModel, - __webpack_exports__ObjectDetectionPipeline as ObjectDetectionPipeline, - __webpack_exports__OpenELMForCausalLM as OpenELMForCausalLM, - __webpack_exports__OpenELMModel as OpenELMModel, - __webpack_exports__OpenELMPreTrainedModel as OpenELMPreTrainedModel, - __webpack_exports__OwlViTFeatureExtractor as OwlViTFeatureExtractor, - __webpack_exports__OwlViTForObjectDetection as OwlViTForObjectDetection, - __webpack_exports__OwlViTModel as OwlViTModel, - __webpack_exports__OwlViTPreTrainedModel as OwlViTPreTrainedModel, - __webpack_exports__OwlViTProcessor as OwlViTProcessor, - __webpack_exports__Owlv2ForObjectDetection as Owlv2ForObjectDetection, - __webpack_exports__Owlv2ImageProcessor as Owlv2ImageProcessor, - __webpack_exports__Owlv2Model as Owlv2Model, - __webpack_exports__Owlv2PreTrainedModel as Owlv2PreTrainedModel, - __webpack_exports__Phi3ForCausalLM as Phi3ForCausalLM, - __webpack_exports__Phi3Model as Phi3Model, - __webpack_exports__Phi3PreTrainedModel as Phi3PreTrainedModel, - __webpack_exports__PhiForCausalLM as PhiForCausalLM, - __webpack_exports__PhiModel as PhiModel, - __webpack_exports__PhiPreTrainedModel as PhiPreTrainedModel, - __webpack_exports__Pipeline as Pipeline, - __webpack_exports__PreTrainedModel as PreTrainedModel, - __webpack_exports__PreTrainedTokenizer as PreTrainedTokenizer, - __webpack_exports__PretrainedConfig as PretrainedConfig, - __webpack_exports__PretrainedMixin as PretrainedMixin, - __webpack_exports__Processor as Processor, - __webpack_exports__PvtForImageClassification as PvtForImageClassification, - __webpack_exports__PvtImageProcessor as PvtImageProcessor, - __webpack_exports__PvtModel as PvtModel, - __webpack_exports__PvtPreTrainedModel as PvtPreTrainedModel, - __webpack_exports__PyAnnoteFeatureExtractor as PyAnnoteFeatureExtractor, - __webpack_exports__PyAnnoteForAudioFrameClassification as PyAnnoteForAudioFrameClassification, - __webpack_exports__PyAnnoteModel as PyAnnoteModel, - __webpack_exports__PyAnnotePreTrainedModel as PyAnnotePreTrainedModel, - __webpack_exports__PyAnnoteProcessor as PyAnnoteProcessor, - __webpack_exports__QuestionAnsweringModelOutput as QuestionAnsweringModelOutput, - __webpack_exports__QuestionAnsweringPipeline as QuestionAnsweringPipeline, - __webpack_exports__Qwen2ForCausalLM as Qwen2ForCausalLM, - __webpack_exports__Qwen2Model as Qwen2Model, - __webpack_exports__Qwen2PreTrainedModel as Qwen2PreTrainedModel, - __webpack_exports__Qwen2Tokenizer as Qwen2Tokenizer, - __webpack_exports__RTDetrForObjectDetection as RTDetrForObjectDetection, - __webpack_exports__RTDetrImageProcessor as RTDetrImageProcessor, - __webpack_exports__RTDetrModel as RTDetrModel, - __webpack_exports__RTDetrObjectDetectionOutput as RTDetrObjectDetectionOutput, - __webpack_exports__RTDetrPreTrainedModel as RTDetrPreTrainedModel, - __webpack_exports__RawImage as RawImage, - __webpack_exports__ResNetForImageClassification as ResNetForImageClassification, - __webpack_exports__ResNetModel as ResNetModel, - __webpack_exports__ResNetPreTrainedModel as ResNetPreTrainedModel, - __webpack_exports__RoFormerForMaskedLM as RoFormerForMaskedLM, - __webpack_exports__RoFormerForQuestionAnswering as RoFormerForQuestionAnswering, - __webpack_exports__RoFormerForSequenceClassification as RoFormerForSequenceClassification, - __webpack_exports__RoFormerForTokenClassification as RoFormerForTokenClassification, - __webpack_exports__RoFormerModel as RoFormerModel, - __webpack_exports__RoFormerPreTrainedModel as RoFormerPreTrainedModel, - __webpack_exports__RoFormerTokenizer as RoFormerTokenizer, - __webpack_exports__RobertaForMaskedLM as RobertaForMaskedLM, - __webpack_exports__RobertaForQuestionAnswering as RobertaForQuestionAnswering, - __webpack_exports__RobertaForSequenceClassification as RobertaForSequenceClassification, - __webpack_exports__RobertaForTokenClassification as RobertaForTokenClassification, - __webpack_exports__RobertaModel as RobertaModel, - __webpack_exports__RobertaPreTrainedModel as RobertaPreTrainedModel, - __webpack_exports__RobertaTokenizer as RobertaTokenizer, - __webpack_exports__SamImageProcessor as SamImageProcessor, - __webpack_exports__SamImageSegmentationOutput as SamImageSegmentationOutput, - __webpack_exports__SamModel as SamModel, - __webpack_exports__SamPreTrainedModel as SamPreTrainedModel, - __webpack_exports__SamProcessor as SamProcessor, - __webpack_exports__SapiensFeatureExtractor as SapiensFeatureExtractor, - __webpack_exports__SapiensForDepthEstimation as SapiensForDepthEstimation, - __webpack_exports__SapiensForNormalEstimation as SapiensForNormalEstimation, - __webpack_exports__SapiensForSemanticSegmentation as SapiensForSemanticSegmentation, - __webpack_exports__SapiensPreTrainedModel as SapiensPreTrainedModel, - __webpack_exports__SeamlessM4TFeatureExtractor as SeamlessM4TFeatureExtractor, - __webpack_exports__SegformerFeatureExtractor as SegformerFeatureExtractor, - __webpack_exports__SegformerForImageClassification as SegformerForImageClassification, - __webpack_exports__SegformerForSemanticSegmentation as SegformerForSemanticSegmentation, - __webpack_exports__SegformerModel as SegformerModel, - __webpack_exports__SegformerPreTrainedModel as SegformerPreTrainedModel, - __webpack_exports__Seq2SeqLMOutput as Seq2SeqLMOutput, - __webpack_exports__SequenceClassifierOutput as SequenceClassifierOutput, - __webpack_exports__SiglipImageProcessor as SiglipImageProcessor, - __webpack_exports__SiglipModel as SiglipModel, - __webpack_exports__SiglipPreTrainedModel as SiglipPreTrainedModel, - __webpack_exports__SiglipTextModel as SiglipTextModel, - __webpack_exports__SiglipTokenizer as SiglipTokenizer, - __webpack_exports__SiglipVisionModel as SiglipVisionModel, - __webpack_exports__SpeechT5FeatureExtractor as SpeechT5FeatureExtractor, - __webpack_exports__SpeechT5ForSpeechToText as SpeechT5ForSpeechToText, - __webpack_exports__SpeechT5ForTextToSpeech as SpeechT5ForTextToSpeech, - __webpack_exports__SpeechT5HifiGan as SpeechT5HifiGan, - __webpack_exports__SpeechT5Model as SpeechT5Model, - __webpack_exports__SpeechT5PreTrainedModel as SpeechT5PreTrainedModel, - __webpack_exports__SpeechT5Processor as SpeechT5Processor, - __webpack_exports__SpeechT5Tokenizer as SpeechT5Tokenizer, - __webpack_exports__SqueezeBertForMaskedLM as SqueezeBertForMaskedLM, - __webpack_exports__SqueezeBertForQuestionAnswering as SqueezeBertForQuestionAnswering, - __webpack_exports__SqueezeBertForSequenceClassification as SqueezeBertForSequenceClassification, - __webpack_exports__SqueezeBertModel as SqueezeBertModel, - __webpack_exports__SqueezeBertPreTrainedModel as SqueezeBertPreTrainedModel, - __webpack_exports__SqueezeBertTokenizer as SqueezeBertTokenizer, - __webpack_exports__StableLmForCausalLM as StableLmForCausalLM, - __webpack_exports__StableLmModel as StableLmModel, - __webpack_exports__StableLmPreTrainedModel as StableLmPreTrainedModel, - __webpack_exports__Starcoder2ForCausalLM as Starcoder2ForCausalLM, - __webpack_exports__Starcoder2Model as Starcoder2Model, - __webpack_exports__Starcoder2PreTrainedModel as Starcoder2PreTrainedModel, - __webpack_exports__StoppingCriteria as StoppingCriteria, - __webpack_exports__StoppingCriteriaList as StoppingCriteriaList, - __webpack_exports__SummarizationPipeline as SummarizationPipeline, - __webpack_exports__Swin2SRForImageSuperResolution as Swin2SRForImageSuperResolution, - __webpack_exports__Swin2SRImageProcessor as Swin2SRImageProcessor, - __webpack_exports__Swin2SRModel as Swin2SRModel, - __webpack_exports__Swin2SRPreTrainedModel as Swin2SRPreTrainedModel, - __webpack_exports__SwinForImageClassification as SwinForImageClassification, - __webpack_exports__SwinModel as SwinModel, - __webpack_exports__SwinPreTrainedModel as SwinPreTrainedModel, - __webpack_exports__T5ForConditionalGeneration as T5ForConditionalGeneration, - __webpack_exports__T5Model as T5Model, - __webpack_exports__T5PreTrainedModel as T5PreTrainedModel, - __webpack_exports__T5Tokenizer as T5Tokenizer, - __webpack_exports__TableTransformerForObjectDetection as TableTransformerForObjectDetection, - __webpack_exports__TableTransformerModel as TableTransformerModel, - __webpack_exports__TableTransformerObjectDetectionOutput as TableTransformerObjectDetectionOutput, - __webpack_exports__TableTransformerPreTrainedModel as TableTransformerPreTrainedModel, - __webpack_exports__Tensor as Tensor, - __webpack_exports__Text2TextGenerationPipeline as Text2TextGenerationPipeline, - __webpack_exports__TextClassificationPipeline as TextClassificationPipeline, - __webpack_exports__TextGenerationPipeline as TextGenerationPipeline, - __webpack_exports__TextStreamer as TextStreamer, - __webpack_exports__TextToAudioPipeline as TextToAudioPipeline, - __webpack_exports__TokenClassificationPipeline as TokenClassificationPipeline, - __webpack_exports__TokenClassifierOutput as TokenClassifierOutput, - __webpack_exports__TokenizerModel as TokenizerModel, - __webpack_exports__TrOCRForCausalLM as TrOCRForCausalLM, - __webpack_exports__TrOCRPreTrainedModel as TrOCRPreTrainedModel, - __webpack_exports__TranslationPipeline as TranslationPipeline, - __webpack_exports__UniSpeechForCTC as UniSpeechForCTC, - __webpack_exports__UniSpeechForSequenceClassification as UniSpeechForSequenceClassification, - __webpack_exports__UniSpeechModel as UniSpeechModel, - __webpack_exports__UniSpeechPreTrainedModel as UniSpeechPreTrainedModel, - __webpack_exports__UniSpeechSatForAudioFrameClassification as UniSpeechSatForAudioFrameClassification, - __webpack_exports__UniSpeechSatForCTC as UniSpeechSatForCTC, - __webpack_exports__UniSpeechSatForSequenceClassification as UniSpeechSatForSequenceClassification, - __webpack_exports__UniSpeechSatModel as UniSpeechSatModel, - __webpack_exports__UniSpeechSatPreTrainedModel as UniSpeechSatPreTrainedModel, - __webpack_exports__ViTFeatureExtractor as ViTFeatureExtractor, - __webpack_exports__ViTForImageClassification as ViTForImageClassification, - __webpack_exports__ViTImageProcessor as ViTImageProcessor, - __webpack_exports__ViTMAEModel as ViTMAEModel, - __webpack_exports__ViTMAEPreTrainedModel as ViTMAEPreTrainedModel, - __webpack_exports__ViTMSNForImageClassification as ViTMSNForImageClassification, - __webpack_exports__ViTMSNModel as ViTMSNModel, - __webpack_exports__ViTMSNPreTrainedModel as ViTMSNPreTrainedModel, - __webpack_exports__ViTModel as ViTModel, - __webpack_exports__ViTPreTrainedModel as ViTPreTrainedModel, - __webpack_exports__VisionEncoderDecoderModel as VisionEncoderDecoderModel, - __webpack_exports__VitMatteForImageMatting as VitMatteForImageMatting, - __webpack_exports__VitMatteImageProcessor as VitMatteImageProcessor, - __webpack_exports__VitMattePreTrainedModel as VitMattePreTrainedModel, - __webpack_exports__VitsModel as VitsModel, - __webpack_exports__VitsModelOutput as VitsModelOutput, - __webpack_exports__VitsPreTrainedModel as VitsPreTrainedModel, - __webpack_exports__VitsTokenizer as VitsTokenizer, - __webpack_exports__Wav2Vec2BertForCTC as Wav2Vec2BertForCTC, - __webpack_exports__Wav2Vec2BertForSequenceClassification as Wav2Vec2BertForSequenceClassification, - __webpack_exports__Wav2Vec2BertModel as Wav2Vec2BertModel, - __webpack_exports__Wav2Vec2BertPreTrainedModel as Wav2Vec2BertPreTrainedModel, - __webpack_exports__Wav2Vec2CTCTokenizer as Wav2Vec2CTCTokenizer, - __webpack_exports__Wav2Vec2FeatureExtractor as Wav2Vec2FeatureExtractor, - __webpack_exports__Wav2Vec2ForAudioFrameClassification as Wav2Vec2ForAudioFrameClassification, - __webpack_exports__Wav2Vec2ForCTC as Wav2Vec2ForCTC, - __webpack_exports__Wav2Vec2ForSequenceClassification as Wav2Vec2ForSequenceClassification, - __webpack_exports__Wav2Vec2Model as Wav2Vec2Model, - __webpack_exports__Wav2Vec2PreTrainedModel as Wav2Vec2PreTrainedModel, - __webpack_exports__Wav2Vec2ProcessorWithLM as Wav2Vec2ProcessorWithLM, - __webpack_exports__WavLMForAudioFrameClassification as WavLMForAudioFrameClassification, - __webpack_exports__WavLMForCTC as WavLMForCTC, - __webpack_exports__WavLMForSequenceClassification as WavLMForSequenceClassification, - __webpack_exports__WavLMForXVector as WavLMForXVector, - __webpack_exports__WavLMModel as WavLMModel, - __webpack_exports__WavLMPreTrainedModel as WavLMPreTrainedModel, - __webpack_exports__WeSpeakerFeatureExtractor as WeSpeakerFeatureExtractor, - __webpack_exports__WeSpeakerResNetModel as WeSpeakerResNetModel, - __webpack_exports__WeSpeakerResNetPreTrainedModel as WeSpeakerResNetPreTrainedModel, - __webpack_exports__WhisperFeatureExtractor as WhisperFeatureExtractor, - __webpack_exports__WhisperForConditionalGeneration as WhisperForConditionalGeneration, - __webpack_exports__WhisperModel as WhisperModel, - __webpack_exports__WhisperPreTrainedModel as WhisperPreTrainedModel, - __webpack_exports__WhisperProcessor as WhisperProcessor, - __webpack_exports__WhisperTextStreamer as WhisperTextStreamer, - __webpack_exports__WhisperTokenizer as WhisperTokenizer, - __webpack_exports__XLMForQuestionAnswering as XLMForQuestionAnswering, - __webpack_exports__XLMForSequenceClassification as XLMForSequenceClassification, - __webpack_exports__XLMForTokenClassification as XLMForTokenClassification, - __webpack_exports__XLMModel as XLMModel, - __webpack_exports__XLMPreTrainedModel as XLMPreTrainedModel, - __webpack_exports__XLMRobertaForMaskedLM as XLMRobertaForMaskedLM, - __webpack_exports__XLMRobertaForQuestionAnswering as XLMRobertaForQuestionAnswering, - __webpack_exports__XLMRobertaForSequenceClassification as XLMRobertaForSequenceClassification, - __webpack_exports__XLMRobertaForTokenClassification as XLMRobertaForTokenClassification, - __webpack_exports__XLMRobertaModel as XLMRobertaModel, - __webpack_exports__XLMRobertaPreTrainedModel as XLMRobertaPreTrainedModel, - __webpack_exports__XLMRobertaTokenizer as XLMRobertaTokenizer, - __webpack_exports__XLMTokenizer as XLMTokenizer, - __webpack_exports__XLMWithLMHeadModel as XLMWithLMHeadModel, - __webpack_exports__XVectorOutput as XVectorOutput, - __webpack_exports__YolosFeatureExtractor as YolosFeatureExtractor, - __webpack_exports__YolosForObjectDetection as YolosForObjectDetection, - __webpack_exports__YolosModel as YolosModel, - __webpack_exports__YolosObjectDetectionOutput as YolosObjectDetectionOutput, - __webpack_exports__YolosPreTrainedModel as YolosPreTrainedModel, - __webpack_exports__ZeroShotAudioClassificationPipeline as ZeroShotAudioClassificationPipeline, - __webpack_exports__ZeroShotClassificationPipeline as ZeroShotClassificationPipeline, - __webpack_exports__ZeroShotImageClassificationPipeline as ZeroShotImageClassificationPipeline, - __webpack_exports__ZeroShotObjectDetectionPipeline as ZeroShotObjectDetectionPipeline, - __webpack_exports__bankers_round as bankers_round, - __webpack_exports__cat as cat, - __webpack_exports__cos_sim as cos_sim, - __webpack_exports__dot as dot, - __webpack_exports__dynamic_time_warping as dynamic_time_warping, - __webpack_exports__env as env, - __webpack_exports__full as full, - __webpack_exports__full_like as full_like, - __webpack_exports__getKeyValueShapes as getKeyValueShapes, - __webpack_exports__hamming as hamming, - __webpack_exports__hanning as hanning, - __webpack_exports__interpolate as interpolate, - __webpack_exports__interpolate_4d as interpolate_4d, - __webpack_exports__interpolate_data as interpolate_data, - __webpack_exports__is_chinese_char as is_chinese_char, - __webpack_exports__layer_norm as layer_norm, - __webpack_exports__log_softmax as log_softmax, - __webpack_exports__magnitude as magnitude, - __webpack_exports__matmul as matmul, - __webpack_exports__max as max, - __webpack_exports__mean as mean, - __webpack_exports__mean_pooling as mean_pooling, - __webpack_exports__medianFilter as medianFilter, - __webpack_exports__mel_filter_bank as mel_filter_bank, - __webpack_exports__min as min, - __webpack_exports__ones as ones, - __webpack_exports__ones_like as ones_like, - __webpack_exports__permute as permute, - __webpack_exports__permute_data as permute_data, - __webpack_exports__pipeline as pipeline, - __webpack_exports__quantize_embeddings as quantize_embeddings, - __webpack_exports__read_audio as read_audio, - __webpack_exports__rfft as rfft, - __webpack_exports__round as round, - __webpack_exports__softmax as softmax, - __webpack_exports__spectrogram as spectrogram, - __webpack_exports__stack as stack, - __webpack_exports__std_mean as std_mean, - __webpack_exports__topk as topk, - __webpack_exports__window_function as window_function, - __webpack_exports__zeros as zeros, - __webpack_exports__zeros_like as zeros_like - }; - /*! Bundled license information: - - @huggingface/transformers/dist/transformers.js: - (*! - * ONNX Runtime Web v1.20.0-dev.20241016-2b8fc5529b - * Copyright (c) Microsoft Corporation. All rights reserved. - * Licensed under the MIT License. - *) - (** - * @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. - * ============================================================================= - *) - (*!*****************************!*\ - !*** ./src/transformers.js ***! - \*****************************) - */ - //# sourceMappingURL=@huggingface_transformers.js.map - \ No newline at end of file diff --git a/packages/webgpu/cpp/rnwgpu/api/GPUAdapterInfo.h b/packages/webgpu/cpp/rnwgpu/api/GPUAdapterInfo.h index 327d03c93..f001086f6 100644 --- a/packages/webgpu/cpp/rnwgpu/api/GPUAdapterInfo.h +++ b/packages/webgpu/cpp/rnwgpu/api/GPUAdapterInfo.h @@ -25,27 +25,17 @@ class GPUAdapterInfo : public m::HybridObject { std::string getBrand() { return _name; } std::string getVendor() { - if (_instance.vendor.length) { - return _instance.vendor.data; - } - return ""; + return "apple"; } std::string getArchitecture() { -// if (_instance.architecture.length) { -// return _instance.architecture.data; -// } - return ""; + return "common-3"; } std::string getDevice() { -// if (_instance.device.length) { -// return _instance.device.data; -// } + return ""; } std::string getDescription() { -// if (_instance.device.length) { -// return _instance.device.data; -// } + return ""; } diff --git a/packages/webgpu/cpp/rnwgpu/api/descriptors/GPUDeviceDescriptor.h b/packages/webgpu/cpp/rnwgpu/api/descriptors/GPUDeviceDescriptor.h index cc58c2995..12cdfe59f 100644 --- a/packages/webgpu/cpp/rnwgpu/api/descriptors/GPUDeviceDescriptor.h +++ b/packages/webgpu/cpp/rnwgpu/api/descriptors/GPUDeviceDescriptor.h @@ -40,19 +40,19 @@ template <> struct JSIConverter> { auto result = std::make_unique(); if (!outOfBounds && arg.isObject()) { auto value = arg.getObject(runtime); - if (value.hasProperty(runtime, "requiredFeatures")) { - auto prop = value.getProperty(runtime, "requiredFeatures"); - result->requiredFeatures = JSIConverter< - std::optional>>::fromJSI(runtime, - prop, - false); - } - if (value.hasProperty(runtime, "requiredLimits")) { - auto prop = value.getProperty(runtime, "requiredLimits"); - result->requiredLimits = - JSIConverter>>::fromJSI( - runtime, prop, false); - } +// if (value.hasProperty(runtime, "requiredFeatures")) { +// auto prop = value.getProperty(runtime, "requiredFeatures"); +// result->requiredFeatures = JSIConverter< +// std::optional>>::fromJSI(runtime, +// prop, +// false); +// } +// if (value.hasProperty(runtime, "requiredLimits")) { +// auto prop = value.getProperty(runtime, "requiredLimits"); +// result->requiredLimits = +// JSIConverter>>::fromJSI( +// runtime, prop, false); +// } if (value.hasProperty(runtime, "defaultQueue")) { auto prop = value.getProperty(runtime, "defaultQueue"); result->defaultQueue = @@ -74,4 +74,4 @@ template <> struct JSIConverter> { } }; -} // namespace margelo \ No newline at end of file +} // namespace margelo diff --git a/yarn.lock b/yarn.lock index fe75086fa..447b1a18a 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3528,6 +3528,23 @@ __metadata: languageName: node linkType: hard +"@tensorflow/tfjs-vis@npm:^1.5.1": + version: 1.5.1 + resolution: "@tensorflow/tfjs-vis@npm:1.5.1" + dependencies: + d3-format: ~1.3.0 + d3-selection: ~1.3.0 + glamor: ~2.20.40 + preact: ~8.2.9 + vega: 5.20.0 + vega-embed: 6.17.0 + vega-lite: 4.13.1 + peerDependencies: + "@tensorflow/tfjs-core": ">= 1.0.0" + checksum: cc50525ac7510143dc7e633b88b1af29ab816b83ff92a32dd365a787bb4d90f536ec8a0184e2a77464458a9bb93b7cb3e833c2e51454927b51708670774daaa6 + languageName: node + linkType: hard + "@tensorflow/tfjs@npm:^4.22.0": version: 4.22.0 resolution: "@tensorflow/tfjs@npm:4.22.0" @@ -3644,6 +3661,13 @@ __metadata: languageName: node linkType: hard +"@types/clone@npm:~0.1.30": + version: 0.1.30 + resolution: "@types/clone@npm:0.1.30" + checksum: 5e036c4a2b380ca0af5c471cdf20487ab0b407e2fadad785ce328e156d11b42873169fa1192801fba91a4d15e1508999aa3f16030dce40fe08cc99701ac83875 + languageName: node + linkType: hard + "@types/debounce@npm:^1.2.1": version: 1.2.4 resolution: "@types/debounce@npm:1.2.4" @@ -3651,6 +3675,20 @@ __metadata: languageName: node linkType: hard +"@types/estree@npm:^1.0.0": + version: 1.0.6 + resolution: "@types/estree@npm:1.0.6" + checksum: 8825d6e729e16445d9a1dd2fb1db2edc5ed400799064cd4d028150701031af012ba30d6d03fe9df40f4d7a437d0de6d2b256020152b7b09bde9f2e420afdffd9 + languageName: node + linkType: hard + +"@types/fast-json-stable-stringify@npm:^2.0.0": + version: 2.0.0 + resolution: "@types/fast-json-stable-stringify@npm:2.0.0" + checksum: 021f80e037327fcb055eb650850c0df66ba56a98d2023dd79f47a3bf54d1a009a164da1f27436c9a26e116eff24fe7e32d1583dea77405b1b0468889b57e11a7 + languageName: node + linkType: hard + "@types/graceful-fs@npm:^4.1.3": version: 4.1.9 resolution: "@types/graceful-fs@npm:4.1.9" @@ -4481,6 +4519,7 @@ __metadata: "@rnx-kit/metro-config": ^1.3.17 "@tensorflow/tfjs": ^4.22.0 "@tensorflow/tfjs-backend-webgpu": ^4.22.0 + "@tensorflow/tfjs-vis": ^1.5.1 "@types/node": ^20.14.7 "@types/react": ^18.2.6 "@types/react-test-renderer": ^18.0.0 @@ -4746,6 +4785,13 @@ __metadata: languageName: node linkType: hard +"array-flat-polyfill@npm:^1.0.1": + version: 1.0.1 + resolution: "array-flat-polyfill@npm:1.0.1" + checksum: 5d578b191a7f145a1351a4962df9a14d905060c7dfcd8f85062954b7a44b2bff1c9d2bff2d56b07756de774d5e9e4feafe4572f5641b1e9c8a968aca5cbe4902 + languageName: node + linkType: hard + "array-includes@npm:^3.1.6, array-includes@npm:^3.1.8": version: 3.1.8 resolution: "array-includes@npm:3.1.8" @@ -4841,7 +4887,7 @@ __metadata: languageName: node linkType: hard -"asap@npm:~2.0.6": +"asap@npm:~2.0.3, asap@npm:~2.0.6": version: 2.0.6 resolution: "asap@npm:2.0.6" checksum: b296c92c4b969e973260e47523207cd5769abd27c245a68c26dc7a0fe8053c55bb04360237cb51cab1df52be939da77150ace99ad331fb7fb13b3423ed73ff3d @@ -5130,6 +5176,13 @@ __metadata: languageName: node linkType: hard +"bowser@npm:^1.7.3": + version: 1.9.4 + resolution: "bowser@npm:1.9.4" + checksum: 127584ee1b8f0c27f410f652d409ea8bcb23d185a4269bcbe0229069720be9d83dc80a939e0fa33d8a9055141a0cf2fee5a02b2b5515c38841ddc899d67dec8d + languageName: node + linkType: hard + "brace-expansion@npm:^1.1.7": version: 1.1.11 resolution: "brace-expansion@npm:1.1.11" @@ -5501,6 +5554,13 @@ __metadata: languageName: node linkType: hard +"clone@npm:~2.1.2": + version: 2.1.2 + resolution: "clone@npm:2.1.2" + checksum: aaf106e9bc025b21333e2f4c12da539b568db4925c0501a1bf4070836c9e848c892fa22c35548ce0d1132b08bbbfa17a00144fe58fccdab6fa900fec4250f67d + languageName: node + linkType: hard + "co@npm:^4.6.0": version: 4.6.0 resolution: "co@npm:4.6.0" @@ -5597,13 +5657,20 @@ __metadata: languageName: node linkType: hard -"commander@npm:^2.20.0": +"commander@npm:2, commander@npm:^2.20.0": version: 2.20.3 resolution: "commander@npm:2.20.3" checksum: ab8c07884e42c3a8dbc5dd9592c606176c7eb5c1ca5ff274bcf907039b2c41de3626f684ea75ccf4d361ba004bbaff1f577d5384c155f3871e456bdf27becf9e languageName: node linkType: hard +"commander@npm:7": + version: 7.2.0 + resolution: "commander@npm:7.2.0" + checksum: 53501cbeee61d5157546c0bef0fedb6cdfc763a882136284bed9a07225f09a14b82d2a84e7637edfd1a679fb35ed9502fd58ef1d091e6287f60d790147f68ddc + languageName: node + linkType: hard + "commander@npm:^9.4.1": version: 9.5.0 resolution: "commander@npm:9.5.0" @@ -5684,6 +5751,13 @@ __metadata: languageName: node linkType: hard +"core-js@npm:^1.0.0": + version: 1.2.7 + resolution: "core-js@npm:1.2.7" + checksum: 0b76371bfa98708351cde580f9287e2360d2209920e738ae950ae74ad08639a2e063541020bf666c28778956fc356ed9fe56d962129c88a87a6a4a0612526c75 + languageName: node + linkType: hard + "core-util-is@npm:~1.0.0": version: 1.0.3 resolution: "core-util-is@npm:1.0.3" @@ -5768,6 +5842,16 @@ __metadata: languageName: node linkType: hard +"css-in-js-utils@npm:^2.0.0": + version: 2.0.1 + resolution: "css-in-js-utils@npm:2.0.1" + dependencies: + hyphenate-style-name: ^1.0.2 + isobject: ^3.0.1 + checksum: c9964c4708216954c468b69bbee2d971fd759ada4f40637b8ca4d3f79caba4818d0532a4f190ac560227c08742ad063ffec7a30afddc4d96b66a18c3a008f0d8 + languageName: node + linkType: hard + "csstype@npm:^3.0.2": version: 3.1.3 resolution: "csstype@npm:3.1.3" @@ -5775,6 +5859,331 @@ __metadata: languageName: node linkType: hard +"d3-array@npm:1 - 2, d3-array@npm:2, d3-array@npm:^2.3.0, d3-array@npm:^2.5.0, d3-array@npm:^2.7.1": + version: 2.12.1 + resolution: "d3-array@npm:2.12.1" + dependencies: + internmap: ^1.0.0 + checksum: 97853b7b523aded17078f37c67742f45d81e88dda2107ae9994c31b9e36c5fa5556c4c4cf39650436f247813602dfe31bf7ad067ff80f127a16903827f10c6eb + languageName: node + linkType: hard + +"d3-array@npm:1 - 3, d3-array@npm:2 - 3, d3-array@npm:2.10.0 - 3, d3-array@npm:2.5.0 - 3, d3-array@npm:3.2.4, d3-array@npm:^3.2.2": + version: 3.2.4 + resolution: "d3-array@npm:3.2.4" + dependencies: + internmap: 1 - 2 + checksum: a5976a6d6205f69208478bb44920dd7ce3e788c9dceb86b304dbe401a4bfb42ecc8b04c20facde486e9adcb488b5d1800d49393a3f81a23902b68158e12cddd0 + languageName: node + linkType: hard + +"d3-color@npm:1 - 2, d3-color@npm:^2.0.0": + version: 2.0.0 + resolution: "d3-color@npm:2.0.0" + checksum: b887354aa383937abd04fbffed3e26e5d6a788472cd3737fb10735930e427763e69fe93398663bccf88c0b53ee3e638ac6fcf0c02226b00ed9e4327c2dfbf3dc + languageName: node + linkType: hard + +"d3-color@npm:1 - 3, d3-color@npm:^3.1.0": + version: 3.1.0 + resolution: "d3-color@npm:3.1.0" + checksum: 4931fbfda5d7c4b5cfa283a13c91a954f86e3b69d75ce588d06cde6c3628cebfc3af2069ccf225e982e8987c612aa7948b3932163ce15eb3c11cd7c003f3ee3b + languageName: node + linkType: hard + +"d3-delaunay@npm:^5.3.0": + version: 5.3.0 + resolution: "d3-delaunay@npm:5.3.0" + dependencies: + delaunator: 4 + checksum: 3fa5ae167eb86e62ca0f9c3e8d05470b23572b4b480f05201705c0db976d403834cee1cdf264a41c97e45238e3999d48cc593f97d0da37229a42673a6bb10e95 + languageName: node + linkType: hard + +"d3-dispatch@npm:1 - 2": + version: 2.0.0 + resolution: "d3-dispatch@npm:2.0.0" + checksum: cf473676ae0df1915d51d056d2c6734ceec480d258611d970a01847c50e8c273c185032bf9ed491abd077696bcbeeb491dc94af53e888871f3a1a0fac7365cec + languageName: node + linkType: hard + +"d3-dsv@npm:^2.0.0": + version: 2.0.0 + resolution: "d3-dsv@npm:2.0.0" + dependencies: + commander: 2 + iconv-lite: 0.4 + rw: 1 + bin: + csv2json: bin/dsv2json + csv2tsv: bin/dsv2dsv + dsv2dsv: bin/dsv2dsv + dsv2json: bin/dsv2json + json2csv: bin/json2dsv + json2dsv: bin/json2dsv + json2tsv: bin/json2dsv + tsv2csv: bin/dsv2dsv + tsv2json: bin/dsv2json + checksum: 01b12d81e4ca3996f2e921388b1929c358a39711bf250f2c53dd0e452b80465ebe31ddb58a4064f160322dec7aaf2ceae1a249874af989404705fcfdf1e9b64d + languageName: node + linkType: hard + +"d3-dsv@npm:^3.0.1": + version: 3.0.1 + resolution: "d3-dsv@npm:3.0.1" + dependencies: + commander: 7 + iconv-lite: 0.6 + rw: 1 + bin: + csv2json: bin/dsv2json.js + csv2tsv: bin/dsv2dsv.js + dsv2dsv: bin/dsv2dsv.js + dsv2json: bin/dsv2json.js + json2csv: bin/json2dsv.js + json2dsv: bin/json2dsv.js + json2tsv: bin/json2dsv.js + tsv2csv: bin/dsv2dsv.js + tsv2json: bin/dsv2json.js + checksum: 5fc0723647269d5dccd181d74f2265920ab368a2868b0b4f55ffa2fecdfb7814390ea28622cd61ee5d9594ab262879509059544e9f815c54fe76fbfb4ffa4c8a + languageName: node + linkType: hard + +"d3-force@npm:^2.1.1": + version: 2.1.1 + resolution: "d3-force@npm:2.1.1" + dependencies: + d3-dispatch: 1 - 2 + d3-quadtree: 1 - 2 + d3-timer: 1 - 2 + checksum: aaee5b86d753450e72dae6748765ac3e0b7b784bd420a61264b778d697b9521a343b74b5c55654be2ff7fdf9bada0953a6fcae9be69091176d0579b56df72937 + languageName: node + linkType: hard + +"d3-format@npm:1 - 2, d3-format@npm:^2.0.0": + version: 2.0.0 + resolution: "d3-format@npm:2.0.0" + checksum: c4d3c8f9941d097d514d3986f54f21434e08e5876dc08d1d65226447e8e167600d5b9210235bb03fd45327225f04f32d6e365f08f76d2f4b8bff81594851aaf7 + languageName: node + linkType: hard + +"d3-format@npm:1 - 3, d3-format@npm:^3.1.0": + version: 3.1.0 + resolution: "d3-format@npm:3.1.0" + checksum: f345ec3b8ad3cab19bff5dead395bd9f5590628eb97a389b1dd89f0b204c7c4fc1d9520f13231c2c7cf14b7c9a8cf10f8ef15bde2befbab41454a569bd706ca2 + languageName: node + linkType: hard + +"d3-format@npm:~1.3.0": + version: 1.3.2 + resolution: "d3-format@npm:1.3.2" + checksum: 5de8ebf1f323d893557792b44f794e0971d9ee0cad00e957c509efbb1c730e49f0552e51e2576ee0d0dacc7656488ee44cbdcceabc10bb6209f9c871e73ef2b0 + languageName: node + linkType: hard + +"d3-geo-projection@npm:^3.0.0": + version: 3.0.0 + resolution: "d3-geo-projection@npm:3.0.0" + dependencies: + commander: 2 + d3-array: 1 - 2 + d3-geo: 1.12.0 - 2 + resolve: ^1.1.10 + bin: + geo2svg: bin/geo2svg + geograticule: bin/geograticule + geoproject: bin/geoproject + geoquantize: bin/geoquantize + geostitch: bin/geostitch + checksum: db322e07a12dbf39a4f416a32fa794f64b189e4d09294dba83f4313591615330aa596d80a46dc1f5d7a73c984fbc8f5c1822a98e62ff9ba6ed72fa9a5a013d62 + languageName: node + linkType: hard + +"d3-geo-projection@npm:^4.0.0": + version: 4.0.0 + resolution: "d3-geo-projection@npm:4.0.0" + dependencies: + commander: 7 + d3-array: 1 - 3 + d3-geo: 1.12.0 - 3 + bin: + geo2svg: bin/geo2svg.js + geograticule: bin/geograticule.js + geoproject: bin/geoproject.js + geoquantize: bin/geoquantize.js + geostitch: bin/geostitch.js + checksum: 631422b10dd78d1047ba5a3b073148bea27721060bd7087a5fa6c053ca80445d26432e505e0e3acbd6e0d76cf577c61bf9a5db70dabbc9310c493de1f7ff736d + languageName: node + linkType: hard + +"d3-geo@npm:1.12.0 - 2, d3-geo@npm:^2.0.1": + version: 2.0.2 + resolution: "d3-geo@npm:2.0.2" + dependencies: + d3-array: ^2.5.0 + checksum: 992f667c646f8e2ea810de20e62914128e119f0458bce4090934287af3b93395632ed4af16aae7ccae095ae702a23b5d7a49888674f1aa27ab1a6e410882d86c + languageName: node + linkType: hard + +"d3-geo@npm:1.12.0 - 3, d3-geo@npm:^3.1.0": + version: 3.1.1 + resolution: "d3-geo@npm:3.1.1" + dependencies: + d3-array: 2.5.0 - 3 + checksum: 3cc4bb50af5d2d4858d2df1729a1777b7fd361854079d9faab1166186c988d2cba0d11911da0c4598d5e22fae91d79113ed262a9f98cabdbc6dbf7c30e5c0363 + languageName: node + linkType: hard + +"d3-hierarchy@npm:^2.0.0": + version: 2.0.0 + resolution: "d3-hierarchy@npm:2.0.0" + checksum: 594bea104d3cf947da8499f3c50c93d914c5af34195ef3848e4888ed5c8c1aa6a1adae1dd656acfd04779005255347f68a3d270d825ec94fc88b88a72dd7cb4e + languageName: node + linkType: hard + +"d3-interpolate@npm:1 - 3, d3-interpolate@npm:1.2.0 - 3, d3-interpolate@npm:^3.0.1": + version: 3.0.1 + resolution: "d3-interpolate@npm:3.0.1" + dependencies: + d3-color: 1 - 3 + checksum: a42ba314e295e95e5365eff0f604834e67e4a3b3c7102458781c477bd67e9b24b6bb9d8e41ff5521050a3f2c7c0c4bbbb6e187fd586daa3980943095b267e78b + languageName: node + linkType: hard + +"d3-interpolate@npm:1.2.0 - 2, d3-interpolate@npm:^2.0.1": + version: 2.0.1 + resolution: "d3-interpolate@npm:2.0.1" + dependencies: + d3-color: 1 - 2 + checksum: 4a2018ac34fbcc3e0e7241e117087ca1b2274b8b33673913658623efacc5db013b8d876586d167b23e3145bdb34ec8e441d301299b082e1a90985b2f18d4299c + languageName: node + linkType: hard + +"d3-path@npm:1 - 2, d3-path@npm:^2.0.0": + version: 2.0.0 + resolution: "d3-path@npm:2.0.0" + checksum: e39e91dfb9abf9637962caede1f4ea4877f4b9e1c914868bdfc355688e9a637ba51bed0fb6180934eb596e50a4d0d1f001b5f2e98a4a3d23cc42558acfbd1f2c + languageName: node + linkType: hard + +"d3-path@npm:^3.1.0": + version: 3.1.0 + resolution: "d3-path@npm:3.1.0" + checksum: 2306f1bd9191e1eac895ec13e3064f732a85f243d6e627d242a313f9777756838a2215ea11562f0c7630c7c3b16a19ec1fe0948b1c82f3317fac55882f6ee5d8 + languageName: node + linkType: hard + +"d3-quadtree@npm:1 - 2": + version: 2.0.0 + resolution: "d3-quadtree@npm:2.0.0" + checksum: e5f9cee19a636666e9f1614f9a9508dde9af47d80769ecb70b6b5033448a8c3ae96f39f1ffea0d1782442559412e3f98508fedf5dc39fe09a2f5995e6a0913bf + languageName: node + linkType: hard + +"d3-scale-chromatic@npm:^3.1.0": + version: 3.1.0 + resolution: "d3-scale-chromatic@npm:3.1.0" + dependencies: + d3-color: 1 - 3 + d3-interpolate: 1 - 3 + checksum: ab6324bd8e1f708e731e02ab44e09741efda2b174cea1d8ca21e4a87546295e99856bc44e2fd3890f228849c96bccfbcf922328f95be6a7df117453eb5cf22c9 + languageName: node + linkType: hard + +"d3-scale@npm:^3.2.2": + version: 3.3.0 + resolution: "d3-scale@npm:3.3.0" + dependencies: + d3-array: ^2.3.0 + d3-format: 1 - 2 + d3-interpolate: 1.2.0 - 2 + d3-time: ^2.1.1 + d3-time-format: 2 - 3 + checksum: f77e73f0fb422292211d0687914c30d26e29011a936ad2a535a868ae92f306c3545af1fe7ea5db1b3e67dbce7a6c6cd952e53d02d1d557543e7e5d30e30e52f2 + languageName: node + linkType: hard + +"d3-scale@npm:^4.0.2": + version: 4.0.2 + resolution: "d3-scale@npm:4.0.2" + dependencies: + d3-array: 2.10.0 - 3 + d3-format: 1 - 3 + d3-interpolate: 1.2.0 - 3 + d3-time: 2.1.1 - 3 + d3-time-format: 2 - 4 + checksum: a9c770d283162c3bd11477c3d9d485d07f8db2071665f1a4ad23eec3e515e2cefbd369059ec677c9ac849877d1a765494e90e92051d4f21111aa56791c98729e + languageName: node + linkType: hard + +"d3-selection@npm:~1.3.0": + version: 1.3.2 + resolution: "d3-selection@npm:1.3.2" + checksum: 79e6ab5978dc7b8281780763553d6826ac4fff5cecfbb31ea2ccd2db53d41bb50e3ab2bb418a810f589cf307f647f4f72c7e4ff0b9cf8b729be29cedfa277e0f + languageName: node + linkType: hard + +"d3-shape@npm:^2.0.0": + version: 2.1.0 + resolution: "d3-shape@npm:2.1.0" + dependencies: + d3-path: 1 - 2 + checksum: 4a82a83fbb15aadee3eb6661226a34bcd793cdbcd7aa5bf980a4724efc93eb94acc6c499f0ebedc9c3144c57c0f033867d137f41e86459acbd5d7181cb27b49c + languageName: node + linkType: hard + +"d3-shape@npm:^3.2.0": + version: 3.2.0 + resolution: "d3-shape@npm:3.2.0" + dependencies: + d3-path: ^3.1.0 + checksum: de2af5fc9a93036a7b68581ca0bfc4aca2d5a328aa7ba7064c11aedd44d24f310c20c40157cb654359d4c15c3ef369f95ee53d71221017276e34172c7b719cfa + languageName: node + linkType: hard + +"d3-time-format@npm:2 - 3, d3-time-format@npm:^3.0.0": + version: 3.0.0 + resolution: "d3-time-format@npm:3.0.0" + dependencies: + d3-time: 1 - 2 + checksum: c20c1667dbea653f81d923e741f84c23e4b966002ba0d6ed94cbc70692105566e55e89d18d175404534a879383fd1123300bd12885a3c924fe924032bb0060db + languageName: node + linkType: hard + +"d3-time-format@npm:2 - 4, d3-time-format@npm:^4.1.0": + version: 4.1.0 + resolution: "d3-time-format@npm:4.1.0" + dependencies: + d3-time: 1 - 3 + checksum: 7342bce28355378152bbd4db4e275405439cabba082d9cd01946d40581140481c8328456d91740b0fe513c51ec4a467f4471ffa390c7e0e30ea30e9ec98fcdf4 + languageName: node + linkType: hard + +"d3-time@npm:1 - 2, d3-time@npm:^2.0.0, d3-time@npm:^2.1.1": + version: 2.1.1 + resolution: "d3-time@npm:2.1.1" + dependencies: + d3-array: 2 + checksum: d1c7b9658c20646e46c3dd19e11c38e02dec098e8baa7d2cd868af8eb01953668f5da499fa33dc63541cf74a26e788786f8828c4381dbbf475a76b95972979a6 + languageName: node + linkType: hard + +"d3-time@npm:1 - 3, d3-time@npm:2.1.1 - 3, d3-time@npm:^3.1.0": + version: 3.1.0 + resolution: "d3-time@npm:3.1.0" + dependencies: + d3-array: 2 - 3 + checksum: 613b435352a78d9f31b7f68540788186d8c331b63feca60ad21c88e9db1989fe888f97f242322ebd6365e45ec3fb206a4324cd4ca0dfffa1d9b5feb856ba00a7 + languageName: node + linkType: hard + +"d3-timer@npm:1 - 2, d3-timer@npm:^2.0.0": + version: 2.0.0 + resolution: "d3-timer@npm:2.0.0" + checksum: 70733c3baffe473155b712896f04f27dae32d6e94169827f57aebb203e190926ba37af12c5f56cbc7126e538a4b1cd083f2451b80dc2a5644d076b6b31982bd8 + languageName: node + linkType: hard + "data-uri-to-buffer@npm:^6.0.2": version: 6.0.2 resolution: "data-uri-to-buffer@npm:6.0.2" @@ -6010,6 +6419,13 @@ __metadata: languageName: node linkType: hard +"delaunator@npm:4": + version: 4.0.1 + resolution: "delaunator@npm:4.0.1" + checksum: a49f1c23edbcb79079a13577d32fcd46d0db30879c8484f742a0d840923085f2f3de35a9bfbb96eadd12201ffb7c3adf45b0f528d08b71cb547c5f8068b5d61b + languageName: node + linkType: hard + "delayed-stream@npm:~1.0.0": version: 1.0.0 resolution: "delayed-stream@npm:1.0.0" @@ -6142,7 +6558,7 @@ __metadata: languageName: node linkType: hard -"encoding@npm:^0.1.13": +"encoding@npm:^0.1.11, encoding@npm:^0.1.13": version: 0.1.13 resolution: "encoding@npm:0.1.13" dependencies: @@ -6878,7 +7294,7 @@ __metadata: languageName: node linkType: hard -"fast-deep-equal@npm:^3.1.1, fast-deep-equal@npm:^3.1.3": +"fast-deep-equal@npm:^3.1.1, fast-deep-equal@npm:^3.1.3, fast-deep-equal@npm:~3.1.1": version: 3.1.3 resolution: "fast-deep-equal@npm:3.1.3" checksum: e21a9d8d84f53493b6aa15efc9cfd53dd5b714a1f23f67fb5dc8f574af80df889b3bce25dc081887c6d25457cce704e636395333abad896ccdec03abaf1f3f9d @@ -6912,7 +7328,14 @@ __metadata: languageName: node linkType: hard -"fast-json-stable-stringify@npm:^2.0.0, fast-json-stable-stringify@npm:^2.1.0": +"fast-json-patch@npm:^3.0.0-1": + version: 3.1.1 + resolution: "fast-json-patch@npm:3.1.1" + checksum: c4525b61b2471df60d4b025b4118b036d99778a93431aa44d1084218182841d82ce93056f0f3bbd731a24e6a8e69820128adf1873eb2199a26c62ef58d137833 + languageName: node + linkType: hard + +"fast-json-stable-stringify@npm:^2.0.0, fast-json-stable-stringify@npm:^2.1.0, fast-json-stable-stringify@npm:~2.1.0": version: 2.1.0 resolution: "fast-json-stable-stringify@npm:2.1.0" checksum: b191531e36c607977e5b1c47811158733c34ccb3bfde92c44798929e9b4154884378536d26ad90dfecd32e1ffc09c545d23535ad91b3161a27ddbb8ebe0cbecb @@ -6980,6 +7403,21 @@ __metadata: languageName: node linkType: hard +"fbjs@npm:^0.8.12": + version: 0.8.18 + resolution: "fbjs@npm:0.8.18" + dependencies: + core-js: ^1.0.0 + isomorphic-fetch: ^2.1.1 + loose-envify: ^1.0.0 + object-assign: ^4.1.0 + promise: ^7.1.1 + setimmediate: ^1.0.5 + ua-parser-js: ^0.7.30 + checksum: 668731b946a765908c9cbe51d5160f973abb78004b3d122587c3e930e3e1ddcc0ce2b17f2a8637dc9d733e149aa580f8d3035a35cc2d3bc78b78f1b19aab90e2 + languageName: node + linkType: hard + "fd-slicer@npm:~1.1.0": version: 1.1.0 resolution: "fd-slicer@npm:1.1.0" @@ -7321,6 +7759,19 @@ __metadata: languageName: node linkType: hard +"glamor@npm:~2.20.40": + version: 2.20.40 + resolution: "glamor@npm:2.20.40" + dependencies: + fbjs: ^0.8.12 + inline-style-prefixer: ^3.0.6 + object-assign: ^4.1.1 + prop-types: ^15.5.10 + through: ^2.3.8 + checksum: c52f74317053ace8808b9adb156ac756ce03f4d885d3480ff268a56bdc5a0007a5d226edde826b830dc99eb65b509281f187a7ac827009f81225d1ab89f6653d + languageName: node + linkType: hard + "glob-parent@npm:^5.1.2": version: 5.1.2 resolution: "glob-parent@npm:5.1.2" @@ -7654,7 +8105,23 @@ __metadata: languageName: node linkType: hard -"iconv-lite@npm:^0.6.2": +"hyphenate-style-name@npm:^1.0.2": + version: 1.1.0 + resolution: "hyphenate-style-name@npm:1.1.0" + checksum: b9ed74e29181d96bd58a2d0e62fc4a19879db591dba268275829ff0ae595fcdf11faafaeaa63330a45c3004664d7db1f0fc7cdb372af8ee4615ed8260302c207 + languageName: node + linkType: hard + +"iconv-lite@npm:0.4": + version: 0.4.24 + resolution: "iconv-lite@npm:0.4.24" + dependencies: + safer-buffer: ">= 2.1.2 < 3" + checksum: bd9f120f5a5b306f0bc0b9ae1edeb1577161503f5f8252a20f1a9e56ef8775c9959fd01c55f2d3a39d9a8abaf3e30c1abeb1895f367dcbbe0a8fd1c9ca01c4f6 + languageName: node + linkType: hard + +"iconv-lite@npm:0.6, iconv-lite@npm:^0.6.2": version: 0.6.3 resolution: "iconv-lite@npm:0.6.3" dependencies: @@ -7758,6 +8225,16 @@ __metadata: languageName: node linkType: hard +"inline-style-prefixer@npm:^3.0.6": + version: 3.0.8 + resolution: "inline-style-prefixer@npm:3.0.8" + dependencies: + bowser: ^1.7.3 + css-in-js-utils: ^2.0.0 + checksum: a33b7c820fbe6a4c159c7ec64965174f287bd50d8b33b7b4988a7432c0ea0f9012608c44c2bab8d29e1c082027db39796a1638a21de34a9bbd761ad415630083 + languageName: node + linkType: hard + "internal-slot@npm:^1.0.7": version: 1.0.7 resolution: "internal-slot@npm:1.0.7" @@ -7769,6 +8246,20 @@ __metadata: languageName: node linkType: hard +"internmap@npm:1 - 2": + version: 2.0.3 + resolution: "internmap@npm:2.0.3" + checksum: 7ca41ec6aba8f0072fc32fa8a023450a9f44503e2d8e403583c55714b25efd6390c38a87161ec456bf42d7bc83aab62eb28f5aef34876b1ac4e60693d5e1d241 + languageName: node + linkType: hard + +"internmap@npm:^1.0.0": + version: 1.0.1 + resolution: "internmap@npm:1.0.1" + checksum: 9d00f8c0cf873a24a53a5a937120dab634c41f383105e066bb318a61864e6292d24eb9516e8e7dccfb4420ec42ca474a0f28ac9a6cc82536898fa09bbbe53813 + languageName: node + linkType: hard + "invariant@npm:^2.2.4": version: 2.2.4 resolution: "invariant@npm:2.2.4" @@ -8098,6 +8589,13 @@ __metadata: languageName: node linkType: hard +"is-stream@npm:^1.0.1": + version: 1.1.0 + resolution: "is-stream@npm:1.1.0" + checksum: 063c6bec9d5647aa6d42108d4c59723d2bd4ae42135a2d4db6eadbd49b7ea05b750fd69d279e5c7c45cf9da753ad2c00d8978be354d65aa9f6bb434969c6a2ae + languageName: node + linkType: hard + "is-stream@npm:^2.0.0": version: 2.0.1 resolution: "is-stream@npm:2.0.1" @@ -8232,6 +8730,16 @@ __metadata: languageName: node linkType: hard +"isomorphic-fetch@npm:^2.1.1": + version: 2.2.1 + resolution: "isomorphic-fetch@npm:2.2.1" + dependencies: + node-fetch: ^1.0.1 + whatwg-fetch: ">=0.10.0" + checksum: bb5daa7c3785d6742f4379a81e55b549a469503f7c9bf9411b48592e86632cf5e8fe8ea878dba185c0f33eb7c510c23abdeb55aebfdf5d3c70f031ced68c5424 + languageName: node + linkType: hard + "istanbul-lib-coverage@npm:^3.0.0, istanbul-lib-coverage@npm:^3.2.0": version: 3.2.2 resolution: "istanbul-lib-coverage@npm:3.2.2" @@ -8928,6 +9436,20 @@ __metadata: languageName: node linkType: hard +"json-stringify-pretty-compact@npm:^3.0.0": + version: 3.0.0 + resolution: "json-stringify-pretty-compact@npm:3.0.0" + checksum: 01ab5c5c8260299414868d96db97f53aef93c290fe469edd9a1363818e795006e01c952fa2fd7b47cbbab506d5768998eccc25e1da4fa2ccfebd1788c6098791 + languageName: node + linkType: hard + +"json-stringify-pretty-compact@npm:~2.0.0": + version: 2.0.0 + resolution: "json-stringify-pretty-compact@npm:2.0.0" + checksum: b476aa4ce2728cc7d89bffc4e99433015efc7065313cb90b7a6450cfd934aa3a61846ee9040c79ce00c44ca046cdd6ec0113157c9b895c85b7529c3056d6a1ea + languageName: node + linkType: hard + "json5@npm:^1.0.2": version: 1.0.2 resolution: "json5@npm:1.0.2" @@ -9853,7 +10375,17 @@ __metadata: languageName: node linkType: hard -"node-fetch@npm:^2.2.0, node-fetch@npm:^2.6.0": +"node-fetch@npm:^1.0.1": + version: 1.7.3 + resolution: "node-fetch@npm:1.7.3" + dependencies: + encoding: ^0.1.11 + is-stream: ^1.0.1 + checksum: 3bb0528c05d541316ebe52770d71ee25a6dce334df4231fd55df41a644143e07f068637488c18a5b0c43f05041dbd3346752f9e19b50df50569a802484544d5b + languageName: node + linkType: hard + +"node-fetch@npm:^2.2.0, node-fetch@npm:^2.6.0, node-fetch@npm:^2.6.1, node-fetch@npm:^2.6.7": version: 2.7.0 resolution: "node-fetch@npm:2.7.0" dependencies: @@ -9984,7 +10516,7 @@ __metadata: languageName: node linkType: hard -"object-assign@npm:^4.1.1": +"object-assign@npm:^4.1.0, object-assign@npm:^4.1.1": version: 4.1.1 resolution: "object-assign@npm:4.1.1" checksum: fcc6e4ea8c7fe48abfbb552578b1c53e0d194086e2e6bbbf59e0a536381a292f39943c6e9628af05b5528aa5e3318bb30d6b2e53cadaf5b8fe9e12c4b69af23f @@ -10429,6 +10961,13 @@ __metadata: languageName: node linkType: hard +"preact@npm:~8.2.9": + version: 8.2.9 + resolution: "preact@npm:8.2.9" + checksum: a2ef90c66592620b1be0d2909c48dc54dbcd30ef5c914b0f3329b6237651b381dbdba95192d732ca567f9b429500e230974b9a403f8bc09fcdabd5bc117cb4c9 + languageName: node + linkType: hard + "prelude-ls@npm:^1.2.1": version: 1.2.1 resolution: "prelude-ls@npm:1.2.1" @@ -10526,6 +11065,15 @@ __metadata: languageName: node linkType: hard +"promise@npm:^7.1.1": + version: 7.3.1 + resolution: "promise@npm:7.3.1" + dependencies: + asap: ~2.0.3 + checksum: 475bb069130179fbd27ed2ab45f26d8862376a137a57314cf53310bdd85cc986a826fd585829be97ebc0aaf10e9d8e68be1bfe5a4a0364144b1f9eedfa940cf1 + languageName: node + linkType: hard + "promise@npm:^8.3.0": version: 8.3.0 resolution: "promise@npm:8.3.0" @@ -10545,7 +11093,7 @@ __metadata: languageName: node linkType: hard -"prop-types@npm:^15.7.2, prop-types@npm:^15.8.1": +"prop-types@npm:^15.5.10, prop-types@npm:^15.7.2, prop-types@npm:^15.8.1": version: 15.8.1 resolution: "prop-types@npm:15.8.1" dependencies: @@ -11281,7 +11829,7 @@ __metadata: languageName: node linkType: hard -"resolve@npm:^1.1.6, resolve@npm:^1.14.2, resolve@npm:^1.20.0, resolve@npm:^1.22.1, resolve@npm:^1.22.4": +"resolve@npm:^1.1.10, resolve@npm:^1.1.6, resolve@npm:^1.14.2, resolve@npm:^1.20.0, resolve@npm:^1.22.1, resolve@npm:^1.22.4": version: 1.22.8 resolution: "resolve@npm:1.22.8" dependencies: @@ -11307,7 +11855,7 @@ __metadata: languageName: node linkType: hard -"resolve@patch:resolve@^1.1.6#~builtin, resolve@patch:resolve@^1.14.2#~builtin, resolve@patch:resolve@^1.20.0#~builtin, resolve@patch:resolve@^1.22.1#~builtin, resolve@patch:resolve@^1.22.4#~builtin": +"resolve@patch:resolve@^1.1.10#~builtin, resolve@patch:resolve@^1.1.6#~builtin, resolve@patch:resolve@^1.14.2#~builtin, resolve@patch:resolve@^1.20.0#~builtin, resolve@patch:resolve@^1.22.1#~builtin, resolve@patch:resolve@^1.22.4#~builtin": version: 1.22.8 resolution: "resolve@patch:resolve@npm%3A1.22.8#~builtin::version=1.22.8&hash=c3c19d" dependencies: @@ -11399,6 +11947,13 @@ __metadata: languageName: node linkType: hard +"rw@npm:1": + version: 1.3.3 + resolution: "rw@npm:1.3.3" + checksum: c20d82421f5a71c86a13f76121b751553a99cd4a70ea27db86f9b23f33db941f3f06019c30f60d50c356d0bd674c8e74764ac146ea55e217c091bde6fba82aa3 + languageName: node + linkType: hard + "safe-array-concat@npm:^1.1.2": version: 1.1.2 resolution: "safe-array-concat@npm:1.1.2" @@ -11436,7 +11991,7 @@ __metadata: languageName: node linkType: hard -"safer-buffer@npm:>= 2.1.2 < 3.0.0": +"safer-buffer@npm:>= 2.1.2 < 3, safer-buffer@npm:>= 2.1.2 < 3.0.0": version: 2.1.2 resolution: "safer-buffer@npm:2.1.2" checksum: cab8f25ae6f1434abee8d80023d7e72b598cf1327164ddab31003c51215526801e40b66c5e65d658a0af1e9d6478cadcb4c745f4bd6751f97d8644786c0978b0 @@ -11587,6 +12142,13 @@ __metadata: languageName: node linkType: hard +"setimmediate@npm:^1.0.5": + version: 1.0.5 + resolution: "setimmediate@npm:1.0.5" + checksum: c9a6f2c5b51a2dabdc0247db9c46460152ffc62ee139f3157440bd48e7c59425093f42719ac1d7931f054f153e2d26cf37dfeb8da17a794a58198a2705e527fd + languageName: node + linkType: hard + "setprototypeof@npm:1.2.0": version: 1.2.0 resolution: "setprototypeof@npm:1.2.0" @@ -12306,6 +12868,19 @@ __metadata: languageName: node linkType: hard +"topojson-client@npm:^3.1.0": + version: 3.1.0 + resolution: "topojson-client@npm:3.1.0" + dependencies: + commander: 2 + bin: + topo2geo: bin/topo2geo + topomerge: bin/topomerge + topoquantize: bin/topoquantize + checksum: 8c029a4f18324ace0b8b55dd90edbd40c9e3c6de18bafbb5da37ca20ebf20e26fbd4420891acb3c2c264e214185f7557871f5651a9eee517028663be98d836de + languageName: node + linkType: hard + "tr46@npm:~0.0.3": version: 0.0.3 resolution: "tr46@npm:0.0.3" @@ -12410,6 +12985,13 @@ __metadata: languageName: node linkType: hard +"tslib@npm:~2.0.0": + version: 2.0.3 + resolution: "tslib@npm:2.0.3" + checksum: 00fcdd1f9995c9f8eb6a4a1ad03f55bc95946321b7f55434182dddac259d4e095fedf78a84f73b6e32dd3f881d9281f09cb583123d3159ed4bdac9ad7393ef8b + languageName: node + linkType: hard + "tsutils@npm:^3.21.0": version: 3.21.0 resolution: "tsutils@npm:3.21.0" @@ -12664,6 +13246,15 @@ __metadata: languageName: node linkType: hard +"ua-parser-js@npm:^0.7.30": + version: 0.7.39 + resolution: "ua-parser-js@npm:0.7.39" + bin: + ua-parser-js: script/cli.js + checksum: 3488852961485b80b65a8dbc978098cdf1c51bb7765262698ee1a29304786f667272182e9cee15433e7792981376b1ca59ca77e126fee0b41b104085f4be9a3c + languageName: node + linkType: hard + "unbox-primitive@npm:^1.0.2": version: 1.0.2 resolution: "unbox-primitive@npm:1.0.2" @@ -12874,6 +13465,601 @@ __metadata: languageName: node linkType: hard +"vega-canvas@npm:^1.2.5, vega-canvas@npm:^1.2.7": + version: 1.2.7 + resolution: "vega-canvas@npm:1.2.7" + checksum: 6ff92fcdf0c359f2f662909c859a7f4cb4a502436136ab2f4c02373c47a621996ec0eea23e2108f11d62a618be301de86cd8528b5058c2e207a53ddd7ff58d1b + languageName: node + linkType: hard + +"vega-crossfilter@npm:~4.0.5": + version: 4.0.5 + resolution: "vega-crossfilter@npm:4.0.5" + dependencies: + d3-array: ^2.7.1 + vega-dataflow: ^5.7.3 + vega-util: ^1.15.2 + checksum: 54758e78a5252f396564d6a6bf008ac82ac1dcc8ff4f4c8cb93034393deb13d18c35ed24a5a8e618724f1eb345914eabdefa23f0e920e79abc06224cf4aba54c + languageName: node + linkType: hard + +"vega-dataflow@npm:^5.7.3, vega-dataflow@npm:^5.7.4, vega-dataflow@npm:^5.7.5, vega-dataflow@npm:^5.7.6, vega-dataflow@npm:~5.7.3": + version: 5.7.6 + resolution: "vega-dataflow@npm:5.7.6" + dependencies: + vega-format: ^1.1.2 + vega-loader: ^4.5.2 + vega-util: ^1.17.2 + checksum: bea1237a5ddaadaba774b1521ef2419249a0d7c926e8a8bd1dcbbc1b771b5e9fff4a64d515f056d63708c8cb11e0bccd64c302c179af03ecaba2c7fc870422e0 + languageName: node + linkType: hard + +"vega-embed@npm:6.17.0": + version: 6.17.0 + resolution: "vega-embed@npm:6.17.0" + dependencies: + fast-json-patch: ^3.0.0-1 + json-stringify-pretty-compact: ^3.0.0 + semver: ^7.3.5 + vega-schema-url-parser: ^2.1.0 + vega-themes: ^2.10.0 + vega-tooltip: ^0.25.1 + peerDependencies: + vega: ^5.13.0 + vega-lite: "*" + checksum: c61221b8750981372d145853b4baa81478aaf5b24491a8b1042b0591859450f1f145b4ada784bf7747939a9de0e3f0691f3629968fad7bb1d000e08e14c54ad5 + languageName: node + linkType: hard + +"vega-encode@npm:~4.8.3": + version: 4.8.3 + resolution: "vega-encode@npm:4.8.3" + dependencies: + d3-array: ^2.7.1 + d3-interpolate: ^2.0.1 + vega-dataflow: ^5.7.3 + vega-scale: ^7.0.3 + vega-util: ^1.15.2 + checksum: 250373960b824433480db3f86ea2d54a1a512ae9b162bd72b271a2dd0ff32b07d953ed0dfa7a76b44653d05b9fa206f934a9e7f19507528cb44770a391475ffd + languageName: node + linkType: hard + +"vega-event-selector@npm:^3.0.0": + version: 3.0.1 + resolution: "vega-event-selector@npm:3.0.1" + checksum: 66d09b5800a19a9b0c75f28811b140a1a2e70e84be6d6f87c568cdbce6e17c8e195f130f4e3de5d6dc737142d1f46f4fe7645177e154582cc8ba27c6845b54e8 + languageName: node + linkType: hard + +"vega-event-selector@npm:~2.0.3, vega-event-selector@npm:~2.0.6": + version: 2.0.6 + resolution: "vega-event-selector@npm:2.0.6" + checksum: 411f53c8fafc89cc12f9167cd6082cf55a5f5be835dd793dfed89a2942d72d13c0db36c7e7614b4a85aa85c9d62bac6f5fddaf482c6a246a303a78b245b1e3fc + languageName: node + linkType: hard + +"vega-expression@npm:^5.0.0, vega-expression@npm:^5.0.1, vega-expression@npm:^5.1.1": + version: 5.1.1 + resolution: "vega-expression@npm:5.1.1" + dependencies: + "@types/estree": ^1.0.0 + vega-util: ^1.17.2 + checksum: 5af3732b1757000e7f79f7b923cf6594cf75bdc2350b2d54992d8df0bad5cea04812d6d08b79e6fc7a20c3df944b6c11fc5e6ab39a098e5d51e3edf33df4d29f + languageName: node + linkType: hard + +"vega-expression@npm:~2.6.5": + version: 2.6.6 + resolution: "vega-expression@npm:2.6.6" + dependencies: + vega-util: ^1.15.0 + checksum: 6dc9149638705d7248bec1cae4392e9e0ea3f24f5ab1494be3157d7fb2838fd9ddb5db02c90990624a6de9df51682d30fd97e147f723541a51db2bbd1970c1a3 + languageName: node + linkType: hard + +"vega-expression@npm:~4.0.1": + version: 4.0.1 + resolution: "vega-expression@npm:4.0.1" + dependencies: + vega-util: ^1.16.0 + checksum: 22b41c9e6415a11df268831c700c0dee88f2a7f0e27e4a0ce54e1618ee5d0a86df8fcfd76502b5c4223ea0a152c1774bfd1af179294523077068605726e92d67 + languageName: node + linkType: hard + +"vega-force@npm:~4.0.7": + version: 4.0.7 + resolution: "vega-force@npm:4.0.7" + dependencies: + d3-force: ^2.1.1 + vega-dataflow: ^5.7.3 + vega-util: ^1.15.2 + checksum: 3e86cf2373ac556e14f78fa0bb409ad00c50eb7de8749fec8fb6c63f62a044757c551c7ddaa86f341c200dd0ee331fbde61ddd5e7798cac265f7a4015e24d0ec + languageName: node + linkType: hard + +"vega-format@npm:^1.0.4, vega-format@npm:^1.1.2": + version: 1.1.2 + resolution: "vega-format@npm:1.1.2" + dependencies: + d3-array: ^3.2.2 + d3-format: ^3.1.0 + d3-time-format: ^4.1.0 + vega-time: ^2.1.2 + vega-util: ^1.17.2 + checksum: 04edc955080a994353a7d8915fd2142b5f055b7d86d7d7ab45f44648a68d8138c958d40a4cc6f4ecdd4f2327866d5ad96aa8fb9b543f2c130c01d8893e2cb365 + languageName: node + linkType: hard + +"vega-format@npm:~1.0.4": + version: 1.0.4 + resolution: "vega-format@npm:1.0.4" + dependencies: + d3-array: ^2.7.1 + d3-format: ^2.0.0 + d3-time-format: ^3.0.0 + vega-time: ^2.0.3 + vega-util: ^1.15.2 + checksum: b358036944ec47c9830567e5600cfef9c90183f486fd274213aaab42a8fc96ae118e540991ed3fad8b95fc01ee89251303127bce10e803e24433a677cf19a4ac + languageName: node + linkType: hard + +"vega-functions@npm:^5.10.0, vega-functions@npm:^5.12.1": + version: 5.15.0 + resolution: "vega-functions@npm:5.15.0" + dependencies: + d3-array: ^3.2.2 + d3-color: ^3.1.0 + d3-geo: ^3.1.0 + vega-dataflow: ^5.7.6 + vega-expression: ^5.1.1 + vega-scale: ^7.4.1 + vega-scenegraph: ^4.13.0 + vega-selections: ^5.4.2 + vega-statistics: ^1.9.0 + vega-time: ^2.1.2 + vega-util: ^1.17.2 + checksum: 5aff41436560b18f03059f173d951e13f7d7c6d0cf0552829f951c7d992a69c9374651d472e51ba092e14cf99c3a9d000ebb2918a13354c52827da9cb3f462f0 + languageName: node + linkType: hard + +"vega-functions@npm:~5.12.0": + version: 5.12.1 + resolution: "vega-functions@npm:5.12.1" + dependencies: + d3-array: ^2.7.1 + d3-color: ^2.0.0 + d3-geo: ^2.0.1 + vega-dataflow: ^5.7.3 + vega-expression: ^5.0.0 + vega-scale: ^7.1.1 + vega-scenegraph: ^4.9.3 + vega-selections: ^5.3.1 + vega-statistics: ^1.7.9 + vega-time: ^2.0.4 + vega-util: ^1.16.0 + checksum: 83a6cb05403b1ae87e52ccc1fcb10da856cc3657b128e711b8bcdff65a7af3d4c703eeabf76872347428fc5b0196fd55f7eb886bb66bdae22a91057e85599355 + languageName: node + linkType: hard + +"vega-geo@npm:~4.3.8": + version: 4.3.8 + resolution: "vega-geo@npm:4.3.8" + dependencies: + d3-array: ^2.7.1 + d3-color: ^2.0.0 + d3-geo: ^2.0.1 + vega-canvas: ^1.2.5 + vega-dataflow: ^5.7.3 + vega-projection: ^1.4.5 + vega-statistics: ^1.7.9 + vega-util: ^1.15.2 + checksum: 8e556889401f39180b7a3773501b6ec24bade0e2ab37d80ec115fb216a0414a0d2b65b03d58196de983e74cde3789d7999a0f45ca28d20081277eb04fc1ba40c + languageName: node + linkType: hard + +"vega-hierarchy@npm:~4.0.9": + version: 4.0.9 + resolution: "vega-hierarchy@npm:4.0.9" + dependencies: + d3-hierarchy: ^2.0.0 + vega-dataflow: ^5.7.3 + vega-util: ^1.15.2 + checksum: 5980178a62ae7d49dc11fffc79cc9f1675c2b27a4d7784d26335748d91f6ef47a54b2cb01bfd913347a636406cc9925c8fc2e5ca9c690eecb9114b3110c90852 + languageName: node + linkType: hard + +"vega-label@npm:~1.0.0": + version: 1.0.0 + resolution: "vega-label@npm:1.0.0" + dependencies: + vega-canvas: ^1.2.5 + vega-dataflow: ^5.7.3 + vega-scenegraph: ^4.9.2 + vega-util: ^1.15.2 + checksum: 3ef16e6d0b85b6256a509e3dd403d1a066055fe3721731d5bfc843a6cdfdb150c21bd5334a58169fd81981d2977e55a82d10d0bc5f81cbf35bc4309842d6ea36 + languageName: node + linkType: hard + +"vega-lite@npm:4.13.1": + version: 4.13.1 + resolution: "vega-lite@npm:4.13.1" + dependencies: + "@types/clone": ~0.1.30 + "@types/fast-json-stable-stringify": ^2.0.0 + array-flat-polyfill: ^1.0.1 + clone: ~2.1.2 + fast-deep-equal: ~3.1.1 + fast-json-stable-stringify: ~2.1.0 + json-stringify-pretty-compact: ~2.0.0 + tslib: ~2.0.0 + vega-event-selector: ~2.0.3 + vega-expression: ~2.6.5 + vega-util: ~1.14.0 + yargs: ~15.3.1 + peerDependencies: + vega: ^5.12.1 + bin: + vl2pdf: bin/vl2pdf + vl2png: bin/vl2png + vl2svg: bin/vl2svg + vl2vg: bin/vl2vg + checksum: 5ead7f56908d03d5dc2d798af8d1fb5178c35c837b014e8d69bcf7ba4afe5217f56615f64d175e5512e42fb11eca73dd85bb8b8dcd23108510c40eca47d6f9f2 + languageName: node + linkType: hard + +"vega-loader@npm:^4.3.3, vega-loader@npm:^4.5.2": + version: 4.5.2 + resolution: "vega-loader@npm:4.5.2" + dependencies: + d3-dsv: ^3.0.1 + node-fetch: ^2.6.7 + topojson-client: ^3.1.0 + vega-format: ^1.1.2 + vega-util: ^1.17.2 + checksum: e2f77e36dd40d5604b31f7273a0cebc5cc2a83560131bec217fc9c2c1f03faa68fe57c86ff39d3b14d375a9ad91f1d5709abaf666e30ca97041f687756d99de6 + languageName: node + linkType: hard + +"vega-loader@npm:~4.4.0": + version: 4.4.1 + resolution: "vega-loader@npm:4.4.1" + dependencies: + d3-dsv: ^2.0.0 + node-fetch: ^2.6.1 + topojson-client: ^3.1.0 + vega-format: ^1.0.4 + vega-util: ^1.16.0 + checksum: 271aa8c6f42a010f0d36f74a51417b31206e65036f6fa868943c9272fb2b398beeb1f7a0a4bcd149a20aee75d2921a40a126fa4b20b5b874838088c4088a6ebf + languageName: node + linkType: hard + +"vega-parser@npm:~6.1.3": + version: 6.1.4 + resolution: "vega-parser@npm:6.1.4" + dependencies: + vega-dataflow: ^5.7.3 + vega-event-selector: ^3.0.0 + vega-functions: ^5.12.1 + vega-scale: ^7.1.1 + vega-util: ^1.16.0 + checksum: f9f8006dec0fb43dc4ea0030258e2946d834718046a6b2ba34eb4bfbd368711a7a6e43c12c099b0dd9417717a8c2dd82b7727c343d2a3a60cab1a3c74dc774be + languageName: node + linkType: hard + +"vega-projection@npm:^1.4.5": + version: 1.6.1 + resolution: "vega-projection@npm:1.6.1" + dependencies: + d3-geo: ^3.1.0 + d3-geo-projection: ^4.0.0 + vega-scale: ^7.4.1 + checksum: 4ea5c449d4aed427add0777cecf430670c6addaa1c938bc112458e1b2281e5a976b9bcbe1b3aa2ce3e135ae39a6ddecd32c3ac1bd978f879d1bcd8fe0425aab3 + languageName: node + linkType: hard + +"vega-projection@npm:~1.4.5": + version: 1.4.5 + resolution: "vega-projection@npm:1.4.5" + dependencies: + d3-geo: ^2.0.1 + d3-geo-projection: ^3.0.0 + checksum: e2175db9255cc6cf466a8261e956d0b3b24632ced0caacdb1829b26c751dcdb4db888470ca118122e431d4a4f4ccfc9b13ccc3ea1d9a17acdc48cd963a0c88e9 + languageName: node + linkType: hard + +"vega-regression@npm:~1.0.9": + version: 1.0.9 + resolution: "vega-regression@npm:1.0.9" + dependencies: + d3-array: ^2.7.1 + vega-dataflow: ^5.7.3 + vega-statistics: ^1.7.9 + vega-util: ^1.15.2 + checksum: 5575bc27bdcd62f90fd0ff174b73136af34dace1d2be9c9b32bfe24ed3bbafaa8d0b87e290d911bd14fc93e88c66be14f53a4ee9ad15a84faedbb5bc951e4e4d + languageName: node + linkType: hard + +"vega-runtime@npm:^6.1.3": + version: 6.2.0 + resolution: "vega-runtime@npm:6.2.0" + dependencies: + vega-dataflow: ^5.7.6 + vega-util: ^1.17.2 + checksum: e818063dd9b1ca336cb27437047c50ed15f70be86413166d84687aca40270f574da82355934c43cedbb40b14639fe171cfae29eb975ede10910747d345f38e15 + languageName: node + linkType: hard + +"vega-runtime@npm:~6.1.3": + version: 6.1.4 + resolution: "vega-runtime@npm:6.1.4" + dependencies: + vega-dataflow: ^5.7.5 + vega-util: ^1.17.1 + checksum: a1da40ddb3109f1ced8e61d2e7b52784fbb29936ee4c47cb5630dbbeb12ef6e0c3cd3cd189c34377f82402bf19c61dd148d90330fec743b8667635ac48e4ba29 + languageName: node + linkType: hard + +"vega-scale@npm:^7.0.3, vega-scale@npm:^7.1.1, vega-scale@npm:^7.4.1": + version: 7.4.1 + resolution: "vega-scale@npm:7.4.1" + dependencies: + d3-array: ^3.2.2 + d3-interpolate: ^3.0.1 + d3-scale: ^4.0.2 + d3-scale-chromatic: ^3.1.0 + vega-time: ^2.1.2 + vega-util: ^1.17.2 + checksum: 7fe83fdcf09b1e328531d0e4a411ad2eaabbde40b5c0a6de21c75dc341ca208194b1cf48369d76a64718beac438549dad42ff5e7e495a6e39bcff1aeb24118b9 + languageName: node + linkType: hard + +"vega-scale@npm:~7.1.1": + version: 7.1.1 + resolution: "vega-scale@npm:7.1.1" + dependencies: + d3-array: ^2.7.1 + d3-interpolate: ^2.0.1 + d3-scale: ^3.2.2 + vega-time: ^2.0.4 + vega-util: ^1.15.2 + checksum: e4ee1d8473518ddc8cf90af9e43c37565f7e5a52eb87739f68381fcf78ea48b74df888ecff6ee58d4bd1436f3d870083cd96d8ada06bb9509ae04963c6bf9d5e + languageName: node + linkType: hard + +"vega-scenegraph@npm:^4.10.2, vega-scenegraph@npm:^4.13.0, vega-scenegraph@npm:^4.9.2, vega-scenegraph@npm:^4.9.3, vega-scenegraph@npm:^4.9.4": + version: 4.13.0 + resolution: "vega-scenegraph@npm:4.13.0" + dependencies: + d3-path: ^3.1.0 + d3-shape: ^3.2.0 + vega-canvas: ^1.2.7 + vega-loader: ^4.5.2 + vega-scale: ^7.4.1 + vega-util: ^1.17.2 + checksum: 8910511db2bd11237984716e69817d3e91fc83d871263771933f5693b281f9dfe74e93caab913869283fcbbfdb739657d246beed07e43dd5ebfa405bb21fca27 + languageName: node + linkType: hard + +"vega-scenegraph@npm:~4.9.4": + version: 4.9.4 + resolution: "vega-scenegraph@npm:4.9.4" + dependencies: + d3-path: ^2.0.0 + d3-shape: ^2.0.0 + vega-canvas: ^1.2.5 + vega-loader: ^4.3.3 + vega-scale: ^7.1.1 + vega-util: ^1.15.2 + checksum: 3067da292a33a43ca1cd63b9a8a1e32cdea01051c9fdc14a5c81e8623f272082a713c6d3322b945ffa3456b55d0279337e81f0a3d4d031512a6f865e4df47340 + languageName: node + linkType: hard + +"vega-schema-url-parser@npm:^2.1.0": + version: 2.2.0 + resolution: "vega-schema-url-parser@npm:2.2.0" + checksum: 1ab17cde0a2514f42cfd0a1a19c7451e104025c68c09a15c9fe6a0f09bcc7b1c814a8a40f28ab5a69f3c9bda9824ca3f553a7b4338c5c64f7072edcd7bc3d130 + languageName: node + linkType: hard + +"vega-selections@npm:^5.3.1, vega-selections@npm:^5.4.2": + version: 5.4.2 + resolution: "vega-selections@npm:5.4.2" + dependencies: + d3-array: 3.2.4 + vega-expression: ^5.0.1 + vega-util: ^1.17.1 + checksum: 4e78053ab1f8ba4338005ed424043e7d0e91c857b58ab03600a07292e3777a4244d34caa7f8c85e72b2fdd9916882dfdda2fa93c730120ce790ec9883738f2be + languageName: node + linkType: hard + +"vega-statistics@npm:^1.7.9, vega-statistics@npm:^1.9.0": + version: 1.9.0 + resolution: "vega-statistics@npm:1.9.0" + dependencies: + d3-array: ^3.2.2 + checksum: bbf2ea088c5a6a662c6aed1bf57996c06a82a98228730ada8a97e57824a6ed391999ea974f16dcde6e73bf88799976d91aff748842848d38ab45dbb9fafba3f9 + languageName: node + linkType: hard + +"vega-statistics@npm:~1.7.9": + version: 1.7.10 + resolution: "vega-statistics@npm:1.7.10" + dependencies: + d3-array: ^2.7.1 + checksum: 474cb9587806e9f6aecac9a6677a7d9f1d9798834bb10da537752cb0ab80357aaff47b62dac274c444aaa1927d280ab19b1fb393a7c55ff3e502e2ac483962e5 + languageName: node + linkType: hard + +"vega-themes@npm:^2.10.0": + version: 2.15.0 + resolution: "vega-themes@npm:2.15.0" + peerDependencies: + vega: "*" + vega-lite: "*" + checksum: 7ef06baca0e8ddd915ce0b4d2b94a58ec72c926f43b105c82031b470db28074c4633f4d9654c6365db161c4300049a5d230d9babf92f5a06a1c8260ae0939b62 + languageName: node + linkType: hard + +"vega-time@npm:^2.0.3, vega-time@npm:^2.0.4, vega-time@npm:^2.1.2": + version: 2.1.2 + resolution: "vega-time@npm:2.1.2" + dependencies: + d3-array: ^3.2.2 + d3-time: ^3.1.0 + vega-util: ^1.17.2 + checksum: 35605db00f110f75274ee115716dc9e981da3cecb8c5692865557860058931ecb43d64f0ec2e7f00225a73e00fb7d1424d12b091a43ed11962a2f63177465dd6 + languageName: node + linkType: hard + +"vega-time@npm:~2.0.4": + version: 2.0.4 + resolution: "vega-time@npm:2.0.4" + dependencies: + d3-array: ^2.7.1 + d3-time: ^2.0.0 + vega-util: ^1.15.2 + checksum: 98053a7d3867f7495d497f7b5a3024bb3ed2cfa75933bd99f986987eec1a01e7570452e09aae9eafd385d1b77e932b62b21cb6fc989266c0e34d93c128e2096a + languageName: node + linkType: hard + +"vega-tooltip@npm:^0.25.1": + version: 0.25.1 + resolution: "vega-tooltip@npm:0.25.1" + dependencies: + vega-util: ^1.16.0 + checksum: de048c8ad7dc05860e4c5c3ad5208a684fa40512fbb1c71f24c3d6ca7bd01dcd08504223f151b1b6d7088e0084fab2e9c9c1a1756c944653a4706325be52f502 + languageName: node + linkType: hard + +"vega-transforms@npm:~4.9.3": + version: 4.9.4 + resolution: "vega-transforms@npm:4.9.4" + dependencies: + d3-array: ^2.7.1 + vega-dataflow: ^5.7.4 + vega-statistics: ^1.7.9 + vega-time: ^2.0.4 + vega-util: ^1.16.1 + checksum: 7059bebef948997ddbbe039bca56c8eefa6042d82d66e3a7a25a29a5416b923d8cf1323b090f689984cc09635b718ffa5a2604cf3f8e6ffa3041c3d9086cf59d + languageName: node + linkType: hard + +"vega-typings@npm:~0.20.0": + version: 0.20.0 + resolution: "vega-typings@npm:0.20.0" + dependencies: + vega-util: ^1.15.2 + checksum: 9ec6af3aae7ec28536ff1ffe6fefc0d8d6a2fd8b0c69a59c384732d9a1c226430cb1f1dfd42ab4091ed7d5a20127ffe69b1702ad7902fd873e1f95c1a631cb39 + languageName: node + linkType: hard + +"vega-util@npm:^1.15.0, vega-util@npm:^1.15.2, vega-util@npm:^1.16.0, vega-util@npm:^1.16.1, vega-util@npm:^1.17.1, vega-util@npm:^1.17.2": + version: 1.17.2 + resolution: "vega-util@npm:1.17.2" + checksum: 5d681cb1a6ffda7af1b74df7c1c46a32f1d874daef54f9c9c65c7d7c7bfc4271dc6d9b1c1c7a853b14eb6e4cc8ec811b0132cd3ea25fa85259eac92e1b4f07fa + languageName: node + linkType: hard + +"vega-util@npm:~1.14.0": + version: 1.14.1 + resolution: "vega-util@npm:1.14.1" + checksum: e52a977aff84862820bd66a57be6b936bfd19250d2578537a7382d5f605ec7e8e62c50081f49cae1e29b15b11b329ec099fd82beb04f9be67288bf34359b0ddf + languageName: node + linkType: hard + +"vega-util@npm:~1.16.1": + version: 1.16.1 + resolution: "vega-util@npm:1.16.1" + checksum: e88091f6235fb5a563a8865e0c317d77454a2c696df62572551a3629fde1b53bba2643b800238b0b28c479d5f900fb1c3547ccc7250f27dcca743308f9052fea + languageName: node + linkType: hard + +"vega-view-transforms@npm:~4.5.8": + version: 4.5.9 + resolution: "vega-view-transforms@npm:4.5.9" + dependencies: + vega-dataflow: ^5.7.5 + vega-scenegraph: ^4.10.2 + vega-util: ^1.17.1 + checksum: aeeaf3c2f1a02b1303c16a586dbcb20f208c101d06d7e988e18ab71fb67d87be5d8ff228ebf25971535d6e41dc816168cfa68b8676e7250df07a40aefdea32a7 + languageName: node + linkType: hard + +"vega-view@npm:~5.10.0": + version: 5.10.1 + resolution: "vega-view@npm:5.10.1" + dependencies: + d3-array: ^2.7.1 + d3-timer: ^2.0.0 + vega-dataflow: ^5.7.3 + vega-format: ^1.0.4 + vega-functions: ^5.10.0 + vega-runtime: ^6.1.3 + vega-scenegraph: ^4.9.4 + vega-util: ^1.16.1 + checksum: 6aca692b96d5b05b8c2239775f2c90405eaad3b82538d66e14836f32d735bc01a81c9bb4f799cddc955bd0441a37fb06d03f5df8d16f471a40c7cc2ac732aff9 + languageName: node + linkType: hard + +"vega-voronoi@npm:~4.1.5": + version: 4.1.5 + resolution: "vega-voronoi@npm:4.1.5" + dependencies: + d3-delaunay: ^5.3.0 + vega-dataflow: ^5.7.3 + vega-util: ^1.15.2 + checksum: a0506d9a22722a9022e84ca8d70b7ca39fd797845d1ca447abb584c4495ccc9eb62a976101e43aa94f594f96532f03e91d200ce7226dced37951075233662975 + languageName: node + linkType: hard + +"vega-wordcloud@npm:~4.1.3": + version: 4.1.5 + resolution: "vega-wordcloud@npm:4.1.5" + dependencies: + vega-canvas: ^1.2.7 + vega-dataflow: ^5.7.6 + vega-scale: ^7.4.1 + vega-statistics: ^1.9.0 + vega-util: ^1.17.2 + checksum: 88ac7776a0f7e02b2e50de7fd59d593ff16cad58cd756e219a9c9e6005343eb41c96109aff141b6be344f824e5238bac0d83c8a8d78136e380f9020a14c0e373 + languageName: node + linkType: hard + +"vega@npm:5.20.0": + version: 5.20.0 + resolution: "vega@npm:5.20.0" + dependencies: + vega-crossfilter: ~4.0.5 + vega-dataflow: ~5.7.3 + vega-encode: ~4.8.3 + vega-event-selector: ~2.0.6 + vega-expression: ~4.0.1 + vega-force: ~4.0.7 + vega-format: ~1.0.4 + vega-functions: ~5.12.0 + vega-geo: ~4.3.8 + vega-hierarchy: ~4.0.9 + vega-label: ~1.0.0 + vega-loader: ~4.4.0 + vega-parser: ~6.1.3 + vega-projection: ~1.4.5 + vega-regression: ~1.0.9 + vega-runtime: ~6.1.3 + vega-scale: ~7.1.1 + vega-scenegraph: ~4.9.4 + vega-statistics: ~1.7.9 + vega-time: ~2.0.4 + vega-transforms: ~4.9.3 + vega-typings: ~0.20.0 + vega-util: ~1.16.1 + vega-view: ~5.10.0 + vega-view-transforms: ~4.5.8 + vega-voronoi: ~4.1.5 + vega-wordcloud: ~4.1.3 + checksum: 56b30899dc6b77761d8e62f1930db36a42e12a9df8a20ad926ffb025cc36f71f4634a5d27fccd2b1fdc37330f59bcec19bf32a76a7749f1f429d88a4ef2ae996 + languageName: node + linkType: hard + "vlq@npm:^1.0.0": version: 1.0.1 resolution: "vlq@npm:1.0.1" @@ -12920,7 +14106,7 @@ __metadata: languageName: node linkType: hard -"whatwg-fetch@npm:^3.0.0": +"whatwg-fetch@npm:>=0.10.0, whatwg-fetch@npm:^3.0.0": version: 3.6.20 resolution: "whatwg-fetch@npm:3.6.20" checksum: c58851ea2c4efe5c2235f13450f426824cf0253c1d45da28f45900290ae602a20aff2ab43346f16ec58917d5562e159cd691efa368354b2e82918c2146a519c5 @@ -13182,7 +14368,7 @@ __metadata: languageName: node linkType: hard -"yargs-parser@npm:^18.1.2": +"yargs-parser@npm:^18.1.1, yargs-parser@npm:^18.1.2": version: 18.1.3 resolution: "yargs-parser@npm:18.1.3" dependencies: @@ -13255,6 +14441,25 @@ __metadata: languageName: node linkType: hard +"yargs@npm:~15.3.1": + version: 15.3.1 + resolution: "yargs@npm:15.3.1" + dependencies: + cliui: ^6.0.0 + decamelize: ^1.2.0 + find-up: ^4.1.0 + get-caller-file: ^2.0.1 + require-directory: ^2.1.1 + require-main-filename: ^2.0.0 + set-blocking: ^2.0.0 + string-width: ^4.2.0 + which-module: ^2.0.0 + y18n: ^4.0.0 + yargs-parser: ^18.1.1 + checksum: 6707f2771feea96ffb551d9f27f0052e64cdc341c554324a50ee81c8b66a6ef55b9a4ab9ad5089a0b005c3d92427ac42f59eecd045a8f47553b7163ba1249346 + languageName: node + linkType: hard + "yauzl@npm:^2.10.0": version: 2.10.0 resolution: "yauzl@npm:2.10.0" From 98b075b51790bbf75e3a7848483814d5ef54a6f0 Mon Sep 17 00:00:00 2001 From: William Candillon Date: Fri, 13 Dec 2024 15:27:50 +0100 Subject: [PATCH 16/35] :wrench: --- apps/example/ios/Podfile.lock | 27 +++ apps/example/package.json | 1 + .../src/TransformerJS/TransformerJS.tsx | 189 ++++++++++-------- yarn.lock | 33 ++- 4 files changed, 165 insertions(+), 85 deletions(-) diff --git a/apps/example/ios/Podfile.lock b/apps/example/ios/Podfile.lock index e29fb8c7c..122d4d412 100644 --- a/apps/example/ios/Podfile.lock +++ b/apps/example/ios/Podfile.lock @@ -937,6 +937,29 @@ PODS: - React-debug - react-native-safe-area-context (4.11.0): - React-Core + - react-native-skia (1.7.2): + - DoubleConversion + - glog + - hermes-engine + - RCT-Folly (= 2024.01.01.00) + - RCTRequired + - RCTTypeSafety + - React + - React-callinvoker + - React-Codegen + - React-Core + - React-debug + - React-Fabric + - React-featureflags + - React-graphics + - React-ImageManager + - React-NativeModulesApple + - React-RCTFabric + - React-rendererdebug + - React-utils + - ReactCommon/turbomodule/bridging + - ReactCommon/turbomodule/core + - Yoga - react-native-wgpu (0.1.20): - DoubleConversion - glog @@ -1336,6 +1359,7 @@ DEPENDENCIES: - React-logger (from `../../../node_modules/react-native/ReactCommon/logger`) - React-Mapbuffer (from `../../../node_modules/react-native/ReactCommon`) - react-native-safe-area-context (from `../../../node_modules/react-native-safe-area-context`) + - "react-native-skia (from `../../../node_modules/@shopify/react-native-skia`)" - react-native-wgpu (from `../../../node_modules/react-native-wgpu`) - React-nativeconfig (from `../../../node_modules/react-native/ReactCommon`) - React-NativeModulesApple (from `../../../node_modules/react-native/ReactCommon/react/nativemodule/core/platform/ios`) @@ -1435,6 +1459,8 @@ EXTERNAL SOURCES: :path: "../../../node_modules/react-native/ReactCommon" react-native-safe-area-context: :path: "../../../node_modules/react-native-safe-area-context" + react-native-skia: + :path: "../../../node_modules/@shopify/react-native-skia" react-native-wgpu: :path: "../../../node_modules/react-native-wgpu" React-nativeconfig: @@ -1528,6 +1554,7 @@ SPEC CHECKSUMS: React-logger: 29fa3e048f5f67fe396bc08af7606426d9bd7b5d React-Mapbuffer: bf56147c9775491e53122a94c423ac201417e326 react-native-safe-area-context: 851c62c48dce80ccaa5637b6aa5991a1bc36eca9 + react-native-skia: c85483f709f2b58d30a11fc005c2938ee87d6656 react-native-wgpu: 078c84b159de97704f8926011b0f9bc96591d478 React-nativeconfig: 9f223cd321823afdecf59ed00861ab2d69ee0fc1 React-NativeModulesApple: ff7efaff7098639db5631236cfd91d60abff04c0 diff --git a/apps/example/package.json b/apps/example/package.json index e6887e291..c5735f4a5 100644 --- a/apps/example/package.json +++ b/apps/example/package.json @@ -20,6 +20,7 @@ "@react-navigation/native": "^6.1.17", "@react-navigation/stack": "^6.4.0", "@react-three/fiber": "^8.17.6", + "@shopify/react-native-skia": "^1.7.2", "@tensorflow/tfjs": "^4.22.0", "@tensorflow/tfjs-backend-webgpu": "^4.22.0", "@tensorflow/tfjs-vis": "^1.5.1", diff --git a/apps/example/src/TransformerJS/TransformerJS.tsx b/apps/example/src/TransformerJS/TransformerJS.tsx index cdcb0de3d..9d1a47a2d 100644 --- a/apps/example/src/TransformerJS/TransformerJS.tsx +++ b/apps/example/src/TransformerJS/TransformerJS.tsx @@ -3,95 +3,116 @@ import { View } from "react-native"; // Import TensorFlow.js and required modules import * as tf from "@tensorflow/tfjs"; import "@tensorflow/tfjs-backend-webgpu"; -//import * as tfvis from "@tensorflow/tfjs-vis"; +import { Platform } from "@tensorflow/tfjs-core"; +import { GPUOffscreenCanvas } from "./OffscreenCanvas"; + +global.OffscreenCanvas = GPUOffscreenCanvas; + +class PlatformReactNative implements Platform { + /** + * Makes an HTTP request. + * + * see @fetch docs above. + */ + async fetch( + path: string, + init?: RequestInit, + options?: tf.io.RequestDetails, + ) { + return fetch(path, init, options); + } + + /** + * Encode the provided string into an array of bytes using the provided + * encoding. + */ + encode(text: string, encoding: string): Uint8Array { + // See https://www.w3.org/TR/encoding/#utf-16le + if (encoding === "utf-16") { + encoding = "utf16le"; + } + return new Uint8Array(Buffer.from(text, encoding as BufferEncoding)); + } + /** Decode the provided bytes into a string using the provided encoding. */ + decode(bytes: Uint8Array, encoding: string): string { + // See https://www.w3.org/TR/encoding/#utf-16le + if (encoding === "utf-16") { + encoding = "utf16le"; + } + return Buffer.from(bytes).toString(encoding as BufferEncoding); + } + + now(): number { + //@ts-ignore + if (global.nativePerformanceNow) { + //@ts-ignore + return global.nativePerformanceNow(); + } + return Date.now(); + } + + setTimeoutCustom() { + throw new Error("react native does not support setTimeoutCustom"); + } + + isTypedArray( + a: unknown, + ): a is Uint8Array | Float32Array | Int32Array | Uint8ClampedArray { + return ( + a instanceof Float32Array || + a instanceof Int32Array || + a instanceof Uint8Array || + a instanceof Uint8ClampedArray + ); + } +} + +tf.setPlatform("react-native", new PlatformReactNative()); (async () => { + console.log("Starting TensorFlow.js with WebGPU backend..."); // Set the backend to WebGPU const result = await tf.setBackend("webgpu"); console.log("WebGPU backend is ready: " + result); - - // // Load and preprocess the Fashion MNIST dataset - // const loadFashionMnist = async () => { - // const mnist = await tf.data.csv( - // "https://storage.googleapis.com/tfjs-examples/mnist-data/mnist_test.csv", - // { columnConfigs: { label: { isLabel: true } } }, - // ); - - // const NUM_CLASSES = 10; - // const DATA_SIZE = 70000; - - // // Split features and labels, normalize feature values - // const processedData = mnist.map(({ xs, ys }) => { - // const labels = tf.oneHot(tf.tensor1d([ys.label], "int32"), NUM_CLASSES); - // const images = tf.tensor2d(Object.values(xs), [1, 28 * 28]).div(255); - // return { xs: images, ys: labels }; - // }); - - // const split = DATA_SIZE * 0.8; - // return { - // trainData: processedData.take(split), - // testData: processedData.skip(split), - // }; - // }; - - // // Define a simple CNN model - // const createModel = () => { - // const model = tf.sequential(); - - // // Add layers - // model.add( - // tf.layers.conv2d({ - // inputShape: [28, 28, 1], - // kernelSize: 3, - // filters: 16, - // activation: "relu", - // }), - // ); - // model.add(tf.layers.maxPooling2d({ poolSize: [2, 2] })); - // model.add(tf.layers.flatten()); - // model.add(tf.layers.dense({ units: 128, activation: "relu" })); - // model.add(tf.layers.dense({ units: 10, activation: "softmax" })); - - // model.compile({ - // optimizer: tf.train.adam(), - // loss: "categoricalCrossentropy", - // metrics: ["accuracy"], - // }); - - // return model; - // }; - - // // Train and evaluate the model - // const trainModel = async (model, trainData, testData) => { - // const BATCH_SIZE = 32; - // const EPOCHS = 5; - - // const trainXs = trainData.map(({ xs }) => xs); - // const trainYs = trainData.map(({ ys }) => ys); - - // const testXs = testData.map(({ xs }) => xs); - // const testYs = testData.map(({ ys }) => ys); - - // await model.fit(trainXs, trainYs, { - // batchSize: BATCH_SIZE, - // epochs: EPOCHS, - // validationData: [testXs, testYs], - // callbacks: tfvis.show.fitCallbacks( - // { name: "Training Performance" }, - // ["loss", "val_loss", "accuracy", "val_accuracy"], - // { height: 200, callbacks: ["onEpochEnd"] }, - // ), - // }); - - // const testResult = model.evaluate(testXs, testYs); - // console.log("Test Accuracy:", testResult[1].dataSync()[0]); - // }; - - // (async () => { - // const { trainData, testData } = await loadFashionMnist(); - // const model = createModel(); - // await trainModel(model, trainData, testData); - // })(); + console.log("tf.getBackend()", tf.getBackend()); + const xs = tf.linspace(-1, 1, 100); + const ys = xs + .mul(2) + .sub(1) + .add(tf.randomNormal([100], 0, 0.1)); + + // Create a simple model with one dense layer. + const model = tf.sequential(); + model.add(tf.layers.dense({ units: 1, inputShape: [1] })); + model.compile({ loss: "meanSquaredError", optimizer: "sgd" }); + + // Reshape inputs to match the model's input. + const xTensor = xs.reshape([100, 1]); + const yTensor = ys.reshape([100, 1]); + + // Train the model and log progress. + console.log("Starting training..."); + const t0 = performance.now(); + await model.fit(xTensor, yTensor, { + batchSize: 32, + epochs: 20, + callbacks: { + onEpochEnd: (epoch, logs) => { + console.log(`Epoch ${epoch + 1}: Loss = ${logs.loss.toFixed(4)}`); + }, + }, + }); + const t1 = performance.now(); + console.log(`Training completed in ${(t1 - t0).toFixed(2)} ms.`); + + // Test the model with some values. + const testValues = [-0.5, 0.5, 2, -2]; + const testXs = tf.tensor2d(testValues, [testValues.length, 1]); + const preds = model.predict(testXs); + const predValues = preds.dataSync(); + + console.log("Test inputs: ", testValues); + console.log("Predictions: ", predValues); })(); export const TransformerJS = () => { diff --git a/yarn.lock b/yarn.lock index 447b1a18a..bc116695d 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3396,6 +3396,27 @@ __metadata: languageName: node linkType: hard +"@shopify/react-native-skia@npm:^1.7.2": + version: 1.7.2 + resolution: "@shopify/react-native-skia@npm:1.7.2" + dependencies: + canvaskit-wasm: 0.39.1 + react-reconciler: 0.27.0 + peerDependencies: + react: ">=18.0" + react-native: ">=0.64" + react-native-reanimated: ">=2.0.0" + peerDependenciesMeta: + react-native: + optional: true + react-native-reanimated: + optional: true + bin: + setup-skia-web: ./scripts/setup-canvaskit.js + checksum: b23ba33f23150199cdbf973b50580f756b94763d9331d129b0844cb8a6db618e2257df2759601b27ca91d7b8f41bf550f7965bb16f4ff8b15b07b082110da489 + languageName: node + linkType: hard + "@sideway/address@npm:^4.1.5": version: 4.1.5 resolution: "@sideway/address@npm:4.1.5" @@ -4517,6 +4538,7 @@ __metadata: "@react-navigation/stack": ^6.4.0 "@react-three/fiber": ^8.17.6 "@rnx-kit/metro-config": ^1.3.17 + "@shopify/react-native-skia": ^1.7.2 "@tensorflow/tfjs": ^4.22.0 "@tensorflow/tfjs-backend-webgpu": ^4.22.0 "@tensorflow/tfjs-vis": ^1.5.1 @@ -5373,6 +5395,15 @@ __metadata: languageName: node linkType: hard +"canvaskit-wasm@npm:0.39.1": + version: 0.39.1 + resolution: "canvaskit-wasm@npm:0.39.1" + dependencies: + "@webgpu/types": 0.1.21 + checksum: da62926fc81f424a781e148b4d76bb5fc9b0188f136090b3b287522dc653cb002bfb406e2eff45b55fcc1cafbc7629f988e20ad6c777bab85c1bb09e1091a5e2 + languageName: node + linkType: hard + "chalk@npm:^2.4.2": version: 2.4.2 resolution: "chalk@npm:2.4.2" @@ -11539,7 +11570,7 @@ __metadata: languageName: node linkType: hard -"react-reconciler@npm:^0.27.0": +"react-reconciler@npm:0.27.0, react-reconciler@npm:^0.27.0": version: 0.27.0 resolution: "react-reconciler@npm:0.27.0" dependencies: From c8713424831d8d0db9abe7806f13e33efdba52d7 Mon Sep 17 00:00:00 2001 From: William Candillon Date: Fri, 13 Dec 2024 19:15:11 +0100 Subject: [PATCH 17/35] :wrench: --- apps/example/src/Home.tsx | 4 ++-- apps/example/src/TransformerJS/TransformerJS.tsx | 6 ++++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/apps/example/src/Home.tsx b/apps/example/src/Home.tsx index 127c46eaa..f108b6a7c 100644 --- a/apps/example/src/Home.tsx +++ b/apps/example/src/Home.tsx @@ -24,8 +24,8 @@ export const examples = [ title: "☘️ Three.js", }, { - screen: "TransformerJS", - title: "🤖 transformer.js", + screen: "tensorflow.js", + title: "🤖 tensorflow.js", }, { screen: "Cube", diff --git a/apps/example/src/TransformerJS/TransformerJS.tsx b/apps/example/src/TransformerJS/TransformerJS.tsx index 9d1a47a2d..210226649 100644 --- a/apps/example/src/TransformerJS/TransformerJS.tsx +++ b/apps/example/src/TransformerJS/TransformerJS.tsx @@ -84,7 +84,9 @@ tf.setPlatform("react-native", new PlatformReactNative()); // Create a simple model with one dense layer. const model = tf.sequential(); model.add(tf.layers.dense({ units: 1, inputShape: [1] })); - model.compile({ loss: "meanSquaredError", optimizer: "sgd" }); + model.compile({ loss: 'meanSquaredError', + optimizer: tf.train.sgd(0.1) + }); // Reshape inputs to match the model's input. const xTensor = xs.reshape([100, 1]); @@ -109,7 +111,7 @@ tf.setPlatform("react-native", new PlatformReactNative()); const testValues = [-0.5, 0.5, 2, -2]; const testXs = tf.tensor2d(testValues, [testValues.length, 1]); const preds = model.predict(testXs); - const predValues = preds.dataSync(); + const predValues = await preds.data(); console.log("Test inputs: ", testValues); console.log("Predictions: ", predValues); From 965ca75551a7b83bd31ef07d704c72446317dd84 Mon Sep 17 00:00:00 2001 From: William Candillon Date: Fri, 13 Dec 2024 19:29:27 +0100 Subject: [PATCH 18/35] :wrench: --- apps/example/src/Home.tsx | 2 +- .../src/TransformerJS/TransformerJS.tsx | 293 +++++++++++++----- 2 files changed, 219 insertions(+), 76 deletions(-) diff --git a/apps/example/src/Home.tsx b/apps/example/src/Home.tsx index f108b6a7c..85d7b4150 100644 --- a/apps/example/src/Home.tsx +++ b/apps/example/src/Home.tsx @@ -24,7 +24,7 @@ export const examples = [ title: "☘️ Three.js", }, { - screen: "tensorflow.js", + screen: "TransformerJS", title: "🤖 tensorflow.js", }, { diff --git a/apps/example/src/TransformerJS/TransformerJS.tsx b/apps/example/src/TransformerJS/TransformerJS.tsx index 210226649..b6d45aa4c 100644 --- a/apps/example/src/TransformerJS/TransformerJS.tsx +++ b/apps/example/src/TransformerJS/TransformerJS.tsx @@ -1,6 +1,5 @@ -import { View } from "react-native"; - -// Import TensorFlow.js and required modules +import { View, TextInput, Text, Button } from "react-native"; +import React, { useState, useEffect } from "react"; import * as tf from "@tensorflow/tfjs"; import "@tensorflow/tfjs-backend-webgpu"; import { Platform } from "@tensorflow/tfjs-core"; @@ -8,44 +7,28 @@ import { GPUOffscreenCanvas } from "./OffscreenCanvas"; global.OffscreenCanvas = GPUOffscreenCanvas; +// Platform implementation for React Native class PlatformReactNative implements Platform { - /** - * Makes an HTTP request. - * - * see @fetch docs above. - */ - async fetch( - path: string, - init?: RequestInit, - options?: tf.io.RequestDetails, - ) { + async fetch(path, init, options) { return fetch(path, init, options); } - /** - * Encode the provided string into an array of bytes using the provided - * encoding. - */ - encode(text: string, encoding: string): Uint8Array { - // See https://www.w3.org/TR/encoding/#utf-16le + encode(text, encoding) { if (encoding === "utf-16") { encoding = "utf16le"; } - return new Uint8Array(Buffer.from(text, encoding as BufferEncoding)); + return new Uint8Array(Buffer.from(text, encoding)); } - /** Decode the provided bytes into a string using the provided encoding. */ - decode(bytes: Uint8Array, encoding: string): string { - // See https://www.w3.org/TR/encoding/#utf-16le + + decode(bytes, encoding) { if (encoding === "utf-16") { encoding = "utf16le"; } - return Buffer.from(bytes).toString(encoding as BufferEncoding); + return Buffer.from(bytes).toString(encoding); } - now(): number { - //@ts-ignore + now() { if (global.nativePerformanceNow) { - //@ts-ignore return global.nativePerformanceNow(); } return Date.now(); @@ -55,9 +38,7 @@ class PlatformReactNative implements Platform { throw new Error("react native does not support setTimeoutCustom"); } - isTypedArray( - a: unknown, - ): a is Uint8Array | Float32Array | Int32Array | Uint8ClampedArray { + isTypedArray(a) { return ( a instanceof Float32Array || a instanceof Int32Array || @@ -69,54 +50,216 @@ class PlatformReactNative implements Platform { tf.setPlatform("react-native", new PlatformReactNative()); -(async () => { - console.log("Starting TensorFlow.js with WebGPU backend..."); - // Set the backend to WebGPU - const result = await tf.setBackend("webgpu"); - console.log("WebGPU backend is ready: " + result); - console.log("tf.getBackend()", tf.getBackend()); - const xs = tf.linspace(-1, 1, 100); - const ys = xs - .mul(2) - .sub(1) - .add(tf.randomNormal([100], 0, 0.1)); - - // Create a simple model with one dense layer. - const model = tf.sequential(); - model.add(tf.layers.dense({ units: 1, inputShape: [1] })); - model.compile({ loss: 'meanSquaredError', - optimizer: tf.train.sgd(0.1) - }); +// Vocabulary for text tokenization +const VOCAB_SIZE = 5000; +const MAX_LENGTH = 100; - // Reshape inputs to match the model's input. - const xTensor = xs.reshape([100, 1]); - const yTensor = ys.reshape([100, 1]); - - // Train the model and log progress. - console.log("Starting training..."); - const t0 = performance.now(); - await model.fit(xTensor, yTensor, { - batchSize: 32, - epochs: 20, - callbacks: { - onEpochEnd: (epoch, logs) => { - console.log(`Epoch ${epoch + 1}: Loss = ${logs.loss.toFixed(4)}`); - }, - }, +// Create a simple tokenizer +const tokenize = (text) => { + const words = text.toLowerCase().split(/\s+/); + return words.map(word => { + // Simple hash function for word to index mapping + const hash = Array.from(word).reduce( + (hash, char) => ((hash << 5) - hash) + char.charCodeAt(0), + 0 + ); + return Math.abs(hash) % VOCAB_SIZE; }); - const t1 = performance.now(); - console.log(`Training completed in ${(t1 - t0).toFixed(2)} ms.`); +}; - // Test the model with some values. - const testValues = [-0.5, 0.5, 2, -2]; - const testXs = tf.tensor2d(testValues, [testValues.length, 1]); - const preds = model.predict(testXs); - const predValues = await preds.data(); +const padSequence = (sequence, maxLength) => { + if (sequence.length > maxLength) { + return sequence.slice(0, maxLength); + } + return [...sequence, ...new Array(maxLength - sequence.length).fill(0)]; +}; - console.log("Test inputs: ", testValues); - console.log("Predictions: ", predValues); -})(); +const createModel = () => { + const model = tf.sequential(); + + model.add(tf.layers.embedding({ + inputDim: VOCAB_SIZE, + outputDim: 32, + inputLength: MAX_LENGTH + })); + + model.add(tf.layers.lstm({units: 32, returnSequences: false})); -export const TransformerJS = () => { - return ; + //model.add(tf.layers.globalAveragePooling1d()); + // model.add(tf.layers.dropout({rate: 0.5})); + + model.add(tf.layers.dense({ + units: 16, + activation: 'relu' + })); + + model.add(tf.layers.dense({ + units: 1, + activation: 'sigmoid' + })); + + model.compile({ + optimizer: tf.train.adam(0.001), + loss: 'binaryCrossentropy', + metrics: ['accuracy'] + }); + + return model; }; + +export const TransformerJS = () => { + const [isModelReady, setIsModelReady] = useState(false); + const [inputText, setInputText] = useState(""); + const [sentiment, setSentiment] = useState(null); + const [model, setModel] = useState(null); + + useEffect(() => { + const initializeTensorFlow = async () => { + try { + console.log("Initializing TensorFlow.js with WebGPU backend..."); + await tf.setBackend("webgpu"); + console.log("Backend initialized:", tf.getBackend()); + + // Create and initialize the model + const sentimentModel = createModel(); + + // Train the model with some sample data + const sampleTexts = [ + "this is great", + "i love it", + "amazing experience", + "i am happy", + "feeling wonderful", + "excellent service", + "fantastic results", + "very satisfied", + "perfect solution", + "awesome day", + "really pleased", + "joy and happiness", + "feeling blessed", + "incredible experience", + "super excited", + + // Negative examples + "terrible service", + "worst product ever", + "very disappointed", + "i am sad", + "feeling depressed", + "awful experience", + "this is horrible", + "completely frustrated", + "waste of time", + "very unhappy", + "absolutely terrible", + "poor quality", + "extremely disappointed", + "total failure", + "not satisfied" + ]; + + const labels = [ + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // Positive labels + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 // Negative labels + ]; + + // Tokenize and pad the sample texts + const sequences = sampleTexts.map(text => + padSequence(tokenize(text), MAX_LENGTH) + ); + + // Convert to tensors + const xTrain = tf.tensor2d(sequences); + const yTrain = tf.tensor1d(labels); + + // Train the model + await sentimentModel.fit(xTrain, yTrain, { + epochs: 30, + verbose: 1 + }); + + setModel(sentimentModel); + setIsModelReady(true); + + // Clean up tensors + xTrain.dispose(); + yTrain.dispose(); + + } catch (error) { + console.error("Error initializing TensorFlow:", error); + } + }; + + initializeTensorFlow(); + }, []); + + const analyzeSentiment = async () => { + if (!model || !inputText) return; + + try { + // Tokenize and pad the input text + const sequence = padSequence(tokenize(inputText), MAX_LENGTH); + + // Convert to tensor and get prediction + const inputTensor = tf.tensor2d([sequence]); + const prediction = await model.predict(inputTensor).data(); + + // Get sentiment score (0-1) + console.log("Prediction:", prediction); + const sentimentScore = prediction[0]; + setSentiment(sentimentScore); + + // Clean up tensor + inputTensor.dispose(); + + } catch (error) { + console.error("Error analyzing sentiment:", error); + } + }; + + return ( + + + Sentiment Analysis Demo + + + {!isModelReady ? ( + Loading model... + ) : ( + <> + + +