Skip to content

Score PQ code pairs in one native call for every subspace width - #850

Draft
tlwillke wants to merge 2 commits into
mainfrom
native-pq-pair-direct
Draft

tlwillke wants to merge 2 commits into
mainfrom
native-pq-pair-direct

Conversation

@tlwillke

Copy link
Copy Markdown
Collaborator

Fixes #845. Native graph builds from saved PQ codes slowed down about 60% in #836, which moved diversity scoring from a
centroid-pair table to reading the codebooks in Java.

  • The native provider scores a code pair in one call to a new kernel, pq_code_pair_f32, from a copy of the codebooks
    in which every centroid is zero-padded to one even width. One loop covers every width, so odd and mixed widths are
    fast too. No per-pair allocation.
  • When every subspace is at least 16 floats wide, it scores from a centroid-pair table instead, which is faster there
    and small (such models have few subspaces). The table is built once, belongs to the model and counts in ramBytesUsed().
  • 1-float subspaces keep the Java gather loop. Saved and in-progress codes take the same path. Panama is unchanged.

Code-pair scoring from saved codes, native, GNR (48 cores), JDK 22 (ns per pair, lower is better):

Shape (dims/subspaces) Compression #833 main this PR
128/128 4x 258-268 95-119 94-95
384/128 12x 264-283 543-544 120-124
768/256 12x 540-544 1163-1168 241-258
100/32 12.5x 74-103 144-145 45-54
1024/128 32x 264-317 386-436 166-195
1024/64 64x 130-139 224-227 131-137
1024/16 256x 39-42 101-149 34-39

With in-progress codes this PR is 3-14x faster than #833 and as fast as or faster than main at every shape measured.
With the native kernels capped at AVX2, the table still loses at 8-float subspaces (1024/128: 281-302 ns against
192-194 from the codebooks) and ties or wins from 16 floats, so the cutoff is 16 floats for every ISA.

cohere-english-v3-1M, 128 subspaces of 8 floats, on-disk build, native: 25-31 s on GNR (48 cores) against 40-52 s on
main and 27-30 s at #833; pinned to 8 cores 97-111 s against 202-220 s on main and 106 s at #833. Recall unchanged.

Tests: PQ, retained-memory and native small-vector tests pass on JDK 22 and 25, native and Panama.
TestProductQuantization covers odd, mixed, wide and few-subspace shapes, and that the table follows the provider and
is counted. PQDiversityBenchmark (JMH) times code-pair scoring for saved and in-progress codes, including the shapes above.

Native scores code pairs from zero-padded codebooks in one loop, or from a
centroid-pair table when every subspace is at least 16 floats. Fixes #845.
Also corrects the #836 note: native keeps the pair table for wide subspaces.
@github-actions

Copy link
Copy Markdown
Contributor

Before you submit for review:

  • Does your PR follow guidelines from CONTRIBUTIONS.md?
  • Did you summarize what this PR does clearly and concisely?
  • Did you include performance data for changes which may be performance impacting?
  • Did you include useful docs for any user-facing changes or features?
  • Did you include useful javadocs for developer oriented changes, explaining new concepts or key changes?
  • Did you rebase your branch onto the latest main for regression testing and PR submission?
  • Did you trigger regression testing via Run Bench Main and review results?
  • Did you adhere to the code formatting guidelines (TBD)
  • Did you group your changes for easy review, providing meaningful descriptions for each commit?
  • Did you ensure that all files contain the correct copyright header?
  • Did you add documentation for this feature to the release notes directory?

If you did not complete any of these, then please explain below.

This branch has not been deployed

No deployments
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.

Native PQ index build ~1.8x slower since #836 (wide subspaces, e.g. cohere-1M)

1 participant