|
| 1 | +/** |
| 2 | + * sqlite-vec-ivf-kmeans.c — Pure k-means clustering algorithm. |
| 3 | + * |
| 4 | + * No SQLite dependency. Operates on float arrays in memory. |
| 5 | + * #include'd into sqlite-vec.c after struct definitions. |
| 6 | + */ |
| 7 | + |
| 8 | +#ifndef SQLITE_VEC_IVF_KMEANS_C |
| 9 | +#define SQLITE_VEC_IVF_KMEANS_C |
| 10 | + |
| 11 | +// When opened standalone in an editor, pull in types so the LSP is happy. |
| 12 | +// When #include'd from sqlite-vec.c, SQLITE_VEC_H is already defined. |
| 13 | +#ifndef SQLITE_VEC_H |
| 14 | +#include "sqlite-vec.c" // IWYU pragma: keep |
| 15 | +#endif |
| 16 | + |
| 17 | +#include <float.h> |
| 18 | +#include <string.h> |
| 19 | + |
| 20 | +#define VEC0_IVF_KMEANS_MAX_ITER 25 |
| 21 | +#define VEC0_IVF_KMEANS_DEFAULT_SEED 0 |
| 22 | + |
| 23 | +// Simple xorshift32 PRNG |
| 24 | +static uint32_t ivf_xorshift32(uint32_t *state) { |
| 25 | + uint32_t x = *state; |
| 26 | + x ^= x << 13; |
| 27 | + x ^= x >> 17; |
| 28 | + x ^= x << 5; |
| 29 | + *state = x; |
| 30 | + return x; |
| 31 | +} |
| 32 | + |
| 33 | +// L2 squared distance between two float vectors |
| 34 | +static float ivf_l2_dist(const float *a, const float *b, int D) { |
| 35 | + float sum = 0.0f; |
| 36 | + for (int d = 0; d < D; d++) { |
| 37 | + float diff = a[d] - b[d]; |
| 38 | + sum += diff * diff; |
| 39 | + } |
| 40 | + return sum; |
| 41 | +} |
| 42 | + |
| 43 | +// Find nearest centroid for a single vector. Returns centroid index. |
| 44 | +static int ivf_nearest_centroid(const float *vec, const float *centroids, |
| 45 | + int D, int k) { |
| 46 | + float min_dist = FLT_MAX; |
| 47 | + int best = 0; |
| 48 | + for (int c = 0; c < k; c++) { |
| 49 | + float dist = ivf_l2_dist(vec, ¢roids[c * D], D); |
| 50 | + if (dist < min_dist) { |
| 51 | + min_dist = dist; |
| 52 | + best = c; |
| 53 | + } |
| 54 | + } |
| 55 | + return best; |
| 56 | +} |
| 57 | + |
| 58 | +/** |
| 59 | + * K-means++ initialization. |
| 60 | + * Picks k initial centroids from the data with probability proportional |
| 61 | + * to squared distance from nearest existing centroid. |
| 62 | + */ |
| 63 | +static int ivf_kmeans_init_plusplus(const float *vectors, int N, int D, |
| 64 | + int k, uint32_t seed, float *centroids) { |
| 65 | + if (N <= 0 || k <= 0 || D <= 0) |
| 66 | + return -1; |
| 67 | + if (seed == 0) |
| 68 | + seed = 42; |
| 69 | + |
| 70 | + // Pick first centroid randomly |
| 71 | + int first = ivf_xorshift32(&seed) % N; |
| 72 | + memcpy(centroids, &vectors[first * D], D * sizeof(float)); |
| 73 | + |
| 74 | + if (k == 1) |
| 75 | + return 0; |
| 76 | + |
| 77 | + // Allocate distance array |
| 78 | + float *dists = sqlite3_malloc64((i64)N * sizeof(float)); |
| 79 | + if (!dists) |
| 80 | + return -1; |
| 81 | + |
| 82 | + for (int c = 1; c < k; c++) { |
| 83 | + // Compute D(x) = distance to nearest existing centroid |
| 84 | + double total = 0.0; |
| 85 | + for (int i = 0; i < N; i++) { |
| 86 | + float d = ivf_l2_dist(&vectors[i * D], ¢roids[(c - 1) * D], D); |
| 87 | + if (c == 1 || d < dists[i]) { |
| 88 | + dists[i] = d; |
| 89 | + } |
| 90 | + total += dists[i]; |
| 91 | + } |
| 92 | + |
| 93 | + // Weighted random selection |
| 94 | + if (total <= 0.0) { |
| 95 | + // All distances zero — pick randomly |
| 96 | + int pick = ivf_xorshift32(&seed) % N; |
| 97 | + memcpy(¢roids[c * D], &vectors[pick * D], D * sizeof(float)); |
| 98 | + } else { |
| 99 | + double threshold = ((double)ivf_xorshift32(&seed) / (double)0xFFFFFFFF) * total; |
| 100 | + double cumulative = 0.0; |
| 101 | + int pick = N - 1; |
| 102 | + for (int i = 0; i < N; i++) { |
| 103 | + cumulative += dists[i]; |
| 104 | + if (cumulative >= threshold) { |
| 105 | + pick = i; |
| 106 | + break; |
| 107 | + } |
| 108 | + } |
| 109 | + memcpy(¢roids[c * D], &vectors[pick * D], D * sizeof(float)); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + sqlite3_free(dists); |
| 114 | + return 0; |
| 115 | +} |
| 116 | + |
| 117 | +/** |
| 118 | + * Lloyd's k-means algorithm. |
| 119 | + * |
| 120 | + * @param vectors N*D float array (row-major) |
| 121 | + * @param N number of vectors |
| 122 | + * @param D dimensionality |
| 123 | + * @param k number of clusters |
| 124 | + * @param max_iter maximum iterations |
| 125 | + * @param seed PRNG seed for initialization |
| 126 | + * @param out_centroids output: k*D float array (caller-allocated) |
| 127 | + * @return 0 on success, -1 on error |
| 128 | + */ |
| 129 | +static int ivf_kmeans(const float *vectors, int N, int D, int k, |
| 130 | + int max_iter, uint32_t seed, float *out_centroids) { |
| 131 | + if (N <= 0 || D <= 0 || k <= 0) |
| 132 | + return -1; |
| 133 | + |
| 134 | + // Clamp k to N |
| 135 | + if (k > N) |
| 136 | + k = N; |
| 137 | + |
| 138 | + // Allocate working memory |
| 139 | + int *assignments = sqlite3_malloc64((i64)N * sizeof(int)); |
| 140 | + float *new_centroids = sqlite3_malloc64((i64)k * D * sizeof(float)); |
| 141 | + int *counts = sqlite3_malloc64((i64)k * sizeof(int)); |
| 142 | + |
| 143 | + if (!assignments || !new_centroids || !counts) { |
| 144 | + sqlite3_free(assignments); |
| 145 | + sqlite3_free(new_centroids); |
| 146 | + sqlite3_free(counts); |
| 147 | + return -1; |
| 148 | + } |
| 149 | + |
| 150 | + memset(assignments, -1, N * sizeof(int)); |
| 151 | + |
| 152 | + // Initialize centroids via k-means++ |
| 153 | + if (ivf_kmeans_init_plusplus(vectors, N, D, k, seed, out_centroids) != 0) { |
| 154 | + sqlite3_free(assignments); |
| 155 | + sqlite3_free(new_centroids); |
| 156 | + sqlite3_free(counts); |
| 157 | + return -1; |
| 158 | + } |
| 159 | + |
| 160 | + for (int iter = 0; iter < max_iter; iter++) { |
| 161 | + // Assignment step |
| 162 | + int changed = 0; |
| 163 | + for (int i = 0; i < N; i++) { |
| 164 | + int nearest = ivf_nearest_centroid(&vectors[i * D], out_centroids, D, k); |
| 165 | + if (nearest != assignments[i]) { |
| 166 | + assignments[i] = nearest; |
| 167 | + changed++; |
| 168 | + } |
| 169 | + } |
| 170 | + if (changed == 0) |
| 171 | + break; |
| 172 | + |
| 173 | + // Update step |
| 174 | + memset(new_centroids, 0, (size_t)k * D * sizeof(float)); |
| 175 | + memset(counts, 0, k * sizeof(int)); |
| 176 | + |
| 177 | + for (int i = 0; i < N; i++) { |
| 178 | + int c = assignments[i]; |
| 179 | + counts[c]++; |
| 180 | + for (int d = 0; d < D; d++) { |
| 181 | + new_centroids[c * D + d] += vectors[i * D + d]; |
| 182 | + } |
| 183 | + } |
| 184 | + |
| 185 | + for (int c = 0; c < k; c++) { |
| 186 | + if (counts[c] == 0) { |
| 187 | + // Empty cluster: reassign to farthest point from its nearest centroid |
| 188 | + float max_dist = -1.0f; |
| 189 | + int farthest = 0; |
| 190 | + for (int i = 0; i < N; i++) { |
| 191 | + float d = ivf_l2_dist(&vectors[i * D], |
| 192 | + &out_centroids[assignments[i] * D], D); |
| 193 | + if (d > max_dist) { |
| 194 | + max_dist = d; |
| 195 | + farthest = i; |
| 196 | + } |
| 197 | + } |
| 198 | + memcpy(&out_centroids[c * D], &vectors[farthest * D], |
| 199 | + D * sizeof(float)); |
| 200 | + } else { |
| 201 | + for (int d = 0; d < D; d++) { |
| 202 | + out_centroids[c * D + d] = new_centroids[c * D + d] / counts[c]; |
| 203 | + } |
| 204 | + } |
| 205 | + } |
| 206 | + } |
| 207 | + |
| 208 | + sqlite3_free(assignments); |
| 209 | + sqlite3_free(new_centroids); |
| 210 | + sqlite3_free(counts); |
| 211 | + return 0; |
| 212 | +} |
| 213 | + |
| 214 | +#endif /* SQLITE_VEC_IVF_KMEANS_C */ |
0 commit comments