Skip to content

Enable mmap-ped Faiss indices (IndexFlatCodes-based and HNSW)#996

Merged
sre-ci-robot merged 2 commits intozilliztech:mainfrom
alexanderguzhva:mmap
Dec 19, 2024
Merged

Enable mmap-ped Faiss indices (IndexFlatCodes-based and HNSW)#996
sre-ci-robot merged 2 commits intozilliztech:mainfrom
alexanderguzhva:mmap

Conversation

@alexanderguzhva
Copy link
Collaborator

No description provided.

@mergify
Copy link

mergify bot commented Dec 18, 2024

@alexanderguzhva 🔍 Important: PR Classification Needed!

For efficient project management and a seamless review process, it's essential to classify your PR correctly. Here's how:

  1. If you're fixing a bug, label it as kind/bug.
  2. For small tweaks (less than 20 lines without altering any functionality), please use kind/improvement.
  3. Significant changes that don't modify existing functionalities should be tagged as kind/enhancement.
  4. Adjusting APIs or changing functionality? Go with kind/feature.

For any PR outside the kind/improvement category, ensure you link to the associated issue using the format: “issue: #”.

Thanks for your efforts and contribution to the community!.

Signed-off-by: Alexandr Guzhva <alexanderguzhva@gmail.com>
Signed-off-by: Alexandr Guzhva <alexanderguzhva@gmail.com>
@mergify mergify bot added the ci-passed label Dec 18, 2024
@codecov
Copy link

codecov bot commented Dec 18, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 73.88%. Comparing base (3c46f4c) to head (8ea01d5).
Report is 273 commits behind head on main.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff            @@
##           main     #996       +/-   ##
=========================================
+ Coverage      0   73.88%   +73.88%     
=========================================
  Files         0       82       +82     
  Lines         0     6919     +6919     
=========================================
+ Hits          0     5112     +5112     
- Misses        0     1807     +1807     

see 82 files with indirect coverage changes


// set 'random' access pattern
// todo: check the error
madvise(address, filesize, MADV_RANDOM);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a warn log needs to be added here

@@ -512,7 +594,8 @@ static void read_HNSW(HNSW* hnsw, IOReader* f) {
READVECTOR(hnsw->cum_nneighbor_per_level);
READVECTOR(hnsw->levels);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't these require mmap? Although these only account for a small part

Copy link
Collaborator

@chasingegg chasingegg Dec 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same question, what do we exactly mmap for hnsw, seems neighbors and raw vectors/quant codes @alexanderguzhva

@foxspy
Copy link
Collaborator

foxspy commented Dec 19, 2024

/kind improvement

Copy link
Collaborator

@foxspy foxspy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/lgtm

@sre-ci-robot
Copy link
Collaborator

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: alexanderguzhva, foxspy

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@sre-ci-robot sre-ci-robot merged commit c24fd21 into zilliztech:main Dec 19, 2024
cqy123456 pushed a commit to cqy123456/zilliztech-knowhere that referenced this pull request Dec 31, 2024
…tech#996)

* HNSW_SQenable mmap-ped faiss indices (IndexFlatCodes-based and HNSW)

Signed-off-by: Alexandr Guzhva <alexanderguzhva@gmail.com>

* temporary add IO_FLAG_MMAP_IFC

Signed-off-by: Alexandr Guzhva <alexanderguzhva@gmail.com>

---------

Signed-off-by: Alexandr Guzhva <alexanderguzhva@gmail.com>
cqy123456 pushed a commit to cqy123456/zilliztech-knowhere that referenced this pull request Feb 5, 2025
…tech#996)

* HNSW_SQenable mmap-ped faiss indices (IndexFlatCodes-based and HNSW)

Signed-off-by: Alexandr Guzhva <alexanderguzhva@gmail.com>

* temporary add IO_FLAG_MMAP_IFC

Signed-off-by: Alexandr Guzhva <alexanderguzhva@gmail.com>

---------

Signed-off-by: Alexandr Guzhva <alexanderguzhva@gmail.com>
facebook-github-bot pushed a commit to facebookresearch/faiss that referenced this pull request Mar 11, 2025
Summary:
This PR introduces a backport of a combination of zilliztech/knowhere#996 and zilliztech/knowhere#1032 that allow to have memory-mapped and zerocopy indces.

The root underlying idea is that we replace certain `std::vector<>` containers with a custom `faiss::MaybeOwnedVector<>` container, which may behave either as `std::vector<>`, or as a view of a certain pointer / descriptor. We don't replace all the instances of `std::vector<>`, but the largest ones.

This change affects `IndexFlatCodes`-based and `IndexHNSW` CPU indices.

(done) alter IVF lists as well.
(done) alter binary indices as well.

Memory-mapped index works like this:
```C++
std::unique_ptr<faiss::Index> index_mm(
            faiss::read_index(filenamename.c_str(), faiss::IO_FLAG_MMAP_IFC));
```
In theory, it should be ready to be used from Python. All the descriptor management should be working.

Zero-copy index works like this:
```C++
#include <faiss/impl/zerocopy_io.h>

faiss::ZeroCopyIOReader reader(buffer.data(), buffer.size());
std::unique_ptr<faiss::Index> index_zc(faiss::read_index(&reader));
```
All the pointer management for `faiss::ZeroCopyIOReader` should be handled manually.
I'm not sure how to plug this into Python yet, maybe, some ref-counting is required.

(done) some refactoring

Pull Request resolved: #4199

Reviewed By: mengdilin

Differential Revision: D69972250

Pulled By: mdouze

fbshipit-source-id: 98a3f94d6884814873d3534ee25f960892ef1076
samanthawaters8882michaeldonovan added a commit to samanthawaters8882michaeldonovan/faiss that referenced this pull request Oct 12, 2025
Summary:
This PR introduces a backport of a combination of zilliztech/knowhere#996 and zilliztech/knowhere#1032 that allow to have memory-mapped and zerocopy indces.

The root underlying idea is that we replace certain `std::vector<>` containers with a custom `faiss::MaybeOwnedVector<>` container, which may behave either as `std::vector<>`, or as a view of a certain pointer / descriptor. We don't replace all the instances of `std::vector<>`, but the largest ones.

This change affects `IndexFlatCodes`-based and `IndexHNSW` CPU indices.

(done) alter IVF lists as well.
(done) alter binary indices as well.

Memory-mapped index works like this:
```C++
std::unique_ptr<faiss::Index> index_mm(
            faiss::read_index(filenamename.c_str(), faiss::IO_FLAG_MMAP_IFC));
```
In theory, it should be ready to be used from Python. All the descriptor management should be working.

Zero-copy index works like this:
```C++
#include <faiss/impl/zerocopy_io.h>

faiss::ZeroCopyIOReader reader(buffer.data(), buffer.size());
std::unique_ptr<faiss::Index> index_zc(faiss::read_index(&reader));
```
All the pointer management for `faiss::ZeroCopyIOReader` should be handled manually.
I'm not sure how to plug this into Python yet, maybe, some ref-counting is required.

(done) some refactoring

Pull Request resolved: facebookresearch/faiss#4199

Reviewed By: mengdilin

Differential Revision: D69972250

Pulled By: mdouze

fbshipit-source-id: 98a3f94d6884814873d3534ee25f960892ef1076
dimitraseferiadi pushed a commit to dimitraseferiadi/SuCo that referenced this pull request Mar 8, 2026
Summary:
This PR introduces a backport of a combination of zilliztech/knowhere#996 and zilliztech/knowhere#1032 that allow to have memory-mapped and zerocopy indces.

The root underlying idea is that we replace certain `std::vector<>` containers with a custom `faiss::MaybeOwnedVector<>` container, which may behave either as `std::vector<>`, or as a view of a certain pointer / descriptor. We don't replace all the instances of `std::vector<>`, but the largest ones.

This change affects `IndexFlatCodes`-based and `IndexHNSW` CPU indices.

(done) alter IVF lists as well.
(done) alter binary indices as well.

Memory-mapped index works like this:
```C++
std::unique_ptr<faiss::Index> index_mm(
            faiss::read_index(filenamename.c_str(), faiss::IO_FLAG_MMAP_IFC));
```
In theory, it should be ready to be used from Python. All the descriptor management should be working.

Zero-copy index works like this:
```C++
#include <faiss/impl/zerocopy_io.h>

faiss::ZeroCopyIOReader reader(buffer.data(), buffer.size());
std::unique_ptr<faiss::Index> index_zc(faiss::read_index(&reader));
```
All the pointer management for `faiss::ZeroCopyIOReader` should be handled manually.
I'm not sure how to plug this into Python yet, maybe, some ref-counting is required.

(done) some refactoring

Pull Request resolved: facebookresearch#4199

Reviewed By: mengdilin

Differential Revision: D69972250

Pulled By: mdouze

fbshipit-source-id: 98a3f94d6884814873d3534ee25f960892ef1076
dimitraseferiadi pushed a commit to dimitraseferiadi/SuCo that referenced this pull request Mar 16, 2026
Summary:
This PR introduces a backport of a combination of zilliztech/knowhere#996 and zilliztech/knowhere#1032 that allow to have memory-mapped and zerocopy indces.

The root underlying idea is that we replace certain `std::vector<>` containers with a custom `faiss::MaybeOwnedVector<>` container, which may behave either as `std::vector<>`, or as a view of a certain pointer / descriptor. We don't replace all the instances of `std::vector<>`, but the largest ones.

This change affects `IndexFlatCodes`-based and `IndexHNSW` CPU indices.

(done) alter IVF lists as well.
(done) alter binary indices as well.

Memory-mapped index works like this:
```C++
std::unique_ptr<faiss::Index> index_mm(
            faiss::read_index(filenamename.c_str(), faiss::IO_FLAG_MMAP_IFC));
```
In theory, it should be ready to be used from Python. All the descriptor management should be working.

Zero-copy index works like this:
```C++
#include <faiss/impl/zerocopy_io.h>

faiss::ZeroCopyIOReader reader(buffer.data(), buffer.size());
std::unique_ptr<faiss::Index> index_zc(faiss::read_index(&reader));
```
All the pointer management for `faiss::ZeroCopyIOReader` should be handled manually.
I'm not sure how to plug this into Python yet, maybe, some ref-counting is required.

(done) some refactoring

Pull Request resolved: facebookresearch#4199

Reviewed By: mengdilin

Differential Revision: D69972250

Pulled By: mdouze

fbshipit-source-id: 98a3f94d6884814873d3534ee25f960892ef1076
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants