Commit 8431e04
Replace squared-distance IP with direct dot-product in multi-bit RaBitQ (#4877)
Summary:
Pull Request resolved: #4877
Refactor the multi-bit RaBitQ inner product computation from the
squared-distance-then-convert approach to a direct dot-product formulation.
Before: IP = -0.5 * (||q-c||² + (||r||² - ||x||²) + (-2·||r||/ipnorm)·ex_ip - ||q||²)
After: IP = <q,c> + <c,r> + (||r||/ipnorm)·ex_ip
Both are mathematically equivalent to <q, x>. The new form is simpler and
makes the code structurally immune to the D95166460 bug class where
the degenerate case (x ≈ centroid) required complex metric-specific
branching that was easy to get wrong.
Changes:
- Per-document factors (compute_ex_factors): IP branch now computes
f_add_ex = <c, r> and f_rescale_ex = ||r||/ipnorm (positive, no -2 factor)
- Degenerate case simplified to metric-agnostic f_add_ex=0, f_rescale_ex=0
- Core distance function (compute_full_multibit_distance): accepts a single
qr_base parameter instead of qr_to_c_L2sqr + qr_norm_L2sqr, eliminates
the -0.5*(dist - qr_norm_L2sqr) IP post-processing
- Added q_dot_c field to QueryFactorsData, computed at all query-setup sites
- L2 path, 1-bit path, and SIMD kernels are completely unchanged
- All four index types covered: IndexRaBitQ, IndexIVFRaBitQ,
IndexRaBitQFastScan, IndexIVFRaBitQFastScan
Breaking change: serialized multi-bit IP indexes must be re-encoded.
L2 indexes are unaffected.
Reviewed By: ddrcoder, latham-meta
Differential Revision: D95419974
fbshipit-source-id: 3a5a33b5a3065d172a2f57578f3a78bdf562b87c1 parent 471ddad commit 8431e04
7 files changed
Lines changed: 81 additions & 40 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
283 | 283 | | |
284 | 284 | | |
285 | 285 | | |
286 | | - | |
287 | 286 | | |
288 | 287 | | |
289 | 288 | | |
| 289 | + | |
| 290 | + | |
290 | 291 | | |
291 | 292 | | |
292 | 293 | | |
| |||
813 | 814 | | |
814 | 815 | | |
815 | 816 | | |
816 | | - | |
817 | | - | |
| 817 | + | |
| 818 | + | |
| 819 | + | |
818 | 820 | | |
819 | 821 | | |
820 | 822 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
751 | 751 | | |
752 | 752 | | |
753 | 753 | | |
754 | | - | |
755 | | - | |
| 754 | + | |
| 755 | + | |
| 756 | + | |
756 | 757 | | |
757 | 758 | | |
758 | 759 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
244 | 244 | | |
245 | 245 | | |
246 | 246 | | |
| 247 | + | |
247 | 248 | | |
248 | 249 | | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
249 | 253 | | |
250 | 254 | | |
251 | 255 | | |
| |||
307 | 311 | | |
308 | 312 | | |
309 | 313 | | |
310 | | - | |
311 | | - | |
| 314 | + | |
312 | 315 | | |
313 | 316 | | |
314 | 317 | | |
| |||
317 | 320 | | |
318 | 321 | | |
319 | 322 | | |
320 | | - | |
| 323 | + | |
321 | 324 | | |
322 | | - | |
323 | | - | |
324 | | - | |
| 325 | + | |
325 | 326 | | |
326 | 327 | | |
327 | 328 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
70 | 70 | | |
71 | 71 | | |
72 | 72 | | |
| 73 | + | |
73 | 74 | | |
74 | 75 | | |
75 | 76 | | |
| |||
320 | 321 | | |
321 | 322 | | |
322 | 323 | | |
323 | | - | |
324 | | - | |
| 324 | + | |
325 | 325 | | |
326 | 326 | | |
327 | 327 | | |
| |||
332 | 332 | | |
333 | 333 | | |
334 | 334 | | |
335 | | - | |
336 | | - | |
| 335 | + | |
337 | 336 | | |
338 | 337 | | |
339 | 338 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
314 | 314 | | |
315 | 315 | | |
316 | 316 | | |
| 317 | + | |
| 318 | + | |
| 319 | + | |
317 | 320 | | |
318 | 321 | | |
319 | 322 | | |
320 | 323 | | |
321 | 324 | | |
322 | | - | |
323 | | - | |
| 325 | + | |
324 | 326 | | |
325 | 327 | | |
326 | 328 | | |
| |||
366 | 368 | | |
367 | 369 | | |
368 | 370 | | |
| 371 | + | |
| 372 | + | |
369 | 373 | | |
370 | 374 | | |
371 | 375 | | |
| |||
480 | 484 | | |
481 | 485 | | |
482 | 486 | | |
| 487 | + | |
| 488 | + | |
| 489 | + | |
483 | 490 | | |
484 | 491 | | |
485 | 492 | | |
486 | 493 | | |
487 | 494 | | |
488 | | - | |
489 | | - | |
| 495 | + | |
490 | 496 | | |
491 | 497 | | |
492 | 498 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
216 | 216 | | |
217 | 217 | | |
218 | 218 | | |
219 | | - | |
220 | | - | |
221 | | - | |
222 | | - | |
223 | | - | |
224 | | - | |
225 | | - | |
226 | | - | |
227 | | - | |
228 | | - | |
229 | | - | |
230 | | - | |
231 | | - | |
232 | | - | |
233 | | - | |
234 | | - | |
235 | | - | |
236 | | - | |
237 | | - | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
238 | 226 | | |
239 | 227 | | |
240 | 228 | | |
| |||
276 | 264 | | |
277 | 265 | | |
278 | 266 | | |
279 | | - | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
280 | 270 | | |
281 | 271 | | |
282 | 272 | | |
283 | 273 | | |
284 | | - | |
| 274 | + | |
285 | 275 | | |
286 | 276 | | |
287 | 277 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
965 | 965 | | |
966 | 966 | | |
967 | 967 | | |
| 968 | + | |
| 969 | + | |
| 970 | + | |
| 971 | + | |
| 972 | + | |
| 973 | + | |
| 974 | + | |
| 975 | + | |
| 976 | + | |
| 977 | + | |
| 978 | + | |
| 979 | + | |
| 980 | + | |
| 981 | + | |
| 982 | + | |
| 983 | + | |
| 984 | + | |
| 985 | + | |
| 986 | + | |
| 987 | + | |
| 988 | + | |
| 989 | + | |
| 990 | + | |
| 991 | + | |
| 992 | + | |
| 993 | + | |
| 994 | + | |
| 995 | + | |
| 996 | + | |
| 997 | + | |
| 998 | + | |
| 999 | + | |
| 1000 | + | |
| 1001 | + | |
| 1002 | + | |
| 1003 | + | |
| 1004 | + | |
| 1005 | + | |
| 1006 | + | |
| 1007 | + | |
| 1008 | + | |
| 1009 | + | |
968 | 1010 | | |
969 | 1011 | | |
970 | 1012 | | |
| |||
0 commit comments