usearch 2.8.2 → 2.8.4

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 (44) hide show
  1. package/.github/workflows/release.yml +1 -1
  2. package/.vscode/settings.json +1 -0
  3. package/CITATION.cff +1 -1
  4. package/CONTRIBUTING.md +258 -6
  5. package/Cargo.toml +1 -1
  6. package/README.md +57 -53
  7. package/conanfile.py +1 -1
  8. package/cpp/README.md +89 -21
  9. package/csharp/DEV_SETUP.md +1 -1
  10. package/csharp/nuget/nuget-package.props +1 -1
  11. package/docs/{compilation.rst → contributing.rst} +2 -2
  12. package/docs/index.rst +1 -1
  13. package/include/usearch/index.hpp +7 -6
  14. package/include/usearch/index_dense.hpp +11 -7
  15. package/javascript/usearch.js +4 -4
  16. package/javascript/usearch.test.js +22 -0
  17. package/package-ci.json +10 -0
  18. package/package.json +1 -5
  19. package/python/README.md +1 -1
  20. package/simsimd/CMakeLists.txt +1 -1
  21. package/simsimd/README.md +7 -1
  22. package/simsimd/include/simsimd/probability.h +24 -24
  23. package/simsimd/include/simsimd/types.h +8 -0
  24. package/simsimd/javascript/{test.js → test/simsimd.test.js} +15 -15
  25. package/simsimd/package.json +4 -8
  26. package/simsimd/setup.py +3 -0
  27. package/wasmer.toml +1 -1
  28. package/docs/compilation.md +0 -215
  29. package/simsimd/.clang-format +0 -10
  30. package/simsimd/.github/workflows/prerelease.yml +0 -89
  31. package/simsimd/.github/workflows/release.yml +0 -141
  32. package/simsimd/.github/workflows/update_version.sh +0 -5
  33. package/simsimd/.vscode/launch.json +0 -53
  34. package/simsimd/.vscode/settings.json +0 -114
  35. package/simsimd/.vscode/tasks.json +0 -23
  36. package/simsimd/go.mod +0 -3
  37. package/simsimd/golang/bench_test.go +0 -40
  38. package/simsimd/golang/simsimd.go +0 -47
  39. package/simsimd/golang/unit_test.go +0 -40
  40. package/simsimd/pyproject.toml +0 -42
  41. package/simsimd/python/bench.ipynb +0 -187
  42. package/simsimd/python/bench.py +0 -268
  43. package/simsimd/python/lib.c +0 -492
  44. package/simsimd/python/test.py +0 -195
package/cpp/README.md CHANGED
@@ -25,14 +25,20 @@ metric_punned_t metric(256, metric_kind_t::l2sq_k, scalar_kind_t::f32_k);
25
25
  index_dense_t index = index_dense_t::make(metric);
26
26
  float vec[3] = {0.1, 0.3, 0.2};
27
27
 
28
- index.reserve(10);
29
- index.add(/* key: */ 42, /* vector: */ {&vec[0], 3});
30
- auto results = index.search(/* query: */ {&vec[0], 3}, 5 /* neighbors */);
28
+ index.reserve(10); // Pre-allocate memory for 10 vectors
29
+ index.add(42, &vec[0]); // Pass a key and a vector
30
+ auto results = index.search(&vec[0], 5); // Pass a query and limit number of results
31
31
 
32
32
  for (std::size_t i = 0; i != results.size(); ++i)
33
33
  results[i].element.key, results[i].element.vector, results[i].distance;
34
34
  ```
35
35
 
36
+ Here we:
37
+
38
+ - define a metric of kind [`metric_kind_t::l2sq_k`](https://unum-cloud.github.io/usearch/cpp/reference.html#_CPPv413metric_kind_t),
39
+ - to be applied to [`scalar_kind_t::f32_k`](https://unum-cloud.github.io/usearch/cpp/reference.html#_CPPv413scalar_kind_t) floating-point vectors,
40
+ - instantiate an [`index_dense_t`](https://unum-cloud.github.io/usearch/cpp/reference.html#_CPPv4I00EN4unum7usearch14index_dense_gtE) index.
41
+
36
42
  The `add` is thread-safe for concurrent index construction.
37
43
  It also has an overload for different vector types, casting them under the hood.
38
44
  The same applies to the `search`, `get`, `cluster`, and `distance_between` functions.
@@ -52,20 +58,78 @@ index.load("index.usearch"); // Copying from disk
52
58
  index.view("index.usearch"); // Memory-mapping from disk
53
59
  ```
54
60
 
55
- ## User-Defined Metrics in C++
61
+ ## Multi-Threading
56
62
 
57
- For advanced users, more compile-time abstractions are available.
63
+ Most AI, HPC, or Big Data packages use some form of a thread pool.
64
+ Instead of spawning additional threads within USearch, we focus on the thread safety of `add()` function, simplifying resource management.
58
65
 
59
66
  ```cpp
60
- template <typename distance_at = default_distance_t, // `float`
61
- typename key_at = default_key_t, // `int64_t`, `uuid_t`
62
- typename compressed_slot_at = default_slot_t, // `uint32_t`, `uint40_t`
63
- typename dynamic_allocator_at = std::allocator<byte_t>, //
64
- typename tape_allocator_at = dynamic_allocator_at> //
65
- class index_gt;
67
+ #pragma omp parallel for
68
+ for (std::size_t i = 0; i < n; ++i)
69
+ native.add(key, span_t{vector, dims});
70
+ ```
71
+
72
+ During initialization, we allocate enough temporary memory for all the cores on the machine.
73
+ On the call, the user can supply the identifier of the current thread, making this library easy to integrate with OpenMP and similar tools.
74
+
75
+ Moreover, you can take advantage of one of the provided "executors" to parallelize the search:
76
+
77
+ - `executor_openmp_t`, that would use OpenMP under the hood.
78
+ - `executor_stl_t`, that will spawn `std::thread` instances.
79
+ - `dummy_executor_t`, that will run everything sequentially.
80
+
81
+ ## Clustering
82
+
83
+ Aside from basic Create-Read-Update-Delete (CRUD) operations and search, USearch also supports clustering.
84
+ Once the index is constructed, you can either:
85
+
86
+ - Identify a cluster to which any external vector belongs, once mapped onto the index.
87
+ - Split the entire index into a set of clusters, each with its own centroid.
88
+
89
+ For the first, the interface accepts a vector and a "clustering level", which is essentially the index of the HNSW graph layer, in which to search.
90
+ If you pass zero, the traversal will happen in every level except the bottom one.
91
+ Otherwise, the search will be limited to the specified level.
92
+
93
+ ```cpp
94
+ some_scalar_t vector[3] = {0.1, 0.3, 0.2};
95
+ cluster_result_t result = index.cluster(&vector, index.max_level() / 2);
96
+ match_t cluster = result.cluster;
97
+ member_cref_t member = cluster.member;
98
+ distance_t distance = cluster.distance;
66
99
  ```
67
100
 
68
- The following distances are pre-packaged:
101
+ If you wish to split the whole structure into clusters, you must provide an iterator over a range of vectors, that will be processed in parallel using the previously described function.
102
+ Unlike the previous function, you don't have to manually specify the level, as the algorithm will pick the best one for you, depending on the number of clusters you want to highlight.
103
+ Aside from that auto-tuning, this function will regroup some of the clusters, if they are too small, and return the final number of clusters.
104
+
105
+ ```cpp
106
+ std::size_t queries_count = queries_end - queries_begin;
107
+ index_dense_clustering_config_t config;
108
+ config.min_clusters = 1000;
109
+ config.max_clusters = 2000;
110
+ config.mode = index_dense_clustering_config_t::merge_smallest_k;
111
+
112
+ // Outputs:
113
+ vector_key_t cluster_centroids_keys[queries_count];
114
+ distance_t distances_to_cluster_centroids[queries_count];
115
+ executor_default_t thread_pool;
116
+ dummy_progress_t progress_bar;
117
+
118
+ clustering_result_t result = cluster(
119
+ queries_begin, queries_end,
120
+ config,
121
+ &cluster_centroids_keys, &distances_to_cluster_centroids,
122
+ thread_pool, progress_bar);
123
+ ```
124
+
125
+ This approach requires basic understanding of templates meta-programming to implement the `queries_begin` and `queries_end` smart-iterators.
126
+ On the bright side, it allows iteratively deepening into a specific cluster.
127
+
128
+ As in many other bulk-processing APIs, the `executor` and `progress` are optional.
129
+
130
+ ## User-Defined Metrics
131
+
132
+ In its high-level interface, USearch supports a variety of metrics, including the most popular ones:
69
133
 
70
134
  - `metric_cos_gt<scalar_t>` for "Cosine" or "Angular" distance.
71
135
  - `metric_ip_gt<scalar_t>` for "Inner Product" or "Dot Product" distance.
@@ -78,16 +142,20 @@ The following distances are pre-packaged:
78
142
  - `metric_haversine_gt<scalar_t>` for "Haversine" or "Great Circle" distance between coordinates used in GIS applications.
79
143
  - `metric_divergence_gt<scalar_t>` for the "Jensen Shannon" similarity between probability distributions.
80
144
 
81
- ## Multi-Threading
145
+ In reality, for most common types, one of the [SimSIMD](https://github.com/ashvardanian/SimSIMD) backends will be triggered, providing hardware-acceleration for most common CPUs.
82
146
 
83
- Most AI, HPC, or Big Data packages use some form of a thread pool.
84
- Instead of spawning additional threads within USearch, we focus on the thread safety of `add()` function, simplifying resource management.
147
+ If you need a different metric, you can implement it yourself and wrap it into a `metric_punned_t`, which is our alternative to the `std::function`.
148
+ Unlike the `std::function`, it is a trivial type, which is important for performance.
149
+
150
+ ## Advanced Interface
151
+
152
+ If you are proficient in C++ and ready to get your hands dirty, you can use the low-level interface.
85
153
 
86
154
  ```cpp
87
- #pragma omp parallel for
88
- for (std::size_t i = 0; i < n; ++i)
89
- native.add(key, span_t{vector, dims});
155
+ template <typename distance_at = default_distance_t, // `float`
156
+ typename key_at = default_key_t, // `int64_t`, `uuid_t`
157
+ typename compressed_slot_at = default_slot_t, // `uint32_t`, `uint40_t`
158
+ typename dynamic_allocator_at = std::allocator<byte_t>, //
159
+ typename tape_allocator_at = dynamic_allocator_at> //
160
+ class index_gt;
90
161
  ```
91
-
92
- During initialization, we allocate enough temporary memory for all the cores on the machine.
93
- On the call, the user can supply the identifier of the current thread, making this library easy to integrate with OpenMP and similar tools.
@@ -11,7 +11,7 @@ To obtain all libraries, download the artifacts generated by the GitHub workflow
11
11
 
12
12
  To get a library for your current platform (Linux or Macos), execute: `./eng/build_lib.sh`.
13
13
 
14
- For more details, refer to the [compilation guide](https://github.com/unum-cloud/usearch/blob/main/docs/compilation.md).
14
+ For more details, refer to the [compilation guide](https://github.com/unum-cloud/usearch/blob/main/CONTRIBUTING.md).
15
15
 
16
16
  # Build and Test
17
17
 
@@ -1,7 +1,7 @@
1
1
  <?xml version="1.0" encoding="utf-8"?>
2
2
  <Project>
3
3
  <PropertyGroup>
4
- <Version Condition="'$(Version)' == ''">2.8.2</Version>
4
+ <Version Condition="'$(Version)' == ''">2.8.4</Version>
5
5
 
6
6
  <Authors>Unum</Authors>
7
7
  <Company>Unum</Company>
@@ -1,5 +1,5 @@
1
1
  ========================
2
- Compilation
2
+ Contributing
3
3
  ========================
4
4
 
5
- .. mdinclude:: compilation.md
5
+ .. mdinclude:: ../CONTRIBUTING.md
package/docs/index.rst CHANGED
@@ -22,7 +22,7 @@ Overview
22
22
  :hidden:
23
23
  :caption: �
24
24
 
25
- compilation
25
+ contributing
26
26
  benchmarks
27
27
  format
28
28
 
@@ -11,7 +11,7 @@
11
11
 
12
12
  #define USEARCH_VERSION_MAJOR 2
13
13
  #define USEARCH_VERSION_MINOR 8
14
- #define USEARCH_VERSION_PATCH 2
14
+ #define USEARCH_VERSION_PATCH 4
15
15
 
16
16
  // Inferring C++ version
17
17
  // https://stackoverflow.com/a/61552074
@@ -547,6 +547,7 @@ class max_heap_gt {
547
547
  inline bool empty() const noexcept { return !size_; }
548
548
  inline std::size_t size() const noexcept { return size_; }
549
549
  inline std::size_t capacity() const noexcept { return capacity_; }
550
+
550
551
  /// @brief Selects the largest element in the heap.
551
552
  /// @return Reference to the stored element.
552
553
  inline element_t const& top() const noexcept { return elements_[0]; }
@@ -2205,7 +2206,7 @@ class index_gt {
2205
2206
  node_t node = nodes_[candidate.slot];
2206
2207
  return {member_cref_t{node.ckey(), candidate.slot}, candidate.distance};
2207
2208
  }
2208
- inline std::size_t merge_into( //
2209
+ inline std::size_t merge_into( //
2209
2210
  vector_key_t* keys, distance_t* distances, //
2210
2211
  std::size_t old_count, std::size_t max_count) const noexcept {
2211
2212
 
@@ -2279,10 +2280,10 @@ class index_gt {
2279
2280
  typename callback_at = dummy_callback_t, //
2280
2281
  typename prefetch_at = dummy_prefetch_t //
2281
2282
  >
2282
- add_result_t add( //
2283
+ add_result_t add( //
2283
2284
  vector_key_t key, value_at&& value, metric_at&& metric, //
2284
- index_update_config_t config = {}, //
2285
- callback_at&& callback = callback_at{}, //
2285
+ index_update_config_t config = {}, //
2286
+ callback_at&& callback = callback_at{}, //
2286
2287
  prefetch_at&& prefetch = prefetch_at{}) usearch_noexcept_m {
2287
2288
 
2288
2289
  add_result_t result;
@@ -2389,7 +2390,7 @@ class index_gt {
2389
2390
  >
2390
2391
  add_result_t update( //
2391
2392
  member_iterator_t iterator, //
2392
- vector_key_t key, //
2393
+ vector_key_t key, //
2393
2394
  value_at&& value, //
2394
2395
  metric_at&& metric, //
2395
2396
  index_update_config_t config = {}, //
@@ -323,7 +323,7 @@ class index_dense_gt {
323
323
  /// @brief Schema: input buffer, bytes in input buffer, output buffer.
324
324
  using cast_t = std::function<bool(byte_t const*, std::size_t, byte_t*)>;
325
325
  /// @brief Punned index.
326
- using index_t = index_gt< //
326
+ using index_t = index_gt< //
327
327
  distance_t, vector_key_t, compressed_slot_t, //
328
328
  dynamic_allocator_t, tape_allocator_t>;
329
329
  using index_allocator_t = aligned_allocator_gt<index_t, 64>;
@@ -1512,10 +1512,14 @@ class index_dense_gt {
1512
1512
  * @brief Implements clustering, classifying the given objects (vectors of member keys)
1513
1513
  * into a given number of clusters.
1514
1514
  *
1515
- * @param[in] queries_begin Iterator targeting the fiest query.
1516
- * @param[in] queries_end
1515
+ * @param[in] queries_begin Iterator pointing to the first query.
1516
+ * @param[in] queries_end Iterator pointing to the last query.
1517
1517
  * @param[in] executor Thread-pool to execute the job in parallel.
1518
1518
  * @param[in] progress Callback to report the execution progress.
1519
+ * @param[in] config Configuration parameters for clustering.
1520
+ *
1521
+ * @param[out] cluster_keys Pointer to the array where the cluster keys will be exported.
1522
+ * @param[out] cluster_distances Pointer to the array where the distances to those centroids will be exported.
1519
1523
  */
1520
1524
  template < //
1521
1525
  typename queries_iterator_at, //
@@ -1526,7 +1530,7 @@ class index_dense_gt {
1526
1530
  queries_iterator_at queries_begin, //
1527
1531
  queries_iterator_at queries_end, //
1528
1532
  index_dense_clustering_config_t config, //
1529
- vector_key_t* cluster_keys, //
1533
+ vector_key_t* cluster_keys, //
1530
1534
  distance_t* cluster_distances, //
1531
1535
  executor_at&& executor = executor_at{}, //
1532
1536
  progress_at&& progress = progress_at{}) {
@@ -1715,7 +1719,7 @@ class index_dense_gt {
1715
1719
  }
1716
1720
 
1717
1721
  template <typename scalar_at>
1718
- add_result_t add_( //
1722
+ add_result_t add_( //
1719
1723
  vector_key_t key, scalar_at const* vector, //
1720
1724
  std::size_t thread, bool force_vector_copy, cast_t const& cast) {
1721
1725
 
@@ -1811,8 +1815,8 @@ class index_dense_gt {
1811
1815
  }
1812
1816
 
1813
1817
  template <typename scalar_at>
1814
- aggregated_distances_t distance_between_( //
1815
- vector_key_t key, scalar_at const* vector, //
1818
+ aggregated_distances_t distance_between_( //
1819
+ vector_key_t key, scalar_at const* vector, //
1816
1820
  std::size_t thread, cast_t const& cast) const {
1817
1821
 
1818
1822
  // Cast the vector, if needed for compatibility with `metric_`
@@ -84,7 +84,7 @@ class BatchMatches {
84
84
  }
85
85
 
86
86
  function isOneKey(keys) {
87
- return typeof keys === 'number' || typeof keys === 'bigint';
87
+ return (!Number.isNaN(keys) && typeof keys === 'number') || typeof keys === 'bigint';
88
88
  }
89
89
 
90
90
  function normalizeKeys(keys) {
@@ -92,7 +92,7 @@ function normalizeKeys(keys) {
92
92
  keys = BigUint64Array.of(BigInt(keys));
93
93
  } else if (Array.isArray(keys)) {
94
94
  keys = keys.map(key => {
95
- if (typeof key !== 'bigint' && typeof key !== 'number')
95
+ if ((typeof key !== 'bigint' && typeof key !== 'number') || Number.isNaN(key))
96
96
  throw new Error("All keys must be integers or bigints.");
97
97
  return BigInt(key);
98
98
  });
@@ -151,7 +151,7 @@ class Index {
151
151
  if (typeof dimensionsOrConfigs === 'object' && dimensionsOrConfigs !== null) {
152
152
  // Parameters are provided as an object
153
153
  ({ dimensions, metric = MetricKind.Cos, quantization = ScalarKind.F32, connectivity = 0, expansion_add = 0, expansion_search = 0, multi = false } = dimensionsOrConfigs);
154
- } else if (typeof dimensionsOrConfigs === 'number' || typeof dimensionsOrConfigs === 'bigint') {
154
+ } else if ((typeof dimensionsOrConfigs === 'number' && !Number.isNaN(dimensionsOrConfigs)) || typeof dimensionsOrConfigs === 'bigint') {
155
155
  // Parameters are provided as individual arguments
156
156
  dimensions = dimensionsOrConfigs;
157
157
  } else {
@@ -238,7 +238,7 @@ class Index {
238
238
  * @throws Will throw an error if `vectors` is not a valid input type (TypedArray or an array of arrays) or if its flattened size is not a multiple of dimensions.
239
239
  */
240
240
  search(vectors, k) {
241
- if (typeof k !== 'number' || k <= 0) {
241
+ if ((!Number.isNaN(k) && typeof k !== 'number') || k <= 0) {
242
242
  throw new Error("`k` must be a positive integer representing the number of nearest neighbors to search for.");
243
243
  }
244
244
 
@@ -35,3 +35,25 @@ test('Batch operations', () => {
35
35
  assert.deepEqual(results.keys, new BigUint64Array([15n, 16n]), 'keys should be 15 and 16');
36
36
  assert.deepEqual(results.distances, new Float32Array([45, 130]), 'distances should be 45 and 130');
37
37
  });
38
+
39
+
40
+ test('Operations with invalid values', () => {
41
+ const indexBatch = new usearch.Index(2, 'l2sq');
42
+
43
+ const keys = [NaN, 16n];
44
+ const vectors = [new Float32Array([10, 30]), new Float32Array([1, 5])];
45
+
46
+ try {
47
+ indexBatch.add(keys, vectors);
48
+ throw new Error('indexBatch.add should have thrown an error.');
49
+ } catch (err) {
50
+ assert.equal(err.message, 'All keys must be integers or bigints.');
51
+ }
52
+
53
+ try {
54
+ indexBatch.search(NaN, 2);
55
+ throw new Error('indexBatch.search should have thrown an error.');
56
+ } catch (err) {
57
+ assert.equal(err.message, 'Vectors must be a TypedArray or an array of arrays.');
58
+ }
59
+ });
@@ -0,0 +1,10 @@
1
+ {
2
+ "name": "usearch-ci",
3
+ "version": "1.0.0",
4
+ "devDependencies": {
5
+ "@semantic-release/exec": "^6.0.3",
6
+ "@semantic-release/git": "^10.0.1",
7
+ "conventional-changelog-eslint": "^3.0.9",
8
+ "semantic-release": "^21.1.2"
9
+ }
10
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "usearch",
3
- "version": "2.8.2",
3
+ "version": "2.8.4",
4
4
  "description": "Smaller & Faster Single-File Vector Search Engine from Unum",
5
5
  "author": "Ash Vardanian (https://ashvardanian.com/)",
6
6
  "license": "Apache 2.0",
@@ -27,10 +27,6 @@
27
27
  "test": "node --test ./javascript/usearch.test.js"
28
28
  },
29
29
  "devDependencies": {
30
- "@semantic-release/exec": "^6.0.3",
31
- "@semantic-release/git": "^10.0.1",
32
- "conventional-changelog-eslint": "^3.0.9",
33
- "semantic-release": "^21.1.2",
34
30
  "typescript": "^5.1.6"
35
31
  }
36
32
  }
package/python/README.md CHANGED
@@ -10,7 +10,7 @@ pip install usearch
10
10
 
11
11
  ```python
12
12
  import numpy as np
13
- from usearch.index import Index
13
+ from usearch.index import Index, Matches
14
14
 
15
15
  index = Index(
16
16
  ndim=3, # Define the number of dimensions in input vectors
@@ -48,7 +48,7 @@ if(CMAKE_CXX_COMPILER_ID MATCHES "^(Apple)?Clang$")
48
48
  endif()
49
49
  add_compile_options(-pedantic -ferror-limit=1)
50
50
  elseif(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
51
- add_compile_options(-march=native -pedantic -fmax-errors=1)
51
+ add_compile_options(-march=native -pedantic -fmax-errors=1 -Wno-tautological-constant-compare)
52
52
  elseif(CMAKE_CXX_COMPILER_ID STREQUAL "IntelLLVM")
53
53
  add_compile_options(-w -ferror-limit=1)
54
54
  endif()
package/simsimd/README.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # SimSIMD 📏
2
2
 
3
+ <div>
4
+ <a href="https://pypi.org/project/simsimd/"> <img alt="PyPI - Downloads" src="https://img.shields.io/pypi/dm/simsimd?label=pypi%20downloads"> </a>
5
+ <a href="https://www.npmjs.com/package/simsimd"> <img alt="npm" src="https://img.shields.io/npm/dy/simsimd?label=npm%20dowloads"> </a>
6
+ <img alt="GitHub code size in bytes" src="https://img.shields.io/github/languages/code-size/ashvardanian/simsimd">
7
+ </div>
8
+
3
9
  ## Efficient Alternative to [`scipy.spatial.distance`][scipy] and [`numpy.inner`][numpy]
4
10
 
5
11
  SimSIMD leverages SIMD intrinsics, capabilities that only select compilers effectively utilize. This framework supports conventional AVX2 instructions on x86, NEON on Arm, as well as __rare__ AVX-512 FP16 instructions on x86 and Scalable Vector Extensions (SVE) on Arm. Designed specifically for Machine Learning contexts, it's optimized for handling high-dimensional vector embeddings.
@@ -189,7 +195,7 @@ __To test and benchmark JavaScript bindings__:
189
195
  ```sh
190
196
  npm install --dev
191
197
  npm test
192
- npm bench
198
+ npm run bench
193
199
  ```
194
200
 
195
201
  __To test and benchmark GoLang bindings__:
@@ -52,17 +52,17 @@
52
52
  extern "C" {
53
53
  #endif
54
54
 
55
- SIMSIMD_MAKE_KL(serial, f32, f32, SIMSIMD_IDENTIFY, 1e-6) // simsimd_serial_f32_kl
56
- SIMSIMD_MAKE_JS(serial, f32, f32, SIMSIMD_IDENTIFY, 1e-6) // simsimd_serial_f32_js
55
+ SIMSIMD_MAKE_KL(serial, f32, f32, SIMSIMD_IDENTIFY, SIMSIMD_F32_DIVISION_EPSILON) // simsimd_serial_f32_kl
56
+ SIMSIMD_MAKE_JS(serial, f32, f32, SIMSIMD_IDENTIFY, SIMSIMD_F32_DIVISION_EPSILON) // simsimd_serial_f32_js
57
57
 
58
- SIMSIMD_MAKE_KL(serial, f16, f32, SIMSIMD_UNCOMPRESS_F16, 1e-3) // simsimd_serial_f16_kl
59
- SIMSIMD_MAKE_JS(serial, f16, f32, SIMSIMD_UNCOMPRESS_F16, 1e-3) // simsimd_serial_f16_js
58
+ SIMSIMD_MAKE_KL(serial, f16, f32, SIMSIMD_UNCOMPRESS_F16, SIMSIMD_F32_DIVISION_EPSILON) // simsimd_serial_f16_kl
59
+ SIMSIMD_MAKE_JS(serial, f16, f32, SIMSIMD_UNCOMPRESS_F16, SIMSIMD_F32_DIVISION_EPSILON) // simsimd_serial_f16_js
60
60
 
61
- SIMSIMD_MAKE_KL(accurate, f32, f64, SIMSIMD_IDENTIFY, 1e-6) // simsimd_accurate_f32_kl
62
- SIMSIMD_MAKE_JS(accurate, f32, f64, SIMSIMD_IDENTIFY, 1e-6) // simsimd_accurate_f32_js
61
+ SIMSIMD_MAKE_KL(accurate, f32, f64, SIMSIMD_IDENTIFY, SIMSIMD_F32_DIVISION_EPSILON) // simsimd_accurate_f32_kl
62
+ SIMSIMD_MAKE_JS(accurate, f32, f64, SIMSIMD_IDENTIFY, SIMSIMD_F32_DIVISION_EPSILON) // simsimd_accurate_f32_js
63
63
 
64
- SIMSIMD_MAKE_KL(accurate, f16, f64, SIMSIMD_UNCOMPRESS_F16, 1e-6) // simsimd_accurate_f16_kl
65
- SIMSIMD_MAKE_JS(accurate, f16, f64, SIMSIMD_UNCOMPRESS_F16, 1e-6) // simsimd_accurate_f16_js
64
+ SIMSIMD_MAKE_KL(accurate, f16, f64, SIMSIMD_UNCOMPRESS_F16, SIMSIMD_F32_DIVISION_EPSILON) // simsimd_accurate_f16_kl
65
+ SIMSIMD_MAKE_JS(accurate, f16, f64, SIMSIMD_UNCOMPRESS_F16, SIMSIMD_F32_DIVISION_EPSILON) // simsimd_accurate_f16_js
66
66
 
67
67
  #if SIMSIMD_TARGET_ARM
68
68
  #if SIMSIMD_TARGET_ARM_NEON
@@ -108,7 +108,7 @@ __attribute__((target("+simd"))) //
108
108
  inline static simsimd_f32_t
109
109
  simsimd_neon_f32_kl(simsimd_f32_t const* a, simsimd_f32_t const* b, simsimd_size_t n) {
110
110
  float32x4_t sum_vec = vdupq_n_f32(0);
111
- simsimd_f32_t epsilon = 1e-6;
111
+ simsimd_f32_t epsilon = SIMSIMD_F32_DIVISION_EPSILON;
112
112
  float32x4_t epsilon_vec = vdupq_n_f32(epsilon);
113
113
  simsimd_size_t i = 0;
114
114
  for (; i + 4 <= n; i += 4) {
@@ -129,7 +129,7 @@ simsimd_neon_f32_kl(simsimd_f32_t const* a, simsimd_f32_t const* b, simsimd_size
129
129
  __attribute__((target("+simd"))) inline static simsimd_f32_t
130
130
  simsimd_neon_f32_js(simsimd_f32_t const* a, simsimd_f32_t const* b, simsimd_size_t n) {
131
131
  float32x4_t sum_vec = vdupq_n_f32(0);
132
- simsimd_f32_t epsilon = 1e-6;
132
+ simsimd_f32_t epsilon = SIMSIMD_F32_DIVISION_EPSILON;
133
133
  float32x4_t epsilon_vec = vdupq_n_f32(epsilon);
134
134
  simsimd_size_t i = 0;
135
135
  for (; i + 4 <= n; i += 4) {
@@ -147,11 +147,11 @@ simsimd_neon_f32_js(simsimd_f32_t const* a, simsimd_f32_t const* b, simsimd_size
147
147
  simsimd_f32_t log2_normalizer = 0.693147181f;
148
148
  simsimd_f32_t sum = vaddvq_f32(sum_vec) * log2_normalizer;
149
149
  for (; i < n; ++i) {
150
- simsimd_f32_t mi = a[i] + b[i];
150
+ simsimd_f32_t mi = 0.5f * (a[i] + b[i]);
151
151
  sum += a[i] * SIMSIMD_LOG((a[i] + epsilon) / (mi + epsilon));
152
152
  sum += b[i] * SIMSIMD_LOG((b[i] + epsilon) / (mi + epsilon));
153
153
  }
154
- return sum * 0.5f;
154
+ return sum;
155
155
  }
156
156
 
157
157
  /*
@@ -168,7 +168,7 @@ __attribute__((target("+simd+fp16"))) //
168
168
  inline static simsimd_f32_t
169
169
  simsimd_neon_f16_kl(simsimd_f16_t const* a, simsimd_f16_t const* b, simsimd_size_t n) {
170
170
  float32x4_t sum_vec = vdupq_n_f32(0);
171
- simsimd_f32_t epsilon = 1e-3;
171
+ simsimd_f32_t epsilon = SIMSIMD_F32_DIVISION_EPSILON;
172
172
  float32x4_t epsilon_vec = vdupq_n_f32(epsilon);
173
173
  simsimd_size_t i = 0;
174
174
  for (; i + 4 <= n; i += 4) {
@@ -191,7 +191,7 @@ __attribute__((target("+simd+fp16"))) //
191
191
  inline static simsimd_f32_t
192
192
  simsimd_neon_f16_js(simsimd_f16_t const* a, simsimd_f16_t const* b, simsimd_size_t n) {
193
193
  float32x4_t sum_vec = vdupq_n_f32(0);
194
- simsimd_f32_t epsilon = 1e-3;
194
+ simsimd_f32_t epsilon = SIMSIMD_F32_DIVISION_EPSILON;
195
195
  float32x4_t epsilon_vec = vdupq_n_f32(epsilon);
196
196
  simsimd_size_t i = 0;
197
197
  for (; i + 4 <= n; i += 4) {
@@ -211,11 +211,11 @@ simsimd_neon_f16_js(simsimd_f16_t const* a, simsimd_f16_t const* b, simsimd_size
211
211
  for (; i < n; ++i) {
212
212
  simsimd_f32_t ai = SIMSIMD_UNCOMPRESS_F16(a[i]);
213
213
  simsimd_f32_t bi = SIMSIMD_UNCOMPRESS_F16(b[i]);
214
- simsimd_f32_t mi = ai + bi;
214
+ simsimd_f32_t mi = 0.5f * (ai + bi);
215
215
  sum += ai * SIMSIMD_LOG((ai + epsilon) / (mi + epsilon));
216
216
  sum += bi * SIMSIMD_LOG((bi + epsilon) / (mi + epsilon));
217
217
  }
218
- return sum / 2;
218
+ return sum;
219
219
  }
220
220
 
221
221
  #endif // SIMSIMD_TARGET_ARM_NEON
@@ -268,7 +268,7 @@ __attribute__((target("avx2,f16c,fma"))) //
268
268
  inline static simsimd_f32_t
269
269
  simsimd_avx2_f16_kl(simsimd_f16_t const* a, simsimd_f16_t const* b, simsimd_size_t n) {
270
270
  __m256 sum_vec = _mm256_set1_ps(0);
271
- simsimd_f32_t epsilon = 1e-5;
271
+ simsimd_f32_t epsilon = SIMSIMD_F32_DIVISION_EPSILON;
272
272
  __m256 epsilon_vec = _mm256_set1_ps(epsilon);
273
273
  simsimd_size_t i = 0;
274
274
  for (; i + 8 <= n; i += 8) {
@@ -300,7 +300,7 @@ __attribute__((target("avx2,f16c,fma"))) //
300
300
  inline static simsimd_f32_t
301
301
  simsimd_avx2_f16_js(simsimd_f16_t const* a, simsimd_f16_t const* b, simsimd_size_t n) {
302
302
  __m256 sum_vec = _mm256_set1_ps(0);
303
- simsimd_f32_t epsilon = 1e-5;
303
+ simsimd_f32_t epsilon = SIMSIMD_F32_DIVISION_EPSILON;
304
304
  __m256 epsilon_vec = _mm256_set1_ps(epsilon);
305
305
  simsimd_size_t i = 0;
306
306
  for (; i + 8 <= n; i += 8) {
@@ -374,7 +374,7 @@ __attribute__((target("avx512f,avx512vl,bmi2"))) //
374
374
  inline static simsimd_f32_t
375
375
  simsimd_avx512_f32_kl(simsimd_f32_t const* a, simsimd_f32_t const* b, simsimd_size_t n) {
376
376
  __m512 sum_vec = _mm512_set1_ps(0);
377
- simsimd_f32_t epsilon = 1e-6;
377
+ simsimd_f32_t epsilon = SIMSIMD_F32_DIVISION_EPSILON;
378
378
  __m512 epsilon_vec = _mm512_set1_ps(epsilon);
379
379
  __m512 a_vec, b_vec;
380
380
 
@@ -405,7 +405,7 @@ inline static simsimd_f32_t
405
405
  simsimd_avx512_f32_js(simsimd_f32_t const* a, simsimd_f32_t const* b, simsimd_size_t n) {
406
406
  __m512 sum_a_vec = _mm512_set1_ps(0);
407
407
  __m512 sum_b_vec = _mm512_set1_ps(0);
408
- simsimd_f32_t epsilon = 1e-6;
408
+ simsimd_f32_t epsilon = SIMSIMD_F32_DIVISION_EPSILON;
409
409
  __m512 epsilon_vec = _mm512_set1_ps(epsilon);
410
410
  __m512 a_vec, b_vec;
411
411
 
@@ -425,8 +425,8 @@ simsimd_avx512_f32_js_cycle:
425
425
  __mmask16 nonzero_mask_b = _mm512_cmp_ps_mask(b_vec, epsilon_vec, _CMP_GE_OQ);
426
426
  __mmask16 nonzero_mask = nonzero_mask_a & nonzero_mask_b;
427
427
  __m512 m_recip_approx = _mm512_rcp14_ps(m_vec);
428
- __m512 ratio_a_vec = _mm512_mul_ps(a_vec, m_recip_approx);
429
- __m512 ratio_b_vec = _mm512_mul_ps(b_vec, m_recip_approx);
428
+ __m512 ratio_a_vec = _mm512_div_ps(a_vec, m_vec);
429
+ __m512 ratio_b_vec = _mm512_div_ps(b_vec, m_vec);
430
430
  __m512 log_ratio_a_vec = simsimd_avx512_f32_log2(ratio_a_vec);
431
431
  __m512 log_ratio_b_vec = simsimd_avx512_f32_log2(ratio_b_vec);
432
432
  sum_a_vec = _mm512_maskz_fmadd_ps(nonzero_mask, a_vec, log_ratio_a_vec, sum_a_vec);
@@ -472,7 +472,7 @@ __attribute__((target("avx512f,avx512vl,avx512fp16,bmi2"))) //
472
472
  inline static simsimd_f32_t
473
473
  simsimd_avx512_f16_kl(simsimd_f16_t const* a, simsimd_f16_t const* b, simsimd_size_t n) {
474
474
  __m512h sum_vec = _mm512_set1_ph((_Float16)0);
475
- __m512h epsilon_vec = _mm512_set1_ph((_Float16)1e-6f);
475
+ __m512h epsilon_vec = _mm512_set1_ph((_Float16)SIMSIMD_F16_DIVISION_EPSILON);
476
476
  __m512h a_vec, b_vec;
477
477
 
478
478
  simsimd_avx512_f16_kl_cycle:
@@ -502,7 +502,7 @@ inline static simsimd_f32_t
502
502
  simsimd_avx512_f16_js(simsimd_f16_t const* a, simsimd_f16_t const* b, simsimd_size_t n) {
503
503
  __m512h sum_a_vec = _mm512_set1_ph((_Float16)0);
504
504
  __m512h sum_b_vec = _mm512_set1_ph((_Float16)0);
505
- __m512h epsilon_vec = _mm512_set1_ph((_Float16)1e-6f);
505
+ __m512h epsilon_vec = _mm512_set1_ph((_Float16)SIMSIMD_F16_DIVISION_EPSILON);
506
506
  __m512h a_vec, b_vec;
507
507
 
508
508
  simsimd_avx512_f16_js_cycle:
@@ -91,6 +91,14 @@
91
91
  #define SIMSIMD_LOG(x) (logf(x))
92
92
  #endif
93
93
 
94
+ #ifndef SIMSIMD_F32_DIVISION_EPSILON
95
+ #define SIMSIMD_F32_DIVISION_EPSILON 1e-7
96
+ #endif
97
+
98
+ #ifndef SIMSIMD_F16_DIVISION_EPSILON
99
+ #define SIMSIMD_F16_DIVISION_EPSILON 1e-3
100
+ #endif
101
+
94
102
  #ifdef __cplusplus
95
103
  extern "C" {
96
104
  #endif
@@ -4,12 +4,6 @@ import assert from 'node:assert';
4
4
 
5
5
  const simsimd = bindings('simsimd');
6
6
 
7
- const f32Array1 = new Float32Array([1.0, 2.0, 3.0]);
8
- const f32Array2 = new Float32Array([4.0, 5.0, 6.0]);
9
-
10
- const u8Array1 = new Uint8Array([1, 2, 3]);
11
- const u8Array2 = new Uint8Array([4, 5, 6]);
12
-
13
7
  function assertAlmostEqual(actual, expected, tolerance = 1e-6) {
14
8
  const lowerBound = expected - tolerance;
15
9
  const upperBound = expected + tolerance;
@@ -17,20 +11,26 @@ function assertAlmostEqual(actual, expected, tolerance = 1e-6) {
17
11
  }
18
12
 
19
13
  test('Distance from itself', () => {
20
- assertAlmostEqual(simsimd.sqeuclidean(f32Array1, f32Array1), 0.0, 0.01);
21
- assertAlmostEqual(simsimd.cosine(f32Array1, f32Array1), 0.0, 0.01);
22
14
 
23
- // Inner-product distance on non-nroamalized vectors would yield:
24
- // 1 - 1 - 4 - 9 = -13
25
- assertAlmostEqual(simsimd.inner(f32Array1, f32Array1), -13.0, 0.01);
15
+ const f32s = new Float32Array([1.0, 2.0, 3.0]);
16
+ assertAlmostEqual(simsimd.sqeuclidean(f32s, f32s), 0.0, 0.01);
17
+ assertAlmostEqual(simsimd.cosine(f32s, f32s), 0.0, 0.01);
26
18
 
27
- assertAlmostEqual(simsimd.kullbackleibler(f32Array1, f32Array1), 0.0, 0.01);
28
- assertAlmostEqual(simsimd.jensenshannon(f32Array1, f32Array1), 0.0, 0.01);
19
+ const f32sNormalized = new Float32Array([1.0 / Math.sqrt(14), 2.0 / Math.sqrt(14), 3.0 / Math.sqrt(14)]);
20
+ assertAlmostEqual(simsimd.inner(f32sNormalized, f32sNormalized), 0.0, 0.01);
29
21
 
30
- assertAlmostEqual(simsimd.hamming(u8Array1, u8Array1), 0.0, 0.01);
31
- assertAlmostEqual(simsimd.jaccard(u8Array1, u8Array1), 0.0, 0.01);
22
+ const f32sDistribution = new Float32Array([1.0 / 6, 2.0 / 6, 3.0 / 6]);
23
+ assertAlmostEqual(simsimd.kullbackleibler(f32sDistribution, f32sDistribution), 0.0, 0.01);
24
+ assertAlmostEqual(simsimd.jensenshannon(f32sDistribution, f32sDistribution), 0.0, 0.01);
25
+
26
+ const u8s = new Uint8Array([1, 2, 3]);
27
+ assertAlmostEqual(simsimd.hamming(u8s, u8s), 0.0, 0.01);
28
+ assertAlmostEqual(simsimd.jaccard(u8s, u8s), 0.0, 0.01);
32
29
  });
33
30
 
31
+ const f32Array1 = new Float32Array([1.0, 2.0, 3.0]);
32
+ const f32Array2 = new Float32Array([4.0, 5.0, 6.0]);
33
+
34
34
  test('Squared Euclidean Distance', () => {
35
35
  const result = simsimd.sqeuclidean(f32Array1, f32Array2);
36
36
  assertAlmostEqual(result, 27.0, 0.01);