0

I trained a HSMW Index using faiss on Windows. I wanted to deploy this to an Azure Function App, hence I just pip to install faiss-cpu==1.7.3 rather than the recommended install with conda.

The code that searches for the most similar vector is

distances, indices = index.search(vector, k)

works without issue on Windows, Python 3.10.4, faiss-cpu==1.7.3

However, in the Azure Function App and also on Ubuntu, it throws the following error.

  File "/home/vboxuser/Documents/VectorDB/src/match_vector.py", line 30, in get_matching_vectors
    distances, indices = index.search(vector, k)
  File "/home/vboxuser/Documents/VectorDB/.venv/lib/python3.10/site-packages/faiss/__init__.py", line 322, in replacement_search
    self.search_c(n, swig_ptr(x), k, swig_ptr(D), swig_ptr(I))
  File "/home/vboxuser/Documents/VectorDB/.venv/lib/python3.10/site-packages/faiss/swigfaiss.py", line 5436, in search
    return _swigfaiss.IndexHNSW_search(self, n, x, k, distances, labels)
TypeError: in method 'IndexHNSW_search', argument 3 of type 'float const *'

I would love to understand what causes this and whether there is a work around so that I can run it in the Azure Function App.

Thanks

sinoroc
  • 18,409
  • 2
  • 39
  • 70
Edgar H
  • 1,376
  • 2
  • 17
  • 31
  • 1
    Did you happen to check this -https://github.com/facebookresearch/faiss/issues/461 – SiddheshDesai Apr 17 '23 at 12:56
  • 1
    @SiddheshDesai That worked. Thank you. I feel stupid for missing that. If you want to submit it as an answer, I can accept it. The code I added is `vector = np.array(vector).astype(np.float32)` – Edgar H Apr 17 '23 at 16:45

1 Answers1

1

According to the solution in this github issue, Refer here:- TypeError: in method 'IndexFlat_add', argument 3 of type 'float const *' · Issue #461 · facebookresearch/faiss · GitHub Make sure the generated vector types or array is of numpy.float32

Code 1:-

import numpy as np
d = 128
n = 10000
xb = np.random.rand(n, d).astype(np.float32)

vector = [0.5] * d
vector = np.array(vector).astype(np.float32)

distances = np.linalg.norm(xb - vector, axis=1)

k = 5
indices = np.argsort(distances)[:k]

print("Distances:", distances[indices])
print("Indices:", indices)

Output:-

enter image description here

Code 2:-

import numpy as np

d = 128

n = 10000

x = np.random.rand(n, d).astype(np.float32)

print (x)

Output:-

enter image description here

SiddheshDesai
  • 3,668
  • 1
  • 2
  • 11