-
Notifications
You must be signed in to change notification settings - Fork 15
MB-62985 - Add support for binary quantised vectors. #329
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
metonymic-smokey
wants to merge
13
commits into
master
Choose a base branch
from
bq
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 11 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
26093ec
wip
metonymic-smokey 3af2383
use binary specific funcs
metonymic-smokey daa8fc6
cleanup
metonymic-smokey fd22b0d
try an idea
metonymic-smokey 7eb9574
only search top k*2
metonymic-smokey 401a144
use BIVF
metonymic-smokey 581dbc9
use ivf dist compute
metonymic-smokey 127a63d
refactor + cleanup
metonymic-smokey 815537a
reduce oversampling and check
metonymic-smokey 5c210e3
account for deleted vecs
metonymic-smokey e652071
use BFlat before Flat indexes
metonymic-smokey 60f83f1
binary vec - search without ids
metonymic-smokey b20ae9d
adapted section format to selectively use binary indexes
metonymic-smokey File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,6 +19,7 @@ package zap | |
|
|
||
| import ( | ||
| "encoding/binary" | ||
| "log" | ||
| "sync" | ||
| "sync/atomic" | ||
| "time" | ||
|
|
@@ -57,28 +58,29 @@ func (vc *vectorIndexCache) Clear() { | |
| // map. It's false otherwise. | ||
| func (vc *vectorIndexCache) loadOrCreate(fieldID uint16, mem []byte, | ||
| loadDocVecIDMap bool, except *roaring.Bitmap) ( | ||
| index *faiss.IndexImpl, vecDocIDMap map[int64]uint32, docVecIDMap map[uint32][]int64, | ||
| indexes []*faiss.IndexImpl, vecDocIDMap map[int64]uint32, docVecIDMap map[uint32][]int64, | ||
| vecIDsToExclude []int64, err error) { | ||
| index, vecDocIDMap, docVecIDMap, vecIDsToExclude, err = vc.loadFromCache( | ||
| indexes, vecDocIDMap, docVecIDMap, vecIDsToExclude, err = vc.loadFromCache( | ||
| fieldID, loadDocVecIDMap, mem, except) | ||
| return index, vecDocIDMap, docVecIDMap, vecIDsToExclude, err | ||
| return indexes, vecDocIDMap, docVecIDMap, vecIDsToExclude, err | ||
| } | ||
|
|
||
| // function to load the vectorDocIDMap and if required, docVecIDMap from cache | ||
| // If not, it will create these and add them to the cache. | ||
| func (vc *vectorIndexCache) loadFromCache(fieldID uint16, loadDocVecIDMap bool, | ||
| mem []byte, except *roaring.Bitmap) (index *faiss.IndexImpl, vecDocIDMap map[int64]uint32, | ||
| docVecIDMap map[uint32][]int64, vecIDsToExclude []int64, err error) { | ||
| mem []byte, except *roaring.Bitmap) (indexes []*faiss.IndexImpl, | ||
| vecDocIDMap map[int64]uint32, docVecIDMap map[uint32][]int64, | ||
| vecIDsToExclude []int64, err error) { | ||
|
|
||
| vc.m.RLock() | ||
|
|
||
| entry, ok := vc.cache[fieldID] | ||
| if ok { | ||
| index, vecDocIDMap, docVecIDMap = entry.load() | ||
| indexes, vecDocIDMap, docVecIDMap = entry.load() | ||
| vecIDsToExclude = getVecIDsToExclude(vecDocIDMap, except) | ||
| if !loadDocVecIDMap || (loadDocVecIDMap && len(entry.docVecIDMap) > 0) { | ||
| vc.m.RUnlock() | ||
| return index, vecDocIDMap, docVecIDMap, vecIDsToExclude, nil | ||
| return indexes, vecDocIDMap, docVecIDMap, vecIDsToExclude, nil | ||
| } | ||
|
|
||
| vc.m.RUnlock() | ||
|
|
@@ -88,7 +90,7 @@ func (vc *vectorIndexCache) loadFromCache(fieldID uint16, loadDocVecIDMap bool, | |
| // typically seen for the first filtered query. | ||
| docVecIDMap = vc.addDocVecIDMapToCacheLOCKED(entry) | ||
| vc.m.Unlock() | ||
| return index, vecDocIDMap, docVecIDMap, vecIDsToExclude, nil | ||
| return indexes, vecDocIDMap, docVecIDMap, vecIDsToExclude, nil | ||
| } | ||
|
|
||
| vc.m.RUnlock() | ||
|
|
@@ -117,20 +119,20 @@ func (vc *vectorIndexCache) addDocVecIDMapToCacheLOCKED(ce *cacheEntry) map[uint | |
| // Rebuilding the cache on a miss. | ||
| func (vc *vectorIndexCache) createAndCacheLOCKED(fieldID uint16, mem []byte, | ||
| loadDocVecIDMap bool, except *roaring.Bitmap) ( | ||
| index *faiss.IndexImpl, vecDocIDMap map[int64]uint32, | ||
| indexes []*faiss.IndexImpl, vecDocIDMap map[int64]uint32, | ||
| docVecIDMap map[uint32][]int64, vecIDsToExclude []int64, err error) { | ||
|
|
||
| // Handle concurrent accesses (to avoid unnecessary work) by adding a | ||
| // check within the write lock here. | ||
| entry := vc.cache[fieldID] | ||
| if entry != nil { | ||
| index, vecDocIDMap, docVecIDMap = entry.load() | ||
| indexes, vecDocIDMap, docVecIDMap = entry.load() | ||
| vecIDsToExclude = getVecIDsToExclude(vecDocIDMap, except) | ||
| if !loadDocVecIDMap || (loadDocVecIDMap && len(entry.docVecIDMap) > 0) { | ||
| return index, vecDocIDMap, docVecIDMap, vecIDsToExclude, nil | ||
| return indexes, vecDocIDMap, docVecIDMap, vecIDsToExclude, nil | ||
| } | ||
| docVecIDMap = vc.addDocVecIDMapToCacheLOCKED(entry) | ||
| return index, vecDocIDMap, docVecIDMap, vecIDsToExclude, nil | ||
| return indexes, vecDocIDMap, docVecIDMap, vecIDsToExclude, nil | ||
| } | ||
|
|
||
| // if the cache doesn't have the entry, construct the vector to doc id map and | ||
|
|
@@ -161,21 +163,39 @@ func (vc *vectorIndexCache) createAndCacheLOCKED(fieldID uint16, mem []byte, | |
| } | ||
| } | ||
|
|
||
| indexes = make([]*faiss.IndexImpl, 0) | ||
| binaryIndexSize, n := binary.Uvarint(mem[pos : pos+binary.MaxVarintLen64]) | ||
| pos += n | ||
|
|
||
| // Read binary index with proper flags | ||
| binaryIndex, err := faiss.ReadBinaryIndexFromBuffer(mem[pos:pos+int(binaryIndexSize)], faissIOFlags) | ||
| if err != nil { | ||
| log.Printf("Error reading binary index: %v", err) | ||
| return nil, nil, nil, nil, err | ||
| } | ||
| indexes = append(indexes, binaryIndex) | ||
| pos += int(binaryIndexSize) | ||
|
|
||
| indexSize, n := binary.Uvarint(mem[pos : pos+binary.MaxVarintLen64]) | ||
| pos += n | ||
|
|
||
| index, err = faiss.ReadIndexFromBuffer(mem[pos:pos+int(indexSize)], faissIOFlags) | ||
| index, err := faiss.ReadIndexFromBuffer(mem[pos:pos+int(indexSize)], faissIOFlags) | ||
| if err != nil { | ||
| return nil, nil, nil, nil, err | ||
| } | ||
| indexes = append(indexes, index) | ||
|
|
||
| vc.insertLOCKED(fieldID, index, vecDocIDMap, loadDocVecIDMap, docVecIDMap) | ||
| return index, vecDocIDMap, docVecIDMap, vecIDsToExclude, nil | ||
| cacheEntryStub := cacheEntryReqs{ | ||
| index: indexes[1], | ||
| binaryIndex: indexes[0], | ||
CascadingRadium marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| vecDocIDMap: vecDocIDMap, | ||
| } | ||
|
|
||
| vc.insertLOCKED(fieldID, cacheEntryStub) | ||
| return indexes, vecDocIDMap, docVecIDMap, vecIDsToExclude, nil | ||
| } | ||
|
|
||
| func (vc *vectorIndexCache) insertLOCKED(fieldIDPlus1 uint16, | ||
| index *faiss.IndexImpl, vecDocIDMap map[int64]uint32, loadDocVecIDMap bool, | ||
| docVecIDMap map[uint32][]int64) { | ||
| func (vc *vectorIndexCache) insertLOCKED(fieldIDPlus1 uint16, ce cacheEntryReqs) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. take pointer to the cacheEntryReqs struct, don't copy the struct |
||
| // the first time we've hit the cache, try to spawn a monitoring routine | ||
| // which will reconcile the moving averages for all the fields being hit | ||
| if len(vc.cache) == 0 { | ||
|
|
@@ -189,8 +209,7 @@ func (vc *vectorIndexCache) insertLOCKED(fieldIDPlus1 uint16, | |
| // this makes the average to be kept above the threshold value for a | ||
| // longer time and thereby the index to be resident in the cache | ||
| // for longer time. | ||
| vc.cache[fieldIDPlus1] = createCacheEntry(index, vecDocIDMap, | ||
| loadDocVecIDMap, docVecIDMap, 0.4) | ||
| vc.cache[fieldIDPlus1] = createCacheEntry(&ce, 0.4, ce.loadDocVecIDMap) | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -283,19 +302,33 @@ func (e *ewma) add(val uint64) { | |
|
|
||
| // ----------------------------------------------------------------------------- | ||
|
|
||
| func createCacheEntry(index *faiss.IndexImpl, vecDocIDMap map[int64]uint32, | ||
| loadDocVecIDMap bool, docVecIDMap map[uint32][]int64, alpha float64) *cacheEntry { | ||
| // required info to create a cache entry. | ||
| type cacheEntryReqs struct { | ||
| alpha float64 | ||
| index *faiss.IndexImpl | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe just have the indexes array itself here |
||
| binaryIndex *faiss.IndexImpl | ||
| vecDocIDMap map[int64]uint32 | ||
| // Used to indicate if the below fields are populated - will only be | ||
| // used for pre-filtered queries. | ||
| loadDocVecIDMap bool | ||
| docVecIDMap map[uint32][]int64 | ||
| clusterAssignment map[int64]*roaring.Bitmap | ||
| } | ||
|
|
||
| func createCacheEntry(stub *cacheEntryReqs, alpha float64, | ||
| loadDocVecIDMap bool) *cacheEntry { | ||
| ce := &cacheEntry{ | ||
| index: index, | ||
| vecDocIDMap: vecDocIDMap, | ||
| index: stub.index, | ||
| binaryIndex: stub.binaryIndex, | ||
| vecDocIDMap: stub.vecDocIDMap, | ||
| tracker: &ewma{ | ||
| alpha: alpha, | ||
| sample: 1, | ||
| }, | ||
| refs: 1, | ||
| } | ||
| if loadDocVecIDMap { | ||
| ce.docVecIDMap = docVecIDMap | ||
| ce.docVecIDMap = stub.docVecIDMap | ||
| } | ||
| return ce | ||
| } | ||
|
|
@@ -309,6 +342,7 @@ type cacheEntry struct { | |
| refs int64 | ||
|
|
||
| index *faiss.IndexImpl | ||
| binaryIndex *faiss.IndexImpl | ||
| vecDocIDMap map[int64]uint32 | ||
| docVecIDMap map[uint32][]int64 | ||
| } | ||
|
|
@@ -325,16 +359,19 @@ func (ce *cacheEntry) decRef() { | |
| atomic.AddInt64(&ce.refs, -1) | ||
| } | ||
|
|
||
| func (ce *cacheEntry) load() (*faiss.IndexImpl, map[int64]uint32, map[uint32][]int64) { | ||
| func (ce *cacheEntry) load() ([]*faiss.IndexImpl, | ||
| map[int64]uint32, map[uint32][]int64) { | ||
| ce.incHit() | ||
| ce.addRef() | ||
| return ce.index, ce.vecDocIDMap, ce.docVecIDMap | ||
| return []*faiss.IndexImpl{ce.binaryIndex, ce.index}, ce.vecDocIDMap, ce.docVecIDMap | ||
| } | ||
|
|
||
| func (ce *cacheEntry) close() { | ||
| go func() { | ||
| ce.index.Close() | ||
| ce.index = nil | ||
| ce.binaryIndex.Close() | ||
| ce.binaryIndex = nil | ||
| ce.vecDocIDMap = nil | ||
| ce.docVecIDMap = nil | ||
| }() | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.