interplane.Get Omega
Read in your language

sqlite

[Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI.

Brought by drakestapleton

branch master Branches Tags History

sqlite / ext / vec1 / test / vec1bqcos.test

107 lines · 2889 bytes Raw Blame History
1# 2026-10-18
2#
3# The author disclaims copyright to this source code. In place of
4# a legal notice, here is a blessing:
5#
6# May you do good and not evil.
7# May you find forgiveness for yourself and forgive others.
8# May you share freely, never taking more than you give.
9#
10#***********************************************************************
11#
12# Test recall of binary-quantized ("bq") indexes.
13#
14
15source [file join [file dirname [info script]] vec1_common.tcl]
16set testprefix vec1bqcos
17
18vec1_load db
19expr srand(0)
20
21set N 20000 ;# Number of vectors in the table
22set D 256 ;# Dimensions per vector
23set NQ 100 ;# Number of queries
24set NB 64 ;# Number of buckets
25
26# Return a normally distributed random value.
27proc gauss {} {
28 set u1 [expr {1.0 - rand()}]
29 set u2 [expr {rand()}]
30 expr {sqrt(-2.0 * log($u1)) * cos(6.283185307179586 * $u2)}
31}
32
33# Generate the common offset vector.
34set lOffset [list]
35for {set ii 0} {$ii < $D} {incr ii} {
36 lappend lOffset [gauss]
37}
38
39# Return a unit-length vector equal to the common offset plus
40# gaussian noise.
41proc offset_vector {} {
42 global lOffset
43 set lVec [list]
44 set sum 0.0
45 foreach o $lOffset {
46 set x [expr {$o + [gauss]}]
47 lappend lVec $x
48 set sum [expr {$sum + $x*$x}]
49 }
50 set norm [expr {sqrt($sum)}]
51 set lRet [list]
52 foreach x $lVec {
53 lappend lRet [expr {$x / $norm}]
54 }
55 binary format f* $lRet
56}
57db func offset_vector offset_vector
58
59do_execsql_test 1.0 {
60 CREATE VIRTUAL TABLE t USING vec1(v);
61 CREATE TABLE q(id INTEGER PRIMARY KEY, v BLOB);
62 WITH s(i) AS (
63 SELECT 1 UNION ALL SELECT i+1 FROM s WHERE i<CAST($N AS INTEGER)
64 )
65 INSERT INTO t(rowid, v) SELECT i, offset_vector() FROM s;
66 INSERT INTO q SELECT rowid, v FROM t WHERE rowid<=CAST($NQ AS INTEGER);
67}
68
69# Exact top-10 (by cosine distance) for each query.
70do_test 1.1 {
71 set aTruth [dict create]
72 db eval { SELECT id, v FROM q } {
73 dict set aTruth $id [db eval {
74 SELECT rowid FROM t ORDER BY vec1_cos_distance(t.v, $v) LIMIT 10
75 }]
76 }
77 dict size $aTruth
78} $NQ
79
80foreach {tn extra} {
81 1 {"distance": "cos"}
82 2 {"distance": "l2"}
83 3 {"distance": "cos", "wht": false}
84} {
85 set p "{\"nbucket\": $NB, \"quantizer\": \"bq\", \"codesize\": [expr $D/8], $extra}"
86
87 do_test 2.$tn.1 {
88 set model [db one { SELECT vec1_train(v, $p) FROM t }]
89 execsql { INSERT INTO t(cmd, arg) VALUES('rebuild', $model) }
90 } {}
91
92 # Search all buckets, with no re-ranking.
93 set hits 0
94 set qp "{\"k\": 10, \"nprobe\": $NB}"
95 db eval { SELECT id, v FROM q } {
96 foreach r [db eval { SELECT rowid FROM t($v, $qp) }] {
97 if {[lsearch -exact [dict get $aTruth $id] $r]>=0} { incr hits }
98 }
99 }
100 set recall [expr {double($hits) / (10 * $NQ)}]
101 set fmt [format " {%s} recall@10 = %.3f" $extra $recall]
102 do_test 2.$tn.2.$fmt {
103 expr {$recall > 0.25}
104 } 1
105}
106
107finish_test