Skip to content

Commit 0618f03

Browse files
authored
Add fix for nmslib pragma on arm (#2574)
Signed-off-by: John Mazanec <jmazane@amazon.com>
1 parent 897d1f8 commit 0618f03

File tree

4 files changed

+33
-87
lines changed

4 files changed

+33
-87
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
2020
### Maintenance
2121
* Update package name to fix compilation issue [#2513](https://github.com/opensearch-project/k-NN/pull/2513)
2222
* Update gradle to 8.13 to fix command exec on java 21 [#2571](https://github.com/opensearch-project/k-NN/pull/2571)
23+
* Add fix for nmslib pragma on arm [#2574](https://github.com/opensearch-project/k-NN/pull/2574)
2324
### Refactoring
2425
* Small Refactor Post Lucene 10.0.1 upgrade [#2541](https://github.com/opensearch-project/k-NN/pull/2541)
2526
* Refactor codec to leverage backwards_codecs [#2546](https://github.com/opensearch-project/k-NN/pull/2546)

DEVELOPER_GUIDE.md

+5-87
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,6 @@ One easy way to install on mac or linux is to use pip:
6262
pip install cmake==3.24.0
6363
```
6464

65-
On Mac M series machines, install cmake using:
66-
```bash
67-
brew install cmake
68-
```
69-
7065
#### Faiss Dependencies
7166

7267
To build the *faiss* JNI library, you need to have openmp, lapack and blas installed. For more information on *faiss*
@@ -77,87 +72,15 @@ dependencies, please refer to [their documentation](https://github.com/facebookr
7772
brew install openblas
7873
```
7974

80-
Additionally, the `gcc` toolchain needs to be installed on Mac. To install, run:
81-
```bash
82-
brew install gcc
83-
```
84-
85-
#### Additional setup for Mac M series Machines
86-
87-
The following commands enable running/building k-NN on M series machines:
88-
75+
OpenMP can be installed on Mac via:
8976
```bash
90-
// Go to k-NN folder
91-
cd k-NN
92-
93-
// Build to generate the necessary files to be modified below (will fail)
94-
./gradlew build
95-
96-
//Go to jni folder
97-
cd jni
98-
99-
// File changes required
100-
sed -i -e 's/\/usr\/local\/opt\/libomp\//\/opt\/homebrew\/opt\/llvm\//g' cmake/init-faiss.cmake
101-
sed -i -e 's/__aarch64__/__undefine_aarch64__/g' external/faiss/faiss/utils/distances_simd.cpp
102-
sed -i -e 's/pragma message WARN/pragma message /g' external/nmslib/similarity_search/src/distcomp_scalar.cc
103-
// Change -mcpu value to use chip version according to your M series, for example, -mcpu=apple-m1. You can see the supported mcpu values via gcc --print-supported-cpus
104-
sed -i -e 's/-march=native/-mcpu=apple-m1/g' external/nmslib/similarity_search/CMakeLists.txt
105-
sed -i -e 's/-mcpu=apple-a14/-mcpu=apple-m1/g' external/nmslib/python_bindings/setup.py
106-
107-
// Install llvm
108-
brew install llvm
109-
echo 'export PATH="/opt/homebrew/opt/llvm/bin:$PATH"' >> ~/.zshrc
110-
source ~/.zshrc
111-
112-
// Set compiler path for CMAKE
113-
export CC=/opt/homebrew/opt/llvm/bin/clang
114-
export CXX=/opt/homebrew/opt/llvm/bin/clang++
115-
116-
// In case of linking issues with the external libraries and clang, you can try setting the CMAKE compiler to gcc/g++ instead through the following commands:
117-
export CC=gcc
118-
export CXX=g++
119-
sed -i '' '/set(CMAKE_CXX_STANDARD_REQUIRED True)/a\'$'\n''set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Xclang -fopenmp -L/opt/homebrew/opt/libomp/lib -I/opt/homebrew/opt/libomp/include -lomp -arch arm64 -fexceptions")'$'\n''' CMakeLists.txt
120-
121-
122-
// Build
123-
cmake . --fresh
124-
make
125-
```
126-
127-
Next, obtain a minimum distribution tarball of the k-NN version you want to build:
128-
129-
1. Fork the [OpenSearch Repo](https://github.com/opensearch-project/OpenSearch) into your github account.
130-
2. Clone the repository locally
131-
3. Run the following commands:
132-
```cd OpenSearch && ./gradlew -p distribution/archives/darwin-tar assemble```
133-
4. You should see a opensearch-min-<version>-SNAPSHOT-darwin-x64.tar.gz file present in distribution/archives/darwin-tar/build/distributions/
134-
5. Build k-NN by passing the OpenSearch distribution path in `./gradlew <integTest/run> -PcustomDistributionUrl="<Full path to .tar.gz file you noted above>"`
135-
136-
If you want to start OpenSearch directly on Mac M series, make sure to use JDK for ARM. Otherwise, you will see the following error: `mach-o file, but is an incompatible architecture (have 'arm64', need 'x86_64')`. It is better to start OpenSearch by running `bash opensearch-tar-install.sh` instead of `./bin/opensearch`. To run `./bin/opensearch`, the environment variable `JAVA_LIBRARY_PATH` needs to be set correctly so that OpenSearch can find the JNI library:
137-
138-
```
139-
export OPENSEARCH_HOME=the directory of opensearch...
140-
export JAVA_LIBRARY_PATH=$JAVA_LIBRARY_PATH:$OPENSEARCH_HOME/plugins/opensearch-knn/lib
77+
brew install libomp
14178
```
14279

143-
CMAKE will use as JAVA_HOME environment whatever your gradle is currently using. For example:
80+
Additionally, the `gcc` toolchain might need to be installed on Mac. To install, run:
14481
```bash
145-
Java home directory found by gradle: /opt/homebrew/Cellar/openjdk@21/21.0.5/libexec/openjdk.jdk/Contents/Home
146-
=======================================
147-
OpenSearch Build Hamster says Hello!
148-
Gradle Version : 8.4
149-
OS Info : Mac OS X 14.4 (aarch64)
150-
JDK Version : 21 (Homebrew JDK)
151-
JAVA_HOME : /opt/homebrew/Cellar/openjdk@21/21.0.5/libexec/openjdk.jdk/Contents/Home
152-
Random Testing Seed : 8AB32A4719AA345E
153-
In FIPS 140 mode : false
154-
=======================================
82+
brew install gcc
15583
```
156-
The JAVA_HOME used by gradle will be the default that the project will be using.
157-
158-
#### Environment
159-
160-
Currently, the plugin only supports Linux on x64 and arm platforms.
16184

16285
## Use an Editor
16386

@@ -211,11 +134,6 @@ Build OpenSearch k-NN using `gradlew build`
211134
./gradlew build
212135
```
213136

214-
For Mac M series machines use
215-
```
216-
./gradlew build -PcustomDistributionUrl="<Full path to .tar.gz file file you noted above>"
217-
```
218-
219137

220138
### JNI Library
221139

@@ -268,7 +186,7 @@ If you want to make a custom patch on JNI library
268186
1. Make a change on top of current version of JNI library and push the commit locally.
269187
2. Create a patch file for the change using `git format-patch -o patches HEAD^`
270188
3. Place the patch file under `jni/patches`
271-
4. Make a change in `jni/CmakeLists.txt`, `.github/workflows/CI.yml` to apply the patch during build
189+
4. Make a change in `jni/cmake/init-nmslib.cmake` or `jni/cmake/init-faiss.cmake` to apply the patch during build
272190

273191
By default, in the cmake build system, these patches will be applied and committed to the native libraries. In order to
274192
successfully make the commits the `user.name` and `user.email` git configurations need to be setup. If you cannot set

jni/cmake/init-nmslib.cmake

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ if(NOT DEFINED APPLY_LIB_PATCHES OR "${APPLY_LIB_PATCHES}" STREQUAL true)
2020
list(APPEND PATCH_FILE_LIST "${CMAKE_CURRENT_SOURCE_DIR}/patches/nmslib/0002-Adds-ability-to-pass-ef-parameter-in-the-query-for-h.patch")
2121
list(APPEND PATCH_FILE_LIST "${CMAKE_CURRENT_SOURCE_DIR}/patches/nmslib/0003-Added-streaming-apis-for-vector-index-loading-in-Hnsw.patch")
2222
list(APPEND PATCH_FILE_LIST "${CMAKE_CURRENT_SOURCE_DIR}/patches/nmslib/0004-Added-a-new-save-apis-in-Hnsw-with-streaming-interfa.patch")
23+
list(APPEND PATCH_FILE_LIST "${CMAKE_CURRENT_SOURCE_DIR}/patches/nmslib/0005-Add-util-include-to-fix-pragma-error.patch")
2324

2425
# Get patch id of the last commit
2526
execute_process(COMMAND sh -c "git --no-pager show HEAD | git patch-id --stable" OUTPUT_VARIABLE PATCH_ID_OUTPUT_FROM_COMMIT WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/external/nmslib)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
From d9e3486454a843448119dff5b7011b9184123aa2 Mon Sep 17 00:00:00 2001
2+
From: John Mazanec <jmazane@amazon.com>
3+
Date: Mon, 3 Mar 2025 12:33:59 -0800
4+
Subject: [PATCH] Add util include to fix pragma error
5+
6+
Adds include method in distcomp_scalar.cc to fix error related
7+
to pragma statements.
8+
---
9+
similarity_search/src/distcomp_scalar.cc | 1 +
10+
1 file changed, 1 insertion(+)
11+
12+
diff --git a/similarity_search/src/distcomp_scalar.cc b/similarity_search/src/distcomp_scalar.cc
13+
index 0aed6eb..245075f 100644
14+
--- a/similarity_search/src/distcomp_scalar.cc
15+
+++ b/similarity_search/src/distcomp_scalar.cc
16+
@@ -15,6 +15,7 @@
17+
#include "portable_intrinsics.h"
18+
#include "distcomp.h"
19+
#include "string.h"
20+
+#include "utils.h"
21+
22+
#include <cstdlib>
23+
#include <limits>
24+
--
25+
2.39.5 (Apple Git-154)
26+

0 commit comments

Comments
 (0)