doppler-gpu 0.5.1 → 0.6.1

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 (953) hide show
  1. package/CHANGELOG.md +33 -577
  2. package/LICENSE +21 -201
  3. package/README.md +165 -222
  4. package/models/catalog.json +241 -33
  5. package/package.json +106 -87
  6. package/src/capsule-runtime.d.ts +25 -0
  7. package/src/capsule-runtime.js +24 -0
  8. package/src/capsule.d.ts +8 -0
  9. package/src/capsule.js +4 -0
  10. package/src/cli/argument-contract.js +200 -0
  11. package/src/cli/browser-report-output.js +24 -0
  12. package/src/cli/cli-model-resolution.js +2 -2
  13. package/src/cli/cli-output.js +10 -0
  14. package/src/cli/commands/bundle.js +828 -0
  15. package/src/cli/doppler-cli.js +50 -1457
  16. package/src/cli/doppler-serve.js +18 -244
  17. package/src/cli/output/manifest.js +368 -0
  18. package/src/cli/runtime-profiles.js +1 -1
  19. package/src/cli/serve/capsule-handler.d.ts +33 -0
  20. package/src/cli/serve/capsule-handler.js +105 -0
  21. package/src/cli/serve/capsule-http.js +102 -0
  22. package/src/cli/serve/receipt.js +269 -0
  23. package/src/client/capsule-host.browser.js +2 -0
  24. package/src/client/capsule-host.d.ts +5 -0
  25. package/src/client/capsule-host.js +2 -0
  26. package/src/client/doppler-api.browser.js +42 -0
  27. package/src/client/doppler-api.d.ts +38 -1
  28. package/src/client/doppler-api.js +41 -1
  29. package/src/client/doppler-registry.js +15 -7
  30. package/src/client/electron/index.d.ts +4 -0
  31. package/src/client/electron/index.js +13 -0
  32. package/src/client/electron/ipc-contract.d.ts +9 -0
  33. package/src/client/electron/ipc-contract.js +56 -0
  34. package/src/client/electron/release-state.d.ts +53 -0
  35. package/src/client/electron/release-state.js +425 -0
  36. package/src/client/electron/renderer-runtime.d.ts +19 -0
  37. package/src/client/electron/renderer-runtime.js +104 -0
  38. package/src/client/failure-taxonomy.js +1 -28
  39. package/src/client/fault-injection.js +0 -8
  40. package/src/client/inspection.js +1 -1
  41. package/src/client/model-host/index.d.ts +140 -0
  42. package/src/client/model-host/index.js +548 -0
  43. package/src/client/model-host/model-evidence.js +255 -0
  44. package/src/client/model-host/model-session.d.ts +323 -0
  45. package/src/client/{runtime → model-host}/model-session.js +202 -148
  46. package/src/client/provider.d.ts +18 -2
  47. package/src/client/provider.js +26 -3
  48. package/src/client/receipt.js +30 -72
  49. package/src/client/runtime/capsule-acquisition.d.ts +18 -0
  50. package/src/client/runtime/capsule-acquisition.js +103 -0
  51. package/src/client/runtime/capsule-adapter-execution.d.ts +17 -0
  52. package/src/client/runtime/capsule-adapter-execution.js +63 -0
  53. package/src/client/runtime/capsule-artifact-source.js +62 -0
  54. package/src/client/runtime/capsule-embedding.d.ts +29 -0
  55. package/src/client/runtime/capsule-embedding.js +76 -0
  56. package/src/client/runtime/capsule-forecast-program.d.ts +2 -0
  57. package/src/client/runtime/capsule-forecast-program.js +85 -0
  58. package/src/client/runtime/capsule-forecast.d.ts +17 -0
  59. package/src/client/runtime/capsule-forecast.js +31 -0
  60. package/src/client/runtime/capsule-operation-adapters.d.ts +12 -0
  61. package/src/client/runtime/capsule-operation-adapters.js +69 -0
  62. package/src/client/runtime/capsule-operation-executor.d.ts +25 -0
  63. package/src/client/runtime/capsule-operation-executor.js +101 -0
  64. package/src/client/runtime/capsule-program-adapter.js +153 -0
  65. package/src/client/runtime/capsule-release-authorization.js +25 -0
  66. package/src/client/runtime/capsule-rerank.d.ts +50 -0
  67. package/src/client/runtime/capsule-rerank.js +105 -0
  68. package/src/client/runtime/capsule-session-execution.js +89 -0
  69. package/src/client/runtime/command-executor.d.ts +15 -0
  70. package/src/client/runtime/command-executor.js +128 -0
  71. package/src/client/runtime/composition-root.d.ts +77 -0
  72. package/src/client/runtime/composition-root.js +352 -0
  73. package/src/client/runtime/fetch-capsule-artifact-store.d.ts +10 -0
  74. package/src/client/runtime/fetch-capsule-artifact-store.js +25 -0
  75. package/src/client/runtime/index.d.ts +1 -117
  76. package/src/client/runtime/index.js +1 -433
  77. package/src/client/runtime/lora.d.ts +14 -0
  78. package/src/client/runtime/lora.js +19 -1
  79. package/src/client/runtime/model-manager.js +12 -0
  80. package/src/client/runtime/model-rerank.js +30 -0
  81. package/src/client/runtime/model-session.d.ts +1 -221
  82. package/src/client/runtime/model-source.d.ts +9 -0
  83. package/src/client/runtime/model-source.js +46 -17
  84. package/src/client/runtime/node-quickstart-cache.js +1 -1
  85. package/src/client/runtime/resolution-policy.d.ts +20 -0
  86. package/src/client/runtime/resolution-policy.js +84 -0
  87. package/src/client/runtime/resource-binder.d.ts +24 -0
  88. package/src/client/runtime/resource-binder.js +135 -0
  89. package/src/client/runtime/scoped-session.d.ts +10 -1
  90. package/src/client/runtime/scoped-session.js +20 -1
  91. package/src/client/runtime/session-controller.d.ts +19 -0
  92. package/src/client/runtime/session-controller.js +92 -0
  93. package/src/client/runtime/source-runtime.js +1 -1
  94. package/src/client/runtime/target-selector.d.ts +9 -0
  95. package/src/client/runtime/target-selector.js +2 -0
  96. package/src/client/runtime/types.d.ts +1 -0
  97. package/src/client/runtime/verified-capsule-artifact-store.js +101 -0
  98. package/src/client/wrap-pipeline-handle.js +3 -9
  99. package/src/config/backward-registry-loader.js +1 -1
  100. package/src/config/capsule-adapter-policy.js +30 -0
  101. package/src/config/capsule-adapters.d.ts +21 -0
  102. package/src/config/capsule-adapters.js +40 -0
  103. package/src/config/capsule-adapters.json +9 -0
  104. package/src/config/capsule-loading.d.ts +2 -0
  105. package/src/config/capsule-loading.js +14 -0
  106. package/src/config/capsule-loading.schema.json +12 -0
  107. package/src/config/capsule-operation.d.ts +19 -0
  108. package/src/config/capsule-operation.js +63 -0
  109. package/src/config/capsule-operations.json +17 -0
  110. package/src/config/capsule-release-contract.d.ts +66 -0
  111. package/src/config/capsule-release-contract.js +197 -0
  112. package/src/config/capsule-release-events.d.ts +53 -0
  113. package/src/config/capsule-release-events.js +172 -0
  114. package/src/config/capsule-sequence-receipt.js +19 -0
  115. package/src/config/capsule-serve.d.ts +9 -0
  116. package/src/config/capsule-serve.js +28 -0
  117. package/src/config/capsule-serve.schema.json +14 -0
  118. package/src/config/capsule-signature.d.ts +6 -0
  119. package/src/config/capsule-signature.js +50 -0
  120. package/src/config/capsule-v2.d.ts +70 -0
  121. package/src/config/capsule-v2.js +431 -0
  122. package/src/config/capsule-v3.d.ts +19 -0
  123. package/src/config/capsule-v3.js +72 -0
  124. package/src/config/capsule.d.ts +22 -0
  125. package/src/config/capsule.js +52 -0
  126. package/src/config/conversion/amplify/amplify-120m-f16-af32.json +1 -1
  127. package/src/config/conversion/diffusiongemma/diffusiongemma-26b-a4b-it-q4k-ehf16-af16.json +3 -3
  128. package/src/config/conversion/embeddinggemma/google-embeddinggemma-300m-q4k-ehf16-af32.json +2 -2
  129. package/src/config/conversion/esm/esm2-t12-35m-ur50d-f32-af32.json +62 -4
  130. package/src/config/conversion/esmc/esmc-300m-f32-af32.json +1 -1
  131. package/src/config/conversion/gemma3/gemma-3-1b-it-f16-af32.json +2 -2
  132. package/src/config/conversion/gemma3/gemma-3-1b-it-q4k-ehf16-af32.json +3 -3
  133. package/src/config/conversion/gemma3/gemma-3-270m-it-f16-af32.json +3 -3
  134. package/src/config/conversion/gemma3/gemma-3-270m-it-q4k-ehf16-af16.json +3 -3
  135. package/src/config/conversion/gemma3/gemma-3-270m-it-q4k-ehf16-af32.json +3 -3
  136. package/src/config/conversion/gemma3/translategemma-4b-1b-enes-q4k-ehf16-af32.json +3 -3
  137. package/src/config/conversion/gemma3/translategemma-4b-it-q4k-ehf16-af32.json +3 -3
  138. package/src/config/conversion/gemma4/gemma-4-12b-it-text-q4k-ehf16-af16.json +5 -5
  139. package/src/config/conversion/gemma4/gemma-4-12b-it-text-q4k-ehf16-af32.json +4 -4
  140. package/src/config/conversion/gemma4/gemma-4-12b-it-text-q4k-ehf16-hq4k-af16.json +6 -6
  141. package/src/config/conversion/gemma4/gemma-4-12b-it-text-w4a16-ct-ehf16-af16.json +3 -3
  142. package/src/config/conversion/gemma4/gemma-4-31b-it-text-q4k-ehf16-af16.json +5 -5
  143. package/src/config/conversion/gemma4/gemma-4-31b-it-text-q4k-ehf16-af32.json +4 -4
  144. package/src/config/conversion/gemma4/gemma-4-e2b-it-q4k-ehf16-af16-int4ple.json +6 -6
  145. package/src/config/conversion/gemma4/gemma-4-e2b-it-q4k-ehf16-af32-int4ple.json +3 -3
  146. package/src/config/conversion/gemma4/gemma-4-e2b-it-q4k-ehf16-af32.json +3 -3
  147. package/src/config/conversion/gemma4/gemma-4-moe-q4k-ehf16-af32.json +2 -2
  148. package/src/config/conversion/glmocr/glm-ocr-f16-af32.json +442 -0
  149. package/src/config/conversion/gpt-oss-20b-f16-xmxfp4.json +2 -2
  150. package/src/config/conversion/janus/janus-pro-1b-text-q4k-ehaf16.json +2 -2
  151. package/src/config/conversion/lfm2/lfm2.5-1.2b-instruct-q4k-ehf16-af32.json +2 -2
  152. package/src/config/conversion/minicpm/minicpm4-0-5b-f16-af32.json +3 -3
  153. package/src/config/conversion/muse-glimmer/muse-glimmer-30b-text-bf16-af32.json +529 -0
  154. package/src/config/conversion/muse-glimmer/muse-glimmer-30b-text-f16-af32.json +529 -0
  155. package/src/config/conversion/nucleotide-transformer/nucleotide-transformer-v2-50m-f32-af32.json +1 -1
  156. package/src/config/conversion/qwen3/qwen-3-5-0-8b-q4k-ehaf16.json +6 -6
  157. package/src/config/conversion/qwen3/qwen-3-5-2b-q4k-ehaf16.json +6 -6
  158. package/src/config/conversion/qwen3/qwen-3-5-9b-f16-af32.json +1 -1
  159. package/src/config/conversion/qwen3/qwen-3-5-9b-q4k-ehaf16.json +6 -6
  160. package/src/config/conversion/qwen3/qwen-3-6-27b-q4k-eaf16.json +7 -7
  161. package/src/config/conversion/qwen3/qwen-3-6-27b-q4k-ehaf16.json +6 -6
  162. package/src/config/conversion/qwen3/qwen-3-embedding-0-6b-f16-af32.json +463 -0
  163. package/src/config/conversion/qwen3/qwen-3-embedding-0-6b-q4k-ehf16-af32.json +3 -3
  164. package/src/config/conversion/qwen3/qwen-3-reranker-0-6b-f16-af32.json +3 -3
  165. package/src/config/conversion/qwen3/qwen-3-reranker-0-6b-f16-true-logit-af32.json +481 -0
  166. package/src/config/conversion/qwen3/qwen-3-reranker-0-6b-q4k-ehf16-af32.json +60 -8
  167. package/src/config/conversion/qwen3/qwen3.8-27b-text-f16-af32.json +561 -0
  168. package/src/config/conversion/qwen3/qwen3.8-27b-text-q4k-ehaf16.json +558 -0
  169. package/src/config/dtype-transition-contract.js +40 -0
  170. package/src/config/embedding-contract.js +26 -0
  171. package/src/config/embedding-reference.js +85 -0
  172. package/src/config/evidence/source-intake-policy.json +1 -1
  173. package/src/config/execution-contract-check.d.ts +16 -73
  174. package/src/config/execution-contract-check.js +7 -429
  175. package/src/config/forecast-manifest.js +56 -0
  176. package/src/config/generation-contract.d.ts +39 -0
  177. package/src/config/generation-contract.js +66 -0
  178. package/src/config/generation-contract.json +32 -0
  179. package/src/config/heterogeneous-attention-contract.js +46 -0
  180. package/src/config/initial-execution-identity.d.ts +58 -0
  181. package/src/config/initial-execution-identity.js +331 -0
  182. package/src/config/kernel-path-loader.d.ts +17 -0
  183. package/src/config/kernel-path-loader.js +42 -8
  184. package/src/config/kernel-registry-contract.d.ts +25 -0
  185. package/src/config/kernel-registry-contract.js +46 -0
  186. package/src/config/kernels/kernel-ref-digests.js +66 -18
  187. package/src/config/kernels/registry.js +1 -1
  188. package/src/config/kernels/registry.json +13617 -11377
  189. package/src/config/lora-layouts.d.ts +10 -0
  190. package/src/config/lora-layouts.js +17 -0
  191. package/src/config/lora-layouts.json +9 -0
  192. package/src/config/manifest-inference-validation.js +575 -0
  193. package/src/config/merge/execution-patches.js +12 -0
  194. package/src/config/{merge-helpers.js → merge/kernel-path-policy.js} +0 -52
  195. package/src/config/merge/value-selection.js +38 -0
  196. package/src/config/merge.js +43 -11
  197. package/src/config/model-ir-v2.d.ts +57 -0
  198. package/src/config/model-ir-v2.js +258 -0
  199. package/src/config/model-ir.d.ts +49 -0
  200. package/src/config/model-ir.js +180 -0
  201. package/src/config/param-validator.js +1 -1
  202. package/src/config/platforms/amd-strix-halo-gfx1151.json +17 -0
  203. package/src/config/platforms/loader.js +2 -1
  204. package/src/config/production-release-evidence.d.ts +23 -0
  205. package/src/config/production-release-evidence.js +423 -0
  206. package/src/config/production-release.js +321 -0
  207. package/src/{client/doppler-registry.json → config/quickstart-registry.json} +7 -7
  208. package/src/config/required-inference-fields-contract-check.js +2 -2
  209. package/src/config/rerank-reference.d.ts +28 -0
  210. package/src/config/rerank-reference.js +89 -0
  211. package/src/config/revocation-policy.d.ts +65 -0
  212. package/src/config/revocation-policy.js +290 -0
  213. package/src/config/revocation-registry.json +11 -0
  214. package/src/config/revocation-updates.d.ts +68 -0
  215. package/src/config/revocation-updates.js +440 -0
  216. package/src/config/runtime/profiles/gemma4-31b-f16-activations-probe.json +1 -1
  217. package/src/config/runtime/profiles/muse-glimmer-30b-source-parity-f32kv.json +30 -0
  218. package/src/config/runtime/profiles/muse-glimmer-30b-source-parity.json +110 -0
  219. package/src/config/runtime/profiles/qwen3-6-27b-f16-activations-probe.json +1 -1
  220. package/src/config/runtime/profiles/qwen3-8-27b-source-parity.json +87 -0
  221. package/src/config/runtime-merge.js +1 -1
  222. package/src/config/runtime.js +1 -1
  223. package/src/config/schema/browser-suite-metrics.schema.d.ts +1 -0
  224. package/src/config/schema/browser-suite-metrics.schema.js +2 -0
  225. package/src/config/schema/conversion-report.schema.d.ts +3 -0
  226. package/src/config/schema/conversion-report.schema.js +29 -0
  227. package/src/config/schema/converter.schema.d.ts +1 -0
  228. package/src/config/schema/debug.schema.d.ts +1 -0
  229. package/src/config/schema/doppler.schema.js +6 -5
  230. package/src/config/schema/emulation.schema.js +1 -1
  231. package/src/config/schema/execution-v1.schema.d.ts +3 -0
  232. package/src/config/schema/execution-v1.schema.js +19 -0
  233. package/src/config/schema/harness.schema.d.ts +13 -0
  234. package/src/config/schema/harness.schema.js +6 -0
  235. package/src/config/schema/inference.schema.d.ts +21 -2
  236. package/src/config/schema/kernel-thresholds.schema.js +1 -1
  237. package/src/config/schema/manifest.schema.d.ts +26 -7
  238. package/src/config/schema/manifest.schema.js +10 -1
  239. package/src/config/schema/program-bundle.schema.d.ts +52 -6
  240. package/src/config/schema/program-bundle.schema.js +145 -10
  241. package/src/config/sequence-reference.d.ts +26 -0
  242. package/src/config/sequence-reference.js +100 -0
  243. package/src/config/source-packages/litert/gemma-4-e2b-it.json +2 -2
  244. package/src/config/target-plan.d.ts +108 -0
  245. package/src/config/target-plan.js +411 -0
  246. package/src/config/training-defaults.js +1 -1
  247. package/src/config/transforms/capability-transform-resolver.js +4 -105
  248. package/src/config/transforms/execution-graph/dtype.js +591 -0
  249. package/src/config/transforms/execution-graph/fusion.js +436 -0
  250. package/src/config/transforms/execution-graph/validation.js +414 -0
  251. package/src/config/transforms/execution-graph-transforms.js +13 -1419
  252. package/src/converter/artifact-identity.js +475 -0
  253. package/src/converter/conversion-plan.js +1 -1
  254. package/src/converter/core.js +94 -2096
  255. package/src/converter/forge-candidate-evaluation.js +271 -0
  256. package/src/converter/forge-qualification.js +163 -0
  257. package/src/converter/forge-stages.js +800 -0
  258. package/src/converter/manifest-builder.js +683 -0
  259. package/src/converter/quantization-info.js +4 -1
  260. package/src/converter/quantizer.js +76 -13
  261. package/src/converter/shard-packer.d.ts +1 -1
  262. package/src/converter/shard-packer.js +2 -2
  263. package/src/converter/tensor-transform.js +640 -0
  264. package/src/converter/tokenizer-contract.js +243 -0
  265. package/src/converter/tokenizer-manifest.js +243 -0
  266. package/src/debug/history.js +0 -8
  267. package/src/debug/index.js +0 -10
  268. package/src/debug/tensor.js +1 -1
  269. package/src/debug/trace.js +0 -7
  270. package/src/experimental/adapters/adapter-manager.js +2 -0
  271. package/src/experimental/adapters/adapter-manifest.d.ts +6 -0
  272. package/src/experimental/adapters/adapter-manifest.js +11 -0
  273. package/src/experimental/adapters/artifact-contract.d.ts +2 -2
  274. package/src/experimental/adapters/artifact-contract.js +5 -2
  275. package/src/experimental/adapters/lora-loader.d.ts +10 -1
  276. package/src/experimental/adapters/lora-loader.js +110 -48
  277. package/src/{inference/pipelines → experimental}/audio/gemma4.js +9 -31
  278. package/src/experimental/bdpa/basis-decomposed-paged.d.ts +50 -0
  279. package/src/experimental/bdpa/basis-decomposed-paged.js +276 -0
  280. package/src/experimental/bridge/trainer-artifact-bridge.js +3 -3
  281. package/src/experimental/browser/browser-converter.js +11 -11
  282. package/src/experimental/diffusion/index.d.ts +1 -1
  283. package/src/experimental/diffusion/index.js +1 -1
  284. package/src/{inference/pipelines → experimental}/diffusion/pipeline.d.ts +2 -2
  285. package/src/{inference/pipelines → experimental}/diffusion/pipeline.js +17 -17
  286. package/src/{inference/pipelines → experimental}/diffusion/text-encoder-gpu.js +21 -19
  287. package/src/experimental/diffusion/vae.js +596 -0
  288. package/src/experimental/distribution/shard-delivery/receipt.js +588 -0
  289. package/src/experimental/distribution/shard-delivery/retry.js +392 -0
  290. package/src/experimental/distribution/shard-delivery/transport.js +831 -0
  291. package/src/experimental/distribution/shard-delivery.js +4 -1709
  292. package/src/experimental/energy/index.d.ts +2 -2
  293. package/src/experimental/energy/index.js +2 -2
  294. package/src/{inference/pipelines → experimental}/energy/pipeline.js +13 -13
  295. package/src/{inference/pipelines → experimental}/energy/types.d.ts +2 -2
  296. package/src/experimental/logits/cpu-output.d.ts +27 -0
  297. package/src/{inference/pipelines/text/logits/utils.js → experimental/logits/cpu-output.js} +19 -27
  298. package/src/experimental/logits/cpu.d.ts +87 -0
  299. package/src/experimental/logits/cpu.js +95 -0
  300. package/src/experimental/orchestration/energy-row-head-pipeline.d.ts +103 -0
  301. package/src/{inference/pipelines/energy-head/row-head-pipeline.js → experimental/orchestration/energy-row-head-pipeline.js} +13 -13
  302. package/src/experimental/orchestration/index.d.ts +1 -1
  303. package/src/experimental/orchestration/index.js +1 -1
  304. package/src/experimental/training/artifacts.js +2 -2
  305. package/src/experimental/training/checkpoint.js +2 -2
  306. package/src/experimental/training/distillation/artifacts.js +1 -1
  307. package/src/experimental/training/distillation/dataset.js +2 -2
  308. package/src/experimental/training/distillation/eval.js +1 -1
  309. package/src/experimental/training/distillation/runtime.js +2 -2
  310. package/src/experimental/training/distillation/student-fixture/execution.js +166 -0
  311. package/src/experimental/training/distillation/student-fixture/model.js +353 -0
  312. package/src/experimental/training/distillation/student-fixture.js +2 -469
  313. package/src/experimental/training/lora/checkpoint.js +746 -0
  314. package/src/experimental/training/lora/execution.js +550 -0
  315. package/src/experimental/training/lora/plan.js +387 -0
  316. package/src/experimental/training/lora/recovery.js +418 -0
  317. package/src/experimental/training/lora-pipeline.js +7 -1949
  318. package/src/experimental/training/operator-artifacts.js +2 -2
  319. package/src/experimental/training/operator-command.js +4 -4
  320. package/src/experimental/training/runner/evaluation.js +62 -0
  321. package/src/experimental/training/runner/lifecycle.js +257 -0
  322. package/src/experimental/training/runner.js +5 -268
  323. package/src/experimental/training/suite/execution.js +879 -0
  324. package/src/experimental/training/suite/plan.js +855 -0
  325. package/src/experimental/training/suite.js +5 -1659
  326. package/src/experimental/training/workloads.d.ts +5 -5
  327. package/src/experimental/training/workloads.js +6 -6
  328. package/src/experimental/turboquant/codebook.js +470 -0
  329. package/src/formats/quantization-constants.d.ts +11 -0
  330. package/src/formats/quantization-constants.js +11 -0
  331. package/src/formats/rdrr/integrity.js +1 -1
  332. package/src/formats/rdrr/lowerings.js +0 -29
  333. package/src/formats/rdrr/merkle.js +1 -1
  334. package/src/formats/rdrr/tensor-config-validator.js +25 -5
  335. package/src/formats/rdrr/validation.js +3 -3
  336. package/src/gpu/command-recorder.d.ts +10 -1
  337. package/src/gpu/command-recorder.js +58 -14
  338. package/src/gpu/constant-buffer.js +29 -0
  339. package/src/gpu/device-state.js +44 -0
  340. package/src/gpu/device.js +11 -41
  341. package/src/gpu/kernel-runtime.js +2 -1
  342. package/src/gpu/kernels/activation-static-qdq.js +1 -1
  343. package/src/gpu/kernels/append_embedding.wgsl +14 -0
  344. package/src/gpu/kernels/attention/executors.js +960 -0
  345. package/src/gpu/kernels/attention/plan.js +472 -0
  346. package/src/gpu/kernels/attention.d.ts +4 -0
  347. package/src/gpu/kernels/attention.js +145 -1545
  348. package/src/gpu/kernels/attention_relative.wgsl +59 -0
  349. package/src/gpu/kernels/backward/adam.js +2 -1
  350. package/src/gpu/kernels/backward/attention_backward.js +1 -1
  351. package/src/gpu/kernels/backward/bias_add_backward.d.ts +1 -1
  352. package/src/gpu/kernels/backward/bias_add_backward.js +1 -1
  353. package/src/gpu/kernels/backward/causal_conv1d_silu_backward.js +2 -1
  354. package/src/gpu/kernels/backward/conv2d_backward.d.ts +1 -1
  355. package/src/gpu/kernels/backward/conv2d_backward.js +2 -1
  356. package/src/gpu/kernels/backward/cross_entropy_backward.d.ts +1 -1
  357. package/src/gpu/kernels/backward/cross_entropy_backward.js +1 -1
  358. package/src/gpu/kernels/backward/embed_backward.js +2 -1
  359. package/src/gpu/kernels/backward/executor.d.ts +34 -0
  360. package/src/gpu/kernels/backward/executor.js +158 -0
  361. package/src/gpu/kernels/backward/gated_delta_recurrent_backward.js +2 -1
  362. package/src/gpu/kernels/backward/gated_rmsnorm_backward.js +2 -1
  363. package/src/gpu/kernels/backward/gelu_backward.d.ts +1 -1
  364. package/src/gpu/kernels/backward/gelu_backward.js +1 -1
  365. package/src/gpu/kernels/backward/groupnorm_backward.d.ts +1 -1
  366. package/src/gpu/kernels/backward/groupnorm_backward.js +1 -1
  367. package/src/gpu/kernels/backward/layernorm_backward.d.ts +1 -1
  368. package/src/gpu/kernels/backward/layernorm_backward.js +2 -1
  369. package/src/gpu/kernels/backward/matmul.js +133 -0
  370. package/src/gpu/kernels/backward/matmul_backward.d.ts +0 -1
  371. package/src/gpu/kernels/backward/matmul_backward.js +1 -1
  372. package/src/gpu/kernels/backward/pixel_shuffle_backward.d.ts +1 -1
  373. package/src/gpu/kernels/backward/pixel_shuffle_backward.js +1 -1
  374. package/src/gpu/kernels/backward/rmsnorm_backward.d.ts +1 -1
  375. package/src/gpu/kernels/backward/rmsnorm_backward.js +2 -1
  376. package/src/gpu/kernels/backward/rope_backward.d.ts +1 -1
  377. package/src/gpu/kernels/backward/rope_backward.js +2 -1
  378. package/src/gpu/kernels/backward/scale_backward.d.ts +1 -1
  379. package/src/gpu/kernels/backward/scale_backward.js +2 -1
  380. package/src/gpu/kernels/backward/silu_backward.d.ts +1 -1
  381. package/src/gpu/kernels/backward/silu_backward.js +1 -1
  382. package/src/gpu/kernels/backward/softmax_backward.d.ts +1 -1
  383. package/src/gpu/kernels/backward/softmax_backward.js +1 -1
  384. package/src/gpu/kernels/backward/upsample2d_backward.d.ts +1 -1
  385. package/src/gpu/kernels/backward/upsample2d_backward.js +1 -1
  386. package/src/gpu/kernels/backward/utils.js +2 -392
  387. package/src/gpu/kernels/bind-group-validation.js +16 -0
  388. package/src/gpu/kernels/cast.js +2 -1
  389. package/src/gpu/kernels/check-finiteness.js +13 -49
  390. package/src/gpu/kernels/check-hot-vocab-stop.js +13 -39
  391. package/src/gpu/kernels/check-stop.js +14 -33
  392. package/src/gpu/kernels/check_finiteness.wgsl +33 -0
  393. package/src/gpu/kernels/check_hot_vocab_stop.wgsl +29 -0
  394. package/src/gpu/kernels/check_stop.wgsl +20 -0
  395. package/src/gpu/kernels/clamp.js +1 -1
  396. package/src/gpu/kernels/conv2d.js +1 -1
  397. package/src/gpu/kernels/cross_entropy_loss.js +1 -1
  398. package/src/gpu/kernels/depthwise_conv1d.js +1 -1
  399. package/src/gpu/kernels/depthwise_conv2d.js +1 -1
  400. package/src/gpu/kernels/dequant.js +7 -2
  401. package/src/gpu/kernels/diffusion-gemma-sampling.js +3 -5
  402. package/src/gpu/kernels/dispatch.js +12 -1
  403. package/src/gpu/kernels/embedding-pool.js +72 -0
  404. package/src/gpu/kernels/embedding_pool_f16.wgsl +38 -0
  405. package/src/gpu/kernels/embedding_pool_f32.wgsl +36 -0
  406. package/src/gpu/kernels/energy.js +2 -1
  407. package/src/gpu/kernels/forecast_output.wgsl +17 -0
  408. package/src/gpu/kernels/fused-gate-up-gelu.js +1 -1
  409. package/src/gpu/kernels/fused_ffn.js +10 -220
  410. package/src/gpu/kernels/fused_matmul_residual.js +2 -1
  411. package/src/gpu/kernels/fused_matmul_rmsnorm.js +2 -1
  412. package/src/gpu/kernels/fused_normed_ffn_q4k.wgsl +208 -0
  413. package/src/gpu/kernels/gated-short-conv.js +11 -69
  414. package/src/gpu/kernels/gated_delta_conv.wgsl +66 -0
  415. package/src/gpu/kernels/gated_delta_conv_f16.wgsl +68 -0
  416. package/src/gpu/kernels/gated_delta_fused_decode.wgsl +173 -0
  417. package/src/gpu/kernels/gated_delta_fused_decode_f16.wgsl +175 -0
  418. package/src/gpu/kernels/gated_delta_recurrent.wgsl +165 -0
  419. package/src/gpu/kernels/gated_delta_recurrent_f16.wgsl +167 -0
  420. package/src/gpu/kernels/gated_short_conv.wgsl +47 -0
  421. package/src/gpu/kernels/gather.d.ts +1 -1
  422. package/src/gpu/kernels/gather.js +30 -6
  423. package/src/gpu/kernels/gather_bf16.wgsl +69 -0
  424. package/src/gpu/kernels/gelu.d.ts +4 -0
  425. package/src/gpu/kernels/gelu.js +12 -2
  426. package/src/gpu/kernels/gelu.wgsl +16 -0
  427. package/src/gpu/kernels/gelu_f16.wgsl +16 -0
  428. package/src/gpu/kernels/gemma4-route-expert.js +1 -1
  429. package/src/gpu/kernels/grouped_pointwise_conv2d.js +1 -1
  430. package/src/gpu/kernels/groupnorm.js +1 -1
  431. package/src/gpu/kernels/index.d.ts +38 -5
  432. package/src/gpu/kernels/index.js +18 -4
  433. package/src/gpu/kernels/instance_stats.wgsl +26 -0
  434. package/src/gpu/kernels/kernel-base.js +1 -1
  435. package/src/gpu/kernels/kernel-cache-lifecycle.d.ts +9 -0
  436. package/src/gpu/kernels/kernel-cache-lifecycle.js +26 -0
  437. package/src/gpu/kernels/kernel-configs.d.ts +10 -61
  438. package/src/gpu/kernels/kernel-configs.js +26 -54
  439. package/src/gpu/kernels/kernel-execution.js +141 -0
  440. package/src/gpu/kernels/kv-cache-write.js +4 -4
  441. package/src/gpu/kernels/kv-quantize.js +4 -1
  442. package/src/gpu/kernels/l2-normalize.js +36 -0
  443. package/src/gpu/kernels/l2_normalize.wgsl +53 -0
  444. package/src/gpu/kernels/layernorm.d.ts +4 -4
  445. package/src/gpu/kernels/layernorm.js +44 -7
  446. package/src/gpu/kernels/linear-attention-core.js +29 -449
  447. package/src/gpu/kernels/linear_attention.js +1 -1
  448. package/src/gpu/kernels/lm-head-argmax.js +2 -2
  449. package/src/gpu/kernels/lm-head-select-logits.js +2 -2
  450. package/src/gpu/kernels/logit-finalize.js +103 -0
  451. package/src/gpu/kernels/logit-merge.js +13 -125
  452. package/src/gpu/kernels/logit-scatter.js +40 -0
  453. package/src/gpu/kernels/logit_finalize_f16.wgsl +43 -0
  454. package/src/gpu/kernels/logit_finalize_f32.wgsl +41 -0
  455. package/src/gpu/kernels/logit_merge_geometric.wgsl +23 -0
  456. package/src/gpu/kernels/logit_merge_max.wgsl +23 -0
  457. package/src/gpu/kernels/logit_merge_weighted.wgsl +23 -0
  458. package/src/gpu/kernels/logit_scatter_f16.wgsl +26 -0
  459. package/src/gpu/kernels/logit_scatter_f32.wgsl +24 -0
  460. package/src/gpu/kernels/matmul-dispatch.js +6 -1
  461. package/src/gpu/kernels/matmul-selection.js +3 -2
  462. package/src/gpu/kernels/matmul.js +4 -15
  463. package/src/gpu/kernels/matmul_bf16w_f32a.wgsl +93 -0
  464. package/src/gpu/kernels/matmul_gemv_subgroup_bf16w.wgsl +433 -0
  465. package/src/gpu/kernels/modulate.js +1 -1
  466. package/src/gpu/kernels/moe.js +3 -1
  467. package/src/gpu/kernels/moe_router_bias_add_f16_f16.wgsl +23 -0
  468. package/src/gpu/kernels/moe_router_bias_add_f16_f32.wgsl +23 -0
  469. package/src/gpu/kernels/moe_router_bias_add_f32_f16.wgsl +23 -0
  470. package/src/gpu/kernels/moe_router_bias_add_f32_f32.wgsl +21 -0
  471. package/src/gpu/kernels/patch_context.wgsl +29 -0
  472. package/src/gpu/kernels/pipeline-cache.js +3 -1
  473. package/src/gpu/kernels/pixel_shuffle.js +1 -1
  474. package/src/gpu/kernels/probe_shader_f16.wgsl +7 -0
  475. package/src/gpu/kernels/relu.js +1 -1
  476. package/src/gpu/kernels/rep-penalty.js +30 -5
  477. package/src/gpu/kernels/rep_penalty.wgsl +25 -30
  478. package/src/gpu/kernels/rep_penalty_f16.wgsl +24 -25
  479. package/src/gpu/kernels/repeat_channels.js +1 -1
  480. package/src/gpu/kernels/residual.js +2 -2
  481. package/src/gpu/kernels/rmsnorm-pair.js +1 -1
  482. package/src/gpu/kernels/rmsnorm-qk.js +1 -1
  483. package/src/gpu/kernels/rmsnorm-stats.js +14 -113
  484. package/src/gpu/kernels/rmsnorm.js +1 -1
  485. package/src/gpu/kernels/rmsnorm_serial.wgsl +19 -0
  486. package/src/gpu/kernels/rmsnorm_stats.wgsl +59 -0
  487. package/src/gpu/kernels/rmsnorm_stats_subgroups.wgsl +76 -0
  488. package/src/gpu/kernels/rope-precompute.js +160 -0
  489. package/src/gpu/kernels/rope-qk.js +1 -1
  490. package/src/gpu/kernels/rope.js +2 -2
  491. package/src/gpu/kernels/rope_precompute.wgsl +74 -0
  492. package/src/gpu/kernels/sample.d.ts +1 -0
  493. package/src/gpu/kernels/sample.js +36 -17
  494. package/src/gpu/kernels/sample.wgsl +112 -157
  495. package/src/gpu/kernels/sample_f16.wgsl +168 -162
  496. package/src/gpu/kernels/scale.js +1 -1
  497. package/src/gpu/kernels/shader-cache.js +8 -4
  498. package/src/gpu/kernels/shader-source-scope.js +68 -0
  499. package/src/gpu/kernels/silu.js +2 -1
  500. package/src/gpu/kernels/soft-embedding.js +3 -1
  501. package/src/gpu/kernels/softmax.js +4 -2
  502. package/src/gpu/kernels/split-qkv-rmsnorm-qk.js +1 -1
  503. package/src/gpu/kernels/split-qkv-rmsnorm-rope-qk.js +1 -1
  504. package/src/gpu/kernels/split_qg.js +1 -1
  505. package/src/gpu/kernels/split_qkv.d.ts +2 -0
  506. package/src/gpu/kernels/split_qkv.js +18 -3
  507. package/src/gpu/kernels/split_qkv.wgsl +5 -2
  508. package/src/gpu/kernels/split_qkv_f16.wgsl +5 -2
  509. package/src/gpu/kernels/submit_probe.wgsl +6 -0
  510. package/src/gpu/kernels/transpose.js +1 -1
  511. package/src/gpu/kernels/turboquant-codebook.js +4 -535
  512. package/src/gpu/kernels/uniform-utils.js +7 -2
  513. package/src/gpu/kernels/upsample2d.js +1 -1
  514. package/src/gpu/kernels/utils.js +9 -330
  515. package/src/gpu/kernels/vision-average-pool.d.ts +13 -0
  516. package/src/gpu/kernels/vision-average-pool.js +68 -0
  517. package/src/gpu/kernels/vision-patch-embed.d.ts +17 -0
  518. package/src/gpu/kernels/vision-patch-embed.js +117 -0
  519. package/src/gpu/kernels/vision-position-embedding.d.ts +13 -0
  520. package/src/gpu/kernels/vision-position-embedding.js +79 -0
  521. package/src/gpu/kernels/vision-rope-2d.d.ts +16 -0
  522. package/src/gpu/kernels/vision-rope-2d.js +73 -0
  523. package/src/gpu/kernels/vision-spatial-merge.d.ts +19 -0
  524. package/src/gpu/kernels/vision-spatial-merge.js +100 -0
  525. package/src/gpu/kernels/vision_average_pool.wgsl +42 -0
  526. package/src/gpu/kernels/vision_patch_embed.wgsl +59 -0
  527. package/src/gpu/kernels/vision_patch_embed_f16w.wgsl +61 -0
  528. package/src/gpu/kernels/vision_position_embedding.wgsl +33 -0
  529. package/src/gpu/kernels/vision_position_embedding_f16.wgsl +35 -0
  530. package/src/gpu/kernels/vision_rope_2d.wgsl +60 -0
  531. package/src/gpu/kernels/vision_spatial_merge.wgsl +48 -0
  532. package/src/gpu/readback-buffer.js +20 -0
  533. package/src/gpu/submit-probe.js +2 -26
  534. package/src/gpu/weight-buffer.d.ts +1 -0
  535. package/src/gpu/weight-buffer.js +7 -0
  536. package/src/index.d.ts +12 -0
  537. package/src/index.js +2 -0
  538. package/src/inference/browser-harness/benchmark-execution.js +872 -0
  539. package/src/inference/browser-harness/benchmark-rerank.js +284 -0
  540. package/src/inference/browser-harness/decode-diagnostics.js +241 -0
  541. package/src/inference/browser-harness/diffusion-performance.js +102 -0
  542. package/src/inference/browser-harness/inference-execution.js +543 -0
  543. package/src/inference/{browser-harness-model-helpers.js → browser-harness/model-initialization.js} +14 -145
  544. package/src/inference/browser-harness/model-resolution.js +137 -0
  545. package/src/inference/browser-harness/report.js +338 -0
  546. package/src/inference/browser-harness/request.js +263 -0
  547. package/src/inference/browser-harness/runtime-config.d.ts +25 -0
  548. package/src/inference/browser-harness/runtime-config.js +255 -0
  549. package/src/inference/browser-harness/runtime-isolation.d.ts +13 -0
  550. package/src/inference/browser-harness/runtime-isolation.js +45 -0
  551. package/src/inference/browser-harness/runtime-report.d.ts +26 -0
  552. package/src/inference/browser-harness/runtime-report.js +117 -0
  553. package/src/inference/browser-harness/suite-summary.js +39 -0
  554. package/src/inference/browser-harness/text-evidence.js +694 -0
  555. package/src/inference/browser-harness/text-execution.js +757 -0
  556. package/src/inference/browser-harness/text-input.js +725 -0
  557. package/src/inference/{browser-harness-suite-helpers.js → browser-harness/timing-diagnostics.js} +1 -379
  558. package/src/inference/browser-harness-contract-helpers.js +1 -1
  559. package/src/inference/browser-harness-diffusion-energy-suites.js +8 -3
  560. package/src/inference/browser-harness-runtime-helpers.d.ts +3 -56
  561. package/src/inference/browser-harness-runtime-helpers.js +3 -423
  562. package/src/inference/browser-harness-text-helpers.js +31 -2199
  563. package/src/inference/browser-harness.d.ts +15 -2
  564. package/src/inference/browser-harness.js +38 -1952
  565. package/src/inference/kv-cache/base.js +67 -381
  566. package/src/inference/kv-cache/basis-decomposed-paged.d.ts +2 -50
  567. package/src/inference/kv-cache/basis-decomposed-paged.js +1 -276
  568. package/src/inference/kv-cache/quantized.d.ts +2 -2
  569. package/src/inference/kv-cache/quantized.js +4 -8
  570. package/src/inference/kv-cache/update.js +309 -0
  571. package/src/inference/model-checkpoint-evidence.d.ts +75 -0
  572. package/src/inference/model-checkpoint-evidence.js +264 -0
  573. package/src/inference/moe-router.js +36 -216
  574. package/src/inference/multi-model-network.js +5 -341
  575. package/src/inference/pipelines/audio/index.js +1 -20
  576. package/src/inference/pipelines/audio/mel.js +0 -38
  577. package/src/inference/pipelines/context.js +6 -2
  578. package/src/inference/pipelines/diffusion/profile-timing.js +10 -0
  579. package/src/inference/pipelines/diffusion/runtime-resources.js +54 -0
  580. package/src/inference/pipelines/diffusion/sd3/execution.js +805 -0
  581. package/src/inference/pipelines/diffusion/sd3/plan.js +125 -0
  582. package/src/inference/pipelines/diffusion/sd3-transformer.js +44 -895
  583. package/src/inference/pipelines/diffusion/{vae.js → vae/execution.js} +46 -618
  584. package/src/inference/pipelines/diffusion/{helpers.js → weight-contract.js} +1 -68
  585. package/src/inference/pipelines/energy-head/row-head-pipeline.d.ts +1 -103
  586. package/src/inference/pipelines/factory.js +14 -3
  587. package/src/inference/pipelines/shader-scoped-pipeline.js +29 -0
  588. package/src/inference/pipelines/structured/json-grammar-mask.d.ts +2 -0
  589. package/src/inference/pipelines/structured/json-grammar-mask.js +115 -124
  590. package/src/inference/pipelines/text/abort-contract.js +22 -0
  591. package/src/inference/pipelines/text/attention/attn-config.js +1 -16
  592. package/src/inference/pipelines/text/attention/gate-projection.js +30 -0
  593. package/src/inference/pipelines/text/attention/heterogeneous-contract.js +8 -0
  594. package/src/inference/pipelines/text/attention/interpreter/immediate.js +211 -0
  595. package/src/inference/pipelines/text/attention/interpreter/recorded.js +451 -0
  596. package/src/inference/pipelines/text/attention/interpreter.js +89 -439
  597. package/src/inference/pipelines/text/attention/plan.d.ts +1 -0
  598. package/src/inference/pipelines/text/attention/plan.js +17 -0
  599. package/src/inference/pipelines/text/attention/projection-executor.js +331 -0
  600. package/src/inference/pipelines/text/attention/projection-plan.js +259 -0
  601. package/src/inference/pipelines/text/attention/projections.js +20 -537
  602. package/src/inference/pipelines/text/attention/query-transform.js +11 -0
  603. package/src/inference/pipelines/text/attention/record.js +0 -6
  604. package/src/inference/pipelines/text/attention/recorded-health.js +21 -0
  605. package/src/inference/pipelines/text/attention/rope-observation.js +40 -0
  606. package/src/inference/pipelines/text/attention/run.js +0 -6
  607. package/src/inference/pipelines/text/attention/types.js +3 -31
  608. package/src/inference/pipelines/text/chat-format.d.ts +3 -0
  609. package/src/inference/pipelines/text/chat-format.js +42 -0
  610. package/src/inference/pipelines/text/config/normalization.js +729 -0
  611. package/src/inference/pipelines/text/config/validation.js +9 -0
  612. package/src/inference/pipelines/text/config.d.ts +12 -1
  613. package/src/inference/pipelines/text/config.js +14 -1228
  614. package/src/inference/pipelines/text/debug-utils/health-metrics.js +57 -0
  615. package/src/inference/pipelines/text/debug-utils/index.js +3 -8
  616. package/src/inference/pipelines/text/debug-utils/profiles.js +8 -0
  617. package/src/inference/pipelines/text/debug-utils/readback-decoding.js +24 -0
  618. package/src/inference/pipelines/text/debug-utils/tensor.js +1 -1
  619. package/src/inference/pipelines/text/dtype-contract.js +1 -68
  620. package/src/inference/pipelines/text/embed.js +178 -241
  621. package/src/inference/pipelines/text/embedding-contract.d.ts +7 -0
  622. package/src/inference/pipelines/text/embedding-contract.js +21 -0
  623. package/src/inference/pipelines/text/embedding-extraction.js +225 -0
  624. package/src/inference/pipelines/text/embedding-normalization.js +39 -0
  625. package/src/inference/pipelines/text/execution-runtime-builders.js +0 -32
  626. package/src/inference/pipelines/text/execution-v1/plan.js +69 -0
  627. package/src/inference/pipelines/text/execution-v1/validation.js +233 -0
  628. package/src/inference/pipelines/text/execution-v1.js +7 -283
  629. package/src/inference/pipelines/text/execution.js +517 -0
  630. package/src/inference/pipelines/text/ffn/dense-executor.js +723 -0
  631. package/src/inference/pipelines/text/ffn/dense-plan.js +327 -0
  632. package/src/inference/pipelines/text/ffn/dense.js +6 -970
  633. package/src/inference/pipelines/text/ffn/sandwich.js +10 -9
  634. package/src/inference/pipelines/text/ffn/standard.js +2 -1
  635. package/src/inference/pipelines/text/generator/attention-lifecycle.js +24 -0
  636. package/src/inference/pipelines/text/generator/decode-runtime.js +985 -0
  637. package/src/inference/pipelines/text/generator/decode.js +964 -0
  638. package/src/inference/pipelines/text/generator/diffusion.js +607 -0
  639. package/src/inference/pipelines/text/generator/logits-config.js +63 -0
  640. package/src/inference/pipelines/text/generator/prefill-observation.js +29 -0
  641. package/src/inference/pipelines/text/generator/prefill-policy.js +56 -0
  642. package/src/inference/pipelines/text/generator/prefill-runtime.js +896 -0
  643. package/src/inference/pipelines/text/{generator-prefill-helpers.js → generator/prefix-embedding.js} +16 -121
  644. package/src/inference/pipelines/text/generator/prompt-input.js +47 -0
  645. package/src/inference/pipelines/text/generator/recovery.js +310 -0
  646. package/src/inference/pipelines/text/{generator-helpers.js → generator/session-context.js} +42 -110
  647. package/src/inference/pipelines/text/generator/text.js +448 -0
  648. package/src/inference/pipelines/text/generator/training.js +753 -0
  649. package/src/inference/pipelines/text/generator-decode-policy.js +1 -1
  650. package/src/inference/pipelines/text/generator-logits-observation.js +48 -0
  651. package/src/inference/pipelines/text/generator-runtime.js +23 -154
  652. package/src/inference/pipelines/text/generator-steps.js +45 -980
  653. package/src/inference/pipelines/text/generator.js +179 -3552
  654. package/src/inference/pipelines/text/image-transcription.js +304 -0
  655. package/src/inference/pipelines/text/init-qkv-fusion.js +18 -9
  656. package/src/inference/pipelines/text/init-rope.js +37 -189
  657. package/src/inference/pipelines/text/init.d.ts +8 -3
  658. package/src/inference/pipelines/text/init.js +6 -216
  659. package/src/inference/pipelines/text/layer-execution-contract.js +97 -0
  660. package/src/inference/pipelines/text/layer-input-execution.js +435 -0
  661. package/src/inference/pipelines/text/layer-plan-gpu.js +13 -6
  662. package/src/inference/pipelines/text/layer-plan.d.ts +21 -5
  663. package/src/inference/pipelines/text/layer-plan.js +45 -3
  664. package/src/inference/pipelines/text/layer.js +52 -522
  665. package/src/inference/pipelines/text/lifecycle.js +839 -0
  666. package/src/inference/pipelines/text/linear-attention/plan.js +554 -0
  667. package/src/inference/pipelines/text/linear-attention-ab-fusion.js +2 -2
  668. package/src/inference/pipelines/text/linear-attention.d.ts +2 -2
  669. package/src/inference/pipelines/text/linear-attention.js +28 -534
  670. package/src/inference/pipelines/text/logits/cpu-output.d.ts +1 -0
  671. package/src/inference/pipelines/text/logits/cpu-output.js +5 -0
  672. package/src/inference/pipelines/text/logits/cpu.d.ts +1 -87
  673. package/src/inference/pipelines/text/logits/cpu.js +8 -95
  674. package/src/inference/pipelines/text/logits/gpu-executor.js +368 -0
  675. package/src/inference/pipelines/text/logits/gpu.d.ts +27 -0
  676. package/src/inference/pipelines/text/logits/gpu.js +116 -579
  677. package/src/inference/pipelines/text/logits/index.d.ts +2 -1
  678. package/src/inference/pipelines/text/logits/index.js +62 -102
  679. package/src/inference/pipelines/text/logits/output-transform.js +54 -0
  680. package/src/inference/pipelines/text/logits/plan.js +184 -0
  681. package/src/inference/pipelines/text/logits/readback.d.ts +6 -0
  682. package/src/inference/pipelines/text/logits/readback.js +9 -0
  683. package/src/inference/pipelines/text/logits/scale-policy.js +20 -0
  684. package/src/inference/pipelines/text/logits/types.d.ts +1 -0
  685. package/src/inference/pipelines/text/lora-apply.js +12 -4
  686. package/src/inference/pipelines/text/lora-types.d.ts +12 -0
  687. package/src/inference/pipelines/text/modality-token-contract.js +115 -0
  688. package/src/inference/pipelines/text/model-load.js +0 -20
  689. package/src/inference/pipelines/text/moe/expert-loading.js +9 -0
  690. package/src/inference/pipelines/text/moe/gpu-executor.js +513 -0
  691. package/src/inference/pipelines/text/moe/plan.js +47 -0
  692. package/src/inference/pipelines/text/moe-cpu.js +9 -135
  693. package/src/inference/pipelines/text/moe-gpu.js +21 -545
  694. package/src/inference/pipelines/text/normalization-contract.js +17 -0
  695. package/src/inference/pipelines/text/per-layer/materialize.js +736 -0
  696. package/src/inference/pipelines/text/per-layer/plan.js +362 -0
  697. package/src/inference/pipelines/text/per-layer-inputs.js +4 -1037
  698. package/src/inference/pipelines/text/pipeline-load-timing.js +77 -0
  699. package/src/inference/pipelines/text/probes.js +20 -0
  700. package/src/inference/pipelines/text/sampling-config.js +22 -113
  701. package/src/inference/pipelines/text/sampling.d.ts +9 -1
  702. package/src/inference/pipelines/text/sampling.js +18 -375
  703. package/src/inference/pipelines/text/state.d.ts +8 -0
  704. package/src/inference/pipelines/text/state.js +1 -0
  705. package/src/inference/pipelines/text/tsir-fixture-writer.js +88 -16
  706. package/src/inference/pipelines/text/types.d.ts +7 -3
  707. package/src/inference/pipelines/text/weight-loading-config.js +194 -0
  708. package/src/inference/pipelines/text.d.ts +2 -2
  709. package/src/inference/pipelines/text.js +46 -1633
  710. package/src/inference/pipelines/video/frame-sampler.js +0 -13
  711. package/src/inference/pipelines/video/index.js +0 -21
  712. package/src/inference/pipelines/vision/attention.js +59 -0
  713. package/src/inference/pipelines/vision/encoder.js +254 -369
  714. package/src/inference/pipelines/vision/gemma4.js +204 -318
  715. package/src/inference/pipelines/vision/glmocr-preprocess.js +273 -0
  716. package/src/inference/pipelines/vision/glmocr-rope.js +106 -0
  717. package/src/inference/pipelines/vision/glmocr.js +557 -0
  718. package/src/inference/pipelines/vision/image-preprocess.js +0 -27
  719. package/src/inference/pipelines/vision/index.js +8 -27
  720. package/src/inference/pipelines/vision/ops.js +11 -33
  721. package/src/inference/pipelines/vision/patch-embed.js +51 -145
  722. package/src/inference/pipelines/vision/spatial-merge.js +78 -0
  723. package/src/inference/pipelines/vision/weight-loading.js +79 -0
  724. package/src/inference/refactor-receipt.js +2 -2
  725. package/src/inference/rerank.js +182 -0
  726. package/src/inference/token-sampling.js +319 -0
  727. package/src/inference/tokenizer.js +1 -1
  728. package/src/inference/tokenizers/behavior-flags.js +6 -3
  729. package/src/inference/tokenizers/bundled/decoders.js +133 -0
  730. package/src/inference/tokenizers/bundled/registry.js +216 -0
  731. package/src/inference/tokenizers/bundled.js +23 -353
  732. package/src/loader/doppler-loader.d.ts +4 -0
  733. package/src/loader/doppler-loader.js +119 -930
  734. package/src/loader/embedding-limit-preflight.js +11 -4
  735. package/src/loader/embedding-loader.js +1 -3
  736. package/src/loader/gpu-tensor-materialization.js +20 -0
  737. package/src/loader/layer-loader.js +15 -119
  738. package/src/loader/layer-tensor-aliases.js +99 -0
  739. package/src/loader/layer-weight-shape.js +29 -0
  740. package/src/loader/load-timing.js +64 -0
  741. package/src/loader/loader-types.d.ts +3 -0
  742. package/src/loader/model-executor.js +819 -0
  743. package/src/loader/model-load-validation.js +15 -0
  744. package/src/loader/quantization-constants.d.ts +11 -29
  745. package/src/loader/quantization-constants.js +1 -2
  746. package/src/loader/shard-resolver.js +0 -14
  747. package/src/loader/tensor-load-error.js +14 -0
  748. package/src/loader/tensor-shard-indices.js +8 -0
  749. package/src/loader/tensors/bf16-loader.js +125 -0
  750. package/src/loader/tensors/functional-descriptor-loader.js +3 -375
  751. package/src/loader/tensors/load-executor.js +577 -0
  752. package/src/loader/tensors/source-transform/executor.js +123 -0
  753. package/src/loader/tensors/source-transform/plan.js +107 -0
  754. package/src/loader/tensors/source-transform.js +15 -264
  755. package/src/loader/tensors/tensor-loader.d.ts +4 -4
  756. package/src/loader/tensors/tensor-loader.js +7 -653
  757. package/src/memory/buffer-pool.js +7 -29
  758. package/src/rules/capability-transform-resolver.js +80 -0
  759. package/src/rules/execution-contract.d.ts +73 -0
  760. package/src/rules/execution-contract.js +429 -0
  761. package/src/rules/execution-rules-contract-check.js +1 -1
  762. package/src/rules/generated/rule-bundle.json +4305 -0
  763. package/src/rules/inference/capability-transforms.rules.json +3 -3
  764. package/src/rules/layer-pattern-contract-check.js +1 -1
  765. package/src/rules/rule-registry.js +70 -57
  766. package/src/storage/artifact-storage-context.js +2 -2
  767. package/src/storage/backend-selection.js +43 -0
  768. package/src/storage/backends/opfs-store.d.ts +41 -0
  769. package/src/storage/distribution-transport.js +5 -0
  770. package/src/storage/download/integrity.d.ts +33 -0
  771. package/src/storage/download/integrity.js +130 -0
  772. package/src/storage/download/resume-inspection.d.ts +22 -0
  773. package/src/storage/download/resume-inspection.js +134 -0
  774. package/src/storage/download/retry.js +57 -0
  775. package/src/storage/download/state.js +131 -0
  776. package/src/storage/download/transport.js +99 -0
  777. package/src/storage/downloader.js +74 -453
  778. package/src/storage/model-cache.d.ts +1 -0
  779. package/src/storage/model-cache.js +51 -11
  780. package/src/storage/shard-manager.d.ts +5 -0
  781. package/src/storage/shard-manager.js +50 -369
  782. package/src/storage/shards/index.js +60 -0
  783. package/src/storage/shards/integrity.js +225 -0
  784. package/src/storage/shards/lifecycle.js +22 -0
  785. package/src/storage/source-artifact-store.js +14 -4
  786. package/src/tooling/boundary-evidence.d.ts +4 -4
  787. package/src/tooling/boundary-evidence.js +20 -20
  788. package/src/tooling/browser-command-runner.js +13 -6
  789. package/src/tooling/capsule-v2.d.ts +11 -0
  790. package/src/tooling/capsule-v2.js +58 -0
  791. package/src/tooling/capsule.js +20 -0
  792. package/src/tooling/command-api-constants.js +1 -1
  793. package/src/tooling/command-api-family-normalizers.js +163 -5
  794. package/src/tooling/command-api.d.ts +42 -3
  795. package/src/tooling/command-api.js +7 -2
  796. package/src/tooling/command-envelope.js +1 -1
  797. package/src/tooling/{command-api-helpers.js → command-request-normalization.js} +1 -1
  798. package/src/tooling/command-runner.html +49 -1
  799. package/src/tooling/command-validation.js +1 -24
  800. package/src/tooling/conversion-run-timing.js +24 -0
  801. package/src/tooling/diagnose-runner.js +11 -9
  802. package/src/tooling/distribution-shard-transport.js +11 -0
  803. package/src/tooling/execution-cost-ledger.d.ts +76 -2
  804. package/src/tooling/execution-cost-ledger.js +197 -15
  805. package/src/tooling/hf-registry/catalog-io.js +63 -0
  806. package/src/tooling/litert/package.js +863 -0
  807. package/src/tooling/litert-package-runtime.js +3 -849
  808. package/src/tooling/model-capsule-forge.js +478 -0
  809. package/src/tooling/node-browser/execution.d.ts +40 -0
  810. package/src/tooling/node-browser/execution.js +308 -0
  811. package/src/tooling/node-browser/static-server.js +189 -0
  812. package/src/tooling/node-browser/transport.d.ts +80 -0
  813. package/src/tooling/node-browser/transport.js +622 -0
  814. package/src/tooling/node-browser-command-runner.d.ts +18 -78
  815. package/src/tooling/node-browser-command-runner.js +10 -1112
  816. package/src/tooling/node-capsule-artifact-store.js +44 -0
  817. package/src/tooling/node-command-execution.js +237 -0
  818. package/src/tooling/node-command-runner.js +26 -205
  819. package/src/tooling/node-converter/input.js +521 -0
  820. package/src/tooling/node-converter/output.js +244 -0
  821. package/src/tooling/node-converter-chunk-batches.js +21 -0
  822. package/src/tooling/node-converter.js +7 -720
  823. package/src/tooling/node-source-runtime.js +16 -276
  824. package/src/tooling/node-webgpu-provider.v1.json +33 -0
  825. package/src/tooling/node-webgpu.js +178 -453
  826. package/src/tooling/opfs-cache.d.ts +5 -0
  827. package/src/tooling/opfs-cache.js +4 -0
  828. package/src/tooling/production-release.d.ts +19 -0
  829. package/src/tooling/production-release.js +647 -0
  830. package/src/tooling/program-bundle/embedding-reference.js +19 -0
  831. package/src/tooling/program-bundle/materialize.js +324 -0
  832. package/src/tooling/program-bundle/rerank-reference.js +22 -0
  833. package/src/tooling/program-bundle/sequence-reference.js +42 -0
  834. package/src/tooling/program-bundle/validation.js +31 -0
  835. package/src/tooling/program-bundle/wgsl-closure.js +254 -0
  836. package/src/tooling/program-bundle-parity.d.ts +11 -4
  837. package/src/tooling/program-bundle-parity.js +262 -109
  838. package/src/tooling/program-bundle.d.ts +7 -0
  839. package/src/tooling/program-bundle.js +186 -591
  840. package/src/tooling/provider-v1-contract.d.ts +61 -0
  841. package/src/tooling/provider-v1-contract.js +192 -0
  842. package/src/tooling/registered-variant-calibration-job.d.ts +98 -0
  843. package/src/tooling/registered-variant-calibration-job.js +210 -0
  844. package/src/tooling/registered-variant-calibration.d.ts +167 -7
  845. package/src/tooling/registered-variant-calibration.js +57 -7
  846. package/src/tooling/runtime-optimization/candidates.js +664 -0
  847. package/src/tooling/runtime-optimization-campaign.js +166 -0
  848. package/src/tooling/runtime-optimization-index.js +16 -1
  849. package/src/tooling/runtime-optimization.d.ts +44 -0
  850. package/src/tooling/runtime-optimization.js +13 -656
  851. package/src/tooling/source-intake.d.ts +1 -1
  852. package/src/tooling/source-intake.js +34 -17
  853. package/src/tooling/source-package-profiles.js +1 -1
  854. package/src/tooling/source-runtime/bundle-materialize.js +298 -0
  855. package/src/tooling/source-runtime/bundle-plan.js +121 -0
  856. package/src/tooling/source-runtime/node-loader.js +258 -0
  857. package/src/tooling/source-runtime-bundle.js +26 -411
  858. package/src/tooling/source-runtime-converter-config.js +1 -1
  859. package/src/tooling-experimental-exports.js +0 -5
  860. package/src/tooling-exports/evidence.d.ts +14 -3
  861. package/src/tooling-exports/evidence.js +14 -3
  862. package/src/tooling-exports/storage.d.ts +7 -0
  863. package/src/tooling-exports/storage.js +5 -0
  864. package/src/tooling-exports.d.ts +3 -0
  865. package/src/tooling-exports.js +2 -0
  866. package/src/tooling-exports.shared.d.ts +17 -3
  867. package/src/tooling-exports.shared.js +9 -3
  868. package/src/training.d.ts +6 -6
  869. package/src/training.js +4 -4
  870. package/src/version.js +1 -1
  871. package/tests/kernels/browser/kernel-suite.js +512 -0
  872. package/tests/kernels/browser/test-page.js +170 -14
  873. package/tests/kernels/harness/tolerance.js +1 -0
  874. package/tests/kernels/reference/index.js +13 -1
  875. package/tests/kernels/reference/vision.js +160 -0
  876. package/src/converter/quantizer.d.ts +0 -121
  877. package/src/gpu/kernels/backward/utils.d.ts +0 -90
  878. package/src/gpu/kernels/uniform-utils.d.ts +0 -66
  879. package/src/gpu/kernels/utils.d.ts +0 -147
  880. package/src/inference/pipelines/text/debug-utils/utils.js +0 -118
  881. package/src/inference/pipelines/text/logits/utils.d.ts +0 -58
  882. package/src/inference/pipelines/text/moe-cpu-gptoss.js +0 -115
  883. package/src/inference/pipelines/text/moe-helpers.js +0 -21
  884. package/src/rules/converter/execution.rules.json +0 -20
  885. package/src/rules/converter/tensor-roles.rules.json +0 -13
  886. package/src/rules/converter/tokenizer.rules.json +0 -12
  887. package/src/rules/inference/attention.rules.json +0 -69
  888. package/src/rules/inference/config.rules.json +0 -82
  889. package/src/rules/inference/dtype.rules.json +0 -113
  890. package/src/rules/inference/execution.rules.json +0 -195
  891. package/src/rules/inference/ffn.rules.json +0 -123
  892. package/src/rules/inference/layer-pattern.rules.json +0 -16
  893. package/src/rules/inference/layer.rules.json +0 -7
  894. package/src/rules/inference/moe.rules.json +0 -145
  895. package/src/rules/kernels/attention.rules.json +0 -68
  896. package/src/rules/kernels/conv2d.rules.json +0 -6
  897. package/src/rules/kernels/depthwise-conv2d.rules.json +0 -6
  898. package/src/rules/kernels/dequant.rules.json +0 -58
  899. package/src/rules/kernels/energy.rules.json +0 -22
  900. package/src/rules/kernels/fused-ffn.rules.json +0 -15
  901. package/src/rules/kernels/fused-matmul-residual.rules.json +0 -6
  902. package/src/rules/kernels/fused-matmul-rmsnorm.rules.json +0 -8
  903. package/src/rules/kernels/gather.rules.json +0 -93
  904. package/src/rules/kernels/gelu.rules.json +0 -11
  905. package/src/rules/kernels/grouped-pointwise-conv2d.rules.json +0 -6
  906. package/src/rules/kernels/groupnorm.rules.json +0 -10
  907. package/src/rules/kernels/kv_quantize.rules.json +0 -9
  908. package/src/rules/kernels/layernorm.rules.json +0 -6
  909. package/src/rules/kernels/linear-attention.rules.json +0 -6
  910. package/src/rules/kernels/lm-head-argmax.rules.json +0 -10
  911. package/src/rules/kernels/matmul.rules.json +0 -95
  912. package/src/rules/kernels/modulate.rules.json +0 -6
  913. package/src/rules/kernels/moe.rules.gptoss.json +0 -105
  914. package/src/rules/kernels/moe.rules.json +0 -14
  915. package/src/rules/kernels/moe.rules.mixtral.json +0 -75
  916. package/src/rules/kernels/pixel_shuffle.rules.json +0 -6
  917. package/src/rules/kernels/relu.rules.json +0 -6
  918. package/src/rules/kernels/rep-penalty.rules.json +0 -6
  919. package/src/rules/kernels/repeat-channels.rules.json +0 -6
  920. package/src/rules/kernels/residual.rules.json +0 -12
  921. package/src/rules/kernels/rmsnorm-qk.rules.json +0 -6
  922. package/src/rules/kernels/rmsnorm.rules.json +0 -46
  923. package/src/rules/kernels/rope-qk.rules.json +0 -6
  924. package/src/rules/kernels/rope.rules.json +0 -6
  925. package/src/rules/kernels/sample.rules.json +0 -6
  926. package/src/rules/kernels/scale.rules.json +0 -8
  927. package/src/rules/kernels/silu.rules.json +0 -22
  928. package/src/rules/kernels/softmax.rules.json +0 -25
  929. package/src/rules/kernels/split-qg.rules.json +0 -6
  930. package/src/rules/kernels/split-qkv.rules.json +0 -6
  931. package/src/rules/kernels/upsample2d.rules.json +0 -6
  932. package/src/rules/loader/tensor-loader.rules.json +0 -42
  933. package/src/rules/loader/weights.rules.json +0 -42
  934. package/src/rules/tooling/command-runtime.rules.json +0 -65
  935. package/src/tooling/hf-registry-utils.js +0 -395
  936. /package/src/converter/{tokenizer-utils.js → tokenizer-special-tokens.js} +0 -0
  937. /package/src/{inference/pipelines/text/bdpa-steamroller.js → experimental/bdpa/steamroller.js} +0 -0
  938. /package/src/{inference/pipelines → experimental}/diffusion/scheduler.js +0 -0
  939. /package/src/{inference/pipelines → experimental}/energy/pipeline.d.ts +0 -0
  940. /package/src/{inference/pipelines → experimental}/energy/quintel.d.ts +0 -0
  941. /package/src/{inference/pipelines → experimental}/energy/quintel.js +0 -0
  942. /package/src/{utils → formats}/array-buffer.js +0 -0
  943. /package/src/{utils → formats}/canonical-hash.js +0 -0
  944. /package/src/{utils → formats}/clone-json.js +0 -0
  945. /package/src/{utils → formats}/encode-utf8.js +0 -0
  946. /package/src/{utils → formats}/load-json.js +0 -0
  947. /package/src/{utils → formats}/plain-object.js +0 -0
  948. /package/src/{utils → formats}/sha256.js +0 -0
  949. /package/src/{utils → formats}/stable-sort-object.js +0 -0
  950. /package/src/{gpu/kernels → rules}/rule-matcher.js +0 -0
  951. /package/src/{utils → storage}/hf-resolve-url.js +0 -0
  952. /package/src/{utils → storage}/runtime-env.js +0 -0
  953. /package/src/tooling/{command-runner-shared.js → command-runtime-execution.js} +0 -0
@@ -30,225 +30,17 @@ import {
30
30
  import { watchFinalizedCheckpoints } from './checkpoint-watch.js';
31
31
  import { loadLoRAFromManifest } from '../adapters/lora-loader.js';
32
32
  import { createUploadedTensor } from './tensor-factory.js';
33
- import { stableSortObject } from '../../utils/stable-sort-object.js';
33
+ import { stableSortObject } from '../../formats/stable-sort-object.js';
34
34
  import { LORA_MODULE_ALIASES } from '../../inference/pipelines/text/lora.js';
35
35
  import { loadDistillModelHandle } from './suite.js';
36
36
  import { createDistillStudentRuntimeModelFixture } from './distillation/student-fixture.js';
37
37
  import { f16ToF32Array } from '../../inference/kv-cache/types.js';
38
- import { sha256BytesHex, sha256Hex } from '../../utils/sha256.js';
39
-
40
- const CAUSAL_LM_TEXT_PAIR_RUNNER_KEYS = Object.freeze([
41
- 'gemma-3-270m-it-f16-af32::text-pairs::text_generation',
42
- 'gemma-3-270m-it-q4k-ehf16-af32::text-pairs::text_generation',
43
- 'gemma4-e2b-it::text-pairs::text_generation',
44
- 'gemma-4-e2b-it-q4k-ehf16-af32::text-pairs::text_generation',
45
- 'gemma-4-e2b-it-q4k-ehf16-af32-int4ple::text-pairs::text_generation',
46
- 'qwen-3-5-0-8b-q4k-ehaf16::text-pairs::text_generation',
47
- 'qwen-3-5-2b-q4k-ehaf16::text-pairs::text_generation',
48
- 'qwen-3-5-9b-hf-bf16::text-pairs::text_generation',
49
- 'qwen-3-6-27b-q4k-ehaf16::text-pairs::text_generation',
50
- 'qwen-3-6-27b-q4k-eaf16::text-pairs::text_generation',
51
- ]);
52
-
53
- export const LORA_RUNNER_SUPPORT_CONTRACT = Object.freeze({
54
- supportedBaseModelId: 'training-toy',
55
- supportedDatasetFormat: 'toy_linear_classification_jsonl',
56
- registeredBaseModelIds: Object.freeze([
57
- 'training-toy',
58
- 'gemma-3-270m-it-f16-af32',
59
- 'gemma-3-270m-it-q4k-ehf16-af32',
60
- 'gemma4-e2b-it',
61
- 'gemma-4-e2b-it-q4k-ehf16-af32',
62
- 'gemma-4-e2b-it-q4k-ehf16-af32-int4ple',
63
- 'qwen-3-5-0-8b-q4k-ehaf16',
64
- 'qwen-3-5-2b-q4k-ehaf16',
65
- 'qwen-3-5-9b-hf-bf16',
66
- 'qwen-3-6-27b-q4k-ehaf16',
67
- 'qwen-3-6-27b-q4k-eaf16',
68
- ]),
69
- registeredDatasetFormats: Object.freeze([
70
- 'toy_linear_classification_jsonl',
71
- 'text-pairs',
72
- ]),
73
- implementedRunnerKeys: Object.freeze([
74
- 'training-toy::toy_linear_classification_jsonl::classification',
75
- ...CAUSAL_LM_TEXT_PAIR_RUNNER_KEYS,
76
- ]),
77
- });
78
-
79
- export const LORA_RUNNER_BASE_MODEL_REGISTRY = Object.freeze({
80
- 'training-toy': Object.freeze({
81
- baseModelId: 'training-toy',
82
- family: 'training_fixture',
83
- runnerKind: 'toy_linear_classification',
84
- }),
85
- 'gemma-3-270m-it-f16-af32': Object.freeze({
86
- baseModelId: 'gemma-3-270m-it-f16-af32',
87
- modelRef: 'gemma-3-270m-it-f16-af32',
88
- family: 'gemma3',
89
- runnerKind: 'causal_lm_text_generation',
90
- }),
91
- 'gemma-3-270m-it-q4k-ehf16-af32': Object.freeze({
92
- baseModelId: 'gemma-3-270m-it-q4k-ehf16-af32',
93
- modelRef: 'gemma-3-270m-it-q4k-ehf16-af32',
94
- family: 'gemma3',
95
- runnerKind: 'causal_lm_text_generation',
96
- requiresExternalTrainer: true,
97
- }),
98
- 'gemma4-e2b-it': Object.freeze({
99
- baseModelId: 'gemma4-e2b-it',
100
- modelRef: 'gemma-4-e2b-it-q4k-ehf16-af32',
101
- family: 'gemma4',
102
- runnerKind: 'causal_lm_text_generation',
103
- requiresExternalTrainer: true,
104
- }),
105
- 'gemma-4-e2b-it-q4k-ehf16-af32': Object.freeze({
106
- baseModelId: 'gemma-4-e2b-it-q4k-ehf16-af32',
107
- modelRef: 'gemma-4-e2b-it-q4k-ehf16-af32',
108
- family: 'gemma4',
109
- runnerKind: 'causal_lm_text_generation',
110
- requiresExternalTrainer: true,
111
- }),
112
- 'gemma-4-e2b-it-q4k-ehf16-af32-int4ple': Object.freeze({
113
- baseModelId: 'gemma-4-e2b-it-q4k-ehf16-af32-int4ple',
114
- modelRef: 'gemma-4-e2b-it-q4k-ehf16-af32-int4ple',
115
- family: 'gemma4',
116
- runnerKind: 'causal_lm_text_generation',
117
- requiresExternalTrainer: true,
118
- }),
119
- 'qwen-3-5-0-8b-q4k-ehaf16': Object.freeze({
120
- baseModelId: 'qwen-3-5-0-8b-q4k-ehaf16',
121
- modelRef: 'qwen-3-5-0-8b-q4k-ehaf16',
122
- family: 'qwen3',
123
- runnerKind: 'causal_lm_text_generation',
124
- requiresExternalTrainer: true,
125
- nativeLoraTargets: Object.freeze([
126
- Object.freeze({ module: 'down_proj', layer: 'last' }),
127
- ]),
128
- }),
129
- 'qwen-3-5-2b-q4k-ehaf16': Object.freeze({
130
- baseModelId: 'qwen-3-5-2b-q4k-ehaf16',
131
- modelRef: 'qwen-3-5-2b-q4k-ehaf16',
132
- family: 'qwen3',
133
- runnerKind: 'causal_lm_text_generation',
134
- requiresExternalTrainer: true,
135
- }),
136
- 'qwen-3-5-9b-hf-bf16': Object.freeze({
137
- baseModelId: 'qwen-3-5-9b-hf-bf16',
138
- modelRef: 'Qwen/Qwen3.5-9B',
139
- family: 'qwen3',
140
- runnerKind: 'causal_lm_text_generation',
141
- requiresExternalTrainer: true,
142
- }),
143
- 'qwen-3-6-27b-q4k-ehaf16': Object.freeze({
144
- baseModelId: 'qwen-3-6-27b-q4k-ehaf16',
145
- modelRef: 'qwen-3-6-27b-q4k-ehaf16',
146
- family: 'qwen3',
147
- runnerKind: 'causal_lm_text_generation',
148
- requiresExternalTrainer: true,
149
- }),
150
- 'qwen-3-6-27b-q4k-eaf16': Object.freeze({
151
- baseModelId: 'qwen-3-6-27b-q4k-eaf16',
152
- modelRef: 'qwen-3-6-27b-q4k-eaf16',
153
- family: 'qwen3',
154
- runnerKind: 'causal_lm_text_generation',
155
- requiresExternalTrainer: true,
156
- }),
157
- });
158
-
159
- export const LORA_RUNNER_DATASET_FORMAT_REGISTRY = Object.freeze({
160
- toy_linear_classification_jsonl: Object.freeze({
161
- datasetFormat: 'toy_linear_classification_jsonl',
162
- datasetKind: 'toy_linear_classification',
163
- }),
164
- 'text-pairs': Object.freeze({
165
- datasetFormat: 'text-pairs',
166
- datasetKind: 'causal_lm_text_pairs',
167
- }),
168
- });
169
-
170
- function getPipelineConfig(workload) {
171
- return workload?.pipeline || workload?.lora || {};
172
- }
173
-
174
- function normalizeLoraTargetModules(adapter) {
175
- const rawModules = Array.isArray(adapter?.targetModules) ? adapter.targetModules : [];
176
- const modules = [];
177
- for (const rawModule of rawModules) {
178
- const normalized = String(rawModule || '').trim();
179
- if (!normalized) continue;
180
- const moduleName = LORA_MODULE_ALIASES[normalized] || normalized;
181
- if (!modules.includes(moduleName)) {
182
- modules.push(moduleName);
183
- }
184
- }
185
- if (modules.length === 0) {
186
- throw new Error('Causal-LM LoRA workload requires adapter.targetModules.');
187
- }
188
- return modules;
189
- }
190
-
191
- function getCausalLmBaseModelRef(workload) {
192
- const pipeline = getPipelineConfig(workload);
193
- const baseModel = LORA_RUNNER_BASE_MODEL_REGISTRY[String(workload?.baseModelId || '')] || null;
194
- return String(
195
- pipeline.baseModelRef
196
- || workload.baseModelRef
197
- || workload.studentModelId
198
- || baseModel?.modelRef
199
- || workload.baseModelId
200
- || ''
201
- );
202
- }
203
-
204
- function getRunnerKey(baseModelId, datasetFormat, taskType) {
205
- return `${baseModelId}::${datasetFormat}::${taskType}`;
206
- }
207
-
208
- function isCausalLmLoraWorkload(workload, compatibility = getLoraRunnerCompatibility(workload)) {
209
- return compatibility.observed.baseModelRunnerKind === 'causal_lm_text_generation'
210
- || compatibility.observed.datasetKind === 'causal_lm_text_pairs'
211
- || compatibility.observed.taskType === 'text_generation';
212
- }
213
-
214
- export function getLoraRunnerCompatibility(workload) {
215
- const baseModelId = String(workload?.baseModelId || '');
216
- const pipeline = getPipelineConfig(workload);
217
- const datasetFormat = String(pipeline?.datasetFormat || '');
218
- const taskType = String(pipeline?.taskType || '');
219
- const baseModel = LORA_RUNNER_BASE_MODEL_REGISTRY[baseModelId] || null;
220
- const dataset = LORA_RUNNER_DATASET_FORMAT_REGISTRY[datasetFormat] || null;
221
- const runnerKey = getRunnerKey(baseModelId, datasetFormat, taskType);
222
- const blockedReasons = [];
223
- if (!baseModel) {
224
- blockedReasons.push('base_model_not_registered_for_current_lora_runner');
225
- }
226
- if (!dataset) {
227
- blockedReasons.push('dataset_format_not_supported_by_current_lora_runner');
228
- }
229
- if (baseModel && dataset && !LORA_RUNNER_SUPPORT_CONTRACT.implementedRunnerKeys.includes(runnerKey)) {
230
- blockedReasons.push('runner_combination_not_supported_by_current_lora_runner');
231
- }
232
- return {
233
- schemaVersion: 1,
234
- supported: blockedReasons.length === 0,
235
- runnerContract: LORA_RUNNER_SUPPORT_CONTRACT,
236
- observed: {
237
- baseModelId,
238
- datasetFormat,
239
- taskType,
240
- runnerKey,
241
- baseModelFamily: baseModel?.family || null,
242
- baseModelRunnerKind: baseModel?.runnerKind || null,
243
- requiresExternalTrainer: baseModel?.requiresExternalTrainer === true,
244
- nativeLoraTargets: baseModel?.nativeLoraTargets || Object.freeze([]),
245
- datasetKind: dataset?.datasetKind || null,
246
- registeredBaseModel: Boolean(baseModel),
247
- registeredDatasetFormat: Boolean(dataset),
248
- },
249
- blockedReasons,
250
- };
251
- }
38
+ import { sha256BytesHex, sha256Hex } from '../../formats/sha256.js';
39
+ import { LORA_RUNNER_BASE_MODEL_REGISTRY, finiteMetric, getLoraRunnerCompatibility, getPipelineConfig, isCausalLmLoraWorkload, isObjectRecord, normalizeLoraTargetModules, normalizeProviderEvalReport, preflightCausalLmLoraWorkload } from './lora/plan.js';
40
+ import { buildArtifact, createCausalLmDatasetBatches, evaluateCausalLmLoraModel, evaluateToyLoraModel, exportCausalLmLoraModel, exportProviderCausalLmLoraModel, exportToyLoraModel, loadCausalLmTextPairSamples, loadToyLoraDataset, makeTensorFromFloat32, makeTensorFromUint32, releaseTensor, resolveDatasetPathForLoadedWorkload } from './lora/checkpoint.js';
41
+ import { runInternalCausalLmLoraPipeline } from './lora/execution.js';
42
+ import { buildRunContract, createLoraRunLayout, runProviderCausalLmLoraPipeline } from './lora/recovery.js';
43
+ export { LORA_RUNNER_BASE_MODEL_REGISTRY, LORA_RUNNER_DATASET_FORMAT_REGISTRY, LORA_RUNNER_SUPPORT_CONTRACT, getLoraRunnerCompatibility, preflightCausalLmLoraWorkload } from './lora/plan.js';
252
44
 
253
45
  export function assertLoraRunnerCompatibility(workload) {
254
46
  const compatibility = getLoraRunnerCompatibility(workload);
@@ -266,130 +58,10 @@ export function assertLoraRunnerCompatibility(workload) {
266
58
  ].join(' '));
267
59
  }
268
60
 
269
- function summarizeTextPairLengths(rows) {
270
- let minPromptChars = Number.POSITIVE_INFINITY;
271
- let maxPromptChars = 0;
272
- let minCompletionChars = Number.POSITIVE_INFINITY;
273
- let maxCompletionChars = 0;
274
- for (const row of rows) {
275
- const promptChars = row.prompt.length;
276
- const completionChars = row.completion.length;
277
- minPromptChars = Math.min(minPromptChars, promptChars);
278
- maxPromptChars = Math.max(maxPromptChars, promptChars);
279
- minCompletionChars = Math.min(minCompletionChars, completionChars);
280
- maxCompletionChars = Math.max(maxCompletionChars, completionChars);
281
- }
282
- if (!rows.length) {
283
- minPromptChars = 0;
284
- minCompletionChars = 0;
285
- }
286
- return {
287
- minPromptChars,
288
- maxPromptChars,
289
- minCompletionChars,
290
- maxCompletionChars,
291
- };
292
- }
293
-
294
- async function pathIsReadable(filePath) {
295
- try {
296
- await access(filePath);
297
- return true;
298
- } catch {
299
- return false;
300
- }
301
- }
302
-
303
- async function resolveDatasetPathForLoadedWorkload(datasetPath, loadedWorkload) {
304
- const source = String(datasetPath || '');
305
- if (!source || isAbsolute(source) || /^https?:\/\//i.test(source)) {
306
- return source;
307
- }
308
- const candidates = [];
309
- const pushCandidate = (candidate) => {
310
- if (candidate && !candidates.includes(candidate)) {
311
- candidates.push(candidate);
312
- }
313
- };
314
- pushCandidate(resolve(source));
315
- let cursor = loadedWorkload?.absolutePath ? dirname(resolve(String(loadedWorkload.absolutePath))) : null;
316
- while (cursor) {
317
- pushCandidate(join(cursor, source));
318
- const parent = dirname(cursor);
319
- if (parent === cursor) break;
320
- cursor = parent;
321
- }
322
- for (const candidate of candidates) {
323
- if (await pathIsReadable(candidate)) {
324
- return candidate;
325
- }
326
- }
327
- return resolve(source);
328
- }
329
-
330
- export async function preflightCausalLmLoraWorkload(workload, options = {}) {
331
- const compatibility = getLoraRunnerCompatibility(workload);
332
- if (!isCausalLmLoraWorkload(workload, compatibility)) {
333
- throw new Error('preflightCausalLmLoraWorkload requires a causal-LM LoRA workload.');
334
- }
335
- if (!workload?.datasetPath) {
336
- throw new Error('preflightCausalLmLoraWorkload requires workload.datasetPath.');
337
- }
338
- const datasetPath = options.datasetPath || workload.datasetPath;
339
- const dataset = await loadTextPairsDataset(datasetPath, {
340
- fetch: options.fetch,
341
- readFile: options.readFile,
342
- });
343
- if (dataset.rowCount < 1) {
344
- throw new Error(`Causal-LM LoRA dataset ${workload.datasetPath} has no rows.`);
345
- }
346
- const pipeline = getPipelineConfig(workload);
347
- const adapter = pipeline.adapter || {};
348
- return {
349
- schemaVersion: 1,
350
- supported: compatibility.supported,
351
- runnerKey: compatibility.observed.runnerKey,
352
- baseModelId: compatibility.observed.baseModelId,
353
- baseModelFamily: compatibility.observed.baseModelFamily,
354
- datasetPath: dataset.absolutePath,
355
- datasetFormat: compatibility.observed.datasetFormat,
356
- taskType: compatibility.observed.taskType,
357
- rowCount: dataset.rowCount,
358
- firstRowId: dataset.rows[0]?.id || null,
359
- lastRowId: dataset.rows[dataset.rows.length - 1]?.id || null,
360
- textPairFields: {
361
- prompt: dataset.rows[0]?.promptField || null,
362
- completion: dataset.rows[0]?.completionField || null,
363
- },
364
- textPairLengths: summarizeTextPairLengths(dataset.rows),
365
- adapter: {
366
- rank: Number(adapter.rank ?? 0),
367
- alpha: Number(adapter.alpha ?? 0),
368
- targetModules: normalizeLoraTargetModules(adapter),
369
- },
370
- blockedReasons: compatibility.blockedReasons.slice(),
371
- };
372
- }
373
-
374
61
  function stableJson(value) {
375
62
  return JSON.stringify(stableSortObject(value));
376
63
  }
377
64
 
378
- function makeTensorFromFloat32(values, shape, label) {
379
- const data = values instanceof Float32Array ? values : new Float32Array(values);
380
- return createUploadedTensor(data, 'f32', shape, label);
381
- }
382
-
383
- function makeTensorFromUint32(values, shape, label) {
384
- const data = values instanceof Uint32Array ? values : new Uint32Array(values);
385
- return createUploadedTensor(data, 'u32', shape, label);
386
- }
387
-
388
- function releaseTensor(tensor) {
389
- if (!tensor?.buffer) return;
390
- releaseBuffer(tensor.buffer);
391
- }
392
-
393
65
  function createToyLoraModel(workload) {
394
66
  const targetModule = workload.pipeline.adapter.targetModules[0];
395
67
  if (!targetModule) {
@@ -449,52 +121,6 @@ function createToyLoraModel(workload) {
449
121
  };
450
122
  }
451
123
 
452
- function normalizeToyRow(record, index) {
453
- if (!record || typeof record !== 'object' || Array.isArray(record)) {
454
- throw new Error(`LoRA toy dataset row ${index + 1} must be an object.`);
455
- }
456
- const values = Array.isArray(record.input)
457
- ? record.input
458
- : (Array.isArray(record.features) ? record.features : null);
459
- if (!Array.isArray(values) || values.length !== 3) {
460
- throw new Error(`LoRA toy dataset row ${index + 1} requires input[3].`);
461
- }
462
- const input = values.map((value, valueIndex) => {
463
- const parsed = Number(value);
464
- if (!Number.isFinite(parsed)) {
465
- throw new Error(`LoRA toy dataset row ${index + 1} input[${valueIndex}] must be finite.`);
466
- }
467
- return parsed;
468
- });
469
- const target = Number(record.target ?? record.label);
470
- if (!Number.isInteger(target) || target < 0 || target > 1) {
471
- throw new Error(`LoRA toy dataset row ${index + 1} requires integer target 0 or 1.`);
472
- }
473
- return {
474
- id: String(record.id || `row-${index + 1}`),
475
- input,
476
- target,
477
- };
478
- }
479
-
480
- async function loadToyLoraDataset(datasetPath) {
481
- const absolutePath = resolve(String(datasetPath));
482
- const raw = await readFile(absolutePath, 'utf8');
483
- const rows = absolutePath.endsWith('.json')
484
- ? JSON.parse(raw)
485
- : parseJsonl(raw);
486
- if (!Array.isArray(rows)) {
487
- throw new Error(`LoRA dataset "${absolutePath}" must be a JSON array or JSONL file.`);
488
- }
489
- const normalizedRows = rows.map((row, index) => normalizeToyRow(row, index));
490
- return {
491
- absolutePath,
492
- raw,
493
- rows: normalizedRows,
494
- datasetHash: hashArtifactPayload({ rows: normalizedRows }),
495
- };
496
- }
497
-
498
124
  function createToyDatasetBatches(rows, batchSize) {
499
125
  return {
500
126
  async *batches() {
@@ -533,1579 +159,11 @@ function createToyDatasetBatches(rows, batchSize) {
533
159
  };
534
160
  }
535
161
 
536
- function createLossGradient(loss, lossScale) {
537
- const lossElements = loss.shape.reduce((acc, value) => acc * value, 1);
538
- const gradData = new Float32Array(lossElements);
539
- gradData.fill(lossScale);
540
- return createUploadedTensor(gradData, 'f32', loss.shape, 'lora_causal_lm_loss_grad');
541
- }
542
-
543
- function createCausalLmTrainingObjective() {
544
- return {
545
- name: 'causal_lm_cross_entropy',
546
- async forward({ model, batch, tape }) {
547
- if (typeof model.forwardCausalLm === 'function') {
548
- return model.forwardCausalLm(batch, tape);
549
- }
550
- const logits = await model.forward(batch.input, tape);
551
- return { logits };
552
- },
553
- async computeLoss({ batch, config, tape, forwardState }) {
554
- const loss = await crossEntropyLoss(forwardState.logits, batch.targets, config, tape);
555
- return {
556
- loss,
557
- components: {
558
- supervised_token_count: batch.supervisedTokenCount,
559
- ignored_target_count: batch.ignoredTargetCount,
560
- },
561
- };
562
- },
563
- backwardTargets({ batch, loss, lossScale }) {
564
- if (!Number.isInteger(batch.supervisedTokenCount) || batch.supervisedTokenCount < 1) {
565
- throw new Error('Causal-LM training batch requires supervisedTokenCount >= 1.');
566
- }
567
- return createLossGradient(loss, lossScale / batch.supervisedTokenCount);
568
- },
569
- };
570
- }
571
-
572
- function createCausalLmDatasetBatches(samples) {
573
- return {
574
- async *batches() {
575
- for (const sample of samples) {
576
- const inputTensor = makeTensorFromUint32(
577
- sample.inputIds,
578
- [sample.inputIds.length],
579
- `lora_causal_lm_input_${sample.id}`
580
- );
581
- const targetTensor = makeTensorFromUint32(
582
- sample.targetIds,
583
- [sample.targetIds.length],
584
- `lora_causal_lm_target_${sample.id}`
585
- );
586
- try {
587
- yield {
588
- id: sample.id,
589
- input: inputTensor,
590
- targets: targetTensor,
591
- prompt: sample.prompt,
592
- completion: sample.completion,
593
- supervisedTokenCount: sample.supervisedTokenCount,
594
- ignoredTargetCount: sample.ignoredTargetCount,
595
- };
596
- } finally {
597
- releaseTensor(inputTensor);
598
- releaseTensor(targetTensor);
599
- }
600
- }
601
- },
602
- };
603
- }
604
-
605
- async function loadCausalLmTextPairSamples(workload, datasetPath, tokenizer) {
606
- const dataset = await loadTextPairsDataset(datasetPath);
607
- const pipeline = getPipelineConfig(workload);
608
- const maxLength = Math.floor(Number(
609
- pipeline.maxLength
610
- ?? pipeline.sequenceLength
611
- ?? workload.training?.maxLength
612
- ));
613
- if (!Number.isInteger(maxLength) || maxLength < 2) {
614
- throw new Error('Causal-LM LoRA workload requires lora.maxLength or lora.sequenceLength >= 2.');
615
- }
616
- if (typeof pipeline.joinWith !== 'string') {
617
- throw new Error('Causal-LM LoRA workload requires lora.joinWith.');
618
- }
619
- const joinWith = pipeline.joinWith;
620
- const samples = await tokenizeTextPairs(tokenizer, dataset.rows, {
621
- maxLength,
622
- joinWith,
623
- });
624
- if (samples.length < 1) {
625
- throw new Error(`Causal-LM LoRA dataset ${dataset.absolutePath} produced no tokenized samples.`);
626
- }
627
- const tokenizationContract = {
628
- version: 2,
629
- objective: 'completion_only_causal_lm',
630
- truncation: 'preserve_completion_head_tail_prompt',
631
- maxLength,
632
- joinWith,
633
- };
634
- const supervisedTokenCount = samples.reduce(
635
- (sum, sample) => sum + sample.supervisedTokenCount,
636
- 0
637
- );
638
- const ignoredTargetCount = samples.reduce(
639
- (sum, sample) => sum + sample.ignoredTargetCount,
640
- 0
641
- );
642
- const truncatedPromptTokenCount = samples.reduce(
643
- (sum, sample) => sum + sample.truncatedPromptTokenCount,
644
- 0
645
- );
646
- return {
647
- ...dataset,
648
- samples,
649
- datasetHash: hashArtifactPayload({
650
- rows: dataset.rows,
651
- tokenization: tokenizationContract,
652
- }),
653
- tokenization: {
654
- ...tokenizationContract,
655
- sampleCount: samples.length,
656
- supervisedTokenCount,
657
- ignoredTargetCount,
658
- truncatedPromptTokenCount,
659
- truncatedSampleCount: samples.filter(
660
- (sample) => sample.truncatedPromptTokenCount > 0
661
- ).length,
662
- },
663
- };
664
- }
665
-
666
- async function createCausalLmLoraFixture(workload) {
667
- const pipeline = getPipelineConfig(workload);
668
- const adapter = pipeline.adapter || {};
669
- const modelRef = getCausalLmBaseModelRef(workload);
670
- const handle = await loadDistillModelHandle(modelRef, 'lora base', {
671
- runtime: {
672
- shared: {
673
- debug: {
674
- logLevel: {
675
- defaultLogLevel: 'debug',
676
- },
677
- },
678
- },
679
- inference: {
680
- compute: {
681
- activationDtype: 'f32',
682
- keepF32Weights: true,
683
- },
684
- },
685
- },
686
- });
687
- let fixture = null;
688
- try {
689
- fixture = await createDistillStudentRuntimeModelFixture({
690
- training: {
691
- precision: workload.training?.precision || {},
692
- },
693
- }, {
694
- distillRuntime: {
695
- studentPipeline: handle.pipeline,
696
- studentGraphMode: 'transformer_full',
697
- },
698
- studentGraphMode: 'transformer_full',
699
- loraAdapter: {
700
- rank: adapter.rank,
701
- alpha: adapter.alpha,
702
- targetModules: normalizeLoraTargetModules(adapter),
703
- },
704
- });
705
- } catch (error) {
706
- if (handle.pipeline && typeof handle.pipeline.unload === 'function') {
707
- await handle.pipeline.unload();
708
- }
709
- throw error;
710
- }
711
- return {
712
- model: fixture.model,
713
- baseModelRef: modelRef,
714
- baseModelUrl: handle.modelUrl || null,
715
- baseManifest: handle.manifest || null,
716
- tokenizer: handle.pipeline.tokenizer,
717
- cleanup() {
718
- fixture.cleanup();
719
- if (handle.pipeline && typeof handle.pipeline.unload === 'function') {
720
- return handle.pipeline.unload();
721
- }
722
- return undefined;
723
- },
724
- };
725
- }
726
-
727
- function collectProtectedBuffers(model) {
728
- const protectedBuffers = new Set();
729
- const groups = model.paramGroups();
730
- for (const params of Object.values(groups)) {
731
- for (const tensor of params) {
732
- if (tensor?.buffer) {
733
- protectedBuffers.add(tensor.buffer);
734
- }
735
- }
736
- }
737
- return protectedBuffers;
738
- }
739
-
740
- async function captureLoraParameterState(model) {
741
- const entries = typeof model?.loraTensorEntries === 'function'
742
- ? model.loraTensorEntries()
743
- : [];
744
- if (entries.length === 0) {
745
- throw new Error('Causal-LM LoRA parameter receipt requires adapter tensors.');
746
- }
747
- const tensors = [];
748
- for (const entry of [...entries].sort((left, right) => left.name.localeCompare(right.name))) {
749
- const tensor = entry.tensor;
750
- const elementCount = tensor.shape.reduce((product, value) => product * value, 1);
751
- const bytesPerElement = tensor.dtype === 'f16' ? 2 : 4;
752
- const raw = await readBuffer(tensor.buffer, elementCount * bytesPerElement);
753
- const values = tensor.dtype === 'f16'
754
- ? f16ToF32Array(new Uint16Array(raw))
755
- : new Float32Array(raw);
756
- let nonzeroCount = 0;
757
- let sumSquares = 0;
758
- let maxAbs = 0;
759
- for (const value of values) {
760
- if (value !== 0) nonzeroCount += 1;
761
- sumSquares += value * value;
762
- maxAbs = Math.max(maxAbs, Math.abs(value));
763
- }
764
- tensors.push({
765
- name: entry.name,
766
- dtype: tensor.dtype,
767
- shape: [...tensor.shape],
768
- elementCount,
769
- hash: sha256BytesHex(new Uint8Array(raw)),
770
- nonzeroCount,
771
- maxAbs,
772
- l2Norm: Math.sqrt(sumSquares),
773
- values,
774
- });
775
- }
776
- return {
777
- aggregateHash: sha256Hex(tensors.map((tensor) => `${tensor.name}\0${tensor.hash}\n`).join('')),
778
- tensors,
779
- };
780
- }
781
-
782
- function buildLoraParameterReceipt(initial, final) {
783
- const initialByName = new Map(initial.tensors.map((tensor) => [tensor.name, tensor]));
784
- let totalSquaredDelta = 0;
785
- const tensors = final.tensors.map((finalTensor) => {
786
- const initialTensor = initialByName.get(finalTensor.name);
787
- if (!initialTensor || initialTensor.elementCount !== finalTensor.elementCount) {
788
- throw new Error(`LoRA parameter receipt shape mismatch for ${finalTensor.name}.`);
789
- }
790
- let squaredDelta = 0;
791
- let maxAbsDelta = 0;
792
- for (let index = 0; index < finalTensor.values.length; index += 1) {
793
- const delta = finalTensor.values[index] - initialTensor.values[index];
794
- squaredDelta += delta * delta;
795
- maxAbsDelta = Math.max(maxAbsDelta, Math.abs(delta));
796
- }
797
- totalSquaredDelta += squaredDelta;
798
- return {
799
- name: finalTensor.name,
800
- dtype: finalTensor.dtype,
801
- shape: finalTensor.shape,
802
- elementCount: finalTensor.elementCount,
803
- initialHash: initialTensor.hash,
804
- finalHash: finalTensor.hash,
805
- changed: initialTensor.hash !== finalTensor.hash,
806
- initialNonzeroCount: initialTensor.nonzeroCount,
807
- finalNonzeroCount: finalTensor.nonzeroCount,
808
- l2Delta: Math.sqrt(squaredDelta),
809
- maxAbsDelta,
810
- };
811
- });
812
- return {
813
- tensorCount: tensors.length,
814
- changedTensorCount: tensors.filter((tensor) => tensor.changed).length,
815
- nonzeroFinalTensorCount: tensors.filter((tensor) => tensor.finalNonzeroCount > 0).length,
816
- initialAggregateHash: initial.aggregateHash,
817
- finalAggregateHash: final.aggregateHash,
818
- aggregateChanged: initial.aggregateHash !== final.aggregateHash,
819
- l2Delta: Math.sqrt(totalSquaredDelta),
820
- tensors,
821
- };
822
- }
823
-
824
- function disposeTapeOutputs(tape, protectedBuffers = new Set()) {
825
- if (!Array.isArray(tape?.records)) return;
826
- const released = new Set();
827
- for (const record of tape.records) {
828
- const output = record?.output;
829
- if (output?.buffer && !protectedBuffers.has(output.buffer) && !released.has(output.buffer)) {
830
- released.add(output.buffer);
831
- releaseBuffer(output.buffer);
832
- }
833
- }
834
- }
835
-
836
- function argmax(values) {
837
- let bestIndex = 0;
838
- let bestValue = Number.NEGATIVE_INFINITY;
839
- for (let index = 0; index < values.length; index += 1) {
840
- const value = Number.isFinite(values[index]) ? values[index] : Number.NEGATIVE_INFINITY;
841
- if (value > bestValue) {
842
- bestValue = value;
843
- bestIndex = index;
844
- }
845
- }
846
- return bestIndex;
847
- }
848
-
849
- async function evaluateToyLoraModel(workload, model, dataset, layout = null, checkpointMeta = {}) {
850
- const protectedBuffers = collectProtectedBuffers(model);
851
- const evalReports = [];
852
- const evalDatasets = Array.isArray(workload.evalDatasets) ? workload.evalDatasets : [];
853
- for (const evalDataset of evalDatasets) {
854
- if (evalDataset.evalKind !== 'classification' && evalDataset.evalKind !== 'text_generation') {
855
- throw new Error(`LoRA eval currently supports classification/text_generation only, got "${evalDataset.evalKind}".`);
856
- }
857
- const evalDatasetMaterialized = evalDataset.datasetPath === dataset.absolutePath
858
- ? dataset
859
- : await loadToyLoraDataset(evalDataset.datasetPath);
860
- const rows = evalDatasetMaterialized.rows;
861
- const predictions = [];
862
- const labels = [];
863
- for (const row of rows) {
864
- const tape = new AutogradTape(loadBackwardRegistry());
865
- const inputTensor = makeTensorFromFloat32(row.input, [1, 3], 'lora_eval_input');
866
- let logits = null;
867
- try {
868
- logits = await model.forward(inputTensor, tape);
869
- const logitsData = new Float32Array(await readBuffer(logits.buffer));
870
- predictions.push(String(argmax(logitsData)));
871
- labels.push(String(row.target));
872
- } finally {
873
- releaseTensor(inputTensor);
874
- if (logits?.buffer && !protectedBuffers.has(logits.buffer)) {
875
- releaseBuffer(logits.buffer);
876
- }
877
- disposeTapeOutputs(tape, protectedBuffers);
878
- }
879
- }
880
- const metrics = computeEvalMetrics('classification', predictions, labels, {});
881
- const reportPayload = {
882
- artifactType: 'training_eval_report',
883
- schemaVersion: 1,
884
- generatedAt: new Date().toISOString(),
885
- workloadId: workload.id,
886
- workloadPath: checkpointMeta.workloadPath || null,
887
- workloadSha256: checkpointMeta.workloadSha256 || null,
888
- configHash: checkpointMeta.configHash || workload.configHash,
889
- datasetPath: evalDataset.datasetPath,
890
- datasetHash: evalDatasetMaterialized.datasetHash,
891
- baseModelId: workload.baseModelId,
892
- stage: 'lora',
893
- checkpointStep: checkpointMeta.checkpointStep ?? null,
894
- evalDatasetId: evalDataset.id,
895
- metrics,
896
- primaryMetric: metrics.primaryMetric,
897
- primaryScore: metrics.primaryScore,
898
- accuracy: metrics.accuracy?.score ?? null,
899
- };
900
- const reportFile = layout
901
- ? await writeJsonArtifact(
902
- join(layout.eval, `${checkpointMeta.checkpointId || 'checkpoint'}__${evalDataset.id}.json`),
903
- reportPayload
904
- )
905
- : null;
906
- evalReports.push({
907
- ...reportPayload,
908
- reportPath: reportFile?.path || null,
909
- });
910
- }
911
- return evalReports;
912
- }
913
-
914
- function buildRunContract(loadedWorkload) {
915
- return {
916
- artifactType: 'training_run_contract',
917
- schemaVersion: 1,
918
- generatedAt: new Date().toISOString(),
919
- workloadId: loadedWorkload.workload.id,
920
- workloadPath: loadedWorkload.absolutePath,
921
- workloadSha256: loadedWorkload.workloadSha256,
922
- configHash: loadedWorkload.workload.configHash,
923
- claimBoundary: loadedWorkload.workload.claimBoundary,
924
- kind: loadedWorkload.workload.kind,
925
- evalDatasets: loadedWorkload.workload.evalDatasets,
926
- };
927
- }
928
-
929
- function buildArtifact(loadedWorkload, options) {
930
- const workload = loadedWorkload.workload;
931
- const payload = buildArtifactBase({
932
- artifactType: options.artifactType,
933
- reportId: `${options.prefix}_${workload.id}_${options.id}`,
934
- workload,
935
- workloadPath: loadedWorkload.absolutePath,
936
- workloadSha256: loadedWorkload.workloadSha256,
937
- datasetPath: options.datasetPath || workload.datasetPath,
938
- datasetHash: options.datasetHash || null,
939
- baseModelId: workload.baseModelId,
940
- stage: options.stage || 'lora',
941
- checkpointStep: options.checkpointStep ?? null,
942
- parentArtifacts: options.parentArtifacts || [],
943
- runtime: 'node',
944
- surface: 'node',
945
- claimBoundary: workload.claimBoundary,
946
- configHash: options.configHash || workload.configHash,
947
- });
948
- return {
949
- ...payload,
950
- artifactHash: hashArtifactPayload(payload),
951
- };
952
- }
953
-
954
- async function exportToyLoraModel(loadedWorkload, layout, model, checkpointId, checkpointStep, datasetHash) {
955
- const workload = loadedWorkload.workload;
956
- const targetModule = model.targetModule || workload.pipeline.adapter.targetModules[0];
957
- const weightsFilename = `${checkpointId}.adapters.safetensors`;
958
- const exported = await exportLoRAAdapter({
959
- id: workload.pipeline.export?.id || `${workload.id}-${checkpointId}`,
960
- name: workload.pipeline.export?.name || `${workload.id}-${checkpointId}`,
961
- baseModel: workload.baseModelId,
962
- rank: workload.pipeline.adapter.rank,
963
- alpha: workload.pipeline.adapter.alpha,
964
- targetModules: [targetModule],
965
- tensors: [
966
- { name: `layers.0.${targetModule}.lora_a`, tensor: model.adapter.A },
967
- { name: `layers.0.${targetModule}.lora_b`, tensor: model.adapter.B },
968
- ],
969
- weightsFormat: 'safetensors',
970
- weightsPath: weightsFilename,
971
- });
972
- const manifestPath = join(layout.exports, `${checkpointId}.adapter.manifest.json`);
973
- const weightsPath = join(layout.exports, weightsFilename);
974
- if (!exported.weights) {
975
- throw new Error('LoRA safetensors export did not return weights bytes.');
976
- }
977
- await writeFile(weightsPath, new Uint8Array(exported.weights));
978
- await writeFile(manifestPath, exported.json, 'utf8');
979
- await loadLoRAFromManifest(exported.manifest, {
980
- readFile: async (filePath) => readFile(join(layout.exports, filePath)),
981
- });
982
- const artifactPayload = {
983
- ...buildArtifact(loadedWorkload, {
984
- prefix: 'lora_export',
985
- id: checkpointId,
986
- artifactType: 'lora_adapter_manifest',
987
- checkpointStep,
988
- datasetHash,
989
- }),
990
- checkpointId,
991
- manifestPath,
992
- weightsPath,
993
- weightsSha256: exported.weightsSha256 || null,
994
- manifest: exported.manifest,
995
- };
996
- const artifactFile = await writeJsonArtifact(
997
- join(layout.exports, `${checkpointId}.export.json`),
998
- artifactPayload
999
- );
1000
- return {
1001
- checkpointId,
1002
- manifestPath,
1003
- weightsPath,
1004
- weightsSha256: exported.weightsSha256 || null,
1005
- exportPath: artifactFile.path,
1006
- manifest: exported.manifest,
1007
- };
1008
- }
1009
-
1010
- async function exportCausalLmLoraModel(loadedWorkload, layout, fixture, checkpointId, checkpointStep, datasetHash) {
1011
- const workload = loadedWorkload.workload;
1012
- const pipeline = getPipelineConfig(workload);
1013
- const adapter = pipeline.adapter || {};
1014
- const weightsFilename = `${checkpointId}.adapters.safetensors`;
1015
- const tensors = typeof fixture.model.loraTensorEntries === 'function'
1016
- ? fixture.model.loraTensorEntries()
1017
- : [];
1018
- if (!tensors.length) {
1019
- throw new Error('Causal-LM LoRA export requires trained adapter tensors.');
1020
- }
1021
- const exported = await exportLoRAAdapter({
1022
- id: pipeline.export?.id || `${workload.id}-${checkpointId}`,
1023
- name: pipeline.export?.name || `${workload.id}-${checkpointId}`,
1024
- baseModel: workload.baseModelId,
1025
- rank: adapter.rank,
1026
- alpha: adapter.alpha,
1027
- targetModules: normalizeLoraTargetModules(adapter),
1028
- tensors,
1029
- weightsFormat: 'safetensors',
1030
- weightsPath: weightsFilename,
1031
- metadata: {
1032
- baseModelRef: fixture.baseModelRef,
1033
- baseModelUrl: fixture.baseModelUrl,
1034
- datasetFormat: pipeline.datasetFormat,
1035
- taskType: pipeline.taskType,
1036
- },
1037
- });
1038
- const manifestPath = join(layout.exports, `${checkpointId}.adapter.manifest.json`);
1039
- const runtimeManifestPath = join(layout.exports, 'runtime-adapter-manifest.json');
1040
- const weightsPath = join(layout.exports, weightsFilename);
1041
- if (!exported.weights) {
1042
- throw new Error('Causal-LM LoRA safetensors export did not return weights bytes.');
1043
- }
1044
- await writeFile(weightsPath, new Uint8Array(exported.weights));
1045
- await writeFile(manifestPath, exported.json, 'utf8');
1046
- await writeFile(runtimeManifestPath, exported.json, 'utf8');
1047
- await loadLoRAFromManifest(exported.manifest, {
1048
- readFile: async (filePath) => readFile(join(layout.exports, filePath)),
1049
- });
1050
- const artifactPayload = {
1051
- ...buildArtifact(loadedWorkload, {
1052
- prefix: 'lora_export',
1053
- id: checkpointId,
1054
- artifactType: 'lora_adapter_manifest',
1055
- checkpointStep,
1056
- datasetHash,
1057
- }),
1058
- checkpointId,
1059
- manifestPath,
1060
- runtimeManifestPath,
1061
- weightsPath,
1062
- weightsSha256: exported.weightsSha256 || null,
1063
- manifest: exported.manifest,
1064
- };
1065
- const artifactFile = await writeJsonArtifact(
1066
- join(layout.exports, `${checkpointId}.export.json`),
1067
- artifactPayload
1068
- );
1069
- return {
1070
- checkpointId,
1071
- manifestPath,
1072
- runtimeManifestPath,
1073
- weightsPath,
1074
- weightsSha256: exported.weightsSha256 || null,
1075
- exportPath: artifactFile.path,
1076
- manifest: exported.manifest,
1077
- };
1078
- }
1079
-
1080
- async function readSupervisedLossStats(loss, targets, vocabSize) {
1081
- const lossElementCount = loss.shape.reduce((product, value) => product * value, 1);
1082
- const targetElementCount = targets.shape.reduce((product, value) => product * value, 1);
1083
- const [rawLoss, rawTargets] = await Promise.all([
1084
- readBuffer(loss.buffer, lossElementCount * (loss.dtype === 'f16' ? 2 : 4)),
1085
- readBuffer(targets.buffer, targetElementCount * 4),
1086
- ]);
1087
- const data = loss.dtype === 'f16'
1088
- ? f16ToF32Array(new Uint16Array(rawLoss))
1089
- : new Float32Array(rawLoss);
1090
- const targetIds = new Uint32Array(rawTargets);
1091
- if (data.length !== targetIds.length) {
1092
- throw new Error(
1093
- `Causal-LM loss/target length mismatch: ${data.length} losses for ${targetIds.length} targets.`
1094
- );
1095
- }
1096
- let sum = 0;
1097
- let supervisedTokenCount = 0;
1098
- for (let index = 0; index < data.length; index += 1) {
1099
- if (targetIds[index] >= vocabSize) continue;
1100
- const value = data[index];
1101
- if (!Number.isFinite(value)) {
1102
- throw new Error(`Causal-LM loss is non-finite at supervised target ${index}.`);
1103
- }
1104
- sum += value;
1105
- supervisedTokenCount += 1;
1106
- }
1107
- if (supervisedTokenCount < 1) {
1108
- throw new Error('Causal-LM evaluation batch contains no supervised targets.');
1109
- }
1110
- return {
1111
- lossSum: sum,
1112
- meanLoss: sum / supervisedTokenCount,
1113
- supervisedTokenCount,
1114
- ignoredTargetCount: data.length - supervisedTokenCount,
1115
- };
1116
- }
1117
-
1118
- function finiteMetric(value) {
1119
- const parsed = Number(value);
1120
- return Number.isFinite(parsed) ? parsed : null;
1121
- }
1122
-
1123
- function buildLossQualityClaim(evalDataset, meanLoss, baselineReport) {
1124
- const baselineLoss = finiteMetric(baselineReport?.loss ?? baselineReport?.primaryScore);
1125
- const adapterLoss = finiteMetric(meanLoss);
1126
- if (baselineLoss === null || adapterLoss === null) {
1127
- return null;
1128
- }
1129
- const quality = evalDataset.quality || null;
1130
- const absoluteImprovement = baselineLoss - adapterLoss;
1131
- const relativeImprovement = baselineLoss === 0 ? 0 : absoluteImprovement / Math.abs(baselineLoss);
1132
- const minAbsoluteImprovement = finiteMetric(quality?.minAbsoluteImprovement) ?? 0;
1133
- const minRelativeImprovement = finiteMetric(quality?.minRelativeImprovement) ?? 0;
1134
- const improved = absoluteImprovement >= minAbsoluteImprovement
1135
- && relativeImprovement >= minRelativeImprovement;
1136
- return {
1137
- baseline: quality?.baseline || 'base_model',
1138
- metric: 'loss',
1139
- selectionGoal: 'min',
1140
- adapterScore: adapterLoss,
1141
- baselineScore: baselineLoss,
1142
- delta: adapterLoss - baselineLoss,
1143
- absoluteImprovement,
1144
- relativeImprovement,
1145
- minAbsoluteImprovement,
1146
- minRelativeImprovement,
1147
- improved,
1148
- requireImprovement: quality?.requireImprovement === true,
1149
- };
1150
- }
1151
-
1152
- async function computeCausalLmTextPairLosses(workload, fixture, samples, protectedBuffers) {
1153
- const sampleLosses = [];
1154
- let lossSum = 0;
1155
- let supervisedTokenCount = 0;
1156
- let ignoredTargetCount = 0;
1157
- for await (const resolvedBatch of createCausalLmDatasetBatches(samples).batches()) {
1158
- const tape = new AutogradTape(loadBackwardRegistry());
1159
- let loss = null;
1160
- try {
1161
- const { logits } = await fixture.model.forwardCausalLm(resolvedBatch, tape);
1162
- loss = await crossEntropyLoss(logits, resolvedBatch.targets, { training: { precision: workload.training.precision } }, tape);
1163
- const stats = await readSupervisedLossStats(
1164
- loss,
1165
- resolvedBatch.targets,
1166
- logits.shape.at(-1)
1167
- );
1168
- sampleLosses.push(stats.meanLoss);
1169
- lossSum += stats.lossSum;
1170
- supervisedTokenCount += stats.supervisedTokenCount;
1171
- ignoredTargetCount += stats.ignoredTargetCount;
1172
- } finally {
1173
- disposeTapeOutputs(tape, protectedBuffers);
1174
- }
1175
- }
1176
- return {
1177
- sampleLosses,
1178
- sampleCount: sampleLosses.length,
1179
- lossSum,
1180
- supervisedTokenCount,
1181
- ignoredTargetCount,
1182
- meanLoss: supervisedTokenCount > 0 ? lossSum / supervisedTokenCount : null,
1183
- };
1184
- }
1185
-
1186
- async function evaluateCausalLmLoraModel(workload, fixture, dataset, layout = null, checkpointMeta = {}) {
1187
- const protectedBuffers = collectProtectedBuffers(fixture.model);
1188
- const evalReports = [];
1189
- const evalDatasets = Array.isArray(workload.evalDatasets) ? workload.evalDatasets : [];
1190
- for (const evalDataset of evalDatasets) {
1191
- if (evalDataset.evalKind !== 'text_generation') {
1192
- throw new Error(`Causal-LM LoRA eval supports text_generation only, got "${evalDataset.evalKind}".`);
1193
- }
1194
- const evalDatasetPath = await resolveDatasetPathForLoadedWorkload(evalDataset.datasetPath, {
1195
- absolutePath: checkpointMeta.workloadPath || null,
1196
- });
1197
- const evalDatasetMaterialized = evalDataset.datasetPath === dataset.absolutePath
1198
- ? dataset
1199
- : await loadCausalLmTextPairSamples(workload, evalDatasetPath, fixture.tokenizer);
1200
- const lossStats = await computeCausalLmTextPairLosses(
1201
- workload,
1202
- fixture,
1203
- evalDatasetMaterialized.samples,
1204
- protectedBuffers
1205
- );
1206
- const meanLoss = lossStats.meanLoss;
1207
- const baselineReports = checkpointMeta.baselineReportsByEvalDatasetId || {};
1208
- const baselineReport = baselineReports[evalDataset.id] || null;
1209
- const qualityClaim = buildLossQualityClaim(evalDataset, meanLoss, baselineReport);
1210
- const reportPayload = {
1211
- artifactType: 'training_eval_report',
1212
- schemaVersion: 1,
1213
- generatedAt: new Date().toISOString(),
1214
- workloadId: workload.id,
1215
- workloadPath: checkpointMeta.workloadPath || null,
1216
- workloadSha256: checkpointMeta.workloadSha256 || null,
1217
- configHash: checkpointMeta.configHash || workload.configHash,
1218
- datasetPath: evalDataset.datasetPath,
1219
- datasetHash: evalDatasetMaterialized.datasetHash,
1220
- baseModelId: workload.baseModelId,
1221
- baseModelRef: fixture.baseModelRef,
1222
- stage: checkpointMeta.stage || 'lora',
1223
- checkpointStep: checkpointMeta.checkpointStep ?? null,
1224
- checkpointId: checkpointMeta.checkpointId || null,
1225
- evalDatasetId: evalDataset.id,
1226
- metrics: {
1227
- loss: {
1228
- score: meanLoss,
1229
- samples: lossStats.sampleCount,
1230
- supervisedTokens: lossStats.supervisedTokenCount,
1231
- ignoredTargets: lossStats.ignoredTargetCount,
1232
- },
1233
- },
1234
- primaryMetric: 'loss',
1235
- primaryScore: meanLoss,
1236
- loss: meanLoss,
1237
- baseline: baselineReport
1238
- ? {
1239
- checkpointId: baselineReport.checkpointId || null,
1240
- stage: baselineReport.stage || null,
1241
- primaryMetric: baselineReport.primaryMetric || null,
1242
- primaryScore: baselineReport.primaryScore ?? null,
1243
- loss: baselineReport.loss ?? null,
1244
- reportPath: baselineReport.reportPath || null,
1245
- }
1246
- : null,
1247
- qualityClaim,
1248
- };
1249
- const reportFile = layout
1250
- ? await writeJsonArtifact(
1251
- join(layout.eval, `${checkpointMeta.checkpointId || 'checkpoint'}__${evalDataset.id}.json`),
1252
- reportPayload
1253
- )
1254
- : null;
1255
- evalReports.push({
1256
- ...reportPayload,
1257
- reportPath: reportFile?.path || null,
1258
- });
1259
- }
1260
- return evalReports;
1261
- }
1262
-
1263
- function getCausalLmFreezeConfig(workload) {
1264
- const pipeline = getPipelineConfig(workload);
1265
- const freeze = pipeline.freeze;
1266
- if (!freeze || typeof freeze !== 'object' || Array.isArray(freeze)) {
1267
- throw new Error('Causal-LM LoRA workload requires lora.freeze.');
1268
- }
1269
- return {
1270
- encoder: freeze.encoder === true,
1271
- prior: freeze.prior === true,
1272
- decoder: freeze.decoder === true,
1273
- base: freeze.base === true,
1274
- lora: freeze.lora === true,
1275
- };
1276
- }
1277
-
1278
- function createLoraRunnerTrainingConfig(workload, freeze) {
1279
- return {
1280
- training: {
1281
- enabled: true,
1282
- optimizer: {
1283
- type: workload.training.optimizer.type,
1284
- lr: workload.training.optimizer.lr,
1285
- beta1: workload.training.optimizer.beta1,
1286
- beta2: workload.training.optimizer.beta2,
1287
- eps: workload.training.optimizer.eps,
1288
- weightDecay: workload.training.optimizer.weightDecay,
1289
- scheduler: workload.training.optimizer.scheduler,
1290
- },
1291
- gradient: {
1292
- maxNorm: workload.training.gradientClipping.maxNorm,
1293
- },
1294
- precision: workload.training.precision,
1295
- lossScaling: { enabled: false },
1296
- distill: {
1297
- enabled: false,
1298
- stage: 'stage_a',
1299
- teacherModelId: null,
1300
- studentModelId: null,
1301
- datasetId: null,
1302
- datasetPath: null,
1303
- languagePair: null,
1304
- sourceLangs: null,
1305
- targetLangs: null,
1306
- pairAllowlist: null,
1307
- strictPairContract: false,
1308
- shardIndex: null,
1309
- shardCount: null,
1310
- resumeFrom: null,
1311
- artifactDir: null,
1312
- stageAArtifact: null,
1313
- stageAArtifactHash: null,
1314
- temperature: 1,
1315
- alphaKd: 1,
1316
- alphaCe: 0,
1317
- allowHintFallback: false,
1318
- tripletMargin: 0.2,
1319
- studentGraphMode: 'transformer_full',
1320
- freeze,
1321
- },
1322
- ul: {
1323
- enabled: false,
1324
- stage: 'stage1_joint',
1325
- stage1Artifact: null,
1326
- stage1ArtifactHash: null,
1327
- artifactDir: null,
1328
- lambda0: 5,
1329
- seed: workload.seed,
1330
- noiseSchedule: { name: 'linear', minSigma: 0.1, maxSigma: 1, steps: 1 },
1331
- priorAlignment: { enabled: false, weight: 1 },
1332
- decoderSigmoidWeight: { enabled: false, maxWeight: 1 },
1333
- lossWeights: { prior: 1, decoder: 1, recon: 1 },
1334
- freeze: null,
1335
- },
1336
- },
1337
- };
1338
- }
1339
-
1340
- function createLoraOptimizer(workload) {
1341
- return new AdamOptimizer({
1342
- training: {
1343
- optimizer: {
1344
- type: workload.training.optimizer.type,
1345
- lr: workload.training.optimizer.lr,
1346
- beta1: workload.training.optimizer.beta1,
1347
- beta2: workload.training.optimizer.beta2,
1348
- eps: workload.training.optimizer.eps,
1349
- weightDecay: workload.training.optimizer.weightDecay,
1350
- scheduler: workload.training.optimizer.scheduler,
1351
- },
1352
- gradient: {
1353
- maxNorm: workload.training.gradientClipping.maxNorm,
1354
- },
1355
- precision: workload.training.precision,
1356
- },
1357
- });
1358
- }
1359
-
1360
- async function createLoraRunLayout(options, workload) {
1361
- const layout = options.runRoot
1362
- ? {
1363
- runRoot: resolve(String(options.runRoot)),
1364
- logs: join(resolve(String(options.runRoot)), 'logs'),
1365
- checkpoints: join(resolve(String(options.runRoot)), 'checkpoints'),
1366
- eval: join(resolve(String(options.runRoot)), 'eval'),
1367
- scoreboard: join(resolve(String(options.runRoot)), 'scoreboard'),
1368
- exports: join(resolve(String(options.runRoot)), 'exports'),
1369
- compare: join(resolve(String(options.runRoot)), 'compare'),
1370
- qualityGate: join(resolve(String(options.runRoot)), 'quality-gate'),
1371
- }
1372
- : await createTrainingRunLayout({
1373
- kind: 'lora',
1374
- workloadId: workload.id,
1375
- timestamp: options.timestamp || null,
1376
- });
1377
- await Promise.all(Object.values(layout).map((dirPath) => mkdir(dirPath, { recursive: true })));
1378
- return layout;
1379
- }
1380
-
1381
- function isObjectRecord(value) {
1382
- return value && typeof value === 'object' && !Array.isArray(value);
1383
- }
1384
-
1385
- function hashTextPairsDataset(dataset) {
1386
- return hashArtifactPayload({ rows: dataset.rows });
1387
- }
1388
-
1389
- function parseCausalLmLoraTensorName(name) {
1390
- const text = String(name || '');
1391
- const match = text.match(/(?:^|\.)layers?\.?(\d+)\.(?:[A-Za-z0-9_]+\.)*([A-Za-z0-9_]+)\.lora_([ab])(?:\.[A-Za-z0-9_]+)?$/i);
1392
- if (!match) return null;
1393
- const layer = Number.parseInt(match[1], 10);
1394
- const rawModule = match[2].toLowerCase();
1395
- const module = LORA_MODULE_ALIASES[rawModule];
1396
- if (!module) return null;
1397
- return {
1398
- layer,
1399
- module,
1400
- kind: match[3].toLowerCase() === 'a' ? 'a' : 'b',
1401
- };
1402
- }
1403
-
1404
- function normalizeTensorShape(value, label) {
1405
- const shape = Array.isArray(value) ? value.map((entry) => Number(entry)) : [];
1406
- if (shape.length !== 2 || shape.some((entry) => !Number.isInteger(entry) || entry < 1)) {
1407
- throw new Error(`${label} requires shape [rows, cols].`);
1408
- }
1409
- return shape;
1410
- }
1411
-
1412
- function normalizeTrainerTensor(entry, index) {
1413
- if (!isObjectRecord(entry)) {
1414
- throw new Error(`Causal-LM trainer tensor ${index + 1} must be an object.`);
1415
- }
1416
- const name = String(entry.name || '').trim();
1417
- if (!name) {
1418
- throw new Error(`Causal-LM trainer tensor ${index + 1} requires name.`);
1419
- }
1420
- const tensor = entry.tensor ?? entry.data ?? entry.values;
1421
- if (tensor === undefined || tensor === null) {
1422
- throw new Error(`Causal-LM trainer tensor "${name}" requires tensor data.`);
1423
- }
1424
- const shape = normalizeTensorShape(entry.shape ?? tensor?.shape, `Causal-LM trainer tensor "${name}"`);
1425
- let normalizedTensor = tensor;
1426
- if (Array.isArray(tensor)) {
1427
- normalizedTensor = new Float32Array(tensor.map((value) => Number(value)));
1428
- }
1429
- if (normalizedTensor instanceof Float32Array && normalizedTensor.length !== shape[0] * shape[1]) {
1430
- throw new Error(
1431
- `Causal-LM trainer tensor "${name}" shape mismatch: expected ${shape[0] * shape[1]}, got ${normalizedTensor.length}.`
1432
- );
1433
- }
1434
- return {
1435
- name,
1436
- shape,
1437
- dtype: entry.dtype || 'f32',
1438
- tensor: normalizedTensor,
1439
- };
1440
- }
1441
-
1442
- function assertCausalLmTensorCoverage(tensors, adapter) {
1443
- const targetModules = normalizeLoraTargetModules(adapter);
1444
- if (!targetModules.length) {
1445
- throw new Error('Causal-LM LoRA export requires at least one target module.');
1446
- }
1447
- const targetSet = new Set(targetModules);
1448
- const layerModules = new Map();
1449
- const seenModules = new Set();
1450
- for (const tensor of tensors) {
1451
- const parsed = parseCausalLmLoraTensorName(tensor.name);
1452
- if (!parsed) {
1453
- throw new Error(`Unrecognized Causal-LM LoRA tensor name: ${tensor.name}`);
1454
- }
1455
- if (!targetSet.has(parsed.module)) {
1456
- throw new Error(
1457
- `Causal-LM LoRA tensor "${tensor.name}" targets module "${parsed.module}" outside workload targetModules.`
1458
- );
1459
- }
1460
- seenModules.add(parsed.module);
1461
- if (!layerModules.has(parsed.layer)) {
1462
- layerModules.set(parsed.layer, new Map());
1463
- }
1464
- const modules = layerModules.get(parsed.layer);
1465
- if (!modules.has(parsed.module)) {
1466
- modules.set(parsed.module, new Set());
1467
- }
1468
- modules.get(parsed.module).add(parsed.kind);
1469
- }
1470
- if (layerModules.size === 0) {
1471
- throw new Error('Causal-LM trainer returned no LoRA layer tensors.');
1472
- }
1473
- for (const moduleName of targetModules) {
1474
- if (!seenModules.has(moduleName)) {
1475
- throw new Error(`Causal-LM trainer returned no tensors for target module "${moduleName}".`);
1476
- }
1477
- }
1478
- for (const [layerIndex, modules] of layerModules.entries()) {
1479
- for (const [moduleName, kinds] of modules.entries()) {
1480
- if (!kinds?.has('a') || !kinds?.has('b')) {
1481
- throw new Error(
1482
- `Causal-LM trainer layer ${layerIndex} module ${moduleName} must include both lora_a and lora_b tensors.`
1483
- );
1484
- }
1485
- }
1486
- }
1487
- }
1488
-
1489
- function normalizeCausalLmTrainerOutput(output, workload) {
1490
- if (!isObjectRecord(output)) {
1491
- throw new Error('Causal-LM trainer must return an object.');
1492
- }
1493
- const rawTensors = Array.isArray(output.tensors)
1494
- ? output.tensors
1495
- : (Array.isArray(output.weights) ? output.weights : null);
1496
- if (!rawTensors || rawTensors.length === 0) {
1497
- throw new Error('Causal-LM trainer must return non-empty tensors.');
1498
- }
1499
- const tensors = rawTensors.map((entry, index) => normalizeTrainerTensor(entry, index));
1500
- assertCausalLmTensorCoverage(tensors, getPipelineConfig(workload).adapter);
1501
- const checkpointStep = Number.isInteger(Number(output.checkpointStep))
1502
- ? Number(output.checkpointStep)
1503
- : Number(workload.training?.steps || 0);
1504
- const checkpointId = String(output.checkpointId || `checkpoint-${String(checkpointStep).padStart(6, '0')}`).trim();
1505
- if (!/^[A-Za-z0-9_-]+$/.test(checkpointId)) {
1506
- throw new Error(`Causal-LM trainer checkpointId "${checkpointId}" must use alphanumeric, underscore, or hyphen characters.`);
1507
- }
1508
- return {
1509
- checkpointId,
1510
- checkpointStep,
1511
- adapterId: String(output.adapterId || '').trim() || null,
1512
- adapterName: String(output.adapterName || '').trim() || null,
1513
- trainerId: String(output.trainerId || '').trim() || null,
1514
- runnerId: String(output.runnerId || '').trim() || null,
1515
- metrics: isObjectRecord(output.metrics) ? output.metrics : {},
1516
- receipts: Array.isArray(output.receipts) ? output.receipts.slice() : [],
1517
- evalReports: Array.isArray(output.evalReports) ? output.evalReports.slice() : [],
1518
- tensors,
1519
- };
1520
- }
1521
-
1522
- function normalizeProviderEvalReport(entry, index, context) {
1523
- if (!isObjectRecord(entry)) {
1524
- throw new Error(`Causal-LM trainer evalReports[${index}] must be an object.`);
1525
- }
1526
- const evalDatasetId = String(entry.evalDatasetId || entry.id || '').trim();
1527
- if (!evalDatasetId) {
1528
- throw new Error(`Causal-LM trainer evalReports[${index}].evalDatasetId is required.`);
1529
- }
1530
- const primaryMetric = String(entry.primaryMetric || 'loss').trim();
1531
- if (!primaryMetric) {
1532
- throw new Error(`Causal-LM trainer evalReports[${index}].primaryMetric is required.`);
1533
- }
1534
- const primaryScore = finiteMetric(entry.primaryScore ?? entry.score ?? entry.loss);
1535
- if (primaryScore === null) {
1536
- throw new Error(`Causal-LM trainer evalReports[${index}].primaryScore must be finite.`);
1537
- }
1538
- const loss = finiteMetric(entry.loss ?? (primaryMetric === 'loss' ? primaryScore : null));
1539
- const baselineScore = finiteMetric(entry.baseline?.primaryScore ?? entry.baseline?.loss);
1540
- const qualityClaim = isObjectRecord(entry.qualityClaim)
1541
- ? entry.qualityClaim
1542
- : (baselineScore === null || loss === null
1543
- ? null
1544
- : {
1545
- baseline: entry.baseline?.stage || 'base_model',
1546
- metric: 'loss',
1547
- selectionGoal: 'min',
1548
- adapterScore: loss,
1549
- baselineScore,
1550
- delta: loss - baselineScore,
1551
- absoluteImprovement: baselineScore - loss,
1552
- relativeImprovement: baselineScore === 0 ? 0 : (baselineScore - loss) / Math.abs(baselineScore),
1553
- minAbsoluteImprovement: 0,
1554
- minRelativeImprovement: 0,
1555
- improved: loss <= baselineScore,
1556
- requireImprovement: false,
1557
- });
1558
- const agentEval = isObjectRecord(entry.agentEval)
1559
- ? entry.agentEval
1560
- : (isObjectRecord(entry.heldoutGate) ? entry.heldoutGate : null);
1561
- return {
1562
- artifactType: 'training_eval_report',
1563
- schemaVersion: 1,
1564
- generatedAt: new Date().toISOString(),
1565
- workloadId: context.workload.id,
1566
- workloadPath: context.loadedWorkload.absolutePath || null,
1567
- workloadSha256: context.loadedWorkload.workloadSha256 || null,
1568
- configHash: context.workload.configHash,
1569
- datasetPath: entry.datasetPath || context.dataset.absolutePath,
1570
- datasetHash: entry.datasetHash || context.datasetHash,
1571
- baseModelId: context.workload.baseModelId,
1572
- baseModelRef: context.preflight.baseModelId,
1573
- stage: entry.stage || 'lora',
1574
- checkpointId: context.checkpointId,
1575
- checkpointStep: context.checkpointStep,
1576
- evalDatasetId,
1577
- metrics: isObjectRecord(entry.metrics)
1578
- ? entry.metrics
1579
- : {
1580
- [primaryMetric]: {
1581
- score: primaryScore,
1582
- samples: finiteMetric(entry.samples) ?? context.dataset.rowCount,
1583
- },
1584
- },
1585
- primaryMetric,
1586
- primaryScore,
1587
- loss,
1588
- baseline: isObjectRecord(entry.baseline) ? entry.baseline : null,
1589
- qualityClaim,
1590
- agentEval,
1591
- };
1592
- }
1593
-
1594
- async function writeProviderEvalReports(context) {
1595
- const reports = [];
1596
- for (let index = 0; index < context.trainerOutput.evalReports.length; index += 1) {
1597
- const report = normalizeProviderEvalReport(context.trainerOutput.evalReports[index], index, context);
1598
- const reportFile = await writeJsonArtifact(
1599
- join(context.layout.eval, `${context.checkpointId}__${report.evalDatasetId}.json`),
1600
- report
1601
- );
1602
- const materialized = {
1603
- ...report,
1604
- reportPath: reportFile.path,
1605
- };
1606
- reports.push(materialized);
1607
- await appendScoreboardRow(context.layout.scoreboard, {
1608
- artifactType: 'training_scoreboard',
1609
- schemaVersion: 1,
1610
- generatedAt: new Date().toISOString(),
1611
- checkpointId: materialized.checkpointId,
1612
- checkpointStep: materialized.checkpointStep,
1613
- stage: materialized.stage,
1614
- evalDatasetId: materialized.evalDatasetId,
1615
- primaryMetric: materialized.primaryMetric,
1616
- primaryScore: materialized.primaryScore,
1617
- loss: materialized.loss,
1618
- qualityClaim: materialized.qualityClaim || null,
1619
- agentEval: materialized.agentEval || null,
1620
- metrics: {
1621
- [materialized.primaryMetric]: materialized.primaryScore,
1622
- primaryScore: materialized.primaryScore,
1623
- agent_heldout_gate: materialized.agentEval?.passRate ?? null,
1624
- },
1625
- }, {
1626
- selectionMetric: context.workload.selectionMetric,
1627
- selectionGoal: context.workload.selectionGoal,
1628
- });
1629
- }
1630
- return reports;
1631
- }
1632
-
1633
- async function resolveCausalLmTrainer(loadedWorkload, options = {}) {
1634
- if (typeof options.causalLmTrainer === 'function') {
1635
- return {
1636
- train: options.causalLmTrainer,
1637
- runnerId: options.causalLmTrainer.runnerId || 'injected_causal_lm_lora_trainer',
1638
- source: 'runLoraPipeline.options.causalLmTrainer',
1639
- exportName: null,
1640
- };
1641
- }
1642
- const trainerConfig = getPipelineConfig(loadedWorkload.workload).trainer;
1643
- if (!trainerConfig) {
1644
- throw new Error(
1645
- 'causal_lm_trainer_not_configured: provide runLoraPipeline({ causalLmTrainer }) or lora.trainer.modulePath in the workload.'
1646
- );
1647
- }
1648
- const modulePath = String(trainerConfig.modulePath || trainerConfig.path || '').trim();
1649
- if (!modulePath) {
1650
- throw new Error('causal_lm_trainer_not_configured: lora.trainer.modulePath is required.');
1651
- }
1652
- const exportName = String(trainerConfig.exportName || 'trainCausalLmLora').trim();
1653
- if (!exportName) {
1654
- throw new Error('causal_lm_trainer_not_configured: lora.trainer.exportName is required.');
1655
- }
1656
- const workloadDir = loadedWorkload.absolutePath
1657
- ? dirname(resolve(String(loadedWorkload.absolutePath)))
1658
- : process.cwd();
1659
- const absoluteModulePath = isAbsolute(modulePath)
1660
- ? modulePath
1661
- : resolve(workloadDir, modulePath);
1662
- const trainerModule = await import(pathToFileURL(absoluteModulePath).href);
1663
- const train = trainerModule[exportName];
1664
- if (typeof train !== 'function') {
1665
- throw new Error(`causal_lm_trainer_not_configured: ${absoluteModulePath} does not export ${exportName}().`);
1666
- }
1667
- return {
1668
- train,
1669
- runnerId: String(trainerConfig.runnerId || exportName).trim(),
1670
- source: absoluteModulePath,
1671
- exportName,
1672
- };
1673
- }
1674
-
1675
- async function exportProviderCausalLmLoraModel(
1676
- loadedWorkload,
1677
- layout,
1678
- trainerOutput,
1679
- checkpointId,
1680
- checkpointStep,
1681
- datasetHash,
1682
- trainerInfo,
1683
- preflight
1684
- ) {
1685
- const workload = loadedWorkload.workload;
1686
- const pipeline = getPipelineConfig(workload);
1687
- const weightsFilename = `${checkpointId}.adapters.safetensors`;
1688
- const exported = await exportLoRAAdapter({
1689
- id: pipeline.export?.id || trainerOutput.adapterId || `${workload.id}-${checkpointId}`,
1690
- name: pipeline.export?.name || trainerOutput.adapterName || `${workload.id}-${checkpointId}`,
1691
- baseModel: workload.baseModelId,
1692
- rank: pipeline.adapter.rank,
1693
- alpha: pipeline.adapter.alpha,
1694
- targetModules: normalizeLoraTargetModules(pipeline.adapter),
1695
- tensors: trainerOutput.tensors,
1696
- weightsFormat: 'safetensors',
1697
- weightsPath: weightsFilename,
1698
- metadata: {
1699
- runnerKind: 'causal_lm_text_generation',
1700
- runnerKey: preflight.runnerKey,
1701
- runnerId: trainerOutput.runnerId || trainerInfo.runnerId,
1702
- trainerId: trainerOutput.trainerId,
1703
- trainerSource: trainerInfo.source,
1704
- datasetHash,
1705
- workloadSha256: loadedWorkload.workloadSha256,
1706
- metrics: trainerOutput.metrics,
1707
- receipts: trainerOutput.receipts,
1708
- },
1709
- });
1710
- if (!exported.weights) {
1711
- throw new Error('Causal-LM LoRA safetensors export did not return weights bytes.');
1712
- }
1713
- const manifestPath = join(layout.exports, `${checkpointId}.adapter.manifest.json`);
1714
- const runtimeManifestPath = join(layout.exports, 'runtime-adapter-manifest.json');
1715
- const weightsPath = join(layout.exports, weightsFilename);
1716
- await writeFile(weightsPath, new Uint8Array(exported.weights));
1717
- await writeFile(manifestPath, exported.json, 'utf8');
1718
- await writeFile(runtimeManifestPath, exported.json, 'utf8');
1719
- await loadLoRAFromManifest(exported.manifest, {
1720
- readFile: async (filePath) => readFile(join(layout.exports, filePath)),
1721
- });
1722
- const artifactPayload = {
1723
- ...buildArtifact(loadedWorkload, {
1724
- prefix: 'lora_export',
1725
- id: checkpointId,
1726
- artifactType: 'lora_adapter_manifest',
1727
- checkpointStep,
1728
- datasetHash,
1729
- }),
1730
- checkpointId,
1731
- manifestPath,
1732
- runtimeManifestPath,
1733
- weightsPath,
1734
- weightsSha256: exported.weightsSha256 || null,
1735
- runnerKey: preflight.runnerKey,
1736
- trainer: {
1737
- runnerId: trainerOutput.runnerId || trainerInfo.runnerId,
1738
- trainerId: trainerOutput.trainerId,
1739
- source: trainerInfo.source,
1740
- exportName: trainerInfo.exportName,
1741
- },
1742
- metrics: trainerOutput.metrics,
1743
- manifest: exported.manifest,
1744
- };
1745
- const artifactFile = await writeJsonArtifact(
1746
- join(layout.exports, `${checkpointId}.export.json`),
1747
- artifactPayload
1748
- );
1749
- return {
1750
- checkpointId,
1751
- manifestPath,
1752
- runtimeManifestPath,
1753
- weightsPath,
1754
- weightsSha256: exported.weightsSha256 || null,
1755
- exportPath: artifactFile.path,
1756
- manifest: exported.manifest,
1757
- };
1758
- }
1759
-
1760
162
  function hasExternalCausalLmTrainer(loadedWorkload, options = {}) {
1761
163
  return typeof options.causalLmTrainer === 'function'
1762
164
  || Boolean(getPipelineConfig(loadedWorkload.workload).trainer);
1763
165
  }
1764
166
 
1765
- function assertInternalCausalLmTrainerAllowed(workload, compatibility) {
1766
- if (compatibility.observed.requiresExternalTrainer !== true) return;
1767
- throw new Error(
1768
- `Causal-LM LoRA base "${workload.baseModelId}" requires lora.trainer.modulePath or runLoraPipeline({ causalLmTrainer }) ` +
1769
- 'because the internal full-graph runner does not train packed q4k base weights.'
1770
- );
1771
- }
1772
-
1773
- async function runInternalCausalLmLoraPipeline(options, layout, compatibility) {
1774
- const loadedWorkload = options.loadedWorkload;
1775
- const workload = loadedWorkload.workload;
1776
- const pipeline = getPipelineConfig(workload);
1777
- const datasetPath = await resolveDatasetPathForLoadedWorkload(workload.datasetPath, loadedWorkload);
1778
- const preflight = await preflightCausalLmLoraWorkload(workload, {
1779
- datasetPath,
1780
- fetch: options.fetch,
1781
- readFile: options.readFile,
1782
- });
1783
- if (!preflight.supported || preflight.blockedReasons.length > 0) {
1784
- throw new Error(`Causal-LM LoRA workload is blocked: ${preflight.blockedReasons.join(',')}`);
1785
- }
1786
- assertInternalCausalLmTrainerAllowed(workload, compatibility);
1787
- if (Math.floor(Number(workload.training.batchSize)) !== 1) {
1788
- throw new Error('Causal-LM LoRA workload requires training.batchSize=1.');
1789
- }
1790
- const freeze = getCausalLmFreezeConfig(workload);
1791
- const fixture = await createCausalLmLoraFixture(workload);
1792
- try {
1793
- const dataset = await loadCausalLmTextPairSamples(workload, datasetPath, fixture.tokenizer);
1794
- const initialParameterState = await captureLoraParameterState(fixture.model);
1795
- const evalReports = [];
1796
- const checkpointArtifacts = [];
1797
- const exports = [];
1798
- const baselineEvalReports = await evaluateCausalLmLoraModel(workload, fixture, dataset, layout, {
1799
- checkpointId: 'base-model',
1800
- checkpointStep: 0,
1801
- stage: 'base_model',
1802
- configHash: workload.configHash,
1803
- workloadPath: loadedWorkload.absolutePath,
1804
- workloadSha256: loadedWorkload.workloadSha256,
1805
- });
1806
- for (const report of baselineEvalReports) {
1807
- evalReports.push(report);
1808
- await appendScoreboardRow(layout.scoreboard, {
1809
- artifactType: 'training_scoreboard',
1810
- schemaVersion: 1,
1811
- generatedAt: new Date().toISOString(),
1812
- checkpointId: report.checkpointId,
1813
- checkpointStep: report.checkpointStep,
1814
- stage: report.stage,
1815
- evalDatasetId: report.evalDatasetId,
1816
- primaryMetric: report.primaryMetric,
1817
- primaryScore: report.primaryScore,
1818
- loss: report.loss,
1819
- metrics: {
1820
- loss: report.loss,
1821
- primaryScore: report.primaryScore,
1822
- },
1823
- }, {
1824
- selectionMetric: workload.selectionMetric,
1825
- selectionGoal: workload.selectionGoal,
1826
- });
1827
- }
1828
- const baselineReportsByEvalDatasetId = Object.fromEntries(
1829
- baselineEvalReports.map((report) => [report.evalDatasetId, report])
1830
- );
1831
- const finalizedCheckpointIds = new Set();
1832
- const runner = new TrainingRunner(createLoraRunnerTrainingConfig(workload, freeze), {
1833
- optimizer: createLoraOptimizer(workload),
1834
- crossEntropyLoss,
1835
- clipGradients,
1836
- trainingObjective: createCausalLmTrainingObjective(),
1837
- onCheckpoint: async (checkpoint) => {
1838
- const checkpointId = `checkpoint-${String(checkpoint.step).padStart(6, '0')}`;
1839
- if (finalizedCheckpointIds.has(checkpointId)) return;
1840
- finalizedCheckpointIds.add(checkpointId);
1841
- const checkpointPayload = {
1842
- ...buildArtifact(loadedWorkload, {
1843
- prefix: 'lora_ckpt',
1844
- id: checkpointId,
1845
- artifactType: 'training_checkpoint',
1846
- datasetPath: dataset.absolutePath,
1847
- datasetHash: dataset.datasetHash,
1848
- checkpointStep: checkpoint.step,
1849
- }),
1850
- checkpointId,
1851
- checkpointPath: checkpoint.path,
1852
- optimizerStatePresent: true,
1853
- schedulerStatePresent: workload.training.optimizer.scheduler.enabled === true,
1854
- runnerKey: compatibility.observed.runnerKey,
1855
- baseModelRef: fixture.baseModelRef,
1856
- tokenization: dataset.tokenization,
1857
- resumeLineage: checkpoint.metadata?.lineage || null,
1858
- };
1859
- await writeJsonArtifact(
1860
- join(layout.checkpoints, checkpointId, 'checkpoint.json'),
1861
- checkpointPayload
1862
- );
1863
- const checkpointArtifact = await writeJsonArtifact(
1864
- join(layout.checkpoints, checkpointId, 'checkpoint.complete.json'),
1865
- checkpointPayload
1866
- );
1867
- checkpointArtifacts.push({
1868
- checkpointId,
1869
- checkpointPath: checkpoint.path,
1870
- markerPath: checkpointArtifact.path,
1871
- checkpointStep: checkpoint.step,
1872
- });
1873
- if (pipeline.export?.enabled === true && pipeline.export.atCheckpoints === true) {
1874
- exports.push(await exportCausalLmLoraModel(
1875
- loadedWorkload,
1876
- layout,
1877
- fixture,
1878
- checkpointId,
1879
- checkpoint.step,
1880
- dataset.datasetHash
1881
- ));
1882
- }
1883
- const reports = await evaluateCausalLmLoraModel(workload, fixture, dataset, layout, {
1884
- checkpointId,
1885
- checkpointStep: checkpoint.step,
1886
- configHash: workload.configHash,
1887
- workloadPath: loadedWorkload.absolutePath,
1888
- workloadSha256: loadedWorkload.workloadSha256,
1889
- baselineReportsByEvalDatasetId,
1890
- });
1891
- for (const report of reports) {
1892
- evalReports.push(report);
1893
- await appendScoreboardRow(layout.scoreboard, {
1894
- artifactType: 'training_scoreboard',
1895
- schemaVersion: 1,
1896
- generatedAt: new Date().toISOString(),
1897
- checkpointId,
1898
- checkpointStep: checkpoint.step,
1899
- stage: report.stage,
1900
- evalDatasetId: report.evalDatasetId,
1901
- primaryMetric: report.primaryMetric,
1902
- primaryScore: report.primaryScore,
1903
- loss: report.loss,
1904
- qualityClaim: report.qualityClaim || null,
1905
- metrics: {
1906
- loss: report.loss,
1907
- primaryScore: report.primaryScore,
1908
- },
1909
- }, {
1910
- selectionMetric: workload.selectionMetric,
1911
- selectionGoal: workload.selectionGoal,
1912
- });
1913
- }
1914
- },
1915
- });
1916
- const metrics = await runner.run(
1917
- fixture.model,
1918
- createCausalLmDatasetBatches(dataset.samples),
1919
- {
1920
- epochs: 1,
1921
- batchSize: 1,
1922
- shuffle: false,
1923
- maxSteps: workload.training.steps,
1924
- checkpointEvery: workload.checkpointEvery,
1925
- checkpointKey: join(layout.checkpoints, 'latest.state.json'),
1926
- modelId: workload.baseModelId,
1927
- modelUrl: fixture.baseModelUrl,
1928
- tokenizerHash: fixture.baseManifest?.tokenizerHash || null,
1929
- }
1930
- );
1931
- const finalParameterState = await captureLoraParameterState(fixture.model);
1932
- const parameterReceipt = buildLoraParameterReceipt(
1933
- initialParameterState,
1934
- finalParameterState
1935
- );
1936
- const finalCheckpointId = runner.lastCheckpoint
1937
- ? `checkpoint-${String(runner.lastCheckpoint.step).padStart(6, '0')}`
1938
- : null;
1939
- if (
1940
- pipeline.export?.enabled !== false
1941
- && finalCheckpointId
1942
- && exports.every((entry) => entry.checkpointId !== finalCheckpointId)
1943
- ) {
1944
- exports.push(await exportCausalLmLoraModel(
1945
- loadedWorkload,
1946
- layout,
1947
- fixture,
1948
- finalCheckpointId,
1949
- runner.lastCheckpoint.step,
1950
- dataset.datasetHash
1951
- ));
1952
- }
1953
- return {
1954
- ok: true,
1955
- kind: 'lora',
1956
- action: 'run',
1957
- runnerKind: 'causal_lm_lora',
1958
- workloadId: workload.id,
1959
- runRoot: layout.runRoot,
1960
- preflight,
1961
- checkpointArtifacts,
1962
- evalReports,
1963
- exports,
1964
- metrics,
1965
- parameterReceipt,
1966
- lastCheckpoint: runner.lastCheckpoint,
1967
- dataset: {
1968
- path: dataset.absolutePath,
1969
- rowCount: dataset.rowCount,
1970
- sampleCount: dataset.samples.length,
1971
- datasetHash: dataset.datasetHash,
1972
- tokenization: dataset.tokenization,
1973
- },
1974
- baseModel: {
1975
- id: workload.baseModelId,
1976
- ref: fixture.baseModelRef,
1977
- url: fixture.baseModelUrl,
1978
- },
1979
- };
1980
- } finally {
1981
- await fixture.cleanup();
1982
- }
1983
- }
1984
-
1985
- async function runProviderCausalLmLoraPipeline(options, compatibility) {
1986
- const loadedWorkload = options.loadedWorkload;
1987
- const workload = loadedWorkload.workload;
1988
- const pipeline = getPipelineConfig(workload);
1989
- if (!workload.datasetPath) {
1990
- throw new Error('preflightCausalLmLoraWorkload requires workload.datasetPath.');
1991
- }
1992
- const layout = await createLoraRunLayout(options, workload);
1993
- await writeRunContract(layout, buildRunContract(loadedWorkload));
1994
- await writeWorkloadLock(layout, loadedWorkload);
1995
- const datasetPath = await resolveDatasetPathForLoadedWorkload(workload.datasetPath, loadedWorkload);
1996
- const dataset = await loadTextPairsDataset(datasetPath, {
1997
- fetch: options.fetch,
1998
- readFile: options.readFile,
1999
- });
2000
- if (dataset.rowCount < 1) {
2001
- throw new Error(`Causal-LM LoRA dataset ${dataset.absolutePath} has no rows.`);
2002
- }
2003
- const datasetHash = hashTextPairsDataset(dataset);
2004
- const preflight = await preflightCausalLmLoraWorkload(workload, {
2005
- datasetPath: dataset.absolutePath,
2006
- fetch: options.fetch,
2007
- readFile: options.readFile,
2008
- });
2009
- const trainerInfo = await resolveCausalLmTrainer(loadedWorkload, options);
2010
- const trainerResult = await trainerInfo.train({
2011
- schemaVersion: 1,
2012
- runnerKind: 'causal_lm_lora',
2013
- workload,
2014
- loadedWorkload,
2015
- compatibility,
2016
- preflight,
2017
- dataset: {
2018
- absolutePath: dataset.absolutePath,
2019
- rowCount: dataset.rowCount,
2020
- rows: dataset.rows,
2021
- datasetHash,
2022
- },
2023
- adapter: pipeline.adapter,
2024
- training: workload.training,
2025
- export: pipeline.export,
2026
- layout,
2027
- });
2028
- const trainerOutput = normalizeCausalLmTrainerOutput(trainerResult, workload);
2029
- const checkpointPayload = {
2030
- ...buildArtifact(loadedWorkload, {
2031
- prefix: 'lora_ckpt',
2032
- id: trainerOutput.checkpointId,
2033
- artifactType: 'training_checkpoint',
2034
- datasetPath: dataset.absolutePath,
2035
- datasetHash,
2036
- checkpointStep: trainerOutput.checkpointStep,
2037
- }),
2038
- checkpointId: trainerOutput.checkpointId,
2039
- checkpointPath: join(layout.checkpoints, trainerOutput.checkpointId, 'trainer-output.json'),
2040
- optimizerStatePresent: false,
2041
- schedulerStatePresent: false,
2042
- runnerKey: preflight.runnerKey,
2043
- trainer: {
2044
- runnerId: trainerOutput.runnerId || trainerInfo.runnerId,
2045
- trainerId: trainerOutput.trainerId,
2046
- source: trainerInfo.source,
2047
- exportName: trainerInfo.exportName,
2048
- },
2049
- tensorNames: trainerOutput.tensors.map((entry) => entry.name),
2050
- metrics: trainerOutput.metrics,
2051
- receipts: trainerOutput.receipts,
2052
- };
2053
- await writeJsonArtifact(
2054
- join(layout.checkpoints, trainerOutput.checkpointId, 'trainer-output.json'),
2055
- checkpointPayload
2056
- );
2057
- const checkpointArtifact = await writeJsonArtifact(
2058
- join(layout.checkpoints, trainerOutput.checkpointId, 'checkpoint.complete.json'),
2059
- checkpointPayload
2060
- );
2061
- const exported = pipeline.export?.enabled === false
2062
- ? null
2063
- : await exportProviderCausalLmLoraModel(
2064
- loadedWorkload,
2065
- layout,
2066
- trainerOutput,
2067
- trainerOutput.checkpointId,
2068
- trainerOutput.checkpointStep,
2069
- datasetHash,
2070
- trainerInfo,
2071
- preflight
2072
- );
2073
- const evalReports = await writeProviderEvalReports({
2074
- loadedWorkload,
2075
- workload,
2076
- layout,
2077
- dataset,
2078
- datasetHash,
2079
- preflight,
2080
- trainerOutput,
2081
- checkpointId: trainerOutput.checkpointId,
2082
- checkpointStep: trainerOutput.checkpointStep,
2083
- });
2084
- return {
2085
- ok: true,
2086
- kind: 'lora',
2087
- action: 'run',
2088
- runnerKind: 'causal_lm_lora',
2089
- workloadId: workload.id,
2090
- runRoot: layout.runRoot,
2091
- preflight,
2092
- checkpointArtifacts: [{
2093
- checkpointId: trainerOutput.checkpointId,
2094
- checkpointPath: checkpointPayload.checkpointPath,
2095
- markerPath: checkpointArtifact.path,
2096
- checkpointStep: trainerOutput.checkpointStep,
2097
- }],
2098
- evalReports,
2099
- exports: exported ? [exported] : [],
2100
- metrics: trainerOutput.metrics,
2101
- lastCheckpoint: {
2102
- id: trainerOutput.checkpointId,
2103
- step: trainerOutput.checkpointStep,
2104
- path: checkpointPayload.checkpointPath,
2105
- },
2106
- };
2107
- }
2108
-
2109
167
  async function runCausalLmLoraPipeline(options, compatibility) {
2110
168
  if (!options.loadedWorkload.workload.datasetPath) {
2111
169
  throw new Error('preflightCausalLmLoraWorkload requires workload.datasetPath.');