Skip to content

Commit 8cb7dbc

Browse files
committed
support gpu sharding
currently there is bug on faiss facebookresearch/faiss#2357 to use sharding you have to use this version of faiss for now git clone -b fix-cpi-gpu-faiss_index_cpu_to_gpu_multiple_with_options https://github.com/AviadHAv/faiss.git
1 parent a4c3dd4 commit 8cb7dbc

2 files changed

Lines changed: 29 additions & 17 deletions

File tree

gpu_bindings.go

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ package faiss
88
#include <stddef.h>
99
#include <faiss/c_api/gpu/StandardGpuResources_c.h>
1010
#include <faiss/c_api/gpu/GpuAutoTune_c.h>
11+
#include <faiss/c_api/gpu/GpuIndicesOptions_c.h>
1112
*/
1213
import "C"
1314
import (
@@ -35,13 +36,14 @@ func TransferToGpu(index Index) (Index, error) {
3536
return &faissIndex{idx: gpuIndex, resources: gpuResources}, nil
3637
}
3738

38-
func TransferToAllGPUs(index Index, gpuIndexes []int) (Index, error) {
39+
// to use sharding use this version of faiss - git clone -b fix-cpi-gpu-faiss_index_cpu_to_gpu_multiple_with_options https://github.com/AviadHAv/faiss.git
40+
// TransferToAllGPUs - gpuIndexes - which gpus to use [2,4,5] , index - flat or idmap , shard - should shard accross all gpus if not will put same data on all gpus
41+
func TransferToAllGPUs(index Index, gpuIndexes []int, sharding bool) (Index, error) {
3942
amountOfGPUs := len(gpuIndexes)
4043
gpuResources := make([]*C.FaissStandardGpuResources, len(gpuIndexes))
4144
for i := 0; i < len(gpuIndexes); i++ {
4245
var resourceIndex *C.FaissStandardGpuResources
4346
gpuResources[i] = resourceIndex
44-
4547
}
4648

4749
var gpuIndex *C.FaissGpuIndex
@@ -51,13 +53,23 @@ func TransferToAllGPUs(index Index, gpuIndexes []int) (Index, error) {
5153
return nil, errors.New("error on init gpu %v")
5254
}
5355
}
54-
55-
exitCode := C.faiss_index_cpu_to_gpu_multiple(
56-
(**C.FaissStandardGpuResources)(unsafe.Pointer(&gpuResources[0])),
57-
(*C.int)(unsafe.Pointer(&gpuIndexes[0])),
58-
C.size_t(len(gpuIndexes)),
59-
index.cPtr(),
60-
&gpuIndex)
56+
var exitCode C.int
57+
if sharding {
58+
exitCode = C.faiss_index_cpu_to_gpu_multiple_sharding(
59+
(**C.FaissStandardGpuResources)(unsafe.Pointer(&gpuResources[0])),
60+
C.size_t(len(gpuResources)),
61+
(*C.int)(unsafe.Pointer(&gpuIndexes[0])),
62+
C.size_t(len(gpuIndexes)),
63+
index.cPtr(),
64+
&gpuIndex)
65+
} else {
66+
exitCode = C.faiss_index_cpu_to_gpu_multiple(
67+
(**C.FaissStandardGpuResources)(unsafe.Pointer(&gpuResources[0])),
68+
(*C.int)(unsafe.Pointer(&gpuIndexes[0])),
69+
C.size_t(len(gpuIndexes)),
70+
index.cPtr(),
71+
&gpuIndex)
72+
}
6173

6274
if exitCode != 0 {
6375
return nil, errors.New("error transferring to gpu")

gpu_bindings_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
//+build gpu
1+
//go:build gpu
2+
// +build gpu
23

34
package faiss
45

@@ -15,7 +16,7 @@ func TestFlatIndexOnGpuFunctionality(t *testing.T) {
1516
gpuIdx, err := TransferToGpu(index)
1617
require.Nil(t, err)
1718

18-
vectorsToAdd := []float32{1,2,3,4,5}
19+
vectorsToAdd := []float32{1, 2, 3, 4, 5}
1920
err = gpuIdx.Add(vectorsToAdd)
2021
require.Nil(t, err)
2122

@@ -49,7 +50,7 @@ func TestIndexIDMapOnGPU(t *testing.T) {
4950
gpuIndex, err := TransferToGpu(indexMap)
5051
require.Nil(t, err)
5152

52-
vectorsToAdd := []float32{1,2,3,4,5}
53+
vectorsToAdd := []float32{1, 2, 3, 4, 5}
5354
ids := make([]int64, len(vectorsToAdd))
5455
for i := 0; i < len(vectorsToAdd); i++ {
5556
ids[i] = int64(i)
@@ -78,7 +79,7 @@ func TestTransferToGpuAndBack(t *testing.T) {
7879
gpuIndex, err := TransferToGpu(indexMap)
7980
require.Nil(t, err)
8081

81-
vectorsToAdd := []float32{1,2,4,7,11}
82+
vectorsToAdd := []float32{1, 2, 4, 7, 11}
8283
ids := make([]int64, len(vectorsToAdd))
8384
for i := 0; i < len(vectorsToAdd); i++ {
8485
ids[i] = int64(i)
@@ -101,7 +102,6 @@ func TestTransferToGpuAndBack(t *testing.T) {
101102
require.Nil(t, err)
102103
require.Equal(t, float32(1), distances2[0])
103104

104-
105105
cpuIndex, err := TransferToCpu(gpuIndex)
106106
require.Nil(t, err)
107107
require.Equal(t, int64(4), cpuIndex.Ntotal())
@@ -117,11 +117,11 @@ func TestTransferToGpuAndBack(t *testing.T) {
117117

118118
func TestFreeGPUResource(t *testing.T) {
119119
for i := 0; i < 20; i++ {
120-
gpus:= []int{0}
120+
gpus := []int{0}
121121
t.Logf("creating index %v", i)
122122
flatIndex, err := NewIndexFlatIP(256)
123123
require.Nil(t, err)
124-
flatIndexGpu, err := TransferToAllGPUs(flatIndex, gpus)
124+
flatIndexGpu, err := TransferToAllGPUs(flatIndex, gpus, false)
125125
require.Nil(t, err)
126126

127127
t.Log("created indexes, freeing..")
@@ -131,4 +131,4 @@ func TestFreeGPUResource(t *testing.T) {
131131
time.Sleep(1 * time.Second)
132132
}
133133

134-
}
134+
}

0 commit comments

Comments
 (0)