parakeet.js 0.0.2 → 0.0.3
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/.gitmodules +3 -0
- package/README.md +240 -239
- package/examples/hf-spaces-demo/README.md +6 -9
- package/examples/hf-spaces-demo/package.json +1 -1
- package/examples/hf-spaces-demo/src/App.js +307 -316
- package/examples/react-demo/package.json +19 -19
- package/examples/react-demo/src/App.jsx +324 -326
- package/examples/react-demo-dev/src/App.jsx +23 -24
- package/package.json +1 -1
- package/publish.ps1 +65 -0
- package/src/hub.js +235 -241
- package/src/parakeet.js +15 -8
- package/src/preprocessor.js +75 -68
- package/docs/parakeet-transformers-js/.gitattributes +0 -2
- package/docs/parakeet-transformers-js/.prettierignore +0 -8
- package/docs/parakeet-transformers-js/.prettierrc +0 -10
- package/docs/parakeet-transformers-js/.tmp_features.json +0 -1
- package/docs/parakeet-transformers-js/LICENSE +0 -202
- package/docs/parakeet-transformers-js/README.md +0 -448
- package/docs/parakeet-transformers-js/assets/nemo128.onnx +0 -0
- package/docs/parakeet-transformers-js/assets/nemo80.onnx +0 -0
- package/docs/parakeet-transformers-js/debug_test.js +0 -84
- package/docs/parakeet-transformers-js/dev/inspect_decoder.cjs +0 -9
- package/docs/parakeet-transformers-js/dev/inspect_joiner.cjs +0 -9
- package/docs/parakeet-transformers-js/dev/js_step_by_step.js +0 -249
- package/docs/parakeet-transformers-js/dev/parakeet_cli.js +0 -91
- package/docs/parakeet-transformers-js/jest.config.mjs +0 -194
- package/docs/parakeet-transformers-js/js_preprocessing.json +0 -225
- package/docs/parakeet-transformers-js/js_step_by_step.json +0 -837
- package/docs/parakeet-transformers-js/js_step_by_step_v2.json +0 -450
- package/docs/parakeet-transformers-js/js_step_by_step_v3.json +0 -450
- package/docs/parakeet-transformers-js/js_steps.json +0 -821
- package/docs/parakeet-transformers-js/package-lock.json +0 -12251
- package/docs/parakeet-transformers-js/package.json +0 -96
- package/docs/parakeet-transformers-js/src/audio_features.js +0 -178
- package/docs/parakeet-transformers-js/src/backends/onnx.js +0 -210
- package/docs/parakeet-transformers-js/src/base/feature_extraction_utils.js +0 -54
- package/docs/parakeet-transformers-js/src/base/image_processors_utils.js +0 -1105
- package/docs/parakeet-transformers-js/src/base/processing_utils.js +0 -173
- package/docs/parakeet-transformers-js/src/configs.js +0 -455
- package/docs/parakeet-transformers-js/src/env.js +0 -167
- package/docs/parakeet-transformers-js/src/generation/configuration_utils.js +0 -388
- package/docs/parakeet-transformers-js/src/generation/logits_process.js +0 -727
- package/docs/parakeet-transformers-js/src/generation/logits_sampler.js +0 -204
- package/docs/parakeet-transformers-js/src/generation/parameters.js +0 -35
- package/docs/parakeet-transformers-js/src/generation/stopping_criteria.js +0 -156
- package/docs/parakeet-transformers-js/src/generation/streamers.js +0 -225
- package/docs/parakeet-transformers-js/src/models/audio_spectrogram_transformer/feature_extraction_audio_spectrogram_transformer.js +0 -85
- package/docs/parakeet-transformers-js/src/models/auto/feature_extraction_auto.js +0 -25
- package/docs/parakeet-transformers-js/src/models/auto/image_processing_auto.js +0 -29
- package/docs/parakeet-transformers-js/src/models/auto/processing_auto.js +0 -85
- package/docs/parakeet-transformers-js/src/models/beit/image_processing_beit.js +0 -5
- package/docs/parakeet-transformers-js/src/models/bit/image_processing_bit.js +0 -5
- package/docs/parakeet-transformers-js/src/models/chinese_clip/image_processing_chinese_clip.js +0 -5
- package/docs/parakeet-transformers-js/src/models/clap/feature_extraction_clap.js +0 -159
- package/docs/parakeet-transformers-js/src/models/clip/image_processing_clip.js +0 -6
- package/docs/parakeet-transformers-js/src/models/convnext/image_processing_convnext.js +0 -46
- package/docs/parakeet-transformers-js/src/models/dac/feature_extraction_dac.js +0 -3
- package/docs/parakeet-transformers-js/src/models/deit/image_processing_deit.js +0 -6
- package/docs/parakeet-transformers-js/src/models/detr/image_processing_detr.js +0 -52
- package/docs/parakeet-transformers-js/src/models/donut/image_processing_donut.js +0 -31
- package/docs/parakeet-transformers-js/src/models/dpt/image_processing_dpt.js +0 -6
- package/docs/parakeet-transformers-js/src/models/efficientnet/image_processing_efficientnet.js +0 -14
- package/docs/parakeet-transformers-js/src/models/encodec/feature_extraction_encodec.js +0 -32
- package/docs/parakeet-transformers-js/src/models/feature_extractors.js +0 -17
- package/docs/parakeet-transformers-js/src/models/florence2/processing_florence2.js +0 -131
- package/docs/parakeet-transformers-js/src/models/gemma3n/feature_extraction_gemma3n.js +0 -97
- package/docs/parakeet-transformers-js/src/models/gemma3n/processing_gemma3n.js +0 -74
- package/docs/parakeet-transformers-js/src/models/glpn/image_processing_glpn.js +0 -5
- package/docs/parakeet-transformers-js/src/models/grounding_dino/image_processing_grounding_dino.js +0 -29
- package/docs/parakeet-transformers-js/src/models/grounding_dino/processing_grounding_dino.js +0 -101
- package/docs/parakeet-transformers-js/src/models/idefics3/image_processing_idefics3.js +0 -232
- package/docs/parakeet-transformers-js/src/models/idefics3/processing_idefics3.js +0 -136
- package/docs/parakeet-transformers-js/src/models/image_processors.js +0 -40
- package/docs/parakeet-transformers-js/src/models/janus/image_processing_janus.js +0 -27
- package/docs/parakeet-transformers-js/src/models/janus/processing_janus.js +0 -123
- package/docs/parakeet-transformers-js/src/models/jina_clip/image_processing_jina_clip.js +0 -26
- package/docs/parakeet-transformers-js/src/models/jina_clip/processing_jina_clip.js +0 -24
- package/docs/parakeet-transformers-js/src/models/llava/processing_llava.js +0 -44
- package/docs/parakeet-transformers-js/src/models/llava_onevision/image_processing_llava_onevision.js +0 -5
- package/docs/parakeet-transformers-js/src/models/mask2former/image_processing_mask2former.js +0 -5
- package/docs/parakeet-transformers-js/src/models/maskformer/image_processing_maskformer.js +0 -18
- package/docs/parakeet-transformers-js/src/models/mgp_str/processing_mgp_str.js +0 -172
- package/docs/parakeet-transformers-js/src/models/mobilenet_v1/image_processing_mobilenet_v1.js +0 -7
- package/docs/parakeet-transformers-js/src/models/mobilenet_v2/image_processing_mobilenet_v2.js +0 -7
- package/docs/parakeet-transformers-js/src/models/mobilenet_v3/image_processing_mobilenet_v3.js +0 -7
- package/docs/parakeet-transformers-js/src/models/mobilenet_v4/image_processing_mobilenet_v4.js +0 -7
- package/docs/parakeet-transformers-js/src/models/mobilevit/image_processing_mobilevit.js +0 -6
- package/docs/parakeet-transformers-js/src/models/moonshine/feature_extraction_moonshine.js +0 -26
- package/docs/parakeet-transformers-js/src/models/moonshine/processing_moonshine.js +0 -20
- package/docs/parakeet-transformers-js/src/models/nougat/image_processing_nougat.js +0 -5
- package/docs/parakeet-transformers-js/src/models/owlv2/image_processing_owlv2.js +0 -5
- package/docs/parakeet-transformers-js/src/models/owlvit/image_processing_owlvit.js +0 -12
- package/docs/parakeet-transformers-js/src/models/owlvit/processing_owlvit.js +0 -7
- package/docs/parakeet-transformers-js/src/models/paligemma/processing_paligemma.js +0 -83
- package/docs/parakeet-transformers-js/src/models/parakeet/feature_extraction_parakeet.js +0 -3
- package/docs/parakeet-transformers-js/src/models/parakeet/modeling_parakeet.js +0 -3
- package/docs/parakeet-transformers-js/src/models/parakeet/processing_parakeet.js +0 -3
- package/docs/parakeet-transformers-js/src/models/parakeet/tokenization_parakeet.js +0 -3
- package/docs/parakeet-transformers-js/src/models/phi3_v/image_processing_phi3_v.js +0 -163
- package/docs/parakeet-transformers-js/src/models/phi3_v/processing_phi3_v.js +0 -53
- package/docs/parakeet-transformers-js/src/models/processors.js +0 -22
- package/docs/parakeet-transformers-js/src/models/pvt/image_processing_pvt.js +0 -5
- package/docs/parakeet-transformers-js/src/models/pyannote/feature_extraction_pyannote.js +0 -85
- package/docs/parakeet-transformers-js/src/models/pyannote/processing_pyannote.js +0 -24
- package/docs/parakeet-transformers-js/src/models/qwen2_vl/image_processing_qwen2_vl.js +0 -52
- package/docs/parakeet-transformers-js/src/models/qwen2_vl/processing_qwen2_vl.js +0 -53
- package/docs/parakeet-transformers-js/src/models/rt_detr/image_processing_rt_detr.js +0 -12
- package/docs/parakeet-transformers-js/src/models/sam/image_processing_sam.js +0 -242
- package/docs/parakeet-transformers-js/src/models/sam/processing_sam.js +0 -20
- package/docs/parakeet-transformers-js/src/models/sapiens/image_processing_sapiens.js +0 -13
- package/docs/parakeet-transformers-js/src/models/seamless_m4t/feature_extraction_seamless_m4t.js +0 -175
- package/docs/parakeet-transformers-js/src/models/segformer/image_processing_segformer.js +0 -13
- package/docs/parakeet-transformers-js/src/models/siglip/image_processing_siglip.js +0 -5
- package/docs/parakeet-transformers-js/src/models/smolvlm/image_processing_smolvlm.js +0 -2
- package/docs/parakeet-transformers-js/src/models/smolvlm/processing_smolvlm.js +0 -2
- package/docs/parakeet-transformers-js/src/models/snac/feature_extraction_snac.js +0 -3
- package/docs/parakeet-transformers-js/src/models/speecht5/feature_extraction_speecht5.js +0 -4
- package/docs/parakeet-transformers-js/src/models/speecht5/processing_speecht5.js +0 -17
- package/docs/parakeet-transformers-js/src/models/swin2sr/image_processing_swin2sr.js +0 -24
- package/docs/parakeet-transformers-js/src/models/ultravox/processing_ultravox.js +0 -54
- package/docs/parakeet-transformers-js/src/models/vit/image_processing_vit.js +0 -7
- package/docs/parakeet-transformers-js/src/models/vitmatte/image_processing_vitmatte.js +0 -50
- package/docs/parakeet-transformers-js/src/models/vitpose/image_processing_vitpose.js +0 -89
- package/docs/parakeet-transformers-js/src/models/wav2vec2/feature_extraction_wav2vec2.js +0 -44
- package/docs/parakeet-transformers-js/src/models/wav2vec2/processing_wav2vec2.js +0 -17
- package/docs/parakeet-transformers-js/src/models/wav2vec2_with_lm/processing_wav2vec2_with_lm.js +0 -17
- package/docs/parakeet-transformers-js/src/models/wespeaker/feature_extraction_wespeaker.js +0 -95
- package/docs/parakeet-transformers-js/src/models/whisper/common_whisper.js +0 -157
- package/docs/parakeet-transformers-js/src/models/whisper/feature_extraction_whisper.js +0 -92
- package/docs/parakeet-transformers-js/src/models/whisper/generation_whisper.js +0 -89
- package/docs/parakeet-transformers-js/src/models/whisper/processing_whisper.js +0 -21
- package/docs/parakeet-transformers-js/src/models/yolos/image_processing_yolos.js +0 -12
- package/docs/parakeet-transformers-js/src/models.js +0 -8644
- package/docs/parakeet-transformers-js/src/ops/registry.js +0 -133
- package/docs/parakeet-transformers-js/src/ort_env.js +0 -8
- package/docs/parakeet-transformers-js/src/parakeet.js +0 -792
- package/docs/parakeet-transformers-js/src/pipelines.js +0 -3540
- package/docs/parakeet-transformers-js/src/processors.js +0 -16
- package/docs/parakeet-transformers-js/src/tokenizers.js +0 -4432
- package/docs/parakeet-transformers-js/src/transformers.js +0 -50
- package/docs/parakeet-transformers-js/src/utils/audio.js +0 -893
- package/docs/parakeet-transformers-js/src/utils/constants.js +0 -9
- package/docs/parakeet-transformers-js/src/utils/core.js +0 -259
- package/docs/parakeet-transformers-js/src/utils/data-structures.js +0 -574
- package/docs/parakeet-transformers-js/src/utils/devices.js +0 -22
- package/docs/parakeet-transformers-js/src/utils/dtypes.js +0 -63
- package/docs/parakeet-transformers-js/src/utils/generic.js +0 -35
- package/docs/parakeet-transformers-js/src/utils/hub.js +0 -780
- package/docs/parakeet-transformers-js/src/utils/image.js +0 -834
- package/docs/parakeet-transformers-js/src/utils/maths.js +0 -1061
- package/docs/parakeet-transformers-js/src/utils/tensor.js +0 -1539
- package/docs/parakeet-transformers-js/src/utils/video.js +0 -128
- package/docs/parakeet-transformers-js/test/decoder.test.js +0 -114
- package/docs/parakeet-transformers-js/test/encoder.test.js +0 -108
- package/docs/parakeet-transformers-js/test/preprocessor.test.js +0 -85
- package/docs/parakeet-transformers-js/test/tokenizer.test.js +0 -24
- package/docs/parakeet-transformers-js/test/transcribe.js +0 -89
- package/docs/parakeet-transformers-js/tsconfig.json +0 -21
- package/docs/parakeet-transformers-js/webpack.config.js +0 -223
|
@@ -1,574 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
/**
|
|
3
|
-
* @file Custom data structures.
|
|
4
|
-
*
|
|
5
|
-
* These are only used internally, meaning an end-user shouldn't
|
|
6
|
-
* need to access anything here.
|
|
7
|
-
*
|
|
8
|
-
* @module utils/data-structures
|
|
9
|
-
*/
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* Efficient Heap-based Implementation of a Priority Queue.
|
|
14
|
-
* It uses an array-based binary heap, where the root is at index `0`, and the
|
|
15
|
-
* children of node `i` are located at indices `2i + 1` and `2i + 2`, respectively.
|
|
16
|
-
*
|
|
17
|
-
* Adapted from the following sources:
|
|
18
|
-
* - https://stackoverflow.com/a/42919752/13989043 (original)
|
|
19
|
-
* - https://github.com/belladoreai/llama-tokenizer-js (minor improvements)
|
|
20
|
-
*/
|
|
21
|
-
export class PriorityQueue {
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
* Create a new PriorityQueue.
|
|
25
|
-
* @param {function(any, any): boolean} comparator Comparator function to determine priority. Defaults to a MaxHeap.
|
|
26
|
-
*/
|
|
27
|
-
constructor(comparator = (a, b) => a > b, maxSize = Infinity) {
|
|
28
|
-
this._heap = [];
|
|
29
|
-
this._comparator = comparator;
|
|
30
|
-
this._maxSize = maxSize;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
* The size of the queue
|
|
35
|
-
*/
|
|
36
|
-
get size() {
|
|
37
|
-
return this._heap.length;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
/**
|
|
41
|
-
* Check if the queue is empty.
|
|
42
|
-
* @returns {boolean} `true` if the queue is empty, `false` otherwise.
|
|
43
|
-
*/
|
|
44
|
-
isEmpty() {
|
|
45
|
-
return this.size === 0;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
/**
|
|
49
|
-
* Return the element with the highest priority in the queue.
|
|
50
|
-
* @returns {any} The highest priority element in the queue.
|
|
51
|
-
*/
|
|
52
|
-
peek() {
|
|
53
|
-
return this._heap[0];
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
/**
|
|
57
|
-
* Add one or more elements to the queue.
|
|
58
|
-
* @param {...any} values The values to push into the queue.
|
|
59
|
-
* @returns {number} The new size of the queue.
|
|
60
|
-
*/
|
|
61
|
-
push(...values) {
|
|
62
|
-
return this.extend(values);
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
/**
|
|
66
|
-
* Add multiple elements to the queue.
|
|
67
|
-
* @param {any[]} values The values to push into the queue.
|
|
68
|
-
* @returns {number} The new size of the queue.
|
|
69
|
-
*/
|
|
70
|
-
extend(values) {
|
|
71
|
-
for (const value of values) {
|
|
72
|
-
if (this.size < this._maxSize) {
|
|
73
|
-
this._heap.push(value);
|
|
74
|
-
this._siftUp();
|
|
75
|
-
} else {
|
|
76
|
-
// Get index of value with the lowest priority
|
|
77
|
-
const smallest = this._smallest();
|
|
78
|
-
|
|
79
|
-
// If the new value has higher priority than the smallest value in the heap
|
|
80
|
-
// then replace the smallest value with the new value and update the heap
|
|
81
|
-
if (this._comparator(value, this._heap[smallest])) {
|
|
82
|
-
this._heap[smallest] = value;
|
|
83
|
-
this._siftUpFrom(smallest);
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
return this.size;
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
/**
|
|
91
|
-
* Remove and return the element with the highest priority in the queue.
|
|
92
|
-
* @returns {any} The element with the highest priority in the queue.
|
|
93
|
-
*/
|
|
94
|
-
pop() {
|
|
95
|
-
const poppedValue = this.peek();
|
|
96
|
-
const bottom = this.size - 1;
|
|
97
|
-
if (bottom > 0) {
|
|
98
|
-
this._swap(0, bottom);
|
|
99
|
-
}
|
|
100
|
-
this._heap.pop();
|
|
101
|
-
this._siftDown();
|
|
102
|
-
return poppedValue;
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
/**
|
|
106
|
-
* Replace the element with the highest priority in the queue with a new value.
|
|
107
|
-
* @param {*} value The new value.
|
|
108
|
-
* @returns {*} The replaced value.
|
|
109
|
-
*/
|
|
110
|
-
replace(value) {
|
|
111
|
-
const replacedValue = this.peek();
|
|
112
|
-
this._heap[0] = value;
|
|
113
|
-
this._siftDown();
|
|
114
|
-
return replacedValue;
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
/**
|
|
118
|
-
* Compute the index for the parent of the node at index `i`.
|
|
119
|
-
* @param {number} i The index of the node to get the parent of.
|
|
120
|
-
* @returns {number} The index of the parent node.
|
|
121
|
-
* @private
|
|
122
|
-
*/
|
|
123
|
-
_parent(i) {
|
|
124
|
-
return ((i + 1) >>> 1) - 1;
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
/**
|
|
128
|
-
* Compute the index for the left child of the node at index `i`.
|
|
129
|
-
* @param {number} i The index of the node to get the left child of.
|
|
130
|
-
* @returns {number} The index of the left child.
|
|
131
|
-
* @private
|
|
132
|
-
*/
|
|
133
|
-
_left(i) {
|
|
134
|
-
return (i << 1) + 1;
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
/**
|
|
138
|
-
* Compute the index for the right child of the node at index `i`.
|
|
139
|
-
* @param {number} i The index of the node to get the right child of.
|
|
140
|
-
* @returns {number} The index of the right child.
|
|
141
|
-
* @private
|
|
142
|
-
*/
|
|
143
|
-
_right(i) {
|
|
144
|
-
return (i + 1) << 1;
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
/**
|
|
148
|
-
* Check if the element at index `i` is greater than the element at index `j`.
|
|
149
|
-
* @param {number} i The index of the first element to compare.
|
|
150
|
-
* @param {number} j The index of the second element to compare.
|
|
151
|
-
* @returns {boolean} `true` if the element at index `i` is greater than the element at index `j`, `false` otherwise.
|
|
152
|
-
* @private
|
|
153
|
-
*/
|
|
154
|
-
_greater(i, j) {
|
|
155
|
-
return this._comparator(this._heap[i], this._heap[j]);
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
/**
|
|
159
|
-
* Swap the elements at indices `i` and `j`.
|
|
160
|
-
* @param {number} i The index of the first element to swap.
|
|
161
|
-
* @param {number} j The index of the second element to swap.
|
|
162
|
-
* @private
|
|
163
|
-
*/
|
|
164
|
-
_swap(i, j) {
|
|
165
|
-
const temp = this._heap[i];
|
|
166
|
-
this._heap[i] = this._heap[j];
|
|
167
|
-
this._heap[j] = temp;
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
/**
|
|
171
|
-
* Maintain the heap property by updating positions in the heap,
|
|
172
|
-
* starting at the last element and moving up the heap.
|
|
173
|
-
* @private
|
|
174
|
-
*/
|
|
175
|
-
_siftUp() {
|
|
176
|
-
this._siftUpFrom(this.size - 1);
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
/**
|
|
180
|
-
* Helper function to sift up from a given node.
|
|
181
|
-
* @param {number} node The index of the node to start sifting up from.
|
|
182
|
-
*/
|
|
183
|
-
_siftUpFrom(node) {
|
|
184
|
-
while (node > 0 && this._greater(node, this._parent(node))) {
|
|
185
|
-
this._swap(node, this._parent(node));
|
|
186
|
-
node = this._parent(node);
|
|
187
|
-
}
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
/**
|
|
191
|
-
* Maintain the heap property by updating positions in the heap,
|
|
192
|
-
* starting at the first element and moving down the heap.
|
|
193
|
-
* @private
|
|
194
|
-
*/
|
|
195
|
-
_siftDown() {
|
|
196
|
-
let node = 0;
|
|
197
|
-
while (
|
|
198
|
-
(this._left(node) < this.size && this._greater(this._left(node), node)) ||
|
|
199
|
-
(this._right(node) < this.size && this._greater(this._right(node), node))
|
|
200
|
-
) {
|
|
201
|
-
const maxChild = (this._right(node) < this.size && this._greater(this._right(node), this._left(node)))
|
|
202
|
-
? this._right(node)
|
|
203
|
-
: this._left(node);
|
|
204
|
-
this._swap(node, maxChild);
|
|
205
|
-
node = maxChild;
|
|
206
|
-
}
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
/**
|
|
210
|
-
* Get the index of the smallest element in the heap. Since we use an array-based heap,
|
|
211
|
-
* the index can be computed without needing to traverse the heap.
|
|
212
|
-
* @private
|
|
213
|
-
*/
|
|
214
|
-
_smallest() {
|
|
215
|
-
return (2 ** (Math.floor(Math.log2(this.size))) - 1);
|
|
216
|
-
}
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
/**
|
|
220
|
-
* A trie structure to efficiently store and search for strings.
|
|
221
|
-
*/
|
|
222
|
-
export class CharTrie {
|
|
223
|
-
constructor() {
|
|
224
|
-
this.root = CharTrieNode.default();
|
|
225
|
-
}
|
|
226
|
-
|
|
227
|
-
/**
|
|
228
|
-
* Adds one or more `texts` to the trie.
|
|
229
|
-
* @param {string[]} texts The strings to add to the trie.
|
|
230
|
-
*/
|
|
231
|
-
extend(texts) {
|
|
232
|
-
for (const text of texts) {
|
|
233
|
-
this.push(text);
|
|
234
|
-
}
|
|
235
|
-
}
|
|
236
|
-
|
|
237
|
-
/**
|
|
238
|
-
* Adds text to the trie.
|
|
239
|
-
* @param {string} text The string to add to the trie.
|
|
240
|
-
*/
|
|
241
|
-
push(text) {
|
|
242
|
-
let node = this.root;
|
|
243
|
-
for (const ch of text) {
|
|
244
|
-
let child = node.children.get(ch);
|
|
245
|
-
if (child === undefined) {
|
|
246
|
-
child = CharTrieNode.default();
|
|
247
|
-
node.children.set(ch, child);
|
|
248
|
-
}
|
|
249
|
-
node = child;
|
|
250
|
-
}
|
|
251
|
-
node.isLeaf = true;
|
|
252
|
-
}
|
|
253
|
-
|
|
254
|
-
/**
|
|
255
|
-
* Searches the trie for all strings with a common prefix of `text`.
|
|
256
|
-
* @param {string} text The common prefix to search for.
|
|
257
|
-
* @yields {string} Each string in the trie that has `text` as a prefix.
|
|
258
|
-
*/
|
|
259
|
-
*commonPrefixSearch(text) {
|
|
260
|
-
let node = this.root;
|
|
261
|
-
if (node === undefined) return;
|
|
262
|
-
|
|
263
|
-
let prefix = "";
|
|
264
|
-
for (const ch of text) {
|
|
265
|
-
prefix += ch;
|
|
266
|
-
node = node.children.get(ch);
|
|
267
|
-
if (node === undefined) return;
|
|
268
|
-
if (node.isLeaf) {
|
|
269
|
-
yield prefix;
|
|
270
|
-
}
|
|
271
|
-
}
|
|
272
|
-
}
|
|
273
|
-
}
|
|
274
|
-
|
|
275
|
-
/**
|
|
276
|
-
* Represents a node in a character trie.
|
|
277
|
-
*/
|
|
278
|
-
class CharTrieNode {
|
|
279
|
-
/**
|
|
280
|
-
* Create a new CharTrieNode.
|
|
281
|
-
* @param {boolean} isLeaf Whether the node is a leaf node or not.
|
|
282
|
-
* @param {Map<string, CharTrieNode>} children A map containing the node's children, where the key is a character and the value is a `CharTrieNode`.
|
|
283
|
-
*/
|
|
284
|
-
constructor(isLeaf, children) {
|
|
285
|
-
this.isLeaf = isLeaf;
|
|
286
|
-
this.children = children;
|
|
287
|
-
}
|
|
288
|
-
|
|
289
|
-
/**
|
|
290
|
-
* Returns a new `CharTrieNode` instance with default values.
|
|
291
|
-
* @returns {CharTrieNode} A new `CharTrieNode` instance with `isLeaf` set to `false` and an empty `children` map.
|
|
292
|
-
*/
|
|
293
|
-
static default() {
|
|
294
|
-
return new CharTrieNode(false, new Map());
|
|
295
|
-
}
|
|
296
|
-
}
|
|
297
|
-
|
|
298
|
-
/**
|
|
299
|
-
* A lattice data structure to be used for tokenization.
|
|
300
|
-
*/
|
|
301
|
-
export class TokenLattice {
|
|
302
|
-
/**
|
|
303
|
-
* Creates a new TokenLattice instance.
|
|
304
|
-
*
|
|
305
|
-
* @param {string} sentence The input sentence to be tokenized.
|
|
306
|
-
* @param {number} bosTokenId The beginning-of-sequence token ID.
|
|
307
|
-
* @param {number} eosTokenId The end-of-sequence token ID.
|
|
308
|
-
*/
|
|
309
|
-
constructor(sentence, bosTokenId, eosTokenId) {
|
|
310
|
-
this.chars = Array.from(sentence);
|
|
311
|
-
this.len = this.chars.length;
|
|
312
|
-
this.bosTokenId = bosTokenId;
|
|
313
|
-
this.eosTokenId = eosTokenId;
|
|
314
|
-
this.nodes = [];
|
|
315
|
-
this.beginNodes = Array.from({ length: this.len + 1 }, () => []);
|
|
316
|
-
this.endNodes = Array.from({ length: this.len + 1 }, () => []);
|
|
317
|
-
|
|
318
|
-
const bos = new TokenLatticeNode(this.bosTokenId, 0, 0, 0, 0.0);
|
|
319
|
-
const eos = new TokenLatticeNode(this.eosTokenId, 1, this.len, 0, 0.0);
|
|
320
|
-
this.nodes.push(bos.clone());
|
|
321
|
-
this.nodes.push(eos.clone());
|
|
322
|
-
this.beginNodes[this.len].push(eos);
|
|
323
|
-
this.endNodes[0].push(bos);
|
|
324
|
-
}
|
|
325
|
-
|
|
326
|
-
/**
|
|
327
|
-
* Inserts a new token node into the token lattice.
|
|
328
|
-
*
|
|
329
|
-
* @param {number} pos The starting position of the token.
|
|
330
|
-
* @param {number} length The length of the token.
|
|
331
|
-
* @param {number} score The score of the token.
|
|
332
|
-
* @param {number} tokenId The token ID of the token.
|
|
333
|
-
*/
|
|
334
|
-
insert(pos, length, score, tokenId) {
|
|
335
|
-
const nodeId = this.nodes.length;
|
|
336
|
-
const node = new TokenLatticeNode(tokenId, nodeId, pos, length, score);
|
|
337
|
-
this.beginNodes[pos].push(node);
|
|
338
|
-
this.endNodes[pos + length].push(node);
|
|
339
|
-
this.nodes.push(node);
|
|
340
|
-
}
|
|
341
|
-
|
|
342
|
-
/**
|
|
343
|
-
* Implements the Viterbi algorithm to compute the most likely sequence of tokens.
|
|
344
|
-
*
|
|
345
|
-
* @returns {TokenLatticeNode[]} The most likely sequence of tokens.
|
|
346
|
-
*/
|
|
347
|
-
viterbi() {
|
|
348
|
-
const len = this.len;
|
|
349
|
-
let pos = 0;
|
|
350
|
-
while (pos <= len) {
|
|
351
|
-
if (this.beginNodes[pos].length == 0) {
|
|
352
|
-
return [];
|
|
353
|
-
}
|
|
354
|
-
for (let rnode of this.beginNodes[pos]) {
|
|
355
|
-
rnode.prev = null;
|
|
356
|
-
let bestScore = 0.0;
|
|
357
|
-
let bestNode = null;
|
|
358
|
-
for (let lnode of this.endNodes[pos]) {
|
|
359
|
-
const score = lnode.backtraceScore + rnode.score;
|
|
360
|
-
if (bestNode === null || score > bestScore) {
|
|
361
|
-
bestNode = lnode.clone();
|
|
362
|
-
bestScore = score;
|
|
363
|
-
}
|
|
364
|
-
}
|
|
365
|
-
|
|
366
|
-
if (bestNode !== null) {
|
|
367
|
-
rnode.prev = bestNode;
|
|
368
|
-
rnode.backtraceScore = bestScore;
|
|
369
|
-
} else {
|
|
370
|
-
return [];
|
|
371
|
-
}
|
|
372
|
-
}
|
|
373
|
-
++pos;
|
|
374
|
-
}
|
|
375
|
-
|
|
376
|
-
const results = [];
|
|
377
|
-
const root = this.beginNodes[len][0];
|
|
378
|
-
const prev = root.prev;
|
|
379
|
-
if (prev === null) {
|
|
380
|
-
return [];
|
|
381
|
-
}
|
|
382
|
-
|
|
383
|
-
let node = prev.clone();
|
|
384
|
-
while (node.prev !== null) {
|
|
385
|
-
results.push(node.clone());
|
|
386
|
-
const n = node.clone();
|
|
387
|
-
node = n.prev.clone();
|
|
388
|
-
}
|
|
389
|
-
|
|
390
|
-
results.reverse();
|
|
391
|
-
return results;
|
|
392
|
-
}
|
|
393
|
-
|
|
394
|
-
/**
|
|
395
|
-
* @param {TokenLatticeNode} node
|
|
396
|
-
* @returns {string} The array of nodes representing the most likely sequence of tokens.
|
|
397
|
-
*/
|
|
398
|
-
piece(node) {
|
|
399
|
-
return this.chars.slice(node.pos, node.pos + node.length).join('');
|
|
400
|
-
}
|
|
401
|
-
|
|
402
|
-
/**
|
|
403
|
-
* @returns {string[]} The most likely sequence of tokens.
|
|
404
|
-
*/
|
|
405
|
-
tokens() {
|
|
406
|
-
const nodes = this.viterbi();
|
|
407
|
-
return nodes.map(x => this.piece(x));
|
|
408
|
-
}
|
|
409
|
-
|
|
410
|
-
/**
|
|
411
|
-
* @returns {number[]} The most likely sequence of token ids.
|
|
412
|
-
*/
|
|
413
|
-
tokenIds() {
|
|
414
|
-
const nodes = this.viterbi();
|
|
415
|
-
return nodes.map(x => x.tokenId);
|
|
416
|
-
}
|
|
417
|
-
}
|
|
418
|
-
class TokenLatticeNode {
|
|
419
|
-
/**
|
|
420
|
-
* Represents a node in a token lattice for a given sentence.
|
|
421
|
-
* @param {number} tokenId The ID of the token associated with this node.
|
|
422
|
-
* @param {number} nodeId The ID of this node.
|
|
423
|
-
* @param {number} pos The starting position of the token in the sentence.
|
|
424
|
-
* @param {number} length The length of the token.
|
|
425
|
-
* @param {number} score The score associated with the token.
|
|
426
|
-
*/
|
|
427
|
-
constructor(tokenId, nodeId, pos, length, score) {
|
|
428
|
-
this.tokenId = tokenId;
|
|
429
|
-
this.nodeId = nodeId;
|
|
430
|
-
this.pos = pos;
|
|
431
|
-
this.length = length;
|
|
432
|
-
this.score = score;
|
|
433
|
-
this.prev = null;
|
|
434
|
-
this.backtraceScore = 0.0;
|
|
435
|
-
}
|
|
436
|
-
|
|
437
|
-
/**
|
|
438
|
-
* Returns a clone of this node.
|
|
439
|
-
* @returns {TokenLatticeNode} A clone of this node.
|
|
440
|
-
*/
|
|
441
|
-
clone() {
|
|
442
|
-
const n = new TokenLatticeNode(this.tokenId, this.nodeId, this.pos, this.length, this.score);
|
|
443
|
-
n.prev = this.prev;
|
|
444
|
-
n.backtraceScore = this.backtraceScore;
|
|
445
|
-
return n;
|
|
446
|
-
}
|
|
447
|
-
}
|
|
448
|
-
|
|
449
|
-
/**
|
|
450
|
-
* A data structure which uses a trie to split a string into tokens based on a dictionary.
|
|
451
|
-
* It can also use a regular expression to preprocess the input text before splitting.
|
|
452
|
-
*
|
|
453
|
-
* NOTE: To ensure multi-byte characters are handled correctly, we operate at byte-level instead of character-level.
|
|
454
|
-
*/
|
|
455
|
-
export class DictionarySplitter {
|
|
456
|
-
/**
|
|
457
|
-
* @param {string[]} dictionary The dictionary of words to use for splitting.
|
|
458
|
-
*/
|
|
459
|
-
constructor(dictionary) {
|
|
460
|
-
this.trie = this._buildTrie(dictionary);
|
|
461
|
-
}
|
|
462
|
-
|
|
463
|
-
/**
|
|
464
|
-
* Builds a trie from the given dictionary.
|
|
465
|
-
* @param {string[]} dictionary The dictionary of words to build the trie from.
|
|
466
|
-
* @returns {Object} The root node of the trie.
|
|
467
|
-
* @private
|
|
468
|
-
*/
|
|
469
|
-
_buildTrie(dictionary) {
|
|
470
|
-
const trie = Object.create(null);
|
|
471
|
-
for (const word of dictionary) {
|
|
472
|
-
let node = trie;
|
|
473
|
-
for (let i = 0; i < word.length; ++i) {
|
|
474
|
-
node = (node[word[i]] ??= Object.create(null));
|
|
475
|
-
}
|
|
476
|
-
node.end = word;
|
|
477
|
-
}
|
|
478
|
-
return trie;
|
|
479
|
-
}
|
|
480
|
-
|
|
481
|
-
/**
|
|
482
|
-
* Splits the input text into tokens based on the dictionary.
|
|
483
|
-
* @param {string} text The input text to split.
|
|
484
|
-
* @returns {string[]} An array of tokens.
|
|
485
|
-
*/
|
|
486
|
-
split(text) {
|
|
487
|
-
const result = [];
|
|
488
|
-
const n = text.length;
|
|
489
|
-
let start = 0;
|
|
490
|
-
let i = 0;
|
|
491
|
-
|
|
492
|
-
while (i < n) {
|
|
493
|
-
let node = this.trie;
|
|
494
|
-
let match = null;
|
|
495
|
-
let j = i;
|
|
496
|
-
|
|
497
|
-
while (j < n && (node = node[text[j]])) {
|
|
498
|
-
if (node.end) {
|
|
499
|
-
// Always keep the last (i.e., longest) match.
|
|
500
|
-
match = node.end;
|
|
501
|
-
}
|
|
502
|
-
++j;
|
|
503
|
-
}
|
|
504
|
-
|
|
505
|
-
if (match) {
|
|
506
|
-
if (i > start) {
|
|
507
|
-
result.push(text.slice(start, i));
|
|
508
|
-
}
|
|
509
|
-
result.push(match);
|
|
510
|
-
i += match.length;
|
|
511
|
-
start = i;
|
|
512
|
-
} else {
|
|
513
|
-
++i;
|
|
514
|
-
}
|
|
515
|
-
}
|
|
516
|
-
if (start < n) {
|
|
517
|
-
result.push(text.slice(start));
|
|
518
|
-
}
|
|
519
|
-
return result;
|
|
520
|
-
}
|
|
521
|
-
}
|
|
522
|
-
|
|
523
|
-
/**
|
|
524
|
-
* A simple Least Recently Used (LRU) cache implementation in JavaScript.
|
|
525
|
-
* This cache stores key-value pairs and evicts the least recently used item
|
|
526
|
-
* when the capacity is exceeded.
|
|
527
|
-
*/
|
|
528
|
-
export class LRUCache {
|
|
529
|
-
/**
|
|
530
|
-
* Creates an LRUCache instance.
|
|
531
|
-
* @param {number} capacity The maximum number of items the cache can hold.
|
|
532
|
-
*/
|
|
533
|
-
constructor(capacity) {
|
|
534
|
-
this.capacity = capacity;
|
|
535
|
-
this.cache = new Map();
|
|
536
|
-
}
|
|
537
|
-
|
|
538
|
-
/**
|
|
539
|
-
* Retrieves the value associated with the given key and marks the key as recently used.
|
|
540
|
-
* @param {any} key The key to retrieve.
|
|
541
|
-
* @returns {any} The value associated with the key, or undefined if the key does not exist.
|
|
542
|
-
*/
|
|
543
|
-
get(key) {
|
|
544
|
-
if (!this.cache.has(key)) return undefined;
|
|
545
|
-
const value = this.cache.get(key);
|
|
546
|
-
this.cache.delete(key);
|
|
547
|
-
this.cache.set(key, value);
|
|
548
|
-
return value;
|
|
549
|
-
}
|
|
550
|
-
|
|
551
|
-
/**
|
|
552
|
-
* Inserts or updates the key-value pair in the cache.
|
|
553
|
-
* If the key already exists, it is updated and marked as recently used.
|
|
554
|
-
* If the cache exceeds its capacity, the least recently used item is evicted.
|
|
555
|
-
* @param {any} key The key to add or update.
|
|
556
|
-
* @param {any} value The value to associate with the key.
|
|
557
|
-
*/
|
|
558
|
-
put(key, value) {
|
|
559
|
-
if (this.cache.has(key)) {
|
|
560
|
-
this.cache.delete(key);
|
|
561
|
-
}
|
|
562
|
-
this.cache.set(key, value);
|
|
563
|
-
if (this.cache.size > this.capacity) {
|
|
564
|
-
this.cache.delete(this.cache.keys().next().value);
|
|
565
|
-
}
|
|
566
|
-
}
|
|
567
|
-
|
|
568
|
-
/**
|
|
569
|
-
* Clears the cache.
|
|
570
|
-
*/
|
|
571
|
-
clear() {
|
|
572
|
-
this.cache.clear();
|
|
573
|
-
}
|
|
574
|
-
}
|
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
/**
|
|
3
|
-
* The list of devices supported by Transformers.js
|
|
4
|
-
*/
|
|
5
|
-
export const DEVICE_TYPES = Object.freeze({
|
|
6
|
-
auto: 'auto', // Auto-detect based on device and environment
|
|
7
|
-
gpu: 'gpu', // Auto-detect GPU
|
|
8
|
-
cpu: 'cpu', // CPU
|
|
9
|
-
wasm: 'wasm', // WebAssembly
|
|
10
|
-
webgpu: 'webgpu', // WebGPU
|
|
11
|
-
cuda: 'cuda', // CUDA
|
|
12
|
-
dml: 'dml', // DirectML
|
|
13
|
-
|
|
14
|
-
webnn: 'webnn', // WebNN (default)
|
|
15
|
-
'webnn-npu': 'webnn-npu', // WebNN NPU
|
|
16
|
-
'webnn-gpu': 'webnn-gpu', // WebNN GPU
|
|
17
|
-
'webnn-cpu': 'webnn-cpu', // WebNN CPU
|
|
18
|
-
});
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* @typedef {keyof typeof DEVICE_TYPES} DeviceType
|
|
22
|
-
*/
|
|
@@ -1,63 +0,0 @@
|
|
|
1
|
-
/// <reference types="@webgpu/types" />
|
|
2
|
-
|
|
3
|
-
import { apis } from "../env.js";
|
|
4
|
-
|
|
5
|
-
import { DEVICE_TYPES } from "./devices.js";
|
|
6
|
-
|
|
7
|
-
// TODO: Use the adapter from `env.backends.onnx.webgpu.adapter` to check for `shader-f16` support,
|
|
8
|
-
// when available in https://github.com/microsoft/onnxruntime/pull/19940.
|
|
9
|
-
// For more information, see https://github.com/microsoft/onnxruntime/pull/19857#issuecomment-1999984753
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* Checks if WebGPU fp16 support is available in the current environment.
|
|
13
|
-
*/
|
|
14
|
-
export const isWebGpuFp16Supported = (function () {
|
|
15
|
-
/** @type {boolean} */
|
|
16
|
-
let cachedResult;
|
|
17
|
-
|
|
18
|
-
return async function () {
|
|
19
|
-
if (cachedResult === undefined) {
|
|
20
|
-
if (!apis.IS_WEBGPU_AVAILABLE) {
|
|
21
|
-
cachedResult = false;
|
|
22
|
-
} else {
|
|
23
|
-
try {
|
|
24
|
-
const adapter = await navigator.gpu.requestAdapter();
|
|
25
|
-
cachedResult = adapter.features.has('shader-f16');
|
|
26
|
-
} catch (e) {
|
|
27
|
-
cachedResult = false;
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
return cachedResult;
|
|
32
|
-
};
|
|
33
|
-
})();
|
|
34
|
-
|
|
35
|
-
export const DATA_TYPES = Object.freeze({
|
|
36
|
-
auto: 'auto', // Auto-detect based on environment
|
|
37
|
-
fp32: 'fp32',
|
|
38
|
-
fp16: 'fp16',
|
|
39
|
-
q8: 'q8',
|
|
40
|
-
int8: 'int8',
|
|
41
|
-
uint8: 'uint8',
|
|
42
|
-
q4: 'q4',
|
|
43
|
-
bnb4: 'bnb4',
|
|
44
|
-
q4f16: 'q4f16', // fp16 model with int4 block weight quantization
|
|
45
|
-
});
|
|
46
|
-
/** @typedef {keyof typeof DATA_TYPES} DataType */
|
|
47
|
-
|
|
48
|
-
export const DEFAULT_DEVICE_DTYPE_MAPPING = Object.freeze({
|
|
49
|
-
// NOTE: If not specified, will default to fp32
|
|
50
|
-
[DEVICE_TYPES.wasm]: DATA_TYPES.q8,
|
|
51
|
-
});
|
|
52
|
-
|
|
53
|
-
/** @type {Record<Exclude<DataType, "auto">, string>} */
|
|
54
|
-
export const DEFAULT_DTYPE_SUFFIX_MAPPING = Object.freeze({
|
|
55
|
-
[DATA_TYPES.fp32]: '',
|
|
56
|
-
[DATA_TYPES.fp16]: '_fp16',
|
|
57
|
-
[DATA_TYPES.int8]: '_int8',
|
|
58
|
-
[DATA_TYPES.uint8]: '_uint8',
|
|
59
|
-
[DATA_TYPES.q8]: '_quantized',
|
|
60
|
-
[DATA_TYPES.q4]: '_q4',
|
|
61
|
-
[DATA_TYPES.q4f16]: '_q4f16',
|
|
62
|
-
[DATA_TYPES.bnb4]: '_bnb4',
|
|
63
|
-
});
|
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
/**
|
|
3
|
-
* A base class for creating callable objects.
|
|
4
|
-
* See [here](https://stackoverflow.com/q/76073890) for more information.
|
|
5
|
-
*
|
|
6
|
-
* @type {new () => {(...args: any[]): any, _call(...args: any[]): any}}
|
|
7
|
-
*/
|
|
8
|
-
export const Callable = /** @type {any} */ (class {
|
|
9
|
-
/**
|
|
10
|
-
* Creates a new instance of the Callable class.
|
|
11
|
-
*/
|
|
12
|
-
constructor() {
|
|
13
|
-
/**
|
|
14
|
-
* Creates a closure that delegates to a private method '_call' with the given arguments.
|
|
15
|
-
* @type {any}
|
|
16
|
-
* @param {...any} args Zero or more arguments to pass to the '_call' method.
|
|
17
|
-
* @returns {*} The result of calling the '_call' method.
|
|
18
|
-
*/
|
|
19
|
-
let closure = function (...args) {
|
|
20
|
-
return closure._call(...args)
|
|
21
|
-
}
|
|
22
|
-
return Object.setPrototypeOf(closure, new.target.prototype)
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* This method should be implemented in subclasses to provide the
|
|
27
|
-
* functionality of the callable object.
|
|
28
|
-
*
|
|
29
|
-
* @param {any[]} args
|
|
30
|
-
* @throws {Error} If the subclass does not implement the `_call` method.
|
|
31
|
-
*/
|
|
32
|
-
_call(...args) {
|
|
33
|
-
throw Error('Must implement _call method in subclass')
|
|
34
|
-
}
|
|
35
|
-
});
|