UNPKG

simsimd

Version:

Portable mixed-precision BLAS-like vector math library for x86 and ARM

203 lines (202 loc) 8.07 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.toBinary = exports.jensenshannon = exports.kullbackleibler = exports.jaccard = exports.hamming = exports.dot = exports.inner = exports.cosine = exports.euclidean = exports.sqeuclidean = void 0; const node_gyp_build_1 = __importDefault(require("node-gyp-build")); const path = __importStar(require("node:path")); const node_fs_1 = require("node:fs"); const bindings_1 = require("bindings"); const fallback = __importStar(require("./fallback.js")); let compiled; try { let builddir = getBuildDir(getDirName()); compiled = (0, node_gyp_build_1.default)(builddir); } catch (e) { compiled = fallback; console.warn("It seems like your environment doesn't support the native simsimd module, so we are providing a JS fallback."); } /** * @brief Computes the squared Euclidean distance between two vectors. * @param {Float64Array|Float32Array|Int8Array|Uint8Array} a - The first vector. * @param {Float64Array|Float32Array|Int8Array|Uint8Array} b - The second vector. * @returns {number} The squared Euclidean distance between vectors a and b. */ const sqeuclidean = (a, b) => { return compiled.sqeuclidean(a, b); }; exports.sqeuclidean = sqeuclidean; /** * @brief Computes the Euclidean distance between two vectors. * @param {Float64Array|Float32Array|Int8Array|Uint8Array} a - The first vector. * @param {Float64Array|Float32Array|Int8Array|Uint8Array} b - The second vector. * @returns {number} The Euclidean distance between vectors a and b. */ const euclidean = (a, b) => { return compiled.euclidean(a, b); }; exports.euclidean = euclidean; /** * @brief Computes the cosine distance between two vectors. * @param {Float64Array|Float32Array|Int8Array|Uint8Array} a - The first vector. * @param {Float64Array|Float32Array|Int8Array|Uint8Array} b - The second vector. * @returns {number} The cosine distance between vectors a and b. */ const cosine = (a, b) => { return compiled.cosine(a, b); }; exports.cosine = cosine; /** * @brief Computes the inner product of two vectors (same as dot product). * @param {Float64Array|Float32Array|Int8Array|Uint8Array} a - The first vector. * @param {Float64Array|Float32Array|Int8Array|Uint8Array} b - The second vector. * @returns {number} The inner product of vectors a and b. */ const inner = (a, b) => { return compiled.inner(a, b); }; exports.inner = inner; /** * @brief Computes the dot product of two vectors (same as inner product). * @param {Float64Array|Float32Array|Int8Array|Uint8Array} a - The first vector. * @param {Float64Array|Float32Array|Int8Array|Uint8Array} b - The second vector. * @returns {number} The dot product of vectors a and b. */ const dot = (a, b) => { return compiled.dot(a, b); }; exports.dot = dot; /** * @brief Computes the bitwise Hamming distance between two vectors. * @param {Uint8Array} a - The first vector. * @param {Uint8Array} b - The second vector. * @returns {number} The Hamming distance between vectors a and b. */ const hamming = (a, b) => { return compiled.hamming(a, b); }; exports.hamming = hamming; /** * @brief Computes the bitwise Jaccard distance between two vectors. * @param {Uint8Array} a - The first vector. * @param {Uint8Array} b - The second vector. * @returns {number} The Jaccard distance between vectors a and b. */ const jaccard = (a, b) => { return compiled.jaccard(a, b); }; exports.jaccard = jaccard; /** * @brief Computes the Kullback-Leibler divergence between two vectors. * @param {Float64Array|Float32Array} a - The first vector. * @param {Float64Array|Float32Array} b - The second vector. * @returns {number} The Kullback-Leibler divergence between vectors a and b. */ const kullbackleibler = (a, b) => { return compiled.kullbackleibler(a, b); }; exports.kullbackleibler = kullbackleibler; /** * @brief Computes the Jensen-Shannon divergence between two vectors. * @param {Float64Array|Float32Array} a - The first vector. * @param {Float64Array|Float32Array} b - The second vector. * @returns {number} The Jensen-Shannon divergence between vectors a and b. */ const jensenshannon = (a, b) => { return compiled.jensenshannon(a, b); }; exports.jensenshannon = jensenshannon; /** * Quantizes a floating-point vector into a binary vector (1 for positive values, 0 for non-positive values) and packs the result into a Uint8Array, where each element represents 8 binary values from the original vector. * This function is useful for preparing data for bitwise distance computations, such as Hamming or Jaccard indices. * * @param {Float32Array | Float64Array | Int8Array} vector The floating-point vector to be quantized and packed. * @returns {Uint8Array} A Uint8Array where each byte represents 8 binary quantized values from the input vector. */ const toBinary = (vector) => { const byteLength = Math.ceil(vector.length / 8); const packedVector = new Uint8Array(byteLength); for (let i = 0; i < vector.length; i++) { if (vector[i] > 0) { const byteIndex = Math.floor(i / 8); const bitPosition = 7 - (i % 8); packedVector[byteIndex] |= (1 << bitPosition); } } return packedVector; }; exports.toBinary = toBinary; exports.default = { dot: exports.dot, inner: exports.inner, sqeuclidean: exports.sqeuclidean, euclidean: exports.euclidean, cosine: exports.cosine, hamming: exports.hamming, jaccard: exports.jaccard, kullbackleibler: exports.kullbackleibler, jensenshannon: exports.jensenshannon, toBinary: exports.toBinary, }; /** * @brief Finds the directory where the native build of the simsimd module is located. * @param {string} dir - The directory to start the search from. */ function getBuildDir(dir) { if ((0, node_fs_1.existsSync)(path.join(dir, "build"))) return dir; if ((0, node_fs_1.existsSync)(path.join(dir, "prebuilds"))) return dir; if (path.basename(dir) === ".next") { // special case for next.js on custom node (not vercel) const sideways = path.join(dir, "..", "node_modules", "simsimd"); if ((0, node_fs_1.existsSync)(sideways)) return getBuildDir(sideways); } if (dir === "/") throw new Error("Could not find native build for simsimd"); return getBuildDir(path.join(dir, "..")); } function getDirName() { try { if (__dirname) return __dirname; } catch (e) { } return (0, bindings_1.getRoot)((0, bindings_1.getFileName)()); }