forked from PaddlePaddle/FastDeploy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
faiss.cmake
163 lines (145 loc) · 5.67 KB
/
faiss.cmake
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
include(ExternalProject)
set(FAISS_PROJECT external_faiss_download)
set(FAISS_FILENAME faiss)
set(FAISS_PREFIX_DIR ${THIRD_LIBS_PATH}/${FAISS_FILENAME})
set(FAISS_SOURCE_DIR ${THIRD_LIBS_PATH}/${FAISS_FILENAME}/src/${FAISS_PROJECT})
set(FAISS_INSTALL_DIR ${THIRD_LIBS_PATH}/install/${FAISS_FILENAME})
set(FAISS_INC_DIR ${FAISS_INSTALL_DIR}/include CACHE PATH "faiss include directory." FORCE)
if(ANDROID)
set(FAISS_LIB_DIR ${FAISS_INSTALL_DIR}/lib/${ANDROID_ABI} CACHE PATH "faiss lib directory." FORCE)
else()
set(FAISS_LIB_DIR ${FAISS_INSTALL_DIR}/lib CACHE PATH "faiss lib directory." FORCE)
endif()
if(NOT WITH_FAISS_STATIC)
message(FATAL_ERROR "Not support WITH_FAISS_STATIC=OFF now!")
endif()
set(FAISS_URL_PREFIX "https://bj.bcebos.com/fastdeploy/test")
if(ANDROID)
# check ABI, toolchain
if((NOT ANDROID_ABI MATCHES "armeabi-v7a") AND (NOT ANDROID_ABI MATCHES "arm64-v8a"))
message(FATAL_ERROR "FAISS only support armeabi-v7a, arm64-v8a now.")
endif()
if(NOT ANDROID_TOOLCHAIN MATCHES "clang")
message(FATAL_ERROR "Currently, only support clang toolchain but found ${ANDROID_TOOLCHAIN}.")
endif()
endif()
set(FAISS_VERSION 1.7.3)
# URL
if(NOT FAISS_URL)
if(WIN32)
set(FAISS_URL "${FAISS_URL_PREFIX}/faiss-win-x64-${FAISS_VERSION}.zip")
elseif(APPLE)
if(CURRENT_OSX_ARCH MATCHES "arm64")
set(FAISS_URL "${FAISS_URL_PREFIX}/faiss-osx-arm64-${FAISS_VERSION}.tgz")
else()
set(FAISS_URL "${FAISS_URL_PREFIX}/faiss-osx-x64-${FAISS_VERSION}.tgz")
endif()
elseif(ANDROID)
set(FAISS_URL "${FAISS_URL_PREFIX}/faiss-android-${ANDROID_ABI}-${FAISS_VERSION}.tgz")
else() # Linux
if(CMAKE_HOST_SYSTEM_PROCESSOR MATCHES "aarch64")
message(FATAL_ERROR "Not support for Linux aarch64 now!")
else()
if(WITH_FAISS_GPU)
set(FAISS_URL "${FAISS_URL_PREFIX}/faiss-linux-x64-gpu-${FAISS_VERSION}.tgz")
else()
set(FAISS_URL "${FAISS_URL_PREFIX}/faiss-linux-x64-${FAISS_VERSION}.tgz")
endif()
endif()
endif()
endif()
# FAISS Headers
include_directories(${FAISS_INC_DIR})
# FAISS Libs paths
if(WIN32)
set(FAISS_LIB "${FAISS_LIB_DIR}/faiss.lib")
elseif(APPLE)
set(FAISS_LIB "${FAISS_LIB_DIR}/libfaiss.a")
elseif(ANDROID)
set(FAISS_LIB "${FAISS_LIB_DIR}/libfaiss.a")
else() # Linux
set(FAISS_LIB "${FAISS_LIB_DIR}/libfaiss.a")
endif()
# Download FAISS
if(ANDROID)
ExternalProject_Add(
${FAISS_PROJECT}
${EXTERNAL_PROJECT_LOG_ARGS}
URL ${FAISS_URL}
PREFIX ${FAISS_PREFIX_DIR}
DOWNLOAD_NO_PROGRESS 1
CONFIGURE_COMMAND ""
BUILD_COMMAND ""
UPDATE_COMMAND ""
INSTALL_COMMAND
${CMAKE_COMMAND} -E remove_directory ${FAISS_INSTALL_DIR} &&
${CMAKE_COMMAND} -E make_directory ${FAISS_INSTALL_DIR} &&
${CMAKE_COMMAND} -E make_directory ${FAISS_INSTALL_DIR}/lib &&
${CMAKE_COMMAND} -E rename ${FAISS_SOURCE_DIR}/lib/ ${FAISS_INSTALL_DIR}/lib/${ANDROID_ABI} &&
${CMAKE_COMMAND} -E copy_directory ${FAISS_SOURCE_DIR}/include ${FAISS_INC_DIR}
BUILD_BYPRODUCTS ${FAISS_LIB})
else()
ExternalProject_Add(
${FAISS_PROJECT}
${EXTERNAL_PROJECT_LOG_ARGS}
URL ${FAISS_URL}
PREFIX ${FAISS_PREFIX_DIR}
DOWNLOAD_NO_PROGRESS 1
CONFIGURE_COMMAND ""
BUILD_COMMAND ""
UPDATE_COMMAND ""
INSTALL_COMMAND
${CMAKE_COMMAND} -E remove_directory ${FAISS_INSTALL_DIR} &&
${CMAKE_COMMAND} -E make_directory ${FAISS_INSTALL_DIR} &&
${CMAKE_COMMAND} -E rename ${FAISS_SOURCE_DIR}/lib/ ${FAISS_INSTALL_DIR}/lib &&
${CMAKE_COMMAND} -E copy_directory ${FAISS_SOURCE_DIR}/include ${FAISS_INC_DIR}
BUILD_BYPRODUCTS ${FAISS_LIB})
endif()
set(FAISS_LIBRARIES)
add_library(external_faiss STATIC IMPORTED GLOBAL)
set_property(TARGET external_faiss PROPERTY IMPORTED_LOCATION ${FAISS_LIB})
add_dependencies(external_faiss ${FAISS_PROJECT})
list(APPEND FAISS_LIBRARIES external_faiss)
# Add BLAS/LAPACK/OpenBLAS (needed by FAISS)
if(WIN32)
add_library(external_blas STATIC IMPORTED GLOBAL)
set_property(TARGET external_blas PROPERTY IMPORTED_LOCATION ${FAISS_LIB_DIR}/BLAS.lib)
add_dependencies(external_blas ${FAISS_PROJECT})
list(APPEND FAISS_LIBRARIES external_blas)
add_library(external_lapack STATIC IMPORTED GLOBAL)
set_property(TARGET external_lapack PROPERTY IMPORTED_LOCATION ${FAISS_LIB_DIR}/LAPACK.lib)
add_dependencies(external_lapack ${FAISS_PROJECT})
list(APPEND FAISS_LIBRARIES external_lapack)
elseif(APPLE)
find_package(BLAS REQUIRED)
list(APPEND FAISS_LIBRARIES ${BLAS_LIBRARIES})
find_package(LAPACK REQUIRED)
list(APPEND FAISS_LIBRARIES ${LAPACK_LIBRARIES})
elseif(ANDROID)
# OpenBLAS static lib already merged into libfaiss.a
message(STATUS "For Android, OpenBLAS static lib was already merged into libfaiss.a")
else() # Linux
find_package(BLAS REQUIRED)
list(APPEND FAISS_LIBRARIES ${BLAS_LIBRARIES})
find_package(LAPACK REQUIRED)
list(APPEND FAISS_LIBRARIES ${LAPACK_LIBRARIES})
endif()
# Add OpenMP (REQUIRED), OpenMP must be avaliable.
find_package(OpenMP REQUIRED)
list(APPEND FAISS_LIBRARIES OpenMP::OpenMP_CXX)
set(FAISS_INCLUDE_DIRS ${FAISS_INC_DIR})
set(FAISS_LIBS ${FAISS_LIBRARIES})
set(FAISS_FOUND TRUE)