@huggingface/transformers 4.0.0-next.3 → 4.0.0-next.5

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 (168) hide show
  1. package/README.md +10 -4
  2. package/dist/ort-wasm-simd-threaded.jsep.mjs +28 -28
  3. package/dist/transformers.js +3109 -2099
  4. package/dist/transformers.min.js +17 -19
  5. package/dist/transformers.node.cjs +3100 -2060
  6. package/dist/transformers.node.min.cjs +19 -21
  7. package/dist/transformers.node.min.mjs +19 -21
  8. package/dist/transformers.node.mjs +3085 -2060
  9. package/dist/transformers.web.js +1312 -276
  10. package/dist/transformers.web.min.js +15 -15
  11. package/package.json +4 -4
  12. package/src/backends/onnx.js +66 -10
  13. package/src/backends/utils/cacheWasm.js +9 -6
  14. package/src/configs.js +52 -3
  15. package/src/env.js +66 -7
  16. package/src/generation/logits_sampler.js +3 -15
  17. package/src/image_processors_utils.js +2 -6
  18. package/src/models/afmoe/modeling_afmoe.js +5 -0
  19. package/src/models/auto/image_processing_auto.js +2 -1
  20. package/src/models/auto/modeling_auto.js +2 -1
  21. package/src/models/auto/tokenization_auto.js +2 -1
  22. package/src/models/clap/feature_extraction_clap.js +2 -1
  23. package/src/models/cohere2/modeling_cohere2.js +5 -0
  24. package/src/models/marian/tokenization_marian.js +3 -2
  25. package/src/models/modeling_utils.js +14 -4
  26. package/src/models/models.js +6 -0
  27. package/src/models/paligemma/processing_paligemma.js +3 -2
  28. package/src/models/processors.js +2 -0
  29. package/src/models/qwen2_5_vl/modeling_qwen2_5_vl.js +5 -0
  30. package/src/models/qwen2_5_vl/processing_qwen2_5_vl.js +3 -0
  31. package/src/models/qwen2_vl/image_processing_qwen2_vl.js +54 -0
  32. package/src/models/qwen2_vl/modeling_qwen2_vl.js +8 -2
  33. package/src/models/qwen3_5/modeling_qwen3_5.js +3 -0
  34. package/src/models/qwen3_5_moe/modeling_qwen3_5_moe.js +3 -0
  35. package/src/models/qwen3_vl/modeling_qwen3_vl.js +3 -0
  36. package/src/models/qwen3_vl/processing_qwen3_vl.js +3 -0
  37. package/src/models/registry.js +9 -1
  38. package/src/models/session.js +16 -50
  39. package/src/models/whisper/feature_extraction_whisper.js +2 -1
  40. package/src/models/whisper/modeling_whisper.js +6 -5
  41. package/src/models/xlm/tokenization_xlm.js +2 -1
  42. package/src/pipelines/automatic-speech-recognition.js +3 -2
  43. package/src/pipelines/index.js +395 -0
  44. package/src/pipelines/text-generation.js +4 -0
  45. package/src/pipelines/text-to-audio.js +4 -2
  46. package/src/pipelines/zero-shot-classification.js +3 -2
  47. package/src/pipelines.js +104 -356
  48. package/src/tokenization_utils.js +42 -21
  49. package/src/transformers.js +8 -1
  50. package/src/utils/audio.js +2 -1
  51. package/src/utils/cache.js +4 -1
  52. package/src/utils/core.js +23 -1
  53. package/src/utils/devices.js +22 -0
  54. package/src/utils/dtypes.js +55 -0
  55. package/src/utils/hub/files.js +17 -2
  56. package/src/utils/hub/utils.js +10 -4
  57. package/src/utils/hub.js +57 -17
  58. package/src/utils/image.js +2 -1
  59. package/src/utils/logger.js +67 -0
  60. package/src/utils/model-loader.js +35 -17
  61. package/src/utils/model_registry/ModelRegistry.js +299 -0
  62. package/src/utils/model_registry/clear_cache.js +128 -0
  63. package/src/utils/model_registry/get_file_metadata.js +149 -0
  64. package/src/utils/model_registry/get_files.js +42 -0
  65. package/src/utils/model_registry/get_model_files.js +182 -0
  66. package/src/utils/model_registry/get_pipeline_files.js +53 -0
  67. package/src/utils/model_registry/get_processor_files.js +20 -0
  68. package/src/utils/model_registry/get_tokenizer_files.js +21 -0
  69. package/src/utils/model_registry/is_cached.js +92 -0
  70. package/src/utils/random.js +225 -0
  71. package/src/utils/tensor.js +8 -21
  72. package/src/utils/video.js +2 -2
  73. package/types/backends/onnx.d.ts.map +1 -1
  74. package/types/backends/utils/cacheWasm.d.ts.map +1 -1
  75. package/types/configs.d.ts.map +1 -1
  76. package/types/env.d.ts +42 -24
  77. package/types/env.d.ts.map +1 -1
  78. package/types/generation/logits_sampler.d.ts +2 -2
  79. package/types/generation/logits_sampler.d.ts.map +1 -1
  80. package/types/image_processors_utils.d.ts.map +1 -1
  81. package/types/models/afmoe/modeling_afmoe.d.ts +8 -0
  82. package/types/models/afmoe/modeling_afmoe.d.ts.map +1 -0
  83. package/types/models/auto/image_processing_auto.d.ts.map +1 -1
  84. package/types/models/auto/modeling_auto.d.ts.map +1 -1
  85. package/types/models/auto/tokenization_auto.d.ts.map +1 -1
  86. package/types/models/clap/feature_extraction_clap.d.ts.map +1 -1
  87. package/types/models/cohere2/modeling_cohere2.d.ts +8 -0
  88. package/types/models/cohere2/modeling_cohere2.d.ts.map +1 -0
  89. package/types/models/marian/tokenization_marian.d.ts.map +1 -1
  90. package/types/models/modeling_utils.d.ts.map +1 -1
  91. package/types/models/models.d.ts +6 -0
  92. package/types/models/paligemma/processing_paligemma.d.ts.map +1 -1
  93. package/types/models/processors.d.ts +2 -0
  94. package/types/models/qwen2_5_vl/modeling_qwen2_5_vl.d.ts +4 -0
  95. package/types/models/qwen2_5_vl/modeling_qwen2_5_vl.d.ts.map +1 -0
  96. package/types/models/qwen2_5_vl/processing_qwen2_5_vl.d.ts +4 -0
  97. package/types/models/qwen2_5_vl/processing_qwen2_5_vl.d.ts.map +1 -0
  98. package/types/models/qwen2_vl/image_processing_qwen2_vl.d.ts +3 -0
  99. package/types/models/qwen2_vl/image_processing_qwen2_vl.d.ts.map +1 -1
  100. package/types/models/qwen2_vl/modeling_qwen2_vl.d.ts +1 -0
  101. package/types/models/qwen2_vl/modeling_qwen2_vl.d.ts.map +1 -1
  102. package/types/models/qwen3_5/modeling_qwen3_5.d.ts +4 -0
  103. package/types/models/qwen3_5/modeling_qwen3_5.d.ts.map +1 -0
  104. package/types/models/qwen3_5_moe/modeling_qwen3_5_moe.d.ts +4 -0
  105. package/types/models/qwen3_5_moe/modeling_qwen3_5_moe.d.ts.map +1 -0
  106. package/types/models/qwen3_vl/modeling_qwen3_vl.d.ts +4 -0
  107. package/types/models/qwen3_vl/modeling_qwen3_vl.d.ts.map +1 -0
  108. package/types/models/qwen3_vl/processing_qwen3_vl.d.ts +4 -0
  109. package/types/models/qwen3_vl/processing_qwen3_vl.d.ts.map +1 -0
  110. package/types/models/registry.d.ts.map +1 -1
  111. package/types/models/session.d.ts.map +1 -1
  112. package/types/models/whisper/feature_extraction_whisper.d.ts.map +1 -1
  113. package/types/models/whisper/modeling_whisper.d.ts.map +1 -1
  114. package/types/models/xlm/tokenization_xlm.d.ts.map +1 -1
  115. package/types/pipelines/automatic-speech-recognition.d.ts.map +1 -1
  116. package/types/pipelines/index.d.ts +299 -0
  117. package/types/pipelines/index.d.ts.map +1 -0
  118. package/types/pipelines/text-generation.d.ts +5 -1
  119. package/types/pipelines/text-generation.d.ts.map +1 -1
  120. package/types/pipelines/text-to-audio.d.ts.map +1 -1
  121. package/types/pipelines/zero-shot-classification.d.ts.map +1 -1
  122. package/types/pipelines.d.ts +50 -291
  123. package/types/pipelines.d.ts.map +1 -1
  124. package/types/tokenization_utils.d.ts +44 -26
  125. package/types/tokenization_utils.d.ts.map +1 -1
  126. package/types/transformers.d.ts +6 -1
  127. package/types/transformers.d.ts.map +1 -1
  128. package/types/utils/audio.d.ts.map +1 -1
  129. package/types/utils/cache.d.ts +6 -0
  130. package/types/utils/cache.d.ts.map +1 -1
  131. package/types/utils/core.d.ts +59 -2
  132. package/types/utils/core.d.ts.map +1 -1
  133. package/types/utils/devices.d.ts +15 -0
  134. package/types/utils/devices.d.ts.map +1 -1
  135. package/types/utils/dtypes.d.ts +16 -0
  136. package/types/utils/dtypes.d.ts.map +1 -1
  137. package/types/utils/hub/files.d.ts +6 -0
  138. package/types/utils/hub/files.d.ts.map +1 -1
  139. package/types/utils/hub/utils.d.ts +2 -1
  140. package/types/utils/hub/utils.d.ts.map +1 -1
  141. package/types/utils/hub.d.ts +29 -0
  142. package/types/utils/hub.d.ts.map +1 -1
  143. package/types/utils/image.d.ts.map +1 -1
  144. package/types/utils/logger.d.ts +28 -0
  145. package/types/utils/logger.d.ts.map +1 -0
  146. package/types/utils/model-loader.d.ts +15 -0
  147. package/types/utils/model-loader.d.ts.map +1 -1
  148. package/types/utils/model_registry/ModelRegistry.d.ts +211 -0
  149. package/types/utils/model_registry/ModelRegistry.d.ts.map +1 -0
  150. package/types/utils/model_registry/clear_cache.d.ts +74 -0
  151. package/types/utils/model_registry/clear_cache.d.ts.map +1 -0
  152. package/types/utils/model_registry/get_file_metadata.d.ts +20 -0
  153. package/types/utils/model_registry/get_file_metadata.d.ts.map +1 -0
  154. package/types/utils/model_registry/get_files.d.ts +23 -0
  155. package/types/utils/model_registry/get_files.d.ts.map +1 -0
  156. package/types/utils/model_registry/get_model_files.d.ts +22 -0
  157. package/types/utils/model_registry/get_model_files.d.ts.map +1 -0
  158. package/types/utils/model_registry/get_pipeline_files.d.ts +21 -0
  159. package/types/utils/model_registry/get_pipeline_files.d.ts.map +1 -0
  160. package/types/utils/model_registry/get_processor_files.d.ts +9 -0
  161. package/types/utils/model_registry/get_processor_files.d.ts.map +1 -0
  162. package/types/utils/model_registry/get_tokenizer_files.d.ts +9 -0
  163. package/types/utils/model_registry/get_tokenizer_files.d.ts.map +1 -0
  164. package/types/utils/model_registry/is_cached.d.ts +62 -0
  165. package/types/utils/model_registry/is_cached.d.ts.map +1 -0
  166. package/types/utils/random.d.ts +86 -0
  167. package/types/utils/random.d.ts.map +1 -0
  168. package/types/utils/tensor.d.ts.map +1 -1
@@ -5,32 +5,31 @@ var __export = (target, all) => {
5
5
  };
6
6
 
7
7
  // ignore-modules:node:fs
8
- var emptyObj = {};
9
- var node_fs_default = emptyObj;
8
+ var node_fs_default = {};
10
9
 
11
10
  // ignore-modules:node:path
12
- var emptyObj2 = {};
13
- var node_path_default = emptyObj2;
11
+ var node_path_default = {};
14
12
 
15
13
  // ignore-modules:node:url
16
- var emptyObj3 = {};
17
- var node_url_default = emptyObj3;
14
+ var node_url_default = {};
18
15
 
19
16
  // src/env.js
20
- var VERSION = "4.0.0-next.3";
21
- var IS_PROCESS_AVAILABLE = typeof process !== "undefined";
22
- var IS_NODE_ENV = IS_PROCESS_AVAILABLE && process?.release?.name === "node";
17
+ var VERSION = "4.0.0-next.5";
23
18
  var IS_FS_AVAILABLE = !isEmpty(node_fs_default);
24
19
  var IS_PATH_AVAILABLE = !isEmpty(node_path_default);
20
+ var IS_WEB_CACHE_AVAILABLE = typeof self !== "undefined" && "caches" in self;
25
21
  var IS_DENO_RUNTIME = typeof globalThis.Deno !== "undefined";
26
22
  var IS_BUN_RUNTIME = typeof globalThis.Bun !== "undefined";
23
+ var IS_DENO_WEB_RUNTIME = IS_DENO_RUNTIME && IS_WEB_CACHE_AVAILABLE && !IS_FS_AVAILABLE;
24
+ var IS_PROCESS_AVAILABLE = typeof process !== "undefined";
25
+ var IS_NODE_ENV = IS_PROCESS_AVAILABLE && process?.release?.name === "node" && !IS_DENO_WEB_RUNTIME;
27
26
  var IS_BROWSER_ENV = typeof window !== "undefined" && typeof window.document !== "undefined";
28
27
  var IS_WEBWORKER_ENV = typeof self !== "undefined" && ["DedicatedWorkerGlobalScope", "ServiceWorkerGlobalScope", "SharedWorkerGlobalScope"].includes(
29
28
  self.constructor?.name
30
29
  );
31
- var IS_WEB_CACHE_AVAILABLE = typeof self !== "undefined" && "caches" in self;
32
30
  var IS_WEBGPU_AVAILABLE = IS_NODE_ENV || typeof navigator !== "undefined" && "gpu" in navigator;
33
31
  var IS_WEBNN_AVAILABLE = typeof navigator !== "undefined" && "ml" in navigator;
32
+ var IS_CRYPTO_AVAILABLE = typeof crypto !== "undefined" && typeof crypto.getRandomValues === "function";
34
33
  var isSafari = () => {
35
34
  if (typeof navigator === "undefined") {
36
35
  return false;
@@ -62,7 +61,9 @@ var apis = Object.freeze({
62
61
  /** Whether the filesystem API is available */
63
62
  IS_FS_AVAILABLE,
64
63
  /** Whether the path API is available */
65
- IS_PATH_AVAILABLE
64
+ IS_PATH_AVAILABLE,
65
+ /** Whether the crypto API is available */
66
+ IS_CRYPTO_AVAILABLE
66
67
  });
67
68
  var RUNNING_LOCALLY = IS_FS_AVAILABLE && IS_PATH_AVAILABLE;
68
69
  var dirname__ = "./";
@@ -77,6 +78,20 @@ if (RUNNING_LOCALLY) {
77
78
  var DEFAULT_CACHE_DIR = RUNNING_LOCALLY ? node_path_default.join(dirname__, "/.cache/") : null;
78
79
  var DEFAULT_LOCAL_MODEL_PATH = "/models/";
79
80
  var localModelPath = RUNNING_LOCALLY ? node_path_default.join(dirname__, DEFAULT_LOCAL_MODEL_PATH) : DEFAULT_LOCAL_MODEL_PATH;
81
+ var DEFAULT_FETCH = typeof globalThis.fetch === "function" ? globalThis.fetch.bind(globalThis) : void 0;
82
+ var LogLevel = Object.freeze({
83
+ /** All messages including debug output (value: 10) */
84
+ DEBUG: 10,
85
+ /** Errors, warnings, and info messages (value: 20) */
86
+ INFO: 20,
87
+ /** Errors and warnings (value: 30) */
88
+ WARNING: 30,
89
+ /** Only error messages (value: 40) */
90
+ ERROR: 40,
91
+ /** No logging output (value: 50) */
92
+ NONE: 50
93
+ });
94
+ var logLevel = LogLevel.WARNING;
80
95
  var env = {
81
96
  version: VERSION,
82
97
  /////////////////// Backends settings ///////////////////
@@ -85,28 +100,132 @@ var env = {
85
100
  // onnxruntime-web/onnxruntime-node
86
101
  onnx: {}
87
102
  },
103
+ /////////////////// Logging settings ///////////////////
104
+ get logLevel() {
105
+ return logLevel;
106
+ },
107
+ set logLevel(level) {
108
+ logLevel = level;
109
+ env.backends.onnx?.setLogLevel?.(level);
110
+ },
88
111
  /////////////////// Model settings ///////////////////
89
112
  allowRemoteModels: true,
90
113
  remoteHost: "https://huggingface.co/",
91
114
  remotePathTemplate: "{model}/resolve/{revision}/",
92
- allowLocalModels: !(IS_BROWSER_ENV || IS_WEBWORKER_ENV),
115
+ allowLocalModels: !(IS_BROWSER_ENV || IS_WEBWORKER_ENV || IS_DENO_WEB_RUNTIME),
116
+ // Default to true for non-web environments, false for web environments
93
117
  localModelPath,
94
118
  useFS: IS_FS_AVAILABLE,
95
119
  /////////////////// Cache settings ///////////////////
96
- useBrowserCache: IS_WEB_CACHE_AVAILABLE && !IS_DENO_RUNTIME,
120
+ useBrowserCache: IS_WEB_CACHE_AVAILABLE,
97
121
  useFSCache: IS_FS_AVAILABLE,
98
122
  cacheDir: DEFAULT_CACHE_DIR,
99
123
  useCustomCache: false,
100
124
  customCache: null,
101
125
  useWasmCache: IS_WEB_CACHE_AVAILABLE || IS_FS_AVAILABLE,
102
- cacheKey: "transformers-cache"
126
+ cacheKey: "transformers-cache",
127
+ /////////////////// Custom fetch /////////////////////
128
+ fetch: DEFAULT_FETCH
103
129
  //////////////////////////////////////////////////////
104
130
  };
105
131
  function isEmpty(obj) {
106
132
  return Object.keys(obj).length === 0;
107
133
  }
108
134
 
109
- // ../../node_modules/.pnpm/@huggingface+tokenizers@0.1.1/node_modules/@huggingface/tokenizers/dist/tokenizers.mjs
135
+ // src/utils/core.js
136
+ function dispatchCallback(progress_callback, data) {
137
+ if (progress_callback) progress_callback(data);
138
+ }
139
+ function isIntegralNumber(x) {
140
+ return Number.isInteger(x) || typeof x === "bigint";
141
+ }
142
+ function isNullishDimension(x) {
143
+ return x === null || x === void 0 || x === -1;
144
+ }
145
+ function calculateDimensions(arr) {
146
+ const dimensions = [];
147
+ let current = arr;
148
+ while (Array.isArray(current)) {
149
+ dimensions.push(current.length);
150
+ current = current[0];
151
+ }
152
+ return dimensions;
153
+ }
154
+ function mergeArrays(...arrs) {
155
+ return Array.prototype.concat.apply([], arrs);
156
+ }
157
+ function product(...a) {
158
+ return a.reduce((a2, b) => a2.flatMap((d) => b.map((e) => [d, e])));
159
+ }
160
+ function calculateReflectOffset(i, w) {
161
+ return Math.abs((i + w) % (2 * w) - w);
162
+ }
163
+ function pick(o, props) {
164
+ return Object.assign(
165
+ {},
166
+ ...props.map((prop) => {
167
+ if (o[prop] !== void 0) {
168
+ return { [prop]: o[prop] };
169
+ }
170
+ })
171
+ );
172
+ }
173
+ function count(arr, value) {
174
+ let count2 = 0;
175
+ for (const v of arr) {
176
+ if (v === value) ++count2;
177
+ }
178
+ return count2;
179
+ }
180
+
181
+ // src/utils/logger.js
182
+ var logger = {
183
+ /**
184
+ * Log an error message. Only suppressed when logLevel is NONE.
185
+ * @param {...any} args - Arguments to log
186
+ */
187
+ error(...args) {
188
+ if (env.logLevel <= LogLevel.ERROR) {
189
+ console.error(...args);
190
+ }
191
+ },
192
+ /**
193
+ * Log a warning message. Shown when logLevel <= WARNING.
194
+ * @param {...any} args - Arguments to log
195
+ */
196
+ warn(...args) {
197
+ if (env.logLevel <= LogLevel.WARNING) {
198
+ console.warn(...args);
199
+ }
200
+ },
201
+ /**
202
+ * Log an info message. Shown when logLevel <= INFO.
203
+ * @param {...any} args - Arguments to log
204
+ */
205
+ info(...args) {
206
+ if (env.logLevel <= LogLevel.INFO) {
207
+ console.log(...args);
208
+ }
209
+ },
210
+ /**
211
+ * Log a debug message. Only shown when logLevel is DEBUG.
212
+ * @param {...any} args - Arguments to log
213
+ */
214
+ debug(...args) {
215
+ if (env.logLevel <= LogLevel.DEBUG) {
216
+ console.log(...args);
217
+ }
218
+ },
219
+ /**
220
+ * Log a message (alias for info). Shown when logLevel <= INFO.
221
+ * @param {...any} args - Arguments to log
222
+ */
223
+ log(...args) {
224
+ this.info(...args);
225
+ }
226
+ };
227
+
228
+ // ../../node_modules/.pnpm/@huggingface+tokenizers@0.1.2/node_modules/@huggingface/tokenizers/dist/tokenizers.mjs
110
229
  var DictionarySplitter = class {
111
230
  /**
112
231
  * @param dictionary The dictionary of words to use for splitting.
@@ -230,6 +349,14 @@ var PROBLEMATIC_REGEX_MAP = /* @__PURE__ */ new Map([
230
349
  // JS doesn't support possessive quantifiers (these are used in recent OpenAI tokenizers).
231
350
  ["[^\\r\\n\\p{L}\\p{N}]?+", "[^\\r\\n\\p{L}\\p{N}]?"],
232
351
  ["[^\\s\\p{L}\\p{N}]++", "[^\\s\\p{L}\\p{N}]+"],
352
+ // JS doesn't support atomic groups (these are used in AFMoE tokenizers).
353
+ ["(?>\\p{Nd}{510})", "(?:\\p{Nd}{510})"],
354
+ // JS doesn't support stacking quantifiers.
355
+ // Uncaught SyntaxError: Invalid regular expression: /\p{Nd}{3}+/u: Nothing to repeat
356
+ ["\\p{Nd}{3}+", "(?:\\p{Nd}{3})+"],
357
+ // \G is an invalid escape in JS, and in most cases is just used as an optimization.
358
+ // So, we can safely remove it.
359
+ ["\\G", ""],
233
360
  // Used to override the default (invalid) regex of the bloom pretokenizer.
234
361
  // For more information, see https://github.com/huggingface/transformers.js/issues/94
235
362
  [` ?[^(\\s|[${BLOOM_SPLIT_CHARS}])]+`, ` ?[^\\s${BLOOM_SPLIT_CHARS}]+`]
@@ -239,6 +366,7 @@ var clean_up_tokenization = (text) => text.replace(/ \./g, ".").replace(/ \?/g,
239
366
  var create_pattern = (pattern, invert = true) => {
240
367
  if (pattern.Regex !== void 0) {
241
368
  let regex = pattern.Regex.replace(/\\([#&~])/g, "$1");
369
+ regex = regex.replace(/\\A/g, "^").replace(/\\z/g, "$").replace(/\\Z/g, "(?=\\r?\\n?$)");
242
370
  for (const [key, value] of PROBLEMATIC_REGEX_MAP) {
243
371
  regex = regex.replaceAll(key, value);
244
372
  }
@@ -5380,52 +5508,6 @@ var Callable2 = (
5380
5508
  }
5381
5509
  );
5382
5510
 
5383
- // src/utils/core.js
5384
- function dispatchCallback(progress_callback, data) {
5385
- if (progress_callback) progress_callback(data);
5386
- }
5387
- function isIntegralNumber(x) {
5388
- return Number.isInteger(x) || typeof x === "bigint";
5389
- }
5390
- function isNullishDimension(x) {
5391
- return x === null || x === void 0 || x === -1;
5392
- }
5393
- function calculateDimensions(arr) {
5394
- const dimensions = [];
5395
- let current = arr;
5396
- while (Array.isArray(current)) {
5397
- dimensions.push(current.length);
5398
- current = current[0];
5399
- }
5400
- return dimensions;
5401
- }
5402
- function mergeArrays(...arrs) {
5403
- return Array.prototype.concat.apply([], arrs);
5404
- }
5405
- function product(...a) {
5406
- return a.reduce((a2, b) => a2.flatMap((d) => b.map((e) => [d, e])));
5407
- }
5408
- function calculateReflectOffset(i, w) {
5409
- return Math.abs((i + w) % (2 * w) - w);
5410
- }
5411
- function pick(o, props) {
5412
- return Object.assign(
5413
- {},
5414
- ...props.map((prop) => {
5415
- if (o[prop] !== void 0) {
5416
- return { [prop]: o[prop] };
5417
- }
5418
- })
5419
- );
5420
- }
5421
- function count(arr, value) {
5422
- let count2 = 0;
5423
- for (const v of arr) {
5424
- if (v === value) ++count2;
5425
- }
5426
- return count2;
5427
- }
5428
-
5429
5511
  // src/utils/hub/files.js
5430
5512
  var CONTENT_TYPE_MAP = {
5431
5513
  txt: "text/plain",
@@ -5603,11 +5685,23 @@ var FileCache = class {
5603
5685
  throw error;
5604
5686
  }
5605
5687
  }
5688
+ /**
5689
+ * Deletes the cache entry for the given request.
5690
+ * @param {string} request
5691
+ * @returns {Promise<boolean>} A Promise that resolves to `true` if the cache entry was deleted, `false` otherwise.
5692
+ */
5693
+ async delete(request) {
5694
+ let filePath = node_path_default.join(this.path, request);
5695
+ try {
5696
+ await node_fs_default.promises.unlink(filePath);
5697
+ return true;
5698
+ } catch (error) {
5699
+ return false;
5700
+ }
5701
+ }
5606
5702
  // TODO add the rest?
5607
5703
  // addAll(requests: RequestInfo[]): Promise<void>;
5608
- // delete(request: RequestInfo | URL, options?: CacheQueryOptions): Promise<boolean>;
5609
5704
  // keys(request?: RequestInfo | URL, options?: CacheQueryOptions): Promise<ReadonlyArray<Request>>;
5610
- // match(request: RequestInfo | URL, options?: CacheQueryOptions): Promise<Response | undefined>;
5611
5705
  // matchAll(request?: RequestInfo | URL, options?: CacheQueryOptions): Promise<ReadonlyArray<Response>>;
5612
5706
  };
5613
5707
 
@@ -5669,12 +5763,12 @@ function handleError(status, remoteURL, fatal) {
5669
5763
  const message = ERROR_MAPPING[status] ?? `Error (${status}) occurred while trying to load file`;
5670
5764
  throw Error(`${message}: "${remoteURL}".`);
5671
5765
  }
5672
- async function readResponse(response, progress_callback) {
5766
+ async function readResponse(response, progress_callback, expectedSize) {
5673
5767
  const contentLength = response.headers.get("Content-Length");
5674
- if (contentLength === null) {
5675
- console.warn("Unable to determine content-length from response headers. Will expand buffer when needed.");
5768
+ let total = contentLength ? parseInt(contentLength, 10) : expectedSize ?? 0;
5769
+ if (contentLength === null && !expectedSize) {
5770
+ logger.warn("Unable to determine content-length from response headers. Will expand buffer when needed.");
5676
5771
  }
5677
- let total = parseInt(contentLength ?? "0");
5678
5772
  let buffer = new Uint8Array(total);
5679
5773
  let loaded = 0;
5680
5774
  const reader = response.body.getReader();
@@ -5719,7 +5813,7 @@ async function getCache(file_cache_dir = null) {
5719
5813
  try {
5720
5814
  cache = await caches.open(env.cacheKey);
5721
5815
  } catch (e) {
5722
- console.warn("An error occurred while opening the browser cache:", e);
5816
+ logger.warn("An error occurred while opening the browser cache:", e);
5723
5817
  }
5724
5818
  }
5725
5819
  if (!cache && env.useFSCache) {
@@ -5742,16 +5836,109 @@ async function tryCache(cache, ...names) {
5742
5836
  return void 0;
5743
5837
  }
5744
5838
 
5839
+ // src/utils/model_registry/get_file_metadata.js
5840
+ async function fetch_file_head(urlOrPath) {
5841
+ if (!isValidUrl(urlOrPath, ["http:", "https:"])) {
5842
+ return null;
5843
+ }
5844
+ const headers = getFetchHeaders(urlOrPath);
5845
+ headers.set("Range", "bytes=0-0");
5846
+ return env.fetch(urlOrPath, { method: "GET", headers });
5847
+ }
5848
+ async function get_file_metadata(path_or_repo_id, filename, options = {}) {
5849
+ const cache = await getCache(options?.cache_dir);
5850
+ const { localPath, remoteURL, proposedCacheKey, validModelId } = buildResourcePaths(
5851
+ path_or_repo_id,
5852
+ filename,
5853
+ options,
5854
+ cache
5855
+ );
5856
+ const cachedResponse = await checkCachedResource(cache, localPath, proposedCacheKey);
5857
+ if (cachedResponse !== void 0 && typeof cachedResponse !== "string") {
5858
+ const size = cachedResponse.headers.get("content-length");
5859
+ const contentType = cachedResponse.headers.get("content-type");
5860
+ return {
5861
+ exists: true,
5862
+ size: size ? parseInt(size, 10) : void 0,
5863
+ contentType: contentType || void 0,
5864
+ fromCache: true
5865
+ };
5866
+ }
5867
+ if (env.allowLocalModels) {
5868
+ const isURL = isValidUrl(localPath, ["http:", "https:"]);
5869
+ if (!isURL) {
5870
+ try {
5871
+ const response = await getFile(localPath);
5872
+ if (typeof response !== "string" && response.status !== 404) {
5873
+ const size = response.headers.get("content-length");
5874
+ const contentType = response.headers.get("content-type");
5875
+ return {
5876
+ exists: true,
5877
+ size: size ? parseInt(size, 10) : void 0,
5878
+ contentType: contentType || void 0,
5879
+ fromCache: false
5880
+ };
5881
+ }
5882
+ } catch (e) {
5883
+ }
5884
+ }
5885
+ }
5886
+ if (env.allowRemoteModels && !options.local_files_only && validModelId) {
5887
+ try {
5888
+ const rangeResponse = await fetch_file_head(remoteURL);
5889
+ if (rangeResponse && rangeResponse.status >= 200 && rangeResponse.status < 300) {
5890
+ let size;
5891
+ const contentType = rangeResponse.headers.get("content-type");
5892
+ if (rangeResponse.status === 206) {
5893
+ const contentRange = rangeResponse.headers.get("content-range");
5894
+ if (contentRange) {
5895
+ const match = contentRange.match(/bytes \d+-\d+\/(\d+)/);
5896
+ if (match) {
5897
+ size = parseInt(match[1], 10);
5898
+ }
5899
+ }
5900
+ } else if (rangeResponse.status === 200) {
5901
+ try {
5902
+ await rangeResponse.body?.cancel();
5903
+ } catch (cancelError) {
5904
+ }
5905
+ }
5906
+ if (size === void 0) {
5907
+ const contentLength = rangeResponse.headers.get("content-length");
5908
+ size = contentLength ? parseInt(contentLength, 10) : void 0;
5909
+ }
5910
+ return {
5911
+ exists: true,
5912
+ size,
5913
+ contentType: contentType || void 0,
5914
+ fromCache: false
5915
+ };
5916
+ }
5917
+ } catch (e) {
5918
+ logger.warn(`Unable to fetch file metadata for "${remoteURL}": ${e}`);
5919
+ }
5920
+ }
5921
+ return { exists: false, fromCache: false };
5922
+ }
5923
+
5745
5924
  // src/utils/hub.js
5746
5925
  async function getFile(urlOrPath) {
5747
5926
  if (env.useFS && !isValidUrl(urlOrPath, ["http:", "https:", "blob:"])) {
5748
5927
  return new FileResponse(
5749
5928
  urlOrPath instanceof URL ? urlOrPath.protocol === "file:" ? urlOrPath.pathname : urlOrPath.toString() : urlOrPath
5750
5929
  );
5751
- } else if (typeof process !== "undefined" && process?.release?.name === "node") {
5930
+ } else {
5931
+ return env.fetch(urlOrPath, {
5932
+ headers: getFetchHeaders(urlOrPath)
5933
+ });
5934
+ }
5935
+ }
5936
+ function getFetchHeaders(urlOrPath) {
5937
+ const isNode = typeof process !== "undefined" && process?.release?.name === "node";
5938
+ const headers = new Headers();
5939
+ if (isNode) {
5752
5940
  const IS_CI = !!process.env?.TESTING_REMOTELY;
5753
5941
  const version = env.version;
5754
- const headers = new Headers();
5755
5942
  headers.set("User-Agent", `transformers.js/${version}; is_ci/${IS_CI};`);
5756
5943
  const isHFURL = isValidUrl(urlOrPath, ["http:", "https:"], ["huggingface.co", "hf.co"]);
5757
5944
  if (isHFURL) {
@@ -5760,10 +5947,9 @@ async function getFile(urlOrPath) {
5760
5947
  headers.set("Authorization", `Bearer ${token}`);
5761
5948
  }
5762
5949
  }
5763
- return fetch(urlOrPath, { headers });
5764
5950
  } else {
5765
- return fetch(urlOrPath);
5766
5951
  }
5952
+ return headers;
5767
5953
  }
5768
5954
  function buildResourcePaths(path_or_repo_id, filename, options = {}, cache = null) {
5769
5955
  const revision = options.revision ?? "main";
@@ -5823,7 +6009,7 @@ async function storeCachedResource(path_or_repo_id, filename, cache, cacheKey, r
5823
6009
  }
5824
6010
  )
5825
6011
  ).catch((err) => {
5826
- console.warn(`Unable to add response to browser cache: ${err}.`);
6012
+ logger.warn(`Unable to add response to browser cache: ${err}.`);
5827
6013
  });
5828
6014
  }
5829
6015
  }
@@ -5847,7 +6033,7 @@ async function loadResourceFile(path_or_repo_id, filename, fatal = true, options
5847
6033
  response = await getFile(localPath);
5848
6034
  cacheKey = localPath;
5849
6035
  } catch (e) {
5850
- console.warn(`Unable to load from local path "${localPath}": "${e}"`);
6036
+ logger.warn(`Unable to load from local path "${localPath}": "${e}"`);
5851
6037
  }
5852
6038
  } else if (options.local_files_only) {
5853
6039
  throw new Error(`\`local_files_only=true\`, but attempted to load a remote file from: ${requestURL}.`);
@@ -5906,14 +6092,31 @@ async function loadResourceFile(path_or_repo_id, filename, fatal = true, options
5906
6092
  total: buffer.length
5907
6093
  });
5908
6094
  } else {
5909
- buffer = await readResponse(response, (data) => {
5910
- dispatchCallback(options.progress_callback, {
5911
- status: "progress",
5912
- name: path_or_repo_id,
5913
- file: filename,
5914
- ...data
5915
- });
5916
- });
6095
+ let expectedSize;
6096
+ const contentLength = response.headers.get("content-length");
6097
+ if (contentLength) {
6098
+ expectedSize = parseInt(contentLength, 10);
6099
+ } else {
6100
+ try {
6101
+ const metadata = await get_file_metadata(path_or_repo_id, filename, options);
6102
+ if (metadata.size) {
6103
+ expectedSize = metadata.size;
6104
+ }
6105
+ } catch (e) {
6106
+ }
6107
+ }
6108
+ buffer = await readResponse(
6109
+ response,
6110
+ (data) => {
6111
+ dispatchCallback(options.progress_callback, {
6112
+ status: "progress",
6113
+ name: path_or_repo_id,
6114
+ file: filename,
6115
+ ...data
6116
+ });
6117
+ },
6118
+ expectedSize
6119
+ );
5917
6120
  }
5918
6121
  }
5919
6122
  result = buffer;
@@ -6760,20 +6963,9 @@ var uint16_to_float32 = /* @__PURE__ */ (function() {
6760
6963
  // ignore-modules:onnxruntime-node
6761
6964
  var onnxruntime_node_exports = {};
6762
6965
  __export(onnxruntime_node_exports, {
6763
- Readable: () => Readable,
6764
- createReadStream: () => createReadStream,
6765
- createWriteStream: () => createWriteStream,
6766
- default: () => onnxruntime_node_default,
6767
- pipeline: () => pipeline
6966
+ default: () => onnxruntime_node_default
6768
6967
  });
6769
- var noop = () => {
6770
- };
6771
- var emptyObj4 = {};
6772
- var onnxruntime_node_default = emptyObj4;
6773
- var Readable = { fromWeb: noop };
6774
- var pipeline = noop;
6775
- var createWriteStream = noop;
6776
- var createReadStream = noop;
6968
+ var onnxruntime_node_default = {};
6777
6969
 
6778
6970
  // src/backends/onnx.js
6779
6971
  import * as ONNX_WEB from "onnxruntime-web/webgpu";
@@ -6791,9 +6983,9 @@ async function loadAndCacheFile(url) {
6791
6983
  }
6792
6984
  }
6793
6985
  } catch (error) {
6794
- console.warn(`Failed to load ${fileName} from cache:`, error);
6986
+ logger.warn(`Failed to load ${fileName} from cache:`, error);
6795
6987
  }
6796
- const response = await fetch(url);
6988
+ const response = await env.fetch(url);
6797
6989
  if (!response.ok) {
6798
6990
  throw new Error(`Failed to fetch ${fileName}: ${response.status} ${response.statusText}`);
6799
6991
  }
@@ -6801,7 +6993,7 @@ async function loadAndCacheFile(url) {
6801
6993
  try {
6802
6994
  await cache.put(url, response.clone());
6803
6995
  } catch (e) {
6804
- console.warn(`Failed to cache ${fileName}:`, e);
6996
+ logger.warn(`Failed to cache ${fileName}:`, e);
6805
6997
  }
6806
6998
  }
6807
6999
  return response;
@@ -6812,7 +7004,7 @@ async function loadWasmBinary(wasmURL) {
6812
7004
  try {
6813
7005
  return await response.arrayBuffer();
6814
7006
  } catch (error) {
6815
- console.warn("Failed to read WASM binary:", error);
7007
+ logger.warn("Failed to read WASM binary:", error);
6816
7008
  return null;
6817
7009
  }
6818
7010
  }
@@ -6822,11 +7014,12 @@ async function loadWasmFactory(libURL) {
6822
7014
  try {
6823
7015
  let code = await response.text();
6824
7016
  const baseUrl = libURL.split("/").slice(0, -1).join("/");
6825
- code = code.replace(/import\.meta\.url/g, `"${baseUrl}"`);
7017
+ code = code.replaceAll("import.meta.url", `"${baseUrl}"`);
7018
+ code = code.replaceAll("globalThis.process?.versions?.node", "false");
6826
7019
  const blob = new Blob([code], { type: "text/javascript" });
6827
7020
  return URL.createObjectURL(blob);
6828
7021
  } catch (error) {
6829
- console.warn("Failed to read WASM binary:", error);
7022
+ logger.warn("Failed to read WASM binary:", error);
6830
7023
  return null;
6831
7024
  }
6832
7025
  }
@@ -6873,8 +7066,26 @@ var DEVICE_TO_EXECUTION_PROVIDER_MAPPING = Object.freeze({
6873
7066
  "webnn-cpu": { name: "webnn", deviceType: "cpu" }
6874
7067
  // WebNN CPU
6875
7068
  });
6876
- var LOG_LEVELS = ["verbose", "info", "warning", "error", "fatal"];
6877
- var DEFAULT_LOG_LEVEL = 4;
7069
+ function getOnnxLogSeverityLevel(logLevel2) {
7070
+ if (logLevel2 <= LogLevel.DEBUG) {
7071
+ return 0;
7072
+ } else if (logLevel2 <= LogLevel.INFO) {
7073
+ return 2;
7074
+ } else if (logLevel2 <= LogLevel.WARNING) {
7075
+ return 3;
7076
+ } else if (logLevel2 <= LogLevel.ERROR) {
7077
+ return 3;
7078
+ } else {
7079
+ return 4;
7080
+ }
7081
+ }
7082
+ var ONNX_LOG_LEVEL_NAMES = {
7083
+ 0: "verbose",
7084
+ 1: "info",
7085
+ 2: "warning",
7086
+ 3: "error",
7087
+ 4: "fatal"
7088
+ };
6878
7089
  var supportedDevices = [];
6879
7090
  var defaultDevices;
6880
7091
  var ONNX;
@@ -6950,7 +7161,7 @@ async function ensureWasmLoaded() {
6950
7161
  ONNX_ENV.wasm.wasmBinary = wasmBinary;
6951
7162
  }
6952
7163
  } catch (err) {
6953
- console.warn("Failed to pre-load WASM binary:", err);
7164
+ logger.warn("Failed to pre-load WASM binary:", err);
6954
7165
  }
6955
7166
  })() : Promise.resolve(),
6956
7167
  // Load and cache the WASM factory
@@ -6961,7 +7172,7 @@ async function ensureWasmLoaded() {
6961
7172
  ONNX_ENV.wasm.wasmPaths.mjs = wasmFactoryBlob;
6962
7173
  }
6963
7174
  } catch (err) {
6964
- console.warn("Failed to pre-load WASM factory:", err);
7175
+ logger.warn("Failed to pre-load WASM factory:", err);
6965
7176
  }
6966
7177
  })() : Promise.resolve()
6967
7178
  ]);
@@ -6970,9 +7181,10 @@ async function ensureWasmLoaded() {
6970
7181
  }
6971
7182
  async function createInferenceSession(buffer_or_path, session_options, session_config) {
6972
7183
  await ensureWasmLoaded();
7184
+ const logSeverityLevel = getOnnxLogSeverityLevel(env.logLevel ?? LogLevel.WARNING);
6973
7185
  const load = () => InferenceSession.create(buffer_or_path, {
6974
- // Set default log level, but allow overriding through session options
6975
- logSeverityLevel: DEFAULT_LOG_LEVEL,
7186
+ // Set default log severity level, but allow overriding through session options
7187
+ logSeverityLevel,
6976
7188
  ...session_options
6977
7189
  });
6978
7190
  const session = await (IS_WEB_ENV ? webInitChain = webInitChain.then(load) : load());
@@ -6989,7 +7201,6 @@ function isONNXTensor(x) {
6989
7201
  return x instanceof ONNX.Tensor;
6990
7202
  }
6991
7203
  var ONNX_ENV = ONNX?.env;
6992
- ONNX_ENV.logLevel = LOG_LEVELS[DEFAULT_LOG_LEVEL];
6993
7204
  if (ONNX_ENV?.wasm) {
6994
7205
  if (
6995
7206
  // @ts-ignore Cannot find name 'ServiceWorkerGlobalScope'.ts(2304)
@@ -7012,7 +7223,15 @@ if (ONNX_ENV?.webgpu) {
7012
7223
  function isONNXProxy() {
7013
7224
  return ONNX_ENV?.wasm?.proxy;
7014
7225
  }
7015
- env.backends.onnx = ONNX_ENV;
7226
+ function setLogLevel(logLevel2) {
7227
+ const severityLevel = getOnnxLogSeverityLevel(logLevel2);
7228
+ ONNX_ENV.logLevel = ONNX_LOG_LEVEL_NAMES[severityLevel];
7229
+ }
7230
+ setLogLevel(env.logLevel ?? LogLevel.WARNING);
7231
+ env.backends.onnx = {
7232
+ ...ONNX_ENV,
7233
+ setLogLevel
7234
+ };
7016
7235
 
7017
7236
  // src/ops/registry.js
7018
7237
  var wrap = async (session_bytes, session_options, names) => {
@@ -8099,6 +8318,16 @@ var DEVICE_TYPES = Object.freeze({
8099
8318
  "webnn-cpu": "webnn-cpu"
8100
8319
  // WebNN CPU
8101
8320
  });
8321
+ var DEFAULT_DEVICE = apis.IS_NODE_ENV ? "cpu" : "wasm";
8322
+ function selectDevice(deviceConfig, fileName, { warn } = {}) {
8323
+ if (!deviceConfig) return DEFAULT_DEVICE;
8324
+ if (typeof deviceConfig === "string") return deviceConfig;
8325
+ if (deviceConfig.hasOwnProperty(fileName)) return deviceConfig[fileName];
8326
+ if (warn) {
8327
+ warn(`device not specified for "${fileName}". Using the default device (${DEFAULT_DEVICE}).`);
8328
+ }
8329
+ return DEFAULT_DEVICE;
8330
+ }
8102
8331
 
8103
8332
  // src/utils/dtypes.js
8104
8333
  var isWebGpuFp16Supported = /* @__PURE__ */ (function() {
@@ -8132,6 +8361,7 @@ var DATA_TYPES = Object.freeze({
8132
8361
  q4f16: "q4f16"
8133
8362
  // fp16 model with int4 block weight quantization
8134
8363
  });
8364
+ var DEFAULT_DEVICE_DTYPE = DATA_TYPES.fp32;
8135
8365
  var DEFAULT_DEVICE_DTYPE_MAPPING = Object.freeze({
8136
8366
  // NOTE: If not specified, will default to fp32
8137
8367
  [DEVICE_TYPES.wasm]: DATA_TYPES.q8
@@ -8146,6 +8376,45 @@ var DEFAULT_DTYPE_SUFFIX_MAPPING = Object.freeze({
8146
8376
  [DATA_TYPES.q4f16]: "_q4f16",
8147
8377
  [DATA_TYPES.bnb4]: "_bnb4"
8148
8378
  });
8379
+ function selectDtype(dtype, fileName, selectedDevice, { configDtype = null, warn } = {}) {
8380
+ let resolved;
8381
+ let needsWarn = false;
8382
+ if (dtype && typeof dtype !== "string") {
8383
+ if (dtype.hasOwnProperty(fileName)) {
8384
+ resolved = dtype[fileName];
8385
+ } else {
8386
+ resolved = null;
8387
+ needsWarn = true;
8388
+ }
8389
+ } else {
8390
+ resolved = /** @type {string|null|undefined} */
8391
+ dtype;
8392
+ }
8393
+ let result;
8394
+ if (resolved === DATA_TYPES.auto) {
8395
+ if (configDtype) {
8396
+ const fallback = typeof configDtype === "string" ? configDtype : configDtype?.[fileName];
8397
+ if (fallback && fallback !== DATA_TYPES.auto && DATA_TYPES.hasOwnProperty(fallback)) {
8398
+ return (
8399
+ /** @type {DataType} */
8400
+ fallback
8401
+ );
8402
+ }
8403
+ }
8404
+ result = DEFAULT_DEVICE_DTYPE_MAPPING[selectedDevice] ?? DEFAULT_DEVICE_DTYPE;
8405
+ } else if (resolved && DATA_TYPES.hasOwnProperty(resolved)) {
8406
+ result = /** @type {DataType} */
8407
+ resolved;
8408
+ } else {
8409
+ result = DEFAULT_DEVICE_DTYPE_MAPPING[selectedDevice] ?? DEFAULT_DEVICE_DTYPE;
8410
+ }
8411
+ if (needsWarn && warn) {
8412
+ warn(
8413
+ `dtype not specified for "${fileName}". Using the default dtype (${result}) for this device (${selectedDevice}).`
8414
+ );
8415
+ }
8416
+ return result;
8417
+ }
8149
8418
  var DataTypeMap = Object.freeze({
8150
8419
  float32: Float32Array,
8151
8420
  // @ts-ignore ts(2552) Limited availability of Float16Array across browsers:
@@ -8167,6 +8436,167 @@ var DataTypeMap = Object.freeze({
8167
8436
  int4: Int8Array
8168
8437
  });
8169
8438
 
8439
+ // src/utils/random.js
8440
+ var Random = class {
8441
+ constructor(seed) {
8442
+ this._mt = new Uint32Array(624);
8443
+ this._idx = 625;
8444
+ this._gauss_next = null;
8445
+ this._random_fn = this.random.bind(this);
8446
+ this.seed(seed);
8447
+ }
8448
+ /**
8449
+ * Seeds this instance's PRNG.
8450
+ *
8451
+ * When called with a number, initializes the state deterministically from that value.
8452
+ * When called with no arguments (or `undefined`/`null`), seeds from OS entropy
8453
+ * via `crypto.getRandomValues`, matching Python's `random.seed()` behaviour.
8454
+ *
8455
+ * @param {number} [n] The seed value. Omit to seed from OS entropy.
8456
+ */
8457
+ seed(n) {
8458
+ if (n === void 0 || n === null) {
8459
+ if (apis.IS_CRYPTO_AVAILABLE) {
8460
+ const buf = new Uint32Array(1);
8461
+ crypto.getRandomValues(buf);
8462
+ n = buf[0];
8463
+ } else {
8464
+ n = Date.now() >>> 0;
8465
+ }
8466
+ }
8467
+ const mt = this._mt;
8468
+ const u = (a, b) => Math.imul(a, b) >>> 0, key = [];
8469
+ for (let v = n || 0; v > 0; v = Math.floor(v / 4294967296)) key.push(v & 4294967295);
8470
+ if (!key.length) key.push(0);
8471
+ mt[0] = 19650218;
8472
+ for (let k = 1; k < 624; ++k) mt[k] = u(1812433253, mt[k - 1] ^ mt[k - 1] >>> 30) + k >>> 0;
8473
+ let i = 1, j = 0;
8474
+ for (let k = Math.max(624, key.length); k > 0; --k, ++i, ++j) {
8475
+ if (i >= 624) {
8476
+ mt[0] = mt[623];
8477
+ i = 1;
8478
+ }
8479
+ if (j >= key.length) j = 0;
8480
+ mt[i] = (mt[i] ^ u(mt[i - 1] ^ mt[i - 1] >>> 30, 1664525)) + key[j] + j >>> 0;
8481
+ }
8482
+ for (let k = 623; k > 0; --k, ++i) {
8483
+ if (i >= 624) {
8484
+ mt[0] = mt[623];
8485
+ i = 1;
8486
+ }
8487
+ mt[i] = (mt[i] ^ u(mt[i - 1] ^ mt[i - 1] >>> 30, 1566083941)) - i >>> 0;
8488
+ }
8489
+ mt[0] = 2147483648;
8490
+ this._idx = 624;
8491
+ this._gauss_next = null;
8492
+ }
8493
+ /**
8494
+ * Generates a random unsigned 32-bit integer.
8495
+ *
8496
+ * Performs the "twist" step when the state buffer is exhausted,
8497
+ * then applies the standard MT19937 tempering transform.
8498
+ *
8499
+ * @returns {number} A random integer in the range [0, 2^32 - 1].
8500
+ */
8501
+ _int32() {
8502
+ const mt = this._mt;
8503
+ if (this._idx >= 624) {
8504
+ for (let k = 0; k < 624; ++k) {
8505
+ const y2 = mt[k] & 2147483648 | mt[(k + 1) % 624] & 2147483647;
8506
+ mt[k] = (mt[(k + 397) % 624] ^ y2 >>> 1 ^ (y2 & 1 ? 2567483615 : 0)) >>> 0;
8507
+ }
8508
+ this._idx = 0;
8509
+ }
8510
+ let y = mt[this._idx++];
8511
+ y ^= y >>> 11;
8512
+ y ^= y << 7 & 2636928640;
8513
+ y ^= y << 15 & 4022730752;
8514
+ y ^= y >>> 18;
8515
+ return y >>> 0;
8516
+ }
8517
+ /**
8518
+ * Generates a random floating-point number in the half-open interval [0, 1).
8519
+ *
8520
+ * Combines two 32-bit integers (using 53 bits of precision) to produce
8521
+ * a uniformly distributed double, matching Python's `random.random()`.
8522
+ *
8523
+ * @returns {number} A random float in [0, 1).
8524
+ */
8525
+ random() {
8526
+ return ((this._int32() >>> 5) * 67108864 + (this._int32() >>> 6)) / 9007199254740992;
8527
+ }
8528
+ /**
8529
+ * Generates a random number from a Gaussian (normal) distribution.
8530
+ *
8531
+ * Uses the Box-Muller transform with a cached spare value,
8532
+ * matching Python's `random.gauss()` output for the same seed.
8533
+ *
8534
+ * @param {number} [mu=0] The mean of the distribution.
8535
+ * @param {number} [sigma=1] The standard deviation of the distribution.
8536
+ * @returns {number} A normally distributed random value.
8537
+ */
8538
+ gauss(mu = 0, sigma = 1) {
8539
+ let z = this._gauss_next;
8540
+ this._gauss_next = null;
8541
+ if (z === null) {
8542
+ const x2pi = this.random() * 2 * Math.PI, g2rad = Math.sqrt(-2 * Math.log(1 - this.random()));
8543
+ z = Math.cos(x2pi) * g2rad;
8544
+ this._gauss_next = Math.sin(x2pi) * g2rad;
8545
+ }
8546
+ return mu + z * sigma;
8547
+ }
8548
+ /**
8549
+ * Shuffles an array in-place using the Fisher-Yates algorithm.
8550
+ *
8551
+ * Uses rejection sampling via `getrandbits`-style bit masking to ensure
8552
+ * a uniform distribution, matching Python's `random.shuffle()`.
8553
+ *
8554
+ * @param {any[]} arr The array to shuffle in-place.
8555
+ */
8556
+ shuffle(arr) {
8557
+ for (let i = arr.length - 1; i > 0; --i) {
8558
+ const k = 32 - Math.clz32(i + 1);
8559
+ let r = this._int32() >>> 32 - k;
8560
+ while (r > i) r = this._int32() >>> 32 - k;
8561
+ const t = arr[i];
8562
+ arr[i] = arr[r];
8563
+ arr[r] = t;
8564
+ }
8565
+ }
8566
+ /**
8567
+ * Selects a single element from a weighted population.
8568
+ *
8569
+ * Matches Python's `random.choices(population, weights=weights, k=1)[0]`
8570
+ *
8571
+ * @param {any[]} population The array of items to choose from.
8572
+ * @param {number[]} weights An array of non-negative weights, one per population element.
8573
+ * @returns {*} A single randomly selected element from the population.
8574
+ */
8575
+ choices(population, weights) {
8576
+ return population[_weightedIndexWith(this._random_fn, weights)];
8577
+ }
8578
+ };
8579
+ function _weightedIndexWith(randomFn, weights) {
8580
+ let sum = 0;
8581
+ for (let i = 0; i < weights.length; ++i) sum += weights[i];
8582
+ let x = randomFn() * sum;
8583
+ for (let i = 0; i < weights.length; ++i) {
8584
+ x -= weights[i];
8585
+ if (x < 0) return i;
8586
+ }
8587
+ return weights.length - 1;
8588
+ }
8589
+ var _default = new Random();
8590
+ var random = Object.freeze({
8591
+ Random,
8592
+ seed: _default.seed.bind(_default),
8593
+ random: _default.random.bind(_default),
8594
+ gauss: _default.gauss.bind(_default),
8595
+ shuffle: _default.shuffle.bind(_default),
8596
+ choices: _default.choices.bind(_default)
8597
+ });
8598
+ var _weightedIndex = (weights) => _weightedIndexWith(random.random, weights);
8599
+
8170
8600
  // src/utils/tensor.js
8171
8601
  var Tensor2 = class _Tensor {
8172
8602
  /**
@@ -9297,24 +9727,17 @@ function rand(size) {
9297
9727
  const length = size.reduce((a, b) => a * b, 1);
9298
9728
  return new Tensor2(
9299
9729
  "float32",
9300
- Float32Array.from({ length }, () => Math.random()),
9730
+ Float32Array.from({ length }, () => random.random()),
9301
9731
  size
9302
9732
  );
9303
9733
  }
9304
9734
  function randn(size) {
9305
9735
  const length = size.reduce((a, b) => a * b, 1);
9306
- const data = new Float32Array(length);
9307
- for (let i = 0; i < length; i += 2) {
9308
- const u = 1 - Math.random();
9309
- const v = Math.random();
9310
- const mag = Math.sqrt(-2 * Math.log(u));
9311
- const angle = 2 * Math.PI * v;
9312
- data[i] = mag * Math.cos(angle);
9313
- if (i + 1 < length) {
9314
- data[i + 1] = mag * Math.sin(angle);
9315
- }
9316
- }
9317
- return new Tensor2("float32", data, size);
9736
+ return new Tensor2(
9737
+ "float32",
9738
+ Float32Array.from({ length }, () => random.gauss()),
9739
+ size
9740
+ );
9318
9741
  }
9319
9742
  function quantize_embeddings(tensor, precision) {
9320
9743
  if (tensor.dims.length !== 2) {
@@ -9343,13 +9766,24 @@ function quantize_embeddings(tensor, precision) {
9343
9766
  return new Tensor2(dtype, outputData, [tensor.dims[0], tensor.dims[1] / 8]);
9344
9767
  }
9345
9768
 
9769
+ // src/utils/model_registry/get_tokenizer_files.js
9770
+ async function get_tokenizer_files(modelId) {
9771
+ if (!modelId) {
9772
+ throw new Error("modelId is required for get_tokenizer_files");
9773
+ }
9774
+ const metadata = await get_file_metadata(modelId, "tokenizer_config.json", {});
9775
+ if (metadata.exists) {
9776
+ return ["tokenizer.json", "tokenizer_config.json"];
9777
+ }
9778
+ return [];
9779
+ }
9780
+
9346
9781
  // src/tokenization_utils.js
9347
9782
  async function loadTokenizer(pretrained_model_name_or_path, options) {
9348
- const info = await Promise.all([
9349
- getModelJSON(pretrained_model_name_or_path, "tokenizer.json", true, options),
9350
- getModelJSON(pretrained_model_name_or_path, "tokenizer_config.json", true, options)
9351
- ]);
9352
- return info;
9783
+ const tokenizerFiles = await get_tokenizer_files(pretrained_model_name_or_path);
9784
+ return await Promise.all(
9785
+ tokenizerFiles.map((file) => getModelJSON(pretrained_model_name_or_path, file, true, options))
9786
+ );
9353
9787
  }
9354
9788
  function prepareTensorForDecode(tensor) {
9355
9789
  const dims = tensor.dims;
@@ -9523,10 +9957,10 @@ var PreTrainedTokenizer = class extends Callable2 {
9523
9957
  * @param {string|string[]} [options.text_pair=null] Optional second sequence to be encoded. If set, must be the same type as text.
9524
9958
  * @param {boolean|'max_length'} [options.padding=false] Whether to pad the input sequences.
9525
9959
  * @param {boolean} [options.add_special_tokens=true] Whether or not to add the special tokens associated with the corresponding model.
9526
- * @param {boolean} [options.truncation=null] Whether to truncate the input sequences.
9527
- * @param {number} [options.max_length=null] Maximum length of the returned list and optionally padding length.
9960
+ * @param {boolean|null} [options.truncation=null] Whether to truncate the input sequences.
9961
+ * @param {number|null} [options.max_length=null] Maximum length of the returned list and optionally padding length.
9528
9962
  * @param {boolean} [options.return_tensor=true] Whether to return the results as Tensors or arrays.
9529
- * @param {boolean} [options.return_token_type_ids=null] Whether to return the token type ids.
9963
+ * @param {boolean|null} [options.return_token_type_ids=null] Whether to return the token type ids.
9530
9964
  * @returns {BatchEncoding} Object to be passed to the model.
9531
9965
  */
9532
9966
  _call(text, {
@@ -9572,12 +10006,12 @@ var PreTrainedTokenizer = class extends Callable2 {
9572
10006
  max_length = this.model_max_length;
9573
10007
  } else if (truncation === null) {
9574
10008
  if (padding === true) {
9575
- console.warn(
10009
+ logger.warn(
9576
10010
  "`max_length` is ignored when `padding: true` and there is no truncation strategy. To pad to max length, use `padding: 'max_length'`."
9577
10011
  );
9578
10012
  max_length = this.model_max_length;
9579
10013
  } else if (padding === false) {
9580
- console.warn(
10014
+ logger.warn(
9581
10015
  "Truncation was not explicitly activated but `max_length` is provided a specific value, please use `truncation: true` to explicitly truncate examples to max length."
9582
10016
  );
9583
10017
  truncation = true;
@@ -9661,9 +10095,9 @@ var PreTrainedTokenizer = class extends Callable2 {
9661
10095
  *
9662
10096
  * @param {string} text The text to encode.
9663
10097
  * @param {Object} options An optional object containing the following properties:
9664
- * @param {string} [options.text_pair=null] The optional second text to encode.
10098
+ * @param {string|null} [options.text_pair=null] The optional second text to encode.
9665
10099
  * @param {boolean} [options.add_special_tokens=true] Whether or not to add the special tokens associated with the corresponding model.
9666
- * @param {boolean} [options.return_token_type_ids=null] Whether to return token_type_ids.
10100
+ * @param {boolean|null} [options.return_token_type_ids=null] Whether to return token_type_ids.
9667
10101
  * @returns {{input_ids: number[], attention_mask: number[], token_type_ids?: number[]}} An object containing the encoded text.
9668
10102
  * @private
9669
10103
  */
@@ -9683,7 +10117,7 @@ var PreTrainedTokenizer = class extends Callable2 {
9683
10117
  * Converts a string into a sequence of tokens.
9684
10118
  * @param {string} text The sequence to be encoded.
9685
10119
  * @param {Object} options An optional object containing the following properties:
9686
- * @param {string} [options.pair] A second sequence to be encoded with the first.
10120
+ * @param {string|null} [options.pair] A second sequence to be encoded with the first.
9687
10121
  * @param {boolean} [options.add_special_tokens=false] Whether or not to add the special tokens associated with the corresponding model.
9688
10122
  * @returns {string[]} The list of tokens.
9689
10123
  */
@@ -9695,9 +10129,9 @@ var PreTrainedTokenizer = class extends Callable2 {
9695
10129
  *
9696
10130
  * @param {string} text The text to encode.
9697
10131
  * @param {Object} options An optional object containing the following properties:
9698
- * @param {string} [options.text_pair=null] The optional second text to encode.
10132
+ * @param {string|null} [options.text_pair=null] The optional second text to encode.
9699
10133
  * @param {boolean} [options.add_special_tokens=true] Whether or not to add the special tokens associated with the corresponding model.
9700
- * @param {boolean} [options.return_token_type_ids=null] Whether to return token_type_ids.
10134
+ * @param {boolean|null} [options.return_token_type_ids=null] Whether to return token_type_ids.
9701
10135
  * @returns {number[]} An array of token IDs representing the encoded text(s).
9702
10136
  */
9703
10137
  encode(text, { text_pair = null, add_special_tokens = true, return_token_type_ids = null } = {}) {
@@ -9744,7 +10178,7 @@ var PreTrainedTokenizer = class extends Callable2 {
9744
10178
  * @param {number[]|bigint[]} token_ids List of token ids to decode
9745
10179
  * @param {Object} decode_args Optional arguments for decoding
9746
10180
  * @param {boolean} [decode_args.skip_special_tokens=false] Whether to skip special tokens during decoding
9747
- * @param {boolean} [decode_args.clean_up_tokenization_spaces=null] Whether to clean up tokenization spaces during decoding.
10181
+ * @param {boolean|null} [decode_args.clean_up_tokenization_spaces=null] Whether to clean up tokenization spaces during decoding.
9748
10182
  * 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`.
9749
10183
  * @returns {string} The decoded string
9750
10184
  */
@@ -9760,7 +10194,7 @@ var PreTrainedTokenizer = class extends Callable2 {
9760
10194
  * template for better generation tracking.
9761
10195
  *
9762
10196
  * @param {Object} options An optional object containing the following properties:
9763
- * @param {string} [options.chat_template=null]
10197
+ * @param {string|null} [options.chat_template=null]
9764
10198
  * A Jinja template or the name of a template to use for this conversion.
9765
10199
  * It is usually not necessary to pass anything to this argument,
9766
10200
  * as the model's template will be used by default.
@@ -9829,7 +10263,7 @@ var PreTrainedTokenizer = class extends Callable2 {
9829
10263
  * @param {Message[]} conversation A list of message objects with `"role"` and `"content"` keys,
9830
10264
  * representing the chat history so far.
9831
10265
  * @param {Object} options An optional object containing the following properties:
9832
- * @param {string} [options.chat_template=null] A Jinja template to use for this conversion. If
10266
+ * @param {string|null} [options.chat_template=null] A Jinja template to use for this conversion. If
9833
10267
  * this is not passed, the model's chat template will be used instead.
9834
10268
  * @param {Object[]} [options.tools=null]
9835
10269
  * A list of tools (callable functions) that will be accessible to the model. If the template does not
@@ -9850,7 +10284,7 @@ var PreTrainedTokenizer = class extends Callable2 {
9850
10284
  * @param {boolean} [options.tokenize=true] Whether to tokenize the output. If false, the output will be a string.
9851
10285
  * @param {boolean} [options.padding=false] Whether to pad sequences to the maximum length. Has no effect if tokenize is false.
9852
10286
  * @param {boolean} [options.truncation=false] Whether to truncate sequences to the maximum length. Has no effect if tokenize is false.
9853
- * @param {number} [options.max_length=null] Maximum length (in tokens) to use for padding or truncation. Has no effect if tokenize is false.
10287
+ * @param {number|null} [options.max_length=null] Maximum length (in tokens) to use for padding or truncation. Has no effect if tokenize is false.
9854
10288
  * If not specified, the tokenizer's `max_length` attribute will be used as a default.
9855
10289
  * @param {boolean} [options.return_tensor=true] Whether to return the output as a Tensor or an Array. Has no effect if tokenize is false.
9856
10290
  * @param {boolean} [options.return_dict=true] Whether to return a dictionary with named outputs. Has no effect if tokenize is false.
@@ -10128,7 +10562,7 @@ var MarianTokenizer = class extends PreTrainedTokenizer {
10128
10562
  super(tokenizerJSON, tokenizerConfig);
10129
10563
  this.languageRegex = /^(>>\w+<<)\s*/g;
10130
10564
  this.supported_language_codes = Array.from(this.get_vocab().keys()).filter((x) => this.languageRegex.test(x));
10131
- console.warn(
10565
+ logger.warn(
10132
10566
  'WARNING: `MarianTokenizer` is not yet supported by Hugging Face\'s "fast" tokenizers library. Therefore, you may experience slightly inaccurate results.'
10133
10567
  );
10134
10568
  }
@@ -10148,7 +10582,7 @@ var MarianTokenizer = class extends PreTrainedTokenizer {
10148
10582
  } else if (remainder.length === 2) {
10149
10583
  const [language, text2] = remainder;
10150
10584
  if (!this.supported_language_codes.includes(language)) {
10151
- console.warn(
10585
+ logger.warn(
10152
10586
  `Unsupported language code "${language}" detected, which may lead to unexpected behavior. Should be one of: ${JSON.stringify(this.supported_language_codes)}`
10153
10587
  );
10154
10588
  }
@@ -10865,7 +11299,7 @@ var XLMTokenizer = class extends PreTrainedTokenizer {
10865
11299
  return_token_type_ids = true;
10866
11300
  constructor(tokenizerJSON, tokenizerConfig) {
10867
11301
  super(tokenizerJSON, tokenizerConfig);
10868
- console.warn(
11302
+ logger.warn(
10869
11303
  'WARNING: `XLMTokenizer` is not yet supported by Hugging Face\'s "fast" tokenizers library. Therefore, you may experience slightly inaccurate results.'
10870
11304
  );
10871
11305
  }
@@ -10899,7 +11333,7 @@ var AutoTokenizer = class {
10899
11333
  const tokenizerName = tokenizerConfig.tokenizer_class?.replace(/Fast$/, "") ?? "PreTrainedTokenizer";
10900
11334
  let cls = tokenizers_exports[tokenizerName];
10901
11335
  if (!cls) {
10902
- console.warn(`Unknown tokenizer class "${tokenizerName}", attempting to construct from base class.`);
11336
+ logger.warn(`Unknown tokenizer class "${tokenizerName}", attempting to construct from base class.`);
10903
11337
  cls = PreTrainedTokenizer;
10904
11338
  }
10905
11339
  return new cls(tokenizerJSON, tokenizerConfig);
@@ -11048,6 +11482,8 @@ __export(processors_exports, {
11048
11482
  Processor: () => Processor,
11049
11483
  PyAnnoteProcessor: () => PyAnnoteProcessor,
11050
11484
  Qwen2VLProcessor: () => Qwen2VLProcessor,
11485
+ Qwen2_5_VLProcessor: () => Qwen2_5_VLProcessor,
11486
+ Qwen3VLProcessor: () => Qwen3VLProcessor,
11051
11487
  Sam2Processor: () => Sam2Processor,
11052
11488
  Sam2VideoProcessor: () => Sam2VideoProcessor,
11053
11489
  SamProcessor: () => SamProcessor,
@@ -11122,14 +11558,14 @@ __export(feature_extractors_exports, {
11122
11558
  });
11123
11559
 
11124
11560
  // ignore-modules:node:stream
11125
- var noop2 = () => {
11561
+ var noop = () => {
11126
11562
  };
11127
- var Readable2 = { fromWeb: noop2 };
11563
+ var Readable = { fromWeb: noop };
11128
11564
 
11129
11565
  // ignore-modules:node:stream/promises
11130
- var noop3 = () => {
11566
+ var noop2 = () => {
11131
11567
  };
11132
- var pipeline2 = noop3;
11568
+ var pipeline = noop2;
11133
11569
 
11134
11570
  // src/utils/io.js
11135
11571
  async function saveBlob(path, blob) {
@@ -11146,9 +11582,9 @@ async function saveBlob(path, blob) {
11146
11582
  URL.revokeObjectURL(dataURL);
11147
11583
  } else if (apis.IS_FS_AVAILABLE) {
11148
11584
  const webStream = blob.stream();
11149
- const nodeStream = Readable2.fromWeb(webStream);
11585
+ const nodeStream = Readable.fromWeb(webStream);
11150
11586
  const fileStream = node_fs_default.createWriteStream(path);
11151
- await pipeline2(nodeStream, fileStream);
11587
+ await pipeline(nodeStream, fileStream);
11152
11588
  } else {
11153
11589
  throw new Error("Unable to save because filesystem is disabled in this environment.");
11154
11590
  }
@@ -11164,7 +11600,7 @@ async function read_audio(url, sampling_rate) {
11164
11600
  const response = await (await getFile(url)).arrayBuffer();
11165
11601
  const audioCTX = new AudioContext({ sampleRate: sampling_rate });
11166
11602
  if (typeof sampling_rate === "undefined") {
11167
- console.warn(`No sampling rate provided, using default of ${audioCTX.sampleRate}Hz.`);
11603
+ logger.warn(`No sampling rate provided, using default of ${audioCTX.sampleRate}Hz.`);
11168
11604
  }
11169
11605
  const decoded = await audioCTX.decodeAudioData(response);
11170
11606
  let audio;
@@ -11820,7 +12256,7 @@ var ClapFeatureExtractor = class extends FeatureExtractor {
11820
12256
  if (diff > 0) {
11821
12257
  if (truncation === "rand_trunc") {
11822
12258
  longer = true;
11823
- const idx = Math.floor(Math.random() * (diff + 1));
12259
+ const idx = Math.floor(random.random() * (diff + 1));
11824
12260
  waveform = waveform.subarray(idx, idx + max_length);
11825
12261
  input_mel = await this._extract_fbank_features(
11826
12262
  waveform,
@@ -12553,7 +12989,7 @@ var WhisperFeatureExtractor = class extends FeatureExtractor {
12553
12989
  const length = max_length ?? this.config.n_samples;
12554
12990
  if (audio.length > length) {
12555
12991
  if (audio.length > this.config.n_samples) {
12556
- console.warn(
12992
+ logger.warn(
12557
12993
  "Attempting to extract features for audio longer than 30 seconds. If using a pipeline to extract transcript from a long audio clip, remember to specify `chunk_length_s` and/or `stride_length_s`."
12558
12994
  );
12559
12995
  }
@@ -12600,8 +13036,7 @@ var ChatterboxProcessor = class extends Processor {
12600
13036
  };
12601
13037
 
12602
13038
  // ignore-modules:sharp
12603
- var emptyObj5 = {};
12604
- var sharp_default = emptyObj5;
13039
+ var sharp_default = {};
12605
13040
 
12606
13041
  // src/utils/image.js
12607
13042
  var createCanvasFunction;
@@ -12924,7 +13359,7 @@ var RawImage = class _RawImage {
12924
13359
  case "box":
12925
13360
  case "hamming":
12926
13361
  if (resampleMethod === "box" || resampleMethod === "hamming") {
12927
- console.warn(
13362
+ logger.warn(
12928
13363
  `Resampling method ${resampleMethod} is not yet supported. Using bilinear instead.`
12929
13364
  );
12930
13365
  resampleMethod = "bilinear";
@@ -13423,30 +13858,9 @@ function compute_segments(mask_probs, pred_scores, pred_labels, mask_threshold,
13423
13858
  }
13424
13859
  return [segmentation, segments];
13425
13860
  }
13426
- function smart_resize(height, width, factor = 28, min_pixels = 56 * 56, max_pixels = 14 * 14 * 4 * 1280) {
13427
- if (height < factor || width < factor) {
13428
- throw new Error(`height:${height} or width:${width} must be larger than factor:${factor}`);
13429
- } else if (Math.max(height, width) / Math.min(height, width) > 200) {
13430
- throw new Error(
13431
- `absolute aspect ratio must be smaller than 200, got ${Math.max(height, width) / Math.min(height, width)}`
13432
- );
13433
- }
13434
- let h_bar = Math.round(height / factor) * factor;
13435
- let w_bar = Math.round(width / factor) * factor;
13436
- if (h_bar * w_bar > max_pixels) {
13437
- const beta = Math.sqrt(height * width / max_pixels);
13438
- h_bar = Math.floor(height / beta / factor) * factor;
13439
- w_bar = Math.floor(width / beta / factor) * factor;
13440
- } else if (h_bar * w_bar < min_pixels) {
13441
- const beta = Math.sqrt(min_pixels / (height * width));
13442
- h_bar = Math.ceil(height * beta / factor) * factor;
13443
- w_bar = Math.ceil(width * beta / factor) * factor;
13444
- }
13445
- return [h_bar, w_bar];
13446
- }
13447
13861
  function post_process_panoptic_segmentation(outputs, threshold = 0.5, mask_threshold = 0.5, overlap_mask_area_threshold = 0.8, label_ids_to_fuse = null, target_sizes = null) {
13448
13862
  if (label_ids_to_fuse === null) {
13449
- console.warn("`label_ids_to_fuse` unset. No instance will be fused.");
13863
+ logger.warn("`label_ids_to_fuse` unset. No instance will be fused.");
13450
13864
  label_ids_to_fuse = /* @__PURE__ */ new Set();
13451
13865
  }
13452
13866
  const class_queries_logits = outputs.class_queries_logits ?? outputs.logits;
@@ -13713,9 +14127,6 @@ var ImageProcessor = class extends Callable2 {
13713
14127
  return [newWidth, newHeight];
13714
14128
  } else if (this.size_divisibility !== void 0) {
13715
14129
  return enforce_size_divisibility([srcWidth, srcHeight], this.size_divisibility);
13716
- } else if (this.min_pixels !== void 0 && this.max_pixels !== void 0) {
13717
- const factor = this.config.patch_size * this.config.merge_size;
13718
- return smart_resize(srcHeight, srcWidth, factor, this.min_pixels, this.max_pixels);
13719
14130
  } else {
13720
14131
  throw new Error(
13721
14132
  `Could not resize image due to unsupported \`this.size\` option in config: ${JSON.stringify(size)}`
@@ -14539,7 +14950,40 @@ var PvtImageProcessor = class extends ImageProcessor {
14539
14950
  };
14540
14951
 
14541
14952
  // src/models/qwen2_vl/image_processing_qwen2_vl.js
14953
+ function smart_resize(height, width, factor = 28, min_pixels = 56 * 56, max_pixels = 14 * 14 * 4 * 1280) {
14954
+ if (height < factor || width < factor) {
14955
+ throw new Error(`height:${height} or width:${width} must be larger than factor:${factor}`);
14956
+ } else if (Math.max(height, width) / Math.min(height, width) > 200) {
14957
+ throw new Error(
14958
+ `absolute aspect ratio must be smaller than 200, got ${Math.max(height, width) / Math.min(height, width)}`
14959
+ );
14960
+ }
14961
+ let h_bar = Math.round(height / factor) * factor;
14962
+ let w_bar = Math.round(width / factor) * factor;
14963
+ if (h_bar * w_bar > max_pixels) {
14964
+ const beta = Math.sqrt(height * width / max_pixels);
14965
+ h_bar = Math.floor(height / beta / factor) * factor;
14966
+ w_bar = Math.floor(width / beta / factor) * factor;
14967
+ } else if (h_bar * w_bar < min_pixels) {
14968
+ const beta = Math.sqrt(min_pixels / (height * width));
14969
+ h_bar = Math.ceil(height * beta / factor) * factor;
14970
+ w_bar = Math.ceil(width * beta / factor) * factor;
14971
+ }
14972
+ return [h_bar, w_bar];
14973
+ }
14542
14974
  var Qwen2VLImageProcessor = class extends ImageProcessor {
14975
+ constructor(config) {
14976
+ super(config);
14977
+ this.min_pixels = config.min_pixels ?? config.size?.shortest_edge;
14978
+ this.max_pixels = config.max_pixels ?? config.size?.longest_edge;
14979
+ this.patch_size = config.patch_size;
14980
+ this.merge_size = config.merge_size;
14981
+ }
14982
+ /** @type {ImageProcessor['get_resize_output_image_size']} */
14983
+ get_resize_output_image_size(image, size) {
14984
+ const factor = this.patch_size * this.merge_size;
14985
+ return smart_resize(image.height, image.width, factor, this.min_pixels, this.max_pixels);
14986
+ }
14543
14987
  async _call(images, ...args) {
14544
14988
  const { pixel_values, original_sizes, reshaped_input_sizes } = await super._call(images, ...args);
14545
14989
  let patches = pixel_values;
@@ -14941,7 +15385,7 @@ var AutoImageProcessor = class {
14941
15385
  let image_processor_class = image_processors_exports[key?.replace(/Fast$/, "")];
14942
15386
  if (!image_processor_class) {
14943
15387
  if (key !== void 0) {
14944
- console.warn(
15388
+ logger.warn(
14945
15389
  `Image processor type '${key}' not found, assuming base ImageProcessor. Please report this at ${GITHUB_ISSUE_URL}.`
14946
15390
  );
14947
15391
  }
@@ -15610,7 +16054,7 @@ var PaliGemmaProcessor = class extends Processor {
15610
16054
  // `images` is required, `text` is optional
15611
16055
  async _call(images, text = null, kwargs = {}) {
15612
16056
  if (!text) {
15613
- console.warn(
16057
+ logger.warn(
15614
16058
  "You are using PaliGemma without a text prefix. It will perform as a picture-captioning model."
15615
16059
  );
15616
16060
  text = "";
@@ -15632,7 +16076,7 @@ var PaliGemmaProcessor = class extends Processor {
15632
16076
  return expanded_sample.slice(0, bos_index) + bos_token + expanded_sample.slice(bos_index) + "\n";
15633
16077
  });
15634
16078
  } else {
15635
- console.warn(
16079
+ logger.warn(
15636
16080
  "You are passing both `text` and `images` to `PaliGemmaProcessor`. The processor expects special image tokens in the text, as many tokens as there are images per each text. It is recommended to add `<image>` tokens in the very beginning of your text. For this call, we will infer how many images each text has and add special tokens."
15637
16081
  );
15638
16082
  input_strings = text.map(
@@ -15788,6 +16232,14 @@ var Qwen2VLProcessor = class extends Processor {
15788
16232
  }
15789
16233
  };
15790
16234
 
16235
+ // src/models/qwen2_5_vl/processing_qwen2_5_vl.js
16236
+ var Qwen2_5_VLProcessor = class extends Qwen2VLProcessor {
16237
+ };
16238
+
16239
+ // src/models/qwen3_vl/processing_qwen3_vl.js
16240
+ var Qwen3VLProcessor = class extends Qwen2_5_VLProcessor {
16241
+ };
16242
+
15791
16243
  // src/models/sam/processing_sam.js
15792
16244
  var SamProcessor = class extends Processor {
15793
16245
  static image_processor_class = AutoImageProcessor;
@@ -16033,6 +16485,8 @@ function getNormalizedConfig(config) {
16033
16485
  case "gemma3n":
16034
16486
  case "chatterbox":
16035
16487
  case "mistral3":
16488
+ case "qwen2_5_vl":
16489
+ case "qwen3_vl":
16036
16490
  init_normalized_config = getNormalizedConfig(config.text_config);
16037
16491
  break;
16038
16492
  case "moondream1":
@@ -16069,6 +16523,7 @@ function getNormalizedConfig(config) {
16069
16523
  case "nanochat":
16070
16524
  case "apertus":
16071
16525
  case "arcee":
16526
+ case "afmoe":
16072
16527
  case "lfm2":
16073
16528
  case "lfm2_moe":
16074
16529
  case "smollm3":
@@ -16079,10 +16534,13 @@ function getNormalizedConfig(config) {
16079
16534
  case "granite":
16080
16535
  case "granitemoehybrid":
16081
16536
  case "cohere":
16537
+ case "cohere2":
16082
16538
  case "mistral":
16083
16539
  case "starcoder2":
16084
16540
  case "qwen2":
16085
16541
  case "qwen2_vl":
16542
+ case "qwen2_5_vl_text":
16543
+ case "qwen3_vl_text":
16086
16544
  case "phi":
16087
16545
  case "phi3":
16088
16546
  case "phi3_v":
@@ -16227,7 +16685,10 @@ function getCacheShapes(config, options) {
16227
16685
  const pkv_prefix = options?.prefix ?? "past_key_values";
16228
16686
  const conv_prefix = pkv_prefix === "present" ? "present" : "past";
16229
16687
  const cache_values = {};
16230
- const { layer_types, num_attention_heads, num_key_value_heads, hidden_size, conv_L_cache } = config;
16688
+ const { layer_types, num_attention_heads, num_key_value_heads, hidden_size, conv_L_cache } = (
16689
+ /** @type {any} */
16690
+ config
16691
+ );
16231
16692
  const head_dim = hidden_size / num_attention_heads;
16232
16693
  const batch_size = options?.batch_size ?? 1;
16233
16694
  for (let i = 0; i < layer_types.length; ++i) {
@@ -16278,6 +16739,48 @@ function getCacheShapes(config, options) {
16278
16739
  }
16279
16740
  }
16280
16741
  return cache_values;
16742
+ } else if (["qwen3_5", "qwen3_5_moe"].includes(config.model_type)) {
16743
+ const pkv_prefix = options?.prefix ?? "past_key_values";
16744
+ const conv_prefix = pkv_prefix === "present" ? "present" : "past";
16745
+ const cache_values = {};
16746
+ const {
16747
+ head_dim,
16748
+ layer_types,
16749
+ num_attention_heads,
16750
+ num_key_value_heads,
16751
+ hidden_size,
16752
+ linear_num_value_heads,
16753
+ linear_num_key_heads,
16754
+ linear_key_head_dim,
16755
+ linear_value_head_dim,
16756
+ linear_conv_kernel_dim
16757
+ } = (
16758
+ /** @type {any} */
16759
+ config.text_config
16760
+ );
16761
+ const key_dim = linear_key_head_dim * linear_num_key_heads;
16762
+ const value_dim = linear_value_head_dim * linear_num_value_heads;
16763
+ const conv_dim = key_dim * 2 + value_dim;
16764
+ const final_head_dim = head_dim ?? hidden_size / num_attention_heads;
16765
+ const batch_size = options?.batch_size ?? 1;
16766
+ for (let i = 0; i < layer_types.length; ++i) {
16767
+ if (layer_types[i] === "full_attention") {
16768
+ for (const kv of ["key", "value"]) {
16769
+ cache_values[`${pkv_prefix}.${i}.${kv}`] = [batch_size, num_key_value_heads, 0, final_head_dim];
16770
+ }
16771
+ } else if (layer_types[i] === "linear_attention") {
16772
+ cache_values[`${conv_prefix}_conv.${i}`] = [batch_size, conv_dim, linear_conv_kernel_dim];
16773
+ cache_values[`${conv_prefix}_recurrent.${i}`] = [
16774
+ batch_size,
16775
+ linear_num_value_heads,
16776
+ linear_key_head_dim,
16777
+ linear_value_head_dim
16778
+ ];
16779
+ } else {
16780
+ throw new Error(`Unsupported layer type: ${layer_types[i]}`);
16781
+ }
16782
+ }
16783
+ return cache_values;
16281
16784
  }
16282
16785
  return getKeyValueShapes(config, options);
16283
16786
  }
@@ -16382,6 +16885,22 @@ var AutoConfig = class {
16382
16885
  };
16383
16886
 
16384
16887
  // src/utils/model-loader.js
16888
+ function resolveExternalDataFormat(config, fullName, fileName) {
16889
+ if (!config) return 0;
16890
+ if (typeof config === "object" && config !== null) {
16891
+ if (config.hasOwnProperty(fullName)) return +config[fullName];
16892
+ if (config.hasOwnProperty(fileName)) return +config[fileName];
16893
+ return 0;
16894
+ }
16895
+ return +config;
16896
+ }
16897
+ function getExternalDataChunkNames(fullName, numChunks) {
16898
+ const names = [];
16899
+ for (let i = 0; i < numChunks; ++i) {
16900
+ names.push(`${fullName}_data${i === 0 ? "" : "_" + i}`);
16901
+ }
16902
+ return names;
16903
+ }
16385
16904
  async function getCoreModelFile(pretrained_model_name_or_path, fileName, options, suffix) {
16386
16905
  const baseName = `${fileName}${suffix}.onnx`;
16387
16906
  const fullPath = `${options.subfolder ?? ""}/${baseName}`;
@@ -16391,27 +16910,15 @@ async function getModelDataFiles(pretrained_model_name_or_path, fileName, suffix
16391
16910
  const baseName = `${fileName}${suffix}.onnx`;
16392
16911
  const return_path = apis.IS_NODE_ENV;
16393
16912
  let externalDataPromises = [];
16394
- if (use_external_data_format) {
16395
- let external_data_format;
16396
- if (typeof use_external_data_format === "object") {
16397
- if (use_external_data_format.hasOwnProperty(baseName)) {
16398
- external_data_format = use_external_data_format[baseName];
16399
- } else if (use_external_data_format.hasOwnProperty(fileName)) {
16400
- external_data_format = use_external_data_format[fileName];
16401
- } else {
16402
- external_data_format = false;
16403
- }
16404
- } else {
16405
- external_data_format = use_external_data_format;
16406
- }
16407
- const num_chunks = +external_data_format;
16913
+ const num_chunks = resolveExternalDataFormat(use_external_data_format, baseName, fileName);
16914
+ if (num_chunks > 0) {
16408
16915
  if (num_chunks > MAX_EXTERNAL_DATA_CHUNKS) {
16409
16916
  throw new Error(
16410
16917
  `The number of external data chunks (${num_chunks}) exceeds the maximum allowed value (${MAX_EXTERNAL_DATA_CHUNKS}).`
16411
16918
  );
16412
16919
  }
16413
- for (let i = 0; i < num_chunks; ++i) {
16414
- const path = `${baseName}_data${i === 0 ? "" : "_" + i}`;
16920
+ const chunkNames = getExternalDataChunkNames(baseName, num_chunks);
16921
+ for (const path of chunkNames) {
16415
16922
  const fullPath = `${options.subfolder ?? ""}/${path}`;
16416
16923
  externalDataPromises.push(
16417
16924
  new Promise(async (resolve, reject) => {
@@ -16441,18 +16948,11 @@ async function getModelDataFiles(pretrained_model_name_or_path, fileName, suffix
16441
16948
  // src/models/session.js
16442
16949
  async function getSession(pretrained_model_name_or_path, fileName, options, is_decoder = false) {
16443
16950
  let custom_config = options.config?.["transformers.js_config"] ?? {};
16444
- let device = options.device ?? custom_config.device;
16445
- if (device && typeof device !== "string") {
16446
- if (device.hasOwnProperty(fileName)) {
16447
- device = device[fileName];
16448
- } else {
16449
- console.warn(`device not specified for "${fileName}". Using the default device.`);
16450
- device = null;
16451
- }
16452
- }
16453
16951
  const selectedDevice = (
16454
16952
  /** @type {import("../utils/devices.js").DeviceType} */
16455
- device ?? (apis.IS_NODE_ENV ? "cpu" : "wasm")
16953
+ selectDevice(options.device ?? custom_config.device, fileName, {
16954
+ warn: (msg) => logger.info(msg)
16955
+ })
16456
16956
  );
16457
16957
  const executionProviders = deviceToExecutionProviders(selectedDevice);
16458
16958
  const device_config = custom_config.device_config ?? {};
@@ -16462,31 +16962,12 @@ async function getSession(pretrained_model_name_or_path, fileName, options, is_d
16462
16962
  ...device_config[selectedDevice]
16463
16963
  };
16464
16964
  }
16465
- let dtype = options.dtype ?? custom_config.dtype;
16466
- if (typeof dtype !== "string") {
16467
- if (dtype && dtype.hasOwnProperty(fileName)) {
16468
- dtype = dtype[fileName];
16469
- } else {
16470
- dtype = DEFAULT_DEVICE_DTYPE_MAPPING[selectedDevice] ?? DATA_TYPES.fp32;
16471
- console.warn(
16472
- `dtype not specified for "${fileName}". Using the default dtype (${dtype}) for this device (${selectedDevice}).`
16473
- );
16474
- }
16475
- }
16476
- if (dtype === DATA_TYPES.auto) {
16477
- let config_dtype = custom_config.dtype;
16478
- if (typeof config_dtype !== "string") {
16479
- config_dtype = config_dtype?.[fileName];
16480
- }
16481
- if (config_dtype && config_dtype !== DATA_TYPES.auto && DATA_TYPES.hasOwnProperty(config_dtype)) {
16482
- dtype = config_dtype;
16483
- } else {
16484
- dtype = DEFAULT_DEVICE_DTYPE_MAPPING[selectedDevice] ?? DATA_TYPES.fp32;
16485
- }
16486
- }
16487
16965
  const selectedDtype = (
16488
16966
  /** @type {import("../utils/dtypes.js").DataType} */
16489
- dtype
16967
+ selectDtype(options.dtype ?? custom_config.dtype, fileName, selectedDevice, {
16968
+ configDtype: custom_config.dtype,
16969
+ warn: (msg) => logger.info(msg)
16970
+ })
16490
16971
  );
16491
16972
  if (!DEFAULT_DTYPE_SUFFIX_MAPPING.hasOwnProperty(selectedDtype)) {
16492
16973
  throw new Error(`Invalid dtype: ${selectedDtype}. Should be one of: ${Object.keys(DATA_TYPES).join(", ")}`);
@@ -16506,7 +16987,7 @@ async function getSession(pretrained_model_name_or_path, fileName, options, is_d
16506
16987
  if (free_dimension_overrides) {
16507
16988
  session_options.freeDimensionOverrides ??= free_dimension_overrides;
16508
16989
  } else if (selectedDevice.startsWith("webnn") && !session_options.freeDimensionOverrides) {
16509
- console.warn(
16990
+ logger.warn(
16510
16991
  `WebNN does not currently support dynamic shapes and requires 'free_dimension_overrides' to be set in config.json, preferably as a field within config["transformers.js_config"]["device_config"]["${selectedDevice}"]. When 'free_dimension_overrides' is not set, you may experience significant performance degradation.`
16511
16992
  );
16512
16993
  }
@@ -16602,8 +17083,8 @@ async function sessionRun(session, inputs) {
16602
17083
  return [k, unpacked];
16603
17084
  })
16604
17085
  );
16605
- console.error(`An error occurred during model execution: "${e}".`);
16606
- console.error("Inputs given to model:", formatted);
17086
+ logger.error(`An error occurred during model execution: "${e}".`);
17087
+ logger.error("Inputs given to model:", formatted);
16607
17088
  throw e;
16608
17089
  }
16609
17090
  }
@@ -16627,7 +17108,7 @@ function validateInputs(session, inputs) {
16627
17108
  const numInputsNeeded = session.inputNames.length;
16628
17109
  if (numInputsProvided > numInputsNeeded) {
16629
17110
  let ignored = Object.keys(inputs).filter((inputName) => !session.inputNames.includes(inputName));
16630
- console.warn(
17111
+ logger.warn(
16631
17112
  `WARNING: Too many inputs were provided (${numInputsProvided} > ${numInputsNeeded}). The following inputs will be ignored: "${ignored.join(", ")}".`
16632
17113
  );
16633
17114
  }
@@ -17760,22 +18241,11 @@ var LogitsSampler = class extends Callable2 {
17760
18241
  }
17761
18242
  /**
17762
18243
  * Selects an item randomly based on the specified probabilities.
17763
- * @param {import("../transformers.js").DataArray} probabilities An array of probabilities to use for selection.
18244
+ * @param {Float32Array} probabilities An array of probabilities to use for selection.
17764
18245
  * @returns {number} The index of the selected item.
17765
18246
  */
17766
18247
  randomSelect(probabilities) {
17767
- let sumProbabilities = 0;
17768
- for (let i = 0; i < probabilities.length; ++i) {
17769
- sumProbabilities += probabilities[i];
17770
- }
17771
- let r = Math.random() * sumProbabilities;
17772
- for (let i = 0; i < probabilities.length; ++i) {
17773
- r -= probabilities[i];
17774
- if (r <= 0) {
17775
- return i;
17776
- }
17777
- }
17778
- return 0;
18248
+ return _weightedIndex(probabilities);
17779
18249
  }
17780
18250
  /**
17781
18251
  * Returns a Sampler object based on the specified options.
@@ -18281,7 +18751,7 @@ var PreTrainedModel = class extends Callable2 {
18281
18751
  if (modelType === void 0) {
18282
18752
  const type = modelName ?? config?.model_type;
18283
18753
  if (type !== "custom") {
18284
- console.warn(
18754
+ logger.warn(
18285
18755
  `Model type for '${type}' not found, assuming encoder-only architecture. Please report this at ${GITHUB_ISSUE_URL}.`
18286
18756
  );
18287
18757
  }
@@ -18372,7 +18842,7 @@ var PreTrainedModel = class extends Callable2 {
18372
18842
  processors.push(new ClassifierFreeGuidanceLogitsProcessor(generation_config.guidance_scale));
18373
18843
  }
18374
18844
  if (generation_config.temperature === 0 && generation_config.do_sample) {
18375
- console.warn(
18845
+ logger.warn(
18376
18846
  "`do_sample` changed to false because `temperature: 0` implies greedy sampling (always selecting the most likely token), which is incompatible with `do_sample: true`."
18377
18847
  );
18378
18848
  generation_config.do_sample = false;
@@ -18745,7 +19215,7 @@ var PreTrainedModel = class extends Callable2 {
18745
19215
  const pkvs = /* @__PURE__ */ Object.create(null);
18746
19216
  for (const name in decoderResults) {
18747
19217
  if (name.startsWith("present")) {
18748
- const newName = name.replace("present_ssm", "past_ssm").replace("present_conv", "past_conv").replace("present", "past_key_values");
19218
+ const newName = name.replace("present_ssm", "past_ssm").replace("present_conv", "past_conv").replace("present_recurrent", "past_recurrent").replace("present", "past_key_values");
18749
19219
  const is_encoder_pkv = name.includes("encoder");
18750
19220
  if (is_encoder_pkv && pastKeyValues) {
18751
19221
  pkvs[newName] = pastKeyValues[newName];
@@ -18917,7 +19387,9 @@ async function generic_text_to_text_forward(self2, {
18917
19387
  }
18918
19388
  }
18919
19389
  if (!position_ids) {
18920
- if (self2.config.model_type === "qwen2_vl") {
19390
+ if (["qwen2_vl", "qwen2_5_vl_text", "qwen3_vl_text", "qwen3_5_text", "qwen3_5_moe_text"].includes(
19391
+ self2.config.model_type
19392
+ )) {
18921
19393
  const { image_grid_thw, video_grid_thw } = kwargs;
18922
19394
  [position_ids] = self2.get_rope_index(input_ids, image_grid_thw, video_grid_thw, attention_mask);
18923
19395
  }
@@ -19099,6 +19571,9 @@ __export(models_exports, {
19099
19571
  ASTForAudioClassification: () => ASTForAudioClassification,
19100
19572
  ASTModel: () => ASTModel,
19101
19573
  ASTPreTrainedModel: () => ASTPreTrainedModel,
19574
+ AfmoeForCausalLM: () => AfmoeForCausalLM,
19575
+ AfmoeModel: () => AfmoeModel,
19576
+ AfmoePreTrainedModel: () => AfmoePreTrainedModel,
19102
19577
  AlbertForMaskedLM: () => AlbertForMaskedLM,
19103
19578
  AlbertForQuestionAnswering: () => AlbertForQuestionAnswering,
19104
19579
  AlbertForSequenceClassification: () => AlbertForSequenceClassification,
@@ -19158,6 +19633,9 @@ __export(models_exports, {
19158
19633
  CodeGenForCausalLM: () => CodeGenForCausalLM,
19159
19634
  CodeGenModel: () => CodeGenModel,
19160
19635
  CodeGenPreTrainedModel: () => CodeGenPreTrainedModel,
19636
+ Cohere2ForCausalLM: () => Cohere2ForCausalLM,
19637
+ Cohere2Model: () => Cohere2Model,
19638
+ Cohere2PreTrainedModel: () => Cohere2PreTrainedModel,
19161
19639
  CohereForCausalLM: () => CohereForCausalLM,
19162
19640
  CohereModel: () => CohereModel,
19163
19641
  CoherePreTrainedModel: () => CoherePreTrainedModel,
@@ -19503,9 +19981,13 @@ __export(models_exports, {
19503
19981
  Qwen2PreTrainedModel: () => Qwen2PreTrainedModel,
19504
19982
  Qwen2VLForConditionalGeneration: () => Qwen2VLForConditionalGeneration,
19505
19983
  Qwen2VLPreTrainedModel: () => Qwen2VLPreTrainedModel,
19984
+ Qwen2_5_VLForConditionalGeneration: () => Qwen2_5_VLForConditionalGeneration,
19506
19985
  Qwen3ForCausalLM: () => Qwen3ForCausalLM,
19507
19986
  Qwen3Model: () => Qwen3Model,
19508
19987
  Qwen3PreTrainedModel: () => Qwen3PreTrainedModel,
19988
+ Qwen3VLForConditionalGeneration: () => Qwen3VLForConditionalGeneration,
19989
+ Qwen3_5ForConditionalGeneration: () => Qwen3_5ForConditionalGeneration,
19990
+ Qwen3_5MoeForConditionalGeneration: () => Qwen3_5MoeForConditionalGeneration,
19509
19991
  RFDetrForObjectDetection: () => RFDetrForObjectDetection,
19510
19992
  RFDetrModel: () => RFDetrModel,
19511
19993
  RFDetrObjectDetectionOutput: () => RFDetrObjectDetectionOutput,
@@ -19716,6 +20198,14 @@ var ApertusModel = class extends ApertusPreTrainedModel {
19716
20198
  var ApertusForCausalLM = class extends ApertusPreTrainedModel {
19717
20199
  };
19718
20200
 
20201
+ // src/models/afmoe/modeling_afmoe.js
20202
+ var AfmoePreTrainedModel = class extends PreTrainedModel {
20203
+ };
20204
+ var AfmoeModel = class extends AfmoePreTrainedModel {
20205
+ };
20206
+ var AfmoeForCausalLM = class extends AfmoePreTrainedModel {
20207
+ };
20208
+
19719
20209
  // src/models/arcee/modeling_arcee.js
19720
20210
  var ArceePreTrainedModel = class extends PreTrainedModel {
19721
20211
  };
@@ -20155,6 +20645,14 @@ var CohereModel = class extends CoherePreTrainedModel {
20155
20645
  var CohereForCausalLM = class extends CoherePreTrainedModel {
20156
20646
  };
20157
20647
 
20648
+ // src/models/cohere2/modeling_cohere2.js
20649
+ var Cohere2PreTrainedModel = class extends PreTrainedModel {
20650
+ };
20651
+ var Cohere2Model = class extends Cohere2PreTrainedModel {
20652
+ };
20653
+ var Cohere2ForCausalLM = class extends Cohere2PreTrainedModel {
20654
+ };
20655
+
20158
20656
  // src/models/convbert/modeling_convbert.js
20159
20657
  var ConvBertPreTrainedModel = class extends PreTrainedModel {
20160
20658
  };
@@ -22267,6 +22765,7 @@ var Qwen2VLPreTrainedModel = class extends PreTrainedModel {
22267
22765
  ];
22268
22766
  };
22269
22767
  var Qwen2VLForConditionalGeneration = class extends Qwen2VLPreTrainedModel {
22768
+ image_grid_thw_name = "grid_thw";
22270
22769
  /**
22271
22770
  * Calculate the 3D rope index based on image and video's temporal, height and width in LLM.
22272
22771
  *
@@ -22431,7 +22930,10 @@ var Qwen2VLForConditionalGeneration = class extends Qwen2VLPreTrainedModel {
22431
22930
  }
22432
22931
  }
22433
22932
  async encode_image({ pixel_values, image_grid_thw }) {
22434
- const features = (await sessionRun(this.sessions["vision_encoder"], { pixel_values, grid_thw: image_grid_thw })).image_features;
22933
+ const features = (await sessionRun(this.sessions["vision_encoder"], {
22934
+ pixel_values,
22935
+ [this.image_grid_thw_name]: image_grid_thw
22936
+ })).image_features;
22435
22937
  return features;
22436
22938
  }
22437
22939
  _merge_input_ids_with_image_features(kwargs) {
@@ -22461,6 +22963,11 @@ var Qwen2VLForConditionalGeneration = class extends Qwen2VLPreTrainedModel {
22461
22963
  }
22462
22964
  };
22463
22965
 
22966
+ // src/models/qwen2_5_vl/modeling_qwen2_5_vl.js
22967
+ var Qwen2_5_VLForConditionalGeneration = class extends Qwen2VLForConditionalGeneration {
22968
+ image_grid_thw_name = "image_grid_thw";
22969
+ };
22970
+
22464
22971
  // src/models/qwen3/modeling_qwen3.js
22465
22972
  var Qwen3PreTrainedModel = class extends PreTrainedModel {
22466
22973
  };
@@ -22469,6 +22976,18 @@ var Qwen3Model = class extends Qwen3PreTrainedModel {
22469
22976
  var Qwen3ForCausalLM = class extends Qwen3PreTrainedModel {
22470
22977
  };
22471
22978
 
22979
+ // src/models/qwen3_vl/modeling_qwen3_vl.js
22980
+ var Qwen3VLForConditionalGeneration = class extends Qwen2_5_VLForConditionalGeneration {
22981
+ };
22982
+
22983
+ // src/models/qwen3_5/modeling_qwen3_5.js
22984
+ var Qwen3_5ForConditionalGeneration = class extends Qwen3VLForConditionalGeneration {
22985
+ };
22986
+
22987
+ // src/models/qwen3_5_moe/modeling_qwen3_5_moe.js
22988
+ var Qwen3_5MoeForConditionalGeneration = class extends Qwen3_5ForConditionalGeneration {
22989
+ };
22990
+
22472
22991
  // src/models/resnet/modeling_resnet.js
22473
22992
  var ResNetPreTrainedModel = class extends PreTrainedModel {
22474
22993
  };
@@ -23526,7 +24045,7 @@ var WhisperForConditionalGeneration = class extends WhisperPreTrainedModel {
23526
24045
  const task = generation_config.task;
23527
24046
  if (generation_config.is_multilingual) {
23528
24047
  if (!language) {
23529
- console.warn("No language specified - defaulting to English (en).");
24048
+ logger.warn("No language specified - defaulting to English (en).");
23530
24049
  language = "en";
23531
24050
  }
23532
24051
  const language_code = whisper_language_to_code(language);
@@ -23541,7 +24060,7 @@ var WhisperForConditionalGeneration = class extends WhisperPreTrainedModel {
23541
24060
  if (!generation_config.return_timestamps && generation_config.no_timestamps_token_id && init_tokens.at(-1) !== generation_config.no_timestamps_token_id) {
23542
24061
  init_tokens.push(generation_config.no_timestamps_token_id);
23543
24062
  } else if (generation_config.return_timestamps && init_tokens.at(-1) === generation_config.no_timestamps_token_id) {
23544
- console.warn(
24063
+ logger.warn(
23545
24064
  "<|notimestamps|> prompt token is removed from generation_config since `return_timestamps` is set to `true`."
23546
24065
  );
23547
24066
  init_tokens.pop();
@@ -23583,7 +24102,7 @@ var WhisperForConditionalGeneration = class extends WhisperPreTrainedModel {
23583
24102
  );
23584
24103
  }
23585
24104
  if (generation_config.task === "translate") {
23586
- console.warn("Token-level timestamps may not be reliable for task 'translate'.");
24105
+ logger.warn("Token-level timestamps may not be reliable for task 'translate'.");
23587
24106
  }
23588
24107
  generation_config.output_attentions = true;
23589
24108
  generation_config.return_dict_in_generate = true;
@@ -23624,13 +24143,13 @@ var WhisperForConditionalGeneration = class extends WhisperPreTrainedModel {
23624
24143
  );
23625
24144
  }
23626
24145
  if (num_frames == null) {
23627
- console.warn(
24146
+ logger.warn(
23628
24147
  "`num_frames` has not been set, meaning the entire audio will be analyzed. This may lead to inaccurate token-level timestamps for short audios (< 30 seconds)."
23629
24148
  );
23630
24149
  }
23631
24150
  let median_filter_width = this.config.median_filter_width;
23632
24151
  if (median_filter_width === void 0) {
23633
- console.warn("Model config has no `median_filter_width`, using default value of 7.");
24152
+ logger.warn("Model config has no `median_filter_width`, using default value of 7.");
23634
24153
  median_filter_width = 7;
23635
24154
  }
23636
24155
  const batch = generate_outputs.cross_attentions;
@@ -23948,6 +24467,7 @@ var MODEL_MAPPING_NAMES_DECODER_ONLY = /* @__PURE__ */ new Map([
23948
24467
  ["apertus", "ApertusModel"],
23949
24468
  ["nanochat", "NanoChatModel"],
23950
24469
  ["arcee", "ArceeModel"],
24470
+ ["afmoe", "AfmoeModel"],
23951
24471
  ["lfm2", "Lfm2Model"],
23952
24472
  ["lfm2_moe", "Lfm2MoeModel"],
23953
24473
  ["smollm3", "SmolLM3Model"],
@@ -23959,6 +24479,7 @@ var MODEL_MAPPING_NAMES_DECODER_ONLY = /* @__PURE__ */ new Map([
23959
24479
  ["granite", "GraniteModel"],
23960
24480
  ["granitemoehybrid", "GraniteMoeHybridModel"],
23961
24481
  ["cohere", "CohereModel"],
24482
+ ["cohere2", "Cohere2Model"],
23962
24483
  ["gemma", "GemmaModel"],
23963
24484
  ["gemma2", "Gemma2Model"],
23964
24485
  ["vaultgemma", "VaultGemmaModel"],
@@ -24061,6 +24582,7 @@ var MODEL_FOR_CAUSAL_LM_MAPPING_NAMES = /* @__PURE__ */ new Map([
24061
24582
  ["apertus", "ApertusForCausalLM"],
24062
24583
  ["llama4_text", "Llama4ForCausalLM"],
24063
24584
  ["arcee", "ArceeForCausalLM"],
24585
+ ["afmoe", "AfmoeForCausalLM"],
24064
24586
  ["lfm2", "Lfm2ForCausalLM"],
24065
24587
  ["lfm2_moe", "Lfm2MoeForCausalLM"],
24066
24588
  ["smollm3", "SmolLM3ForCausalLM"],
@@ -24072,6 +24594,7 @@ var MODEL_FOR_CAUSAL_LM_MAPPING_NAMES = /* @__PURE__ */ new Map([
24072
24594
  ["granite", "GraniteForCausalLM"],
24073
24595
  ["granitemoehybrid", "GraniteMoeHybridForCausalLM"],
24074
24596
  ["cohere", "CohereForCausalLM"],
24597
+ ["cohere2", "Cohere2ForCausalLM"],
24075
24598
  ["gemma", "GemmaForCausalLM"],
24076
24599
  ["gemma2", "Gemma2ForCausalLM"],
24077
24600
  ["vaultgemma", "VaultGemmaForCausalLM"],
@@ -24150,7 +24673,11 @@ var MODEL_FOR_IMAGE_TEXT_TO_TEXT_MAPPING_NAMES = /* @__PURE__ */ new Map([
24150
24673
  ["llava_onevision", "LlavaOnevisionForConditionalGeneration"],
24151
24674
  ["moondream1", "Moondream1ForConditionalGeneration"],
24152
24675
  ["florence2", "Florence2ForConditionalGeneration"],
24153
- ["qwen2-vl", "Qwen2VLForConditionalGeneration"],
24676
+ ["qwen2_vl", "Qwen2VLForConditionalGeneration"],
24677
+ ["qwen2_5_vl", "Qwen2_5_VLForConditionalGeneration"],
24678
+ ["qwen3_vl", "Qwen3VLForConditionalGeneration"],
24679
+ ["qwen3_5", "Qwen3_5ForConditionalGeneration"],
24680
+ ["qwen3_5_moe", "Qwen3_5MoeForConditionalGeneration"],
24154
24681
  ["idefics3", "Idefics3ForConditionalGeneration"],
24155
24682
  ["smolvlm", "SmolVLMForConditionalGeneration"],
24156
24683
  ["paligemma", "PaliGemmaForConditionalGeneration"],
@@ -24456,7 +24983,7 @@ var PretrainedMixin = class {
24456
24983
  }
24457
24984
  if (this.BASE_IF_FAIL) {
24458
24985
  if (!CUSTOM_ARCHITECTURES.has(model_type)) {
24459
- console.warn(`Unknown model class "${model_type}", attempting to construct from base class.`);
24986
+ logger.warn(`Unknown model class "${model_type}", attempting to construct from base class.`);
24460
24987
  }
24461
24988
  return await PreTrainedModel.from_pretrained(pretrained_model_name_or_path, options);
24462
24989
  } else {
@@ -24859,6 +25386,10 @@ function isChat(x) {
24859
25386
  }
24860
25387
  var TextGenerationPipeline = class extends /** @type {new (options: TextPipelineConstructorArgs) => TextGenerationPipelineType} */
24861
25388
  Pipeline {
25389
+ /**
25390
+ * @param {string | string[] | import('../tokenization_utils.js').Message[] | import('../tokenization_utils.js').Message[][]} texts
25391
+ * @param {Partial<TextGenerationConfig>} generate_kwargs
25392
+ */
24862
25393
  async _call(texts, generate_kwargs = {}) {
24863
25394
  let isBatched = false;
24864
25395
  let isChatInput = false;
@@ -24954,12 +25485,12 @@ Pipeline {
24954
25485
  );
24955
25486
  this.entailment_id = this.label2id["entailment"];
24956
25487
  if (this.entailment_id === void 0) {
24957
- console.warn("Could not find 'entailment' in label2id mapping. Using 2 as entailment_id.");
25488
+ logger.warn("Could not find 'entailment' in label2id mapping. Using 2 as entailment_id.");
24958
25489
  this.entailment_id = 2;
24959
25490
  }
24960
25491
  this.contradiction_id = this.label2id["contradiction"] ?? this.label2id["not_entailment"];
24961
25492
  if (this.contradiction_id === void 0) {
24962
- console.warn("Could not find 'contradiction' in label2id mapping. Using 0 as contradiction_id.");
25493
+ logger.warn("Could not find 'contradiction' in label2id mapping. Using 0 as contradiction_id.");
24963
25494
  this.contradiction_id = 0;
24964
25495
  }
24965
25496
  }
@@ -25097,10 +25628,10 @@ Pipeline {
25097
25628
  }
25098
25629
  async _call_wav2vec2(audio, kwargs) {
25099
25630
  if (kwargs.language) {
25100
- console.warn('`language` parameter is not yet supported for `wav2vec2` models, defaulting to "English".');
25631
+ logger.warn('`language` parameter is not yet supported for `wav2vec2` models, defaulting to "English".');
25101
25632
  }
25102
25633
  if (kwargs.task) {
25103
- console.warn('`task` parameter is not yet supported for `wav2vec2` models, defaulting to "transcribe".');
25634
+ logger.warn('`task` parameter is not yet supported for `wav2vec2` models, defaulting to "transcribe".');
25104
25635
  }
25105
25636
  const single = !Array.isArray(audio);
25106
25637
  const batchedAudio = single ? [audio] : audio;
@@ -25232,7 +25763,7 @@ Pipeline {
25232
25763
  }
25233
25764
  async _prepare_speaker_embeddings(speaker_embeddings, batch_size) {
25234
25765
  if (typeof speaker_embeddings === "string" || speaker_embeddings instanceof URL) {
25235
- speaker_embeddings = new Float32Array(await (await fetch(speaker_embeddings)).arrayBuffer());
25766
+ speaker_embeddings = new Float32Array(await (await env.fetch(speaker_embeddings)).arrayBuffer());
25236
25767
  }
25237
25768
  if (speaker_embeddings instanceof Float32Array) {
25238
25769
  speaker_embeddings = new Tensor2("float32", speaker_embeddings, [speaker_embeddings.length]);
@@ -25318,7 +25849,7 @@ Pipeline {
25318
25849
  }
25319
25850
  async _call_text_to_spectrogram(text_inputs, { speaker_embeddings }) {
25320
25851
  if (!this.vocoder) {
25321
- console.log("No vocoder specified, using default HifiGan vocoder.");
25852
+ logger.info("No vocoder specified, using default HifiGan vocoder.");
25322
25853
  this.vocoder = await AutoModel.from_pretrained(this.DEFAULT_VOCODER_ID, { dtype: "fp32" });
25323
25854
  }
25324
25855
  const { input_ids } = this.tokenizer(text_inputs, {
@@ -25785,7 +26316,7 @@ Pipeline {
25785
26316
  }
25786
26317
  };
25787
26318
 
25788
- // src/pipelines.js
26319
+ // src/pipelines/index.js
25789
26320
  var SUPPORTED_TASKS = Object.freeze({
25790
26321
  "text-classification": {
25791
26322
  tokenizer: AutoTokenizer,
@@ -26081,7 +26612,191 @@ var TASK_ALIASES = Object.freeze({
26081
26612
  // Add for backwards compatibility
26082
26613
  embeddings: "feature-extraction"
26083
26614
  });
26084
- async function pipeline3(task, model = null, {
26615
+
26616
+ // src/utils/model_registry/get_model_files.js
26617
+ async function get_model_files(modelId, { config = null, dtype: overrideDtype = null, device: overrideDevice = null, model_file_name = null } = {}) {
26618
+ config = await AutoConfig.from_pretrained(modelId, { config });
26619
+ const files = [
26620
+ // Add config.json (always loaded)
26621
+ "config.json"
26622
+ ];
26623
+ const custom_config = config["transformers.js_config"] ?? {};
26624
+ const use_external_data_format = custom_config.use_external_data_format;
26625
+ const subfolder = "onnx";
26626
+ const rawDevice = overrideDevice ?? custom_config.device;
26627
+ let dtype = overrideDtype ?? custom_config.dtype;
26628
+ let modelType;
26629
+ const architectures = (
26630
+ /** @type {string[]} */
26631
+ config.architectures || []
26632
+ );
26633
+ let foundInMapping = false;
26634
+ for (const arch of architectures) {
26635
+ const mappedType = MODEL_TYPE_MAPPING.get(arch);
26636
+ if (mappedType !== void 0) {
26637
+ modelType = mappedType;
26638
+ foundInMapping = true;
26639
+ break;
26640
+ }
26641
+ }
26642
+ if (!foundInMapping && config.model_type) {
26643
+ const mappedType = MODEL_TYPE_MAPPING.get(config.model_type);
26644
+ if (mappedType !== void 0) {
26645
+ modelType = mappedType;
26646
+ foundInMapping = true;
26647
+ }
26648
+ }
26649
+ if (!foundInMapping) {
26650
+ const archList = architectures.length > 0 ? architectures.join(", ") : "(none)";
26651
+ logger.warn(
26652
+ `[get_model_files] Architecture(s) not found in MODEL_TYPE_MAPPING: [${archList}] for model type '${config.model_type}'. Falling back to EncoderOnly (single model.onnx file). If you encounter issues, please report at: ${GITHUB_ISSUE_URL}`
26653
+ );
26654
+ modelType = MODEL_TYPES.EncoderOnly;
26655
+ }
26656
+ const add_model_file = (fileName, baseName = null) => {
26657
+ baseName = baseName ?? fileName;
26658
+ const selectedDevice = selectDevice(rawDevice, fileName);
26659
+ const selectedDtype = selectDtype(dtype, fileName, selectedDevice);
26660
+ const suffix = DEFAULT_DTYPE_SUFFIX_MAPPING[selectedDtype] ?? "";
26661
+ const fullName = `${baseName}${suffix}.onnx`;
26662
+ const fullPath = subfolder ? `${subfolder}/${fullName}` : fullName;
26663
+ files.push(fullPath);
26664
+ const num_chunks = resolveExternalDataFormat(use_external_data_format, fullName, fileName);
26665
+ for (const dataFileName of getExternalDataChunkNames(fullName, num_chunks)) {
26666
+ const dataFilePath = subfolder ? `${subfolder}/${dataFileName}` : dataFileName;
26667
+ files.push(dataFilePath);
26668
+ }
26669
+ };
26670
+ const singleModelName = model_file_name ?? "model";
26671
+ if (modelType === MODEL_TYPES.DecoderOnly) {
26672
+ add_model_file("model", singleModelName);
26673
+ files.push("generation_config.json");
26674
+ } else if (modelType === MODEL_TYPES.DecoderOnlyWithoutHead) {
26675
+ add_model_file("model", singleModelName);
26676
+ } else if (modelType === MODEL_TYPES.Seq2Seq || modelType === MODEL_TYPES.Vision2Seq) {
26677
+ add_model_file("model", "encoder_model");
26678
+ add_model_file("decoder_model_merged");
26679
+ files.push("generation_config.json");
26680
+ } else if (modelType === MODEL_TYPES.MaskGeneration) {
26681
+ add_model_file("model", "vision_encoder");
26682
+ add_model_file("prompt_encoder_mask_decoder");
26683
+ } else if (modelType === MODEL_TYPES.EncoderDecoder) {
26684
+ add_model_file("model", "encoder_model");
26685
+ add_model_file("decoder_model_merged");
26686
+ } else if (modelType === MODEL_TYPES.ImageTextToText) {
26687
+ add_model_file("embed_tokens");
26688
+ add_model_file("vision_encoder");
26689
+ add_model_file("decoder_model_merged");
26690
+ if (config.is_encoder_decoder) {
26691
+ add_model_file("model", "encoder_model");
26692
+ }
26693
+ files.push("generation_config.json");
26694
+ } else if (modelType === MODEL_TYPES.AudioTextToText) {
26695
+ add_model_file("embed_tokens");
26696
+ add_model_file("audio_encoder");
26697
+ add_model_file("decoder_model_merged");
26698
+ files.push("generation_config.json");
26699
+ } else if (modelType === MODEL_TYPES.ImageAudioTextToText) {
26700
+ add_model_file("embed_tokens");
26701
+ add_model_file("audio_encoder");
26702
+ add_model_file("vision_encoder");
26703
+ add_model_file("decoder_model_merged");
26704
+ files.push("generation_config.json");
26705
+ } else if (modelType === MODEL_TYPES.Musicgen) {
26706
+ add_model_file("model", "text_encoder");
26707
+ add_model_file("decoder_model_merged");
26708
+ add_model_file("encodec_decode");
26709
+ files.push("generation_config.json");
26710
+ } else if (modelType === MODEL_TYPES.MultiModality) {
26711
+ add_model_file("prepare_inputs_embeds");
26712
+ add_model_file("model", "language_model");
26713
+ add_model_file("lm_head");
26714
+ add_model_file("gen_head");
26715
+ add_model_file("gen_img_embeds");
26716
+ add_model_file("image_decode");
26717
+ files.push("generation_config.json");
26718
+ } else if (modelType === MODEL_TYPES.Phi3V) {
26719
+ add_model_file("prepare_inputs_embeds");
26720
+ add_model_file("model");
26721
+ add_model_file("vision_encoder");
26722
+ files.push("generation_config.json");
26723
+ } else if (modelType === MODEL_TYPES.Chatterbox) {
26724
+ add_model_file("embed_tokens");
26725
+ add_model_file("speech_encoder");
26726
+ add_model_file("model", "language_model");
26727
+ add_model_file("conditional_decoder");
26728
+ files.push("generation_config.json");
26729
+ } else if (modelType === MODEL_TYPES.AutoEncoder) {
26730
+ add_model_file("encoder_model");
26731
+ add_model_file("decoder_model");
26732
+ } else if (modelType === MODEL_TYPES.Supertonic) {
26733
+ add_model_file("text_encoder");
26734
+ add_model_file("latent_denoiser");
26735
+ add_model_file("voice_decoder");
26736
+ } else {
26737
+ add_model_file("model", singleModelName);
26738
+ }
26739
+ return files;
26740
+ }
26741
+
26742
+ // src/utils/model_registry/get_processor_files.js
26743
+ async function get_processor_files(modelId) {
26744
+ if (!modelId) {
26745
+ throw new Error("modelId is required");
26746
+ }
26747
+ const metadata = await get_file_metadata(modelId, IMAGE_PROCESSOR_NAME, {});
26748
+ return metadata.exists ? [IMAGE_PROCESSOR_NAME] : [];
26749
+ }
26750
+
26751
+ // src/utils/model_registry/get_files.js
26752
+ async function get_files(modelId, {
26753
+ config = null,
26754
+ dtype = null,
26755
+ device = null,
26756
+ model_file_name = null,
26757
+ include_tokenizer = true,
26758
+ include_processor = true
26759
+ } = {}) {
26760
+ const files = await get_model_files(modelId, { config, dtype, device, model_file_name });
26761
+ if (include_tokenizer) {
26762
+ const tokenizerFiles = await get_tokenizer_files(modelId);
26763
+ files.push(...tokenizerFiles);
26764
+ }
26765
+ if (include_processor) {
26766
+ const processorFiles = await get_processor_files(modelId);
26767
+ files.push(...processorFiles);
26768
+ }
26769
+ return files;
26770
+ }
26771
+
26772
+ // src/utils/model_registry/get_pipeline_files.js
26773
+ function get_task_components(task) {
26774
+ const taskConfig = SUPPORTED_TASKS[task];
26775
+ if (!taskConfig) {
26776
+ return null;
26777
+ }
26778
+ return {
26779
+ tokenizer: !!taskConfig.tokenizer,
26780
+ processor: !!taskConfig.processor
26781
+ };
26782
+ }
26783
+ async function get_pipeline_files(task, modelId, options = {}) {
26784
+ task = TASK_ALIASES[task] ?? task;
26785
+ const components = get_task_components(task);
26786
+ if (!components) {
26787
+ throw new Error(
26788
+ `Unsupported pipeline task: ${task}. Must be one of [${Object.keys(SUPPORTED_TASKS).join(", ")}]`
26789
+ );
26790
+ }
26791
+ return get_files(modelId, {
26792
+ ...options,
26793
+ include_tokenizer: components.tokenizer,
26794
+ include_processor: components.processor
26795
+ });
26796
+ }
26797
+
26798
+ // src/pipelines.js
26799
+ async function pipeline2(task, model = null, {
26085
26800
  progress_callback = null,
26086
26801
  config = null,
26087
26802
  cache_dir = null,
@@ -26101,13 +26816,51 @@ async function pipeline3(task, model = null, {
26101
26816
  }
26102
26817
  if (!model) {
26103
26818
  model = pipelineInfo.default.model;
26104
- console.log(`No model specified. Using default model: "${model}".`);
26819
+ logger.info(`No model specified. Using default model: "${model}".`);
26105
26820
  if (!dtype && pipelineInfo.default.dtype) {
26106
26821
  dtype = pipelineInfo.default.dtype;
26107
26822
  }
26108
26823
  }
26824
+ let files_loading = {};
26825
+ if (progress_callback) {
26826
+ const expected_files = await get_pipeline_files(task, model, {
26827
+ device,
26828
+ dtype
26829
+ });
26830
+ const metadata = await Promise.all(expected_files.map(async (file) => get_file_metadata(model, file)));
26831
+ metadata.forEach((m, i) => {
26832
+ if (m.exists) {
26833
+ files_loading[expected_files[i]] = {
26834
+ loaded: 0,
26835
+ total: m.size ?? 0
26836
+ };
26837
+ }
26838
+ });
26839
+ }
26109
26840
  const pretrainedOptions = {
26110
- progress_callback,
26841
+ progress_callback: progress_callback ? (
26842
+ /** @param {import('./utils/core.js').ProgressInfo} info */
26843
+ (info) => {
26844
+ if (info.status === "progress") {
26845
+ files_loading[info.file] = {
26846
+ loaded: info.loaded,
26847
+ total: info.total
26848
+ };
26849
+ const loaded = Object.values(files_loading).reduce((acc, curr) => acc + curr.loaded, 0);
26850
+ const total = Object.values(files_loading).reduce((acc, curr) => acc + curr.total, 0);
26851
+ const progress = total > 0 ? loaded / total * 100 : 0;
26852
+ progress_callback({
26853
+ status: "progress_total",
26854
+ name: info.name,
26855
+ progress,
26856
+ loaded,
26857
+ total,
26858
+ files: structuredClone(files_loading)
26859
+ });
26860
+ }
26861
+ progress_callback(info);
26862
+ }
26863
+ ) : void 0,
26111
26864
  config,
26112
26865
  cache_dir,
26113
26866
  local_files_only,
@@ -26424,7 +27177,7 @@ async function load_video(src, { num_frames = null, fps = null } = {}) {
26424
27177
  }
26425
27178
  await new Promise((resolve) => video.onloadedmetadata = resolve);
26426
27179
  if (video.seekable.start(0) === video.seekable.end(0)) {
26427
- const response = await fetch(video.src);
27180
+ const response = await env.fetch(video.src);
26428
27181
  const blob = await response.blob();
26429
27182
  video.src = URL.createObjectURL(blob);
26430
27183
  await new Promise((resolve) => video.onloadedmetadata = resolve);
@@ -26460,11 +27213,282 @@ async function load_video(src, { num_frames = null, fps = null } = {}) {
26460
27213
  video.remove();
26461
27214
  return new RawVideo(frames, duration);
26462
27215
  }
27216
+
27217
+ // src/utils/model_registry/is_cached.js
27218
+ async function check_files_cache(modelId, files, options = {}) {
27219
+ const cache = await getCache(options?.cache_dir);
27220
+ if (!cache) {
27221
+ const fileStatuses2 = files.map((filename) => ({ file: filename, cached: false }));
27222
+ return { allCached: false, files: fileStatuses2 };
27223
+ }
27224
+ const fileStatuses = await Promise.all(
27225
+ files.map(async (filename) => {
27226
+ const { localPath, proposedCacheKey } = buildResourcePaths(modelId, filename, options, cache);
27227
+ const cached = await checkCachedResource(cache, localPath, proposedCacheKey);
27228
+ return { file: filename, cached: !!cached };
27229
+ })
27230
+ );
27231
+ return { allCached: fileStatuses.every((f) => f.cached), files: fileStatuses };
27232
+ }
27233
+ async function is_cached(modelId, options = {}) {
27234
+ if (!modelId) {
27235
+ throw new Error("modelId is required");
27236
+ }
27237
+ const files = await get_files(modelId, options);
27238
+ return await check_files_cache(modelId, files, options);
27239
+ }
27240
+ async function is_pipeline_cached(task, modelId, options = {}) {
27241
+ if (!task) {
27242
+ throw new Error("task is required");
27243
+ }
27244
+ if (!modelId) {
27245
+ throw new Error("modelId is required");
27246
+ }
27247
+ const files = await get_pipeline_files(task, modelId, options);
27248
+ return await check_files_cache(modelId, files, options);
27249
+ }
27250
+
27251
+ // src/utils/model_registry/clear_cache.js
27252
+ async function clear_files_from_cache(modelId, files, options = {}) {
27253
+ const cache = await getCache(options?.cache_dir);
27254
+ if (!cache) {
27255
+ return {
27256
+ filesDeleted: 0,
27257
+ filesCached: 0,
27258
+ files: files.map((filename) => ({ file: filename, deleted: false, wasCached: false }))
27259
+ };
27260
+ }
27261
+ if (!cache.delete) {
27262
+ throw new Error("Cache does not support delete operation");
27263
+ }
27264
+ const results = await Promise.all(
27265
+ files.map(async (filename) => {
27266
+ const { localPath, proposedCacheKey } = buildResourcePaths(modelId, filename, options, cache);
27267
+ const cached = await checkCachedResource(cache, localPath, proposedCacheKey);
27268
+ const wasCached = !!cached;
27269
+ let deleted = false;
27270
+ if (wasCached) {
27271
+ const deletedWithProposed = await cache.delete(proposedCacheKey);
27272
+ const deletedWithLocal = !deletedWithProposed && proposedCacheKey !== localPath ? await cache.delete(localPath) : false;
27273
+ deleted = deletedWithProposed || deletedWithLocal;
27274
+ }
27275
+ return { file: filename, deleted, wasCached };
27276
+ })
27277
+ );
27278
+ return {
27279
+ filesDeleted: results.filter((r) => r.deleted).length,
27280
+ filesCached: results.filter((r) => r.wasCached).length,
27281
+ files: results
27282
+ };
27283
+ }
27284
+ async function clear_cache(modelId, options = {}) {
27285
+ if (!modelId) {
27286
+ throw new Error("modelId is required");
27287
+ }
27288
+ const files = await get_files(modelId, options);
27289
+ return await clear_files_from_cache(modelId, files, options);
27290
+ }
27291
+ async function clear_pipeline_cache(task, modelId, options = {}) {
27292
+ if (!task) {
27293
+ throw new Error("task is required");
27294
+ }
27295
+ if (!modelId) {
27296
+ throw new Error("modelId is required");
27297
+ }
27298
+ const files = await get_pipeline_files(task, modelId, options);
27299
+ return await clear_files_from_cache(modelId, files, options);
27300
+ }
27301
+
27302
+ // src/utils/model_registry/ModelRegistry.js
27303
+ var ModelRegistry = class {
27304
+ /**
27305
+ * Get all files (model, tokenizer, processor) needed for a model.
27306
+ *
27307
+ * @param {string} modelId - The model id (e.g., "onnx-community/bert-base-uncased-ONNX")
27308
+ * @param {Object} [options] - Optional parameters
27309
+ * @param {import('../../configs.js').PretrainedConfig} [options.config=null] - Pre-loaded config
27310
+ * @param {import('../dtypes.js').DataType|Record<string, import('../dtypes.js').DataType>} [options.dtype=null] - Override dtype
27311
+ * @param {import('../devices.js').DeviceType|Record<string, import('../devices.js').DeviceType>} [options.device=null] - Override device
27312
+ * @param {string} [options.model_file_name=null] - Override the model file name (excluding .onnx suffix)
27313
+ * @param {boolean} [options.include_tokenizer=true] - Whether to check for tokenizer files
27314
+ * @param {boolean} [options.include_processor=true] - Whether to check for processor files
27315
+ * @returns {Promise<string[]>} Array of file paths
27316
+ *
27317
+ * @example
27318
+ * const files = await ModelRegistry.get_files('onnx-community/gpt2-ONNX');
27319
+ * console.log(files); // ['config.json', 'tokenizer.json', 'onnx/model_q4.onnx', ...]
27320
+ */
27321
+ static async get_files(modelId, options = {}) {
27322
+ return get_files(modelId, options);
27323
+ }
27324
+ /**
27325
+ * Get all files needed for a specific pipeline task.
27326
+ * Automatically determines which components are needed based on the task.
27327
+ *
27328
+ * @param {string} task - The pipeline task (e.g., "text-generation", "background-removal")
27329
+ * @param {string} modelId - The model id (e.g., "onnx-community/bert-base-uncased-ONNX")
27330
+ * @param {Object} [options] - Optional parameters
27331
+ * @param {import('../../configs.js').PretrainedConfig} [options.config=null] - Pre-loaded config
27332
+ * @param {import('../dtypes.js').DataType|Record<string, import('../dtypes.js').DataType>} [options.dtype=null] - Override dtype
27333
+ * @param {import('../devices.js').DeviceType|Record<string, import('../devices.js').DeviceType>} [options.device=null] - Override device
27334
+ * @param {string} [options.model_file_name=null] - Override the model file name (excluding .onnx suffix)
27335
+ * @returns {Promise<string[]>} Array of file paths
27336
+ *
27337
+ * @example
27338
+ * const files = await ModelRegistry.get_pipeline_files('text-generation', 'onnx-community/gpt2-ONNX');
27339
+ * console.log(files); // ['config.json', 'tokenizer.json', 'onnx/model_q4.onnx', ...]
27340
+ */
27341
+ static async get_pipeline_files(task, modelId, options = {}) {
27342
+ return get_pipeline_files(task, modelId, options);
27343
+ }
27344
+ /**
27345
+ * Get model files needed for a specific model.
27346
+ *
27347
+ * @param {string} modelId - The model id
27348
+ * @param {Object} [options] - Optional parameters
27349
+ * @param {import('../../configs.js').PretrainedConfig} [options.config=null] - Pre-loaded config
27350
+ * @param {import('../dtypes.js').DataType|Record<string, import('../dtypes.js').DataType>} [options.dtype=null] - Override dtype
27351
+ * @param {import('../devices.js').DeviceType|Record<string, import('../devices.js').DeviceType>} [options.device=null] - Override device
27352
+ * @param {string} [options.model_file_name=null] - Override the model file name (excluding .onnx suffix)
27353
+ * @returns {Promise<string[]>} Array of model file paths
27354
+ *
27355
+ * @example
27356
+ * const files = await ModelRegistry.get_model_files('onnx-community/bert-base-uncased-ONNX');
27357
+ * console.log(files); // ['config.json', 'onnx/model_q4.onnx', 'generation_config.json']
27358
+ */
27359
+ static async get_model_files(modelId, options = {}) {
27360
+ return get_model_files(modelId, options);
27361
+ }
27362
+ /**
27363
+ * Get tokenizer files needed for a specific model.
27364
+ *
27365
+ * @param {string} modelId - The model id
27366
+ * @returns {Promise<string[]>} Array of tokenizer file paths
27367
+ *
27368
+ * @example
27369
+ * const files = await ModelRegistry.get_tokenizer_files('onnx-community/gpt2-ONNX');
27370
+ * console.log(files); // ['tokenizer.json', 'tokenizer_config.json']
27371
+ */
27372
+ static async get_tokenizer_files(modelId) {
27373
+ return get_tokenizer_files(modelId);
27374
+ }
27375
+ /**
27376
+ * Get processor files needed for a specific model.
27377
+ *
27378
+ * @param {string} modelId - The model id
27379
+ * @returns {Promise<string[]>} Array of processor file paths
27380
+ *
27381
+ * @example
27382
+ * const files = await ModelRegistry.get_processor_files('onnx-community/vit-base-patch16-224-ONNX');
27383
+ * console.log(files); // ['preprocessor_config.json']
27384
+ */
27385
+ static async get_processor_files(modelId) {
27386
+ return get_processor_files(modelId);
27387
+ }
27388
+ /**
27389
+ * Check if a model and all its required files are cached.
27390
+ *
27391
+ * @param {string} modelId - The model id
27392
+ * @param {Object} [options] - Optional parameters
27393
+ * @param {import('../dtypes.js').DataType|Record<string, import('../dtypes.js').DataType>} [options.dtype=null] - Override dtype
27394
+ * @param {import('../devices.js').DeviceType|Record<string, import('../devices.js').DeviceType>} [options.device=null] - Override device
27395
+ * @returns {Promise<import('./is_cached.js').CacheCheckResult>} Object with allCached boolean and files array with cache status
27396
+ *
27397
+ * @example
27398
+ * const status = await ModelRegistry.is_cached('onnx-community/bert-base-uncased-ONNX');
27399
+ * console.log(status.allCached); // true or false
27400
+ */
27401
+ static async is_cached(modelId, options = {}) {
27402
+ return is_cached(modelId, options);
27403
+ }
27404
+ /**
27405
+ * Check if all files for a specific pipeline task are cached.
27406
+ * Automatically determines which components are needed based on the task.
27407
+ *
27408
+ * @param {string} task - The pipeline task (e.g., "text-generation", "background-removal")
27409
+ * @param {string} modelId - The model id
27410
+ * @param {Object} [options] - Optional parameters
27411
+ * @param {string} [options.cache_dir] - Custom cache directory
27412
+ * @param {string} [options.revision] - Model revision (default: 'main')
27413
+ * @param {import('../../configs.js').PretrainedConfig} [options.config] - Pre-loaded config
27414
+ * @param {import('../dtypes.js').DataType|Record<string, import('../dtypes.js').DataType>} [options.dtype=null] - Override dtype
27415
+ * @param {import('../devices.js').DeviceType|Record<string, import('../devices.js').DeviceType>} [options.device=null] - Override device
27416
+ * @returns {Promise<import('./is_cached.js').CacheCheckResult>} Object with allCached boolean and files array with cache status
27417
+ *
27418
+ * @example
27419
+ * const status = await ModelRegistry.is_pipeline_cached('text-generation', 'onnx-community/gpt2-ONNX');
27420
+ * console.log(status.allCached); // true or false
27421
+ */
27422
+ static async is_pipeline_cached(task, modelId, options = {}) {
27423
+ return is_pipeline_cached(task, modelId, options);
27424
+ }
27425
+ /**
27426
+ * Get metadata for a specific file without downloading it.
27427
+ *
27428
+ * @param {string} path_or_repo_id - Model id or path
27429
+ * @param {string} filename - The file name
27430
+ * @param {import('../hub.js').PretrainedOptions} [options] - Optional parameters
27431
+ * @returns {Promise<{exists: boolean, size?: number, contentType?: string, fromCache?: boolean}>} File metadata
27432
+ *
27433
+ * @example
27434
+ * const metadata = await ModelRegistry.get_file_metadata('onnx-community/gpt2-ONNX', 'config.json');
27435
+ * console.log(metadata.exists, metadata.size); // true, 665
27436
+ */
27437
+ static async get_file_metadata(path_or_repo_id, filename, options = {}) {
27438
+ return get_file_metadata(path_or_repo_id, filename, options);
27439
+ }
27440
+ /**
27441
+ * Clears all cached files for a given model.
27442
+ * Automatically determines which files are needed and removes them from the cache.
27443
+ *
27444
+ * @param {string} modelId - The model id (e.g., "onnx-community/gpt2-ONNX")
27445
+ * @param {Object} [options] - Optional parameters
27446
+ * @param {string} [options.cache_dir] - Custom cache directory
27447
+ * @param {string} [options.revision] - Model revision (default: 'main')
27448
+ * @param {import('../../configs.js').PretrainedConfig} [options.config] - Pre-loaded config
27449
+ * @param {import('../dtypes.js').DataType|Record<string, import('../dtypes.js').DataType>} [options.dtype] - Override dtype
27450
+ * @param {import('../devices.js').DeviceType|Record<string, import('../devices.js').DeviceType>} [options.device] - Override device
27451
+ * @param {boolean} [options.include_tokenizer=true] - Whether to clear tokenizer files
27452
+ * @param {boolean} [options.include_processor=true] - Whether to clear processor files
27453
+ * @returns {Promise<import('./clear_cache.js').CacheClearResult>} Object with deletion statistics and file status
27454
+ *
27455
+ * @example
27456
+ * const result = await ModelRegistry.clear_cache('onnx-community/bert-base-uncased-ONNX');
27457
+ * console.log(`Deleted ${result.filesDeleted} of ${result.filesCached} cached files`);
27458
+ */
27459
+ static async clear_cache(modelId, options = {}) {
27460
+ return clear_cache(modelId, options);
27461
+ }
27462
+ /**
27463
+ * Clears all cached files for a specific pipeline task.
27464
+ * Automatically determines which components are needed based on the task.
27465
+ *
27466
+ * @param {string} task - The pipeline task (e.g., "text-generation", "image-classification")
27467
+ * @param {string} modelId - The model id (e.g., "onnx-community/gpt2-ONNX")
27468
+ * @param {Object} [options] - Optional parameters
27469
+ * @param {string} [options.cache_dir] - Custom cache directory
27470
+ * @param {string} [options.revision] - Model revision (default: 'main')
27471
+ * @param {import('../../configs.js').PretrainedConfig} [options.config] - Pre-loaded config
27472
+ * @param {import('../dtypes.js').DataType|Record<string, import('../dtypes.js').DataType>} [options.dtype] - Override dtype
27473
+ * @param {import('../devices.js').DeviceType|Record<string, import('../devices.js').DeviceType>} [options.device] - Override device
27474
+ * @returns {Promise<import('./clear_cache.js').CacheClearResult>} Object with deletion statistics and file status
27475
+ *
27476
+ * @example
27477
+ * const result = await ModelRegistry.clear_pipeline_cache('text-generation', 'onnx-community/gpt2-ONNX');
27478
+ * console.log(`Deleted ${result.filesDeleted} of ${result.filesCached} cached files`);
27479
+ */
27480
+ static async clear_pipeline_cache(task, modelId, options = {}) {
27481
+ return clear_pipeline_cache(task, modelId, options);
27482
+ }
27483
+ };
26463
27484
  export {
26464
27485
  ASTFeatureExtractor,
26465
27486
  ASTForAudioClassification,
26466
27487
  ASTModel,
26467
27488
  ASTPreTrainedModel,
27489
+ AfmoeForCausalLM,
27490
+ AfmoeModel,
27491
+ AfmoePreTrainedModel,
26468
27492
  AlbertForMaskedLM,
26469
27493
  AlbertForQuestionAnswering,
26470
27494
  AlbertForSequenceClassification,
@@ -26583,6 +27607,9 @@ export {
26583
27607
  CodeGenPreTrainedModel,
26584
27608
  CodeGenTokenizer,
26585
27609
  CodeLlamaTokenizer,
27610
+ Cohere2ForCausalLM,
27611
+ Cohere2Model,
27612
+ Cohere2PreTrainedModel,
26586
27613
  CohereForCausalLM,
26587
27614
  CohereModel,
26588
27615
  CoherePreTrainedModel,
@@ -26828,6 +27855,7 @@ export {
26828
27855
  LlavaPreTrainedModel,
26829
27856
  LlavaProcessor,
26830
27857
  LlavaQwen2ForCausalLM,
27858
+ LogLevel,
26831
27859
  LogitsProcessor,
26832
27860
  LogitsProcessorList,
26833
27861
  LogitsWarper,
@@ -26927,6 +27955,7 @@ export {
26927
27955
  MobileViTV2ForImageClassification,
26928
27956
  MobileViTV2Model,
26929
27957
  MobileViTV2PreTrainedModel,
27958
+ ModelRegistry,
26930
27959
  ModernBertDecoderForCausalLM,
26931
27960
  ModernBertDecoderModel,
26932
27961
  ModernBertDecoderPreTrainedModel,
@@ -27038,9 +28067,15 @@ export {
27038
28067
  Qwen2VLImageProcessor,
27039
28068
  Qwen2VLPreTrainedModel,
27040
28069
  Qwen2VLProcessor,
28070
+ Qwen2_5_VLForConditionalGeneration,
28071
+ Qwen2_5_VLProcessor,
27041
28072
  Qwen3ForCausalLM,
27042
28073
  Qwen3Model,
27043
28074
  Qwen3PreTrainedModel,
28075
+ Qwen3VLForConditionalGeneration,
28076
+ Qwen3VLProcessor,
28077
+ Qwen3_5ForConditionalGeneration,
28078
+ Qwen3_5MoeForConditionalGeneration,
27044
28079
  RFDetrForObjectDetection,
27045
28080
  RFDetrModel,
27046
28081
  RFDetrObjectDetectionOutput,
@@ -27293,10 +28328,11 @@ export {
27293
28328
  ones,
27294
28329
  ones_like,
27295
28330
  permute,
27296
- pipeline3 as pipeline,
28331
+ pipeline2 as pipeline,
27297
28332
  quantize_embeddings,
27298
28333
  rand,
27299
28334
  randn,
28335
+ random,
27300
28336
  read_audio,
27301
28337
  rfft,
27302
28338
  slice2 as slice,