numkong 7.0.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 (294) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +495 -0
  3. package/binding.gyp +540 -0
  4. package/c/dispatch.h +512 -0
  5. package/c/dispatch_bf16.c +389 -0
  6. package/c/dispatch_bf16c.c +52 -0
  7. package/c/dispatch_e2m3.c +263 -0
  8. package/c/dispatch_e3m2.c +243 -0
  9. package/c/dispatch_e4m3.c +276 -0
  10. package/c/dispatch_e5m2.c +272 -0
  11. package/c/dispatch_f16.c +376 -0
  12. package/c/dispatch_f16c.c +58 -0
  13. package/c/dispatch_f32.c +378 -0
  14. package/c/dispatch_f32c.c +99 -0
  15. package/c/dispatch_f64.c +296 -0
  16. package/c/dispatch_f64c.c +98 -0
  17. package/c/dispatch_i16.c +96 -0
  18. package/c/dispatch_i32.c +89 -0
  19. package/c/dispatch_i4.c +150 -0
  20. package/c/dispatch_i64.c +86 -0
  21. package/c/dispatch_i8.c +289 -0
  22. package/c/dispatch_other.c +330 -0
  23. package/c/dispatch_u1.c +148 -0
  24. package/c/dispatch_u16.c +124 -0
  25. package/c/dispatch_u32.c +118 -0
  26. package/c/dispatch_u4.c +150 -0
  27. package/c/dispatch_u64.c +102 -0
  28. package/c/dispatch_u8.c +303 -0
  29. package/c/numkong.c +950 -0
  30. package/include/README.md +573 -0
  31. package/include/module.modulemap +129 -0
  32. package/include/numkong/attention/sapphireamx.h +1361 -0
  33. package/include/numkong/attention/sme.h +2066 -0
  34. package/include/numkong/attention.h +49 -0
  35. package/include/numkong/capabilities.h +748 -0
  36. package/include/numkong/cast/README.md +262 -0
  37. package/include/numkong/cast/haswell.h +975 -0
  38. package/include/numkong/cast/icelake.h +470 -0
  39. package/include/numkong/cast/neon.h +1192 -0
  40. package/include/numkong/cast/rvv.h +1021 -0
  41. package/include/numkong/cast/sapphire.h +262 -0
  42. package/include/numkong/cast/serial.h +2262 -0
  43. package/include/numkong/cast/skylake.h +856 -0
  44. package/include/numkong/cast/v128relaxed.h +180 -0
  45. package/include/numkong/cast.h +230 -0
  46. package/include/numkong/curved/README.md +223 -0
  47. package/include/numkong/curved/genoa.h +182 -0
  48. package/include/numkong/curved/haswell.h +276 -0
  49. package/include/numkong/curved/neon.h +205 -0
  50. package/include/numkong/curved/neonbfdot.h +212 -0
  51. package/include/numkong/curved/neonhalf.h +212 -0
  52. package/include/numkong/curved/rvv.h +305 -0
  53. package/include/numkong/curved/serial.h +207 -0
  54. package/include/numkong/curved/skylake.h +457 -0
  55. package/include/numkong/curved/smef64.h +506 -0
  56. package/include/numkong/curved.h +517 -0
  57. package/include/numkong/curved.hpp +144 -0
  58. package/include/numkong/dot/README.md +425 -0
  59. package/include/numkong/dot/alder.h +563 -0
  60. package/include/numkong/dot/genoa.h +315 -0
  61. package/include/numkong/dot/haswell.h +1688 -0
  62. package/include/numkong/dot/icelake.h +883 -0
  63. package/include/numkong/dot/neon.h +818 -0
  64. package/include/numkong/dot/neonbfdot.h +244 -0
  65. package/include/numkong/dot/neonfhm.h +360 -0
  66. package/include/numkong/dot/neonhalf.h +198 -0
  67. package/include/numkong/dot/neonsdot.h +508 -0
  68. package/include/numkong/dot/rvv.h +714 -0
  69. package/include/numkong/dot/rvvbb.h +72 -0
  70. package/include/numkong/dot/rvvbf16.h +123 -0
  71. package/include/numkong/dot/rvvhalf.h +129 -0
  72. package/include/numkong/dot/sapphire.h +141 -0
  73. package/include/numkong/dot/serial.h +838 -0
  74. package/include/numkong/dot/sierra.h +405 -0
  75. package/include/numkong/dot/skylake.h +1084 -0
  76. package/include/numkong/dot/sve.h +379 -0
  77. package/include/numkong/dot/svebfdot.h +74 -0
  78. package/include/numkong/dot/svehalf.h +123 -0
  79. package/include/numkong/dot/v128relaxed.h +1258 -0
  80. package/include/numkong/dot.h +1070 -0
  81. package/include/numkong/dot.hpp +94 -0
  82. package/include/numkong/dots/README.md +496 -0
  83. package/include/numkong/dots/alder.h +114 -0
  84. package/include/numkong/dots/genoa.h +94 -0
  85. package/include/numkong/dots/haswell.h +295 -0
  86. package/include/numkong/dots/icelake.h +171 -0
  87. package/include/numkong/dots/neon.h +120 -0
  88. package/include/numkong/dots/neonbfdot.h +58 -0
  89. package/include/numkong/dots/neonfhm.h +94 -0
  90. package/include/numkong/dots/neonhalf.h +57 -0
  91. package/include/numkong/dots/neonsdot.h +108 -0
  92. package/include/numkong/dots/rvv.h +2486 -0
  93. package/include/numkong/dots/sapphireamx.h +3973 -0
  94. package/include/numkong/dots/serial.h +2844 -0
  95. package/include/numkong/dots/sierra.h +97 -0
  96. package/include/numkong/dots/skylake.h +196 -0
  97. package/include/numkong/dots/sme.h +5372 -0
  98. package/include/numkong/dots/smebi32.h +461 -0
  99. package/include/numkong/dots/smef64.h +1318 -0
  100. package/include/numkong/dots/smehalf.h +47 -0
  101. package/include/numkong/dots/v128relaxed.h +294 -0
  102. package/include/numkong/dots.h +2804 -0
  103. package/include/numkong/dots.hpp +639 -0
  104. package/include/numkong/each/README.md +469 -0
  105. package/include/numkong/each/haswell.h +1658 -0
  106. package/include/numkong/each/icelake.h +272 -0
  107. package/include/numkong/each/neon.h +1104 -0
  108. package/include/numkong/each/neonbfdot.h +212 -0
  109. package/include/numkong/each/neonhalf.h +410 -0
  110. package/include/numkong/each/rvv.h +1121 -0
  111. package/include/numkong/each/sapphire.h +477 -0
  112. package/include/numkong/each/serial.h +260 -0
  113. package/include/numkong/each/skylake.h +1562 -0
  114. package/include/numkong/each.h +2146 -0
  115. package/include/numkong/each.hpp +434 -0
  116. package/include/numkong/geospatial/README.md +147 -0
  117. package/include/numkong/geospatial/haswell.h +593 -0
  118. package/include/numkong/geospatial/neon.h +571 -0
  119. package/include/numkong/geospatial/rvv.h +701 -0
  120. package/include/numkong/geospatial/serial.h +309 -0
  121. package/include/numkong/geospatial/skylake.h +577 -0
  122. package/include/numkong/geospatial/v128relaxed.h +613 -0
  123. package/include/numkong/geospatial.h +453 -0
  124. package/include/numkong/geospatial.hpp +235 -0
  125. package/include/numkong/matrix.hpp +336 -0
  126. package/include/numkong/maxsim/README.md +187 -0
  127. package/include/numkong/maxsim/alder.h +511 -0
  128. package/include/numkong/maxsim/genoa.h +115 -0
  129. package/include/numkong/maxsim/haswell.h +553 -0
  130. package/include/numkong/maxsim/icelake.h +480 -0
  131. package/include/numkong/maxsim/neonsdot.h +394 -0
  132. package/include/numkong/maxsim/sapphireamx.h +877 -0
  133. package/include/numkong/maxsim/serial.h +490 -0
  134. package/include/numkong/maxsim/sme.h +929 -0
  135. package/include/numkong/maxsim/v128relaxed.h +280 -0
  136. package/include/numkong/maxsim.h +571 -0
  137. package/include/numkong/maxsim.hpp +133 -0
  138. package/include/numkong/mesh/README.md +227 -0
  139. package/include/numkong/mesh/haswell.h +2235 -0
  140. package/include/numkong/mesh/neon.h +1329 -0
  141. package/include/numkong/mesh/neonbfdot.h +842 -0
  142. package/include/numkong/mesh/neonhalf.h +616 -0
  143. package/include/numkong/mesh/rvv.h +916 -0
  144. package/include/numkong/mesh/serial.h +742 -0
  145. package/include/numkong/mesh/skylake.h +1135 -0
  146. package/include/numkong/mesh/v128relaxed.h +1052 -0
  147. package/include/numkong/mesh.h +652 -0
  148. package/include/numkong/mesh.hpp +762 -0
  149. package/include/numkong/numkong.h +78 -0
  150. package/include/numkong/numkong.hpp +57 -0
  151. package/include/numkong/probability/README.md +173 -0
  152. package/include/numkong/probability/haswell.h +267 -0
  153. package/include/numkong/probability/neon.h +225 -0
  154. package/include/numkong/probability/rvv.h +409 -0
  155. package/include/numkong/probability/serial.h +169 -0
  156. package/include/numkong/probability/skylake.h +324 -0
  157. package/include/numkong/probability.h +383 -0
  158. package/include/numkong/probability.hpp +120 -0
  159. package/include/numkong/random.h +50 -0
  160. package/include/numkong/random.hpp +285 -0
  161. package/include/numkong/reduce/README.md +547 -0
  162. package/include/numkong/reduce/alder.h +632 -0
  163. package/include/numkong/reduce/genoa.h +201 -0
  164. package/include/numkong/reduce/haswell.h +3783 -0
  165. package/include/numkong/reduce/icelake.h +549 -0
  166. package/include/numkong/reduce/neon.h +3841 -0
  167. package/include/numkong/reduce/neonbfdot.h +353 -0
  168. package/include/numkong/reduce/neonfhm.h +665 -0
  169. package/include/numkong/reduce/neonhalf.h +157 -0
  170. package/include/numkong/reduce/neonsdot.h +357 -0
  171. package/include/numkong/reduce/rvv.h +3407 -0
  172. package/include/numkong/reduce/serial.h +757 -0
  173. package/include/numkong/reduce/sierra.h +338 -0
  174. package/include/numkong/reduce/skylake.h +3792 -0
  175. package/include/numkong/reduce/v128relaxed.h +2302 -0
  176. package/include/numkong/reduce.h +1597 -0
  177. package/include/numkong/reduce.hpp +633 -0
  178. package/include/numkong/scalar/README.md +89 -0
  179. package/include/numkong/scalar/haswell.h +113 -0
  180. package/include/numkong/scalar/neon.h +122 -0
  181. package/include/numkong/scalar/neonhalf.h +70 -0
  182. package/include/numkong/scalar/rvv.h +211 -0
  183. package/include/numkong/scalar/sapphire.h +63 -0
  184. package/include/numkong/scalar/serial.h +332 -0
  185. package/include/numkong/scalar/v128relaxed.h +56 -0
  186. package/include/numkong/scalar.h +683 -0
  187. package/include/numkong/set/README.md +179 -0
  188. package/include/numkong/set/haswell.h +334 -0
  189. package/include/numkong/set/icelake.h +485 -0
  190. package/include/numkong/set/neon.h +364 -0
  191. package/include/numkong/set/rvv.h +226 -0
  192. package/include/numkong/set/rvvbb.h +117 -0
  193. package/include/numkong/set/serial.h +174 -0
  194. package/include/numkong/set/sve.h +185 -0
  195. package/include/numkong/set/v128relaxed.h +240 -0
  196. package/include/numkong/set.h +457 -0
  197. package/include/numkong/set.hpp +114 -0
  198. package/include/numkong/sets/README.md +149 -0
  199. package/include/numkong/sets/haswell.h +63 -0
  200. package/include/numkong/sets/icelake.h +66 -0
  201. package/include/numkong/sets/neon.h +61 -0
  202. package/include/numkong/sets/serial.h +43 -0
  203. package/include/numkong/sets/smebi32.h +1099 -0
  204. package/include/numkong/sets/v128relaxed.h +58 -0
  205. package/include/numkong/sets.h +339 -0
  206. package/include/numkong/sparse/README.md +156 -0
  207. package/include/numkong/sparse/icelake.h +463 -0
  208. package/include/numkong/sparse/neon.h +288 -0
  209. package/include/numkong/sparse/serial.h +117 -0
  210. package/include/numkong/sparse/sve2.h +507 -0
  211. package/include/numkong/sparse/turin.h +322 -0
  212. package/include/numkong/sparse.h +363 -0
  213. package/include/numkong/sparse.hpp +113 -0
  214. package/include/numkong/spatial/README.md +435 -0
  215. package/include/numkong/spatial/alder.h +607 -0
  216. package/include/numkong/spatial/genoa.h +290 -0
  217. package/include/numkong/spatial/haswell.h +960 -0
  218. package/include/numkong/spatial/icelake.h +586 -0
  219. package/include/numkong/spatial/neon.h +773 -0
  220. package/include/numkong/spatial/neonbfdot.h +165 -0
  221. package/include/numkong/spatial/neonhalf.h +118 -0
  222. package/include/numkong/spatial/neonsdot.h +261 -0
  223. package/include/numkong/spatial/rvv.h +984 -0
  224. package/include/numkong/spatial/rvvbf16.h +123 -0
  225. package/include/numkong/spatial/rvvhalf.h +117 -0
  226. package/include/numkong/spatial/sapphire.h +343 -0
  227. package/include/numkong/spatial/serial.h +346 -0
  228. package/include/numkong/spatial/sierra.h +323 -0
  229. package/include/numkong/spatial/skylake.h +606 -0
  230. package/include/numkong/spatial/sve.h +224 -0
  231. package/include/numkong/spatial/svebfdot.h +122 -0
  232. package/include/numkong/spatial/svehalf.h +109 -0
  233. package/include/numkong/spatial/v128relaxed.h +717 -0
  234. package/include/numkong/spatial.h +1425 -0
  235. package/include/numkong/spatial.hpp +183 -0
  236. package/include/numkong/spatials/README.md +580 -0
  237. package/include/numkong/spatials/alder.h +94 -0
  238. package/include/numkong/spatials/genoa.h +94 -0
  239. package/include/numkong/spatials/haswell.h +219 -0
  240. package/include/numkong/spatials/icelake.h +113 -0
  241. package/include/numkong/spatials/neon.h +109 -0
  242. package/include/numkong/spatials/neonbfdot.h +60 -0
  243. package/include/numkong/spatials/neonfhm.h +92 -0
  244. package/include/numkong/spatials/neonhalf.h +58 -0
  245. package/include/numkong/spatials/neonsdot.h +109 -0
  246. package/include/numkong/spatials/rvv.h +1960 -0
  247. package/include/numkong/spatials/sapphireamx.h +1149 -0
  248. package/include/numkong/spatials/serial.h +226 -0
  249. package/include/numkong/spatials/sierra.h +96 -0
  250. package/include/numkong/spatials/skylake.h +184 -0
  251. package/include/numkong/spatials/sme.h +1901 -0
  252. package/include/numkong/spatials/smef64.h +465 -0
  253. package/include/numkong/spatials/v128relaxed.h +240 -0
  254. package/include/numkong/spatials.h +3021 -0
  255. package/include/numkong/spatials.hpp +508 -0
  256. package/include/numkong/tensor.hpp +1592 -0
  257. package/include/numkong/trigonometry/README.md +184 -0
  258. package/include/numkong/trigonometry/haswell.h +652 -0
  259. package/include/numkong/trigonometry/neon.h +639 -0
  260. package/include/numkong/trigonometry/rvv.h +699 -0
  261. package/include/numkong/trigonometry/serial.h +703 -0
  262. package/include/numkong/trigonometry/skylake.h +721 -0
  263. package/include/numkong/trigonometry/v128relaxed.h +666 -0
  264. package/include/numkong/trigonometry.h +467 -0
  265. package/include/numkong/trigonometry.hpp +166 -0
  266. package/include/numkong/types.h +1384 -0
  267. package/include/numkong/types.hpp +5603 -0
  268. package/include/numkong/vector.hpp +698 -0
  269. package/javascript/README.md +246 -0
  270. package/javascript/dist/cjs/numkong-wasm.d.ts +166 -0
  271. package/javascript/dist/cjs/numkong-wasm.js +617 -0
  272. package/javascript/dist/cjs/numkong.d.ts +343 -0
  273. package/javascript/dist/cjs/numkong.js +523 -0
  274. package/javascript/dist/cjs/package.json +3 -0
  275. package/javascript/dist/cjs/types.d.ts +284 -0
  276. package/javascript/dist/cjs/types.js +653 -0
  277. package/javascript/dist/esm/numkong-wasm.d.ts +166 -0
  278. package/javascript/dist/esm/numkong-wasm.js +595 -0
  279. package/javascript/dist/esm/numkong.d.ts +343 -0
  280. package/javascript/dist/esm/numkong.js +452 -0
  281. package/javascript/dist/esm/package.json +3 -0
  282. package/javascript/dist/esm/types.d.ts +284 -0
  283. package/javascript/dist/esm/types.js +630 -0
  284. package/javascript/dist-package-cjs.json +3 -0
  285. package/javascript/dist-package-esm.json +3 -0
  286. package/javascript/node-gyp-build.d.ts +1 -0
  287. package/javascript/numkong-wasm.ts +756 -0
  288. package/javascript/numkong.c +689 -0
  289. package/javascript/numkong.ts +575 -0
  290. package/javascript/tsconfig-base.json +39 -0
  291. package/javascript/tsconfig-cjs.json +8 -0
  292. package/javascript/tsconfig-esm.json +8 -0
  293. package/javascript/types.ts +674 -0
  294. package/package.json +87 -0
@@ -0,0 +1,630 @@
1
+ /**
2
+ * @brief Custom TypedArray classes for non-native numeric types.
3
+ * @file javascript/types.ts
4
+ * @author Ash Vardanian
5
+ * @date February 3, 2026
6
+ *
7
+ * This file provides TypedArray wrappers for numeric types not natively supported
8
+ * by JavaScript, using NumKong's SIMD-optimized conversion functions from the C library.
9
+ */
10
+ // Import conversion functions from the compiled native module
11
+ // These will be defined in numkong.ts after the module is loaded
12
+ let conversionFunctions;
13
+ // This will be called by numkong.ts after loading the module
14
+ export function setConversionFunctions(fns) {
15
+ conversionFunctions = fns;
16
+ }
17
+ /** @brief Numeric data type enum — integer switch, compiles to jump table. */
18
+ export var DType;
19
+ (function (DType) {
20
+ DType[DType["F64"] = 0] = "F64";
21
+ DType[DType["F32"] = 1] = "F32";
22
+ DType[DType["F16"] = 2] = "F16";
23
+ DType[DType["BF16"] = 3] = "BF16";
24
+ DType[DType["E4M3"] = 4] = "E4M3";
25
+ DType[DType["E5M2"] = 5] = "E5M2";
26
+ DType[DType["E2M3"] = 6] = "E2M3";
27
+ DType[DType["E3M2"] = 7] = "E3M2";
28
+ DType[DType["I8"] = 8] = "I8";
29
+ DType[DType["U8"] = 9] = "U8";
30
+ DType[DType["U1"] = 10] = "U1";
31
+ DType[DType["I32"] = 11] = "I32";
32
+ DType[DType["U32"] = 12] = "U32";
33
+ })(DType || (DType = {}));
34
+ /** @brief O(1) array lookup for DType → string conversion (needed at N-API/WASM boundaries). */
35
+ export const DTYPE_STRINGS = [
36
+ 'f64', 'f32', 'f16', 'bf16', 'e4m3', 'e5m2', 'e2m3', 'e3m2', 'i8', 'u8', 'u1', 'i32', 'u32',
37
+ ];
38
+ /** @brief Convert a DType enum value to its string representation. */
39
+ export function dtypeToString(d) { return DTYPE_STRINGS[d]; }
40
+ /** @brief Infer the DType from a TypedArray instance. */
41
+ function inferDtype(arr) {
42
+ if (arr instanceof Float64Array)
43
+ return DType.F64;
44
+ if (arr instanceof Float32Array)
45
+ return DType.F32;
46
+ if (arr instanceof Int32Array)
47
+ return DType.I32;
48
+ if (arr instanceof Int8Array)
49
+ return DType.I8;
50
+ if (arr instanceof Uint8Array)
51
+ return DType.U8;
52
+ if (arr instanceof Uint16Array)
53
+ return DType.F16;
54
+ if (arr instanceof Uint32Array)
55
+ return DType.U32;
56
+ throw new Error(`Cannot infer dtype from ${arr.constructor.name}`);
57
+ }
58
+ /**
59
+ * @brief Abstract base class for all tensor types.
60
+ *
61
+ * All fields are embedded — zero dynamic allocation. DType is a numeric enum
62
+ * (integer switch). Mirrors the C++ pattern: buffer + byteOffset + dtype.
63
+ */
64
+ export class TensorBase {
65
+ constructor(buffer, byteOffset, dtype) {
66
+ this.buffer = buffer;
67
+ this.byteOffset = byteOffset;
68
+ this.dtype = dtype;
69
+ }
70
+ /** @brief Bytes per element for this tensor's dtype (compiles to jump table). */
71
+ get bytesPerElement() {
72
+ switch (this.dtype) {
73
+ case DType.F64: return 8;
74
+ case DType.F32:
75
+ case DType.I32:
76
+ case DType.U32: return 4;
77
+ case DType.F16:
78
+ case DType.BF16: return 2;
79
+ default: return 1;
80
+ }
81
+ }
82
+ /** @brief Total byte length of the tensor data. */
83
+ get byteLength() { return this.length * this.bytesPerElement; }
84
+ }
85
+ /**
86
+ * @brief Abstract rank-1 tensor base class.
87
+ */
88
+ export class VectorBase extends TensorBase {
89
+ constructor(buffer, byteOffset, length, dtype) {
90
+ super(buffer, byteOffset, dtype);
91
+ this.length = length;
92
+ }
93
+ get rank() { return 1; }
94
+ }
95
+ /**
96
+ * @brief Non-owning rank-1 tensor view (like std::span<T>).
97
+ *
98
+ * Zero-copy wrapper for existing memory. Ideal for cross-module WASM interop
99
+ * where data already lives on the WASM heap.
100
+ */
101
+ export class VectorView extends VectorBase {
102
+ constructor(buffer, byteOffset, length, dtype) {
103
+ super(buffer, byteOffset, length, dtype);
104
+ }
105
+ /** @brief Create a VectorView from any TypedArray, inferring or accepting dtype. */
106
+ static from(arr, dtype) {
107
+ const d = dtype ?? inferDtype(arr);
108
+ return new VectorView(arr.buffer, arr.byteOffset, arr.length, d);
109
+ }
110
+ }
111
+ /**
112
+ * @brief Owning rank-1 tensor (like std::vector<T>).
113
+ *
114
+ * Allocates its own ArrayBuffer. Use for storing results or when you need
115
+ * independent ownership of the data.
116
+ */
117
+ export class Vector extends VectorBase {
118
+ constructor(lengthOrBuffer, dtypeOrLength, dtype) {
119
+ if (typeof lengthOrBuffer === 'number') {
120
+ const length = lengthOrBuffer;
121
+ const dt = dtypeOrLength;
122
+ let bpe;
123
+ switch (dt) {
124
+ case DType.F64:
125
+ bpe = 8;
126
+ break;
127
+ case DType.F32:
128
+ case DType.I32:
129
+ case DType.U32:
130
+ bpe = 4;
131
+ break;
132
+ case DType.F16:
133
+ case DType.BF16:
134
+ bpe = 2;
135
+ break;
136
+ default:
137
+ bpe = 1;
138
+ break;
139
+ }
140
+ super(new ArrayBuffer(length * bpe), 0, length, dt);
141
+ }
142
+ else {
143
+ super(lengthOrBuffer, 0, dtypeOrLength, dtype);
144
+ }
145
+ }
146
+ /** @brief Create an owning Vector by copying data from a TypedArray. */
147
+ static fromTypedArray(arr, dtype) {
148
+ const d = dtype ?? inferDtype(arr);
149
+ return new Vector(arr.buffer.slice(arr.byteOffset, arr.byteOffset + arr.byteLength), arr.length, d);
150
+ }
151
+ /** @brief Create an owning Vector by copying data from any TensorBase. */
152
+ static fromView(view) {
153
+ return new Vector(view.buffer.slice(view.byteOffset, view.byteOffset + view.byteLength), view.length, view.dtype);
154
+ }
155
+ /** @brief Return a TypedArray view over this Vector's owned buffer (zero-copy). */
156
+ toTypedArray() {
157
+ switch (this.dtype) {
158
+ case DType.F64: return new Float64Array(this.buffer, 0, this.length);
159
+ case DType.F32: return new Float32Array(this.buffer, 0, this.length);
160
+ case DType.I32: return new Int32Array(this.buffer, 0, this.length);
161
+ case DType.U32: return new Uint32Array(this.buffer, 0, this.length);
162
+ case DType.F16:
163
+ case DType.BF16: return new Uint16Array(this.buffer, 0, this.length);
164
+ case DType.I8: return new Int8Array(this.buffer, 0, this.length);
165
+ default: return new Uint8Array(this.buffer, 0, this.length);
166
+ }
167
+ }
168
+ }
169
+ /**
170
+ * @brief Abstract rank-2 tensor base class.
171
+ *
172
+ * All 4 dimension fields are embedded — no dynamic allocation.
173
+ * Strides are in bytes to match the C API directly.
174
+ */
175
+ export class MatrixBase extends TensorBase {
176
+ constructor(buffer, byteOffset, dtype, rows, cols, rowStride, colStride) {
177
+ super(buffer, byteOffset, dtype);
178
+ this.rows = rows;
179
+ this.cols = cols;
180
+ this.rowStride = rowStride;
181
+ this.colStride = colStride;
182
+ }
183
+ get length() { return this.rows * this.cols; }
184
+ get rank() { return 2; }
185
+ }
186
+ /**
187
+ * @brief Owning rank-2 tensor (row-major, C-contiguous by default).
188
+ *
189
+ * Strides are byte strides. Default for C-contiguous layout:
190
+ * rowStride = cols * bytesPerElement, colStride = bytesPerElement.
191
+ */
192
+ export class Matrix extends MatrixBase {
193
+ constructor(rowsOrBuffer, colsOrByteOffset, dtype, rows, cols, rowStride, colStride) {
194
+ if (typeof rowsOrBuffer === 'number') {
195
+ const r = rowsOrBuffer;
196
+ const c = colsOrByteOffset;
197
+ let bpe;
198
+ switch (dtype) {
199
+ case DType.F64:
200
+ bpe = 8;
201
+ break;
202
+ case DType.F32:
203
+ case DType.I32:
204
+ case DType.U32:
205
+ bpe = 4;
206
+ break;
207
+ case DType.F16:
208
+ case DType.BF16:
209
+ bpe = 2;
210
+ break;
211
+ default:
212
+ bpe = 1;
213
+ break;
214
+ }
215
+ super(new ArrayBuffer(r * c * bpe), 0, dtype, r, c, c * bpe, bpe);
216
+ }
217
+ else {
218
+ const r = rows;
219
+ const c = cols;
220
+ let bpe;
221
+ switch (dtype) {
222
+ case DType.F64:
223
+ bpe = 8;
224
+ break;
225
+ case DType.F32:
226
+ case DType.I32:
227
+ case DType.U32:
228
+ bpe = 4;
229
+ break;
230
+ case DType.F16:
231
+ case DType.BF16:
232
+ bpe = 2;
233
+ break;
234
+ default:
235
+ bpe = 1;
236
+ break;
237
+ }
238
+ super(rowsOrBuffer, colsOrByteOffset, dtype, r, c, rowStride ?? c * bpe, colStride ?? bpe);
239
+ }
240
+ }
241
+ static fromTypedArray(array, rows, cols, dtype) {
242
+ const d = dtype ?? inferDtype(array);
243
+ const buf = array.buffer.slice(array.byteOffset, array.byteOffset + array.byteLength);
244
+ return new Matrix(buf, 0, d, rows, cols);
245
+ }
246
+ toTypedArray() {
247
+ switch (this.dtype) {
248
+ case DType.F64: return new Float64Array(this.buffer, this.byteOffset, this.rows * this.cols);
249
+ case DType.F32: return new Float32Array(this.buffer, this.byteOffset, this.rows * this.cols);
250
+ case DType.I32: return new Int32Array(this.buffer, this.byteOffset, this.rows * this.cols);
251
+ case DType.U32: return new Uint32Array(this.buffer, this.byteOffset, this.rows * this.cols);
252
+ case DType.F16:
253
+ case DType.BF16: return new Uint16Array(this.buffer, this.byteOffset, this.rows * this.cols);
254
+ case DType.I8: return new Int8Array(this.buffer, this.byteOffset, this.rows * this.cols);
255
+ default: return new Uint8Array(this.buffer, this.byteOffset, this.rows * this.cols);
256
+ }
257
+ }
258
+ row(index) {
259
+ return new VectorView(this.buffer, this.byteOffset + index * this.rowStride, this.cols, this.dtype);
260
+ }
261
+ }
262
+ /**
263
+ * @brief Opaque packed matrix container.
264
+ *
265
+ * Packed layout is not indexable — this is a data container for packed GEMM kernels.
266
+ * N-API path: buffer is a V8-managed ArrayBuffer, auto-freed by GC.
267
+ * WASM path: stores a heap pointer, dispose() calls Module._free().
268
+ */
269
+ export class PackedMatrix {
270
+ constructor(buffer, width, depth, dtype, byteLength) {
271
+ this._disposed = false;
272
+ this.buffer = buffer;
273
+ this.width = width;
274
+ this.depth = depth;
275
+ this.dtype = dtype;
276
+ this.byteLength = byteLength;
277
+ }
278
+ dispose() { this._disposed = true; }
279
+ get disposed() { return this._disposed; }
280
+ }
281
+ /**
282
+ * @brief Determines the output dtype for a given kernel family and input dtype.
283
+ * Mirrors nk_kernel_output_dtype from C.
284
+ */
285
+ export function outputDtype(family, input) {
286
+ switch (input) {
287
+ case DType.F64: return DType.F64;
288
+ case DType.F32: return DType.F64;
289
+ case DType.F16:
290
+ case DType.BF16:
291
+ case DType.E4M3:
292
+ case DType.E5M2:
293
+ case DType.E2M3:
294
+ case DType.E3M2:
295
+ return DType.F32;
296
+ case DType.I8: return family === 'dots' ? DType.I32 : DType.F32;
297
+ case DType.U8: return family === 'dots' ? DType.U32 : DType.F32;
298
+ default: return DType.F32;
299
+ }
300
+ }
301
+ /**
302
+ * @brief IEEE 754 Half Precision Float (f16)
303
+ *
304
+ * 16-bit floating point: 1 sign bit, 5 exponent bits, 10 mantissa bits
305
+ * Range: ~±65504, precision: ~3-4 decimal digits
306
+ *
307
+ * Common in GPU inference, model compression, and mixed-precision training.
308
+ * Supported natively on Apple Silicon, NVIDIA GPUs (fp16), AMD GPUs.
309
+ */
310
+ export class Float16Array extends Uint16Array {
311
+ constructor(length, byteOffset, arrayLength) {
312
+ if (typeof length === 'number') {
313
+ super(length);
314
+ }
315
+ else if (ArrayBuffer.isView(length) || length instanceof ArrayBuffer) {
316
+ super(length, byteOffset, arrayLength);
317
+ }
318
+ else {
319
+ // Convert from array-like of numbers
320
+ const src = length;
321
+ const arr = new Uint16Array(src.length);
322
+ if (conversionFunctions) {
323
+ for (let i = 0; i < src.length; i++) {
324
+ arr[i] = conversionFunctions.castF32ToF16(src[i]);
325
+ }
326
+ }
327
+ super(arr);
328
+ }
329
+ }
330
+ /**
331
+ * @brief Converts the entire f16 array to f32 (Float32Array).
332
+ * @returns Float32Array with decoded values
333
+ */
334
+ toFloat32Array() {
335
+ if (!conversionFunctions) {
336
+ throw new Error('Conversion functions not initialized');
337
+ }
338
+ const result = new Float32Array(this.length);
339
+ for (let i = 0; i < this.length; i++) {
340
+ result[i] = conversionFunctions.castF16ToF32(this[i]);
341
+ }
342
+ return result;
343
+ }
344
+ /**
345
+ * @brief Gets the f32 value at the specified index.
346
+ * @param index Array index
347
+ * @returns Decoded f32 value
348
+ */
349
+ getFloat32(index) {
350
+ if (!conversionFunctions) {
351
+ throw new Error('Conversion functions not initialized');
352
+ }
353
+ return conversionFunctions.castF16ToF32(this[index]);
354
+ }
355
+ /**
356
+ * @brief Sets the value at the specified index from an f32 value.
357
+ * @param index Array index
358
+ * @param value f32 value to encode and store
359
+ */
360
+ setFloat32(index, value) {
361
+ if (!conversionFunctions) {
362
+ throw new Error('Conversion functions not initialized');
363
+ }
364
+ this[index] = conversionFunctions.castF32ToF16(value);
365
+ }
366
+ }
367
+ /**
368
+ * @brief Brain Float 16 (bf16)
369
+ *
370
+ * 16-bit floating point: 1 sign bit, 8 exponent bits, 7 mantissa bits
371
+ * Range: same as f32 (~±3.4e38), precision: ~2-3 decimal digits
372
+ *
373
+ * Designed by Google for TPUs, optimized for ML training (wider range than f16).
374
+ * Supported on Google TPUs, Intel Sapphire Rapids, AMD Genoa, ARM Neoverse V2.
375
+ * Truncated f32 (top 16 bits), making conversion very cheap.
376
+ */
377
+ export class BFloat16Array extends Uint16Array {
378
+ constructor(length, byteOffset, arrayLength) {
379
+ if (typeof length === 'number') {
380
+ super(length);
381
+ }
382
+ else if (ArrayBuffer.isView(length) || length instanceof ArrayBuffer) {
383
+ super(length, byteOffset, arrayLength);
384
+ }
385
+ else {
386
+ const src = length;
387
+ const arr = new Uint16Array(src.length);
388
+ if (conversionFunctions) {
389
+ for (let i = 0; i < src.length; i++) {
390
+ arr[i] = conversionFunctions.castF32ToBF16(src[i]);
391
+ }
392
+ }
393
+ super(arr);
394
+ }
395
+ }
396
+ toFloat32Array() {
397
+ if (!conversionFunctions) {
398
+ throw new Error('Conversion functions not initialized');
399
+ }
400
+ const result = new Float32Array(this.length);
401
+ for (let i = 0; i < this.length; i++) {
402
+ result[i] = conversionFunctions.castBF16ToF32(this[i]);
403
+ }
404
+ return result;
405
+ }
406
+ getFloat32(index) {
407
+ if (!conversionFunctions) {
408
+ throw new Error('Conversion functions not initialized');
409
+ }
410
+ return conversionFunctions.castBF16ToF32(this[index]);
411
+ }
412
+ setFloat32(index, value) {
413
+ if (!conversionFunctions) {
414
+ throw new Error('Conversion functions not initialized');
415
+ }
416
+ this[index] = conversionFunctions.castF32ToBF16(value);
417
+ }
418
+ }
419
+ /**
420
+ * @brief FP8 E4M3 (4-bit exponent, 3-bit mantissa)
421
+ *
422
+ * 8-bit floating point: 1 sign bit, 4 exponent bits, 3 mantissa bits
423
+ * Range: ~±448, precision: ~1 decimal digit
424
+ *
425
+ * Optimized for forward pass inference with higher precision than E5M2.
426
+ * Supported on NVIDIA Hopper H100 GPUs, AMD Instinct MI300.
427
+ */
428
+ export class E4M3Array extends Uint8Array {
429
+ constructor(length, byteOffset, arrayLength) {
430
+ if (typeof length === 'number') {
431
+ super(length);
432
+ }
433
+ else if (ArrayBuffer.isView(length) || length instanceof ArrayBuffer) {
434
+ super(length, byteOffset, arrayLength);
435
+ }
436
+ else {
437
+ const src = length;
438
+ const arr = new Uint8Array(src.length);
439
+ if (conversionFunctions) {
440
+ for (let i = 0; i < src.length; i++) {
441
+ arr[i] = conversionFunctions.castF32ToE4M3(src[i]);
442
+ }
443
+ }
444
+ super(arr);
445
+ }
446
+ }
447
+ toFloat32Array() {
448
+ if (!conversionFunctions) {
449
+ throw new Error('Conversion functions not initialized');
450
+ }
451
+ const result = new Float32Array(this.length);
452
+ for (let i = 0; i < this.length; i++) {
453
+ result[i] = conversionFunctions.castE4M3ToF32(this[i]);
454
+ }
455
+ return result;
456
+ }
457
+ getFloat32(index) {
458
+ if (!conversionFunctions) {
459
+ throw new Error('Conversion functions not initialized');
460
+ }
461
+ return conversionFunctions.castE4M3ToF32(this[index]);
462
+ }
463
+ setFloat32(index, value) {
464
+ if (!conversionFunctions) {
465
+ throw new Error('Conversion functions not initialized');
466
+ }
467
+ this[index] = conversionFunctions.castF32ToE4M3(value);
468
+ }
469
+ }
470
+ /**
471
+ * @brief FP8 E5M2 (5-bit exponent, 2-bit mantissa)
472
+ *
473
+ * 8-bit floating point: 1 sign bit, 5 exponent bits, 2 mantissa bits
474
+ * Range: ~±57344, precision: <1 decimal digit
475
+ *
476
+ * Optimized for backward pass training with wider range than E4M3.
477
+ * Supported on NVIDIA Hopper H100 GPUs, AMD Instinct MI300.
478
+ */
479
+ export class E5M2Array extends Uint8Array {
480
+ constructor(length, byteOffset, arrayLength) {
481
+ if (typeof length === 'number') {
482
+ super(length);
483
+ }
484
+ else if (ArrayBuffer.isView(length) || length instanceof ArrayBuffer) {
485
+ super(length, byteOffset, arrayLength);
486
+ }
487
+ else {
488
+ const src = length;
489
+ const arr = new Uint8Array(src.length);
490
+ if (conversionFunctions) {
491
+ for (let i = 0; i < src.length; i++) {
492
+ arr[i] = conversionFunctions.castF32ToE5M2(src[i]);
493
+ }
494
+ }
495
+ super(arr);
496
+ }
497
+ }
498
+ toFloat32Array() {
499
+ if (!conversionFunctions) {
500
+ throw new Error('Conversion functions not initialized');
501
+ }
502
+ const result = new Float32Array(this.length);
503
+ for (let i = 0; i < this.length; i++) {
504
+ result[i] = conversionFunctions.castE5M2ToF32(this[i]);
505
+ }
506
+ return result;
507
+ }
508
+ getFloat32(index) {
509
+ if (!conversionFunctions) {
510
+ throw new Error('Conversion functions not initialized');
511
+ }
512
+ return conversionFunctions.castE5M2ToF32(this[index]);
513
+ }
514
+ setFloat32(index, value) {
515
+ if (!conversionFunctions) {
516
+ throw new Error('Conversion functions not initialized');
517
+ }
518
+ this[index] = conversionFunctions.castF32ToE5M2(value);
519
+ }
520
+ }
521
+ /**
522
+ * @brief Binary Array (u1) - Bit-packed binary vectors
523
+ *
524
+ * 1-bit per element, packed into bytes (8 bits per byte)
525
+ * Used for binary embeddings, hashing, and Hamming/Jaccard distances.
526
+ *
527
+ * Common in semantic search with binary quantization (Cohere, Voyage).
528
+ */
529
+ export class BinaryArray extends Uint8Array {
530
+ constructor(bitLength) {
531
+ const byteLength = Math.ceil(bitLength / 8);
532
+ super(byteLength);
533
+ this._bitLength = bitLength;
534
+ }
535
+ /**
536
+ * @brief Gets the bit value at the specified index.
537
+ * @param index Bit index (0 to bitLength-1)
538
+ * @returns 0 or 1
539
+ */
540
+ getBit(index) {
541
+ if (index < 0 || index >= this._bitLength) {
542
+ throw new RangeError('Index out of bounds');
543
+ }
544
+ const byteIndex = index >>> 3; // index / 8
545
+ const bitIndex = index & 7; // index % 8
546
+ return (this[byteIndex] >>> bitIndex) & 1;
547
+ }
548
+ /**
549
+ * @brief Sets the bit value at the specified index.
550
+ * @param index Bit index (0 to bitLength-1)
551
+ * @param value 0 or 1
552
+ */
553
+ setBit(index, value) {
554
+ if (index < 0 || index >= this._bitLength) {
555
+ throw new RangeError('Index out of bounds');
556
+ }
557
+ const byteIndex = index >>> 3;
558
+ const bitIndex = index & 7;
559
+ if (value) {
560
+ this[byteIndex] |= (1 << bitIndex);
561
+ }
562
+ else {
563
+ this[byteIndex] &= ~(1 << bitIndex);
564
+ }
565
+ }
566
+ /**
567
+ * @brief Returns the logical bit length of the array.
568
+ */
569
+ get bitLength() {
570
+ return this._bitLength;
571
+ }
572
+ /**
573
+ * @brief Creates a BinaryArray from a Float32Array (positive values = 1, else 0).
574
+ * @param vector Source floating-point vector
575
+ * @returns Binary array with quantized values
576
+ */
577
+ static fromFloat32Array(vector) {
578
+ const binary = new BinaryArray(vector.length);
579
+ for (let i = 0; i < vector.length; i++) {
580
+ if (vector[i] > 0) {
581
+ binary.setBit(i, 1);
582
+ }
583
+ }
584
+ return binary;
585
+ }
586
+ /**
587
+ * @brief Creates a BinaryArray from a Float64Array (positive values = 1, else 0).
588
+ * @param vector Source floating-point vector
589
+ * @returns Binary array with quantized values
590
+ */
591
+ static fromFloat64Array(vector) {
592
+ const binary = new BinaryArray(vector.length);
593
+ for (let i = 0; i < vector.length; i++) {
594
+ if (vector[i] > 0) {
595
+ binary.setBit(i, 1);
596
+ }
597
+ }
598
+ return binary;
599
+ }
600
+ }
601
+ /**
602
+ * @brief Type guard to check if an object is a Float16Array.
603
+ */
604
+ export function isFloat16Array(obj) {
605
+ return obj instanceof Float16Array;
606
+ }
607
+ /**
608
+ * @brief Type guard to check if an object is a BFloat16Array.
609
+ */
610
+ export function isBFloat16Array(obj) {
611
+ return obj instanceof BFloat16Array;
612
+ }
613
+ /**
614
+ * @brief Type guard to check if an object is an E4M3Array.
615
+ */
616
+ export function isE4M3Array(obj) {
617
+ return obj instanceof E4M3Array;
618
+ }
619
+ /**
620
+ * @brief Type guard to check if an object is an E5M2Array.
621
+ */
622
+ export function isE5M2Array(obj) {
623
+ return obj instanceof E5M2Array;
624
+ }
625
+ /**
626
+ * @brief Type guard to check if an object is a BinaryArray.
627
+ */
628
+ export function isBinaryArray(obj) {
629
+ return obj instanceof BinaryArray;
630
+ }
@@ -0,0 +1,3 @@
1
+ {
2
+ "type": "commonjs"
3
+ }
@@ -0,0 +1,3 @@
1
+ {
2
+ "type": "module"
3
+ }
@@ -0,0 +1 @@
1
+ declare module "node-gyp-build";