Skip to content

Conversation

@tracywong117
Copy link
Contributor

@tracywong117 tracywong117 commented Oct 3, 2025

Summary

Fix excessive memory usage (3x index size) in IndexBinaryHash search operations caused by pass-by-value semantics in variadic template dispatch.

Problem

When searching a 114GB IndexBinaryHash index, RAM usage peaked at ~342GB.
Root cause:

  • dispatch_HammingComputer used Types... args (pass-by-value)
  • Run_search_single_query::f used Types... args (pass-by-value)

This caused arguments to be copied for each instantiation, inflating memory usage.

Solution

  • faiss/IndexBinaryHash.cpp: Update Run_search_single_query call sites to pass pointers to arguments.

Testing

  • Tested with 114GB IndexBinaryHash index
  • RAM usage reduced from ~342GB to ~114GB (expected size)
  • Search results remain identical
  • No performance regression observed

Backward Compatibility

  • Function name and external API remain stable for Faiss users
  • Behavior unchanged except reduced memory footprint during search

@meta-cla
Copy link

meta-cla bot commented Oct 3, 2025

Hi @tracywong117!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at [email protected]. Thanks!

@meta-cla
Copy link

meta-cla bot commented Oct 3, 2025

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

@meta-cla meta-cla bot added the CLA Signed label Oct 3, 2025
@alexanderguzhva
Copy link
Contributor

@tracywong117 great, thanks! If you believe that there will be no side effects from this change, then please feel free to replace the original dispatch version with a new one without creating an additional facility

@mdouze
Copy link
Contributor

mdouze commented Oct 8, 2025

Hi @tracywong117
Thanks for catching this bug!
The preferred fix would be to pass pointers rather than making a new dispatching function. This would make the pass-by-value vs. pass-by-reference more explicit than the _perfect fix in the version that you suggest.
Thanks!

@tracywong117
Copy link
Contributor Author

tracywong117 commented Oct 8, 2025

Hi @mdouze @alexanderguzhva

Thanks for the feedback! I'll update the PR to use pointers instead of perfect forwarding. Will push the changes shortly.

@meta-codesync
Copy link
Contributor

meta-codesync bot commented Oct 13, 2025

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

@junjieqi
Copy link
Contributor

@tracywong117 It is an awesome fix! Thank for making the change! Would you mind fixing the format issue so we could merge the code? Thank!

@meta-codesync
Copy link
Contributor

meta-codesync bot commented Oct 14, 2025

@junjieqi merged this pull request in 041ac84.

AlSchlo pushed a commit to AlSchlo/faiss-panorama that referenced this pull request Oct 20, 2025
… dispatch_HammingComputer (facebookresearch#4600)

Summary:
Fixes excessive memory usage (3x index size) in IndexBinaryHash search operations caused by pass-by-value semantics in variadic template dispatch.

## Problem
When searching a 114GB IndexBinaryHash index, RAM usage peaked at ~342GB (3x index size) due to argument copying in the template dispatch mechanism:
- `dispatch_HammingComputer` uses `Types... args` (pass-by-value)
- `Run_search_single_query::f` uses `Types... args` (pass-by-value)
- This causes all arguments to be copied at each template instantiation

## Solution
1. **Added `dispatch_HammingComputer_perfect_forward`** in `utils/hamming_distance/hamdis-inl.h`: New function using perfect forwarding of arguments
2. **Fixed `Run_search_single_query`** in `IndexBinaryHash.cpp`: Changed to use perfect forwarding

## Testing
- Tested with 114GB IndexBinaryHash index
- RAM usage reduced from ~342GB to ~114GB (expected size)
- Search results remain identical
- No performance regression observed

## Backward Compatibility
- Original `dispatch_HammingComputer` remains unchanged
- No breaking changes to public API

Pull Request resolved: facebookresearch#4600

Reviewed By: mnorris11

Differential Revision: D84538582

Pulled By: junjieqi

fbshipit-source-id: 2e63e3b2ff3e8b37f7ffe919801d93bf941c3533
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.

5 participants