-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathsqlite-vector.c
More file actions
2837 lines (2345 loc) · 110 KB
/
sqlite-vector.c
File metadata and controls
2837 lines (2345 loc) · 110 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// sqlite-vector.c
// sqlitevector
//
// Created by Marco Bambini on 16/06/25.
//
#include "fp16/fp16.h"
#include "sqlite-vector.h"
#include "distance-cpu.h"
#include <math.h>
#include <float.h>
#include <stdio.h>
#include <ctype.h>
#include <limits.h>
#include <stdlib.h>
#include <string.h>
#ifndef _WIN32
#include <unistd.h>
#endif
#include <stdint.h>
#include <stdbool.h>
#include <stddef.h>
#if defined(_WIN32) || ((defined(__linux__) && !defined(__GLIBC__) && !defined(__ANDROID__))) || defined(SQLITE_WASM_EXTRA_INIT)
// Provide strcasestr function implementation for environments that lack it:
// - Windows (MinGW, MSVC, etc.)
// - Linux with non-glibc C libraries (musl, uclibc, etc.)
// - WebAssembly builds
char *strcasestr(const char *haystack, const char *needle) {
if (!haystack || !needle) return NULL;
if (!*needle) return (char *)haystack;
for (; *haystack; ++haystack) {
const char *h = haystack;
const char *n = needle;
while (*h && *n && tolower((unsigned char)*h) == tolower((unsigned char)*n)) {
++h;
++n;
}
if (!*n) return (char *)haystack;
}
return NULL;
}
#endif
#ifdef SQLITE_WASM_EXTRA_INIT
#define sqlite3_mutex_alloc(_type) NULL
#define sqlite3_mutex_enter(_mutex)
#define sqlite3_mutex_leave(_mutex)
#define sqlite3_mutex void
#endif
#ifndef SQLITE_CORE
SQLITE_EXTENSION_INIT1
#endif
#define DEBUG_VECTOR_ALWAYS(...) do {printf(__VA_ARGS__ );printf("\n");} while (0)
#if ENABLE_VECTOR_DEBUG
#define DEBUG_VECTOR(...) do {printf(__VA_ARGS__ );printf("\n");} while (0)
#else
#define DEBUG_VECTOR(...)
#endif
#define DEBUG_VECTOR_SERIALIZATION 0
#if DEBUG_VECTOR_SERIALIZATION
#define VECTOR_PRINT(_b,_t,_n) vector_print(_b,_t,_n)
#else
#define VECTOR_PRINT(_b,_t,_n)
#endif
#define SKIP_SPACES(_p) while (*(_p) && isspace((unsigned char)*(_p))) (_p)++
#define TRIM_TRAILING(_start, _len) while ((_len) > 0 && isspace((unsigned char)(_start)[(_len) - 1])) (_len)--
#define DEFAULT_MAX_MEMORY 30*1024*1024
#define MAX_TABLES 128
#define STATIC_SQL_SIZE 2048
#define INT64_TO_INT8PTR(_val, _ptr) do { \
(_ptr)[0] = (int8_t)(((_val) >> 0) & 0xFF); \
(_ptr)[1] = (int8_t)(((_val) >> 8) & 0xFF); \
(_ptr)[2] = (int8_t)(((_val) >> 16) & 0xFF); \
(_ptr)[3] = (int8_t)(((_val) >> 24) & 0xFF); \
(_ptr)[4] = (int8_t)(((_val) >> 32) & 0xFF); \
(_ptr)[5] = (int8_t)(((_val) >> 40) & 0xFF); \
(_ptr)[6] = (int8_t)(((_val) >> 48) & 0xFF); \
(_ptr)[7] = (int8_t)(((_val) >> 56) & 0xFF); \
} while(0)
#define INT64_FROM_INT8PTR(_ptr) \
((int64_t)((uint8_t)(_ptr)[0]) | \
((int64_t)((uint8_t)(_ptr)[1]) << 8) | \
((int64_t)((uint8_t)(_ptr)[2]) << 16) | \
((int64_t)((uint8_t)(_ptr)[3]) << 24) | \
((int64_t)((uint8_t)(_ptr)[4]) << 32) | \
((int64_t)((uint8_t)(_ptr)[5]) << 40) | \
((int64_t)((uint8_t)(_ptr)[6]) << 48) | \
((int64_t)((uint8_t)(_ptr)[7]) << 56))
#define SWAP(_t, a, b) do { _t tmp = (a); (a) = (b); (b) = tmp; } while (0)
#define KEY_MATCH(_k) (key_len == (int)sizeof(_k) - 1 && strncasecmp(key, _k, key_len) == 0)
#define VECTOR_COLUMN_IDX 0
#define VECTOR_COLUMN_VECTOR 1
#define VECTOR_COLUMN_K 2
#define VECTOR_COLUMN_MEMIDX 3
#define VECTOR_COLUMN_ROWID 4
#define VECTOR_COLUMN_DISTANCE 5
#define OPTION_KEY_TYPE "type"
#define OPTION_KEY_DIMENSION "dimension"
#define OPTION_KEY_NORMALIZED "normalized"
#define OPTION_KEY_MAXMEMORY "max_memory"
#define OPTION_KEY_DISTANCE "distance"
#define OPTION_KEY_QUANTTYPE "qtype"
#define OPTION_KEY_QUANTSCALE "qscale" // used only in serialize/unserialize
#define OPTION_KEY_QUANTOFFSET "qoffset" // used only in serialize/unserialize
#define VECTOR_INTERNAL_TABLE "CREATE TABLE IF NOT EXISTS _sqliteai_vector (tblname TEXT, colname TEXT, key TEXT, value ANY, PRIMARY KEY(tblname, colname, key));"
typedef struct {
vector_type v_type; // vector type
int v_dim; // vector dimension
bool v_normalized; // is vector normalized ?
vector_distance v_distance; // vector distance function
vector_qtype q_type; // quantization type
uint64_t max_memory; // max memory
} vector_options;
typedef struct {
char *t_name; // table name
char *c_name; // column name
char *pk_name; // INTEGER primary key name (in case of WITHOUT ROWID tables) or rowid if NULL
vector_options options; // options parsed in key=value arguments
float scale; // computed value by quantization
float offset; // computed value by quantization
bool binary_mean; // binary mean option for 1BIT quantization
void *preloaded;
int precounter;
} table_context;
typedef struct {
table_context tables[MAX_TABLES]; // simple array of MAX_TABLES tables
int table_count; // number of entries in tables array
} vector_context;
typedef struct {
sqlite3_vtab base; // Base class - must be first
sqlite3 *db;
vector_context *ctx;
} vFullScan;
typedef struct {
sqlite3_vtab_cursor base; // Base class - must be first
table_context *table;
// STREAMING VT INTERFACE
bool is_streaming;
bool is_quantized;
struct {
int64_t rowid;
double distance;
distance_function_t distance_fn;
sqlite3_stmt *vm;
void *vector;
int vsize;
int vdim;
void *data;
int dcounter;
int dindex;
int is_eof;
} stream;
// NON-STREAMING VT INTERFACE
int64_t *rowids;
double *distance;
int size;
int max_index;
int row_index;
int row_count;
} vFullScanCursor;
typedef bool (*keyvalue_callback)(sqlite3_context *context, void *xdata, const char *key, int key_len, const char *value, int value_len);
typedef int (*vcursor_run_callback)(sqlite3 *db, vFullScanCursor *c, const void *v1, int v1size);
typedef int (*vcursor_sort_callback)(vFullScanCursor *c);
extern distance_function_t dispatch_distance_table[VECTOR_DISTANCE_MAX][VECTOR_TYPE_MAX];
extern const char *distance_backend_name;
static sqlite3_mutex *qmutex;
// MARK: - SQLite Utils -
bool sqlite_system_exists (sqlite3 *db, const char *name, const char *type) {
DEBUG_VECTOR("system_exists %s: %s", type, name);
sqlite3_stmt *vm = NULL;
bool result = false;
char sql[1024];
snprintf(sql, sizeof(sql), "SELECT EXISTS (SELECT 1 FROM sqlite_master WHERE type='%s' AND name=? COLLATE NOCASE);", type);
int rc = sqlite3_prepare_v2(db, sql, -1, &vm, NULL);
if (rc != SQLITE_OK) goto finalize;
rc = sqlite3_bind_text(vm, 1, name, -1, SQLITE_STATIC);
if (rc != SQLITE_OK) goto finalize;
rc = sqlite3_step(vm);
if (rc == SQLITE_ROW) {
result = (bool)sqlite3_column_int(vm, 0);
rc = SQLITE_OK;
}
finalize:
if (rc != SQLITE_OK) DEBUG_VECTOR_ALWAYS("Error executing %s in system_exists for type %s name %s (%s).", sql, type, name, sqlite3_errmsg(db));
if (vm) sqlite3_finalize(vm);
return result;
}
bool sqlite_table_exists (sqlite3 *db, const char *name) {
return sqlite_system_exists(db, name, "table");
}
bool sqlite_trigger_exists (sqlite3 *db, const char *name) {
return sqlite_system_exists(db, name, "trigger");
}
static bool context_result_error (sqlite3_context *context, int rc, const char *format, ...) {
char buffer[4096];
va_list arg;
va_start (arg, format);
vsnprintf(buffer, sizeof(buffer), format, arg);
va_end (arg);
if (context) {
sqlite3_result_error(context, buffer, -1);
sqlite3_result_error_code(context, rc);
}
return false;
}
static const char *sqlite_type_name (int type) {
switch (type) {
case SQLITE_TEXT: return "TEXT";
case SQLITE_INTEGER: return "INTEGER";
case SQLITE_FLOAT: return "REAL";
case SQLITE_BLOB: return "BLOB";
}
return "N/A";
}
static char *sqlite_strdup (const char *str) {
if (!str) return NULL;
size_t len = strlen(str) + 1;
char *result = (char*)sqlite3_malloc((int)len);
if (result) memcpy(result, str, len);
return result;
}
static void *sqlite_memdup (const void *v, int len) {
if (!v) return NULL;
void *result = (void *)sqlite3_malloc((int)len);
if (result) memcpy(result, v, len);
return result;
}
bool sqlite_column_exists (sqlite3 *db, const char *table_name, const char *column_name) {
char sql[STATIC_SQL_SIZE];
sqlite3_snprintf(sizeof(sql), sql, "SELECT EXISTS(SELECT 1 FROM pragma_table_info('%q') WHERE name = ?1);", table_name);
bool result = false;
sqlite3_stmt *stmt = NULL;
if (sqlite3_prepare_v2(db, sql, -1, &stmt, NULL) == SQLITE_OK) {
sqlite3_bind_text(stmt, 1, column_name, -1, SQLITE_STATIC);
if (sqlite3_step(stmt) == SQLITE_ROW) {
result = (sqlite3_column_int(stmt, 0) != 0);
}
}
sqlite3_finalize(stmt);
return result;
}
bool sqlite_column_is_blob (sqlite3 *db, const char *table_name, const char *column_name) {
char sql[STATIC_SQL_SIZE];
sqlite3_snprintf(sizeof(sql), sql, "SELECT type FROM pragma_table_info('%q') WHERE name=?", table_name);
bool result = false;
sqlite3_stmt *stmt = NULL;
if (sqlite3_prepare_v2(db, sql, -1, &stmt, NULL) == SQLITE_OK) {
sqlite3_bind_text(stmt, 1, column_name, -1, SQLITE_STATIC);
if (sqlite3_step(stmt) == SQLITE_ROW) {
// see https://www.sqlite.org/datatype3.html (Determination Of Column Affinity)
const char *type = (const char *)sqlite3_column_text(stmt, 0);
result = (type == NULL) || strcasestr(type, "BLOB");
}
}
sqlite3_finalize(stmt);
return result;
}
static bool sqlite_table_is_without_rowid (sqlite3 *db, const char *table_name) {
const char *sql = "SELECT sql FROM sqlite_master WHERE type='table' AND name=?";
sqlite3_stmt *stmt = NULL;
bool result = false;
if (sqlite3_prepare_v2(db, sql, -1, &stmt, NULL) == SQLITE_OK) {
sqlite3_bind_text(stmt, 1, table_name, -1, SQLITE_STATIC);
if (sqlite3_step(stmt) == SQLITE_ROW) {
const char *statement = (const char *)sqlite3_column_text(stmt, 0);
result = (statement && strcasestr(statement, "WITHOUT ROWID"));
}
}
sqlite3_finalize(stmt);
return result;
}
static char *sqlite_get_int_prikey_column (sqlite3 *db, const char *table_name) {
char sql[STATIC_SQL_SIZE];
sqlite3_snprintf(sizeof(sql), sql, "SELECT COUNT(*), type, name FROM pragma_table_info('%q') WHERE pk > 0;", table_name);
char *prikey = NULL;
sqlite3_stmt *stmt = NULL;
if (sqlite3_prepare_v2(db, sql, -1, &stmt, NULL) == SQLITE_OK) {
sqlite3_bind_text(stmt, 1, table_name, -1, SQLITE_STATIC);
if (sqlite3_step(stmt) == SQLITE_ROW) {
int count = sqlite3_column_int(stmt, 0);
if (count == 1) {
const char *decl_type = (const char *)sqlite3_column_text(stmt, 1);
// see https://www.sqlite.org/datatype3.html (Determination Of Column Affinity)
if (decl_type && strcasestr(decl_type, "INT")) {
prikey = sqlite_strdup((const char *)sqlite3_column_text(stmt, 2));
}
}
}
}
sqlite3_finalize(stmt);
return prikey;
}
static bool sqlite_sanity_check (sqlite3_context *context, const char *table_name, const char *column_name) {
// sanity check table and column name
sqlite3 *db = sqlite3_context_db_handle(context);
// table_name must exists
if (sqlite_table_exists(db, table_name) == false) {
context_result_error(context, SQLITE_ERROR, "Table '%s' does not exist", table_name);
return false;
}
// column_name must exists
if (sqlite_column_exists(db, table_name, column_name) == false) {
context_result_error(context, SQLITE_ERROR, "Column '%s' does not exist in table '%s'", column_name, table_name);
return false;
}
// column_name must be of type BLOB
if (sqlite_column_is_blob(db, table_name, column_name) == false) {
context_result_error(context, SQLITE_ERROR, "Column '%s' in table '%s' must be of type BLOB", column_name, table_name);
return false;
}
return true;
}
static int sqlite_vtab_set_error (sqlite3_vtab *vtab, const char *format, ...) {
va_list arg;
va_start (arg, format);
char *err = sqlite3_vmprintf(format, arg);
va_end (arg);
vtab->zErrMsg = err;
return SQLITE_ERROR;
}
static sqlite3_int64 sqlite_read_int64 (sqlite3 *db, const char *sql) {
sqlite3_int64 value = 0;
sqlite3_stmt *stmt = NULL;
if (sqlite3_prepare_v2(db, sql, -1, &stmt, NULL) == SQLITE_OK) {
if (sqlite3_step(stmt) == SQLITE_ROW) {
value = sqlite3_column_int64(stmt, 0);
}
}
sqlite3_finalize(stmt);
return value;
}
static void *sqlite_common_set_error (sqlite3_context *context, sqlite3_vtab *vtab, int rc, const char *format, ...) {
char buffer[4096];
char *err = NULL;
va_list arg;
va_start (arg, format);
if (vtab) err = sqlite3_vmprintf(format, arg);
else if (context) vsnprintf(buffer, sizeof(buffer), format, arg);
va_end (arg);
if (vtab) {
vtab->zErrMsg = err;
} else if (context) {
sqlite3_result_error(context, buffer, -1);
sqlite3_result_error_code(context, rc);
}
return NULL;
}
static int sqlite_serialize (sqlite3_context *context, const char *table_name, const char *column_name, int type, const char *key, int64_t ivalue, double fvalue) {
const char *sql = "REPLACE INTO _sqliteai_vector (tblname, colname, key, value) VALUES (?, ?, ?, ?);";
sqlite3 *db = sqlite3_context_db_handle(context);
sqlite3_stmt *vm = NULL;
int rc = sqlite3_prepare_v2(db, sql, -1, &vm, NULL);
if (rc != SQLITE_OK) goto cleanup;
rc = sqlite3_bind_text(vm, 1, table_name, -1, SQLITE_STATIC);
if (rc != SQLITE_OK) goto cleanup;
rc = sqlite3_bind_text(vm, 2, column_name, -1, SQLITE_STATIC);
if (rc != SQLITE_OK) goto cleanup;
rc = sqlite3_bind_text(vm, 3, key, -1, SQLITE_STATIC);
if (rc != SQLITE_OK) goto cleanup;
switch (type) {
case SQLITE_INTEGER: rc = sqlite3_bind_int64(vm, 4, (sqlite3_int64)ivalue); break;
case SQLITE_FLOAT: rc = sqlite3_bind_double(vm, 4, fvalue); break;
}
if (rc != SQLITE_OK) goto cleanup;
rc = sqlite3_step(vm);
if (rc == SQLITE_DONE) rc = SQLITE_OK;
cleanup:
if (rc != SQLITE_OK) sqlite3_result_error(context, sqlite3_errmsg(db), -1);
if (vm) sqlite3_finalize(vm);
return rc;
}
static int sqlite_unserialize (sqlite3_context *context, table_context *ctx) {
const char *sql = "SELECT key, value FROM _sqliteai_vector WHERE tblname = ? AND colname = ?;";
sqlite3 *db = sqlite3_context_db_handle(context);
sqlite3_stmt *vm = NULL;
int rc = sqlite3_prepare_v2(db, sql, -1, &vm, NULL);
if (rc != SQLITE_OK) goto cleanup;
rc = sqlite3_bind_text(vm, 1, ctx->t_name, -1, SQLITE_STATIC);
if (rc != SQLITE_OK) goto cleanup;
rc = sqlite3_bind_text(vm, 2, ctx->c_name, -1, SQLITE_STATIC);
if (rc != SQLITE_OK) goto cleanup;
while (1) {
rc = sqlite3_step(vm);
if (rc == SQLITE_DONE) {rc = SQLITE_OK; break;}
if (rc != SQLITE_ROW) break;
const char *key = (const char *)sqlite3_column_text(vm, 0);
if (strcmp(key, OPTION_KEY_QUANTTYPE) == 0) {
ctx->options.q_type = (vector_qtype)sqlite3_column_int(vm, 1);
continue;
}
if (strcmp(key, OPTION_KEY_QUANTSCALE) == 0) {
ctx->scale = (float)sqlite3_column_double(vm, 1);
continue;
}
if (strcmp(key, OPTION_KEY_QUANTOFFSET) == 0) {
ctx->offset = (float)sqlite3_column_double(vm, 1);
continue;
}
}
cleanup:
//if (rc != SQLITE_OK) sqlite3_result_error(context, sqlite3_errmsg(db), -1);
if (vm) sqlite3_finalize(vm);
return rc;
}
// MARK: - Quantization -
static inline uint8_t q_round_u8 (float s) {
if (!isfinite(s)) {
return (s > 0.0f) ? 255u : 0u; /* NaN -> 0, +Inf -> 255, -Inf -> 0 */
}
float r = s + 0.5f * (1.0f - 2.0f * (s < 0.0f)); /* half away from zero */
if (r >= 255.0f) return 255u;
if (r <= 0.0f) return 0u;
int ir = (int)r; /* safe after the clamp above */
return (uint8_t)ir;
}
static inline int8_t q_round_s8 (float s) {
if (!isfinite(s)) {
return (s > 0.0f) ? 127 : (s < 0.0f ? -128 : 0);
}
/* half-away-from-zero */
float r = s + 0.5f * (1.0f - 2.0f * (s < 0.0f));
if (r >= 127.0f) return 127;
if (r <= -128.0f) return -128;
return (int8_t)(int)r;
}
static inline void quantize_float32_to_unsigned8bit (const float *v, uint8_t *q, float offset, float scale, int n) {
int i = 0;
for (; i + 3 < n; i += 4) {
float s0 = (v[i] - offset) * scale;
float s1 = (v[i + 1] - offset) * scale;
float s2 = (v[i + 2] - offset) * scale;
float s3 = (v[i + 3] - offset) * scale;
int r0 = (int)(s0 + 0.5f * (1.0f - 2.0f * (s0 < 0.0f)));
int r1 = (int)(s1 + 0.5f * (1.0f - 2.0f * (s1 < 0.0f)));
int r2 = (int)(s2 + 0.5f * (1.0f - 2.0f * (s2 < 0.0f)));
int r3 = (int)(s3 + 0.5f * (1.0f - 2.0f * (s3 < 0.0f)));
r0 = r0 > 255 ? 255 : (r0 < 0 ? 0 : r0);
r1 = r1 > 255 ? 255 : (r1 < 0 ? 0 : r1);
r2 = r2 > 255 ? 255 : (r2 < 0 ? 0 : r2);
r3 = r3 > 255 ? 255 : (r3 < 0 ? 0 : r3);
q[i] = (uint8_t)r0;
q[i + 1] = (uint8_t)r1;
q[i + 2] = (uint8_t)r2;
q[i + 3] = (uint8_t)r3;
}
// Handle remaining elements
for (; i < n; ++i) {
float scaled = (v[i] - offset) * scale;
int rounded = (int)(scaled + 0.5f * (1.0f - 2.0f * (scaled < 0.0f)));
rounded = rounded > 255 ? 255 : (rounded < 0 ? 0 : rounded);
q[i] = (uint8_t)rounded;
}
}
static inline void quantize_float16_to_unsigned8bit (const uint16_t *v, uint8_t *q, float offset, float scale, int n) {
int i = 0;
for (; i + 3 < n; i += 4) {
float x0 = float16_to_float32(v[i ]);
float x1 = float16_to_float32(v[i + 1]);
float x2 = float16_to_float32(v[i + 2]);
float x3 = float16_to_float32(v[i + 3]);
q[i ] = q_round_u8((x0 - offset) * scale);
q[i + 1] = q_round_u8((x1 - offset) * scale);
q[i + 2] = q_round_u8((x2 - offset) * scale);
q[i + 3] = q_round_u8((x3 - offset) * scale);
}
for (; i < n; ++i) {
float x = float16_to_float32(v[i]);
q[i] = q_round_u8((x - offset) * scale);
}
}
static inline void quantize_bfloat16_to_unsigned8bit (const uint16_t *v, uint8_t *q, float offset, float scale, int n) {
int i = 0;
for (; i + 3 < n; i += 4) {
float x0 = bfloat16_to_float32(v[i ]);
float x1 = bfloat16_to_float32(v[i + 1]);
float x2 = bfloat16_to_float32(v[i + 2]);
float x3 = bfloat16_to_float32(v[i + 3]);
q[i ] = q_round_u8((x0 - offset) * scale);
q[i + 1] = q_round_u8((x1 - offset) * scale);
q[i + 2] = q_round_u8((x2 - offset) * scale);
q[i + 3] = q_round_u8((x3 - offset) * scale);
}
for (; i < n; ++i) {
float x = bfloat16_to_float32(v[i]);
q[i] = q_round_u8((x - offset) * scale);
}
}
static inline void quantize_u8_to_unsigned8bit (const uint8_t *v, uint8_t *q, float offset, float scale, int n) {
int i = 0;
for (; i + 3 < n; i += 4) {
float x0 = (float)v[i ];
float x1 = (float)v[i + 1];
float x2 = (float)v[i + 2];
float x3 = (float)v[i + 3];
q[i ] = q_round_u8((x0 - offset) * scale);
q[i + 1] = q_round_u8((x1 - offset) * scale);
q[i + 2] = q_round_u8((x2 - offset) * scale);
q[i + 3] = q_round_u8((x3 - offset) * scale);
}
for (; i < n; ++i) {
float x = (float)v[i];
q[i] = q_round_u8((x - offset) * scale);
}
}
static inline void quantize_i8_to_unsigned8bit (const int8_t *v, uint8_t *q, float offset, float scale, int n) {
int i = 0;
for (; i + 3 < n; i += 4) {
float x0 = (float)v[i ];
float x1 = (float)v[i + 1];
float x2 = (float)v[i + 2];
float x3 = (float)v[i + 3];
q[i ] = q_round_u8((x0 - offset) * scale);
q[i + 1] = q_round_u8((x1 - offset) * scale);
q[i + 2] = q_round_u8((x2 - offset) * scale);
q[i + 3] = q_round_u8((x3 - offset) * scale);
}
for (; i < n; ++i) {
float x = (float)v[i];
q[i] = q_round_u8((x - offset) * scale);
}
}
static inline void quantize_float32_to_signed8bit (const float *v, int8_t *q, float offset, float scale, int n) {
int i = 0;
for (; i + 3 < n; i += 4) {
float s0 = (v[i] - offset) * scale;
float s1 = (v[i + 1] - offset) * scale;
float s2 = (v[i + 2] - offset) * scale;
float s3 = (v[i + 3] - offset) * scale;
int r0 = (int)(s0 + 0.5f * (1.0f - 2.0f * (s0 < 0.0f)));
int r1 = (int)(s1 + 0.5f * (1.0f - 2.0f * (s1 < 0.0f)));
int r2 = (int)(s2 + 0.5f * (1.0f - 2.0f * (s2 < 0.0f)));
int r3 = (int)(s3 + 0.5f * (1.0f - 2.0f * (s3 < 0.0f)));
r0 = r0 > 127 ? 127 : (r0 < -128 ? -128 : r0);
r1 = r1 > 127 ? 127 : (r1 < -128 ? -128 : r1);
r2 = r2 > 127 ? 127 : (r2 < -128 ? -128 : r2);
r3 = r3 > 127 ? 127 : (r3 < -128 ? -128 : r3);
q[i] = (int8_t)r0;
q[i + 1] = (int8_t)r1;
q[i + 2] = (int8_t)r2;
q[i + 3] = (int8_t)r3;
}
for (; i < n; ++i) {
float scaled = (v[i] - offset) * scale;
int rounded = (int)(scaled + 0.5f * (1.0f - 2.0f * (scaled < 0.0f)));
rounded = rounded > 127 ? 127 : (rounded < -128 ? -128 : rounded);
q[i] = (int8_t)rounded;
}
}
static inline void quantize_float16_to_signed8bit (const uint16_t *v, int8_t *q, float offset, float scale, int n) {
int i = 0;
for (; i + 3 < n; i += 4) {
float x0 = float16_to_float32(v[i ]);
float x1 = float16_to_float32(v[i + 1]);
float x2 = float16_to_float32(v[i + 2]);
float x3 = float16_to_float32(v[i + 3]);
q[i ] = q_round_s8((x0 - offset) * scale);
q[i + 1] = q_round_s8((x1 - offset) * scale);
q[i + 2] = q_round_s8((x2 - offset) * scale);
q[i + 3] = q_round_s8((x3 - offset) * scale);
}
for (; i < n; ++i) {
float x = float16_to_float32(v[i]);
q[i] = q_round_s8((x - offset) * scale);
}
}
static inline void quantize_bfloat16_to_signed8bit (const uint16_t *v, int8_t *q, float offset, float scale, int n) {
int i = 0;
for (; i + 3 < n; i += 4) {
float x0 = bfloat16_to_float32(v[i ]);
float x1 = bfloat16_to_float32(v[i + 1]);
float x2 = bfloat16_to_float32(v[i + 2]);
float x3 = bfloat16_to_float32(v[i + 3]);
q[i ] = q_round_s8((x0 - offset) * scale);
q[i + 1] = q_round_s8((x1 - offset) * scale);
q[i + 2] = q_round_s8((x2 - offset) * scale);
q[i + 3] = q_round_s8((x3 - offset) * scale);
}
for (; i < n; ++i) {
float x = bfloat16_to_float32(v[i]);
q[i] = q_round_s8((x - offset) * scale);
}
}
static inline void quantize_u8_to_signed8bit (const uint8_t *v, int8_t *q, float offset, float scale, int n) {
int i = 0;
for (; i + 3 < n; i += 4) {
float x0 = (float)v[i ];
float x1 = (float)v[i + 1];
float x2 = (float)v[i + 2];
float x3 = (float)v[i + 3];
q[i ] = q_round_s8((x0 - offset) * scale);
q[i + 1] = q_round_s8((x1 - offset) * scale);
q[i + 2] = q_round_s8((x2 - offset) * scale);
q[i + 3] = q_round_s8((x3 - offset) * scale);
}
for (; i < n; ++i) {
float x = (float)v[i];
q[i] = q_round_s8((x - offset) * scale);
}
}
static inline void quantize_i8_to_signed8bit (const int8_t *v, int8_t *q, float offset, float scale, int n) {
int i = 0;
for (; i + 3 < n; i += 4) {
float x0 = (float)v[i ];
float x1 = (float)v[i + 1];
float x2 = (float)v[i + 2];
float x3 = (float)v[i + 3];
q[i ] = q_round_s8((x0 - offset) * scale);
q[i + 1] = q_round_s8((x1 - offset) * scale);
q[i + 2] = q_round_s8((x2 - offset) * scale);
q[i + 3] = q_round_s8((x3 - offset) * scale);
}
for (; i < n; ++i) {
float x = (float)v[i];
q[i] = q_round_s8((x - offset) * scale);
}
}
static inline void quantize_float32 (const float *v, uint8_t *q, float offset, float scale, int dim, vector_qtype qtype) {
if (qtype == VECTOR_QUANT_U8BIT) quantize_float32_to_unsigned8bit(v, q, offset, scale, dim);
else quantize_float32_to_signed8bit(v, (int8_t *)q, offset, scale, dim);
}
static inline void quantize_float16 (const uint16_t *v, uint8_t *q, float offset, float scale, int dim, vector_qtype qtype) {
if (qtype == VECTOR_QUANT_U8BIT) quantize_float16_to_unsigned8bit(v, q, offset, scale, dim);
else quantize_float16_to_signed8bit(v, (int8_t *)q, offset, scale, dim);
}
static inline void quantize_bfloat16 (const uint16_t *v, uint8_t *q, float offset, float scale, int dim, vector_qtype qtype) {
if (qtype == VECTOR_QUANT_U8BIT) quantize_bfloat16_to_unsigned8bit(v, q, offset, scale, dim);
else quantize_bfloat16_to_signed8bit(v, (int8_t *)q, offset, scale, dim);
}
static inline void quantize_u8 (const uint8_t *v, uint8_t *q, float offset, float scale, int dim, vector_qtype qtype) {
if (qtype == VECTOR_QUANT_U8BIT) quantize_u8_to_unsigned8bit(v, q, offset, scale, dim);
else quantize_u8_to_signed8bit(v, (int8_t *)q, offset, scale, dim);
}
static inline void quantize_i8 (const int8_t *v, uint8_t *q, float offset, float scale, int dim, vector_qtype qtype) {
if (qtype == VECTOR_QUANT_U8BIT) quantize_i8_to_unsigned8bit(v, q, offset, scale, dim);
else quantize_i8_to_signed8bit(v, (int8_t *)q, offset, scale, dim);
}
static void quantize_binary (const float *input, uint8_t *output, int dim, bool is_binary_mean) {
float threshold = 0.0f;
if (is_binary_mean) {
// compute mean as threshold
float sum = 0.0f;
for (int i = 0; i < dim; i++) {
sum += input[i];
}
threshold = sum / dim;
}
// quantize
memset(output, 0, (dim + 7) / 8);
for (int i = 0; i < dim; i++) {
if (input[i] >= threshold) {
output[i / 8] |= (1 << (i % 8));
}
}
}
static void quantize_binary_f16 (const uint16_t *input, uint8_t *output, int dim, bool is_binary_mean) {
float threshold = 0.0f;
if (is_binary_mean) {
float sum = 0.0f;
for (int i = 0; i < dim; i++) {
sum += float16_to_float32(input[i]);
}
threshold = sum / dim;
}
memset(output, 0, (dim + 7) / 8);
for (int i = 0; i < dim; i++) {
if (float16_to_float32(input[i]) >= threshold) {
output[i / 8] |= (1 << (i % 8));
}
}
}
static void quantize_binary_bf16 (const uint16_t *input, uint8_t *output, int dim, bool is_binary_mean) {
float threshold = 0.0f;
if (is_binary_mean) {
float sum = 0.0f;
for (int i = 0; i < dim; i++) {
sum += bfloat16_to_float32(input[i]);
}
threshold = sum / dim;
}
memset(output, 0, (dim + 7) / 8);
for (int i = 0; i < dim; i++) {
if (bfloat16_to_float32(input[i]) >= threshold) {
output[i / 8] |= (1 << (i % 8));
}
}
}
static void quantize_binary_u8 (const uint8_t *input, uint8_t *output, int dim) {
// For unsigned 8-bit, threshold at 128 (midpoint)
memset(output, 0, (dim + 7) / 8);
for (int i = 0; i < dim; i++) {
if (input[i] >= 128) {
output[i / 8] |= (1 << (i % 8));
}
}
}
static void quantize_binary_i8 (const int8_t *input, uint8_t *output, int dim) {
// For signed 8-bit, threshold at 0 (sign-based)
memset(output, 0, (dim + 7) / 8);
for (int i = 0; i < dim; i++) {
if (input[i] >= 0) {
output[i / 8] |= (1 << (i % 8));
}
}
}
// MARK: - General Utils -
static int vector_type_to_size (vector_type type) {
switch (type) {
case VECTOR_TYPE_F32: return sizeof(float); // 4 bytes
case VECTOR_TYPE_F16: return sizeof(uint16_t); // 2 bytes
case VECTOR_TYPE_BF16: return sizeof(uint16_t); // 2 bytes
case VECTOR_TYPE_U8: return sizeof(uint8_t); // 1 byte
case VECTOR_TYPE_I8: return sizeof(int8_t); // 1 byte
case VECTOR_TYPE_BIT: return 0; // Special: use vector_bytes_for_dim()
}
return -1; // error
}
static vector_type vector_name_to_type (const char *vname) {
if ((strcasecmp(vname, "F32") == 0) || (strcasecmp(vname, "FLOAT32") == 0)) return VECTOR_TYPE_F32;
if ((strcasecmp(vname, "F16") == 0) || (strcasecmp(vname, "FLOAT16") == 0)) return VECTOR_TYPE_F16;
if ((strcasecmp(vname, "BF16") == 0) || (strcasecmp(vname, "FLOATB16") == 0)) return VECTOR_TYPE_BF16;
if ((strcasecmp(vname, "U8") == 0) || (strcasecmp(vname, "UINT8") == 0)) return VECTOR_TYPE_U8;
if ((strcasecmp(vname, "I8") == 0) || (strcasecmp(vname, "INT8") == 0)) return VECTOR_TYPE_I8;
if (strcasecmp(vname, "BIT") == 0 || strcasecmp(vname, "BINARY") == 0 || strcasecmp(vname, "1BIT") == 0) return VECTOR_TYPE_BIT;
return 0;
}
const char *vector_type_to_name (vector_type type) {
switch (type) {
case VECTOR_TYPE_F32: return "FLOAT32";
case VECTOR_TYPE_F16: return "FLOAT16";
case VECTOR_TYPE_BF16: return "FLOATB16";
case VECTOR_TYPE_U8: return "UINT8";
case VECTOR_TYPE_I8: return "INT8";
case VECTOR_TYPE_BIT: return "BIT";
}
return "N/A";
}
static size_t vector_bytes_for_dim (vector_type type, int dim) {
// returns total bytes needed to store a vector of given type and dimension
if (type == VECTOR_TYPE_BIT) {
return (size_t)((dim + 7) / 8); // Ceil division: pack 8 dimensions per byte
}
return (size_t)dim * vector_type_to_size(type);
}
static vector_qtype quant_name_to_type (const char *qname) {
if (strcasecmp(qname, "UINT8") == 0) return VECTOR_QUANT_U8BIT;
if (strcasecmp(qname, "INT8") == 0) return VECTOR_QUANT_S8BIT;
if (strcasecmp(qname, "1BIT") == 0 || strcasecmp(qname, "BIT") == 0 || strcasecmp(qname, "BINARY") == 0) return VECTOR_QUANT_1BIT;
return -1;
}
static vector_distance distance_name_to_type (const char *dname) {
if (strcasecmp(dname, "L2") == 0) return VECTOR_DISTANCE_L2;
if (strcasecmp(dname, "EUCLIDEAN") == 0) return VECTOR_DISTANCE_L2;
if (strcasecmp(dname, "SQUARED_L2") == 0) return VECTOR_DISTANCE_SQUARED_L2;
if (strcasecmp(dname, "COSINE") == 0) return VECTOR_DISTANCE_COSINE;
if (strcasecmp(dname, "DOT") == 0) return VECTOR_DISTANCE_DOT;
if (strcasecmp(dname, "INNER") == 0) return VECTOR_DISTANCE_DOT;
if (strcasecmp(dname, "L1") == 0) return VECTOR_DISTANCE_L1;
if (strcasecmp(dname, "MANHATTAN") == 0) return VECTOR_DISTANCE_L1;
if (strcasecmp(dname, "HAMMING") == 0) return VECTOR_DISTANCE_HAMMING;
return 0;
}
const char *vector_distance_to_name (vector_distance type) {
switch (type) {
case VECTOR_DISTANCE_L2: return "L2";
case VECTOR_DISTANCE_SQUARED_L2: return "SQUARED_L2";
case VECTOR_DISTANCE_COSINE: return "COSINE";
case VECTOR_DISTANCE_DOT: return "DOT";
case VECTOR_DISTANCE_L1: return "L1";
case VECTOR_DISTANCE_HAMMING: return "HAMMING";
}
return "N/A";
}
#if DEBUG_VECTOR_SERIALIZATION
static void vector_print (void *buf, vector_type type, int n) {
printf("type: %s - dim: %d [", vector_type_to_name(type), n);
for (int i=0; i<n; ++i) {
switch (type) {
case VECTOR_TYPE_F32: {
float *f = (float *)buf;
printf("%f,", f[i]);
}
break;
case VECTOR_TYPE_F16: {
uint16_t *f = (uint16_t *)buf;
printf("%f,", float16_to_float32(f[i]));
}
break;
case VECTOR_TYPE_BF16: {
uint16_t *f = (uint16_t *)buf;
printf("%f,", bfloat16_to_float32(f[i]));
}
break;
case VECTOR_TYPE_U8: {
uint8_t *u = (uint8_t *)buf;
printf("%d,", u[i]);
}
break;
case VECTOR_TYPE_I8: {
int8_t *u = (int8_t *)buf;
printf("%d,", u[i]);
}
break;
case VECTOR_TYPE_BIT: {
uint8_t *b = (uint8_t *)buf;
printf("%d,", (b[i / 8] >> (i % 8)) & 1);
}
break;
}
}
printf("]\n");
}
#endif
static bool sanity_check_args (sqlite3_context *context, const char *func_name, int argc, sqlite3_value **argv, int ntypes, int *types) {
if (argc != ntypes) {
context_result_error(context, SQLITE_ERROR, "Function '%s' expects %d arguments, but %d were provided", func_name, ntypes, argc);
return false;
}
for (int i=0; i<argc; ++i) {
int actual_type = sqlite3_value_type(argv[i]);
if (actual_type != types[i]) {
context_result_error(context, SQLITE_ERROR, "Function '%s': argument %d must be of type %s (got %s)", func_name, (i+1), sqlite_type_name(types[i]), sqlite_type_name(actual_type));
return false;
}
}
return true;
}
static bool parse_keyvalue_string (sqlite3_context *context, const char *str, keyvalue_callback callback, void *xdata) {
if (!str) return true;
const char *p = str;
while (*p) {
SKIP_SPACES(p);
const char *key_start = p;
while (*p && *p != '=' && *p != ',') p++;
int key_len = (int)(p - key_start);
TRIM_TRAILING(key_start, key_len);
if (*p != '=') {