doppler-gpu 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (1405) hide show
  1. package/BRANDING.md +14 -0
  2. package/CHANGELOG.md +158 -0
  3. package/LICENSE +201 -0
  4. package/NOTICE +5 -0
  5. package/README.md +85 -0
  6. package/SECURITY.md +19 -0
  7. package/package.json +195 -0
  8. package/src/adapters/adapter-manager.d.ts +200 -0
  9. package/src/adapters/adapter-manager.js +509 -0
  10. package/src/adapters/adapter-manifest.d.ts +290 -0
  11. package/src/adapters/adapter-manifest.js +320 -0
  12. package/src/adapters/adapter-registry.d.ts +192 -0
  13. package/src/adapters/adapter-registry.js +477 -0
  14. package/src/adapters/index.d.ts +89 -0
  15. package/src/adapters/index.js +42 -0
  16. package/src/adapters/lora-loader.d.ts +105 -0
  17. package/src/adapters/lora-loader.js +425 -0
  18. package/src/bootstrap.d.ts +1 -0
  19. package/src/bootstrap.js +30 -0
  20. package/src/bridge/extension/background.d.ts +14 -0
  21. package/src/bridge/extension/background.js +168 -0
  22. package/src/bridge/extension/manifest.json +34 -0
  23. package/src/bridge/extension-client.d.ts +114 -0
  24. package/src/bridge/extension-client.js +409 -0
  25. package/src/bridge/index.d.ts +69 -0
  26. package/src/bridge/index.js +53 -0
  27. package/src/bridge/node-bridge.js +26 -0
  28. package/src/bridge/protocol.d.ts +96 -0
  29. package/src/bridge/protocol.js +130 -0
  30. package/src/browser/browser-converter.d.ts +71 -0
  31. package/src/browser/browser-converter.js +978 -0
  32. package/src/browser/file-picker.d.ts +63 -0
  33. package/src/browser/file-picker.js +281 -0
  34. package/src/browser/gguf-importer.d.ts +136 -0
  35. package/src/browser/gguf-importer.js +532 -0
  36. package/src/browser/gguf-parser-browser.d.ts +14 -0
  37. package/src/browser/gguf-parser-browser.js +17 -0
  38. package/src/browser/quantization.d.ts +69 -0
  39. package/src/browser/quantization.js +328 -0
  40. package/src/browser/safetensors-parser-browser.d.ts +193 -0
  41. package/src/browser/safetensors-parser-browser.js +347 -0
  42. package/src/browser/shard-io-browser.d.ts +57 -0
  43. package/src/browser/shard-io-browser.js +89 -0
  44. package/src/browser/tensor-source-download.d.ts +27 -0
  45. package/src/browser/tensor-source-download.js +245 -0
  46. package/src/browser/tensor-source-file.d.ts +26 -0
  47. package/src/browser/tensor-source-file.js +53 -0
  48. package/src/browser/tensor-source-http.d.ts +29 -0
  49. package/src/browser/tensor-source-http.js +130 -0
  50. package/src/cli/config/doppler-cli-policy.json +28 -0
  51. package/src/cli/doppler-cli.d.ts +28 -0
  52. package/src/cli/doppler-cli.js +1664 -0
  53. package/src/client/doppler-api.browser.d.ts +1 -0
  54. package/src/client/doppler-api.browser.js +389 -0
  55. package/src/client/doppler-api.d.ts +83 -0
  56. package/src/client/doppler-api.js +389 -0
  57. package/src/client/doppler-provider/generation.d.ts +25 -0
  58. package/src/client/doppler-provider/generation.js +126 -0
  59. package/src/client/doppler-provider/index.d.ts +2 -0
  60. package/src/client/doppler-provider/index.js +3 -0
  61. package/src/client/doppler-provider/model-manager.d.ts +71 -0
  62. package/src/client/doppler-provider/model-manager.js +739 -0
  63. package/src/client/doppler-provider/provider.d.ts +5 -0
  64. package/src/client/doppler-provider/provider.js +102 -0
  65. package/src/client/doppler-provider/source-runtime.d.ts +23 -0
  66. package/src/client/doppler-provider/source-runtime.js +677 -0
  67. package/src/client/doppler-provider/types.d.ts +127 -0
  68. package/src/client/doppler-provider/types.js +17 -0
  69. package/src/client/doppler-provider.d.ts +46 -0
  70. package/src/client/doppler-provider.js +36 -0
  71. package/src/client/doppler-registry.d.ts +23 -0
  72. package/src/client/doppler-registry.js +86 -0
  73. package/src/client/doppler-registry.json +41 -0
  74. package/src/config/README.md +57 -0
  75. package/src/config/backward-registry-loader.d.ts +3 -0
  76. package/src/config/backward-registry-loader.js +23 -0
  77. package/src/config/conversion/README.md +147 -0
  78. package/src/config/conversion/embeddinggemma/google-embeddinggemma-300m-q4k-ehf16-af32.json +285 -0
  79. package/src/config/conversion/gemma3/gemma-3-1b-it-f16-af32.json +321 -0
  80. package/src/config/conversion/gemma3/gemma-3-1b-it-q4k-ehf16-af32.json +322 -0
  81. package/src/config/conversion/gemma3/gemma-3-270m-it-q4k-ehf16-af32.json +322 -0
  82. package/src/config/conversion/gemma3/translategemma-4b-1b-enes-q4k-ehf16-af32.json +322 -0
  83. package/src/config/conversion/gemma3/translategemma-4b-it-q4k-ehf16-af32.json +323 -0
  84. package/src/config/conversion/gemma4/gemma-4-moe-q4k-ehf16-af32.json +158 -0
  85. package/src/config/conversion/gpt-oss-20b-f16-xmxfp4.json +164 -0
  86. package/src/config/conversion/janus/janus-pro-1b-text-q4k-ehaf16.json +158 -0
  87. package/src/config/conversion/lfm2/lfm2.5-1.2b-instruct-q4k-ehf16-af32.json +339 -0
  88. package/src/config/conversion/qwen3/qwen-3-5-0-8b-q4k-ehaf16.json +385 -0
  89. package/src/config/conversion/qwen3/qwen-3-5-2b-q4k-ehaf16.json +385 -0
  90. package/src/config/conversion/sana/sana-sprint-0.6b-f16.json +219 -0
  91. package/src/config/execution-contract-check.d.ts +72 -0
  92. package/src/config/execution-contract-check.js +290 -0
  93. package/src/config/index.d.ts +52 -0
  94. package/src/config/index.js +22 -0
  95. package/src/config/kernel-path-loader.d.ts +156 -0
  96. package/src/config/kernel-path-loader.js +396 -0
  97. package/src/config/kernels/backward-registry.json +99 -0
  98. package/src/config/kernels/kernel-ref-digests.d.ts +1 -0
  99. package/src/config/kernels/kernel-ref-digests.js +233 -0
  100. package/src/config/kernels/kernel-ref.d.ts +17 -0
  101. package/src/config/kernels/kernel-ref.js +75 -0
  102. package/src/config/kernels/moe/gpt-oss.paths.json +49 -0
  103. package/src/config/kernels/moe/mixtral.paths.json +46 -0
  104. package/src/config/kernels/registry.d.ts +86 -0
  105. package/src/config/kernels/registry.js +116 -0
  106. package/src/config/kernels/registry.json +9446 -0
  107. package/src/config/merge-contract-check.d.ts +16 -0
  108. package/src/config/merge-contract-check.js +311 -0
  109. package/src/config/merge-helpers.d.ts +58 -0
  110. package/src/config/merge-helpers.js +179 -0
  111. package/src/config/merge.d.ts +153 -0
  112. package/src/config/merge.js +445 -0
  113. package/src/config/param-categories.d.ts +17 -0
  114. package/src/config/param-categories.js +72 -0
  115. package/src/config/param-validator.d.ts +26 -0
  116. package/src/config/param-validator.js +274 -0
  117. package/src/config/platforms/amd-rdna3.json +16 -0
  118. package/src/config/platforms/apple-m1.json +16 -0
  119. package/src/config/platforms/apple-m2.json +16 -0
  120. package/src/config/platforms/apple-m3.json +16 -0
  121. package/src/config/platforms/generic.json +14 -0
  122. package/src/config/platforms/loader.d.ts +65 -0
  123. package/src/config/platforms/loader.js +158 -0
  124. package/src/config/platforms/nvidia-gb200-8gpu.json +45 -0
  125. package/src/config/platforms/nvidia-gb200-nvl72.json +45 -0
  126. package/src/config/platforms/nvidia-gh200-nvl2.json +44 -0
  127. package/src/config/platforms/nvidia-gh200.json +44 -0
  128. package/src/config/platforms/nvidia-rtx30.json +16 -0
  129. package/src/config/platforms/nvidia-rtx40.json +16 -0
  130. package/src/config/quantization-contract-check.d.ts +12 -0
  131. package/src/config/quantization-contract-check.js +91 -0
  132. package/src/config/required-inference-fields-contract-check.d.ts +24 -0
  133. package/src/config/required-inference-fields-contract-check.js +238 -0
  134. package/src/config/runtime/compute/f16-activations.json +30 -0
  135. package/src/config/runtime/compute/f16-batched.json +32 -0
  136. package/src/config/runtime/default.json +100 -0
  137. package/src/config/runtime/diagnostics/debug-logits.json +53 -0
  138. package/src/config/runtime/diagnostics/drift-policies.json +147 -0
  139. package/src/config/runtime/experiments/bench/gemma3-bench-q4k.json +54 -0
  140. package/src/config/runtime/experiments/debug/gemma3-debug-q4k.json +210 -0
  141. package/src/config/runtime/experiments/verify/gemma3-verify.json +39 -0
  142. package/src/config/runtime/experiments/verify/lfm2-verify.json +46 -0
  143. package/src/config/runtime/experiments/verify/translategemma-verify.json +39 -0
  144. package/src/config/runtime/kernels/dequant-f16-q4k.json +13 -0
  145. package/src/config/runtime/kernels/dequant-f32-q4k.json +13 -0
  146. package/src/config/runtime/kernels/embeddinggemma-q4k-dequant-f32a.json +37 -0
  147. package/src/config/runtime/kernels/fused-q4k.json +13 -0
  148. package/src/config/runtime/kernels/gemma2-q4k-dequant-f16a.json +33 -0
  149. package/src/config/runtime/kernels/gemma2-q4k-dequant-f32a-nosubgroups.json +33 -0
  150. package/src/config/runtime/kernels/gemma2-q4k-fused-f32a.json +33 -0
  151. package/src/config/runtime/kernels/safe-q4k.json +13 -0
  152. package/src/config/runtime/model/gemma2-debug.json +77 -0
  153. package/src/config/runtime/model/gemma2-pipeline-debug.json +66 -0
  154. package/src/config/runtime/model/gemma2-pipeline.json +75 -0
  155. package/src/config/runtime/model/gemma3-layer-probe.json +85 -0
  156. package/src/config/runtime/model/qwen3-5-layer-probe.json +52 -0
  157. package/src/config/runtime/model/qwen3-5-layer3-boundary-probe.json +108 -0
  158. package/src/config/runtime/model/qwen3-5-linear-attn-debug.json +90 -0
  159. package/src/config/runtime/overlays/debug-f32a.json +3 -0
  160. package/src/config/runtime/overlays/trace-config.json +28 -0
  161. package/src/config/runtime/platform/metal-apple-q4k.json +11 -0
  162. package/src/config/runtime/profiles/default.json +10 -0
  163. package/src/config/runtime/profiles/low-memory.json +40 -0
  164. package/src/config/runtime/profiles/production.json +48 -0
  165. package/src/config/runtime/profiles/simulation.json +31 -0
  166. package/src/config/runtime/profiles/throughput.json +37 -0
  167. package/src/config/runtime/profiles/trace-layers.json +127 -0
  168. package/src/config/runtime/profiles/vector-stability.json +54 -0
  169. package/src/config/runtime/profiles/vector-throughput.json +28 -0
  170. package/src/config/runtime/profiles/verbose-trace.json +39 -0
  171. package/src/config/runtime/tiers/gemma4-16gb.json +69 -0
  172. package/src/config/runtime/tiers/gemma4-24gb.json +66 -0
  173. package/src/config/runtime/tiers/gemma4-32gb.json +66 -0
  174. package/src/config/runtime-merge.d.ts +5 -0
  175. package/src/config/runtime-merge.js +30 -0
  176. package/src/config/runtime.d.ts +28 -0
  177. package/src/config/runtime.js +69 -0
  178. package/src/config/schema/adapter.schema.d.ts +53 -0
  179. package/src/config/schema/adapter.schema.js +60 -0
  180. package/src/config/schema/backward-registry.schema.d.ts +14 -0
  181. package/src/config/schema/backward-registry.schema.js +46 -0
  182. package/src/config/schema/benchmark.schema.d.ts +54 -0
  183. package/src/config/schema/benchmark.schema.js +74 -0
  184. package/src/config/schema/bridge.schema.d.ts +25 -0
  185. package/src/config/schema/bridge.schema.js +22 -0
  186. package/src/config/schema/browser-suite-metrics.schema.d.ts +16 -0
  187. package/src/config/schema/browser-suite-metrics.schema.js +44 -0
  188. package/src/config/schema/buffer-pool.schema.d.ts +92 -0
  189. package/src/config/schema/buffer-pool.schema.js +50 -0
  190. package/src/config/schema/conversion-report.schema.d.ts +36 -0
  191. package/src/config/schema/conversion-report.schema.js +97 -0
  192. package/src/config/schema/conversion.schema.d.ts +191 -0
  193. package/src/config/schema/conversion.schema.js +13 -0
  194. package/src/config/schema/converter.schema.d.ts +117 -0
  195. package/src/config/schema/converter.schema.js +140 -0
  196. package/src/config/schema/debug.schema.d.ts +292 -0
  197. package/src/config/schema/debug.schema.js +134 -0
  198. package/src/config/schema/diffusion.schema.d.ts +88 -0
  199. package/src/config/schema/diffusion.schema.js +62 -0
  200. package/src/config/schema/distill-training.schema.d.ts +48 -0
  201. package/src/config/schema/distill-training.schema.js +139 -0
  202. package/src/config/schema/distribution.schema.d.ts +155 -0
  203. package/src/config/schema/distribution.schema.js +81 -0
  204. package/src/config/schema/doppler.schema.d.ts +74 -0
  205. package/src/config/schema/doppler.schema.js +341 -0
  206. package/src/config/schema/ecosystem.schema.d.ts +255 -0
  207. package/src/config/schema/ecosystem.schema.js +534 -0
  208. package/src/config/schema/emulation.schema.d.ts +351 -0
  209. package/src/config/schema/emulation.schema.js +301 -0
  210. package/src/config/schema/energy.schema.d.ts +102 -0
  211. package/src/config/schema/energy.schema.js +72 -0
  212. package/src/config/schema/execution-v1.schema.d.ts +258 -0
  213. package/src/config/schema/execution-v1.schema.js +211 -0
  214. package/src/config/schema/gpu-cache.schema.d.ts +26 -0
  215. package/src/config/schema/gpu-cache.schema.js +8 -0
  216. package/src/config/schema/harness.schema.d.ts +34 -0
  217. package/src/config/schema/harness.schema.js +21 -0
  218. package/src/config/schema/hotswap.schema.d.ts +55 -0
  219. package/src/config/schema/hotswap.schema.js +18 -0
  220. package/src/config/schema/index.d.ts +869 -0
  221. package/src/config/schema/index.js +498 -0
  222. package/src/config/schema/inference-defaults.schema.d.ts +276 -0
  223. package/src/config/schema/inference-defaults.schema.js +189 -0
  224. package/src/config/schema/inference.schema.d.ts +298 -0
  225. package/src/config/schema/inference.schema.js +39 -0
  226. package/src/config/schema/intent-bundle.schema.d.ts +28 -0
  227. package/src/config/schema/intent-bundle.schema.js +12 -0
  228. package/src/config/schema/kernel-path.schema.d.ts +184 -0
  229. package/src/config/schema/kernel-path.schema.js +9 -0
  230. package/src/config/schema/kernel-registry.schema.d.ts +199 -0
  231. package/src/config/schema/kernel-registry.schema.js +46 -0
  232. package/src/config/schema/kernel-thresholds.schema.d.ts +302 -0
  233. package/src/config/schema/kernel-thresholds.schema.js +195 -0
  234. package/src/config/schema/kernel-warmup.schema.d.ts +19 -0
  235. package/src/config/schema/kernel-warmup.schema.js +5 -0
  236. package/src/config/schema/kvcache.schema.d.ts +131 -0
  237. package/src/config/schema/kvcache.schema.js +41 -0
  238. package/src/config/schema/loading.schema.d.ts +153 -0
  239. package/src/config/schema/loading.schema.js +84 -0
  240. package/src/config/schema/lora.schema.d.ts +12 -0
  241. package/src/config/schema/lora.schema.js +12 -0
  242. package/src/config/schema/manifest.schema.d.ts +520 -0
  243. package/src/config/schema/manifest.schema.js +145 -0
  244. package/src/config/schema/memory-limits.schema.d.ts +107 -0
  245. package/src/config/schema/memory-limits.schema.js +57 -0
  246. package/src/config/schema/moe.schema.d.ts +78 -0
  247. package/src/config/schema/moe.schema.js +40 -0
  248. package/src/config/schema/platform.schema.d.ts +121 -0
  249. package/src/config/schema/platform.schema.js +1 -0
  250. package/src/config/schema/quantization-defaults.schema.d.ts +34 -0
  251. package/src/config/schema/quantization-defaults.schema.js +5 -0
  252. package/src/config/schema/quantization.schema.d.ts +10 -0
  253. package/src/config/schema/quantization.schema.js +33 -0
  254. package/src/config/schema/shared-runtime.schema.d.ts +75 -0
  255. package/src/config/schema/shared-runtime.schema.js +45 -0
  256. package/src/config/schema/speculation-self.schema.js +54 -0
  257. package/src/config/schema/speculative.schema.d.ts +21 -0
  258. package/src/config/schema/speculative.schema.js +11 -0
  259. package/src/config/schema/storage.schema.d.ts +123 -0
  260. package/src/config/schema/storage.schema.js +66 -0
  261. package/src/config/schema/tooling.schema.d.ts +29 -0
  262. package/src/config/schema/tooling.schema.js +12 -0
  263. package/src/config/schema/training-metrics.schema.d.ts +89 -0
  264. package/src/config/schema/training-metrics.schema.js +374 -0
  265. package/src/config/schema/training.schema.d.ts +88 -0
  266. package/src/config/schema/training.schema.js +106 -0
  267. package/src/config/schema/tuner.schema.d.ts +39 -0
  268. package/src/config/schema/tuner.schema.js +13 -0
  269. package/src/config/schema/ul-training.schema.d.ts +61 -0
  270. package/src/config/schema/ul-training.schema.js +140 -0
  271. package/src/config/schema/units.schema.d.ts +27 -0
  272. package/src/config/schema/units.schema.js +30 -0
  273. package/src/config/training-defaults.d.ts +24 -0
  274. package/src/config/training-defaults.js +99 -0
  275. package/src/config/transforms/capability-transform-resolver.d.ts +34 -0
  276. package/src/config/transforms/capability-transform-resolver.js +62 -0
  277. package/src/config/transforms/execution-graph-transforms.d.ts +92 -0
  278. package/src/config/transforms/execution-graph-transforms.js +512 -0
  279. package/src/converter/conversion-plan.d.ts +54 -0
  280. package/src/converter/conversion-plan.js +268 -0
  281. package/src/converter/core.d.ts +263 -0
  282. package/src/converter/core.js +1530 -0
  283. package/src/converter/index.d.ts +98 -0
  284. package/src/converter/index.js +59 -0
  285. package/src/converter/parsers/diffusion.d.ts +50 -0
  286. package/src/converter/parsers/diffusion.js +327 -0
  287. package/src/converter/parsers/gguf.d.ts +22 -0
  288. package/src/converter/parsers/gguf.js +46 -0
  289. package/src/converter/parsers/index.d.ts +21 -0
  290. package/src/converter/parsers/index.js +12 -0
  291. package/src/converter/parsers/transformer.d.ts +31 -0
  292. package/src/converter/parsers/transformer.js +198 -0
  293. package/src/converter/quantization-info.d.ts +37 -0
  294. package/src/converter/quantization-info.js +422 -0
  295. package/src/converter/quantizer.d.ts +101 -0
  296. package/src/converter/quantizer.js +444 -0
  297. package/src/converter/rope-config.d.ts +15 -0
  298. package/src/converter/rope-config.js +262 -0
  299. package/src/converter/shard-packer.d.ts +138 -0
  300. package/src/converter/shard-packer.js +425 -0
  301. package/src/converter/tokenizer-utils.d.ts +12 -0
  302. package/src/converter/tokenizer-utils.js +104 -0
  303. package/src/debug/capture-policy.d.ts +88 -0
  304. package/src/debug/capture-policy.js +196 -0
  305. package/src/debug/config.d.ts +78 -0
  306. package/src/debug/config.js +347 -0
  307. package/src/debug/history.d.ts +65 -0
  308. package/src/debug/history.js +82 -0
  309. package/src/debug/index.d.ts +268 -0
  310. package/src/debug/index.js +215 -0
  311. package/src/debug/log.d.ts +46 -0
  312. package/src/debug/log.js +132 -0
  313. package/src/debug/perf.d.ts +33 -0
  314. package/src/debug/perf.js +51 -0
  315. package/src/debug/reference/README.md +114 -0
  316. package/src/debug/reference/hf_attn_debug.py +114 -0
  317. package/src/debug/reference/hf_embed_check.py +89 -0
  318. package/src/debug/reference/hf_layer_out.py +100 -0
  319. package/src/debug/reference/hf_qwen35_linear_attn_debug.py +268 -0
  320. package/src/debug/reference/hf_rope_check.py +116 -0
  321. package/src/debug/reference/hf_weights.py +75 -0
  322. package/src/debug/signals.d.ts +63 -0
  323. package/src/debug/signals.js +39 -0
  324. package/src/debug/stats.d.ts +47 -0
  325. package/src/debug/stats.js +160 -0
  326. package/src/debug/tensor.d.ts +125 -0
  327. package/src/debug/tensor.js +268 -0
  328. package/src/debug/trace.d.ts +17 -0
  329. package/src/debug/trace.js +188 -0
  330. package/src/diffusion/config/expansion-lanes.json +29 -0
  331. package/src/diffusion/image-regression.d.ts +31 -0
  332. package/src/diffusion/image-regression.js +107 -0
  333. package/src/diffusion/index.d.ts +8 -0
  334. package/src/diffusion/index.js +8 -0
  335. package/src/distribution/p2p-control-plane.d.ts +52 -0
  336. package/src/distribution/p2p-control-plane.js +272 -0
  337. package/src/distribution/p2p-observability.d.ts +116 -0
  338. package/src/distribution/p2p-observability.js +303 -0
  339. package/src/distribution/p2p-transport-contract.d.ts +57 -0
  340. package/src/distribution/p2p-transport-contract.js +310 -0
  341. package/src/distribution/p2p-transport.js +75 -0
  342. package/src/distribution/p2p-webrtc-browser.d.ts +37 -0
  343. package/src/distribution/p2p-webrtc-browser.js +454 -0
  344. package/src/distribution/shard-delivery.d.ts +251 -0
  345. package/src/distribution/shard-delivery.js +2186 -0
  346. package/src/energy/index.d.ts +2 -0
  347. package/src/energy/index.js +2 -0
  348. package/src/errors/doppler-error.d.ts +20 -0
  349. package/src/errors/doppler-error.js +37 -0
  350. package/src/errors/index.d.ts +1 -0
  351. package/src/errors/index.js +1 -0
  352. package/src/formats/gguf/index.d.ts +8 -0
  353. package/src/formats/gguf/index.js +4 -0
  354. package/src/formats/gguf/types.d.ts +137 -0
  355. package/src/formats/gguf/types.js +460 -0
  356. package/src/formats/index.d.ts +51 -0
  357. package/src/formats/index.js +13 -0
  358. package/src/formats/rdrr/classification.d.ts +54 -0
  359. package/src/formats/rdrr/classification.js +329 -0
  360. package/src/formats/rdrr/groups.d.ts +35 -0
  361. package/src/formats/rdrr/groups.js +73 -0
  362. package/src/formats/rdrr/index.d.ts +25 -0
  363. package/src/formats/rdrr/index.js +19 -0
  364. package/src/formats/rdrr/manifest.d.ts +32 -0
  365. package/src/formats/rdrr/manifest.js +108 -0
  366. package/src/formats/rdrr/parsing.d.ts +27 -0
  367. package/src/formats/rdrr/parsing.js +167 -0
  368. package/src/formats/rdrr/tensor-config-validator.d.ts +42 -0
  369. package/src/formats/rdrr/tensor-config-validator.js +156 -0
  370. package/src/formats/rdrr/types.d.ts +201 -0
  371. package/src/formats/rdrr/types.js +16 -0
  372. package/src/formats/rdrr/validation.d.ts +9 -0
  373. package/src/formats/rdrr/validation.js +213 -0
  374. package/src/formats/safetensors/index.d.ts +8 -0
  375. package/src/formats/safetensors/index.js +4 -0
  376. package/src/formats/safetensors/types.d.ts +67 -0
  377. package/src/formats/safetensors/types.js +102 -0
  378. package/src/formats/tokenizer/index.d.ts +5 -0
  379. package/src/formats/tokenizer/index.js +3 -0
  380. package/src/formats/tokenizer/types.d.ts +9 -0
  381. package/src/formats/tokenizer/types.js +22 -0
  382. package/src/generation/index.d.ts +18 -0
  383. package/src/generation/index.js +32 -0
  384. package/src/gpu/command-recorder.d.ts +175 -0
  385. package/src/gpu/command-recorder.js +498 -0
  386. package/src/gpu/device.d.ts +142 -0
  387. package/src/gpu/device.js +532 -0
  388. package/src/gpu/kernel-runtime.d.ts +20 -0
  389. package/src/gpu/kernel-runtime.js +39 -0
  390. package/src/gpu/kernel-selection-cache.d.ts +13 -0
  391. package/src/gpu/kernel-selection-cache.js +13 -0
  392. package/src/gpu/kernel-selection-log.d.ts +12 -0
  393. package/src/gpu/kernel-selection-log.js +28 -0
  394. package/src/gpu/kernel-selector.d.ts +11 -0
  395. package/src/gpu/kernel-selector.js +10 -0
  396. package/src/gpu/kernel-tuner/benchmarks.d.ts +144 -0
  397. package/src/gpu/kernel-tuner/benchmarks.js +902 -0
  398. package/src/gpu/kernel-tuner/cache.d.ts +55 -0
  399. package/src/gpu/kernel-tuner/cache.js +152 -0
  400. package/src/gpu/kernel-tuner/index.d.ts +59 -0
  401. package/src/gpu/kernel-tuner/index.js +38 -0
  402. package/src/gpu/kernel-tuner/tuner.d.ts +82 -0
  403. package/src/gpu/kernel-tuner/tuner.js +247 -0
  404. package/src/gpu/kernel-tuner/types.d.ts +101 -0
  405. package/src/gpu/kernel-tuner/types.js +4 -0
  406. package/src/gpu/kernel-tuner.d.ts +33 -0
  407. package/src/gpu/kernel-tuner.js +12 -0
  408. package/src/gpu/kernels/README.md +127 -0
  409. package/src/gpu/kernels/attention.d.ts +236 -0
  410. package/src/gpu/kernels/attention.js +1437 -0
  411. package/src/gpu/kernels/attention.wgsl +249 -0
  412. package/src/gpu/kernels/attention_bdpa_decode_f16.wgsl +246 -0
  413. package/src/gpu/kernels/attention_decode.wgsl +233 -0
  414. package/src/gpu/kernels/attention_decode_chunked_f16.wgsl +183 -0
  415. package/src/gpu/kernels/attention_decode_chunked_f16kv.wgsl +208 -0
  416. package/src/gpu/kernels/attention_decode_f16.wgsl +202 -0
  417. package/src/gpu/kernels/attention_decode_f16kv.wgsl +224 -0
  418. package/src/gpu/kernels/attention_decode_online_f16.wgsl +223 -0
  419. package/src/gpu/kernels/attention_decode_online_f16kv.wgsl +225 -0
  420. package/src/gpu/kernels/attention_decode_optimized.wgsl +445 -0
  421. package/src/gpu/kernels/attention_decode_paged_f16.wgsl +172 -0
  422. package/src/gpu/kernels/attention_decode_paged_f16kv.wgsl +174 -0
  423. package/src/gpu/kernels/attention_decode_subgroup.wgsl +233 -0
  424. package/src/gpu/kernels/attention_decode_tiered_f16.wgsl +218 -0
  425. package/src/gpu/kernels/attention_decode_tiered_f16kv.wgsl +220 -0
  426. package/src/gpu/kernels/attention_decode_tiered_int4_f16kv.wgsl +242 -0
  427. package/src/gpu/kernels/attention_decode_tiered_int8_f16kv.wgsl +242 -0
  428. package/src/gpu/kernels/attention_f16.wgsl +214 -0
  429. package/src/gpu/kernels/attention_f16kv.wgsl +242 -0
  430. package/src/gpu/kernels/attention_small.wgsl +260 -0
  431. package/src/gpu/kernels/attention_small_f16.wgsl +240 -0
  432. package/src/gpu/kernels/attention_small_f16kv.wgsl +266 -0
  433. package/src/gpu/kernels/attention_streaming.wgsl +149 -0
  434. package/src/gpu/kernels/attention_streaming_f16.wgsl +147 -0
  435. package/src/gpu/kernels/attention_streaming_f16kv.wgsl +151 -0
  436. package/src/gpu/kernels/backward/adam.d.ts +28 -0
  437. package/src/gpu/kernels/backward/adam.js +203 -0
  438. package/src/gpu/kernels/backward/adam.wgsl +50 -0
  439. package/src/gpu/kernels/backward/attention_backward.d.ts +22 -0
  440. package/src/gpu/kernels/backward/attention_backward.js +364 -0
  441. package/src/gpu/kernels/backward/attention_backward.wgsl +49 -0
  442. package/src/gpu/kernels/backward/bias_add_backward.d.ts +17 -0
  443. package/src/gpu/kernels/backward/bias_add_backward.js +24 -0
  444. package/src/gpu/kernels/backward/bias_add_backward.wgsl +33 -0
  445. package/src/gpu/kernels/backward/conv2d_backward.d.ts +31 -0
  446. package/src/gpu/kernels/backward/conv2d_backward.js +148 -0
  447. package/src/gpu/kernels/backward/conv2d_backward_input.wgsl +83 -0
  448. package/src/gpu/kernels/backward/conv2d_backward_weight.wgsl +70 -0
  449. package/src/gpu/kernels/backward/cross_entropy_backward.d.ts +23 -0
  450. package/src/gpu/kernels/backward/cross_entropy_backward.js +29 -0
  451. package/src/gpu/kernels/backward/cross_entropy_backward.wgsl +39 -0
  452. package/src/gpu/kernels/backward/embed_backward.d.ts +29 -0
  453. package/src/gpu/kernels/backward/embed_backward.js +118 -0
  454. package/src/gpu/kernels/backward/embed_backward.wgsl +73 -0
  455. package/src/gpu/kernels/backward/gelu_backward.d.ts +16 -0
  456. package/src/gpu/kernels/backward/gelu_backward.js +39 -0
  457. package/src/gpu/kernels/backward/gelu_backward.wgsl +38 -0
  458. package/src/gpu/kernels/backward/groupnorm_backward.d.ts +24 -0
  459. package/src/gpu/kernels/backward/groupnorm_backward.js +29 -0
  460. package/src/gpu/kernels/backward/groupnorm_backward.wgsl +143 -0
  461. package/src/gpu/kernels/backward/index.d.ts +17 -0
  462. package/src/gpu/kernels/backward/index.js +23 -0
  463. package/src/gpu/kernels/backward/layernorm_backward.d.ts +22 -0
  464. package/src/gpu/kernels/backward/layernorm_backward.js +135 -0
  465. package/src/gpu/kernels/backward/layernorm_backward.wgsl +194 -0
  466. package/src/gpu/kernels/backward/matmul_backward.d.ts +32 -0
  467. package/src/gpu/kernels/backward/matmul_backward.js +124 -0
  468. package/src/gpu/kernels/backward/matmul_backward.wgsl +90 -0
  469. package/src/gpu/kernels/backward/matmul_transpose_a.wgsl +84 -0
  470. package/src/gpu/kernels/backward/pixel_shuffle_backward.d.ts +22 -0
  471. package/src/gpu/kernels/backward/pixel_shuffle_backward.js +30 -0
  472. package/src/gpu/kernels/backward/pixel_shuffle_backward.wgsl +54 -0
  473. package/src/gpu/kernels/backward/rmsnorm_backward.d.ts +24 -0
  474. package/src/gpu/kernels/backward/rmsnorm_backward.js +101 -0
  475. package/src/gpu/kernels/backward/rmsnorm_backward.wgsl +78 -0
  476. package/src/gpu/kernels/backward/rope_backward.d.ts +25 -0
  477. package/src/gpu/kernels/backward/rope_backward.js +109 -0
  478. package/src/gpu/kernels/backward/rope_backward.wgsl +59 -0
  479. package/src/gpu/kernels/backward/scale_backward.d.ts +16 -0
  480. package/src/gpu/kernels/backward/scale_backward.js +84 -0
  481. package/src/gpu/kernels/backward/scale_backward.wgsl +27 -0
  482. package/src/gpu/kernels/backward/silu_backward.d.ts +16 -0
  483. package/src/gpu/kernels/backward/silu_backward.js +39 -0
  484. package/src/gpu/kernels/backward/silu_backward.wgsl +31 -0
  485. package/src/gpu/kernels/backward/softmax_backward.d.ts +16 -0
  486. package/src/gpu/kernels/backward/softmax_backward.js +43 -0
  487. package/src/gpu/kernels/backward/softmax_backward.wgsl +44 -0
  488. package/src/gpu/kernels/backward/upsample2d_backward.d.ts +21 -0
  489. package/src/gpu/kernels/backward/upsample2d_backward.js +30 -0
  490. package/src/gpu/kernels/backward/upsample2d_backward.wgsl +59 -0
  491. package/src/gpu/kernels/backward/utils.d.ts +45 -0
  492. package/src/gpu/kernels/backward/utils.js +371 -0
  493. package/src/gpu/kernels/bf16_to_f16.wgsl +54 -0
  494. package/src/gpu/kernels/bf16_to_f32.wgsl +70 -0
  495. package/src/gpu/kernels/bias_add.wgsl +42 -0
  496. package/src/gpu/kernels/bias_add_f16.wgsl +47 -0
  497. package/src/gpu/kernels/cast.d.ts +67 -0
  498. package/src/gpu/kernels/cast.js +339 -0
  499. package/src/gpu/kernels/cast_f16_to_f32.wgsl +31 -0
  500. package/src/gpu/kernels/cast_f32_to_f16.wgsl +36 -0
  501. package/src/gpu/kernels/check-finiteness.d.ts +15 -0
  502. package/src/gpu/kernels/check-finiteness.js +149 -0
  503. package/src/gpu/kernels/check-stop.d.ts +31 -0
  504. package/src/gpu/kernels/check-stop.js +170 -0
  505. package/src/gpu/kernels/clamp.d.ts +22 -0
  506. package/src/gpu/kernels/clamp.js +42 -0
  507. package/src/gpu/kernels/clamp.wgsl +24 -0
  508. package/src/gpu/kernels/codegen/patches/attention_decode_f16.from.attention_decode.diff +213 -0
  509. package/src/gpu/kernels/codegen/patches/attention_decode_f16kv.from.attention_decode.diff +89 -0
  510. package/src/gpu/kernels/codegen/patches/attention_f16.from.attention.diff +258 -0
  511. package/src/gpu/kernels/codegen/patches/attention_f16kv.from.attention.diff +114 -0
  512. package/src/gpu/kernels/codegen/patches/attention_small_f16.from.attention_small.diff +193 -0
  513. package/src/gpu/kernels/codegen/patches/attention_small_f16kv.from.attention_small.diff +264 -0
  514. package/src/gpu/kernels/codegen/patches/attention_streaming_f16.from.attention_streaming.diff +109 -0
  515. package/src/gpu/kernels/codegen/patches/attention_streaming_f16kv.from.attention_streaming.diff +53 -0
  516. package/src/gpu/kernels/codegen/patches/bias_add_f16.from.bias_add.diff +54 -0
  517. package/src/gpu/kernels/codegen/patches/dequant_mxfp4_expert_f16.from.dequant_mxfp4_expert.diff +125 -0
  518. package/src/gpu/kernels/codegen/patches/fused_ffn_f16.from.fused_ffn.diff +428 -0
  519. package/src/gpu/kernels/codegen/patches/fused_matmul_q4_batched_f16.from.fused_matmul_q4_batched.diff +114 -0
  520. package/src/gpu/kernels/codegen/patches/fused_matmul_rmsnorm_f16.from.fused_matmul_rmsnorm.diff +477 -0
  521. package/src/gpu/kernels/codegen/patches/fused_swiglu_f16.from.fused_swiglu.diff +60 -0
  522. package/src/gpu/kernels/codegen/patches/gelu_f16.from.gelu.diff +58 -0
  523. package/src/gpu/kernels/codegen/patches/layernorm_f16.from.layernorm.diff +82 -0
  524. package/src/gpu/kernels/codegen/patches/matmul_gemv_residual_f16.from.matmul_gemv_residual.diff +87 -0
  525. package/src/gpu/kernels/codegen/patches/moe_gather_f16.from.moe_gather.diff +181 -0
  526. package/src/gpu/kernels/codegen/patches/residual_f16.from.residual.diff +62 -0
  527. package/src/gpu/kernels/codegen/patches/rmsnorm_f16.from.rmsnorm.diff +430 -0
  528. package/src/gpu/kernels/codegen/patches/rope_f16.from.rope.diff +462 -0
  529. package/src/gpu/kernels/codegen/patches/sample_f16.from.sample.diff +307 -0
  530. package/src/gpu/kernels/codegen/patches/scatter_add_dynamic_f16.from.scatter_add_dynamic.diff +65 -0
  531. package/src/gpu/kernels/codegen/patches/silu_f16.from.silu.diff +81 -0
  532. package/src/gpu/kernels/codegen/patches/topk_f16.from.topk.diff +231 -0
  533. package/src/gpu/kernels/codegen/wgsl-patch-variants.js +152 -0
  534. package/src/gpu/kernels/codegen/wgsl-variants.js +733 -0
  535. package/src/gpu/kernels/constants.d.ts +168 -0
  536. package/src/gpu/kernels/constants.js +142 -0
  537. package/src/gpu/kernels/conv2d.d.ts +34 -0
  538. package/src/gpu/kernels/conv2d.js +91 -0
  539. package/src/gpu/kernels/conv2d.wgsl +70 -0
  540. package/src/gpu/kernels/conv2d_f16.wgsl +72 -0
  541. package/src/gpu/kernels/cross_entropy_loss.d.ts +21 -0
  542. package/src/gpu/kernels/cross_entropy_loss.js +60 -0
  543. package/src/gpu/kernels/cross_entropy_loss.wgsl +39 -0
  544. package/src/gpu/kernels/depthwise_conv2d.d.ts +29 -0
  545. package/src/gpu/kernels/depthwise_conv2d.js +109 -0
  546. package/src/gpu/kernels/depthwise_conv2d.wgsl +55 -0
  547. package/src/gpu/kernels/depthwise_conv2d_f16.wgsl +59 -0
  548. package/src/gpu/kernels/dequant.d.ts +108 -0
  549. package/src/gpu/kernels/dequant.js +575 -0
  550. package/src/gpu/kernels/dequant_f16_out.wgsl +153 -0
  551. package/src/gpu/kernels/dequant_f16_out_vec4.wgsl +152 -0
  552. package/src/gpu/kernels/dequant_f16_rowwise.wgsl +139 -0
  553. package/src/gpu/kernels/dequant_f32_rowwise.wgsl +133 -0
  554. package/src/gpu/kernels/dequant_mxfp4.wgsl +120 -0
  555. package/src/gpu/kernels/dequant_mxfp4_expert.wgsl +129 -0
  556. package/src/gpu/kernels/dequant_mxfp4_expert_f16.wgsl +105 -0
  557. package/src/gpu/kernels/dequant_mxfp4_vec4.wgsl +116 -0
  558. package/src/gpu/kernels/dequant_q6k.wgsl +140 -0
  559. package/src/gpu/kernels/dequant_q8_0.wgsl +98 -0
  560. package/src/gpu/kernels/dequant_shared.wgsl +200 -0
  561. package/src/gpu/kernels/dequant_shared_vec4.wgsl +160 -0
  562. package/src/gpu/kernels/dequant_subgroup.wgsl +206 -0
  563. package/src/gpu/kernels/dispatch.d.ts +157 -0
  564. package/src/gpu/kernels/dispatch.js +235 -0
  565. package/src/gpu/kernels/energy.d.ts +113 -0
  566. package/src/gpu/kernels/energy.js +448 -0
  567. package/src/gpu/kernels/energy_eval.wgsl +26 -0
  568. package/src/gpu/kernels/energy_eval_f16.wgsl +30 -0
  569. package/src/gpu/kernels/energy_quintel_grad.wgsl +92 -0
  570. package/src/gpu/kernels/energy_quintel_grad_f16.wgsl +96 -0
  571. package/src/gpu/kernels/energy_quintel_reduce.wgsl +112 -0
  572. package/src/gpu/kernels/energy_quintel_reduce_f16.wgsl +116 -0
  573. package/src/gpu/kernels/energy_quintel_update.wgsl +92 -0
  574. package/src/gpu/kernels/energy_quintel_update_f16.wgsl +96 -0
  575. package/src/gpu/kernels/energy_update.wgsl +25 -0
  576. package/src/gpu/kernels/energy_update_f16.wgsl +30 -0
  577. package/src/gpu/kernels/feature-check.d.ts +42 -0
  578. package/src/gpu/kernels/feature-check.js +70 -0
  579. package/src/gpu/kernels/fused_ffn.d.ts +65 -0
  580. package/src/gpu/kernels/fused_ffn.js +326 -0
  581. package/src/gpu/kernels/fused_ffn.wgsl +420 -0
  582. package/src/gpu/kernels/fused_ffn_f16.wgsl +213 -0
  583. package/src/gpu/kernels/fused_ffn_q4k.wgsl +375 -0
  584. package/src/gpu/kernels/fused_matmul_q4.wgsl +404 -0
  585. package/src/gpu/kernels/fused_matmul_q4_batched.wgsl +194 -0
  586. package/src/gpu/kernels/fused_matmul_q4_batched_f16.wgsl +170 -0
  587. package/src/gpu/kernels/fused_matmul_q4_batched_f16a.wgsl +154 -0
  588. package/src/gpu/kernels/fused_matmul_q4_f16a.wgsl +219 -0
  589. package/src/gpu/kernels/fused_matmul_q4_multicol_f16.wgsl +216 -0
  590. package/src/gpu/kernels/fused_matmul_q4_multicol_f16a.wgsl +204 -0
  591. package/src/gpu/kernels/fused_matmul_residual.d.ts +46 -0
  592. package/src/gpu/kernels/fused_matmul_residual.js +175 -0
  593. package/src/gpu/kernels/fused_matmul_rmsnorm.d.ts +64 -0
  594. package/src/gpu/kernels/fused_matmul_rmsnorm.js +290 -0
  595. package/src/gpu/kernels/fused_matmul_rmsnorm.wgsl +324 -0
  596. package/src/gpu/kernels/fused_matmul_rmsnorm_f16.wgsl +303 -0
  597. package/src/gpu/kernels/fused_swiglu.wgsl +63 -0
  598. package/src/gpu/kernels/fused_swiglu_f16.wgsl +57 -0
  599. package/src/gpu/kernels/gated-short-conv.d.ts +63 -0
  600. package/src/gpu/kernels/gated-short-conv.js +284 -0
  601. package/src/gpu/kernels/gather.d.ts +64 -0
  602. package/src/gpu/kernels/gather.js +137 -0
  603. package/src/gpu/kernels/gather.wgsl +61 -0
  604. package/src/gpu/kernels/gather_f16.wgsl +65 -0
  605. package/src/gpu/kernels/gather_f16_f16_out.wgsl +55 -0
  606. package/src/gpu/kernels/gather_f16_out.wgsl +55 -0
  607. package/src/gpu/kernels/gather_f16_vec4.wgsl +76 -0
  608. package/src/gpu/kernels/gather_f16_vec4_f16_out.wgsl +68 -0
  609. package/src/gpu/kernels/gather_vec4.wgsl +74 -0
  610. package/src/gpu/kernels/gather_vec4_f16_out.wgsl +68 -0
  611. package/src/gpu/kernels/gelu.d.ts +33 -0
  612. package/src/gpu/kernels/gelu.js +55 -0
  613. package/src/gpu/kernels/gelu.wgsl +64 -0
  614. package/src/gpu/kernels/gelu_f16.wgsl +66 -0
  615. package/src/gpu/kernels/gptoss_mxfp4_expert_fused.wgsl +127 -0
  616. package/src/gpu/kernels/gptoss_router_topk.wgsl +119 -0
  617. package/src/gpu/kernels/grouped_pointwise_conv2d.d.ts +27 -0
  618. package/src/gpu/kernels/grouped_pointwise_conv2d.js +103 -0
  619. package/src/gpu/kernels/grouped_pointwise_conv2d.wgsl +44 -0
  620. package/src/gpu/kernels/grouped_pointwise_conv2d_f16.wgsl +48 -0
  621. package/src/gpu/kernels/groupnorm.d.ts +31 -0
  622. package/src/gpu/kernels/groupnorm.js +102 -0
  623. package/src/gpu/kernels/groupnorm_apply.wgsl +41 -0
  624. package/src/gpu/kernels/groupnorm_apply_f16.wgsl +46 -0
  625. package/src/gpu/kernels/groupnorm_stats.wgsl +76 -0
  626. package/src/gpu/kernels/groupnorm_stats_f16.wgsl +79 -0
  627. package/src/gpu/kernels/index.d.ts +374 -0
  628. package/src/gpu/kernels/index.js +315 -0
  629. package/src/gpu/kernels/kernel-base.d.ts +33 -0
  630. package/src/gpu/kernels/kernel-base.js +46 -0
  631. package/src/gpu/kernels/kernel-configs.d.ts +65 -0
  632. package/src/gpu/kernels/kernel-configs.js +60 -0
  633. package/src/gpu/kernels/kernel-tuning.d.ts +42 -0
  634. package/src/gpu/kernels/kernel-tuning.js +158 -0
  635. package/src/gpu/kernels/kv-quantize.d.ts +37 -0
  636. package/src/gpu/kernels/kv-quantize.js +141 -0
  637. package/src/gpu/kernels/kv_quantize_int4.wgsl +119 -0
  638. package/src/gpu/kernels/kv_quantize_int8.wgsl +119 -0
  639. package/src/gpu/kernels/layernorm.d.ts +37 -0
  640. package/src/gpu/kernels/layernorm.js +96 -0
  641. package/src/gpu/kernels/layernorm.wgsl +121 -0
  642. package/src/gpu/kernels/layernorm_f16.wgsl +103 -0
  643. package/src/gpu/kernels/linear-attention-core.d.ts +39 -0
  644. package/src/gpu/kernels/linear-attention-core.js +559 -0
  645. package/src/gpu/kernels/logit-merge.d.ts +110 -0
  646. package/src/gpu/kernels/logit-merge.js +394 -0
  647. package/src/gpu/kernels/matmul-dispatch.d.ts +38 -0
  648. package/src/gpu/kernels/matmul-dispatch.js +156 -0
  649. package/src/gpu/kernels/matmul-selection.d.ts +87 -0
  650. package/src/gpu/kernels/matmul-selection.js +518 -0
  651. package/src/gpu/kernels/matmul.d.ts +114 -0
  652. package/src/gpu/kernels/matmul.js +384 -0
  653. package/src/gpu/kernels/matmul_f16.wgsl +170 -0
  654. package/src/gpu/kernels/matmul_f16_tiled.wgsl +165 -0
  655. package/src/gpu/kernels/matmul_f16w_f32a.wgsl +89 -0
  656. package/src/gpu/kernels/matmul_f16w_f32a_tiled.wgsl +154 -0
  657. package/src/gpu/kernels/matmul_f32.wgsl +100 -0
  658. package/src/gpu/kernels/matmul_gemv.wgsl +80 -0
  659. package/src/gpu/kernels/matmul_gemv_f16a.wgsl +81 -0
  660. package/src/gpu/kernels/matmul_gemv_residual.wgsl +119 -0
  661. package/src/gpu/kernels/matmul_gemv_residual_f16.wgsl +78 -0
  662. package/src/gpu/kernels/matmul_gemv_subgroup.wgsl +429 -0
  663. package/src/gpu/kernels/matmul_gemv_subgroup_f16a.wgsl +674 -0
  664. package/src/gpu/kernels/modulate.d.ts +29 -0
  665. package/src/gpu/kernels/modulate.js +57 -0
  666. package/src/gpu/kernels/modulate.wgsl +40 -0
  667. package/src/gpu/kernels/modulate_f16.wgsl +43 -0
  668. package/src/gpu/kernels/moe.d.ts +164 -0
  669. package/src/gpu/kernels/moe.js +542 -0
  670. package/src/gpu/kernels/moe_gather.wgsl +170 -0
  671. package/src/gpu/kernels/moe_gather_f16.wgsl +82 -0
  672. package/src/gpu/kernels/moe_gather_vec4.wgsl +74 -0
  673. package/src/gpu/kernels/moe_offsets.wgsl +48 -0
  674. package/src/gpu/kernels/pipeline-cache.d.ts +88 -0
  675. package/src/gpu/kernels/pipeline-cache.js +305 -0
  676. package/src/gpu/kernels/pixel_shuffle.d.ts +27 -0
  677. package/src/gpu/kernels/pixel_shuffle.js +57 -0
  678. package/src/gpu/kernels/pixel_shuffle.wgsl +43 -0
  679. package/src/gpu/kernels/pixel_shuffle_f16.wgsl +46 -0
  680. package/src/gpu/kernels/relu.d.ts +18 -0
  681. package/src/gpu/kernels/relu.js +66 -0
  682. package/src/gpu/kernels/relu.wgsl +22 -0
  683. package/src/gpu/kernels/relu_f16.wgsl +24 -0
  684. package/src/gpu/kernels/rep-penalty.js +103 -0
  685. package/src/gpu/kernels/rep_penalty.wgsl +57 -0
  686. package/src/gpu/kernels/rep_penalty_f16.wgsl +54 -0
  687. package/src/gpu/kernels/repeat_channels.d.ts +21 -0
  688. package/src/gpu/kernels/repeat_channels.js +68 -0
  689. package/src/gpu/kernels/repeat_channels.wgsl +28 -0
  690. package/src/gpu/kernels/repeat_channels_f16.wgsl +30 -0
  691. package/src/gpu/kernels/residual.d.ts +74 -0
  692. package/src/gpu/kernels/residual.js +173 -0
  693. package/src/gpu/kernels/residual.wgsl +56 -0
  694. package/src/gpu/kernels/residual_f16.wgsl +36 -0
  695. package/src/gpu/kernels/residual_f16_vec4.wgsl +48 -0
  696. package/src/gpu/kernels/residual_vec4.wgsl +47 -0
  697. package/src/gpu/kernels/rmsnorm.d.ts +53 -0
  698. package/src/gpu/kernels/rmsnorm.js +256 -0
  699. package/src/gpu/kernels/rmsnorm.wgsl +449 -0
  700. package/src/gpu/kernels/rmsnorm_f16.wgsl +198 -0
  701. package/src/gpu/kernels/rope.d.ts +50 -0
  702. package/src/gpu/kernels/rope.js +91 -0
  703. package/src/gpu/kernels/rope.wgsl +344 -0
  704. package/src/gpu/kernels/rope_f16.wgsl +271 -0
  705. package/src/gpu/kernels/rule-matcher.d.ts +30 -0
  706. package/src/gpu/kernels/rule-matcher.js +42 -0
  707. package/src/gpu/kernels/rule-registry.d.ts +7 -0
  708. package/src/gpu/kernels/rule-registry.js +54 -0
  709. package/src/gpu/kernels/sample.d.ts +75 -0
  710. package/src/gpu/kernels/sample.js +565 -0
  711. package/src/gpu/kernels/sample.wgsl +407 -0
  712. package/src/gpu/kernels/sample_f16.wgsl +363 -0
  713. package/src/gpu/kernels/sana_linear_attention.d.ts +27 -0
  714. package/src/gpu/kernels/sana_linear_attention.js +129 -0
  715. package/src/gpu/kernels/sana_linear_attention_apply.wgsl +43 -0
  716. package/src/gpu/kernels/sana_linear_attention_apply_f16.wgsl +46 -0
  717. package/src/gpu/kernels/sana_linear_attention_summary.wgsl +51 -0
  718. package/src/gpu/kernels/sana_linear_attention_summary_f16.wgsl +53 -0
  719. package/src/gpu/kernels/scale.d.ts +35 -0
  720. package/src/gpu/kernels/scale.js +44 -0
  721. package/src/gpu/kernels/scale.wgsl +38 -0
  722. package/src/gpu/kernels/scatter_add.wgsl +88 -0
  723. package/src/gpu/kernels/scatter_add_dynamic.wgsl +59 -0
  724. package/src/gpu/kernels/scatter_add_dynamic_f16.wgsl +52 -0
  725. package/src/gpu/kernels/scatter_add_dynamic_f16_weights.wgsl +50 -0
  726. package/src/gpu/kernels/scatter_add_vec4.wgsl +70 -0
  727. package/src/gpu/kernels/shader-cache.d.ts +56 -0
  728. package/src/gpu/kernels/shader-cache.js +234 -0
  729. package/src/gpu/kernels/silu.d.ts +76 -0
  730. package/src/gpu/kernels/silu.js +406 -0
  731. package/src/gpu/kernels/silu.wgsl +109 -0
  732. package/src/gpu/kernels/silu_f16.wgsl +108 -0
  733. package/src/gpu/kernels/softmax.d.ts +57 -0
  734. package/src/gpu/kernels/softmax.js +125 -0
  735. package/src/gpu/kernels/softmax.wgsl +388 -0
  736. package/src/gpu/kernels/softmax_subgroup.wgsl +175 -0
  737. package/src/gpu/kernels/split_qg.d.ts +50 -0
  738. package/src/gpu/kernels/split_qg.js +46 -0
  739. package/src/gpu/kernels/split_qg.wgsl +58 -0
  740. package/src/gpu/kernels/split_qg_f16.wgsl +62 -0
  741. package/src/gpu/kernels/split_qkv.d.ts +51 -0
  742. package/src/gpu/kernels/split_qkv.js +51 -0
  743. package/src/gpu/kernels/split_qkv.wgsl +71 -0
  744. package/src/gpu/kernels/split_qkv_f16.wgsl +75 -0
  745. package/src/gpu/kernels/topk.wgsl +243 -0
  746. package/src/gpu/kernels/topk_f16.wgsl +108 -0
  747. package/src/gpu/kernels/topk_f16_weights.wgsl +101 -0
  748. package/src/gpu/kernels/transpose.d.ts +21 -0
  749. package/src/gpu/kernels/transpose.js +51 -0
  750. package/src/gpu/kernels/transpose.wgsl +33 -0
  751. package/src/gpu/kernels/types.d.ts +21 -0
  752. package/src/gpu/kernels/types.js +4 -0
  753. package/src/gpu/kernels/uniform-utils.d.ts +48 -0
  754. package/src/gpu/kernels/uniform-utils.js +94 -0
  755. package/src/gpu/kernels/upsample2d.d.ts +25 -0
  756. package/src/gpu/kernels/upsample2d.js +67 -0
  757. package/src/gpu/kernels/upsample2d.wgsl +34 -0
  758. package/src/gpu/kernels/upsample2d_f16.wgsl +38 -0
  759. package/src/gpu/kernels/utils.d.ts +106 -0
  760. package/src/gpu/kernels/utils.js +257 -0
  761. package/src/gpu/multi-model-recorder.d.ts +21 -0
  762. package/src/gpu/multi-model-recorder.js +31 -0
  763. package/src/gpu/partitioned-buffer-pool.d.ts +28 -0
  764. package/src/gpu/partitioned-buffer-pool.js +57 -0
  765. package/src/gpu/perf-guards.d.ts +25 -0
  766. package/src/gpu/perf-guards.js +133 -0
  767. package/src/gpu/profiler.d.ts +114 -0
  768. package/src/gpu/profiler.js +406 -0
  769. package/src/gpu/readback-utils.d.ts +16 -0
  770. package/src/gpu/readback-utils.js +41 -0
  771. package/src/gpu/submit-tracker.d.ts +111 -0
  772. package/src/gpu/submit-tracker.js +242 -0
  773. package/src/gpu/tensor.d.ts +69 -0
  774. package/src/gpu/tensor.js +75 -0
  775. package/src/gpu/uniform-cache.d.ts +109 -0
  776. package/src/gpu/uniform-cache.js +263 -0
  777. package/src/gpu/weight-buffer.d.ts +121 -0
  778. package/src/gpu/weight-buffer.js +132 -0
  779. package/src/hotswap/intent-bundle.d.ts +37 -0
  780. package/src/hotswap/intent-bundle.js +129 -0
  781. package/src/hotswap/manifest.d.ts +42 -0
  782. package/src/hotswap/manifest.js +124 -0
  783. package/src/hotswap/model-swap.js +42 -0
  784. package/src/hotswap/runtime.d.ts +31 -0
  785. package/src/hotswap/runtime.js +150 -0
  786. package/src/index-browser.d.ts +92 -0
  787. package/src/index-browser.js +68 -0
  788. package/src/index-internal.d.ts +2 -0
  789. package/src/index-internal.js +2 -0
  790. package/src/index.d.ts +103 -0
  791. package/src/index.js +76 -0
  792. package/src/inference/README.md +593 -0
  793. package/src/inference/browser-harness-contract-helpers.d.ts +5 -0
  794. package/src/inference/browser-harness-contract-helpers.js +26 -0
  795. package/src/inference/browser-harness-diffusion-energy-suites.d.ts +2 -0
  796. package/src/inference/browser-harness-diffusion-energy-suites.js +269 -0
  797. package/src/inference/browser-harness-model-helpers.d.ts +16 -0
  798. package/src/inference/browser-harness-model-helpers.js +217 -0
  799. package/src/inference/browser-harness-report-helpers.d.ts +7 -0
  800. package/src/inference/browser-harness-report-helpers.js +42 -0
  801. package/src/inference/browser-harness-runtime-helpers.d.ts +61 -0
  802. package/src/inference/browser-harness-runtime-helpers.js +416 -0
  803. package/src/inference/browser-harness-suite-helpers.d.ts +28 -0
  804. package/src/inference/browser-harness-suite-helpers.js +270 -0
  805. package/src/inference/browser-harness-text-helpers.d.ts +34 -0
  806. package/src/inference/browser-harness-text-helpers.js +879 -0
  807. package/src/inference/browser-harness.d.ts +249 -0
  808. package/src/inference/browser-harness.js +1150 -0
  809. package/src/inference/decode-buffers.d.ts +108 -0
  810. package/src/inference/decode-buffers.js +181 -0
  811. package/src/inference/decode-ring.d.ts +52 -0
  812. package/src/inference/decode-ring.js +273 -0
  813. package/src/inference/expert-router.d.ts +27 -0
  814. package/src/inference/expert-router.js +55 -0
  815. package/src/inference/fixtures/embedding-semantic-fixtures.json +382 -0
  816. package/src/inference/functiongemma.d.ts +15 -0
  817. package/src/inference/functiongemma.js +1 -0
  818. package/src/inference/kv-cache/base.d.ts +150 -0
  819. package/src/inference/kv-cache/base.js +1105 -0
  820. package/src/inference/kv-cache/basis-decomposed-paged.d.ts +50 -0
  821. package/src/inference/kv-cache/basis-decomposed-paged.js +276 -0
  822. package/src/inference/kv-cache/index.d.ts +35 -0
  823. package/src/inference/kv-cache/index.js +20 -0
  824. package/src/inference/kv-cache/sliding-window.d.ts +72 -0
  825. package/src/inference/kv-cache/sliding-window.js +243 -0
  826. package/src/inference/kv-cache/tiered.d.ts +89 -0
  827. package/src/inference/kv-cache/tiered.js +576 -0
  828. package/src/inference/kv-cache/types.d.ts +188 -0
  829. package/src/inference/kv-cache/types.js +80 -0
  830. package/src/inference/kv-cache.d.ts +36 -0
  831. package/src/inference/kv-cache.js +18 -0
  832. package/src/inference/moe-router.d.ts +212 -0
  833. package/src/inference/moe-router.js +600 -0
  834. package/src/inference/multi-model-network.d.ts +139 -0
  835. package/src/inference/multi-model-network.js +771 -0
  836. package/src/inference/multi-pipeline-pool.d.ts +62 -0
  837. package/src/inference/multi-pipeline-pool.js +161 -0
  838. package/src/inference/network-evolution.d.ts +55 -0
  839. package/src/inference/network-evolution.js +79 -0
  840. package/src/inference/pipelines/context.d.ts +21 -0
  841. package/src/inference/pipelines/context.js +184 -0
  842. package/src/inference/pipelines/diffusion/helpers.d.ts +29 -0
  843. package/src/inference/pipelines/diffusion/helpers.js +120 -0
  844. package/src/inference/pipelines/diffusion/index.d.ts +3 -0
  845. package/src/inference/pipelines/diffusion/index.js +3 -0
  846. package/src/inference/pipelines/diffusion/init.d.ts +24 -0
  847. package/src/inference/pipelines/diffusion/init.js +138 -0
  848. package/src/inference/pipelines/diffusion/pipeline.d.ts +38 -0
  849. package/src/inference/pipelines/diffusion/pipeline.js +782 -0
  850. package/src/inference/pipelines/diffusion/sana-transformer.d.ts +53 -0
  851. package/src/inference/pipelines/diffusion/sana-transformer.js +738 -0
  852. package/src/inference/pipelines/diffusion/scheduler.d.ts +35 -0
  853. package/src/inference/pipelines/diffusion/scheduler.js +153 -0
  854. package/src/inference/pipelines/diffusion/sd3-transformer.d.ts +20 -0
  855. package/src/inference/pipelines/diffusion/sd3-transformer.js +1194 -0
  856. package/src/inference/pipelines/diffusion/sd3-weights.d.ts +21 -0
  857. package/src/inference/pipelines/diffusion/sd3-weights.js +287 -0
  858. package/src/inference/pipelines/diffusion/text-encoder-gpu.d.ts +87 -0
  859. package/src/inference/pipelines/diffusion/text-encoder-gpu.js +1224 -0
  860. package/src/inference/pipelines/diffusion/text-encoder.d.ts +29 -0
  861. package/src/inference/pipelines/diffusion/text-encoder.js +195 -0
  862. package/src/inference/pipelines/diffusion/types.d.ts +116 -0
  863. package/src/inference/pipelines/diffusion/types.js +1 -0
  864. package/src/inference/pipelines/diffusion/vae.d.ts +20 -0
  865. package/src/inference/pipelines/diffusion/vae.js +1375 -0
  866. package/src/inference/pipelines/diffusion/weights.d.ts +40 -0
  867. package/src/inference/pipelines/diffusion/weights.js +155 -0
  868. package/src/inference/pipelines/dream/energy-head-pipeline.d.ts +29 -0
  869. package/src/inference/pipelines/dream/energy-head-pipeline.js +6 -0
  870. package/src/inference/pipelines/dream/pipeline.d.ts +17 -0
  871. package/src/inference/pipelines/dream/pipeline.js +8 -0
  872. package/src/inference/pipelines/energy/index.d.ts +1 -0
  873. package/src/inference/pipelines/energy/index.js +1 -0
  874. package/src/inference/pipelines/energy/pipeline.d.ts +27 -0
  875. package/src/inference/pipelines/energy/pipeline.js +697 -0
  876. package/src/inference/pipelines/energy/quintel.d.ts +92 -0
  877. package/src/inference/pipelines/energy/quintel.js +218 -0
  878. package/src/inference/pipelines/energy/types.d.ts +63 -0
  879. package/src/inference/pipelines/energy/types.js +1 -0
  880. package/src/inference/pipelines/energy-head/index.d.ts +6 -0
  881. package/src/inference/pipelines/energy-head/index.js +6 -0
  882. package/src/inference/pipelines/energy-head/row-head-pipeline.d.ts +103 -0
  883. package/src/inference/pipelines/energy-head/row-head-pipeline.js +491 -0
  884. package/src/inference/pipelines/factory.d.ts +10 -0
  885. package/src/inference/pipelines/factory.js +6 -0
  886. package/src/inference/pipelines/index.d.ts +22 -0
  887. package/src/inference/pipelines/index.js +19 -0
  888. package/src/inference/pipelines/registry.d.ts +15 -0
  889. package/src/inference/pipelines/registry.js +23 -0
  890. package/src/inference/pipelines/rng.d.ts +2 -0
  891. package/src/inference/pipelines/rng.js +17 -0
  892. package/src/inference/pipelines/structured/index.d.ts +8 -0
  893. package/src/inference/pipelines/structured/index.js +8 -0
  894. package/src/inference/pipelines/structured/json-head-pipeline.d.ts +58 -0
  895. package/src/inference/pipelines/structured/json-head-pipeline.js +196 -0
  896. package/src/inference/pipelines/text/attention/attn-config.d.ts +28 -0
  897. package/src/inference/pipelines/text/attention/attn-config.js +51 -0
  898. package/src/inference/pipelines/text/attention/dispatch-params.d.ts +148 -0
  899. package/src/inference/pipelines/text/attention/dispatch-params.js +251 -0
  900. package/src/inference/pipelines/text/attention/index.d.ts +24 -0
  901. package/src/inference/pipelines/text/attention/index.js +17 -0
  902. package/src/inference/pipelines/text/attention/output-projection.d.ts +12 -0
  903. package/src/inference/pipelines/text/attention/output-projection.js +8 -0
  904. package/src/inference/pipelines/text/attention/projections.d.ts +114 -0
  905. package/src/inference/pipelines/text/attention/projections.js +531 -0
  906. package/src/inference/pipelines/text/attention/record.d.ts +36 -0
  907. package/src/inference/pipelines/text/attention/record.js +523 -0
  908. package/src/inference/pipelines/text/attention/run.d.ts +38 -0
  909. package/src/inference/pipelines/text/attention/run.js +813 -0
  910. package/src/inference/pipelines/text/attention/types.d.ts +108 -0
  911. package/src/inference/pipelines/text/attention/types.js +67 -0
  912. package/src/inference/pipelines/text/attention.d.ts +23 -0
  913. package/src/inference/pipelines/text/attention.js +12 -0
  914. package/src/inference/pipelines/text/bdpa-steamroller.d.ts +22 -0
  915. package/src/inference/pipelines/text/bdpa-steamroller.js +158 -0
  916. package/src/inference/pipelines/text/buffer-types.d.ts +7 -0
  917. package/src/inference/pipelines/text/buffer-types.js +4 -0
  918. package/src/inference/pipelines/text/chat-format.d.ts +46 -0
  919. package/src/inference/pipelines/text/chat-format.js +365 -0
  920. package/src/inference/pipelines/text/config.d.ts +253 -0
  921. package/src/inference/pipelines/text/config.js +857 -0
  922. package/src/inference/pipelines/text/debug-utils/config.d.ts +144 -0
  923. package/src/inference/pipelines/text/debug-utils/config.js +156 -0
  924. package/src/inference/pipelines/text/debug-utils/index.d.ts +53 -0
  925. package/src/inference/pipelines/text/debug-utils/index.js +44 -0
  926. package/src/inference/pipelines/text/debug-utils/logging.d.ts +106 -0
  927. package/src/inference/pipelines/text/debug-utils/logging.js +152 -0
  928. package/src/inference/pipelines/text/debug-utils/tensor.d.ts +119 -0
  929. package/src/inference/pipelines/text/debug-utils/tensor.js +268 -0
  930. package/src/inference/pipelines/text/debug-utils/utils.d.ts +77 -0
  931. package/src/inference/pipelines/text/debug-utils/utils.js +139 -0
  932. package/src/inference/pipelines/text/debug-utils.d.ts +42 -0
  933. package/src/inference/pipelines/text/debug-utils.js +34 -0
  934. package/src/inference/pipelines/text/drift-policy.d.ts +57 -0
  935. package/src/inference/pipelines/text/drift-policy.js +133 -0
  936. package/src/inference/pipelines/text/dtype-contract.d.ts +38 -0
  937. package/src/inference/pipelines/text/dtype-contract.js +87 -0
  938. package/src/inference/pipelines/text/embed.d.ts +67 -0
  939. package/src/inference/pipelines/text/embed.js +490 -0
  940. package/src/inference/pipelines/text/execution-plan.d.ts +123 -0
  941. package/src/inference/pipelines/text/execution-plan.js +371 -0
  942. package/src/inference/pipelines/text/execution-runtime-builders.d.ts +45 -0
  943. package/src/inference/pipelines/text/execution-runtime-builders.js +382 -0
  944. package/src/inference/pipelines/text/execution-v1.d.ts +45 -0
  945. package/src/inference/pipelines/text/execution-v1.js +288 -0
  946. package/src/inference/pipelines/text/failure-reduction.d.ts +84 -0
  947. package/src/inference/pipelines/text/failure-reduction.js +180 -0
  948. package/src/inference/pipelines/text/ffn/dense.d.ts +40 -0
  949. package/src/inference/pipelines/text/ffn/dense.js +753 -0
  950. package/src/inference/pipelines/text/ffn/index.d.ts +23 -0
  951. package/src/inference/pipelines/text/ffn/index.js +16 -0
  952. package/src/inference/pipelines/text/ffn/moe.d.ts +21 -0
  953. package/src/inference/pipelines/text/ffn/moe.js +49 -0
  954. package/src/inference/pipelines/text/ffn/sandwich.d.ts +25 -0
  955. package/src/inference/pipelines/text/ffn/sandwich.js +196 -0
  956. package/src/inference/pipelines/text/ffn/standard.d.ts +23 -0
  957. package/src/inference/pipelines/text/ffn/standard.js +113 -0
  958. package/src/inference/pipelines/text/ffn/types.d.ts +30 -0
  959. package/src/inference/pipelines/text/ffn/types.js +25 -0
  960. package/src/inference/pipelines/text/ffn.d.ts +31 -0
  961. package/src/inference/pipelines/text/ffn.js +18 -0
  962. package/src/inference/pipelines/text/finiteness-guard-status.d.ts +11 -0
  963. package/src/inference/pipelines/text/finiteness-guard-status.js +21 -0
  964. package/src/inference/pipelines/text/finiteness-policy.d.ts +35 -0
  965. package/src/inference/pipelines/text/finiteness-policy.js +45 -0
  966. package/src/inference/pipelines/text/generator-helpers.d.ts +34 -0
  967. package/src/inference/pipelines/text/generator-helpers.js +178 -0
  968. package/src/inference/pipelines/text/generator-runtime.d.ts +95 -0
  969. package/src/inference/pipelines/text/generator-runtime.js +492 -0
  970. package/src/inference/pipelines/text/generator-steps.d.ts +136 -0
  971. package/src/inference/pipelines/text/generator-steps.js +1294 -0
  972. package/src/inference/pipelines/text/generator.d.ts +46 -0
  973. package/src/inference/pipelines/text/generator.js +1779 -0
  974. package/src/inference/pipelines/text/index.d.ts +5 -0
  975. package/src/inference/pipelines/text/index.js +6 -0
  976. package/src/inference/pipelines/text/init.d.ts +316 -0
  977. package/src/inference/pipelines/text/init.js +1187 -0
  978. package/src/inference/pipelines/text/kernel-trace.d.ts +152 -0
  979. package/src/inference/pipelines/text/kernel-trace.js +330 -0
  980. package/src/inference/pipelines/text/layer-plan.d.ts +65 -0
  981. package/src/inference/pipelines/text/layer-plan.js +249 -0
  982. package/src/inference/pipelines/text/layer.d.ts +56 -0
  983. package/src/inference/pipelines/text/layer.js +974 -0
  984. package/src/inference/pipelines/text/linear-attention.d.ts +109 -0
  985. package/src/inference/pipelines/text/linear-attention.js +948 -0
  986. package/src/inference/pipelines/text/logits/cpu.d.ts +81 -0
  987. package/src/inference/pipelines/text/logits/cpu.js +91 -0
  988. package/src/inference/pipelines/text/logits/gpu.d.ts +113 -0
  989. package/src/inference/pipelines/text/logits/gpu.js +419 -0
  990. package/src/inference/pipelines/text/logits/index.d.ts +62 -0
  991. package/src/inference/pipelines/text/logits/index.js +316 -0
  992. package/src/inference/pipelines/text/logits/types.d.ts +46 -0
  993. package/src/inference/pipelines/text/logits/types.js +4 -0
  994. package/src/inference/pipelines/text/logits/utils.d.ts +56 -0
  995. package/src/inference/pipelines/text/logits/utils.js +68 -0
  996. package/src/inference/pipelines/text/logits.d.ts +27 -0
  997. package/src/inference/pipelines/text/logits.js +16 -0
  998. package/src/inference/pipelines/text/lora-apply.d.ts +28 -0
  999. package/src/inference/pipelines/text/lora-apply.js +76 -0
  1000. package/src/inference/pipelines/text/lora-types.d.ts +39 -0
  1001. package/src/inference/pipelines/text/lora-types.js +18 -0
  1002. package/src/inference/pipelines/text/lora.d.ts +18 -0
  1003. package/src/inference/pipelines/text/lora.js +12 -0
  1004. package/src/inference/pipelines/text/model-load.d.ts +58 -0
  1005. package/src/inference/pipelines/text/model-load.js +778 -0
  1006. package/src/inference/pipelines/text/moe-cache.d.ts +32 -0
  1007. package/src/inference/pipelines/text/moe-cache.js +108 -0
  1008. package/src/inference/pipelines/text/moe-cpu-gptoss.d.ts +9 -0
  1009. package/src/inference/pipelines/text/moe-cpu-gptoss.js +115 -0
  1010. package/src/inference/pipelines/text/moe-cpu.d.ts +13 -0
  1011. package/src/inference/pipelines/text/moe-cpu.js +120 -0
  1012. package/src/inference/pipelines/text/moe-gpu.d.ts +13 -0
  1013. package/src/inference/pipelines/text/moe-gpu.js +653 -0
  1014. package/src/inference/pipelines/text/moe-helpers.d.ts +12 -0
  1015. package/src/inference/pipelines/text/moe-helpers.js +21 -0
  1016. package/src/inference/pipelines/text/moe-impl.d.ts +117 -0
  1017. package/src/inference/pipelines/text/moe-impl.js +9 -0
  1018. package/src/inference/pipelines/text/moe-shape-validator.d.ts +40 -0
  1019. package/src/inference/pipelines/text/moe-shape-validator.js +98 -0
  1020. package/src/inference/pipelines/text/operator-events.d.ts +125 -0
  1021. package/src/inference/pipelines/text/operator-events.js +305 -0
  1022. package/src/inference/pipelines/text/operator-identity.d.ts +60 -0
  1023. package/src/inference/pipelines/text/operator-identity.js +133 -0
  1024. package/src/inference/pipelines/text/ops.d.ts +167 -0
  1025. package/src/inference/pipelines/text/ops.js +437 -0
  1026. package/src/inference/pipelines/text/probes.d.ts +31 -0
  1027. package/src/inference/pipelines/text/probes.js +240 -0
  1028. package/src/inference/pipelines/text/sampling-config.d.ts +54 -0
  1029. package/src/inference/pipelines/text/sampling-config.js +71 -0
  1030. package/src/inference/pipelines/text/sampling.d.ts +54 -0
  1031. package/src/inference/pipelines/text/sampling.js +249 -0
  1032. package/src/inference/pipelines/text/stage-names.d.ts +88 -0
  1033. package/src/inference/pipelines/text/stage-names.js +202 -0
  1034. package/src/inference/pipelines/text/state.d.ts +114 -0
  1035. package/src/inference/pipelines/text/state.js +165 -0
  1036. package/src/inference/pipelines/text/types.d.ts +682 -0
  1037. package/src/inference/pipelines/text/types.js +4 -0
  1038. package/src/inference/pipelines/text/weights.d.ts +110 -0
  1039. package/src/inference/pipelines/text/weights.js +173 -0
  1040. package/src/inference/pipelines/text.d.ts +162 -0
  1041. package/src/inference/pipelines/text.js +852 -0
  1042. package/src/inference/pipelines/vision/encoder.js +386 -0
  1043. package/src/inference/pipelines/vision/image-preprocess.js +157 -0
  1044. package/src/inference/pipelines/vision/index.js +173 -0
  1045. package/src/inference/pipelines/vision/ops.js +78 -0
  1046. package/src/inference/pipelines/vision/patch-embed.js +151 -0
  1047. package/src/inference/speculative.d.ts +239 -0
  1048. package/src/inference/speculative.js +402 -0
  1049. package/src/inference/test-harness.d.ts +178 -0
  1050. package/src/inference/test-harness.js +361 -0
  1051. package/src/inference/tokenizer.d.ts +72 -0
  1052. package/src/inference/tokenizer.js +251 -0
  1053. package/src/inference/tokenizers/base.d.ts +39 -0
  1054. package/src/inference/tokenizers/base.js +69 -0
  1055. package/src/inference/tokenizers/bpe.d.ts +27 -0
  1056. package/src/inference/tokenizers/bpe.js +180 -0
  1057. package/src/inference/tokenizers/bundled.d.ts +63 -0
  1058. package/src/inference/tokenizers/bundled.js +1009 -0
  1059. package/src/inference/tokenizers/sentencepiece.d.ts +28 -0
  1060. package/src/inference/tokenizers/sentencepiece.js +401 -0
  1061. package/src/inference/tokenizers/types.d.ts +166 -0
  1062. package/src/inference/tokenizers/types.js +7 -0
  1063. package/src/loader/doppler-loader.d.ts +139 -0
  1064. package/src/loader/doppler-loader.js +1103 -0
  1065. package/src/loader/dtype-utils.d.ts +40 -0
  1066. package/src/loader/dtype-utils.js +61 -0
  1067. package/src/loader/embedding-loader.d.ts +56 -0
  1068. package/src/loader/embedding-loader.js +211 -0
  1069. package/src/loader/experts/expert-cache.d.ts +156 -0
  1070. package/src/loader/experts/expert-cache.js +386 -0
  1071. package/src/loader/experts/expert-loader.d.ts +108 -0
  1072. package/src/loader/experts/expert-loader.js +392 -0
  1073. package/src/loader/final-weights-loader.d.ts +88 -0
  1074. package/src/loader/final-weights-loader.js +339 -0
  1075. package/src/loader/index.d.ts +150 -0
  1076. package/src/loader/index.js +124 -0
  1077. package/src/loader/layer-loader.d.ts +63 -0
  1078. package/src/loader/layer-loader.js +474 -0
  1079. package/src/loader/loader-state.d.ts +51 -0
  1080. package/src/loader/loader-state.js +142 -0
  1081. package/src/loader/loader-types.d.ts +236 -0
  1082. package/src/loader/loader-types.js +4 -0
  1083. package/src/loader/manifest-config.d.ts +97 -0
  1084. package/src/loader/manifest-config.js +134 -0
  1085. package/src/loader/memory-monitor.d.ts +112 -0
  1086. package/src/loader/memory-monitor.js +284 -0
  1087. package/src/loader/multi-model-loader.d.ts +51 -0
  1088. package/src/loader/multi-model-loader.js +133 -0
  1089. package/src/loader/quantization-constants.d.ts +23 -0
  1090. package/src/loader/quantization-constants.js +14 -0
  1091. package/src/loader/shard-cache.d.ts +60 -0
  1092. package/src/loader/shard-cache.js +663 -0
  1093. package/src/loader/shard-resolver.d.ts +12 -0
  1094. package/src/loader/shard-resolver.js +144 -0
  1095. package/src/loader/tensors/tensor-loader.d.ts +157 -0
  1096. package/src/loader/tensors/tensor-loader.js +652 -0
  1097. package/src/loader/tensors/tensor-reader.d.ts +22 -0
  1098. package/src/loader/tensors/tensor-reader.js +113 -0
  1099. package/src/loader/tensors/tensor-role.d.ts +7 -0
  1100. package/src/loader/tensors/tensor-role.js +12 -0
  1101. package/src/loader/weight-downcast.d.ts +62 -0
  1102. package/src/loader/weight-downcast.js +213 -0
  1103. package/src/loader/weights.d.ts +22 -0
  1104. package/src/loader/weights.js +4 -0
  1105. package/src/memory/address-table.d.ts +104 -0
  1106. package/src/memory/address-table.js +114 -0
  1107. package/src/memory/buffer-pool.d.ts +204 -0
  1108. package/src/memory/buffer-pool.js +867 -0
  1109. package/src/memory/capability.d.ts +49 -0
  1110. package/src/memory/capability.js +95 -0
  1111. package/src/memory/heap-manager.d.ts +104 -0
  1112. package/src/memory/heap-manager.js +264 -0
  1113. package/src/memory/unified-detect.d.ts +59 -0
  1114. package/src/memory/unified-detect.js +192 -0
  1115. package/src/rules/converter/execution.rules.json +20 -0
  1116. package/src/rules/converter/tensor-roles.rules.json +13 -0
  1117. package/src/rules/converter/tokenizer.rules.json +7 -0
  1118. package/src/rules/execution-rules-contract-check.d.ts +17 -0
  1119. package/src/rules/execution-rules-contract-check.js +287 -0
  1120. package/src/rules/inference/attention.rules.json +54 -0
  1121. package/src/rules/inference/capability-transforms.rules.json +24 -0
  1122. package/src/rules/inference/config.rules.json +57 -0
  1123. package/src/rules/inference/dtype.rules.json +99 -0
  1124. package/src/rules/inference/execution.rules.json +56 -0
  1125. package/src/rules/inference/ffn.rules.json +35 -0
  1126. package/src/rules/inference/layer-pattern.rules.json +16 -0
  1127. package/src/rules/inference/layer.rules.json +7 -0
  1128. package/src/rules/inference/moe.rules.json +48 -0
  1129. package/src/rules/kernels/attention.rules.json +61 -0
  1130. package/src/rules/kernels/conv2d.rules.json +6 -0
  1131. package/src/rules/kernels/depthwise-conv2d.rules.json +6 -0
  1132. package/src/rules/kernels/dequant.rules.json +58 -0
  1133. package/src/rules/kernels/energy.rules.json +22 -0
  1134. package/src/rules/kernels/fused-ffn.rules.json +13 -0
  1135. package/src/rules/kernels/fused-matmul-residual.rules.json +6 -0
  1136. package/src/rules/kernels/fused-matmul-rmsnorm.rules.json +8 -0
  1137. package/src/rules/kernels/gather.rules.json +12 -0
  1138. package/src/rules/kernels/gelu.rules.json +11 -0
  1139. package/src/rules/kernels/grouped-pointwise-conv2d.rules.json +6 -0
  1140. package/src/rules/kernels/groupnorm.rules.json +10 -0
  1141. package/src/rules/kernels/kernel-validator.d.ts +24 -0
  1142. package/src/rules/kernels/kernel-validator.js +160 -0
  1143. package/src/rules/kernels/kv_quantize.rules.json +7 -0
  1144. package/src/rules/kernels/layernorm.rules.json +6 -0
  1145. package/src/rules/kernels/matmul.rules.json +60 -0
  1146. package/src/rules/kernels/modulate.rules.json +6 -0
  1147. package/src/rules/kernels/moe.rules.gptoss.json +105 -0
  1148. package/src/rules/kernels/moe.rules.json +11 -0
  1149. package/src/rules/kernels/moe.rules.mixtral.json +75 -0
  1150. package/src/rules/kernels/pixel_shuffle.rules.json +6 -0
  1151. package/src/rules/kernels/relu.rules.json +6 -0
  1152. package/src/rules/kernels/repeat-channels.rules.json +6 -0
  1153. package/src/rules/kernels/residual.rules.json +12 -0
  1154. package/src/rules/kernels/rmsnorm.rules.json +11 -0
  1155. package/src/rules/kernels/rope.rules.json +6 -0
  1156. package/src/rules/kernels/sample.rules.json +6 -0
  1157. package/src/rules/kernels/sana-linear-attention.rules.json +6 -0
  1158. package/src/rules/kernels/scale.rules.json +6 -0
  1159. package/src/rules/kernels/silu.rules.json +21 -0
  1160. package/src/rules/kernels/softmax.rules.json +25 -0
  1161. package/src/rules/kernels/split-qg.rules.json +6 -0
  1162. package/src/rules/kernels/split-qkv.rules.json +6 -0
  1163. package/src/rules/kernels/upsample2d.rules.json +6 -0
  1164. package/src/rules/layer-pattern-contract-check.d.ts +17 -0
  1165. package/src/rules/layer-pattern-contract-check.js +231 -0
  1166. package/src/rules/loader/tensor-loader.rules.json +24 -0
  1167. package/src/rules/loader/weights.rules.json +41 -0
  1168. package/src/rules/rule-registry.d.ts +77 -0
  1169. package/src/rules/rule-registry.js +264 -0
  1170. package/src/rules/tooling/command-runtime.rules.json +65 -0
  1171. package/src/storage/backends/idb-store.d.ts +52 -0
  1172. package/src/storage/backends/idb-store.js +590 -0
  1173. package/src/storage/backends/memory-store.d.ts +36 -0
  1174. package/src/storage/backends/memory-store.js +242 -0
  1175. package/src/storage/backends/opfs-store.d.ts +41 -0
  1176. package/src/storage/backends/opfs-store.js +478 -0
  1177. package/src/storage/blake3.d.ts +17 -0
  1178. package/src/storage/blake3.js +269 -0
  1179. package/src/storage/download-types.d.ts +157 -0
  1180. package/src/storage/download-types.js +48 -0
  1181. package/src/storage/downloader.d.ts +103 -0
  1182. package/src/storage/downloader.js +1121 -0
  1183. package/src/storage/emulated-vram.d.ts +264 -0
  1184. package/src/storage/emulated-vram.js +576 -0
  1185. package/src/storage/export.d.ts +20 -0
  1186. package/src/storage/export.js +159 -0
  1187. package/src/storage/index.d.ts +256 -0
  1188. package/src/storage/index.js +188 -0
  1189. package/src/storage/inventory.d.ts +26 -0
  1190. package/src/storage/inventory.js +218 -0
  1191. package/src/storage/preflight.d.ts +144 -0
  1192. package/src/storage/preflight.js +316 -0
  1193. package/src/storage/quickstart-downloader.d.ts +157 -0
  1194. package/src/storage/quickstart-downloader.js +268 -0
  1195. package/src/storage/quota.d.ts +150 -0
  1196. package/src/storage/quota.js +305 -0
  1197. package/src/storage/registry.d.ts +28 -0
  1198. package/src/storage/registry.js +131 -0
  1199. package/src/storage/reports.d.ts +20 -0
  1200. package/src/storage/reports.js +94 -0
  1201. package/src/storage/shard-manager.d.ts +151 -0
  1202. package/src/storage/shard-manager.js +861 -0
  1203. package/src/storage/source-artifact-store.d.ts +52 -0
  1204. package/src/storage/source-artifact-store.js +234 -0
  1205. package/src/sw.d.ts +1 -0
  1206. package/src/sw.js +187 -0
  1207. package/src/tooling/bench-runner.js +64 -0
  1208. package/src/tooling/browser-command-runner.d.ts +28 -0
  1209. package/src/tooling/browser-command-runner.js +85 -0
  1210. package/src/tooling/command-api-constants.d.ts +9 -0
  1211. package/src/tooling/command-api-constants.js +9 -0
  1212. package/src/tooling/command-api-family-normalizers.d.ts +9 -0
  1213. package/src/tooling/command-api-family-normalizers.js +395 -0
  1214. package/src/tooling/command-api-helpers.d.ts +25 -0
  1215. package/src/tooling/command-api-helpers.js +265 -0
  1216. package/src/tooling/command-api.d.ts +177 -0
  1217. package/src/tooling/command-api.js +82 -0
  1218. package/src/tooling/command-envelope.d.ts +84 -0
  1219. package/src/tooling/command-envelope.js +201 -0
  1220. package/src/tooling/command-runner-shared.d.ts +75 -0
  1221. package/src/tooling/command-runner-shared.js +191 -0
  1222. package/src/tooling/command-runner.html +45 -0
  1223. package/src/tooling/command-validation.js +44 -0
  1224. package/src/tooling/conversion-config-materializer.d.ts +24 -0
  1225. package/src/tooling/conversion-config-materializer.js +96 -0
  1226. package/src/tooling/diagnose-runner.d.ts +28 -0
  1227. package/src/tooling/diagnose-runner.js +333 -0
  1228. package/src/tooling/distill-runner.js +56 -0
  1229. package/src/tooling/hf-registry-utils.d.ts +59 -0
  1230. package/src/tooling/hf-registry-utils.js +324 -0
  1231. package/src/tooling/kernel-path-builder/custom-runtime-facts.d.ts +18 -0
  1232. package/src/tooling/kernel-path-builder/custom-runtime-facts.js +72 -0
  1233. package/src/tooling/kernel-path-builder/index.d.ts +65 -0
  1234. package/src/tooling/kernel-path-builder/index.js +1348 -0
  1235. package/src/tooling/kernel-path-builder/runtime-overlay.d.ts +9 -0
  1236. package/src/tooling/kernel-path-builder/runtime-overlay.js +149 -0
  1237. package/src/tooling/lean-execution-contract-runner.d.ts +43 -0
  1238. package/src/tooling/lean-execution-contract-runner.js +158 -0
  1239. package/src/tooling/lean-execution-contract.d.ts +16 -0
  1240. package/src/tooling/lean-execution-contract.js +228 -0
  1241. package/src/tooling/lora-runner.js +37 -0
  1242. package/src/tooling/node-browser-command-runner.d.ts +34 -0
  1243. package/src/tooling/node-browser-command-runner.js +827 -0
  1244. package/src/tooling/node-command-runner.d.ts +36 -0
  1245. package/src/tooling/node-command-runner.js +187 -0
  1246. package/src/tooling/node-convert-worker-pool.d.ts +16 -0
  1247. package/src/tooling/node-convert-worker-pool.js +186 -0
  1248. package/src/tooling/node-convert-worker.d.ts +1 -0
  1249. package/src/tooling/node-convert-worker.js +60 -0
  1250. package/src/tooling/node-converter.d.ts +1 -0
  1251. package/src/tooling/node-converter.js +1330 -0
  1252. package/src/tooling/node-file-fetch.d.ts +1 -0
  1253. package/src/tooling/node-file-fetch.js +38 -0
  1254. package/src/tooling/node-source-runtime.d.ts +19 -0
  1255. package/src/tooling/node-source-runtime.js +612 -0
  1256. package/src/tooling/node-webgpu.d.ts +16 -0
  1257. package/src/tooling/node-webgpu.js +421 -0
  1258. package/src/tooling/opfs-cache.d.ts +12 -0
  1259. package/src/tooling/opfs-cache.js +260 -0
  1260. package/src/tooling/runtime-input-composition.d.ts +38 -0
  1261. package/src/tooling/runtime-input-composition.js +90 -0
  1262. package/src/tooling/source-runtime-bundle.d.ts +141 -0
  1263. package/src/tooling/source-runtime-bundle.js +712 -0
  1264. package/src/tooling/source-runtime-materializer.d.ts +6 -0
  1265. package/src/tooling/source-runtime-materializer.js +93 -0
  1266. package/src/tooling/verify-runner.js +63 -0
  1267. package/src/tooling-exports.browser.d.ts +7 -0
  1268. package/src/tooling-exports.browser.js +2 -0
  1269. package/src/tooling-exports.d.ts +22 -0
  1270. package/src/tooling-exports.js +7 -0
  1271. package/src/tooling-exports.shared.d.ts +106 -0
  1272. package/src/tooling-exports.shared.js +93 -0
  1273. package/src/training/README.md +153 -0
  1274. package/src/training/artifacts.d.ts +160 -0
  1275. package/src/training/artifacts.js +896 -0
  1276. package/src/training/attention-backward.d.ts +30 -0
  1277. package/src/training/attention-backward.js +232 -0
  1278. package/src/training/attention-forward.d.ts +22 -0
  1279. package/src/training/attention-forward.js +82 -0
  1280. package/src/training/autograd.d.ts +51 -0
  1281. package/src/training/autograd.js +408 -0
  1282. package/src/training/backward-kernels.js +39 -0
  1283. package/src/training/checkpoint-watch.d.ts +8 -0
  1284. package/src/training/checkpoint-watch.js +139 -0
  1285. package/src/training/checkpoint.d.ts +36 -0
  1286. package/src/training/checkpoint.js +281 -0
  1287. package/src/training/clip.d.ts +9 -0
  1288. package/src/training/clip.js +55 -0
  1289. package/src/training/dataloader.d.ts +8 -0
  1290. package/src/training/dataloader.js +44 -0
  1291. package/src/training/datasets/index.d.ts +12 -0
  1292. package/src/training/datasets/index.js +6 -0
  1293. package/src/training/datasets/jsonl.d.ts +11 -0
  1294. package/src/training/datasets/jsonl.js +50 -0
  1295. package/src/training/datasets/reploid.d.ts +3 -0
  1296. package/src/training/datasets/reploid.js +36 -0
  1297. package/src/training/datasets/text-pairs.d.ts +21 -0
  1298. package/src/training/datasets/text-pairs.js +42 -0
  1299. package/src/training/datasets/token-batch.d.ts +21 -0
  1300. package/src/training/datasets/token-batch.js +52 -0
  1301. package/src/training/datasets/translation-pairs.d.ts +34 -0
  1302. package/src/training/datasets/translation-pairs.js +49 -0
  1303. package/src/training/distillation/artifacts.d.ts +71 -0
  1304. package/src/training/distillation/artifacts.js +132 -0
  1305. package/src/training/distillation/checkpoint-watch.d.ts +10 -0
  1306. package/src/training/distillation/checkpoint-watch.js +58 -0
  1307. package/src/training/distillation/dataset.d.ts +59 -0
  1308. package/src/training/distillation/dataset.js +337 -0
  1309. package/src/training/distillation/eval.d.ts +34 -0
  1310. package/src/training/distillation/eval.js +313 -0
  1311. package/src/training/distillation/index.d.ts +29 -0
  1312. package/src/training/distillation/index.js +29 -0
  1313. package/src/training/distillation/runtime.d.ts +20 -0
  1314. package/src/training/distillation/runtime.js +121 -0
  1315. package/src/training/distillation/scoreboard.d.ts +6 -0
  1316. package/src/training/distillation/scoreboard.js +8 -0
  1317. package/src/training/distillation/stage-a.d.ts +45 -0
  1318. package/src/training/distillation/stage-a.js +338 -0
  1319. package/src/training/distillation/stage-b.d.ts +24 -0
  1320. package/src/training/distillation/stage-b.js +20 -0
  1321. package/src/training/distillation/student-fixture.d.ts +22 -0
  1322. package/src/training/distillation/student-fixture.js +846 -0
  1323. package/src/training/distillation/suite-data.d.ts +45 -0
  1324. package/src/training/distillation/suite-data.js +189 -0
  1325. package/src/training/export.d.ts +32 -0
  1326. package/src/training/export.js +112 -0
  1327. package/src/training/index.d.ts +62 -0
  1328. package/src/training/index.js +51 -0
  1329. package/src/training/lora-pipeline.d.ts +40 -0
  1330. package/src/training/lora-pipeline.js +793 -0
  1331. package/src/training/lora.d.ts +19 -0
  1332. package/src/training/lora.js +71 -0
  1333. package/src/training/loss-scaling.d.ts +21 -0
  1334. package/src/training/loss-scaling.js +80 -0
  1335. package/src/training/loss.d.ts +10 -0
  1336. package/src/training/loss.js +40 -0
  1337. package/src/training/objectives/base.d.ts +58 -0
  1338. package/src/training/objectives/base.js +38 -0
  1339. package/src/training/objectives/cross_entropy.d.ts +18 -0
  1340. package/src/training/objectives/cross_entropy.js +34 -0
  1341. package/src/training/objectives/distill_kd.d.ts +16 -0
  1342. package/src/training/objectives/distill_kd.js +369 -0
  1343. package/src/training/objectives/distill_triplet.d.ts +16 -0
  1344. package/src/training/objectives/distill_triplet.js +414 -0
  1345. package/src/training/objectives/index.d.ts +12 -0
  1346. package/src/training/objectives/index.js +6 -0
  1347. package/src/training/objectives/ul_stage1_joint.d.ts +16 -0
  1348. package/src/training/objectives/ul_stage1_joint.js +188 -0
  1349. package/src/training/objectives/ul_stage2_base.d.ts +16 -0
  1350. package/src/training/objectives/ul_stage2_base.js +218 -0
  1351. package/src/training/operator-artifacts.d.ts +62 -0
  1352. package/src/training/operator-artifacts.js +140 -0
  1353. package/src/training/operator-command.d.ts +5 -0
  1354. package/src/training/operator-command.js +455 -0
  1355. package/src/training/operator-eval.d.ts +48 -0
  1356. package/src/training/operator-eval.js +230 -0
  1357. package/src/training/operator-scoreboard.d.ts +5 -0
  1358. package/src/training/operator-scoreboard.js +44 -0
  1359. package/src/training/optimizer.d.ts +22 -0
  1360. package/src/training/optimizer.js +127 -0
  1361. package/src/training/runner.d.ts +248 -0
  1362. package/src/training/runner.js +1236 -0
  1363. package/src/training/suite.d.ts +299 -0
  1364. package/src/training/suite.js +2196 -0
  1365. package/src/training/tensor-factory.d.ts +9 -0
  1366. package/src/training/tensor-factory.js +13 -0
  1367. package/src/training/trainer.d.ts +89 -0
  1368. package/src/training/trainer.js +299 -0
  1369. package/src/training/ul_dataset.d.ts +47 -0
  1370. package/src/training/ul_dataset.js +151 -0
  1371. package/src/training/ul_schedule.d.ts +6 -0
  1372. package/src/training/ul_schedule.js +29 -0
  1373. package/src/training/workload-packs/distill-translategemma-medium.json +103 -0
  1374. package/src/training/workload-packs/distill-translategemma-tiny.json +103 -0
  1375. package/src/training/workload-packs/lora-toy-medium.json +91 -0
  1376. package/src/training/workload-packs/lora-toy-tiny.json +91 -0
  1377. package/src/training/workload-packs/registry.json +98 -0
  1378. package/src/training/workload-packs/ul-training-medium.json +55 -0
  1379. package/src/training/workload-packs/ul-training-tiny.json +55 -0
  1380. package/src/training/workloads.d.ts +164 -0
  1381. package/src/training/workloads.js +530 -0
  1382. package/src/types/chrome.d.ts +36 -0
  1383. package/src/types/chrome.js +1 -0
  1384. package/src/types/gpu.d.ts +185 -0
  1385. package/src/types/gpu.js +5 -0
  1386. package/src/types/index.d.ts +4 -0
  1387. package/src/types/index.js +4 -0
  1388. package/src/types/inference.d.ts +197 -0
  1389. package/src/types/inference.js +5 -0
  1390. package/src/types/model.d.ts +130 -0
  1391. package/src/types/model.js +5 -0
  1392. package/src/utils/format-bytes.js +20 -0
  1393. package/src/utils/hf-resolve-url.d.ts +16 -0
  1394. package/src/utils/hf-resolve-url.js +17 -0
  1395. package/src/utils/index.d.ts +7 -0
  1396. package/src/utils/index.js +7 -0
  1397. package/src/utils/load-json.d.ts +5 -0
  1398. package/src/utils/load-json.js +33 -0
  1399. package/src/utils/plain-object.d.ts +1 -0
  1400. package/src/utils/plain-object.js +3 -0
  1401. package/src/utils/sha256.d.ts +4 -0
  1402. package/src/utils/sha256.js +135 -0
  1403. package/src/version.d.ts +2 -0
  1404. package/src/version.js +2 -0
  1405. package/tools/convert-safetensors-node.js +240 -0
@@ -0,0 +1,2186 @@
1
+ import { log } from '../debug/index.js';
2
+ import { getExpectedShardHash } from '../formats/rdrr/index.js';
3
+ import {
4
+ computeHash,
5
+ createStreamingHasher,
6
+ createShardWriter,
7
+ deleteShard,
8
+ getShardStoredSize,
9
+ loadShard as loadShardFromStore,
10
+ shardExists,
11
+ streamShardRange,
12
+ } from '../storage/shard-manager.js';
13
+ import { ERROR_CODES, createDopplerError } from '../errors/doppler-error.js';
14
+ import { DEFAULT_DISTRIBUTION_CONFIG } from '../config/schema/distribution.schema.js';
15
+ import {
16
+ P2P_TRANSPORT_CONTRACT_VERSION,
17
+ P2P_TRANSPORT_ERROR_CODES,
18
+ assertSupportedP2PTransportContract,
19
+ createP2PTransportError,
20
+ normalizeP2PTransportError,
21
+ normalizeP2PTransportResult,
22
+ isP2PTransportRetryable,
23
+ } from './p2p-transport-contract.js';
24
+ import {
25
+ normalizeP2PControlPlaneConfig,
26
+ resolveP2PSessionToken,
27
+ evaluateP2PPolicyDecision,
28
+ } from './p2p-control-plane.js';
29
+ import { createBrowserWebRTCDataPlaneTransport } from './p2p-webrtc-browser.js';
30
+
31
+ const DISTRIBUTION_SOURCE_CACHE = 'cache';
32
+ const DISTRIBUTION_SOURCE_P2P = 'p2p';
33
+ const DISTRIBUTION_SOURCE_HTTP = 'http';
34
+ const DISTRIBUTION_DECISION_TRACE_SCHEMA_VERSION = 1;
35
+ const DISTRIBUTION_DELIVERY_METRICS_SCHEMA_VERSION = 1;
36
+ const DISTRIBUTION_DELIVERY_METRICS_EVENT_SCHEMA_VERSION = 1;
37
+
38
+ const DISTRIBUTION_SOURCES = Object.freeze(
39
+ [...DEFAULT_DISTRIBUTION_CONFIG.sourceOrder]
40
+ );
41
+ const DEFAULT_SOURCE_MATRIX = Object.freeze({
42
+ cache: { ...DEFAULT_DISTRIBUTION_CONFIG.sourceMatrix.cache },
43
+ p2p: { ...DEFAULT_DISTRIBUTION_CONFIG.sourceMatrix.p2p },
44
+ http: { ...DEFAULT_DISTRIBUTION_CONFIG.sourceMatrix.http },
45
+ });
46
+
47
+ const DEFAULT_P2P_TIMEOUT_MS = DEFAULT_DISTRIBUTION_CONFIG.p2p.timeoutMs;
48
+ const DEFAULT_P2P_MAX_RETRIES = DEFAULT_DISTRIBUTION_CONFIG.p2p.maxRetries;
49
+ const DEFAULT_P2P_RETRY_DELAY_MS = DEFAULT_DISTRIBUTION_CONFIG.p2p.retryDelayMs;
50
+ const DEFAULT_P2P_RATE_LIMIT_PER_MINUTE = DEFAULT_DISTRIBUTION_CONFIG.p2p.abuse.rateLimitPerMinute;
51
+ const DEFAULT_P2P_MAX_CONSECUTIVE_FAILURES = DEFAULT_DISTRIBUTION_CONFIG.p2p.abuse.maxConsecutiveFailures;
52
+ const DEFAULT_P2P_QUARANTINE_MS = DEFAULT_DISTRIBUTION_CONFIG.p2p.abuse.quarantineMs;
53
+ const DEFAULT_P2P_CONTROL_PLANE_TOKEN_REFRESH_SKEW_MS = DEFAULT_DISTRIBUTION_CONFIG.p2p.controlPlane.tokenRefreshSkewMs;
54
+
55
+ const inFlightDeliveries = new Map();
56
+ const p2pTransportPolicyState = new WeakMap();
57
+
58
+ function normalizeDistributionSourceOrder(rawSources = []) {
59
+ if (rawSources === undefined || rawSources === null) {
60
+ return [...DISTRIBUTION_SOURCES];
61
+ }
62
+ if (!Array.isArray(rawSources)) {
63
+ throw new Error('distribution.sourceOrder must be an array when provided.');
64
+ }
65
+
66
+ const normalized = [];
67
+ const seen = new Set();
68
+
69
+ for (const value of rawSources) {
70
+ const source = String(value || '').trim().toLowerCase();
71
+ if (!DISTRIBUTION_SOURCES.includes(source)) {
72
+ throw new Error(`distribution.sourceOrder contains unsupported source "${source || value}".`);
73
+ }
74
+ if (seen.has(source)) continue;
75
+ seen.add(source);
76
+ normalized.push(source);
77
+ }
78
+
79
+ if (normalized.length === 0) {
80
+ throw new Error('distribution.sourceOrder must include at least one supported source.');
81
+ }
82
+ return normalized;
83
+ }
84
+
85
+ function normalizeInteger(value, fallback, allowZero = false) {
86
+ const parsed = Number(value);
87
+ const min = allowZero ? 0 : 1;
88
+ return Number.isFinite(parsed) && parsed >= min && Number.isInteger(parsed)
89
+ ? parsed
90
+ : fallback;
91
+ }
92
+
93
+ function normalizeRequiredInteger(value, label, { allowZero = false, fallback = null } = {}) {
94
+ if (value === undefined || value === null) {
95
+ if (fallback !== null) {
96
+ return fallback;
97
+ }
98
+ throw new Error(`${label} is required.`);
99
+ }
100
+ const parsed = Number(value);
101
+ const min = allowZero ? 0 : 1;
102
+ if (!Number.isInteger(parsed) || parsed < min) {
103
+ throw new Error(
104
+ `${label} must be a ${allowZero ? 'non-negative' : 'positive'} integer when provided.`
105
+ );
106
+ }
107
+ return parsed;
108
+ }
109
+
110
+ function normalizeContentEncodings(value) {
111
+ if (!value) return [];
112
+ return value
113
+ .split(',')
114
+ .map((entry) => entry.trim().toLowerCase())
115
+ .filter(Boolean);
116
+ }
117
+
118
+ function normalizeManifestVersionSet(value) {
119
+ if (value === undefined || value === null) return null;
120
+ const normalized = String(value).trim();
121
+ return normalized || null;
122
+ }
123
+
124
+ function normalizeSamplingRate(value, fallback = 1, label = 'distribution.sourceDecision.trace.samplingRate') {
125
+ if (value === undefined || value === null) {
126
+ return fallback;
127
+ }
128
+ const parsed = Number(value);
129
+ if (!Number.isFinite(parsed)) {
130
+ throw new Error(`${label} must be a finite number between 0 and 1 when provided.`);
131
+ }
132
+ if (parsed < 0 || parsed > 1) {
133
+ throw new Error(`${label} must be between 0 and 1 when provided.`);
134
+ }
135
+ return parsed;
136
+ }
137
+
138
+ function normalizeOptionalToken(value) {
139
+ if (value === undefined || value === null) {
140
+ return null;
141
+ }
142
+ const normalized = String(value).trim();
143
+ return normalized || null;
144
+ }
145
+
146
+ function normalizeOptionalTimestamp(value) {
147
+ if (value === undefined || value === null) {
148
+ return null;
149
+ }
150
+ const parsed = Number(value);
151
+ if (!Number.isFinite(parsed) || parsed <= 0) {
152
+ return null;
153
+ }
154
+ return Math.floor(parsed);
155
+ }
156
+
157
+ function hashStringToUnitInterval(value) {
158
+ let hash = 2166136261;
159
+ for (let i = 0; i < value.length; i += 1) {
160
+ hash ^= value.charCodeAt(i);
161
+ hash = Math.imul(hash, 16777619);
162
+ }
163
+ return (hash >>> 0) / 4294967295;
164
+ }
165
+
166
+ function shouldEmitDecisionTrace(config, shardIndex, expectedManifestVersionSet, sourceOrder) {
167
+ if (config.enabled !== true) {
168
+ return false;
169
+ }
170
+ const samplingRate = normalizeSamplingRate(config.samplingRate, 1);
171
+ if (samplingRate >= 1) {
172
+ return true;
173
+ }
174
+ if (samplingRate <= 0) {
175
+ return false;
176
+ }
177
+ if (config.deterministic !== false) {
178
+ const seed = [
179
+ String(shardIndex),
180
+ normalizeManifestVersionSet(expectedManifestVersionSet) ?? '',
181
+ Array.isArray(sourceOrder) ? sourceOrder.join(',') : '',
182
+ ].join('|');
183
+ return hashStringToUnitInterval(seed) < samplingRate;
184
+ }
185
+ return Math.random() < samplingRate;
186
+ }
187
+
188
+ function createShardSizeMismatchError(message, details = {}) {
189
+ const error = createDopplerError(
190
+ ERROR_CODES.DISTRIBUTION_SHARD_SIZE_MISMATCH,
191
+ message
192
+ );
193
+ Object.assign(error, details);
194
+ return error;
195
+ }
196
+
197
+ function parseContentLengthHeader(response, shardIndex) {
198
+ const raw = response?.headers?.get?.('content-length');
199
+ if (raw == null || raw === '') return null;
200
+ const parsed = Number(raw);
201
+ if (!Number.isInteger(parsed) || parsed < 0) {
202
+ throw createShardSizeMismatchError(
203
+ `Invalid content-length header for shard ${shardIndex}: ${raw}`,
204
+ {
205
+ code: 'http_content_length_invalid',
206
+ headerValue: raw,
207
+ }
208
+ );
209
+ }
210
+ return parsed;
211
+ }
212
+
213
+ function parseContentRangeHeader(response, shardIndex) {
214
+ const raw = response?.headers?.get?.('content-range');
215
+ if (raw == null || raw.trim() === '') return null;
216
+ const match = /^bytes\s+(\d+)-(\d+)\/(\d+|\*)$/iu.exec(raw.trim());
217
+ if (!match) {
218
+ throw createShardSizeMismatchError(
219
+ `Invalid content-range header for shard ${shardIndex}: ${raw}`,
220
+ {
221
+ code: 'http_content_range_invalid',
222
+ headerValue: raw,
223
+ }
224
+ );
225
+ }
226
+ const start = Number(match[1]);
227
+ const end = Number(match[2]);
228
+ const total = match[3] === '*' ? null : Number(match[3]);
229
+ if (!Number.isInteger(start) || !Number.isInteger(end) || end < start) {
230
+ throw createShardSizeMismatchError(
231
+ `Invalid content-range byte span for shard ${shardIndex}: ${raw}`,
232
+ {
233
+ code: 'http_content_range_invalid_span',
234
+ headerValue: raw,
235
+ }
236
+ );
237
+ }
238
+ if (total != null && (!Number.isInteger(total) || total <= 0 || total <= end)) {
239
+ throw createShardSizeMismatchError(
240
+ `Invalid content-range total size for shard ${shardIndex}: ${raw}`,
241
+ {
242
+ code: 'http_content_range_invalid_total',
243
+ headerValue: raw,
244
+ }
245
+ );
246
+ }
247
+ return {
248
+ start,
249
+ end,
250
+ total,
251
+ length: end - start + 1,
252
+ };
253
+ }
254
+
255
+ function assertHttpResponseBoundaryHeaders(response, shardIndex, contentLength, contentRange) {
256
+ if (response.status === 206 && !contentRange) {
257
+ throw createShardSizeMismatchError(
258
+ `Shard ${shardIndex} returned HTTP 206 without content-range header.`,
259
+ {
260
+ code: 'http_content_range_missing',
261
+ }
262
+ );
263
+ }
264
+ if (contentRange && response.status !== 206) {
265
+ throw createShardSizeMismatchError(
266
+ `Shard ${shardIndex} returned content-range header with unexpected HTTP ${response.status}.`,
267
+ {
268
+ code: 'http_content_range_unexpected_status',
269
+ status: response.status,
270
+ }
271
+ );
272
+ }
273
+ if (
274
+ contentLength != null
275
+ && contentRange
276
+ && contentLength !== contentRange.length
277
+ ) {
278
+ throw createShardSizeMismatchError(
279
+ `Shard ${shardIndex} content-length/content-range mismatch: content-length=${contentLength}, range-length=${contentRange.length}.`,
280
+ {
281
+ code: 'http_header_length_mismatch',
282
+ contentLength,
283
+ contentRangeLength: contentRange.length,
284
+ }
285
+ );
286
+ }
287
+ }
288
+
289
+ function assertHttpResumeAlignment(
290
+ response,
291
+ shardIndex,
292
+ resumeOffset,
293
+ contentRange
294
+ ) {
295
+ if (!Number.isInteger(resumeOffset) || resumeOffset <= 0) {
296
+ return { resetState: false };
297
+ }
298
+ if (response.status === 200) {
299
+ return { resetState: true };
300
+ }
301
+ if (response.status !== 206 || !contentRange) {
302
+ throw createShardSizeMismatchError(
303
+ `Shard ${shardIndex} resume response mismatch: expected HTTP 206 with content-range for offset ${resumeOffset}, got HTTP ${response.status}.`,
304
+ {
305
+ code: 'http_resume_response_mismatch',
306
+ status: response.status,
307
+ resumeOffset,
308
+ }
309
+ );
310
+ }
311
+ if (contentRange.start !== resumeOffset) {
312
+ throw createShardSizeMismatchError(
313
+ `Shard ${shardIndex} resume content-range start mismatch: expected ${resumeOffset}, got ${contentRange.start}.`,
314
+ {
315
+ code: 'http_resume_offset_mismatch',
316
+ resumeOffset,
317
+ contentRangeStart: contentRange.start,
318
+ }
319
+ );
320
+ }
321
+ return { resetState: false };
322
+ }
323
+
324
+ function assertHttpPayloadBoundary(shardIndex, bytesReceived, contentLength, contentRange, expectedSize) {
325
+ if (contentLength != null && bytesReceived !== contentLength) {
326
+ throw createShardSizeMismatchError(
327
+ `Shard ${shardIndex} content-length mismatch: expected ${contentLength}, received ${bytesReceived}.`,
328
+ {
329
+ code: 'http_content_length_mismatch',
330
+ contentLength,
331
+ bytesReceived,
332
+ }
333
+ );
334
+ }
335
+ if (contentRange && bytesReceived !== contentRange.length) {
336
+ throw createShardSizeMismatchError(
337
+ `Shard ${shardIndex} content-range mismatch: expected ${contentRange.length} bytes, received ${bytesReceived}.`,
338
+ {
339
+ code: 'http_content_range_length_mismatch',
340
+ contentRangeLength: contentRange.length,
341
+ bytesReceived,
342
+ }
343
+ );
344
+ }
345
+ if (contentRange?.total != null && Number.isFinite(expectedSize)) {
346
+ const normalizedExpectedSize = Math.floor(expectedSize);
347
+ if (normalizedExpectedSize >= 0 && contentRange.total !== normalizedExpectedSize) {
348
+ throw createShardSizeMismatchError(
349
+ `Shard ${shardIndex} content-range total mismatch: expected ${normalizedExpectedSize}, got ${contentRange.total}.`,
350
+ {
351
+ code: 'http_content_range_total_mismatch',
352
+ expectedSize: normalizedExpectedSize,
353
+ contentRangeTotal: contentRange.total,
354
+ }
355
+ );
356
+ }
357
+ }
358
+ }
359
+
360
+ function assertP2PPayloadRangeStart(
361
+ shardIndex,
362
+ rangeStart,
363
+ expectedStart
364
+ ) {
365
+ if (rangeStart == null) {
366
+ return;
367
+ }
368
+ if (!Number.isInteger(rangeStart) || rangeStart < 0) {
369
+ throw createShardSizeMismatchError(
370
+ `Shard ${shardIndex} p2p payload rangeStart must be a non-negative integer.`,
371
+ {
372
+ code: 'p2p_range_start_invalid',
373
+ rangeStart,
374
+ }
375
+ );
376
+ }
377
+ if (rangeStart !== expectedStart) {
378
+ throw createShardSizeMismatchError(
379
+ `Shard ${shardIndex} p2p resume range mismatch: expected start ${expectedStart}, got ${rangeStart}.`,
380
+ {
381
+ code: 'p2p_resume_offset_mismatch',
382
+ expectedStart,
383
+ rangeStart,
384
+ }
385
+ );
386
+ }
387
+ }
388
+
389
+ function assertP2PTotalSize(shardIndex, totalSize, expectedSize) {
390
+ if (totalSize == null || !Number.isFinite(expectedSize)) {
391
+ return;
392
+ }
393
+ const normalizedExpectedSize = Math.floor(expectedSize);
394
+ if (totalSize !== normalizedExpectedSize) {
395
+ throw createShardSizeMismatchError(
396
+ `Shard ${shardIndex} p2p totalSize mismatch: expected ${normalizedExpectedSize}, got ${totalSize}.`,
397
+ {
398
+ code: 'p2p_total_size_mismatch',
399
+ expectedSize: normalizedExpectedSize,
400
+ totalSize,
401
+ }
402
+ );
403
+ }
404
+ }
405
+
406
+ function assertP2PPayloadBoundary(
407
+ shardIndex,
408
+ rangeStart,
409
+ payloadBytes,
410
+ totalSize,
411
+ writeToStore
412
+ ) {
413
+ if (totalSize == null) {
414
+ return;
415
+ }
416
+ if (!Number.isInteger(rangeStart) || rangeStart < 0) {
417
+ throw createShardSizeMismatchError(
418
+ `Shard ${shardIndex} p2p payload rangeStart must be a non-negative integer for boundary checks.`,
419
+ {
420
+ code: 'p2p_payload_range_start_invalid',
421
+ rangeStart,
422
+ }
423
+ );
424
+ }
425
+ if (!Number.isInteger(payloadBytes) || payloadBytes < 0) {
426
+ throw createShardSizeMismatchError(
427
+ `Shard ${shardIndex} p2p payload size must be a non-negative integer for boundary checks.`,
428
+ {
429
+ code: 'p2p_payload_size_invalid',
430
+ payloadBytes,
431
+ }
432
+ );
433
+ }
434
+ if (rangeStart + payloadBytes > totalSize) {
435
+ throw createShardSizeMismatchError(
436
+ `Shard ${shardIndex} p2p payload exceeds total size: start=${rangeStart}, bytes=${payloadBytes}, total=${totalSize}.`,
437
+ {
438
+ code: 'p2p_payload_exceeds_total',
439
+ rangeStart,
440
+ payloadBytes,
441
+ totalSize,
442
+ }
443
+ );
444
+ }
445
+ if (!writeToStore && rangeStart === 0 && payloadBytes !== totalSize) {
446
+ throw createShardSizeMismatchError(
447
+ `Shard ${shardIndex} p2p payload size mismatch: expected ${totalSize}, got ${payloadBytes}.`,
448
+ {
449
+ code: 'p2p_payload_size_mismatch',
450
+ payloadBytes,
451
+ totalSize,
452
+ }
453
+ );
454
+ }
455
+ }
456
+
457
+ function assertRequiredContentEncoding(response, requiredEncoding, context) {
458
+ if (!requiredEncoding) return;
459
+ const required = requiredEncoding.trim().toLowerCase();
460
+ if (!required) return;
461
+ const found = normalizeContentEncodings(response.headers.get('content-encoding'));
462
+ if (!found.includes(required)) {
463
+ const foundValue = found.length > 0 ? found.join(', ') : 'none';
464
+ throw new Error(`Missing required content-encoding "${required}" for ${context} (found: ${foundValue})`);
465
+ }
466
+ }
467
+
468
+ function buildShardUrl(baseUrl, shardInfo) {
469
+ const base = String(baseUrl || '').replace(/\/$/, '');
470
+ const filename = String(shardInfo?.filename || '').replace(/^\/+/, '');
471
+ return `${base}/${filename}`;
472
+ }
473
+
474
+ function bytesToHex(bytes) {
475
+ return Array.from(bytes)
476
+ .map((byte) => byte.toString(16).padStart(2, '0'))
477
+ .join('');
478
+ }
479
+
480
+ function normalizeP2PConfig(config = {}) {
481
+ const enabled = config?.enabled === true;
482
+ const rawTimeoutMs = config?.timeoutMs;
483
+ const rawMaxRetries = config?.maxRetries;
484
+ const rawRetryDelayMs = config?.retryDelayMs;
485
+ const rawSecurity = config?.security && typeof config.security === 'object'
486
+ ? config.security
487
+ : {};
488
+ const rawAbuse = config?.abuse && typeof config.abuse === 'object'
489
+ ? config.abuse
490
+ : {};
491
+ const rawControlPlane = config?.controlPlane && typeof config.controlPlane === 'object'
492
+ ? config.controlPlane
493
+ : {};
494
+ const rawWebRTC = config?.webrtc && typeof config.webrtc === 'object'
495
+ ? config.webrtc
496
+ : {};
497
+
498
+ let transport = config?.transport;
499
+ if (typeof transport !== 'function' && rawWebRTC.enabled === true) {
500
+ transport = createBrowserWebRTCDataPlaneTransport(rawWebRTC);
501
+ }
502
+ if (typeof transport !== 'function') {
503
+ transport = null;
504
+ }
505
+
506
+ const contractVersion = assertSupportedP2PTransportContract(
507
+ config?.contractVersion ?? P2P_TRANSPORT_CONTRACT_VERSION
508
+ );
509
+
510
+ return {
511
+ enabled,
512
+ timeoutMs: normalizeRequiredInteger(
513
+ rawTimeoutMs,
514
+ 'distribution.p2p.timeoutMs',
515
+ { fallback: DEFAULT_P2P_TIMEOUT_MS }
516
+ ),
517
+ maxRetries: normalizeRequiredInteger(
518
+ rawMaxRetries,
519
+ 'distribution.p2p.maxRetries',
520
+ { allowZero: true, fallback: DEFAULT_P2P_MAX_RETRIES }
521
+ ),
522
+ retryDelayMs: normalizeRequiredInteger(
523
+ rawRetryDelayMs,
524
+ 'distribution.p2p.retryDelayMs',
525
+ { allowZero: true, fallback: DEFAULT_P2P_RETRY_DELAY_MS }
526
+ ),
527
+ transport,
528
+ contractVersion,
529
+ controlPlane: normalizeP2PControlPlaneConfig({
530
+ ...DEFAULT_DISTRIBUTION_CONFIG.p2p.controlPlane,
531
+ ...rawControlPlane,
532
+ tokenRefreshSkewMs: rawControlPlane.tokenRefreshSkewMs
533
+ ?? DEFAULT_P2P_CONTROL_PLANE_TOKEN_REFRESH_SKEW_MS,
534
+ }),
535
+ security: {
536
+ requireSessionToken: rawSecurity.requireSessionToken === true,
537
+ sessionToken: normalizeOptionalToken(rawSecurity.sessionToken),
538
+ tokenExpiresAtMs: normalizeOptionalTimestamp(rawSecurity.tokenExpiresAtMs),
539
+ },
540
+ abuse: {
541
+ rateLimitPerMinute: normalizeRequiredInteger(
542
+ rawAbuse.rateLimitPerMinute,
543
+ 'distribution.p2p.abuse.rateLimitPerMinute',
544
+ { allowZero: true, fallback: DEFAULT_P2P_RATE_LIMIT_PER_MINUTE }
545
+ ),
546
+ maxConsecutiveFailures: normalizeRequiredInteger(
547
+ rawAbuse.maxConsecutiveFailures,
548
+ 'distribution.p2p.abuse.maxConsecutiveFailures',
549
+ { fallback: DEFAULT_P2P_MAX_CONSECUTIVE_FAILURES }
550
+ ),
551
+ quarantineMs: normalizeRequiredInteger(
552
+ rawAbuse.quarantineMs,
553
+ 'distribution.p2p.abuse.quarantineMs',
554
+ { allowZero: true, fallback: DEFAULT_P2P_QUARANTINE_MS }
555
+ ),
556
+ },
557
+ };
558
+ }
559
+
560
+ function getP2PTransportPolicyState(transport) {
561
+ if (typeof transport !== 'function') {
562
+ return null;
563
+ }
564
+ let state = p2pTransportPolicyState.get(transport);
565
+ if (!state) {
566
+ state = {
567
+ requestTimestamps: [],
568
+ consecutiveFailures: 0,
569
+ quarantinedUntilMs: 0,
570
+ };
571
+ p2pTransportPolicyState.set(transport, state);
572
+ }
573
+ return state;
574
+ }
575
+
576
+ function createP2PPolicyDeniedError(message, details = {}) {
577
+ return createP2PTransportError(
578
+ P2P_TRANSPORT_ERROR_CODES.policyDenied,
579
+ message,
580
+ details,
581
+ false
582
+ );
583
+ }
584
+
585
+ function isSessionTokenExpiredOrExpiring(tokenExpiresAtMs, nowMs = Date.now(), skewMs = 0) {
586
+ if (!Number.isFinite(tokenExpiresAtMs)) {
587
+ return false;
588
+ }
589
+ const threshold = nowMs + Math.max(0, Math.floor(skewMs));
590
+ return threshold >= tokenExpiresAtMs;
591
+ }
592
+
593
+ function applyControlPlaneSessionUpdate(p2pConfig, sessionUpdate) {
594
+ if (!sessionUpdate || !p2pConfig?.security) {
595
+ return;
596
+ }
597
+ if (sessionUpdate.hasSessionToken === true) {
598
+ p2pConfig.security.sessionToken = normalizeOptionalToken(sessionUpdate.sessionToken);
599
+ }
600
+ if (sessionUpdate.hasTokenExpiresAtMs === true) {
601
+ p2pConfig.security.tokenExpiresAtMs = normalizeOptionalTimestamp(sessionUpdate.tokenExpiresAtMs);
602
+ }
603
+ }
604
+
605
+ async function refreshP2PSessionTokenFromControlPlane(p2pConfig, context, nowMs = Date.now()) {
606
+ const controlPlane = p2pConfig?.controlPlane;
607
+ if (!controlPlane?.enabled || typeof controlPlane.tokenProvider !== 'function') {
608
+ return;
609
+ }
610
+
611
+ const requiresSessionToken = p2pConfig?.security?.requireSessionToken === true;
612
+ const token = p2pConfig?.security?.sessionToken ?? null;
613
+ const tokenExpiresAtMs = p2pConfig?.security?.tokenExpiresAtMs ?? null;
614
+ let reason = null;
615
+
616
+ if (requiresSessionToken && !token) {
617
+ reason = 'missing';
618
+ } else if (isSessionTokenExpiredOrExpiring(tokenExpiresAtMs, nowMs, 0)) {
619
+ reason = 'expired';
620
+ } else if (
621
+ isSessionTokenExpiredOrExpiring(tokenExpiresAtMs, nowMs, controlPlane.tokenRefreshSkewMs)
622
+ ) {
623
+ reason = 'refresh';
624
+ }
625
+
626
+ if (!reason) {
627
+ return;
628
+ }
629
+
630
+ const sessionUpdate = await resolveP2PSessionToken(controlPlane, {
631
+ ...context,
632
+ reason,
633
+ nowMs,
634
+ currentSessionToken: token,
635
+ currentTokenExpiresAtMs: tokenExpiresAtMs,
636
+ });
637
+ applyControlPlaneSessionUpdate(p2pConfig, sessionUpdate);
638
+
639
+ if (requiresSessionToken && !p2pConfig.security.sessionToken) {
640
+ throw createP2PPolicyDeniedError(
641
+ `P2P shard ${context?.shardIndex} requires a session token from control plane.`,
642
+ {
643
+ shardIndex: context?.shardIndex ?? null,
644
+ policyReason: 'session_token_missing_after_refresh',
645
+ }
646
+ );
647
+ }
648
+ }
649
+
650
+ async function enforceP2PControlPlanePolicy(p2pConfig, context, nowMs = Date.now()) {
651
+ const controlPlane = p2pConfig?.controlPlane;
652
+ if (!controlPlane?.enabled || typeof controlPlane.policyEvaluator !== 'function') {
653
+ return;
654
+ }
655
+ const decision = await evaluateP2PPolicyDecision(controlPlane, {
656
+ ...context,
657
+ nowMs,
658
+ currentSessionToken: p2pConfig?.security?.sessionToken ?? null,
659
+ currentTokenExpiresAtMs: p2pConfig?.security?.tokenExpiresAtMs ?? null,
660
+ });
661
+ applyControlPlaneSessionUpdate(p2pConfig, decision.sessionUpdate);
662
+ if (decision.allow !== false) {
663
+ return;
664
+ }
665
+ throw createP2PPolicyDeniedError(
666
+ `P2P shard ${context?.shardIndex} denied by control-plane policy.`,
667
+ {
668
+ shardIndex: context?.shardIndex ?? null,
669
+ policyReason: decision.reason ?? 'policy_denied_control_plane',
670
+ controlPlaneMetadata: decision.metadata ?? null,
671
+ }
672
+ );
673
+ }
674
+
675
+ function enforceP2PSecurityAndAbusePolicy(p2pConfig, state, shardIndex, nowMs = Date.now()) {
676
+ const security = p2pConfig?.security ?? {};
677
+ const abuse = p2pConfig?.abuse ?? {};
678
+
679
+ if (security.requireSessionToken === true && !security.sessionToken) {
680
+ throw createP2PPolicyDeniedError(
681
+ `P2P shard ${shardIndex} requires a session token.`,
682
+ {
683
+ shardIndex,
684
+ policyReason: 'session_token_missing',
685
+ }
686
+ );
687
+ }
688
+ if (
689
+ Number.isFinite(security.tokenExpiresAtMs)
690
+ && nowMs >= security.tokenExpiresAtMs
691
+ ) {
692
+ throw createP2PPolicyDeniedError(
693
+ `P2P shard ${shardIndex} session token expired.`,
694
+ {
695
+ shardIndex,
696
+ policyReason: 'session_token_expired',
697
+ tokenExpiresAtMs: security.tokenExpiresAtMs,
698
+ }
699
+ );
700
+ }
701
+
702
+ if (!state) {
703
+ return;
704
+ }
705
+
706
+ if (Number.isFinite(state.quarantinedUntilMs) && nowMs < state.quarantinedUntilMs) {
707
+ throw createP2PPolicyDeniedError(
708
+ `P2P shard ${shardIndex} transport is quarantined.`,
709
+ {
710
+ shardIndex,
711
+ policyReason: 'transport_quarantined',
712
+ quarantinedUntilMs: state.quarantinedUntilMs,
713
+ }
714
+ );
715
+ }
716
+
717
+ const limit = Number.isFinite(abuse.rateLimitPerMinute)
718
+ ? Math.max(0, Math.floor(abuse.rateLimitPerMinute))
719
+ : 0;
720
+ if (limit > 0) {
721
+ const cutoff = nowMs - 60000;
722
+ state.requestTimestamps = state.requestTimestamps.filter((stamp) => stamp >= cutoff);
723
+ if (state.requestTimestamps.length >= limit) {
724
+ throw createP2PPolicyDeniedError(
725
+ `P2P shard ${shardIndex} transport rate limit exceeded.`,
726
+ {
727
+ shardIndex,
728
+ policyReason: 'rate_limited',
729
+ rateLimitPerMinute: limit,
730
+ }
731
+ );
732
+ }
733
+ state.requestTimestamps.push(nowMs);
734
+ }
735
+ }
736
+
737
+ function markP2PTransportSuccess(state) {
738
+ if (!state) {
739
+ return;
740
+ }
741
+ state.consecutiveFailures = 0;
742
+ state.quarantinedUntilMs = 0;
743
+ }
744
+
745
+ function markP2PTransportFailure(p2pConfig, state, normalizedError, nowMs = Date.now()) {
746
+ if (!state) {
747
+ return;
748
+ }
749
+ if (!normalizedError || normalizedError.code === P2P_TRANSPORT_ERROR_CODES.aborted) {
750
+ return;
751
+ }
752
+ const maxFailures = Number.isFinite(p2pConfig?.abuse?.maxConsecutiveFailures)
753
+ ? Math.max(1, Math.floor(p2pConfig.abuse.maxConsecutiveFailures))
754
+ : DEFAULT_P2P_MAX_CONSECUTIVE_FAILURES;
755
+ const quarantineMs = Number.isFinite(p2pConfig?.abuse?.quarantineMs)
756
+ ? Math.max(0, Math.floor(p2pConfig.abuse.quarantineMs))
757
+ : DEFAULT_P2P_QUARANTINE_MS;
758
+ state.consecutiveFailures += 1;
759
+ if (quarantineMs > 0 && state.consecutiveFailures >= maxFailures) {
760
+ state.quarantinedUntilMs = nowMs + quarantineMs;
761
+ }
762
+ }
763
+
764
+ function normalizeAntiRollbackConfig(config = {}) {
765
+ const antiRollback = config?.antiRollback && typeof config.antiRollback === 'object'
766
+ ? config.antiRollback
767
+ : {};
768
+ return {
769
+ enabled: antiRollback.enabled !== false,
770
+ requireExpectedHash: antiRollback.requireExpectedHash !== false,
771
+ requireExpectedSize: antiRollback.requireExpectedSize === true,
772
+ requireManifestVersionSet: antiRollback.requireManifestVersionSet !== false,
773
+ };
774
+ }
775
+
776
+ function normalizeDecisionTraceConfig(config = {}) {
777
+ const sourceDecision = config?.sourceDecision && typeof config.sourceDecision === 'object'
778
+ ? config.sourceDecision
779
+ : {};
780
+ const trace = sourceDecision.trace && typeof sourceDecision.trace === 'object'
781
+ ? sourceDecision.trace
782
+ : {};
783
+ return {
784
+ deterministic: sourceDecision.deterministic !== false,
785
+ enabled: trace.enabled === true,
786
+ includeSkippedSources: trace.includeSkippedSources !== false,
787
+ samplingRate: normalizeSamplingRate(trace.samplingRate, 1),
788
+ };
789
+ }
790
+
791
+ function normalizeSourceMatrix(config = {}) {
792
+ const matrix = config?.sourceMatrix && typeof config.sourceMatrix === 'object'
793
+ ? config.sourceMatrix
794
+ : {};
795
+ const defaultMatrix = DEFAULT_SOURCE_MATRIX;
796
+ const normalized = {};
797
+ for (const source of DISTRIBUTION_SOURCES) {
798
+ const entry = matrix[source] && typeof matrix[source] === 'object'
799
+ ? matrix[source]
800
+ : {};
801
+ normalized[source] = {
802
+ onHit: entry.onHit === 'return' ? 'return' : defaultMatrix[source].onHit,
803
+ onMiss: entry.onMiss === 'terminal' ? 'terminal' : 'next',
804
+ onFailure: entry.onFailure === 'terminal' ? 'terminal' : 'next',
805
+ };
806
+ }
807
+ return normalized;
808
+ }
809
+
810
+ function createDecisionTrace(order, plan, shardIndex, deterministic, expectedManifestVersionSet) {
811
+ return {
812
+ schemaVersion: DISTRIBUTION_DECISION_TRACE_SCHEMA_VERSION,
813
+ deterministic: deterministic === true,
814
+ shardIndex,
815
+ expectedManifestVersionSet: normalizeManifestVersionSet(expectedManifestVersionSet),
816
+ sourceOrder: [...order],
817
+ plan: plan.map((entry) => ({
818
+ source: entry.source,
819
+ enabled: entry.enabled,
820
+ reason: entry.reason,
821
+ })),
822
+ attempts: [],
823
+ };
824
+ }
825
+
826
+ function appendDecisionTraceAttempt(trace, entry) {
827
+ if (!trace) return;
828
+ trace.attempts.push({
829
+ source: entry.source,
830
+ status: entry.status,
831
+ reason: entry.reason ?? null,
832
+ code: entry.code ?? null,
833
+ message: entry.message ?? null,
834
+ durationMs: Number.isFinite(entry.durationMs) ? entry.durationMs : null,
835
+ bytes: Number.isFinite(entry.bytes) ? entry.bytes : null,
836
+ hash: typeof entry.hash === 'string' ? entry.hash : null,
837
+ path: typeof entry.path === 'string' ? entry.path : null,
838
+ manifestVersionSet: normalizeManifestVersionSet(entry.manifestVersionSet),
839
+ });
840
+ }
841
+
842
+ function attachDecisionTrace(result, trace) {
843
+ if (!trace) return result;
844
+ return {
845
+ ...result,
846
+ decisionTrace: trace,
847
+ };
848
+ }
849
+
850
+ function createSourceCounter() {
851
+ return {
852
+ cache: 0,
853
+ p2p: 0,
854
+ http: 0,
855
+ };
856
+ }
857
+
858
+ function createLatencySummary(durations) {
859
+ const values = durations.filter((value) => Number.isFinite(value));
860
+ if (values.length === 0) {
861
+ return {
862
+ count: 0,
863
+ min: null,
864
+ max: null,
865
+ avg: null,
866
+ };
867
+ }
868
+ let sum = 0;
869
+ let min = Number.POSITIVE_INFINITY;
870
+ let max = Number.NEGATIVE_INFINITY;
871
+ for (const value of values) {
872
+ sum += value;
873
+ if (value < min) min = value;
874
+ if (value > max) max = value;
875
+ }
876
+ return {
877
+ count: values.length,
878
+ min,
879
+ max,
880
+ avg: sum / values.length,
881
+ };
882
+ }
883
+
884
+ function appendAttemptLogAttempt(logEntries, entry) {
885
+ if (!Array.isArray(logEntries)) return;
886
+ logEntries.push({
887
+ source: entry.source,
888
+ status: entry.status,
889
+ code: entry.code ?? null,
890
+ durationMs: Number.isFinite(entry.durationMs) ? entry.durationMs : null,
891
+ writeDurationMs: Number.isFinite(entry.writeDurationMs) ? entry.writeDurationMs : null,
892
+ });
893
+ }
894
+
895
+ function createDeliveryMetrics(order, result, attempts, totalDurationMs) {
896
+ const sourceAttempts = createSourceCounter();
897
+ const retries = createSourceCounter();
898
+ const failureCodes = {};
899
+ const p2pDurations = [];
900
+ const httpDurations = [];
901
+ let storageWriteMs = Number.isFinite(result?.writeDurationMs) ? result.writeDurationMs : null;
902
+ let attemptCount = 0;
903
+ const attemptsBySource = createSourceCounter();
904
+
905
+ for (const attempt of attempts) {
906
+ if (attempt?.status !== 'success' && attempt?.status !== 'failed') {
907
+ continue;
908
+ }
909
+ attemptCount += 1;
910
+ const source = attempt?.source;
911
+ if (source === DISTRIBUTION_SOURCE_CACHE || source === DISTRIBUTION_SOURCE_P2P || source === DISTRIBUTION_SOURCE_HTTP) {
912
+ sourceAttempts[source] += 1;
913
+ attemptsBySource[source] += 1;
914
+ if (source === DISTRIBUTION_SOURCE_P2P && Number.isFinite(attempt.durationMs)) {
915
+ p2pDurations.push(attempt.durationMs);
916
+ }
917
+ if (source === DISTRIBUTION_SOURCE_HTTP && Number.isFinite(attempt.durationMs)) {
918
+ httpDurations.push(attempt.durationMs);
919
+ }
920
+ }
921
+ if (attempt.status === 'failed') {
922
+ const code = typeof attempt.code === 'string' && attempt.code
923
+ ? attempt.code
924
+ : 'unknown';
925
+ failureCodes[code] = (failureCodes[code] ?? 0) + 1;
926
+ }
927
+ if (storageWriteMs == null && Number.isFinite(attempt.writeDurationMs)) {
928
+ storageWriteMs = attempt.writeDurationMs;
929
+ }
930
+ }
931
+
932
+ for (const source of [DISTRIBUTION_SOURCE_CACHE, DISTRIBUTION_SOURCE_P2P, DISTRIBUTION_SOURCE_HTTP]) {
933
+ retries[source] = Math.max(0, attemptsBySource[source] - 1);
934
+ }
935
+
936
+ return {
937
+ schemaVersion: DISTRIBUTION_DELIVERY_METRICS_SCHEMA_VERSION,
938
+ totalDurationMs: Number.isFinite(totalDurationMs) ? totalDurationMs : 0,
939
+ sourceOrder: Array.isArray(order) ? [...order] : [...DISTRIBUTION_SOURCES],
940
+ successSource: result?.source ?? null,
941
+ attemptCount,
942
+ sourceAttempts,
943
+ retries,
944
+ failureCodes,
945
+ p2pRttMs: createLatencySummary(p2pDurations),
946
+ httpRttMs: createLatencySummary(httpDurations),
947
+ storageWriteMs,
948
+ };
949
+ }
950
+
951
+ async function emitDeliveryMetricsHook(hook, payload) {
952
+ if (typeof hook !== 'function') {
953
+ return;
954
+ }
955
+ try {
956
+ await hook(payload);
957
+ } catch (error) {
958
+ log.warn(
959
+ 'Distribution',
960
+ `delivery metrics hook failed: ${error?.message || String(error)}`
961
+ );
962
+ }
963
+ }
964
+
965
+ function assertExpectedHash(resultHash, expectedHash, shardIndex) {
966
+ if (!expectedHash) return;
967
+ if (!resultHash) {
968
+ const error = createDopplerError(
969
+ ERROR_CODES.DISTRIBUTION_SHARD_HASH_MISMATCH,
970
+ `Shard ${shardIndex} missing hash result`
971
+ );
972
+ error.code = 'hash_missing';
973
+ throw error;
974
+ }
975
+ if (resultHash !== expectedHash) {
976
+ const error = createDopplerError(
977
+ ERROR_CODES.DISTRIBUTION_SHARD_HASH_MISMATCH,
978
+ `Hash mismatch for shard ${shardIndex}: expected ${expectedHash}, got ${resultHash}`
979
+ );
980
+ error.code = 'hash_mismatch';
981
+ error.expectedHash = expectedHash;
982
+ error.actualHash = resultHash;
983
+ throw error;
984
+ }
985
+ }
986
+
987
+ function assertExpectedSize(bytes, expectedSize, shardIndex) {
988
+ if (!Number.isFinite(expectedSize)) return;
989
+ const expected = Math.floor(expectedSize);
990
+ const actual = Number.isFinite(bytes) ? Math.floor(bytes) : -1;
991
+ if (expected < 0 || actual < 0) return;
992
+ if (actual !== expected) {
993
+ const error = createDopplerError(
994
+ ERROR_CODES.DISTRIBUTION_SHARD_SIZE_MISMATCH,
995
+ `Size mismatch for shard ${shardIndex}: expected ${expected}, got ${actual}`
996
+ );
997
+ error.code = 'size_mismatch';
998
+ error.expectedSize = expected;
999
+ error.actualSize = actual;
1000
+ throw error;
1001
+ }
1002
+ }
1003
+
1004
+ function assertExpectedManifestVersionSet(resultVersionSet, expectedVersionSet, shardIndex, source) {
1005
+ const expected = normalizeManifestVersionSet(expectedVersionSet);
1006
+ if (!expected) return;
1007
+ const actual = normalizeManifestVersionSet(resultVersionSet);
1008
+ if (!actual) {
1009
+ const error = createDopplerError(
1010
+ ERROR_CODES.DISTRIBUTION_SHARD_MANIFEST_VERSION_SET_MISMATCH,
1011
+ `Shard ${shardIndex} source "${source}" missing manifestVersionSet while antiRollback.requireManifestVersionSet=true.`
1012
+ );
1013
+ error.code = 'manifest_version_set_missing';
1014
+ error.expectedManifestVersionSet = expected;
1015
+ error.actualManifestVersionSet = actual;
1016
+ throw error;
1017
+ }
1018
+ if (actual !== expected) {
1019
+ const error = createDopplerError(
1020
+ ERROR_CODES.DISTRIBUTION_SHARD_MANIFEST_VERSION_SET_MISMATCH,
1021
+ `Shard ${shardIndex} source "${source}" manifestVersionSet mismatch: expected ${expected}, got ${actual}`
1022
+ );
1023
+ error.code = 'manifest_version_set_mismatch';
1024
+ error.expectedManifestVersionSet = expected;
1025
+ error.actualManifestVersionSet = actual;
1026
+ throw error;
1027
+ }
1028
+ }
1029
+
1030
+ function parseDownloadOptions(options = {}) {
1031
+ return {
1032
+ algorithm: options.algorithm,
1033
+ onProgress: options.onProgress ?? null,
1034
+ onDeliveryMetrics: options.onDeliveryMetrics ?? null,
1035
+ signal: options.signal,
1036
+ requiredEncoding: options.requiredEncoding ?? null,
1037
+ writeToStore: options.writeToStore ?? false,
1038
+ expectedHash: options.expectedHash ?? null,
1039
+ expectedSize: Number.isFinite(options.expectedSize) ? Math.floor(options.expectedSize) : null,
1040
+ expectedManifestVersionSet: normalizeManifestVersionSet(options.expectedManifestVersionSet),
1041
+ maxRetries: options.maxRetries,
1042
+ initialRetryDelayMs: options.initialRetryDelayMs,
1043
+ maxRetryDelayMs: options.maxRetryDelayMs,
1044
+ };
1045
+ }
1046
+
1047
+ function createDeliveryKey(baseUrl, shardIndex, options, order, sourceMatrix) {
1048
+ return [
1049
+ String(baseUrl || ''),
1050
+ String(shardIndex),
1051
+ String(options.algorithm || ''),
1052
+ String(options.expectedHash || ''),
1053
+ String(options.expectedSize ?? ''),
1054
+ String(options.expectedManifestVersionSet ?? ''),
1055
+ JSON.stringify(sourceMatrix || null),
1056
+ String(options.writeToStore === true),
1057
+ order.join(','),
1058
+ ].join('|');
1059
+ }
1060
+
1061
+ function createAbortError(label = 'operation aborted') {
1062
+ const error = new Error(label);
1063
+ error.name = 'AbortError';
1064
+ return error;
1065
+ }
1066
+
1067
+ function awaitWithSignal(promise, signal, label) {
1068
+ if (!signal) return promise;
1069
+ if (signal.aborted) {
1070
+ return Promise.reject(createAbortError(label));
1071
+ }
1072
+ return new Promise((resolve, reject) => {
1073
+ const onAbort = () => reject(createAbortError(label));
1074
+ signal.addEventListener('abort', onAbort, { once: true });
1075
+ promise.then(
1076
+ (value) => {
1077
+ signal.removeEventListener('abort', onAbort);
1078
+ resolve(value);
1079
+ },
1080
+ (error) => {
1081
+ signal.removeEventListener('abort', onAbort);
1082
+ reject(error);
1083
+ }
1084
+ );
1085
+ });
1086
+ }
1087
+
1088
+ async function withTimeout(promise, timeoutMs, label = 'operation') {
1089
+ if (!timeoutMs || timeoutMs <= 0) {
1090
+ return promise;
1091
+ }
1092
+
1093
+ let timer;
1094
+ const timeout = new Promise((_, reject) => {
1095
+ timer = setTimeout(() => {
1096
+ const error = new Error(`${label} timed out after ${timeoutMs}ms`);
1097
+ error.name = 'TimeoutError';
1098
+ reject(error);
1099
+ }, timeoutMs);
1100
+ });
1101
+
1102
+ try {
1103
+ return await Promise.race([promise, timeout]);
1104
+ } finally {
1105
+ clearTimeout(timer);
1106
+ }
1107
+ }
1108
+
1109
+ export function resolveShardDeliveryPlan(options = {}) {
1110
+ const order = normalizeDistributionSourceOrder(options.sourceOrder);
1111
+ const plan = [];
1112
+ for (const source of order) {
1113
+ if (source === DISTRIBUTION_SOURCE_CACHE) {
1114
+ const enabled = options.enableSourceCache !== false;
1115
+ plan.push({
1116
+ source,
1117
+ enabled,
1118
+ reason: enabled ? 'enabled' : 'cache_disabled',
1119
+ });
1120
+ continue;
1121
+ }
1122
+ if (source === DISTRIBUTION_SOURCE_P2P) {
1123
+ const enabled = options.p2pEnabled === true && options.p2pTransportAvailable === true;
1124
+ let reason = 'enabled';
1125
+ if (options.p2pEnabled !== true) {
1126
+ reason = 'p2p_disabled';
1127
+ } else if (options.p2pTransportAvailable !== true) {
1128
+ reason = 'p2p_transport_unconfigured';
1129
+ }
1130
+ plan.push({ source, enabled, reason });
1131
+ continue;
1132
+ }
1133
+ if (source === DISTRIBUTION_SOURCE_HTTP) {
1134
+ const enabled = options.httpEnabled !== false;
1135
+ plan.push({
1136
+ source,
1137
+ enabled,
1138
+ reason: enabled ? 'enabled' : 'http_disabled',
1139
+ });
1140
+ continue;
1141
+ }
1142
+ }
1143
+ return { order, plan };
1144
+ }
1145
+
1146
+ async function seedHasherFromStoredPrefix(hasher, shardIndex, expectedPrefixBytes) {
1147
+ if (!Number.isInteger(expectedPrefixBytes) || expectedPrefixBytes <= 0) {
1148
+ return;
1149
+ }
1150
+ let hashedBytes = 0;
1151
+ for await (const chunk of streamShardRange(shardIndex, 0, expectedPrefixBytes)) {
1152
+ if (!chunk?.byteLength) continue;
1153
+ const remaining = expectedPrefixBytes - hashedBytes;
1154
+ if (remaining <= 0) break;
1155
+ const next = chunk.byteLength > remaining
1156
+ ? chunk.subarray(0, remaining)
1157
+ : chunk;
1158
+ hasher.update(next);
1159
+ hashedBytes += next.byteLength;
1160
+ if (hashedBytes >= expectedPrefixBytes) break;
1161
+ }
1162
+ if (hashedBytes !== expectedPrefixBytes) {
1163
+ throw createShardSizeMismatchError(
1164
+ `Shard ${shardIndex} stored resume prefix mismatch: expected ${expectedPrefixBytes} bytes, read ${hashedBytes}.`,
1165
+ {
1166
+ code: 'resume_state_prefix_mismatch',
1167
+ expectedPrefixBytes,
1168
+ actualPrefixBytes: hashedBytes,
1169
+ }
1170
+ );
1171
+ }
1172
+ }
1173
+
1174
+ async function resolvePersistedResumeOffset(writeToStore, shardIndex, expectedSize) {
1175
+ if (!writeToStore) return 0;
1176
+ const storedSize = await getShardStoredSize(shardIndex);
1177
+ const resumeOffset = Number.isFinite(storedSize)
1178
+ ? Math.max(0, Math.floor(storedSize))
1179
+ : 0;
1180
+ if (resumeOffset <= 0) return 0;
1181
+ if (Number.isFinite(expectedSize)) {
1182
+ const normalizedExpected = Math.max(0, Math.floor(expectedSize));
1183
+ if (resumeOffset > normalizedExpected) {
1184
+ throw createShardSizeMismatchError(
1185
+ `Shard ${shardIndex} stored resume bytes exceed expected size: stored=${resumeOffset}, expected=${normalizedExpected}.`,
1186
+ {
1187
+ code: 'resume_state_oversize',
1188
+ storedBytes: resumeOffset,
1189
+ expectedSize: normalizedExpected,
1190
+ }
1191
+ );
1192
+ }
1193
+ if (resumeOffset === normalizedExpected) {
1194
+ return 0;
1195
+ }
1196
+ }
1197
+ return resumeOffset;
1198
+ }
1199
+
1200
+ async function createHttpTransferState(writeToStore, shardIndex, algorithm, resumeOffset = 0) {
1201
+ const normalizedResumeOffset = Number.isInteger(resumeOffset) && resumeOffset > 0
1202
+ ? resumeOffset
1203
+ : 0;
1204
+ const hasher = await createStreamingHasher(algorithm);
1205
+ if (normalizedResumeOffset > 0) {
1206
+ await seedHasherFromStoredPrefix(hasher, shardIndex, normalizedResumeOffset);
1207
+ }
1208
+ return {
1209
+ hasher,
1210
+ chunks: writeToStore ? null : [],
1211
+ writer: writeToStore
1212
+ ? await createShardWriter(shardIndex, {
1213
+ append: normalizedResumeOffset > 0,
1214
+ expectedOffset: normalizedResumeOffset,
1215
+ })
1216
+ : null,
1217
+ writerClosed: false,
1218
+ receivedBytes: normalizedResumeOffset,
1219
+ writeDurationMs: 0,
1220
+ };
1221
+ }
1222
+
1223
+ async function resetHttpTransferState(state, writeToStore, shardIndex, algorithm) {
1224
+ await state.writer?.abort?.();
1225
+ state.hasher = await createStreamingHasher(algorithm);
1226
+ state.chunks = writeToStore ? null : [];
1227
+ state.writer = writeToStore ? await createShardWriter(shardIndex) : null;
1228
+ state.writerClosed = false;
1229
+ state.receivedBytes = 0;
1230
+ state.writeDurationMs = 0;
1231
+ }
1232
+
1233
+ async function appendHttpTransferChunk(state, chunk) {
1234
+ const bytes = chunk instanceof Uint8Array ? chunk : new Uint8Array(chunk);
1235
+ state.hasher.update(bytes);
1236
+ if (state.writer) {
1237
+ const writeStart = performance.now();
1238
+ await state.writer.write(bytes);
1239
+ state.writeDurationMs += performance.now() - writeStart;
1240
+ } else if (state.chunks) {
1241
+ state.chunks.push(bytes.slice(0));
1242
+ }
1243
+ state.receivedBytes += bytes.byteLength;
1244
+ }
1245
+
1246
+ async function finalizeHttpTransferState(state, startTime, shardIndex) {
1247
+ const hashBytes = await state.hasher.finalize();
1248
+ const hash = bytesToHex(hashBytes);
1249
+ if (state.writer) {
1250
+ const closeStart = performance.now();
1251
+ await state.writer.close();
1252
+ state.writerClosed = true;
1253
+ state.writeDurationMs += performance.now() - closeStart;
1254
+ const elapsed = (performance.now() - startTime) / 1000;
1255
+ const speed = elapsed > 0 ? state.receivedBytes / elapsed : 0;
1256
+ const speedDisplay = `${(speed / (1024 * 1024)).toFixed(2)}MB/s`;
1257
+ log.verbose(
1258
+ 'Distribution',
1259
+ `Shard ${shardIndex}: http stream (${state.receivedBytes} bytes, ${elapsed.toFixed(2)}s, ${speedDisplay})`
1260
+ );
1261
+ return {
1262
+ buffer: null,
1263
+ bytes: state.receivedBytes,
1264
+ hash,
1265
+ wrote: true,
1266
+ source: DISTRIBUTION_SOURCE_HTTP,
1267
+ path: 'http-stream-store',
1268
+ writeDurationMs: state.writeDurationMs,
1269
+ };
1270
+ }
1271
+
1272
+ const buffer = !state.chunks || state.chunks.length === 0
1273
+ ? new ArrayBuffer(0)
1274
+ : await new Blob(state.chunks).arrayBuffer();
1275
+ return {
1276
+ buffer,
1277
+ bytes: buffer.byteLength,
1278
+ hash,
1279
+ wrote: false,
1280
+ source: DISTRIBUTION_SOURCE_HTTP,
1281
+ path: 'http-stream-buffer',
1282
+ writeDurationMs: null,
1283
+ };
1284
+ }
1285
+
1286
+ async function abortHttpTransferState(state) {
1287
+ if (state.writer && !state.writerClosed) {
1288
+ await state.writer.abort?.();
1289
+ state.writerClosed = true;
1290
+ }
1291
+ }
1292
+
1293
+ async function persistHttpTransferState(state) {
1294
+ if (!state.writer || state.writerClosed) {
1295
+ return;
1296
+ }
1297
+ if (state.receivedBytes > 0) {
1298
+ const closeStart = performance.now();
1299
+ await state.writer.close();
1300
+ state.writerClosed = true;
1301
+ state.writeDurationMs += performance.now() - closeStart;
1302
+ return;
1303
+ }
1304
+ await state.writer.abort?.();
1305
+ state.writerClosed = true;
1306
+ }
1307
+
1308
+ async function clearPersistedShardState(shardIndex) {
1309
+ const deleted = await deleteShard(shardIndex);
1310
+ if (deleted) {
1311
+ return;
1312
+ }
1313
+ const writer = await createShardWriter(shardIndex, {
1314
+ append: false,
1315
+ expectedOffset: 0,
1316
+ });
1317
+ await writer.abort?.();
1318
+ }
1319
+
1320
+ async function recoverHttpRejectedResumeRange(
1321
+ baseUrl,
1322
+ shardInfo,
1323
+ shardIndex,
1324
+ options,
1325
+ transferState,
1326
+ writeToStore
1327
+ ) {
1328
+ await abortHttpTransferState(transferState);
1329
+ if (writeToStore) {
1330
+ await clearPersistedShardState(shardIndex);
1331
+ }
1332
+ return downloadShardFromHttp(baseUrl, shardInfo, shardIndex, {
1333
+ ...options,
1334
+ __disablePersistedResume: true,
1335
+ __resumeRangeRecoveryAttempted: true,
1336
+ });
1337
+ }
1338
+
1339
+ async function downloadShardFromHttp(baseUrl, shardInfo, shardIndex, options = {}) {
1340
+ const {
1341
+ signal,
1342
+ algorithm,
1343
+ onProgress,
1344
+ requiredEncoding,
1345
+ writeToStore = false,
1346
+ } = options;
1347
+
1348
+ if (!algorithm) {
1349
+ throw new Error('Missing hash algorithm for shard download.');
1350
+ }
1351
+
1352
+ const startTime = performance.now();
1353
+ const url = buildShardUrl(baseUrl, shardInfo);
1354
+ let lastError;
1355
+ const maxRetries = normalizeRequiredInteger(
1356
+ options.maxRetries,
1357
+ 'download.maxRetries',
1358
+ { allowZero: true, fallback: 3 }
1359
+ );
1360
+ const initialRetryDelayMs = normalizeRequiredInteger(
1361
+ options.initialRetryDelayMs,
1362
+ 'download.initialRetryDelayMs',
1363
+ { allowZero: true, fallback: 1000 }
1364
+ );
1365
+ const maxRetryDelayMs = normalizeRequiredInteger(
1366
+ options.maxRetryDelayMs,
1367
+ 'download.maxRetryDelayMs',
1368
+ { allowZero: true, fallback: 30000 }
1369
+ );
1370
+ const progressTotalBytes = Number.isFinite(options.expectedSize)
1371
+ ? Math.floor(options.expectedSize)
1372
+ : (Number.isFinite(shardInfo?.size) ? Math.floor(shardInfo.size) : 0);
1373
+ let retryDelay = initialRetryDelayMs;
1374
+ const disablePersistedResume = options.__disablePersistedResume === true;
1375
+ let resumeOffset = 0;
1376
+ if (!disablePersistedResume) {
1377
+ try {
1378
+ resumeOffset = await resolvePersistedResumeOffset(
1379
+ writeToStore,
1380
+ shardIndex,
1381
+ options.expectedSize
1382
+ );
1383
+ } catch (error) {
1384
+ if (writeToStore && error?.code === 'resume_state_oversize') {
1385
+ await clearPersistedShardState(shardIndex);
1386
+ resumeOffset = 0;
1387
+ } else {
1388
+ throw error;
1389
+ }
1390
+ }
1391
+ }
1392
+ const startedWithResume = resumeOffset > 0;
1393
+ let transferState;
1394
+ try {
1395
+ transferState = await createHttpTransferState(
1396
+ writeToStore,
1397
+ shardIndex,
1398
+ algorithm,
1399
+ resumeOffset
1400
+ );
1401
+ } catch (error) {
1402
+ if (writeToStore && error?.code === 'resume_state_prefix_mismatch') {
1403
+ await clearPersistedShardState(shardIndex);
1404
+ resumeOffset = 0;
1405
+ transferState = await createHttpTransferState(
1406
+ writeToStore,
1407
+ shardIndex,
1408
+ algorithm,
1409
+ 0
1410
+ );
1411
+ } else {
1412
+ throw error;
1413
+ }
1414
+ }
1415
+
1416
+ for (let attempt = 0; attempt <= maxRetries; attempt += 1) {
1417
+ try {
1418
+ const resumeOffset = transferState.receivedBytes;
1419
+ const requestHeaders = resumeOffset > 0
1420
+ ? { range: `bytes=${resumeOffset}-` }
1421
+ : undefined;
1422
+ const response = await fetch(url, { signal, headers: requestHeaders });
1423
+ if (!response.ok) {
1424
+ const error = new Error(`HTTP ${response.status}: ${response.statusText}`);
1425
+ error.status = response.status;
1426
+ throw error;
1427
+ }
1428
+
1429
+ assertRequiredContentEncoding(response, requiredEncoding, `shard ${shardIndex}`);
1430
+ const contentLength = parseContentLengthHeader(response, shardIndex);
1431
+ const contentRange = parseContentRangeHeader(response, shardIndex);
1432
+ assertHttpResponseBoundaryHeaders(response, shardIndex, contentLength, contentRange);
1433
+ const { resetState } = assertHttpResumeAlignment(
1434
+ response,
1435
+ shardIndex,
1436
+ resumeOffset,
1437
+ contentRange
1438
+ );
1439
+ if (resetState) {
1440
+ await resetHttpTransferState(transferState, writeToStore, shardIndex, algorithm);
1441
+ }
1442
+
1443
+ if (!response.body) {
1444
+ const buffer = await response.arrayBuffer();
1445
+ assertHttpPayloadBoundary(
1446
+ shardIndex,
1447
+ buffer.byteLength,
1448
+ contentLength,
1449
+ contentRange,
1450
+ options.expectedSize
1451
+ );
1452
+ await appendHttpTransferChunk(transferState, new Uint8Array(buffer));
1453
+ const total = progressTotalBytes > 0 ? progressTotalBytes : transferState.receivedBytes;
1454
+ const percent = total > 0
1455
+ ? Math.min(100, Math.floor((transferState.receivedBytes / total) * 100))
1456
+ : 100;
1457
+ onProgress?.({
1458
+ shardIndex,
1459
+ receivedBytes: transferState.receivedBytes,
1460
+ totalBytes: total,
1461
+ percent,
1462
+ });
1463
+
1464
+ const finalized = await finalizeHttpTransferState(transferState, startTime, shardIndex);
1465
+ const result = {
1466
+ ...finalized,
1467
+ path: finalized.wrote ? finalized.path : 'http-blob',
1468
+ manifestVersionSet: options.expectedManifestVersionSet ?? null,
1469
+ };
1470
+ if (
1471
+ writeToStore
1472
+ && startedWithResume
1473
+ && options.__resumeRecoveryAttempted !== true
1474
+ && options.expectedHash
1475
+ && result.hash !== options.expectedHash
1476
+ ) {
1477
+ await clearPersistedShardState(shardIndex);
1478
+ return downloadShardFromHttp(baseUrl, shardInfo, shardIndex, {
1479
+ ...options,
1480
+ __disablePersistedResume: true,
1481
+ __resumeRecoveryAttempted: true,
1482
+ });
1483
+ }
1484
+ return result;
1485
+ }
1486
+
1487
+ const reader = response.body.getReader();
1488
+ let attemptBytes = 0;
1489
+
1490
+ try {
1491
+ while (true) {
1492
+ const { done, value } = await reader.read();
1493
+ if (done) break;
1494
+
1495
+ if (value?.length) {
1496
+ await appendHttpTransferChunk(transferState, value);
1497
+ attemptBytes += value.length;
1498
+ }
1499
+
1500
+ const total = progressTotalBytes > 0 ? progressTotalBytes : transferState.receivedBytes;
1501
+ onProgress?.({
1502
+ shardIndex,
1503
+ receivedBytes: transferState.receivedBytes,
1504
+ totalBytes: total,
1505
+ percent: total > 0 ? (transferState.receivedBytes / total) * 100 : 0,
1506
+ });
1507
+ }
1508
+
1509
+ assertHttpPayloadBoundary(
1510
+ shardIndex,
1511
+ attemptBytes,
1512
+ contentLength,
1513
+ contentRange,
1514
+ options.expectedSize
1515
+ );
1516
+ const finalized = await finalizeHttpTransferState(transferState, startTime, shardIndex);
1517
+ const result = {
1518
+ ...finalized,
1519
+ manifestVersionSet: options.expectedManifestVersionSet ?? null,
1520
+ };
1521
+ if (
1522
+ writeToStore
1523
+ && startedWithResume
1524
+ && options.__resumeRecoveryAttempted !== true
1525
+ && options.expectedHash
1526
+ && result.hash !== options.expectedHash
1527
+ ) {
1528
+ await clearPersistedShardState(shardIndex);
1529
+ return downloadShardFromHttp(baseUrl, shardInfo, shardIndex, {
1530
+ ...options,
1531
+ __disablePersistedResume: true,
1532
+ __resumeRecoveryAttempted: true,
1533
+ });
1534
+ }
1535
+ return result;
1536
+ } catch (error) {
1537
+ throw error;
1538
+ }
1539
+ } catch (error) {
1540
+ lastError = error;
1541
+
1542
+ if (error?.name === 'AbortError') {
1543
+ if (writeToStore) {
1544
+ await persistHttpTransferState(transferState);
1545
+ } else {
1546
+ await abortHttpTransferState(transferState);
1547
+ }
1548
+ throw error;
1549
+ }
1550
+
1551
+ if (
1552
+ error?.status === 416
1553
+ && transferState.receivedBytes > 0
1554
+ && options.__resumeRangeRecoveryAttempted !== true
1555
+ ) {
1556
+ return recoverHttpRejectedResumeRange(
1557
+ baseUrl,
1558
+ shardInfo,
1559
+ shardIndex,
1560
+ options,
1561
+ transferState,
1562
+ writeToStore
1563
+ );
1564
+ }
1565
+
1566
+ if (Number.isInteger(error?.status) && error.status >= 400 && error.status < 500 && error.status !== 429) {
1567
+ await abortHttpTransferState(transferState);
1568
+ throw error;
1569
+ }
1570
+ if (typeof error?.code === 'string' && error.code.startsWith('http_')) {
1571
+ await abortHttpTransferState(transferState);
1572
+ throw error;
1573
+ }
1574
+
1575
+ if (attempt < maxRetries) {
1576
+ await new Promise((resolve) => setTimeout(resolve, retryDelay));
1577
+ retryDelay = Math.min(retryDelay * 2, maxRetryDelayMs);
1578
+ continue;
1579
+ }
1580
+
1581
+ if (writeToStore) {
1582
+ await persistHttpTransferState(transferState);
1583
+ } else {
1584
+ await abortHttpTransferState(transferState);
1585
+ }
1586
+ }
1587
+ }
1588
+
1589
+ if (writeToStore) {
1590
+ await persistHttpTransferState(transferState);
1591
+ } else {
1592
+ await abortHttpTransferState(transferState);
1593
+ }
1594
+ throw lastError;
1595
+ }
1596
+
1597
+ async function downloadShardFromP2P(shardIndex, shardInfo, p2pConfig, options = {}) {
1598
+ const transport = p2pConfig.transport;
1599
+ if (!p2pConfig.enabled || typeof transport !== 'function') {
1600
+ throw createP2PTransportError(
1601
+ P2P_TRANSPORT_ERROR_CODES.unconfigured,
1602
+ 'P2P transport is not configured',
1603
+ { shardIndex }
1604
+ );
1605
+ }
1606
+ const transportState = getP2PTransportPolicyState(transport);
1607
+
1608
+ const writeToStore = options.writeToStore === true;
1609
+ const algorithm = options.algorithm;
1610
+ if (writeToStore && !algorithm) {
1611
+ throw new Error(`Missing hash algorithm for shard ${shardIndex} p2p transfer.`);
1612
+ }
1613
+
1614
+ const expectedSize = Number.isFinite(options.expectedSize)
1615
+ ? Math.floor(options.expectedSize)
1616
+ : null;
1617
+ const disablePersistedResume = options.__disablePersistedResume === true;
1618
+ let seededResumeOffset = 0;
1619
+ let transferState = null;
1620
+ if (writeToStore) {
1621
+ if (!disablePersistedResume) {
1622
+ try {
1623
+ seededResumeOffset = await resolvePersistedResumeOffset(
1624
+ true,
1625
+ shardIndex,
1626
+ expectedSize
1627
+ );
1628
+ } catch (error) {
1629
+ if (error?.code === 'resume_state_oversize') {
1630
+ await clearPersistedShardState(shardIndex);
1631
+ seededResumeOffset = 0;
1632
+ } else {
1633
+ throw error;
1634
+ }
1635
+ }
1636
+ }
1637
+ try {
1638
+ transferState = await createHttpTransferState(
1639
+ true,
1640
+ shardIndex,
1641
+ algorithm,
1642
+ seededResumeOffset
1643
+ );
1644
+ } catch (error) {
1645
+ if (error?.code === 'resume_state_prefix_mismatch') {
1646
+ await clearPersistedShardState(shardIndex);
1647
+ seededResumeOffset = 0;
1648
+ transferState = await createHttpTransferState(true, shardIndex, algorithm, 0);
1649
+ } else {
1650
+ throw error;
1651
+ }
1652
+ }
1653
+ }
1654
+ const startedWithResume = writeToStore && seededResumeOffset > 0;
1655
+
1656
+ const startTime = performance.now();
1657
+ let lastError = null;
1658
+ const maxRetries = Math.max(0, p2pConfig.maxRetries);
1659
+ for (let attempt = 0; attempt <= maxRetries; attempt += 1) {
1660
+ try {
1661
+ const requestResumeOffset = transferState?.receivedBytes ?? 0;
1662
+ const nowMs = Date.now();
1663
+ const attemptContext = {
1664
+ shardIndex,
1665
+ attempt,
1666
+ maxRetries,
1667
+ resumeOffset: requestResumeOffset,
1668
+ expectedHash: options.expectedHash ?? null,
1669
+ expectedSize: options.expectedSize ?? null,
1670
+ expectedManifestVersionSet: options.expectedManifestVersionSet ?? null,
1671
+ };
1672
+ await refreshP2PSessionTokenFromControlPlane(
1673
+ p2pConfig,
1674
+ attemptContext,
1675
+ nowMs
1676
+ );
1677
+ await enforceP2PControlPlanePolicy(
1678
+ p2pConfig,
1679
+ attemptContext,
1680
+ nowMs
1681
+ );
1682
+ enforceP2PSecurityAndAbusePolicy(
1683
+ p2pConfig,
1684
+ transportState,
1685
+ shardIndex,
1686
+ nowMs
1687
+ );
1688
+ const transportResult = await withTimeout(
1689
+ transport({
1690
+ shardIndex,
1691
+ shardInfo,
1692
+ signal: options.signal,
1693
+ source: DISTRIBUTION_SOURCE_P2P,
1694
+ timeoutMs: p2pConfig.timeoutMs,
1695
+ contractVersion: p2pConfig.contractVersion,
1696
+ attempt,
1697
+ maxRetries,
1698
+ resumeOffset: requestResumeOffset,
1699
+ expectedHash: options.expectedHash ?? null,
1700
+ expectedSize: options.expectedSize ?? null,
1701
+ expectedManifestVersionSet: options.expectedManifestVersionSet ?? null,
1702
+ }),
1703
+ p2pConfig.timeoutMs,
1704
+ `P2P shard ${shardIndex}`
1705
+ );
1706
+ const payload = normalizeP2PTransportResult(
1707
+ transportResult,
1708
+ `P2P transport result for shard ${shardIndex}`
1709
+ );
1710
+ if (!payload) {
1711
+ throw createP2PTransportError(
1712
+ P2P_TRANSPORT_ERROR_CODES.payloadInvalid,
1713
+ `P2P transport returned empty payload for shard ${shardIndex}`,
1714
+ { shardIndex }
1715
+ );
1716
+ }
1717
+
1718
+ const payloadRangeStart = payload.rangeStart;
1719
+ const payloadTotalSize = payload.totalSize;
1720
+ assertP2PTotalSize(shardIndex, payloadTotalSize, expectedSize);
1721
+
1722
+ const onProgress = options.onProgress ?? null;
1723
+ const transferResult = await (async () => {
1724
+ if (!writeToStore) {
1725
+ assertP2PPayloadRangeStart(shardIndex, payloadRangeStart, 0);
1726
+ assertP2PPayloadBoundary(
1727
+ shardIndex,
1728
+ 0,
1729
+ payload.data.byteLength,
1730
+ payloadTotalSize,
1731
+ false
1732
+ );
1733
+ onProgress?.({
1734
+ shardIndex,
1735
+ receivedBytes: payload.data.byteLength,
1736
+ totalBytes: expectedSize ?? payloadTotalSize ?? payload.data.byteLength,
1737
+ percent: 100,
1738
+ });
1739
+ return {
1740
+ buffer: payload.data,
1741
+ bytes: payload.data.byteLength,
1742
+ source: DISTRIBUTION_SOURCE_P2P,
1743
+ path: 'p2p-transport',
1744
+ wrote: false,
1745
+ writeDurationMs: null,
1746
+ };
1747
+ }
1748
+
1749
+ let effectiveRangeStart = payloadRangeStart;
1750
+ if (effectiveRangeStart == null) {
1751
+ effectiveRangeStart = requestResumeOffset;
1752
+ }
1753
+ if (requestResumeOffset > 0 && effectiveRangeStart === 0) {
1754
+ await resetHttpTransferState(transferState, true, shardIndex, algorithm);
1755
+ } else {
1756
+ assertP2PPayloadRangeStart(
1757
+ shardIndex,
1758
+ effectiveRangeStart,
1759
+ transferState.receivedBytes
1760
+ );
1761
+ }
1762
+ assertP2PPayloadBoundary(
1763
+ shardIndex,
1764
+ effectiveRangeStart,
1765
+ payload.data.byteLength,
1766
+ payloadTotalSize,
1767
+ true
1768
+ );
1769
+ await appendHttpTransferChunk(transferState, new Uint8Array(payload.data));
1770
+ onProgress?.({
1771
+ shardIndex,
1772
+ receivedBytes: transferState.receivedBytes,
1773
+ totalBytes: expectedSize ?? payloadTotalSize ?? transferState.receivedBytes,
1774
+ percent: 100,
1775
+ });
1776
+ const finalized = await finalizeHttpTransferState(transferState, startTime, shardIndex);
1777
+ if (Number.isFinite(expectedSize)) {
1778
+ assertExpectedSize(finalized.bytes, expectedSize, shardIndex);
1779
+ } else if (Number.isInteger(payloadTotalSize)) {
1780
+ assertExpectedSize(finalized.bytes, payloadTotalSize, shardIndex);
1781
+ }
1782
+ return {
1783
+ ...finalized,
1784
+ source: DISTRIBUTION_SOURCE_P2P,
1785
+ path: 'p2p-stream-store',
1786
+ };
1787
+ })();
1788
+ const result = {
1789
+ ...transferResult,
1790
+ manifestVersionSet: normalizeManifestVersionSet(
1791
+ payload.manifestVersionSet ?? options.expectedManifestVersionSet
1792
+ ),
1793
+ };
1794
+ if (!result.hash && result.buffer instanceof ArrayBuffer) {
1795
+ result.hash = await computeHash(result.buffer, options.algorithm);
1796
+ }
1797
+ if (writeToStore) {
1798
+ try {
1799
+ assertExpectedManifestVersionSet(
1800
+ result.manifestVersionSet,
1801
+ options.expectedManifestVersionSet,
1802
+ shardIndex,
1803
+ DISTRIBUTION_SOURCE_P2P
1804
+ );
1805
+ if (Number.isFinite(expectedSize)) {
1806
+ assertExpectedSize(result.bytes, expectedSize, shardIndex);
1807
+ }
1808
+ if (options.expectedHash) {
1809
+ assertExpectedHash(result.hash, options.expectedHash, shardIndex);
1810
+ }
1811
+ } catch (verificationError) {
1812
+ await clearPersistedShardState(shardIndex);
1813
+ if (
1814
+ startedWithResume
1815
+ && options.__resumeRecoveryAttempted !== true
1816
+ && options.expectedHash
1817
+ && verificationError?.code === 'hash_mismatch'
1818
+ ) {
1819
+ return downloadShardFromP2P(shardIndex, shardInfo, p2pConfig, {
1820
+ ...options,
1821
+ __disablePersistedResume: true,
1822
+ __resumeRecoveryAttempted: true,
1823
+ });
1824
+ }
1825
+ throw verificationError;
1826
+ }
1827
+ }
1828
+ markP2PTransportSuccess(transportState);
1829
+ return result;
1830
+ } catch (error) {
1831
+ if (typeof error?.code === 'string' && error.code.startsWith('p2p_')) {
1832
+ if (writeToStore) {
1833
+ await clearPersistedShardState(shardIndex);
1834
+ }
1835
+ throw error;
1836
+ }
1837
+
1838
+ const normalized = normalizeP2PTransportError(error, {
1839
+ shardIndex,
1840
+ attempt,
1841
+ maxRetries,
1842
+ label: `P2P shard ${shardIndex}`,
1843
+ });
1844
+ lastError = normalized;
1845
+ markP2PTransportFailure(
1846
+ p2pConfig,
1847
+ transportState,
1848
+ normalized,
1849
+ Date.now()
1850
+ );
1851
+ if (normalized?.code === P2P_TRANSPORT_ERROR_CODES.aborted) {
1852
+ if (writeToStore) {
1853
+ await persistHttpTransferState(transferState);
1854
+ }
1855
+ const abortError = createAbortError(normalized.message || 'P2P transport aborted');
1856
+ throw abortError;
1857
+ }
1858
+ if (attempt < maxRetries && isP2PTransportRetryable(normalized)) {
1859
+ await new Promise((resolve) => setTimeout(resolve, p2pConfig.retryDelayMs));
1860
+ continue;
1861
+ }
1862
+ if (writeToStore) {
1863
+ await persistHttpTransferState(transferState);
1864
+ }
1865
+ throw normalized;
1866
+ }
1867
+ }
1868
+
1869
+ if (writeToStore) {
1870
+ await persistHttpTransferState(transferState);
1871
+ }
1872
+ throw lastError;
1873
+ }
1874
+
1875
+ async function executeDeliveryPlan(
1876
+ baseUrl,
1877
+ shardIndex,
1878
+ shardInfo,
1879
+ plan,
1880
+ p2p,
1881
+ options,
1882
+ trace,
1883
+ decisionTraceConfig,
1884
+ sourceMatrix,
1885
+ attemptLog
1886
+ ) {
1887
+ let lastError = null;
1888
+ const enabledSources = plan.filter((entry) => entry.enabled);
1889
+
1890
+ for (const step of plan) {
1891
+ if (!step.enabled) {
1892
+ if (decisionTraceConfig.includeSkippedSources === true) {
1893
+ appendDecisionTraceAttempt(trace, {
1894
+ source: step.source,
1895
+ status: 'skipped',
1896
+ reason: step.reason,
1897
+ });
1898
+ }
1899
+ appendAttemptLogAttempt(attemptLog, {
1900
+ source: step.source,
1901
+ status: 'skipped',
1902
+ });
1903
+ continue;
1904
+ }
1905
+
1906
+ const attemptStart = performance.now();
1907
+ try {
1908
+ let result = null;
1909
+ if (step.source === DISTRIBUTION_SOURCE_CACHE) {
1910
+ if (!(await shardExists(shardIndex))) {
1911
+ const cacheMiss = new Error(`Shard ${shardIndex} missing from local cache`);
1912
+ cacheMiss.code = 'cache_miss';
1913
+ throw cacheMiss;
1914
+ }
1915
+ const buffer = await loadShardFromStore(shardIndex, { verify: false });
1916
+ result = {
1917
+ buffer,
1918
+ bytes: buffer.byteLength,
1919
+ hash: await computeHash(buffer, options.algorithm),
1920
+ wrote: false,
1921
+ source: DISTRIBUTION_SOURCE_CACHE,
1922
+ path: 'cache',
1923
+ manifestVersionSet: options.expectedManifestVersionSet ?? null,
1924
+ writeDurationMs: null,
1925
+ };
1926
+ } else if (step.source === DISTRIBUTION_SOURCE_P2P) {
1927
+ result = await downloadShardFromP2P(shardIndex, shardInfo, p2p, options);
1928
+ if (!result.hash) {
1929
+ if (!(result.buffer instanceof ArrayBuffer)) {
1930
+ throw new Error(`Shard ${shardIndex} p2p result missing hash and buffer.`);
1931
+ }
1932
+ result.hash = await computeHash(result.buffer, options.algorithm);
1933
+ }
1934
+ } else if (step.source === DISTRIBUTION_SOURCE_HTTP) {
1935
+ result = await downloadShardFromHttp(baseUrl, shardInfo, shardIndex, { ...options });
1936
+ }
1937
+
1938
+ assertExpectedManifestVersionSet(
1939
+ result.manifestVersionSet,
1940
+ options.expectedManifestVersionSet,
1941
+ shardIndex,
1942
+ step.source
1943
+ );
1944
+ assertExpectedHash(result.hash, options.expectedHash, shardIndex);
1945
+ assertExpectedSize(result.bytes, options.expectedSize, shardIndex);
1946
+
1947
+ appendDecisionTraceAttempt(trace, {
1948
+ source: step.source,
1949
+ status: 'success',
1950
+ durationMs: performance.now() - attemptStart,
1951
+ bytes: result.bytes,
1952
+ hash: result.hash,
1953
+ path: result.path,
1954
+ manifestVersionSet: result.manifestVersionSet,
1955
+ });
1956
+ appendAttemptLogAttempt(attemptLog, {
1957
+ source: step.source,
1958
+ status: 'success',
1959
+ durationMs: performance.now() - attemptStart,
1960
+ writeDurationMs: result.writeDurationMs,
1961
+ });
1962
+ return result;
1963
+ } catch (error) {
1964
+ if (error?.name === 'AbortError') {
1965
+ throw error;
1966
+ }
1967
+ lastError = error;
1968
+ appendDecisionTraceAttempt(trace, {
1969
+ source: step.source,
1970
+ status: 'failed',
1971
+ reason: step.reason,
1972
+ code: error?.code || null,
1973
+ message: error?.message || String(error),
1974
+ durationMs: performance.now() - attemptStart,
1975
+ });
1976
+ appendAttemptLogAttempt(attemptLog, {
1977
+ source: step.source,
1978
+ status: 'failed',
1979
+ code: error?.code || null,
1980
+ durationMs: performance.now() - attemptStart,
1981
+ });
1982
+ const enabledIndex = enabledSources.findIndex((entry) => entry.source === step.source);
1983
+ const isLastEnabled = enabledIndex === enabledSources.length - 1;
1984
+ const transitionType = (
1985
+ error?.code === 'cache_miss'
1986
+ || error?.code === 'p2p_unconfigured'
1987
+ || error?.code === P2P_TRANSPORT_ERROR_CODES.unconfigured
1988
+ || error?.code === P2P_TRANSPORT_ERROR_CODES.unavailable
1989
+ )
1990
+ ? 'onMiss'
1991
+ : 'onFailure';
1992
+ const transition = sourceMatrix?.[step.source]?.[transitionType] || 'next';
1993
+ if (isLastEnabled || transition === 'terminal') {
1994
+ log.warn('Distribution', `All shard delivery sources failed for shard ${shardIndex}: ${error.message}`);
1995
+ throw error;
1996
+ }
1997
+ log.debug('Distribution', `Shard ${shardIndex} source "${step.source}" failed (${error.code || 'error'}): ${error.message}`);
1998
+ continue;
1999
+ }
2000
+ }
2001
+
2002
+ throw lastError || new Error(`No shard delivery source available for shard ${shardIndex}`);
2003
+ }
2004
+
2005
+ export async function downloadShard(
2006
+ baseUrl,
2007
+ shardIndex,
2008
+ shardInfo,
2009
+ options = {}
2010
+ ) {
2011
+ const {
2012
+ sourceOrder,
2013
+ distributionConfig = {},
2014
+ distribution = {},
2015
+ maxRetries,
2016
+ initialRetryDelayMs,
2017
+ maxRetryDelayMs,
2018
+ requiredEncoding,
2019
+ algorithm,
2020
+ signal,
2021
+ onProgress = null,
2022
+ onDeliveryMetrics = null,
2023
+ writeToStore = false,
2024
+ enableSourceCache = true,
2025
+ p2pTransport,
2026
+ expectedSize,
2027
+ } = options;
2028
+
2029
+ if (!algorithm) {
2030
+ throw new Error('Missing hash algorithm for shard download verification.');
2031
+ }
2032
+
2033
+ const activeConfig = {
2034
+ ...(distributionConfig || {}),
2035
+ ...distribution,
2036
+ sourceOrder: sourceOrder || distributionConfig?.sourceOrder || distributionConfig?.sources,
2037
+ };
2038
+
2039
+ const antiRollback = normalizeAntiRollbackConfig(activeConfig);
2040
+ const decisionTraceConfig = normalizeDecisionTraceConfig(activeConfig);
2041
+ const sourceMatrix = normalizeSourceMatrix(activeConfig);
2042
+ const order = normalizeDistributionSourceOrder(activeConfig.sourceOrder);
2043
+
2044
+ const p2p = normalizeP2PConfig({
2045
+ ...activeConfig.p2p,
2046
+ transport: activeConfig?.p2p?.transport || p2pTransport,
2047
+ });
2048
+
2049
+ const downloadOptions = parseDownloadOptions({
2050
+ ...options,
2051
+ algorithm,
2052
+ onProgress,
2053
+ onDeliveryMetrics,
2054
+ signal,
2055
+ requiredEncoding: requiredEncoding ?? activeConfig.requiredContentEncoding ?? null,
2056
+ expectedHash:
2057
+ options.expectedHash
2058
+ ?? getExpectedShardHash(shardInfo, algorithm)
2059
+ ?? activeConfig.expectedHash
2060
+ ?? null,
2061
+ expectedSize: expectedSize ?? shardInfo?.size ?? null,
2062
+ expectedManifestVersionSet: options.expectedManifestVersionSet ?? null,
2063
+ writeToStore,
2064
+ maxRetries: maxRetries ?? activeConfig.maxRetries,
2065
+ initialRetryDelayMs: initialRetryDelayMs ?? activeConfig.initialRetryDelayMs,
2066
+ maxRetryDelayMs: maxRetryDelayMs ?? activeConfig.maxRetryDelayMs,
2067
+ });
2068
+
2069
+ if (antiRollback.enabled && antiRollback.requireExpectedHash && !downloadOptions.expectedHash) {
2070
+ throw createDopplerError(
2071
+ ERROR_CODES.DISTRIBUTION_SHARD_HASH_MISMATCH,
2072
+ `Missing expected hash for shard ${shardIndex} while antiRollback.requireExpectedHash=true.`
2073
+ );
2074
+ }
2075
+
2076
+ if (
2077
+ antiRollback.enabled
2078
+ && antiRollback.requireExpectedSize
2079
+ && !Number.isFinite(downloadOptions.expectedSize)
2080
+ ) {
2081
+ throw createDopplerError(
2082
+ ERROR_CODES.DISTRIBUTION_SHARD_SIZE_MISMATCH,
2083
+ `Missing expected size for shard ${shardIndex} while antiRollback.requireExpectedSize=true.`
2084
+ );
2085
+ }
2086
+
2087
+ if (
2088
+ antiRollback.enabled
2089
+ && antiRollback.requireManifestVersionSet
2090
+ && !downloadOptions.expectedManifestVersionSet
2091
+ ) {
2092
+ throw createDopplerError(
2093
+ ERROR_CODES.DISTRIBUTION_SHARD_MANIFEST_VERSION_SET_MISMATCH,
2094
+ `Missing expected manifestVersionSet for shard ${shardIndex} while antiRollback.requireManifestVersionSet=true.`
2095
+ );
2096
+ }
2097
+
2098
+ const planResult = resolveShardDeliveryPlan({
2099
+ sourceOrder: order,
2100
+ enableSourceCache,
2101
+ p2pEnabled: p2p.enabled,
2102
+ p2pTransportAvailable: typeof p2p.transport === 'function',
2103
+ httpEnabled: true,
2104
+ });
2105
+
2106
+ const trace = decisionTraceConfig.enabled
2107
+ && shouldEmitDecisionTrace(
2108
+ decisionTraceConfig,
2109
+ shardIndex,
2110
+ downloadOptions.expectedManifestVersionSet,
2111
+ order
2112
+ )
2113
+ ? createDecisionTrace(
2114
+ order,
2115
+ planResult.plan,
2116
+ shardIndex,
2117
+ decisionTraceConfig.deterministic,
2118
+ downloadOptions.expectedManifestVersionSet
2119
+ )
2120
+ : null;
2121
+
2122
+ const dedupeKey = createDeliveryKey(baseUrl, shardIndex, downloadOptions, order, sourceMatrix);
2123
+ if (inFlightDeliveries.has(dedupeKey)) {
2124
+ return await awaitWithSignal(
2125
+ inFlightDeliveries.get(dedupeKey),
2126
+ signal,
2127
+ `Shard ${shardIndex} delivery aborted`
2128
+ );
2129
+ }
2130
+
2131
+ const deliveryPromise = (async () => {
2132
+ const deliveryStart = performance.now();
2133
+ const attemptLog = [];
2134
+ const result = await executeDeliveryPlan(
2135
+ baseUrl,
2136
+ shardIndex,
2137
+ shardInfo,
2138
+ planResult.plan,
2139
+ p2p,
2140
+ downloadOptions,
2141
+ trace,
2142
+ decisionTraceConfig,
2143
+ sourceMatrix,
2144
+ attemptLog
2145
+ );
2146
+ const metrics = createDeliveryMetrics(
2147
+ order,
2148
+ result,
2149
+ attemptLog,
2150
+ performance.now() - deliveryStart
2151
+ );
2152
+ const resultWithMetrics = {
2153
+ ...result,
2154
+ deliveryMetrics: metrics,
2155
+ };
2156
+ await emitDeliveryMetricsHook(downloadOptions.onDeliveryMetrics, {
2157
+ schemaVersion: DISTRIBUTION_DELIVERY_METRICS_EVENT_SCHEMA_VERSION,
2158
+ shardIndex,
2159
+ source: result.source ?? null,
2160
+ path: result.path ?? null,
2161
+ expectedManifestVersionSet: downloadOptions.expectedManifestVersionSet ?? null,
2162
+ deliveryMetrics: metrics,
2163
+ decisionTrace: trace ?? null,
2164
+ });
2165
+ return attachDecisionTrace(resultWithMetrics, trace);
2166
+ })();
2167
+
2168
+ inFlightDeliveries.set(dedupeKey, deliveryPromise);
2169
+ try {
2170
+ return await awaitWithSignal(
2171
+ deliveryPromise,
2172
+ signal,
2173
+ `Shard ${shardIndex} delivery aborted`
2174
+ );
2175
+ } finally {
2176
+ inFlightDeliveries.delete(dedupeKey);
2177
+ }
2178
+ }
2179
+
2180
+ export function getSourceOrder(config = {}) {
2181
+ return normalizeDistributionSourceOrder(config.sourceOrder || config.sources || DISTRIBUTION_SOURCES);
2182
+ }
2183
+
2184
+ export function getInFlightShardDeliveryCount() {
2185
+ return inFlightDeliveries.size;
2186
+ }