【誰か】faiss-gpuをWin用にビルドできない【助けて】
どちゃくそ苦しめられているので恥もへったくれもなく書きました
事前にやっておくこと
CUDA:12.4(※パスを通す)
https://developer.nvidia.com/cuda-12-4-0-download-archive
cuDNN:8.9.7.29(※パスを通す)
https://developer.nvidia.com/rdp/cudnn-archive
python:3.11.9(※パスを通す)
https://www.python.org/downloads/release/python-3119/
swig:4.2.1(※binディレクトリのパスを通す)
https://sourceforge.net/projects/swig/files/swigwin/swigwin-4.2.1/swigwin-4.2.1.zip/download?use_mirror=jaist
cmake:3.30.3(※パスを通す)
https://cmake.org/download/
Ninja(※パスを通す)
https://github.com/ninja-build/ninja/releases/download/v1.12.1/ninja-winarm64.zip
Intel MKL
https://www.intel.com/content/www/us/en/developer/tools/oneapi/onemkl.html
以下のパスを環境変数 PATH に追加:
C:\Program Files (x86)\Intel\oneAPI\mkl\latest\bin;C:\Program Files (x86)\Intel\oneAPI\compiler\latest\windows\bin
新しい環境変数 MKLROOT を作成し、値を設定:
C:\Program Files (x86)\Intel\oneAPI\mkl\latest;
新しい環境変数LIBを作成し、値を設定:
C:\Users\ユーザー名\AppData\Local\Programs\Python\Python311\libs;C:\Program Files (x86)\Windows Kits\10\Lib\10.0.22621.0\um\x64;C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.41.34120\lib\x64;C:\Program Files (x86)\Windows Kits\10\Lib\10.0.22621.0\ucrt\x64;C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.41.34120\atlmfc\lib\x64;C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Auxiliary\VS\lib\x64;C:\Program Files (x86)\Windows Kits\10\Lib\10.0.22621.0\ucrt\x64;C:\Program Files (x86)\Windows Kits\NETFXSDK\4.8\Lib\um\x64;C:\Program Files (x86)\Intel\oneAPI\mkl\latest\lib\intel64;C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v12.4\lib\x64;
新しい環境変数INCLUDE、値を設定:
C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.41.34120\include;C:\Program Files (x86)\Windows Kits\10\Include\10.0.22621.0\ucrt;C:\Program Files (x86)\Windows Kits\10\Include\10.0.22621.0\um;C:\Program Files (x86)\Windows Kits\10\Include\10.0.22621.0\shared;
Build Tools for Visual Studio 2022
「デスクトップ開発用C++」ワークロードを選択
以下のコマンドを実行
git clone https://github.com/facebookresearch/faiss.git
cd faiss
python -m venv venv
venv\Scripts\activate
pip install NumPy
一部ソースがwin環境だとビルドできないので修正する必要があります。
E:/faiss/faiss/gpu/impl/PQCodeDistances-inl.cuh の 2 行を編集します。
512行目:
auto outCodeDistancesView = outCodeDistancesF.view<3>(
↓
auto outCodeDistancesView = outCodeDistancesF.template view<3>(
547行目:
auto outDistancesCodeViewCols = outCodeDistancesView.view<2>(
↓
auto outDistancesCodeViewCols = outCodeDistancesView.template view<2>(
"E:\faiss\faiss\gpu\impl\BinaryDistance.cu"の8行目(他のインクルード文の前)に以下を追加。
#include <cuda_runtime.h>
"E:\faiss\faiss\gpu\GpuIndexBinaryFlat.cu"の8行目(他のインクルード文の前)に以下を追加。
#include <cuda_runtime.h>
#include <cuda_fp16.h>
E:\faiss\faiss\gpu\utils\MathOperators.cuhの行目(他のインクルード文の前)に以下を追加。
#include <cuda_fp16.h>
さらに286行目付近のコードを探し
static inline __device__ half zero() {
#if CUDA_VERSION >= 9000 || defined(USE_AMD_ROCM)
return 0;
#else
half h;
h.x = 0;
return h;
#endif
}
};
この行を以下のように修正します:
static inline __device__ half zero() {
#if CUDA_VERSION >= 9000 || defined(USE_AMD_ROCM)
return __float2half(0.0f);
#else
half h;
h.x = 0;
return h;
#endif
}
};
E:\faiss\faiss\gpu\impl\IVFAppend.cuの7行目(他のインクルード文の前)に以下を追加。
#include <cuda_fp16.h>
28行目あたりに以下を挿入
template<typename T>
__device__ __forceinline__ T getZero();
template<>
__device__ __forceinline__ float getZero<float>() {
return 0.0f;
}
template<>
__device__ __forceinline__ __half getZero<__half>() {
return __float2half(0.0f);
}
template<>
__device__ __forceinline__ uint8_t getZero<uint8_t>() {
return 0;
}
そして
for (int dim = 0; dim < encodedVecs.getSize(1); ++dim) {
EncodeT enc = valid ? encodedVecs[vecId][dim] : (EncodeT)0;
WarpPackedBits<EncodeT, EncodeBits>::write(
laneId, enc, valid, data);
data += wordsPerVectorBlockDim;
}
この行を以下のように修正します:
for (int dim = 0; dim < encodedVecs.getSize(1); ++dim) {
EncodeT enc = valid ? encodedVecs[vecId][dim] : __float2half(0.0f);
WarpPackedBits<EncodeT, EncodeBits>::write(
laneId, enc, valid, data);
data += wordsPerVectorBlockDim;
}
書き換えが終わったらビルド。
cmake -G "Ninja" ^
-S "E:/faiss" ^
-B "E:/faiss/build" ^
-DCMAKE_CUDA_COMPILER="C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v12.4/bin/nvcc.exe" ^
-DCMAKE_RC_COMPILER="C:/Program Files (x86)/Windows Kits/10/bin/10.0.22621.0/x64/rc.exe" ^
-DCMAKE_MT="C:/Program Files (x86)/Windows Kits/10/bin/10.0.22621.0/x64/mt.exe" ^
-DCMAKE_BUILD_TYPE=Release ^
-DCMAKE_INSTALL_PREFIX="E:/faiss/install" ^
-DBUILD_SHARED_LIBS=ON ^
-DBLA_VENDOR=Intel10_64_dyn ^
-DMKL_LIBRARIES="C:/Program Files (x86)/Intel/oneAPI/mkl/latest/lib/mkl_intel_ilp64_dll.lib;C:/Program Files (x86)/Intel/oneAPI/mkl/latest/lib/mkl_intel_thread_dll.lib;C:/Program Files (x86)/Intel/oneAPI/mkl/latest/lib/mkl_core_dll.lib" ^
-DSWIG_DIR="C:/swigwin-4.2.1" ^
-DSWIG_EXECUTABLE="C:/swigwin-4.2.1/swig.exe" ^
-DPython_EXECUTABLE="E:/faiss/venv/Scripts/python.exe" ^
-DPython_LIBRARIES="C:\Users\ユーザー名\AppData\Local\Programs\Python\Python311\libs" ^
-DCMAKE_CUDA_FLAGS="-allow-unsupported-compiler --expt-relaxed-constexpr --expt-extended-lambda -DCUDA_NO_HALF_OPERATORS -DCUDA_NO_HALF_CONVERSIONS -DCUDA_NO_HALF2_OPERATORS -Xcompiler \"/MD\" --disable-warnings"
-DFAISS_ENABLE_PYTHON=ON ^
-DFAISS_ENABLE_GPU=ON ^
-DFAISS_ENABLE_C_API=ON ^
-DCMAKE_CUDA_ARCHITECTURES="86" ^
-DCMAKE_CXX_FLAGS="-I\"E:/faiss\" -I\"E:/faiss/build\" /std:c++17" ^
-DCMAKE_EXE_LINKER_FLAGS="//LIB:\"C:\Users\ユーザー名\AppData\Local\Programs\Python\Python311\libs\" //LIB:\"C:\Program Files (x86)\Windows Kits\10\Lib\10.0.22621.0\um\x64\" //LIB:\" ^
-DCMAKE_SHARED_LINKER_FLAGS="//LIB:\"C:\Users\ユーザー名\AppData\Local\Programs\Python\Python311\libs\" //LIB:\"C:\Program Files (x86)\Windows Kits\10\Lib\10.0.22621.0\um\x64\" //LIB:\" ^
-DCMAKE_CUDA_STANDARD=17 ^
-DCMAKE_VERBOSE_MAKEFILE=ON ^
-DPython_ROOT_DIR="E:/faiss/venv" ^
-DPython_EXECUTABLE="C:/Users/ユーザー名/AppData/Local/Programs/Python/Python311" ^
-DPython_INCLUDE_DIRS="C:/Users/ユーザー名/AppData/Local/Programs/Python/Python311/include" ^
-DPython_NumPy_INCLUDE_DIRS="E:/faiss/venv/Lib/site-packages/numpy/core/include" ^
-DCMAKE_MSVC_RUNTIME_LIBRARY="MultiThreadedDLL" ^
-DCMAKE_SYSTEM_VERSION=10.0.22621.0 ^
-DCUDAToolkit_ROOT="C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v12.4" ^
-DFAISS_ENABLE_RAFT=OFF ^
-DFAISS_OPT_LEVEL=avx2 ^
-DFAISS_USE_LTO=ON ^
-DFAISS_LIBRARY="E:/faiss/build/faiss/libfaiss.lib" ^
-DFAISS_INCLUDE_DIR="E:/faiss/faiss" ^
-CUDA_HOST_COMPILER="C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.41.34120\bin\Hostx64\x64\cl.exe" ^
-DCMAKE_PREFIX_PATH="C:/Program Files (x86)/Windows Kits/10/Lib/10.0.22621.0/um/x64/"
-- Build files have been written to: E:/faiss/buildと表示されたら、以下を実行。
cmake --build ./build --target faiss --config Release -- -v
ビルドが終わったら次はこちらを実行
cmake --build ./build --target swigfaiss --config Release -- -v
しかし以下のエラーが出ます。たすけて・・・
(venv) E:\faiss>cmake --build ./build --target swigfaiss --config Release -- -v
[1/5] C:\PROGRA~1\MICROS~2\2022\COMMUN~1\VC\Tools\MSVC\1441~1.341\bin\Hostx64\x64\cl.exe /nologo /TP -Dfaiss_python_callbacks_EXPORTS -IE:\faiss\faiss\python\..\.. -IC:\Users\minam\AppData\Local\Programs\Python\Python311\include /DWIN32 /D_WINDOWS /EHsc /O2 /Ob2 /DNDEBUG -std:c++17 -MD /showIncludes /Fofaiss\python\CMakeFiles\faiss_python_callbacks.dir\python_callbacks.cpp.obj /Fdfaiss\python\CMakeFiles\faiss_python_callbacks.dir\ /FS -c E:\faiss\faiss\python\python_callbacks.cpp
[2/5] C:\Windows\system32\cmd.exe /C "cd . && "C:\Program Files\CMake\bin\cmake.exe" -E vs_link_dll --intdir=faiss\python\CMakeFiles\faiss_python_callbacks.dir --rc=C:\PROGRA~2\WI3CF2~1\10\bin\100226~1.0\x64\rc.exe --mt=C:\PROGRA~2\WI3CF2~1\10\bin\100226~1.0\x64\mt.exe --manifests -- C:\PROGRA~1\MICROS~2\2022\COMMUN~1\VC\Tools\MSVC\1441~1.341\bin\Hostx64\x64\link.exe /nologo faiss\python\CMakeFiles\faiss_python_callbacks.dir\python_callbacks.cpp.obj /out:faiss\python\faiss_python_callbacks.dll /implib:faiss\python\faiss_python_callbacks.lib /pdb:faiss\python\faiss_python_callbacks.pdb /dll /version:0.0 /machine:x64 /INCREMENTAL:NO kernel32.lib user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.lib advapi32.lib && cd ."
FAILED: faiss/python/faiss_python_callbacks.dll faiss/python/faiss_python_callbacks.lib
C:\Windows\system32\cmd.exe /C "cd . && "C:\Program Files\CMake\bin\cmake.exe" -E vs_link_dll --intdir=faiss\python\CMakeFiles\faiss_python_callbacks.dir --rc=C:\PROGRA~2\WI3CF2~1\10\bin\100226~1.0\x64\rc.exe --mt=C:\PROGRA~2\WI3CF2~1\10\bin\100226~1.0\x64\mt.exe --manifests -- C:\PROGRA~1\MICROS~2\2022\COMMUN~1\VC\Tools\MSVC\1441~1.341\bin\Hostx64\x64\link.exe /nologo faiss\python\CMakeFiles\faiss_python_callbacks.dir\python_callbacks.cpp.obj /out:faiss\python\faiss_python_callbacks.dll /implib:faiss\python\faiss_python_callbacks.lib /pdb:faiss\python\faiss_python_callbacks.pdb /dll /version:0.0 /machine:x64 /INCREMENTAL:NO kernel32.lib user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.lib advapi32.lib && cd ."
LINK: command "C:\PROGRA~1\MICROS~2\2022\COMMUN~1\VC\Tools\MSVC\1441~1.341\bin\Hostx64\x64\link.exe /nologo faiss\python\CMakeFiles\faiss_python_callbacks.dir\python_callbacks.cpp.obj /out:faiss\python\faiss_python_callbacks.dll /implib:faiss\python\faiss_python_callbacks.lib /pdb:faiss\python\faiss_python_callbacks.pdb /dll /version:0.0 /machine:x64 /INCREMENTAL:NO kernel32.lib user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.lib advapi32.lib /MANIFEST:EMBED,ID=2" failed (exit code 1120) with the following output:
python_callbacks.cpp.obj : error LNK2019: 未解決の外部シンボル "public: __cdecl faiss::FaissException::FaissException(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,char const *,char const *,int)" (??0FaissException@faiss@@QEAA@AEBV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@PEBD1H@Z) が関数 "public: virtual unsigned __int64 __cdecl PyCallbackIOReader::operator()(void *,unsigned __int64,unsigned __int64)" (??RPyCallbackIOReader@@UEAA_KPEAX_K1@Z) で参照されました
python_callbacks.cpp.obj : error LNK2001: 外部シンボル "public: virtual char const * __cdecl faiss::FaissException::what(void)const " (?what@FaissException@faiss@@UEBAPEBDXZ) は未解決です
python_callbacks.cpp.obj : error LNK2001: 外部シンボル "public: virtual int __cdecl faiss::IOReader::filedescriptor(void)" (?filedescriptor@IOReader@faiss@@UEAAHXZ) は未解決です
python_callbacks.cpp.obj : error LNK2001: 外部シンボル "public: virtual int __cdecl faiss::IOWriter::filedescriptor(void)" (?filedescriptor@IOWriter@faiss@@UEAAHXZ) は未解決です
faiss\python\faiss_python_callbacks.dll : fatal error LNK1120: 4 件の未解決の外部参照
ninja: build stopped: subcommand failed.