Skip to content

Commit 89ec326

Browse files
authored
Python build with a far more portable wheel (#396)
* Identified the appropriate build flags to get a working python build that doesn't rely on -march=native or -mtune=native. We've run benchmarks on multiple computers that indicate the only important flag other than -mavx2 -msse2 -mfma is -funroll-loops. Optimization levels such as -O1, -O2, or -O3 actually makes for less performant code. -Ofast is unavailble for use in Python, as it causes problems with floating point math in Python * 1.22 was left in a comment despite 1.25 being the value specified * Python 3.8 is not supported by numpy 1.25, so we're removing it.
1 parent 2c9912a commit 89ec326

File tree

4 files changed

+56
-15
lines changed

4 files changed

+56
-15
lines changed

.github/workflows/build-python.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ jobs:
66
strategy:
77
fail-fast: false
88
matrix:
9-
cibw-identifier: ["cp38-manylinux_x86_64", "cp39-manylinux_x86_64", "cp310-manylinux_x86_64", "cp311-manylinux_x86_64"]
9+
cibw-identifier: ["cp39-manylinux_x86_64", "cp310-manylinux_x86_64", "cp311-manylinux_x86_64"]
1010
runs-on: ubuntu-latest
1111
defaults:
1212
run:
@@ -25,7 +25,7 @@ jobs:
2525
strategy:
2626
fail-fast: false
2727
matrix:
28-
cibw-identifier: ["cp38-win_amd64", "cp39-win_amd64", "cp310-win_amd64", "cp311-win_amd64"]
28+
cibw-identifier: ["cp39-win_amd64", "cp310-win_amd64", "cp311-win_amd64"]
2929
runs-on: windows-latest
3030
defaults:
3131
run:

CMakeLists.txt

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -279,17 +279,18 @@ if(MSVC)
279279
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_RELEASE ${PROJECT_SOURCE_DIR}/x64/Release)
280280
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY_RELEASE ${PROJECT_SOURCE_DIR}/x64/Release)
281281
else()
282-
set(ENV{TCMALLOC_LARGE_ALLOC_REPORT_THRESHOLD} 500000000000)
283-
# set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -g -DDEBUG -O0 -fsanitize=address -fsanitize=leak -fsanitize=undefined")
284-
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -g -DDEBUG -Wall -Wextra")
282+
set(ENV{TCMALLOC_LARGE_ALLOC_REPORT_THRESHOLD} 500000000000)
283+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -mavx2 -mfma -msse2 -ftree-vectorize -fno-builtin-malloc -fno-builtin-calloc -fno-builtin-realloc -fno-builtin-free -fopenmp -fopenmp-simd -funroll-loops -Wfatal-errors -DUSE_AVX2")
284+
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -g -DDEBUG")
285285
if (NOT PYBIND)
286-
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -Ofast -DNDEBUG -march=native -mtune=native -ftree-vectorize")
286+
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -DNDEBUG -Ofast")
287+
if (NOT PORTABLE)
288+
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -march=native -mtune=native")
289+
endif()
287290
else()
288-
#-Ofast is super problematic for python. see: https://moyix.blogspot.com/2022/09/someones-been-messing-with-my-subnormals.html
289-
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -DNDEBUG -march=native -mtune=native -ftree-vectorize")
290-
add_compile_options(-fPIC)
291+
# -Ofast is not supported in a python extension module
292+
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -DNDEBUG -fPIC")
291293
endif()
292-
add_compile_options(-march=native -Wall -fno-builtin-malloc -fno-builtin-calloc -fno-builtin-realloc -fno-builtin-free -fopenmp -fopenmp-simd -funroll-loops -Wfatal-errors -DUSE_AVX2)
293294
endif()
294295

295296
add_subdirectory(src)

pyproject.toml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,22 @@ requires = [
33
"setuptools>=59.6",
44
"pybind11>=2.10.0",
55
"cmake>=3.22",
6-
"numpy>=1.21",
6+
"numpy==1.25", # this is important to keep fixed. It also means anyone using something other than 1.25 won't be able to use this library
77
"wheel",
88
"ninja"
99
]
1010
build-backend = "setuptools.build_meta"
1111

1212
[project]
1313
name = "diskannpy"
14-
version = "0.5.0.rc2"
14+
version = "0.5.0.rc4"
1515

1616
description = "DiskANN Python extension module"
17-
# readme = "../README.md"
18-
requires-python = ">=3.8"
17+
readme = "README.md"
18+
requires-python = ">=3.9"
1919
license = {text = "MIT License"}
2020
dependencies = [
21-
"numpy"
21+
"numpy==1.25"
2222
]
2323
authors = [
2424
{name = "Harsha Vardhan Simhadri", email = "[email protected]"},

python/README.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# diskannpy
2+
3+
## Installation
4+
Packages published to PyPI will always be built using the latest numpy major.minor release (at this time, 1.25).
5+
6+
Conda distributions for versions 1.19-1.25 will be completed as a future effort. In the meantime, feel free to
7+
clone this repository and build it yourself.
8+
9+
## Local Build Instructions
10+
Please see the [Project README](https://github.com/microsoft/DiskANN/blob/main/README.md) for system dependencies and requirements.
11+
12+
After ensuring you've followed the directions to build the project library and executables, you will be ready to also
13+
build `diskannpy` with these additional instructions.
14+
15+
### Changing Numpy Version
16+
In the root folder of DiskANN, there is a file `pyproject.toml`. You will need to edit the version of numpy in both the
17+
`[build-system.requires]` section, as well as the `[project.dependencies]` section. The version numbers must match.
18+
19+
```bash
20+
python3.11 -m venv venv # versions from python3.8 and up should work. on windows, you might need to use py -3.11 -m venv venv
21+
source venv/bin/activate # linux
22+
# or
23+
venv\Scripts\Activate.{ps1, bat} # windows
24+
pip install build
25+
python -m build
26+
```
27+
28+
The built wheel will be placed in the `dist` directory in your DiskANN root. Install it using `pip install dist/<wheel name>.whl`
29+
30+
## Citations
31+
Please cite this software in your work as:
32+
```
33+
@misc{diskann-github,
34+
author = {Simhadri, Harsha Vardhan and Krishnaswamy, Ravishankar and Srinivasa, Gopal and Subramanya, Suhas Jayaram and Antonijevic, Andrija and Pryce, Dax and Kaczynski, David and Williams, Shane and Gollapudi, Siddarth and Sivashankar, Varun and Karia, Neel and Singh, Aditi and Jaiswal, Shikhar and Mahapatro, Neelam and Adams, Philip and Tower, Bryan}},
35+
title = {{DiskANN: Graph-structured Indices for Scalable, Fast, Fresh and Filtered Approximate Nearest Neighbor Search}},
36+
url = {https://github.com/Microsoft/DiskANN},
37+
version = {0.5},
38+
year = {2023}
39+
}
40+
```

0 commit comments

Comments
 (0)