Skip to content

Route Flat/RCQ sgemm_ through cblas_sgemm to reach OpenBLAS SME (opt-in) - #5617

Open
adit4443ya wants to merge 1 commit into
facebookresearch:mainfrom
adit4443ya:sme-wins-only
Open

adit4443ya wants to merge 1 commit into
facebookresearch:mainfrom
adit4443ya:sme-wins-only

Conversation

@adit4443ya

Copy link
Copy Markdown

Summary:
Adds a compile-time-guarded path (FAISS_SME_CBLAS_SGEMM) that redirects
FAISS's Fortran sgemm_("Transpose", "Not transpose", ...) calls at the two
BLAS-dominated hot sites through OpenBLAS's cblas_sgemm entry point. Only
the CBLAS entry reaches OpenBLAS's SME direct-sgemm dispatch gate
(interface/gemm.c); the Fortran sgemm_ entry never does. This lets FAISS's
matrix multiplies land on the Arm SME matrix engine (fmopa) on SME-capable
hardware, with zero OpenBLAS source edits.

The macro is opt-in and a strict no-op when undefined: the #else branch is
byte-identical to the previous Fortran sgemm_ call, so builds without the
flag (and all non-SME hardware) are unaffected.

Measured on Arm Oryon-class SME hardware, single-thread
BLAS, both NDK-clang-21 and clang-24, correctness-gated:

  • Flat brute-force search (IndexFlat::search): 1.4x-2.8x, growing with GEMM
    size (2.0-2.2x at d=128/nb=16384; 2.8x at d>=512).
  • RCQ (ResidualCoarseQuantizer) training: 1.47x-1.98x, growing with d.

Implementation:

  • faiss/utils/distances.cpp: declares cblas_sgemm in the existing extern-C
    block (blasint is int in the linked OpenBLAS; CBLAS enums are ints, so no
    vendor header is needed, matching how sgemm_ is already hand-declared);
    adds an anonymous-namespace helper sme_cblas_sgemm_tn that performs the
    Fortran-column-major -> row-major translation (transpose-pack A into a tight
    [K][M] scratch; B is already the row-major left operand); and guards the
    exhaustive_inner_product_blas call site (drives the Flat win).
  • faiss/utils/simd_impl/distances_arm_sve.cpp: same declaration + helper, and
    guards the exhaustive_L2sqr_blas_cmax<ARM_SVE> call site (drives the RCQ
    training win). Adds the <vector> include the helper needs.

Only these two call sites are converted -- the two sites shown to drive the
confirmed wins. Other sgemm_ sites are left on the unchanged Fortran path.

Summary:
Adds a compile-time-guarded path (`FAISS_SME_CBLAS_SGEMM`) that redirects
FAISS's Fortran `sgemm_("Transpose", "Not transpose", ...)` calls at the two
BLAS-dominated hot sites through OpenBLAS's `cblas_sgemm` entry point. Only
the CBLAS entry reaches OpenBLAS's SME direct-sgemm dispatch gate
(`interface/gemm.c`); the Fortran `sgemm_` entry never does. This lets FAISS's
matrix multiplies land on the Arm SME matrix engine (fmopa) on SME-capable
hardware, with zero OpenBLAS source edits.

The macro is opt-in and a strict no-op when undefined: the `#else` branch is
byte-identical to the previous Fortran `sgemm_` call, so builds without the
flag (and all non-SME hardware) are unaffected.
@meta-cla

meta-cla Bot commented Sep 9, 2026

Copy link
Copy Markdown

Hi @adit4443ya!

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 cla@meta.com. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant