Skip to content

Commit 243bd75

Browse files
authored
Merge pull request #1679 from tursodatabase/vector-search-float16
vector search: add float16 support
2 parents 59f189e + 6ed738e commit 243bd75

14 files changed

Lines changed: 1142 additions & 215 deletions

File tree

libsql-ffi/bundled/SQLite3MultipleCiphers/src/sqlite3.c

Lines changed: 325 additions & 5 deletions
Large diffs are not rendered by default.

libsql-ffi/bundled/src/sqlite3.c

Lines changed: 325 additions & 5 deletions
Large diffs are not rendered by default.

libsql-sqlite3/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,5 @@ libsql
6262
/crates/target/
6363
/has_tclsh*
6464
/libsql.wasm
65+
test_libsql_f16_table.h
66+
test_libsql_f16

libsql-sqlite3/Makefile.in

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ LIBOBJS0 = alter.lo analyze.lo attach.lo auth.lo \
195195
sqlite3session.lo select.lo sqlite3rbu.lo status.lo stmt.lo \
196196
table.lo threads.lo tokenize.lo treeview.lo trigger.lo \
197197
update.lo userauth.lo upsert.lo util.lo vacuum.lo \
198-
vector.lo vectorfloat32.lo vectorfloat64.lo vectorfloat1bit.lo vectorfloat8.lo \
198+
vector.lo vectorfloat32.lo vectorfloat64.lo vectorfloat1bit.lo vectorfloat8.lo vectorfloat16.lo \
199199
vectorIndex.lo vectordiskann.lo vectorvtab.lo \
200200
vdbe.lo vdbeapi.lo vdbeaux.lo vdbeblob.lo vdbemem.lo vdbesort.lo \
201201
vdbetrace.lo vdbevtab.lo \
@@ -304,6 +304,7 @@ SRC = \
304304
$(TOP)/src/vector.c \
305305
$(TOP)/src/vectorInt.h \
306306
$(TOP)/src/vectorfloat1bit.c \
307+
$(TOP)/src/vectorfloat16.c \
307308
$(TOP)/src/vectorfloat32.c \
308309
$(TOP)/src/vectorfloat64.c \
309310
$(TOP)/src/vectorfloat8.c \
@@ -1143,6 +1144,9 @@ vector.lo: $(TOP)/src/vector.c $(HDR)
11431144
vectorfloat1bit.lo: $(TOP)/src/vectorfloat1bit.c $(HDR)
11441145
$(LTCOMPILE) $(TEMP_STORE) -c $(TOP)/src/vectorfloat1bit.c
11451146

1147+
vectorfloat16.lo: $(TOP)/src/vectorfloat16.c $(HDR)
1148+
$(LTCOMPILE) $(TEMP_STORE) -c $(TOP)/src/vectorfloat16.c
1149+
11461150
vectorfloat32.lo: $(TOP)/src/vectorfloat32.c $(HDR)
11471151
$(LTCOMPILE) $(TEMP_STORE) -c $(TOP)/src/vectorfloat32.c
11481152

libsql-sqlite3/src/test_libsql_diskann.c

Lines changed: 0 additions & 179 deletions
This file was deleted.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* GENERATE: python3 test_libsql_f16.py > test_libsql_f16_table.h
3+
* BUILD: cc test_libsql_f16.c -I ../ -L ../.libs -llibsql -lm -o test_libsql_f16
4+
* RUN: LD_LIBRARY_PATH=../.libs ./test_libsql_diskann
5+
*/
6+
7+
#include "assert.h"
8+
#include "stdbool.h"
9+
#include "stdarg.h"
10+
#include "stddef.h"
11+
#include "vectorfloat16.c"
12+
#include "test_libsql_f16_table.h"
13+
14+
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
15+
#define ensure(condition, ...) { if (!(condition)) { eprintf(__VA_ARGS__); exit(1); } }
16+
17+
int main() {
18+
for(int i = 0; i < 65536; i++){
19+
u32 expected = F16ToF32[i];
20+
float actual = vectorF16ToFloat(i);
21+
u32 actual_u32 = *((u32*)&actual);
22+
ensure(expected == actual_u32, "conversion from %x failed: %f != %f (%x != %x)", i, *(float*)&expected, *(float*)&actual_u32, expected, actual_u32);
23+
}
24+
for(int i = 0; i < 65536; i++){
25+
u16 expected = F32ToF16[i];
26+
u16 actual = vectorF16FromFloat(*(float*)&F32[i]);
27+
ensure(expected == actual, "conversion from %x (%f, it=%d) failed: %x != %x", F32[i], *(float*)&F32[i], i, expected, actual);
28+
}
29+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import random
2+
import struct
3+
import numpy as np
4+
5+
u32_list = [random.randint(0, 2**32) for _ in range(65536)]
6+
7+
print("""
8+
u32 F32[65536] = {
9+
""")
10+
for i, x in enumerate(u32_list):
11+
if i % 8 == 0: print(" ", end='');
12+
print('{:>10}u, '.format(x), end='')
13+
if i % 8 == 7: print()
14+
print("};")
15+
16+
17+
print("""
18+
u16 F32ToF16[65536] = {
19+
""")
20+
for i, x in enumerate(u32_list):
21+
if i % 8 == 0: print(" ", end='');
22+
u32_bytes = struct.pack('<I', x)
23+
f32 = np.float16(struct.unpack('<f', u32_bytes)[0])
24+
f16_bytes = struct.pack('<e', f32)
25+
u16 = struct.unpack('<H', f16_bytes)[0]
26+
print('{:>10}, '.format(u16), end='')
27+
if i % 8 == 7: print()
28+
print("};")
29+
30+
print("""
31+
u32 F16ToF32[65536] = {
32+
""")
33+
34+
for x in range(65536):
35+
if x % 8 == 0: print(" ", end='');
36+
u16_bytes = struct.pack('<H', x)
37+
f16 = struct.unpack('<e', u16_bytes)[0]
38+
f32_bytes = struct.pack('<f', f16)
39+
u32 = struct.unpack('<I', f32_bytes)[0]
40+
print('{:>10}u, '.format(u32), end='')
41+
if x % 8 == 7: print()
42+
print("};")

0 commit comments

Comments
 (0)