@huggingface/transformers 3.0.0-alpha.0

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.
Files changed (96) hide show
  1. package/LICENSE +202 -0
  2. package/README.md +376 -0
  3. package/dist/ort-wasm-simd-threaded.jsep.wasm +0 -0
  4. package/dist/transformers.cjs +30741 -0
  5. package/dist/transformers.cjs.map +1 -0
  6. package/dist/transformers.js +33858 -0
  7. package/dist/transformers.js.map +1 -0
  8. package/dist/transformers.min.cjs +173 -0
  9. package/dist/transformers.min.cjs.map +1 -0
  10. package/dist/transformers.min.js +231 -0
  11. package/dist/transformers.min.js.map +1 -0
  12. package/package.json +92 -0
  13. package/src/backends/onnx.js +151 -0
  14. package/src/configs.js +360 -0
  15. package/src/env.js +152 -0
  16. package/src/generation/configuration_utils.js +381 -0
  17. package/src/generation/logits_process.js +716 -0
  18. package/src/generation/logits_sampler.js +204 -0
  19. package/src/generation/parameters.js +35 -0
  20. package/src/generation/stopping_criteria.js +156 -0
  21. package/src/generation/streamers.js +212 -0
  22. package/src/models/whisper/common_whisper.js +151 -0
  23. package/src/models/whisper/generation_whisper.js +89 -0
  24. package/src/models.js +7028 -0
  25. package/src/ops/registry.js +92 -0
  26. package/src/pipelines.js +3341 -0
  27. package/src/processors.js +2614 -0
  28. package/src/tokenizers.js +4395 -0
  29. package/src/transformers.js +28 -0
  30. package/src/utils/audio.js +704 -0
  31. package/src/utils/constants.js +2 -0
  32. package/src/utils/core.js +149 -0
  33. package/src/utils/data-structures.js +445 -0
  34. package/src/utils/devices.js +11 -0
  35. package/src/utils/dtypes.js +62 -0
  36. package/src/utils/generic.js +35 -0
  37. package/src/utils/hub.js +671 -0
  38. package/src/utils/image.js +745 -0
  39. package/src/utils/maths.js +1050 -0
  40. package/src/utils/tensor.js +1378 -0
  41. package/types/backends/onnx.d.ts +26 -0
  42. package/types/backends/onnx.d.ts.map +1 -0
  43. package/types/configs.d.ts +59 -0
  44. package/types/configs.d.ts.map +1 -0
  45. package/types/env.d.ts +106 -0
  46. package/types/env.d.ts.map +1 -0
  47. package/types/generation/configuration_utils.d.ts +320 -0
  48. package/types/generation/configuration_utils.d.ts.map +1 -0
  49. package/types/generation/logits_process.d.ts +354 -0
  50. package/types/generation/logits_process.d.ts.map +1 -0
  51. package/types/generation/logits_sampler.d.ts +51 -0
  52. package/types/generation/logits_sampler.d.ts.map +1 -0
  53. package/types/generation/parameters.d.ts +47 -0
  54. package/types/generation/parameters.d.ts.map +1 -0
  55. package/types/generation/stopping_criteria.d.ts +81 -0
  56. package/types/generation/stopping_criteria.d.ts.map +1 -0
  57. package/types/generation/streamers.d.ts +81 -0
  58. package/types/generation/streamers.d.ts.map +1 -0
  59. package/types/models/whisper/common_whisper.d.ts +8 -0
  60. package/types/models/whisper/common_whisper.d.ts.map +1 -0
  61. package/types/models/whisper/generation_whisper.d.ts +76 -0
  62. package/types/models/whisper/generation_whisper.d.ts.map +1 -0
  63. package/types/models.d.ts +3845 -0
  64. package/types/models.d.ts.map +1 -0
  65. package/types/ops/registry.d.ts +11 -0
  66. package/types/ops/registry.d.ts.map +1 -0
  67. package/types/pipelines.d.ts +2403 -0
  68. package/types/pipelines.d.ts.map +1 -0
  69. package/types/processors.d.ts +917 -0
  70. package/types/processors.d.ts.map +1 -0
  71. package/types/tokenizers.d.ts +999 -0
  72. package/types/tokenizers.d.ts.map +1 -0
  73. package/types/transformers.d.ts +13 -0
  74. package/types/transformers.d.ts.map +1 -0
  75. package/types/utils/audio.d.ts +130 -0
  76. package/types/utils/audio.d.ts.map +1 -0
  77. package/types/utils/constants.d.ts +2 -0
  78. package/types/utils/constants.d.ts.map +1 -0
  79. package/types/utils/core.d.ts +91 -0
  80. package/types/utils/core.d.ts.map +1 -0
  81. package/types/utils/data-structures.d.ts +236 -0
  82. package/types/utils/data-structures.d.ts.map +1 -0
  83. package/types/utils/devices.d.ts +8 -0
  84. package/types/utils/devices.d.ts.map +1 -0
  85. package/types/utils/dtypes.d.ts +22 -0
  86. package/types/utils/dtypes.d.ts.map +1 -0
  87. package/types/utils/generic.d.ts +11 -0
  88. package/types/utils/generic.d.ts.map +1 -0
  89. package/types/utils/hub.d.ts +191 -0
  90. package/types/utils/hub.d.ts.map +1 -0
  91. package/types/utils/image.d.ts +119 -0
  92. package/types/utils/image.d.ts.map +1 -0
  93. package/types/utils/maths.d.ts +280 -0
  94. package/types/utils/maths.d.ts.map +1 -0
  95. package/types/utils/tensor.d.ts +392 -0
  96. package/types/utils/tensor.d.ts.map +1 -0
@@ -0,0 +1,191 @@
1
+ /**
2
+ * Helper function to get a file, using either the Fetch API or FileSystem API.
3
+ *
4
+ * @param {URL|string} urlOrPath The URL/path of the file to get.
5
+ * @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).
6
+ */
7
+ export function getFile(urlOrPath: URL | string): Promise<FileResponse | Response>;
8
+ /**
9
+ *
10
+ * Retrieves a file from either a remote URL using the Fetch API or from the local file system using the FileSystem API.
11
+ * If the filesystem is available and `env.useCache = true`, the file will be downloaded and cached.
12
+ *
13
+ * @param {string} path_or_repo_id This can be either:
14
+ * - a string, the *model id* of a model repo on huggingface.co.
15
+ * - a path to a *directory* potentially containing the file.
16
+ * @param {string} filename The name of the file to locate in `path_or_repo`.
17
+ * @param {boolean} [fatal=true] Whether to throw an error if the file is not found.
18
+ * @param {PretrainedOptions} [options] An object containing optional parameters.
19
+ *
20
+ * @throws Will throw an error if the file is not found and `fatal` is true.
21
+ * @returns {Promise<Uint8Array>} A Promise that resolves with the file content as a buffer.
22
+ */
23
+ export function getModelFile(path_or_repo_id: string, filename: string, fatal?: boolean, options?: PretrainedOptions): Promise<Uint8Array>;
24
+ /**
25
+ * Fetches a JSON file from a given path and file name.
26
+ *
27
+ * @param {string} modelPath The path to the directory containing the file.
28
+ * @param {string} fileName The name of the file to fetch.
29
+ * @param {boolean} [fatal=true] Whether to throw an error if the file is not found.
30
+ * @param {PretrainedOptions} [options] An object containing optional parameters.
31
+ * @returns {Promise<Object>} The JSON data parsed into a JavaScript object.
32
+ * @throws Will throw an error if the file is not found and `fatal` is true.
33
+ */
34
+ export function getModelJSON(modelPath: string, fileName: string, fatal?: boolean, options?: PretrainedOptions): Promise<any>;
35
+ /**
36
+ * Options for loading a pretrained model.
37
+ */
38
+ export type PretrainedOptions = {
39
+ /**
40
+ * If specified, this function will be called during model construction, to provide the user with progress updates.
41
+ */
42
+ progress_callback?: Function;
43
+ /**
44
+ * Configuration for the model to use instead of an automatically loaded configuration. Configuration can be automatically loaded when:
45
+ * - The model is a model provided by the library (loaded with the *model id* string of a pretrained model).
46
+ * - 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.
47
+ */
48
+ config?: any;
49
+ /**
50
+ * Path to a directory in which a downloaded pretrained model configuration should be cached if the standard cache should not be used.
51
+ */
52
+ cache_dir?: string;
53
+ /**
54
+ * Whether or not to only look at local files (e.g., not try downloading the model).
55
+ */
56
+ local_files_only?: boolean;
57
+ /**
58
+ * The specific model version to use. It can be a branch name, a tag name, or a commit id,
59
+ * 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.
60
+ * NOTE: This setting is ignored for local requests.
61
+ */
62
+ revision?: string;
63
+ };
64
+ /**
65
+ * Options for loading a pretrained model.
66
+ */
67
+ export type ModelSpecificPretrainedOptions = {
68
+ /**
69
+ * In case the relevant files are located inside a subfolder of the model repo on huggingface.co,
70
+ * you can specify the folder name here.
71
+ */
72
+ subfolder?: string;
73
+ /**
74
+ * If specified, load the model with this name (excluding the .onnx suffix). Currently only valid for encoder- or decoder-only models.
75
+ */
76
+ model_file_name?: string;
77
+ /**
78
+ * The device to run the model on. If not specified, the device will be chosen from the environment settings.
79
+ */
80
+ device?: import("./devices.js").DeviceType | Record<string, import("./devices.js").DeviceType>;
81
+ /**
82
+ * The data type to use for the model. If not specified, the data type will be chosen from the environment settings.
83
+ */
84
+ dtype?: import("./dtypes.js").DataType | Record<string, import("./dtypes.js").DataType>;
85
+ /**
86
+ * Whether to load the model using the external data format (used for models >= 2GB in size).
87
+ */
88
+ use_external_data_format?: boolean | Record<string, boolean>;
89
+ /**
90
+ * (Optional) User-specified session options passed to the runtime. If not provided, suitable defaults will be chosen.
91
+ */
92
+ session_options?: any;
93
+ };
94
+ /**
95
+ * Options for loading a pretrained model.
96
+ */
97
+ export type PretrainedModelOptions = PretrainedOptions & ModelSpecificPretrainedOptions;
98
+ /**
99
+ * @typedef {Object} PretrainedOptions Options for loading a pretrained model.
100
+ * @property {function} [progress_callback=null] If specified, this function will be called during model construction, to provide the user with progress updates.
101
+ * @property {Object} [config=null] Configuration for the model to use instead of an automatically loaded configuration. Configuration can be automatically loaded when:
102
+ * - The model is a model provided by the library (loaded with the *model id* string of a pretrained model).
103
+ * - 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.
104
+ * @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.
105
+ * @property {boolean} [local_files_only=false] Whether or not to only look at local files (e.g., not try downloading the model).
106
+ * @property {string} [revision='main'] The specific model version to use. It can be a branch name, a tag name, or a commit id,
107
+ * 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.
108
+ * NOTE: This setting is ignored for local requests.
109
+ */
110
+ /**
111
+ * @typedef {Object} ModelSpecificPretrainedOptions Options for loading a pretrained model.
112
+ * @property {string} [subfolder='onnx'] In case the relevant files are located inside a subfolder of the model repo on huggingface.co,
113
+ * you can specify the folder name here.
114
+ * @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.
115
+ * @property {import("./devices.js").DeviceType|Record<string, import("./devices.js").DeviceType>} [device=null] The device to run the model on. If not specified, the device will be chosen from the environment settings.
116
+ * @property {import("./dtypes.js").DataType|Record<string, import("./dtypes.js").DataType>} [dtype=null] The data type to use for the model. If not specified, the data type will be chosen from the environment settings.
117
+ * @property {boolean|Record<string, boolean>} [use_external_data_format=false] Whether to load the model using the external data format (used for models >= 2GB in size).
118
+ * @property {Object} [session_options] (Optional) User-specified session options passed to the runtime. If not provided, suitable defaults will be chosen.
119
+ */
120
+ /**
121
+ * @typedef {PretrainedOptions & ModelSpecificPretrainedOptions} PretrainedModelOptions Options for loading a pretrained model.
122
+ */
123
+ declare class FileResponse {
124
+ /**
125
+ * Creates a new `FileResponse` object.
126
+ * @param {string|URL} filePath
127
+ */
128
+ constructor(filePath: string | URL);
129
+ /**
130
+ * Mapping from file extensions to MIME types.
131
+ */
132
+ _CONTENT_TYPE_MAP: {
133
+ txt: string;
134
+ html: string;
135
+ css: string;
136
+ js: string;
137
+ json: string;
138
+ png: string;
139
+ jpg: string;
140
+ jpeg: string;
141
+ gif: string;
142
+ };
143
+ filePath: string | URL;
144
+ headers: Headers;
145
+ exists: any;
146
+ status: number;
147
+ statusText: string;
148
+ body: ReadableStream<any>;
149
+ /**
150
+ * Updates the 'content-type' header property of the response based on the extension of
151
+ * the file specified by the filePath property of the current object.
152
+ * @returns {void}
153
+ */
154
+ updateContentType(): void;
155
+ /**
156
+ * Clone the current FileResponse object.
157
+ * @returns {FileResponse} A new FileResponse object with the same properties as the current object.
158
+ */
159
+ clone(): FileResponse;
160
+ /**
161
+ * Reads the contents of the file specified by the filePath property and returns a Promise that
162
+ * resolves with an ArrayBuffer containing the file's contents.
163
+ * @returns {Promise<ArrayBuffer>} A Promise that resolves with an ArrayBuffer containing the file's contents.
164
+ * @throws {Error} If the file cannot be read.
165
+ */
166
+ arrayBuffer(): Promise<ArrayBuffer>;
167
+ /**
168
+ * Reads the contents of the file specified by the filePath property and returns a Promise that
169
+ * resolves with a Blob containing the file's contents.
170
+ * @returns {Promise<Blob>} A Promise that resolves with a Blob containing the file's contents.
171
+ * @throws {Error} If the file cannot be read.
172
+ */
173
+ blob(): Promise<Blob>;
174
+ /**
175
+ * Reads the contents of the file specified by the filePath property and returns a Promise that
176
+ * resolves with a string containing the file's contents.
177
+ * @returns {Promise<string>} A Promise that resolves with a string containing the file's contents.
178
+ * @throws {Error} If the file cannot be read.
179
+ */
180
+ text(): Promise<string>;
181
+ /**
182
+ * Reads the contents of the file specified by the filePath property and returns a Promise that
183
+ * resolves with a parsed JavaScript object containing the file's contents.
184
+ *
185
+ * @returns {Promise<Object>} A Promise that resolves with a parsed JavaScript object containing the file's contents.
186
+ * @throws {Error} If the file cannot be read.
187
+ */
188
+ json(): Promise<any>;
189
+ }
190
+ export {};
191
+ //# sourceMappingURL=hub.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"hub.d.ts","sourceRoot":"","sources":["../../src/utils/hub.js"],"names":[],"mappings":"AAsLA;;;;;GAKG;AACH,mCAHW,GAAG,GAAC,MAAM,GACR,QAAQ,YAAY,GAAC,QAAQ,CAAC,CAgC1C;AA2GD;;;;;;;;;;;;;;GAcG;AACH,8CAVW,MAAM,YAGN,MAAM,UACN,OAAO,YACP,iBAAiB,GAGf,QAAQ,UAAU,CAAC,CAyO/B;AAED;;;;;;;;;GASG;AACH,wCAPW,MAAM,YACN,MAAM,UACN,OAAO,YACP,iBAAiB,GACf,YAAe,CAc3B;;;;;;;;;;;;;;;;;;gBA/jBa,MAAM;;;;uBACN,OAAO;;;;;;eACP,MAAM;;;;;;;;;;gBAON,MAAM;;;;sBAEN,MAAM;;;;aACN,OAAO,cAAc,EAAE,UAAU,GAAC,OAAO,MAAM,EAAE,OAAO,cAAc,EAAE,UAAU,CAAC;;;;YACnF,OAAO,aAAa,EAAE,QAAQ,GAAC,OAAO,MAAM,EAAE,OAAO,aAAa,EAAE,QAAQ,CAAC;;;;+BAC7E,OAAO,GAAC,OAAO,MAAM,EAAE,OAAO,CAAC;;;;;;;;;qCAKhC,iBAAiB,GAAG,8BAA8B;AAzB/D;;;;;;;;;;;GAWG;AAEH;;;;;;;;;GASG;AAEH;;GAEG;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"}
@@ -0,0 +1,119 @@
1
+ export class RawImage {
2
+ /**
3
+ * Helper method for reading an image from a variety of input types.
4
+ * @param {RawImage|string|URL} input
5
+ * @returns The image object.
6
+ *
7
+ * **Example:** Read image from a URL.
8
+ * ```javascript
9
+ * let image = await RawImage.read('https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/football-match.jpg');
10
+ * // RawImage {
11
+ * // "data": Uint8ClampedArray [ 25, 25, 25, 19, 19, 19, ... ],
12
+ * // "width": 800,
13
+ * // "height": 533,
14
+ * // "channels": 3
15
+ * // }
16
+ * ```
17
+ */
18
+ static read(input: RawImage | string | URL): Promise<RawImage>;
19
+ /**
20
+ * Read an image from a canvas.
21
+ * @param {HTMLCanvasElement|OffscreenCanvas} canvas The canvas to read the image from.
22
+ * @returns {RawImage} The image object.
23
+ */
24
+ static fromCanvas(canvas: HTMLCanvasElement | OffscreenCanvas): RawImage;
25
+ /**
26
+ * Read an image from a URL or file path.
27
+ * @param {string|URL} url The URL or file path to read the image from.
28
+ * @returns {Promise<RawImage>} The image object.
29
+ */
30
+ static fromURL(url: string | URL): Promise<RawImage>;
31
+ /**
32
+ * Helper method to create a new Image from a blob.
33
+ * @param {Blob} blob The blob to read the image from.
34
+ * @returns {Promise<RawImage>} The image object.
35
+ */
36
+ static fromBlob(blob: Blob): Promise<RawImage>;
37
+ /**
38
+ * Helper method to create a new Image from a tensor
39
+ * @param {Tensor} tensor
40
+ */
41
+ static fromTensor(tensor: Tensor, channel_format?: string): RawImage;
42
+ /**
43
+ * Create a new `RawImage` object.
44
+ * @param {Uint8ClampedArray|Uint8Array} data The pixel data.
45
+ * @param {number} width The width of the image.
46
+ * @param {number} height The height of the image.
47
+ * @param {1|2|3|4} channels The number of channels.
48
+ */
49
+ constructor(data: Uint8ClampedArray | Uint8Array, width: number, height: number, channels: 1 | 2 | 3 | 4);
50
+ data: Uint8Array | Uint8ClampedArray;
51
+ width: number;
52
+ height: number;
53
+ channels: 2 | 1 | 3 | 4;
54
+ /**
55
+ * Returns the size of the image (width, height).
56
+ * @returns {[number, number]} The size of the image (width, height).
57
+ */
58
+ get size(): [number, number];
59
+ /**
60
+ * Convert the image to grayscale format.
61
+ * @returns {RawImage} `this` to support chaining.
62
+ */
63
+ grayscale(): RawImage;
64
+ /**
65
+ * Convert the image to RGB format.
66
+ * @returns {RawImage} `this` to support chaining.
67
+ */
68
+ rgb(): RawImage;
69
+ /**
70
+ * Convert the image to RGBA format.
71
+ * @returns {RawImage} `this` to support chaining.
72
+ */
73
+ rgba(): RawImage;
74
+ /**
75
+ * Resize the image to the given dimensions. This method uses the canvas API to perform the resizing.
76
+ * @param {number} width The width of the new image.
77
+ * @param {number} height The height of the new image.
78
+ * @param {Object} options Additional options for resizing.
79
+ * @param {0|1|2|3|4|5|string} [options.resample] The resampling method to use.
80
+ * @returns {Promise<RawImage>} `this` to support chaining.
81
+ */
82
+ resize(width: number, height: number, { resample, }?: {
83
+ resample?: 0 | 1 | 2 | 3 | 4 | 5 | string;
84
+ }): Promise<RawImage>;
85
+ pad([left, right, top, bottom]: [any, any, any, any]): Promise<any>;
86
+ crop([x_min, y_min, x_max, y_max]: [any, any, any, any]): Promise<any>;
87
+ center_crop(crop_width: any, crop_height: any): Promise<any>;
88
+ toBlob(type?: string, quality?: number): Promise<any>;
89
+ toTensor(channel_format?: string): Tensor;
90
+ toCanvas(): any;
91
+ /**
92
+ * Helper method to update the image data.
93
+ * @param {Uint8ClampedArray} data The new image data.
94
+ * @param {number} width The new width of the image.
95
+ * @param {number} height The new height of the image.
96
+ * @param {1|2|3|4|null} [channels] The new number of channels of the image.
97
+ * @private
98
+ */
99
+ private _update;
100
+ /**
101
+ * Clone the image
102
+ * @returns {RawImage} The cloned image
103
+ */
104
+ clone(): RawImage;
105
+ /**
106
+ * Helper method for converting image to have a certain number of channels
107
+ * @param {number} numChannels The number of channels. Must be 1, 3, or 4.
108
+ * @returns {RawImage} `this` to support chaining.
109
+ */
110
+ convert(numChannels: number): RawImage;
111
+ /**
112
+ * Save the image to the given path.
113
+ * @param {string} path The path to save the image to.
114
+ */
115
+ save(path: string): Promise<any>;
116
+ toSharp(): any;
117
+ }
118
+ import { Tensor } from './tensor.js';
119
+ //# sourceMappingURL=image.d.ts.map
@@ -0,0 +1 @@
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;IAED;;;;OAIG;IACH,0BAHW,iBAAiB,GAAC,eAAe,GAC/B,QAAQ,CAUpB;IAED;;;;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;IAlID;;;;;;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;IAgHD;;;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;uBA5tBsB,aAAa"}
@@ -0,0 +1,280 @@
1
+ /**
2
+ * @file Helper module for mathematical processing.
3
+ *
4
+ * These functions and classes are only used internally,
5
+ * meaning an end-user shouldn't need to access anything here.
6
+ *
7
+ * @module utils/maths
8
+ */
9
+ /**
10
+ * @typedef {Int8Array | Uint8Array | Uint8ClampedArray | Int16Array | Uint16Array | Int32Array | Uint32Array | Float32Array | Float64Array} TypedArray
11
+ * @typedef {BigInt64Array | BigUint64Array} BigTypedArray
12
+ * @typedef {TypedArray | BigTypedArray} AnyTypedArray
13
+ */
14
+ /**
15
+ * @param {TypedArray} input
16
+ */
17
+ 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;
18
+ /**
19
+ * Helper method to permute a `AnyTypedArray` directly
20
+ * @template {AnyTypedArray} T
21
+ * @param {T} array
22
+ * @param {number[]} dims
23
+ * @param {number[]} axes
24
+ * @returns {[T, number[]]} The permuted array and the new shape.
25
+ */
26
+ export function permute_data<T extends AnyTypedArray>(array: T, dims: number[], axes: number[]): [T, number[]];
27
+ /**
28
+ * Compute the softmax of an array of numbers.
29
+ * @template {TypedArray|number[]} T
30
+ * @param {T} arr The array of numbers to compute the softmax of.
31
+ * @returns {T} The softmax array.
32
+ */
33
+ export function softmax<T extends number[] | TypedArray>(arr: T): T;
34
+ /**
35
+ * Calculates the logarithm of the softmax function for the input array.
36
+ * @template {TypedArray|number[]} T
37
+ * @param {T} arr The input array to calculate the log_softmax function for.
38
+ * @returns {T} The resulting log_softmax array.
39
+ */
40
+ export function log_softmax<T extends number[] | TypedArray>(arr: T): T;
41
+ /**
42
+ * Calculates the dot product of two arrays.
43
+ * @param {number[]} arr1 The first array.
44
+ * @param {number[]} arr2 The second array.
45
+ * @returns {number} The dot product of arr1 and arr2.
46
+ */
47
+ export function dot(arr1: number[], arr2: number[]): number;
48
+ /**
49
+ * Computes the cosine similarity between two arrays.
50
+ *
51
+ * @param {number[]} arr1 The first array.
52
+ * @param {number[]} arr2 The second array.
53
+ * @returns {number} The cosine similarity between the two arrays.
54
+ */
55
+ export function cos_sim(arr1: number[], arr2: number[]): number;
56
+ /**
57
+ * Calculates the magnitude of a given array.
58
+ * @param {number[]} arr The array to calculate the magnitude of.
59
+ * @returns {number} The magnitude of the array.
60
+ */
61
+ export function magnitude(arr: number[]): number;
62
+ /**
63
+ * Returns the value and index of the minimum element in an array.
64
+ * @param {number[]|TypedArray} arr array of numbers.
65
+ * @returns {number[]} the value and index of the minimum element, of the form: [valueOfMin, indexOfMin]
66
+ * @throws {Error} If array is empty.
67
+ */
68
+ export function min(arr: number[] | TypedArray): number[];
69
+ /**
70
+ * Returns the value and index of the maximum element in an array.
71
+ * @param {number[]|AnyTypedArray} arr array of numbers.
72
+ * @returns {[number, number]} the value and index of the maximum element, of the form: [valueOfMax, indexOfMax]
73
+ * @throws {Error} If array is empty.
74
+ */
75
+ export function max(arr: number[] | AnyTypedArray): [number, number];
76
+ /**
77
+ * Performs median filter on the provided data. Padding is done by mirroring the data.
78
+ * @param {AnyTypedArray} data The input array
79
+ * @param {number} windowSize The window size
80
+ */
81
+ export function medianFilter(data: AnyTypedArray, windowSize: number): any;
82
+ /**
83
+ * Helper function to round a number to a given number of decimals
84
+ * @param {number} num The number to round
85
+ * @param {number} decimals The number of decimals
86
+ * @returns {number} The rounded number
87
+ */
88
+ export function round(num: number, decimals: number): number;
89
+ /**
90
+ * Helper function to round a number to the nearest integer, with ties rounded to the nearest even number.
91
+ * Also known as "bankers' rounding". This is the default rounding mode in python. For example:
92
+ * 1.5 rounds to 2 and 2.5 rounds to 2.
93
+ *
94
+ * @param {number} x The number to round
95
+ * @returns {number} The rounded number
96
+ */
97
+ export function bankers_round(x: number): number;
98
+ /**
99
+ * Measures similarity between two temporal sequences (e.g., input audio and output tokens
100
+ * to generate token-level timestamps).
101
+ * @param {number[][]} matrix
102
+ * @returns {number[][]}
103
+ */
104
+ export function dynamic_time_warping(matrix: number[][]): number[][];
105
+ export class FFT {
106
+ constructor(fft_length: any);
107
+ fft_length: any;
108
+ isPowerOfTwo: boolean;
109
+ fft: P2FFT | NP2FFT;
110
+ outputBufferSize: number;
111
+ realTransform(out: any, input: any): void;
112
+ transform(out: any, input: any): void;
113
+ }
114
+ export type TypedArray = Int8Array | Uint8Array | Uint8ClampedArray | Int16Array | Uint16Array | Int32Array | Uint32Array | Float32Array | Float64Array;
115
+ export type BigTypedArray = BigInt64Array | BigUint64Array;
116
+ export type AnyTypedArray = TypedArray | BigTypedArray;
117
+ /**
118
+ * Implementation of Radix-4 FFT.
119
+ *
120
+ * P2FFT class provides functionality for performing Fast Fourier Transform on arrays
121
+ * which are a power of two in length.
122
+ * Code adapted from https://www.npmjs.com/package/fft.js
123
+ */
124
+ declare class P2FFT {
125
+ /**
126
+ * @param {number} size The size of the input array. Must be a power of two larger than 1.
127
+ * @throws {Error} FFT size must be a power of two larger than 1.
128
+ */
129
+ constructor(size: number);
130
+ size: number;
131
+ _csize: number;
132
+ table: Float64Array;
133
+ _width: number;
134
+ _bitrev: Int32Array;
135
+ /**
136
+ * Create a complex number array with size `2 * size`
137
+ *
138
+ * @returns {Float64Array} A complex number array with size `2 * size`
139
+ */
140
+ createComplexArray(): Float64Array;
141
+ /**
142
+ * Converts a complex number representation stored in a Float64Array to an array of real numbers.
143
+ *
144
+ * @param {Float64Array} complex The complex number representation to be converted.
145
+ * @param {number[]} [storage] An optional array to store the result in.
146
+ * @returns {number[]} An array of real numbers representing the input complex number representation.
147
+ */
148
+ fromComplexArray(complex: Float64Array, storage?: number[]): number[];
149
+ /**
150
+ * Convert a real-valued input array to a complex-valued output array.
151
+ * @param {Float64Array} input The real-valued input array.
152
+ * @param {Float64Array} [storage] Optional buffer to store the output array.
153
+ * @returns {Float64Array} The complex-valued output array.
154
+ */
155
+ toComplexArray(input: Float64Array, storage?: Float64Array): Float64Array;
156
+ /**
157
+ * Performs a Fast Fourier Transform (FFT) on the given input data and stores the result in the output buffer.
158
+ *
159
+ * @param {Float64Array} out The output buffer to store the result.
160
+ * @param {Float64Array} data The input data to transform.
161
+ *
162
+ * @throws {Error} Input and output buffers must be different.
163
+ *
164
+ * @returns {void}
165
+ */
166
+ transform(out: Float64Array, data: Float64Array): void;
167
+ /**
168
+ * Performs a real-valued forward FFT on the given input buffer and stores the result in the given output buffer.
169
+ * The input buffer must contain real values only, while the output buffer will contain complex values. The input and
170
+ * output buffers must be different.
171
+ *
172
+ * @param {Float64Array} out The output buffer.
173
+ * @param {Float64Array} data The input buffer containing real values.
174
+ *
175
+ * @throws {Error} If the input and output buffers are the same.
176
+ */
177
+ realTransform(out: Float64Array, data: Float64Array): void;
178
+ /**
179
+ * Performs an inverse FFT transformation on the given `data` array, and stores the result in `out`.
180
+ * The `out` array must be a different buffer than the `data` array. The `out` array will contain the
181
+ * result of the transformation. The `data` array will not be modified.
182
+ *
183
+ * @param {Float64Array} out The output buffer for the transformed data.
184
+ * @param {Float64Array} data The input data to transform.
185
+ * @throws {Error} If `out` and `data` refer to the same buffer.
186
+ * @returns {void}
187
+ */
188
+ inverseTransform(out: Float64Array, data: Float64Array): void;
189
+ /**
190
+ * Performs a radix-4 implementation of a discrete Fourier transform on a given set of data.
191
+ *
192
+ * @param {Float64Array} out The output buffer for the transformed data.
193
+ * @param {Float64Array} data The input buffer of data to be transformed.
194
+ * @param {number} inv A scaling factor to apply to the transform.
195
+ * @returns {void}
196
+ */
197
+ _transform4(out: Float64Array, data: Float64Array, inv: number): void;
198
+ /**
199
+ * Performs a radix-2 implementation of a discrete Fourier transform on a given set of data.
200
+ *
201
+ * @param {Float64Array} data The input buffer of data to be transformed.
202
+ * @param {Float64Array} out The output buffer for the transformed data.
203
+ * @param {number} outOff The offset at which to write the output data.
204
+ * @param {number} off The offset at which to begin reading the input data.
205
+ * @param {number} step The step size for indexing the input data.
206
+ * @returns {void}
207
+ */
208
+ _singleTransform2(data: Float64Array, out: Float64Array, outOff: number, off: number, step: number): void;
209
+ /**
210
+ * Performs radix-4 transformation on input data of length 8
211
+ *
212
+ * @param {Float64Array} data Input data array of length 8
213
+ * @param {Float64Array} out Output data array of length 8
214
+ * @param {number} outOff Index of output array to start writing from
215
+ * @param {number} off Index of input array to start reading from
216
+ * @param {number} step Step size between elements in input array
217
+ * @param {number} inv Scaling factor for inverse transform
218
+ *
219
+ * @returns {void}
220
+ */
221
+ _singleTransform4(data: Float64Array, out: Float64Array, outOff: number, off: number, step: number, inv: number): void;
222
+ /**
223
+ * Real input radix-4 implementation
224
+ * @param {Float64Array} out Output array for the transformed data
225
+ * @param {Float64Array} data Input array of real data to be transformed
226
+ * @param {number} inv The scale factor used to normalize the inverse transform
227
+ */
228
+ _realTransform4(out: Float64Array, data: Float64Array, inv: number): void;
229
+ /**
230
+ * Performs a single real input radix-2 transformation on the provided data
231
+ *
232
+ * @param {Float64Array} data The input data array
233
+ * @param {Float64Array} out The output data array
234
+ * @param {number} outOff The output offset
235
+ * @param {number} off The input offset
236
+ * @param {number} step The step
237
+ *
238
+ * @returns {void}
239
+ */
240
+ _singleRealTransform2(data: Float64Array, out: Float64Array, outOff: number, off: number, step: number): void;
241
+ /**
242
+ * Computes a single real-valued transform using radix-4 algorithm.
243
+ * This method is only called for len=8.
244
+ *
245
+ * @param {Float64Array} data The input data array.
246
+ * @param {Float64Array} out The output data array.
247
+ * @param {number} outOff The offset into the output array.
248
+ * @param {number} off The offset into the input array.
249
+ * @param {number} step The step size for the input array.
250
+ * @param {number} inv The value of inverse.
251
+ */
252
+ _singleRealTransform4(data: Float64Array, out: Float64Array, outOff: number, off: number, step: number, inv: number): void;
253
+ }
254
+ /**
255
+ * NP2FFT class provides functionality for performing Fast Fourier Transform on arrays
256
+ * which are not a power of two in length. In such cases, the chirp-z transform is used.
257
+ *
258
+ * For more information, see: https://math.stackexchange.com/questions/77118/non-power-of-2-ffts/77156#77156
259
+ */
260
+ declare class NP2FFT {
261
+ /**
262
+ * Constructs a new NP2FFT object.
263
+ * @param {number} fft_length The length of the FFT
264
+ */
265
+ constructor(fft_length: number);
266
+ bufferSize: number;
267
+ _a: number;
268
+ _chirpBuffer: Float64Array;
269
+ _buffer1: Float64Array;
270
+ _buffer2: Float64Array;
271
+ _outBuffer1: Float64Array;
272
+ _outBuffer2: Float64Array;
273
+ _slicedChirpBuffer: Float64Array;
274
+ _f: P2FFT;
275
+ _transform(output: any, input: any, real: any): void;
276
+ transform(output: any, input: any): void;
277
+ realTransform(output: any, input: any): void;
278
+ }
279
+ export {};
280
+ //# sourceMappingURL=maths.d.ts.map
@@ -0,0 +1 @@
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;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;AAGD;;;;;GAKG;AACH,6CAHW,MAAM,EAAE,EAAE,GACR,MAAM,EAAE,EAAE,CA+EtB;AA5KD;IACI,6BAUC;IATG,gBAA4B;IAC5B,sBAA4C;IAExC,oBAAgC;IAChC,yBAAsC;IAO9C,0CAEC;IAED,sCAEC;CACJ;yBAt3BY,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;AAuPvC;;;;;;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"}