@genai-fi/nanogpt 1.4.1 → 1.6.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 (341) hide show
  1. package/dist/{DatasetBuilder-DU1G1OKX.js → DatasetBuilder-DRDW8tcM.js} +85 -86
  2. package/dist/Generator-BcR_14Cj.js +316 -0
  3. package/dist/{RealDiv-CSnvtN2E.js → RealDiv-Cl8k3YTx.js} +54 -62
  4. package/dist/{Reshape-BlylqwWy.js → Reshape-CKxKPKqi.js} +9 -12
  5. package/dist/Reshape-H5zZRgr9.js +16 -0
  6. package/dist/RoPECache-dYscFINl.js +722 -0
  7. package/dist/TeachableLLM.js +29 -34
  8. package/dist/api/beamer.d.ts +1 -0
  9. package/dist/api/beamer.js +19 -10
  10. package/dist/api/responses.js +11 -12
  11. package/dist/api/training.js +12 -14
  12. package/dist/axis_util-rF9RnwKE.js +37 -0
  13. package/dist/backend.js +5 -6
  14. package/dist/{backend_util-DAV0I34P.js → backend_util-Cpp0Gwlp.js} +180 -185
  15. package/dist/{binary_op_util-CrYk9LXL.js → binary_op_util-WY9YIl0q.js} +3 -7
  16. package/dist/broadcast_to-CGvLNiQ0.js +20 -0
  17. package/dist/checks/appendCache.js +7 -7
  18. package/dist/checks/attentionMask.js +9 -8
  19. package/dist/checks/check.js +1 -2
  20. package/dist/checks/gelu.js +6 -6
  21. package/dist/checks/index.js +24 -25
  22. package/dist/checks/matMulGelu.js +7 -7
  23. package/dist/checks/normRMS.js +14 -12
  24. package/dist/checks/normRMSGrad.js +9 -8
  25. package/dist/checks/packUnpack.js +7 -7
  26. package/dist/checks/qkv.js +1 -34
  27. package/dist/checks/rope.js +6 -6
  28. package/dist/checks/weights.js +1 -2
  29. package/dist/clip_by_value-D-UX9am5.js +13 -0
  30. package/dist/complex-Cl904TqN.js +13 -0
  31. package/dist/{complex_util-CkazZsaH.js → complex_util-BBlPAm-s.js} +1 -2
  32. package/dist/concat-Oip5acVY.js +14 -0
  33. package/dist/{concat_util-CWzvQQlK.js → concat_util-CY45Hf4F.js} +2 -3
  34. package/dist/data/docx.js +3 -4
  35. package/dist/data/pdf.js +11 -14
  36. package/dist/data/stream.js +1 -2
  37. package/dist/data/textLoader.js +16 -15
  38. package/dist/data-CV_i_H6B.js +11 -0
  39. package/dist/data.js +5 -12
  40. package/dist/dist-CMMLi-qO.js +2675 -0
  41. package/dist/{dist-BGVm64AY.js → dist-DeVH1nGn.js} +1465 -1573
  42. package/dist/{dist-CwK5S7Ls.js → dist-DyL7qSLJ.js} +2664 -2400
  43. package/dist/dropout16-CA31Ud5a.js +20 -0
  44. package/dist/dropout_util-SiFMbFXz.js +22 -0
  45. package/dist/{eventemitter3-D_qV3Lof.js → eventemitter3-wmfmOP_f.js} +2 -3
  46. package/dist/expand_dims-Dh3Fulyn.js +10 -0
  47. package/dist/floor-BwjmUb7X.js +8 -0
  48. package/dist/gather-B5Z1RFt5.js +14 -0
  49. package/dist/gatherSub-CRUfUzBa.js +22 -0
  50. package/dist/gelu-Xx9-ohH9.js +25 -0
  51. package/dist/{gpgpu_math-20tPK8LM.js → gpgpu_math-48m8q3jI.js} +265 -286
  52. package/dist/inference/Beamer.d.ts +14 -2
  53. package/dist/inference/Beamer.js +170 -105
  54. package/dist/inference/Generator.js +1 -259
  55. package/dist/inference/tokenisePrompt.d.ts +2 -1
  56. package/dist/inference/tokenisePrompt.js +15 -11
  57. package/dist/inference/types.d.ts +7 -0
  58. package/dist/inference/utilities.js +1 -2
  59. package/dist/{jszip.min-DKa1Rjyn.js → jszip.min-Btv48m44.js} +68 -64
  60. package/dist/{kernel_funcs_utils-ql8Y8qPn.js → kernel_funcs_utils-BYRpSfV_.js} +39 -42
  61. package/dist/layers/BaseLayer.js +9 -10
  62. package/dist/layers/{CausalSelfAttention.d.ts → GQA.d.ts} +8 -3
  63. package/dist/layers/GQA.js +102 -0
  64. package/dist/layers/HeadLayer.d.ts +12 -0
  65. package/dist/layers/HeadLayer.js +28 -0
  66. package/dist/layers/LoRA.js +17 -20
  67. package/dist/layers/MHA.d.ts +43 -0
  68. package/dist/layers/MHA.js +102 -0
  69. package/dist/layers/MLP.d.ts +5 -1
  70. package/dist/layers/MLP.js +33 -20
  71. package/dist/layers/PositionEmbedding.js +14 -13
  72. package/dist/layers/RMSNorm.d.ts +2 -0
  73. package/dist/layers/RMSNorm.js +16 -11
  74. package/dist/layers/RoPECache.js +1 -337
  75. package/dist/layers/TiedEmbedding.js +15 -14
  76. package/dist/layers/TransformerBlock.d.ts +4 -10
  77. package/dist/layers/TransformerBlock.js +29 -30
  78. package/dist/layers/WeightStore.js +4 -5
  79. package/dist/layers-CFqEip3s.js +13 -0
  80. package/dist/layers.d.ts +1 -1
  81. package/dist/layers.js +4 -12
  82. package/dist/loader/load.js +27 -24
  83. package/dist/loader/loadHF.js +3 -3
  84. package/dist/loader/loadTransformers.js +22 -7
  85. package/dist/loader/loadZipMeta.js +1 -2
  86. package/dist/loader/newZipLoad.js +1 -2
  87. package/dist/loader/oldZipLoad.js +8 -9
  88. package/dist/loader/save.js +31 -17
  89. package/dist/loader/types.d.ts +8 -2
  90. package/dist/main.js +3281 -3463
  91. package/dist/matMul16-tE26fnhf.js +87 -0
  92. package/dist/{matMulGelu-CBoqTZM7.js → matMulGelu-ASUWlKqu.js} +33 -33
  93. package/dist/mat_mul-R9_DWmIT.js +16 -0
  94. package/dist/models/Llama.d.ts +16 -0
  95. package/dist/models/Llama.js +88 -0
  96. package/dist/models/NanoGPTV1.js +53 -54
  97. package/dist/models/NanoGPTV2.js +48 -49
  98. package/dist/models/Qwen3.d.ts +16 -0
  99. package/dist/models/Qwen3.js +88 -0
  100. package/dist/models/config.d.ts +18 -1
  101. package/dist/models/config.js +3 -8
  102. package/dist/models/factory.js +14 -11
  103. package/dist/models/model.d.ts +5 -7
  104. package/dist/models/model.js +1 -2
  105. package/dist/models-DyBpJJwM.js +13 -0
  106. package/dist/models.js +6 -14
  107. package/dist/mul16-C7sqgqik.js +41 -0
  108. package/dist/non_max_suppression_impl-B0H318rb.js +72 -0
  109. package/dist/not_equal-Bu3O8rh_.js +67 -0
  110. package/dist/one_hot-TEMPumJj.js +14 -0
  111. package/dist/ones-DpPXWa6A.js +13 -0
  112. package/dist/ops/adamAdjust.js +2 -3
  113. package/dist/ops/adamMoments.js +2 -3
  114. package/dist/ops/add16.js +2 -3
  115. package/dist/ops/appendCache.js +9 -8
  116. package/dist/ops/attentionMask.d.ts +1 -1
  117. package/dist/ops/attentionMask.js +8 -5
  118. package/dist/ops/concat16.js +5 -5
  119. package/dist/ops/cpu/adamAdjust.js +7 -8
  120. package/dist/ops/cpu/adamMoments.js +5 -5
  121. package/dist/ops/cpu/appendCache.js +3 -3
  122. package/dist/ops/cpu/attentionMask.js +12 -8
  123. package/dist/ops/cpu/fusedSoftmax.js +3 -3
  124. package/dist/ops/cpu/gatherSub.js +1 -12
  125. package/dist/ops/cpu/gelu.js +12 -13
  126. package/dist/ops/cpu/matMul16.js +7 -7
  127. package/dist/ops/cpu/matMulGelu.js +21 -18
  128. package/dist/ops/cpu/matMulMul.js +8 -9
  129. package/dist/ops/cpu/mulDropout.js +8 -9
  130. package/dist/ops/cpu/normRMS.js +12 -13
  131. package/dist/ops/cpu/qkv.js +14 -13
  132. package/dist/ops/cpu/rope.js +11 -8
  133. package/dist/ops/cpu/scatterSub.js +1 -12
  134. package/dist/ops/dot16.js +16 -17
  135. package/dist/ops/dropout.js +9 -8
  136. package/dist/ops/dropout16.js +1 -22
  137. package/dist/ops/gatherSub.js +3 -4
  138. package/dist/ops/gelu.js +1 -2
  139. package/dist/ops/globalNorm.js +14 -14
  140. package/dist/ops/grads/add16.js +16 -17
  141. package/dist/ops/grads/attentionMask.js +6 -7
  142. package/dist/ops/grads/dropout16.js +1 -1
  143. package/dist/ops/grads/gelu.js +1 -2
  144. package/dist/ops/grads/matMul16.js +1 -2
  145. package/dist/ops/grads/matMulGelu.js +6 -7
  146. package/dist/ops/grads/mul16.js +1 -1
  147. package/dist/ops/grads/normRMS.js +8 -9
  148. package/dist/ops/grads/pack16.js +1 -2
  149. package/dist/ops/grads/qkv.js +10 -10
  150. package/dist/ops/grads/rope.js +1 -2
  151. package/dist/ops/grads/silu.d.ts +2 -0
  152. package/dist/ops/grads/silu.js +1 -0
  153. package/dist/ops/grads/softmax16.js +6 -7
  154. package/dist/ops/grads/unpack16.js +1 -2
  155. package/dist/ops/grads/utils.js +1 -2
  156. package/dist/ops/matMul16.d.ts +2 -2
  157. package/dist/ops/matMul16.js +1 -2
  158. package/dist/ops/matMulGelu.js +3 -4
  159. package/dist/ops/matMulMul.js +2 -3
  160. package/dist/ops/mul16.js +1 -43
  161. package/dist/ops/mulDrop.js +2 -3
  162. package/dist/ops/normRMS.d.ts +1 -1
  163. package/dist/ops/normRMS.js +10 -11
  164. package/dist/ops/pack16.js +1 -2
  165. package/dist/ops/qkv.js +2 -3
  166. package/dist/ops/reshape16.js +11 -11
  167. package/dist/ops/rope.d.ts +1 -1
  168. package/dist/ops/rope.js +1 -2
  169. package/dist/ops/scatterSub.js +3 -4
  170. package/dist/ops/silu.d.ts +3 -0
  171. package/dist/ops/silu.js +1 -0
  172. package/dist/ops/slice16.js +5 -5
  173. package/dist/ops/softmax16.js +3 -4
  174. package/dist/ops/sub16.js +3 -4
  175. package/dist/ops/sum16.js +3 -3
  176. package/dist/ops/transpose16.js +16 -15
  177. package/dist/ops/unpack16.js +1 -2
  178. package/dist/ops/webgl/adamAdjust.js +8 -9
  179. package/dist/ops/webgl/adamMoments.js +2 -3
  180. package/dist/ops/webgl/appendCache.js +2 -3
  181. package/dist/ops/webgl/attentionMask.js +2 -3
  182. package/dist/ops/webgl/dropout16.js +2 -3
  183. package/dist/ops/webgl/fusedSoftmax.js +13 -13
  184. package/dist/ops/webgl/gatherSub.js +2 -3
  185. package/dist/ops/webgl/gelu.js +11 -12
  186. package/dist/ops/webgl/log.js +10 -10
  187. package/dist/ops/webgl/matMul16.js +31 -24
  188. package/dist/ops/webgl/matMulGelu.js +1 -2
  189. package/dist/ops/webgl/matMulMul.js +3 -4
  190. package/dist/ops/webgl/mulDropout.js +2 -3
  191. package/dist/ops/webgl/normRMS.js +11 -10
  192. package/dist/ops/webgl/qkv.js +2 -3
  193. package/dist/ops/webgl/rope.js +10 -9
  194. package/dist/ops/webgl/scatterSub.js +2 -3
  195. package/dist/ops/webgpu/adamAdjust.js +13 -14
  196. package/dist/ops/webgpu/adamMoments.js +13 -14
  197. package/dist/ops/webgpu/add16.js +3 -4
  198. package/dist/ops/webgpu/appendCache.js +20 -21
  199. package/dist/ops/webgpu/attentionMask.js +18 -19
  200. package/dist/ops/webgpu/attentionMask32_program.js +7 -8
  201. package/dist/ops/webgpu/clipScale.js +11 -12
  202. package/dist/ops/webgpu/concat16.js +17 -18
  203. package/dist/ops/webgpu/dropout16.js +6 -7
  204. package/dist/ops/webgpu/gatherSub.js +11 -12
  205. package/dist/ops/webgpu/gelu.js +10 -11
  206. package/dist/ops/webgpu/index.js +17 -16
  207. package/dist/ops/webgpu/matMul16.js +74 -57
  208. package/dist/ops/webgpu/matMul16_program.d.ts +4 -3
  209. package/dist/ops/webgpu/matMul16_program.js +30 -22
  210. package/dist/ops/webgpu/mul16.js +3 -4
  211. package/dist/ops/webgpu/norm2.js +9 -10
  212. package/dist/ops/webgpu/normRMS.js +13 -14
  213. package/dist/ops/webgpu/normRMS16_program.d.ts +2 -1
  214. package/dist/ops/webgpu/normRMS16_program.js +5 -5
  215. package/dist/ops/webgpu/normRMS32_program.d.ts +2 -1
  216. package/dist/ops/webgpu/normRMS32_program.js +5 -5
  217. package/dist/ops/webgpu/normRMSGrad.js +30 -30
  218. package/dist/ops/webgpu/pack16.js +2 -3
  219. package/dist/ops/webgpu/pack16_program.js +3 -4
  220. package/dist/ops/webgpu/qkv.js +10 -11
  221. package/dist/ops/webgpu/rope.js +113 -49
  222. package/dist/ops/webgpu/scatterSub.js +12 -13
  223. package/dist/ops/webgpu/silu.d.ts +14 -0
  224. package/dist/ops/webgpu/silu.js +117 -0
  225. package/dist/ops/webgpu/slice16.js +17 -18
  226. package/dist/ops/webgpu/softmax16.js +9 -9
  227. package/dist/ops/webgpu/softmax16_program.js +9 -10
  228. package/dist/ops/webgpu/softmax16_subgroup_program.js +11 -12
  229. package/dist/ops/webgpu/softmax16grad.js +8 -9
  230. package/dist/ops/webgpu/sub16.js +3 -4
  231. package/dist/ops/webgpu/sum16.js +16 -15
  232. package/dist/ops/webgpu/transpose16.js +13 -12
  233. package/dist/ops/webgpu/transpose16_program.js +3 -4
  234. package/dist/ops/webgpu/transpose16_shared_program.js +17 -18
  235. package/dist/ops/webgpu/unpack16.js +8 -9
  236. package/dist/ops/webgpu/utils/binary_op.js +20 -21
  237. package/dist/ops/webgpu/utils/deviceInfo.js +1 -2
  238. package/dist/ops/webgpu/utils/reductions.js +21 -20
  239. package/dist/ops-Be5ACU5R.js +13 -0
  240. package/dist/ops-C1UpdEYM.js +509 -0
  241. package/dist/{pack16-BhuXNUS7.js → pack16-BHO4t1-M.js} +14 -17
  242. package/dist/pad-Dy-1IP3A.js +13 -0
  243. package/dist/patches/webgpu_backend.js +18 -19
  244. package/dist/patches/webgpu_base.js +2 -3
  245. package/dist/patches/webgpu_program.js +21 -22
  246. package/dist/{pdf-Bzuk68Vy.js → pdf-CbnQ75VG.js} +380 -701
  247. package/dist/{picomatch-BO4HbNfo.js → picomatch--TW6gyBJ.js} +229 -291
  248. package/dist/qkv-BQfNPucX.js +42 -0
  249. package/dist/rand_util-_vCY93Uw.js +44 -0
  250. package/dist/random_normal-DUIT8kyC.js +11 -0
  251. package/dist/range-CYvQC3fW.js +13 -0
  252. package/dist/relu-C4XULT8A.js +8 -0
  253. package/dist/reshape-DJb1jgMt.js +8 -0
  254. package/dist/resize_nearest_neighbor-DAqcTYUe.js +188 -0
  255. package/dist/{chunk-CWhphoD1.js → rolldown-runtime-CwxnZyOB.js} +4 -5
  256. package/dist/rope-JjBVufF_.js +24 -0
  257. package/dist/scatterSub-BOnqk-uY.js +27 -0
  258. package/dist/scatter_nd_util-BepMCpLY.js +34 -0
  259. package/dist/seedrandom-SHEGFK6H.js +282 -0
  260. package/dist/selu_util-DeOTf-sc.js +27 -0
  261. package/dist/shared-BIDMUBCs.js +1864 -0
  262. package/dist/{shared-CfTzpULd.js → shared-CX_iPI5K.js} +2 -3
  263. package/dist/silu-DsAE2cJ5.js +23 -0
  264. package/dist/slice-Dlb1FlzG.js +13 -0
  265. package/dist/{slice_util-Dve4TkDn.js → slice_util-BVgPRoij.js} +3 -4
  266. package/dist/softmax-Dpav_dWw.js +10 -0
  267. package/dist/split-DYnT8chz.js +11 -0
  268. package/dist/squeeze-B3XYGAlg.js +9 -0
  269. package/dist/stack-Dbbt9mIk.js +10 -0
  270. package/dist/step-DA-5DNu9.js +348 -0
  271. package/dist/{stream-BpAwcvHz.js → stream-BBe2apn4.js} +68 -88
  272. package/dist/sum-C8qNbFNp.js +13 -0
  273. package/dist/tensor-ALxWZfmz.js +7 -0
  274. package/dist/tensor1d-BHgkkhTE.js +9 -0
  275. package/dist/tensor2d-ARItvqVn.js +10 -0
  276. package/dist/tensor4d-D1zRBipJ.js +10 -0
  277. package/dist/tfjs_backend-CsXOum0h.js +685 -0
  278. package/dist/tile-DfyQ0KX-.js +10 -0
  279. package/dist/tokenise-DbS2Ea-T.js +14 -0
  280. package/dist/tokenise.js +6 -15
  281. package/dist/tokeniser/BaseTokeniser.js +5 -9
  282. package/dist/tokeniser/CharTokeniser.js +1 -2
  283. package/dist/tokeniser/bpe.js +19 -20
  284. package/dist/topk-BrU2pkrR.js +19 -0
  285. package/dist/training/AdamW.js +35 -31
  286. package/dist/training/BasicTrainer.js +56 -56
  287. package/dist/training/DatasetBuilder.js +1 -2
  288. package/dist/training/Evaluator.js +8 -5
  289. package/dist/training/LRScheduler.js +1 -2
  290. package/dist/training/PreTrainer.js +7 -8
  291. package/dist/training/SFTTrainer.js +7 -8
  292. package/dist/training/configure.js +16 -15
  293. package/dist/training/factory.js +1 -2
  294. package/dist/training/loss.js +1 -2
  295. package/dist/training/orthoGrad.js +2 -3
  296. package/dist/training/prepareData.js +18 -17
  297. package/dist/training/sparseCrossEntropy.js +38 -32
  298. package/dist/training/tasks/TokenStore.js +21 -22
  299. package/dist/training/tasks/tokenStream.js +1 -2
  300. package/dist/training/validateOptions.js +1 -2
  301. package/dist/training/validation.js +1 -2
  302. package/dist/transpose-BC-M_OoT.js +34 -0
  303. package/dist/unsorted_segment_sum-BelC7R-q.js +408 -0
  304. package/dist/utilities/arrayClose.js +5 -4
  305. package/dist/utilities/arrayShape.js +1 -2
  306. package/dist/utilities/datasetID.js +1 -2
  307. package/dist/utilities/dummy.js +15 -15
  308. package/dist/utilities/float16.js +10 -10
  309. package/dist/utilities/maxHeap.d.ts +16 -0
  310. package/dist/utilities/maxHeap.js +54 -0
  311. package/dist/utilities/multinomialCPU.d.ts +1 -2
  312. package/dist/utilities/multinomialCPU.js +6 -8
  313. package/dist/utilities/packed.js +2 -3
  314. package/dist/utilities/parameters.js +34 -19
  315. package/dist/utilities/performance.js +2 -3
  316. package/dist/utilities/profile.js +3 -4
  317. package/dist/utilities/random.js +1 -2
  318. package/dist/utilities/safetensors.js +14 -14
  319. package/dist/utilities/sentences.js +16 -13
  320. package/dist/utilities/tokenParse.js +1 -2
  321. package/dist/utilities/tokens.js +5 -6
  322. package/dist/utilities/topP.d.ts +1 -0
  323. package/dist/utilities/topP.js +20 -13
  324. package/dist/utilities/waitForModel.js +1 -2
  325. package/dist/utilities/weights.js +4 -4
  326. package/dist/utilities/yielder.js +1 -2
  327. package/dist/{v4-BK7K-jy_.js → v4-YCkfo8OR.js} +4 -7
  328. package/dist/{webgpu-IAn9VotJ.js → webgpu-BHX-uBym.js} +156 -163
  329. package/dist/{webgpu_program-B0PokuRJ.js → webgpu_program-DvkFqRlR.js} +28 -29
  330. package/dist/{webgpu_util-DfODg6XI.js → webgpu_util-5QTyovaf.js} +2 -3
  331. package/dist/zeros-hxKGkRfl.js +12 -0
  332. package/package.json +3 -3
  333. package/dist/Reshape-CwxdaU8n.js +0 -17
  334. package/dist/dist-Da20xy8E.js +0 -6375
  335. package/dist/gelu-B6637MKa.js +0 -27
  336. package/dist/layers/CausalSelfAttention.js +0 -99
  337. package/dist/matMul16-DGtlziMn.js +0 -81
  338. package/dist/rope-C8EAiSGY.js +0 -25
  339. package/dist/selu_util-B_AFEfoz.js +0 -24
  340. package/dist/shared-24EoeHJl.js +0 -1314
  341. package/dist/tfjs_backend-h5weiy1O.js +0 -614
@@ -0,0 +1,87 @@
1
+ import { H as e, bt as t } from "./dist-CMMLi-qO.js";
2
+ import { t as n } from "./gelu-Xx9-ohH9.js";
3
+ import { transpose16 as r } from "./ops/transpose16.js";
4
+ import { reshape16 as i } from "./ops/reshape16.js";
5
+ import { t as a } from "./mul16-C7sqgqik.js";
6
+ import { t as o } from "./silu-DsAE2cJ5.js";
7
+ import "./ops/webgl/matMul16.js";
8
+ import "./ops/cpu/matMul16.js";
9
+ import { isPackedTensor as s } from "./utilities/packed.js";
10
+ import { t as c } from "./pack16-BHO4t1-M.js";
11
+ // lib/ops/grads/matMul16.ts
12
+ var l = {
13
+ kernelName: "MatMul16",
14
+ inputsToSave: ["A", "B"],
15
+ outputsToSave: [],
16
+ gradFunc: (e, t, s) => {
17
+ let [c, l] = t;
18
+ if (Array.isArray(e)) throw Error("Expected dy to be a single Tensor");
19
+ let d = e, { transposeA: m, transposeB: h, scale: g, activation: _, originalShape: v, perm: y } = s;
20
+ if (y && v) {
21
+ let e = Array(y.length);
22
+ for (let t = 0; t < y.length; ++t) e[y[t]] = t;
23
+ let t = d;
24
+ d = r(d, e), t.dispose();
25
+ }
26
+ if (v) {
27
+ let e = d;
28
+ d = i(d, v), e.dispose();
29
+ }
30
+ if (_ === "gelu") {
31
+ let e = d, t = u(c, l, m, h);
32
+ d = n(e, t), e.dispose(), t.dispose();
33
+ } else if (_ === "relu2") {
34
+ let e = d, t = u(c, l, m, h, {
35
+ activation: "relu",
36
+ scale: 2
37
+ });
38
+ d = a(e, t), e.dispose(), t.dispose();
39
+ } else if (_ === "silu") {
40
+ let e = d, t = u(c, l, m, h);
41
+ d = o(e, t), e.dispose(), t.dispose();
42
+ }
43
+ if (!m && !h) return {
44
+ A: () => g === void 0 ? u(d, l, !1, !0) : f(d, l, g, !1, !0),
45
+ B: () => g === void 0 ? u(c, d, !0, !1) : p(c, d, g, !0, !1)
46
+ };
47
+ if (!m && h) return {
48
+ A: () => g === void 0 ? u(d, l, !1, !1) : f(d, l, g, !1, !1),
49
+ B: () => g === void 0 ? u(c, d, !0, !1) : p(c, d, g, !0, !1)
50
+ };
51
+ if (m && !h) return {
52
+ A: () => g === void 0 ? u(l, d, !1, !0) : p(l, d, g, !1, !0),
53
+ B: () => g === void 0 ? u(c, d, !1, !1) : p(c, d, g, !1, !1)
54
+ };
55
+ throw Error("Gradient for transposeA=true and transposeB=true is not supported yet.");
56
+ }
57
+ };
58
+ t(l);
59
+ // lib/ops/matMul16.ts
60
+ function u(t, n, r = !1, i = !1, a = {}, o) {
61
+ let l = s(t), u = s(n), d = l || u, f = !d || l ? t : c(t), p = !d || u ? n : c(n), m = e().runKernel("MatMul16", o ? {
62
+ A: f,
63
+ B: p,
64
+ mask: o
65
+ } : {
66
+ A: f,
67
+ B: p
68
+ }, {
69
+ transposeA: r,
70
+ transposeB: i,
71
+ ...a
72
+ });
73
+ return d && !l && f.dispose(), d && !u && p.dispose(), m;
74
+ }
75
+ function d(e, t, n, r = !1, i = !1) {
76
+ return u(e, t, r, i, { scale: n });
77
+ }
78
+ function f(e, t, n, r = !1, i = !1) {
79
+ return u(e, t, r, i, { scaleA: n });
80
+ }
81
+ function p(e, t, n, r = !1, i = !1) {
82
+ return u(e, t, r, i, { scaleB: n });
83
+ }
84
+ function m(e, t, n = !1, r = !1) {
85
+ return u(e, t, n, r, { activation: "gelu" });
86
+ }
87
+ export { d as a, p as i, m as n, l as o, f as r, u as t };
@@ -1,22 +1,23 @@
1
- import { Ci as e, Ii as t, In as n, Ps as r, di as i, gr as a, ii as o, oc as s } from "./dist-Da20xy8E.js";
2
- import { a as c } from "./gpgpu_math-20tPK8LM.js";
3
- import { t as l } from "./Reshape-BlylqwWy.js";
4
- //#region node_modules/@tensorflow/tfjs-backend-webgl/dist/mulmat_packed_gpu.js
1
+ import { C as e, H as t, Y as n, lt as r, qi as i, xt as a, yi as o } from "./dist-CMMLi-qO.js";
2
+ import { t as s } from "./mat_mul-R9_DWmIT.js";
3
+ import { t as c } from "./Reshape-CKxKPKqi.js";
4
+ import { a as l } from "./gpgpu_math-48m8q3jI.js";
5
+ // node_modules/@tensorflow/tfjs-backend-webgl/dist/mulmat_packed_gpu.js
5
6
  var u = class {
6
- constructor(e, t, n, r = !1, i = !1, a = !1, o = null, s = !1, l = !1) {
7
- this.variableNames = ["matrixA", "matrixB"], this.packedInputs = !0, this.packedOutput = !0, this.outputShape = n, this.enableShapeUniforms = c(this.outputShape.length);
7
+ constructor(e, t, n, r = !1, i = !1, a = !1, o = null, s = !1, c = !1) {
8
+ this.variableNames = ["matrixA", "matrixB"], this.packedInputs = !0, this.packedOutput = !0, this.outputShape = n, this.enableShapeUniforms = l(this.outputShape.length);
8
9
  let u = r ? e[1] : e[2], d = Math.ceil(u / 2), f = r ? "i * 2, rc.y" : "rc.y, i * 2", p = i ? "rc.z, i * 2" : "i * 2, rc.z", m = r ? ["a.xxyy", "a.zzww"] : ["a.xxzz", "a.yyww"], h = i ? ["b.xzxz", "b.ywyw"] : ["b.xyxy", "b.zwzw"], g = "", _ = "";
9
10
  o && (g = s ? `vec4 activation(vec4 a) {
10
11
  vec4 b = getPreluActivationWeightsAtOutCoords();
11
12
  ${o}
12
- }` : l ? `vec4 activation(vec4 a) {
13
+ }` : c ? `vec4 activation(vec4 a) {
13
14
  vec4 b = getLeakyreluAlphaAtOutCoords();
14
15
  ${o}
15
16
  }` : `vec4 activation(vec4 x) {
16
17
  ${o}
17
18
  }`, _ = "result = activation(result);");
18
19
  let v = a ? "result += getBiasAtOutCoords();" : "";
19
- a && this.variableNames.push("bias"), s && this.variableNames.push("preluActivationWeights"), l && this.variableNames.push("leakyreluAlpha");
20
+ a && this.variableNames.push("bias"), s && this.variableNames.push("preluActivationWeights"), c && this.variableNames.push("leakyreluAlpha");
20
21
  let y = "rc.x", b = "rc.x";
21
22
  e[0] < t[0] ? y = `imod(rc.x, ${e[0]})` : t[0] < e[0] && (b = `imod(rc.x, ${t[0]})`), this.userCode = `
22
23
  ${g}
@@ -79,9 +80,9 @@ var u = class {
79
80
  vec4 dgelu = 0.5 * (1.0 + t) + 0.5 * a * sech2 * du_dx;
80
81
  return dgelu * b;
81
82
  `, h = 1e3;
82
- function g({ a: t, b: i, transposeA: a, transposeB: o, backend: c, activationSnippet: d, multiplier: f }) {
83
- let p = t.shape.length, m = i.shape.length, h = a ? t.shape[p - 2] : t.shape[p - 1], g = o ? i.shape[m - 1] : i.shape[m - 2], _ = a ? t.shape[p - 1] : t.shape[p - 2], v = o ? i.shape[m - 2] : i.shape[m - 1], y = t.shape.slice(0, -2), b = i.shape.slice(0, -2), x = s(y), S = s(b), C = n(t.shape.slice(0, -2), i.shape.slice(0, -2)).concat([_, v]);
84
- r(h === g, () => `Error in matMul: inner shapes (${h}) and (${g}) of Tensors with shapes ${t.shape} and ${i.shape} and transposeA=${a} and transposeB=${o} must match.`);
83
+ function g({ a: t, b: n, transposeA: a, transposeB: s, backend: l, activationSnippet: d, multiplier: f }) {
84
+ let p = t.shape.length, m = n.shape.length, h = a ? t.shape[p - 2] : t.shape[p - 1], g = s ? n.shape[m - 1] : n.shape[m - 2], _ = a ? t.shape[p - 1] : t.shape[p - 2], v = s ? n.shape[m - 2] : n.shape[m - 1], y = t.shape.slice(0, -2), b = n.shape.slice(0, -2), x = i(y), S = i(b), C = e(t.shape.slice(0, -2), n.shape.slice(0, -2)).concat([_, v]);
85
+ o(h === g, () => `Error in matMul: inner shapes (${h}) and (${g}) of Tensors with shapes ${t.shape} and ${n.shape} and transposeA=${a} and transposeB=${s} must match.`);
85
86
  let w = a ? [
86
87
  x,
87
88
  h,
@@ -90,7 +91,7 @@ function g({ a: t, b: i, transposeA: a, transposeB: o, backend: c, activationSni
90
91
  x,
91
92
  _,
92
93
  h
93
- ], T = o ? [
94
+ ], T = s ? [
94
95
  S,
95
96
  v,
96
97
  g
@@ -98,27 +99,27 @@ function g({ a: t, b: i, transposeA: a, transposeB: o, backend: c, activationSni
98
99
  S,
99
100
  g,
100
101
  v
101
- ], E = l({
102
+ ], E = c({
102
103
  inputs: { x: t },
103
- backend: c,
104
+ backend: l,
104
105
  attrs: { shape: w }
105
- }), D = l({
106
- inputs: { x: i },
107
- backend: c,
106
+ }), D = c({
107
+ inputs: { x: n },
108
+ backend: l,
108
109
  attrs: { shape: T }
109
- }), O = [E, D], k = Math.max(x, S), A = d, j = e(t.dtype, i.dtype), M = new u(w, T, [
110
+ }), O = [E, D], k = Math.max(x, S), A = d, j = r(t.dtype, n.dtype), M = new u(w, T, [
110
111
  k,
111
112
  _,
112
113
  v
113
- ], a, o, !1, A, !!f, !1), N = [E, D];
114
+ ], a, s, !1, A, !!f, !1), N = [E, D];
114
115
  f && N.push(f);
115
- let P = c.runWebGLProgram(M, N, j), F = l({
116
+ let P = l.runWebGLProgram(M, N, j), F = c({
116
117
  inputs: { x: P },
117
- backend: c,
118
+ backend: l,
118
119
  attrs: { shape: C }
119
120
  });
120
121
  O.push(P);
121
- for (let e of O) c.disposeIntermediateTensorInfo(e);
122
+ for (let e of O) l.disposeIntermediateTensorInfo(e);
122
123
  return F;
123
124
  }
124
125
  function _(e) {
@@ -133,30 +134,29 @@ function _(e) {
133
134
  activationSnippet: p
134
135
  });
135
136
  }
136
- t({
137
+ a({
137
138
  kernelName: "MatMulGelu",
138
139
  backendName: "webgl",
139
140
  kernelFunc: _
140
141
  });
141
142
  function v(e) {
142
- let { dy: t, x: n, kernel: r } = e.inputs, s = e.backend;
143
- return i(() => {
144
- let e = o().makeTensorFromTensorInfo(g({
145
- a: n,
146
- b: r,
143
+ let { dy: r, x: i, kernel: a } = e.inputs, o = e.backend;
144
+ return n(() => {
145
+ let e = t().makeTensorFromTensorInfo(g({
146
+ a: i,
147
+ b: a,
147
148
  transposeA: !1,
148
149
  transposeB: !1,
149
- backend: s,
150
+ backend: o,
150
151
  activationSnippet: m,
151
- multiplier: t
152
+ multiplier: r
152
153
  }));
153
- return [a(e, r, !1, !0), a(n, e, !0, !1)];
154
+ return [s(e, a, !1, !0), s(i, e, !0, !1)];
154
155
  });
155
156
  }
156
- t({
157
+ a({
157
158
  kernelName: "MatMulGeluGrad",
158
159
  backendName: "webgl",
159
160
  kernelFunc: v
160
161
  });
161
- //#endregion
162
162
  export { u as i, g as n, _ as r, h as t };
@@ -0,0 +1,16 @@
1
+ import { Q as e, Rt as t, Z as n, rt as r, st as i } from "./dist-CMMLi-qO.js";
2
+ // node_modules/@tensorflow/tfjs-core/dist/ops/mat_mul.js
3
+ function a(n, a, o = !1, s = !1) {
4
+ let c = e(n, "a", "matMul"), l = e(a, "b", "matMul");
5
+ [c, l] = i(c, l);
6
+ let u = {
7
+ a: c,
8
+ b: l
9
+ }, d = {
10
+ transposeA: o,
11
+ transposeB: s
12
+ };
13
+ return r.runKernel(t, u, d);
14
+ }
15
+ var o = /* @__PURE__ */ n({ matMul_: a });
16
+ export { o as t };
@@ -0,0 +1,16 @@
1
+ import { GPTConfigLlama } from './config';
2
+ import { Tensor } from '@tensorflow/tfjs-core';
3
+ import { default as Model, ModelForwardAttributes } from './model';
4
+ export default class Llama extends Model<ModelForwardAttributes, GPTConfigLlama> {
5
+ private wte;
6
+ private head;
7
+ private blocks;
8
+ private lnF;
9
+ private ropeCache?;
10
+ constructor(config?: Partial<GPTConfigLlama>);
11
+ getClassName(): string;
12
+ private inputPhase;
13
+ forward(attrs: ModelForwardAttributes, idx: Tensor): Tensor;
14
+ project(embeddings: Tensor): Tensor;
15
+ dispose(): void;
16
+ }
@@ -0,0 +1,88 @@
1
+ import e from "../layers/TransformerBlock.js";
2
+ import t from "../layers/TiedEmbedding.js";
3
+ import { t as n } from "../RoPECache-dYscFINl.js";
4
+ import r from "../layers/RMSNorm.js";
5
+ import { W as i, Y as a } from "../dist-CMMLi-qO.js";
6
+ import o from "./model.js";
7
+ import { packingSupported as s } from "../utilities/packed.js";
8
+ import { r as c, t as l } from "../pack16-BHO4t1-M.js";
9
+ import u from "../layers/HeadLayer.js";
10
+ // lib/models/Llama.ts
11
+ var d = {
12
+ modelType: "llama",
13
+ vocabSize: 2e3,
14
+ blockSize: 128,
15
+ nLayer: 6,
16
+ nHead: 4,
17
+ nEmbed: 256,
18
+ mlpFactor: 4
19
+ }, f = class extends o {
20
+ wte;
21
+ head;
22
+ blocks;
23
+ lnF;
24
+ ropeCache;
25
+ constructor(i = {}) {
26
+ super({
27
+ ...d,
28
+ ...i
29
+ });
30
+ let a = {
31
+ activation: "swiglu",
32
+ hiddenSize: Math.max(256, Math.ceil(this.config.nEmbed * (8 / 3) / 256) * 256),
33
+ useGamma: !0,
34
+ useQKNorm: !1,
35
+ ropeInterleaved: !1
36
+ };
37
+ this.wte = new t(this.config, "token_embedding", this), this.ropeCache = new n(this.config), this.blocks = [];
38
+ for (let t = 0; t < this.config.nLayer; t++) this.blocks.push(new e(t, this.config, a, this));
39
+ this.lnF = new r(this.config, a, "final_rms_norm", this), this.head = new u(this.config, "lm_head", this);
40
+ }
41
+ getClassName() {
42
+ return "llama";
43
+ }
44
+ inputPhase(e) {
45
+ return a(() => this.wte.embed(e));
46
+ }
47
+ forward(e, t) {
48
+ return this.validateInput(t), e.ropeCache = this.ropeCache, e.outputEmbeddings && (e.embeddings = []), a(() => {
49
+ this.startMemory();
50
+ let n = this.inputPhase(t);
51
+ if (e.cache && e.cache.length !== this.blocks.length) throw console.error("Cache", e.cache), Error(`Cache length ${e.cache.length} does not match number of blocks ${this.blocks.length}`);
52
+ let r = e.mixedPrecision === !0 && s(), a = r ? l(n) : n;
53
+ r && n !== a && n.dispose();
54
+ for (let t = 0; t < this.blocks.length; t++) {
55
+ if (e.layerDrop && Math.random() < e.layerDrop * (t / this.blocks.length)) continue;
56
+ let n = this.blocks[t], o = Math.random() * 1e9, s = {
57
+ ...e,
58
+ seed: o,
59
+ pastKV: e.cache ? e.cache[t] : void 0,
60
+ mixedPrecision: r,
61
+ epsilon: 1e-6
62
+ }, c = e.checkpointing && e.training ? n.callCheckpoint(s, a) : n.call(s, a);
63
+ e.outputEmbeddings ? (i(a), e.embeddings.push({
64
+ name: `block_output_${t}`,
65
+ tensor: a
66
+ })) : a.dispose(), a = c;
67
+ }
68
+ if (n = this.lnF.call({
69
+ ...e,
70
+ mixedPrecision: r
71
+ }, a), a.dispose(), e.skipLogits) return this.endMemory("Forward"), n;
72
+ let o = this.head.project(n);
73
+ e.outputEmbeddings ? (i(n), e.embeddings.push({
74
+ name: "final_norm_output",
75
+ tensor: n
76
+ })) : n.dispose();
77
+ let u = r ? c(o) : o;
78
+ return r && o !== u && o.dispose(), u;
79
+ });
80
+ }
81
+ project(e) {
82
+ return a(() => this.head.project(e));
83
+ }
84
+ dispose() {
85
+ this.weightStore.dispose(), this.wte.dispose(), this.head.dispose(), this.blocks.forEach((e) => e.dispose()), this.lnF.dispose();
86
+ }
87
+ };
88
+ export { f as default };
@@ -1,13 +1,13 @@
1
- import { di as e, oi as t } from "../dist-Da20xy8E.js";
2
- import { packingSupported as n } from "../utilities/packed.js";
3
- import { r, t as i } from "../pack16-BhuXNUS7.js";
4
- import a from "../layers/RMSNorm.js";
5
- import o from "../layers/TransformerBlock.js";
6
- import s from "../layers/TiedEmbedding.js";
7
- import c from "../layers/RoPECache.js";
8
- import l from "./model.js";
9
- import u from "../layers/PositionEmbedding.js";
10
- //#region lib/models/NanoGPTV1.ts
1
+ import e from "../layers/TransformerBlock.js";
2
+ import t from "../layers/TiedEmbedding.js";
3
+ import { t as n } from "../RoPECache-dYscFINl.js";
4
+ import r from "../layers/RMSNorm.js";
5
+ import { W as i, Y as a } from "../dist-CMMLi-qO.js";
6
+ import o from "./model.js";
7
+ import s from "../layers/PositionEmbedding.js";
8
+ import { packingSupported as c } from "../utilities/packed.js";
9
+ import { r as l, t as u } from "../pack16-BHO4t1-M.js";
10
+ // lib/models/NanoGPTV1.ts
11
11
  var d = {
12
12
  modelType: "GenAI_NanoGPT_v1",
13
13
  vocabSize: 2e3,
@@ -17,79 +17,78 @@ var d = {
17
17
  nEmbed: 256,
18
18
  mlpFactor: 4,
19
19
  useRope: !0
20
- }, f = class extends l {
20
+ }, f = class extends o {
21
21
  wte;
22
22
  wpe;
23
23
  blocks;
24
24
  lnF;
25
25
  ropeCache;
26
- constructor(e = {}) {
26
+ constructor(i = {}) {
27
27
  super({
28
28
  ...d,
29
- ...e
29
+ ...i
30
30
  });
31
- let t = {
31
+ let a = {
32
32
  activation: "gelu",
33
33
  hiddenFactor: this.config.mlpFactor,
34
34
  useGamma: !0,
35
35
  useQKNorm: !1
36
36
  };
37
- this.wte = new s(this.config, "token_embedding", this), this.config.useRope === !1 ? this.wpe = new u(this.config, "positional_embedding", this) : this.ropeCache = new c(this.config), this.blocks = [];
38
- for (let e = 0; e < this.config.nLayer; e++) this.blocks.push(new o(e, this.config, t, this));
39
- this.lnF = new a(this.config, t, "final_rms_norm", this);
37
+ this.wte = new t(this.config, "token_embedding", this), this.config.useRope === !1 ? this.wpe = new s(this.config, "positional_embedding", this) : this.ropeCache = new n(this.config), this.blocks = [];
38
+ for (let t = 0; t < this.config.nLayer; t++) this.blocks.push(new e(t, this.config, a, this));
39
+ this.lnF = new r(this.config, a, "final_rms_norm", this);
40
40
  }
41
41
  getClassName() {
42
42
  return "GenAI_NanoGPT_v1";
43
43
  }
44
- inputPhase(t, n) {
45
- return e(() => {
46
- let e = this.wte.embed(t);
44
+ inputPhase(e, t) {
45
+ return a(() => {
46
+ let n = this.wte.embed(e);
47
47
  if (this.config.useRope === !1) {
48
- let t = this.wpe.call(n, e);
49
- if (Array.isArray(t)) throw Error("PositionEmbedding output should not be an array");
50
- return t;
48
+ let e = this.wpe.call(t, n);
49
+ if (Array.isArray(e)) throw Error("PositionEmbedding output should not be an array");
50
+ return e;
51
51
  }
52
- return e;
52
+ return n;
53
53
  });
54
54
  }
55
- forward(a, o) {
56
- return this.validateInput(o), a.ropeCache = this.ropeCache, a.outputEmbeddings && (a.embeddings = []), e(() => {
55
+ forward(e, t) {
56
+ return this.validateInput(t), e.ropeCache = this.ropeCache, e.outputEmbeddings && (e.embeddings = []), a(() => {
57
57
  this.startMemory();
58
- let e = this.inputPhase(o, a);
59
- if (a.cache && a.cache.length !== this.blocks.length) throw console.error("Cache", a.cache), Error(`Cache length ${a.cache.length} does not match number of blocks ${this.blocks.length}`);
60
- let s = a.mixedPrecision === !0 && n(), c = s ? i(e) : e;
61
- s && e !== c && e.dispose();
62
- for (let e = 0; e < this.blocks.length; e++) {
63
- let n = this.blocks[e], r = Math.random() * 1e9, i = {
64
- ...a,
65
- seed: r,
66
- pastKV: a.cache ? a.cache[e] : void 0,
67
- mixedPrecision: s
68
- }, o = a.checkpointing && a.training ? n.callCheckpoint(i, c) : n.call(i, c);
69
- a.outputEmbeddings ? (t(c), a.embeddings.push({
70
- name: `block_output_${e}`,
71
- tensor: c
72
- })) : c.dispose(), c = o;
58
+ let n = this.inputPhase(t, e);
59
+ if (e.cache && e.cache.length !== this.blocks.length) throw console.error("Cache", e.cache), Error(`Cache length ${e.cache.length} does not match number of blocks ${this.blocks.length}`);
60
+ let r = e.mixedPrecision === !0 && c(), a = r ? u(n) : n;
61
+ r && n !== a && n.dispose();
62
+ for (let t = 0; t < this.blocks.length; t++) {
63
+ let n = this.blocks[t], o = Math.random() * 1e9, s = {
64
+ ...e,
65
+ seed: o,
66
+ pastKV: e.cache ? e.cache[t] : void 0,
67
+ mixedPrecision: r
68
+ }, c = e.checkpointing && e.training ? n.callCheckpoint(s, a) : n.call(s, a);
69
+ e.outputEmbeddings ? (i(a), e.embeddings.push({
70
+ name: `block_output_${t}`,
71
+ tensor: a
72
+ })) : a.dispose(), a = c;
73
73
  }
74
- if (e = this.lnF.call({
75
- ...a,
76
- mixedPrecision: s
77
- }, c), c.dispose(), a.skipLogits) return this.endMemory("Forward"), e;
78
- let l = this.wte.project(e);
79
- a.outputEmbeddings ? (t(e), a.embeddings.push({
74
+ if (n = this.lnF.call({
75
+ ...e,
76
+ mixedPrecision: r
77
+ }, a), a.dispose(), e.skipLogits) return this.endMemory("Forward"), n;
78
+ let o = this.wte.project(n);
79
+ e.outputEmbeddings ? (i(n), e.embeddings.push({
80
80
  name: "final_norm_output",
81
- tensor: e
82
- })) : e.dispose();
83
- let u = s ? r(l) : l;
84
- return s && l !== u && l.dispose(), u;
81
+ tensor: n
82
+ })) : n.dispose();
83
+ let s = r ? l(o) : o;
84
+ return r && o !== s && o.dispose(), s;
85
85
  });
86
86
  }
87
- project(t) {
88
- return e(() => this.wte.project(t));
87
+ project(e) {
88
+ return a(() => this.wte.project(e));
89
89
  }
90
90
  dispose() {
91
91
  this.weightStore.dispose(), this.wte.dispose(), this.wpe && this.wpe.dispose(), this.blocks.forEach((e) => e.dispose()), this.lnF.dispose();
92
92
  }
93
93
  };
94
- //#endregion
95
94
  export { f as default };
@@ -1,12 +1,12 @@
1
- import { di as e, oi as t } from "../dist-Da20xy8E.js";
2
- import { packingSupported as n } from "../utilities/packed.js";
3
- import { r, t as i } from "../pack16-BhuXNUS7.js";
4
- import a from "../layers/RMSNorm.js";
5
- import o from "../layers/TransformerBlock.js";
6
- import s from "../layers/TiedEmbedding.js";
7
- import c from "../layers/RoPECache.js";
8
- import l from "./model.js";
9
- //#region lib/models/NanoGPTV2.ts
1
+ import e from "../layers/TransformerBlock.js";
2
+ import t from "../layers/TiedEmbedding.js";
3
+ import { t as n } from "../RoPECache-dYscFINl.js";
4
+ import r from "../layers/RMSNorm.js";
5
+ import { W as i, Y as a } from "../dist-CMMLi-qO.js";
6
+ import o from "./model.js";
7
+ import { packingSupported as s } from "../utilities/packed.js";
8
+ import { r as c, t as l } from "../pack16-BHO4t1-M.js";
9
+ // lib/models/NanoGPTV2.ts
10
10
  var u = {
11
11
  modelType: "GenAI_NanoGPT_v2",
12
12
  vocabSize: 2e3,
@@ -15,72 +15,71 @@ var u = {
15
15
  nHead: 4,
16
16
  nEmbed: 256,
17
17
  mlpFactor: 4
18
- }, d = class extends l {
18
+ }, d = class extends o {
19
19
  wte;
20
20
  wpe;
21
21
  blocks;
22
22
  lnF;
23
23
  ropeCache;
24
- constructor(e = {}) {
24
+ constructor(i = {}) {
25
25
  super({
26
26
  ...u,
27
- ...e
27
+ ...i
28
28
  });
29
- let t = {
29
+ let a = {
30
30
  activation: "relu2",
31
31
  hiddenFactor: this.config.mlpFactor,
32
32
  useGamma: !1,
33
33
  useQKNorm: !0
34
34
  };
35
- this.wte = new s(this.config, "token_embedding", this), this.ropeCache = new c(this.config), this.blocks = [];
36
- for (let e = 0; e < this.config.nLayer; e++) this.blocks.push(new o(e, this.config, t, this));
37
- this.lnF = new a(this.config, t, "final_rms_norm", this);
35
+ this.wte = new t(this.config, "token_embedding", this), this.ropeCache = new n(this.config), this.blocks = [];
36
+ for (let t = 0; t < this.config.nLayer; t++) this.blocks.push(new e(t, this.config, a, this));
37
+ this.lnF = new r(this.config, a, "final_rms_norm", this);
38
38
  }
39
39
  getClassName() {
40
40
  return "GenAI_NanoGPT_v2";
41
41
  }
42
- inputPhase(t) {
43
- return e(() => this.wte.embed(t));
42
+ inputPhase(e) {
43
+ return a(() => this.wte.embed(e));
44
44
  }
45
- forward(a, o) {
46
- return this.validateInput(o), a.ropeCache = this.ropeCache, a.outputEmbeddings && (a.embeddings = []), e(() => {
45
+ forward(e, t) {
46
+ return this.validateInput(t), e.ropeCache = this.ropeCache, e.outputEmbeddings && (e.embeddings = []), a(() => {
47
47
  this.startMemory();
48
- let e = this.inputPhase(o);
49
- if (a.cache && a.cache.length !== this.blocks.length) throw console.error("Cache", a.cache), Error(`Cache length ${a.cache.length} does not match number of blocks ${this.blocks.length}`);
50
- let s = a.mixedPrecision === !0 && n(), c = s ? i(e) : e;
51
- s && e !== c && e.dispose();
52
- for (let e = 0; e < this.blocks.length; e++) {
53
- if (a.layerDrop && Math.random() < a.layerDrop * (e / this.blocks.length)) continue;
54
- let n = this.blocks[e], r = Math.random() * 1e9, i = {
55
- ...a,
56
- seed: r,
57
- pastKV: a.cache ? a.cache[e] : void 0,
58
- mixedPrecision: s
59
- }, o = a.checkpointing && a.training ? n.callCheckpoint(i, c) : n.call(i, c);
60
- a.outputEmbeddings ? (t(c), a.embeddings.push({
61
- name: `block_output_${e}`,
62
- tensor: c
63
- })) : c.dispose(), c = o;
48
+ let n = this.inputPhase(t);
49
+ if (e.cache && e.cache.length !== this.blocks.length) throw console.error("Cache", e.cache), Error(`Cache length ${e.cache.length} does not match number of blocks ${this.blocks.length}`);
50
+ let r = e.mixedPrecision === !0 && s(), a = r ? l(n) : n;
51
+ r && n !== a && n.dispose();
52
+ for (let t = 0; t < this.blocks.length; t++) {
53
+ if (e.layerDrop && Math.random() < e.layerDrop * (t / this.blocks.length)) continue;
54
+ let n = this.blocks[t], o = Math.random() * 1e9, s = {
55
+ ...e,
56
+ seed: o,
57
+ pastKV: e.cache ? e.cache[t] : void 0,
58
+ mixedPrecision: r
59
+ }, c = e.checkpointing && e.training ? n.callCheckpoint(s, a) : n.call(s, a);
60
+ e.outputEmbeddings ? (i(a), e.embeddings.push({
61
+ name: `block_output_${t}`,
62
+ tensor: a
63
+ })) : a.dispose(), a = c;
64
64
  }
65
- if (e = this.lnF.call({
66
- ...a,
67
- mixedPrecision: s
68
- }, c), c.dispose(), a.skipLogits) return this.endMemory("Forward"), e;
69
- let l = this.wte.project(e);
70
- a.outputEmbeddings ? (t(e), a.embeddings.push({
65
+ if (n = this.lnF.call({
66
+ ...e,
67
+ mixedPrecision: r
68
+ }, a), a.dispose(), e.skipLogits) return this.endMemory("Forward"), n;
69
+ let o = this.wte.project(n);
70
+ e.outputEmbeddings ? (i(n), e.embeddings.push({
71
71
  name: "final_norm_output",
72
- tensor: e
73
- })) : e.dispose();
74
- let u = s ? r(l) : l;
75
- return s && l !== u && l.dispose(), u;
72
+ tensor: n
73
+ })) : n.dispose();
74
+ let u = r ? c(o) : o;
75
+ return r && o !== u && o.dispose(), u;
76
76
  });
77
77
  }
78
- project(t) {
79
- return e(() => this.wte.project(t));
78
+ project(e) {
79
+ return a(() => this.wte.project(e));
80
80
  }
81
81
  dispose() {
82
82
  this.weightStore.dispose(), this.wte.dispose(), this.wpe && this.wpe.dispose(), this.blocks.forEach((e) => e.dispose()), this.lnF.dispose();
83
83
  }
84
84
  };
85
- //#endregion
86
85
  export { d as default };
@@ -0,0 +1,16 @@
1
+ import { GPTConfigQwen3 } from './config';
2
+ import { Tensor } from '@tensorflow/tfjs-core';
3
+ import { default as Model, ModelForwardAttributes } from './model';
4
+ export default class Qwen3 extends Model<ModelForwardAttributes, GPTConfigQwen3> {
5
+ private wte;
6
+ private head;
7
+ private blocks;
8
+ private lnF;
9
+ private ropeCache?;
10
+ constructor(config?: Partial<GPTConfigQwen3>);
11
+ getClassName(): string;
12
+ private inputPhase;
13
+ forward(attrs: ModelForwardAttributes, idx: Tensor): Tensor;
14
+ project(embeddings: Tensor): Tensor;
15
+ dispose(): void;
16
+ }