Skip to content

fix(refine): normalize the refined cosine leaf reference - #382

Draft
pavanmanishd wants to merge 1 commit into
timescale:mainfrom
pavanmanishd:fix/refine-cosine-norm
Draft

pavanmanishd wants to merge 1 commit into
timescale:mainfrom
pavanmanishd:fix/refine-cosine-norm

Conversation

@pavanmanishd

@pavanmanishd pavanmanishd commented Oct 18, 2026 •

Copy link
Copy Markdown
Contributor

Refs #183.

Trying to reproduce the reported collapse on current main with the issue's dataset and settings, it does not reproduce: with refinement forced on, nytimes-256-angular at nprobe 320 with the full pool measures within half a percent of the unrefined build rather than the 0.9549 to 0.6411 the issue reports. What does reproduce is a smaller, systematic loss from refining, and it is the defect the issue suspected: the refine kernel wrote the refined mean without normalizing it for cosine, while every other centroid write path normalizes, so the head reference was not unit norm — which the Phase-A probe re-rank's squared-L2 ordering assumes.

With the normalization, refinement no longer loses to no-refinement at the sample budget measured (0.9413 to 0.9460 at nprobe 150, 0.9764 to 0.9806 at nprobe 320), and the unit tests pin the kernel invariant: the cosine case fails without it.

Note for reviewers: the maintainer's #353 covers the same normalization, writes both copies (posting head and centroid tree) and handles the zero mean better, so this is at most a test-only follow-up to it.

The page-backed leaf refinement replaces each leaf's encode reference with
the mean of the rows routed to it, but wrote that mean without normalizing
it for cosine. Every other centroid write path normalizes cosine leaf
centroids before writing them: k-means means drift below unit norm. Two
consumers depend on a unit-norm reference:

- the Phase-A probe re-rank orders the routed clusters by the squared L2
  distance between the rotated query and the rotated centroid, which is
  only a cosine ranking while both vectors are unit norm, so a reference
  with a smaller norm looks closer regardless of its direction; and
- the head reference centers every RaBitQ residual in the leaf, while the
  tree write and the parent centroid pages both carry unit-norm leaf
  references for cosine.

Measured on nytimes-256-angular (290k x 256d, cosine, maintenance_work_mem
= 64MB so the build is subsampled, 1000 queries, parallel build): with
refinement forced on, recall@10 at nprobe=150 goes from 0.9413 to 0.9460
and at nprobe=320 with the full rerank pool from 0.9764 to 0.9806, against
0.9440 and 0.9817 for the unrefined build.

The kernel takes a cosine flag and both callers pass their metric. New unit
tests pin the kernel's behavior: cosine means are written at unit norm with
their direction preserved, L2 means are untouched, empty leaves are skipped
and a mean that cancels to the origin stays finite.
@erimatnor

Copy link
Copy Markdown
Member

Looks like a duplicate of #353

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.

2 participants