deglib 0.3.2__tar.gz → 0.3.3__tar.gz

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 (139) hide show
  1. {deglib-0.3.2/src/deglib.egg-info → deglib-0.3.3}/PKG-INFO +1 -1
  2. {deglib-0.3.2 → deglib-0.3.3}/lib/bench/include/build.h +1 -1
  3. deglib-0.3.3/lib/deglib/include/deglib/concurrent.h +246 -0
  4. {deglib-0.3.2 → deglib-0.3.3}/lib/deglib/include/deglib/distance/int4.h +1 -1
  5. deglib-0.3.3/lib/deglib/include/deglib/distance/int4_ip.h +289 -0
  6. {deglib-0.3.2 → deglib-0.3.3}/lib/deglib/include/deglib/distance/int8.h +1 -1
  7. {deglib-0.3.2 → deglib-0.3.3}/lib/deglib/include/deglib/distance/uint4_ip.h +68 -113
  8. deglib-0.3.3/lib/deglib/include/deglib/distance/uint4_l1.h +236 -0
  9. deglib-0.3.3/lib/deglib/include/deglib/distance/uint4_l2.h +296 -0
  10. deglib-0.3.3/lib/deglib/include/deglib/distance/uint8_l1.h +212 -0
  11. {deglib-0.3.2 → deglib-0.3.3}/lib/deglib/include/deglib/distance/uint8_l2.h +29 -40
  12. {deglib-0.3.2 → deglib-0.3.3}/lib/deglib/include/deglib/distances.h +13 -11
  13. {deglib-0.3.2 → deglib-0.3.3}/lib/deglib/include/deglib/graph/dynamic_graph.h +1 -0
  14. {deglib-0.3.2 → deglib-0.3.3}/lib/deglib/include/deglib/graph/internal_graph.h +76 -57
  15. {deglib-0.3.2 → deglib-0.3.3}/lib/deglib/include/deglib/graph/readonly_graph.h +1 -2
  16. {deglib-0.3.2 → deglib-0.3.3}/lib/deglib/include/deglib/graph/sizebounded_graph.h +1 -0
  17. {deglib-0.3.2 → deglib-0.3.3}/lib/deglib/include/deglib/graph/visited_list_pool.h +2 -0
  18. {deglib-0.3.2 → deglib-0.3.3}/lib/deglib/include/deglib/optimization.h +4 -2
  19. deglib-0.3.3/lib/deglib/include/deglib/search/entry_selector.h +1054 -0
  20. {deglib-0.3.2 → deglib-0.3.3}/lib/deglib/include/deglib/search/searcher.h +7 -7
  21. {deglib-0.3.2 → deglib-0.3.3}/lib/deglib/include/deglib/search.h +2 -2
  22. {deglib-0.3.2 → deglib-0.3.3}/lib/deglib/include/deglib/transformation/mips.h +98 -20
  23. {deglib-0.3.2 → deglib-0.3.3}/lib/deglib/include/deglib/transformation/scalar/int4.h +4 -3
  24. {deglib-0.3.2 → deglib-0.3.3}/lib/deglib/include/deglib/transformation/scalar/int8.h +4 -3
  25. {deglib-0.3.2 → deglib-0.3.3}/lib/deglib/include/deglib/transformation/scalar/uint4.h +1 -1
  26. {deglib-0.3.2 → deglib-0.3.3}/lib/deglib/include/deglib/utils/cpu.h +115 -0
  27. {deglib-0.3.2 → deglib-0.3.3}/lib/test/CMakeLists.txt +1 -2
  28. {deglib-0.3.2 → deglib-0.3.3}/lib/test/src/common/test_helpers.h +0 -8
  29. {deglib-0.3.2 → deglib-0.3.3}/src/deg_cpp/bindings_distances.cpp +2 -2
  30. {deglib-0.3.2 → deglib-0.3.3}/src/deg_cpp/bindings_graph.cpp +4 -3
  31. {deglib-0.3.2 → deglib-0.3.3}/src/deg_cpp/bindings_optimization.cpp +1 -1
  32. {deglib-0.3.2 → deglib-0.3.3}/src/deg_cpp/bindings_search.cpp +2 -2
  33. {deglib-0.3.2 → deglib-0.3.3}/src/deg_cpp/bindings_transformation.cpp +15 -3
  34. {deglib-0.3.2 → deglib-0.3.3}/src/deglib/__init__.py +1 -1
  35. {deglib-0.3.2 → deglib-0.3.3}/src/deglib/builder.py +43 -21
  36. {deglib-0.3.2 → deglib-0.3.3}/src/deglib/distances.py +9 -7
  37. {deglib-0.3.2 → deglib-0.3.3}/src/deglib/graph.py +30 -8
  38. {deglib-0.3.2 → deglib-0.3.3}/src/deglib/optimization.py +3 -0
  39. {deglib-0.3.2 → deglib-0.3.3}/src/deglib/search.py +35 -26
  40. {deglib-0.3.2 → deglib-0.3.3}/src/deglib/transformation.py +6 -4
  41. {deglib-0.3.2 → deglib-0.3.3/src/deglib.egg-info}/PKG-INFO +1 -1
  42. {deglib-0.3.2 → deglib-0.3.3}/src/deglib.egg-info/SOURCES.txt +3 -3
  43. {deglib-0.3.2 → deglib-0.3.3}/tests/test_builder.py +7 -7
  44. {deglib-0.3.2 → deglib-0.3.3}/tests/test_deglib.py +9 -6
  45. {deglib-0.3.2 → deglib-0.3.3}/tests/test_distances.py +28 -0
  46. {deglib-0.3.2 → deglib-0.3.3}/tests/test_graph.py +12 -16
  47. {deglib-0.3.2 → deglib-0.3.3}/tests/test_quantization_matrix.py +64 -11
  48. {deglib-0.3.2 → deglib-0.3.3}/tests/test_searcher.py +12 -14
  49. {deglib-0.3.2 → deglib-0.3.3}/tests/test_transformation_graph.py +14 -14
  50. deglib-0.3.2/lib/deglib/include/deglib/concurrent.h +0 -158
  51. deglib-0.3.2/lib/deglib/include/deglib/distance/int4_ip.h +0 -198
  52. deglib-0.3.2/lib/deglib/include/deglib/distance/int4_l2.h +0 -411
  53. deglib-0.3.2/lib/deglib/include/deglib/distance/int8_l2.h +0 -360
  54. deglib-0.3.2/lib/deglib/include/deglib/distance/uint4_l2.h +0 -353
  55. deglib-0.3.2/lib/deglib/include/deglib/search/kmeans.h +0 -471
  56. {deglib-0.3.2 → deglib-0.3.3}/CMakeLists.txt +0 -0
  57. {deglib-0.3.2 → deglib-0.3.3}/LICENSE +0 -0
  58. {deglib-0.3.2 → deglib-0.3.3}/MANIFEST.in +0 -0
  59. {deglib-0.3.2 → deglib-0.3.3}/README.md +0 -0
  60. {deglib-0.3.2 → deglib-0.3.3}/lib/CMakeLists.txt +0 -0
  61. {deglib-0.3.2 → deglib-0.3.3}/lib/bench/CMakeLists.txt +0 -0
  62. {deglib-0.3.2 → deglib-0.3.3}/lib/bench/include/benchmark.h +0 -0
  63. {deglib-0.3.2 → deglib-0.3.3}/lib/bench/include/dataset.h +0 -0
  64. {deglib-0.3.2 → deglib-0.3.3}/lib/bench/include/file_io.h +0 -0
  65. {deglib-0.3.2 → deglib-0.3.3}/lib/bench/include/logging.h +0 -0
  66. {deglib-0.3.2 → deglib-0.3.3}/lib/bench/include/repository.h +0 -0
  67. {deglib-0.3.2 → deglib-0.3.3}/lib/bench/include/stopwatch.h +0 -0
  68. {deglib-0.3.2 → deglib-0.3.3}/lib/cmake_modules/DetectCPUFeatures.cmake +0 -0
  69. {deglib-0.3.2 → deglib-0.3.3}/lib/deglib/CMakeLists.txt +0 -0
  70. {deglib-0.3.2 → deglib-0.3.3}/lib/deglib/include/deglib/analysis.h +0 -0
  71. {deglib-0.3.2 → deglib-0.3.3}/lib/deglib/include/deglib/builder.h +0 -0
  72. {deglib-0.3.2 → deglib-0.3.3}/lib/deglib/include/deglib/config.h +0 -0
  73. {deglib-0.3.2 → deglib-0.3.3}/lib/deglib/include/deglib/deglib.h +0 -0
  74. {deglib-0.3.2 → deglib-0.3.3}/lib/deglib/include/deglib/distance/evp_ip.h +0 -0
  75. {deglib-0.3.2 → deglib-0.3.3}/lib/deglib/include/deglib/distance/fp16.h +0 -0
  76. {deglib-0.3.2 → deglib-0.3.3}/lib/deglib/include/deglib/distance/fp16_ip.h +0 -0
  77. {deglib-0.3.2 → deglib-0.3.3}/lib/deglib/include/deglib/distance/fp16_l2.h +0 -0
  78. {deglib-0.3.2 → deglib-0.3.3}/lib/deglib/include/deglib/distance/fp32.h +0 -0
  79. {deglib-0.3.2 → deglib-0.3.3}/lib/deglib/include/deglib/distance/fp32_ip.h +0 -0
  80. {deglib-0.3.2 → deglib-0.3.3}/lib/deglib/include/deglib/distance/fp32_l2.h +0 -0
  81. {deglib-0.3.2 → deglib-0.3.3}/lib/deglib/include/deglib/distance/int8_ip.h +0 -0
  82. {deglib-0.3.2 → deglib-0.3.3}/lib/deglib/include/deglib/distance/residual_mode.h +0 -0
  83. {deglib-0.3.2 → deglib-0.3.3}/lib/deglib/include/deglib/distance/tail_mask.h +0 -0
  84. {deglib-0.3.2 → deglib-0.3.3}/lib/deglib/include/deglib/distance/uint4.h +0 -0
  85. {deglib-0.3.2 → deglib-0.3.3}/lib/deglib/include/deglib/distance/uint7_ip.h +0 -0
  86. {deglib-0.3.2 → deglib-0.3.3}/lib/deglib/include/deglib/distance/uint7_l2.h +0 -0
  87. {deglib-0.3.2 → deglib-0.3.3}/lib/deglib/include/deglib/distance/uint8.h +0 -0
  88. {deglib-0.3.2 → deglib-0.3.3}/lib/deglib/include/deglib/distance/uint8_ip.h +0 -0
  89. {deglib-0.3.2 → deglib-0.3.3}/lib/deglib/include/deglib/filter.h +0 -0
  90. {deglib-0.3.2 → deglib-0.3.3}/lib/deglib/include/deglib/graph/intrusive_list.h +0 -0
  91. {deglib-0.3.2 → deglib-0.3.3}/lib/deglib/include/deglib/graph/mutable_graph.h +0 -0
  92. {deglib-0.3.2 → deglib-0.3.3}/lib/deglib/include/deglib/graph.h +0 -0
  93. {deglib-0.3.2 → deglib-0.3.3}/lib/deglib/include/deglib/optimization/flas/fast_linear_assignment_sorter.h +0 -0
  94. {deglib-0.3.2 → deglib-0.3.3}/lib/deglib/include/deglib/optimization/flas/fast_linear_assignment_sorter_mt.h +0 -0
  95. {deglib-0.3.2 → deglib-0.3.3}/lib/deglib/include/deglib/optimization/flas/junker_volgenant_solver.h +0 -0
  96. {deglib-0.3.2 → deglib-0.3.3}/lib/deglib/include/deglib/optimization/pruning.h +0 -0
  97. {deglib-0.3.2 → deglib-0.3.3}/lib/deglib/include/deglib/search/pareto_line.h +0 -0
  98. {deglib-0.3.2 → deglib-0.3.3}/lib/deglib/include/deglib/search/recall_table.h +0 -0
  99. {deglib-0.3.2 → deglib-0.3.3}/lib/deglib/include/deglib/search/reranker.h +0 -0
  100. {deglib-0.3.2 → deglib-0.3.3}/lib/deglib/include/deglib/search/result_list.h +0 -0
  101. {deglib-0.3.2 → deglib-0.3.3}/lib/deglib/include/deglib/serialization.h +0 -0
  102. {deglib-0.3.2 → deglib-0.3.3}/lib/deglib/include/deglib/transformation/evp.h +0 -0
  103. {deglib-0.3.2 → deglib-0.3.3}/lib/deglib/include/deglib/transformation/normalize.h +0 -0
  104. {deglib-0.3.2 → deglib-0.3.3}/lib/deglib/include/deglib/transformation/rotation.h +0 -0
  105. {deglib-0.3.2 → deglib-0.3.3}/lib/deglib/include/deglib/transformation/scalar/common.h +0 -0
  106. {deglib-0.3.2 → deglib-0.3.3}/lib/deglib/include/deglib/transformation/scalar/fp16.h +0 -0
  107. {deglib-0.3.2 → deglib-0.3.3}/lib/deglib/include/deglib/transformation/scalar/uint7.h +0 -0
  108. {deglib-0.3.2 → deglib-0.3.3}/lib/deglib/include/deglib/transformation/scalar/uint8.h +0 -0
  109. {deglib-0.3.2 → deglib-0.3.3}/lib/deglib/include/deglib/transformation.h +0 -0
  110. {deglib-0.3.2 → deglib-0.3.3}/lib/deglib/include/deglib/tuning.h +0 -0
  111. {deglib-0.3.2 → deglib-0.3.3}/lib/deglib/include/deglib/utils/memory.h +0 -0
  112. {deglib-0.3.2 → deglib-0.3.3}/lib/deglib/include/deglib/utils/random.h +0 -0
  113. {deglib-0.3.2 → deglib-0.3.3}/lib/test/src/unit/graph/test_dynamic_graph_common.h +0 -0
  114. {deglib-0.3.2 → deglib-0.3.3}/pyproject.toml +0 -0
  115. {deglib-0.3.2 → deglib-0.3.3}/setup.cfg +0 -0
  116. {deglib-0.3.2 → deglib-0.3.3}/setup.py +0 -0
  117. {deglib-0.3.2 → deglib-0.3.3}/src/deg_cpp/bindings_tuning.cpp +0 -0
  118. {deglib-0.3.2 → deglib-0.3.3}/src/deg_cpp/deglib_cpp.cpp +0 -0
  119. {deglib-0.3.2 → deglib-0.3.3}/src/deglib/analysis.py +0 -0
  120. {deglib-0.3.2 → deglib-0.3.3}/src/deglib/cpu.py +0 -0
  121. {deglib-0.3.2 → deglib-0.3.3}/src/deglib/tuning.py +0 -0
  122. {deglib-0.3.2 → deglib-0.3.3}/src/deglib/utils.py +0 -0
  123. {deglib-0.3.2 → deglib-0.3.3}/src/deglib.egg-info/dependency_links.txt +0 -0
  124. {deglib-0.3.2 → deglib-0.3.3}/src/deglib.egg-info/requires.txt +0 -0
  125. {deglib-0.3.2 → deglib-0.3.3}/src/deglib.egg-info/top_level.txt +0 -0
  126. {deglib-0.3.2 → deglib-0.3.3}/tests/test_analysis.py +0 -0
  127. {deglib-0.3.2 → deglib-0.3.3}/tests/test_cpu.py +0 -0
  128. {deglib-0.3.2 → deglib-0.3.3}/tests/test_evp_quantizer.py +0 -0
  129. {deglib-0.3.2 → deglib-0.3.3}/tests/test_filter.py +0 -0
  130. {deglib-0.3.2 → deglib-0.3.3}/tests/test_l2_normalize.py +0 -0
  131. {deglib-0.3.2 → deglib-0.3.3}/tests/test_pareto_line.py +0 -0
  132. {deglib-0.3.2 → deglib-0.3.3}/tests/test_presort.py +0 -0
  133. {deglib-0.3.2 → deglib-0.3.3}/tests/test_recall_table.py +0 -0
  134. {deglib-0.3.2 → deglib-0.3.3}/tests/test_rerank.py +0 -0
  135. {deglib-0.3.2 → deglib-0.3.3}/tests/test_rotation_transform.py +0 -0
  136. {deglib-0.3.2 → deglib-0.3.3}/tests/test_scalar_quantizer.py +0 -0
  137. {deglib-0.3.2 → deglib-0.3.3}/tests/test_scalar_quantizer_int4.py +0 -0
  138. {deglib-0.3.2 → deglib-0.3.3}/tests/test_transformation.py +0 -0
  139. {deglib-0.3.2 → deglib-0.3.3}/tests/test_tuning.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: deglib
3
- Version: 0.3.2
3
+ Version: 0.3.3
4
4
  Summary: Python bindings for the Dynamic Exploration Graph library by Nico Hezel
5
5
  Author: Nico Hezel
6
6
  Author-email: Bruno Schilling <bruno.schilling@htw-berlin.de>
@@ -80,7 +80,7 @@ inline void create_graph(
80
80
 
81
81
  auto builder = deglib::builder::EvenRegularGraphBuilder(graph, rnd, lid, k_ext, eps_ext, k_opt, eps_opt, i_opt, improve_tries);
82
82
  builder.setThreadCount(thread_count);
83
- builder.setBatchSize(10, 10);
83
+ builder.setBatchSize(32, 10);
84
84
 
85
85
  auto base_size = uint32_t(repository.size());
86
86
  auto addEntry = [&builder, &repository, feature_byte_size](auto idx) {
@@ -0,0 +1,246 @@
1
+ #pragma once
2
+
3
+ #include "deglib/config.h"
4
+
5
+ #include <algorithm>
6
+ #include <atomic>
7
+ #include <condition_variable>
8
+ #include <functional>
9
+ #include <mutex>
10
+ #include <thread>
11
+ #include <vector>
12
+
13
+ namespace deglib::concurrent {
14
+
15
+ namespace detail {
16
+
17
+ /**
18
+ * Lightweight persistent worker pool backing `parallel_for` and `parallel_batch_for`.
19
+ *
20
+ * Keeps OS threads (and their `thread_local` workspaces) alive across calls so repeated
21
+ * short parallel batches avoid `std::thread` spawn/join and workspace reallocation overhead.
22
+ * Re-entrant or concurrent callers fall back to ad-hoc `std::thread` execution without blocking.
23
+ */
24
+ class PersistentThreadPool {
25
+ public:
26
+ PersistentThreadPool() = default;
27
+ PersistentThreadPool(const PersistentThreadPool&) = delete;
28
+ PersistentThreadPool& operator=(const PersistentThreadPool&) = delete;
29
+
30
+ template <class WorkerFn>
31
+ bool try_run(size_t actual_threads, WorkerFn&& worker) {
32
+ if (busy_.exchange(true, std::memory_order_acquire)) {
33
+ return false;
34
+ }
35
+ struct BusyGuard {
36
+ std::atomic<bool>& flag;
37
+ ~BusyGuard() noexcept { flag.store(false, std::memory_order_release); }
38
+ } guard{busy_};
39
+
40
+ std::atomic<size_t> remaining(actual_threads - 1);
41
+ {
42
+ std::lock_guard<std::mutex> lk(mtx_);
43
+ while (workers_.size() < actual_threads - 1) {
44
+ const size_t tid = workers_.size() + 1;
45
+ workers_.emplace_back([this, tid] { worker_loop(tid); });
46
+ }
47
+ job_fn_ = [&](size_t tid) { worker(tid); };
48
+ active_threads_ = actual_threads;
49
+ remaining_ = &remaining;
50
+ ++generation_;
51
+ }
52
+ work_cv_.notify_all();
53
+
54
+ worker(0); // caller participates as worker 0
55
+
56
+ if (remaining.load(std::memory_order_acquire) > 0) {
57
+ std::unique_lock<std::mutex> lk(mtx_);
58
+ done_cv_.wait(lk, [&] { return remaining.load(std::memory_order_acquire) == 0; });
59
+ }
60
+ return true;
61
+ }
62
+
63
+ private:
64
+ void worker_loop(size_t tid) {
65
+ size_t my_gen = 0;
66
+ while (true) {
67
+ std::function<void(size_t)> fn;
68
+ std::atomic<size_t>* rem_ptr = nullptr;
69
+ {
70
+ std::unique_lock<std::mutex> lk(mtx_);
71
+ work_cv_.wait(lk, [&] { return generation_ != my_gen && tid < active_threads_; });
72
+ my_gen = generation_;
73
+ fn = job_fn_;
74
+ rem_ptr = remaining_;
75
+ }
76
+ fn(tid);
77
+ if (rem_ptr->fetch_sub(1, std::memory_order_acq_rel) == 1) {
78
+ std::lock_guard<std::mutex> lk(mtx_);
79
+ done_cv_.notify_one();
80
+ }
81
+ }
82
+ }
83
+
84
+ std::atomic<bool> busy_{false};
85
+ std::mutex mtx_;
86
+ std::condition_variable work_cv_;
87
+ std::condition_variable done_cv_;
88
+ std::vector<std::thread> workers_;
89
+ std::function<void(size_t)> job_fn_;
90
+ std::atomic<size_t>* remaining_ = nullptr;
91
+ size_t active_threads_ = 1;
92
+ size_t generation_ = 0;
93
+ };
94
+
95
+ inline PersistentThreadPool& default_pool() {
96
+ // Intentional process-lifetime singleton (avoids Windows DLL_PROCESS_DETACH join deadlocks).
97
+ static auto* pool = new PersistentThreadPool();
98
+ return *pool;
99
+ }
100
+
101
+ template <class WorkerFn>
102
+ inline void run_workers(size_t actual_threads, WorkerFn&& worker) {
103
+ if (default_pool().try_run(actual_threads, worker)) {
104
+ return;
105
+ }
106
+ std::vector<std::thread> threads;
107
+ threads.reserve(actual_threads - 1);
108
+ for (size_t thread_id = 1; thread_id < actual_threads; ++thread_id) {
109
+ threads.emplace_back(worker, thread_id);
110
+ }
111
+ worker(0);
112
+ for (auto& thread : threads) {
113
+ thread.join();
114
+ }
115
+ }
116
+
117
+ } // namespace detail
118
+
119
+ // Multithreaded executor
120
+ // The helper function copied from https://github.com/nmslib/hnswlib/blob/master/examples/cpp/example_mt_search.cpp (and that itself is copied from nmslib)
121
+ // An alternative is using #pragme omp parallel for or any other C++ threading
122
+ template <class Function>
123
+ inline void parallel_for(size_t start, size_t end, size_t numThreads, Function fn) {
124
+ if (numThreads <= 0) {
125
+ numThreads = std::thread::hardware_concurrency();
126
+ }
127
+
128
+ const size_t count = end - start;
129
+ if (count == 0) {
130
+ return;
131
+ }
132
+
133
+ const size_t actualThreads = std::min(std::max<size_t>(1, numThreads), count);
134
+ if (actualThreads == 1) {
135
+ for (size_t id = start; id < end; id++) {
136
+ fn(id, 0);
137
+ }
138
+ return;
139
+ }
140
+
141
+ std::atomic<size_t> current(start);
142
+ std::exception_ptr lastException = nullptr;
143
+ std::mutex lastExceptMutex;
144
+
145
+ auto worker = [&](size_t threadId) {
146
+ while (true) {
147
+ const size_t id = current.fetch_add(1, std::memory_order_relaxed);
148
+ if (id >= end) {
149
+ break;
150
+ }
151
+ try {
152
+ fn(id, threadId);
153
+ } catch (...) {
154
+ std::unique_lock<std::mutex> lastExcepLock(lastExceptMutex);
155
+ lastException = std::current_exception();
156
+ current.store(end, std::memory_order_relaxed);
157
+ break;
158
+ }
159
+ }
160
+ };
161
+
162
+ detail::run_workers(actualThreads, worker);
163
+ if (lastException) {
164
+ std::rethrow_exception(lastException);
165
+ }
166
+ }
167
+
168
+ // ---------------------------------------------------------------------------
169
+ // parallel_batch_for: parallel loop over contiguous batches.
170
+ //
171
+ // Splits [start, end) into min(end - start, actualThreads * chunksPerThread)
172
+ // contiguous chunks and invokes
173
+ // fn(begin, end, threadId)
174
+ // once per non-empty chunk, with the half-open range [begin, end) and
175
+ // threadId in [0, actualThreads).
176
+ //
177
+ // Unlike parallel_for (which calls fn once per element and increments a
178
+ // shared atomic counter per element), the counter is only touched once per
179
+ // CHUNK. On fine-grained work (short fn bodies) that removes the cache-line
180
+ // ping-pong between workers, which is what makes element-wise loops with a
181
+ // shared counter scale poorly. When chunksPerThread > 1, chunks are claimed
182
+ // dynamically by the workers, balancing variable-cost workloads and OS
183
+ // preemption while keeping atomic contention low.
184
+ //
185
+ // The caller thread participates as worker 0 (like parallel_for). The range
186
+ // [begin, end) is passed so callers can write `for (size_t i = begin; i < end;
187
+ // ++i) { ... }` or use std::copy_n / std::memcpy on the whole slice.
188
+ // Exceptions thrown by fn are captured and re-thrown on the caller thread.
189
+ // ---------------------------------------------------------------------------
190
+ template <class Function>
191
+ inline void parallel_batch_for(size_t start, size_t end, size_t numThreads, Function fn, size_t chunksPerThread = 1) {
192
+ if (numThreads <= 0) {
193
+ numThreads = std::thread::hardware_concurrency();
194
+ }
195
+
196
+ const size_t count = end - start;
197
+ if (count == 0) {
198
+ return;
199
+ }
200
+
201
+ const size_t actualThreads = std::min(std::max<size_t>(1, numThreads), count);
202
+ if (actualThreads == 1) {
203
+ fn(start, end, 0);
204
+ return;
205
+ }
206
+
207
+ const size_t mult = std::max<size_t>(1, chunksPerThread);
208
+ const size_t numChunks = std::min(count, actualThreads * mult);
209
+
210
+ // Static partition of [0, count) into numChunks contiguous chunks:
211
+ // base = count / numChunks, rem = count % numChunks
212
+ // chunk c = [c*base + min(c, rem), c*base + min(c, rem) + base + (c < rem))
213
+ // The first `rem` chunks have base+1 elements, the rest have base.
214
+ const size_t base = count / numChunks;
215
+ const size_t rem = count % numChunks;
216
+
217
+ std::atomic<size_t> current(0);
218
+ std::exception_ptr lastException = nullptr;
219
+ std::mutex lastExceptMutex;
220
+
221
+ auto worker = [&](size_t threadId) {
222
+ while (true) {
223
+ const size_t c = current.fetch_add(1, std::memory_order_relaxed);
224
+ if (c >= numChunks) {
225
+ break;
226
+ }
227
+ const size_t cbegin = start + c * base + std::min(c, rem);
228
+ const size_t clen = base + (c < rem ? 1 : 0);
229
+ try {
230
+ fn(cbegin, cbegin + clen, threadId);
231
+ } catch (...) {
232
+ std::unique_lock<std::mutex> lastExcepLock(lastExceptMutex);
233
+ lastException = std::current_exception();
234
+ current.store(numChunks, std::memory_order_relaxed);
235
+ break;
236
+ }
237
+ }
238
+ };
239
+
240
+ detail::run_workers(actualThreads, worker);
241
+ if (lastException) {
242
+ std::rethrow_exception(lastException);
243
+ }
244
+ }
245
+
246
+ } // namespace deglib::concurrent
@@ -5,7 +5,7 @@
5
5
  namespace deglib::distances {
6
6
 
7
7
  // Shared INT4 distance utilities and declarations.
8
- // Base header for all INT4 metric modules (int4_l2.h, int4_ip.h).
8
+ // Base header for INT4 metric modules (int4_ip.h).
9
9
 
10
10
  } // end namespace deglib::distances
11
11
 
@@ -0,0 +1,289 @@
1
+ #pragma once
2
+
3
+ #include "deglib/distance/int4.h"
4
+ #include "deglib/distance/residual_mode.h"
5
+ #include "deglib/distance/tail_mask.h"
6
+
7
+ #include <cstdint>
8
+ #include <cstring>
9
+ #include <stdexcept>
10
+ #include <variant>
11
+
12
+ namespace deglib::distances::int4_ip {
13
+
14
+ inline static int8_t sign_extend_nibble(uint8_t n) {
15
+ int8_t s = static_cast<int8_t>(n & 0x0F);
16
+ if (s & 0x08) s |= 0xF0;
17
+ return s;
18
+ }
19
+
20
+ // Scalar fallback — no SIMD required.
21
+ // Packed int4 contract: for odd dimensions, the last byte stores the final element
22
+ // in its low nibble and 0x0 in its high nibble, so summing all num_bytes = (dim + 1) / 2
23
+ // bytes is exact without an odd-dimension tail branch.
24
+ class InnerProductInt4 {
25
+ public:
26
+ static constexpr const char* get_instruction() { return "Scalar"; }
27
+
28
+ inline static float compare(const void* pVect1v, const void* pVect2v, const void* qty_ptr) {
29
+ int64_t result = 0;
30
+ const uint8_t* a = static_cast<const uint8_t*>(pVect1v);
31
+ const uint8_t* b = static_cast<const uint8_t*>(pVect2v);
32
+
33
+ const size_t dim = *((size_t*)qty_ptr);
34
+ const size_t num_bytes = (dim + 1) / 2;
35
+
36
+ for (size_t i = 0; i < num_bytes; ++i) {
37
+ uint8_t byte_a = a[i];
38
+ uint8_t byte_b = b[i];
39
+
40
+ int32_t a0 = sign_extend_nibble(byte_a);
41
+ int32_t a1 = sign_extend_nibble(byte_a >> 4);
42
+ int32_t b0 = sign_extend_nibble(byte_b);
43
+ int32_t b1 = sign_extend_nibble(byte_b >> 4);
44
+
45
+ result += a0 * b0 + a1 * b1;
46
+ }
47
+
48
+ return -static_cast<float>(result);
49
+ }
50
+ };
51
+
52
+ #if defined(DEGLIB_X86)
53
+
54
+ DEGLIB_TARGET_AVX2 inline static int64_t int4_ip_hsum256(__m256i s) {
55
+ __m128i sum128 = _mm_add_epi32(_mm256_castsi256_si128(s), _mm256_extracti128_si256(s, 1));
56
+ sum128 = _mm_add_epi32(sum128, _mm_shuffle_epi32(sum128, _MM_SHUFFLE(1, 0, 3, 2)));
57
+ sum128 = _mm_add_epi32(sum128, _mm_shuffle_epi32(sum128, _MM_SHUFFLE(2, 3, 0, 1)));
58
+ return static_cast<int64_t>(_mm_cvtsi128_si32(sum128));
59
+ }
60
+
61
+ DEGLIB_TARGET_AVX512 inline static int64_t int4_ip_hsum512(__m512i s) {
62
+ return static_cast<int64_t>(_mm512_reduce_add_epi32(s));
63
+ }
64
+
65
+ DEGLIB_TARGET_AVX512 inline static void int4_ip_acc64_avx512(
66
+ __m512i xx, __m512i yy, __m512i mask, __m512i s8, __m512i eights, __m512i& sum16, __m512i& bias16
67
+ ) noexcept {
68
+ const __m512i a_lo_u8 = _mm512_xor_si512(_mm512_and_si512(xx, mask), s8);
69
+ const __m512i a_hi_u8 = _mm512_xor_si512(_mm512_and_si512(_mm512_srli_epi16(xx, 4), mask), s8);
70
+ const __m512i b_lo_s8 = _mm512_sub_epi8(_mm512_xor_si512(_mm512_and_si512(yy, mask), s8), s8);
71
+ const __m512i b_hi_s8 = _mm512_sub_epi8(_mm512_xor_si512(_mm512_and_si512(_mm512_srli_epi16(yy, 4), mask), s8), s8);
72
+
73
+ sum16 = _mm512_add_epi16(sum16, _mm512_maddubs_epi16(a_lo_u8, b_lo_s8));
74
+ sum16 = _mm512_add_epi16(sum16, _mm512_maddubs_epi16(a_hi_u8, b_hi_s8));
75
+ bias16 = _mm512_add_epi16(bias16, _mm512_maddubs_epi16(eights, b_lo_s8));
76
+ bias16 = _mm512_add_epi16(bias16, _mm512_maddubs_epi16(eights, b_hi_s8));
77
+ }
78
+
79
+ DEGLIB_TARGET_AVX2 inline static void int4_ip_acc32_avx2(
80
+ __m256i xx, __m256i yy, __m256i mask, __m256i s8, __m256i eights, __m256i& sum16, __m256i& bias16
81
+ ) noexcept {
82
+ const __m256i a_lo_u8 = _mm256_xor_si256(_mm256_and_si256(xx, mask), s8);
83
+ const __m256i a_hi_u8 = _mm256_xor_si256(_mm256_and_si256(_mm256_srli_epi16(xx, 4), mask), s8);
84
+ const __m256i b_lo_s8 = _mm256_sub_epi8(_mm256_xor_si256(_mm256_and_si256(yy, mask), s8), s8);
85
+ const __m256i b_hi_s8 = _mm256_sub_epi8(_mm256_xor_si256(_mm256_and_si256(_mm256_srli_epi16(yy, 4), mask), s8), s8);
86
+
87
+ sum16 = _mm256_add_epi16(sum16, _mm256_maddubs_epi16(a_lo_u8, b_lo_s8));
88
+ sum16 = _mm256_add_epi16(sum16, _mm256_maddubs_epi16(a_hi_u8, b_hi_s8));
89
+ bias16 = _mm256_add_epi16(bias16, _mm256_maddubs_epi16(eights, b_lo_s8));
90
+ bias16 = _mm256_add_epi16(bias16, _mm256_maddubs_epi16(eights, b_hi_s8));
91
+ }
92
+
93
+ // In AVX2/AVX512: bias 'a' by +8 into unsigned [0, 15] so maddubs_epi16(a+8, b) accumulates in 16-bit,
94
+ // then subtract 8 * sum(b) at chunk boundaries when widening to 32-bit.
95
+ template <ResidualMode Mode = ResidualMode::Full>
96
+ class InnerProductInt4_AVX512 {
97
+ static constexpr bool HasDualSimd = has_flag(Mode, ResidualMode::DualSimd);
98
+ static constexpr bool HasSimd = has_flag(Mode, ResidualMode::Simd);
99
+ static constexpr bool HasTail = has_flag(Mode, ResidualMode::Tail);
100
+
101
+ public:
102
+ static constexpr const char* get_instruction() { return "AVX512"; }
103
+
104
+ DEGLIB_TARGET_AVX512 inline static float compare(const void* pVect1v, const void* pVect2v, const void* qty_ptr) {
105
+ const size_t dim = *((size_t*)qty_ptr);
106
+ const uint8_t* a = static_cast<const uint8_t*>(pVect1v);
107
+ const uint8_t* b = static_cast<const uint8_t*>(pVect2v);
108
+
109
+ const size_t num_bytes = (dim + 1) / 2;
110
+ const uint8_t* last_byte = a + num_bytes;
111
+
112
+ const __m512i mask = _mm512_set1_epi8(0x0F);
113
+ const __m512i s8 = _mm512_set1_epi8(0x08);
114
+ const __m512i eights = _mm512_set1_epi8(8);
115
+ const __m512i ones = _mm512_set1_epi16(1);
116
+ __m512i sum32 = _mm512_setzero_si512();
117
+ __m512i sum16_1 = _mm512_setzero_si512();
118
+ __m512i sum16_2 = _mm512_setzero_si512();
119
+ __m512i bias16 = _mm512_setzero_si512();
120
+
121
+ if constexpr (HasDualSimd) {
122
+ while (a + 127 < last_byte) {
123
+ const uint8_t* chunk_end = std::min(last_byte - 127, a + 36 * 128);
124
+ while (a < chunk_end) {
125
+ int4_ip_acc64_avx512(
126
+ _mm512_loadu_si512(reinterpret_cast<const __m512i*>(a)),
127
+ _mm512_loadu_si512(reinterpret_cast<const __m512i*>(b)),
128
+ mask, s8, eights, sum16_1, bias16
129
+ );
130
+ int4_ip_acc64_avx512(
131
+ _mm512_loadu_si512(reinterpret_cast<const __m512i*>(a + 64)),
132
+ _mm512_loadu_si512(reinterpret_cast<const __m512i*>(b + 64)),
133
+ mask, s8, eights, sum16_2, bias16
134
+ );
135
+ a += 128;
136
+ b += 128;
137
+ }
138
+ if (a + 127 < last_byte) {
139
+ sum32 = _mm512_add_epi32(sum32, _mm512_madd_epi16(sum16_1, ones));
140
+ sum32 = _mm512_add_epi32(sum32, _mm512_madd_epi16(sum16_2, ones));
141
+ sum32 = _mm512_sub_epi32(sum32, _mm512_madd_epi16(bias16, ones));
142
+ sum16_1 = _mm512_setzero_si512();
143
+ sum16_2 = _mm512_setzero_si512();
144
+ bias16 = _mm512_setzero_si512();
145
+ }
146
+ }
147
+ }
148
+
149
+ if constexpr (HasSimd) {
150
+ if (a + 63 < last_byte) {
151
+ int4_ip_acc64_avx512(
152
+ _mm512_loadu_si512(reinterpret_cast<const __m512i*>(a)),
153
+ _mm512_loadu_si512(reinterpret_cast<const __m512i*>(b)),
154
+ mask, s8, eights, sum16_1, bias16
155
+ );
156
+ a += 64;
157
+ b += 64;
158
+ }
159
+ }
160
+
161
+ if constexpr (HasTail) {
162
+ const ptrdiff_t rem = last_byte - a;
163
+ if (rem > 0) {
164
+ const __mmask64 tm = tail_mask::mask64(static_cast<size_t>(rem));
165
+ int4_ip_acc64_avx512(
166
+ _mm512_maskz_loadu_epi8(tm, a),
167
+ _mm512_maskz_loadu_epi8(tm, b),
168
+ mask, s8, eights, sum16_1, bias16
169
+ );
170
+ }
171
+ }
172
+
173
+ sum32 = _mm512_add_epi32(sum32, _mm512_madd_epi16(sum16_1, ones));
174
+ sum32 = _mm512_add_epi32(sum32, _mm512_madd_epi16(sum16_2, ones));
175
+ sum32 = _mm512_sub_epi32(sum32, _mm512_madd_epi16(bias16, ones));
176
+ const int64_t result = int4_ip_hsum512(sum32);
177
+ return -static_cast<float>(result);
178
+ }
179
+ };
180
+
181
+ template <ResidualMode Mode = ResidualMode::Full>
182
+ class InnerProductInt4_AVX2 {
183
+ static constexpr bool HasDualSimd = has_flag(Mode, ResidualMode::DualSimd);
184
+ static constexpr bool HasSimd = has_flag(Mode, ResidualMode::Simd);
185
+ static constexpr bool HasTail = has_flag(Mode, ResidualMode::Tail);
186
+
187
+ public:
188
+ static constexpr const char* get_instruction() { return "AVX2"; }
189
+
190
+ DEGLIB_TARGET_AVX2 inline static float compare(const void* pVect1v, const void* pVect2v, const void* qty_ptr) {
191
+ const size_t dim = *((size_t*)qty_ptr);
192
+ const uint8_t* a = static_cast<const uint8_t*>(pVect1v);
193
+ const uint8_t* b = static_cast<const uint8_t*>(pVect2v);
194
+
195
+ const size_t num_bytes = (dim + 1) / 2;
196
+ const uint8_t* last_byte = a + num_bytes;
197
+
198
+ const __m256i mask = _mm256_set1_epi8(0x0F);
199
+ const __m256i s8 = _mm256_set1_epi8(0x08);
200
+ const __m256i eights = _mm256_set1_epi8(8);
201
+ const __m256i ones = _mm256_set1_epi16(1);
202
+ __m256i sum32 = _mm256_setzero_si256();
203
+ __m256i sum16_1 = _mm256_setzero_si256();
204
+ __m256i sum16_2 = _mm256_setzero_si256();
205
+ __m256i bias16 = _mm256_setzero_si256();
206
+
207
+ if constexpr (HasDualSimd) {
208
+ while (a + 63 < last_byte) {
209
+ const uint8_t* chunk_end = std::min(last_byte - 63, a + 36 * 64);
210
+ while (a < chunk_end) {
211
+ int4_ip_acc32_avx2(
212
+ _mm256_loadu_si256(reinterpret_cast<const __m256i*>(a)),
213
+ _mm256_loadu_si256(reinterpret_cast<const __m256i*>(b)),
214
+ mask, s8, eights, sum16_1, bias16
215
+ );
216
+ int4_ip_acc32_avx2(
217
+ _mm256_loadu_si256(reinterpret_cast<const __m256i*>(a + 32)),
218
+ _mm256_loadu_si256(reinterpret_cast<const __m256i*>(b + 32)),
219
+ mask, s8, eights, sum16_2, bias16
220
+ );
221
+ a += 64;
222
+ b += 64;
223
+ }
224
+ if (a + 63 < last_byte) {
225
+ sum32 = _mm256_add_epi32(sum32, _mm256_madd_epi16(sum16_1, ones));
226
+ sum32 = _mm256_add_epi32(sum32, _mm256_madd_epi16(sum16_2, ones));
227
+ sum32 = _mm256_sub_epi32(sum32, _mm256_madd_epi16(bias16, ones));
228
+ sum16_1 = _mm256_setzero_si256();
229
+ sum16_2 = _mm256_setzero_si256();
230
+ bias16 = _mm256_setzero_si256();
231
+ }
232
+ }
233
+ }
234
+
235
+ if constexpr (HasSimd) {
236
+ if (a + 31 < last_byte) {
237
+ int4_ip_acc32_avx2(
238
+ _mm256_loadu_si256(reinterpret_cast<const __m256i*>(a)),
239
+ _mm256_loadu_si256(reinterpret_cast<const __m256i*>(b)),
240
+ mask, s8, eights, sum16_1, bias16
241
+ );
242
+ a += 32;
243
+ b += 32;
244
+ }
245
+ }
246
+
247
+ if constexpr (HasTail) {
248
+ const ptrdiff_t rem = last_byte - a;
249
+ if (rem > 0) {
250
+ const __m256i ra = tail_mask::load_tail_bytes_32(a, static_cast<size_t>(rem), num_bytes);
251
+ const __m256i rb = tail_mask::load_tail_bytes_32(b, static_cast<size_t>(rem), num_bytes);
252
+ int4_ip_acc32_avx2(ra, rb, mask, s8, eights, sum16_1, bias16);
253
+ }
254
+ }
255
+
256
+ sum32 = _mm256_add_epi32(sum32, _mm256_madd_epi16(sum16_1, ones));
257
+ sum32 = _mm256_add_epi32(sum32, _mm256_madd_epi16(sum16_2, ones));
258
+ sum32 = _mm256_sub_epi32(sum32, _mm256_madd_epi16(bias16, ones));
259
+ const int64_t result = int4_ip_hsum256(sum32);
260
+ return -static_cast<float>(result);
261
+ }
262
+ };
263
+
264
+ #endif // DEGLIB_X86
265
+
266
+ using DistanceVariant = std::variant<
267
+ InnerProductInt4
268
+ #if defined(DEGLIB_X86)
269
+ ,
270
+ InnerProductInt4_AVX512<ResidualMode::Full>,
271
+ InnerProductInt4_AVX2<ResidualMode::Full>
272
+ #endif
273
+ >;
274
+
275
+ inline DistanceVariant select_dist(const size_t dim, const deglib::cpu::InstructionSet instruction = deglib::cpu::InstructionSet::Auto) {
276
+ const auto target = deglib::cpu::resolve_instruction_set(instruction);
277
+
278
+ #if defined(DEGLIB_X86)
279
+ if (target == deglib::cpu::InstructionSet::AVX512 || target == deglib::cpu::InstructionSet::AVX512_VNNI) {
280
+ return InnerProductInt4_AVX512<ResidualMode::Full>{};
281
+ } else if (target == deglib::cpu::InstructionSet::AVX2 || target == deglib::cpu::InstructionSet::AVX2_VNNI) {
282
+ return InnerProductInt4_AVX2<ResidualMode::Full>{};
283
+ }
284
+ #endif
285
+
286
+ return InnerProductInt4{};
287
+ }
288
+
289
+ } // namespace deglib::distances::int4_ip
@@ -5,6 +5,6 @@
5
5
  namespace deglib::distances {
6
6
 
7
7
  // Shared INT8 distance utilities and declarations.
8
- // Base header for all INT8 metric modules (int8_ip.h, int8_l2.h).
8
+ // Base header for INT8 metric modules (int8_ip.h).
9
9
 
10
10
  } // end namespace deglib::distances