@genai-fi/nanogpt 1.6.1 → 1.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (426) hide show
  1. package/dist/{binary_op_util-WY9YIl0q.js → Reshape-CqftURg0.js} +75 -60
  2. package/dist/TeachableLLM.d.ts +12 -12
  3. package/dist/api/beamer.d.ts +4 -4
  4. package/dist/api/responses.d.ts +4 -4
  5. package/dist/api/training.d.ts +9 -9
  6. package/dist/backend.d.ts +1 -1
  7. package/dist/backend_util-DZIc6pyV.js +1081 -0
  8. package/dist/checks/index.d.ts +11 -11
  9. package/dist/checks/weights.d.ts +1 -1
  10. package/dist/data/docx.d.ts +1 -1
  11. package/dist/data/pdf.d.ts +1 -1
  12. package/dist/data/stream.d.ts +1 -1
  13. package/dist/data/textLoader.d.ts +1 -1
  14. package/dist/data.d.ts +3 -3
  15. package/dist/device-CZPGtQBU.js +9 -0
  16. package/dist/device.d.ts +2 -0
  17. package/dist/{dist-DeVH1nGn.js → dist-DiszGjH-.js} +1833 -1844
  18. package/dist/{dist-CMMLi-qO.js → dist-JOPw9zTN.js} +69 -11
  19. package/dist/inference/Beamer.d.ts +3 -3
  20. package/dist/inference/Generator.d.ts +4 -4
  21. package/dist/inference/tokenisePrompt.d.ts +2 -2
  22. package/dist/inference/types.d.ts +1 -1
  23. package/dist/inference/utilities.d.ts +1 -1
  24. package/dist/layers/BaseLayer.d.ts +5 -4
  25. package/dist/layers/GQA.d.ts +24 -26
  26. package/dist/layers/HeadLayer.d.ts +2 -2
  27. package/dist/layers/LoRA.d.ts +1 -1
  28. package/dist/layers/MHA.d.ts +3 -4
  29. package/dist/layers/MLP.d.ts +2 -2
  30. package/dist/layers/PositionEmbedding.d.ts +3 -3
  31. package/dist/layers/RMSNorm.d.ts +2 -2
  32. package/dist/layers/RoPECache.d.ts +2 -1
  33. package/dist/layers/TiedEmbedding.d.ts +2 -2
  34. package/dist/layers/TransformerBlock.d.ts +9 -6
  35. package/dist/layers.d.ts +4 -4
  36. package/dist/loader/load.d.ts +1 -1
  37. package/dist/loader/loadHF.d.ts +3 -3
  38. package/dist/loader/loadTransformers.d.ts +2 -2
  39. package/dist/loader/loadZipMeta.d.ts +2 -2
  40. package/dist/loader/newZipLoad.d.ts +2 -2
  41. package/dist/loader/oldZipLoad.d.ts +4 -4
  42. package/dist/loader/save.d.ts +5 -5
  43. package/dist/loader/types.d.ts +23 -10
  44. package/dist/main.d.ts +40 -24
  45. package/dist/matMul16-CiMKJM8v.js +2610 -0
  46. package/dist/models/Llama.d.ts +2 -2
  47. package/dist/models/LoopNano.d.ts +16 -0
  48. package/dist/models/NanoGPTV1.d.ts +2 -2
  49. package/dist/models/NanoGPTV2.d.ts +2 -2
  50. package/dist/models/Qwen3.d.ts +2 -2
  51. package/dist/models/config.d.ts +11 -1
  52. package/dist/models/factory.d.ts +2 -2
  53. package/dist/models/model.d.ts +6 -6
  54. package/dist/models.d.ts +4 -4
  55. package/dist/nanogpt.js +37041 -0
  56. package/dist/ops/adamAdjust.d.ts +2 -0
  57. package/dist/ops/adamMoments.d.ts +2 -0
  58. package/dist/ops/adamW.d.ts +2 -0
  59. package/dist/ops/add16.d.ts +1 -0
  60. package/dist/ops/appendCache.d.ts +2 -0
  61. package/dist/ops/attentionMask.d.ts +11 -1
  62. package/dist/ops/dropout16.d.ts +2 -0
  63. package/dist/ops/gatherSub.d.ts +2 -0
  64. package/dist/ops/gelu.d.ts +3 -0
  65. package/dist/ops/log.d.ts +1 -0
  66. package/dist/ops/matMul16.d.ts +4 -0
  67. package/dist/ops/matMulGelu.d.ts +3 -0
  68. package/dist/ops/matMulMul.d.ts +2 -0
  69. package/dist/ops/mul16.d.ts +1 -0
  70. package/dist/ops/mulDrop.d.ts +2 -0
  71. package/dist/ops/normRMS.d.ts +3 -0
  72. package/dist/ops/pack16.d.ts +1 -0
  73. package/dist/ops/qkv.d.ts +13 -1
  74. package/dist/ops/rope.d.ts +4 -1
  75. package/dist/ops/scatterSub.d.ts +4 -1
  76. package/dist/ops/silu.d.ts +1 -0
  77. package/dist/ops/softmax16.d.ts +1 -0
  78. package/dist/ops/sparseCrossEntropy.d.ts +15 -0
  79. package/dist/ops/unpack16.d.ts +1 -0
  80. package/dist/ops/webgl/sparseCrossEntropy.d.ts +1 -0
  81. package/dist/ops/webgpu/adamW.d.ts +1 -0
  82. package/dist/ops/webgpu/concat16.d.ts +16 -0
  83. package/dist/ops/webgpu/index.d.ts +29 -0
  84. package/dist/ops/webgpu/normRMS16_program.d.ts +2 -2
  85. package/dist/ops/webgpu/normRMS32_program.d.ts +2 -2
  86. package/dist/ops/webgpu/pack16_program.d.ts +1 -1
  87. package/dist/ops/webgpu/softmax16_program.d.ts +16 -0
  88. package/dist/ops/webgpu/sparseCrossEntropy.d.ts +1 -0
  89. package/dist/ops/webgpu/utils/binary_op.d.ts +16 -0
  90. package/dist/ops/webgpu/utils/deviceInfo.d.ts +1 -1
  91. package/dist/ops/webgpu/utils/reductions.d.ts +1 -1
  92. package/dist/patches/webgpu_backend.d.ts +2 -1
  93. package/dist/patches/webgpu_base.d.ts +0 -1
  94. package/dist/patches/webgpu_program.d.ts +21 -0
  95. package/dist/{shared-BIDMUBCs.js → shared-BARkvvG4.js} +587 -505
  96. package/dist/tokenise.d.ts +4 -4
  97. package/dist/tokeniser/BaseTokeniser.d.ts +3 -3
  98. package/dist/tokeniser/CharTokeniser.d.ts +2 -2
  99. package/dist/tokeniser/bpe.d.ts +2 -2
  100. package/dist/tokeniser/type.d.ts +2 -2
  101. package/dist/training/AdamW.d.ts +19 -2
  102. package/dist/training/BasicTrainer.d.ts +5 -5
  103. package/dist/training/DatasetBuilder.d.ts +2 -2
  104. package/dist/training/Evaluator.d.ts +1 -1
  105. package/dist/training/LRScheduler.d.ts +1 -1
  106. package/dist/training/PreTrainer.d.ts +6 -6
  107. package/dist/training/SFTTrainer.d.ts +6 -6
  108. package/dist/training/configure.d.ts +2 -2
  109. package/dist/training/factory.d.ts +6 -5
  110. package/dist/training/prepareData.d.ts +6 -6
  111. package/dist/training/tasks/tokenStream.d.ts +3 -3
  112. package/dist/training/types.d.ts +6 -5
  113. package/dist/training/validateOptions.d.ts +1 -1
  114. package/dist/training/validation.d.ts +3 -3
  115. package/dist/utilities/datasetID.d.ts +1 -1
  116. package/dist/utilities/dummy.d.ts +1 -1
  117. package/dist/utilities/packed.d.ts +1 -1
  118. package/dist/utilities/parameters.d.ts +1 -1
  119. package/dist/utilities/sentences.d.ts +1 -1
  120. package/dist/utilities/waitForModel.d.ts +1 -1
  121. package/dist/webgpu-D4syGmzo.js +3221 -0
  122. package/dist/webgpu-wUCQopBN.js +1007 -0
  123. package/dist/{webgpu_program-DvkFqRlR.js → webgpu_backend-CzY9TzvA.js} +174 -223
  124. package/dist/{patches/webgpu_base.js → webgpu_base-Bjg7okYf.js} +6 -9
  125. package/package.json +12 -9
  126. package/dist/DatasetBuilder-DRDW8tcM.js +0 -731
  127. package/dist/Generator-8IK28IWR.js +0 -316
  128. package/dist/RealDiv-Cl8k3YTx.js +0 -354
  129. package/dist/Reshape-CKxKPKqi.js +0 -91
  130. package/dist/Reshape-H5zZRgr9.js +0 -16
  131. package/dist/RoPECache-dYscFINl.js +0 -722
  132. package/dist/TeachableLLM.js +0 -220
  133. package/dist/api/beamer.js +0 -61
  134. package/dist/api/responses.js +0 -177
  135. package/dist/api/training.js +0 -215
  136. package/dist/axis_util-rF9RnwKE.js +0 -37
  137. package/dist/backend.js +0 -12
  138. package/dist/backend_util-Cpp0Gwlp.js +0 -394
  139. package/dist/broadcast_to-CGvLNiQ0.js +0 -20
  140. package/dist/checks/appendCache.js +0 -55
  141. package/dist/checks/attentionMask.js +0 -57
  142. package/dist/checks/check.js +0 -31
  143. package/dist/checks/gelu.js +0 -46
  144. package/dist/checks/index.js +0 -27
  145. package/dist/checks/matMulGelu.js +0 -84
  146. package/dist/checks/normRMS.js +0 -30
  147. package/dist/checks/normRMSGrad.js +0 -23
  148. package/dist/checks/packUnpack.js +0 -46
  149. package/dist/checks/qkv.js +0 -1
  150. package/dist/checks/rope.js +0 -30
  151. package/dist/checks/weights.js +0 -26
  152. package/dist/clip_by_value-D-UX9am5.js +0 -13
  153. package/dist/complex-Cl904TqN.js +0 -13
  154. package/dist/complex_util-BBlPAm-s.js +0 -59
  155. package/dist/concat-Oip5acVY.js +0 -14
  156. package/dist/concat_util-CY45Hf4F.js +0 -18
  157. package/dist/data/docx.js +0 -17
  158. package/dist/data/pdf.js +0 -14
  159. package/dist/data/stream.js +0 -1
  160. package/dist/data/textLoader.js +0 -575
  161. package/dist/data-CV_i_H6B.js +0 -11
  162. package/dist/data.js +0 -5
  163. package/dist/dist-DyL7qSLJ.js +0 -7809
  164. package/dist/dropout16-CA31Ud5a.js +0 -20
  165. package/dist/dropout_util-SiFMbFXz.js +0 -22
  166. package/dist/eventemitter3-wmfmOP_f.js +0 -90
  167. package/dist/expand_dims-Dh3Fulyn.js +0 -10
  168. package/dist/floor-BwjmUb7X.js +0 -8
  169. package/dist/gather-B5Z1RFt5.js +0 -14
  170. package/dist/gatherSub-CRUfUzBa.js +0 -22
  171. package/dist/gelu-Xx9-ohH9.js +0 -25
  172. package/dist/gpgpu_math-48m8q3jI.js +0 -1591
  173. package/dist/inference/Beamer.js +0 -215
  174. package/dist/inference/Generator.js +0 -1
  175. package/dist/inference/tokenisePrompt.js +0 -17
  176. package/dist/inference/types.js +0 -0
  177. package/dist/inference/utilities.js +0 -31
  178. package/dist/jszip.min-Btv48m44.js +0 -3037
  179. package/dist/kernel_funcs_utils-BYRpSfV_.js +0 -229
  180. package/dist/layers/BaseLayer.js +0 -75
  181. package/dist/layers/GQA.js +0 -102
  182. package/dist/layers/HeadLayer.js +0 -28
  183. package/dist/layers/LoRA.js +0 -45
  184. package/dist/layers/MHA.js +0 -102
  185. package/dist/layers/MLP.js +0 -47
  186. package/dist/layers/PositionEmbedding.js +0 -28
  187. package/dist/layers/RMSNorm.js +0 -25
  188. package/dist/layers/RoPECache.js +0 -1
  189. package/dist/layers/TiedEmbedding.js +0 -33
  190. package/dist/layers/TransformerBlock.js +0 -50
  191. package/dist/layers/WeightStore.js +0 -68
  192. package/dist/layers-CFqEip3s.js +0 -13
  193. package/dist/layers.js +0 -6
  194. package/dist/loader/load.js +0 -63
  195. package/dist/loader/loadHF.js +0 -17
  196. package/dist/loader/loadTransformers.js +0 -61
  197. package/dist/loader/loadZipMeta.js +0 -15
  198. package/dist/loader/newZipLoad.js +0 -24
  199. package/dist/loader/oldZipLoad.js +0 -36
  200. package/dist/loader/save.js +0 -89
  201. package/dist/loader/types.js +0 -0
  202. package/dist/main.js +0 -12145
  203. package/dist/matMul16-tE26fnhf.js +0 -87
  204. package/dist/matMulGelu-ASUWlKqu.js +0 -162
  205. package/dist/mat_mul-R9_DWmIT.js +0 -16
  206. package/dist/models/Llama.js +0 -88
  207. package/dist/models/NanoGPTV1.js +0 -94
  208. package/dist/models/NanoGPTV2.js +0 -85
  209. package/dist/models/Qwen3.js +0 -88
  210. package/dist/models/config.js +0 -32
  211. package/dist/models/factory.js +0 -16
  212. package/dist/models/model.js +0 -75
  213. package/dist/models-DyBpJJwM.js +0 -13
  214. package/dist/models.js +0 -6
  215. package/dist/mul16-C7sqgqik.js +0 -41
  216. package/dist/non_max_suppression_impl-B0H318rb.js +0 -72
  217. package/dist/not_equal-Bu3O8rh_.js +0 -67
  218. package/dist/one_hot-TEMPumJj.js +0 -14
  219. package/dist/ones-DpPXWa6A.js +0 -13
  220. package/dist/ops/adamAdjust.js +0 -17
  221. package/dist/ops/adamMoments.js +0 -15
  222. package/dist/ops/add16.js +0 -11
  223. package/dist/ops/appendCache.js +0 -26
  224. package/dist/ops/attentionMask.js +0 -19
  225. package/dist/ops/concat16.js +0 -8
  226. package/dist/ops/cpu/adamAdjust.js +0 -15
  227. package/dist/ops/cpu/adamMoments.js +0 -16
  228. package/dist/ops/cpu/appendCache.js +0 -65
  229. package/dist/ops/cpu/attentionMask.js +0 -20
  230. package/dist/ops/cpu/fusedSoftmax.js +0 -22
  231. package/dist/ops/cpu/gatherSub.js +0 -1
  232. package/dist/ops/cpu/gelu.js +0 -35
  233. package/dist/ops/cpu/matMul16.js +0 -14
  234. package/dist/ops/cpu/matMulGelu.js +0 -44
  235. package/dist/ops/cpu/matMulMul.js +0 -19
  236. package/dist/ops/cpu/mulDropout.js +0 -19
  237. package/dist/ops/cpu/normRMS.js +0 -34
  238. package/dist/ops/cpu/qkv.js +0 -74
  239. package/dist/ops/cpu/rope.js +0 -84
  240. package/dist/ops/cpu/scatterSub.js +0 -1
  241. package/dist/ops/dot16.js +0 -28
  242. package/dist/ops/dropout.js +0 -12
  243. package/dist/ops/dropout16.js +0 -1
  244. package/dist/ops/gatherSub.js +0 -12
  245. package/dist/ops/gelu.js +0 -1
  246. package/dist/ops/globalNorm.js +0 -19
  247. package/dist/ops/grads/add16.js +0 -26
  248. package/dist/ops/grads/attentionMask.js +0 -25
  249. package/dist/ops/grads/dropout16.js +0 -1
  250. package/dist/ops/grads/gelu.js +0 -1
  251. package/dist/ops/grads/matMul16.js +0 -1
  252. package/dist/ops/grads/matMulGelu.js +0 -21
  253. package/dist/ops/grads/mul16.js +0 -1
  254. package/dist/ops/grads/normRMS.js +0 -36
  255. package/dist/ops/grads/pack16.js +0 -1
  256. package/dist/ops/grads/qkv.js +0 -46
  257. package/dist/ops/grads/rope.js +0 -1
  258. package/dist/ops/grads/silu.js +0 -1
  259. package/dist/ops/grads/softmax16.js +0 -22
  260. package/dist/ops/grads/unpack16.js +0 -1
  261. package/dist/ops/grads/utils.js +0 -11
  262. package/dist/ops/log.js +0 -1
  263. package/dist/ops/matMul16.js +0 -1
  264. package/dist/ops/matMulGelu.js +0 -19
  265. package/dist/ops/matMulMul.js +0 -15
  266. package/dist/ops/mul16.js +0 -1
  267. package/dist/ops/mulDrop.js +0 -14
  268. package/dist/ops/normRMS.js +0 -21
  269. package/dist/ops/pack16.js +0 -1
  270. package/dist/ops/qkv.js +0 -15
  271. package/dist/ops/reshape16.js +0 -33
  272. package/dist/ops/rope.js +0 -1
  273. package/dist/ops/scatterSub.js +0 -12
  274. package/dist/ops/silu.js +0 -1
  275. package/dist/ops/slice16.js +0 -11
  276. package/dist/ops/softmax16.js +0 -8
  277. package/dist/ops/sub16.js +0 -10
  278. package/dist/ops/sum16.js +0 -13
  279. package/dist/ops/transpose16.js +0 -33
  280. package/dist/ops/unpack16.js +0 -1
  281. package/dist/ops/webgl/adamAdjust.js +0 -81
  282. package/dist/ops/webgl/adamMoments.js +0 -43
  283. package/dist/ops/webgl/appendCache.js +0 -52
  284. package/dist/ops/webgl/attentionMask.js +0 -63
  285. package/dist/ops/webgl/dropout16.js +0 -11
  286. package/dist/ops/webgl/fusedSoftmax.js +0 -70
  287. package/dist/ops/webgl/gatherSub.js +0 -27
  288. package/dist/ops/webgl/gelu.js +0 -47
  289. package/dist/ops/webgl/log.js +0 -14
  290. package/dist/ops/webgl/matMul16.js +0 -48
  291. package/dist/ops/webgl/matMulGelu.js +0 -1
  292. package/dist/ops/webgl/matMulMul.js +0 -23
  293. package/dist/ops/webgl/mulDropout.js +0 -31
  294. package/dist/ops/webgl/normRMS.js +0 -115
  295. package/dist/ops/webgl/qkv.js +0 -53
  296. package/dist/ops/webgl/rope.js +0 -73
  297. package/dist/ops/webgl/scatterSub.js +0 -27
  298. package/dist/ops/webgpu/adamAdjust.js +0 -76
  299. package/dist/ops/webgpu/adamMoments.js +0 -75
  300. package/dist/ops/webgpu/add16.js +0 -13
  301. package/dist/ops/webgpu/appendCache.js +0 -129
  302. package/dist/ops/webgpu/attentionMask.js +0 -41
  303. package/dist/ops/webgpu/attentionMask32_program.js +0 -61
  304. package/dist/ops/webgpu/clipScale.js +0 -44
  305. package/dist/ops/webgpu/concat16.js +0 -110
  306. package/dist/ops/webgpu/dropout16.js +0 -58
  307. package/dist/ops/webgpu/gatherSub.js +0 -51
  308. package/dist/ops/webgpu/gelu.js +0 -146
  309. package/dist/ops/webgpu/index.js +0 -27
  310. package/dist/ops/webgpu/matMul16.js +0 -91
  311. package/dist/ops/webgpu/matMul16_program.js +0 -311
  312. package/dist/ops/webgpu/mul16.js +0 -13
  313. package/dist/ops/webgpu/norm2.js +0 -45
  314. package/dist/ops/webgpu/normRMS.js +0 -25
  315. package/dist/ops/webgpu/normRMS16_program.js +0 -28
  316. package/dist/ops/webgpu/normRMS32_program.js +0 -28
  317. package/dist/ops/webgpu/normRMSGrad.js +0 -225
  318. package/dist/ops/webgpu/pack16.js +0 -20
  319. package/dist/ops/webgpu/pack16_program.js +0 -92
  320. package/dist/ops/webgpu/qkv.js +0 -63
  321. package/dist/ops/webgpu/rope.js +0 -227
  322. package/dist/ops/webgpu/scatterSub.js +0 -52
  323. package/dist/ops/webgpu/silu.js +0 -117
  324. package/dist/ops/webgpu/slice16.js +0 -73
  325. package/dist/ops/webgpu/softmax16.js +0 -18
  326. package/dist/ops/webgpu/softmax16_program.js +0 -88
  327. package/dist/ops/webgpu/softmax16_subgroup_program.js +0 -69
  328. package/dist/ops/webgpu/softmax16grad.js +0 -30
  329. package/dist/ops/webgpu/sub16.js +0 -13
  330. package/dist/ops/webgpu/sum16.js +0 -30
  331. package/dist/ops/webgpu/transpose16.js +0 -38
  332. package/dist/ops/webgpu/transpose16_program.js +0 -50
  333. package/dist/ops/webgpu/transpose16_shared_program.js +0 -78
  334. package/dist/ops/webgpu/unpack16.js +0 -59
  335. package/dist/ops/webgpu/utils/binary_op.js +0 -140
  336. package/dist/ops/webgpu/utils/deviceInfo.js +0 -10
  337. package/dist/ops/webgpu/utils/reductions.js +0 -264
  338. package/dist/ops-Be5ACU5R.js +0 -13
  339. package/dist/ops-C1UpdEYM.js +0 -509
  340. package/dist/pack16-BHO4t1-M.js +0 -36
  341. package/dist/pad-Dy-1IP3A.js +0 -13
  342. package/dist/patches/webgpu_backend.js +0 -42
  343. package/dist/patches/webgpu_program.js +0 -292
  344. package/dist/picomatch--TW6gyBJ.js +0 -999
  345. package/dist/qkv-BQfNPucX.js +0 -42
  346. package/dist/rand_util-_vCY93Uw.js +0 -44
  347. package/dist/random_normal-DUIT8kyC.js +0 -11
  348. package/dist/range-CYvQC3fW.js +0 -13
  349. package/dist/relu-C4XULT8A.js +0 -8
  350. package/dist/reshape-DJb1jgMt.js +0 -8
  351. package/dist/resize_nearest_neighbor-DAqcTYUe.js +0 -188
  352. package/dist/rope-JjBVufF_.js +0 -24
  353. package/dist/scatterSub-BOnqk-uY.js +0 -27
  354. package/dist/scatter_nd_util-BepMCpLY.js +0 -34
  355. package/dist/seedrandom-SHEGFK6H.js +0 -282
  356. package/dist/selu_util-DeOTf-sc.js +0 -27
  357. package/dist/shared-CX_iPI5K.js +0 -4
  358. package/dist/silu-DsAE2cJ5.js +0 -23
  359. package/dist/slice-Dlb1FlzG.js +0 -13
  360. package/dist/slice_util-BVgPRoij.js +0 -207
  361. package/dist/softmax-Dpav_dWw.js +0 -10
  362. package/dist/split-DYnT8chz.js +0 -11
  363. package/dist/squeeze-B3XYGAlg.js +0 -9
  364. package/dist/stack-Dbbt9mIk.js +0 -10
  365. package/dist/step-DA-5DNu9.js +0 -348
  366. package/dist/stream-BBe2apn4.js +0 -2280
  367. package/dist/sum-C8qNbFNp.js +0 -13
  368. package/dist/tensor-ALxWZfmz.js +0 -7
  369. package/dist/tensor1d-BHgkkhTE.js +0 -9
  370. package/dist/tensor2d-ARItvqVn.js +0 -10
  371. package/dist/tensor4d-D1zRBipJ.js +0 -10
  372. package/dist/tfjs_backend-CsXOum0h.js +0 -685
  373. package/dist/tile-DfyQ0KX-.js +0 -10
  374. package/dist/tokenise-DbS2Ea-T.js +0 -14
  375. package/dist/tokenise.js +0 -6
  376. package/dist/tokeniser/BaseTokeniser.js +0 -137
  377. package/dist/tokeniser/CharTokeniser.js +0 -85
  378. package/dist/tokeniser/bpe.js +0 -156
  379. package/dist/tokeniser/messages.js +0 -0
  380. package/dist/tokeniser/type.js +0 -0
  381. package/dist/topk-BrU2pkrR.js +0 -19
  382. package/dist/training/AdamW.js +0 -132
  383. package/dist/training/BasicTrainer.js +0 -274
  384. package/dist/training/DatasetBuilder.js +0 -1
  385. package/dist/training/Evaluator.js +0 -51
  386. package/dist/training/LRScheduler.js +0 -37
  387. package/dist/training/PreTrainer.js +0 -21
  388. package/dist/training/SFTTrainer.js +0 -23
  389. package/dist/training/configure.js +0 -33
  390. package/dist/training/factory.js +0 -7
  391. package/dist/training/loss.js +0 -18
  392. package/dist/training/orthoGrad.js +0 -9
  393. package/dist/training/prepareData.js +0 -51
  394. package/dist/training/sparseCrossEntropy.js +0 -53
  395. package/dist/training/tasks/TokenStore.js +0 -217
  396. package/dist/training/tasks/tokenStream.js +0 -45
  397. package/dist/training/types.js +0 -0
  398. package/dist/training/validateOptions.js +0 -18
  399. package/dist/training/validation.js +0 -30
  400. package/dist/transpose-BC-M_OoT.js +0 -34
  401. package/dist/unsorted_segment_sum-BelC7R-q.js +0 -408
  402. package/dist/utilities/arrayClose.js +0 -29
  403. package/dist/utilities/arrayShape.js +0 -7
  404. package/dist/utilities/datasetID.js +0 -17
  405. package/dist/utilities/dummy.js +0 -36
  406. package/dist/utilities/float16.js +0 -46
  407. package/dist/utilities/maxHeap.js +0 -54
  408. package/dist/utilities/multinomialCPU.js +0 -7
  409. package/dist/utilities/naming.js +0 -0
  410. package/dist/utilities/packed.js +0 -12
  411. package/dist/utilities/parameters.js +0 -53
  412. package/dist/utilities/performance.js +0 -15
  413. package/dist/utilities/profile.js +0 -32
  414. package/dist/utilities/random.js +0 -18
  415. package/dist/utilities/safetensors.js +0 -63
  416. package/dist/utilities/sentences.js +0 -35
  417. package/dist/utilities/tokenParse.js +0 -16
  418. package/dist/utilities/tokens.js +0 -39
  419. package/dist/utilities/topP.js +0 -25
  420. package/dist/utilities/waitForModel.js +0 -11
  421. package/dist/utilities/weights.js +0 -40
  422. package/dist/utilities/yielder.js +0 -6
  423. package/dist/v4-YCkfo8OR.js +0 -27
  424. package/dist/webgpu-BHX-uBym.js +0 -518
  425. package/dist/webgpu_util-5QTyovaf.js +0 -105
  426. package/dist/zeros-hxKGkRfl.js +0 -12
@@ -1,220 +0,0 @@
1
- import { validateConfig as e } from "./models/config.js";
2
- import { saveModel as t } from "./loader/save.js";
3
- import { loadModel as n } from "./loader/load.js";
4
- import { t as r } from "./eventemitter3-wmfmOP_f.js";
5
- import { dummyPassTrainAsync as i } from "./utilities/dummy.js";
6
- import a from "./tokeniser/CharTokeniser.js";
7
- import o from "./utilities/profile.js";
8
- import s from "./tokeniser/bpe.js";
9
- import c from "./models/factory.js";
10
- import l from "./api/responses.js";
11
- import u from "./api/training.js";
12
- import { selectBackend as d } from "./backend.js";
13
- import { getBackendDevice as f } from "./patches/webgpu_base.js";
14
- import p from "./api/beamer.js";
15
- // lib/TeachableLLM.ts
16
- var m = class m {
17
- static instances = /* @__PURE__ */ new Set();
18
- ee = new r();
19
- _config;
20
- _model;
21
- _tokeniser;
22
- _status = "loading";
23
- _memoryRequirements;
24
- _responses = null;
25
- _training = null;
26
- _beaming = null;
27
- meta = {
28
- version: 2,
29
- application: "@genai-fi/nanogpt"
30
- };
31
- static async selectBackend(e, t) {
32
- if (await d(e, t), e === "webgpu") {
33
- let e = f();
34
- e && e.lost.then(() => {
35
- console.warn("WebGPU device lost"), m.instances.forEach((e) => {
36
- e.setStatus("error"), e.ee.emit("lost");
37
- });
38
- });
39
- }
40
- }
41
- constructor(e, t) {
42
- this._config = t?.config, this._tokeniser = e, this._model = t, t?.metaData && (this.meta = t.metaData), m.instances.add(this);
43
- }
44
- get vocab() {
45
- return this._tokeniser?.getVocab() || [];
46
- }
47
- get mode() {
48
- return this._model?.metaData?.mode ?? "untrained";
49
- }
50
- set mode(e) {
51
- if (!this._model) throw Error("model_not_initialized.");
52
- (this._model.metaData.mode !== "conversational" || e !== "completion") && e !== "untrained" && (this._model.metaData.mode = e, this.ee.emit("mode", e));
53
- }
54
- get loaded() {
55
- return !!this._model && !!this._tokeniser && !!this._config;
56
- }
57
- get config() {
58
- if (!this._config) throw Error("configuration_not_initialized.");
59
- return this._config;
60
- }
61
- get model() {
62
- if (!this._model) throw Error("model_not_initialized.");
63
- return this._model;
64
- }
65
- get tokeniser() {
66
- if (!this._tokeniser) throw Error("tokeniser_not_initialized.");
67
- return this._tokeniser;
68
- }
69
- get status() {
70
- return this._status;
71
- }
72
- get ready() {
73
- return this._status === "ready" && !!this._model && !!this._tokeniser;
74
- }
75
- get busy() {
76
- return this._status === "busy" || this._status === "training";
77
- }
78
- createLoRA(e, t) {
79
- if (!this._model) throw Error("model_not_initialized.");
80
- this._model.createLoRA(e, t), this.ee.emit("changeLoRA");
81
- }
82
- deleteLoRA(e) {
83
- if (!this._model) throw Error("model_not_initialized.");
84
- this._model.deleteLoRA(e), this.ee.emit("changeLoRA");
85
- }
86
- renameLoRA(e, t) {
87
- if (!this._model) throw Error("model_not_initialized.");
88
- this._model.renameLoRA(e, t), this.ee.emit("changeLoRA");
89
- }
90
- attachLoRA(e) {
91
- if (!this._model) throw Error("model_not_initialized.");
92
- this.model.lora?.name !== e && (this._model.attachLoRA(e), this.ee.emit("changeLoRA"));
93
- }
94
- detachLoRA() {
95
- if (!this._model) throw Error("model_not_initialized.");
96
- this._model.detachLoRA(), this.ee.emit("changeLoRA");
97
- }
98
- hasLoRA(e) {
99
- if (!this._model) throw Error("model_not_initialized.");
100
- return this._model.hasLoRA(e);
101
- }
102
- listLoRAs() {
103
- if (!this._model) throw Error("model_not_initialized.");
104
- return this._model.listLoRAs();
105
- }
106
- estimateTrainingMemoryUsage(e) {
107
- let t = this._memoryRequirements ?? {
108
- perBatch: 0,
109
- tapeSize: 0,
110
- gradients: 0
111
- }, n = t.perBatch * e, r = t.gradients;
112
- return n * .66 + r * 4;
113
- }
114
- setStatus(e) {
115
- this._status !== e && (this._status = e, this.ee.emit("status", e));
116
- }
117
- saveModel(e) {
118
- if (!this._model || !this._tokeniser) throw Error("model_or_tokeniser_not_initialized.");
119
- let n = (e?.includeOptimizer && this._training?.getPretrainingJob()) ?? null;
120
- return t(this._model, this._tokeniser, {
121
- ...e,
122
- name: e?.name || this.meta.name
123
- }, n ? {
124
- optimizer: n.trainer.optimizer,
125
- trainingLog: n.trainer.log
126
- } : void 0);
127
- }
128
- static loadModel(t, r) {
129
- let a = new m();
130
- return n(t, r).then(({ model: t, tokeniser: n, metaData: r, optimizer: o, log: s }) => {
131
- e(t.config), a._model = t, a._tokeniser = n, a._config = t.config, r && (a.meta = r), a.setStatus("warmup"), i(t).then((e) => {
132
- a._memoryRequirements = e, o && t.metaData.pretrainingSettings && t.metaData.pretrainingData && a.training.restore(t.metaData.pretrainingSettings, s || [], o, t.metaData.pretrainingData), a.setStatus("ready"), a.ee.emit("loaded"), a.ee.emit("mode", a.mode);
133
- }).catch((e) => {
134
- a.setStatus("error"), a.ee.emit("error", e), console.error("Error during warmup:", e);
135
- });
136
- }).catch((e) => {
137
- a.setStatus("error"), a.ee.emit("error", e), console.error("Error loading model:", e);
138
- }), a;
139
- }
140
- static create(t, n) {
141
- e(n);
142
- let r = n, o = t === "char" ? new a(r.vocabSize) : t === "bpe" ? new s(r.vocabSize) : t, l = c(r), u = new m(o, l);
143
- return u.setStatus("warmup"), i(l).then((e) => {
144
- u._memoryRequirements = e, u.tokeniser.trained ? (u.setStatus("ready"), u.ee.emit("loaded"), u.ee.emit("mode", u.mode)) : (u.setStatus("awaitingTokens"), u.ee.emit("loaded"), u.ee.emit("mode", u.mode), u.tokeniser.once("trainStatus", (e) => {
145
- e === "trained" && u.setStatus("ready");
146
- }));
147
- }).catch((e) => {
148
- u.setStatus("error"), u.ee.emit("error", e), console.error("Error during warmup:", e);
149
- }), u;
150
- }
151
- getProfiler() {
152
- return this._model?.getProfiler();
153
- }
154
- get enableProfiler() {
155
- return !!this._model?.getProfiler();
156
- }
157
- set enableProfiler(e) {
158
- if (e) {
159
- if (!this._config) return;
160
- this.model.getProfiler() || this.model.setProfiler(new o());
161
- } else this.model.getProfiler() && this.model.setProfiler(null);
162
- }
163
- getNumParams() {
164
- return this._model ? this._model.getNumParams() : 0;
165
- }
166
- async trainTokeniser(e) {
167
- if (!this._tokeniser) throw Error("tokeniser_not_initialized.");
168
- let t = await this._tokeniser.train(e);
169
- return this._status === "awaitingTokens" && this.setStatus("ready"), t;
170
- }
171
- get responses() {
172
- if (!this._responses) {
173
- if (!this._model || !this._tokeniser) throw Error("model_or_tokeniser_not_initialized.");
174
- this._responses = new l(this._model, this._tokeniser), this._responses.on("error", (e) => {
175
- this.setStatus("error"), this.ee.emit("error", e);
176
- }), this._responses.on("status", (e) => {
177
- e === "busy" ? this.setStatus("busy") : e === "ready" && this.setStatus("ready");
178
- });
179
- }
180
- return this._responses;
181
- }
182
- get beaming() {
183
- if (!this._beaming) {
184
- if (!this._model || !this._tokeniser) throw Error("model_or_tokeniser_not_initialized.");
185
- this._beaming = new p(this._model, this._tokeniser), this._beaming.on("error", (e) => {
186
- this.setStatus("error"), this.ee.emit("error", e);
187
- }), this._beaming.on("status", (e) => {
188
- e === "busy" ? this.setStatus("busy") : e === "ready" && this.setStatus("ready");
189
- });
190
- }
191
- return this._beaming;
192
- }
193
- get training() {
194
- if (!this._training) {
195
- if (!this._model || !this._tokeniser) throw Error("model_or_tokeniser_not_initialized.");
196
- this._training = new u(this._model, this._tokeniser), this._training.on("running", () => {
197
- this.setStatus("busy");
198
- }), this._training.on("completed", () => {
199
- this._training?.training || this.setStatus("ready");
200
- }), this._training.on("cancelled", () => {
201
- this._training?.training || this.setStatus("ready");
202
- }), this._training.on("error", (e) => {
203
- this.setStatus("error"), this.ee.emit("error", e);
204
- }), this._training.on("mode", () => {
205
- this.ee.emit("mode", this._model?.metaData.mode);
206
- });
207
- }
208
- return this._training;
209
- }
210
- dispose() {
211
- this._responses &&= (this._responses.dispose(), null), this._training &&= (this._training.dispose(), null), this._model?.dispose(), this.ee.removeAllListeners(), m.instances.delete(this);
212
- }
213
- on(e, t) {
214
- e === "loaded" && this.loaded ? setTimeout(() => t(), 0) : this.ee.on(e, t);
215
- }
216
- off(e, t) {
217
- this.ee.off(e, t);
218
- }
219
- };
220
- export { m as default };
@@ -1,61 +0,0 @@
1
- import { t as e } from "../eventemitter3-wmfmOP_f.js";
2
- import t from "../inference/Beamer.js";
3
- import { t as n } from "../v4-YCkfo8OR.js";
4
- // lib/api/beamer.ts
5
- var r = class {
6
- ee;
7
- _model;
8
- _tokeniser;
9
- _busyCount = 0;
10
- _jobs = /* @__PURE__ */ new Map();
11
- _queue = [];
12
- constructor(t, n) {
13
- this._model = t, this._tokeniser = n, this.ee = new e();
14
- }
15
- on(e, t) {
16
- this.ee.on(e, t);
17
- }
18
- off(e, t) {
19
- this.ee.off(e, t);
20
- }
21
- cancel(e) {
22
- if (e) {
23
- let t = this._jobs.get(e);
24
- t && (t.canceled = !0, t.beamer.cancel());
25
- } else for (let e of this._jobs.values()) e.canceled = !0, e.beamer.cancel();
26
- }
27
- startJob(e) {
28
- this._busyCount++, this.ee.emit("status", "busy"), e.beamer.beam((t) => {
29
- this.ee.emit("progress", e.id, t);
30
- }).then((t) => {
31
- this._busyCount--, this._busyCount === 0 && this.ee.emit("status", "ready"), this.ee.emit("done", e.id, t);
32
- let n = this._queue.shift();
33
- n && this.startJob(n);
34
- }).catch((t) => {
35
- this._busyCount--, this._busyCount === 0 && this.ee.emit("status", "ready"), this.ee.emit("error", t), this._jobs.delete(e.id);
36
- let n = this._queue.shift();
37
- n && this.startJob(n);
38
- });
39
- }
40
- more(e) {
41
- let t = this._jobs.get(e);
42
- if (t) {
43
- if (this._busyCount > 0) return this._queue.push(t), e;
44
- this.startJob(t);
45
- }
46
- }
47
- create(e, r) {
48
- let i = n(), a = new t(this._model, this._tokeniser);
49
- a.begin(e, r);
50
- let o = {
51
- id: i,
52
- beamer: a,
53
- canceled: !1,
54
- conversation: e,
55
- options: r
56
- };
57
- for (let [e, t] of this._jobs.entries()) t.beamer.dispose(), this._jobs.delete(e);
58
- return this._jobs.set(i, o), this._busyCount > 0 ? (this._queue.push(o), i) : (this.startJob(o), i);
59
- }
60
- };
61
- export { r as default };
@@ -1,177 +0,0 @@
1
- import { t as e } from "../Generator-8IK28IWR.js";
2
- import { t } from "../eventemitter3-wmfmOP_f.js";
3
- import { t as n } from "../v4-YCkfo8OR.js";
4
- // lib/api/responses.ts
5
- var r = class {
6
- ee;
7
- _model;
8
- _tokeniser;
9
- _busyCount = 0;
10
- _responses = /* @__PURE__ */ new Map();
11
- _jobQueue = [];
12
- _hookedResponses = /* @__PURE__ */ new Set();
13
- _resumeWaiters = /* @__PURE__ */ new Map();
14
- constructor(e, n) {
15
- this._model = e, this._tokeniser = n, this.ee = new t();
16
- }
17
- get queued() {
18
- return this._jobQueue.length;
19
- }
20
- async _processNextJob() {
21
- if (this._jobQueue.length === 0) return;
22
- let e = this._jobQueue.shift();
23
- try {
24
- let t = await this._runForRecord(e.id, e.options, e.callback);
25
- e.resolve && e.resolve(t);
26
- } catch (t) {
27
- e.reject && e.reject(t), this.ee.emit("error", t);
28
- }
29
- }
30
- async _runForRecord(e, t, n) {
31
- let r = t.previous_response_id ? this._responses.get(t.previous_response_id) : void 0;
32
- r && (t.input && Array.isArray(t.input) || t.input && typeof t.input == "string" && (t.input = [{
33
- role: t.nonConversational ? "text" : "user",
34
- content: t.input
35
- }]));
36
- let i = r ? r.generator : this._responses.get(e).generator, a = n ? () => n({
37
- output: i.getConversation(),
38
- id: e,
39
- done: !1
40
- }) : null;
41
- a && i.on("tokens", a);
42
- let o = this._responses.get(e), s = {
43
- ...t,
44
- _onChunk: async () => {
45
- this._hookedResponses.has(e) && await new Promise((t) => {
46
- let n = this._resumeWaiters.get(e) || [];
47
- n.push(t), this._resumeWaiters.set(e, n), this.ee.emit("hook", e);
48
- });
49
- }
50
- };
51
- this.ee.emit("generating", t.previous_response_id || e);
52
- let c = t.input ? i.generate(Array.isArray(t.input) ? t.input : [{
53
- role: t.nonConversational ? "text" : "user",
54
- content: t.input
55
- }], s) : i.generate(s);
56
- if (t.background) {
57
- let t = {
58
- output: null,
59
- id: e,
60
- done: !1
61
- };
62
- return c.then((n) => {
63
- o.done = !0, t.done = !0, t.output = n, this._hookedResponses.delete(e), this._resumeWaiters.delete(e);
64
- let r = n[0]?._id || e;
65
- n.forEach((e) => {
66
- e._id === void 0 && (e._id = r);
67
- }), a && i.off("tokens", a), this.ee.emit("done", e), this._processNextJob();
68
- }), t;
69
- }
70
- let l = await c, u = l[0]?._id || e;
71
- return l.forEach((e) => {
72
- e._id === void 0 && (e._id = u);
73
- }), a && i.off("tokens", a), o.done = !0, this._hookedResponses.delete(e), this._resumeWaiters.delete(e), this.ee.emit("done", e), this._processNextJob(), {
74
- output: l,
75
- id: e,
76
- done: !0
77
- };
78
- }
79
- on(e, t) {
80
- this.ee.on(e, t);
81
- }
82
- off(e, t) {
83
- this.ee.off(e, t);
84
- }
85
- generator() {
86
- if (!this._model || !this._tokeniser) throw Error("model_or_tokeniser_not_initialized.");
87
- let t = new e(this._model, this._tokeniser);
88
- return t.on("start", () => {
89
- this._busyCount === 0 && this.ee.emit("status", "busy"), this._busyCount++;
90
- }), t.on("stop", () => {
91
- this._busyCount--, this._busyCount === 0 && this.ee.emit("status", "ready");
92
- }), t;
93
- }
94
- _cleanup() {
95
- let e = Date.now();
96
- this._responses.forEach((t, n) => {
97
- t.done && e - t.timestamp > 3e5 && (t.generator.dispose(), this._responses.delete(n));
98
- });
99
- }
100
- async create(e, t) {
101
- let r = n(), i = e.previous_response_id ? this._responses.get(e.previous_response_id) : void 0;
102
- i ? i.timestamp = Date.now() : this._cleanup();
103
- let a = {
104
- id: r,
105
- generator: i ? i.generator : this.generator(),
106
- done: !1,
107
- timestamp: Date.now(),
108
- options: e,
109
- callback: t
110
- };
111
- if (this._responses.set(r, a), this._busyCount > 0 && !e.background) {
112
- if (this._jobQueue.length > 10) throw Error("queue_too_long");
113
- return new Promise((n, i) => {
114
- this._jobQueue.push({
115
- id: r,
116
- options: e,
117
- callback: t,
118
- resolve: n,
119
- reject: i
120
- });
121
- });
122
- }
123
- return this._runForRecord(r, e, t);
124
- }
125
- async retry(e, t) {
126
- let n = this._responses.get(e);
127
- if (n) {
128
- let r = n.generator.getConversation();
129
- if (t !== void 0 && t >= 0 && t < r.length && r.splice(t), n.done = !1, n.timestamp = Date.now(), this._busyCount > 0 && !n.options.background) {
130
- if (this._jobQueue.length > 10) throw Error("queue_too_long");
131
- return new Promise((t, r) => {
132
- this._jobQueue.push({
133
- id: e,
134
- options: n.options,
135
- callback: n.callback,
136
- resolve: t,
137
- reject: r
138
- });
139
- });
140
- }
141
- return this._runForRecord(e, n.options, n.callback);
142
- }
143
- throw Error("response_not_found");
144
- }
145
- getResponse(e) {
146
- let t = this._responses.get(e);
147
- return t ? {
148
- output: t.generator.getConversation(),
149
- id: t.id,
150
- done: t.done
151
- } : null;
152
- }
153
- cancel(e) {
154
- let t = this._responses.get(e);
155
- return t ? (this._hookedResponses.delete(e), (this._resumeWaiters.get(e) || []).forEach((e) => e()), this._resumeWaiters.delete(e), t.generator.stop(), !0) : !1;
156
- }
157
- hook(e) {
158
- return this._responses.has(e) ? (this._hookedResponses.add(e), !0) : !1;
159
- }
160
- unhook(e) {
161
- this._hookedResponses.delete(e), (this._resumeWaiters.get(e) || []).forEach((e) => e()), this._resumeWaiters.delete(e);
162
- }
163
- resume(e) {
164
- let t = this._resumeWaiters.get(e);
165
- if (!t || t.length === 0) return this._hookedResponses.has(e);
166
- let n = t.shift();
167
- return t.length === 0 && this._resumeWaiters.delete(e), n(), !0;
168
- }
169
- dispose() {
170
- this._resumeWaiters.forEach((e) => {
171
- e.forEach((e) => e());
172
- }), this._resumeWaiters.clear(), this._hookedResponses.clear(), this._responses.forEach((e) => {
173
- e.generator.dispose();
174
- }), this._responses.clear(), this.ee.removeAllListeners();
175
- }
176
- };
177
- export { r as default };
@@ -1,215 +0,0 @@
1
- import { t as e } from "../eventemitter3-wmfmOP_f.js";
2
- import { TokenStore as t } from "../training/tasks/TokenStore.js";
3
- import n from "../training/factory.js";
4
- import { t as r } from "../v4-YCkfo8OR.js";
5
- import i from "../training/prepareData.js";
6
- import a from "../utilities/datasetID.js";
7
- // lib/api/training.ts
8
- var o = class {
9
- ee;
10
- _model;
11
- _tokeniser;
12
- _jobs = /* @__PURE__ */ new Map();
13
- _breakCounter = 1;
14
- constructor(t, n) {
15
- this.ee = new e(), this._model = t, this._tokeniser = n;
16
- }
17
- setState(e, t, n) {
18
- let r = () => {
19
- throw Error(`invalid_state_transition_from_${e.state}_to_${t}`);
20
- };
21
- switch (t) {
22
- case "pending":
23
- e.state !== "completed" && e.state !== "paused" && e.state !== "cancelled" && e.state !== "pending" && r(), e.state = t;
24
- break;
25
- case "running":
26
- e.state !== "pending" && e.state !== "paused" && r(), e.state = t, this.ee.emit("running", e.id);
27
- break;
28
- case "paused":
29
- e.state !== "pausing" && r(), e.state = t, this.ee.emit("paused", e.id);
30
- break;
31
- case "completed":
32
- e.state !== "running" && r(), e.state = t, this.ee.emit("completed", e.id);
33
- break;
34
- case "pausing":
35
- e.state !== "running" && r(), e.state = t, this.ee.emit("pausing", e.id);
36
- break;
37
- case "cancelling":
38
- e.state !== "running" && e.state !== "paused" && e.state !== "pausing" && r(), e.state = t, this.ee.emit("cancelling", e.id);
39
- break;
40
- case "cancelled":
41
- e.state !== "paused" && e.state !== "cancelling" && r(), e.state = t, this.ee.emit("cancelled", e.id);
42
- break;
43
- case "error":
44
- e.state = t, n && this.ee.emit("error", e.id, n);
45
- break;
46
- default: throw Error("invalid_state");
47
- }
48
- }
49
- get activeJobs() {
50
- return this._jobs.values().reduce((e, t) => t.state === "running" ? e + 1 : e, 0);
51
- }
52
- get training() {
53
- return this.activeJobs > 0;
54
- }
55
- assertValidJobId(e) {
56
- if (typeof e != "string" || e.trim().length === 0) throw Error("invalid_job_id");
57
- }
58
- assertValidOptions(e) {
59
- if (!Number.isInteger(e.batchSize) || e.batchSize <= 0) throw Error("invalid_batch_size");
60
- if (e.maxEpochs !== void 0 && (!Number.isFinite(e.maxEpochs) || e.maxEpochs <= 0)) throw Error("invalid_max_epochs");
61
- if (e.logInterval !== void 0 && (!Number.isInteger(e.logInterval) || e.logInterval <= 0)) throw Error("invalid_log_interval");
62
- if (e.learningRate !== void 0 && (!Number.isFinite(e.learningRate) || e.learningRate <= 0)) throw Error("invalid_learning_rate");
63
- if (e.validationSplit !== void 0 && (!Number.isFinite(e.validationSplit) || e.validationSplit <= 0 || e.validationSplit >= 1)) throw Error("invalid_validation_split");
64
- if (e.dropout !== void 0 && (!Number.isFinite(e.dropout) || e.dropout < 0 || e.dropout > 1)) throw Error("invalid_dropout");
65
- if (e.layerDrop !== void 0 && (!Number.isFinite(e.layerDrop) || e.layerDrop < 0 || e.layerDrop > 1)) throw Error("invalid_layer_drop");
66
- if (e.labelSmoothing !== void 0 && (!Number.isFinite(e.labelSmoothing) || e.labelSmoothing < 0 || e.labelSmoothing > 1)) throw Error("invalid_label_smoothing");
67
- }
68
- assertValidDatasets(e) {
69
- if (e.length === 0) throw Error("invalid_datasets");
70
- let t = /* @__PURE__ */ new Set();
71
- for (let n of e) {
72
- if (!n.id.trim()) throw Error("invalid_dataset_id");
73
- if (!n.name.trim()) throw Error("invalid_dataset_name");
74
- if (t.has(n.id)) throw Error("duplicate_dataset_id");
75
- t.add(n.id);
76
- }
77
- }
78
- assertValidDataInput(e) {
79
- if (e instanceof t) {
80
- if (e.tokeniserId !== this._tokeniser.id) throw Error("tokeniser_mismatch");
81
- if (e.getTokenCount() === 0) throw Error("empty_training_data");
82
- } else if (e.length === 0) throw Error("empty_training_data");
83
- }
84
- on(e, t) {
85
- this.ee.on(e, t);
86
- }
87
- off(e, t) {
88
- this.ee.off(e, t);
89
- }
90
- restore(e, t, i, o) {
91
- if (this.assertValidOptions(e), this.assertValidDatasets(o), t.length === 0) throw Error("invalid_log");
92
- let s = {
93
- id: r(),
94
- state: "completed",
95
- trainer: n(this._model, this._tokeniser, e, i),
96
- history: t,
97
- progress: t[t.length - 1]?.totalTokens / t[t.length - 1]?.duration || 0,
98
- remaining: 0,
99
- options: e,
100
- totalTokens: t[t.length - 1]?.totalTokens || 0,
101
- datasets: o,
102
- datasetId: a(o),
103
- breakOnLog: /* @__PURE__ */ new Set(),
104
- resumeCounter: 0
105
- };
106
- return s.trainer.log = t, s.trainer.resumeFromLog(t[t.length - 1]), this._jobs.set(s.id, s), s;
107
- }
108
- launchJob(e) {
109
- e.trainDataset && (this.setState(e, "running"), e.trainer.prepareToTrain(e.options), e.trainer.trainOnDataset(e.trainDataset, e.options, e.validationDataset, (t) => {
110
- e.history = e.trainer.log, e.progress = t.totalTokens / e.totalTokens, e.remaining = Math.max(0, (e.totalTokens - t.totalTokens) / t.totalTokens * t.duration), e.breakOnLog.size > 0 && e.state === "running" && (this.setState(e, "pausing"), e.trainer.stop()), this.ee.emit("progress", e);
111
- }).then(() => {
112
- e.state === "running" ? this.setState(e, "completed") : e.state === "pausing" ? this.setState(e, "paused") : e.state === "cancelling" && this.setState(e, "cancelled");
113
- }).catch((t) => {
114
- this.setState(e, "error", t);
115
- }));
116
- }
117
- async job(e, o = [], s, c) {
118
- if (this.assertValidOptions(e), this.assertValidDataInput(o), this.assertValidDatasets(s), c && c instanceof t && c.tokeniserId !== this._tokeniser.id) throw Error("tokeniser_mismatch");
119
- let l = Array.from(this._jobs.keys());
120
- for (let t of l) {
121
- let n = this._jobs.get(t);
122
- if (n && n.id !== e.previous_job_id) {
123
- if (n.options.method.type === e.method.type && n.state !== "running") n.trainer.dispose(), this._jobs.delete(n.id);
124
- else if (n.options.method.type === e.method.type && n.state === "running") throw Error("training_in_progress");
125
- }
126
- }
127
- let u = e.previous_job_id ? this.getJob(e.previous_job_id) : {
128
- id: r(),
129
- state: "pending",
130
- trainer: n(this._model, this._tokeniser, e),
131
- history: [],
132
- progress: 0,
133
- remaining: 0,
134
- options: e,
135
- totalTokens: 0,
136
- datasets: s,
137
- breakOnLog: /* @__PURE__ */ new Set(),
138
- resumeCounter: 0
139
- };
140
- if (!u) throw Error("invalid_previous_job_id");
141
- if (this._jobs.set(u.id, u), this.setState(u, "pending"), o instanceof t) {
142
- if (u.datasetId && o.datasetId !== u.datasetId) {
143
- let e = /* @__PURE__ */ Error("dataset_mismatch");
144
- throw this.setState(u, "error", e), e;
145
- }
146
- if (o.tokeniserId !== this._tokeniser.id) {
147
- let e = /* @__PURE__ */ Error("tokeniser_mismatch");
148
- throw this.setState(u, "error", e), e;
149
- }
150
- u.datasetId = o.datasetId;
151
- }
152
- if (!u.trainDataset) try {
153
- let t = this._model.metaData.mode, n = await i(e, this._model, this._tokeniser, o, u.datasetId ?? a(s), c, s);
154
- t !== this._model.metaData.mode && this.ee.emit("mode"), u.trainDataset = n.trainDataset, u.validationDataset = n.validationDataset, u.totalTokens = n.totalTokens;
155
- } catch (e) {
156
- throw this.setState(u, "error", /* @__PURE__ */ Error("prepare_data_failed")), u.trainer.dispose(), this._jobs.delete(u.id), e;
157
- }
158
- if (e.previous_job_id) {
159
- this.assertValidOptions(e), u.options = e;
160
- try {
161
- u.trainer.configure(e);
162
- } catch (e) {
163
- throw this.setState(u, "error", /* @__PURE__ */ Error("trainer_configuration_failed")), u.trainer.dispose(), this._jobs.delete(u.id), e;
164
- }
165
- } else try {
166
- u.trainer.configure(e);
167
- } catch (e) {
168
- throw this.setState(u, "error", /* @__PURE__ */ Error("trainer_configuration_failed")), u.trainer.dispose(), this._jobs.delete(u.id), e;
169
- }
170
- return this.launchJob(u), u;
171
- }
172
- getJob(e) {
173
- return this.assertValidJobId(e), this._jobs.get(e) ?? null;
174
- }
175
- async resume(e) {
176
- this.assertValidJobId(e);
177
- let t = this.getJob(e);
178
- t && t.state === "paused" && (t.resumeCounter++, t.resumeCounter < t.breakOnLog.size || (t.resumeCounter = 0, this.setState(t, "pending"), this.launchJob(t)));
179
- }
180
- cancel(e) {
181
- if (e) {
182
- this.assertValidJobId(e);
183
- let t = this.getJob(e);
184
- if (!t) return;
185
- if (t.state === "paused") {
186
- this.setState(t, "cancelled");
187
- return;
188
- }
189
- (t.state === "running" || t.state === "pausing") && (this.setState(t, "cancelling"), t.trainer.stop());
190
- } else for (let e of this._jobs.values()) e.state === "paused" ? this.setState(e, "cancelled") : (e.state === "running" || e.state === "pausing") && (this.setState(e, "cancelling"), e.trainer.stop());
191
- }
192
- addBreak(e) {
193
- let t = this.getJob(e);
194
- if (t) {
195
- let e = this._breakCounter++;
196
- return t.breakOnLog.add(e), e;
197
- }
198
- }
199
- deleteBreak(e, t) {
200
- let n = this.getJob(e);
201
- n && n.breakOnLog.delete(t);
202
- }
203
- getPretrainingJob() {
204
- for (let e of this._jobs.values()) if (e.options.method.type === "pretraining") return e;
205
- return null;
206
- }
207
- dispose() {
208
- for (let e of this._jobs.values()) {
209
- if (e.state === "running") throw Error("job_still_running");
210
- e.trainer.dispose();
211
- }
212
- this._jobs.clear(), this.ee.removeAllListeners();
213
- }
214
- };
215
- export { o as default };