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,523 @@
1
+ "use strict";
2
+ /**
3
+ * @fileoverview NumKong - Portable mixed-precision BLAS-like vector math library
4
+ *
5
+ * NumKong provides SIMD-accelerated distance metrics and vector operations for
6
+ * x86, ARM, RISC-V, and WASM platforms. The library automatically detects and uses
7
+ * the best available SIMD instruction set at runtime.
8
+ *
9
+ * @module numkong
10
+ * @author Ash Vardanian
11
+ *
12
+ * @example
13
+ * ```typescript
14
+ * import { dot, euclidean, Float16Array } from 'numkong';
15
+ *
16
+ * // Auto-detected types
17
+ * const a = new Float32Array([1, 2, 3]);
18
+ * const b = new Float32Array([4, 5, 6]);
19
+ * dot(a, b); // 32
20
+ * euclidean(a, b); // 5.196...
21
+ *
22
+ * // Custom types with explicit dtype
23
+ * const c = new Float16Array([1, 2, 3]);
24
+ * const d = new Float16Array([4, 5, 6]);
25
+ * dot(c, d, DType.F16); // 32
26
+ * ```
27
+ */
28
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
29
+ if (k2 === undefined) k2 = k;
30
+ var desc = Object.getOwnPropertyDescriptor(m, k);
31
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
32
+ desc = { enumerable: true, get: function() { return m[k]; } };
33
+ }
34
+ Object.defineProperty(o, k2, desc);
35
+ }) : (function(o, m, k, k2) {
36
+ if (k2 === undefined) k2 = k;
37
+ o[k2] = m[k];
38
+ }));
39
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
40
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
41
+ }) : function(o, v) {
42
+ o["default"] = v;
43
+ });
44
+ var __importStar = (this && this.__importStar) || (function () {
45
+ var ownKeys = function(o) {
46
+ ownKeys = Object.getOwnPropertyNames || function (o) {
47
+ var ar = [];
48
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
49
+ return ar;
50
+ };
51
+ return ownKeys(o);
52
+ };
53
+ return function (mod) {
54
+ if (mod && mod.__esModule) return mod;
55
+ var result = {};
56
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
57
+ __setModuleDefault(result, mod);
58
+ return result;
59
+ };
60
+ })();
61
+ var __importDefault = (this && this.__importDefault) || function (mod) {
62
+ return (mod && mod.__esModule) ? mod : { "default": mod };
63
+ };
64
+ Object.defineProperty(exports, "__esModule", { value: true });
65
+ exports.toBinary = exports.jaccard = exports.hamming = exports.hasCapability = exports.getCapabilities = exports.DType = exports.cast = exports.castF32ToE5M2 = exports.castE5M2ToF32 = exports.castF32ToE4M3 = exports.castE4M3ToF32 = exports.castF32ToBF16 = exports.castBF16ToF32 = exports.castF32ToF16 = exports.castF16ToF32 = exports.outputDtype = exports.PackedMatrix = exports.Matrix = exports.MatrixBase = exports.Vector = exports.VectorView = exports.VectorBase = exports.TensorBase = exports.BinaryArray = exports.E5M2Array = exports.E4M3Array = exports.BFloat16Array = exports.Float16Array = exports.Capability = void 0;
66
+ exports.sqeuclidean = sqeuclidean;
67
+ exports.euclidean = euclidean;
68
+ exports.angular = angular;
69
+ exports.inner = inner;
70
+ exports.dot = dot;
71
+ exports.kullbackleibler = kullbackleibler;
72
+ exports.jensenshannon = jensenshannon;
73
+ exports.dotsPackedSize = dotsPackedSize;
74
+ exports.dotsPack = dotsPack;
75
+ exports.dotsPacked = dotsPacked;
76
+ exports.angularsPacked = angularsPacked;
77
+ exports.euclideansPacked = euclideansPacked;
78
+ exports.dotsSymmetric = dotsSymmetric;
79
+ exports.angularsSymmetric = angularsSymmetric;
80
+ exports.euclideansSymmetric = euclideansSymmetric;
81
+ const node_gyp_build_1 = __importDefault(require("node-gyp-build"));
82
+ const path = __importStar(require("node:path"));
83
+ const node_fs_1 = require("node:fs");
84
+ const bindings_1 = require("bindings");
85
+ const types_js_1 = require("./types.js");
86
+ Object.defineProperty(exports, "Float16Array", { enumerable: true, get: function () { return types_js_1.Float16Array; } });
87
+ Object.defineProperty(exports, "BFloat16Array", { enumerable: true, get: function () { return types_js_1.BFloat16Array; } });
88
+ Object.defineProperty(exports, "E4M3Array", { enumerable: true, get: function () { return types_js_1.E4M3Array; } });
89
+ Object.defineProperty(exports, "E5M2Array", { enumerable: true, get: function () { return types_js_1.E5M2Array; } });
90
+ Object.defineProperty(exports, "BinaryArray", { enumerable: true, get: function () { return types_js_1.BinaryArray; } });
91
+ Object.defineProperty(exports, "TensorBase", { enumerable: true, get: function () { return types_js_1.TensorBase; } });
92
+ Object.defineProperty(exports, "VectorBase", { enumerable: true, get: function () { return types_js_1.VectorBase; } });
93
+ Object.defineProperty(exports, "VectorView", { enumerable: true, get: function () { return types_js_1.VectorView; } });
94
+ Object.defineProperty(exports, "Vector", { enumerable: true, get: function () { return types_js_1.Vector; } });
95
+ Object.defineProperty(exports, "MatrixBase", { enumerable: true, get: function () { return types_js_1.MatrixBase; } });
96
+ Object.defineProperty(exports, "Matrix", { enumerable: true, get: function () { return types_js_1.Matrix; } });
97
+ Object.defineProperty(exports, "PackedMatrix", { enumerable: true, get: function () { return types_js_1.PackedMatrix; } });
98
+ Object.defineProperty(exports, "DType", { enumerable: true, get: function () { return types_js_1.DType; } });
99
+ Object.defineProperty(exports, "outputDtype", { enumerable: true, get: function () { return types_js_1.outputDtype; } });
100
+ let compiled;
101
+ try {
102
+ let builddir = getBuildDir(getDirName());
103
+ compiled = (0, node_gyp_build_1.default)(builddir);
104
+ // Initialize conversion functions for types.ts
105
+ (0, types_js_1.setConversionFunctions)({
106
+ castF16ToF32: compiled.castF16ToF32,
107
+ castF32ToF16: compiled.castF32ToF16,
108
+ castBF16ToF32: compiled.castBF16ToF32,
109
+ castF32ToBF16: compiled.castF32ToBF16,
110
+ castE4M3ToF32: compiled.castE4M3ToF32,
111
+ castF32ToE4M3: compiled.castF32ToE4M3,
112
+ castE5M2ToF32: compiled.castE5M2ToF32,
113
+ castF32ToE5M2: compiled.castF32ToE5M2,
114
+ cast: compiled.cast,
115
+ });
116
+ }
117
+ catch (e) {
118
+ // Native addon not available
119
+ // For WASM usage, import the Emscripten module directly (see test/test-wasm.mjs)
120
+ throw new Error("NumKong native addon not found. Build with `npm run build` or use WASM " +
121
+ "by importing the Emscripten module directly. See test/test-wasm.mjs for examples.");
122
+ }
123
+ /**
124
+ * CPU capability bit masks in chronological order (by first commercial silicon).
125
+ * Use these with getCapabilities() to check for specific SIMD support.
126
+ */
127
+ exports.Capability = {
128
+ SERIAL: 1n << 0n, // Always: Fallback
129
+ NEON: 1n << 1n, // 2013: ARM NEON
130
+ HASWELL: 1n << 2n, // 2013: Intel AVX2
131
+ SKYLAKE: 1n << 3n, // 2017: Intel AVX-512
132
+ NEONHALF: 1n << 4n, // 2017: ARM NEON FP16
133
+ NEONSDOT: 1n << 5n, // 2017: ARM NEON i8 dot
134
+ NEONFHM: 1n << 6n, // 2018: ARM NEON FP16 FML
135
+ ICELAKE: 1n << 7n, // 2019: Intel AVX-512 VNNI
136
+ GENOA: 1n << 8n, // 2020: Intel/AMD AVX-512 BF16
137
+ NEONBFDOT: 1n << 9n, // 2020: ARM NEON BF16
138
+ SVE: 1n << 10n, // 2020: ARM SVE
139
+ SVEHALF: 1n << 11n, // 2020: ARM SVE FP16
140
+ SVESDOT: 1n << 12n, // 2020: ARM SVE i8 dot
141
+ SIERRA: 1n << 13n, // 2021: Intel AVX2+VNNI
142
+ SVEBFDOT: 1n << 14n, // 2021: ARM SVE BF16
143
+ SVE2: 1n << 15n, // 2022: ARM SVE2
144
+ V128RELAXED: 1n << 16n, // 2022: WASM Relaxed SIMD
145
+ SAPPHIRE: 1n << 17n, // 2023: Intel AVX-512 FP16
146
+ SAPPHIREAMX: 1n << 18n, // 2023: Intel Sapphire AMX
147
+ RVV: 1n << 19n, // 2023: RISC-V Vector
148
+ RVVHALF: 1n << 20n, // 2023: RISC-V Zvfh
149
+ RVVBF16: 1n << 21n, // 2023: RISC-V Zvfbfwma
150
+ GRANITEAMX: 1n << 22n, // 2024: Intel Granite AMX FP16
151
+ TURIN: 1n << 23n, // 2024: AMD Turin AVX-512 CD
152
+ SME: 1n << 24n, // 2024: ARM SME
153
+ SME2: 1n << 25n, // 2024: ARM SME2
154
+ SMEF64: 1n << 26n, // 2024: ARM SME F64
155
+ SMEFA64: 1n << 27n, // 2024: ARM SME FA64
156
+ SVE2P1: 1n << 28n, // 2025+: ARM SVE2.1
157
+ SME2P1: 1n << 29n, // 2025+: ARM SME2.1
158
+ SMEHALF: 1n << 30n, // 2025+: ARM SME F16F16
159
+ SMEBF16: 1n << 31n, // 2025+: ARM SME B16B16
160
+ SMELUT2: 1n << 32n, // 2025+: ARM SME LUTv2
161
+ RVVBB: 1n << 33n, // 2025+: RISC-V Zvbb
162
+ };
163
+ /** Convert a single FP16 value (as uint16 bits) to FP32 */
164
+ exports.castF16ToF32 = compiled.castF16ToF32;
165
+ /** Convert a single FP32 value to FP16 (returns uint16 bits) */
166
+ exports.castF32ToF16 = compiled.castF32ToF16;
167
+ /** Convert a single BF16 value (as uint16 bits) to FP32 */
168
+ exports.castBF16ToF32 = compiled.castBF16ToF32;
169
+ /** Convert a single FP32 value to BF16 (returns uint16 bits) */
170
+ exports.castF32ToBF16 = compiled.castF32ToBF16;
171
+ /** Convert a single E4M3 value (as uint8 bits) to FP32 */
172
+ exports.castE4M3ToF32 = compiled.castE4M3ToF32;
173
+ /** Convert a single FP32 value to E4M3 (returns uint8 bits) */
174
+ exports.castF32ToE4M3 = compiled.castF32ToE4M3;
175
+ /** Convert a single E5M2 value (as uint8 bits) to FP32 */
176
+ exports.castE5M2ToF32 = compiled.castE5M2ToF32;
177
+ /** Convert a single FP32 value to E5M2 (returns uint8 bits) */
178
+ exports.castF32ToE5M2 = compiled.castF32ToE5M2;
179
+ /** Bulk conversion between different numeric types (modifies destination array in-place) */
180
+ exports.cast = compiled.cast;
181
+ /**
182
+ * Extract a TypedArray from a TensorBase for the N-API backend.
183
+ *
184
+ * The native backend doesn't benefit from zero-copy TensorBase (Node.js TypedArrays
185
+ * already share process memory), but accepting TensorBase keeps the API uniform.
186
+ */
187
+ function unwrapTensor(input) {
188
+ switch (input.dtype) {
189
+ case types_js_1.DType.F64: return { arr: new Float64Array(input.buffer, input.byteOffset, input.length), dtype: input.dtype };
190
+ case types_js_1.DType.F32: return { arr: new Float32Array(input.buffer, input.byteOffset, input.length), dtype: input.dtype };
191
+ case types_js_1.DType.F16:
192
+ case types_js_1.DType.BF16: return { arr: new Uint16Array(input.buffer, input.byteOffset, input.length), dtype: input.dtype };
193
+ case types_js_1.DType.I8: return { arr: new Int8Array(input.buffer, input.byteOffset, input.length), dtype: input.dtype };
194
+ case types_js_1.DType.U8:
195
+ case types_js_1.DType.U1: return { arr: new Uint8Array(input.buffer, input.byteOffset, input.length), dtype: input.dtype };
196
+ default: return { arr: new Uint8Array(input.buffer, input.byteOffset, input.length), dtype: input.dtype };
197
+ }
198
+ }
199
+ /**
200
+ * Returns the runtime-detected SIMD capabilities as a bitmask.
201
+ *
202
+ * The bitmask includes flags for various SIMD instruction sets like AVX2, AVX-512,
203
+ * ARM NEON, ARM SVE, ARM SME, RISC-V Vector, and WASM SIMD extensions.
204
+ * Use with Capability constants to check for specific instruction sets.
205
+ *
206
+ * @returns {bigint} Bitmask of capability flags (use with Capability constants)
207
+ *
208
+ * @example
209
+ * ```ts
210
+ * import { getCapabilities, Capability } from 'numkong';
211
+ *
212
+ * const caps = getCapabilities();
213
+ * console.log(`Capabilities: 0x${caps.toString(16)}`);
214
+ *
215
+ * // Check for specific SIMD support
216
+ * if (caps & Capability.HASWELL) {
217
+ * console.log('AVX2 available');
218
+ * }
219
+ * ```
220
+ */
221
+ const getCapabilities = () => {
222
+ return compiled.getCapabilities();
223
+ };
224
+ exports.getCapabilities = getCapabilities;
225
+ /**
226
+ * Checks if a specific SIMD capability is available at runtime.
227
+ *
228
+ * This is a convenience wrapper around getCapabilities() that tests for a single capability.
229
+ *
230
+ * @param {bigint} cap - Capability flag to check (from Capability constants)
231
+ * @returns {boolean} True if the capability is available, false otherwise
232
+ *
233
+ * @example
234
+ * ```ts
235
+ * import { hasCapability, Capability } from 'numkong';
236
+ *
237
+ * if (hasCapability(Capability.HASWELL)) {
238
+ * console.log('Intel AVX2 (Haswell) available');
239
+ * }
240
+ * if (hasCapability(Capability.NEON)) {
241
+ * console.log('ARM NEON available');
242
+ * }
243
+ * if (hasCapability(Capability.V128RELAXED)) {
244
+ * console.log('WASM Relaxed SIMD available');
245
+ * }
246
+ * ```
247
+ */
248
+ const hasCapability = (cap) => {
249
+ return ((0, exports.getCapabilities)() & cap) !== 0n;
250
+ };
251
+ exports.hasCapability = hasCapability;
252
+ function sqeuclidean(a, b, dtype) {
253
+ if (a instanceof types_js_1.TensorBase) {
254
+ const u = unwrapTensor(a), v = unwrapTensor(b);
255
+ return compiled.sqeuclidean(u.arr, v.arr, (0, types_js_1.dtypeToString)(u.dtype));
256
+ }
257
+ return dtype !== undefined ? compiled.sqeuclidean(a, b, (0, types_js_1.dtypeToString)(dtype)) : compiled.sqeuclidean(a, b);
258
+ }
259
+ function euclidean(a, b, dtype) {
260
+ if (a instanceof types_js_1.TensorBase) {
261
+ const u = unwrapTensor(a), v = unwrapTensor(b);
262
+ return compiled.euclidean(u.arr, v.arr, (0, types_js_1.dtypeToString)(u.dtype));
263
+ }
264
+ return dtype !== undefined ? compiled.euclidean(a, b, (0, types_js_1.dtypeToString)(dtype)) : compiled.euclidean(a, b);
265
+ }
266
+ function angular(a, b, dtype) {
267
+ if (a instanceof types_js_1.TensorBase) {
268
+ const u = unwrapTensor(a), v = unwrapTensor(b);
269
+ return compiled.angular(u.arr, v.arr, (0, types_js_1.dtypeToString)(u.dtype));
270
+ }
271
+ return dtype !== undefined ? compiled.angular(a, b, (0, types_js_1.dtypeToString)(dtype)) : compiled.angular(a, b);
272
+ }
273
+ function inner(a, b, dtype) {
274
+ if (a instanceof types_js_1.TensorBase) {
275
+ const u = unwrapTensor(a), v = unwrapTensor(b);
276
+ return compiled.inner(u.arr, v.arr, (0, types_js_1.dtypeToString)(u.dtype));
277
+ }
278
+ return dtype !== undefined ? compiled.inner(a, b, (0, types_js_1.dtypeToString)(dtype)) : compiled.inner(a, b);
279
+ }
280
+ function dot(a, b, dtype) {
281
+ if (a instanceof types_js_1.TensorBase) {
282
+ const u = unwrapTensor(a), v = unwrapTensor(b);
283
+ return compiled.dot(u.arr, v.arr, (0, types_js_1.dtypeToString)(u.dtype));
284
+ }
285
+ return dtype !== undefined ? compiled.dot(a, b, (0, types_js_1.dtypeToString)(dtype)) : compiled.dot(a, b);
286
+ }
287
+ /**
288
+ * Computes the bitwise Hamming distance between two vectors.
289
+ *
290
+ * Both vectors are treated as bit-packed (u1 dtype), where each byte contains 8 bits.
291
+ * Use toBinary() to convert numeric arrays to bit-packed format.
292
+ *
293
+ * @param {Uint8Array | BinaryArray} a - The first bit-packed vector.
294
+ * @param {Uint8Array | BinaryArray} b - The second bit-packed vector.
295
+ * @returns {number} The Hamming distance (number of differing bits) between vectors a and b.
296
+ */
297
+ const hamming = (a, b) => {
298
+ if (a instanceof types_js_1.TensorBase) {
299
+ const u = unwrapTensor(a), v = unwrapTensor(b);
300
+ return compiled.hamming(u.arr, v.arr);
301
+ }
302
+ return compiled.hamming(a, b);
303
+ };
304
+ exports.hamming = hamming;
305
+ /**
306
+ * Computes the bitwise Jaccard distance between two vectors.
307
+ *
308
+ * Both vectors are treated as bit-packed (u1 dtype), where each byte contains 8 bits.
309
+ * Use toBinary() to convert numeric arrays to bit-packed format.
310
+ *
311
+ * @param {Uint8Array | BinaryArray} a - The first bit-packed vector.
312
+ * @param {Uint8Array | BinaryArray} b - The second bit-packed vector.
313
+ * @returns {number} The Jaccard distance (1 - Jaccard similarity) between vectors a and b.
314
+ */
315
+ const jaccard = (a, b) => {
316
+ if (a instanceof types_js_1.TensorBase) {
317
+ const u = unwrapTensor(a), v = unwrapTensor(b);
318
+ return compiled.jaccard(u.arr, v.arr);
319
+ }
320
+ return compiled.jaccard(a, b);
321
+ };
322
+ exports.jaccard = jaccard;
323
+ function kullbackleibler(a, b, dtype) {
324
+ if (a instanceof types_js_1.TensorBase) {
325
+ const u = unwrapTensor(a), v = unwrapTensor(b);
326
+ return compiled.kullbackleibler(u.arr, v.arr, (0, types_js_1.dtypeToString)(u.dtype));
327
+ }
328
+ return dtype !== undefined ? compiled.kullbackleibler(a, b, (0, types_js_1.dtypeToString)(dtype)) : compiled.kullbackleibler(a, b);
329
+ }
330
+ function jensenshannon(a, b, dtype) {
331
+ if (a instanceof types_js_1.TensorBase) {
332
+ const u = unwrapTensor(a), v = unwrapTensor(b);
333
+ return compiled.jensenshannon(u.arr, v.arr, (0, types_js_1.dtypeToString)(u.dtype));
334
+ }
335
+ return dtype !== undefined ? compiled.jensenshannon(a, b, (0, types_js_1.dtypeToString)(dtype)) : compiled.jensenshannon(a, b);
336
+ }
337
+ /**
338
+ * Quantizes a numeric vector into a bit-packed binary representation.
339
+ *
340
+ * Converts each element to a single bit: 1 for positive values, 0 for non-positive values.
341
+ * The bits are packed into bytes (8 bits per byte) in big-endian bit order within each byte.
342
+ * This is the required format for hamming() and jaccard() distance functions.
343
+ *
344
+ * @param {Float32Array | Float64Array | Int8Array} vector - The vector to quantize and pack.
345
+ * @returns {Uint8Array} A bit-packed array where each byte contains 8 binary values.
346
+ *
347
+ * @example
348
+ * ```ts
349
+ * const vec = new Float32Array([1.5, -2.3, 0.0, 3.1, -1.0, 2.0, 0.5, -0.5]);
350
+ * const binary = toBinary(vec);
351
+ * // Result: Uint8Array([0b10010110]) = [0x96]
352
+ * // bits: [1, 0, 0, 1, 0, 1, 1, 0] for elements [+, -, 0, +, -, +, +, -]
353
+ *
354
+ * // Use with Hamming distance
355
+ * const a = toBinary(new Float32Array([1, 2, 3]));
356
+ * const b = toBinary(new Float32Array([1, -2, 3]));
357
+ * const dist = hamming(a, b); // Counts differing bits
358
+ * ```
359
+ */
360
+ const toBinary = (vector) => {
361
+ const byteLength = Math.ceil(vector.length / 8);
362
+ const packedVector = new Uint8Array(byteLength);
363
+ for (let i = 0; i < vector.length; i++) {
364
+ if (vector[i] > 0) {
365
+ const byteIndex = Math.floor(i / 8);
366
+ const bitPosition = 7 - (i % 8);
367
+ packedVector[byteIndex] |= (1 << bitPosition);
368
+ }
369
+ }
370
+ return packedVector;
371
+ };
372
+ exports.toBinary = toBinary;
373
+ /**
374
+ * Extract a TypedArray from a Matrix for passing to the N-API backend.
375
+ */
376
+ function unwrapMatrix(matrix) {
377
+ switch (matrix.dtype) {
378
+ case types_js_1.DType.F64: return { array: new Float64Array(matrix.buffer, matrix.byteOffset, matrix.rows * matrix.cols), dtype: matrix.dtype };
379
+ case types_js_1.DType.F32: return { array: new Float32Array(matrix.buffer, matrix.byteOffset, matrix.rows * matrix.cols), dtype: matrix.dtype };
380
+ case types_js_1.DType.F16:
381
+ case types_js_1.DType.BF16: return { array: new Uint16Array(matrix.buffer, matrix.byteOffset, matrix.rows * matrix.cols), dtype: matrix.dtype };
382
+ case types_js_1.DType.I8: return { array: new Int8Array(matrix.buffer, matrix.byteOffset, matrix.rows * matrix.cols), dtype: matrix.dtype };
383
+ case types_js_1.DType.U8: return { array: new Uint8Array(matrix.buffer, matrix.byteOffset, matrix.rows * matrix.cols), dtype: matrix.dtype };
384
+ default: return { array: new Uint8Array(matrix.buffer, matrix.byteOffset, matrix.rows * matrix.cols), dtype: matrix.dtype };
385
+ }
386
+ }
387
+ /**
388
+ * Extract a result TypedArray from a Matrix matching its output dtype.
389
+ */
390
+ function unwrapResultMatrix(matrix) {
391
+ switch (matrix.dtype) {
392
+ case types_js_1.DType.F64: return new Float64Array(matrix.buffer, matrix.byteOffset, matrix.rows * matrix.cols);
393
+ case types_js_1.DType.F32: return new Float32Array(matrix.buffer, matrix.byteOffset, matrix.rows * matrix.cols);
394
+ case types_js_1.DType.I32: return new Int32Array(matrix.buffer, matrix.byteOffset, matrix.rows * matrix.cols);
395
+ case types_js_1.DType.U32: return new Uint32Array(matrix.buffer, matrix.byteOffset, matrix.rows * matrix.cols);
396
+ default: return new Float64Array(matrix.buffer, matrix.byteOffset, matrix.rows * matrix.cols);
397
+ }
398
+ }
399
+ /**
400
+ * Query the packed buffer byte count for a given matrix shape and dtype.
401
+ */
402
+ function dotsPackedSize(width, depth, dtype) {
403
+ return compiled.dotsPackedSize(width, depth, (0, types_js_1.dtypeToString)(dtype));
404
+ }
405
+ /**
406
+ * Pack a Matrix for use with packed GEMM-like operations.
407
+ */
408
+ function dotsPack(matrix) {
409
+ const { array, dtype } = unwrapMatrix(matrix);
410
+ const result = compiled.dotsPack(array, matrix.rows, matrix.cols, matrix.rowStride, (0, types_js_1.dtypeToString)(dtype));
411
+ return new types_js_1.PackedMatrix(result.buffer, result.width, result.depth, matrix.dtype, result.byteLength);
412
+ }
413
+ function packedOperation(compiledName, family, a, packed, out) {
414
+ if (a.cols !== packed.depth) {
415
+ throw new Error(`Matrix cols (${a.cols}) must match packed depth (${packed.depth})`);
416
+ }
417
+ const outDtype = (0, types_js_1.outputDtype)(family, a.dtype);
418
+ if (!out) {
419
+ out = new types_js_1.Matrix(a.rows, packed.width, outDtype);
420
+ }
421
+ const aUnwrapped = unwrapMatrix(a);
422
+ const resultArray = unwrapResultMatrix(out);
423
+ compiled[compiledName](aUnwrapped.array, packed.buffer, resultArray, a.rows, packed.width, a.cols, a.rowStride, out.rowStride, (0, types_js_1.dtypeToString)(a.dtype));
424
+ return out;
425
+ }
426
+ function symmetricOperation(compiledName, family, vectors, out, rowStart = 0, rowCount) {
427
+ const count = rowCount ?? vectors.rows - rowStart;
428
+ const outDtype = (0, types_js_1.outputDtype)(family, vectors.dtype);
429
+ if (!out) {
430
+ out = new types_js_1.Matrix(vectors.rows, vectors.rows, outDtype);
431
+ }
432
+ const vectorsUnwrapped = unwrapMatrix(vectors);
433
+ const resultArray = unwrapResultMatrix(out);
434
+ compiled[compiledName](vectorsUnwrapped.array, resultArray, vectors.rows, vectors.cols, vectors.rowStride, out.rowStride, rowStart, count, (0, types_js_1.dtypeToString)(vectors.dtype));
435
+ return out;
436
+ }
437
+ function dotsPacked(a, packed, out) {
438
+ return packedOperation('dotsPacked', 'dots', a, packed, out);
439
+ }
440
+ function angularsPacked(a, packed, out) {
441
+ return packedOperation('angularsPacked', 'angulars', a, packed, out);
442
+ }
443
+ function euclideansPacked(a, packed, out) {
444
+ return packedOperation('euclideansPacked', 'euclideans', a, packed, out);
445
+ }
446
+ function dotsSymmetric(vectors, out, options) {
447
+ return symmetricOperation('dotsSymmetric', 'dots', vectors, out, options?.rowStart ?? 0, options?.rowCount);
448
+ }
449
+ function angularsSymmetric(vectors, out, options) {
450
+ return symmetricOperation('angularsSymmetric', 'angulars', vectors, out, options?.rowStart ?? 0, options?.rowCount);
451
+ }
452
+ function euclideansSymmetric(vectors, out, options) {
453
+ return symmetricOperation('euclideansSymmetric', 'euclideans', vectors, out, options?.rowStart ?? 0, options?.rowCount);
454
+ }
455
+ exports.default = {
456
+ dot,
457
+ inner,
458
+ sqeuclidean,
459
+ euclidean,
460
+ angular,
461
+ hamming: exports.hamming,
462
+ jaccard: exports.jaccard,
463
+ kullbackleibler,
464
+ jensenshannon,
465
+ toBinary: exports.toBinary,
466
+ Float16Array: types_js_1.Float16Array,
467
+ BFloat16Array: types_js_1.BFloat16Array,
468
+ E4M3Array: types_js_1.E4M3Array,
469
+ E5M2Array: types_js_1.E5M2Array,
470
+ BinaryArray: types_js_1.BinaryArray,
471
+ TensorBase: types_js_1.TensorBase,
472
+ VectorBase: types_js_1.VectorBase,
473
+ VectorView: types_js_1.VectorView,
474
+ Vector: types_js_1.Vector,
475
+ MatrixBase: types_js_1.MatrixBase,
476
+ Matrix: types_js_1.Matrix,
477
+ PackedMatrix: types_js_1.PackedMatrix,
478
+ castF16ToF32: exports.castF16ToF32,
479
+ castF32ToF16: exports.castF32ToF16,
480
+ castBF16ToF32: exports.castBF16ToF32,
481
+ castF32ToBF16: exports.castF32ToBF16,
482
+ castE4M3ToF32: exports.castE4M3ToF32,
483
+ castF32ToE4M3: exports.castF32ToE4M3,
484
+ castE5M2ToF32: exports.castE5M2ToF32,
485
+ castF32ToE5M2: exports.castF32ToE5M2,
486
+ cast: exports.cast,
487
+ dotsPack,
488
+ dotsPacked,
489
+ angularsPacked,
490
+ euclideansPacked,
491
+ dotsSymmetric,
492
+ angularsSymmetric,
493
+ euclideansSymmetric,
494
+ dotsPackedSize,
495
+ outputDtype: types_js_1.outputDtype,
496
+ };
497
+ /**
498
+ * Finds the directory where the native build of the numkong module is located.
499
+ * @param {string} dir - The directory to start the search from.
500
+ */
501
+ function getBuildDir(dir) {
502
+ if ((0, node_fs_1.existsSync)(path.join(dir, "build")))
503
+ return dir;
504
+ if ((0, node_fs_1.existsSync)(path.join(dir, "prebuilds")))
505
+ return dir;
506
+ if (path.basename(dir) === ".next") {
507
+ // special case for next.js on custom node (not vercel)
508
+ const sideways = path.join(dir, "..", "node_modules", "numkong");
509
+ if ((0, node_fs_1.existsSync)(sideways))
510
+ return getBuildDir(sideways);
511
+ }
512
+ if (dir === "/")
513
+ throw new Error("Could not find native build for numkong");
514
+ return getBuildDir(path.join(dir, ".."));
515
+ }
516
+ function getDirName() {
517
+ try {
518
+ if (__dirname)
519
+ return __dirname;
520
+ }
521
+ catch (e) { }
522
+ return (0, bindings_1.getRoot)((0, bindings_1.getFileName)());
523
+ }
@@ -0,0 +1,3 @@
1
+ {
2
+ "type": "commonjs"
3
+ }