Skip to content

Add support for IndexIDMap with Cagra fp16#4411

Closed
jinsolp wants to merge 24 commits intofacebookresearch:mainfrom
jinsolp:add-fea-cuvs-cagra-fp16
Closed

Add support for IndexIDMap with Cagra fp16#4411
jinsolp wants to merge 24 commits intofacebookresearch:mainfrom
jinsolp:add-fea-cuvs-cagra-fp16

Conversation

@jinsolp
Copy link
Contributor

@jinsolp jinsolp commented Jun 25, 2025

This PR does 2 things

@jinsolp
Copy link
Contributor Author

jinsolp commented Jun 26, 2025

One of the issues that I've noticed is that the current extended API approach results in a lot of warnings from all child structs at compile time. This is because we have an overload of a function (e.g. add(n, x) and add(n, x, faiss.Float16)) in the base struct (Index.h), but the child structs only override one of these overloaded functions (because most of them don't bother about the latter add).

The straightforward solution is to add using Index::add; to every single child struct, but this seems very tedious... (and we will have to do it for every extended-api function)
Any suggestions to tackling this??

Copy link
Contributor

@mdouze mdouze left a comment

Choose a reason for hiding this comment

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

For this change to work without user visible degradation, we should implement implicit conversion fp16->fp32 in C++ for add/search/train. There are a few fp16 routines for CPU here: https://github.com/facebookresearch/faiss/blob/main/faiss/utils/fp16.h

Copy link
Contributor

@mdouze mdouze left a comment

Choose a reason for hiding this comment

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

makes me think that the codepath to provide fp16 or bf16 pytorch arrays is here:

assert type(x) is torch.Tensor

these have not been adapted to the type-tagged add/train/search variants.

@jinsolp
Copy link
Contributor Author

jinsolp commented Jul 11, 2025

@mdouze Thanks for your feedback! I have made a few changes : )

  • Renamed all extended APIs with the Ex postfix because of cpp compile warnings related to not overriding overloaded functions.
  • Added the numeric_type tag support for torch_utils.py

@mdouze
Copy link
Contributor

mdouze commented Jul 18, 2025

I don't understand the rationale for the Ex suffix. Where does the cpp compiler issue warnings?

@jinsolp
Copy link
Contributor Author

jinsolp commented Jul 18, 2025

@mdouze In the current CI runs for cuVS build, it can be seen that it raises a warning for every single index https://github.com/facebookresearch/faiss/actions/runs/16378195325/job/46284306200

e.g. overloaded virtual function "faiss::Index::train" is only partially overridden in class "faiss::IndexPQ"

I've stated the reason for this in the comment above

One of the issues that I've noticed is that the current extended API approach results in a lot of warnings from all child structs at compile time. This is because we have an overload of a function (e.g. add(n, x) and add(n, x, faiss.Float16)) in the base struct (Index.h), but the child structs only override one of these overloaded functions (because most of them don't bother about the latter add).
The straightforward solution is to add using Index::add; to every single child struct, but this seems very tedious... (and we will have to do it for every extended-api function)

TLDR; warning happen because most child structs don't override the overloaded functions (i.e. most child structs don't override the extended API).

There are two ways to tackle this cpp warning

  1. add using in every single index struct so that it can see the base Index struct's overloaded functions with numeric types (the extended functions)
  • pro: we have the same name: the original add and the add with void* and numeric_type.
  • con: we have to change every single index in FAISS
  1. change the name of the extended functions so that they are not an "overload". i.e. instead of having two add functions with different signatures, just have add and addEx
  • pro: we only need to change a few files where we are using the extended API
  • con: we have the *Ex postfix for extended API

@mnorris11
Copy link

Sorry for delayed responses. At least to me, it does not seem like a problem to annotate this 1 line on all the indexes that require it, if the API stays as add(), etc. It looks cleaner and keeps with the current pattern.

@jinsolp
Copy link
Contributor Author

jinsolp commented Jul 21, 2025

Got it, thanks @mnorris11 , I'll fix this today and push : )

@facebook-github-bot
Copy link
Contributor

@mnorris11 has imported this pull request. If you are a Meta employee, you can view this in D78695771.

@jinsolp
Copy link
Contributor Author

jinsolp commented Jul 21, 2025

@mnorris11 Now the diff is really big, but we are 1) keeping the previous API (without the Ex) and 2) compiling without warnings.

swig wasn't able to process macros or work with using. So what I've done is manually override functions if the child struct is already overriding its original counterpart. (i.e. if the child class is already overriding add() I also overrode add(void*, numeric_type).
All overrode functions are just forwarded to its base struct's function to keep the logic consistent as before.

@facebook-github-bot
Copy link
Contributor

@mnorris11 has imported this pull request. If you are a Meta employee, you can view this in D78695771.

@facebook-github-bot
Copy link
Contributor

@mnorris11 merged this pull request in 71f775b.

@jinsolp jinsolp deleted the add-fea-cuvs-cagra-fp16 branch July 22, 2025 17:53
facebook-github-bot pushed a commit that referenced this pull request Jul 25, 2025
Summary:
Closes #4430 by exposing int8 for cuVS cagra.
This PR (#4411) needs to be merged before this one .

Pull Request resolved: #4439

Reviewed By: junjieqi

Differential Revision: D78746656

Pulled By: mnorris11

fbshipit-source-id: 79147c8196f298c443796a615fabefac34fb188d
dian-lun-lin pushed a commit to ahuber21/faiss that referenced this pull request Jul 30, 2025
Summary:
This PR does 2 things
- Enable support fot `IndexIDMap` with Cagra fp16 (original support introduced in facebookresearch#4188)
    - Added tests in `test_cagra.py`
- Reflecting feedback about python API from facebookresearch#4384 (comment)

Pull Request resolved: facebookresearch#4411

Reviewed By: junjieqi

Differential Revision: D78695771

Pulled By: mnorris11

fbshipit-source-id: 4b3a0869bed5d33165354f415c748812b0d4b253
dian-lun-lin pushed a commit to ahuber21/faiss that referenced this pull request Jul 30, 2025
Summary:
Closes facebookresearch#4430 by exposing int8 for cuVS cagra.
This PR (facebookresearch#4411) needs to be merged before this one .

Pull Request resolved: facebookresearch#4439

Reviewed By: junjieqi

Differential Revision: D78746656

Pulled By: mnorris11

fbshipit-source-id: 79147c8196f298c443796a615fabefac34fb188d
samanthawaters8882michaeldonovan added a commit to samanthawaters8882michaeldonovan/faiss that referenced this pull request Oct 12, 2025
Summary:
This PR does 2 things
- Enable support fot `IndexIDMap` with Cagra fp16 (original support introduced in facebookresearch/faiss#4188)
    - Added tests in `test_cagra.py`
- Reflecting feedback about python API from facebookresearch/faiss#4384 (comment)

Pull Request resolved: facebookresearch/faiss#4411

Reviewed By: junjieqi

Differential Revision: D78695771

Pulled By: mnorris11

fbshipit-source-id: 4b3a0869bed5d33165354f415c748812b0d4b253
samanthawaters8882michaeldonovan added a commit to samanthawaters8882michaeldonovan/faiss that referenced this pull request Oct 12, 2025
Summary:
Closes facebookresearch/faiss#4430 by exposing int8 for cuVS cagra.
This PR (facebookresearch/faiss#4411) needs to be merged before this one .

Pull Request resolved: facebookresearch/faiss#4439

Reviewed By: junjieqi

Differential Revision: D78746656

Pulled By: mnorris11

fbshipit-source-id: 79147c8196f298c443796a615fabefac34fb188d
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