Skip to content

reorganize the scalar quantizer code for dynamic dispatch#4849

Open
mdouze wants to merge 2 commits into
facebookresearch:mainfrom
mdouze:export-D94588394
Open

reorganize the scalar quantizer code for dynamic dispatch#4849
mdouze wants to merge 2 commits into
facebookresearch:mainfrom
mdouze:export-D94588394

Conversation

@mdouze
Copy link
Copy Markdown
Contributor

@mdouze mdouze commented Feb 27, 2026

Summary:
This version introduces a cleaner separation between the SIMD templatized code.
It also avoids multiple levels of indirection that existed in the original diff.

The dispatching functions are now in sq-dispatch.h, which is piloted by a macro that tells it which SIMD level to instanciate.

ScalarQuantizer.cpp instanciates level NONE and the sq-XXX.cpp versions instanciate the other SIMD levels.

Differential Revision: D94588394

…spatch

Summary:
Split the SIMD-gated template specializations out of ScalarQuantizer.cpp
into per-SIMD compilation units and wire up the Dynamic Dispatch (DD)
infrastructure (`COMPILE_SIMD_*`, `with_simd_level`, `DISPATCH_SIMDLevel`).

This follows the established pattern from `pq_code_distance/` and
`distances/`.

**New files:**
- `sq_impl.h` — declares `sq_select_quantizer<SL>`,
  `sq_select_distance_computer<SL>`, `sq_select_InvertedListScanner<SL>`
- `sq-inl.h` — private implementation header with shared template bodies
  (`select_quantizer_1_body`, `select_distance_computer_body`,
  `select_InvertedListScanner_body`) and scanner class templates
  (`IVFSQScannerIP`, `IVFSQScannerL2`)
- `sq-generic.cpp` — `SIMDLevel::NONE` specializations (always compiled)
- `sq-avx2.cpp` — `SIMDLevel::AVX2` specializations (`d%8` alignment)
- `sq-avx512.cpp` — `SIMDLevel::AVX512` + `AVX512_SPR` forwarding
- `sq-neon.cpp` — `SIMDLevel::ARM_NEON` specializations (`d%8` alignment)

**Modified files:**
- `ScalarQuantizer.cpp` — rewritten to use `with_simd_level` dispatch
  with nullptr-fallback to NONE
- `quantizers.h`, `distance_computers.h` — lint formatting only
- `xplat.bzl`, `CMakeLists.txt` — register new SIMD files and headers

Each per-SIMD factory returns `nullptr` when the dimension doesn't align,
and the caller falls back to NONE. This avoids ODR issues from
instantiating `<NONE>` templates in multiple TUs.

The sub-headers (codecs.h, quantizers.h, similarities.h,
distance_computers.h) keep their original compiler-defined guards
(`__AVX512F__`, `__AVX2__`, `USE_NEON`, etc.) because `COMPILE_SIMD_*`
macros are globally visible in DD mode but the SIMD intrinsics are only
available in per-SIMD TUs. The `USE_*` macros are now defined in
`sq-inl.h`.

Differential Revision: D94375408
@meta-codesync
Copy link
Copy Markdown
Contributor

meta-codesync Bot commented Feb 27, 2026

@mdouze has exported this pull request. If you are a Meta employee, you can view the originating Diff in D94588394.

mdouze added a commit to mdouze/faiss that referenced this pull request Feb 27, 2026
…search#4849)

Summary:
Pull Request resolved: facebookresearch#4849

This version introduces a cleaner separation between the SIMD templatized code.
It also avoids multiple levels of indirection that existed in the original diff.

The dispatching functions are now in sq-dispatch.h, which is piloted by a macro that tells it which SIMD level to instanciate.

ScalarQuantizer.cpp instanciates level NONE and the sq-XXX.cpp versions instanciate the other SIMD levels.

Differential Revision: D94588394
mdouze added a commit to mdouze/faiss that referenced this pull request Mar 2, 2026
…search#4849)

Summary:
Pull Request resolved: facebookresearch#4849

This version introduces a cleaner separation between the SIMD templatized code.
It also avoids multiple levels of indirection that existed in the original diff.

The dispatching functions are now in sq-dispatch.h, which is piloted by a macro that tells it which SIMD level to instanciate.

ScalarQuantizer.cpp instanciates level NONE and the sq-XXX.cpp versions instanciate the other SIMD levels.

Differential Revision: D94588394
@mdouze mdouze force-pushed the export-D94588394 branch from dfecf79 to 3afd3e7 Compare March 2, 2026 07:44
mdouze added a commit to mdouze/faiss that referenced this pull request Mar 2, 2026
…search#4849)

Summary:
Pull Request resolved: facebookresearch#4849

This version introduces a cleaner separation between the SIMD templatized code.
It also avoids multiple levels of indirection that existed in the original diff.

The dispatching functions are now in sq-dispatch.h, which is piloted by a macro that tells it which SIMD level to instanciate.

ScalarQuantizer.cpp instanciates level NONE and the sq-XXX.cpp versions instanciate the other SIMD levels.

Differential Revision: D94588394
@mdouze mdouze force-pushed the export-D94588394 branch from 3afd3e7 to 8ec29c9 Compare March 2, 2026 07:48
mdouze added a commit to mdouze/faiss that referenced this pull request Mar 2, 2026
…search#4849)

Summary:
Pull Request resolved: facebookresearch#4849

This version introduces a cleaner separation between the SIMD templatized code.
It also avoids multiple levels of indirection that existed in the original diff.

The dispatching functions are now in sq-dispatch.h, which is piloted by a macro that tells it which SIMD level to instanciate.

ScalarQuantizer.cpp instanciates level NONE and the sq-XXX.cpp versions instanciate the other SIMD levels.

Differential Revision: D94588394
@mdouze mdouze force-pushed the export-D94588394 branch from 8ec29c9 to f17636d Compare March 2, 2026 08:50
…search#4849)

Summary:
Pull Request resolved: facebookresearch#4849

This version introduces a cleaner separation between the SIMD templatized code.
It also avoids multiple levels of indirection that existed in the original diff.

The dispatching functions are now in sq-dispatch.h, which is piloted by a macro that tells it which SIMD level to instanciate.

ScalarQuantizer.cpp instanciates level NONE and the sq-XXX.cpp versions instanciate the other SIMD levels.

Differential Revision: D94588394
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.

3 participants