chore: update node_modules with new binary files and dependencies

- Add new binary files for nodemon, onnxruntime-web, and xenova/transformers
- Update various JavaScript and TypeScript files in node_modules
- Remove unused files and dependencies
- Add new test fixtures and documentation files
This commit is contained in:
你的名字
2025-07-18 08:41:48 +08:00
parent db8c0adc79
commit 4c2f5c69a1
4655 changed files with 811329 additions and 35112 deletions

202
node_modules/@xenova/transformers/LICENSE generated vendored Normal file
View File

@@ -0,0 +1,202 @@
Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions for use, reproduction,
and distribution as defined by Sections 1 through 9 of this document.
"Licensor" shall mean the copyright owner or entity authorized by
the copyright owner that is granting the License.
"Legal Entity" shall mean the union of the acting entity and all
other entities that control, are controlled by, or are under common
control with that entity. For the purposes of this definition,
"control" means (i) the power, direct or indirect, to cause the
direction or management of such entity, whether by contract or
otherwise, or (ii) ownership of fifty percent (50%) or more of the
outstanding shares, or (iii) beneficial ownership of such entity.
"You" (or "Your") shall mean an individual or Legal Entity
exercising permissions granted by this License.
"Source" form shall mean the preferred form for making modifications,
including but not limited to software source code, documentation
source, and configuration files.
"Object" form shall mean any form resulting from mechanical
transformation or translation of a Source form, including but
not limited to compiled object code, generated documentation,
and conversions to other media types.
"Work" shall mean the work of authorship, whether in Source or
Object form, made available under the License, as indicated by a
copyright notice that is included in or attached to the work
(an example is provided in the Appendix below).
"Derivative Works" shall mean any work, whether in Source or Object
form, that is based on (or derived from) the Work and for which the
editorial revisions, annotations, elaborations, or other modifications
represent, as a whole, an original work of authorship. For the purposes
of this License, Derivative Works shall not include works that remain
separable from, or merely link (or bind by name) to the interfaces of,
the Work and Derivative Works thereof.
"Contribution" shall mean any work of authorship, including
the original version of the Work and any modifications or additions
to that Work or Derivative Works thereof, that is intentionally
submitted to Licensor for inclusion in the Work by the copyright owner
or by an individual or Legal Entity authorized to submit on behalf of
the copyright owner. For the purposes of this definition, "submitted"
means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control systems,
and issue tracking systems that are managed by, or on behalf of, the
Licensor for the purpose of discussing and improving the Work, but
excluding communication that is conspicuously marked or otherwise
designated in writing by the copyright owner as "Not a Contribution."
"Contributor" shall mean Licensor and any individual or Legal Entity
on behalf of whom a Contribution has been received by Licensor and
subsequently incorporated within the Work.
2. Grant of Copyright License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
copyright license to reproduce, prepare Derivative Works of,
publicly display, publicly perform, sublicense, and distribute the
Work and such Derivative Works in Source or Object form.
3. Grant of Patent License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
(except as stated in this section) patent license to make, have made,
use, offer to sell, sell, import, and otherwise transfer the Work,
where such license applies only to those patent claims licensable
by such Contributor that are necessarily infringed by their
Contribution(s) alone or by combination of their Contribution(s)
with the Work to which such Contribution(s) was submitted. If You
institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work
or a Contribution incorporated within the Work constitutes direct
or contributory patent infringement, then any patent licenses
granted to You under this License for that Work shall terminate
as of the date such litigation is filed.
4. Redistribution. You may reproduce and distribute copies of the
Work or Derivative Works thereof in any medium, with or without
modifications, and in Source or Object form, provided that You
meet the following conditions:
(a) You must give any other recipients of the Work or
Derivative Works a copy of this License; and
(b) You must cause any modified files to carry prominent notices
stating that You changed the files; and
(c) You must retain, in the Source form of any Derivative Works
that You distribute, all copyright, patent, trademark, and
attribution notices from the Source form of the Work,
excluding those notices that do not pertain to any part of
the Derivative Works; and
(d) If the Work includes a "NOTICE" text file as part of its
distribution, then any Derivative Works that You distribute must
include a readable copy of the attribution notices contained
within such NOTICE file, excluding those notices that do not
pertain to any part of the Derivative Works, in at least one
of the following places: within a NOTICE text file distributed
as part of the Derivative Works; within the Source form or
documentation, if provided along with the Derivative Works; or,
within a display generated by the Derivative Works, if and
wherever such third-party notices normally appear. The contents
of the NOTICE file are for informational purposes only and
do not modify the License. You may add Your own attribution
notices within Derivative Works that You distribute, alongside
or as an addendum to the NOTICE text from the Work, provided
that such additional attribution notices cannot be construed
as modifying the License.
You may add Your own copyright statement to Your modifications and
may provide additional or different license terms and conditions
for use, reproduction, or distribution of Your modifications, or
for any such Derivative Works as a whole, provided Your use,
reproduction, and distribution of the Work otherwise complies with
the conditions stated in this License.
5. Submission of Contributions. Unless You explicitly state otherwise,
any Contribution intentionally submitted for inclusion in the Work
by You to the Licensor shall be under the terms and conditions of
this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify
the terms of any separate license agreement you may have executed
with Licensor regarding such Contributions.
6. Trademarks. This License does not grant permission to use the trade
names, trademarks, service marks, or product names of the Licensor,
except as required for reasonable and customary use in describing the
origin of the Work and reproducing the content of the NOTICE file.
7. Disclaimer of Warranty. Unless required by applicable law or
agreed to in writing, Licensor provides the Work (and each
Contributor provides its Contributions) on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied, including, without limitation, any warranties or conditions
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
PARTICULAR PURPOSE. You are solely responsible for determining the
appropriateness of using or redistributing the Work and assume any
risks associated with Your exercise of permissions under this License.
8. Limitation of Liability. In no event and under no legal theory,
whether in tort (including negligence), contract, or otherwise,
unless required by applicable law (such as deliberate and grossly
negligent acts) or agreed to in writing, shall any Contributor be
liable to You for damages, including any direct, indirect, special,
incidental, or consequential damages of any character arising as a
result of this License or out of the use or inability to use the
Work (including but not limited to damages for loss of goodwill,
work stoppage, computer failure or malfunction, or any and all
other commercial damages or losses), even if such Contributor
has been advised of the possibility of such damages.
9. Accepting Warranty or Additional Liability. While redistributing
the Work or Derivative Works thereof, You may choose to offer,
and charge a fee for, acceptance of support, warranty, indemnity,
or other liability obligations and/or rights consistent with this
License. However, in accepting such obligations, You may act only
on Your own behalf and on Your sole responsibility, not on behalf
of any other Contributor, and only if You agree to indemnify,
defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason
of your accepting any such warranty or additional liability.
END OF TERMS AND CONDITIONS
APPENDIX: How to apply the Apache License to your work.
To apply the Apache License to your work, attach the following
boilerplate notice, with the fields enclosed by brackets "[]"
replaced with your own identifying information. (Don't include
the brackets!) The text should be enclosed in the appropriate
comment syntax for the file format. We also recommend that a
file or class name and description of purpose be included on the
same "printed page" as the copyright notice for easier
identification within third-party archives.
Copyright [yyyy] [name of copyright owner]
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.

361
node_modules/@xenova/transformers/README.md generated vendored Normal file
View File

@@ -0,0 +1,361 @@
<p align="center">
<br/>
<picture>
<source media="(prefers-color-scheme: dark)" srcset="https://huggingface.co/datasets/Xenova/transformers.js-docs/raw/main/transformersjs-dark.svg" width="500" style="max-width: 100%;">
<source media="(prefers-color-scheme: light)" srcset="https://huggingface.co/datasets/Xenova/transformers.js-docs/raw/main/transformersjs-light.svg" width="500" style="max-width: 100%;">
<img alt="transformers.js javascript library logo" src="https://huggingface.co/datasets/Xenova/transformers.js-docs/raw/main/transformersjs-light.svg" width="500" style="max-width: 100%;">
</picture>
<br/>
</p>
<p align="center">
<a href="https://www.npmjs.com/package/@xenova/transformers">
<img alt="NPM" src="https://img.shields.io/npm/v/@xenova/transformers">
</a>
<a href="https://www.npmjs.com/package/@xenova/transformers">
<img alt="NPM Downloads" src="https://img.shields.io/npm/dw/@xenova/transformers">
</a>
<a href="https://www.jsdelivr.com/package/npm/@xenova/transformers">
<img alt="jsDelivr Hits" src="https://img.shields.io/jsdelivr/npm/hw/@xenova/transformers">
</a>
<a href="https://github.com/xenova/transformers.js/blob/main/LICENSE">
<img alt="License" src="https://img.shields.io/github/license/xenova/transformers.js?color=blue">
</a>
<a href="https://huggingface.co/docs/transformers.js/index">
<img alt="Documentation" src="https://img.shields.io/website/http/huggingface.co/docs/transformers.js/index.svg?down_color=red&down_message=offline&up_message=online">
</a>
</p>
State-of-the-art Machine Learning for the web. Run 🤗 Transformers directly in your browser, with no need for a server!
Transformers.js is designed to be functionally equivalent to Hugging Face's [transformers](https://github.com/huggingface/transformers) python library, meaning you can run the same pretrained models using a very similar API. These models support common tasks in different modalities, such as:
- 📝 **Natural Language Processing**: text classification, named entity recognition, question answering, language modeling, summarization, translation, multiple choice, and text generation.
- 🖼️ **Computer Vision**: image classification, object detection, and segmentation.
- 🗣️ **Audio**: automatic speech recognition and audio classification.
- 🐙 **Multimodal**: zero-shot image classification.
Transformers.js uses [ONNX Runtime](https://onnxruntime.ai/) to run models in the browser. The best part about it, is that you can easily [convert](#convert-your-models-to-onnx) your pretrained PyTorch, TensorFlow, or JAX models to ONNX using [🤗 Optimum](https://github.com/huggingface/optimum#onnx--onnx-runtime).
For more information, check out the full [documentation](https://huggingface.co/docs/transformers.js).
## Quick tour
It's super simple to translate from existing code! Just like the python library, we support the `pipeline` API. Pipelines group together a pretrained model with preprocessing of inputs and postprocessing of outputs, making it the easiest way to run models with the library.
<table>
<tr>
<th width="440px" align="center"><b>Python (original)</b></th>
<th width="440px" align="center"><b>Javascript (ours)</b></th>
</tr>
<tr>
<td>
```python
from transformers import pipeline
# Allocate a pipeline for sentiment-analysis
pipe = pipeline('sentiment-analysis')
out = pipe('I love transformers!')
# [{'label': 'POSITIVE', 'score': 0.999806941}]
```
</td>
<td>
```javascript
import { pipeline } from '@xenova/transformers';
// Allocate a pipeline for sentiment-analysis
let pipe = await pipeline('sentiment-analysis');
let out = await pipe('I love transformers!');
// [{'label': 'POSITIVE', 'score': 0.999817686}]
```
</td>
</tr>
</table>
You can also use a different model by specifying the model id or path as the second argument to the `pipeline` function. For example:
```javascript
// Use a different model for sentiment-analysis
let pipe = await pipeline('sentiment-analysis', 'Xenova/bert-base-multilingual-uncased-sentiment');
```
## Installation
To install via [NPM](https://www.npmjs.com/package/@xenova/transformers), run:
```bash
npm i @xenova/transformers
```
Alternatively, you can use it in vanilla JS, without any bundler, by using a CDN or static hosting. For example, using [ES Modules](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules), you can import the library with:
```html
<script type="module">
import { pipeline } from 'https://cdn.jsdelivr.net/npm/@xenova/transformers@2.17.2';
</script>
```
## Examples
Want to jump straight in? Get started with one of our sample applications/templates:
| Name | Description | Links |
|-------------------|----------------------------------|-------------------------------|
| Whisper Web | Speech recognition w/ Whisper | [code](https://github.com/xenova/whisper-web), [demo](https://huggingface.co/spaces/Xenova/whisper-web) |
| Doodle Dash | Real-time sketch-recognition game | [blog](https://huggingface.co/blog/ml-web-games), [code](https://github.com/xenova/doodle-dash), [demo](https://huggingface.co/spaces/Xenova/doodle-dash) |
| Code Playground | In-browser code completion website | [code](https://github.com/xenova/transformers.js/tree/main/examples/code-completion/), [demo](https://huggingface.co/spaces/Xenova/ai-code-playground) |
| Semantic Image Search (client-side) | Search for images with text | [code](https://github.com/xenova/transformers.js/tree/main/examples/semantic-image-search-client/), [demo](https://huggingface.co/spaces/Xenova/semantic-image-search-client) |
| Semantic Image Search (server-side) | Search for images with text (Supabase) | [code](https://github.com/xenova/transformers.js/tree/main/examples/semantic-image-search/), [demo](https://huggingface.co/spaces/Xenova/semantic-image-search) |
| Vanilla JavaScript | In-browser object detection | [video](https://scrimba.com/scrim/cKm9bDAg), [code](https://github.com/xenova/transformers.js/tree/main/examples/vanilla-js/), [demo](https://huggingface.co/spaces/Scrimba/vanilla-js-object-detector) |
| React | Multilingual translation website | [code](https://github.com/xenova/transformers.js/tree/main/examples/react-translator/), [demo](https://huggingface.co/spaces/Xenova/react-translator) |
| Text to speech (client-side) | In-browser speech synthesis | [code](https://github.com/xenova/transformers.js/tree/main/examples/text-to-speech-client/), [demo](https://huggingface.co/spaces/Xenova/text-to-speech-client) |
| Browser extension | Text classification extension | [code](https://github.com/xenova/transformers.js/tree/main/examples/extension/) |
| Electron | Text classification application | [code](https://github.com/xenova/transformers.js/tree/main/examples/electron/) |
| Next.js (client-side) | Sentiment analysis (in-browser inference) | [code](https://github.com/xenova/transformers.js/tree/main/examples/next-client/), [demo](https://huggingface.co/spaces/Xenova/next-example-app) |
| Next.js (server-side) | Sentiment analysis (Node.js inference) | [code](https://github.com/xenova/transformers.js/tree/main/examples/next-server/), [demo](https://huggingface.co/spaces/Xenova/next-server-example-app) |
| Node.js | Sentiment analysis API | [code](https://github.com/xenova/transformers.js/tree/main/examples/node/) |
| Demo site | A collection of demos | [code](https://github.com/xenova/transformers.js/tree/main/examples/demo-site/), [demo](https://xenova.github.io/transformers.js/) |
Check out the Transformers.js [template](https://huggingface.co/new-space?template=static-templates%2Ftransformers.js) on Hugging Face to get started in one click!
## Custom usage
By default, Transformers.js uses [hosted pretrained models](https://huggingface.co/models?library=transformers.js) and [precompiled WASM binaries](https://cdn.jsdelivr.net/npm/@xenova/transformers@2.17.2/dist/), which should work out-of-the-box. You can customize this as follows:
### Settings
```javascript
import { env } from '@xenova/transformers';
// Specify a custom location for models (defaults to '/models/').
env.localModelPath = '/path/to/models/';
// Disable the loading of remote models from the Hugging Face Hub:
env.allowRemoteModels = false;
// Set location of .wasm files. Defaults to use a CDN.
env.backends.onnx.wasm.wasmPaths = '/path/to/files/';
```
For a full list of available settings, check out the [API Reference](https://huggingface.co/docs/transformers.js/api/env).
### Convert your models to ONNX
We recommend using our [conversion script](https://github.com/xenova/transformers.js/blob/main/scripts/convert.py) to convert your PyTorch, TensorFlow, or JAX models to ONNX in a single command. Behind the scenes, it uses [🤗 Optimum](https://huggingface.co/docs/optimum) to perform conversion and quantization of your model.
```bash
python -m scripts.convert --quantize --model_id <model_name_or_path>
```
For example, convert and quantize [bert-base-uncased](https://huggingface.co/bert-base-uncased) using:
```bash
python -m scripts.convert --quantize --model_id bert-base-uncased
```
This will save the following files to `./models/`:
```
bert-base-uncased/
├── config.json
├── tokenizer.json
├── tokenizer_config.json
└── onnx/
├── model.onnx
└── model_quantized.onnx
```
For the full list of supported architectures, see the [Optimum documentation](https://huggingface.co/docs/optimum/main/en/exporters/onnx/overview).
## Supported tasks/models
Here is the list of all tasks and architectures currently supported by Transformers.js.
If you don't see your task/model listed here or it is not yet supported, feel free
to open up a feature request [here](https://github.com/xenova/transformers.js/issues/new/choose).
To find compatible models on the Hub, select the "transformers.js" library tag in the filter menu (or visit [this link](https://huggingface.co/models?library=transformers.js)).
You can refine your search by selecting the task you're interested in (e.g., [text-classification](https://huggingface.co/models?pipeline_tag=text-classification&library=transformers.js)).
### Tasks
#### Natural Language Processing
| Task | ID | Description | Supported? |
|--------------------------|----|-------------|------------|
| [Fill-Mask](https://huggingface.co/tasks/fill-mask) | `fill-mask` | Masking some of the words in a sentence and predicting which words should replace those masks. | ✅ [(docs)](https://huggingface.co/docs/transformers.js/api/pipelines#module_pipelines.FillMaskPipeline)<br>[(models)](https://huggingface.co/models?pipeline_tag=fill-mask&library=transformers.js) |
| [Question Answering](https://huggingface.co/tasks/question-answering) | `question-answering` | Retrieve the answer to a question from a given text. | ✅ [(docs)](https://huggingface.co/docs/transformers.js/api/pipelines#module_pipelines.QuestionAnsweringPipeline)<br>[(models)](https://huggingface.co/models?pipeline_tag=question-answering&library=transformers.js) |
| [Sentence Similarity](https://huggingface.co/tasks/sentence-similarity) | `sentence-similarity` | Determining how similar two texts are. | ✅ [(docs)](https://huggingface.co/docs/transformers.js/api/pipelines#module_pipelines.FeatureExtractionPipeline)<br>[(models)](https://huggingface.co/models?pipeline_tag=feature-extraction&library=transformers.js) |
| [Summarization](https://huggingface.co/tasks/summarization) | `summarization` | Producing a shorter version of a document while preserving its important information. | ✅ [(docs)](https://huggingface.co/docs/transformers.js/api/pipelines#module_pipelines.SummarizationPipeline)<br>[(models)](https://huggingface.co/models?pipeline_tag=summarization&library=transformers.js) |
| [Table Question Answering](https://huggingface.co/tasks/table-question-answering) | `table-question-answering` | Answering a question about information from a given table. | ❌ |
| [Text Classification](https://huggingface.co/tasks/text-classification) | `text-classification` or `sentiment-analysis` | Assigning a label or class to a given text. | ✅ [(docs)](https://huggingface.co/docs/transformers.js/api/pipelines#module_pipelines.TextClassificationPipeline)<br>[(models)](https://huggingface.co/models?pipeline_tag=text-classification&library=transformers.js) |
| [Text Generation](https://huggingface.co/tasks/text-generation#completion-generation-models) | `text-generation` | Producing new text by predicting the next word in a sequence. | ✅ [(docs)](https://huggingface.co/docs/transformers.js/api/pipelines#module_pipelines.TextGenerationPipeline)<br>[(models)](https://huggingface.co/models?pipeline_tag=text-generation&library=transformers.js) |
| [Text-to-text Generation](https://huggingface.co/tasks/text-generation#text-to-text-generation-models) | `text2text-generation` | Converting one text sequence into another text sequence. | ✅ [(docs)](https://huggingface.co/docs/transformers.js/api/pipelines#module_pipelines.Text2TextGenerationPipeline)<br>[(models)](https://huggingface.co/models?pipeline_tag=text2text-generation&library=transformers.js) |
| [Token Classification](https://huggingface.co/tasks/token-classification) | `token-classification` or `ner` | Assigning a label to each token in a text. | ✅ [(docs)](https://huggingface.co/docs/transformers.js/api/pipelines#module_pipelines.TokenClassificationPipeline)<br>[(models)](https://huggingface.co/models?pipeline_tag=token-classification&library=transformers.js) |
| [Translation](https://huggingface.co/tasks/translation) | `translation` | Converting text from one language to another. | ✅ [(docs)](https://huggingface.co/docs/transformers.js/api/pipelines#module_pipelines.TranslationPipeline)<br>[(models)](https://huggingface.co/models?pipeline_tag=translation&library=transformers.js) |
| [Zero-Shot Classification](https://huggingface.co/tasks/zero-shot-classification) | `zero-shot-classification` | Classifying text into classes that are unseen during training. | ✅ [(docs)](https://huggingface.co/docs/transformers.js/api/pipelines#module_pipelines.ZeroShotClassificationPipeline)<br>[(models)](https://huggingface.co/models?pipeline_tag=zero-shot-classification&library=transformers.js) |
| [Feature Extraction](https://huggingface.co/tasks/feature-extraction) | `feature-extraction` | Transforming raw data into numerical features that can be processed while preserving the information in the original dataset. | ✅ [(docs)](https://huggingface.co/docs/transformers.js/api/pipelines#module_pipelines.FeatureExtractionPipeline)<br>[(models)](https://huggingface.co/models?pipeline_tag=feature-extraction&library=transformers.js) |
#### Vision
| Task | ID | Description | Supported? |
|--------------------------|----|-------------|------------|
| [Depth Estimation](https://huggingface.co/tasks/depth-estimation) | `depth-estimation` | Predicting the depth of objects present in an image. | ✅ [(docs)](https://huggingface.co/docs/transformers.js/api/pipelines#module_pipelines.DepthEstimationPipeline)<br>[(models)](https://huggingface.co/models?pipeline_tag=depth-estimation&library=transformers.js) |
| [Image Classification](https://huggingface.co/tasks/image-classification) | `image-classification` | Assigning a label or class to an entire image. | ✅ [(docs)](https://huggingface.co/docs/transformers.js/api/pipelines#module_pipelines.ImageClassificationPipeline)<br>[(models)](https://huggingface.co/models?pipeline_tag=image-classification&library=transformers.js) |
| [Image Segmentation](https://huggingface.co/tasks/image-segmentation) | `image-segmentation` | Divides an image into segments where each pixel is mapped to an object. This task has multiple variants such as instance segmentation, panoptic segmentation and semantic segmentation. | ✅ [(docs)](https://huggingface.co/docs/transformers.js/api/pipelines#module_pipelines.ImageSegmentationPipeline)<br>[(models)](https://huggingface.co/models?pipeline_tag=image-segmentation&library=transformers.js) |
| [Image-to-Image](https://huggingface.co/tasks/image-to-image) | `image-to-image` | Transforming a source image to match the characteristics of a target image or a target image domain. | ✅ [(docs)](https://huggingface.co/docs/transformers.js/api/pipelines#module_pipelines.ImageToImagePipeline)<br>[(models)](https://huggingface.co/models?pipeline_tag=image-to-image&library=transformers.js) |
| [Mask Generation](https://huggingface.co/tasks/mask-generation) | `mask-generation` | Generate masks for the objects in an image. | ❌ |
| [Object Detection](https://huggingface.co/tasks/object-detection) | `object-detection` | Identify objects of certain defined classes within an image. | ✅ [(docs)](https://huggingface.co/docs/transformers.js/api/pipelines#module_pipelines.ObjectDetectionPipeline)<br>[(models)](https://huggingface.co/models?pipeline_tag=object-detection&library=transformers.js) |
| [Video Classification](https://huggingface.co/tasks/video-classification) | n/a | Assigning a label or class to an entire video. | ❌ |
| [Unconditional Image Generation](https://huggingface.co/tasks/unconditional-image-generation) | n/a | Generating images with no condition in any context (like a prompt text or another image). | ❌ |
| [Image Feature Extraction](https://huggingface.co/tasks/image-feature-extraction) | `image-feature-extraction` | Transforming raw data into numerical features that can be processed while preserving the information in the original image. | ✅ [(docs)](https://huggingface.co/docs/transformers.js/api/pipelines#module_pipelines.ImageFeatureExtractionPipeline)<br>[(models)](https://huggingface.co/models?pipeline_tag=image-feature-extraction&library=transformers.js) |
#### Audio
| Task | ID | Description | Supported? |
|--------------------------|----|-------------|------------|
| [Audio Classification](https://huggingface.co/tasks/audio-classification) | `audio-classification` | Assigning a label or class to a given audio. | ✅ [(docs)](https://huggingface.co/docs/transformers.js/api/pipelines#module_pipelines.AudioClassificationPipeline)<br>[(models)](https://huggingface.co/models?pipeline_tag=audio-classification&library=transformers.js) |
| [Audio-to-Audio](https://huggingface.co/tasks/audio-to-audio) | n/a | Generating audio from an input audio source. | ❌ |
| [Automatic Speech Recognition](https://huggingface.co/tasks/automatic-speech-recognition) | `automatic-speech-recognition` | Transcribing a given audio into text. | ✅ [(docs)](https://huggingface.co/docs/transformers.js/api/pipelines#module_pipelines.AutomaticSpeechRecognitionPipeline)<br>[(models)](https://huggingface.co/models?pipeline_tag=automatic-speech-recognition&library=transformers.js) |
| [Text-to-Speech](https://huggingface.co/tasks/text-to-speech) | `text-to-speech` or `text-to-audio` | Generating natural-sounding speech given text input. | ✅ [(docs)](https://huggingface.co/docs/transformers.js/api/pipelines#module_pipelines.TextToAudioPipeline)<br>[(models)](https://huggingface.co/models?pipeline_tag=text-to-audio&library=transformers.js) |
#### Tabular
| Task | ID | Description | Supported? |
|--------------------------|----|-------------|------------|
| [Tabular Classification](https://huggingface.co/tasks/tabular-classification) | n/a | Classifying a target category (a group) based on set of attributes. | ❌ |
| [Tabular Regression](https://huggingface.co/tasks/tabular-regression) | n/a | Predicting a numerical value given a set of attributes. | ❌ |
#### Multimodal
| Task | ID | Description | Supported? |
|--------------------------|----|-------------|------------|
| [Document Question Answering](https://huggingface.co/tasks/document-question-answering) | `document-question-answering` | Answering questions on document images. | ✅ [(docs)](https://huggingface.co/docs/transformers.js/api/pipelines#module_pipelines.DocumentQuestionAnsweringPipeline)<br>[(models)](https://huggingface.co/models?pipeline_tag=document-question-answering&library=transformers.js) |
| [Image-to-Text](https://huggingface.co/tasks/image-to-text) | `image-to-text` | Output text from a given image. | ✅ [(docs)](https://huggingface.co/docs/transformers.js/api/pipelines#module_pipelines.ImageToTextPipeline)<br>[(models)](https://huggingface.co/models?pipeline_tag=image-to-text&library=transformers.js) |
| [Text-to-Image](https://huggingface.co/tasks/text-to-image) | `text-to-image` | Generates images from input text. | ❌ |
| [Visual Question Answering](https://huggingface.co/tasks/visual-question-answering) | `visual-question-answering` | Answering open-ended questions based on an image. | ❌ |
| [Zero-Shot Audio Classification](https://huggingface.co/learn/audio-course/chapter4/classification_models#zero-shot-audio-classification) | `zero-shot-audio-classification` | Classifying audios into classes that are unseen during training. | ✅ [(docs)](https://huggingface.co/docs/transformers.js/api/pipelines#module_pipelines.ZeroShotAudioClassificationPipeline)<br>[(models)](https://huggingface.co/models?other=zero-shot-audio-classification&library=transformers.js) |
| [Zero-Shot Image Classification](https://huggingface.co/tasks/zero-shot-image-classification) | `zero-shot-image-classification` | Classifying images into classes that are unseen during training. | ✅ [(docs)](https://huggingface.co/docs/transformers.js/api/pipelines#module_pipelines.ZeroShotImageClassificationPipeline)<br>[(models)](https://huggingface.co/models?pipeline_tag=zero-shot-image-classification&library=transformers.js) |
| [Zero-Shot Object Detection](https://huggingface.co/tasks/zero-shot-object-detection) | `zero-shot-object-detection` | Identify objects of classes that are unseen during training. | ✅ [(docs)](https://huggingface.co/docs/transformers.js/api/pipelines#module_pipelines.ZeroShotObjectDetectionPipeline)<br>[(models)](https://huggingface.co/models?other=zero-shot-object-detection&library=transformers.js) |
#### Reinforcement Learning
| Task | ID | Description | Supported? |
|--------------------------|----|-------------|------------|
| [Reinforcement Learning](https://huggingface.co/tasks/reinforcement-learning) | n/a | Learning from actions by interacting with an environment through trial and error and receiving rewards (negative or positive) as feedback. | ❌ |
### Models
1. **[ALBERT](https://huggingface.co/docs/transformers/model_doc/albert)** (from Google Research and the Toyota Technological Institute at Chicago) released with the paper [ALBERT: A Lite BERT for Self-supervised Learning of Language Representations](https://arxiv.org/abs/1909.11942), by Zhenzhong Lan, Mingda Chen, Sebastian Goodman, Kevin Gimpel, Piyush Sharma, Radu Soricut.
1. **[Audio Spectrogram Transformer](https://huggingface.co/docs/transformers/model_doc/audio-spectrogram-transformer)** (from MIT) released with the paper [AST: Audio Spectrogram Transformer](https://arxiv.org/abs/2104.01778) by Yuan Gong, Yu-An Chung, James Glass.
1. **[BART](https://huggingface.co/docs/transformers/model_doc/bart)** (from Facebook) released with the paper [BART: Denoising Sequence-to-Sequence Pre-training for Natural Language Generation, Translation, and Comprehension](https://arxiv.org/abs/1910.13461) by Mike Lewis, Yinhan Liu, Naman Goyal, Marjan Ghazvininejad, Abdelrahman Mohamed, Omer Levy, Ves Stoyanov and Luke Zettlemoyer.
1. **[BEiT](https://huggingface.co/docs/transformers/model_doc/beit)** (from Microsoft) released with the paper [BEiT: BERT Pre-Training of Image Transformers](https://arxiv.org/abs/2106.08254) by Hangbo Bao, Li Dong, Furu Wei.
1. **[BERT](https://huggingface.co/docs/transformers/model_doc/bert)** (from Google) released with the paper [BERT: Pre-training of Deep Bidirectional Transformers for Language Understanding](https://arxiv.org/abs/1810.04805) by Jacob Devlin, Ming-Wei Chang, Kenton Lee and Kristina Toutanova.
1. **[Blenderbot](https://huggingface.co/docs/transformers/model_doc/blenderbot)** (from Facebook) released with the paper [Recipes for building an open-domain chatbot](https://arxiv.org/abs/2004.13637) by Stephen Roller, Emily Dinan, Naman Goyal, Da Ju, Mary Williamson, Yinhan Liu, Jing Xu, Myle Ott, Kurt Shuster, Eric M. Smith, Y-Lan Boureau, Jason Weston.
1. **[BlenderbotSmall](https://huggingface.co/docs/transformers/model_doc/blenderbot-small)** (from Facebook) released with the paper [Recipes for building an open-domain chatbot](https://arxiv.org/abs/2004.13637) by Stephen Roller, Emily Dinan, Naman Goyal, Da Ju, Mary Williamson, Yinhan Liu, Jing Xu, Myle Ott, Kurt Shuster, Eric M. Smith, Y-Lan Boureau, Jason Weston.
1. **[BLOOM](https://huggingface.co/docs/transformers/model_doc/bloom)** (from BigScience workshop) released by the [BigScience Workshop](https://bigscience.huggingface.co/).
1. **[CamemBERT](https://huggingface.co/docs/transformers/model_doc/camembert)** (from Inria/Facebook/Sorbonne) released with the paper [CamemBERT: a Tasty French Language Model](https://arxiv.org/abs/1911.03894) by Louis Martin*, Benjamin Muller*, Pedro Javier Ortiz Suárez*, Yoann Dupont, Laurent Romary, Éric Villemonte de la Clergerie, Djamé Seddah and Benoît Sagot.
1. **[Chinese-CLIP](https://huggingface.co/docs/transformers/model_doc/chinese_clip)** (from OFA-Sys) released with the paper [Chinese CLIP: Contrastive Vision-Language Pretraining in Chinese](https://arxiv.org/abs/2211.01335) by An Yang, Junshu Pan, Junyang Lin, Rui Men, Yichang Zhang, Jingren Zhou, Chang Zhou.
1. **[CLAP](https://huggingface.co/docs/transformers/model_doc/clap)** (from LAION-AI) released with the paper [Large-scale Contrastive Language-Audio Pretraining with Feature Fusion and Keyword-to-Caption Augmentation](https://arxiv.org/abs/2211.06687) by Yusong Wu, Ke Chen, Tianyu Zhang, Yuchen Hui, Taylor Berg-Kirkpatrick, Shlomo Dubnov.
1. **[CLIP](https://huggingface.co/docs/transformers/model_doc/clip)** (from OpenAI) released with the paper [Learning Transferable Visual Models From Natural Language Supervision](https://arxiv.org/abs/2103.00020) by Alec Radford, Jong Wook Kim, Chris Hallacy, Aditya Ramesh, Gabriel Goh, Sandhini Agarwal, Girish Sastry, Amanda Askell, Pamela Mishkin, Jack Clark, Gretchen Krueger, Ilya Sutskever.
1. **[CLIPSeg](https://huggingface.co/docs/transformers/model_doc/clipseg)** (from University of Göttingen) released with the paper [Image Segmentation Using Text and Image Prompts](https://arxiv.org/abs/2112.10003) by Timo Lüddecke and Alexander Ecker.
1. **[CodeGen](https://huggingface.co/docs/transformers/model_doc/codegen)** (from Salesforce) released with the paper [A Conversational Paradigm for Program Synthesis](https://arxiv.org/abs/2203.13474) by Erik Nijkamp, Bo Pang, Hiroaki Hayashi, Lifu Tu, Huan Wang, Yingbo Zhou, Silvio Savarese, Caiming Xiong.
1. **[CodeLlama](https://huggingface.co/docs/transformers/model_doc/llama_code)** (from MetaAI) released with the paper [Code Llama: Open Foundation Models for Code](https://ai.meta.com/research/publications/code-llama-open-foundation-models-for-code/) by Baptiste Rozière, Jonas Gehring, Fabian Gloeckle, Sten Sootla, Itai Gat, Xiaoqing Ellen Tan, Yossi Adi, Jingyu Liu, Tal Remez, Jérémy Rapin, Artyom Kozhevnikov, Ivan Evtimov, Joanna Bitton, Manish Bhatt, Cristian Canton Ferrer, Aaron Grattafiori, Wenhan Xiong, Alexandre Défossez, Jade Copet, Faisal Azhar, Hugo Touvron, Louis Martin, Nicolas Usunier, Thomas Scialom, Gabriel Synnaeve.
1. **[ConvBERT](https://huggingface.co/docs/transformers/model_doc/convbert)** (from YituTech) released with the paper [ConvBERT: Improving BERT with Span-based Dynamic Convolution](https://arxiv.org/abs/2008.02496) by Zihang Jiang, Weihao Yu, Daquan Zhou, Yunpeng Chen, Jiashi Feng, Shuicheng Yan.
1. **[ConvNeXT](https://huggingface.co/docs/transformers/model_doc/convnext)** (from Facebook AI) released with the paper [A ConvNet for the 2020s](https://arxiv.org/abs/2201.03545) by Zhuang Liu, Hanzi Mao, Chao-Yuan Wu, Christoph Feichtenhofer, Trevor Darrell, Saining Xie.
1. **[ConvNeXTV2](https://huggingface.co/docs/transformers/model_doc/convnextv2)** (from Facebook AI) released with the paper [ConvNeXt V2: Co-designing and Scaling ConvNets with Masked Autoencoders](https://arxiv.org/abs/2301.00808) by Sanghyun Woo, Shoubhik Debnath, Ronghang Hu, Xinlei Chen, Zhuang Liu, In So Kweon, Saining Xie.
1. **[DeBERTa](https://huggingface.co/docs/transformers/model_doc/deberta)** (from Microsoft) released with the paper [DeBERTa: Decoding-enhanced BERT with Disentangled Attention](https://arxiv.org/abs/2006.03654) by Pengcheng He, Xiaodong Liu, Jianfeng Gao, Weizhu Chen.
1. **[DeBERTa-v2](https://huggingface.co/docs/transformers/model_doc/deberta-v2)** (from Microsoft) released with the paper [DeBERTa: Decoding-enhanced BERT with Disentangled Attention](https://arxiv.org/abs/2006.03654) by Pengcheng He, Xiaodong Liu, Jianfeng Gao, Weizhu Chen.
1. **[DeiT](https://huggingface.co/docs/transformers/model_doc/deit)** (from Facebook) released with the paper [Training data-efficient image transformers & distillation through attention](https://arxiv.org/abs/2012.12877) by Hugo Touvron, Matthieu Cord, Matthijs Douze, Francisco Massa, Alexandre Sablayrolles, Hervé Jégou.
1. **[Depth Anything](https://huggingface.co/docs/transformers/main/model_doc/depth_anything)** (from University of Hong Kong and TikTok) released with the paper [Depth Anything: Unleashing the Power of Large-Scale Unlabeled Data](https://arxiv.org/abs/2401.10891) by Lihe Yang, Bingyi Kang, Zilong Huang, Xiaogang Xu, Jiashi Feng, Hengshuang Zhao.
1. **[DETR](https://huggingface.co/docs/transformers/model_doc/detr)** (from Facebook) released with the paper [End-to-End Object Detection with Transformers](https://arxiv.org/abs/2005.12872) by Nicolas Carion, Francisco Massa, Gabriel Synnaeve, Nicolas Usunier, Alexander Kirillov, Sergey Zagoruyko.
1. **[DINOv2](https://huggingface.co/docs/transformers/model_doc/dinov2)** (from Meta AI) released with the paper [DINOv2: Learning Robust Visual Features without Supervision](https://arxiv.org/abs/2304.07193) by Maxime Oquab, Timothée Darcet, Théo Moutakanni, Huy Vo, Marc Szafraniec, Vasil Khalidov, Pierre Fernandez, Daniel Haziza, Francisco Massa, Alaaeldin El-Nouby, Mahmoud Assran, Nicolas Ballas, Wojciech Galuba, Russell Howes, Po-Yao Huang, Shang-Wen Li, Ishan Misra, Michael Rabbat, Vasu Sharma, Gabriel Synnaeve, Hu Xu, Hervé Jegou, Julien Mairal, Patrick Labatut, Armand Joulin, Piotr Bojanowski.
1. **[DistilBERT](https://huggingface.co/docs/transformers/model_doc/distilbert)** (from HuggingFace), released together with the paper [DistilBERT, a distilled version of BERT: smaller, faster, cheaper and lighter](https://arxiv.org/abs/1910.01108) by Victor Sanh, Lysandre Debut and Thomas Wolf. The same method has been applied to compress GPT2 into [DistilGPT2](https://github.com/huggingface/transformers/tree/main/examples/research_projects/distillation), RoBERTa into [DistilRoBERTa](https://github.com/huggingface/transformers/tree/main/examples/research_projects/distillation), Multilingual BERT into [DistilmBERT](https://github.com/huggingface/transformers/tree/main/examples/research_projects/distillation) and a German version of DistilBERT.
1. **[DiT](https://huggingface.co/docs/transformers/model_doc/dit)** (from Microsoft Research) released with the paper [DiT: Self-supervised Pre-training for Document Image Transformer](https://arxiv.org/abs/2203.02378) by Junlong Li, Yiheng Xu, Tengchao Lv, Lei Cui, Cha Zhang, Furu Wei.
1. **[Donut](https://huggingface.co/docs/transformers/model_doc/donut)** (from NAVER), released together with the paper [OCR-free Document Understanding Transformer](https://arxiv.org/abs/2111.15664) by Geewook Kim, Teakgyu Hong, Moonbin Yim, Jeongyeon Nam, Jinyoung Park, Jinyeong Yim, Wonseok Hwang, Sangdoo Yun, Dongyoon Han, Seunghyun Park.
1. **[DPT](https://huggingface.co/docs/transformers/master/model_doc/dpt)** (from Intel Labs) released with the paper [Vision Transformers for Dense Prediction](https://arxiv.org/abs/2103.13413) by René Ranftl, Alexey Bochkovskiy, Vladlen Koltun.
1. **[EfficientNet](https://huggingface.co/docs/transformers/model_doc/efficientnet)** (from Google Brain) released with the paper [EfficientNet: Rethinking Model Scaling for Convolutional Neural Networks](https://arxiv.org/abs/1905.11946) by Mingxing Tan, Quoc V. Le.
1. **[ELECTRA](https://huggingface.co/docs/transformers/model_doc/electra)** (from Google Research/Stanford University) released with the paper [ELECTRA: Pre-training text encoders as discriminators rather than generators](https://arxiv.org/abs/2003.10555) by Kevin Clark, Minh-Thang Luong, Quoc V. Le, Christopher D. Manning.
1. **[ESM](https://huggingface.co/docs/transformers/model_doc/esm)** (from Meta AI) are transformer protein language models. **ESM-1b** was released with the paper [Biological structure and function emerge from scaling unsupervised learning to 250 million protein sequences](https://www.pnas.org/content/118/15/e2016239118) by Alexander Rives, Joshua Meier, Tom Sercu, Siddharth Goyal, Zeming Lin, Jason Liu, Demi Guo, Myle Ott, C. Lawrence Zitnick, Jerry Ma, and Rob Fergus. **ESM-1v** was released with the paper [Language models enable zero-shot prediction of the effects of mutations on protein function](https://doi.org/10.1101/2021.07.09.450648) by Joshua Meier, Roshan Rao, Robert Verkuil, Jason Liu, Tom Sercu and Alexander Rives. **ESM-2 and ESMFold** were released with the paper [Language models of protein sequences at the scale of evolution enable accurate structure prediction](https://doi.org/10.1101/2022.07.20.500902) by Zeming Lin, Halil Akin, Roshan Rao, Brian Hie, Zhongkai Zhu, Wenting Lu, Allan dos Santos Costa, Maryam Fazel-Zarandi, Tom Sercu, Sal Candido, Alexander Rives.
1. **[Falcon](https://huggingface.co/docs/transformers/model_doc/falcon)** (from Technology Innovation Institute) by Almazrouei, Ebtesam and Alobeidli, Hamza and Alshamsi, Abdulaziz and Cappelli, Alessandro and Cojocaru, Ruxandra and Debbah, Merouane and Goffinet, Etienne and Heslow, Daniel and Launay, Julien and Malartic, Quentin and Noune, Badreddine and Pannier, Baptiste and Penedo, Guilherme.
1. **FastViT** (from Apple) released with the paper [FastViT: A Fast Hybrid Vision Transformer using Structural Reparameterization](https://arxiv.org/abs/2303.14189) by Pavan Kumar Anasosalu Vasu, James Gabriel, Jeff Zhu, Oncel Tuzel and Anurag Ranjan.
1. **[FLAN-T5](https://huggingface.co/docs/transformers/model_doc/flan-t5)** (from Google AI) released in the repository [google-research/t5x](https://github.com/google-research/t5x/blob/main/docs/models.md#flan-t5-checkpoints) by Hyung Won Chung, Le Hou, Shayne Longpre, Barret Zoph, Yi Tay, William Fedus, Eric Li, Xuezhi Wang, Mostafa Dehghani, Siddhartha Brahma, Albert Webson, Shixiang Shane Gu, Zhuyun Dai, Mirac Suzgun, Xinyun Chen, Aakanksha Chowdhery, Sharan Narang, Gaurav Mishra, Adams Yu, Vincent Zhao, Yanping Huang, Andrew Dai, Hongkun Yu, Slav Petrov, Ed H. Chi, Jeff Dean, Jacob Devlin, Adam Roberts, Denny Zhou, Quoc V. Le, and Jason Wei
1. **[GLPN](https://huggingface.co/docs/transformers/model_doc/glpn)** (from KAIST) released with the paper [Global-Local Path Networks for Monocular Depth Estimation with Vertical CutDepth](https://arxiv.org/abs/2201.07436) by Doyeon Kim, Woonghyun Ga, Pyungwhan Ahn, Donggyu Joo, Sehwan Chun, Junmo Kim.
1. **[GPT Neo](https://huggingface.co/docs/transformers/model_doc/gpt_neo)** (from EleutherAI) released in the repository [EleutherAI/gpt-neo](https://github.com/EleutherAI/gpt-neo) by Sid Black, Stella Biderman, Leo Gao, Phil Wang and Connor Leahy.
1. **[GPT NeoX](https://huggingface.co/docs/transformers/model_doc/gpt_neox)** (from EleutherAI) released with the paper [GPT-NeoX-20B: An Open-Source Autoregressive Language Model](https://arxiv.org/abs/2204.06745) by Sid Black, Stella Biderman, Eric Hallahan, Quentin Anthony, Leo Gao, Laurence Golding, Horace He, Connor Leahy, Kyle McDonell, Jason Phang, Michael Pieler, USVSN Sai Prashanth, Shivanshu Purohit, Laria Reynolds, Jonathan Tow, Ben Wang, Samuel Weinbach
1. **[GPT-2](https://huggingface.co/docs/transformers/model_doc/gpt2)** (from OpenAI) released with the paper [Language Models are Unsupervised Multitask Learners](https://blog.openai.com/better-language-models/) by Alec Radford*, Jeffrey Wu*, Rewon Child, David Luan, Dario Amodei** and Ilya Sutskever**.
1. **[GPT-J](https://huggingface.co/docs/transformers/model_doc/gptj)** (from EleutherAI) released in the repository [kingoflolz/mesh-transformer-jax](https://github.com/kingoflolz/mesh-transformer-jax/) by Ben Wang and Aran Komatsuzaki.
1. **[GPTBigCode](https://huggingface.co/docs/transformers/model_doc/gpt_bigcode)** (from BigCode) released with the paper [SantaCoder: don't reach for the stars!](https://arxiv.org/abs/2301.03988) by Loubna Ben Allal, Raymond Li, Denis Kocetkov, Chenghao Mou, Christopher Akiki, Carlos Munoz Ferrandis, Niklas Muennighoff, Mayank Mishra, Alex Gu, Manan Dey, Logesh Kumar Umapathi, Carolyn Jane Anderson, Yangtian Zi, Joel Lamy Poirier, Hailey Schoelkopf, Sergey Troshin, Dmitry Abulkhanov, Manuel Romero, Michael Lappert, Francesco De Toni, Bernardo García del Río, Qian Liu, Shamik Bose, Urvashi Bhattacharyya, Terry Yue Zhuo, Ian Yu, Paulo Villegas, Marco Zocca, Sourab Mangrulkar, David Lansky, Huu Nguyen, Danish Contractor, Luis Villa, Jia Li, Dzmitry Bahdanau, Yacine Jernite, Sean Hughes, Daniel Fried, Arjun Guha, Harm de Vries, Leandro von Werra.
1. **[HerBERT](https://huggingface.co/docs/transformers/model_doc/herbert)** (from Allegro.pl, AGH University of Science and Technology) released with the paper [KLEJ: Comprehensive Benchmark for Polish Language Understanding](https://www.aclweb.org/anthology/2020.acl-main.111.pdf) by Piotr Rybak, Robert Mroczkowski, Janusz Tracz, Ireneusz Gawlik.
1. **[Hubert](https://huggingface.co/docs/transformers/model_doc/hubert)** (from Facebook) released with the paper [HuBERT: Self-Supervised Speech Representation Learning by Masked Prediction of Hidden Units](https://arxiv.org/abs/2106.07447) by Wei-Ning Hsu, Benjamin Bolte, Yao-Hung Hubert Tsai, Kushal Lakhotia, Ruslan Salakhutdinov, Abdelrahman Mohamed.
1. **[LongT5](https://huggingface.co/docs/transformers/model_doc/longt5)** (from Google AI) released with the paper [LongT5: Efficient Text-To-Text Transformer for Long Sequences](https://arxiv.org/abs/2112.07916) by Mandy Guo, Joshua Ainslie, David Uthus, Santiago Ontanon, Jianmo Ni, Yun-Hsuan Sung, Yinfei Yang.
1. **[LLaMA](https://huggingface.co/docs/transformers/model_doc/llama)** (from The FAIR team of Meta AI) released with the paper [LLaMA: Open and Efficient Foundation Language Models](https://arxiv.org/abs/2302.13971) by Hugo Touvron, Thibaut Lavril, Gautier Izacard, Xavier Martinet, Marie-Anne Lachaux, Timothée Lacroix, Baptiste Rozière, Naman Goyal, Eric Hambro, Faisal Azhar, Aurelien Rodriguez, Armand Joulin, Edouard Grave, Guillaume Lample.
1. **[Llama2](https://huggingface.co/docs/transformers/model_doc/llama2)** (from The FAIR team of Meta AI) released with the paper [Llama2: Open Foundation and Fine-Tuned Chat Models](https://ai.meta.com/research/publications/llama-2-open-foundation-and-fine-tuned-chat-models/XXX) by Hugo Touvron, Louis Martin, Kevin Stone, Peter Albert, Amjad Almahairi, Yasmine Babaei, Nikolay Bashlykov, Soumya Batra, Prajjwal Bhargava, Shruti Bhosale, Dan Bikel, Lukas Blecher, Cristian Canton Ferrer, Moya Chen, Guillem Cucurull, David Esiobu, Jude Fernandes, Jeremy Fu, Wenyin Fu, Brian Fuller, Cynthia Gao, Vedanuj Goswami, Naman Goyal, Anthony Hartshorn, Saghar Hosseini, Rui Hou, Hakan Inan, Marcin Kardas, Viktor Kerkez Madian Khabsa, Isabel Kloumann, Artem Korenev, Punit Singh Koura, Marie-Anne Lachaux, Thibaut Lavril, Jenya Lee, Diana Liskovich, Yinghai Lu, Yuning Mao, Xavier Martinet, Todor Mihaylov, Pushka rMishra, Igor Molybog, Yixin Nie, Andrew Poulton, Jeremy Reizenstein, Rashi Rungta, Kalyan Saladi, Alan Schelten, Ruan Silva, Eric Michael Smith, Ranjan Subramanian, Xiaoqing EllenTan, Binh Tang, Ross Taylor, Adina Williams, Jian Xiang Kuan, Puxin Xu, Zheng Yan, Iliyan Zarov, Yuchen Zhang, Angela Fan, Melanie Kambadur, Sharan Narang, Aurelien Rodriguez, Robert Stojnic, Sergey Edunov, Thomas Scialom.
1. **[M2M100](https://huggingface.co/docs/transformers/model_doc/m2m_100)** (from Facebook) released with the paper [Beyond English-Centric Multilingual Machine Translation](https://arxiv.org/abs/2010.11125) by Angela Fan, Shruti Bhosale, Holger Schwenk, Zhiyi Ma, Ahmed El-Kishky, Siddharth Goyal, Mandeep Baines, Onur Celebi, Guillaume Wenzek, Vishrav Chaudhary, Naman Goyal, Tom Birch, Vitaliy Liptchinsky, Sergey Edunov, Edouard Grave, Michael Auli, Armand Joulin.
1. **[MarianMT](https://huggingface.co/docs/transformers/model_doc/marian)** Machine translation models trained using [OPUS](http://opus.nlpl.eu/) data by Jörg Tiedemann. The [Marian Framework](https://marian-nmt.github.io/) is being developed by the Microsoft Translator Team.
1. **[mBART](https://huggingface.co/docs/transformers/model_doc/mbart)** (from Facebook) released with the paper [Multilingual Denoising Pre-training for Neural Machine Translation](https://arxiv.org/abs/2001.08210) by Yinhan Liu, Jiatao Gu, Naman Goyal, Xian Li, Sergey Edunov, Marjan Ghazvininejad, Mike Lewis, Luke Zettlemoyer.
1. **[mBART-50](https://huggingface.co/docs/transformers/model_doc/mbart)** (from Facebook) released with the paper [Multilingual Translation with Extensible Multilingual Pretraining and Finetuning](https://arxiv.org/abs/2008.00401) by Yuqing Tang, Chau Tran, Xian Li, Peng-Jen Chen, Naman Goyal, Vishrav Chaudhary, Jiatao Gu, Angela Fan.
1. **[Mistral](https://huggingface.co/docs/transformers/model_doc/mistral)** (from Mistral AI) by The [Mistral AI](https://mistral.ai) team: Albert Jiang, Alexandre Sablayrolles, Arthur Mensch, Chris Bamford, Devendra Singh Chaplot, Diego de las Casas, Florian Bressand, Gianna Lengyel, Guillaume Lample, Lélio Renard Lavaud, Lucile Saulnier, Marie-Anne Lachaux, Pierre Stock, Teven Le Scao, Thibaut Lavril, Thomas Wang, Timothée Lacroix, William El Sayed.
1. **[MMS](https://huggingface.co/docs/transformers/model_doc/mms)** (from Facebook) released with the paper [Scaling Speech Technology to 1,000+ Languages](https://arxiv.org/abs/2305.13516) by Vineel Pratap, Andros Tjandra, Bowen Shi, Paden Tomasello, Arun Babu, Sayani Kundu, Ali Elkahky, Zhaoheng Ni, Apoorv Vyas, Maryam Fazel-Zarandi, Alexei Baevski, Yossi Adi, Xiaohui Zhang, Wei-Ning Hsu, Alexis Conneau, Michael Auli.
1. **[MobileBERT](https://huggingface.co/docs/transformers/model_doc/mobilebert)** (from CMU/Google Brain) released with the paper [MobileBERT: a Compact Task-Agnostic BERT for Resource-Limited Devices](https://arxiv.org/abs/2004.02984) by Zhiqing Sun, Hongkun Yu, Xiaodan Song, Renjie Liu, Yiming Yang, and Denny Zhou.
1. **[MobileViT](https://huggingface.co/docs/transformers/model_doc/mobilevit)** (from Apple) released with the paper [MobileViT: Light-weight, General-purpose, and Mobile-friendly Vision Transformer](https://arxiv.org/abs/2110.02178) by Sachin Mehta and Mohammad Rastegari.
1. **[MobileViTV2](https://huggingface.co/docs/transformers/model_doc/mobilevitv2)** (from Apple) released with the paper [Separable Self-attention for Mobile Vision Transformers](https://arxiv.org/abs/2206.02680) by Sachin Mehta and Mohammad Rastegari.
1. **[MPNet](https://huggingface.co/docs/transformers/model_doc/mpnet)** (from Microsoft Research) released with the paper [MPNet: Masked and Permuted Pre-training for Language Understanding](https://arxiv.org/abs/2004.09297) by Kaitao Song, Xu Tan, Tao Qin, Jianfeng Lu, Tie-Yan Liu.
1. **[MPT](https://huggingface.co/docs/transformers/model_doc/mpt)** (from MosaiML) released with the repository [llm-foundry](https://github.com/mosaicml/llm-foundry/) by the MosaicML NLP Team.
1. **[MT5](https://huggingface.co/docs/transformers/model_doc/mt5)** (from Google AI) released with the paper [mT5: A massively multilingual pre-trained text-to-text transformer](https://arxiv.org/abs/2010.11934) by Linting Xue, Noah Constant, Adam Roberts, Mihir Kale, Rami Al-Rfou, Aditya Siddhant, Aditya Barua, Colin Raffel.
1. **[NLLB](https://huggingface.co/docs/transformers/model_doc/nllb)** (from Meta) released with the paper [No Language Left Behind: Scaling Human-Centered Machine Translation](https://arxiv.org/abs/2207.04672) by the NLLB team.
1. **[Nougat](https://huggingface.co/docs/transformers/model_doc/nougat)** (from Meta AI) released with the paper [Nougat: Neural Optical Understanding for Academic Documents](https://arxiv.org/abs/2308.13418) by Lukas Blecher, Guillem Cucurull, Thomas Scialom, Robert Stojnic.
1. **[OPT](https://huggingface.co/docs/transformers/master/model_doc/opt)** (from Meta AI) released with the paper [OPT: Open Pre-trained Transformer Language Models](https://arxiv.org/abs/2205.01068) by Susan Zhang, Stephen Roller, Naman Goyal, Mikel Artetxe, Moya Chen, Shuohui Chen et al.
1. **[OWL-ViT](https://huggingface.co/docs/transformers/model_doc/owlvit)** (from Google AI) released with the paper [Simple Open-Vocabulary Object Detection with Vision Transformers](https://arxiv.org/abs/2205.06230) by Matthias Minderer, Alexey Gritsenko, Austin Stone, Maxim Neumann, Dirk Weissenborn, Alexey Dosovitskiy, Aravindh Mahendran, Anurag Arnab, Mostafa Dehghani, Zhuoran Shen, Xiao Wang, Xiaohua Zhai, Thomas Kipf, and Neil Houlsby.
1. **[OWLv2](https://huggingface.co/docs/transformers/model_doc/owlv2)** (from Google AI) released with the paper [Scaling Open-Vocabulary Object Detection](https://arxiv.org/abs/2306.09683) by Matthias Minderer, Alexey Gritsenko, Neil Houlsby.
1. **[Phi](https://huggingface.co/docs/transformers/main/model_doc/phi)** (from Microsoft) released with the papers - [Textbooks Are All You Need](https://arxiv.org/abs/2306.11644) by Suriya Gunasekar, Yi Zhang, Jyoti Aneja, Caio César Teodoro Mendes, Allie Del Giorno, Sivakanth Gopi, Mojan Javaheripi, Piero Kauffmann, Gustavo de Rosa, Olli Saarikivi, Adil Salim, Shital Shah, Harkirat Singh Behl, Xin Wang, Sébastien Bubeck, Ronen Eldan, Adam Tauman Kalai, Yin Tat Lee and Yuanzhi Li, [Textbooks Are All You Need II: phi-1.5 technical report](https://arxiv.org/abs/2309.05463) by Yuanzhi Li, Sébastien Bubeck, Ronen Eldan, Allie Del Giorno, Suriya Gunasekar and Yin Tat Lee.
1. **[Qwen2](https://huggingface.co/docs/transformers/model_doc/qwen2)** (from the Qwen team, Alibaba Group) released with the paper [Qwen Technical Report](https://arxiv.org/abs/2309.16609) by Jinze Bai, Shuai Bai, Yunfei Chu, Zeyu Cui, Kai Dang, Xiaodong Deng, Yang Fan, Wenbin Ge, Yu Han, Fei Huang, Binyuan Hui, Luo Ji, Mei Li, Junyang Lin, Runji Lin, Dayiheng Liu, Gao Liu, Chengqiang Lu, Keming Lu, Jianxin Ma, Rui Men, Xingzhang Ren, Xuancheng Ren, Chuanqi Tan, Sinan Tan, Jianhong Tu, Peng Wang, Shijie Wang, Wei Wang, Shengguang Wu, Benfeng Xu, Jin Xu, An Yang, Hao Yang, Jian Yang, Shusheng Yang, Yang Yao, Bowen Yu, Hongyi Yuan, Zheng Yuan, Jianwei Zhang, Xingxuan Zhang, Yichang Zhang, Zhenru Zhang, Chang Zhou, Jingren Zhou, Xiaohuan Zhou and Tianhang Zhu.
1. **[ResNet](https://huggingface.co/docs/transformers/model_doc/resnet)** (from Microsoft Research) released with the paper [Deep Residual Learning for Image Recognition](https://arxiv.org/abs/1512.03385) by Kaiming He, Xiangyu Zhang, Shaoqing Ren, Jian Sun.
1. **[RoBERTa](https://huggingface.co/docs/transformers/model_doc/roberta)** (from Facebook), released together with the paper [RoBERTa: A Robustly Optimized BERT Pretraining Approach](https://arxiv.org/abs/1907.11692) by Yinhan Liu, Myle Ott, Naman Goyal, Jingfei Du, Mandar Joshi, Danqi Chen, Omer Levy, Mike Lewis, Luke Zettlemoyer, Veselin Stoyanov.
1. **[RoFormer](https://huggingface.co/docs/transformers/model_doc/roformer)** (from ZhuiyiTechnology), released together with the paper [RoFormer: Enhanced Transformer with Rotary Position Embedding](https://arxiv.org/abs/2104.09864) by Jianlin Su and Yu Lu and Shengfeng Pan and Bo Wen and Yunfeng Liu.
1. **[SegFormer](https://huggingface.co/docs/transformers/model_doc/segformer)** (from NVIDIA) released with the paper [SegFormer: Simple and Efficient Design for Semantic Segmentation with Transformers](https://arxiv.org/abs/2105.15203) by Enze Xie, Wenhai Wang, Zhiding Yu, Anima Anandkumar, Jose M. Alvarez, Ping Luo.
1. **[Segment Anything](https://huggingface.co/docs/transformers/model_doc/sam)** (from Meta AI) released with the paper [Segment Anything](https://arxiv.org/pdf/2304.02643v1.pdf) by Alexander Kirillov, Eric Mintun, Nikhila Ravi, Hanzi Mao, Chloe Rolland, Laura Gustafson, Tete Xiao, Spencer Whitehead, Alex Berg, Wan-Yen Lo, Piotr Dollar, Ross Girshick.
1. **[SigLIP](https://huggingface.co/docs/transformers/main/model_doc/siglip)** (from Google AI) released with the paper [Sigmoid Loss for Language Image Pre-Training](https://arxiv.org/abs/2303.15343) by Xiaohua Zhai, Basil Mustafa, Alexander Kolesnikov, Lucas Beyer.
1. **[SpeechT5](https://huggingface.co/docs/transformers/model_doc/speecht5)** (from Microsoft Research) released with the paper [SpeechT5: Unified-Modal Encoder-Decoder Pre-Training for Spoken Language Processing](https://arxiv.org/abs/2110.07205) by Junyi Ao, Rui Wang, Long Zhou, Chengyi Wang, Shuo Ren, Yu Wu, Shujie Liu, Tom Ko, Qing Li, Yu Zhang, Zhihua Wei, Yao Qian, Jinyu Li, Furu Wei.
1. **[SqueezeBERT](https://huggingface.co/docs/transformers/model_doc/squeezebert)** (from Berkeley) released with the paper [SqueezeBERT: What can computer vision teach NLP about efficient neural networks?](https://arxiv.org/abs/2006.11316) by Forrest N. Iandola, Albert E. Shaw, Ravi Krishna, and Kurt W. Keutzer.
1. **[StableLm](https://huggingface.co/docs/transformers/model_doc/stablelm)** (from Stability AI) released with the paper [StableLM 3B 4E1T (Technical Report)](https://stability.wandb.io/stability-llm/stable-lm/reports/StableLM-3B-4E1T--VmlldzoyMjU4?accessToken=u3zujipenkx5g7rtcj9qojjgxpconyjktjkli2po09nffrffdhhchq045vp0wyfo) by Jonathan Tow, Marco Bellagente, Dakota Mahan, Carlos Riquelme Ruiz, Duy Phung, Maksym Zhuravinskyi, Nathan Cooper, Nikhil Pinnaparaju, Reshinth Adithyan, and James Baicoianu.
1. **[Starcoder2](https://huggingface.co/docs/transformers/main/model_doc/starcoder2)** (from BigCode team) released with the paper [StarCoder 2 and The Stack v2: The Next Generation](https://arxiv.org/abs/2402.19173) by Anton Lozhkov, Raymond Li, Loubna Ben Allal, Federico Cassano, Joel Lamy-Poirier, Nouamane Tazi, Ao Tang, Dmytro Pykhtar, Jiawei Liu, Yuxiang Wei, Tianyang Liu, Max Tian, Denis Kocetkov, Arthur Zucker, Younes Belkada, Zijian Wang, Qian Liu, Dmitry Abulkhanov, Indraneil Paul, Zhuang Li, Wen-Ding Li, Megan Risdal, Jia Li, Jian Zhu, Terry Yue Zhuo, Evgenii Zheltonozhskii, Nii Osae Osae Dade, Wenhao Yu, Lucas Krauß, Naman Jain, Yixuan Su, Xuanli He, Manan Dey, Edoardo Abati, Yekun Chai, Niklas Muennighoff, Xiangru Tang, Muhtasham Oblokulov, Christopher Akiki, Marc Marone, Chenghao Mou, Mayank Mishra, Alex Gu, Binyuan Hui, Tri Dao, Armel Zebaze, Olivier Dehaene, Nicolas Patry, Canwen Xu, Julian McAuley, Han Hu, Torsten Scholak, Sebastien Paquet, Jennifer Robinson, Carolyn Jane Anderson, Nicolas Chapados, Mostofa Patwary, Nima Tajbakhsh, Yacine Jernite, Carlos Muñoz Ferrandis, Lingming Zhang, Sean Hughes, Thomas Wolf, Arjun Guha, Leandro von Werra, and Harm de Vries.
1. **[Swin Transformer](https://huggingface.co/docs/transformers/model_doc/swin)** (from Microsoft) released with the paper [Swin Transformer: Hierarchical Vision Transformer using Shifted Windows](https://arxiv.org/abs/2103.14030) by Ze Liu, Yutong Lin, Yue Cao, Han Hu, Yixuan Wei, Zheng Zhang, Stephen Lin, Baining Guo.
1. **[Swin2SR](https://huggingface.co/docs/transformers/model_doc/swin2sr)** (from University of Würzburg) released with the paper [Swin2SR: SwinV2 Transformer for Compressed Image Super-Resolution and Restoration](https://arxiv.org/abs/2209.11345) by Marcos V. Conde, Ui-Jin Choi, Maxime Burchi, Radu Timofte.
1. **[T5](https://huggingface.co/docs/transformers/model_doc/t5)** (from Google AI) released with the paper [Exploring the Limits of Transfer Learning with a Unified Text-to-Text Transformer](https://arxiv.org/abs/1910.10683) by Colin Raffel and Noam Shazeer and Adam Roberts and Katherine Lee and Sharan Narang and Michael Matena and Yanqi Zhou and Wei Li and Peter J. Liu.
1. **[T5v1.1](https://huggingface.co/docs/transformers/model_doc/t5v1.1)** (from Google AI) released in the repository [google-research/text-to-text-transfer-transformer](https://github.com/google-research/text-to-text-transfer-transformer/blob/main/released_checkpoints.md#t511) by Colin Raffel and Noam Shazeer and Adam Roberts and Katherine Lee and Sharan Narang and Michael Matena and Yanqi Zhou and Wei Li and Peter J. Liu.
1. **[Table Transformer](https://huggingface.co/docs/transformers/model_doc/table-transformer)** (from Microsoft Research) released with the paper [PubTables-1M: Towards Comprehensive Table Extraction From Unstructured Documents](https://arxiv.org/abs/2110.00061) by Brandon Smock, Rohith Pesala, Robin Abraham.
1. **[TrOCR](https://huggingface.co/docs/transformers/model_doc/trocr)** (from Microsoft), released together with the paper [TrOCR: Transformer-based Optical Character Recognition with Pre-trained Models](https://arxiv.org/abs/2109.10282) by Minghao Li, Tengchao Lv, Lei Cui, Yijuan Lu, Dinei Florencio, Cha Zhang, Zhoujun Li, Furu Wei.
1. **[UniSpeech](https://huggingface.co/docs/transformers/model_doc/unispeech)** (from Microsoft Research) released with the paper [UniSpeech: Unified Speech Representation Learning with Labeled and Unlabeled Data](https://arxiv.org/abs/2101.07597) by Chengyi Wang, Yu Wu, Yao Qian, Kenichi Kumatani, Shujie Liu, Furu Wei, Michael Zeng, Xuedong Huang.
1. **[UniSpeechSat](https://huggingface.co/docs/transformers/model_doc/unispeech-sat)** (from Microsoft Research) released with the paper [UNISPEECH-SAT: UNIVERSAL SPEECH REPRESENTATION LEARNING WITH SPEAKER AWARE PRE-TRAINING](https://arxiv.org/abs/2110.05752) by Sanyuan Chen, Yu Wu, Chengyi Wang, Zhengyang Chen, Zhuo Chen, Shujie Liu, Jian Wu, Yao Qian, Furu Wei, Jinyu Li, Xiangzhan Yu.
1. **[Vision Transformer (ViT)](https://huggingface.co/docs/transformers/model_doc/vit)** (from Google AI) released with the paper [An Image is Worth 16x16 Words: Transformers for Image Recognition at Scale](https://arxiv.org/abs/2010.11929) by Alexey Dosovitskiy, Lucas Beyer, Alexander Kolesnikov, Dirk Weissenborn, Xiaohua Zhai, Thomas Unterthiner, Mostafa Dehghani, Matthias Minderer, Georg Heigold, Sylvain Gelly, Jakob Uszkoreit, Neil Houlsby.
1. **[ViTMatte](https://huggingface.co/docs/transformers/model_doc/vitmatte)** (from HUST-VL) released with the paper [ViTMatte: Boosting Image Matting with Pretrained Plain Vision Transformers](https://arxiv.org/abs/2305.15272) by Jingfeng Yao, Xinggang Wang, Shusheng Yang, Baoyuan Wang.
1. **[VITS](https://huggingface.co/docs/transformers/model_doc/vits)** (from Kakao Enterprise) released with the paper [Conditional Variational Autoencoder with Adversarial Learning for End-to-End Text-to-Speech](https://arxiv.org/abs/2106.06103) by Jaehyeon Kim, Jungil Kong, Juhee Son.
1. **[Wav2Vec2](https://huggingface.co/docs/transformers/model_doc/wav2vec2)** (from Facebook AI) released with the paper [wav2vec 2.0: A Framework for Self-Supervised Learning of Speech Representations](https://arxiv.org/abs/2006.11477) by Alexei Baevski, Henry Zhou, Abdelrahman Mohamed, Michael Auli.
1. **[Wav2Vec2-BERT](https://huggingface.co/docs/transformers/main/model_doc/wav2vec2-bert)** (from Meta AI) released with the paper [Seamless: Multilingual Expressive and Streaming Speech Translation](https://ai.meta.com/research/publications/seamless-multilingual-expressive-and-streaming-speech-translation/) by the Seamless Communication team.
1. **[WavLM](https://huggingface.co/docs/transformers/model_doc/wavlm)** (from Microsoft Research) released with the paper [WavLM: Large-Scale Self-Supervised Pre-Training for Full Stack Speech Processing](https://arxiv.org/abs/2110.13900) by Sanyuan Chen, Chengyi Wang, Zhengyang Chen, Yu Wu, Shujie Liu, Zhuo Chen, Jinyu Li, Naoyuki Kanda, Takuya Yoshioka, Xiong Xiao, Jian Wu, Long Zhou, Shuo Ren, Yanmin Qian, Yao Qian, Jian Wu, Michael Zeng, Furu Wei.
1. **[Whisper](https://huggingface.co/docs/transformers/model_doc/whisper)** (from OpenAI) released with the paper [Robust Speech Recognition via Large-Scale Weak Supervision](https://cdn.openai.com/papers/whisper.pdf) by Alec Radford, Jong Wook Kim, Tao Xu, Greg Brockman, Christine McLeavey, Ilya Sutskever.
1. **[XLM](https://huggingface.co/docs/transformers/model_doc/xlm)** (from Facebook) released together with the paper [Cross-lingual Language Model Pretraining](https://arxiv.org/abs/1901.07291) by Guillaume Lample and Alexis Conneau.
1. **[XLM-RoBERTa](https://huggingface.co/docs/transformers/model_doc/xlm-roberta)** (from Facebook AI), released together with the paper [Unsupervised Cross-lingual Representation Learning at Scale](https://arxiv.org/abs/1911.02116) by Alexis Conneau*, Kartikay Khandelwal*, Naman Goyal, Vishrav Chaudhary, Guillaume Wenzek, Francisco Guzmán, Edouard Grave, Myle Ott, Luke Zettlemoyer and Veselin Stoyanov.
1. **[YOLOS](https://huggingface.co/docs/transformers/model_doc/yolos)** (from Huazhong University of Science & Technology) released with the paper [You Only Look at One Sequence: Rethinking Transformer in Vision through Object Detection](https://arxiv.org/abs/2106.00666) by Yuxin Fang, Bencheng Liao, Xinggang Wang, Jiemin Fang, Jiyang Qi, Rui Wu, Jianwei Niu, Wenyu Liu.

Binary file not shown.

Binary file not shown.

Binary file not shown.

BIN
node_modules/@xenova/transformers/dist/ort-wasm.wasm generated vendored Normal file

Binary file not shown.

26758
node_modules/@xenova/transformers/dist/transformers.js generated vendored Normal file

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

84
node_modules/@xenova/transformers/package.json generated vendored Normal file
View File

@@ -0,0 +1,84 @@
{
"name": "@xenova/transformers",
"version": "2.17.2",
"description": "State-of-the-art Machine Learning for the web. Run 🤗 Transformers directly in your browser, with no need for a server!",
"main": "./src/transformers.js",
"types": "./types/transformers.d.ts",
"type": "module",
"scripts": {
"typegen": "tsc ./src/transformers.js --allowJs --declaration --emitDeclarationOnly --declarationMap --outDir types",
"dev": "webpack serve --no-client-overlay",
"build": "webpack && npm run typegen",
"generate-tests": "python -m tests.generate_tests",
"test": "node --experimental-vm-modules node_modules/jest/bin/jest.js --verbose --maxConcurrency 1",
"readme": "python ./docs/scripts/build_readme.py",
"docs-api": "node ./docs/scripts/generate.js",
"docs-preview": "doc-builder preview transformers.js ./docs/source/ --not_python_module",
"docs-build": "doc-builder build transformers.js ./docs/source/ --not_python_module --build_dir ./docs/build/ --repo_owner xenova"
},
"repository": {
"type": "git",
"url": "git+https://github.com/xenova/transformers.js.git"
},
"keywords": [
"transformers",
"transformers.js",
"huggingface",
"hugging face",
"machine learning",
"deep learning",
"artificial intelligence",
"AI",
"ML"
],
"author": "Xenova",
"license": "Apache-2.0",
"bugs": {
"url": "https://github.com/xenova/transformers.js/issues"
},
"homepage": "https://github.com/xenova/transformers.js#readme",
"dependencies": {
"onnxruntime-web": "1.14.0",
"sharp": "^0.32.0",
"@huggingface/jinja": "^0.2.2"
},
"optionalDependencies": {
"onnxruntime-node": "1.14.0"
},
"devDependencies": {
"@types/jest": "^29.5.1",
"catharsis": "github:xenova/catharsis",
"copy-webpack-plugin": "^11.0.0",
"jest": "^29.5.0",
"jest-environment-node": "^29.5.0",
"jsdoc-to-markdown": "^8.0.1",
"typescript": "^5.2.2",
"wavefile": "^11.0.0",
"webpack": "^5.80.0",
"webpack-cli": "^5.0.2",
"webpack-dev-server": "^4.13.3"
},
"overrides": {
"semver": "^7.5.4",
"protobufjs": "^7.2.6"
},
"files": [
"src",
"dist",
"types",
"README.md",
"LICENSE"
],
"browser": {
"fs": false,
"path": false,
"url": false,
"sharp": false,
"onnxruntime-node": false
},
"publishConfig": {
"access": "public"
},
"jsdelivr": "./dist/transformers.min.js",
"unpkg": "./dist/transformers.min.js"
}

50
node_modules/@xenova/transformers/src/backends/onnx.js generated vendored Normal file
View File

@@ -0,0 +1,50 @@
/**
* @file Handler file for choosing the correct version of ONNX Runtime, based on the environment.
* Ideally, we could import the `onnxruntime-web` and `onnxruntime-node` packages only when needed,
* but dynamic imports don't seem to work with the current webpack version and/or configuration.
* This is possibly due to the experimental nature of top-level await statements.
* So, we just import both packages, and use the appropriate one based on the environment:
* - When running in node, we use `onnxruntime-node`.
* - When running in the browser, we use `onnxruntime-web` (`onnxruntime-node` is not bundled).
*
* This module is not directly exported, but can be accessed through the environment variables:
* ```javascript
* import { env } from '@xenova/transformers';
* console.log(env.backends.onnx);
* ```
*
* @module backends/onnx
*/
// NOTE: Import order matters here. We need to import `onnxruntime-node` before `onnxruntime-web`.
// In either case, we select the default export if it exists, otherwise we use the named export.
import * as ONNX_NODE from 'onnxruntime-node';
import * as ONNX_WEB from 'onnxruntime-web';
/** @type {import('onnxruntime-web')} The ONNX runtime module. */
export let ONNX;
export const executionProviders = [
// 'webgpu',
'wasm'
];
if (typeof process !== 'undefined' && process?.release?.name === 'node') {
// Running in a node-like environment.
ONNX = ONNX_NODE.default ?? ONNX_NODE;
// Add `cpu` execution provider, with higher precedence that `wasm`.
executionProviders.unshift('cpu');
} else {
// Running in a browser-environment
ONNX = ONNX_WEB.default ?? ONNX_WEB;
// SIMD for WebAssembly does not operate correctly in some recent versions of iOS (16.4.x).
// As a temporary fix, we disable it for now.
// For more information, see: https://github.com/microsoft/onnxruntime/issues/15644
const isIOS = typeof navigator !== 'undefined' && /iP(hone|od|ad).+16_4.+AppleWebKit/.test(navigator.userAgent);
if (isIOS) {
ONNX.env.wasm.simd = false;
}
}

107
node_modules/@xenova/transformers/src/configs.js generated vendored Normal file
View File

@@ -0,0 +1,107 @@
/**
* @file Helper module for using model configs. For more information, see the corresponding
* [Python documentation](https://huggingface.co/docs/transformers/main/en/model_doc/auto#transformers.AutoConfig).
*
* **Example:** Load an `AutoConfig`.
*
* ```javascript
* import { AutoConfig } from '@xenova/transformers';
* let config = await AutoConfig.from_pretrained('bert-base-uncased');
* console.log(config);
* // PretrainedConfig {
* // "model_type": "bert",
* // "is_encoder_decoder": false,
* // "architectures": [
* // "BertForMaskedLM"
* // ],
* // "vocab_size": 30522
* // "num_attention_heads": 12,
* // "num_hidden_layers": 12,
* // "hidden_size": 768,
* // "max_position_embeddings": 512,
* // ...
* // }
* ```
*
* @module configs
*/
import {
getModelJSON,
} from './utils/hub.js';
/**
* @typedef {import('./utils/hub.js').PretrainedOptions} PretrainedOptions
*/
/**
* Loads a config from the specified path.
* @param {string} pretrained_model_name_or_path The path to the config directory.
* @param {PretrainedOptions} options Additional options for loading the config.
* @returns {Promise<Array>} A promise that resolves with information about the loaded config.
*/
async function loadConfig(pretrained_model_name_or_path, options) {
let info = await getModelJSON(pretrained_model_name_or_path, 'config.json', true, options);
return info;
}
/**
* Base class for all configuration classes. For more information, see the corresponding
* [Python documentation](https://huggingface.co/docs/transformers/main/en/main_classes/configuration#transformers.PretrainedConfig).
*/
export class PretrainedConfig {
// NOTE: Typo in original
/**
* Create a new PreTrainedTokenizer instance.
* @param {Object} configJSON The JSON of the config.
*/
constructor(configJSON) {
this.model_type = null;
this.is_encoder_decoder = false;
Object.assign(this, configJSON);
}
/**
* 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<PretrainedConfig>} 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',
} = {}) {
let data = config ?? await loadConfig(pretrained_model_name_or_path, {
progress_callback,
config,
cache_dir,
local_files_only,
revision,
})
return new this(data);
}
}
/**
* Helper class which is used to instantiate pretrained configs with the `from_pretrained` function.
*
* @example
* let config = await AutoConfig.from_pretrained('bert-base-uncased');
*/
export class AutoConfig {
/** @type {PretrainedConfig.from_pretrained} */
static async from_pretrained(...args) {
return PretrainedConfig.from_pretrained(...args);
}
}

128
node_modules/@xenova/transformers/src/env.js generated vendored Normal file
View File

@@ -0,0 +1,128 @@
/**
* @file Module used to configure Transformers.js.
*
* **Example:** Disable remote models.
* ```javascript
* import { env } from '@xenova/transformers';
* env.allowRemoteModels = false;
* ```
*
* **Example:** Set local model path.
* ```javascript
* import { env } from '@xenova/transformers';
* env.localModelPath = '/path/to/local/models/';
* ```
*
* **Example:** Set cache directory.
* ```javascript
* import { env } from '@xenova/transformers';
* env.cacheDir = '/path/to/cache/directory/';
* ```
*
* @module env
*/
import fs from 'fs';
import path from 'path';
import url from 'url';
import { ONNX } from './backends/onnx.js';
const { env: onnx_env } = ONNX;
const VERSION = '2.17.2';
// Check if various APIs are available (depends on environment)
const WEB_CACHE_AVAILABLE = typeof self !== 'undefined' && 'caches' in self;
const FS_AVAILABLE = !isEmpty(fs); // check if file system is available
const PATH_AVAILABLE = !isEmpty(path); // check if path is available
const RUNNING_LOCALLY = FS_AVAILABLE && PATH_AVAILABLE;
const __dirname = RUNNING_LOCALLY
? path.dirname(path.dirname(url.fileURLToPath(import.meta.url)))
: './';
// Only used for environments with access to file system
const DEFAULT_CACHE_DIR = RUNNING_LOCALLY
? path.join(__dirname, '/.cache/')
: null;
// Set local model path, based on available APIs
const DEFAULT_LOCAL_MODEL_PATH = '/models/';
const localModelPath = RUNNING_LOCALLY
? path.join(__dirname, DEFAULT_LOCAL_MODEL_PATH)
: DEFAULT_LOCAL_MODEL_PATH;
if (onnx_env?.wasm) {
// Set path to wasm files. This is needed when running in a web worker.
// https://onnxruntime.ai/docs/api/js/interfaces/Env.WebAssemblyFlags.html#wasmPaths
// We use remote wasm files by default to make it easier for newer users.
// In practice, users should probably self-host the necessary .wasm files.
onnx_env.wasm.wasmPaths = RUNNING_LOCALLY
? path.join(__dirname, '/dist/')
: `https://cdn.jsdelivr.net/npm/@xenova/transformers@${VERSION}/dist/`;
}
/**
* Global variable used to control execution. This provides users a simple way to configure Transformers.js.
* @property {Object} backends Expose environment variables of different backends,
* allowing users to set these variables if they want to.
* @property {string} __dirname Directory name of module. Useful for resolving local paths.
* @property {string} version This version of Transformers.js.
* @property {boolean} allowRemoteModels Whether to allow loading of remote files, defaults to `true`.
* If set to `false`, it will have the same effect as setting `local_files_only=true` when loading pipelines, models, tokenizers, processors, etc.
* @property {string} remoteHost Host URL to load models from. Defaults to the Hugging Face Hub.
* @property {string} remotePathTemplate Path template to fill in and append to `remoteHost` when loading models.
* @property {boolean} allowLocalModels Whether to allow loading of local files, defaults to `true`.
* If set to `false`, it will skip the local file check and try to load the model from the remote host.
* @property {string} localModelPath Path to load local models from. Defaults to `/models/`.
* @property {boolean} useFS Whether to use the file system to load files. By default, it is `true` if available.
* @property {boolean} useBrowserCache Whether to use Cache API to cache models. By default, it is `true` if available.
* @property {boolean} useFSCache Whether to use the file system to cache files. By default, it is `true` if available.
* @property {string} cacheDir The directory to use for caching files with the file system. By default, it is `./.cache`.
* @property {boolean} useCustomCache Whether to use a custom cache system (defined by `customCache`), defaults to `false`.
* @property {Object} customCache The custom cache to use. Defaults to `null`. Note: this must be an object which
* implements the `match` and `put` functions of the Web Cache API. For more information, see https://developer.mozilla.org/en-US/docs/Web/API/Cache
*/
export const env = {
/////////////////// Backends settings ///////////////////
backends: {
// onnxruntime-web/onnxruntime-node
onnx: onnx_env,
// TensorFlow.js
tfjs: {},
},
__dirname,
version: VERSION,
/////////////////// Model settings ///////////////////
allowRemoteModels: true,
remoteHost: 'https://huggingface.co/',
remotePathTemplate: '{model}/resolve/{revision}/',
allowLocalModels: true,
localModelPath: localModelPath,
useFS: FS_AVAILABLE,
/////////////////// Cache settings ///////////////////
useBrowserCache: WEB_CACHE_AVAILABLE,
useFSCache: FS_AVAILABLE,
cacheDir: DEFAULT_CACHE_DIR,
useCustomCache: false,
customCache: null,
//////////////////////////////////////////////////////
}
/**
* @param {Object} obj
* @private
*/
function isEmpty(obj) {
return Object.keys(obj).length === 0;
}

6267
node_modules/@xenova/transformers/src/models.js generated vendored Normal file

File diff suppressed because it is too large Load Diff

3287
node_modules/@xenova/transformers/src/pipelines.js generated vendored Normal file

File diff suppressed because it is too large Load Diff

2248
node_modules/@xenova/transformers/src/processors.js generated vendored Normal file

File diff suppressed because it is too large Load Diff

4479
node_modules/@xenova/transformers/src/tokenizers.js generated vendored Normal file

File diff suppressed because it is too large Load Diff

24
node_modules/@xenova/transformers/src/transformers.js generated vendored Normal file
View File

@@ -0,0 +1,24 @@
/**
* @file Entry point for the Transformers.js library. Only the exports from this file
* are available to the end user, and are grouped as follows:
*
* 1. [Pipelines](./pipelines)
* 2. [Environment variables](./env)
* 3. [Models](./models)
* 4. [Tokenizers](./tokenizers)
* 5. [Processors](./processors)
*
* @module transformers
*/
export * from './pipelines.js';
export * from './env.js';
export * from './models.js';
export * from './tokenizers.js';
export * from './processors.js';
export * from './configs.js';
export * from './utils/audio.js';
export * from './utils/image.js';
export * from './utils/tensor.js';
export * from './utils/maths.js';

672
node_modules/@xenova/transformers/src/utils/audio.js generated vendored Normal file
View File

@@ -0,0 +1,672 @@
/**
* @file Helper module for audio processing.
*
* These functions and classes are only used internally,
* meaning an end-user shouldn't need to access anything here.
*
* @module utils/audio
*/
import {
getFile,
} from './hub.js';
import { FFT, max } from './maths.js';
import {
calculateReflectOffset,
} from './core.js';
/**
* Helper function to read audio from a path/URL.
* @param {string|URL} url The path/URL to load the audio from.
* @param {number} sampling_rate The sampling rate to use when decoding the audio.
* @returns {Promise<Float32Array>} The decoded audio as a `Float32Array`.
*/
export async function read_audio(url, sampling_rate) {
if (typeof AudioContext === 'undefined') {
// Running in node or an environment without AudioContext
throw Error(
"Unable to load audio from path/URL since `AudioContext` is not available in your environment. " +
"Instead, audio data should be passed directly to the pipeline/processor. " +
"For more information and some example code, see https://huggingface.co/docs/transformers.js/guides/node-audio-processing."
)
}
const response = await (await getFile(url)).arrayBuffer();
const audioCTX = new AudioContext({ sampleRate: sampling_rate });
if (typeof sampling_rate === 'undefined') {
console.warn(`No sampling rate provided, using default of ${audioCTX.sampleRate}Hz.`)
}
const decoded = await audioCTX.decodeAudioData(response);
/** @type {Float32Array} */
let audio;
// We now replicate HuggingFace's `ffmpeg_read` method:
if (decoded.numberOfChannels === 2) {
// When downmixing a stereo audio file to mono using the -ac 1 option in FFmpeg,
// the audio signal is summed across both channels to create a single mono channel.
// However, if the audio is at full scale (i.e. the highest possible volume level),
// the summing of the two channels can cause the audio signal to clip or distort.
// To prevent this clipping, FFmpeg applies a scaling factor of 1/sqrt(2) (~ 0.707)
// to the audio signal before summing the two channels. This scaling factor ensures
// that the combined audio signal will not exceed the maximum possible level, even
// if both channels are at full scale.
// After applying this scaling factor, the audio signal from both channels is summed
// to create a single mono channel. It's worth noting that this scaling factor is
// only applied when downmixing stereo audio to mono using the -ac 1 option in FFmpeg.
// If you're using a different downmixing method, or if you're not downmixing the
// audio at all, this scaling factor may not be needed.
const SCALING_FACTOR = Math.sqrt(2);
const left = decoded.getChannelData(0);
const right = decoded.getChannelData(1);
audio = new Float32Array(left.length);
for (let i = 0; i < decoded.length; ++i) {
audio[i] = SCALING_FACTOR * (left[i] + right[i]) / 2;
}
} else {
// If the audio is not stereo, we can just use the first channel:
audio = decoded.getChannelData(0);
}
return audio;
}
/**
* Generates a Hanning window of length M.
*
* @param {number} M The length of the Hanning window to generate.
* @returns {Float64Array} The generated Hanning window.
*/
export function hanning(M) {
if (M < 1) {
return new Float64Array();
}
if (M === 1) {
return new Float64Array([1]);
}
const denom = M - 1;
const factor = Math.PI / denom;
const cos_vals = new Float64Array(M);
for (let i = 0; i < M; ++i) {
const n = 2 * i - denom;
cos_vals[i] = 0.5 + 0.5 * Math.cos(factor * n);
}
return cos_vals;
}
const HERTZ_TO_MEL_MAPPING = {
"htk": (/** @type {number} */ freq) => 2595.0 * Math.log10(1.0 + (freq / 700.0)),
"kaldi": (/** @type {number} */ freq) => 1127.0 * Math.log(1.0 + (freq / 700.0)),
"slaney": (/** @type {number} */ freq, min_log_hertz = 1000.0, min_log_mel = 15.0, logstep = 27.0 / Math.log(6.4)) =>
freq >= min_log_hertz
? min_log_mel + Math.log(freq / min_log_hertz) * logstep
: 3.0 * freq / 200.0,
}
/**
* @template {Float32Array|Float64Array|number} T
* @param {T} freq
* @param {string} [mel_scale]
* @returns {T}
*/
function hertz_to_mel(freq, mel_scale = "htk") {
const fn = HERTZ_TO_MEL_MAPPING[mel_scale];
if (!fn) {
throw new Error('mel_scale should be one of "htk", "slaney" or "kaldi".');
}
return typeof freq === 'number' ? fn(freq) : freq.map(x => fn(x));
}
const MEL_TO_HERTZ_MAPPING = {
"htk": (/** @type {number} */ mels) => 700.0 * (10.0 ** (mels / 2595.0) - 1.0),
"kaldi": (/** @type {number} */ mels) => 700.0 * (Math.exp(mels / 1127.0) - 1.0),
"slaney": (/** @type {number} */ mels, min_log_hertz = 1000.0, min_log_mel = 15.0, logstep = Math.log(6.4) / 27.0) => mels >= min_log_mel
? min_log_hertz * Math.exp(logstep * (mels - min_log_mel))
: 200.0 * mels / 3.0,
}
/**
* @template {Float32Array|Float64Array|number} T
* @param {T} mels
* @param {string} [mel_scale]
* @returns {T}
*/
function mel_to_hertz(mels, mel_scale = "htk") {
const fn = MEL_TO_HERTZ_MAPPING[mel_scale];
if (!fn) {
throw new Error('mel_scale should be one of "htk", "slaney" or "kaldi".');
}
return typeof mels === 'number' ? fn(mels) : mels.map(x => fn(x));
}
/**
* Creates a triangular filter bank.
*
* Adapted from torchaudio and librosa.
*
* @param {Float64Array} fft_freqs Discrete frequencies of the FFT bins in Hz, of shape `(num_frequency_bins,)`.
* @param {Float64Array} filter_freqs Center frequencies of the triangular filters to create, in Hz, of shape `(num_mel_filters,)`.
* @returns {number[][]} of shape `(num_frequency_bins, num_mel_filters)`.
*/
function _create_triangular_filter_bank(fft_freqs, filter_freqs) {
const filter_diff = Float64Array.from(
{ length: filter_freqs.length - 1 },
(_, i) => filter_freqs[i + 1] - filter_freqs[i]
);
const slopes = Array.from({
length: fft_freqs.length
}, () => new Array(filter_freqs.length));
for (let j = 0; j < fft_freqs.length; ++j) {
const slope = slopes[j];
for (let i = 0; i < filter_freqs.length; ++i) {
slope[i] = filter_freqs[i] - fft_freqs[j];
}
}
const numFreqs = filter_freqs.length - 2;
const ret = Array.from({ length: numFreqs }, () => new Array(fft_freqs.length));
for (let j = 0; j < fft_freqs.length; ++j) { // 201
const slope = slopes[j];
for (let i = 0; i < numFreqs; ++i) { // 80
const down = -slope[i] / filter_diff[i];
const up = slope[i + 2] / filter_diff[i + 1];
ret[i][j] = Math.max(0, Math.min(down, up));
}
}
return ret;
}
/**
* Return evenly spaced numbers over a specified interval.
* @param {number} start The starting value of the sequence.
* @param {number} end The end value of the sequence.
* @param {number} num Number of samples to generate.
* @returns `num` evenly spaced samples, calculated over the interval `[start, stop]`.
*/
function linspace(start, end, num) {
const step = (end - start) / (num - 1);
return Float64Array.from({ length: num }, (_, i) => start + step * i);
}
/**
* Creates a frequency bin conversion matrix used to obtain a mel spectrogram. This is called a *mel filter bank*, and
* various implementation exist, which differ in the number of filters, the shape of the filters, the way the filters
* are spaced, the bandwidth of the filters, and the manner in which the spectrum is warped. The goal of these
* features is to approximate the non-linear human perception of the variation in pitch with respect to the frequency.
* @param {number} num_frequency_bins Number of frequencies used to compute the spectrogram (should be the same as in `stft`).
* @param {number} num_mel_filters Number of mel filters to generate.
* @param {number} min_frequency Lowest frequency of interest in Hz.
* @param {number} max_frequency Highest frequency of interest in Hz. This should not exceed `sampling_rate / 2`.
* @param {number} sampling_rate Sample rate of the audio waveform.
* @param {string} [norm] If `"slaney"`, divide the triangular mel weights by the width of the mel band (area normalization).
* @param {string} [mel_scale] The mel frequency scale to use, `"htk"` or `"slaney"`.
* @param {boolean} [triangularize_in_mel_space] If this option is enabled, the triangular filter is applied in mel space rather than frequency space.
* This should be set to `true` in order to get the same results as `torchaudio` when computing mel filters.
* @returns {number[][]} Triangular filter bank matrix, which is a 2D array of shape (`num_frequency_bins`, `num_mel_filters`).
* This is a projection matrix to go from a spectrogram to a mel spectrogram.
*/
export function mel_filter_bank(
num_frequency_bins,
num_mel_filters,
min_frequency,
max_frequency,
sampling_rate,
norm = null,
mel_scale = "htk",
triangularize_in_mel_space = false,
) {
if (norm !== null && norm !== "slaney") {
throw new Error('norm must be one of null or "slaney"');
}
const mel_min = hertz_to_mel(min_frequency, mel_scale);
const mel_max = hertz_to_mel(max_frequency, mel_scale);
const mel_freqs = linspace(mel_min, mel_max, num_mel_filters + 2);
let filter_freqs = mel_to_hertz(mel_freqs, mel_scale);
let fft_freqs; // frequencies of FFT bins in Hz
if (triangularize_in_mel_space) {
const fft_bin_width = sampling_rate / (num_frequency_bins * 2);
fft_freqs = hertz_to_mel(Float64Array.from({ length: num_frequency_bins }, (_, i) => i * fft_bin_width), mel_scale);
filter_freqs = mel_freqs;
} else {
fft_freqs = linspace(0, Math.floor(sampling_rate / 2), num_frequency_bins);
}
const mel_filters = _create_triangular_filter_bank(fft_freqs, filter_freqs);
if (norm !== null && norm === "slaney") {
// Slaney-style mel is scaled to be approx constant energy per channel
for (let i = 0; i < num_mel_filters; ++i) {
const filter = mel_filters[i];
const enorm = 2.0 / (filter_freqs[i + 2] - filter_freqs[i]);
for (let j = 0; j < num_frequency_bins; ++j) {
// Apply this enorm to all frequency bins
filter[j] *= enorm;
}
}
}
// TODO warn if there is a zero row
return mel_filters;
}
/**
* @template {Float32Array|Float64Array} T
* Pads an array with a reflected version of itself on both ends.
* @param {T} array The array to pad.
* @param {number} left The amount of padding to add to the left.
* @param {number} right The amount of padding to add to the right.
* @returns {T} The padded array.
*/
function padReflect(array, left, right) {
// @ts-ignore
const padded = new array.constructor(array.length + left + right);
const w = array.length - 1;
for (let i = 0; i < array.length; ++i) {
padded[left + i] = array[i];
}
for (let i = 1; i <= left; ++i) {
padded[left - i] = array[calculateReflectOffset(i, w)];
}
for (let i = 1; i <= right; ++i) {
padded[w + left + i] = array[calculateReflectOffset(w - i, w)];
}
return padded;
}
/**
* Helper function to compute `amplitude_to_db` and `power_to_db`.
* @template {Float32Array|Float64Array} T
* @param {T} spectrogram
* @param {number} factor
* @param {number} reference
* @param {number} min_value
* @param {number} db_range
* @returns {T}
*/
function _db_conversion_helper(spectrogram, factor, reference, min_value, db_range) {
if (reference <= 0) {
throw new Error('reference must be greater than zero');
}
if (min_value <= 0) {
throw new Error('min_value must be greater than zero');
}
reference = Math.max(min_value, reference);
const logReference = Math.log10(reference);
for (let i = 0; i < spectrogram.length; ++i) {
spectrogram[i] = factor * Math.log10(Math.max(min_value, spectrogram[i]) - logReference)
}
if (db_range !== null) {
if (db_range <= 0) {
throw new Error('db_range must be greater than zero');
}
const maxValue = max(spectrogram)[0] - db_range;
for (let i = 0; i < spectrogram.length; ++i) {
spectrogram[i] = Math.max(spectrogram[i], maxValue);
}
}
return spectrogram;
}
/**
* Converts an amplitude spectrogram to the decibel scale. This computes `20 * log10(spectrogram / reference)`,
* using basic logarithm properties for numerical stability. NOTE: Operates in-place.
*
* The motivation behind applying the log function on the (mel) spectrogram is that humans do not hear loudness on a
* linear scale. Generally to double the perceived volume of a sound we need to put 8 times as much energy into it.
* This means that large variations in energy may not sound all that different if the sound is loud to begin with.
* This compression operation makes the (mel) spectrogram features match more closely what humans actually hear.
*
* @template {Float32Array|Float64Array} T
* @param {T} spectrogram The input amplitude (mel) spectrogram.
* @param {number} [reference=1.0] Sets the input spectrogram value that corresponds to 0 dB.
* For example, use `np.max(spectrogram)` to set the loudest part to 0 dB. Must be greater than zero.
* @param {number} [min_value=1e-5] The spectrogram will be clipped to this minimum value before conversion to decibels,
* to avoid taking `log(0)`. The default of `1e-5` corresponds to a minimum of -100 dB. Must be greater than zero.
* @param {number} [db_range=null] Sets the maximum dynamic range in decibels. For example, if `db_range = 80`, the
* difference between the peak value and the smallest value will never be more than 80 dB. Must be greater than zero.
* @returns {T} The modified spectrogram in decibels.
*/
function amplitude_to_db(spectrogram, reference = 1.0, min_value = 1e-5, db_range = null) {
return _db_conversion_helper(spectrogram, 20.0, reference, min_value, db_range);
}
/**
* Converts a power spectrogram to the decibel scale. This computes `10 * log10(spectrogram / reference)`,
* using basic logarithm properties for numerical stability. NOTE: Operates in-place.
*
* The motivation behind applying the log function on the (mel) spectrogram is that humans do not hear loudness on a
* linear scale. Generally to double the perceived volume of a sound we need to put 8 times as much energy into it.
* This means that large variations in energy may not sound all that different if the sound is loud to begin with.
* This compression operation makes the (mel) spectrogram features match more closely what humans actually hear.
*
* Based on the implementation of `librosa.power_to_db`.
*
* @template {Float32Array|Float64Array} T
* @param {T} spectrogram The input power (mel) spectrogram. Note that a power spectrogram has the amplitudes squared!
* @param {number} [reference=1.0] Sets the input spectrogram value that corresponds to 0 dB.
* For example, use `np.max(spectrogram)` to set the loudest part to 0 dB. Must be greater than zero.
* @param {number} [min_value=1e-10] The spectrogram will be clipped to this minimum value before conversion to decibels,
* to avoid taking `log(0)`. The default of `1e-10` corresponds to a minimum of -100 dB. Must be greater than zero.
* @param {number} [db_range=null] Sets the maximum dynamic range in decibels. For example, if `db_range = 80`, the
* difference between the peak value and the smallest value will never be more than 80 dB. Must be greater than zero.
* @returns {T} The modified spectrogram in decibels.
*/
function power_to_db(spectrogram, reference = 1.0, min_value = 1e-10, db_range = null) {
return _db_conversion_helper(spectrogram, 10.0, reference, min_value, db_range);
}
/**
* Calculates a spectrogram over one waveform using the Short-Time Fourier Transform.
*
* This function can create the following kinds of spectrograms:
* - amplitude spectrogram (`power = 1.0`)
* - power spectrogram (`power = 2.0`)
* - complex-valued spectrogram (`power = None`)
* - log spectrogram (use `log_mel` argument)
* - mel spectrogram (provide `mel_filters`)
* - log-mel spectrogram (provide `mel_filters` and `log_mel`)
*
* In this implementation, the window is assumed to be zero-padded to have the same size as the analysis frame.
* A padded window can be obtained from `window_function()`. The FFT input buffer may be larger than the analysis frame,
* typically the next power of two.
*
* @param {Float32Array|Float64Array} waveform The input waveform of shape `(length,)`. This must be a single real-valued, mono waveform.
* @param {Float32Array|Float64Array} window The windowing function to apply of shape `(frame_length,)`, including zero-padding if necessary. The actual window length may be
* shorter than `frame_length`, but we're assuming the array has already been zero-padded.
* @param {number} frame_length The length of the analysis frames in samples (a.k.a., `fft_length`).
* @param {number} hop_length The stride between successive analysis frames in samples.
* @param {Object} options
* @param {number} [options.fft_length=null] The size of the FFT buffer in samples. This determines how many frequency bins the spectrogram will have.
* For optimal speed, this should be a power of two. If `null`, uses `frame_length`.
* @param {number} [options.power=1.0] If 1.0, returns the amplitude spectrogram. If 2.0, returns the power spectrogram. If `null`, returns complex numbers.
* @param {boolean} [options.center=true] Whether to pad the waveform so that frame `t` is centered around time `t * hop_length`. If `false`, frame
* `t` will start at time `t * hop_length`.
* @param {string} [options.pad_mode="reflect"] Padding mode used when `center` is `true`. Possible values are: `"constant"` (pad with zeros),
* `"edge"` (pad with edge values), `"reflect"` (pads with mirrored values).
* @param {boolean} [options.onesided=true] If `true`, only computes the positive frequencies and returns a spectrogram containing `fft_length // 2 + 1`
* frequency bins. If `false`, also computes the negative frequencies and returns `fft_length` frequency bins.
* @param {number} [options.preemphasis=null] Coefficient for a low-pass filter that applies pre-emphasis before the DFT.
* @param {number[][]} [options.mel_filters=null] The mel filter bank of shape `(num_freq_bins, num_mel_filters)`.
* If supplied, applies this filter bank to create a mel spectrogram.
* @param {number} [options.mel_floor=1e-10] Minimum value of mel frequency banks.
* @param {string} [options.log_mel=null] How to convert the spectrogram to log scale. Possible options are:
* `null` (don't convert), `"log"` (take the natural logarithm) `"log10"` (take the base-10 logarithm), `"dB"` (convert to decibels).
* Can only be used when `power` is not `null`.
* @param {number} [options.reference=1.0] Sets the input spectrogram value that corresponds to 0 dB. For example, use `max(spectrogram)[0]` to set
* the loudest part to 0 dB. Must be greater than zero.
* @param {number} [options.min_value=1e-10] The spectrogram will be clipped to this minimum value before conversion to decibels, to avoid taking `log(0)`.
* For a power spectrogram, the default of `1e-10` corresponds to a minimum of -100 dB. For an amplitude spectrogram, the value `1e-5` corresponds to -100 dB.
* Must be greater than zero.
* @param {number} [options.db_range=null] Sets the maximum dynamic range in decibels. For example, if `db_range = 80`, the difference between the
* peak value and the smallest value will never be more than 80 dB. Must be greater than zero.
* @param {boolean} [options.remove_dc_offset=null] Subtract mean from waveform on each frame, applied before pre-emphasis. This should be set to `true` in
* order to get the same results as `torchaudio.compliance.kaldi.fbank` when computing mel filters.
* @param {number} [options.max_num_frames=null] If provided, limits the number of frames to compute to this value.
* @param {boolean} [options.do_pad=true] If `true`, pads the output spectrogram to have `max_num_frames` frames.
* @param {boolean} [options.transpose=false] If `true`, the returned spectrogram will have shape `(num_frames, num_frequency_bins/num_mel_filters)`. If `false`, the returned spectrogram will have shape `(num_frequency_bins/num_mel_filters, num_frames)`.
* @returns {{data: Float32Array, dims: number[]}} Spectrogram of shape `(num_frequency_bins, length)` (regular spectrogram) or shape `(num_mel_filters, length)` (mel spectrogram).
*/
export function spectrogram(
waveform,
window,
frame_length,
hop_length,
{
fft_length = null,
power = 1.0,
center = true,
pad_mode = "reflect",
onesided = true,
preemphasis = null,
mel_filters = null,
mel_floor = 1e-10,
log_mel = null,
reference = 1.0,
min_value = 1e-10,
db_range = null,
remove_dc_offset = null,
// Custom parameters for efficiency reasons
max_num_frames = null,
do_pad = true,
transpose = false,
} = {}
) {
const window_length = window.length;
if (fft_length === null) {
fft_length = frame_length;
}
if (frame_length > fft_length) {
throw Error(`frame_length (${frame_length}) may not be larger than fft_length (${fft_length})`)
}
if (window_length !== frame_length) {
throw new Error(`Length of the window (${window_length}) must equal frame_length (${frame_length})`);
}
if (hop_length <= 0) {
throw new Error("hop_length must be greater than zero");
}
if (power === null && mel_filters !== null) {
throw new Error(
"You have provided `mel_filters` but `power` is `None`. Mel spectrogram computation is not yet supported for complex-valued spectrogram. " +
"Specify `power` to fix this issue."
);
}
if (center) {
if (pad_mode !== 'reflect') {
throw new Error(`pad_mode="${pad_mode}" not implemented yet.`)
}
const half_window = Math.floor((fft_length - 1) / 2) + 1;
waveform = padReflect(waveform, half_window, half_window);
}
// split waveform into frames of frame_length size
const num_frames = Math.floor(1 + Math.floor((waveform.length - frame_length) / hop_length))
const num_frequency_bins = onesided ? Math.floor(fft_length / 2) + 1 : fft_length
let d1 = num_frames;
let d1Max = num_frames;
// If maximum number of frames is provided, we must either pad or truncate
if (max_num_frames !== null) {
if (max_num_frames > num_frames) { // input is too short, so we pad
if (do_pad) {
d1Max = max_num_frames;
}
} else { // input is too long, so we truncate
d1Max = d1 = max_num_frames;
}
}
// Preallocate arrays to store output.
const fft = new FFT(fft_length);
const inputBuffer = new Float64Array(fft_length);
const outputBuffer = new Float64Array(fft.outputBufferSize);
const magnitudes = new Array(d1);
for (let i = 0; i < d1; ++i) {
// Populate buffer with waveform data
const offset = i * hop_length;
for (let j = 0; j < frame_length; ++j) {
inputBuffer[j] = waveform[offset + j];
}
if (remove_dc_offset) {
let sum = 0;
for (let j = 0; j < frame_length; ++j) {
sum += inputBuffer[j];
}
const mean = sum / frame_length;
for (let j = 0; j < frame_length; ++j) {
inputBuffer[j] -= mean;
}
}
if (preemphasis !== null) {
// Done in reverse to avoid copies and distructive modification
for (let j = frame_length - 1; j >= 1; --j) {
inputBuffer[j] -= preemphasis * inputBuffer[j - 1];
}
inputBuffer[0] *= 1 - preemphasis;
}
for (let j = 0; j < window.length; ++j) {
inputBuffer[j] *= window[j];
}
fft.realTransform(outputBuffer, inputBuffer);
// compute magnitudes
const row = new Array(num_frequency_bins);
for (let j = 0; j < row.length; ++j) {
const j2 = j << 1;
row[j] = outputBuffer[j2] ** 2 + outputBuffer[j2 + 1] ** 2;
}
magnitudes[i] = row;
}
if (power !== null && power !== 2) {
// slight optimization to not sqrt
const pow = 2 / power; // we use 2 since we already squared
for (let i = 0; i < magnitudes.length; ++i) {
const magnitude = magnitudes[i];
for (let j = 0; j < magnitude.length; ++j) {
magnitude[j] **= pow;
}
}
}
// TODO: What if `mel_filters` is null?
const num_mel_filters = mel_filters.length;
// Only here do we create Float32Array
const mel_spec = new Float32Array(num_mel_filters * d1Max);
// Perform matrix muliplication:
// mel_spec = mel_filters @ magnitudes.T
// - mel_filters.shape=(80, 201)
// - magnitudes.shape=(3000, 201) => - magnitudes.T.shape=(201, 3000)
// - mel_spec.shape=(80, 3000)
const dims = transpose ? [d1Max, num_mel_filters] : [num_mel_filters, d1Max];
for (let i = 0; i < num_mel_filters; ++i) { // num melfilters (e.g., 80)
const filter = mel_filters[i];
for (let j = 0; j < d1; ++j) { // num frames (e.g., 3000)
const magnitude = magnitudes[j];
let sum = 0;
for (let k = 0; k < num_frequency_bins; ++k) { // num frequency bins (e.g., 201)
sum += filter[k] * magnitude[k];
}
mel_spec[
transpose
? j * num_mel_filters + i
: i * d1 + j
] = Math.max(mel_floor, sum);
}
}
if (power !== null && log_mel !== null) {
const o = Math.min(mel_spec.length, d1 * num_mel_filters);
switch (log_mel) {
case 'log':
for (let i = 0; i < o; ++i) {
mel_spec[i] = Math.log(mel_spec[i]);
}
break;
case 'log10':
for (let i = 0; i < o; ++i) {
mel_spec[i] = Math.log10(mel_spec[i]);
}
break;
case 'dB':
if (power === 1.0) {
// NOTE: operates in-place
amplitude_to_db(mel_spec, reference, min_value, db_range);
} else if (power === 2.0) {
power_to_db(mel_spec, 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 { data: mel_spec, dims };
}
/**
* Returns an array containing the specified window.
* @param {number} window_length The length of the window in samples.
* @param {string} name The name of the window function.
* @param {Object} options Additional options.
* @param {boolean} [options.periodic=true] Whether the window is periodic or symmetric.
* @param {number} [options.frame_length=null] The length of the analysis frames in samples.
* Provide a value for `frame_length` if the window is smaller than the frame length, so that it will be zero-padded.
* @param {boolean} [options.center=true] Whether to center the window inside the FFT buffer. Only used when `frame_length` is provided.
* @returns {Float64Array} The window of shape `(window_length,)` or `(frame_length,)`.
*/
export function window_function(window_length, name, {
periodic = true,
frame_length = null,
center = true,
} = {}) {
const length = periodic ? window_length + 1 : window_length;
let window;
switch (name) {
case 'boxcar':
window = new Float64Array(length).fill(1.0);
break;
case 'hann':
case 'hann_window':
window = hanning(length);
break;
case 'povey':
window = hanning(length).map(x => Math.pow(x, 0.85));
break;
default:
throw new Error(`Unknown window type ${name}.`);
}
if (periodic) {
window = window.subarray(0, window_length);
}
if (frame_length === null) {
return window;
}
if (window_length > frame_length) {
throw new Error(`Length of the window (${window_length}) may not be larger than frame_length (${frame_length})`);
}
return window;
}

175
node_modules/@xenova/transformers/src/utils/core.js generated vendored Normal file
View File

@@ -0,0 +1,175 @@
/**
* @file Core utility functions/classes for Transformers.js.
*
* These are only used internally, meaning an end-user shouldn't
* need to access anything here.
*
* @module utils/core
*/
/**
* Helper function to dispatch progress callbacks.
*
* @param {Function} progress_callback The progress callback function to dispatch.
* @param {any} data The data to pass to the progress callback function.
* @returns {void}
* @private
*/
export function dispatchCallback(progress_callback, data) {
if (progress_callback) progress_callback(data);
}
/**
* Reverses the keys and values of an object.
*
* @param {Object} data The object to reverse.
* @returns {Object} The reversed object.
* @see https://ultimatecourses.com/blog/reverse-object-keys-and-values-in-javascript
*/
export function reverseDictionary(data) {
// https://ultimatecourses.com/blog/reverse-object-keys-and-values-in-javascript
return Object.fromEntries(Object.entries(data).map(([key, value]) => [value, key]));
}
/**
* Escapes regular expression special characters from a string by replacing them with their escaped counterparts.
*
* @param {string} string The string to escape.
* @returns {string} The escaped string.
*/
export function escapeRegExp(string) {
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string
}
/**
* A base class for creating callable objects.
*
* @type {new () => {(...args: any[]): any, _call(...args: any[]): any}}
*/
export const Callable = /** @type {any} */ (class {
/**
* Creates a new instance of the Callable class.
*/
constructor() {
/**
* Creates a closure that delegates to a private method '_call' with the given arguments.
* @type {any}
* @param {...any} args Zero or more arguments to pass to the '_call' method.
* @returns {*} The result of calling the '_call' method.
*/
let closure = function (...args) {
return closure._call(...args)
}
return Object.setPrototypeOf(closure, new.target.prototype)
}
/**
* This method should be implemented in subclasses to provide the
* functionality of the callable object.
*
* @param {any[]} args
* @throws {Error} If the subclass does not implement the `_call` method.
*/
_call(...args) {
throw Error('Must implement _call method in subclass')
}
});
/**
* Check if a value is a typed array.
* @param {*} val The value to check.
* @returns {boolean} True if the value is a `TypedArray`, false otherwise.
*
* Adapted from https://stackoverflow.com/a/71091338/13989043
*/
export function isTypedArray(val) {
return val?.prototype?.__proto__?.constructor?.name === 'TypedArray';
}
/**
* Check if a value is an integer.
* @param {*} x The value to check.
* @returns {boolean} True if the value is a string, false otherwise.
*/
export function isIntegralNumber(x) {
return Number.isInteger(x) || typeof x === 'bigint'
}
/**
* Check if a value is exists.
* @param {*} x The value to check.
* @returns {boolean} True if the value exists, false otherwise.
*/
export function exists(x) {
return x !== undefined && x !== null;
}
/**
* Calculates the dimensions of a nested array.
*
* @param {any[]} arr The nested array to calculate dimensions for.
* @returns {number[]} An array containing the dimensions of the input array.
*/
export function calculateDimensions(arr) {
const dimensions = [];
let current = arr;
while (Array.isArray(current)) {
dimensions.push(current.length);
current = current[0];
}
return dimensions;
}
/**
* Replicate python's .pop() method for objects.
* @param {Object} obj The object to pop from.
* @param {string} key The key to pop.
* @param {*} defaultValue The default value to return if the key does not exist.
* @returns {*} The value of the popped key.
* @throws {Error} If the key does not exist and no default value is provided.
*/
export function pop(obj, key, defaultValue = undefined) {
const value = obj[key];
if (value !== undefined) {
delete obj[key];
return value;
}
if (defaultValue === undefined) {
throw Error(`Key ${key} does not exist in object.`)
}
return defaultValue;
}
/**
* Efficiently merge arrays, creating a new copy.
* Adapted from https://stackoverflow.com/a/6768642/13989043
* @param {Array[]} arrs Arrays to merge.
* @returns {Array} The merged array.
*/
export function mergeArrays(...arrs) {
return Array.prototype.concat.apply([], arrs);
}
/**
* Compute the Cartesian product of given arrays
* @param {...Array} a Arrays to compute the product
* @returns {Array} Returns the computed Cartesian product as an array
* @private
*/
export function product(...a) {
// Cartesian product of items
// Adapted from https://stackoverflow.com/a/43053803
return a.reduce((a, b) => a.flatMap(d => b.map(e => [d, e])));
}
/**
* Calculates the index offset for a given index and window size.
* @param {number} i The index.
* @param {number} w The window size.
* @returns {number} The index offset.
*/
export function calculateReflectOffset(i, w) {
return Math.abs((i + w) % (2 * w) - w);
}

View File

@@ -0,0 +1,415 @@
/**
* @file Custom data structures.
*
* These are only used internally, meaning an end-user shouldn't
* need to access anything here.
*
* @module utils/data-structures
*/
/**
* Efficient Heap-based Implementation of a Priority Queue.
* It uses an array-based binary heap, where the root is at index `0`, and the
* children of node `i` are located at indices `2i + 1` and `2i + 2`, respectively.
*
* Adapted from the following sources:
* - https://stackoverflow.com/a/42919752/13989043 (original)
* - https://github.com/belladoreai/llama-tokenizer-js (minor improvements)
*/
export class PriorityQueue {
/**
* Create a new PriorityQueue.
* @param {Function} comparator Comparator function to determine priority. Defaults to a MaxHeap.
*/
constructor(comparator = (a, b) => a > b) {
this._heap = [];
this._comparator = comparator;
}
/**
* 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) {
this._heap.push(value);
this._siftUp();
}
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() {
let node = this.size - 1;
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;
}
}
}
/**
* A trie structure to efficiently store and search for strings.
*/
export 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 (let 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 (let ch of text) {
let child = node.children.get(ch);
if (child === undefined) {
child = CharTrieNode.default();
node.children.set(ch, child);
}
node = child;
}
node.isLeaf = true;
}
/**
* Searches the trie for all strings with a common prefix of `text`.
* @param {string} text The common prefix to search for.
* @yields {string} Each string in the trie that has `text` as a prefix.
*/
*commonPrefixSearch(text) {
let node = this.root;
let prefix = "";
for (let i = 0; i < text.length && node !== undefined; ++i) {
const ch = text[i];
prefix += ch;
node = node.children.get(ch);
if (node !== undefined && node.isLeaf) {
yield prefix;
}
}
}
}
/**
* Represents a node in a character trie.
*/
class CharTrieNode {
/**
* Create a new CharTrieNode.
* @param {boolean} isLeaf Whether the node is a leaf node or not.
* @param {Map<string, CharTrieNode>} children A map containing the node's children, where the key is a character and the value is a `CharTrieNode`.
*/
constructor(isLeaf, children) {
this.isLeaf = isLeaf;
this.children = children;
}
/**
* Returns a new `CharTrieNode` instance with default values.
* @returns {CharTrieNode} A new `CharTrieNode` instance with `isLeaf` set to `false` and an empty `children` map.
*/
static default() {
return new CharTrieNode(false, new Map());
}
}
/**
* A lattice data structure to be used for tokenization.
*/
export 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.sentence = sentence;
this.len = sentence.length;
this.bosTokenId = bosTokenId;
this.eosTokenId = eosTokenId;
this.nodes = [];
this.beginNodes = Array.from({ length: this.len + 1 }, () => []);
this.endNodes = Array.from({ length: this.len + 1 }, () => []);
const bos = new TokenLatticeNode(this.bosTokenId, 0, 0, 0, 0.0);
const eos = new TokenLatticeNode(this.eosTokenId, 1, this.len, 0, 0.0);
this.nodes.push(bos.clone());
this.nodes.push(eos.clone());
this.beginNodes[this.len].push(eos);
this.endNodes[0].push(bos);
}
/**
* Inserts a new token node into the token lattice.
*
* @param {number} pos The starting position of the token.
* @param {number} length The length of the token.
* @param {number} score The score of the token.
* @param {number} tokenId The token ID of the token.
*/
insert(pos, length, score, tokenId) {
const nodeId = this.nodes.length;
const node = new TokenLatticeNode(tokenId, nodeId, pos, length, score);
this.beginNodes[pos].push(node);
this.endNodes[pos + length].push(node);
this.nodes.push(node);
}
/**
* Implements the Viterbi algorithm to compute the most likely sequence of tokens.
*
* @returns {TokenLatticeNode[]} The array of nodes representing the most likely sequence of tokens.
*/
viterbi() {
const len = this.len;
let pos = 0;
while (pos <= len) {
if (this.beginNodes[pos].length == 0) {
return [];
}
for (let rnode of this.beginNodes[pos]) {
rnode.prev = null;
let bestScore = 0.0;
let bestNode = null;
for (let lnode of this.endNodes[pos]) {
const score = lnode.backtraceScore + rnode.score;
if (bestNode === null || score > bestScore) {
bestNode = lnode.clone();
bestScore = score;
}
}
if (bestNode !== null) {
rnode.prev = bestNode;
rnode.backtraceScore = bestScore;
} else {
return [];
}
}
++pos;
}
const results = [];
const root = this.beginNodes[len][0];
const prev = root.prev;
if (prev === null) {
return [];
}
let node = prev.clone();
while (node.prev !== null) {
results.push(node.clone());
const n = node.clone();
node = n.prev.clone();
}
results.reverse();
return results;
}
/**
* @param {TokenLatticeNode} node
* @returns {string} The array of nodes representing the most likely sequence of tokens.
*/
piece(node) {
return this.sentence.slice(node.pos, node.pos + node.length);
}
/**
* @returns {Array} The array of nodes representing the most likely sequence of tokens.
*/
tokens() {
const nodes = this.viterbi();
return nodes.map(x => this.piece(x));
}
/**
* @returns {Array} The array of nodes representing the most likely sequence of tokens.
*/
tokenIds() {
const nodes = this.viterbi();
return nodes.map(x => x.tokenId);
}
}
class TokenLatticeNode {
/**
* Represents a node in a token lattice for a given sentence.
* @param {number} tokenId The ID of the token associated with this node.
* @param {number} nodeId The ID of this node.
* @param {number} pos The starting position of the token in the sentence.
* @param {number} length The length of the token.
* @param {number} score The score associated with the token.
*/
constructor(tokenId, nodeId, pos, length, score) {
this.tokenId = tokenId;
this.nodeId = nodeId;
this.pos = pos;
this.length = length;
this.score = score;
this.prev = null;
this.backtraceScore = 0.0;
}
/**
* Returns a clone of this node.
* @returns {TokenLatticeNode} A clone of this node.
*/
clone() {
const n = new TokenLatticeNode(this.tokenId, this.nodeId, this.pos, this.length, this.score);
n.prev = this.prev;
n.backtraceScore = this.backtraceScore;
return n;
}
}

View File

@@ -0,0 +1,873 @@
/**
* @file Classes, functions, and utilities for generation.
*
* @todo Describe how to create a custom `GenerationConfig`.
*
* @module utils/generation
*/
import { Tensor } from './tensor.js';
import {
Callable,
exists,
} from './core.js';
import {
max,
softmax,
log_softmax,
getTopItems,
} from './maths.js';
/**
* A class representing a list of logits processors. A logits processor is a function that modifies the logits
* output of a language model. This class provides methods for adding new processors and applying all processors to a
* batch of logits.
*
* @extends Callable
*/
export class LogitsProcessorList extends 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 {number[]} input_ids The input IDs for the language model.
* @param {number[][]} batchedLogits A 2D array of logits, where each row corresponds to a single
* input sequence in the batch.
*/
_call(input_ids, batchedLogits) {
// NOTE: This is different from the Python code, since vanilla JS does not support vectorized operations.
// As a result, we apply each processor to each item in the batch.
for (let logits of batchedLogits) {
// Modifies logits inplace
this.processors.forEach(
func => func(input_ids, logits)
)
}
}
[Symbol.iterator]() {
return this.processors.values();
}
}
/**
* Base class for processing logits.
* @extends Callable
*/
export class LogitsProcessor extends Callable {
/**
* Apply the processor to the input logits.
*
* @abstract
* @param {Array} input_ids The input ids.
* @param {Tensor} logits The logits to process.
* @throws {Error} Throws an error if `_call` is not implemented in the subclass.
*/
_call(input_ids, logits) {
throw Error("`_call` should be implemented in a subclass")
}
}
/**
* A logits processor that forces a specific token to be generated by the decoder.
*
* @extends LogitsProcessor
*/
export class ForceTokensLogitsProcessor extends LogitsProcessor {
/**
* Constructs a new instance of `ForceTokensLogitsProcessor`.
*
* @param {Array} forced_decoder_ids The ids of tokens that should be forced.
*/
constructor(forced_decoder_ids) {
super();
this.force_token_map = Object.fromEntries(forced_decoder_ids ?? []);
}
/**
* Apply the processor to the input logits.
*
* @param {Array} input_ids The input ids.
* @param {Tensor} logits The logits to process.
* @returns {Tensor} The processed logits.
*/
_call(input_ids, logits) {
let map = this.force_token_map[input_ids.length];
if (exists(map)) { // There exists a mapping
logits.data.fill(-Infinity)
logits.data[map] = 0;
}
return logits;
}
}
/**
* A LogitsProcessor that forces a BOS token at the beginning of the generated sequence.
* @extends LogitsProcessor
*/
export 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 {Array} input_ids The input IDs.
* @param {Object} logits The logits.
* @returns {Object} The logits with BOS token forcing.
*/
_call(input_ids, logits) {
if (input_ids.length === 1) {
logits.data.fill(-Infinity)
logits.data[this.bos_token_id] = 0;
}
return logits;
}
}
/**
* A logits processor that forces end-of-sequence token probability to 1.
*
* @extends LogitsProcessor
*/
export class ForcedEOSTokenLogitsProcessor extends LogitsProcessor {
/**
* Create a ForcedEOSTokenLogitsProcessor.
* @param {number} max_length Max length of the sequence.
* @param {number|number[]} forced_eos_token_id The ID of the end-of-sequence token to be forced.
*/
constructor(max_length, forced_eos_token_id) {
super();
this.max_length = max_length;
this.forced_eos_token_id = forced_eos_token_id;
}
/**
* Apply the processor to input_ids and logits.
*
* @param {number[]} input_ids The input ids.
* @param {Tensor} logits The logits tensor.
*/
_call(input_ids, logits) {
// console.log('call ForcedEOSTokenLogitsProcessor')
// TODO
}
}
/**
* A LogitsProcessor that suppresses a list of tokens as soon as the `generate` function starts
* generating using `begin_index` tokens. This should ensure that the tokens defined by
* `begin_suppress_tokens` at not sampled at the begining of the generation.
* @extends LogitsProcessor
*/
export 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 {Array} input_ids The input IDs.
* @param {Object} logits The logits.
* @returns {Object} The logits with BOS token forcing.
*/
_call(input_ids, logits) {
if (input_ids.length === this.begin_index) {
for (let token_id of this.begin_suppress_tokens) {
logits.data[token_id] = -Infinity;
}
}
return logits;
}
}
/**
* A LogitsProcessor that handles adding timestamps to generated text.
* @extends LogitsProcessor
*/
export class WhisperTimeStampLogitsProcessor extends LogitsProcessor {
/**
* Constructs a new WhisperTimeStampLogitsProcessor.
* @param {Object} generate_config The config object passed to the `generate()` method of a transformer model.
* @param {number} generate_config.eos_token_id The ID of the end-of-sequence token.
* @param {number} generate_config.no_timestamps_token_id The ID of the token used to indicate that a token should not have a timestamp.
* @param {number[][]} [generate_config.forced_decoder_ids] An array of two-element arrays representing decoder IDs that are forced to appear in the output. The second element of each array indicates whether the token is a timestamp.
* @param {number} [generate_config.max_initial_timestamp_index] The maximum index at which an initial timestamp can appear.
*/
constructor(generate_config) {
super();
this.eos_token_id = 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 = (generate_config.forced_decoder_ids || []).length + 2;
if (generate_config.forced_decoder_ids.slice(-1)[0][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 {Array} 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) {
const logitsData = /** @type {Float32Array} */(logits.data);
// suppress <|notimestamps|> which is handled by without_timestamps
logitsData[this.no_timestamps_token_id] = -Infinity;
if (input_ids.length === this.begin_index - 1) {
logitsData.fill(-Infinity);
logitsData[this.timestamp_begin] = 0;
return logits;
}
// timestamps have to appear in pairs, except directly before eos_token; mask logits accordingly
const seq = input_ids.slice(this.begin_index);
const last_was_timestamp = seq.length >= 1 && seq[seq.length - 1] >= this.timestamp_begin;
const penultimate_was_timestamp = seq.length < 2 || seq[seq.length - 2] >= this.timestamp_begin;
if (last_was_timestamp) {
if (penultimate_was_timestamp) { // has to be non-timestamp
logitsData.subarray(this.timestamp_begin).fill(-Infinity);
} else { // cannot be normal text tokens
logitsData.subarray(0, this.eos_token_id).fill(-Infinity);
}
}
// apply the `max_initial_timestamp` option
if (input_ids.length === this.begin_index && this.max_initial_timestamp_index !== null) {
const last_allowed = this.timestamp_begin + this.max_initial_timestamp_index;
logitsData.subarray(last_allowed + 1).fill(-Infinity);
}
// if sum of probability over timestamps is above any other token, sample timestamp
const logprobs = log_softmax(logitsData);
const timestamp_logprob = Math.log(logprobs.subarray(this.timestamp_begin).map(Math.exp).reduce((a, b) => a + b));
const max_text_token_logprob = max(logprobs.subarray(0, this.timestamp_begin))[0];
if (timestamp_logprob > max_text_token_logprob) {
logitsData.subarray(0, this.timestamp_begin).fill(-Infinity);
}
return logits;
}
}
/**
* A logits processor that disallows ngrams of a certain size to be repeated.
*
* @extends LogitsProcessor
*/
export 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 {number[]} prevInputIds List of previous input ids
* @returns {Map<string, number[]>} Map of generated n-grams
*/
getNgrams(prevInputIds) {
const curLen = prevInputIds.length;
/**@type {number[][]} */
const ngrams = [];
for (let j = 0; j < curLen + 1 - this.no_repeat_ngram_size; ++j) {
const ngram = [];
for (let k = 0; k < this.no_repeat_ngram_size; ++k) {
ngram.push(prevInputIds[j + k]);
}
ngrams.push(ngram);
}
/** @type {Map<string, number[]>} */
const generatedNgram = new Map();
for (const ngram of ngrams) {
const prevNgram = ngram.slice(0, ngram.length - 1);
const prevNgramKey = JSON.stringify(prevNgram);
const prevNgramValue = generatedNgram.get(prevNgramKey) ?? [];
prevNgramValue.push(ngram[ngram.length - 1]);
generatedNgram.set(prevNgramKey, prevNgramValue);
}
return generatedNgram;
}
/**
* Generate n-grams from a sequence of token ids.
* @param {Map<string, number[]>} bannedNgrams Map of banned n-grams
* @param {number[]} 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)) ?? [];
return banned;
}
/**
* Calculate banned n-gram tokens
* @param {number[]} prevInputIds List of previous input ids
* @returns {number[]} Map of generated n-grams
*/
calcBannedNgramTokens(prevInputIds) {
const bannedTokens = [];
if (prevInputIds.length + 1 < this.no_repeat_ngram_size) {
// return no banned tokens if we haven't generated no_repeat_ngram_size tokens yet
return bannedTokens;
} else {
const generatedNgrams = this.getNgrams(prevInputIds);
const bannedTokens = this.getGeneratedNgrams(generatedNgrams, prevInputIds);
return bannedTokens;
}
}
/**
* Apply the no-repeat-ngram processor to the logits.
* @param {Array} input_ids The input IDs.
* @param {Object} logits The logits.
* @returns {Object} The logits with no-repeat-ngram processing.
*/
_call(input_ids, logits) {
const bannedTokens = this.calcBannedNgramTokens(input_ids);
for (const token of bannedTokens) {
logits.data[token] = -Infinity;
}
return logits;
}
}
/**
* A logits processor that penalises repeated output tokens.
*
* @extends LogitsProcessor
*/
export 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 {Array} input_ids The input IDs.
* @param {Object} logits The logits.
* @returns {Object} The logits with repetition penalty processing.
*/
_call(input_ids, logits) {
// Modify the logits corresponding to each element in `input_ids`.
// As a consequence, the logits corresponding to tokens that appear
// many times in the output will be penalised more.
for (const input_id of input_ids) {
if (logits.data[input_id] < 0) {
logits.data[input_id] *= this.penalty;
} else {
logits.data[input_id] /= this.penalty;
}
}
return logits
}
}
/**
* A logits processor that enforces a minimum number of tokens.
*
* @extends LogitsProcessor
*/
export 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 {Array} input_ids The input IDs.
* @param {Object} logits The logits.
* @returns {Object} The processed logits.
*/
_call(input_ids, logits) {
if (input_ids.length < this.min_length) {
for (const eos_token of this.eos_token_id) {
logits.data[eos_token] = -Infinity;
}
}
return logits
}
}
/**
* A logits processor that enforces a minimum number of new tokens.
*
* @extends LogitsProcessor
*/
export 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 {Array} input_ids The input IDs.
* @param {Object} logits The logits.
* @returns {Object} The processed logits.
*/
_call(input_ids, logits) {
const new_tokens_length = input_ids.length - this.prompt_length_to_skip;
if (new_tokens_length < this.min_new_tokens) {
for (const eos_token of this.eos_token_id) {
logits.data[eos_token] = -Infinity;
}
}
return logits
}
}
export 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 {Array} input_ids The input IDs.
* @param {Object} logits The logits.
* @returns {Object} The processed logits.
*/
_call(input_ids, logits) {
for (const bad_word_ids of this.bad_words_ids) {
// Whether to modify the logits of the last token in the bad word id sequence
let mark = true;
// For each bad word in the list, if the current sequence of input ids ends with this sequence (excluding the last),
// then we set the logits of the last bad word id to -Infinity.
for (let i = 1; i <= bad_word_ids.length - 1 && bad_word_ids.length < input_ids.length; ++i) {
if (bad_word_ids.at(-i - 1) !== input_ids.at(-i)) {
// We have found a mismatch
mark = false;
break;
}
}
if (mark) {
logits.data[bad_word_ids.at(-1)] = -Infinity;
}
}
return logits
}
}
/**
* @typedef {Object} GenerationConfigType The default configuration parameters.
* @property {number} [max_length=20] 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.
* @property {number} [max_new_tokens=null] The maximum numbers of tokens to generate, ignoring the number of tokens in the prompt.
* @property {number} [min_length=0] 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.
* @property {number} [min_new_tokens=null] The minimum numbers of tokens to generate, ignoring the number of tokens in the prompt.
* @property {boolean|"never"} [early_stopping=false] 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).
* @property {number} [max_time=null] 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.
*
* @property {boolean} [do_sample=false] Whether or not to use sampling; use greedy decoding otherwise.
* @property {number} [num_beams=1] Number of beams for beam search. 1 means no beam search.
* @property {number} [num_beam_groups=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.
* @property {number} [penalty_alpha=null] The values balance the model confidence and the degeneration penalty in contrastive search decoding.
* @property {boolean} [use_cache=true] Whether or not the model should use the past last key/values attentions (if applicable to the model) to speed up decoding.
*
* @property {number} [temperature=1.0] The value used to modulate the next token probabilities.
* @property {number} [top_k=50] The number of highest probability vocabulary tokens to keep for top-k-filtering.
* @property {number} [top_p=1.0] 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.
* @property {number} [typical_p=1.0] Local typicality measures how similar the conditional probability of predicting a target token next is to the expected conditional probability of predicting a random token next, given the partial text already generated. If set to float < 1, the smallest set of the most locally typical tokens with probabilities that add up to `typical_p` or higher are kept for generation. See [this paper](https://arxiv.org/pdf/2202.00666.pdf) for more details.
* @property {number} [epsilon_cutoff=0.0] If set to float strictly between 0 and 1, only tokens with a conditional probability greater than `epsilon_cutoff` will be sampled. In the paper, suggested values range from 3e-4 to 9e-4, depending on the size of the model. See [Truncation Sampling as Language Model Desmoothing](https://arxiv.org/abs/2210.15191) for more details.
* @property {number} [eta_cutoff=0.0] Eta sampling is a hybrid of locally typical sampling and epsilon sampling. If set to float strictly between 0 and 1, a token is only considered if it is greater than either `eta_cutoff` or `sqrt(eta_cutoff) * exp(-entropy(softmax(next_token_logits)))`. The latter term is intuitively the expected next token probability, scaled by `sqrt(eta_cutoff)`. In the paper, suggested values range from 3e-4 to 2e-3, depending on the size of the model. See [Truncation Sampling as Language Model Desmoothing](https://arxiv.org/abs/2210.15191) for more details.
* @property {number} [diversity_penalty=0.0] This value is subtracted from a beam's score if it generates a token same as any beam from other group at a particular time. Note that `diversity_penalty` is only effective if `group beam search` is enabled.
* @property {number} [repetition_penalty=1.0] The parameter for repetition penalty. 1.0 means no penalty. See [this paper](https://arxiv.org/pdf/1909.05858.pdf) for more details.
* @property {number} [encoder_repetition_penalty=1.0] The paramater for encoder_repetition_penalty. An exponential penalty on sequences that are not in the original input. 1.0 means no penalty.
* @property {number} [length_penalty=1.0] Exponential penalty to the length that is used with beam-based generation. It is applied as an exponent to the sequence length, which in turn is used to divide the score of the sequence. Since the score is the log likelihood of the sequence (i.e. negative), `length_penalty` > 0.0 promotes longer sequences, while `length_penalty` < 0.0 encourages shorter sequences.
* @property {number} [no_repeat_ngram_size=0] If set to int > 0, all ngrams of that size can only occur once.
* @property {number[][]} [bad_words_ids=null] 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 `(await tokenizer(bad_words, {add_prefix_space: true, add_special_tokens: false})).input_ids`.
* @property {number[][]|number[][][]} [force_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.
* @property {boolean} [renormalize_logits=false] 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.
* @property {Object[]} [constraints=null] 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.
*
* @property {number} [forced_bos_token_id=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.
* @property {number|number[]} [forced_eos_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.
* @property {boolean} [remove_invalid_values=false] 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.
* @property {number[]} [exponential_decay_length_penalty=null] 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.
* @property {number[]} [suppress_tokens=null] A list of tokens that will be suppressed at generation. The `SupressTokens` logit processor will set their log probs to `-inf` so that they are not sampled.
* @property {number[]} [begin_suppress_tokens=null] A list of tokens that will be suppressed at the beginning of the generation. The `SupressBeginTokens` logit processor will set their log probs to `-inf` so that they are not sampled.
* @property {number[][]} [forced_decoder_ids=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.
*
* @property {number} [num_return_sequences=1] The number of independently computed returned sequences for each element in the batch.
* @property {boolean} [output_attentions=false] Whether or not to return the attentions tensors of all attention layers. See `attentions` under returned tensors for more details.
* @property {boolean} [output_hidden_states=false] Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors for more details.
* @property {boolean} [output_scores=false] Whether or not to return the prediction scores. See `scores` under returned tensors for more details.
* @property {boolean} [return_dict_in_generate=false] Whether or not to return a `ModelOutput` instead of a plain tuple.
*
* @property {number} [pad_token_id=null] The id of the *padding* token.
* @property {number} [bos_token_id=null] The id of the *beginning-of-sequence* token.
* @property {number|number[]} [eos_token_id=null] The id of the *end-of-sequence* token. Optionally, use a list to set multiple *end-of-sequence* tokens.
*
* @property {number} [encoder_no_repeat_ngram_size=0] If set to int > 0, all ngrams of that size that occur in the `encoder_input_ids` cannot occur in the `decoder_input_ids`.
* @property {number} [decoder_start_token_id=null] If an encoder-decoder model starts decoding with a different token than *bos*, the id of that token.
*
* @property {Object} [generation_kwargs={}] 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.
*/
/**
* Class that holds a configuration for a generation task.
* @type {new (kwargs?: GenerationConfigType) => GenerationConfigType}
*/
export const GenerationConfig = /** @type {any} */ (class {
/**
* Create a new GenerationConfig object.
* @param {GenerationConfigType} kwargs
*/
constructor(kwargs = {}) {
// Parameters that control the length of the output
this.max_length = kwargs.max_length ?? 20;
this.max_new_tokens = kwargs.max_new_tokens ?? null;
this.min_length = kwargs.min_length ?? 0;
this.min_new_tokens = kwargs.min_new_tokens ?? null;
this.early_stopping = kwargs.early_stopping ?? false;
this.max_time = kwargs.max_time ?? null;
// Parameters that control the generation strategy used
this.do_sample = kwargs.do_sample ?? false;
this.num_beams = kwargs.num_beams ?? 1;
this.num_beam_groups = kwargs.num_beam_groups ?? 1;
this.penalty_alpha = kwargs.penalty_alpha ?? null;
this.use_cache = kwargs.use_cache ?? true;
// Parameters for manipulation of the model output logits
this.temperature = kwargs.temperature ?? 1.0;
this.top_k = kwargs.top_k ?? 50;
this.top_p = kwargs.top_p ?? 1.0;
this.typical_p = kwargs.typical_p ?? 1.0;
this.epsilon_cutoff = kwargs.epsilon_cutoff ?? 0.0;
this.eta_cutoff = kwargs.eta_cutoff ?? 0.0;
this.diversity_penalty = kwargs.diversity_penalty ?? 0.0;
this.repetition_penalty = kwargs.repetition_penalty ?? 1.0;
this.encoder_repetition_penalty = kwargs.encoder_repetition_penalty ?? 1.0;
this.length_penalty = kwargs.length_penalty ?? 1.0;
this.no_repeat_ngram_size = kwargs.no_repeat_ngram_size ?? 0;
this.bad_words_ids = kwargs.bad_words_ids ?? null;
this.force_words_ids = kwargs.force_words_ids ?? null;
this.renormalize_logits = kwargs.renormalize_logits ?? false;
this.constraints = kwargs.constraints ?? null;
this.forced_bos_token_id = kwargs.forced_bos_token_id ?? null;
this.forced_eos_token_id = kwargs.forced_eos_token_id ?? null;
this.remove_invalid_values = kwargs.remove_invalid_values ?? false;
this.exponential_decay_length_penalty = kwargs.exponential_decay_length_penalty ?? null;
this.suppress_tokens = kwargs.suppress_tokens ?? null;
this.begin_suppress_tokens = kwargs.begin_suppress_tokens ?? null;
this.forced_decoder_ids = kwargs.forced_decoder_ids ?? null;
// Parameters that define the output variables of `generate`
this.num_return_sequences = kwargs.num_return_sequences ?? 1;
this.output_attentions = kwargs.output_attentions ?? false;
this.output_hidden_states = kwargs.output_hidden_states ?? false;
this.output_scores = kwargs.output_scores ?? false;
this.return_dict_in_generate = kwargs.return_dict_in_generate ?? false;
// Special tokens that can be used at generation time
this.pad_token_id = kwargs.pad_token_id ?? null;
this.bos_token_id = kwargs.bos_token_id ?? null;
this.eos_token_id = kwargs.eos_token_id ?? null;
// Generation parameters exclusive to encoder-decoder models
this.encoder_no_repeat_ngram_size = kwargs.encoder_no_repeat_ngram_size ?? 0;
this.decoder_start_token_id = kwargs.decoder_start_token_id ?? null;
// Wild card
this.generation_kwargs = kwargs.generation_kwargs ?? {};
}
});
/**
* Sampler is a base class for all sampling methods used for text generation.
*/
export class Sampler extends Callable {
/**
* Creates a new Sampler object with the specified generation config.
* @param {GenerationConfigType} generation_config The generation config.
*/
constructor(generation_config) {
super();
this.generation_config = generation_config;
}
/**
* Executes the sampler, using the specified logits.
* @param {Tensor} logits
* @param {number} index
* @returns {void}
*/
_call(logits, index = -1) {
// Sample from logits, of dims [batch, sequence_length, vocab_size].
// If index is specified, sample from [batch, index, vocab_size].
return this.sample(logits, index);
}
/**
* Abstract method for sampling the logits.
* @param {Tensor} logits
* @param {number} index
* @throws {Error}
*/
sample(logits, index) {
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);
}
// add temperature
if (this.generation_config.temperature > 0) {
logs = logs.map(x => x / this.generation_config.temperature)
}
return logs;
}
/**
* Selects an item randomly based on the specified probabilities.
* @param {Array} probabilities An array of probabilities to use for selection.
* @returns {number} The index of the selected item.
*/
randomSelect(probabilities) {
// Return index of chosen item
let sumProbabilities = probabilities.reduce((acc, curr) => acc + curr, 0);
let r = Math.random() * sumProbabilities;
for (let i = 0; i < probabilities.length; ++i) {
r -= probabilities[i];
if (r <= 0) {
return i;
}
}
return 0; // return first (most probable) as a fallback
}
/**
* Returns a Sampler object based on the specified options.
* @param {GenerationConfigType} generation_config An object containing options for the sampler.
* @returns {Sampler} A Sampler object.
*/
static getSampler(generation_config) {
// - *greedy decoding*: `num_beams=1` and `do_sample=False`
// - *contrastive search*: `penalty_alpha>0` and `top_k>1`
// - *multinomial sampling*: `num_beams=1` and `do_sample=True`
// - *beam-search decoding*: `num_beams>1` and `do_sample=False`
// - *beam-search multinomial sampling*: `num_beams>1` and `do_sample=True`
// - *diverse beam-search decoding*: `num_beams>1` and `num_beam_groups>1`
// - *constrained beam-search decoding*: `constraints!=None` or `force_words_ids!=None`
// NOTE: beam search is implemented directly into the generation function
if (generation_config.do_sample) {
return new MultinomialSampler(generation_config);
} else if (generation_config.num_beams > 1) {
return new BeamSearchSampler(generation_config);
} else {
if (generation_config.num_return_sequences > 1) {
throw Error(`num_return_sequences has to be 1 when doing greedy search, but is ${generation_config.num_return_sequences}.`)
}
return new GreedySampler(generation_config);
}
}
}
/**
* Class representing a Greedy Sampler.
* @extends Sampler
*/
class GreedySampler extends Sampler {
/**
* Sample the maximum probability of a given logits tensor.
* @param {Tensor} logits
* @param {number} [index=-1]
* @returns {Array} An array with a single tuple, containing the index of the maximum value and a meaningless score (since this is a greedy search).
*/
sample(logits, index = -1) {
// NOTE: no need to do log_softmax here since we only take the maximum
let logs = this.getLogits(logits, index);
let argmax = max(logs)[1];
// Note: score is meaningless in this context, since we are performing
// greedy search (p = 1 => log(p) = 0)
return [
[argmax, 0]
];
}
}
/**
* Class representing a MultinomialSampler.
* @extends Sampler
*/
class MultinomialSampler extends Sampler {
/**
* Sample from the logits.
* @param {Tensor} logits
* @param {number} index
* @returns {Array}
*/
sample(logits, index = -1) {
let k = logits.dims.at(-1); // defaults to vocab size
if (this.generation_config.top_k > 0) {
k = Math.min(this.generation_config.top_k, k);
}
// Get logits of nth token
const logs = this.getLogits(logits, index);
// Get top k tokens
const topLogits = getTopItems(logs, k);
// Compute softmax over logits
const probabilities = softmax(topLogits.map(x => x[1]));
return Array.from({ length: this.generation_config.num_beams }, () => {
const sampledIndex = this.randomSelect(probabilities);
return [
topLogits[sampledIndex][0], // token id
Math.log(probabilities[sampledIndex]), // score
];
});
}
}
/**
* Class representing a BeamSearchSampler.
* @extends Sampler
*/
class BeamSearchSampler extends Sampler {
/**
* Sample from the logits.
* @param {Tensor} logits
* @param {number} index
* @returns {Array}
*/
sample(logits, index = -1) {
let k = logits.dims.at(-1); // defaults to vocab size
if (this.generation_config.top_k > 0) {
k = Math.min(this.generation_config.top_k, k);
}
// Get logits of nth token
const logs = this.getLogits(logits, index);
// Get top k tokens
const topLogits = getTopItems(logs, k);
// Compute softmax over logits
const probabilities = softmax(topLogits.map(x => x[1]));
return Array.from({ length: this.generation_config.num_beams }, (_, i) => {
return [
topLogits[i][0], // token id
Math.log(probabilities[i]), // score
];
});
}
}

658
node_modules/@xenova/transformers/src/utils/hub.js generated vendored Normal file
View File

@@ -0,0 +1,658 @@
/**
* @file Utility functions to interact with the Hugging Face Hub (https://huggingface.co/models)
*
* @module utils/hub
*/
import fs from 'fs';
import path from 'path';
import { env } from '../env.js';
import { dispatchCallback } from './core.js';
/**
* @typedef {Object} PretrainedOptions Options for loading a pretrained model.
* @property {boolean?} [quantized=true] Whether to load the 8-bit quantized version of the model (only applicable when loading model files).
* @property {function} [progress_callback=null] If specified, this function will be called during model construction, to provide the user with progress updates.
* @property {Object} [config=null] Configuration for the model to use instead of an automatically loaded configuration. Configuration can be automatically loaded when:
* - The model is a model provided by the library (loaded with the *model id* string of a pretrained model).
* - The model is loaded by supplying a local directory as `pretrained_model_name_or_path` and a configuration JSON file named *config.json* is found in the directory.
* @property {string} [cache_dir=null] Path to a directory in which a downloaded pretrained model configuration should be cached if the standard cache should not be used.
* @property {boolean} [local_files_only=false] Whether or not to only look at local files (e.g., not try downloading the model).
* @property {string} [revision='main'] The specific model version to use. It can be a branch name, a tag name, or a commit id,
* since we use a git-based system for storing models and other artifacts on huggingface.co, so `revision` can be any identifier allowed by git.
* NOTE: This setting is ignored for local requests.
* @property {string} [model_file_name=null] If specified, load the model with this name (excluding the .onnx suffix). Currently only valid for encoder- or decoder-only models.
*/
class FileResponse {
/**
* Mapping from file extensions to MIME types.
*/
_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',
}
/**
* Creates a new `FileResponse` object.
* @param {string|URL} filePath
*/
constructor(filePath) {
this.filePath = filePath;
this.headers = new Headers();
this.exists = fs.existsSync(filePath);
if (this.exists) {
this.status = 200;
this.statusText = 'OK';
let stats = fs.statSync(filePath);
this.headers.set('content-length', stats.size.toString());
this.updateContentType();
let self = this;
this.body = new ReadableStream({
start(controller) {
self.arrayBuffer().then(buffer => {
controller.enqueue(new Uint8Array(buffer));
controller.close();
})
}
});
} else {
this.status = 404;
this.statusText = 'Not Found';
this.body = null;
}
}
/**
* Updates the 'content-type' header property of the response based on the extension of
* the file specified by the filePath property of the current object.
* @returns {void}
*/
updateContentType() {
// Set content-type header based on file extension
const extension = this.filePath.toString().split('.').pop().toLowerCase();
this.headers.set('content-type', this._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<ArrayBuffer>} 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.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<Blob>} 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.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<string>} 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.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<Object>} A Promise that resolves with a parsed JavaScript object containing the file's contents.
* @throws {Error} If the file cannot be read.
*/
async json() {
return JSON.parse(await this.text());
}
}
/**
* Determines whether the given string is a valid URL.
* @param {string|URL} string The string to test for validity as an URL.
* @param {string[]} [protocols=null] A list of valid protocols. If specified, the protocol must be in this list.
* @param {string[]} [validHosts=null] A list of valid hostnames. If specified, the URL's hostname must be in this list.
* @returns {boolean} True if the string is a valid URL, false otherwise.
*/
function isValidUrl(string, protocols = null, validHosts = null) {
let url;
try {
url = new URL(string);
} catch (_) {
return false;
}
if (protocols && !protocols.includes(url.protocol)) {
return false;
}
if (validHosts && !validHosts.includes(url.hostname)) {
return false;
}
return true;
}
/**
* Helper function to get a file, using either the Fetch API or FileSystem API.
*
* @param {URL|string} urlOrPath The URL/path of the file to get.
* @returns {Promise<FileResponse|Response>} A promise that resolves to a FileResponse object (if the file is retrieved using the FileSystem API), or a Response object (if the file is retrieved using the Fetch API).
*/
export async function getFile(urlOrPath) {
if (env.useFS && !isValidUrl(urlOrPath, ['http:', 'https:', 'blob:'])) {
return new FileResponse(urlOrPath);
} else if (typeof process !== 'undefined' && process?.release?.name === 'node') {
const IS_CI = !!process.env?.TESTING_REMOTELY;
const version = env.version;
const headers = new Headers();
headers.set('User-Agent', `transformers.js/${version}; is_ci/${IS_CI};`);
// Check whether we are making a request to the Hugging Face Hub.
const isHFURL = isValidUrl(urlOrPath, ['http:', 'https:'], ['huggingface.co', 'hf.co']);
if (isHFURL) {
// If an access token is present in the environment variables,
// we add it to the request headers.
// NOTE: We keep `HF_ACCESS_TOKEN` for backwards compatibility (as a fallback).
const token = process.env?.HF_TOKEN ?? process.env?.HF_ACCESS_TOKEN;
if (token) {
headers.set('Authorization', `Bearer ${token}`);
}
}
return fetch(urlOrPath, { headers });
} else {
// Running in a browser-environment, so we use default headers
// NOTE: We do not allow passing authorization headers in the browser,
// since this would require exposing the token to the client.
return fetch(urlOrPath);
}
}
const ERROR_MAPPING = {
// 4xx errors (https://developer.mozilla.org/en-US/docs/Web/HTTP/Status#client_error_responses)
400: 'Bad request error occurred while trying to load file',
401: 'Unauthorized access to file',
403: 'Forbidden access to file',
404: 'Could not locate file',
408: 'Request timeout error occurred while trying to load file',
// 5xx errors (https://developer.mozilla.org/en-US/docs/Web/HTTP/Status#server_error_responses)
500: 'Internal server error error occurred while trying to load file',
502: 'Bad gateway error occurred while trying to load file',
503: 'Service unavailable error occurred while trying to load file',
504: 'Gateway timeout error occurred while trying to load file',
}
/**
* Helper method to handle fatal errors that occur while trying to load a file from the Hugging Face Hub.
* @param {number} status The HTTP status code of the error.
* @param {string} remoteURL The URL of the file that could not be loaded.
* @param {boolean} fatal Whether to raise an error if the file could not be loaded.
* @returns {null} Returns `null` if `fatal = true`.
* @throws {Error} If `fatal = false`.
*/
function handleError(status, remoteURL, fatal) {
if (!fatal) {
// File was not loaded correctly, but it is optional.
// TODO in future, cache the response?
return null;
}
const message = ERROR_MAPPING[status] ?? `Error (${status}) occurred while trying to load file`;
throw Error(`${message}: "${remoteURL}".`);
}
class FileCache {
/**
* Instantiate a `FileCache` object.
* @param {string} path
*/
constructor(path) {
this.path = path;
}
/**
* Checks whether the given request is in the cache.
* @param {string} request
* @returns {Promise<FileResponse | undefined>}
*/
async match(request) {
let filePath = path.join(this.path, request);
let file = new FileResponse(filePath);
if (file.exists) {
return file;
} else {
return undefined;
}
}
/**
* Adds the given response to the cache.
* @param {string} request
* @param {Response|FileResponse} response
* @returns {Promise<void>}
*/
async put(request, response) {
const buffer = Buffer.from(await response.arrayBuffer());
let outputPath = path.join(this.path, request);
try {
await fs.promises.mkdir(path.dirname(outputPath), { recursive: true });
await fs.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<void>;
// delete(request: RequestInfo | URL, options?: CacheQueryOptions): Promise<boolean>;
// keys(request?: RequestInfo | URL, options?: CacheQueryOptions): Promise<ReadonlyArray<Request>>;
// match(request: RequestInfo | URL, options?: CacheQueryOptions): Promise<Response | undefined>;
// matchAll(request?: RequestInfo | URL, options?: CacheQueryOptions): Promise<ReadonlyArray<Response>>;
}
/**
*
* @param {FileCache|Cache} cache The cache to search
* @param {string[]} names The names of the item to search for
* @returns {Promise<FileResponse|Response|undefined>} The item from the cache, or undefined if not found.
*/
async function tryCache(cache, ...names) {
for (let name of names) {
try {
let result = await cache.match(name);
if (result) return result;
} catch (e) {
continue;
}
}
return undefined;
}
/**
*
* Retrieves a file from either a remote URL using the Fetch API or from the local file system using the FileSystem API.
* If the filesystem is available and `env.useCache = true`, the file will be downloaded and cached.
*
* @param {string} path_or_repo_id This can be either:
* - a string, the *model id* of a model repo on huggingface.co.
* - a path to a *directory* potentially containing the file.
* @param {string} filename The name of the file to locate in `path_or_repo`.
* @param {boolean} [fatal=true] Whether to throw an error if the file is not found.
* @param {PretrainedOptions} [options] An object containing optional parameters.
*
* @throws Will throw an error if the file is not found and `fatal` is true.
* @returns {Promise} A Promise that resolves with the file content as a buffer.
*/
export async function getModelFile(path_or_repo_id, filename, fatal = true, options = {}) {
if (!env.allowLocalModels) {
// User has disabled local models, so we just make sure other settings are correct.
if (options.local_files_only) {
throw Error("Invalid configuration detected: local models are disabled (`env.allowLocalModels=false`) but you have requested to only use local models (`local_files_only=true`).")
} else if (!env.allowRemoteModels) {
throw Error("Invalid configuration detected: both local and remote models are disabled. Fix by setting `env.allowLocalModels` or `env.allowRemoteModels` to `true`.")
}
}
// Initiate file retrieval
dispatchCallback(options.progress_callback, {
status: 'initiate',
name: path_or_repo_id,
file: filename
})
// First, check if the a caching backend is available
// If no caching mechanism available, will download the file every time
let cache;
if (!cache && env.useBrowserCache) {
if (typeof caches === 'undefined') {
throw Error('Browser cache is not available in this environment.')
}
try {
// In some cases, the browser cache may be visible, but not accessible due to security restrictions.
// For example, when running an application in an iframe, if a user attempts to load the page in
// incognito mode, the following error is thrown: `DOMException: Failed to execute 'open' on 'CacheStorage':
// An attempt was made to break through the security policy of the user agent.`
// So, instead of crashing, we just ignore the error and continue without using the cache.
cache = await caches.open('transformers-cache');
} catch (e) {
console.warn('An error occurred while opening the browser cache:', e);
}
}
if (!cache && env.useFSCache) {
// TODO throw error if not available
// If `cache_dir` is not specified, use the default cache directory
cache = new FileCache(options.cache_dir ?? env.cacheDir);
}
if (!cache && env.useCustomCache) {
// Allow the user to specify a custom cache system.
if (!env.customCache) {
throw Error('`env.useCustomCache=true`, but `env.customCache` is not defined.')
}
// Check that the required methods are defined:
if (!env.customCache.match || !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.customCache;
}
const revision = options.revision ?? 'main';
let requestURL = pathJoin(path_or_repo_id, filename);
let localPath = pathJoin(env.localModelPath, requestURL);
let remoteURL = pathJoin(
env.remoteHost,
env.remotePathTemplate
.replaceAll('{model}', path_or_repo_id)
.replaceAll('{revision}', encodeURIComponent(revision)),
filename
);
// Choose cache key for filesystem cache
// When using the main revision (default), we use the request URL as the cache key.
// If a specific revision is requested, we account for this in the cache key.
let fsCacheKey = revision === 'main' ? requestURL : pathJoin(path_or_repo_id, revision, filename);
/** @type {string} */
let cacheKey;
let proposedCacheKey = cache instanceof FileCache ? fsCacheKey : remoteURL;
// Whether to cache the final response in the end.
let toCacheResponse = false;
/** @type {Response|FileResponse|undefined} */
let response;
if (cache) {
// A caching system is available, so we try to get the file from it.
// 1. We first try to get from cache using the local path. In some environments (like deno),
// non-URL cache keys are not allowed. In these cases, `response` will be undefined.
// 2. If no response is found, we try to get from cache using the remote URL or file system cache.
response = await tryCache(cache, localPath, proposedCacheKey);
}
const cacheHit = response !== undefined;
if (response === undefined) {
// Caching not available, or file is not cached, so we perform the request
if (env.allowLocalModels) {
// Accessing local models is enabled, so we try to get the file locally.
// If request is a valid HTTP URL, we skip the local file check. Otherwise, we try to get the file locally.
const isURL = isValidUrl(requestURL, ['http:', 'https:']);
if (!isURL) {
try {
response = await getFile(localPath);
cacheKey = localPath; // Update the cache key to be the local path
} catch (e) {
// Something went wrong while trying to get the file locally.
// NOTE: error handling is done in the next step (since `response` will be undefined)
console.warn(`Unable to load from local path "${localPath}": "${e}"`);
}
} else if (options.local_files_only) {
throw new Error(`\`local_files_only=true\`, but attempted to load a remote file from: ${requestURL}.`);
} else if (!env.allowRemoteModels) {
throw new Error(`\`env.allowRemoteModels=false\`, but attempted to load a remote file from: ${requestURL}.`);
}
}
if (response === undefined || response.status === 404) {
// File not found locally. This means either:
// - The user has disabled local file access (`env.allowLocalModels=false`)
// - the path is a valid HTTP url (`response === undefined`)
// - the path is not a valid HTTP url and the file is not present on the file system or local server (`response.status === 404`)
if (options.local_files_only || !env.allowRemoteModels) {
// User requested local files only, but the file is not found locally.
if (fatal) {
throw Error(`\`local_files_only=true\` or \`env.allowRemoteModels=false\` and file was not found locally at "${localPath}".`);
} else {
// File not found, but this file is optional.
// TODO in future, cache the response?
return null;
}
}
// File not found locally, so we try to download it from the remote server
response = await getFile(remoteURL);
if (response.status !== 200) {
return handleError(response.status, remoteURL, fatal);
}
// Success! We use the proposed cache key from earlier
cacheKey = proposedCacheKey;
}
// Only cache the response if:
toCacheResponse =
cache // 1. A caching system is available
&& typeof Response !== 'undefined' // 2. `Response` is defined (i.e., we are in a browser-like environment)
&& response instanceof Response // 3. result is a `Response` object (i.e., not a `FileResponse`)
&& response.status === 200 // 4. request was successful (status code 200)
}
// Start downloading
dispatchCallback(options.progress_callback, {
status: 'download',
name: path_or_repo_id,
file: filename
})
const progressInfo = {
status: 'progress',
name: path_or_repo_id,
file: filename
}
/** @type {Uint8Array} */
let buffer;
if (!options.progress_callback) {
// If no progress callback is specified, we can use the `.arrayBuffer()`
// method to read the response.
buffer = new Uint8Array(await response.arrayBuffer());
} else if (
cacheHit // The item is being read from the cache
&&
typeof navigator !== 'undefined' && /firefox/i.test(navigator.userAgent) // We are in Firefox
) {
// Due to bug in Firefox, we cannot display progress when loading from cache.
// Fortunately, since this should be instantaneous, this should not impact users too much.
buffer = new Uint8Array(await response.arrayBuffer());
// For completeness, we still fire the final progress callback
dispatchCallback(options.progress_callback, {
...progressInfo,
progress: 100,
loaded: buffer.length,
total: buffer.length,
})
} else {
buffer = await readResponse(response, data => {
dispatchCallback(options.progress_callback, {
...progressInfo,
...data,
})
})
}
if (
// Only cache web responses
// i.e., do not cache FileResponses (prevents duplication)
toCacheResponse && cacheKey
&&
// Check again whether request is in cache. If not, we add the response to the cache
(await cache.match(cacheKey) === undefined)
) {
// NOTE: We use `new Response(buffer, ...)` instead of `response.clone()` to handle LFS files
await cache.put(cacheKey, new Response(buffer, {
headers: response.headers
}))
.catch(err => {
// Do not crash if unable to add to cache (e.g., QuotaExceededError).
// Rather, log a warning and proceed with execution.
console.warn(`Unable to add response to browser cache: ${err}.`);
});
}
dispatchCallback(options.progress_callback, {
status: 'done',
name: path_or_repo_id,
file: filename
});
return buffer;
}
/**
* Fetches a JSON file from a given path and file name.
*
* @param {string} modelPath The path to the directory containing the file.
* @param {string} fileName The name of the file to fetch.
* @param {boolean} [fatal=true] Whether to throw an error if the file is not found.
* @param {PretrainedOptions} [options] An object containing optional parameters.
* @returns {Promise<Object>} The JSON data parsed into a JavaScript object.
* @throws Will throw an error if the file is not found and `fatal` is true.
*/
export async function getModelJSON(modelPath, fileName, fatal = true, options = {}) {
let buffer = await getModelFile(modelPath, fileName, fatal, options);
if (buffer === null) {
// Return empty object
return {}
}
let decoder = new TextDecoder('utf-8');
let jsonData = decoder.decode(buffer);
return JSON.parse(jsonData);
}
/**
* Read and track progress when reading a Response object
*
* @param {any} response The Response object to read
* @param {function} progress_callback The function to call with progress updates
* @returns {Promise<Uint8Array>} A Promise that resolves with the Uint8Array buffer
*/
async function readResponse(response, progress_callback) {
const contentLength = response.headers.get('Content-Length');
if (contentLength === null) {
console.warn('Unable to determine content-length from response headers. Will expand buffer when needed.')
}
let total = parseInt(contentLength ?? '0');
let buffer = new Uint8Array(total);
let loaded = 0;
const reader = response.body.getReader();
async function read() {
const { done, value } = await reader.read();
if (done) return;
let newLoaded = loaded + value.length;
if (newLoaded > total) {
total = newLoaded;
// Adding the new data will overflow buffer.
// In this case, we extend the buffer
let newBuffer = new Uint8Array(total);
// copy contents
newBuffer.set(buffer);
buffer = newBuffer;
}
buffer.set(value, loaded)
loaded = newLoaded;
const progress = (loaded / total) * 100;
// Call your function here
progress_callback({
progress: progress,
loaded: loaded,
total: total,
})
return read();
}
// Actually read
await read();
return buffer;
}
/**
* Joins multiple parts of a path into a single path, while handling leading and trailing slashes.
*
* @param {...string} parts Multiple parts of a path.
* @returns {string} A string representing the joined path.
*/
function pathJoin(...parts) {
// https://stackoverflow.com/a/55142565
parts = parts.map((part, index) => {
if (index) {
part = part.replace(new RegExp('^/'), '');
}
if (index !== parts.length - 1) {
part = part.replace(new RegExp('/$'), '');
}
return part;
})
return parts.join('/');
}

731
node_modules/@xenova/transformers/src/utils/image.js generated vendored Normal file
View File

@@ -0,0 +1,731 @@
/**
* @file Helper module for image processing.
*
* These functions and classes are only used internally,
* meaning an end-user shouldn't need to access anything here.
*
* @module utils/image
*/
import { getFile } from './hub.js';
import { env } from '../env.js';
import { Tensor } from './tensor.js';
// Will be empty (or not used) if running in browser or web-worker
import sharp from 'sharp';
const BROWSER_ENV = typeof self !== 'undefined';
const WEBWORKER_ENV = BROWSER_ENV && self.constructor.name === 'DedicatedWorkerGlobalScope';
let createCanvasFunction;
let ImageDataClass;
let loadImageFunction;
if (BROWSER_ENV) {
// Running in browser or web-worker
createCanvasFunction = (/** @type {number} */ width, /** @type {number} */ height) => {
if (!self.OffscreenCanvas) {
throw new Error('OffscreenCanvas not supported by this browser.');
}
return new self.OffscreenCanvas(width, height)
};
loadImageFunction = self.createImageBitmap;
ImageDataClass = self.ImageData;
} else if (sharp) {
// Running in Node.js, electron, or other non-browser environment
loadImageFunction = async (/**@type {sharp.Sharp}*/img) => {
const metadata = await img.metadata();
const rawChannels = metadata.channels;
let { data, info } = await img.rotate().raw().toBuffer({ resolveWithObject: true });
const newImage = new RawImage(new Uint8ClampedArray(data), info.width, info.height, info.channels);
if (rawChannels !== undefined && rawChannels !== info.channels) {
// Make sure the new image has the same number of channels as the input image.
// This is necessary for grayscale images.
newImage.convert(rawChannels);
}
return newImage;
}
} else {
throw new Error('Unable to load image processing library.');
}
// Defined here: https://github.com/python-pillow/Pillow/blob/a405e8406b83f8bfb8916e93971edc7407b8b1ff/src/libImaging/Imaging.h#L262-L268
const RESAMPLING_MAPPING = {
0: 'nearest',
1: 'lanczos',
2: 'bilinear',
3: 'bicubic',
4: 'box',
5: 'hamming',
}
/**
* Mapping from file extensions to MIME types.
*/
const CONTENT_TYPE_MAP = new Map([
['png', 'image/png'],
['jpg', 'image/jpeg'],
['jpeg', 'image/jpeg'],
['gif', 'image/gif'],
]);
export 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 URL or file path.
* @param {string|URL} url The URL or file path to read the image from.
* @returns {Promise<RawImage>} The image object.
*/
static async fromURL(url) {
let response = await getFile(url);
if (response.status !== 200) {
throw new Error(`Unable to read image from "${url}" (${response.status} ${response.statusText})`);
}
let 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<RawImage>} The image object.
*/
static async fromBlob(blob) {
if (BROWSER_ENV) {
// Running in environment with canvas
let img = await loadImageFunction(blob);
const ctx = createCanvasFunction(img.width, img.height).getContext('2d');
// Draw image to context
ctx.drawImage(img, 0, 0);
return new this(ctx.getImageData(0, 0, img.width, img.height).data, img.width, img.height, 4);
} else {
// Use sharp.js to read (and possible resize) the image.
let img = sharp(await blob.arrayBuffer());
return await loadImageFunction(img);
}
}
/**
* Helper method to create a new Image from a tensor
* @param {Tensor} tensor
*/
static fromTensor(tensor, channel_format = 'CHW') {
if (tensor.dims.length !== 3) {
throw new Error(`Tensor should have 3 dimensions, but has ${tensor.dims.length} dimensions.`);
}
if (channel_format === 'CHW') {
tensor = tensor.transpose(1, 2, 0);
} else if (channel_format === 'HWC') {
// Do nothing
} else {
throw new Error(`Unsupported channel format: ${channel_format}`);
}
if (!(tensor.data instanceof Uint8ClampedArray || tensor.data instanceof Uint8Array)) {
throw new Error(`Unsupported tensor type: ${tensor.type}`);
}
switch (tensor.dims[2]) {
case 1:
case 2:
case 3:
case 4:
return new RawImage(tensor.data, tensor.dims[1], tensor.dims[0], tensor.dims[2]);
default:
throw new Error(`Unsupported number of channels: ${tensor.dims[2]}`);
}
}
/**
* Convert the image to grayscale format.
* @returns {RawImage} `this` to support chaining.
*/
grayscale() {
if (this.channels === 1) {
return this;
}
let newData = new Uint8ClampedArray(this.width * this.height * 1);
switch (this.channels) {
case 3: // rgb to grayscale
case 4: // rgba to grayscale
for (let i = 0, offset = 0; i < this.data.length; i += this.channels) {
const red = this.data[i];
const green = this.data[i + 1];
const blue = this.data[i + 2];
newData[offset++] = Math.round(0.2989 * red + 0.5870 * green + 0.1140 * blue);
}
break;
default:
throw new Error(`Conversion failed due to unsupported number of channels: ${this.channels}`);
}
return this._update(newData, this.width, this.height, 1);
}
/**
* Convert the image to RGB format.
* @returns {RawImage} `this` to support chaining.
*/
rgb() {
if (this.channels === 3) {
return this;
}
let newData = new Uint8ClampedArray(this.width * this.height * 3);
switch (this.channels) {
case 1: // grayscale to rgb
for (let i = 0, offset = 0; i < this.data.length; ++i) {
newData[offset++] = this.data[i];
newData[offset++] = this.data[i];
newData[offset++] = this.data[i];
}
break;
case 4: // rgba to rgb
for (let i = 0, offset = 0; i < this.data.length; i += 4) {
newData[offset++] = this.data[i];
newData[offset++] = this.data[i + 1];
newData[offset++] = this.data[i + 2];
}
break;
default:
throw new Error(`Conversion failed due to unsupported number of channels: ${this.channels}`);
}
return this._update(newData, this.width, this.height, 3);
}
/**
* Convert the image to RGBA format.
* @returns {RawImage} `this` to support chaining.
*/
rgba() {
if (this.channels === 4) {
return this;
}
let newData = new Uint8ClampedArray(this.width * this.height * 4);
switch (this.channels) {
case 1: // grayscale to rgba
for (let i = 0, offset = 0; i < this.data.length; ++i) {
newData[offset++] = this.data[i];
newData[offset++] = this.data[i];
newData[offset++] = this.data[i];
newData[offset++] = 255;
}
break;
case 3: // rgb to rgba
for (let i = 0, offset = 0; i < this.data.length; i += 3) {
newData[offset++] = this.data[i];
newData[offset++] = this.data[i + 1];
newData[offset++] = this.data[i + 2];
newData[offset++] = 255;
}
break;
default:
throw new Error(`Conversion failed due to unsupported number of channels: ${this.channels}`);
}
return this._update(newData, this.width, this.height, 4);
}
/**
* Resize the image to the given dimensions. This method uses the canvas API to perform the resizing.
* @param {number} width The width of the new image.
* @param {number} height The height of the new image.
* @param {Object} options Additional options for resizing.
* @param {0|1|2|3|4|5|string} [options.resample] The resampling method to use.
* @returns {Promise<RawImage>} `this` to support chaining.
*/
async resize(width, height, {
resample = 2,
} = {}) {
// Ensure resample method is a string
let resampleMethod = RESAMPLING_MAPPING[resample] ?? resample;
if (BROWSER_ENV) {
// TODO use `resample` in browser environment
// Store number of channels before resizing
let numChannels = this.channels;
// Create canvas object for this image
let canvas = this.toCanvas();
// Actually perform resizing using the canvas API
const ctx = createCanvasFunction(width, height).getContext('2d');
// Draw image to context, resizing in the process
ctx.drawImage(canvas, 0, 0, width, height);
// Create image from the resized data
let resizedImage = new RawImage(ctx.getImageData(0, 0, width, height).data, width, height, 4);
// Convert back so that image has the same number of channels as before
return resizedImage.convert(numChannels);
} else {
// Create sharp image from raw data, and resize
let img = this.toSharp();
switch (resampleMethod) {
case 'box':
case 'hamming':
if (resampleMethod === 'box' || resampleMethod === 'hamming') {
console.warn(`Resampling method ${resampleMethod} is not yet supported. Using bilinear instead.`);
resampleMethod = 'bilinear';
}
case 'nearest':
case 'bilinear':
case 'bicubic':
// Perform resizing using affine transform.
// This matches how the python Pillow library does it.
img = img.affine([width / this.width, 0, 0, height / this.height], {
interpolator: resampleMethod
});
break;
case 'lanczos':
// https://github.com/python-pillow/Pillow/discussions/5519
// https://github.com/lovell/sharp/blob/main/docs/api-resize.md
img = img.resize({
width, height,
fit: 'fill',
kernel: 'lanczos3', // PIL Lanczos uses a kernel size of 3
});
break;
default:
throw new Error(`Resampling method ${resampleMethod} is not supported.`);
}
return await loadImageFunction(img);
}
}
async pad([left, right, top, bottom]) {
left = Math.max(left, 0);
right = Math.max(right, 0);
top = Math.max(top, 0);
bottom = Math.max(bottom, 0);
if (left === 0 && right === 0 && top === 0 && bottom === 0) {
// No padding needed
return this;
}
if (BROWSER_ENV) {
// Store number of channels before padding
let numChannels = this.channels;
// Create canvas object for this image
let canvas = this.toCanvas();
let newWidth = this.width + left + right;
let newHeight = this.height + top + bottom;
// Create a new canvas of the desired size.
const ctx = createCanvasFunction(newWidth, newHeight).getContext('2d');
// Draw image to context, padding in the process
ctx.drawImage(canvas,
0, 0, this.width, this.height,
left, top, newWidth, newHeight
);
// Create image from the padded data
let paddedImage = new RawImage(
ctx.getImageData(0, 0, newWidth, newHeight).data,
newWidth, newHeight, 4);
// Convert back so that image has the same number of channels as before
return paddedImage.convert(numChannels);
} else {
let img = this.toSharp().extend({ left, right, top, bottom });
return await loadImageFunction(img);
}
}
async crop([x_min, y_min, x_max, y_max]) {
// Ensure crop bounds are within the image
x_min = Math.max(x_min, 0);
y_min = Math.max(y_min, 0);
x_max = Math.min(x_max, this.width - 1);
y_max = Math.min(y_max, this.height - 1);
// Do nothing if the crop is the entire image
if (x_min === 0 && y_min === 0 && x_max === this.width - 1 && y_max === this.height - 1) {
return this;
}
const crop_width = x_max - x_min + 1;
const crop_height = y_max - y_min + 1;
if (BROWSER_ENV) {
// Store number of channels before resizing
const numChannels = this.channels;
// Create canvas object for this image
const canvas = this.toCanvas();
// Create a new canvas of the desired size. This is needed since if the
// image is too small, we need to pad it with black pixels.
const ctx = createCanvasFunction(crop_width, crop_height).getContext('2d');
// Draw image to context, cropping in the process
ctx.drawImage(canvas,
x_min, y_min, crop_width, crop_height,
0, 0, crop_width, crop_height
);
// Create image from the resized data
const resizedImage = new RawImage(ctx.getImageData(0, 0, crop_width, crop_height).data, crop_width, crop_height, 4);
// Convert back so that image has the same number of channels as before
return resizedImage.convert(numChannels);
} else {
// Create sharp image from raw data
const img = this.toSharp().extract({
left: x_min,
top: y_min,
width: crop_width,
height: crop_height,
});
return await loadImageFunction(img);
}
}
async center_crop(crop_width, crop_height) {
// If the image is already the desired size, return it
if (this.width === crop_width && this.height === crop_height) {
return this;
}
// Determine bounds of the image in the new canvas
let width_offset = (this.width - crop_width) / 2;
let height_offset = (this.height - crop_height) / 2;
if (BROWSER_ENV) {
// Store number of channels before resizing
let numChannels = this.channels;
// Create canvas object for this image
let canvas = this.toCanvas();
// Create a new canvas of the desired size. This is needed since if the
// image is too small, we need to pad it with black pixels.
const ctx = createCanvasFunction(crop_width, crop_height).getContext('2d');
let sourceX = 0;
let sourceY = 0;
let destX = 0;
let destY = 0;
if (width_offset >= 0) {
sourceX = width_offset;
} else {
destX = -width_offset;
}
if (height_offset >= 0) {
sourceY = height_offset;
} else {
destY = -height_offset;
}
// Draw image to context, cropping in the process
ctx.drawImage(canvas,
sourceX, sourceY, crop_width, crop_height,
destX, destY, crop_width, crop_height
);
// Create image from the resized data
let resizedImage = new RawImage(ctx.getImageData(0, 0, crop_width, crop_height).data, crop_width, crop_height, 4);
// Convert back so that image has the same number of channels as before
return resizedImage.convert(numChannels);
} else {
// Create sharp image from raw data
let img = this.toSharp();
if (width_offset >= 0 && height_offset >= 0) {
// Cropped image lies entirely within the original image
img = img.extract({
left: Math.floor(width_offset),
top: Math.floor(height_offset),
width: crop_width,
height: crop_height,
})
} else if (width_offset <= 0 && height_offset <= 0) {
// Cropped image lies entirely outside the original image,
// so we add padding
let top = Math.floor(-height_offset);
let left = Math.floor(-width_offset);
img = img.extend({
top: top,
left: left,
// Ensures the resulting image has the desired dimensions
right: crop_width - this.width - left,
bottom: crop_height - this.height - top,
});
} else {
// Cropped image lies partially outside the original image.
// We first pad, then crop.
let y_padding = [0, 0];
let y_extract = 0;
if (height_offset < 0) {
y_padding[0] = Math.floor(-height_offset);
y_padding[1] = crop_height - this.height - y_padding[0];
} else {
y_extract = Math.floor(height_offset);
}
let x_padding = [0, 0];
let x_extract = 0;
if (width_offset < 0) {
x_padding[0] = Math.floor(-width_offset);
x_padding[1] = crop_width - this.width - x_padding[0];
} else {
x_extract = Math.floor(width_offset);
}
img = img.extend({
top: y_padding[0],
bottom: y_padding[1],
left: x_padding[0],
right: x_padding[1],
}).extract({
left: x_extract,
top: y_extract,
width: crop_width,
height: crop_height,
})
}
return await loadImageFunction(img);
}
}
async toBlob(type = 'image/png', quality = 1) {
if (!BROWSER_ENV) {
throw new Error('toBlob() is only supported in browser environments.')
}
const canvas = this.toCanvas();
return await canvas.convertToBlob({ type, quality });
}
toTensor(channel_format = 'CHW') {
let tensor = new Tensor(
'uint8',
new Uint8Array(this.data),
[this.height, this.width, this.channels]
);
if (channel_format === 'HWC') {
// Do nothing
} else if (channel_format === 'CHW') { // hwc -> chw
tensor = tensor.permute(2, 0, 1);
} else {
throw new Error(`Unsupported channel format: ${channel_format}`);
}
return tensor;
}
toCanvas() {
if (!BROWSER_ENV) {
throw new Error('toCanvas() is only supported in browser environments.')
}
// Clone, and convert data to RGBA before drawing to canvas.
// This is because the canvas API only supports RGBA
let cloned = this.clone().rgba();
// Create canvas object for the cloned image
let clonedCanvas = createCanvasFunction(cloned.width, cloned.height);
// Draw image to context
let data = new ImageDataClass(cloned.data, cloned.width, cloned.height);
clonedCanvas.getContext('2d').putImageData(data, 0, 0);
return clonedCanvas;
}
/**
* Helper method to update the image data.
* @param {Uint8ClampedArray} data The new image data.
* @param {number} width The new width of the image.
* @param {number} height The new height of the image.
* @param {1|2|3|4|null} [channels] The new number of channels of the image.
* @private
*/
_update(data, width, height, channels = null) {
this.data = data;
this.width = width;
this.height = height;
if (channels !== null) {
this.channels = channels;
}
return this;
}
/**
* Clone the image
* @returns {RawImage} The cloned image
*/
clone() {
return new RawImage(this.data.slice(), this.width, this.height, this.channels);
}
/**
* Helper method for converting image to have a certain number of channels
* @param {number} numChannels The number of channels. Must be 1, 3, or 4.
* @returns {RawImage} `this` to support chaining.
*/
convert(numChannels) {
if (this.channels === numChannels) return this; // Already correct number of channels
switch (numChannels) {
case 1:
this.grayscale();
break;
case 3:
this.rgb();
break;
case 4:
this.rgba();
break;
default:
throw new Error(`Conversion failed due to unsupported number of channels: ${this.channels}`);
}
return this;
}
/**
* Save the image to the given path.
* @param {string} path The path to save the image to.
*/
async save(path) {
if (BROWSER_ENV) {
if (WEBWORKER_ENV) {
throw new Error('Unable to save an image from a Web Worker.')
}
const extension = path.split('.').pop().toLowerCase();
const mime = CONTENT_TYPE_MAP.get(extension) ?? 'image/png';
// Convert image to Blob
const blob = await this.toBlob(mime);
// Convert the canvas content to a data URL
const dataURL = URL.createObjectURL(blob);
// Create an anchor element with the data URL as the href attribute
const downloadLink = document.createElement('a');
downloadLink.href = dataURL;
// Set the download attribute to specify the desired filename for the downloaded image
downloadLink.download = path;
// Trigger the download
downloadLink.click();
// Clean up: remove the anchor element from the DOM
downloadLink.remove();
} else if (!env.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(this.data, {
raw: {
width: this.width,
height: this.height,
channels: this.channels
}
});
}
}

985
node_modules/@xenova/transformers/src/utils/maths.js generated vendored Normal file
View File

@@ -0,0 +1,985 @@
/**
* @file Helper module for mathematical processing.
*
* These functions and classes are only used internally,
* meaning an end-user shouldn't need to access anything here.
*
* @module utils/maths
*/
/**
* @typedef {Int8Array | Uint8Array | Uint8ClampedArray | Int16Array | Uint16Array | Int32Array | Uint32Array | Float32Array | Float64Array} TypedArray
* @typedef {BigInt64Array | BigUint64Array} BigTypedArray
* @typedef {TypedArray | BigTypedArray} AnyTypedArray
*/
/**
* @param {TypedArray} input
*/
export function interpolate_data(input, [in_channels, in_height, in_width], [out_height, out_width], mode = 'bilinear', align_corners = false) {
// TODO use mode and align_corners
// Output image dimensions
const x_scale = out_width / in_width;
const y_scale = out_height / in_height;
// Output image
// @ts-ignore
const out_img = new input.constructor(out_height * out_width * in_channels);
// Pre-calculate strides
const inStride = in_height * in_width;
const outStride = out_height * out_width;
for (let i = 0; i < out_height; ++i) {
for (let j = 0; j < out_width; ++j) {
// Calculate output offset
const outOffset = i * out_width + j;
// Calculate input pixel coordinates
const x = (j + 0.5) / x_scale - 0.5;
const y = (i + 0.5) / y_scale - 0.5;
// Calculate the four nearest input pixels
// We also check if the input pixel coordinates are within the image bounds
let x1 = Math.floor(x);
let y1 = Math.floor(y);
const x2 = Math.min(x1 + 1, in_width - 1);
const y2 = Math.min(y1 + 1, in_height - 1);
x1 = Math.max(x1, 0);
y1 = Math.max(y1, 0);
// Calculate the fractional distances between the input pixel and the four nearest pixels
const s = x - x1;
const t = y - y1;
// Perform bilinear interpolation
const w1 = (1 - s) * (1 - t);
const w2 = s * (1 - t);
const w3 = (1 - s) * t;
const w4 = s * t;
// Calculate the four nearest input pixel indices
const yStride = y1 * in_width;
const xStride = y2 * in_width;
const idx1 = yStride + x1;
const idx2 = yStride + x2;
const idx3 = xStride + x1;
const idx4 = xStride + x2;
for (let k = 0; k < in_channels; ++k) {
// Calculate channel offset
const cOffset = k * inStride;
out_img[k * outStride + outOffset] =
w1 * input[cOffset + idx1] +
w2 * input[cOffset + idx2] +
w3 * input[cOffset + idx3] +
w4 * input[cOffset + idx4];
}
}
}
return out_img;
}
/**
* Helper method to permute a `AnyTypedArray` directly
* @template {AnyTypedArray} T
* @param {T} array
* @param {number[]} dims
* @param {number[]} axes
* @returns {[T, number[]]} The permuted array and the new shape.
*/
export function permute_data(array, dims, axes) {
// Calculate the new shape of the permuted array
// and the stride of the original array
const shape = new Array(axes.length);
const stride = new Array(axes.length);
for (let i = axes.length - 1, s = 1; i >= 0; --i) {
stride[i] = s;
shape[i] = dims[axes[i]];
s *= shape[i];
}
// Precompute inverse mapping of stride
const invStride = axes.map((_, i) => stride[axes.indexOf(i)]);
// Create the permuted array with the new shape
// @ts-ignore
const permutedData = new array.constructor(array.length);
// Permute the original array to the new array
for (let i = 0; i < array.length; ++i) {
let newIndex = 0;
for (let j = dims.length - 1, k = i; j >= 0; --j) {
newIndex += (k % dims[j]) * invStride[j];
k = Math.floor(k / dims[j]);
}
permutedData[newIndex] = array[i];
}
return [permutedData, shape];
}
/**
* Compute the softmax of an array of numbers.
* @template {TypedArray|number[]} T
* @param {T} arr The array of numbers to compute the softmax of.
* @returns {T} The softmax array.
*/
export function softmax(arr) {
// Compute the maximum value in the array
const maxVal = max(arr)[0];
// Compute the exponentials of the array values
const exps = arr.map(x => Math.exp(x - maxVal));
// Compute the sum of the exponentials
// @ts-ignore
const sumExps = exps.reduce((acc, val) => acc + val, 0);
// Compute the softmax values
const softmaxArr = exps.map(x => x / sumExps);
return /** @type {T} */(softmaxArr);
}
/**
* Calculates the logarithm of the softmax function for the input array.
* @template {TypedArray|number[]} T
* @param {T} arr The input array to calculate the log_softmax function for.
* @returns {T} The resulting log_softmax array.
*/
export function log_softmax(arr) {
// Compute the softmax values
const softmaxArr = softmax(arr);
// Apply log formula to each element
const logSoftmaxArr = softmaxArr.map(x => Math.log(x));
return /** @type {T} */(logSoftmaxArr);
}
/**
* Calculates the dot product of two arrays.
* @param {number[]} arr1 The first array.
* @param {number[]} arr2 The second array.
* @returns {number} The dot product of arr1 and arr2.
*/
export function dot(arr1, arr2) {
let result = 0;
for (let i = 0; i < arr1.length; ++i) {
result += arr1[i] * arr2[i];
}
return result;
}
/**
* Get the top k items from an iterable, sorted by descending order
* @param {any[]|TypedArray} items The items to be sorted
* @param {number|null} [top_k=0] The number of top items to return (default: 0 = return all)
* @returns {[number, any][]} The top k items, sorted by descending order
*/
export function getTopItems(items, top_k = 0) {
// if top == 0, return all
items = Array.from(items)
.map((x, i) => [i, x]) // Get indices ([index, score])
.sort((a, b) => b[1] - a[1]) // Sort by log probabilities
if (top_k !== null && top_k > 0) {
items = items.slice(0, top_k); // Get top k items
}
return items
}
/**
* Computes the cosine similarity between two arrays.
*
* @param {number[]} arr1 The first array.
* @param {number[]} arr2 The second array.
* @returns {number} The cosine similarity between the two arrays.
*/
export function cos_sim(arr1, arr2) {
// Calculate dot product of the two arrays
const dotProduct = dot(arr1, arr2);
// Calculate the magnitude of the first array
const magnitudeA = magnitude(arr1);
// Calculate the magnitude of the second array
const magnitudeB = magnitude(arr2);
// Calculate the cosine similarity
const cosineSimilarity = dotProduct / (magnitudeA * magnitudeB);
return cosineSimilarity;
}
/**
* Calculates the magnitude of a given array.
* @param {number[]} arr The array to calculate the magnitude of.
* @returns {number} The magnitude of the array.
*/
export function magnitude(arr) {
return Math.sqrt(arr.reduce((acc, val) => acc + val * val, 0));
}
/**
* Returns the value and index of the minimum element in an array.
* @param {number[]|TypedArray} arr array of numbers.
* @returns {number[]} the value and index of the minimum element, of the form: [valueOfMin, indexOfMin]
* @throws {Error} If array is empty.
*/
export function min(arr) {
if (arr.length === 0) throw Error('Array must not be empty');
let min = arr[0];
let indexOfMin = 0;
for (let i = 1; i < arr.length; ++i) {
if (arr[i] < min) {
min = arr[i];
indexOfMin = i;
}
}
return [min, indexOfMin];
}
/**
* Returns the value and index of the maximum element in an array.
* @param {number[]|AnyTypedArray} arr array of numbers.
* @returns {[number, number]} the value and index of the maximum element, of the form: [valueOfMax, indexOfMax]
* @throws {Error} If array is empty.
*/
export function max(arr) {
if (arr.length === 0) throw Error('Array must not be empty');
let max = arr[0];
let indexOfMax = 0;
for (let i = 1; i < arr.length; ++i) {
if (arr[i] > max) {
max = arr[i];
indexOfMax = i;
}
}
return [Number(max), indexOfMax];
}
function isPowerOfTwo(number) {
// Check if the number is greater than 0 and has only one bit set to 1
return (number > 0) && ((number & (number - 1)) === 0);
}
/**
* Implementation of Radix-4 FFT.
*
* P2FFT class provides functionality for performing Fast Fourier Transform on arrays
* which are a power of two in length.
* Code adapted from https://www.npmjs.com/package/fft.js
*/
class P2FFT {
/**
* @param {number} size The size of the input array. Must be a power of two larger than 1.
* @throws {Error} FFT size must be a power of two larger than 1.
*/
constructor(size) {
this.size = size | 0; // convert to a 32-bit signed integer
if (this.size <= 1 || !isPowerOfTwo(this.size))
throw new Error('FFT size must be a power of two larger than 1');
this._csize = size << 1;
this.table = new Float64Array(this.size * 2);
for (let i = 0; i < this.table.length; i += 2) {
const angle = Math.PI * i / this.size;
this.table[i] = Math.cos(angle);
this.table[i + 1] = -Math.sin(angle);
}
// Find size's power of two
let power = 0;
for (let t = 1; this.size > t; t <<= 1)
++power;
// Calculate initial step's width:
// * If we are full radix-4, it is 2x smaller to give inital len=8
// * Otherwise it is the same as `power` to give len=4
this._width = power % 2 === 0 ? power - 1 : power;
// Pre-compute bit-reversal patterns
this._bitrev = new Int32Array(1 << this._width);
for (let j = 0; j < this._bitrev.length; ++j) {
this._bitrev[j] = 0;
for (let shift = 0; shift < this._width; shift += 2) {
const revShift = this._width - shift - 2;
this._bitrev[j] |= ((j >>> shift) & 3) << revShift;
}
}
}
/**
* Create a complex number array with size `2 * size`
*
* @returns {Float64Array} A complex number array with size `2 * size`
*/
createComplexArray() {
return new Float64Array(this._csize);
}
/**
* Converts a complex number representation stored in a Float64Array to an array of real numbers.
*
* @param {Float64Array} complex The complex number representation to be converted.
* @param {number[]} [storage] An optional array to store the result in.
* @returns {number[]} An array of real numbers representing the input complex number representation.
*/
fromComplexArray(complex, storage) {
const res = storage || new Array(complex.length >>> 1);
for (let i = 0; i < complex.length; i += 2)
res[i >>> 1] = complex[i];
return res;
}
/**
* Convert a real-valued input array to a complex-valued output array.
* @param {Float64Array} input The real-valued input array.
* @param {Float64Array} [storage] Optional buffer to store the output array.
* @returns {Float64Array} The complex-valued output array.
*/
toComplexArray(input, storage) {
const res = storage || this.createComplexArray();
for (let i = 0; i < res.length; i += 2) {
res[i] = input[i >>> 1];
res[i + 1] = 0;
}
return res;
}
/**
* Performs a Fast Fourier Transform (FFT) on the given input data and stores the result in the output buffer.
*
* @param {Float64Array} out The output buffer to store the result.
* @param {Float64Array} data The input data to transform.
*
* @throws {Error} Input and output buffers must be different.
*
* @returns {void}
*/
transform(out, data) {
if (out === data)
throw new Error('Input and output buffers must be different');
this._transform4(out, data, 1 /* DONE */);
}
/**
* Performs a real-valued forward FFT on the given input buffer and stores the result in the given output buffer.
* The input buffer must contain real values only, while the output buffer will contain complex values. The input and
* output buffers must be different.
*
* @param {Float64Array} out The output buffer.
* @param {Float64Array} data The input buffer containing real values.
*
* @throws {Error} If the input and output buffers are the same.
*/
realTransform(out, data) {
if (out === data)
throw new Error('Input and output buffers must be different');
this._realTransform4(out, data, 1 /* DONE */);
}
/**
* Performs an inverse FFT transformation on the given `data` array, and stores the result in `out`.
* The `out` array must be a different buffer than the `data` array. The `out` array will contain the
* result of the transformation. The `data` array will not be modified.
*
* @param {Float64Array} out The output buffer for the transformed data.
* @param {Float64Array} data The input data to transform.
* @throws {Error} If `out` and `data` refer to the same buffer.
* @returns {void}
*/
inverseTransform(out, data) {
if (out === data)
throw new Error('Input and output buffers must be different');
this._transform4(out, data, -1 /* DONE */);
for (let i = 0; i < out.length; ++i)
out[i] /= this.size;
}
/**
* Performs a radix-4 implementation of a discrete Fourier transform on a given set of data.
*
* @param {Float64Array} out The output buffer for the transformed data.
* @param {Float64Array} data The input buffer of data to be transformed.
* @param {number} inv A scaling factor to apply to the transform.
* @returns {void}
*/
_transform4(out, data, inv) {
// radix-4 implementation
const size = this._csize;
// Initial step (permute and transform)
const width = this._width;
let step = 1 << width;
let len = (size / step) << 1;
let outOff;
let t;
const bitrev = this._bitrev;
if (len === 4) {
for (outOff = 0, t = 0; outOff < size; outOff += len, ++t) {
const off = bitrev[t];
this._singleTransform2(data, out, outOff, off, step);
}
} else {
// len === 8
for (outOff = 0, t = 0; outOff < size; outOff += len, ++t) {
const off = bitrev[t];
this._singleTransform4(data, out, outOff, off, step, inv);
}
}
// Loop through steps in decreasing order
const table = this.table;
for (step >>= 2; step >= 2; step >>= 2) {
len = (size / step) << 1;
const quarterLen = len >>> 2;
// Loop through offsets in the data
for (outOff = 0; outOff < size; outOff += len) {
// Full case
const limit = outOff + quarterLen - 1;
for (let i = outOff, k = 0; i < limit; i += 2, k += step) {
const A = i;
const B = A + quarterLen;
const C = B + quarterLen;
const D = C + quarterLen;
// Original values
const Ar = out[A];
const Ai = out[A + 1];
const Br = out[B];
const Bi = out[B + 1];
const Cr = out[C];
const Ci = out[C + 1];
const Dr = out[D];
const Di = out[D + 1];
const tableBr = table[k];
const tableBi = inv * table[k + 1];
const MBr = Br * tableBr - Bi * tableBi;
const MBi = Br * tableBi + Bi * tableBr;
const tableCr = table[2 * k];
const tableCi = inv * table[2 * k + 1];
const MCr = Cr * tableCr - Ci * tableCi;
const MCi = Cr * tableCi + Ci * tableCr;
const tableDr = table[3 * k];
const tableDi = inv * table[3 * k + 1];
const MDr = Dr * tableDr - Di * tableDi;
const MDi = Dr * tableDi + Di * tableDr;
// Pre-Final values
const T0r = Ar + MCr;
const T0i = Ai + MCi;
const T1r = Ar - MCr;
const T1i = Ai - MCi;
const T2r = MBr + MDr;
const T2i = MBi + MDi;
const T3r = inv * (MBr - MDr);
const T3i = inv * (MBi - MDi);
// Final values
out[A] = T0r + T2r;
out[A + 1] = T0i + T2i;
out[B] = T1r + T3i;
out[B + 1] = T1i - T3r;
out[C] = T0r - T2r;
out[C + 1] = T0i - T2i;
out[D] = T1r - T3i;
out[D + 1] = T1i + T3r;
}
}
}
}
/**
* Performs a radix-2 implementation of a discrete Fourier transform on a given set of data.
*
* @param {Float64Array} data The input buffer of data to be transformed.
* @param {Float64Array} out The output buffer for the transformed data.
* @param {number} outOff The offset at which to write the output data.
* @param {number} off The offset at which to begin reading the input data.
* @param {number} step The step size for indexing the input data.
* @returns {void}
*/
_singleTransform2(data, out, outOff, off, step) {
// radix-2 implementation
// NOTE: Only called for len=4
const evenR = data[off];
const evenI = data[off + 1];
const oddR = data[off + step];
const oddI = data[off + step + 1];
out[outOff] = evenR + oddR;
out[outOff + 1] = evenI + oddI;
out[outOff + 2] = evenR - oddR;
out[outOff + 3] = evenI - oddI;
}
/**
* Performs radix-4 transformation on input data of length 8
*
* @param {Float64Array} data Input data array of length 8
* @param {Float64Array} out Output data array of length 8
* @param {number} outOff Index of output array to start writing from
* @param {number} off Index of input array to start reading from
* @param {number} step Step size between elements in input array
* @param {number} inv Scaling factor for inverse transform
*
* @returns {void}
*/
_singleTransform4(data, out, outOff, off, step, inv) {
// radix-4
// NOTE: Only called for len=8
const step2 = step * 2;
const step3 = step * 3;
// Original values
const Ar = data[off];
const Ai = data[off + 1];
const Br = data[off + step];
const Bi = data[off + step + 1];
const Cr = data[off + step2];
const Ci = data[off + step2 + 1];
const Dr = data[off + step3];
const Di = data[off + step3 + 1];
// Pre-Final values
const T0r = Ar + Cr;
const T0i = Ai + Ci;
const T1r = Ar - Cr;
const T1i = Ai - Ci;
const T2r = Br + Dr;
const T2i = Bi + Di;
const T3r = inv * (Br - Dr);
const T3i = inv * (Bi - Di);
// Final values
out[outOff] = T0r + T2r;
out[outOff + 1] = T0i + T2i;
out[outOff + 2] = T1r + T3i;
out[outOff + 3] = T1i - T3r;
out[outOff + 4] = T0r - T2r;
out[outOff + 5] = T0i - T2i;
out[outOff + 6] = T1r - T3i;
out[outOff + 7] = T1i + T3r;
}
/**
* Real input radix-4 implementation
* @param {Float64Array} out Output array for the transformed data
* @param {Float64Array} data Input array of real data to be transformed
* @param {number} inv The scale factor used to normalize the inverse transform
*/
_realTransform4(out, data, inv) {
// Real input radix-4 implementation
const size = this._csize;
// Initial step (permute and transform)
const width = this._width;
let step = 1 << width;
let len = (size / step) << 1;
let outOff;
let t;
const bitrev = this._bitrev;
if (len === 4) {
for (outOff = 0, t = 0; outOff < size; outOff += len, ++t) {
const off = bitrev[t];
this._singleRealTransform2(data, out, outOff, off >>> 1, step >>> 1);
}
} else {
// len === 8
for (outOff = 0, t = 0; outOff < size; outOff += len, ++t) {
const off = bitrev[t];
this._singleRealTransform4(data, out, outOff, off >>> 1, step >>> 1, inv);
}
}
// Loop through steps in decreasing order
const table = this.table;
for (step >>= 2; step >= 2; step >>= 2) {
len = (size / step) << 1;
const halfLen = len >>> 1;
const quarterLen = halfLen >>> 1;
const hquarterLen = quarterLen >>> 1;
// Loop through offsets in the data
for (outOff = 0; outOff < size; outOff += len) {
for (let i = 0, k = 0; i <= hquarterLen; i += 2, k += step) {
const A = outOff + i;
const B = A + quarterLen;
const C = B + quarterLen;
const D = C + quarterLen;
// Original values
const Ar = out[A];
const Ai = out[A + 1];
const Br = out[B];
const Bi = out[B + 1];
const Cr = out[C];
const Ci = out[C + 1];
const Dr = out[D];
const Di = out[D + 1];
// Middle values
const MAr = Ar;
const MAi = Ai;
const tableBr = table[k];
const tableBi = inv * table[k + 1];
const MBr = Br * tableBr - Bi * tableBi;
const MBi = Br * tableBi + Bi * tableBr;
const tableCr = table[2 * k];
const tableCi = inv * table[2 * k + 1];
const MCr = Cr * tableCr - Ci * tableCi;
const MCi = Cr * tableCi + Ci * tableCr;
const tableDr = table[3 * k];
const tableDi = inv * table[3 * k + 1];
const MDr = Dr * tableDr - Di * tableDi;
const MDi = Dr * tableDi + Di * tableDr;
// Pre-Final values
const T0r = MAr + MCr;
const T0i = MAi + MCi;
const T1r = MAr - MCr;
const T1i = MAi - MCi;
const T2r = MBr + MDr;
const T2i = MBi + MDi;
const T3r = inv * (MBr - MDr);
const T3i = inv * (MBi - MDi);
// Final values
out[A] = T0r + T2r;
out[A + 1] = T0i + T2i;
out[B] = T1r + T3i;
out[B + 1] = T1i - T3r;
// Output final middle point
if (i === 0) {
out[C] = T0r - T2r;
out[C + 1] = T0i - T2i;
continue;
}
// Do not overwrite ourselves
if (i === hquarterLen)
continue;
const SA = outOff + quarterLen - i;
const SB = outOff + halfLen - i;
out[SA] = T1r - inv * T3i;
out[SA + 1] = -T1i - inv * T3r;
out[SB] = T0r - inv * T2r;
out[SB + 1] = -T0i + inv * T2i;
}
}
}
// Complete the spectrum by adding its mirrored negative frequency components.
const half = size >>> 1;
for (let i = 2; i < half; i += 2) {
out[size - i] = out[i];
out[size - i + 1] = -out[i + 1];
}
}
/**
* Performs a single real input radix-2 transformation on the provided data
*
* @param {Float64Array} data The input data array
* @param {Float64Array} out The output data array
* @param {number} outOff The output offset
* @param {number} off The input offset
* @param {number} step The step
*
* @returns {void}
*/
_singleRealTransform2(data, out, outOff, off, step) {
// radix-2 implementation
// NOTE: Only called for len=4
const evenR = data[off];
const oddR = data[off + step];
out[outOff] = evenR + oddR;
out[outOff + 1] = 0;
out[outOff + 2] = evenR - oddR;
out[outOff + 3] = 0;
}
/**
* Computes a single real-valued transform using radix-4 algorithm.
* This method is only called for len=8.
*
* @param {Float64Array} data The input data array.
* @param {Float64Array} out The output data array.
* @param {number} outOff The offset into the output array.
* @param {number} off The offset into the input array.
* @param {number} step The step size for the input array.
* @param {number} inv The value of inverse.
*/
_singleRealTransform4(data, out, outOff, off, step, inv) {
// radix-4
// NOTE: Only called for len=8
const step2 = step * 2;
const step3 = step * 3;
// Original values
const Ar = data[off];
const Br = data[off + step];
const Cr = data[off + step2];
const Dr = data[off + step3];
// Pre-Final values
const T0r = Ar + Cr;
const T1r = Ar - Cr;
const T2r = Br + Dr;
const T3r = inv * (Br - Dr);
// Final values
out[outOff] = T0r + T2r;
out[outOff + 1] = 0;
out[outOff + 2] = T1r;
out[outOff + 3] = -T3r;
out[outOff + 4] = T0r - T2r;
out[outOff + 5] = 0;
out[outOff + 6] = T1r;
out[outOff + 7] = T3r;
}
}
/**
* NP2FFT class provides functionality for performing Fast Fourier Transform on arrays
* which are not a power of two in length. In such cases, the chirp-z transform is used.
*
* For more information, see: https://math.stackexchange.com/questions/77118/non-power-of-2-ffts/77156#77156
*/
class NP2FFT {
/**
* Constructs a new NP2FFT object.
* @param {number} fft_length The length of the FFT
*/
constructor(fft_length) {
// Helper variables
const a = 2 * (fft_length - 1);
const b = 2 * (2 * fft_length - 1);
const nextP2 = 2 ** (Math.ceil(Math.log2(b)))
this.bufferSize = nextP2;
this._a = a;
// Define buffers
// Compute chirp for transform
const chirp = new Float64Array(b);
const ichirp = new Float64Array(nextP2);
this._chirpBuffer = new Float64Array(nextP2);
this._buffer1 = new Float64Array(nextP2);
this._buffer2 = new Float64Array(nextP2);
this._outBuffer1 = new Float64Array(nextP2);
this._outBuffer2 = new Float64Array(nextP2);
// Compute complex exponentiation
const theta = -2 * Math.PI / fft_length;
const baseR = Math.cos(theta);
const baseI = Math.sin(theta);
// Precompute helper for chirp-z transform
for (let i = 0; i < b >> 1; ++i) {
// Compute complex power:
const e = (i + 1 - fft_length) ** 2 / 2.0;
// Compute the modulus and argument of the result
const result_mod = Math.sqrt(baseR ** 2 + baseI ** 2) ** e;
const result_arg = e * Math.atan2(baseI, baseR);
// Convert the result back to rectangular form
// and assign to chirp and ichirp
const i2 = 2 * i;
chirp[i2] = result_mod * Math.cos(result_arg);
chirp[i2 + 1] = result_mod * Math.sin(result_arg);
// conjugate
ichirp[i2] = chirp[i2];
ichirp[i2 + 1] = - chirp[i2 + 1];
}
this._slicedChirpBuffer = chirp.subarray(a, b);
// create object to perform Fast Fourier Transforms
// with `nextP2` complex numbers
this._f = new P2FFT(nextP2 >> 1);
this._f.transform(this._chirpBuffer, ichirp);
}
_transform(output, input, real) {
const ib1 = this._buffer1;
const ib2 = this._buffer2;
const ob2 = this._outBuffer1;
const ob3 = this._outBuffer2;
const cb = this._chirpBuffer;
const sb = this._slicedChirpBuffer;
const a = this._a;
if (real) {
// Real multiplication
for (let j = 0; j < sb.length; j += 2) {
const j2 = j + 1
const j3 = j >> 1;
const a_real = input[j3];
ib1[j] = a_real * sb[j];
ib1[j2] = a_real * sb[j2];
}
} else {
// Complex multiplication
for (let j = 0; j < sb.length; j += 2) {
const j2 = j + 1
ib1[j] = input[j] * sb[j] - input[j2] * sb[j2];
ib1[j2] = input[j] * sb[j2] + input[j2] * sb[j];
}
}
this._f.transform(ob2, ib1);
for (let j = 0; j < cb.length; j += 2) {
const j2 = j + 1;
ib2[j] = ob2[j] * cb[j] - ob2[j2] * cb[j2];
ib2[j2] = ob2[j] * cb[j2] + ob2[j2] * cb[j];
}
this._f.inverseTransform(ob3, ib2);
for (let j = 0; j < ob3.length; j += 2) {
const a_real = ob3[j + a];
const a_imag = ob3[j + a + 1];
const b_real = sb[j];
const b_imag = sb[j + 1];
output[j] = a_real * b_real - a_imag * b_imag;
output[j + 1] = a_real * b_imag + a_imag * b_real;
}
}
transform(output, input) {
this._transform(output, input, false);
}
realTransform(output, input) {
this._transform(output, input, true);
}
}
export class FFT {
constructor(fft_length) {
this.fft_length = fft_length;
this.isPowerOfTwo = isPowerOfTwo(fft_length);
if (this.isPowerOfTwo) {
this.fft = new P2FFT(fft_length);
this.outputBufferSize = 2 * fft_length;
} else {
this.fft = new NP2FFT(fft_length);
this.outputBufferSize = this.fft.bufferSize;
}
}
realTransform(out, input) {
this.fft.realTransform(out, input);
}
transform(out, input) {
this.fft.transform(out, input);
}
}
/**
* Performs median filter on the provided data. Padding is done by mirroring the data.
* @param {AnyTypedArray} data The input array
* @param {number} windowSize The window size
*/
export function medianFilter(data, windowSize) {
if (windowSize % 2 === 0 || windowSize <= 0) {
throw new Error('Window size must be a positive odd number');
}
// @ts-ignore
const outputArray = new data.constructor(data.length);
// @ts-ignore
const buffer = new data.constructor(windowSize); // Reusable array for storing values
const halfWindowSize = Math.floor(windowSize / 2);
for (let i = 0; i < data.length; ++i) {
let valuesIndex = 0;
for (let j = -halfWindowSize; j <= halfWindowSize; ++j) {
let index = i + j;
if (index < 0) {
index = Math.abs(index);
} else if (index >= data.length) {
index = 2 * (data.length - 1) - index;
}
buffer[valuesIndex++] = data[index];
}
buffer.sort();
outputArray[i] = buffer[halfWindowSize];
}
return outputArray;
}
/**
* Helper function to round a number to a given number of decimals
* @param {number} num The number to round
* @param {number} decimals The number of decimals
* @returns {number} The rounded number
*/
export function round(num, decimals) {
const pow = Math.pow(10, decimals);
return Math.round(num * pow) / pow;
}
/**
* Helper function to round a number to the nearest integer, with ties rounded to the nearest even number.
* Also known as "bankers' rounding". This is the default rounding mode in python. For example:
* 1.5 rounds to 2 and 2.5 rounds to 2.
*
* @param {number} x The number to round
* @returns {number} The rounded number
*/
export 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;
}

1239
node_modules/@xenova/transformers/src/utils/tensor.js generated vendored Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,5 @@
/** @type {import('onnxruntime-web')} The ONNX runtime module. */
export let ONNX: typeof ONNX_WEB;
export const executionProviders: string[];
import * as ONNX_WEB from 'onnxruntime-web';
//# sourceMappingURL=onnx.d.ts.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"onnx.d.ts","sourceRoot":"","sources":["../../src/backends/onnx.js"],"names":[],"mappings":"AAuBA,iEAAiE;AACjE,iCAAgB;AAEhB,0CAGE;0BARwB,iBAAiB"}

43
node_modules/@xenova/transformers/types/configs.d.ts generated vendored Normal file
View File

@@ -0,0 +1,43 @@
/**
* Base class for all configuration classes. For more information, see the corresponding
* [Python documentation](https://huggingface.co/docs/transformers/main/en/main_classes/configuration#transformers.PretrainedConfig).
*/
export class PretrainedConfig {
/**
* 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<PretrainedConfig>} A new instance of the `PretrainedConfig` class.
*/
static from_pretrained(pretrained_model_name_or_path: string, { progress_callback, config, cache_dir, local_files_only, revision, }?: PretrainedOptions): Promise<PretrainedConfig>;
/**
* Create a new PreTrainedTokenizer instance.
* @param {Object} configJSON The JSON of the config.
*/
constructor(configJSON: any);
model_type: any;
is_encoder_decoder: boolean;
}
/**
* Helper class which is used to instantiate pretrained configs with the `from_pretrained` function.
*
* @example
* let config = await AutoConfig.from_pretrained('bert-base-uncased');
*/
export class AutoConfig {
/**
* 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<PretrainedConfig>} A new instance of the `PretrainedConfig` class.
*/
static from_pretrained(pretrained_model_name_or_path: string, { progress_callback, config, cache_dir, local_files_only, revision, }?: import("./utils/hub.js").PretrainedOptions): Promise<PretrainedConfig>;
}
export type PretrainedOptions = import('./utils/hub.js').PretrainedOptions;
//# sourceMappingURL=configs.d.ts.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"configs.d.ts","sourceRoot":"","sources":["../src/configs.js"],"names":[],"mappings":"AAiDA;;;GAGG;AACH;IAcI;;;;;;;;OAQG;IACH,sDANW,MAAM,0EACN,iBAAiB,GAGf,QAAQ,gBAAgB,CAAC,CAkBrC;IApCD;;;OAGG;IACH,6BAKC;IAJG,gBAAsB;IACtB,4BAA+B;CA+BtC;AAED;;;;;GAKG;AACH;IAlCI;;;;;;;;OAQG;IACH,6MAgBC;CAcJ;gCAxEY,OAAO,gBAAgB,EAAE,iBAAiB"}

28
node_modules/@xenova/transformers/types/env.d.ts generated vendored Normal file
View File

@@ -0,0 +1,28 @@
export namespace env {
export namespace backends {
export { onnx_env as onnx };
export let tfjs: {};
}
export { __dirname };
export { VERSION as version };
export let allowRemoteModels: boolean;
export let remoteHost: string;
export let remotePathTemplate: string;
export let allowLocalModels: boolean;
export { localModelPath };
export { FS_AVAILABLE as useFS };
export { WEB_CACHE_AVAILABLE as useBrowserCache };
export { FS_AVAILABLE as useFSCache };
export { DEFAULT_CACHE_DIR as cacheDir };
export let useCustomCache: boolean;
export let customCache: any;
}
declare const onnx_env: import("onnxruntime-common").Env;
declare const __dirname: any;
declare const VERSION: "2.17.2";
declare const localModelPath: any;
declare const FS_AVAILABLE: boolean;
declare const WEB_CACHE_AVAILABLE: boolean;
declare const DEFAULT_CACHE_DIR: any;
export {};
//# sourceMappingURL=env.d.ts.map

1
node_modules/@xenova/transformers/types/env.d.ts.map generated vendored Normal file
View File

@@ -0,0 +1 @@
{"version":3,"file":"env.d.ts","sourceRoot":"","sources":["../src/env.js"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;AAwCA,6BAEW;AAXX,gCAAyB;AAoBzB,kCAE+B;AAlB/B,oCAAkC;AADlC,2CAA4E;AAW5E,qCAEW"}

3661
node_modules/@xenova/transformers/types/models.d.ts generated vendored Normal file

File diff suppressed because it is too large Load Diff

File diff suppressed because one or more lines are too long

2427
node_modules/@xenova/transformers/types/pipelines.d.ts generated vendored Normal file

File diff suppressed because it is too large Load Diff

File diff suppressed because one or more lines are too long

769
node_modules/@xenova/transformers/types/processors.d.ts generated vendored Normal file
View File

@@ -0,0 +1,769 @@
declare const FeatureExtractor_base: new () => {
(...args: any[]): any;
_call(...args: any[]): any;
};
/**
* Base class for feature extractors.
*
* @extends Callable
*/
export class FeatureExtractor extends FeatureExtractor_base {
/**
* Constructs a new FeatureExtractor instance.
*
* @param {Object} config The configuration for the feature extractor.
*/
constructor(config: any);
config: any;
}
/**
* @typedef {object} ImageFeatureExtractorResult
* @property {Tensor} pixel_values The pixel values of the batched preprocessed images.
* @property {HeightWidth[]} original_sizes Array of two-dimensional tuples like [[480, 640]].
* @property {HeightWidth[]} reshaped_input_sizes Array of two-dimensional tuples like [[1000, 1330]].
*/
/**
* Feature extractor for image models.
*
* @extends FeatureExtractor
*/
export 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: {
image_mean: number[];
image_std: number[];
do_rescale: boolean;
rescale_factor: number;
do_normalize: boolean;
do_resize: boolean;
resample: number;
size: number | any;
do_flip_channel_order?: boolean;
});
image_mean: any;
image_std: any;
resample: any;
do_rescale: any;
rescale_factor: any;
do_normalize: any;
do_resize: any;
do_thumbnail: any;
size: any;
size_divisibility: any;
do_center_crop: any;
crop_size: any;
do_convert_rgb: any;
do_crop_margin: any;
pad_size: any;
do_pad: any;
do_flip_channel_order: any;
/**
* 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<RawImage>} The resized image.
*/
thumbnail(image: RawImage, size: {
height: number;
width: number;
}, resample?: string | 0 | 1 | 2 | 3 | 4 | 5): Promise<RawImage>;
/**
* 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<RawImage>} The cropped image.
*/
crop_margin(image: RawImage, gray_threshold?: number): Promise<RawImage>;
/**
* 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: Float32Array, imgDims: number[], padSize: {
width: number;
height: number;
} | number, { mode, center, constant_values, }?: {
mode?: 'constant' | 'symmetric';
center?: boolean;
constant_values?: number;
}): [Float32Array, number[]];
/**
* Rescale the image' pixel values by `this.rescale_factor`.
* @param {Float32Array} pixelData The pixel data to rescale.
* @returns {void}
*/
rescale(pixelData: Float32Array): void;
/**
* 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: RawImage, size: any): [number, number];
/**
* Resizes the image.
* @param {RawImage} image The image to resize.
* @returns {Promise<RawImage>} The resized image.
*/
resize(image: RawImage): Promise<RawImage>;
/**
* @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<PreprocessedImage>} The preprocessed image.
*/
preprocess(image: RawImage, { do_normalize, do_pad, do_convert_rgb, do_convert_grayscale, do_flip_channel_order, }?: any): Promise<{
/**
* The original size of the image.
*/
original_size: HeightWidth;
/**
* The reshaped input size of the image.
*/
reshaped_input_size: HeightWidth;
/**
* The pixel values of the preprocessed image.
*/
pixel_values: Tensor;
}>;
/**
* 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<ImageFeatureExtractorResult>} An object containing the concatenated pixel values (and other metadata) of the preprocessed images.
*/
_call(images: RawImage[], ...args: any[]): Promise<ImageFeatureExtractorResult>;
}
export class SegformerFeatureExtractor extends ImageFeatureExtractor {
/**
* Converts the output of `SegformerForSemanticSegmentation` into semantic segmentation maps.
* @param {*} outputs Raw outputs of the model.
* @param {number[][]} [target_sizes=null] List of tuples corresponding to the requested final size
* (height, width) of each prediction. If unset, predictions will not be resized.
* @returns {{segmentation: Tensor; labels: number[]}[]} The semantic segmentation maps.
*/
post_process_semantic_segmentation(outputs: any, target_sizes?: number[][]): {
segmentation: Tensor;
labels: number[];
}[];
}
export class DPTFeatureExtractor extends ImageFeatureExtractor {
}
export class DPTImageProcessor extends DPTFeatureExtractor {
}
export class BitImageProcessor extends ImageFeatureExtractor {
}
export class GLPNFeatureExtractor extends ImageFeatureExtractor {
}
export class CLIPFeatureExtractor extends ImageFeatureExtractor {
}
export class ChineseCLIPFeatureExtractor extends ImageFeatureExtractor {
}
export class SiglipImageProcessor extends ImageFeatureExtractor {
}
export class ConvNextFeatureExtractor extends ImageFeatureExtractor {
constructor(config: any);
/**
* Percentage of the image to crop. Only has an effect if this.size < 384.
*/
crop_pct: any;
resize(image: any): Promise<any>;
}
export class ConvNextImageProcessor extends ConvNextFeatureExtractor {
}
export class ViTFeatureExtractor extends ImageFeatureExtractor {
}
export class ViTImageProcessor extends ImageFeatureExtractor {
}
export class EfficientNetImageProcessor extends ImageFeatureExtractor {
constructor(config: any);
include_top: any;
}
export class MobileViTFeatureExtractor extends ImageFeatureExtractor {
}
export class MobileViTImageProcessor extends MobileViTFeatureExtractor {
}
export class OwlViTFeatureExtractor extends ImageFeatureExtractor {
/**
* Post-processes the outputs of the model (for object detection).
* @param {Object} outputs The outputs of the model that must be post-processed
* @param {Tensor} outputs.logits The logits
* @param {Tensor} outputs.pred_boxes The predicted boxes.
* @param {number} [threshold=0.5] The threshold to use for the scores.
* @param {number[][]} [target_sizes=null] The sizes of the original images.
* @param {boolean} [is_zero_shot=false] Whether zero-shot object detection was performed.
* @return {Object[]} An array of objects containing the post-processed outputs.
* @private
*/
post_process_object_detection(outputs: {
logits: Tensor;
pred_boxes: Tensor;
}, threshold?: number, target_sizes?: number[][], is_zero_shot?: boolean): any[];
}
export class Owlv2ImageProcessor extends OwlViTFeatureExtractor {
}
export class DeiTFeatureExtractor extends ImageFeatureExtractor {
}
export class BeitFeatureExtractor extends ImageFeatureExtractor {
}
export class DonutFeatureExtractor extends ImageFeatureExtractor {
pad_image(pixelData: any, imgDims: any, padSize: any, options?: {}): [Float32Array, number[]];
}
export class NougatImageProcessor extends DonutFeatureExtractor {
}
/**
* @typedef {object} DetrFeatureExtractorResultProps
* @property {Tensor} pixel_mask
* @typedef {ImageFeatureExtractorResult & DetrFeatureExtractorResultProps} DetrFeatureExtractorResult
*/
/**
* Detr Feature Extractor.
*
* @extends ImageFeatureExtractor
*/
export 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<DetrFeatureExtractorResult>} An object containing the concatenated pixel values of the preprocessed images.
*/
_call(images: RawImage[]): Promise<DetrFeatureExtractorResult>;
/**
* Post-processes the outputs of the model (for object detection).
* @param {Object} outputs The outputs of the model that must be post-processed
* @param {Tensor} outputs.logits The logits
* @param {Tensor} outputs.pred_boxes The predicted boxes.
* @param {number} [threshold=0.5] The threshold to use for the scores.
* @param {number[][]} [target_sizes=null] The sizes of the original images.
* @param {boolean} [is_zero_shot=false] Whether zero-shot object detection was performed.
* @return {Object[]} An array of objects containing the post-processed outputs.
* @private
*/
post_process_object_detection(outputs: {
logits: Tensor;
pred_boxes: Tensor;
}, threshold?: number, target_sizes?: number[][], is_zero_shot?: boolean): any[];
/**
* Binarize the given masks using `object_mask_threshold`, it returns the associated values of `masks`, `scores` and `labels`.
* @param {Tensor} class_logits The class logits.
* @param {Tensor} mask_logits The mask logits.
* @param {number} object_mask_threshold A number between 0 and 1 used to binarize the masks.
* @param {number} num_labels The number of labels.
* @returns {[Tensor[], number[], number[]]} The binarized masks, the scores, and the labels.
*/
remove_low_and_no_objects(class_logits: Tensor, mask_logits: Tensor, object_mask_threshold: number, num_labels: number): [Tensor[], number[], number[]];
/**
* Checks whether the segment is valid or not.
* @param {Int32Array} mask_labels Labels for each pixel in the mask.
* @param {Tensor[]} mask_probs Probabilities for each pixel in the masks.
* @param {number} k The class id of the segment.
* @param {number} mask_threshold The mask threshold.
* @param {number} overlap_mask_area_threshold The overlap mask area threshold.
* @returns {[boolean, number[]]} Whether the segment is valid or not, and the indices of the valid labels.
*/
check_segment_validity(mask_labels: Int32Array, mask_probs: Tensor[], k: number, mask_threshold?: number, overlap_mask_area_threshold?: number): [boolean, number[]];
/**
* Computes the segments.
* @param {Tensor[]} mask_probs The mask probabilities.
* @param {number[]} pred_scores The predicted scores.
* @param {number[]} pred_labels The predicted labels.
* @param {number} mask_threshold The mask threshold.
* @param {number} overlap_mask_area_threshold The overlap mask area threshold.
* @param {Set<number>} label_ids_to_fuse The label ids to fuse.
* @param {number[]} target_size The target size of the image.
* @returns {[Tensor, Array<{id: number, label_id: number, score: number}>]} The computed segments.
*/
compute_segments(mask_probs: Tensor[], pred_scores: number[], pred_labels: number[], mask_threshold: number, overlap_mask_area_threshold: number, label_ids_to_fuse?: Set<number>, target_size?: number[]): [Tensor, Array<{
id: number;
label_id: number;
score: number;
}>];
/**
* Post-process the model output to generate the final panoptic segmentation.
* @param {*} outputs The model output to post process
* @param {number} [threshold=0.5] The probability score threshold to keep predicted instance masks.
* @param {number} [mask_threshold=0.5] Threshold to use when turning the predicted masks into binary values.
* @param {number} [overlap_mask_area_threshold=0.8] The overlap mask area threshold to merge or discard small disconnected parts within each binary instance mask.
* @param {Set<number>} [label_ids_to_fuse=null] The labels in this state will have all their instances be fused together.
* @param {number[][]} [target_sizes=null] The target sizes to resize the masks to.
* @returns {Array<{ segmentation: Tensor, segments_info: Array<{id: number, label_id: number, score: number}>}>}
*/
post_process_panoptic_segmentation(outputs: any, threshold?: number, mask_threshold?: number, overlap_mask_area_threshold?: number, label_ids_to_fuse?: Set<number>, target_sizes?: number[][]): Array<{
segmentation: Tensor;
segments_info: Array<{
id: number;
label_id: number;
score: number;
}>;
}>;
post_process_instance_segmentation(): void;
}
export class YolosFeatureExtractor extends ImageFeatureExtractor {
/**
* Post-processes the outputs of the model (for object detection).
* @param {Object} outputs The outputs of the model that must be post-processed
* @param {Tensor} outputs.logits The logits
* @param {Tensor} outputs.pred_boxes The predicted boxes.
* @param {number} [threshold=0.5] The threshold to use for the scores.
* @param {number[][]} [target_sizes=null] The sizes of the original images.
* @param {boolean} [is_zero_shot=false] Whether zero-shot object detection was performed.
* @return {Object[]} An array of objects containing the post-processed outputs.
* @private
*/
post_process_object_detection(outputs: {
logits: Tensor;
pred_boxes: Tensor;
}, threshold?: number, target_sizes?: number[][], is_zero_shot?: boolean): any[];
}
/**
* @typedef {object} SamImageProcessorResult
* @property {Tensor} pixel_values
* @property {HeightWidth[]} original_sizes
* @property {HeightWidth[]} reshaped_input_sizes
* @property {Tensor} [input_points]
* @property {Tensor} [input_labels]
*/
export class SamImageProcessor extends ImageFeatureExtractor {
/**
*
* @param {any} input_points
* @param {HeightWidth[]} original_sizes
* @param {HeightWidth[]} reshaped_input_sizes
* @returns {Tensor}
*/
reshape_input_points(input_points: any, original_sizes: HeightWidth[], reshaped_input_sizes: HeightWidth[]): Tensor;
/**
*
* @param {any} input_labels
* @param {Tensor} input_points
* @returns {Tensor}
*/
add_input_labels(input_labels: any, input_points: Tensor): Tensor;
/**
* @param {any[]} images The URL(s) of the image(s) to extract features from.
* @param {any} [input_points] 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} [input_labels] 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]`.
* @returns {Promise<SamImageProcessorResult>}
*/
_call(images: any[], input_points?: any, input_labels?: any): Promise<SamImageProcessorResult>;
/**
* 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[][]} 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[][]} 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 {Tensor[]} Batched masks in batch_size, num_channels, height, width) format, where (height, width) is given by original_size.
*/
post_process_masks(masks: Tensor, original_sizes: number[][], reshaped_input_sizes: number[][], { mask_threshold, binarize, pad_size, }?: {
mask_threshold?: number;
binarize?: boolean;
pad_size?: {
height?: number;
width?: number;
};
}): Tensor[];
}
export class Swin2SRImageProcessor extends ImageFeatureExtractor {
pad_image(pixelData: any, imgDims: any, padSize: any, options?: {}): [Float32Array, number[]];
}
export 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<ImageFeatureExtractorResult>} An object containing the concatenated pixel values of the preprocessed images.
*/
_call(images: RawImage[], trimaps: RawImage[]): Promise<ImageFeatureExtractorResult>;
}
export class WhisperFeatureExtractor extends FeatureExtractor {
constructor(config: any);
window: Float64Array;
/**
* Computes the log-Mel spectrogram of the provided audio waveform.
* @param {Float32Array|Float64Array} waveform The audio waveform to process.
* @returns {{data: Float32Array, dims: number[]}} An object containing the log-Mel spectrogram data as a Float32Array and its dimensions as an array of numbers.
*/
_extract_fbank_features(waveform: Float32Array | Float64Array): {
data: Float32Array;
dims: number[];
};
/**
* 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.
*/
_call(audio: Float32Array | Float64Array): Promise<{
input_features: Tensor;
}>;
}
export class Wav2Vec2FeatureExtractor extends FeatureExtractor {
/**
* @param {Float32Array} input_values
* @returns {Float32Array}
*/
_zero_mean_unit_var_norm(input_values: Float32Array): Float32Array;
/**
* 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.
*/
_call(audio: Float32Array | Float64Array): Promise<{
input_values: Tensor;
attention_mask: Tensor;
}>;
}
export class SeamlessM4TFeatureExtractor extends FeatureExtractor {
constructor(config: any);
mel_filters: number[][];
window: Float64Array;
/**
* 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 {{data: Float32Array, dims: number[]}} An object containing the log-Mel spectrogram data as a Float32Array and its dimensions as an array of numbers.
*/
_extract_fbank_features(waveform: Float32Array | Float64Array, max_length: number): {
data: Float32Array;
dims: number[];
};
/**
* 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.
*/
_call(audio: Float32Array | Float64Array, { padding, pad_to_multiple_of, do_normalize_per_mel_bins, return_attention_mask, }?: {
padding?: boolean;
pad_to_multiple_of?: number;
do_normalize_per_mel_bins?: boolean;
return_attention_mask?: boolean;
}): Promise<{
input_features: Tensor;
attention_mask?: Tensor;
}>;
}
export class ASTFeatureExtractor extends FeatureExtractor {
constructor(config: any);
mel_filters: number[][];
window: Float64Array;
mean: any;
std: any;
/**
* 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 {{data: Float32Array, dims: number[]}} An object containing the log-Mel spectrogram data as a Float32Array and its dimensions as an array of numbers.
*/
_extract_fbank_features(waveform: Float32Array | Float64Array, max_length: number): {
data: Float32Array;
dims: number[];
};
/**
* 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.
*/
_call(audio: Float32Array | Float64Array): Promise<{
input_values: Tensor;
}>;
}
export class ClapFeatureExtractor extends FeatureExtractor {
constructor(config: any);
mel_filters: number[][];
mel_filters_slaney: number[][];
window: Float64Array;
/**
* 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 {{ data: Float32Array; dims: number[]; longer: boolean; }} 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.
*/
_get_input_mel(waveform: Float32Array | Float64Array, max_length: number, truncation: string, padding: string): {
data: Float32Array;
dims: number[];
longer: boolean;
};
/**
* 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 {{data: Float32Array, dims: number[]}} An object containing the log-Mel spectrogram data as a Float32Array and its dimensions as an array of numbers.
*/
_extract_fbank_features(waveform: Float32Array | Float64Array, mel_filters: number[][], max_length?: number): {
data: Float32Array;
dims: number[];
};
/**
* 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.
*/
_call(audio: Float32Array | Float64Array, { max_length, }?: {
max_length?: any;
}): Promise<{
input_features: Tensor;
}>;
}
export class SpeechT5FeatureExtractor extends FeatureExtractor {
}
declare const Processor_base: new () => {
(...args: any[]): any;
_call(...args: any[]): any;
};
/**
* Represents a Processor that extracts features from an input.
* @extends Callable
*/
export class Processor extends Processor_base {
/**
* 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: FeatureExtractor);
feature_extractor: FeatureExtractor;
/**
* 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<any>} A Promise that resolves with the extracted features.
*/
_call(input: any, ...args: any[]): Promise<any>;
}
export class SamProcessor extends Processor {
/**
* @borrows SamImageProcessor#_call as _call
*/
_call(...args: any[]): Promise<any>;
/**
* @borrows SamImageProcessor#post_process_masks as post_process_masks
*/
post_process_masks(...args: any[]): any;
/**
* @borrows SamImageProcessor#reshape_input_points as reshape_input_points
*/
reshape_input_points(...args: any[]): any;
}
/**
* Represents a WhisperProcessor that extracts features from an audio input.
* @extends Processor
*/
export 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<any>} A Promise that resolves with the extracted features.
*/
_call(audio: any): Promise<any>;
}
export 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<any>} A Promise that resolves with the extracted features.
*/
_call(audio: any): Promise<any>;
}
export class SpeechT5Processor extends Processor {
/**
* Calls the feature_extractor function with the given input.
* @param {any} input The input to extract features from.
* @returns {Promise<any>} A Promise that resolves with the extracted features.
*/
_call(input: any): Promise<any>;
}
export class OwlViTProcessor extends Processor {
}
/**
* Helper class which is used to instantiate pretrained processors with the `from_pretrained` function.
* The chosen processor class is determined by the type specified in the processor config.
*
* **Example:** Load a processor using `from_pretrained`.
* ```javascript
* let processor = await AutoProcessor.from_pretrained('openai/whisper-tiny.en');
* ```
*
* **Example:** Run an image through a processor.
* ```javascript
* let processor = await AutoProcessor.from_pretrained('Xenova/clip-vit-base-patch16');
* let image = await RawImage.read('https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/football-match.jpg');
* let image_inputs = await processor(image);
* // {
* // "pixel_values": {
* // "dims": [ 1, 3, 224, 224 ],
* // "type": "float32",
* // "data": Float32Array [ -1.558687686920166, -1.558687686920166, -1.5440893173217773, ... ],
* // "size": 150528
* // },
* // "original_sizes": [
* // [ 533, 800 ]
* // ],
* // "reshaped_input_sizes": [
* // [ 224, 224 ]
* // ]
* // }
* ```
*/
export class AutoProcessor {
static FEATURE_EXTRACTOR_CLASS_MAPPING: {
ImageFeatureExtractor: typeof ImageFeatureExtractor;
WhisperFeatureExtractor: typeof WhisperFeatureExtractor;
ViTFeatureExtractor: typeof ViTFeatureExtractor;
MobileViTFeatureExtractor: typeof MobileViTFeatureExtractor;
MobileViTImageProcessor: typeof MobileViTImageProcessor;
OwlViTFeatureExtractor: typeof OwlViTFeatureExtractor;
Owlv2ImageProcessor: typeof Owlv2ImageProcessor;
CLIPFeatureExtractor: typeof CLIPFeatureExtractor;
ChineseCLIPFeatureExtractor: typeof ChineseCLIPFeatureExtractor;
SiglipImageProcessor: typeof SiglipImageProcessor;
ConvNextFeatureExtractor: typeof ConvNextFeatureExtractor;
ConvNextImageProcessor: typeof ConvNextImageProcessor;
SegformerFeatureExtractor: typeof SegformerFeatureExtractor;
BitImageProcessor: typeof BitImageProcessor;
DPTImageProcessor: typeof DPTImageProcessor;
DPTFeatureExtractor: typeof DPTFeatureExtractor;
GLPNFeatureExtractor: typeof GLPNFeatureExtractor;
BeitFeatureExtractor: typeof BeitFeatureExtractor;
DeiTFeatureExtractor: typeof DeiTFeatureExtractor;
DetrFeatureExtractor: typeof DetrFeatureExtractor;
YolosFeatureExtractor: typeof YolosFeatureExtractor;
DonutFeatureExtractor: typeof DonutFeatureExtractor;
NougatImageProcessor: typeof NougatImageProcessor;
EfficientNetImageProcessor: typeof EfficientNetImageProcessor;
ViTImageProcessor: typeof ViTImageProcessor;
VitMatteImageProcessor: typeof VitMatteImageProcessor;
SamImageProcessor: typeof SamImageProcessor;
Swin2SRImageProcessor: typeof Swin2SRImageProcessor;
Wav2Vec2FeatureExtractor: typeof Wav2Vec2FeatureExtractor;
SeamlessM4TFeatureExtractor: typeof SeamlessM4TFeatureExtractor;
SpeechT5FeatureExtractor: typeof SpeechT5FeatureExtractor;
ASTFeatureExtractor: typeof ASTFeatureExtractor;
ClapFeatureExtractor: typeof ClapFeatureExtractor;
};
static PROCESSOR_CLASS_MAPPING: {
WhisperProcessor: typeof WhisperProcessor;
Wav2Vec2ProcessorWithLM: typeof Wav2Vec2ProcessorWithLM;
SamProcessor: typeof SamProcessor;
SpeechT5Processor: typeof SpeechT5Processor;
OwlViTProcessor: typeof OwlViTProcessor;
};
/**
* 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<Processor>} A new instance of the Processor class.
*/
static from_pretrained(pretrained_model_name_or_path: string, { progress_callback, config, cache_dir, local_files_only, revision, }?: import('./utils/hub.js').PretrainedOptions): Promise<Processor>;
}
/**
* Named tuple to indicate the order we are using is (height x width), even though
* the Graphics industry standard is (width x height).
*/
export type HeightWidth = [height: number, width: number];
export type ImageFeatureExtractorResult = {
/**
* The pixel values of the batched preprocessed images.
*/
pixel_values: Tensor;
/**
* Array of two-dimensional tuples like [[480, 640]].
*/
original_sizes: HeightWidth[];
/**
* Array of two-dimensional tuples like [[1000, 1330]].
*/
reshaped_input_sizes: HeightWidth[];
};
export type DetrFeatureExtractorResultProps = {
pixel_mask: Tensor;
};
export type DetrFeatureExtractorResult = ImageFeatureExtractorResult & DetrFeatureExtractorResultProps;
export type SamImageProcessorResult = {
pixel_values: Tensor;
original_sizes: HeightWidth[];
reshaped_input_sizes: HeightWidth[];
input_points?: Tensor;
input_labels?: Tensor;
};
import { RawImage } from './utils/image.js';
import { Tensor } from './utils/tensor.js';
export {};
//# sourceMappingURL=processors.d.ts.map

File diff suppressed because one or more lines are too long

932
node_modules/@xenova/transformers/types/tokenizers.d.ts generated vendored Normal file
View File

@@ -0,0 +1,932 @@
declare const TokenizerModel_base: new () => {
(...args: any[]): any;
_call(...args: any[]): any;
};
/**
* Abstract base class for tokenizer models.
*
* @extends Callable
*/
export class TokenizerModel extends TokenizerModel_base {
/**
* 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: any, ...args: any[]): TokenizerModel;
/**
* Creates a new instance of TokenizerModel.
* @param {Object} config The configuration object for the TokenizerModel.
*/
constructor(config: any);
config: any;
/** @type {string[]} */
vocab: string[];
/**
* A mapping of tokens to ids.
* @type {Map<string, number>}
*/
tokens_to_ids: Map<string, number>;
unk_token_id: any;
unk_token: any;
end_of_word_suffix: any;
/** @type {boolean} Whether to fuse unknown tokens when encoding. Defaults to false. */
fuse_unk: boolean;
/**
* Internal function to call the TokenizerModel instance.
* @param {string[]} tokens The tokens to encode.
* @returns {string[]} The encoded token IDs.
*/
_call(tokens: string[]): string[];
/**
* 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: string[]): string[];
/**
* 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: string[]): number[];
/**
* Converts a list of token IDs into a list of tokens.
* @param {number[]} ids The token IDs to convert.
* @returns {string[]} The converted tokens.
*/
convert_ids_to_tokens(ids: number[]): string[];
}
declare const PreTrainedTokenizer_base: new () => {
(...args: any[]): any;
_call(...args: any[]): any;
};
/**
* @typedef {Object} Message
* @property {string} role The role of the message (e.g., "user" or "assistant" or "system").
* @property {string} content The content of the message.
*/
export class PreTrainedTokenizer extends PreTrainedTokenizer_base {
/**
* 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<PreTrainedTokenizer>} A new instance of the `PreTrainedTokenizer` class.
*/
static from_pretrained(pretrained_model_name_or_path: string, { progress_callback, config, cache_dir, local_files_only, revision, legacy, }?: PretrainedTokenizerOptions): Promise<PreTrainedTokenizer>;
/**
* Create a new PreTrainedTokenizer instance.
* @param {Object} tokenizerJSON The JSON of the tokenizer.
* @param {Object} tokenizerConfig The config of the tokenizer.
*/
constructor(tokenizerJSON: any, tokenizerConfig: any);
return_token_type_ids: boolean;
_default_chat_template: string;
_tokenizer_config: any;
normalizer: Normalizer;
pre_tokenizer: PreTokenizer;
model: TokenizerModel;
post_processor: PostProcessor;
decoder: Decoder;
special_tokens: any[];
all_special_ids: number[];
/** @type {AddedToken[]} */
added_tokens: AddedToken[];
additional_special_tokens: any;
added_tokens_regex: RegExp;
mask_token: string;
mask_token_id: number;
pad_token: string;
pad_token_id: number;
sep_token: string;
sep_token_id: number;
unk_token: string;
unk_token_id: number;
model_max_length: any;
/** @type {boolean} Whether or not to strip the text when tokenizing (removing excess spaces before and after the string). */
remove_space: boolean;
clean_up_tokenization_spaces: any;
do_lowercase_and_remove_accent: any;
/** @type {'right'|'left'} */
padding_side: 'right' | 'left';
legacy: boolean;
chat_template: any;
_compiled_template_cache: Map<any, any>;
/**
* 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".
*/
getToken(...keys: string[]): string | null;
/**
* @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: string | string[], { text_pair, add_special_tokens, padding, truncation, max_length, return_tensor, return_token_type_ids, }?: {
text_pair?: string | string[];
padding?: boolean | 'max_length';
add_special_tokens?: boolean;
truncation?: boolean;
max_length?: number;
return_tensor?: boolean;
return_token_type_ids?: boolean;
}): {
/**
* List of token ids to be fed to a model.
*/
input_ids: number[] | Tensor | number[][];
/**
* List of indices specifying which tokens should be attended to by the model.
*/
attention_mask: number[] | Tensor | number[][];
/**
* List of token type ids to be fed to a model.
*/
token_type_ids?: number[] | Tensor | number[][];
};
/**
* 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: string | null): string[] | null;
/**
* Encodes a single text or a pair of texts using the model's tokenizer.
*
* @param {string} text The text to encode.
* @param {string|null} text_pair The optional second text to encode.
* @param {Object} options An optional object containing the following properties:
* @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
*/
private _encode_plus;
/**
* Encodes a single text or a pair of texts using the model's tokenizer.
*
* @param {string} text The text to encode.
* @param {string|null} text_pair The optional second text to encode.
* @param {Object} options An optional object containing the following properties:
* @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: string, text_pair?: string | null, { add_special_tokens, return_token_type_ids, }?: {
add_special_tokens?: boolean;
return_token_type_ids?: boolean;
}): number[];
/**
* 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: number[][] | Tensor, decode_args?: any): string[];
/**
* Decodes a sequence of token IDs back to a string.
*
* @param {number[]|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: number[] | Tensor, decode_args?: {
skip_special_tokens?: boolean;
clean_up_tokenization_spaces?: boolean;
}): string;
/**
* Decode a single list of token ids to a string.
* @param {number[]} 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: number[], { skip_special_tokens, clean_up_tokenization_spaces, }: {
skip_special_tokens?: boolean;
clean_up_tokenization_spaces?: boolean;
}): string;
get default_chat_template(): string;
_warned_about_chat_template: boolean;
/**
* 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. When chat_template is None, it will fall back
* to the default_chat_template specified at the class level.
*
* See [here](https://huggingface.co/docs/transformers/chat_templating) for more information.
*
* **Example:** Applying a chat template to a conversation.
*
* ```javascript
* import { AutoTokenizer } from "@xenova/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 });
* // "<s>[INST] Hello, how are you? [/INST]I'm doing great. How can I help you today?</s> [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.
* @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 default chat template will be used instead.
* @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 {Object} [options.tokenizer_kwargs={}] Additional options to pass to the tokenizer.
* @returns {string | Tensor | number[]| number[][]} The tokenized output.
*/
apply_chat_template(conversation: Message[], { chat_template, add_generation_prompt, tokenize, padding, truncation, max_length, return_tensor, tokenizer_kwargs, ...kwargs }?: {
chat_template?: string;
add_generation_prompt?: boolean;
tokenize?: boolean;
padding?: boolean;
truncation?: boolean;
max_length?: number;
return_tensor?: boolean;
tokenizer_kwargs?: any;
}): string | Tensor | number[] | number[][];
}
/**
* BertTokenizer is a class used to tokenize text for BERT models.
* @extends PreTrainedTokenizer
*/
export class BertTokenizer extends PreTrainedTokenizer {
}
/**
* Albert tokenizer
* @extends PreTrainedTokenizer
*/
export class AlbertTokenizer extends PreTrainedTokenizer {
}
export class MobileBertTokenizer extends PreTrainedTokenizer {
}
export class SqueezeBertTokenizer extends PreTrainedTokenizer {
}
export class DebertaTokenizer extends PreTrainedTokenizer {
}
export class DebertaV2Tokenizer extends PreTrainedTokenizer {
}
export class HerbertTokenizer extends PreTrainedTokenizer {
}
export class ConvBertTokenizer extends PreTrainedTokenizer {
}
export class RoFormerTokenizer extends PreTrainedTokenizer {
}
export class DistilBertTokenizer extends PreTrainedTokenizer {
}
export class CamembertTokenizer extends PreTrainedTokenizer {
}
export class XLMTokenizer extends PreTrainedTokenizer {
constructor(tokenizerJSON: any, tokenizerConfig: any);
}
export class ElectraTokenizer extends PreTrainedTokenizer {
}
export class T5Tokenizer extends PreTrainedTokenizer {
}
export class GPT2Tokenizer extends PreTrainedTokenizer {
}
export class BartTokenizer extends PreTrainedTokenizer {
}
export class MBartTokenizer extends PreTrainedTokenizer {
constructor(tokenizerJSON: any, tokenizerConfig: any);
languageRegex: RegExp;
language_codes: any[];
lang_to_token: (x: any) => any;
/**
* 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: string | string[], tokenizer_options: any, generate_kwargs: any): any;
}
export class MBart50Tokenizer extends MBartTokenizer {
}
export class RobertaTokenizer extends PreTrainedTokenizer {
}
export class BloomTokenizer extends GPT2Tokenizer {
constructor(tokenizerJSON: any, tokenizerConfig: any);
}
export class LlamaTokenizer extends PreTrainedTokenizer {
constructor(tokenizerJSON: any, tokenizerConfig: any);
DEFAULT_SYSTEM_PROMPT: string;
use_default_system_prompt: any;
legacy: any;
get default_chat_template(): any;
}
export class CodeLlamaTokenizer extends LlamaTokenizer {
}
export class XLMRobertaTokenizer extends PreTrainedTokenizer {
}
export class MPNetTokenizer extends PreTrainedTokenizer {
}
export class FalconTokenizer extends PreTrainedTokenizer {
}
export class GPTNeoXTokenizer extends PreTrainedTokenizer {
}
export class EsmTokenizer extends PreTrainedTokenizer {
}
export class Qwen2Tokenizer extends PreTrainedTokenizer {
}
export class GemmaTokenizer extends PreTrainedTokenizer {
}
export class Grok1Tokenizer extends PreTrainedTokenizer {
}
/**
* The NllbTokenizer class is used to tokenize text for NLLB ("No Language Left Behind") models.
*
* No Language Left Behind (NLLB) is a first-of-its-kind, AI breakthrough project
* that open-sources models capable of delivering high-quality translations directly
* between any pair of 200+ languages — including low-resource languages like Asturian,
* Luganda, Urdu and more. It aims to help people communicate with anyone, anywhere,
* regardless of their language preferences. For more information, check out their
* [paper](https://arxiv.org/abs/2207.04672).
*
* For a list of supported languages (along with their language codes),
* @see {@link https://github.com/facebookresearch/flores/blob/main/flores200/README.md#languages-in-flores-200}
*/
export class NllbTokenizer extends PreTrainedTokenizer {
constructor(tokenizerJSON: any, tokenizerConfig: any);
languageRegex: RegExp;
language_codes: any[];
lang_to_token: (x: any) => any;
/**
* 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: string | string[], tokenizer_options: any, generate_kwargs: any): any;
}
/**
* The M2M100Tokenizer class is used to tokenize text for M2M100 ("Many-to-Many") models.
*
* M2M100 is a multilingual encoder-decoder (seq-to-seq) model trained for Many-to-Many
* multilingual translation. It was introduced in this [paper](https://arxiv.org/abs/2010.11125)
* and first released in [this](https://github.com/pytorch/fairseq/tree/master/examples/m2m_100) repository.
*
* For a list of supported languages (along with their language codes),
* @see {@link https://huggingface.co/facebook/m2m100_418M#languages-covered}
*/
export class M2M100Tokenizer extends PreTrainedTokenizer {
constructor(tokenizerJSON: any, tokenizerConfig: any);
languageRegex: RegExp;
language_codes: any[];
lang_to_token: (x: any) => string;
/**
* 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: string | string[], tokenizer_options: any, generate_kwargs: any): any;
}
/**
* WhisperTokenizer tokenizer
* @extends PreTrainedTokenizer
*/
export class WhisperTokenizer extends PreTrainedTokenizer {
/**
* Decodes automatic speech recognition (ASR) sequences.
* @param {Array<{tokens: number[], token_timestamps?: number[], stride: number[]}>} sequences The sequences to decode.
* @param {Object} options The options to use for decoding.
* @returns {Array<string|{chunks?: undefined|Array<{language: string|null, timestamp: Array<number|null>, text: string}>}>} The decoded sequences.
*/
_decode_asr(sequences: Array<{
tokens: number[];
token_timestamps?: number[];
stride: number[];
}>, { return_timestamps, return_language, time_precision, force_full_sequences }?: any): (string | {
chunks?: undefined | Array<{
language: string | null;
timestamp: Array<number | null>;
text: string;
}>;
})[];
/**
* 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
*/
private findLongestCommonSequence;
/** @private */
private collateWordTimestamps;
/**
* 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
*/
private combineTokensIntoWords;
/**
* @param {number[]} token_ids List of token IDs to decode.
* @param {Object} decode_args Optional arguments for decoding
* @private
*/
private decodeWithTimestamps;
/**
* Combine tokens into words by splitting at any position where the tokens are decoded as valid unicode points.
* @param {number[]} tokens
* @returns {*}
* @private
*/
private splitTokensOnUnicode;
/**
* Combine tokens into words by splitting at whitespace and punctuation tokens.
* @param {number[]} tokens
* @private
*/
private splitTokensOnSpaces;
/**
* Merges punctuation tokens with neighboring words.
* @param {string[]} words
* @param {number[][]} tokens
* @param {number[][]} indices
* @param {string} prepended
* @param {string} appended
* @private
*/
private mergePunctuations;
/**
* Helper function to build translation inputs for a `WhisperTokenizer`,
* depending on the language, task, and whether to predict timestamp tokens.
*
* Used to override the prefix tokens appended to the start of the label sequence.
*
* **Example: Get ids for a language**
* ```javascript
* // instantiate the tokenizer and set the prefix token to Spanish
* const tokenizer = await WhisperTokenizer.from_pretrained('Xenova/whisper-tiny');
* const forced_decoder_ids = tokenizer.get_decoder_prompt_ids({ language: 'spanish' });
* // [(1, 50262), (2, 50363)]
* ```
*
* @param {Object} options Options to generate the decoder prompt.
* @param {string} [options.language] The language of the transcription text.
* The corresponding language id token is appended to the start of the sequence for multilingual
* speech recognition and speech translation tasks, e.g. for "Spanish" the token "<|es|>" is appended
* to the start of sequence.
* @param {string} [options.task] Task identifier to append at the start of sequence (if any).
* This should be used for mulitlingual fine-tuning, with "transcribe" for speech recognition and
* "translate" for speech translation.
* @param {boolean} [options.no_timestamps] Whether to add the <|notimestamps|> token at the start of the sequence.
* @returns {number[][]} The decoder prompt ids.
*/
get_decoder_prompt_ids({ language, task, no_timestamps, }?: {
language?: string;
task?: string;
no_timestamps?: boolean;
}): number[][];
}
export class CodeGenTokenizer extends PreTrainedTokenizer {
}
export class CLIPTokenizer extends PreTrainedTokenizer {
}
export class SiglipTokenizer extends PreTrainedTokenizer {
}
/**
* @todo This model is not yet supported by Hugging Face's "fast" tokenizers library (https://github.com/huggingface/tokenizers).
* Therefore, this implementation (which is based on fast tokenizers) may produce slightly inaccurate results.
*/
export class MarianTokenizer extends PreTrainedTokenizer {
languageRegex: RegExp;
supported_language_codes: string[];
/**
* 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: string | null): any[];
}
export class Wav2Vec2CTCTokenizer extends PreTrainedTokenizer {
}
export class BlenderbotTokenizer extends PreTrainedTokenizer {
}
export class BlenderbotSmallTokenizer extends BlenderbotTokenizer {
}
export class SpeechT5Tokenizer extends PreTrainedTokenizer {
}
export class NougatTokenizer extends PreTrainedTokenizer {
}
export class VitsTokenizer extends PreTrainedTokenizer {
constructor(tokenizerJSON: any, tokenizerConfig: any);
}
export class CohereTokenizer extends PreTrainedTokenizer {
}
/**
* Helper class which is used to instantiate pretrained tokenizers with the `from_pretrained` function.
* The chosen tokenizer class is determined by the type specified in the tokenizer config.
*
* @example
* const tokenizer = await AutoTokenizer.from_pretrained('Xenova/bert-base-uncased');
*/
export class AutoTokenizer {
static TOKENIZER_CLASS_MAPPING: {
T5Tokenizer: typeof T5Tokenizer;
DistilBertTokenizer: typeof DistilBertTokenizer;
CamembertTokenizer: typeof CamembertTokenizer;
DebertaTokenizer: typeof DebertaTokenizer;
DebertaV2Tokenizer: typeof DebertaV2Tokenizer;
BertTokenizer: typeof BertTokenizer;
HerbertTokenizer: typeof HerbertTokenizer;
ConvBertTokenizer: typeof ConvBertTokenizer;
RoFormerTokenizer: typeof RoFormerTokenizer;
XLMTokenizer: typeof XLMTokenizer;
ElectraTokenizer: typeof ElectraTokenizer;
MobileBertTokenizer: typeof MobileBertTokenizer;
SqueezeBertTokenizer: typeof SqueezeBertTokenizer;
AlbertTokenizer: typeof AlbertTokenizer;
GPT2Tokenizer: typeof GPT2Tokenizer;
BartTokenizer: typeof BartTokenizer;
MBartTokenizer: typeof MBartTokenizer;
MBart50Tokenizer: typeof MBart50Tokenizer;
RobertaTokenizer: typeof RobertaTokenizer;
WhisperTokenizer: typeof WhisperTokenizer;
CodeGenTokenizer: typeof CodeGenTokenizer;
CLIPTokenizer: typeof CLIPTokenizer;
SiglipTokenizer: typeof SiglipTokenizer;
MarianTokenizer: typeof MarianTokenizer;
BloomTokenizer: typeof BloomTokenizer;
NllbTokenizer: typeof NllbTokenizer;
M2M100Tokenizer: typeof M2M100Tokenizer;
LlamaTokenizer: typeof LlamaTokenizer;
CodeLlamaTokenizer: typeof CodeLlamaTokenizer;
XLMRobertaTokenizer: typeof XLMRobertaTokenizer;
MPNetTokenizer: typeof MPNetTokenizer;
FalconTokenizer: typeof FalconTokenizer;
GPTNeoXTokenizer: typeof GPTNeoXTokenizer;
EsmTokenizer: typeof EsmTokenizer;
Wav2Vec2CTCTokenizer: typeof Wav2Vec2CTCTokenizer;
BlenderbotTokenizer: typeof BlenderbotTokenizer;
BlenderbotSmallTokenizer: typeof BlenderbotSmallTokenizer;
SpeechT5Tokenizer: typeof SpeechT5Tokenizer;
NougatTokenizer: typeof NougatTokenizer;
VitsTokenizer: typeof VitsTokenizer;
Qwen2Tokenizer: typeof Qwen2Tokenizer;
GemmaTokenizer: typeof GemmaTokenizer;
Grok1Tokenizer: typeof Grok1Tokenizer;
CohereTokenizer: typeof CohereTokenizer;
PreTrainedTokenizer: typeof PreTrainedTokenizer;
};
/**
* Instantiate one of the tokenizer classes of the library from a pretrained model.
*
* The tokenizer class to instantiate is selected based on the `tokenizer_class` property of the config object
* (either passed as an argument or loaded from `pretrained_model_name_or_path` if possible)
*
* @param {string} pretrained_model_name_or_path The name or path of the pretrained model. Can be either:
* - A string, the *model id* of a pretrained tokenizer hosted inside a model repo on huggingface.co.
* Valid model ids can be located at the root-level, like `bert-base-uncased`, or namespaced under a
* user or organization name, like `dbmdz/bert-base-german-cased`.
* - A path to a *directory* containing tokenizer files, e.g., `./my_model_directory/`.
* @param {PretrainedTokenizerOptions} options Additional options for loading the tokenizer.
*
* @returns {Promise<PreTrainedTokenizer>} A new instance of the PreTrainedTokenizer class.
*/
static from_pretrained(pretrained_model_name_or_path: string, { quantized, progress_callback, config, cache_dir, local_files_only, revision, legacy, }?: PretrainedTokenizerOptions): Promise<PreTrainedTokenizer>;
}
/**
* Additional tokenizer-specific properties.
*/
export type TokenizerProperties = {
/**
* Whether or not the `legacy` behavior of the tokenizer should be used.
*/
legacy?: boolean;
};
export type PretrainedTokenizerOptions = import('./utils/hub.js').PretrainedOptions & TokenizerProperties;
export type BPENode = {
/**
* The token associated with the node
*/
token: string;
/**
* A positional bias for the node.
*/
bias: number;
/**
* The score of the node.
*/
score?: number;
/**
* The previous node in the linked list.
*/
prev?: BPENode;
/**
* The next node in the linked list.
*/
next?: BPENode;
};
export type SplitDelimiterBehavior = 'removed' | 'isolated' | 'mergedWithPrevious' | 'mergedWithNext' | 'contiguous';
export type PostProcessedOutput = {
/**
* List of token produced by the post-processor.
*/
tokens: string[];
/**
* List of token type ids produced by the post-processor.
*/
token_type_ids?: number[];
};
export type EncodingSingle = {
/**
* List of token ids to be fed to a model.
*/
input_ids: number[];
/**
* List of token type ids to be fed to a model
*/
attention_mask: number[];
/**
* List of indices specifying which tokens should be attended to by the model
*/
token_type_ids?: number[];
};
export type Message = {
/**
* The role of the message (e.g., "user" or "assistant" or "system").
*/
role: string;
/**
* The content of the message.
*/
content: string;
};
declare const Normalizer_base: new () => {
(...args: any[]): any;
_call(...args: any[]): any;
};
/**
* A base class for text normalization.
* @abstract
*/
declare class Normalizer extends Normalizer_base {
/**
* 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: any): Normalizer;
/**
* @param {Object} config The configuration object for the normalizer.
*/
constructor(config: any);
config: any;
/**
* 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: string): string;
/**
* Alias for {@link Normalizer#normalize}.
* @param {string} text The text to normalize.
* @returns {string} The normalized text.
*/
_call(text: string): string;
}
declare const PreTokenizer_base: new () => {
(...args: any[]): any;
_call(...args: any[]): any;
};
/**
* A callable class representing a pre-tokenizer used in tokenization. Subclasses
* should implement the `pre_tokenize_text` method to define the specific pre-tokenization logic.
* @extends Callable
*/
declare class PreTokenizer extends PreTokenizer_base {
/**
* 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: any): PreTokenizer;
/**
* 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: string, options?: any): string[];
/**
* 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: string | string[], options?: any): string[];
/**
* 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: string | string[], options?: any): string[];
}
declare const PostProcessor_base: new () => {
(...args: any[]): any;
_call(...args: any[]): any;
};
/**
* @typedef {Object} PostProcessedOutput
* @property {string[]} tokens List of token produced by the post-processor.
* @property {number[]} [token_type_ids] List of token type ids produced by the post-processor.
*/
/**
* @typedef {Object} EncodingSingle
* @property {number[]} input_ids List of token ids to be fed to a model.
* @property {number[]} attention_mask List of token type ids to be fed to a model
* @property {number[]} [token_type_ids] List of indices specifying which tokens should be attended to by the model
*/
/**
* @extends Callable
*/
declare class PostProcessor extends PostProcessor_base {
/**
* 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: any): PostProcessor;
/**
* @param {Object} config The configuration for the post-processor.
*/
constructor(config: any);
config: any;
/**
* 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: any[], ...args: any[]): PostProcessedOutput;
/**
* 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: any[], ...args: any[]): PostProcessedOutput;
}
declare const Decoder_base: new () => {
(...args: any[]): any;
_call(...args: any[]): any;
};
/**
* The base class for token decoders.
* @extends Callable
*/
declare class Decoder extends Decoder_base {
/**
* 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: any): Decoder;
/**
* Creates an instance of `Decoder`.
*
* @param {Object} config The configuration object.
*/
constructor(config: any);
config: any;
/** @type {AddedToken[]} */
added_tokens: AddedToken[];
end_of_word_suffix: any;
trim_offsets: any;
/**
* Calls the `decode` method.
*
* @param {string[]} tokens The list of tokens.
* @returns {string} The decoded string.
*/
_call(tokens: string[]): string;
/**
* Decodes a list of tokens.
* @param {string[]} tokens The list of tokens.
* @returns {string} The decoded string.
*/
decode(tokens: string[]): string;
/**
* 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: string[]): string[];
}
/**
* Represent a token added by the user on top of the existing Model vocabulary.
* AddedToken can be configured to specify the behavior they should have in various situations like:
* - Whether they should only match single words
* - Whether to include any whitespace on its left or right
*/
declare 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: {
content: string;
id: number;
single_word?: boolean;
lstrip?: boolean;
rstrip?: boolean;
normalized?: boolean;
special?: boolean;
});
content: string;
id: number;
single_word: boolean;
lstrip: boolean;
rstrip: boolean;
special: boolean;
normalized: boolean;
}
import { Tensor } from './utils/tensor.js';
export {};
//# sourceMappingURL=tokenizers.d.ts.map

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,11 @@
export * from "./pipelines.js";
export * from "./env.js";
export * from "./models.js";
export * from "./tokenizers.js";
export * from "./processors.js";
export * from "./configs.js";
export * from "./utils/audio.js";
export * from "./utils/image.js";
export * from "./utils/tensor.js";
export * from "./utils/maths.js";
//# sourceMappingURL=transformers.d.ts.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"transformers.d.ts","sourceRoot":"","sources":["../src/transformers.js"],"names":[],"mappings":""}

View File

@@ -0,0 +1,121 @@
/**
* Helper function to read audio from a path/URL.
* @param {string|URL} url The path/URL to load the audio from.
* @param {number} sampling_rate The sampling rate to use when decoding the audio.
* @returns {Promise<Float32Array>} The decoded audio as a `Float32Array`.
*/
export function read_audio(url: string | URL, sampling_rate: number): Promise<Float32Array>;
/**
* Generates a Hanning window of length M.
*
* @param {number} M The length of the Hanning window to generate.
* @returns {Float64Array} The generated Hanning window.
*/
export function hanning(M: number): Float64Array;
/**
* Creates a frequency bin conversion matrix used to obtain a mel spectrogram. This is called a *mel filter bank*, and
* various implementation exist, which differ in the number of filters, the shape of the filters, the way the filters
* are spaced, the bandwidth of the filters, and the manner in which the spectrum is warped. The goal of these
* features is to approximate the non-linear human perception of the variation in pitch with respect to the frequency.
* @param {number} num_frequency_bins Number of frequencies used to compute the spectrogram (should be the same as in `stft`).
* @param {number} num_mel_filters Number of mel filters to generate.
* @param {number} min_frequency Lowest frequency of interest in Hz.
* @param {number} max_frequency Highest frequency of interest in Hz. This should not exceed `sampling_rate / 2`.
* @param {number} sampling_rate Sample rate of the audio waveform.
* @param {string} [norm] If `"slaney"`, divide the triangular mel weights by the width of the mel band (area normalization).
* @param {string} [mel_scale] The mel frequency scale to use, `"htk"` or `"slaney"`.
* @param {boolean} [triangularize_in_mel_space] If this option is enabled, the triangular filter is applied in mel space rather than frequency space.
* This should be set to `true` in order to get the same results as `torchaudio` when computing mel filters.
* @returns {number[][]} Triangular filter bank matrix, which is a 2D array of shape (`num_frequency_bins`, `num_mel_filters`).
* This is a projection matrix to go from a spectrogram to a mel spectrogram.
*/
export function mel_filter_bank(num_frequency_bins: number, num_mel_filters: number, min_frequency: number, max_frequency: number, sampling_rate: number, norm?: string, mel_scale?: string, triangularize_in_mel_space?: boolean): number[][];
/**
* Calculates a spectrogram over one waveform using the Short-Time Fourier Transform.
*
* This function can create the following kinds of spectrograms:
* - amplitude spectrogram (`power = 1.0`)
* - power spectrogram (`power = 2.0`)
* - complex-valued spectrogram (`power = None`)
* - log spectrogram (use `log_mel` argument)
* - mel spectrogram (provide `mel_filters`)
* - log-mel spectrogram (provide `mel_filters` and `log_mel`)
*
* In this implementation, the window is assumed to be zero-padded to have the same size as the analysis frame.
* A padded window can be obtained from `window_function()`. The FFT input buffer may be larger than the analysis frame,
* typically the next power of two.
*
* @param {Float32Array|Float64Array} waveform The input waveform of shape `(length,)`. This must be a single real-valued, mono waveform.
* @param {Float32Array|Float64Array} window The windowing function to apply of shape `(frame_length,)`, including zero-padding if necessary. The actual window length may be
* shorter than `frame_length`, but we're assuming the array has already been zero-padded.
* @param {number} frame_length The length of the analysis frames in samples (a.k.a., `fft_length`).
* @param {number} hop_length The stride between successive analysis frames in samples.
* @param {Object} options
* @param {number} [options.fft_length=null] The size of the FFT buffer in samples. This determines how many frequency bins the spectrogram will have.
* For optimal speed, this should be a power of two. If `null`, uses `frame_length`.
* @param {number} [options.power=1.0] If 1.0, returns the amplitude spectrogram. If 2.0, returns the power spectrogram. If `null`, returns complex numbers.
* @param {boolean} [options.center=true] Whether to pad the waveform so that frame `t` is centered around time `t * hop_length`. If `false`, frame
* `t` will start at time `t * hop_length`.
* @param {string} [options.pad_mode="reflect"] Padding mode used when `center` is `true`. Possible values are: `"constant"` (pad with zeros),
* `"edge"` (pad with edge values), `"reflect"` (pads with mirrored values).
* @param {boolean} [options.onesided=true] If `true`, only computes the positive frequencies and returns a spectrogram containing `fft_length // 2 + 1`
* frequency bins. If `false`, also computes the negative frequencies and returns `fft_length` frequency bins.
* @param {number} [options.preemphasis=null] Coefficient for a low-pass filter that applies pre-emphasis before the DFT.
* @param {number[][]} [options.mel_filters=null] The mel filter bank of shape `(num_freq_bins, num_mel_filters)`.
* If supplied, applies this filter bank to create a mel spectrogram.
* @param {number} [options.mel_floor=1e-10] Minimum value of mel frequency banks.
* @param {string} [options.log_mel=null] How to convert the spectrogram to log scale. Possible options are:
* `null` (don't convert), `"log"` (take the natural logarithm) `"log10"` (take the base-10 logarithm), `"dB"` (convert to decibels).
* Can only be used when `power` is not `null`.
* @param {number} [options.reference=1.0] Sets the input spectrogram value that corresponds to 0 dB. For example, use `max(spectrogram)[0]` to set
* the loudest part to 0 dB. Must be greater than zero.
* @param {number} [options.min_value=1e-10] The spectrogram will be clipped to this minimum value before conversion to decibels, to avoid taking `log(0)`.
* For a power spectrogram, the default of `1e-10` corresponds to a minimum of -100 dB. For an amplitude spectrogram, the value `1e-5` corresponds to -100 dB.
* Must be greater than zero.
* @param {number} [options.db_range=null] Sets the maximum dynamic range in decibels. For example, if `db_range = 80`, the difference between the
* peak value and the smallest value will never be more than 80 dB. Must be greater than zero.
* @param {boolean} [options.remove_dc_offset=null] Subtract mean from waveform on each frame, applied before pre-emphasis. This should be set to `true` in
* order to get the same results as `torchaudio.compliance.kaldi.fbank` when computing mel filters.
* @param {number} [options.max_num_frames=null] If provided, limits the number of frames to compute to this value.
* @param {boolean} [options.do_pad=true] If `true`, pads the output spectrogram to have `max_num_frames` frames.
* @param {boolean} [options.transpose=false] If `true`, the returned spectrogram will have shape `(num_frames, num_frequency_bins/num_mel_filters)`. If `false`, the returned spectrogram will have shape `(num_frequency_bins/num_mel_filters, num_frames)`.
* @returns {{data: Float32Array, dims: number[]}} Spectrogram of shape `(num_frequency_bins, length)` (regular spectrogram) or shape `(num_mel_filters, length)` (mel spectrogram).
*/
export function spectrogram(waveform: Float32Array | Float64Array, window: Float32Array | Float64Array, frame_length: number, hop_length: number, { fft_length, power, center, pad_mode, onesided, preemphasis, mel_filters, mel_floor, log_mel, reference, min_value, db_range, remove_dc_offset, max_num_frames, do_pad, transpose, }?: {
fft_length?: number;
power?: number;
center?: boolean;
pad_mode?: string;
onesided?: boolean;
preemphasis?: number;
mel_filters?: number[][];
mel_floor?: number;
log_mel?: string;
reference?: number;
min_value?: number;
db_range?: number;
remove_dc_offset?: boolean;
max_num_frames?: number;
do_pad?: boolean;
transpose?: boolean;
}): {
data: Float32Array;
dims: number[];
};
/**
* Returns an array containing the specified window.
* @param {number} window_length The length of the window in samples.
* @param {string} name The name of the window function.
* @param {Object} options Additional options.
* @param {boolean} [options.periodic=true] Whether the window is periodic or symmetric.
* @param {number} [options.frame_length=null] The length of the analysis frames in samples.
* Provide a value for `frame_length` if the window is smaller than the frame length, so that it will be zero-padded.
* @param {boolean} [options.center=true] Whether to center the window inside the FFT buffer. Only used when `frame_length` is provided.
* @returns {Float64Array} The window of shape `(window_length,)` or `(frame_length,)`.
*/
export function window_function(window_length: number, name: string, { periodic, frame_length, center, }?: {
periodic?: boolean;
frame_length?: number;
center?: boolean;
}): Float64Array;
//# sourceMappingURL=audio.d.ts.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"audio.d.ts","sourceRoot":"","sources":["../../src/utils/audio.js"],"names":[],"mappings":"AAkBA;;;;;GAKG;AACH,gCAJW,MAAM,GAAC,GAAG,iBACV,MAAM,GACJ,QAAQ,YAAY,CAAC,CAuDjC;AAED;;;;;GAKG;AACH,2BAHW,MAAM,GACJ,YAAY,CAiBxB;AAqGD;;;;;;;;;;;;;;;;GAgBG;AACH,oDAZW,MAAM,mBACN,MAAM,iBACN,MAAM,iBACN,MAAM,iBACN,MAAM,SACN,MAAM,cACN,MAAM,+BACN,OAAO,GAEL,MAAM,EAAE,EAAE,CAkDtB;AAqHD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkDG;AACH,sCApCW,YAAY,GAAC,YAAY,UACzB,YAAY,GAAC,YAAY,gBAEzB,MAAM,cACN,MAAM;IAEW,UAAU,GAA3B,MAAM;IAEW,KAAK,GAAtB,MAAM;IACY,MAAM,GAAxB,OAAO;IAEU,QAAQ,GAAzB,MAAM;IAEY,QAAQ,GAA1B,OAAO;IAEU,WAAW,GAA5B,MAAM;IACe,WAAW,GAAhC,MAAM,EAAE,EAAE;IAEO,SAAS,GAA1B,MAAM;IACW,OAAO,GAAxB,MAAM;IAGW,SAAS,GAA1B,MAAM;IAEW,SAAS,GAA1B,MAAM;IAGW,QAAQ,GAAzB,MAAM;IAEY,gBAAgB,GAAlC,OAAO;IAEU,cAAc,GAA/B,MAAM;IACY,MAAM,GAAxB,OAAO;IACW,SAAS,GAA3B,OAAO;;UACE,YAAY;UAAQ,MAAM,EAAE;EAmM/C;AAED;;;;;;;;;;GAUG;AACH,+CATW,MAAM,QACN,MAAM;IAEY,QAAQ,GAA1B,OAAO;IACU,YAAY,GAA7B,MAAM;IAEY,MAAM,GAAxB,OAAO;IACL,YAAY,CAkCxB"}

View File

@@ -0,0 +1,99 @@
/**
* @file Core utility functions/classes for Transformers.js.
*
* These are only used internally, meaning an end-user shouldn't
* need to access anything here.
*
* @module utils/core
*/
/**
* Helper function to dispatch progress callbacks.
*
* @param {Function} progress_callback The progress callback function to dispatch.
* @param {any} data The data to pass to the progress callback function.
* @returns {void}
* @private
*/
export function dispatchCallback(progress_callback: Function, data: any): void;
/**
* Reverses the keys and values of an object.
*
* @param {Object} data The object to reverse.
* @returns {Object} The reversed object.
* @see https://ultimatecourses.com/blog/reverse-object-keys-and-values-in-javascript
*/
export function reverseDictionary(data: any): any;
/**
* Escapes regular expression special characters from a string by replacing them with their escaped counterparts.
*
* @param {string} string The string to escape.
* @returns {string} The escaped string.
*/
export function escapeRegExp(string: string): string;
/**
* Check if a value is a typed array.
* @param {*} val The value to check.
* @returns {boolean} True if the value is a `TypedArray`, false otherwise.
*
* Adapted from https://stackoverflow.com/a/71091338/13989043
*/
export function isTypedArray(val: any): boolean;
/**
* Check if a value is an integer.
* @param {*} x The value to check.
* @returns {boolean} True if the value is a string, false otherwise.
*/
export function isIntegralNumber(x: any): boolean;
/**
* Check if a value is exists.
* @param {*} x The value to check.
* @returns {boolean} True if the value exists, false otherwise.
*/
export function exists(x: any): boolean;
/**
* Calculates the dimensions of a nested array.
*
* @param {any[]} arr The nested array to calculate dimensions for.
* @returns {number[]} An array containing the dimensions of the input array.
*/
export function calculateDimensions(arr: any[]): number[];
/**
* Replicate python's .pop() method for objects.
* @param {Object} obj The object to pop from.
* @param {string} key The key to pop.
* @param {*} defaultValue The default value to return if the key does not exist.
* @returns {*} The value of the popped key.
* @throws {Error} If the key does not exist and no default value is provided.
*/
export function pop(obj: any, key: string, defaultValue?: any): any;
/**
* Efficiently merge arrays, creating a new copy.
* Adapted from https://stackoverflow.com/a/6768642/13989043
* @param {Array[]} arrs Arrays to merge.
* @returns {Array} The merged array.
*/
export function mergeArrays(...arrs: any[][]): any[];
/**
* Compute the Cartesian product of given arrays
* @param {...Array} a Arrays to compute the product
* @returns {Array} Returns the computed Cartesian product as an array
* @private
*/
export function product(...a: any[][]): any[];
/**
* Calculates the index offset for a given index and window size.
* @param {number} i The index.
* @param {number} w The window size.
* @returns {number} The index offset.
*/
export function calculateReflectOffset(i: number, w: number): number;
/**
* A base class for creating callable objects.
*
* @type {new () => {(...args: any[]): any, _call(...args: any[]): any}}
*/
export const Callable: new () => {
(...args: any[]): any;
_call(...args: any[]): any;
};
//# sourceMappingURL=core.d.ts.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"core.d.ts","sourceRoot":"","sources":["../../src/utils/core.js"],"names":[],"mappings":"AACA;;;;;;;GAOG;AAEH;;;;;;;GAOG;AACH,oEAJW,GAAG,GACD,IAAI,CAKhB;AAED;;;;;;GAMG;AACH,kDAGC;AAED;;;;;GAKG;AACH,qCAHW,MAAM,GACJ,MAAM,CAIlB;AAoCD;;;;;;GAMG;AACH,wCAJa,OAAO,CAMnB;AAGD;;;;GAIG;AACH,0CAFa,OAAO,CAInB;AAED;;;;GAIG;AACH,gCAFa,OAAO,CAInB;AAED;;;;;GAKG;AACH,yCAHW,GAAG,EAAE,GACH,MAAM,EAAE,CAUpB;AAED;;;;;;;GAOG;AACH,mCALW,MAAM,2BAehB;AAED;;;;;GAKG;AACH,qCAHY,OAAO,SAKlB;AAED;;;;;GAKG;AACH,8CAIC;AAED;;;;;GAKG;AACH,0CAJW,MAAM,KACN,MAAM,GACJ,MAAM,CAIlB;AAlID;;;;GAIG;AACH;cAF+B,GAAG,EAAE,GAAG,GAAG;mBAAiB,GAAG,EAAE,GAAG,GAAG;EA6BnE"}

View File

@@ -0,0 +1,224 @@
/**
* @file Custom data structures.
*
* These are only used internally, meaning an end-user shouldn't
* need to access anything here.
*
* @module utils/data-structures
*/
/**
* Efficient Heap-based Implementation of a Priority Queue.
* It uses an array-based binary heap, where the root is at index `0`, and the
* children of node `i` are located at indices `2i + 1` and `2i + 2`, respectively.
*
* Adapted from the following sources:
* - https://stackoverflow.com/a/42919752/13989043 (original)
* - https://github.com/belladoreai/llama-tokenizer-js (minor improvements)
*/
export class PriorityQueue {
/**
* Create a new PriorityQueue.
* @param {Function} comparator Comparator function to determine priority. Defaults to a MaxHeap.
*/
constructor(comparator?: Function);
_heap: any[];
_comparator: Function;
/**
* The size of the queue
*/
get size(): number;
/**
* Check if the queue is empty.
* @returns {boolean} `true` if the queue is empty, `false` otherwise.
*/
isEmpty(): boolean;
/**
* Return the element with the highest priority in the queue.
* @returns {any} The highest priority element in the queue.
*/
peek(): any;
/**
* 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: any[]): number;
/**
* 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: any[]): number;
/**
* Remove and return the element with the highest priority in the queue.
* @returns {any} The element with the highest priority in the queue.
*/
pop(): any;
/**
* 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: any): any;
/**
* 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
*/
private _parent;
/**
* 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
*/
private _left;
/**
* 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
*/
private _right;
/**
* 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
*/
private _greater;
/**
* 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
*/
private _swap;
/**
* Maintain the heap property by updating positions in the heap,
* starting at the last element and moving up the heap.
* @private
*/
private _siftUp;
/**
* Maintain the heap property by updating positions in the heap,
* starting at the first element and moving down the heap.
* @private
*/
private _siftDown;
}
/**
* A trie structure to efficiently store and search for strings.
*/
export class CharTrie {
root: CharTrieNode;
/**
* Adds one or more `texts` to the trie.
* @param {string[]} texts The strings to add to the trie.
*/
extend(texts: string[]): void;
/**
* Adds text to the trie.
* @param {string} text The string to add to the trie.
*/
push(text: string): void;
/**
* 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: string): Generator<string, void, unknown>;
}
/**
* A lattice data structure to be used for tokenization.
*/
export 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: string, bosTokenId: number, eosTokenId: number);
sentence: string;
len: number;
bosTokenId: number;
eosTokenId: number;
nodes: TokenLatticeNode[];
beginNodes: any[][];
endNodes: any[][];
/**
* 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: number, length: number, score: number, tokenId: number): void;
/**
* Implements the Viterbi algorithm to compute the most likely sequence of tokens.
*
* @returns {TokenLatticeNode[]} The array of nodes representing the most likely sequence of tokens.
*/
viterbi(): TokenLatticeNode[];
/**
* @param {TokenLatticeNode} node
* @returns {string} The array of nodes representing the most likely sequence of tokens.
*/
piece(node: TokenLatticeNode): string;
/**
* @returns {Array} The array of nodes representing the most likely sequence of tokens.
*/
tokens(): any[];
/**
* @returns {Array} The array of nodes representing the most likely sequence of tokens.
*/
tokenIds(): any[];
}
/**
* Represents a node in a character trie.
*/
declare class CharTrieNode {
/**
* 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(): CharTrieNode;
/**
* Create a new CharTrieNode.
* @param {boolean} isLeaf Whether the node is a leaf node or not.
* @param {Map<string, CharTrieNode>} children A map containing the node's children, where the key is a character and the value is a `CharTrieNode`.
*/
constructor(isLeaf: boolean, children: Map<string, CharTrieNode>);
isLeaf: boolean;
children: Map<string, CharTrieNode>;
}
declare 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: number, nodeId: number, pos: number, length: number, score: number);
tokenId: number;
nodeId: number;
pos: number;
length: number;
score: number;
prev: any;
backtraceScore: number;
/**
* Returns a clone of this node.
* @returns {TokenLatticeNode} A clone of this node.
*/
clone(): TokenLatticeNode;
}
export {};
//# sourceMappingURL=data-structures.d.ts.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"data-structures.d.ts","sourceRoot":"","sources":["../../src/utils/data-structures.js"],"names":[],"mappings":"AACA;;;;;;;GAOG;AAGH;;;;;;;;GAQG;AACH;IAEI;;;OAGG;IACH,mCAGC;IAFG,aAAe;IACf,sBAA6B;IAGjC;;OAEG;IACH,mBAEC;IAED;;;OAGG;IACH,WAFa,OAAO,CAInB;IAED;;;OAGG;IACH,QAFa,GAAG,CAIf;IAED;;;;OAIG;IACH,gBAHe,GAAG,KACL,MAAM,CAIlB;IAED;;;;OAIG;IACH,eAHW,GAAG,EAAE,GACH,MAAM,CAQlB;IAED;;;OAGG;IACH,OAFa,GAAG,CAWf;IAED;;;;OAIG;IACH,yBAKC;IAED;;;;;OAKG;IACH,gBAEC;IAED;;;;;OAKG;IACH,cAEC;IAED;;;;;OAKG;IACH,eAEC;IAED;;;;;;OAMG;IACH,iBAEC;IAED;;;;;OAKG;IACH,cAIC;IAED;;;;OAIG;IACH,gBAMC;IACD;;;;OAIG;IACH,kBAYC;CACJ;AAED;;GAEG;AACH;IAEQ,mBAAkC;IAGtC;;;OAGG;IACH,cAFW,MAAM,EAAE,QAMlB;IAED;;;OAGG;IACH,WAFW,MAAM,QAahB;IAED;;;;OAIG;IACH,yBAHW,MAAM,oCAchB;CACJ;AAyBD;;GAEG;AACH;IACI;;;;;;OAMG;IACH,sBAJW,MAAM,cACN,MAAM,cACN,MAAM,EAiBhB;IAdG,iBAAwB;IACxB,YAA0B;IAC1B,mBAA4B;IAC5B,mBAA4B;IAC5B,0BAAe;IACf,oBAAgE;IAChE,kBAA8D;IAUlE;;;;;;;OAOG;IACH,YALW,MAAM,UACN,MAAM,SACN,MAAM,WACN,MAAM,QAQhB;IAED;;;;OAIG;IACH,WAFa,gBAAgB,EAAE,CA+C9B;IAED;;;OAGG;IACH,YAHW,gBAAgB,GACd,MAAM,CAIlB;IAED;;OAEG;IACH,gBAGC;IAED;;OAEG;IACH,kBAGC;CACJ;AA9ID;;GAEG;AACH;IAWI;;;OAGG;IACH,kBAFa,YAAY,CAIxB;IAhBD;;;;OAIG;IACH,oBAHW,OAAO,YACP,IAAI,MAAM,EAAE,YAAY,CAAC,EAKnC;IAFG,gBAAoB;IACpB,oCAAwB;CAU/B;AA0HD;IACI;;;;;;;OAOG;IACH,qBANW,MAAM,UACN,MAAM,OACN,MAAM,UACN,MAAM,SACN,MAAM,EAUhB;IAPG,gBAAsB;IACtB,eAAoB;IACpB,YAAc;IACd,eAAoB;IACpB,cAAkB;IAClB,UAAgB;IAChB,uBAAyB;IAG7B;;;OAGG;IACH,SAFa,gBAAgB,CAO5B;CACJ"}

View File

@@ -0,0 +1,593 @@
declare const LogitsProcessorList_base: new () => {
(...args: any[]): any;
_call(...args: any[]): any;
};
/**
* A class representing a list of logits processors. A logits processor is a function that modifies the logits
* output of a language model. This class provides methods for adding new processors and applying all processors to a
* batch of logits.
*
* @extends Callable
*/
export class LogitsProcessorList extends LogitsProcessorList_base {
processors: any[];
/**
* Adds a new logits processor to the list.
*
* @param {LogitsProcessor} item The logits processor function to add.
*/
push(item: LogitsProcessor): void;
/**
* Adds multiple logits processors to the list.
*
* @param {LogitsProcessor[]} items The logits processor functions to add.
*/
extend(items: LogitsProcessor[]): void;
/**
* Applies all logits processors in the list to a batch of logits, modifying them in-place.
*
* @param {number[]} input_ids The input IDs for the language model.
* @param {number[][]} batchedLogits A 2D array of logits, where each row corresponds to a single
* input sequence in the batch.
*/
_call(input_ids: number[], batchedLogits: number[][]): void;
[Symbol.iterator](): IterableIterator<any>;
}
declare const LogitsProcessor_base: new () => {
(...args: any[]): any;
_call(...args: any[]): any;
};
/**
* Base class for processing logits.
* @extends Callable
*/
export class LogitsProcessor extends LogitsProcessor_base {
/**
* Apply the processor to the input logits.
*
* @abstract
* @param {Array} 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: any[], logits: Tensor): void;
}
/**
* A logits processor that forces a specific token to be generated by the decoder.
*
* @extends LogitsProcessor
*/
export class ForceTokensLogitsProcessor extends LogitsProcessor {
/**
* Constructs a new instance of `ForceTokensLogitsProcessor`.
*
* @param {Array} forced_decoder_ids The ids of tokens that should be forced.
*/
constructor(forced_decoder_ids: any[]);
force_token_map: {
[k: string]: any;
};
/**
* Apply the processor to the input logits.
*
* @param {Array} input_ids The input ids.
* @param {Tensor} logits The logits to process.
* @returns {Tensor} The processed logits.
*/
_call(input_ids: any[], logits: Tensor): Tensor;
}
/**
* A LogitsProcessor that forces a BOS token at the beginning of the generated sequence.
* @extends LogitsProcessor
*/
export 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: number);
bos_token_id: number;
/**
* Apply the BOS token forcing to the logits.
* @param {Array} input_ids The input IDs.
* @param {Object} logits The logits.
* @returns {Object} The logits with BOS token forcing.
*/
_call(input_ids: any[], logits: any): any;
}
/**
* A logits processor that forces end-of-sequence token probability to 1.
*
* @extends LogitsProcessor
*/
export class ForcedEOSTokenLogitsProcessor extends LogitsProcessor {
/**
* Create a ForcedEOSTokenLogitsProcessor.
* @param {number} max_length Max length of the sequence.
* @param {number|number[]} forced_eos_token_id The ID of the end-of-sequence token to be forced.
*/
constructor(max_length: number, forced_eos_token_id: number | number[]);
max_length: number;
forced_eos_token_id: number | number[];
/**
* Apply the processor to input_ids and logits.
*
* @param {number[]} input_ids The input ids.
* @param {Tensor} logits The logits tensor.
*/
_call(input_ids: number[], logits: Tensor): void;
}
/**
* A LogitsProcessor that suppresses a list of tokens as soon as the `generate` function starts
* generating using `begin_index` tokens. This should ensure that the tokens defined by
* `begin_suppress_tokens` at not sampled at the begining of the generation.
* @extends LogitsProcessor
*/
export 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: number[], begin_index: number);
begin_suppress_tokens: number[];
begin_index: number;
/**
* Apply the BOS token forcing to the logits.
* @param {Array} input_ids The input IDs.
* @param {Object} logits The logits.
* @returns {Object} The logits with BOS token forcing.
*/
_call(input_ids: any[], logits: any): any;
}
/**
* A LogitsProcessor that handles adding timestamps to generated text.
* @extends LogitsProcessor
*/
export class WhisperTimeStampLogitsProcessor extends LogitsProcessor {
/**
* Constructs a new WhisperTimeStampLogitsProcessor.
* @param {Object} generate_config The config object passed to the `generate()` method of a transformer model.
* @param {number} generate_config.eos_token_id The ID of the end-of-sequence token.
* @param {number} generate_config.no_timestamps_token_id The ID of the token used to indicate that a token should not have a timestamp.
* @param {number[][]} [generate_config.forced_decoder_ids] An array of two-element arrays representing decoder IDs that are forced to appear in the output. The second element of each array indicates whether the token is a timestamp.
* @param {number} [generate_config.max_initial_timestamp_index] The maximum index at which an initial timestamp can appear.
*/
constructor(generate_config: {
eos_token_id: number;
no_timestamps_token_id: number;
forced_decoder_ids?: number[][];
max_initial_timestamp_index?: number;
});
eos_token_id: number;
no_timestamps_token_id: number;
timestamp_begin: number;
begin_index: number;
max_initial_timestamp_index: number;
/**
* Modify the logits to handle timestamp tokens.
* @param {Array} input_ids The input sequence of tokens.
* @param {Tensor} logits The logits output by the model.
* @returns {Tensor} The modified logits.
*/
_call(input_ids: any[], logits: Tensor): Tensor;
}
/**
* A logits processor that disallows ngrams of a certain size to be repeated.
*
* @extends LogitsProcessor
*/
export 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: number);
no_repeat_ngram_size: number;
/**
* Generate n-grams from a sequence of token ids.
* @param {number[]} prevInputIds List of previous input ids
* @returns {Map<string, number[]>} Map of generated n-grams
*/
getNgrams(prevInputIds: number[]): Map<string, number[]>;
/**
* Generate n-grams from a sequence of token ids.
* @param {Map<string, number[]>} bannedNgrams Map of banned n-grams
* @param {number[]} prevInputIds List of previous input ids
* @returns {number[]} Map of generated n-grams
*/
getGeneratedNgrams(bannedNgrams: Map<string, number[]>, prevInputIds: number[]): number[];
/**
* Calculate banned n-gram tokens
* @param {number[]} prevInputIds List of previous input ids
* @returns {number[]} Map of generated n-grams
*/
calcBannedNgramTokens(prevInputIds: number[]): number[];
/**
* Apply the no-repeat-ngram processor to the logits.
* @param {Array} input_ids The input IDs.
* @param {Object} logits The logits.
* @returns {Object} The logits with no-repeat-ngram processing.
*/
_call(input_ids: any[], logits: any): any;
}
/**
* A logits processor that penalises repeated output tokens.
*
* @extends LogitsProcessor
*/
export class RepetitionPenaltyLogitsProcessor extends LogitsProcessor {
/**
* Create a RepetitionPenaltyLogitsProcessor.
* @param {number} penalty The penalty to apply for repeated tokens.
*/
constructor(penalty: number);
penalty: number;
/**
* Apply the repetition penalty to the logits.
* @param {Array} input_ids The input IDs.
* @param {Object} logits The logits.
* @returns {Object} The logits with repetition penalty processing.
*/
_call(input_ids: any[], logits: any): any;
}
/**
* A logits processor that enforces a minimum number of tokens.
*
* @extends LogitsProcessor
*/
export 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: number, eos_token_id: number | number[]);
min_length: number;
eos_token_id: number[];
/**
* Apply logit processor.
* @param {Array} input_ids The input IDs.
* @param {Object} logits The logits.
* @returns {Object} The processed logits.
*/
_call(input_ids: any[], logits: any): any;
}
/**
* A logits processor that enforces a minimum number of new tokens.
*
* @extends LogitsProcessor
*/
export 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: number, min_new_tokens: number, eos_token_id: number | number[]);
prompt_length_to_skip: number;
min_new_tokens: number;
eos_token_id: number[];
/**
* Apply logit processor.
* @param {Array} input_ids The input IDs.
* @param {Object} logits The logits.
* @returns {Object} The processed logits.
*/
_call(input_ids: any[], logits: any): any;
}
export 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: number[][], eos_token_id: number | number[]);
bad_words_ids: number[][];
eos_token_id: number[];
/**
* Apply logit processor.
* @param {Array} input_ids The input IDs.
* @param {Object} logits The logits.
* @returns {Object} The processed logits.
*/
_call(input_ids: any[], logits: any): any;
}
/**
* @typedef {Object} GenerationConfigType The default configuration parameters.
* @property {number} [max_length=20] 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.
* @property {number} [max_new_tokens=null] The maximum numbers of tokens to generate, ignoring the number of tokens in the prompt.
* @property {number} [min_length=0] 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.
* @property {number} [min_new_tokens=null] The minimum numbers of tokens to generate, ignoring the number of tokens in the prompt.
* @property {boolean|"never"} [early_stopping=false] 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).
* @property {number} [max_time=null] 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.
*
* @property {boolean} [do_sample=false] Whether or not to use sampling; use greedy decoding otherwise.
* @property {number} [num_beams=1] Number of beams for beam search. 1 means no beam search.
* @property {number} [num_beam_groups=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.
* @property {number} [penalty_alpha=null] The values balance the model confidence and the degeneration penalty in contrastive search decoding.
* @property {boolean} [use_cache=true] Whether or not the model should use the past last key/values attentions (if applicable to the model) to speed up decoding.
*
* @property {number} [temperature=1.0] The value used to modulate the next token probabilities.
* @property {number} [top_k=50] The number of highest probability vocabulary tokens to keep for top-k-filtering.
* @property {number} [top_p=1.0] 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.
* @property {number} [typical_p=1.0] Local typicality measures how similar the conditional probability of predicting a target token next is to the expected conditional probability of predicting a random token next, given the partial text already generated. If set to float < 1, the smallest set of the most locally typical tokens with probabilities that add up to `typical_p` or higher are kept for generation. See [this paper](https://arxiv.org/pdf/2202.00666.pdf) for more details.
* @property {number} [epsilon_cutoff=0.0] If set to float strictly between 0 and 1, only tokens with a conditional probability greater than `epsilon_cutoff` will be sampled. In the paper, suggested values range from 3e-4 to 9e-4, depending on the size of the model. See [Truncation Sampling as Language Model Desmoothing](https://arxiv.org/abs/2210.15191) for more details.
* @property {number} [eta_cutoff=0.0] Eta sampling is a hybrid of locally typical sampling and epsilon sampling. If set to float strictly between 0 and 1, a token is only considered if it is greater than either `eta_cutoff` or `sqrt(eta_cutoff) * exp(-entropy(softmax(next_token_logits)))`. The latter term is intuitively the expected next token probability, scaled by `sqrt(eta_cutoff)`. In the paper, suggested values range from 3e-4 to 2e-3, depending on the size of the model. See [Truncation Sampling as Language Model Desmoothing](https://arxiv.org/abs/2210.15191) for more details.
* @property {number} [diversity_penalty=0.0] This value is subtracted from a beam's score if it generates a token same as any beam from other group at a particular time. Note that `diversity_penalty` is only effective if `group beam search` is enabled.
* @property {number} [repetition_penalty=1.0] The parameter for repetition penalty. 1.0 means no penalty. See [this paper](https://arxiv.org/pdf/1909.05858.pdf) for more details.
* @property {number} [encoder_repetition_penalty=1.0] The paramater for encoder_repetition_penalty. An exponential penalty on sequences that are not in the original input. 1.0 means no penalty.
* @property {number} [length_penalty=1.0] Exponential penalty to the length that is used with beam-based generation. It is applied as an exponent to the sequence length, which in turn is used to divide the score of the sequence. Since the score is the log likelihood of the sequence (i.e. negative), `length_penalty` > 0.0 promotes longer sequences, while `length_penalty` < 0.0 encourages shorter sequences.
* @property {number} [no_repeat_ngram_size=0] If set to int > 0, all ngrams of that size can only occur once.
* @property {number[][]} [bad_words_ids=null] 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 `(await tokenizer(bad_words, {add_prefix_space: true, add_special_tokens: false})).input_ids`.
* @property {number[][]|number[][][]} [force_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.
* @property {boolean} [renormalize_logits=false] 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.
* @property {Object[]} [constraints=null] 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.
*
* @property {number} [forced_bos_token_id=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.
* @property {number|number[]} [forced_eos_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.
* @property {boolean} [remove_invalid_values=false] 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.
* @property {number[]} [exponential_decay_length_penalty=null] 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.
* @property {number[]} [suppress_tokens=null] A list of tokens that will be suppressed at generation. The `SupressTokens` logit processor will set their log probs to `-inf` so that they are not sampled.
* @property {number[]} [begin_suppress_tokens=null] A list of tokens that will be suppressed at the beginning of the generation. The `SupressBeginTokens` logit processor will set their log probs to `-inf` so that they are not sampled.
* @property {number[][]} [forced_decoder_ids=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.
*
* @property {number} [num_return_sequences=1] The number of independently computed returned sequences for each element in the batch.
* @property {boolean} [output_attentions=false] Whether or not to return the attentions tensors of all attention layers. See `attentions` under returned tensors for more details.
* @property {boolean} [output_hidden_states=false] Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors for more details.
* @property {boolean} [output_scores=false] Whether or not to return the prediction scores. See `scores` under returned tensors for more details.
* @property {boolean} [return_dict_in_generate=false] Whether or not to return a `ModelOutput` instead of a plain tuple.
*
* @property {number} [pad_token_id=null] The id of the *padding* token.
* @property {number} [bos_token_id=null] The id of the *beginning-of-sequence* token.
* @property {number|number[]} [eos_token_id=null] The id of the *end-of-sequence* token. Optionally, use a list to set multiple *end-of-sequence* tokens.
*
* @property {number} [encoder_no_repeat_ngram_size=0] If set to int > 0, all ngrams of that size that occur in the `encoder_input_ids` cannot occur in the `decoder_input_ids`.
* @property {number} [decoder_start_token_id=null] If an encoder-decoder model starts decoding with a different token than *bos*, the id of that token.
*
* @property {Object} [generation_kwargs={}] 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.
*/
/**
* Class that holds a configuration for a generation task.
* @type {new (kwargs?: GenerationConfigType) => GenerationConfigType}
*/
export const GenerationConfig: new (kwargs?: GenerationConfigType) => GenerationConfigType;
declare const Sampler_base: new () => {
(...args: any[]): any;
_call(...args: any[]): any;
};
/**
* Sampler is a base class for all sampling methods used for text generation.
*/
export class Sampler extends Sampler_base {
/**
* Returns a Sampler object based on the specified options.
* @param {GenerationConfigType} generation_config An object containing options for the sampler.
* @returns {Sampler} A Sampler object.
*/
static getSampler(generation_config: GenerationConfigType): Sampler;
/**
* Creates a new Sampler object with the specified generation config.
* @param {GenerationConfigType} generation_config The generation config.
*/
constructor(generation_config: GenerationConfigType);
generation_config: GenerationConfigType;
/**
* Executes the sampler, using the specified logits.
* @param {Tensor} logits
* @param {number} index
* @returns {void}
*/
_call(logits: Tensor, index?: number): void;
/**
* Abstract method for sampling the logits.
* @param {Tensor} logits
* @param {number} index
* @throws {Error}
*/
sample(logits: Tensor, index: number): void;
/**
* Returns the specified logits as an array, with temperature applied.
* @param {Tensor} logits
* @param {number} index
* @returns {Float32Array}
*/
getLogits(logits: Tensor, index: number): Float32Array;
/**
* Selects an item randomly based on the specified probabilities.
* @param {Array} probabilities An array of probabilities to use for selection.
* @returns {number} The index of the selected item.
*/
randomSelect(probabilities: any[]): number;
}
/**
* The default configuration parameters.
*/
export type GenerationConfigType = {
/**
* 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.
*/
max_length?: number;
/**
* The maximum numbers of tokens to generate, ignoring the number of tokens in the prompt.
*/
max_new_tokens?: number;
/**
* 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.
*/
min_length?: number;
/**
* The minimum numbers of tokens to generate, ignoring the number of tokens in the prompt.
*/
min_new_tokens?: number;
/**
* 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).
*/
early_stopping?: boolean | "never";
/**
* 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.
*/
max_time?: number;
/**
* Whether or not to use sampling; use greedy decoding otherwise.
*/
do_sample?: boolean;
/**
* Number of beams for beam search. 1 means no beam search.
*/
num_beams?: number;
/**
* 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.
*/
num_beam_groups?: number;
/**
* The values balance the model confidence and the degeneration penalty in contrastive search decoding.
*/
penalty_alpha?: number;
/**
* Whether or not the model should use the past last key/values attentions (if applicable to the model) to speed up decoding.
*/
use_cache?: boolean;
/**
* The value used to modulate the next token probabilities.
*/
temperature?: number;
/**
* The number of highest probability vocabulary tokens to keep for top-k-filtering.
*/
top_k?: number;
/**
* 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.
*/
top_p?: number;
/**
* 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.
*/
typical_p?: number;
/**
* 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.
*/
epsilon_cutoff?: number;
/**
* 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.
*/
eta_cutoff?: number;
/**
* 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.
*/
diversity_penalty?: number;
/**
* The parameter for repetition penalty. 1.0 means no penalty. See [this paper](https://arxiv.org/pdf/1909.05858.pdf) for more details.
*/
repetition_penalty?: number;
/**
* The paramater for encoder_repetition_penalty. An exponential penalty on sequences that are not in the original input. 1.0 means no penalty.
*/
encoder_repetition_penalty?: number;
/**
* 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.
*/
length_penalty?: number;
/**
* If set to int > 0, all ngrams of that size can only occur once.
*/
no_repeat_ngram_size?: number;
/**
* 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 `(await tokenizer(bad_words, {add_prefix_space: true, add_special_tokens: false})).input_ids`.
*/
bad_words_ids?: number[][];
/**
* 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.
*/
force_words_ids?: number[][] | number[][][];
/**
* 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.
*/
renormalize_logits?: boolean;
/**
* 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.
*/
constraints?: any[];
/**
* 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.
*/
forced_bos_token_id?: number;
/**
* 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.
*/
forced_eos_token_id?: number | number[];
/**
* 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.
*/
remove_invalid_values?: boolean;
/**
* 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.
*/
exponential_decay_length_penalty?: number[];
/**
* A list of tokens that will be suppressed at generation. The `SupressTokens` logit processor will set their log probs to `-inf` so that they are not sampled.
*/
suppress_tokens?: number[];
/**
* A list of tokens that will be suppressed at the beginning of the generation. The `SupressBeginTokens` logit processor will set their log probs to `-inf` so that they are not sampled.
*/
begin_suppress_tokens?: number[];
/**
* 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.
*/
forced_decoder_ids?: number[][];
/**
* The number of independently computed returned sequences for each element in the batch.
*/
num_return_sequences?: number;
/**
* Whether or not to return the attentions tensors of all attention layers. See `attentions` under returned tensors for more details.
*/
output_attentions?: boolean;
/**
* Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors for more details.
*/
output_hidden_states?: boolean;
/**
* Whether or not to return the prediction scores. See `scores` under returned tensors for more details.
*/
output_scores?: boolean;
/**
* Whether or not to return a `ModelOutput` instead of a plain tuple.
*/
return_dict_in_generate?: boolean;
/**
* The id of the *padding* token.
*/
pad_token_id?: number;
/**
* The id of the *beginning-of-sequence* token.
*/
bos_token_id?: number;
/**
* The id of the *end-of-sequence* token. Optionally, use a list to set multiple *end-of-sequence* tokens.
*/
eos_token_id?: number | number[];
/**
* If set to int > 0, all ngrams of that size that occur in the `encoder_input_ids` cannot occur in the `decoder_input_ids`.
*/
encoder_no_repeat_ngram_size?: number;
/**
* If an encoder-decoder model starts decoding with a different token than *bos*, the id of that token.
*/
decoder_start_token_id?: number;
/**
* 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.
*/
generation_kwargs?: any;
};
import { Tensor } from './tensor.js';
export {};
//# sourceMappingURL=generation.d.ts.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"generation.d.ts","sourceRoot":"","sources":["../../src/utils/generation.js"],"names":[],"mappings":";;;;AAoBA;;;;;;GAMG;AACH;IAMQ,kBAAoB;IAGxB;;;;OAIG;IACH,WAFW,eAAe,QAIzB;IAED;;;;OAIG;IACH,cAFW,eAAe,EAAE,QAI3B;IAED;;;;;;OAMG;IACH,iBAJW,MAAM,EAAE,iBACR,MAAM,EAAE,EAAE,QAYpB;IAED,2CAEC;CACJ;;;;;AAED;;;GAGG;AACH;IACI;;;;;;;OAOG;IACH,gCAHW,MAAM,QAKhB;CACJ;AAED;;;;GAIG;AACH;IACI;;;;OAIG;IACH,uCAGC;IADG;;MAAmE;IAGvE;;;;;;OAMG;IACH,gCAHW,MAAM,GACJ,MAAM,CASlB;CACJ;AAED;;;GAGG;AACH;IACI;;;OAGG;IACH,0BAFW,MAAM,EAKhB;IADG,qBAAgC;IAGpC;;;;;OAKG;IACH,0CAMC;CACJ;AAED;;;;GAIG;AACH;IACI;;;;OAIG;IACH,wBAHW,MAAM,uBACN,MAAM,GAAC,MAAM,EAAE,EAMzB;IAFG,mBAA4B;IAC5B,uCAA8C;IAGlD;;;;;OAKG;IACH,iBAHW,MAAM,EAAE,UACR,MAAM,QAKhB;CACJ;AAED;;;;;GAKG;AACH;IACI;;;;OAIG;IACH,mCAHW,MAAM,EAAE,eACR,MAAM,EAMhB;IAFG,gCAAkD;IAClD,oBAA8B;IAGlC;;;;;OAKG;IACH,0CAOC;CACJ;AAED;;;GAGG;AACH;IACI;;;;;;;OAOG;IACH;QALmC,YAAY,EAApC,MAAM;QACkB,sBAAsB,EAA9C,MAAM;QACuB,kBAAkB,GAA/C,MAAM,EAAE,EAAE;QACe,2BAA2B,GAApD,MAAM;OAchB;IAVG,qBAAgD;IAChD,+BAAoE;IACpE,wBAAsD;IAEtD,oBAAwE;IAIxE,oCAA8E;IAIlF;;;;;OAKG;IACH,gCAHW,MAAM,GACJ,MAAM,CA2ClB;CACJ;AAED;;;;GAIG;AACH;IACI;;;OAGG;IACH,kCAFW,MAAM,EAKhB;IADG,6BAAgD;IAGpD;;;;OAIG;IACH,wBAHW,MAAM,EAAE,GACN,IAAI,MAAM,EAAE,MAAM,EAAE,CAAC,CAyBjC;IAED;;;;;OAKG;IACH,iCAJW,IAAI,MAAM,EAAE,MAAM,EAAE,CAAC,gBACrB,MAAM,EAAE,GACN,MAAM,EAAE,CAMpB;IAED;;;;OAIG;IACH,oCAHW,MAAM,EAAE,GACN,MAAM,EAAE,CAapB;IAED;;;;;OAKG;IACH,0CAOC;CACJ;AAED;;;;GAIG;AACH;IACI;;;OAGG;IACH,qBAFW,MAAM,EAKhB;IADG,gBAAsB;IAG1B;;;;;OAKG;IACH,0CAYC;CACJ;AAED;;;;GAIG;AACH;IACI;;;;OAIG;IACH,wBAHW,MAAM,gBACN,MAAM,GAAC,MAAM,EAAE,EAMzB;IAFG,mBAA4B;IAC5B,uBAA+E;IAGnF;;;;;OAKG;IACH,0CAQC;CACJ;AAED;;;;GAIG;AACH;IACI;;;;;OAKG;IACH,mCAJW,MAAM,kBACN,MAAM,gBACN,MAAM,GAAC,MAAM,EAAE,EAOzB;IAHG,8BAAkD;IAClD,uBAAoC;IACpC,uBAA+E;IAGnF;;;;;OAKG;IACH,0CASC;CACJ;AAED;IACI;;;;OAIG;IACH,2BAHW,MAAM,EAAE,EAAE,gBACV,MAAM,GAAC,MAAM,EAAE,EAMzB;IAFG,0BAAkC;IAClC,uBAA+E;IAGnF;;;;;OAKG;IACH,0CAsBC;CACJ;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAwDG;AAEH;;;GAGG;AACH,6CAFwB,oBAAoB,KAAK,oBAAoB,CAmElE;;;;;AAEH;;GAEG;AACH;IA4EI;;;;OAIG;IACH,qCAHW,oBAAoB,GAClB,OAAO,CAwBnB;IAtGD;;;OAGG;IACH,+BAFW,oBAAoB,EAK9B;IADG,wCAA0C;IAG9C;;;;;OAKG;IACH,cAJW,MAAM,UACN,MAAM,GACJ,IAAI,CAMhB;IAED;;;;;OAKG;IACH,eAJW,MAAM,SACN,MAAM,QAKhB;IAED;;;;;OAKG;IACH,kBAJW,MAAM,SACN,MAAM,GACJ,YAAY,CAmBxB;IAED;;;;OAIG;IACH,oCAFa,MAAM,CAclB;CA8BJ;;;;;;;;iBA1Oa,MAAM;;;;qBACN,MAAM;;;;iBACN,MAAM;;;;qBACN,MAAM;;;;;;;qBACN,OAAO,GAAC,OAAO;;;;eAIf,MAAM;;;;gBAEN,OAAO;;;;gBACP,MAAM;;;;sBACN,MAAM;;;;oBACN,MAAM;;;;gBACN,OAAO;;;;kBAEP,MAAM;;;;YACN,MAAM;;;;YACN,MAAM;;;;gBACN,MAAM;;;;qBACN,MAAM;;;;iBACN,MAAM;;;;wBACN,MAAM;;;;yBACN,MAAM;;;;iCACN,MAAM;;;;qBACN,MAAM;;;;2BACN,MAAM;;;;oBACN,MAAM,EAAE,EAAE;;;;sBACV,MAAM,EAAE,EAAE,GAAC,MAAM,EAAE,EAAE,EAAE;;;;yBACvB,OAAO;;;;kBACP,KAAQ;;;;0BAER,MAAM;;;;0BACN,MAAM,GAAC,MAAM,EAAE;;;;4BACf,OAAO;;;;uCACP,MAAM,EAAE;;;;sBACR,MAAM,EAAE;;;;4BACR,MAAM,EAAE;;;;yBACR,MAAM,EAAE,EAAE;;;;2BAEV,MAAM;;;;wBACN,OAAO;;;;2BACP,OAAO;;;;oBACP,OAAO;;;;8BACP,OAAO;;;;mBAEP,MAAM;;;;mBACN,MAAM;;;;mBACN,MAAM,GAAC,MAAM,EAAE;;;;mCAEf,MAAM;;;;6BACN,MAAM;;;;;;uBAvkBG,aAAa"}

154
node_modules/@xenova/transformers/types/utils/hub.d.ts generated vendored Normal file
View File

@@ -0,0 +1,154 @@
/**
* Helper function to get a file, using either the Fetch API or FileSystem API.
*
* @param {URL|string} urlOrPath The URL/path of the file to get.
* @returns {Promise<FileResponse|Response>} A promise that resolves to a FileResponse object (if the file is retrieved using the FileSystem API), or a Response object (if the file is retrieved using the Fetch API).
*/
export function getFile(urlOrPath: URL | string): Promise<FileResponse | Response>;
/**
*
* Retrieves a file from either a remote URL using the Fetch API or from the local file system using the FileSystem API.
* If the filesystem is available and `env.useCache = true`, the file will be downloaded and cached.
*
* @param {string} path_or_repo_id This can be either:
* - a string, the *model id* of a model repo on huggingface.co.
* - a path to a *directory* potentially containing the file.
* @param {string} filename The name of the file to locate in `path_or_repo`.
* @param {boolean} [fatal=true] Whether to throw an error if the file is not found.
* @param {PretrainedOptions} [options] An object containing optional parameters.
*
* @throws Will throw an error if the file is not found and `fatal` is true.
* @returns {Promise} A Promise that resolves with the file content as a buffer.
*/
export function getModelFile(path_or_repo_id: string, filename: string, fatal?: boolean, options?: PretrainedOptions): Promise<any>;
/**
* Fetches a JSON file from a given path and file name.
*
* @param {string} modelPath The path to the directory containing the file.
* @param {string} fileName The name of the file to fetch.
* @param {boolean} [fatal=true] Whether to throw an error if the file is not found.
* @param {PretrainedOptions} [options] An object containing optional parameters.
* @returns {Promise<Object>} The JSON data parsed into a JavaScript object.
* @throws Will throw an error if the file is not found and `fatal` is true.
*/
export function getModelJSON(modelPath: string, fileName: string, fatal?: boolean, options?: PretrainedOptions): Promise<any>;
/**
* Options for loading a pretrained model.
*/
export type PretrainedOptions = {
/**
* Whether to load the 8-bit quantized version of the model (only applicable when loading model files).
*/
quantized?: boolean | null;
/**
* If specified, this function will be called during model construction, to provide the user with progress updates.
*/
progress_callback?: Function;
/**
* Configuration for the model to use instead of an automatically loaded configuration. Configuration can be automatically loaded when:
* - The model is a model provided by the library (loaded with the *model id* string of a pretrained model).
* - The model is loaded by supplying a local directory as `pretrained_model_name_or_path` and a configuration JSON file named *config.json* is found in the directory.
*/
config?: any;
/**
* Path to a directory in which a downloaded pretrained model configuration should be cached if the standard cache should not be used.
*/
cache_dir?: string;
/**
* Whether or not to only look at local files (e.g., not try downloading the model).
*/
local_files_only?: boolean;
/**
* The specific model version to use. It can be a branch name, a tag name, or a commit id,
* since we use a git-based system for storing models and other artifacts on huggingface.co, so `revision` can be any identifier allowed by git.
* NOTE: This setting is ignored for local requests.
*/
revision?: string;
/**
* If specified, load the model with this name (excluding the .onnx suffix). Currently only valid for encoder- or decoder-only models.
*/
model_file_name?: string;
};
/**
* @typedef {Object} PretrainedOptions Options for loading a pretrained model.
* @property {boolean?} [quantized=true] Whether to load the 8-bit quantized version of the model (only applicable when loading model files).
* @property {function} [progress_callback=null] If specified, this function will be called during model construction, to provide the user with progress updates.
* @property {Object} [config=null] Configuration for the model to use instead of an automatically loaded configuration. Configuration can be automatically loaded when:
* - The model is a model provided by the library (loaded with the *model id* string of a pretrained model).
* - The model is loaded by supplying a local directory as `pretrained_model_name_or_path` and a configuration JSON file named *config.json* is found in the directory.
* @property {string} [cache_dir=null] Path to a directory in which a downloaded pretrained model configuration should be cached if the standard cache should not be used.
* @property {boolean} [local_files_only=false] Whether or not to only look at local files (e.g., not try downloading the model).
* @property {string} [revision='main'] The specific model version to use. It can be a branch name, a tag name, or a commit id,
* since we use a git-based system for storing models and other artifacts on huggingface.co, so `revision` can be any identifier allowed by git.
* NOTE: This setting is ignored for local requests.
* @property {string} [model_file_name=null] If specified, load the model with this name (excluding the .onnx suffix). Currently only valid for encoder- or decoder-only models.
*/
declare class FileResponse {
/**
* Creates a new `FileResponse` object.
* @param {string|URL} filePath
*/
constructor(filePath: string | URL);
/**
* Mapping from file extensions to MIME types.
*/
_CONTENT_TYPE_MAP: {
txt: string;
html: string;
css: string;
js: string;
json: string;
png: string;
jpg: string;
jpeg: string;
gif: string;
};
filePath: string | URL;
headers: Headers;
exists: any;
status: number;
statusText: string;
body: ReadableStream<any>;
/**
* 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(): void;
/**
* Clone the current FileResponse object.
* @returns {FileResponse} A new FileResponse object with the same properties as the current object.
*/
clone(): FileResponse;
/**
* 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<ArrayBuffer>} A Promise that resolves with an ArrayBuffer containing the file's contents.
* @throws {Error} If the file cannot be read.
*/
arrayBuffer(): Promise<ArrayBuffer>;
/**
* 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<Blob>} A Promise that resolves with a Blob containing the file's contents.
* @throws {Error} If the file cannot be read.
*/
blob(): Promise<Blob>;
/**
* 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<string>} A Promise that resolves with a string containing the file's contents.
* @throws {Error} If the file cannot be read.
*/
text(): Promise<string>;
/**
* 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<Object>} A Promise that resolves with a parsed JavaScript object containing the file's contents.
* @throws {Error} If the file cannot be read.
*/
json(): Promise<any>;
}
export {};
//# sourceMappingURL=hub.d.ts.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"hub.d.ts","sourceRoot":"","sources":["../../src/utils/hub.js"],"names":[],"mappings":"AAyKA;;;;;GAKG;AACH,mCAHW,GAAG,GAAC,MAAM,GACR,QAAQ,YAAY,GAAC,QAAQ,CAAC,CAgC1C;AA2GD;;;;;;;;;;;;;;GAcG;AACH,8CAVW,MAAM,YAGN,MAAM,UACN,OAAO,YACP,iBAAiB,gBA4O3B;AAED;;;;;;;;;GASG;AACH,wCAPW,MAAM,YACN,MAAM,UACN,OAAO,YACP,iBAAiB,GACf,YAAe,CAc3B;;;;;;;;gBAtjBa,OAAO;;;;;;;;;;;;;;gBAKP,MAAM;;;;uBACN,OAAO;;;;;;eACP,MAAM;;;;sBAGN,MAAM;;AAZpB;;;;;;;;;;;;;GAaG;AAEH;IAeI;;;OAGG;IACH,sBAFW,MAAM,GAAC,GAAG,EA8BpB;IA9CD;;OAEG;IACH;;;;;;;;;;MAUC;IAMG,uBAAwB;IACxB,iBAA4B;IAE5B,YAAqC;IAEjC,eAAiB;IACjB,mBAAsB;IAQtB,0BAOE;IAQV;;;;OAIG;IACH,qBAFa,IAAI,CAMhB;IAED;;;OAGG;IACH,SAFa,YAAY,CASxB;IAED;;;;;OAKG;IACH,eAHa,QAAQ,WAAW,CAAC,CAMhC;IAED;;;;;OAKG;IACH,QAHa,QAAQ,IAAI,CAAC,CAMzB;IAED;;;;;OAKG;IACH,QAHa,QAAQ,MAAM,CAAC,CAM3B;IAED;;;;;;OAMG;IACH,QAHa,YAAe,CAK3B;CACJ"}

View File

@@ -0,0 +1,113 @@
export class RawImage {
/**
* 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 read(input: RawImage | string | URL): Promise<RawImage>;
/**
* 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<RawImage>} The image object.
*/
static fromURL(url: string | URL): Promise<RawImage>;
/**
* Helper method to create a new Image from a blob.
* @param {Blob} blob The blob to read the image from.
* @returns {Promise<RawImage>} The image object.
*/
static fromBlob(blob: Blob): Promise<RawImage>;
/**
* Helper method to create a new Image from a tensor
* @param {Tensor} tensor
*/
static fromTensor(tensor: Tensor, channel_format?: string): 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: Uint8ClampedArray | Uint8Array, width: number, height: number, channels: 1 | 2 | 3 | 4);
data: Uint8Array | Uint8ClampedArray;
width: number;
height: number;
channels: 2 | 1 | 3 | 4;
/**
* Returns the size of the image (width, height).
* @returns {[number, number]} The size of the image (width, height).
*/
get size(): [number, number];
/**
* Convert the image to grayscale format.
* @returns {RawImage} `this` to support chaining.
*/
grayscale(): RawImage;
/**
* Convert the image to RGB format.
* @returns {RawImage} `this` to support chaining.
*/
rgb(): RawImage;
/**
* Convert the image to RGBA format.
* @returns {RawImage} `this` to support chaining.
*/
rgba(): RawImage;
/**
* 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<RawImage>} `this` to support chaining.
*/
resize(width: number, height: number, { resample, }?: {
resample?: 0 | 1 | 2 | 3 | 4 | 5 | string;
}): Promise<RawImage>;
pad([left, right, top, bottom]: [any, any, any, any]): Promise<any>;
crop([x_min, y_min, x_max, y_max]: [any, any, any, any]): Promise<any>;
center_crop(crop_width: any, crop_height: any): Promise<any>;
toBlob(type?: string, quality?: number): Promise<any>;
toTensor(channel_format?: string): Tensor;
toCanvas(): any;
/**
* 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
*/
private _update;
/**
* Clone the image
* @returns {RawImage} The cloned image
*/
clone(): RawImage;
/**
* 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: number): RawImage;
/**
* Save the image to the given path.
* @param {string} path The path to save the image to.
*/
save(path: string): Promise<any>;
toSharp(): any;
}
import { Tensor } from './tensor.js';
//# sourceMappingURL=image.d.ts.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"image.d.ts","sourceRoot":"","sources":["../../src/utils/image.js"],"names":[],"mappings":"AA6EA;IAwBI;;;;;;;;;;;;;;;OAeG;IACH,mBAdW,QAAQ,GAAC,MAAM,GAAC,GAAG,qBAsB7B;IAGD;;;;OAIG;IACH,oBAHW,MAAM,GAAC,GAAG,GACR,QAAQ,QAAQ,CAAC,CAS7B;IAED;;;;OAIG;IACH,sBAHW,IAAI,GACF,QAAQ,QAAQ,CAAC,CAoB7B;IAED;;;OAGG;IACH,0BAFW,MAAM,qCA0BhB;IApHD;;;;;;OAMG;IACH,kBALW,iBAAiB,GAAC,UAAU,SAC5B,MAAM,UACN,MAAM,YACN,CAAC,GAAC,CAAC,GAAC,CAAC,GAAC,CAAC,EAOjB;IAJG,qCAAgB;IAChB,cAAkB;IAClB,eAAoB;IACpB,wBAAwB;IAG5B;;;OAGG;IACH,6BAEC;IAkGD;;;OAGG;IACH,aAFa,QAAQ,CAuBpB;IAED;;;OAGG;IACH,OAFa,QAAQ,CA6BpB;IAED;;;OAGG;IACH,QAFa,QAAQ,CA+BpB;IAED;;;;;;;OAOG;IACH,cANW,MAAM,UACN,MAAM;QAEuB,QAAQ,GAArC,CAAC,GAAC,CAAC,GAAC,CAAC,GAAC,CAAC,GAAC,CAAC,GAAC,CAAC,GAAC,MAAM;QAChB,QAAQ,QAAQ,CAAC,CAqE7B;IAED,oEA0CC;IAED,uEAkDC;IAED,6DAiHC;IAED,sDAOC;IAED,0CAeC;IAED,gBAiBC;IAED;;;;;;;OAOG;IACH,gBAQC;IAED;;;OAGG;IACH,SAFa,QAAQ,CAIpB;IAED;;;;OAIG;IACH,qBAHW,MAAM,GACJ,QAAQ,CAmBpB;IAED;;;OAGG;IACH,WAFW,MAAM,gBAsChB;IAED,eAYC;CACJ;uBA9sBsB,aAAa"}

View File

@@ -0,0 +1,280 @@
/**
* @file Helper module for mathematical processing.
*
* These functions and classes are only used internally,
* meaning an end-user shouldn't need to access anything here.
*
* @module utils/maths
*/
/**
* @typedef {Int8Array | Uint8Array | Uint8ClampedArray | Int16Array | Uint16Array | Int32Array | Uint32Array | Float32Array | Float64Array} TypedArray
* @typedef {BigInt64Array | BigUint64Array} BigTypedArray
* @typedef {TypedArray | BigTypedArray} AnyTypedArray
*/
/**
* @param {TypedArray} input
*/
export function interpolate_data(input: TypedArray, [in_channels, in_height, in_width]: [any, any, any], [out_height, out_width]: [any, any], mode?: string, align_corners?: boolean): any;
/**
* Helper method to permute a `AnyTypedArray` directly
* @template {AnyTypedArray} T
* @param {T} array
* @param {number[]} dims
* @param {number[]} axes
* @returns {[T, number[]]} The permuted array and the new shape.
*/
export function permute_data<T extends AnyTypedArray>(array: T, dims: number[], axes: number[]): [T, number[]];
/**
* Compute the softmax of an array of numbers.
* @template {TypedArray|number[]} T
* @param {T} arr The array of numbers to compute the softmax of.
* @returns {T} The softmax array.
*/
export function softmax<T extends number[] | TypedArray>(arr: T): T;
/**
* Calculates the logarithm of the softmax function for the input array.
* @template {TypedArray|number[]} T
* @param {T} arr The input array to calculate the log_softmax function for.
* @returns {T} The resulting log_softmax array.
*/
export function log_softmax<T extends number[] | TypedArray>(arr: T): T;
/**
* Calculates the dot product of two arrays.
* @param {number[]} arr1 The first array.
* @param {number[]} arr2 The second array.
* @returns {number} The dot product of arr1 and arr2.
*/
export function dot(arr1: number[], arr2: number[]): number;
/**
* Get the top k items from an iterable, sorted by descending order
* @param {any[]|TypedArray} items The items to be sorted
* @param {number|null} [top_k=0] The number of top items to return (default: 0 = return all)
* @returns {[number, any][]} The top k items, sorted by descending order
*/
export function getTopItems(items: any[] | TypedArray, top_k?: number | null): [number, any][];
/**
* Computes the cosine similarity between two arrays.
*
* @param {number[]} arr1 The first array.
* @param {number[]} arr2 The second array.
* @returns {number} The cosine similarity between the two arrays.
*/
export function cos_sim(arr1: number[], arr2: number[]): number;
/**
* Calculates the magnitude of a given array.
* @param {number[]} arr The array to calculate the magnitude of.
* @returns {number} The magnitude of the array.
*/
export function magnitude(arr: number[]): number;
/**
* Returns the value and index of the minimum element in an array.
* @param {number[]|TypedArray} arr array of numbers.
* @returns {number[]} the value and index of the minimum element, of the form: [valueOfMin, indexOfMin]
* @throws {Error} If array is empty.
*/
export function min(arr: number[] | TypedArray): number[];
/**
* Returns the value and index of the maximum element in an array.
* @param {number[]|AnyTypedArray} arr array of numbers.
* @returns {[number, number]} the value and index of the maximum element, of the form: [valueOfMax, indexOfMax]
* @throws {Error} If array is empty.
*/
export function max(arr: number[] | AnyTypedArray): [number, number];
/**
* Performs median filter on the provided data. Padding is done by mirroring the data.
* @param {AnyTypedArray} data The input array
* @param {number} windowSize The window size
*/
export function medianFilter(data: AnyTypedArray, windowSize: number): any;
/**
* Helper function to round a number to a given number of decimals
* @param {number} num The number to round
* @param {number} decimals The number of decimals
* @returns {number} The rounded number
*/
export function round(num: number, decimals: number): number;
/**
* Helper function to round a number to the nearest integer, with ties rounded to the nearest even number.
* Also known as "bankers' rounding". This is the default rounding mode in python. For example:
* 1.5 rounds to 2 and 2.5 rounds to 2.
*
* @param {number} x The number to round
* @returns {number} The rounded number
*/
export function bankers_round(x: number): number;
export class FFT {
constructor(fft_length: any);
fft_length: any;
isPowerOfTwo: boolean;
fft: P2FFT | NP2FFT;
outputBufferSize: number;
realTransform(out: any, input: any): void;
transform(out: any, input: any): void;
}
export type TypedArray = Int8Array | Uint8Array | Uint8ClampedArray | Int16Array | Uint16Array | Int32Array | Uint32Array | Float32Array | Float64Array;
export type BigTypedArray = BigInt64Array | BigUint64Array;
export type AnyTypedArray = TypedArray | BigTypedArray;
/**
* Implementation of Radix-4 FFT.
*
* P2FFT class provides functionality for performing Fast Fourier Transform on arrays
* which are a power of two in length.
* Code adapted from https://www.npmjs.com/package/fft.js
*/
declare 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: number);
size: number;
_csize: number;
table: Float64Array;
_width: number;
_bitrev: Int32Array;
/**
* Create a complex number array with size `2 * size`
*
* @returns {Float64Array} A complex number array with size `2 * size`
*/
createComplexArray(): Float64Array;
/**
* 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: Float64Array, storage?: number[]): number[];
/**
* 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: Float64Array, storage?: Float64Array): Float64Array;
/**
* 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: Float64Array, data: Float64Array): void;
/**
* 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: Float64Array, data: Float64Array): void;
/**
* 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: Float64Array, data: Float64Array): void;
/**
* 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: Float64Array, data: Float64Array, inv: number): void;
/**
* 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: Float64Array, out: Float64Array, outOff: number, off: number, step: number): void;
/**
* 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: Float64Array, out: Float64Array, outOff: number, off: number, step: number, inv: number): void;
/**
* 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: Float64Array, data: Float64Array, inv: number): void;
/**
* 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: Float64Array, out: Float64Array, outOff: number, off: number, step: number): void;
/**
* 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: Float64Array, out: Float64Array, outOff: number, off: number, step: number, inv: number): void;
}
/**
* NP2FFT class provides functionality for performing Fast Fourier Transform on arrays
* which are not a power of two in length. In such cases, the chirp-z transform is used.
*
* For more information, see: https://math.stackexchange.com/questions/77118/non-power-of-2-ffts/77156#77156
*/
declare class NP2FFT {
/**
* Constructs a new NP2FFT object.
* @param {number} fft_length The length of the FFT
*/
constructor(fft_length: number);
bufferSize: number;
_a: number;
_chirpBuffer: Float64Array;
_buffer1: Float64Array;
_buffer2: Float64Array;
_outBuffer1: Float64Array;
_outBuffer2: Float64Array;
_slicedChirpBuffer: Float64Array;
_f: P2FFT;
_transform(output: any, input: any, real: any): void;
transform(output: any, input: any): void;
realTransform(output: any, input: any): void;
}
export {};
//# sourceMappingURL=maths.d.ts.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"maths.d.ts","sourceRoot":"","sources":["../../src/utils/maths.js"],"names":[],"mappings":"AACA;;;;;;;GAOG;AAEH;;;;GAIG;AAEH;;GAEG;AACH,wCAFW,UAAU,yIAqEpB;AAGD;;;;;;;GAOG;AACH,sEAJW,MAAM,EAAE,QACR,MAAM,EAAE,iBAiClB;AAGD;;;;;GAKG;AACH,oEAeC;AAED;;;;;GAKG;AACH,wEAQC;AAED;;;;;GAKG;AACH,0BAJW,MAAM,EAAE,QACR,MAAM,EAAE,GACN,MAAM,CAQlB;AAGD;;;;;GAKG;AACH,mCAJW,GAAG,EAAE,GAAC,UAAU,UAChB,MAAM,GAAC,IAAI,GACT,CAAC,MAAM,EAAE,GAAG,CAAC,EAAE,CAc3B;AAED;;;;;;GAMG;AACH,8BAJW,MAAM,EAAE,QACR,MAAM,EAAE,GACN,MAAM,CAgBlB;AAED;;;;GAIG;AACH,+BAHW,MAAM,EAAE,GACN,MAAM,CAIlB;AAGD;;;;;GAKG;AACH,yBAJW,MAAM,EAAE,GAAC,UAAU,GACjB,MAAM,EAAE,CAcpB;AAGD;;;;;GAKG;AACH,yBAJW,MAAM,EAAE,GAAC,aAAa,GACpB,CAAC,MAAM,EAAE,MAAM,CAAC,CAc5B;AAuoBD;;;;GAIG;AACH,mCAHW,aAAa,cACb,MAAM,OAmChB;AAED;;;;;GAKG;AACH,2BAJW,MAAM,YACN,MAAM,GACJ,MAAM,CAKlB;AAED;;;;;;;GAOG;AACH,iCAHW,MAAM,GACJ,MAAM,CAMlB;AAtFD;IACI,6BAUC;IATG,gBAA4B;IAC5B,sBAA4C;IAExC,oBAAgC;IAChC,yBAAsC;IAO9C,0CAEC;IAED,sCAEC;CACJ;yBA34BY,SAAS,GAAG,UAAU,GAAG,iBAAiB,GAAG,UAAU,GAAG,WAAW,GAAG,UAAU,GAAG,WAAW,GAAG,YAAY,GAAG,YAAY;4BAC9H,aAAa,GAAG,cAAc;4BAC9B,UAAU,GAAG,aAAa;AA4QvC;;;;;;GAMG;AACH;IACI;;;OAGG;IACH,kBAHW,MAAM,EAoChB;IAhCG,aAAoB;IAIpB,eAAuB;IAEvB,oBAA4C;IAe5C,eAAiD;IAGjD,oBAA+C;IAUnD;;;;OAIG;IACH,sBAFa,YAAY,CAIxB;IAED;;;;;;OAMG;IACH,0BAJW,YAAY,YACZ,MAAM,EAAE,GACN,MAAM,EAAE,CAOpB;IAED;;;;;OAKG;IACH,sBAJW,YAAY,YACZ,YAAY,GACV,YAAY,CASxB;IAED;;;;;;;;;OASG;IACH,eAPW,YAAY,QACZ,YAAY,GAIV,IAAI,CAOhB;IAED;;;;;;;;;OASG;IACH,mBALW,YAAY,QACZ,YAAY,QAStB;IAED;;;;;;;;;OASG;IACH,sBALW,YAAY,QACZ,YAAY,GAEV,IAAI,CAShB;IAED;;;;;;;OAOG;IACH,iBALW,YAAY,QACZ,YAAY,OACZ,MAAM,GACJ,IAAI,CA2FhB;IAED;;;;;;;;;OASG;IACH,wBAPW,YAAY,OACZ,YAAY,UACZ,MAAM,OACN,MAAM,QACN,MAAM,GACJ,IAAI,CAehB;IAED;;;;;;;;;;;OAWG;IACH,wBATW,YAAY,OACZ,YAAY,UACZ,MAAM,OACN,MAAM,QACN,MAAM,OACN,MAAM,GAEJ,IAAI,CAqChB;IAED;;;;;OAKG;IACH,qBAJW,YAAY,QACZ,YAAY,OACZ,MAAM,QAoHhB;IAED;;;;;;;;;;OAUG;IACH,4BARW,YAAY,OACZ,YAAY,UACZ,MAAM,OACN,MAAM,QACN,MAAM,GAEJ,IAAI,CAahB;IAED;;;;;;;;;;OAUG;IACH,4BAPW,YAAY,OACZ,YAAY,UACZ,MAAM,OACN,MAAM,QACN,MAAM,OACN,MAAM,QA6BhB;CACJ;AAED;;;;;GAKG;AACH;IAEI;;;OAGG;IACH,wBAFW,MAAM,EAkDhB;IA3CG,mBAAwB;IACxB,WAAW;IAMX,2BAA4C;IAC5C,uBAAwC;IACxC,uBAAwC;IACxC,0BAA2C;IAC3C,0BAA2C;IA0B3C,iCAA8C;IAI9C,UAAgC;IAIpC,qDA8CC;IAED,yCAEC;IAED,6CAEC;CACJ"}

View File

@@ -0,0 +1,318 @@
/**
* Permutes a tensor according to the provided axes.
* @param {any} tensor The input tensor to permute.
* @param {Array} axes The axes to permute the tensor along.
* @returns {Tensor} The permuted tensor.
*/
export function permute(tensor: any, axes: any[]): Tensor;
/**
* Interpolates an Tensor to the given size.
* @param {Tensor} input The input tensor to interpolate. Data must be channel-first (i.e., [c, h, w])
* @param {number[]} size The output size of the image
* @param {string} mode The interpolation mode
* @param {boolean} align_corners Whether to align corners.
* @returns {Tensor} The interpolated tensor.
*/
export function interpolate(input: Tensor, [out_height, out_width]: number[], mode?: string, align_corners?: boolean): Tensor;
/**
* Perform mean pooling of the last hidden state followed by a normalization step.
* @param {Tensor} last_hidden_state Tensor of shape [batchSize, seqLength, embedDim]
* @param {Tensor} attention_mask Tensor of shape [batchSize, seqLength]
* @returns {Tensor} Returns a new Tensor of shape [batchSize, embedDim].
*/
export function mean_pooling(last_hidden_state: Tensor, attention_mask: Tensor): Tensor;
/**
* Apply Layer Normalization for last certain number of dimensions.
* @param {Tensor} input The input tensor
* @param {number[]} normalized_shape input shape from an expected input of size
* @param {Object} options The options for the layer normalization
* @param {number} [options.eps=1e-5] A value added to the denominator for numerical stability.
* @returns {Tensor} The normalized tensor.
*/
export function layer_norm(input: Tensor, normalized_shape: number[], { eps, }?: {
eps?: number;
}): Tensor;
/**
* Concatenates an array of tensors along a specified dimension.
* @param {Tensor[]} tensors The array of tensors to concatenate.
* @param {number} dim The dimension to concatenate along.
* @returns {Tensor} The concatenated tensor.
*/
export function cat(tensors: Tensor[], dim?: number): Tensor;
/**
* Stack an array of tensors along a specified dimension.
* @param {Tensor[]} tensors The array of tensors to stack.
* @param {number} dim The dimension to stack along.
* @returns {Tensor} The stacked tensor.
*/
export function stack(tensors: Tensor[], dim?: number): Tensor;
/**
* Calculates the standard deviation and mean over the dimensions specified by dim. dim can be a single dimension or `null` to reduce over all dimensions.
* @param {Tensor} input the input tenso
* @param {number|null} dim the dimension to reduce. If None, all dimensions are reduced.
* @param {number} correction difference between the sample size and sample degrees of freedom. Defaults to Bessel's correction, correction=1.
* @param {boolean} keepdim whether the output tensor has dim retained or not.
* @returns {Tensor[]} A tuple of (std, mean) tensors.
*/
export function std_mean(input: Tensor, dim?: number | null, correction?: number, keepdim?: boolean): Tensor[];
/**
* Returns the mean value of each row of the input tensor in the given dimension dim.
* @param {Tensor} input the input tensor.
* @param {number|null} dim the dimension to reduce.
* @param {boolean} keepdim whether the output tensor has dim retained or not.
* @returns A new tensor with means taken along the specified dimension.
*/
export function mean(input: Tensor, dim?: number | null, keepdim?: boolean): Tensor;
/**
*
* Measures similarity between two temporal sequences (e.g., input audio and output tokens
* to generate token-level timestamps).
* @param {Tensor} matrix
* @returns {number[][]}
*/
export function dynamicTimeWarping(matrix: Tensor): number[][];
/**
* Returns a tensor filled with the scalar value 1, with the shape defined by the variable argument size.
* @param {number[]} size A sequence of integers defining the shape of the output tensor.
*/
export function ones(size: number[]): Tensor;
/**
* Returns a tensor filled with the scalar value 1, with the same size as input.
* @param {Tensor} tensor The size of input will determine size of the output tensor.
* @returns The ones tensor.
*/
export function ones_like(tensor: Tensor): Tensor;
/**
* Quantizes the embeddings tensor to binary or unsigned binary precision.
* @param {Tensor} tensor The tensor to quantize.
* @param {'binary'|'ubinary'} precision The precision to use for quantization.
* @returns {Tensor} The quantized tensor.
*/
export function quantize_embeddings(tensor: Tensor, precision: 'binary' | 'ubinary'): Tensor;
export class Tensor {
/**
* Create a new Tensor or copy an existing Tensor.
* @param {[DataType, DataArray, number[]]|[import('onnxruntime-common').Tensor]} args
*/
constructor(...args: [DataType, DataArray, number[]] | [import('onnxruntime-common').Tensor]);
/** @type {number[]} Dimensions of the tensor. */
dims: number[];
/** @type {DataType} Type of the tensor. */
type: DataType;
/** @type {DataArray} The data stored in the tensor. */
data: DataArray;
/** @type {number} The number of elements in the tensor. */
size: number;
/**
* Index into a Tensor object.
* @param {number} index The index to access.
* @returns {Tensor} The data at the specified index.
*/
_getitem(index: number): Tensor;
/**
* @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: number | bigint): number;
/**
* @param {number} index
* @param {number} iterSize
* @param {any} iterDims
* @returns {Tensor}
*/
_subarray(index: number, iterSize: number, iterDims: any): Tensor;
/**
* 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(): number | bigint;
/**
* Convert tensor data to a n-dimensional JS list
* @returns {Array}
*/
tolist(): any[];
/**
* Return a new Tensor with the sigmoid function applied to each element.
* @returns {Tensor} The tensor with the sigmoid function applied.
*/
sigmoid(): Tensor;
/**
* Applies the sigmoid function to the tensor in place.
* @returns {Tensor} Returns `this`.
*/
sigmoid_(): Tensor;
/**
* 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: number): Tensor;
/**
* Multiply the tensor by a constant in place.
* @param {number} val The value to multiply by.
* @returns {Tensor} Returns `this`.
*/
mul_(val: number): Tensor;
/**
* 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: number): Tensor;
/**
* Add the tensor by a constant in place.
* @param {number} val The value to add by.
* @returns {Tensor} Returns `this`.
*/
add_(val: number): Tensor;
clone(): Tensor;
slice(...slices: any[]): Tensor;
/**
* 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: number[]): Tensor;
transpose(...dims: any[]): Tensor;
/**
* 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?: number, keepdim?: boolean): Tensor;
/**
* 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?: number | string, dim?: number, keepdim?: boolean): Tensor;
/**
* 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?: number, dim?: number): Tensor;
/**
* 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?: number, dim?: number): Tensor;
/**
* 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(): number[];
/**
* 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 The squeezed tensor
*/
squeeze(dim?: number): Tensor;
/**
* In-place version of @see {@link Tensor.squeeze}
*/
squeeze_(dim?: any): 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 The unsqueezed tensor
*/
unsqueeze(dim?: number): Tensor;
/**
* In-place version of @see {@link Tensor.unsqueeze}
*/
unsqueeze_(dim?: any): this;
/**
* In-place version of @see {@link Tensor.flatten}
*/
flatten_(start_dim?: number, end_dim?: number): 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 The flattened tensor.
*/
flatten(start_dim?: number, end_dim?: number): Tensor;
/**
* 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: number[]): Tensor;
neg_(): this;
neg(): Tensor;
/**
* In-place version of @see {@link Tensor.clamp}
*/
clamp_(min: any, max: any): 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 the output tensor.
*/
clamp(min: number, max: number): Tensor;
/**
* In-place version of @see {@link Tensor.round}
*/
round_(): this;
/**
* Rounds elements of input to the nearest integer.
* @returns the output tensor.
*/
round(): Tensor;
/**
* Performs Tensor dtype conversion.
* @param {DataType} type The desired data type.
* @returns {Tensor} The converted tensor.
*/
to(type: DataType): Tensor;
/**
* 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](): Iterator<any, any, undefined>;
}
/**
* This creates a nested array of a given type and depth (see examples).
*/
export type NestArray<T, Depth extends number, Acc extends never[] = []> = Acc['length'] extends Depth ? T : NestArray<T[], Depth, [...Acc, never]>;
export type DataType = keyof typeof DataTypeMap;
export type DataArray = import('./maths.js').AnyTypedArray | any[];
declare const DataTypeMap: Readonly<{
float32: Float32ArrayConstructor;
float64: Float64ArrayConstructor;
string: ArrayConstructor;
int8: Int8ArrayConstructor;
uint8: Uint8ArrayConstructor;
int16: Int16ArrayConstructor;
uint16: Uint16ArrayConstructor;
int32: Int32ArrayConstructor;
uint32: Uint32ArrayConstructor;
int64: BigInt64ArrayConstructor;
uint64: BigUint64ArrayConstructor;
bool: Uint8ArrayConstructor;
}>;
export {};
//# sourceMappingURL=tensor.d.ts.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"tensor.d.ts","sourceRoot":"","sources":["../../src/utils/tensor.js"],"names":[],"mappings":"AA2qBA;;;;;GAKG;AACH,gCAJW,GAAG,gBAED,MAAM,CAKlB;AAGD;;;;;;;GAOG;AACH,mCANW,MAAM,2BACN,MAAM,EAAE,SACR,MAAM,kBACN,OAAO,GACL,MAAM,CAiBlB;AAED;;;;;GAKG;AACH,gDAJW,MAAM,kBACN,MAAM,GACJ,MAAM,CAwClB;AAED;;;;;;;GAOG;AACH,kCANW,MAAM,oBACN,MAAM,EAAE;IAES,GAAG,GAApB,MAAM;IACJ,MAAM,CA4BlB;AAgED;;;;;GAKG;AACH,6BAJW,MAAM,EAAE,QACR,MAAM,GACJ,MAAM,CAyDlB;AAED;;;;;GAKG;AACH,+BAJW,MAAM,EAAE,QACR,MAAM,GACJ,MAAM,CAMlB;AAGD;;;;;;;GAOG;AACH,gCANW,MAAM,QACN,MAAM,GAAC,IAAI,eACX,MAAM,YACN,OAAO,GACL,MAAM,EAAE,CA8DpB;AAGD;;;;;;GAMG;AACH,4BALW,MAAM,QACN,MAAM,GAAC,IAAI,YACX,OAAO,UAsDjB;AAGD;;;;;;GAMG;AACH,2CAHW,MAAM,GACJ,MAAM,EAAE,EAAE,CAsFtB;AAWD;;;GAGG;AACH,2BAFW,MAAM,EAAE,UASlB;AAED;;;;GAIG;AACH,kCAHW,MAAM,UAKhB;AAED;;;;;GAKG;AACH,4CAJW,MAAM,aACN,QAAQ,GAAC,SAAS,GAChB,MAAM,CAsClB;AA/qCD;IAaI;;;OAGG;IACH,qBAFW,CAAC,QAAQ,EAAE,SAAS,EAAE,MAAM,EAAE,CAAC,GAAC,CAAC,OAAO,oBAAoB,EAAE,MAAM,CAAC,EAmC/E;IAjDD,iDAAiD;IACjD,MADW,MAAM,EAAE,CACd;IAEL,2CAA2C;IAC3C,MADW,QAAQ,CACd;IAEL,uDAAuD;IACvD,MADW,SAAS,CACf;IAEL,2DAA2D;IAC3D,MADW,MAAM,CACZ;IA4DL;;;;OAIG;IACH,gBAHW,MAAM,GACJ,MAAM,CAalB;IAED;;;OAGG;IACH,cAHW,MAAM,GAAC,MAAM,GACX,MAAM,CAUlB;IAED;;;;;OAKG;IACH,iBALW,MAAM,YACN,MAAM,YACN,GAAG,GACD,MAAM,CAYlB;IAED;;;;;OAKG;IACH,QAHa,MAAM,GAAC,MAAM,CAQzB;IAED;;;OAGG;IACH,gBAEC;IAED;;;OAGG;IACH,WAFa,MAAM,CAIlB;IAED;;;OAGG;IACH,YAFa,MAAM,CAOlB;IAED;;;;OAIG;IACH,SAHW,MAAM,GACJ,MAAM,CAIlB;IAED;;;;OAIG;IACH,UAHW,MAAM,GACJ,MAAM,CAOlB;IAGD;;;;OAIG;IACH,SAHW,MAAM,GACJ,MAAM,CAIlB;IAED;;;;OAIG;IACH,UAHW,MAAM,GACJ,MAAM,CAOlB;IACD,gBAEC;IAED,gCA8DC;IAED;;;;OAIG;IACH,iBAHe,MAAM,KACR,MAAM,CAIlB;IAGD,kCAEC;IAID;;;;;;OAMG;IACH,UAJW,MAAM,YACN,OAAO,UAKjB;IAED;;;;;;;OAOG;IACH,SANW,MAAM,GAAC,MAAM,QACb,MAAM,YAEN,OAAO,GACL,MAAM,CA0DlB;IAED;;;;;OAKG;IACH,eAJW,MAAM,QACN,MAAM,GACJ,MAAM,CA2BlB;IAED;;;;;OAKG;IACH,cAJW,MAAM,QACN,MAAM,GACJ,MAAM,CAIlB;IAED;;;;OAIG;IACH,UAFa,MAAM,EAAE,CAIpB;IAED;;;;;;;;OAQG;IACH,cAHW,MAAM,UAShB;IAED;;OAEG;IACH,0BAGC;IAED;;;;;;;OAOG;IACH,gBAHW,MAAM,UAShB;IAED;;OAEG;IACH,4BAGC;IAED;;OAEG;IACH,qDAUC;IAED;;;;;;;OAOG;IACH,oBAJW,MAAM,YACN,MAAM,UAKhB;IAED;;;;OAIG;IACH,cAHe,MAAM,KACR,MAAM,CAuBlB;IAED,aAKC;IACD,cAEC;IAED;;OAEG;IACH,iCAKC;IAED;;;;;OAKG;IACH,WAJW,MAAM,OACN,MAAM,UAKhB;IAED;;OAEG;IACH,eAKC;IAED;;;OAGG;IACH,gBAEC;IAED;;;;OAIG;IACH,SAHW,QAAQ,GACN,MAAM,CAYlB;IAnhBD;;;;OAIG;IACH,mDAYC;CAmgBJ;;;;2EAcY,GAAG,CAAC,QAAQ,CAAC,SAAS,KAAK,GAAG,CAAC,GAAG,UAAU,CAAC,EAAE,EAAE,KAAK,EAAE,CAAC,GAAG,GAAG,EAAE,KAAK,CAAC,CAAC;uBA5lBxE,MAAM,kBAAkB;wBACxB,OAAO,YAAY,EAAE,aAAa,GAAG,GAAG,EAAE;AAjBvD;;;;;;;;;;;;;GAaG"}