@huggingface/transformers 4.0.0-next.0 → 4.0.0-next.10

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 (364) hide show
  1. package/README.md +32 -6
  2. package/dist/ort-wasm-simd-threaded.jsep.mjs +31 -31
  3. package/dist/transformers.js +9261 -1709
  4. package/dist/transformers.min.js +25 -18
  5. package/dist/transformers.node.cjs +6119 -3174
  6. package/dist/transformers.node.min.cjs +25 -23
  7. package/dist/transformers.node.min.mjs +25 -23
  8. package/dist/transformers.node.mjs +6034 -3168
  9. package/dist/transformers.web.js +4255 -1381
  10. package/dist/transformers.web.min.js +23 -19
  11. package/package.json +6 -6
  12. package/src/backends/onnx.js +128 -53
  13. package/src/backends/utils/cacheWasm.js +28 -46
  14. package/src/cache_utils.js +62 -0
  15. package/src/configs.js +123 -23
  16. package/src/env.js +100 -11
  17. package/src/generation/logits_sampler.js +3 -15
  18. package/src/generation/parameters.js +1 -1
  19. package/src/generation/streamers.js +21 -0
  20. package/src/image_processors_utils.js +29 -23
  21. package/src/models/afmoe/modeling_afmoe.js +5 -0
  22. package/src/models/auto/image_processing_auto.js +2 -1
  23. package/src/models/auto/modeling_auto.js +16 -2
  24. package/src/models/auto/tokenization_auto.js +2 -1
  25. package/src/models/chatterbox/modeling_chatterbox.js +1 -1
  26. package/src/models/chmv2/image_processing_chmv2.js +3 -0
  27. package/src/models/chmv2/modeling_chmv2.js +4 -0
  28. package/src/models/clap/feature_extraction_clap.js +2 -1
  29. package/src/models/cohere2/modeling_cohere2.js +5 -0
  30. package/src/models/cohere_asr/feature_extraction_cohere_asr.js +117 -0
  31. package/src/models/cohere_asr/modeling_cohere_asr.js +11 -0
  32. package/src/models/cohere_asr/processing_cohere_asr.js +55 -0
  33. package/src/models/cohere_asr/tokenization_cohere_asr.js +3 -0
  34. package/src/models/deepseek_v3/modeling_deepseek_v3.js +5 -0
  35. package/src/models/detr/image_processing_detr.js +1 -1
  36. package/src/models/eurobert/modeling_eurobert.js +41 -0
  37. package/src/models/feature_extractors.js +3 -0
  38. package/src/models/gemma3/image_processing_gemma3.js +3 -0
  39. package/src/models/gemma3/modeling_gemma3.js +4 -1
  40. package/src/models/gemma3/processing_gemma3.js +45 -0
  41. package/src/models/gemma3n/modeling_gemma3n.js +2 -0
  42. package/src/models/glm46v/image_processing_glm46v.js +12 -0
  43. package/src/models/glm46v/processing_glm46v.js +5 -0
  44. package/src/models/glm_moe_dsa/modeling_glm_moe_dsa.js +5 -0
  45. package/src/models/glm_ocr/modeling_glm_ocr.js +78 -0
  46. package/src/models/granite_speech/feature_extraction_granite_speech.js +58 -0
  47. package/src/models/granite_speech/modeling_granite_speech.js +5 -0
  48. package/src/models/granite_speech/processing_granite_speech.js +62 -0
  49. package/src/models/grounding_dino/image_processing_grounding_dino.js +1 -1
  50. package/src/models/idefics3/modeling_idefics3.js +5 -32
  51. package/src/models/image_processors.js +4 -0
  52. package/src/models/lfm2_vl/image_processing_lfm2_vl.js +305 -0
  53. package/src/models/lfm2_vl/modeling_lfm2_vl.js +13 -0
  54. package/src/models/lfm2_vl/processing_lfm2_vl.js +77 -0
  55. package/src/models/lighton_ocr/modeling_lighton_ocr.js +3 -0
  56. package/src/models/llava/modeling_llava.js +1 -1
  57. package/src/models/marian/tokenization_marian.js +3 -2
  58. package/src/models/mistral3/modeling_mistral3.js +2 -2
  59. package/src/models/mistral4/modeling_mistral4.js +5 -0
  60. package/src/models/modeling_utils.js +283 -300
  61. package/src/models/models.js +26 -1
  62. package/src/models/nemotron_h/modeling_nemotron_h.js +5 -0
  63. package/src/models/olmo_hybrid/modeling_olmo_hybrid.js +5 -0
  64. package/src/models/paligemma/modeling_paligemma.js +2 -25
  65. package/src/models/paligemma/processing_paligemma.js +3 -2
  66. package/src/models/processors.js +8 -0
  67. package/src/models/qwen2_5_vl/modeling_qwen2_5_vl.js +9 -0
  68. package/src/models/qwen2_5_vl/processing_qwen2_5_vl.js +3 -0
  69. package/src/models/qwen2_moe/modeling_qwen2_moe.js +5 -0
  70. package/src/models/qwen2_vl/image_processing_qwen2_vl.js +15 -1
  71. package/src/models/qwen2_vl/modeling_qwen2_vl.js +240 -143
  72. package/src/models/qwen2_vl/processing_qwen2_vl.js +5 -4
  73. package/src/models/qwen3_5/modeling_qwen3_5.js +4 -0
  74. package/src/models/qwen3_5_moe/modeling_qwen3_5_moe.js +4 -0
  75. package/src/models/qwen3_moe/modeling_qwen3_moe.js +5 -0
  76. package/src/models/qwen3_next/modeling_qwen3_next.js +5 -0
  77. package/src/models/qwen3_vl/modeling_qwen3_vl.js +4 -0
  78. package/src/models/qwen3_vl/processing_qwen3_vl.js +3 -0
  79. package/src/models/qwen3_vl_moe/modeling_qwen3_vl_moe.js +4 -0
  80. package/src/models/registry.js +61 -5
  81. package/src/models/sam/image_processing_sam.js +1 -1
  82. package/src/models/session.js +33 -56
  83. package/src/models/smolvlm/modeling_smolvlm.js +7 -0
  84. package/src/models/solar_open/modeling_solar_open.js +5 -0
  85. package/src/models/tokenizers.js +1 -0
  86. package/src/models/ultravox/modeling_ultravox.js +1 -3
  87. package/src/models/voxtral/modeling_voxtral.js +3 -0
  88. package/src/models/voxtral_realtime/feature_extraction_voxtral_realtime.js +71 -0
  89. package/src/models/voxtral_realtime/modeling_voxtral_realtime.js +239 -0
  90. package/src/models/voxtral_realtime/processing_voxtral_realtime.js +113 -0
  91. package/src/models/whisper/feature_extraction_whisper.js +4 -13
  92. package/src/models/whisper/modeling_whisper.js +6 -5
  93. package/src/models/xlm/tokenization_xlm.js +2 -1
  94. package/src/pipelines/automatic-speech-recognition.js +47 -3
  95. package/src/pipelines/document-question-answering.js +1 -1
  96. package/src/pipelines/image-to-text.js +2 -2
  97. package/src/pipelines/index.js +313 -0
  98. package/src/pipelines/summarization.js +1 -1
  99. package/src/pipelines/text-generation.js +5 -1
  100. package/src/pipelines/text-to-audio.js +4 -2
  101. package/src/pipelines/text2text-generation.js +1 -1
  102. package/src/pipelines/translation.js +1 -1
  103. package/src/pipelines/zero-shot-classification.js +3 -2
  104. package/src/pipelines.js +140 -428
  105. package/src/tokenization_utils.js +42 -21
  106. package/src/transformers.js +10 -1
  107. package/src/utils/audio.js +20 -3
  108. package/src/utils/cache/CrossOriginStorageCache.js +251 -0
  109. package/src/utils/cache/FileCache.js +128 -0
  110. package/src/utils/cache/cross-origin-storage.d.ts +38 -0
  111. package/src/utils/cache.js +12 -4
  112. package/src/utils/core.js +23 -1
  113. package/src/utils/devices.js +22 -0
  114. package/src/utils/dtypes.js +55 -0
  115. package/src/utils/hub/{files.js → FileResponse.js} +0 -90
  116. package/src/utils/hub/utils.js +45 -5
  117. package/src/utils/hub.js +67 -23
  118. package/src/utils/image.js +14 -14
  119. package/src/utils/logger.js +67 -0
  120. package/src/utils/lru_cache.js +67 -0
  121. package/src/utils/memoize_promise.js +45 -0
  122. package/src/utils/model-loader.js +35 -17
  123. package/src/utils/model_registry/ModelRegistry.js +382 -0
  124. package/src/utils/model_registry/clear_cache.js +128 -0
  125. package/src/utils/model_registry/get_available_dtypes.js +68 -0
  126. package/src/utils/model_registry/get_file_metadata.js +162 -0
  127. package/src/utils/model_registry/get_files.js +42 -0
  128. package/src/utils/model_registry/get_model_files.js +114 -0
  129. package/src/utils/model_registry/get_pipeline_files.js +44 -0
  130. package/src/utils/model_registry/get_processor_files.js +20 -0
  131. package/src/utils/model_registry/get_tokenizer_files.js +21 -0
  132. package/src/utils/model_registry/is_cached.js +169 -0
  133. package/src/utils/model_registry/resolve_model_type.js +66 -0
  134. package/src/utils/random.js +225 -0
  135. package/src/utils/tensor.js +26 -23
  136. package/src/utils/video.js +2 -2
  137. package/types/backends/onnx.d.ts.map +1 -1
  138. package/types/backends/utils/cacheWasm.d.ts +3 -17
  139. package/types/backends/utils/cacheWasm.d.ts.map +1 -1
  140. package/types/cache_utils.d.ts +29 -0
  141. package/types/cache_utils.d.ts.map +1 -0
  142. package/types/configs.d.ts.map +1 -1
  143. package/types/env.d.ts +60 -27
  144. package/types/env.d.ts.map +1 -1
  145. package/types/generation/logits_sampler.d.ts +2 -2
  146. package/types/generation/logits_sampler.d.ts.map +1 -1
  147. package/types/generation/parameters.d.ts +1 -1
  148. package/types/generation/parameters.d.ts.map +1 -1
  149. package/types/generation/streamers.d.ts +1 -0
  150. package/types/generation/streamers.d.ts.map +1 -1
  151. package/types/image_processors_utils.d.ts +18 -1
  152. package/types/image_processors_utils.d.ts.map +1 -1
  153. package/types/models/afmoe/modeling_afmoe.d.ts +8 -0
  154. package/types/models/afmoe/modeling_afmoe.d.ts.map +1 -0
  155. package/types/models/{ast/modeling_ast.d.ts → audio_spectrogram_transformer/modeling_audio_spectrogram_transformer.d.ts} +1 -1
  156. package/types/models/audio_spectrogram_transformer/modeling_audio_spectrogram_transformer.d.ts.map +1 -0
  157. package/types/models/auto/image_processing_auto.d.ts.map +1 -1
  158. package/types/models/auto/modeling_auto.d.ts +6 -0
  159. package/types/models/auto/modeling_auto.d.ts.map +1 -1
  160. package/types/models/auto/tokenization_auto.d.ts.map +1 -1
  161. package/types/models/chmv2/image_processing_chmv2.d.ts +4 -0
  162. package/types/models/chmv2/image_processing_chmv2.d.ts.map +1 -0
  163. package/types/models/chmv2/modeling_chmv2.d.ts +6 -0
  164. package/types/models/chmv2/modeling_chmv2.d.ts.map +1 -0
  165. package/types/models/clap/feature_extraction_clap.d.ts.map +1 -1
  166. package/types/models/cohere2/modeling_cohere2.d.ts +8 -0
  167. package/types/models/cohere2/modeling_cohere2.d.ts.map +1 -0
  168. package/types/models/cohere_asr/feature_extraction_cohere_asr.d.ts +25 -0
  169. package/types/models/cohere_asr/feature_extraction_cohere_asr.d.ts.map +1 -0
  170. package/types/models/cohere_asr/modeling_cohere_asr.d.ts +9 -0
  171. package/types/models/cohere_asr/modeling_cohere_asr.d.ts.map +1 -0
  172. package/types/models/cohere_asr/processing_cohere_asr.d.ts +27 -0
  173. package/types/models/cohere_asr/processing_cohere_asr.d.ts.map +1 -0
  174. package/types/models/cohere_asr/tokenization_cohere_asr.d.ts +4 -0
  175. package/types/models/cohere_asr/tokenization_cohere_asr.d.ts.map +1 -0
  176. package/types/models/deepseek_v3/modeling_deepseek_v3.d.ts +8 -0
  177. package/types/models/deepseek_v3/modeling_deepseek_v3.d.ts.map +1 -0
  178. package/types/models/detr/image_processing_detr.d.ts +1 -1
  179. package/types/models/eurobert/modeling_eurobert.d.ts +36 -0
  180. package/types/models/eurobert/modeling_eurobert.d.ts.map +1 -0
  181. package/types/models/feature_extractors.d.ts +3 -0
  182. package/types/models/gemma3/image_processing_gemma3.d.ts +4 -0
  183. package/types/models/gemma3/image_processing_gemma3.d.ts.map +1 -0
  184. package/types/models/gemma3/modeling_gemma3.d.ts +4 -1
  185. package/types/models/gemma3/modeling_gemma3.d.ts.map +1 -1
  186. package/types/models/gemma3/processing_gemma3.d.ts +20 -0
  187. package/types/models/gemma3/processing_gemma3.d.ts.map +1 -0
  188. package/types/models/gemma3n/modeling_gemma3n.d.ts +2 -0
  189. package/types/models/gemma3n/modeling_gemma3n.d.ts.map +1 -1
  190. package/types/models/glm46v/image_processing_glm46v.d.ts +4 -0
  191. package/types/models/glm46v/image_processing_glm46v.d.ts.map +1 -0
  192. package/types/models/glm46v/processing_glm46v.d.ts +4 -0
  193. package/types/models/glm46v/processing_glm46v.d.ts.map +1 -0
  194. package/types/models/glm_moe_dsa/modeling_glm_moe_dsa.d.ts +8 -0
  195. package/types/models/glm_moe_dsa/modeling_glm_moe_dsa.d.ts.map +1 -0
  196. package/types/models/glm_ocr/modeling_glm_ocr.d.ts +26 -0
  197. package/types/models/glm_ocr/modeling_glm_ocr.d.ts.map +1 -0
  198. package/types/models/granite_speech/feature_extraction_granite_speech.d.ts +16 -0
  199. package/types/models/granite_speech/feature_extraction_granite_speech.d.ts.map +1 -0
  200. package/types/models/granite_speech/modeling_granite_speech.d.ts +4 -0
  201. package/types/models/granite_speech/modeling_granite_speech.d.ts.map +1 -0
  202. package/types/models/granite_speech/processing_granite_speech.d.ts +19 -0
  203. package/types/models/granite_speech/processing_granite_speech.d.ts.map +1 -0
  204. package/types/models/grounding_dino/image_processing_grounding_dino.d.ts +1 -1
  205. package/types/models/idefics3/modeling_idefics3.d.ts +2 -18
  206. package/types/models/idefics3/modeling_idefics3.d.ts.map +1 -1
  207. package/types/models/image_processors.d.ts +4 -0
  208. package/types/models/lfm2_vl/image_processing_lfm2_vl.d.ts +41 -0
  209. package/types/models/lfm2_vl/image_processing_lfm2_vl.d.ts.map +1 -0
  210. package/types/models/lfm2_vl/modeling_lfm2_vl.d.ts +4 -0
  211. package/types/models/lfm2_vl/modeling_lfm2_vl.d.ts.map +1 -0
  212. package/types/models/lfm2_vl/processing_lfm2_vl.d.ts +18 -0
  213. package/types/models/lfm2_vl/processing_lfm2_vl.d.ts.map +1 -0
  214. package/types/models/lighton_ocr/modeling_lighton_ocr.d.ts +4 -0
  215. package/types/models/lighton_ocr/modeling_lighton_ocr.d.ts.map +1 -0
  216. package/types/models/marian/tokenization_marian.d.ts.map +1 -1
  217. package/types/models/mistral3/modeling_mistral3.d.ts +2 -2
  218. package/types/models/mistral3/modeling_mistral3.d.ts.map +1 -1
  219. package/types/models/mistral4/modeling_mistral4.d.ts +8 -0
  220. package/types/models/mistral4/modeling_mistral4.d.ts.map +1 -0
  221. package/types/models/modeling_utils.d.ts +46 -27
  222. package/types/models/modeling_utils.d.ts.map +1 -1
  223. package/types/models/models.d.ts +26 -1
  224. package/types/models/nemotron_h/modeling_nemotron_h.d.ts +8 -0
  225. package/types/models/nemotron_h/modeling_nemotron_h.d.ts.map +1 -0
  226. package/types/models/olmo_hybrid/modeling_olmo_hybrid.d.ts +8 -0
  227. package/types/models/olmo_hybrid/modeling_olmo_hybrid.d.ts.map +1 -0
  228. package/types/models/paligemma/modeling_paligemma.d.ts +2 -8
  229. package/types/models/paligemma/modeling_paligemma.d.ts.map +1 -1
  230. package/types/models/paligemma/processing_paligemma.d.ts.map +1 -1
  231. package/types/models/processors.d.ts +8 -0
  232. package/types/models/qwen2_5_vl/modeling_qwen2_5_vl.d.ts +7 -0
  233. package/types/models/qwen2_5_vl/modeling_qwen2_5_vl.d.ts.map +1 -0
  234. package/types/models/qwen2_5_vl/processing_qwen2_5_vl.d.ts +4 -0
  235. package/types/models/qwen2_5_vl/processing_qwen2_5_vl.d.ts.map +1 -0
  236. package/types/models/qwen2_moe/modeling_qwen2_moe.d.ts +8 -0
  237. package/types/models/qwen2_moe/modeling_qwen2_moe.d.ts.map +1 -0
  238. package/types/models/qwen2_vl/image_processing_qwen2_vl.d.ts +3 -0
  239. package/types/models/qwen2_vl/image_processing_qwen2_vl.d.ts.map +1 -1
  240. package/types/models/qwen2_vl/modeling_qwen2_vl.d.ts +44 -6
  241. package/types/models/qwen2_vl/modeling_qwen2_vl.d.ts.map +1 -1
  242. package/types/models/qwen2_vl/processing_qwen2_vl.d.ts +1 -0
  243. package/types/models/qwen2_vl/processing_qwen2_vl.d.ts.map +1 -1
  244. package/types/models/qwen3_5/modeling_qwen3_5.d.ts +6 -0
  245. package/types/models/qwen3_5/modeling_qwen3_5.d.ts.map +1 -0
  246. package/types/models/qwen3_5_moe/modeling_qwen3_5_moe.d.ts +7 -0
  247. package/types/models/qwen3_5_moe/modeling_qwen3_5_moe.d.ts.map +1 -0
  248. package/types/models/qwen3_moe/modeling_qwen3_moe.d.ts +8 -0
  249. package/types/models/qwen3_moe/modeling_qwen3_moe.d.ts.map +1 -0
  250. package/types/models/qwen3_next/modeling_qwen3_next.d.ts +8 -0
  251. package/types/models/qwen3_next/modeling_qwen3_next.d.ts.map +1 -0
  252. package/types/models/qwen3_vl/modeling_qwen3_vl.d.ts +7 -0
  253. package/types/models/qwen3_vl/modeling_qwen3_vl.d.ts.map +1 -0
  254. package/types/models/qwen3_vl/processing_qwen3_vl.d.ts +4 -0
  255. package/types/models/qwen3_vl/processing_qwen3_vl.d.ts.map +1 -0
  256. package/types/models/qwen3_vl_moe/modeling_qwen3_vl_moe.d.ts +7 -0
  257. package/types/models/qwen3_vl_moe/modeling_qwen3_vl_moe.d.ts.map +1 -0
  258. package/types/models/registry.d.ts +2 -1
  259. package/types/models/registry.d.ts.map +1 -1
  260. package/types/models/sam/image_processing_sam.d.ts +1 -1
  261. package/types/models/session.d.ts +3 -2
  262. package/types/models/session.d.ts.map +1 -1
  263. package/types/models/smolvlm/modeling_smolvlm.d.ts +8 -0
  264. package/types/models/smolvlm/modeling_smolvlm.d.ts.map +1 -0
  265. package/types/models/solar_open/modeling_solar_open.d.ts +8 -0
  266. package/types/models/solar_open/modeling_solar_open.d.ts.map +1 -0
  267. package/types/models/tokenizers.d.ts +1 -0
  268. package/types/models/ultravox/modeling_ultravox.d.ts +0 -2
  269. package/types/models/ultravox/modeling_ultravox.d.ts.map +1 -1
  270. package/types/models/voxtral/modeling_voxtral.d.ts +4 -0
  271. package/types/models/voxtral/modeling_voxtral.d.ts.map +1 -0
  272. package/types/models/voxtral_realtime/feature_extraction_voxtral_realtime.d.ts +28 -0
  273. package/types/models/voxtral_realtime/feature_extraction_voxtral_realtime.d.ts.map +1 -0
  274. package/types/models/voxtral_realtime/modeling_voxtral_realtime.d.ts +17 -0
  275. package/types/models/voxtral_realtime/modeling_voxtral_realtime.d.ts.map +1 -0
  276. package/types/models/voxtral_realtime/processing_voxtral_realtime.d.ts +44 -0
  277. package/types/models/voxtral_realtime/processing_voxtral_realtime.d.ts.map +1 -0
  278. package/types/models/whisper/feature_extraction_whisper.d.ts.map +1 -1
  279. package/types/models/whisper/modeling_whisper.d.ts.map +1 -1
  280. package/types/models/xlm/tokenization_xlm.d.ts.map +1 -1
  281. package/types/pipelines/automatic-speech-recognition.d.ts +7 -2
  282. package/types/pipelines/automatic-speech-recognition.d.ts.map +1 -1
  283. package/types/pipelines/document-question-answering.d.ts +2 -2
  284. package/types/pipelines/document-question-answering.d.ts.map +1 -1
  285. package/types/pipelines/image-to-text.d.ts +4 -4
  286. package/types/pipelines/image-to-text.d.ts.map +1 -1
  287. package/types/pipelines/index.d.ts +265 -0
  288. package/types/pipelines/index.d.ts.map +1 -0
  289. package/types/pipelines/summarization.d.ts +2 -2
  290. package/types/pipelines/summarization.d.ts.map +1 -1
  291. package/types/pipelines/text-generation.d.ts +7 -3
  292. package/types/pipelines/text-generation.d.ts.map +1 -1
  293. package/types/pipelines/text-to-audio.d.ts.map +1 -1
  294. package/types/pipelines/text2text-generation.d.ts +3 -3
  295. package/types/pipelines/text2text-generation.d.ts.map +1 -1
  296. package/types/pipelines/translation.d.ts +2 -2
  297. package/types/pipelines/translation.d.ts.map +1 -1
  298. package/types/pipelines/zero-shot-classification.d.ts.map +1 -1
  299. package/types/pipelines.d.ts +51 -291
  300. package/types/pipelines.d.ts.map +1 -1
  301. package/types/tokenization_utils.d.ts +44 -26
  302. package/types/tokenization_utils.d.ts.map +1 -1
  303. package/types/transformers.d.ts +7 -1
  304. package/types/transformers.d.ts.map +1 -1
  305. package/types/utils/audio.d.ts +5 -2
  306. package/types/utils/audio.d.ts.map +1 -1
  307. package/types/utils/cache/CrossOriginStorageCache.d.ts +120 -0
  308. package/types/utils/cache/CrossOriginStorageCache.d.ts.map +1 -0
  309. package/types/utils/cache/FileCache.d.ts +39 -0
  310. package/types/utils/cache/FileCache.d.ts.map +1 -0
  311. package/types/utils/cache.d.ts +10 -4
  312. package/types/utils/cache.d.ts.map +1 -1
  313. package/types/utils/core.d.ts +59 -2
  314. package/types/utils/core.d.ts.map +1 -1
  315. package/types/utils/devices.d.ts +15 -0
  316. package/types/utils/devices.d.ts.map +1 -1
  317. package/types/utils/dtypes.d.ts +17 -1
  318. package/types/utils/dtypes.d.ts.map +1 -1
  319. package/types/utils/hub/{files.d.ts → FileResponse.d.ts} +1 -32
  320. package/types/utils/hub/FileResponse.d.ts.map +1 -0
  321. package/types/utils/hub/utils.d.ts +19 -3
  322. package/types/utils/hub/utils.d.ts.map +1 -1
  323. package/types/utils/hub.d.ts +36 -7
  324. package/types/utils/hub.d.ts.map +1 -1
  325. package/types/utils/image.d.ts +1 -1
  326. package/types/utils/logger.d.ts +28 -0
  327. package/types/utils/logger.d.ts.map +1 -0
  328. package/types/utils/lru_cache.d.ts +38 -0
  329. package/types/utils/lru_cache.d.ts.map +1 -0
  330. package/types/utils/memoize_promise.d.ts +14 -0
  331. package/types/utils/memoize_promise.d.ts.map +1 -0
  332. package/types/utils/model-loader.d.ts +15 -0
  333. package/types/utils/model-loader.d.ts.map +1 -1
  334. package/types/utils/model_registry/ModelRegistry.d.ts +298 -0
  335. package/types/utils/model_registry/ModelRegistry.d.ts.map +1 -0
  336. package/types/utils/model_registry/clear_cache.d.ts +74 -0
  337. package/types/utils/model_registry/clear_cache.d.ts.map +1 -0
  338. package/types/utils/model_registry/get_available_dtypes.d.ts +26 -0
  339. package/types/utils/model_registry/get_available_dtypes.d.ts.map +1 -0
  340. package/types/utils/model_registry/get_file_metadata.d.ts +20 -0
  341. package/types/utils/model_registry/get_file_metadata.d.ts.map +1 -0
  342. package/types/utils/model_registry/get_files.d.ts +23 -0
  343. package/types/utils/model_registry/get_files.d.ts.map +1 -0
  344. package/types/utils/model_registry/get_model_files.d.ts +48 -0
  345. package/types/utils/model_registry/get_model_files.d.ts.map +1 -0
  346. package/types/utils/model_registry/get_pipeline_files.d.ts +22 -0
  347. package/types/utils/model_registry/get_pipeline_files.d.ts.map +1 -0
  348. package/types/utils/model_registry/get_processor_files.d.ts +9 -0
  349. package/types/utils/model_registry/get_processor_files.d.ts.map +1 -0
  350. package/types/utils/model_registry/get_tokenizer_files.d.ts +9 -0
  351. package/types/utils/model_registry/get_tokenizer_files.d.ts.map +1 -0
  352. package/types/utils/model_registry/is_cached.d.ts +105 -0
  353. package/types/utils/model_registry/is_cached.d.ts.map +1 -0
  354. package/types/utils/model_registry/resolve_model_type.d.ts +24 -0
  355. package/types/utils/model_registry/resolve_model_type.d.ts.map +1 -0
  356. package/types/utils/random.d.ts +86 -0
  357. package/types/utils/random.d.ts.map +1 -0
  358. package/types/utils/tensor.d.ts.map +1 -1
  359. package/src/utils/data-structures.js +0 -572
  360. package/types/models/ast/modeling_ast.d.ts.map +0 -1
  361. package/types/utils/data-structures.d.ts +0 -294
  362. package/types/utils/data-structures.d.ts.map +0 -1
  363. package/types/utils/hub/files.d.ts.map +0 -1
  364. /package/src/models/{ast/modeling_ast.js → audio_spectrogram_transformer/modeling_audio_spectrogram_transformer.js} +0 -0
@@ -0,0 +1,169 @@
1
+ import { getCache } from '../cache.js';
2
+ import { buildResourcePaths, checkCachedResource } from '../hub.js';
3
+ import { get_files } from './get_files.js';
4
+ import { get_pipeline_files } from './get_pipeline_files.js';
5
+
6
+ /**
7
+ * @typedef {Object} FileCacheStatus
8
+ * @property {string} file - The file path
9
+ * @property {boolean} cached - Whether the file is cached
10
+ */
11
+
12
+ /**
13
+ * @typedef {Object} CacheCheckResult
14
+ * @property {boolean} allCached - Whether all files are cached
15
+ * @property {FileCacheStatus[]} files - Array of files with their cache status
16
+ */
17
+
18
+ /**
19
+ * Internal helper to check cache status for a list of files
20
+ * @private
21
+ * @param {string} modelId - The model id
22
+ * @param {string[]} files - List of file paths to check
23
+ * @param {Object} options - Options including cache_dir
24
+ * @returns {Promise<CacheCheckResult>}
25
+ */
26
+ async function check_files_cache(modelId, files, options = {}) {
27
+ const cache = await getCache(options?.cache_dir);
28
+
29
+ if (!cache) {
30
+ const fileStatuses = files.map((filename) => ({ file: filename, cached: false }));
31
+ // No cache available, all files considered not cached
32
+ return { allCached: false, files: fileStatuses };
33
+ }
34
+
35
+ const fileStatuses = await Promise.all(
36
+ files.map(async (filename) => {
37
+ const { localPath, proposedCacheKey } = buildResourcePaths(modelId, filename, options, cache);
38
+ const cached = await checkCachedResource(cache, localPath, proposedCacheKey);
39
+ return { file: filename, cached: !!cached };
40
+ }),
41
+ );
42
+
43
+ return { allCached: fileStatuses.every((f) => f.cached), files: fileStatuses };
44
+ }
45
+
46
+ /**
47
+ * Internal helper to check whether a single file is cached.
48
+ * @private
49
+ * @param {string} modelId - The model id
50
+ * @param {string} filename - The file path to check
51
+ * @param {Object} options - Options including cache_dir
52
+ * @returns {Promise<boolean>}
53
+ */
54
+ async function is_file_cached(modelId, filename, options = {}) {
55
+ const cache = await getCache(options?.cache_dir);
56
+ if (!cache) return false;
57
+ const { localPath, proposedCacheKey } = buildResourcePaths(modelId, filename, options, cache);
58
+ return !!(await checkCachedResource(cache, localPath, proposedCacheKey));
59
+ }
60
+
61
+ /**
62
+ * Quickly checks if a model is cached by verifying that `config.json` is present,
63
+ * then confirming all required files are cached.
64
+ * Returns a plain boolean — use `is_cached_files` if you need per-file detail.
65
+ *
66
+ * @param {string} modelId The model id (e.g., "Xenova/gpt2")
67
+ * @param {Object} [options] Optional parameters
68
+ * @param {string} [options.cache_dir] Custom cache directory
69
+ * @param {string} [options.revision] Model revision (default: 'main')
70
+ * @param {import('../../configs.js').PretrainedConfig} [options.config] Pre-loaded config
71
+ * @param {import('../dtypes.js').DataType|Record<string, import('../dtypes.js').DataType>} [options.dtype] Override dtype
72
+ * @param {import('../devices.js').DeviceType|Record<string, import('../devices.js').DeviceType>} [options.device] Override device
73
+ * @returns {Promise<boolean>} Whether all required files are cached
74
+ */
75
+ export async function is_cached(modelId, options = {}) {
76
+ if (!modelId) {
77
+ throw new Error('modelId is required');
78
+ }
79
+
80
+ // Fast early-exit: if config.json is missing we can expect the rest not to be cached too.
81
+ if (!(await is_file_cached(modelId, 'config.json', options))) {
82
+ return false;
83
+ }
84
+
85
+ const files = await get_files(modelId, options);
86
+ const result = await check_files_cache(modelId, files, options);
87
+ return result.allCached;
88
+ }
89
+
90
+ /**
91
+ * Checks if all files for a given model are already cached, with per-file detail.
92
+ * Automatically determines which files are needed using get_files().
93
+ *
94
+ * @param {string} modelId The model id (e.g., "Xenova/gpt2")
95
+ * @param {Object} [options] Optional parameters
96
+ * @param {string} [options.cache_dir] Custom cache directory
97
+ * @param {string} [options.revision] Model revision (default: 'main')
98
+ * @param {import('../../configs.js').PretrainedConfig} [options.config] Pre-loaded config
99
+ * @param {import('../dtypes.js').DataType|Record<string, import('../dtypes.js').DataType>} [options.dtype] Override dtype
100
+ * @param {import('../devices.js').DeviceType|Record<string, import('../devices.js').DeviceType>} [options.device] Override device
101
+ * @returns {Promise<CacheCheckResult>} Object with allCached boolean and files array with cache status
102
+ */
103
+ export async function is_cached_files(modelId, options = {}) {
104
+ if (!modelId) {
105
+ throw new Error('modelId is required');
106
+ }
107
+
108
+ const files = await get_files(modelId, options);
109
+ return await check_files_cache(modelId, files, options);
110
+ }
111
+
112
+ /**
113
+ * Quickly checks if all files for a specific pipeline task are cached by verifying
114
+ * that `config.json` is present, then confirming all required files are cached.
115
+ * Returns a plain boolean — use `is_pipeline_cached_files` if you need per-file detail.
116
+ *
117
+ * @param {string} task - The pipeline task (e.g., "text-generation", "image-classification")
118
+ * @param {string} modelId - The model id (e.g., "Xenova/gpt2")
119
+ * @param {Object} [options] - Optional parameters
120
+ * @param {string} [options.cache_dir] - Custom cache directory
121
+ * @param {string} [options.revision] - Model revision (default: 'main')
122
+ * @param {import('../../configs.js').PretrainedConfig} [options.config] - Pre-loaded config
123
+ * @param {import('../dtypes.js').DataType|Record<string, import('../dtypes.js').DataType>} [options.dtype] - Override dtype
124
+ * @param {import('../devices.js').DeviceType|Record<string, import('../devices.js').DeviceType>} [options.device] - Override device
125
+ * @returns {Promise<boolean>} Whether all required files are cached
126
+ */
127
+ export async function is_pipeline_cached(task, modelId, options = {}) {
128
+ if (!task) {
129
+ throw new Error('task is required');
130
+ }
131
+ if (!modelId) {
132
+ throw new Error('modelId is required');
133
+ }
134
+
135
+ // Fast early-exit: if config.json is missing we can expect the rest not to be cached too.
136
+ if (!(await is_file_cached(modelId, 'config.json', options))) {
137
+ return false;
138
+ }
139
+
140
+ const files = await get_pipeline_files(task, modelId, options);
141
+ const result = await check_files_cache(modelId, files, options);
142
+ return result.allCached;
143
+ }
144
+
145
+ /**
146
+ * Checks if all files for a specific pipeline task are already cached, with per-file detail.
147
+ * Automatically determines which components are needed based on the task.
148
+ *
149
+ * @param {string} task - The pipeline task (e.g., "text-generation", "image-classification")
150
+ * @param {string} modelId - The model id (e.g., "Xenova/gpt2")
151
+ * @param {Object} [options] - Optional parameters
152
+ * @param {string} [options.cache_dir] - Custom cache directory
153
+ * @param {string} [options.revision] - Model revision (default: 'main')
154
+ * @param {import('../../configs.js').PretrainedConfig} [options.config] - Pre-loaded config
155
+ * @param {import('../dtypes.js').DataType|Record<string, import('../dtypes.js').DataType>} [options.dtype] - Override dtype
156
+ * @param {import('../devices.js').DeviceType|Record<string, import('../devices.js').DeviceType>} [options.device] - Override device
157
+ * @returns {Promise<CacheCheckResult>} Object with allCached boolean and files array with cache status
158
+ */
159
+ export async function is_pipeline_cached_files(task, modelId, options = {}) {
160
+ if (!task) {
161
+ throw new Error('task is required');
162
+ }
163
+ if (!modelId) {
164
+ throw new Error('modelId is required');
165
+ }
166
+
167
+ const files = await get_pipeline_files(task, modelId, options);
168
+ return await check_files_cache(modelId, files, options);
169
+ }
@@ -0,0 +1,66 @@
1
+ import { MODEL_MAPPING_NAMES, MODEL_TYPES, MODEL_TYPE_MAPPING } from '../../models/modeling_utils.js';
2
+ import { GITHUB_ISSUE_URL } from '../constants.js';
3
+ import { logger } from '../logger.js';
4
+
5
+ /**
6
+ * @typedef {import('../../configs.js').PretrainedConfig} PretrainedConfig
7
+ */
8
+
9
+ /**
10
+ * Resolves the model type (e.g., EncoderOnly, DecoderOnly, Seq2Seq, …) from a
11
+ * model config by checking architectures and model_type against the known
12
+ * MODEL_TYPE_MAPPING.
13
+ *
14
+ * Resolution order:
15
+ * 1. `config.architectures` entries looked up in MODEL_TYPE_MAPPING
16
+ * 2. `config.model_type` looked up directly in MODEL_TYPE_MAPPING
17
+ * 3. `config.model_type` looked up via MODEL_MAPPING_NAMES → architecture → MODEL_TYPE_MAPPING
18
+ * 4. Fallback to `MODEL_TYPES.EncoderOnly`
19
+ *
20
+ * @param {PretrainedConfig} config The model config object.
21
+ * @param {{ warn?: boolean }} [options] Set `warn` to false to suppress the
22
+ * fallback warning (defaults to true).
23
+ * @returns {number} One of the MODEL_TYPES enum values.
24
+ */
25
+ export function resolve_model_type(config, { warn = true } = {}) {
26
+ // @ts-ignore - architectures is set via Object.assign in PretrainedConfig constructor
27
+ const architectures = /** @type {string[]} */ (config.architectures || []);
28
+
29
+ // 1. Try architectures against MODEL_TYPE_MAPPING
30
+ for (const arch of architectures) {
31
+ const mappedType = MODEL_TYPE_MAPPING.get(arch);
32
+ if (mappedType !== undefined) {
33
+ return mappedType;
34
+ }
35
+ }
36
+
37
+ // 2. Try config.model_type directly
38
+ if (config.model_type) {
39
+ const mappedType = MODEL_TYPE_MAPPING.get(config.model_type);
40
+ if (mappedType !== undefined) {
41
+ return mappedType;
42
+ }
43
+
44
+ // 3. Try MODEL_MAPPING_NAMES as a last resort
45
+ for (const mapping of Object.values(MODEL_MAPPING_NAMES)) {
46
+ if (mapping.has(config.model_type)) {
47
+ const resolved = MODEL_TYPE_MAPPING.get(mapping.get(config.model_type));
48
+ if (resolved !== undefined) {
49
+ return resolved;
50
+ }
51
+ }
52
+ }
53
+ }
54
+
55
+ // 4. Fallback
56
+ if (warn) {
57
+ const archList = architectures.length > 0 ? architectures.join(', ') : '(none)';
58
+ logger.warn(
59
+ `[resolve_model_type] Architecture(s) not found in MODEL_TYPE_MAPPING: [${archList}] ` +
60
+ `for model type '${config.model_type}'. Falling back to EncoderOnly (single model.onnx file). ` +
61
+ `If you encounter issues, please report at: ${GITHUB_ISSUE_URL}`,
62
+ );
63
+ }
64
+
65
+ return MODEL_TYPES.EncoderOnly;
66
+ }
@@ -0,0 +1,225 @@
1
+ /**
2
+ * Let there be order amidst the chaos.
3
+ *
4
+ * This file implements Mersenne Twister 19937, matching Python's `random` module exactly for reproducibility.
5
+ *
6
+ * ```javascript
7
+ * import { random } from '@huggingface/transformers';
8
+ *
9
+ * random.seed(42);
10
+ * random.random(); // 0.6394267984578837 (matches Python)
11
+ * random.gauss(0, 1); // normal-distributed value
12
+ * random.choices(['a','b'], [3, 1]); // weighted pick
13
+ *
14
+ * const arr = [1, 2, 3, 4, 5];
15
+ * random.shuffle(arr); // in-place Fisher-Yates shuffle
16
+ *
17
+ * // Use a separate instance to avoid affecting the global state:
18
+ * const rng = new random.Random(42);
19
+ * rng.random(); // 0.6394267984578837 (same seed, independent state)
20
+ * ```
21
+ *
22
+ * **Note on Reproducibility:**
23
+ * Similarly to the [Python random](https://docs.python.org/3/library/random.html#notes-on-reproducibility)
24
+ * module, it is useful to be able to reproduce the sequences given by a pseudo-random number generator.
25
+ * By reusing a seed value, the same sequence should be reproducible from run to run as long as multiple
26
+ * threads or asynchronous operations are not running concurrently.
27
+ *
28
+ * @module utils/random
29
+ */
30
+
31
+ import { apis } from '../env.js';
32
+
33
+ /**
34
+ * Mersenne Twister 19937 PRNG, matching Python's `random.Random` class exactly.
35
+ *
36
+ * Each instance has its own independent state, so seeding one instance does not
37
+ * affect any other instance or the global helper functions.
38
+ *
39
+ * @example
40
+ * const rng1 = new Random(42);
41
+ * const rng2 = new Random(42);
42
+ * rng1.random() === rng2.random(); // true (same seed, independent state)
43
+ */
44
+ export class Random {
45
+ constructor(seed) {
46
+ this._mt = new Uint32Array(624);
47
+ this._idx = 625;
48
+ this._gauss_next = null;
49
+ this._random_fn = this.random.bind(this);
50
+ this.seed(seed);
51
+ }
52
+
53
+ /**
54
+ * Seeds this instance's PRNG.
55
+ *
56
+ * When called with a number, initializes the state deterministically from that value.
57
+ * When called with no arguments (or `undefined`/`null`), seeds from OS entropy
58
+ * via `crypto.getRandomValues`, matching Python's `random.seed()` behaviour.
59
+ *
60
+ * @param {number} [n] The seed value. Omit to seed from OS entropy.
61
+ */
62
+ seed(n) {
63
+ if (n === undefined || n === null) {
64
+ if (apis.IS_CRYPTO_AVAILABLE) {
65
+ const buf = new Uint32Array(1);
66
+ crypto.getRandomValues(buf);
67
+ n = buf[0];
68
+ } else {
69
+ n = Date.now() >>> 0;
70
+ }
71
+ }
72
+ const mt = this._mt;
73
+ const u = (a, b) => Math.imul(a, b) >>> 0,
74
+ key = [];
75
+ for (let v = n || 0; v > 0; v = Math.floor(v / 0x100000000)) key.push(v & 0xffffffff);
76
+ if (!key.length) key.push(0);
77
+ mt[0] = 19650218;
78
+ for (let k = 1; k < 624; ++k) mt[k] = (u(1812433253, mt[k - 1] ^ (mt[k - 1] >>> 30)) + k) >>> 0;
79
+ let i = 1,
80
+ j = 0;
81
+ for (let k = Math.max(624, key.length); k > 0; --k, ++i, ++j) {
82
+ if (i >= 624) {
83
+ mt[0] = mt[623];
84
+ i = 1;
85
+ }
86
+ if (j >= key.length) j = 0;
87
+ mt[i] = ((mt[i] ^ u(mt[i - 1] ^ (mt[i - 1] >>> 30), 1664525)) + key[j] + j) >>> 0;
88
+ }
89
+ for (let k = 623; k > 0; --k, ++i) {
90
+ if (i >= 624) {
91
+ mt[0] = mt[623];
92
+ i = 1;
93
+ }
94
+ mt[i] = ((mt[i] ^ u(mt[i - 1] ^ (mt[i - 1] >>> 30), 1566083941)) - i) >>> 0;
95
+ }
96
+ mt[0] = 0x80000000;
97
+ this._idx = 624;
98
+ this._gauss_next = null;
99
+ }
100
+
101
+ /**
102
+ * Generates a random unsigned 32-bit integer.
103
+ *
104
+ * Performs the "twist" step when the state buffer is exhausted,
105
+ * then applies the standard MT19937 tempering transform.
106
+ *
107
+ * @returns {number} A random integer in the range [0, 2^32 - 1].
108
+ */
109
+ _int32() {
110
+ const mt = this._mt;
111
+ if (this._idx >= 624) {
112
+ for (let k = 0; k < 624; ++k) {
113
+ // twist
114
+ const y = (mt[k] & 0x80000000) | (mt[(k + 1) % 624] & 0x7fffffff);
115
+ mt[k] = (mt[(k + 397) % 624] ^ (y >>> 1) ^ (y & 1 ? 0x9908b0df : 0)) >>> 0;
116
+ }
117
+ this._idx = 0;
118
+ }
119
+ let y = mt[this._idx++];
120
+ y ^= y >>> 11;
121
+ y ^= (y << 7) & 0x9d2c5680;
122
+ y ^= (y << 15) & 0xefc60000;
123
+ y ^= y >>> 18;
124
+ return y >>> 0;
125
+ }
126
+
127
+ /**
128
+ * Generates a random floating-point number in the half-open interval [0, 1).
129
+ *
130
+ * Combines two 32-bit integers (using 53 bits of precision) to produce
131
+ * a uniformly distributed double, matching Python's `random.random()`.
132
+ *
133
+ * @returns {number} A random float in [0, 1).
134
+ */
135
+ random() {
136
+ return ((this._int32() >>> 5) * 67108864.0 + (this._int32() >>> 6)) / 9007199254740992.0;
137
+ }
138
+
139
+ /**
140
+ * Generates a random number from a Gaussian (normal) distribution.
141
+ *
142
+ * Uses the Box-Muller transform with a cached spare value,
143
+ * matching Python's `random.gauss()` output for the same seed.
144
+ *
145
+ * @param {number} [mu=0] The mean of the distribution.
146
+ * @param {number} [sigma=1] The standard deviation of the distribution.
147
+ * @returns {number} A normally distributed random value.
148
+ */
149
+ gauss(mu = 0, sigma = 1) {
150
+ let z = this._gauss_next;
151
+ this._gauss_next = null;
152
+ if (z === null) {
153
+ const x2pi = this.random() * 2 * Math.PI,
154
+ g2rad = Math.sqrt(-2 * Math.log(1 - this.random()));
155
+ z = Math.cos(x2pi) * g2rad;
156
+ this._gauss_next = Math.sin(x2pi) * g2rad;
157
+ }
158
+ return mu + z * sigma;
159
+ }
160
+
161
+ /**
162
+ * Shuffles an array in-place using the Fisher-Yates algorithm.
163
+ *
164
+ * Uses rejection sampling via `getrandbits`-style bit masking to ensure
165
+ * a uniform distribution, matching Python's `random.shuffle()`.
166
+ *
167
+ * @param {any[]} arr The array to shuffle in-place.
168
+ */
169
+ shuffle(arr) {
170
+ for (let i = arr.length - 1; i > 0; --i) {
171
+ const k = 32 - Math.clz32(i + 1);
172
+ let r = this._int32() >>> (32 - k);
173
+ while (r > i) r = this._int32() >>> (32 - k);
174
+ const t = arr[i];
175
+ arr[i] = arr[r];
176
+ arr[r] = t;
177
+ }
178
+ }
179
+
180
+ /**
181
+ * Selects a single element from a weighted population.
182
+ *
183
+ * Matches Python's `random.choices(population, weights=weights, k=1)[0]`
184
+ *
185
+ * @param {any[]} population The array of items to choose from.
186
+ * @param {number[]} weights An array of non-negative weights, one per population element.
187
+ * @returns {*} A single randomly selected element from the population.
188
+ */
189
+ choices(population, weights) {
190
+ return population[_weightedIndexWith(this._random_fn, weights)];
191
+ }
192
+ }
193
+
194
+ /**
195
+ * Returns a random index into `weights`, where each index's probability
196
+ * is proportional to its weight. Uses a linear scan: O(n) time, O(1) memory.
197
+ *
198
+ * @param {() => number} randomFn A function returning a uniform random float in [0, 1).
199
+ * @param {ArrayLike<number>} weights Non-negative weights.
200
+ * @returns {number} A randomly selected index in `[0, weights.length)`.
201
+ */
202
+ function _weightedIndexWith(randomFn, weights) {
203
+ let sum = 0;
204
+ for (let i = 0; i < weights.length; ++i) sum += weights[i];
205
+ let x = randomFn() * sum;
206
+ for (let i = 0; i < weights.length; ++i) {
207
+ x -= weights[i];
208
+ if (x < 0) return i;
209
+ }
210
+ return weights.length - 1; // floating-point guard
211
+ }
212
+
213
+ // Global default instance: mirrors the module-level functions in Python's `random` module.
214
+ const _default = new Random();
215
+ export const random = Object.freeze({
216
+ Random,
217
+ seed: _default.seed.bind(_default),
218
+ random: _default.random.bind(_default),
219
+ gauss: _default.gauss.bind(_default),
220
+ shuffle: _default.shuffle.bind(_default),
221
+ choices: _default.choices.bind(_default),
222
+ });
223
+
224
+ // Private helper function, used by LogitsSampler, but not exported as part of the public API.
225
+ export const _weightedIndex = (weights) => _weightedIndexWith(random.random, weights);
@@ -15,6 +15,8 @@ import { TensorOpRegistry } from '../ops/registry.js';
15
15
 
16
16
  import { DataTypeMap } from './dtypes.js';
17
17
 
18
+ import { random } from './random.js';
19
+
18
20
  /**
19
21
  * @typedef {keyof typeof DataTypeMap} DataType
20
22
  * @typedef {import('./maths.js').AnyTypedArray | any[]} DataArray
@@ -504,11 +506,27 @@ export class Tensor {
504
506
  }
505
507
 
506
508
  const this_data = this.data;
507
- const fn = (/** @type {number} */ a, /** @type {number} */ b) => a + b ** p;
509
+ const is_bigint = this_data instanceof BigInt64Array || this_data instanceof BigUint64Array;
510
+
511
+ if (is_bigint && p !== 1) {
512
+ throw Error(`Expected a floating point tensor as input. Got ${this.type}`);
513
+ }
514
+
515
+ let fn, zero;
516
+ if (is_bigint) {
517
+ fn = (/** @type {bigint} */ a, /** @type {bigint} */ b) => a + b;
518
+ zero = 0n;
519
+ } else {
520
+ fn = (/** @type {number} */ a, /** @type {number} */ b) => a + b ** p;
521
+ zero = 0;
522
+ }
508
523
 
509
524
  if (dim === null) {
510
525
  // @ts-ignore
511
- const val = this_data.reduce(fn, 0) ** (1 / p);
526
+ let val = this_data.reduce(fn, zero);
527
+ if (p !== 1) {
528
+ val = val ** (1 / p);
529
+ }
512
530
  return new Tensor(this.type, [val], []);
513
531
  }
514
532
 
@@ -1591,7 +1609,7 @@ export function rand(size) {
1591
1609
  const length = size.reduce((a, b) => a * b, 1);
1592
1610
  return new Tensor(
1593
1611
  'float32',
1594
- Float32Array.from({ length }, () => Math.random()),
1612
+ Float32Array.from({ length }, () => random.random()),
1595
1613
  size,
1596
1614
  );
1597
1615
  }
@@ -1603,26 +1621,11 @@ export function rand(size) {
1603
1621
  */
1604
1622
  export function randn(size) {
1605
1623
  const length = size.reduce((a, b) => a * b, 1);
1606
- const data = new Float32Array(length);
1607
-
1608
- for (let i = 0; i < length; i += 2) {
1609
- // Box-Muller transform
1610
- const u = 1 - Math.random(); // Avoids log(0)
1611
- const v = Math.random();
1612
-
1613
- const mag = Math.sqrt(-2.0 * Math.log(u));
1614
- const angle = 2.0 * Math.PI * v;
1615
-
1616
- // Assign the first value
1617
- data[i] = mag * Math.cos(angle);
1618
-
1619
- // Assign the second value (if valid index)
1620
- if (i + 1 < length) {
1621
- data[i + 1] = mag * Math.sin(angle);
1622
- }
1623
- }
1624
-
1625
- return new Tensor('float32', data, size);
1624
+ return new Tensor(
1625
+ 'float32',
1626
+ Float32Array.from({ length }, () => random.gauss()),
1627
+ size,
1628
+ );
1626
1629
  }
1627
1630
 
1628
1631
  /**
@@ -1,5 +1,5 @@
1
1
  import { RawImage } from './image.js';
2
- import { apis } from '../env.js';
2
+ import { env, apis } from '../env.js';
3
3
 
4
4
  export class RawVideoFrame {
5
5
  /**
@@ -79,7 +79,7 @@ export async function load_video(src, { num_frames = null, fps = null } = {}) {
79
79
 
80
80
  if (video.seekable.start(0) === video.seekable.end(0)) {
81
81
  // Fallback: Download entire video if not seekable
82
- const response = await fetch(video.src);
82
+ const response = await env.fetch(video.src);
83
83
  const blob = await response.blob();
84
84
  video.src = URL.createObjectURL(blob);
85
85
  await new Promise((resolve) => (video.onloadedmetadata = resolve));
@@ -1 +1 @@
1
- {"version":3,"file":"onnx.d.ts","sourceRoot":"","sources":["../../src/backends/onnx.js"],"names":[],"mappings":"AAkHA;;;;GAIG;AACH,oDAHW,OAAO,qBAAqB,EAAE,UAAU,GAAC,MAAM,GAAC,IAAI,GAClD,sBAAsB,EAAE,CAmBpC;AAoFD;;;;;;GAMG;AACH,uDALW,UAAU,GAAC,MAAM,mBACjB,OAAO,oBAAoB,EAAE,gBAAgB,CAAC,cAAc,wBAE1D,OAAO,CAAC,OAAO,oBAAoB,EAAE,gBAAgB,GAAG;IAAE,MAAM,MAAQ;CAAE,CAAC,CAavF;AASD;;;;;GAKG;AACH,6CAJW,OAAO,oBAAoB,EAAE,gBAAgB,WAC7C,MAAM,CAAC,MAAM,EAAE,OAAO,oBAAoB,EAAE,MAAM,CAAC,GACjD,OAAO,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,oBAAoB,EAAE,MAAM,CAAC,CAAC,CAMxE;AAED;;;;GAIG;AACH,gCAHW,GAAG,GACD,OAAO,CAInB;AAsCD;;;GAGG;AACH,+BAFa,OAAO,CAKnB;;qCA3RY,OAAO,oBAAoB,EAAE,gBAAgB,CAAC,uBAAuB"}
1
+ {"version":3,"file":"onnx.d.ts","sourceRoot":"","sources":["../../src/backends/onnx.js"],"names":[],"mappings":"AA2JA;;;;GAIG;AACH,oDAHW,OAAO,qBAAqB,EAAE,UAAU,GAAC,MAAM,GAAC,IAAI,GAClD,sBAAsB,EAAE,CAmBpC;AAqGD;;;;;;GAMG;AACH,uDALW,UAAU,GAAC,MAAM,mBACjB,OAAO,oBAAoB,EAAE,gBAAgB,CAAC,cAAc,wBAE1D,OAAO,CAAC,OAAO,oBAAoB,EAAE,gBAAgB,GAAG;IAAE,MAAM,MAAQ;CAAE,CAAC,CAcvF;AASD;;;;;GAKG;AACH,6CAJW,OAAO,oBAAoB,EAAE,gBAAgB,WAC7C,MAAM,CAAC,MAAM,EAAE,OAAO,oBAAoB,EAAE,MAAM,CAAC,GACjD,OAAO,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,oBAAoB,EAAE,MAAM,CAAC,CAAC,CAKxE;AAED;;;;GAIG;AACH,gCAHW,GAAG,GACD,OAAO,CAInB;AAID;;;GAGG;AACH,+BAFa,OAAO,CAKnB;;qCAjTY,OAAO,oBAAoB,EAAE,gBAAgB,CAAC,uBAAuB"}
@@ -5,24 +5,10 @@
5
5
  */
6
6
  export function loadWasmBinary(wasmURL: string): Promise<ArrayBuffer | null>;
7
7
  /**
8
- * Loads and caches the WASM Factory for ONNX Runtime.
8
+ * Loads and caches the WASM Factory (.mjs file) for ONNX Runtime.
9
+ * Creates a blob URL from cached content (when safe) to bridge Cache API with dynamic imports used in ORT.
9
10
  * @param {string} libURL The URL of the WASM Factory to load.
10
- * @returns {Promise<string|null>} The blob URL of the WASM Factory, or null if loading failed.
11
+ * @returns {Promise<string|null>} The blob URL (if enabled), original URL (if disabled), or null if loading failed.
11
12
  */
12
13
  export function loadWasmFactory(libURL: string): Promise<string | null>;
13
- /**
14
- * Checks if the given URL is a blob URL (created via URL.createObjectURL).
15
- * Blob URLs should not be cached as they are temporary in-memory references.
16
- * @param {string} url - The URL to check.
17
- * @returns {boolean} True if the URL is a blob URL, false otherwise.
18
- */
19
- export function isBlobURL(url: string): boolean;
20
- /**
21
- * Converts any URL to an absolute URL if needed.
22
- * If the URL is already absolute (http://, https://, or blob:), returns it unchanged (handled by new URL(...)).
23
- * Otherwise, resolves it relative to the current page location (browser) or module location (Node/Bun/Deno).
24
- * @param {string} url - The URL to convert (can be relative or absolute).
25
- * @returns {string} The absolute URL.
26
- */
27
- export function toAbsoluteURL(url: string): string;
28
14
  //# sourceMappingURL=cacheWasm.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"cacheWasm.d.ts","sourceRoot":"","sources":["../../../src/backends/utils/cacheWasm.js"],"names":[],"mappings":"AA8CA;;;;GAIG;AAEH,wCAJW,MAAM,GACJ,OAAO,CAAC,WAAW,GAAC,IAAI,CAAC,CAarC;AAED;;;;GAIG;AACH,wCAHW,MAAM,GACJ,OAAO,CAAC,MAAM,GAAC,IAAI,CAAC,CAiBhC;AAED;;;;;GAKG;AACH,+BAHW,MAAM,GACJ,OAAO,CAInB;AAED;;;;;;GAMG;AACH,mCAHW,MAAM,GACJ,MAAM,CAiBlB"}
1
+ {"version":3,"file":"cacheWasm.d.ts","sourceRoot":"","sources":["../../../src/backends/utils/cacheWasm.js"],"names":[],"mappings":"AA+CA;;;;GAIG;AAEH,wCAJW,MAAM,GACJ,OAAO,CAAC,WAAW,GAAC,IAAI,CAAC,CAarC;AAED;;;;;GAKG;AACH,wCAHW,MAAM,GACJ,OAAO,CAAC,MAAM,GAAC,IAAI,CAAC,CA+BhC"}
@@ -0,0 +1,29 @@
1
+ export type DynamicCache = _DynamicCache & Record<string, Tensor>;
2
+ /**
3
+ * @typedef {_DynamicCache & Record<string, Tensor>} DynamicCache
4
+ */
5
+ export const DynamicCache: new (entries?: Record<string, Tensor>) => DynamicCache;
6
+ /**
7
+ * A cache class that stores past key values as named tensors.
8
+ */
9
+ declare class _DynamicCache {
10
+ /**
11
+ * Create a DynamicCache, optionally pre-populated with entries.
12
+ * @param {Record<string, Tensor>} [entries] Initial name→Tensor mappings.
13
+ */
14
+ constructor(entries?: Record<string, Tensor>);
15
+ /**
16
+ * Get the cached sequence length. This requires at least one attention cache entry to be present.
17
+ * @returns {number} The past sequence length.
18
+ */
19
+ get_seq_length(): number;
20
+ /**
21
+ * Dispose all contained tensors whose data resides on the GPU.
22
+ * Returns a promise that resolves when all disposals are complete.
23
+ * @returns {Promise<void>} Promise that resolves when all GPU tensors are disposed.
24
+ */
25
+ dispose(): Promise<void>;
26
+ }
27
+ import { Tensor } from './utils/tensor.js';
28
+ export {};
29
+ //# sourceMappingURL=cache_utils.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cache_utils.d.ts","sourceRoot":"","sources":["../src/cache_utils.js"],"names":[],"mappings":"2BAwDa,aAAa,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC;AADnD;;GAEG;AAEH,2BAAuC,KAAK,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,KAAK,YAAY,CAE3F;AA3DF;;GAEG;AACH;IACI;;;OAGG;IACH,sBAFW,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,EAchC;IAED;;;OAGG;IACH,kBAFa,MAAM,CAWlB;IAED;;;;OAIG;IACH,WAFa,OAAO,CAAC,IAAI,CAAC,CAUzB;CACJ;uBArDsB,mBAAmB"}
@@ -1 +1 @@
1
- {"version":3,"file":"configs.d.ts","sourceRoot":"","sources":["../src/configs.js"],"names":[],"mappings":"AAuRA;;;;GAIG;AACH,uCAHW,gBAAgB,iBACd,MAAM,CAAC,MAAM,EAAE,MAAM,EAAE,CAAC,CAiEpC;AA8ED;;;GAGG;AACH;IAwBI;;;;;;;;OAQG;IACH,sDANW,MAAM,yEACN,iBAAiB,GAGf,OAAO,CAAC,gBAAgB,CAAC,CAoBrC;IApCD;;;OAGG;IACH,6BAGC;IAnBD,0BAA0B;IAC1B,YADW,MAAM,GAAC,IAAI,CACJ;IAElB,sBAAsB;IACtB,oBADW,OAAO,CACS;IAE3B,qBAAqB;IACrB,yBADW,MAAM,CACO;IAExB,mCAAmC;IACnC,0BADW,oBAAoB,CACN;IAQrB,uBAAkD;CA+BzD;AAED;;;;;GAKG;AACH;IApCI;;;;;;;;OAQG;IACH,sDANW,MAAM,yEACN,iBAAiB,GAGf,OAAO,CAAC,gBAAgB,CAAC,CAoBrC;CAcJ;gCA9cY,OAAO,gBAAgB,EAAE,iBAAiB;+BAI1C,OAAO,iBAAiB,EAAE,gBAAgB;2BAI1C,OAAO,iBAAiB,EAAE,YAAY;;;;;;;;oBA2crC,MAAM,CAAC,OAAO,oBAAoB,EAAE,UAAU,EAAE,YAAY,CAAC;;;;qBAC7D,OAAO,mBAAmB,EAAE,QAAQ,GAAC,MAAM,CAAC,OAAO,mBAAmB,EAAE,QAAQ,EAAE,OAAO,mBAAmB,EAAE,QAAQ,CAAC,GAAC,OAAO;;;;;;+BAC/H,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC;;;;aAGtB,OAAO,oBAAoB,EAAE,UAAU;;;;YACvC,OAAO,mBAAmB,EAAE,QAAQ,GAAC,MAAM,CAAC,MAAM,EAAE,OAAO,mBAAmB,EAAE,QAAQ,CAAC;;;;+BACzF,OAAO,gBAAgB,EAAE,YAAY,GAAC,MAAM,CAAC,MAAM,EAAE,OAAO,gBAAgB,EAAE,YAAY,CAAC;;;;;2BAK5F,IAAI,CAAC,oBAAoB,EAAE,QAAQ,GAAG,eAAe,CAAC"}
1
+ {"version":3,"file":"configs.d.ts","sourceRoot":"","sources":["../src/configs.js"],"names":[],"mappings":"AAiUA;;;;GAIG;AACH,uCAHW,gBAAgB,iBACd,MAAM,CAAC,MAAM,EAAE,MAAM,EAAE,CAAC,CA2HpC;AA8ED;;;GAGG;AACH;IAwBI;;;;;;;;OAQG;IACH,sDANW,MAAM,yEACN,iBAAiB,GAGf,OAAO,CAAC,gBAAgB,CAAC,CAoBrC;IApCD;;;OAGG;IACH,6BAGC;IAnBD,0BAA0B;IAC1B,YADW,MAAM,GAAC,IAAI,CACJ;IAElB,sBAAsB;IACtB,oBADW,OAAO,CACS;IAE3B,qBAAqB;IACrB,yBADW,MAAM,CACO;IAExB,mCAAmC;IACnC,0BADW,oBAAoB,CACN;IAQrB,uBAAkD;CA+BzD;AAED;;;;;GAKG;AACH;IApCI;;;;;;;;OAQG;IACH,sDANW,MAAM,yEACN,iBAAiB,GAGf,OAAO,CAAC,gBAAgB,CAAC,CAoBrC;CAcJ;gCAljBY,OAAO,gBAAgB,EAAE,iBAAiB;+BAI1C,OAAO,iBAAiB,EAAE,gBAAgB;2BAI1C,OAAO,iBAAiB,EAAE,YAAY;;;;;;;;oBA+iBrC,MAAM,CAAC,OAAO,oBAAoB,EAAE,UAAU,EAAE,YAAY,CAAC;;;;qBAC7D,OAAO,mBAAmB,EAAE,QAAQ,GAAC,MAAM,CAAC,OAAO,mBAAmB,EAAE,QAAQ,EAAE,OAAO,mBAAmB,EAAE,QAAQ,CAAC,GAAC,OAAO;;;;;;+BAC/H,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC;;;;aAGtB,OAAO,oBAAoB,EAAE,UAAU;;;;YACvC,OAAO,mBAAmB,EAAE,QAAQ,GAAC,MAAM,CAAC,MAAM,EAAE,OAAO,mBAAmB,EAAE,QAAQ,CAAC;;;;+BACzF,OAAO,gBAAgB,EAAE,YAAY,GAAC,MAAM,CAAC,MAAM,EAAE,OAAO,gBAAgB,EAAE,YAAY,CAAC;;;;;2BAK5F,IAAI,CAAC,oBAAoB,EAAE,QAAQ,GAAG,eAAe,CAAC"}