-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #64 from ashvardanian/main-dev
Make: ESM and CommonJS release with fallbacks (#63)
- Loading branch information
Showing
20 changed files
with
468 additions
and
150 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -84,4 +84,5 @@ jobs: | |
run: | | ||
npm ci --ignore-scripts | ||
npm run install | ||
npm run build-js | ||
npm test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,7 @@ node_modules/ | |
package-ci/ | ||
prebuilds/ | ||
build/ | ||
javascript/dist | ||
|
||
# for Google Benchmark | ||
compare.py | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,3 +10,4 @@ VERSION | |
package-ci.json | ||
setup.py | ||
CMakeLists.txt | ||
build |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
const benchmark = require('benchmark'); | ||
|
||
// Assuming the vectors are of the same length | ||
function cosineDistance(a, b) { | ||
let dotProduct = 0; | ||
let magA = 0; | ||
let magB = 0; | ||
for (let i = 0; i < a.length; i++) { | ||
dotProduct += a[i] * b[i]; | ||
magA += a[i] * a[i]; | ||
magB += b[i] * b[i]; | ||
} | ||
return 1 - (dotProduct / (Math.sqrt(magA) * Math.sqrt(magB))); | ||
} | ||
|
||
|
||
// Generate random data for testing | ||
const dimensions = 1536; // Adjust dimensions as needed | ||
const array1 = Array.from({ length: dimensions }, () => Math.random() * 100); | ||
const array2 = Array.from({ length: dimensions }, () => Math.random() * 100); | ||
const floatArray1 = new Float32Array(array1); | ||
const floatArray2 = new Float32Array(array2); | ||
|
||
|
||
// Create benchmark suite | ||
const singleSuite = new benchmark.Suite('Single Vector Processing'); | ||
|
||
// Single-vector processing benchmarks | ||
singleSuite | ||
|
||
// Pure JavaScript | ||
.add('Array of Numbers', () => { | ||
cosineDistance(array1, array2); | ||
}) | ||
.add('TypedArray of Float32', () => { | ||
cosineDistance(floatArray1, floatArray2); | ||
}) | ||
.on('cycle', (event) => { | ||
if (event.target.error) { | ||
console.error(String(event.target.error)); | ||
} else { | ||
console.log(String(event.target)); | ||
} | ||
}) | ||
.on('complete', () => { | ||
console.log('Fastest Single-Vector Processing is ' + singleSuite.filter('fastest').map('name')); | ||
}) | ||
.run({ | ||
noCache: true, | ||
async: false, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"type": "commonjs" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"type": "module" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
console.warn( | ||
"It seems like your environment does't support the native simsimd module, so we are providing a JS fallback." | ||
); | ||
|
||
/** | ||
* Computes the inner distance between two arrays. | ||
* @param {number[]} arr1 - The first array. | ||
* @param {number[]} arr2 - The second array. | ||
* @returns {number} The inner distance between arr1 and arr2. | ||
*/ | ||
export function inner_distance(arr1: number[], arr2: number[]): number { | ||
if (arr1.length !== arr2.length) { | ||
throw new Error("Vectors must have the same length"); | ||
} | ||
|
||
let result = 0; | ||
for (let i = 0; i < arr1.length; i++) { | ||
result += arr1[i] * arr2[i]; | ||
} | ||
return 1 - result; | ||
} | ||
|
||
/** | ||
* Computes the squared Euclidean distance between two arrays. | ||
* @param {number[]} arr1 - The first array. | ||
* @param {number[]} arr2 - The second array. | ||
* @returns {number} The squared Euclidean distance between arr1 and arr2. | ||
*/ | ||
export function sqeuclidean(arr1: number[], arr2: number[]): number { | ||
if (arr1.length !== arr2.length) { | ||
throw new Error("Vectors must have the same length"); | ||
} | ||
|
||
let result = 0; | ||
for (let i = 0; i < arr1.length; i++) { | ||
result += (arr1[i] - arr2[i]) * (arr1[i] - arr2[i]); | ||
} | ||
return result; | ||
} | ||
|
||
/** | ||
* Computes the cosine distance between two arrays. | ||
* @param {number[]} arr1 - The first array. | ||
* @param {number[]} arr2 - The second array. | ||
* @returns {number} The cosine distance between arr1 and arr2. | ||
*/ | ||
export function cosine(arr1: number[], arr2: number[]): number { | ||
if (arr1.length !== arr2.length) { | ||
throw new Error("Vectors must have the same length"); | ||
} | ||
|
||
let dotProduct = 0; | ||
let magnitudeA = 0; | ||
let magnitudeB = 0; | ||
|
||
for (let i = 0; i < arr1.length; i++) { | ||
dotProduct += arr1[i] * arr2[i]; | ||
magnitudeA += arr1[i] * arr1[i]; | ||
magnitudeB += arr2[i] * arr2[i]; | ||
} | ||
|
||
magnitudeA = Math.sqrt(magnitudeA); | ||
magnitudeB = Math.sqrt(magnitudeB); | ||
|
||
if (magnitudeA === 0 || magnitudeB === 0) { | ||
console.warn( | ||
"Warning: One of the magnitudes is zero. Cosine similarity is undefined." | ||
); | ||
return 0; | ||
} | ||
|
||
return 1 - dotProduct / (magnitudeA * magnitudeB); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
declare module "node-gyp-build"; |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.