@huggingface/transformers 3.0.0 → 3.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -11,25 +11,19 @@
11
11
  </p>
12
12
 
13
13
  <p align="center">
14
- <a href="https://www.npmjs.com/package/@huggingface/transformers">
15
- <img alt="NPM" src="https://img.shields.io/npm/v/@huggingface/transformers">
16
- </a>
17
- <a href="https://www.npmjs.com/package/@huggingface/transformers">
18
- <img alt="NPM Downloads" src="https://img.shields.io/npm/dw/@huggingface/transformers">
19
- </a>
20
- <a href="https://www.jsdelivr.com/package/npm/@huggingface/transformers">
21
- <img alt="jsDelivr Hits" src="https://img.shields.io/jsdelivr/npm/hw/@huggingface/transformers">
22
- </a>
23
- <a href="https://github.com/huggingface/transformers.js/blob/main/LICENSE">
24
- <img alt="License" src="https://img.shields.io/github/license/huggingface/transformers.js?color=blue">
25
- </a>
26
- <a href="https://huggingface.co/docs/transformers.js/index">
27
- <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">
28
- </a>
14
+ <a href="https://www.npmjs.com/package/@huggingface/transformers"><img alt="NPM" src="https://img.shields.io/npm/v/@huggingface/transformers"></a>
15
+ <a href="https://www.npmjs.com/package/@huggingface/transformers"><img alt="NPM Downloads" src="https://img.shields.io/npm/dw/@huggingface/transformers"></a>
16
+ <a href="https://www.jsdelivr.com/package/npm/@huggingface/transformers"><img alt="jsDelivr Hits" src="https://img.shields.io/jsdelivr/npm/hw/@huggingface/transformers"></a>
17
+ <a href="https://github.com/huggingface/transformers.js/blob/main/LICENSE"><img alt="License" src="https://img.shields.io/github/license/huggingface/transformers.js?color=blue"></a>
18
+ <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>
29
19
  </p>
30
20
 
31
21
 
32
- State-of-the-art Machine Learning for the web. Run 🤗 Transformers directly in your browser, with no need for a server!
22
+ <h3 align="center">
23
+ <p>State-of-the-art Machine Learning for the Web</p>
24
+ </h3>
25
+
26
+ Run 🤗 Transformers directly in your browser, with no need for a server!
33
27
 
34
28
  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:
35
29
  - 📝 **Natural Language Processing**: text classification, named entity recognition, question answering, language modeling, summarization, translation, multiple choice, and text generation.
@@ -42,6 +36,22 @@ Transformers.js uses [ONNX Runtime](https://onnxruntime.ai/) to run models in th
42
36
  For more information, check out the full [documentation](https://huggingface.co/docs/transformers.js).
43
37
 
44
38
 
39
+ ## Installation
40
+
41
+
42
+ To install via [NPM](https://www.npmjs.com/package/@huggingface/transformers), run:
43
+ ```bash
44
+ npm i @huggingface/transformers
45
+ ```
46
+
47
+ 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:
48
+ ```html
49
+ <script type="module">
50
+ import { pipeline } from 'https://cdn.jsdelivr.net/npm/@huggingface/transformers@3.0.1';
51
+ </script>
52
+ ```
53
+
54
+
45
55
  ## Quick tour
46
56
 
47
57
 
@@ -72,9 +82,9 @@ out = pipe('I love transformers!')
72
82
  import { pipeline } from '@huggingface/transformers';
73
83
 
74
84
  // Allocate a pipeline for sentiment-analysis
75
- let pipe = await pipeline('sentiment-analysis');
85
+ const pipe = await pipeline('sentiment-analysis');
76
86
 
77
- let out = await pipe('I love transformers!');
87
+ const out = await pipe('I love transformers!');
78
88
  // [{'label': 'POSITIVE', 'score': 0.999817686}]
79
89
  ```
80
90
 
@@ -86,29 +96,40 @@ let out = await pipe('I love transformers!');
86
96
  You can also use a different model by specifying the model id or path as the second argument to the `pipeline` function. For example:
87
97
  ```javascript
88
98
  // Use a different model for sentiment-analysis
89
- let pipe = await pipeline('sentiment-analysis', 'Xenova/bert-base-multilingual-uncased-sentiment');
99
+ const pipe = await pipeline('sentiment-analysis', 'Xenova/bert-base-multilingual-uncased-sentiment');
90
100
  ```
91
101
 
102
+ By default, when running in the browser, the model will be run on your CPU (via WASM). If you would like
103
+ to run the model on your GPU (via WebGPU), you can do this by setting `device: 'webgpu'`, for example:
104
+ ```javascript
105
+ // Run the model on WebGPU
106
+ const pipe = await pipeline('sentiment-analysis', 'Xenova/distilbert-base-uncased-finetuned-sst-2-english', {
107
+ device: 'webgpu',
108
+ });
109
+ ```
92
110
 
93
- ## Installation
94
-
111
+ For more information, check out the [WebGPU guide](https://huggingface.co/docs/transformers.js/guides/webgpu).
95
112
 
96
- To install via [NPM](https://www.npmjs.com/package/@huggingface/transformers), run:
97
- ```bash
98
- npm i @huggingface/transformers
99
- ```
113
+ > [!WARNING]
114
+ > The WebGPU API is still experimental in many browsers, so if you run into any issues,
115
+ > please file a [bug report](https://github.com/huggingface/transformers.js/issues/new?title=%5BWebGPU%5D%20Error%20running%20MODEL_ID_GOES_HERE&assignees=&labels=bug,webgpu&projects=&template=1_bug-report.yml).
100
116
 
101
- 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:
102
- ```html
103
- <script type="module">
104
- import { pipeline } from 'https://cdn.jsdelivr.net/npm/@huggingface/transformers@3.0.0';
105
- </script>
117
+ In resource-constrained environments, such as web browsers, it is advisable to use a quantized version of
118
+ the model to lower bandwidth and optimize performance. This can be achieved by adjusting the `dtype` option,
119
+ which allows you to select the appropriate data type for your model. While the available options may vary
120
+ depending on the specific model, typical choices include `"fp32"` (default for WebGPU), `"fp16"`, `"q8"`
121
+ (default for WASM), and `"q4"`. For more information, check out the [quantization guide](https://huggingface.co/docs/transformers.js/guides/dtypes).
122
+ ```javascript
123
+ // Run the model at 4-bit quantization
124
+ const pipe = await pipeline('sentiment-analysis', 'Xenova/distilbert-base-uncased-finetuned-sst-2-english', {
125
+ dtype: 'q4',
126
+ });
106
127
  ```
107
128
 
108
129
 
109
130
  ## Examples
110
131
 
111
- Want to jump straight in? Get started with one of our sample applications/templates:
132
+ Want to jump straight in? Get started with one of our sample applications/templates, which can be found [here](https://github.com/huggingface/transformers.js-examples).
112
133
 
113
134
  | Name | Description | Links |
114
135
  |-------------------|----------------------------------|-------------------------------|
@@ -134,7 +155,7 @@ Check out the Transformers.js [template](https://huggingface.co/new-space?templa
134
155
 
135
156
 
136
157
 
137
- By default, Transformers.js uses [hosted pretrained models](https://huggingface.co/models?library=transformers.js) and [precompiled WASM binaries](https://cdn.jsdelivr.net/npm/@huggingface/transformers@3.0.0/dist/), which should work out-of-the-box. You can customize this as follows:
158
+ By default, Transformers.js uses [hosted pretrained models](https://huggingface.co/models?library=transformers.js) and [precompiled WASM binaries](https://cdn.jsdelivr.net/npm/@huggingface/transformers@3.0.1/dist/), which should work out-of-the-box. You can customize this as follows:
138
159
 
139
160
  ### Settings
140
161
 
@@ -4460,7 +4460,7 @@ __webpack_require__.r(__webpack_exports__);
4460
4460
 
4461
4461
 
4462
4462
 
4463
- const VERSION = '3.0.0';
4463
+ const VERSION = '3.0.1';
4464
4464
 
4465
4465
  // Check if various APIs are available (depends on environment)
4466
4466
  const IS_BROWSER_ENV = typeof self !== 'undefined';
@@ -4508,7 +4508,7 @@ const apis = Object.freeze({
4508
4508
 
4509
4509
  const RUNNING_LOCALLY = IS_FS_AVAILABLE && IS_PATH_AVAILABLE;
4510
4510
  const dirname__ = RUNNING_LOCALLY
4511
- ? path__WEBPACK_IMPORTED_MODULE_1__.dirname(path__WEBPACK_IMPORTED_MODULE_1__.dirname(url__WEBPACK_IMPORTED_MODULE_2__.fileURLToPath("file:///workspaces/transformers.js/src/env.js")))
4511
+ ? path__WEBPACK_IMPORTED_MODULE_1__.dirname(path__WEBPACK_IMPORTED_MODULE_1__.dirname(url__WEBPACK_IMPORTED_MODULE_2__.fileURLToPath("file:///home/runner/work/transformers.js/transformers.js/src/env.js")))
4512
4512
  : './';
4513
4513
 
4514
4514
  // Only used for environments with access to file system
@@ -7181,7 +7181,7 @@ function replaceTensors(obj) {
7181
7181
 
7182
7182
  /**
7183
7183
  * Converts an array or Tensor of integers to an int64 Tensor.
7184
- * @param {Array|Tensor} items The input integers to be converted.
7184
+ * @param {any[]|Tensor} items The input integers to be converted.
7185
7185
  * @returns {Tensor} The int64 Tensor with the converted values.
7186
7186
  * @throws {Error} If the input array is empty or the input is a batched Tensor and not all sequences have the same length.
7187
7187
  * @private
@@ -8104,35 +8104,37 @@ class PreTrainedModel extends _utils_generic_js__WEBPACK_IMPORTED_MODULE_3__.Cal
8104
8104
  let { decoder_input_ids, ...model_inputs } = model_kwargs;
8105
8105
 
8106
8106
  // Prepare input ids if the user has not defined `decoder_input_ids` manually.
8107
- if (!decoder_input_ids) {
8108
- decoder_start_token_id ??= bos_token_id;
8109
-
8110
- if (this.config.model_type === 'musicgen') {
8111
- // Custom logic (TODO: move to Musicgen class)
8112
- decoder_input_ids = Array.from({
8113
- length: batch_size * this.config.decoder.num_codebooks
8114
- }, () => [decoder_start_token_id]);
8115
-
8116
- } else if (Array.isArray(decoder_start_token_id)) {
8117
- if (decoder_start_token_id.length !== batch_size) {
8118
- throw new Error(
8119
- `\`decoder_start_token_id\` expcted to have length ${batch_size} but got ${decoder_start_token_id.length}`
8120
- )
8107
+ if (!(decoder_input_ids instanceof _utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.Tensor)) {
8108
+ if (!decoder_input_ids) {
8109
+ decoder_start_token_id ??= bos_token_id;
8110
+
8111
+ if (this.config.model_type === 'musicgen') {
8112
+ // Custom logic (TODO: move to Musicgen class)
8113
+ decoder_input_ids = Array.from({
8114
+ length: batch_size * this.config.decoder.num_codebooks
8115
+ }, () => [decoder_start_token_id]);
8116
+
8117
+ } else if (Array.isArray(decoder_start_token_id)) {
8118
+ if (decoder_start_token_id.length !== batch_size) {
8119
+ throw new Error(
8120
+ `\`decoder_start_token_id\` expcted to have length ${batch_size} but got ${decoder_start_token_id.length}`
8121
+ )
8122
+ }
8123
+ decoder_input_ids = decoder_start_token_id;
8124
+ } else {
8125
+ decoder_input_ids = Array.from({
8126
+ length: batch_size,
8127
+ }, () => [decoder_start_token_id]);
8121
8128
  }
8122
- decoder_input_ids = decoder_start_token_id;
8123
- } else {
8129
+ } else if (!Array.isArray(decoder_input_ids[0])) {
8130
+ // Correct batch size
8124
8131
  decoder_input_ids = Array.from({
8125
8132
  length: batch_size,
8126
- }, () => [decoder_start_token_id]);
8133
+ }, () => decoder_input_ids);
8127
8134
  }
8128
- } else if (!Array.isArray(decoder_input_ids[0])) {
8129
- // Correct batch size
8130
- decoder_input_ids = Array.from({
8131
- length: batch_size,
8132
- }, () => decoder_input_ids);
8135
+ decoder_input_ids = toI64Tensor(decoder_input_ids);
8133
8136
  }
8134
8137
 
8135
- decoder_input_ids = toI64Tensor(decoder_input_ids);
8136
8138
  model_kwargs['decoder_attention_mask'] = (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.ones_like)(decoder_input_ids);
8137
8139
 
8138
8140
  return { input_ids: decoder_input_ids, model_inputs };
@@ -9955,8 +9957,11 @@ class WhisperForConditionalGeneration extends WhisperPreTrainedModel {
9955
9957
  class VisionEncoderDecoderModel extends PreTrainedModel {
9956
9958
  main_input_name = 'pixel_values';
9957
9959
  forward_params = [
9960
+ // Encoder inputs
9958
9961
  'pixel_values',
9959
- 'input_ids',
9962
+
9963
+ // Decoder inpputs
9964
+ 'decoder_input_ids',
9960
9965
  'encoder_hidden_states',
9961
9966
  'past_key_values',
9962
9967
  ];
@@ -16572,7 +16577,6 @@ class DocumentQuestionAnsweringPipeline extends (/** @type {new (options: TextIm
16572
16577
 
16573
16578
  /** @type {DocumentQuestionAnsweringPipelineCallback} */
16574
16579
  async _call(image, question, generate_kwargs = {}) {
16575
- throw new Error('This pipeline is not yet supported in Transformers.js v3.'); // TODO: Remove when implemented
16576
16580
 
16577
16581
  // NOTE: For now, we only support a batch size of 1
16578
16582
 
@@ -17373,6 +17377,7 @@ __webpack_require__.r(__webpack_exports__);
17373
17377
  /* harmony export */ DeiTFeatureExtractor: () => (/* binding */ DeiTFeatureExtractor),
17374
17378
  /* harmony export */ DetrFeatureExtractor: () => (/* binding */ DetrFeatureExtractor),
17375
17379
  /* harmony export */ DonutFeatureExtractor: () => (/* binding */ DonutFeatureExtractor),
17380
+ /* harmony export */ DonutImageProcessor: () => (/* binding */ DonutImageProcessor),
17376
17381
  /* harmony export */ EfficientNetImageProcessor: () => (/* binding */ EfficientNetImageProcessor),
17377
17382
  /* harmony export */ FeatureExtractor: () => (/* binding */ FeatureExtractor),
17378
17383
  /* harmony export */ Florence2Processor: () => (/* binding */ Florence2Processor),
@@ -18615,6 +18620,7 @@ class DonutFeatureExtractor extends ImageFeatureExtractor {
18615
18620
  });
18616
18621
  }
18617
18622
  }
18623
+ class DonutImageProcessor extends DonutFeatureExtractor { } // NOTE extends DonutFeatureExtractor
18618
18624
  class NougatImageProcessor extends DonutFeatureExtractor { } // NOTE extends DonutFeatureExtractor
18619
18625
 
18620
18626
  /**
@@ -19975,6 +19981,7 @@ class AutoProcessor {
19975
19981
  MaskFormerFeatureExtractor,
19976
19982
  YolosFeatureExtractor,
19977
19983
  DonutFeatureExtractor,
19984
+ DonutImageProcessor,
19978
19985
  NougatImageProcessor,
19979
19986
  EfficientNetImageProcessor,
19980
19987
 
@@ -30304,6 +30311,7 @@ __webpack_require__.r(__webpack_exports__);
30304
30311
  /* harmony export */ DistilBertTokenizer: () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.DistilBertTokenizer),
30305
30312
  /* harmony export */ DocumentQuestionAnsweringPipeline: () => (/* reexport safe */ _pipelines_js__WEBPACK_IMPORTED_MODULE_1__.DocumentQuestionAnsweringPipeline),
30306
30313
  /* harmony export */ DonutFeatureExtractor: () => (/* reexport safe */ _processors_js__WEBPACK_IMPORTED_MODULE_4__.DonutFeatureExtractor),
30314
+ /* harmony export */ DonutImageProcessor: () => (/* reexport safe */ _processors_js__WEBPACK_IMPORTED_MODULE_4__.DonutImageProcessor),
30307
30315
  /* harmony export */ DonutSwinModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.DonutSwinModel),
30308
30316
  /* harmony export */ DonutSwinPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.DonutSwinPreTrainedModel),
30309
30317
  /* harmony export */ EfficientNetForImageClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.EfficientNetForImageClassification),