kivy-ios icon indicating copy to clipboard operation
kivy-ios copied to clipboard

Help needed in creating recipe for FAISS - weird behaviour when importing .so

Open teticio opened this issue 4 years ago • 1 comments

Hi

I've made quite a lot of progress in building FAISS (Facebook package for accelerated similarity searching). My fork https://github.com/teticio/kivy-ios includes two recipes: one for faiss and one for openmp.

FAISS uses SWIG to wrap a C++ library so that it is callable from Python. As part of this process, it creates a shared object _swigfaiss.so which is imported by "import _swigfaiss". However, this gives an error:

ImportError: dynamic module does not define module export function (PyInit__swigfaiss)

I checked that the shared object does indeed export PyInit__swigfaiss and that it is built correctly for iOS. In fact, I am able to import the _swigfaiss.so manually using ctypes:

import os
import ctypes
dll = ctypes.PyDLL('../lib/python3.9/site-packages/faiss/_swigfaiss.so')
init = dll.PyInit__swigfaiss
init.restype = ctypes.py_object
_swigfaiss = init()

Any idea why the usual import _swifaiss fails, but this works? I find this very weird! Is there anything I can check in the way the library is built? Or should I be going about this in a completely different way?

Thanks!

PS: After patching swigfaiss.py along the lines above, I was able to get FAISS working on the iPhone Simulator. It causes a crash, however, on the iPhone which I have yet to debug. I wonder if there is some problem with statically linking OpenMP leading to some conflict. (My recipe doesn't yet work out of the box for iPhone. I need to make a "fat" build of _swigfaiss.so with arn64 and x86_64 architectures and codesign it automatically.

teticio avatar Jul 20 '21 16:07 teticio

If you are trying to use FAISS in your apps, take a look at my iOS port of Open3D which uses FAISS. https://github.com/kewlbear/Open3D-iOS.

kewlbear avatar Jul 27 '21 07:07 kewlbear