@huggingface/transformers 4.0.0-next.4 → 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 (165) hide show
  1. package/README.md +8 -3
  2. package/dist/ort-wasm-simd-threaded.jsep.mjs +28 -28
  3. package/dist/transformers.js +3093 -2100
  4. package/dist/transformers.min.js +17 -19
  5. package/dist/transformers.node.cjs +3079 -2059
  6. package/dist/transformers.node.min.cjs +19 -21
  7. package/dist/transformers.node.min.mjs +19 -21
  8. package/dist/transformers.node.mjs +3067 -2059
  9. package/dist/transformers.web.js +1295 -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 +51 -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/marian/tokenization_marian.js +3 -2
  24. package/src/models/modeling_utils.js +14 -4
  25. package/src/models/models.js +5 -0
  26. package/src/models/paligemma/processing_paligemma.js +3 -2
  27. package/src/models/processors.js +2 -0
  28. package/src/models/qwen2_5_vl/modeling_qwen2_5_vl.js +5 -0
  29. package/src/models/qwen2_5_vl/processing_qwen2_5_vl.js +3 -0
  30. package/src/models/qwen2_vl/image_processing_qwen2_vl.js +54 -0
  31. package/src/models/qwen2_vl/modeling_qwen2_vl.js +8 -2
  32. package/src/models/qwen3_5/modeling_qwen3_5.js +3 -0
  33. package/src/models/qwen3_5_moe/modeling_qwen3_5_moe.js +3 -0
  34. package/src/models/qwen3_vl/modeling_qwen3_vl.js +3 -0
  35. package/src/models/qwen3_vl/processing_qwen3_vl.js +3 -0
  36. package/src/models/registry.js +7 -1
  37. package/src/models/session.js +16 -50
  38. package/src/models/whisper/feature_extraction_whisper.js +2 -1
  39. package/src/models/whisper/modeling_whisper.js +6 -5
  40. package/src/models/xlm/tokenization_xlm.js +2 -1
  41. package/src/pipelines/automatic-speech-recognition.js +3 -2
  42. package/src/pipelines/index.js +395 -0
  43. package/src/pipelines/text-generation.js +4 -0
  44. package/src/pipelines/text-to-audio.js +4 -2
  45. package/src/pipelines/zero-shot-classification.js +3 -2
  46. package/src/pipelines.js +104 -356
  47. package/src/tokenization_utils.js +42 -21
  48. package/src/transformers.js +6 -1
  49. package/src/utils/audio.js +2 -1
  50. package/src/utils/cache.js +4 -1
  51. package/src/utils/core.js +23 -1
  52. package/src/utils/devices.js +22 -0
  53. package/src/utils/dtypes.js +55 -0
  54. package/src/utils/hub/files.js +17 -2
  55. package/src/utils/hub/utils.js +10 -4
  56. package/src/utils/hub.js +57 -17
  57. package/src/utils/image.js +2 -1
  58. package/src/utils/logger.js +67 -0
  59. package/src/utils/model-loader.js +35 -17
  60. package/src/utils/model_registry/ModelRegistry.js +299 -0
  61. package/src/utils/model_registry/clear_cache.js +128 -0
  62. package/src/utils/model_registry/get_file_metadata.js +149 -0
  63. package/src/utils/model_registry/get_files.js +42 -0
  64. package/src/utils/model_registry/get_model_files.js +182 -0
  65. package/src/utils/model_registry/get_pipeline_files.js +53 -0
  66. package/src/utils/model_registry/get_processor_files.js +20 -0
  67. package/src/utils/model_registry/get_tokenizer_files.js +21 -0
  68. package/src/utils/model_registry/is_cached.js +92 -0
  69. package/src/utils/random.js +225 -0
  70. package/src/utils/tensor.js +8 -21
  71. package/src/utils/video.js +2 -2
  72. package/types/backends/onnx.d.ts.map +1 -1
  73. package/types/backends/utils/cacheWasm.d.ts.map +1 -1
  74. package/types/configs.d.ts.map +1 -1
  75. package/types/env.d.ts +42 -24
  76. package/types/env.d.ts.map +1 -1
  77. package/types/generation/logits_sampler.d.ts +2 -2
  78. package/types/generation/logits_sampler.d.ts.map +1 -1
  79. package/types/image_processors_utils.d.ts.map +1 -1
  80. package/types/models/afmoe/modeling_afmoe.d.ts +8 -0
  81. package/types/models/afmoe/modeling_afmoe.d.ts.map +1 -0
  82. package/types/models/auto/image_processing_auto.d.ts.map +1 -1
  83. package/types/models/auto/modeling_auto.d.ts.map +1 -1
  84. package/types/models/auto/tokenization_auto.d.ts.map +1 -1
  85. package/types/models/clap/feature_extraction_clap.d.ts.map +1 -1
  86. package/types/models/marian/tokenization_marian.d.ts.map +1 -1
  87. package/types/models/modeling_utils.d.ts.map +1 -1
  88. package/types/models/models.d.ts +5 -0
  89. package/types/models/paligemma/processing_paligemma.d.ts.map +1 -1
  90. package/types/models/processors.d.ts +2 -0
  91. package/types/models/qwen2_5_vl/modeling_qwen2_5_vl.d.ts +4 -0
  92. package/types/models/qwen2_5_vl/modeling_qwen2_5_vl.d.ts.map +1 -0
  93. package/types/models/qwen2_5_vl/processing_qwen2_5_vl.d.ts +4 -0
  94. package/types/models/qwen2_5_vl/processing_qwen2_5_vl.d.ts.map +1 -0
  95. package/types/models/qwen2_vl/image_processing_qwen2_vl.d.ts +3 -0
  96. package/types/models/qwen2_vl/image_processing_qwen2_vl.d.ts.map +1 -1
  97. package/types/models/qwen2_vl/modeling_qwen2_vl.d.ts +1 -0
  98. package/types/models/qwen2_vl/modeling_qwen2_vl.d.ts.map +1 -1
  99. package/types/models/qwen3_5/modeling_qwen3_5.d.ts +4 -0
  100. package/types/models/qwen3_5/modeling_qwen3_5.d.ts.map +1 -0
  101. package/types/models/qwen3_5_moe/modeling_qwen3_5_moe.d.ts +4 -0
  102. package/types/models/qwen3_5_moe/modeling_qwen3_5_moe.d.ts.map +1 -0
  103. package/types/models/qwen3_vl/modeling_qwen3_vl.d.ts +4 -0
  104. package/types/models/qwen3_vl/modeling_qwen3_vl.d.ts.map +1 -0
  105. package/types/models/qwen3_vl/processing_qwen3_vl.d.ts +4 -0
  106. package/types/models/qwen3_vl/processing_qwen3_vl.d.ts.map +1 -0
  107. package/types/models/registry.d.ts.map +1 -1
  108. package/types/models/session.d.ts.map +1 -1
  109. package/types/models/whisper/feature_extraction_whisper.d.ts.map +1 -1
  110. package/types/models/whisper/modeling_whisper.d.ts.map +1 -1
  111. package/types/models/xlm/tokenization_xlm.d.ts.map +1 -1
  112. package/types/pipelines/automatic-speech-recognition.d.ts.map +1 -1
  113. package/types/pipelines/index.d.ts +299 -0
  114. package/types/pipelines/index.d.ts.map +1 -0
  115. package/types/pipelines/text-generation.d.ts +5 -1
  116. package/types/pipelines/text-generation.d.ts.map +1 -1
  117. package/types/pipelines/text-to-audio.d.ts.map +1 -1
  118. package/types/pipelines/zero-shot-classification.d.ts.map +1 -1
  119. package/types/pipelines.d.ts +50 -291
  120. package/types/pipelines.d.ts.map +1 -1
  121. package/types/tokenization_utils.d.ts +44 -26
  122. package/types/tokenization_utils.d.ts.map +1 -1
  123. package/types/transformers.d.ts +4 -1
  124. package/types/transformers.d.ts.map +1 -1
  125. package/types/utils/audio.d.ts.map +1 -1
  126. package/types/utils/cache.d.ts +6 -0
  127. package/types/utils/cache.d.ts.map +1 -1
  128. package/types/utils/core.d.ts +59 -2
  129. package/types/utils/core.d.ts.map +1 -1
  130. package/types/utils/devices.d.ts +15 -0
  131. package/types/utils/devices.d.ts.map +1 -1
  132. package/types/utils/dtypes.d.ts +16 -0
  133. package/types/utils/dtypes.d.ts.map +1 -1
  134. package/types/utils/hub/files.d.ts +6 -0
  135. package/types/utils/hub/files.d.ts.map +1 -1
  136. package/types/utils/hub/utils.d.ts +2 -1
  137. package/types/utils/hub/utils.d.ts.map +1 -1
  138. package/types/utils/hub.d.ts +29 -0
  139. package/types/utils/hub.d.ts.map +1 -1
  140. package/types/utils/image.d.ts.map +1 -1
  141. package/types/utils/logger.d.ts +28 -0
  142. package/types/utils/logger.d.ts.map +1 -0
  143. package/types/utils/model-loader.d.ts +15 -0
  144. package/types/utils/model-loader.d.ts.map +1 -1
  145. package/types/utils/model_registry/ModelRegistry.d.ts +211 -0
  146. package/types/utils/model_registry/ModelRegistry.d.ts.map +1 -0
  147. package/types/utils/model_registry/clear_cache.d.ts +74 -0
  148. package/types/utils/model_registry/clear_cache.d.ts.map +1 -0
  149. package/types/utils/model_registry/get_file_metadata.d.ts +20 -0
  150. package/types/utils/model_registry/get_file_metadata.d.ts.map +1 -0
  151. package/types/utils/model_registry/get_files.d.ts +23 -0
  152. package/types/utils/model_registry/get_files.d.ts.map +1 -0
  153. package/types/utils/model_registry/get_model_files.d.ts +22 -0
  154. package/types/utils/model_registry/get_model_files.d.ts.map +1 -0
  155. package/types/utils/model_registry/get_pipeline_files.d.ts +21 -0
  156. package/types/utils/model_registry/get_pipeline_files.d.ts.map +1 -0
  157. package/types/utils/model_registry/get_processor_files.d.ts +9 -0
  158. package/types/utils/model_registry/get_processor_files.d.ts.map +1 -0
  159. package/types/utils/model_registry/get_tokenizer_files.d.ts +9 -0
  160. package/types/utils/model_registry/get_tokenizer_files.d.ts.map +1 -0
  161. package/types/utils/model_registry/is_cached.d.ts +62 -0
  162. package/types/utils/model_registry/is_cached.d.ts.map +1 -0
  163. package/types/utils/random.d.ts +86 -0
  164. package/types/utils/random.d.ts.map +1 -0
  165. package/types/utils/tensor.d.ts.map +1 -1
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@huggingface/transformers",
3
- "version": "4.0.0-next.4",
3
+ "version": "4.0.0-next.5",
4
4
  "description": "State-of-the-art Machine Learning for the web. Run 🤗 Transformers directly in your browser, with no need for a server!",
5
5
  "main": "./dist/transformers.node.cjs",
6
6
  "types": "./types/transformers.d.ts",
@@ -44,9 +44,9 @@
44
44
  "homepage": "https://github.com/huggingface/transformers.js#readme",
45
45
  "dependencies": {
46
46
  "@huggingface/jinja": "^0.5.5",
47
- "@huggingface/tokenizers": "^0.1.1",
48
- "onnxruntime-node": "1.24.1",
49
- "onnxruntime-web": "1.25.0-dev.20260212-1a71a5f46e",
47
+ "@huggingface/tokenizers": "^0.1.2",
48
+ "onnxruntime-node": "1.24.2",
49
+ "onnxruntime-web": "1.25.0-dev.20260228-6e72d31970",
50
50
  "sharp": "^0.34.5"
51
51
  },
52
52
  "devDependencies": {
@@ -16,13 +16,14 @@
16
16
  * @module backends/onnx
17
17
  */
18
18
 
19
- import { env, apis } from '../env.js';
19
+ import { env, apis, LogLevel } from '../env.js';
20
20
 
21
21
  // NOTE: Import order matters here. We need to import `onnxruntime-node` before `onnxruntime-web`.
22
22
  // In either case, we select the default export if it exists, otherwise we use the named export.
23
23
  import * as ONNX_NODE from 'onnxruntime-node';
24
24
  import * as ONNX_WEB from 'onnxruntime-web/webgpu';
25
25
  import { isBlobURL, loadWasmBinary, loadWasmFactory, toAbsoluteURL } from './utils/cacheWasm.js';
26
+ import { logger } from '../utils/logger.js';
26
27
  export { Tensor } from 'onnxruntime-common';
27
28
 
28
29
  /**
@@ -46,9 +47,48 @@ const DEVICE_TO_EXECUTION_PROVIDER_MAPPING = Object.freeze({
46
47
  'webnn-cpu': { name: 'webnn', deviceType: 'cpu' }, // WebNN CPU
47
48
  });
48
49
 
49
- /** @type {Array<'verbose' | 'info' | 'warning' | 'error' | 'fatal'>} */
50
- const LOG_LEVELS = ['verbose', 'info', 'warning', 'error', 'fatal'];
51
- const DEFAULT_LOG_LEVEL = 4; // 'fatal';
50
+ /**
51
+ * Converts any LogLevel value to ONNX Runtime's numeric severity level (0-4).
52
+ * This handles both standard LogLevel values (10, 20, 30, 40, 50) and custom intermediate values.
53
+ *
54
+ * @param {number} logLevel - The LogLevel value to convert
55
+ * @returns {number} ONNX Runtime severity level (0-4)
56
+ */
57
+ function getOnnxLogSeverityLevel(logLevel) {
58
+ // ONNX Runtime's log severity levels are defined as follows:
59
+ // (0) ORT_LOGGING_LEVEL_VERBOSE: Print all log messages.
60
+ // (1) ORT_LOGGING_LEVEL_INFO: Print info and higher level log messages.
61
+ // (2) ORT_LOGGING_LEVEL_WARNING: Print warning and higher level log messages.
62
+ // (3) ORT_LOGGING_LEVEL_ERROR: Print error log messages.
63
+ // (4) ORT_LOGGING_LEVEL_FATAL: Print only fatal log messages.
64
+ //
65
+ // In practice, ONNX Runtime's logging is extremely verbose (especially on session creation).
66
+ // For this reason, we map multiple LogLevel values to the same ONNX severity level to avoid
67
+ // overwhelming users with logs.
68
+ if (logLevel <= LogLevel.DEBUG) {
69
+ return 0; // ORT_LOGGING_LEVEL_VERBOSE
70
+ } else if (logLevel <= LogLevel.INFO) {
71
+ return 2; // ORT_LOGGING_LEVEL_WARNING
72
+ } else if (logLevel <= LogLevel.WARNING) {
73
+ return 3; // ORT_LOGGING_LEVEL_ERROR
74
+ } else if (logLevel <= LogLevel.ERROR) {
75
+ return 3; // ORT_LOGGING_LEVEL_ERROR
76
+ } else {
77
+ return 4; // ORT_LOGGING_LEVEL_FATAL
78
+ }
79
+ }
80
+
81
+ /**
82
+ * Maps ONNX Runtime numeric severity levels to string log levels.
83
+ * @type {Record<0 | 1 | 2 | 3 | 4, 'verbose' | 'info' | 'warning' | 'error' | 'fatal'>}
84
+ */
85
+ const ONNX_LOG_LEVEL_NAMES = {
86
+ 0: 'verbose',
87
+ 1: 'info',
88
+ 2: 'warning',
89
+ 3: 'error',
90
+ 4: 'fatal',
91
+ };
52
92
 
53
93
  /**
54
94
  * The list of supported devices, sorted by priority/performance.
@@ -193,7 +233,7 @@ async function ensureWasmLoaded() {
193
233
  ONNX_ENV.wasm.wasmBinary = wasmBinary;
194
234
  }
195
235
  } catch (err) {
196
- console.warn('Failed to pre-load WASM binary:', err);
236
+ logger.warn('Failed to pre-load WASM binary:', err);
197
237
  }
198
238
  })()
199
239
  : Promise.resolve(),
@@ -208,7 +248,7 @@ async function ensureWasmLoaded() {
208
248
  ONNX_ENV.wasm.wasmPaths.mjs = wasmFactoryBlob;
209
249
  }
210
250
  } catch (err) {
211
- console.warn('Failed to pre-load WASM factory:', err);
251
+ logger.warn('Failed to pre-load WASM factory:', err);
212
252
  }
213
253
  })()
214
254
  : Promise.resolve(),
@@ -227,10 +267,11 @@ async function ensureWasmLoaded() {
227
267
  */
228
268
  export async function createInferenceSession(buffer_or_path, session_options, session_config) {
229
269
  await ensureWasmLoaded();
270
+ const logSeverityLevel = getOnnxLogSeverityLevel(env.logLevel ?? LogLevel.WARNING);
230
271
  const load = () =>
231
272
  InferenceSession.create(buffer_or_path, {
232
- // Set default log level, but allow overriding through session options
233
- logSeverityLevel: DEFAULT_LOG_LEVEL,
273
+ // Set default log severity level, but allow overriding through session options
274
+ logSeverityLevel,
234
275
  ...session_options,
235
276
  });
236
277
  const session = await (IS_WEB_ENV ? (webInitChain = webInitChain.then(load)) : load());
@@ -268,7 +309,6 @@ export function isONNXTensor(x) {
268
309
 
269
310
  /** @type {import('onnxruntime-common').Env} */
270
311
  const ONNX_ENV = ONNX?.env;
271
- ONNX_ENV.logLevel = LOG_LEVELS[DEFAULT_LOG_LEVEL];
272
312
  if (ONNX_ENV?.wasm) {
273
313
  // Initialize wasm backend with suitable default settings.
274
314
 
@@ -311,5 +351,21 @@ export function isONNXProxy() {
311
351
  return ONNX_ENV?.wasm?.proxy;
312
352
  }
313
353
 
354
+ /**
355
+ * A function to map Transformers.js log levels to ONNX Runtime log severity
356
+ * levels, and set the log level environment variable in ONNX Runtime.
357
+ * @param {number} logLevel The log level to set.
358
+ */
359
+ function setLogLevel(logLevel) {
360
+ const severityLevel = getOnnxLogSeverityLevel(logLevel);
361
+ ONNX_ENV.logLevel = ONNX_LOG_LEVEL_NAMES[severityLevel];
362
+ }
363
+
364
+ // Set the initial log level to be the default Transformers.js log level.
365
+ setLogLevel(env.logLevel ?? LogLevel.WARNING);
366
+
314
367
  // Expose ONNX environment variables to `env.backends.onnx`
315
- env.backends.onnx = ONNX_ENV;
368
+ env.backends.onnx = {
369
+ ...ONNX_ENV,
370
+ setLogLevel,
371
+ };
@@ -1,5 +1,7 @@
1
1
  import { getCache } from '../../utils/cache.js';
2
2
  import { isValidUrl } from '../../utils/hub/utils.js';
3
+ import { logger } from '../../utils/logger.js';
4
+ import { env } from '../../env.js';
3
5
 
4
6
  /**
5
7
  * Loads and caches a file from the given URL.
@@ -22,11 +24,11 @@ async function loadAndCacheFile(url) {
22
24
  }
23
25
  }
24
26
  } catch (error) {
25
- console.warn(`Failed to load ${fileName} from cache:`, error);
27
+ logger.warn(`Failed to load ${fileName} from cache:`, error);
26
28
  }
27
29
 
28
30
  // If not in cache, fetch it
29
- const response = await fetch(url);
31
+ const response = await env.fetch(url);
30
32
 
31
33
  if (!response.ok) {
32
34
  throw new Error(`Failed to fetch ${fileName}: ${response.status} ${response.statusText}`);
@@ -37,7 +39,7 @@ async function loadAndCacheFile(url) {
37
39
  try {
38
40
  await cache.put(url, response.clone());
39
41
  } catch (e) {
40
- console.warn(`Failed to cache ${fileName}:`, e);
42
+ logger.warn(`Failed to cache ${fileName}:`, e);
41
43
  }
42
44
  }
43
45
 
@@ -57,7 +59,7 @@ export async function loadWasmBinary(wasmURL) {
57
59
  try {
58
60
  return await response.arrayBuffer();
59
61
  } catch (error) {
60
- console.warn('Failed to read WASM binary:', error);
62
+ logger.warn('Failed to read WASM binary:', error);
61
63
  return null;
62
64
  }
63
65
  }
@@ -75,11 +77,12 @@ export async function loadWasmFactory(libURL) {
75
77
  let code = await response.text();
76
78
  // Fix relative paths when loading factory from blob, overwrite import.meta.url with actual baseURL
77
79
  const baseUrl = libURL.split('/').slice(0, -1).join('/');
78
- code = code.replace(/import\.meta\.url/g, `"${baseUrl}"`);
80
+ code = code.replaceAll('import.meta.url', `"${baseUrl}"`);
81
+ code = code.replaceAll('globalThis.process?.versions?.node', 'false');
79
82
  const blob = new Blob([code], { type: 'text/javascript' });
80
83
  return URL.createObjectURL(blob);
81
84
  } catch (error) {
82
- console.warn('Failed to read WASM binary:', error);
85
+ logger.warn('Failed to read WASM binary:', error);
83
86
  return null;
84
87
  }
85
88
  }
package/src/configs.js CHANGED
@@ -74,6 +74,8 @@ function getNormalizedConfig(config) {
74
74
  case 'gemma3n':
75
75
  case 'chatterbox':
76
76
  case 'mistral3':
77
+ case 'qwen2_5_vl':
78
+ case 'qwen3_vl':
77
79
  // @ts-expect-error TS2339
78
80
  init_normalized_config = getNormalizedConfig(config.text_config);
79
81
  break;
@@ -115,6 +117,7 @@ function getNormalizedConfig(config) {
115
117
  case 'nanochat':
116
118
  case 'apertus':
117
119
  case 'arcee':
120
+ case 'afmoe':
118
121
  case 'lfm2':
119
122
  case 'lfm2_moe':
120
123
  case 'smollm3':
@@ -130,6 +133,8 @@ function getNormalizedConfig(config) {
130
133
  case 'starcoder2':
131
134
  case 'qwen2':
132
135
  case 'qwen2_vl':
136
+ case 'qwen2_5_vl_text':
137
+ case 'qwen3_vl_text':
133
138
  case 'phi':
134
139
  case 'phi3':
135
140
  case 'phi3_v':
@@ -288,11 +293,10 @@ export function getCacheShapes(config, options) {
288
293
  const pkv_prefix = options?.prefix ?? 'past_key_values';
289
294
  const conv_prefix = pkv_prefix === 'present' ? 'present' : 'past';
290
295
 
291
- // Custom caching mechanism for LFM2
292
296
  /** @type {Record<string, number[]>} */
293
297
  const cache_values = {};
294
- // @ts-expect-error TS2339
295
- const { layer_types, num_attention_heads, num_key_value_heads, hidden_size, conv_L_cache } = config;
298
+ const { layer_types, num_attention_heads, num_key_value_heads, hidden_size, conv_L_cache } =
299
+ /** @type {any} */ (config);
296
300
  const head_dim = hidden_size / num_attention_heads;
297
301
  const batch_size = options?.batch_size ?? 1;
298
302
  for (let i = 0; i < layer_types.length; ++i) {
@@ -344,6 +348,50 @@ export function getCacheShapes(config, options) {
344
348
  }
345
349
  }
346
350
  return cache_values;
351
+ } else if (['qwen3_5', 'qwen3_5_moe'].includes(config.model_type)) {
352
+ const pkv_prefix = options?.prefix ?? 'past_key_values';
353
+ const conv_prefix = pkv_prefix === 'present' ? 'present' : 'past';
354
+
355
+ /** @type {Record<string, number[]>} */
356
+ const cache_values = {};
357
+ const {
358
+ head_dim,
359
+ layer_types,
360
+ num_attention_heads,
361
+ num_key_value_heads,
362
+ hidden_size,
363
+ linear_num_value_heads,
364
+ linear_num_key_heads,
365
+ linear_key_head_dim,
366
+ linear_value_head_dim,
367
+ linear_conv_kernel_dim,
368
+ } = /** @type {any} */ (config).text_config;
369
+
370
+ const key_dim = linear_key_head_dim * linear_num_key_heads;
371
+ const value_dim = linear_value_head_dim * linear_num_value_heads;
372
+
373
+ const conv_dim = key_dim * 2 + value_dim;
374
+
375
+ const final_head_dim = head_dim ?? hidden_size / num_attention_heads;
376
+ const batch_size = options?.batch_size ?? 1;
377
+ for (let i = 0; i < layer_types.length; ++i) {
378
+ if (layer_types[i] === 'full_attention') {
379
+ for (const kv of ['key', 'value']) {
380
+ cache_values[`${pkv_prefix}.${i}.${kv}`] = [batch_size, num_key_value_heads, 0, final_head_dim];
381
+ }
382
+ } else if (layer_types[i] === 'linear_attention') {
383
+ cache_values[`${conv_prefix}_conv.${i}`] = [batch_size, conv_dim, linear_conv_kernel_dim];
384
+ cache_values[`${conv_prefix}_recurrent.${i}`] = [
385
+ batch_size,
386
+ linear_num_value_heads,
387
+ linear_key_head_dim,
388
+ linear_value_head_dim,
389
+ ];
390
+ } else {
391
+ throw new Error(`Unsupported layer type: ${layer_types[i]}`);
392
+ }
393
+ }
394
+ return cache_values;
347
395
  }
348
396
  return getKeyValueShapes(config, options);
349
397
  }
package/src/env.js CHANGED
@@ -26,17 +26,21 @@ import fs from 'node:fs';
26
26
  import path from 'node:path';
27
27
  import url from 'node:url';
28
28
 
29
- const VERSION = '4.0.0-next.4';
29
+ const VERSION = '4.0.0-next.5';
30
30
 
31
- const IS_PROCESS_AVAILABLE = typeof process !== 'undefined';
32
- const IS_NODE_ENV = IS_PROCESS_AVAILABLE && process?.release?.name === 'node';
33
31
  const IS_FS_AVAILABLE = !isEmpty(fs);
34
32
  const IS_PATH_AVAILABLE = !isEmpty(path);
33
+ const IS_WEB_CACHE_AVAILABLE = typeof self !== 'undefined' && 'caches' in self;
35
34
 
36
35
  // Runtime detection
37
36
  const IS_DENO_RUNTIME = typeof globalThis.Deno !== 'undefined';
38
37
  const IS_BUN_RUNTIME = typeof globalThis.Bun !== 'undefined';
39
38
 
39
+ const IS_DENO_WEB_RUNTIME = IS_DENO_RUNTIME && IS_WEB_CACHE_AVAILABLE && !IS_FS_AVAILABLE;
40
+
41
+ const IS_PROCESS_AVAILABLE = typeof process !== 'undefined';
42
+ const IS_NODE_ENV = IS_PROCESS_AVAILABLE && process?.release?.name === 'node' && !IS_DENO_WEB_RUNTIME;
43
+
40
44
  // Check if various APIs are available (depends on environment)
41
45
  const IS_BROWSER_ENV = typeof window !== 'undefined' && typeof window.document !== 'undefined';
42
46
  const IS_WEBWORKER_ENV =
@@ -44,9 +48,9 @@ const IS_WEBWORKER_ENV =
44
48
  ['DedicatedWorkerGlobalScope', 'ServiceWorkerGlobalScope', 'SharedWorkerGlobalScope'].includes(
45
49
  self.constructor?.name,
46
50
  );
47
- const IS_WEB_CACHE_AVAILABLE = typeof self !== 'undefined' && 'caches' in self;
48
51
  const IS_WEBGPU_AVAILABLE = IS_NODE_ENV || (typeof navigator !== 'undefined' && 'gpu' in navigator);
49
52
  const IS_WEBNN_AVAILABLE = typeof navigator !== 'undefined' && 'ml' in navigator;
53
+ const IS_CRYPTO_AVAILABLE = typeof crypto !== 'undefined' && typeof crypto.getRandomValues === 'function';
50
54
 
51
55
  /**
52
56
  * Check if the current environment is Safari browser.
@@ -109,6 +113,9 @@ export const apis = Object.freeze({
109
113
 
110
114
  /** Whether the path API is available */
111
115
  IS_PATH_AVAILABLE,
116
+
117
+ /** Whether the crypto API is available */
118
+ IS_CRYPTO_AVAILABLE,
112
119
  });
113
120
 
114
121
  const RUNNING_LOCALLY = IS_FS_AVAILABLE && IS_PATH_AVAILABLE;
@@ -134,12 +141,48 @@ const DEFAULT_CACHE_DIR = RUNNING_LOCALLY ? path.join(dirname__, '/.cache/') : n
134
141
  const DEFAULT_LOCAL_MODEL_PATH = '/models/';
135
142
  const localModelPath = RUNNING_LOCALLY ? path.join(dirname__, DEFAULT_LOCAL_MODEL_PATH) : DEFAULT_LOCAL_MODEL_PATH;
136
143
 
144
+ // Ensure default fetch is called with the correct receiver in browser environments.
145
+ const DEFAULT_FETCH = typeof globalThis.fetch === 'function' ? globalThis.fetch.bind(globalThis) : undefined;
146
+
147
+ /**
148
+ * Log levels for controlling output verbosity.
149
+ *
150
+ * Each level is represented by a number, where higher numbers include all lower level messages.
151
+ * Use these values to set `env.logLevel`.
152
+ *
153
+ * @example
154
+ * import { env, LogLevel } from '@huggingface/transformers';
155
+ *
156
+ * // Set log level to show only errors
157
+ * env.logLevel = LogLevel.ERROR;
158
+ *
159
+ * // Set log level to show errors, warnings, and info
160
+ * env.logLevel = LogLevel.INFO;
161
+ *
162
+ * // Disable all logging
163
+ * env.logLevel = LogLevel.NONE;
164
+ *
165
+ */
166
+ export const LogLevel = Object.freeze({
167
+ /** All messages including debug output (value: 10) */
168
+ DEBUG: 10,
169
+ /** Errors, warnings, and info messages (value: 20) */
170
+ INFO: 20,
171
+ /** Errors and warnings (value: 30) */
172
+ WARNING: 30,
173
+ /** Only error messages (value: 40) */
174
+ ERROR: 40,
175
+ /** No logging output (value: 50) */
176
+ NONE: 50,
177
+ });
178
+
137
179
  /**
138
180
  * Global variable given visible to users to control execution. This provides users a simple way to configure Transformers.js.
139
181
  * @typedef {Object} TransformersEnvironment
140
182
  * @property {string} version This version of Transformers.js.
141
- * @property {{onnx: Partial<import('onnxruntime-common').Env>}} backends Expose environment variables of different backends,
183
+ * @property {{onnx: Partial<import('onnxruntime-common').Env> & { setLogLevel?: (logLevel: number) => void }}} backends Expose environment variables of different backends,
142
184
  * allowing users to set these variables if they want to.
185
+ * @property {number} logLevel The logging level. Use LogLevel enum values. Defaults to LogLevel.ERROR.
143
186
  * @property {boolean} allowRemoteModels Whether to allow loading of remote files, defaults to `true`.
144
187
  * If set to `false`, it will have the same effect as setting `local_files_only=true` when loading pipelines, models, tokenizers, processors, etc.
145
188
  * @property {string} remoteHost Host URL to load models from. Defaults to the Hugging Face Hub.
@@ -158,8 +201,10 @@ const localModelPath = RUNNING_LOCALLY ? path.join(dirname__, DEFAULT_LOCAL_MODE
158
201
  * This can improve performance by avoiding repeated downloads of WASM files. Note: Only the WASM binary is cached.
159
202
  * The MJS loader file still requires network access unless you use a Service Worker.
160
203
  * @property {string} cacheKey The cache key to use for storing models and WASM binaries. Defaults to 'transformers-cache'.
204
+ * @property {(input: string | URL, init?: any) => Promise<any>} fetch The fetch function to use. Defaults to `fetch`.
161
205
  */
162
206
 
207
+ let logLevel = LogLevel.WARNING; // Default log level
163
208
  /** @type {TransformersEnvironment} */
164
209
  export const env = {
165
210
  version: VERSION,
@@ -171,17 +216,27 @@ export const env = {
171
216
  onnx: {},
172
217
  },
173
218
 
219
+ /////////////////// Logging settings ///////////////////
220
+ get logLevel() {
221
+ return logLevel;
222
+ },
223
+ set logLevel(level) {
224
+ logLevel = level;
225
+
226
+ // invoke hook to set ONNX Runtime log level when Transformers.js log level changes
227
+ env.backends.onnx?.setLogLevel?.(level);
228
+ },
174
229
  /////////////////// Model settings ///////////////////
175
230
  allowRemoteModels: true,
176
231
  remoteHost: 'https://huggingface.co/',
177
232
  remotePathTemplate: '{model}/resolve/{revision}/',
178
233
 
179
- allowLocalModels: !(IS_BROWSER_ENV || IS_WEBWORKER_ENV),
234
+ allowLocalModels: !(IS_BROWSER_ENV || IS_WEBWORKER_ENV || IS_DENO_WEB_RUNTIME), // Default to true for non-web environments, false for web environments
180
235
  localModelPath: localModelPath,
181
236
  useFS: IS_FS_AVAILABLE,
182
237
 
183
238
  /////////////////// Cache settings ///////////////////
184
- useBrowserCache: IS_WEB_CACHE_AVAILABLE && !IS_DENO_RUNTIME,
239
+ useBrowserCache: IS_WEB_CACHE_AVAILABLE,
185
240
 
186
241
  useFSCache: IS_FS_AVAILABLE,
187
242
  cacheDir: DEFAULT_CACHE_DIR,
@@ -191,6 +246,10 @@ export const env = {
191
246
 
192
247
  useWasmCache: IS_WEB_CACHE_AVAILABLE || IS_FS_AVAILABLE,
193
248
  cacheKey: 'transformers-cache',
249
+
250
+ /////////////////// Custom fetch /////////////////////
251
+ fetch: DEFAULT_FETCH,
252
+
194
253
  //////////////////////////////////////////////////////
195
254
  };
196
255
 
@@ -6,6 +6,7 @@ import { Callable } from '../utils/generic.js';
6
6
  import { Tensor, topk } from '../utils/tensor.js';
7
7
 
8
8
  import { max, softmax } from '../utils/maths.js';
9
+ import { _weightedIndex } from '../utils/random.js';
9
10
  import { GenerationConfig } from '../generation/configuration_utils.js';
10
11
 
11
12
  /**
@@ -64,24 +65,11 @@ export class LogitsSampler extends Callable {
64
65
 
65
66
  /**
66
67
  * Selects an item randomly based on the specified probabilities.
67
- * @param {import("../transformers.js").DataArray} probabilities An array of probabilities to use for selection.
68
+ * @param {Float32Array} probabilities An array of probabilities to use for selection.
68
69
  * @returns {number} The index of the selected item.
69
70
  */
70
71
  randomSelect(probabilities) {
71
- // Return index of chosen item
72
- let sumProbabilities = 0;
73
- for (let i = 0; i < probabilities.length; ++i) {
74
- sumProbabilities += probabilities[i];
75
- }
76
-
77
- let r = Math.random() * sumProbabilities;
78
- for (let i = 0; i < probabilities.length; ++i) {
79
- r -= probabilities[i];
80
- if (r <= 0) {
81
- return i;
82
- }
83
- }
84
- return 0; // return first (most probable) as a fallback
72
+ return _weightedIndex(probabilities);
85
73
  }
86
74
 
87
75
  /**
@@ -5,6 +5,7 @@ import { RawImage } from './utils/image.js';
5
5
  import { calculateReflectOffset } from './utils/core.js';
6
6
  import { getModelJSON } from './utils/hub.js';
7
7
  import { IMAGE_PROCESSOR_NAME } from './utils/constants.js';
8
+ import { logger } from './utils/logger.js';
8
9
 
9
10
  /**
10
11
  * Named tuple to indicate the order we are using is (height x width),
@@ -450,7 +451,7 @@ export function post_process_panoptic_segmentation(
450
451
  target_sizes = null,
451
452
  ) {
452
453
  if (label_ids_to_fuse === null) {
453
- console.warn('`label_ids_to_fuse` unset. No instance will be fused.');
454
+ logger.warn('`label_ids_to_fuse` unset. No instance will be fused.');
454
455
  label_ids_to_fuse = new Set();
455
456
  }
456
457
 
@@ -864,11 +865,6 @@ export class ImageProcessor extends Callable {
864
865
  return [newWidth, newHeight];
865
866
  } else if (this.size_divisibility !== undefined) {
866
867
  return enforce_size_divisibility([srcWidth, srcHeight], this.size_divisibility);
867
- } else if (this.min_pixels !== undefined && this.max_pixels !== undefined) {
868
- // Custom resize logic for Qwen2-VL models
869
- // @ts-expect-error TS2339
870
- const factor = this.config.patch_size * this.config.merge_size;
871
- return smart_resize(srcHeight, srcWidth, factor, this.min_pixels, this.max_pixels);
872
868
  } else {
873
869
  throw new Error(
874
870
  `Could not resize image due to unsupported \`this.size\` option in config: ${JSON.stringify(size)}`,
@@ -0,0 +1,5 @@
1
+ import { PreTrainedModel } from '../modeling_utils.js';
2
+
3
+ export class AfmoePreTrainedModel extends PreTrainedModel {}
4
+ export class AfmoeModel extends AfmoePreTrainedModel {}
5
+ export class AfmoeForCausalLM extends AfmoePreTrainedModel {}
@@ -2,6 +2,7 @@ import { getModelJSON } from '../../utils/hub.js';
2
2
  import { ImageProcessor } from '../../image_processors_utils.js';
3
3
  import * as AllImageProcessors from '../image_processors.js';
4
4
  import { GITHUB_ISSUE_URL, IMAGE_PROCESSOR_NAME } from '../../utils/constants.js';
5
+ import { logger } from '../../utils/logger.js';
5
6
 
6
7
  export class AutoImageProcessor {
7
8
  /** @type {typeof ImageProcessor.from_pretrained} */
@@ -20,7 +21,7 @@ export class AutoImageProcessor {
20
21
  if (!image_processor_class) {
21
22
  if (key !== undefined) {
22
23
  // Only log a warning if the class is not found and the key is set.
23
- console.warn(
24
+ logger.warn(
24
25
  `Image processor type '${key}' not found, assuming base ImageProcessor. Please report this at ${GITHUB_ISSUE_URL}.`,
25
26
  );
26
27
  }
@@ -43,6 +43,7 @@ import { PreTrainedModel } from '../modeling_utils.js';
43
43
  import { CUSTOM_ARCHITECTURES, MODEL_CLASS_TYPE_MAPPING, MODEL_MAPPINGS } from '../registry.js';
44
44
 
45
45
  import * as ALL_MODEL_FILES from '../models.js';
46
+ import { logger } from '../../utils/logger.js';
46
47
 
47
48
  /**
48
49
  * Base class of all AutoModels. Contains the `from_pretrained` function
@@ -114,7 +115,7 @@ class PretrainedMixin {
114
115
 
115
116
  if (this.BASE_IF_FAIL) {
116
117
  if (!CUSTOM_ARCHITECTURES.has(model_type)) {
117
- console.warn(`Unknown model class "${model_type}", attempting to construct from base class.`);
118
+ logger.warn(`Unknown model class "${model_type}", attempting to construct from base class.`);
118
119
  }
119
120
  return await PreTrainedModel.from_pretrained(pretrained_model_name_or_path, options);
120
121
  } else {
@@ -1,5 +1,6 @@
1
1
  import { PreTrainedTokenizer, loadTokenizer } from '../../tokenization_utils.js';
2
2
  import * as AllTokenizers from '../tokenizers.js';
3
+ import { logger } from '../../utils/logger.js';
3
4
 
4
5
  /**
5
6
  * Helper class which is used to instantiate pretrained tokenizers with the `from_pretrained` function.
@@ -54,7 +55,7 @@ export class AutoTokenizer {
54
55
 
55
56
  let cls = AllTokenizers[tokenizerName];
56
57
  if (!cls) {
57
- console.warn(`Unknown tokenizer class "${tokenizerName}", attempting to construct from base class.`);
58
+ logger.warn(`Unknown tokenizer class "${tokenizerName}", attempting to construct from base class.`);
58
59
  cls = PreTrainedTokenizer;
59
60
  }
60
61
  return new cls(tokenizerJSON, tokenizerConfig);
@@ -1,6 +1,7 @@
1
1
  import { FeatureExtractor, validate_audio_inputs } from '../../feature_extraction_utils.js';
2
2
  import { Tensor } from '../../utils/tensor.js';
3
3
  import { mel_filter_bank, spectrogram, window_function } from '../../utils/audio.js';
4
+ import { random } from '../../utils/random.js';
4
5
 
5
6
  export class ClapFeatureExtractor extends FeatureExtractor {
6
7
  constructor(config) {
@@ -58,7 +59,7 @@ export class ClapFeatureExtractor extends FeatureExtractor {
58
59
  if (diff > 0) {
59
60
  if (truncation === 'rand_trunc') {
60
61
  longer = true;
61
- const idx = Math.floor(Math.random() * (diff + 1));
62
+ const idx = Math.floor(random.random() * (diff + 1));
62
63
  waveform = waveform.subarray(idx, idx + max_length);
63
64
 
64
65
  input_mel = await this._extract_fbank_features(
@@ -1,5 +1,6 @@
1
1
  import { PreTrainedTokenizer } from '../../tokenization_utils.js';
2
2
  import { mergeArrays } from '../../utils/core.js';
3
+ import { logger } from '../../utils/logger.js';
3
4
 
4
5
  /**
5
6
  * @todo This model is not yet supported by Hugging Face's "fast" tokenizers library (https://github.com/huggingface/tokenizers).
@@ -18,7 +19,7 @@ export class MarianTokenizer extends PreTrainedTokenizer {
18
19
 
19
20
  this.supported_language_codes = Array.from(this.get_vocab().keys()).filter((x) => this.languageRegex.test(x));
20
21
 
21
- console.warn(
22
+ logger.warn(
22
23
  'WARNING: `MarianTokenizer` is not yet supported by Hugging Face\'s "fast" tokenizers library. Therefore, you may experience slightly inaccurate results.',
23
24
  );
24
25
  }
@@ -45,7 +46,7 @@ export class MarianTokenizer extends PreTrainedTokenizer {
45
46
  const [language, text] = remainder;
46
47
 
47
48
  if (!this.supported_language_codes.includes(language)) {
48
- console.warn(
49
+ logger.warn(
49
50
  `Unsupported language code "${language}" detected, which may lead to unexpected behavior. Should be one of: ${JSON.stringify(this.supported_language_codes)}`,
50
51
  );
51
52
  }