-
Notifications
You must be signed in to change notification settings - Fork 26
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
C bindings for IDIndexMap #9
Open
uhbuhb
wants to merge
13
commits into
DataIntelligenceCrew:master
Choose a base branch
from
uhbuhb:master
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 all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
53ce01f
index_idmap.go: go binding added
orihabAnyvision c9fed28
_example/flat_index_map.go: example test added to demonstrate usage
orihabAnyvision 4faa3a5
go.mod: require testify to help writing tests
orihabAnyvision 09cd749
index_idmap.go: return Index interface instead of IDMapWrapper
orihabAnyvision 6c44a77
index_idmap_test.go: added (in addition to _example file)
orihabAnyvision 10cdfe4
added gpu_bindings.go to enable transferring index to GPU
orihabAnyvision 73e5334
gpu_bindings.go: added TransferToCpu method
orihabAnyvision eea360d
add cpu functions and build gpu tag to gpu_bindings
uhbuhb 0c3bb82
go.mod: change module to Anyvisionltd instead of DataIntelligenceCrew
uhbuhb fa56e63
gpu_bindings_test.go: replace fmt.Print with t.Log
uhbuhb 2536fd6
index.go: add property for GpuResource
uhbuhb 681a91a
gpu_bindings.go: added Free method which frees the GpuResource and De…
uhbuhb 72e5dea
gpu_bindings_test.go: added a test for TestFreeGPUResource
uhbuhb 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 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 |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"github.com/DataIntelligenceCrew/go-faiss" | ||
) | ||
|
||
func main() { | ||
dimension := 1 | ||
dbSize := 5 | ||
|
||
index, err := faiss.NewIndexFlat(dimension, faiss.MetricL2) | ||
if err != nil { | ||
fmt.Println(err.Error()) | ||
} | ||
indexMap, err := faiss.NewIndexIDMap(index) | ||
if err != nil { | ||
fmt.Println(err.Error()) | ||
} | ||
xb := []float32{1,2,3,4,5} | ||
ids := make([]int64, dbSize) | ||
for i := 0; i < dbSize; i++ { | ||
ids[i] = int64(i) | ||
} | ||
|
||
err = indexMap.AddWithIDs(xb, ids) | ||
if err != nil { | ||
fmt.Println(err.Error()) | ||
} | ||
toFind := xb[dimension:2*dimension] | ||
distances1, resultIds, err := indexMap.Search(toFind, 5) | ||
fmt.Println(distances1, resultIds, err) | ||
fmt.Println(resultIds[0] == ids[1]) | ||
fmt.Println(distances1[0] == 0) | ||
|
||
} |
This file contains 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 |
---|---|---|
@@ -1,3 +1,5 @@ | ||
module github.com/DataIntelligenceCrew/go-faiss | ||
module github.com/Anyvisionltd/go-faiss | ||
|
||
go 1.14 | ||
|
||
require github.com/stretchr/testify v1.7.0 |
This file contains 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 |
---|---|---|
@@ -0,0 +1,60 @@ | ||
//+build gpu | ||
|
||
package faiss | ||
|
||
/* | ||
#include <faiss/c_api/gpu/StandardGpuResources_c.h> | ||
#include <faiss/c_api/gpu/GpuAutoTune_c.h> | ||
*/ | ||
import "C" | ||
import ( | ||
"errors" | ||
) | ||
|
||
|
||
func TransferToGpu(index Index) (Index, error) { | ||
var gpuResources *C.FaissStandardGpuResources | ||
var gpuIndex *C.FaissGpuIndex | ||
c := C.faiss_StandardGpuResources_new(&gpuResources) | ||
if c != 0 { | ||
return nil, errors.New("error on init gpu %v") | ||
} | ||
|
||
exitCode := C.faiss_index_cpu_to_gpu(gpuResources, 0, index.cPtr(), &gpuIndex) | ||
if exitCode != 0 { | ||
return nil, errors.New("error transferring to gpu") | ||
} | ||
|
||
return &faissIndex{idx: gpuIndex, resource: gpuResources}, nil | ||
} | ||
|
||
func TransferToCpu(gpuIndex Index) (Index, error) { | ||
var cpuIndex *C.FaissIndex | ||
|
||
exitCode := C.faiss_index_gpu_to_cpu(gpuIndex.cPtr(), &cpuIndex) | ||
if exitCode != 0 { | ||
return nil, errors.New("error transferring to gpu") | ||
} | ||
|
||
Free(gpuIndex) | ||
|
||
return &faissIndex{idx: cpuIndex}, nil | ||
} | ||
|
||
func Free(index Index) { | ||
var gpuResource *C.FaissStandardGpuResources | ||
gpuResource = index.cGpuResource() | ||
C.faiss_StandardGpuResources_free(gpuResource) | ||
index.Delete() | ||
} | ||
|
||
func CreateGpuIndex() (Index, error) { | ||
var gpuResource *C.FaissStandardGpuResources | ||
var gpuIndex *C.FaissGpuIndex | ||
c := C.faiss_StandardGpuResources_new(&gpuResource) | ||
if c != 0 { | ||
return nil, errors.New("error on init gpu %v") | ||
} | ||
|
||
return &faissIndex{idx: gpuIndex, resource: gpuResource}, nil | ||
} |
This file contains 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 |
---|---|---|
@@ -0,0 +1,21 @@ | ||
//+build cpu | ||
|
||
package faiss | ||
|
||
import "errors" | ||
|
||
func TransferToGpu(index Index) (Index, error) { | ||
return nil, errors.New("Not supported when running in CPU mode..") | ||
} | ||
|
||
func TransferToCpu(index Index) (Index, error) { | ||
return nil, errors.New("Not supported when running in CPU mode..") | ||
} | ||
|
||
func Free(gpuIndex Index) error { | ||
return errors.New("Not supported when running in CPU mode..") | ||
} | ||
|
||
func CreateGpuIndex() (Index, error) { | ||
return nil, errors.New("Not supported when running in CPU mode..") | ||
} |
This file contains 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 |
---|---|---|
@@ -0,0 +1,133 @@ | ||
//+build gpu | ||
|
||
package faiss | ||
|
||
import ( | ||
"github.com/stretchr/testify/require" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestFlatIndexOnGpuFunctionality(t *testing.T) { | ||
index, err := NewIndexFlatL2(1) | ||
require.Nil(t, err) | ||
|
||
gpuIdx, err := TransferToGpu(index) | ||
require.Nil(t, err) | ||
|
||
vectorsToAdd := []float32{1,2,3,4,5} | ||
err = gpuIdx.Add(vectorsToAdd) | ||
require.Nil(t, err) | ||
|
||
distances, resultIds, err := gpuIdx.Search(vectorsToAdd, 5) | ||
require.Nil(t, err) | ||
require.Equal(t, int64(len(vectorsToAdd)), gpuIdx.Ntotal()) | ||
|
||
t.Log(distances, resultIds, err) | ||
for i := range vectorsToAdd { | ||
require.Equal(t, int64(i), resultIds[len(vectorsToAdd)*i]) | ||
require.Zero(t, distances[len(vectorsToAdd)*i]) | ||
} | ||
//This is necessary bc RemoveIDs isn't implemented for GPUIndexs | ||
cpuIdx, err := TransferToCpu(gpuIdx) | ||
require.Nil(t, err) | ||
idsSelector, err := NewIDSelectorBatch([]int64{0}) | ||
cpuIdx.RemoveIDs(idsSelector) | ||
gpuIdx, err = TransferToGpu(cpuIdx) | ||
require.Nil(t, err) | ||
require.Equal(t, int64(len(vectorsToAdd)-1), gpuIdx.Ntotal()) | ||
|
||
} | ||
|
||
func TestIndexIDMapOnGPU(t *testing.T) { | ||
index, err := NewIndexFlatL2(1) | ||
require.Nil(t, err) | ||
|
||
indexMap, err := NewIndexIDMap(index) | ||
require.Nil(t, err) | ||
|
||
gpuIndex, err := TransferToGpu(indexMap) | ||
require.Nil(t, err) | ||
|
||
vectorsToAdd := []float32{1,2,3,4,5} | ||
ids := make([]int64, len(vectorsToAdd)) | ||
for i := 0; i < len(vectorsToAdd); i++ { | ||
ids[i] = int64(i) | ||
} | ||
|
||
err = gpuIndex.AddWithIDs(vectorsToAdd, ids) | ||
require.Nil(t, err) | ||
|
||
distances, resultIds, err := gpuIndex.Search(vectorsToAdd, 5) | ||
require.Nil(t, err) | ||
t.Log(gpuIndex.D(), gpuIndex.Ntotal()) | ||
t.Log(distances, resultIds, err) | ||
for i := range vectorsToAdd { | ||
require.Equal(t, ids[i], resultIds[len(vectorsToAdd)*i]) | ||
require.Zero(t, distances[len(vectorsToAdd)*i]) | ||
} | ||
} | ||
|
||
func TestTransferToGpuAndBack(t *testing.T) { | ||
index, err := NewIndexFlatL2(1) | ||
require.Nil(t, err) | ||
|
||
indexMap, err := NewIndexIDMap(index) | ||
require.Nil(t, err) | ||
|
||
gpuIndex, err := TransferToGpu(indexMap) | ||
require.Nil(t, err) | ||
|
||
vectorsToAdd := []float32{1,2,4,7,11} | ||
ids := make([]int64, len(vectorsToAdd)) | ||
for i := 0; i < len(vectorsToAdd); i++ { | ||
ids[i] = int64(i) | ||
} | ||
|
||
err = gpuIndex.AddWithIDs(vectorsToAdd, ids) | ||
require.Nil(t, err) | ||
|
||
//This is necessary bc RemoveIDs isn't implemented for GPUIndexs | ||
cpuIdx, err := TransferToCpu(gpuIndex) | ||
require.Nil(t, err) | ||
idsSelector, err := NewIDSelectorBatch([]int64{0}) | ||
cpuIdx.RemoveIDs(idsSelector) | ||
gpuIndex, err = TransferToGpu(cpuIdx) | ||
require.Nil(t, err) | ||
|
||
require.Equal(t, int64(4), gpuIndex.Ntotal()) | ||
distances2, resultIds2, err := gpuIndex.Search([]float32{1}, 5) | ||
t.Log(distances2, resultIds2, gpuIndex.Ntotal()) | ||
require.Nil(t, err) | ||
require.Equal(t, float32(1), distances2[0]) | ||
|
||
|
||
cpuIndex, err := TransferToCpu(gpuIndex) | ||
require.Nil(t, err) | ||
require.Equal(t, int64(4), cpuIndex.Ntotal()) | ||
|
||
idsSelector, err = NewIDSelectorBatch([]int64{0}) | ||
cpuIndex.RemoveIDs(idsSelector) | ||
distances2, resultIds2, err = cpuIndex.Search([]float32{1}, 5) | ||
t.Log(distances2, resultIds2, cpuIndex.Ntotal()) | ||
require.Nil(t, err) | ||
require.Equal(t, float32(1), distances2[0]) | ||
|
||
} | ||
|
||
func TestFreeGPUResource(t *testing.T) { | ||
for i := 0; i < 20; i++ { | ||
t.Logf("creating index %v", i) | ||
flatIndex, err := NewIndexFlatIP(256) | ||
require.Nil(t, err) | ||
flatIndexGpu, err := TransferToGpu(flatIndex) | ||
require.Nil(t, err) | ||
|
||
t.Log("created indexes, freeing..") | ||
err = Free(flatIndexGpu) | ||
require.Nil(t, err) | ||
t.Log("freed, memory should be freed..") | ||
time.Sleep(1 * time.Second) | ||
} | ||
|
||
} |
This file contains 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 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 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 |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package faiss | ||
|
||
/* | ||
#include <faiss/c_api/MetaIndexes_c.h> | ||
*/ | ||
import "C" | ||
import ( | ||
"errors" | ||
) | ||
|
||
func NewIndexIDMap(index Index) (Index, error) { | ||
var indexMapPointer *C.FaissIndexIDMap | ||
var pointerToIndexMapPointer **C.FaissIndexIDMap | ||
pointerToIndexMapPointer = &indexMapPointer | ||
if C.faiss_IndexIDMap_new(pointerToIndexMapPointer, index.cPtr()) != 0 { | ||
return nil, errors.New("Error occurred while initializing IndexIDMapWrapper") | ||
} | ||
return &faissIndex{idx: indexMapPointer}, nil | ||
} |
This file contains 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 |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package faiss | ||
|
||
import ( | ||
"fmt" | ||
"github.com/stretchr/testify/require" | ||
"testing" | ||
) | ||
|
||
func TestNewIndexIDMap(t *testing.T) { | ||
dimension := 1 | ||
dbSize := 5 | ||
|
||
index, err := NewIndexFlat(dimension, MetricL2) | ||
if err != nil { | ||
fmt.Println(err.Error()) | ||
} | ||
indexMap, err := NewIndexIDMap(index) | ||
if err != nil { | ||
fmt.Println(err.Error()) | ||
} | ||
xb := []float32{1,2,3,4,5} | ||
ids := make([]int64, dbSize) | ||
for i := 10; i < dbSize; i++ { | ||
ids[i] = int64(i) | ||
} | ||
|
||
err = indexMap.AddWithIDs(xb, ids) | ||
if err != nil { | ||
fmt.Println(err.Error()) | ||
} | ||
toFind := xb[dimension:2*dimension] | ||
distances1, resultIds, err := indexMap.Search(toFind, 5) | ||
require.Equal(t, resultIds[0], ids[1]) | ||
require.Zero(t, distances1[0]) | ||
|
||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think, changing the module name would break the existing code.