Commit 1cb7601
Eliminate per-code denormalization in uniform SQ distance computation (#5166)
Summary:
This PR removes per-code denormalization from the inner loop of L2 and inner product distance computations for uniform scalar quantizers (`QT_8bit_uniform` and `QT_4bit_uniform`), yielding a speedup of up to **1.39x**.
For uniform integer scalar quantizers, `vmin` and `vdiff` are scalars shared across all dimensions. The reconstructed value for each component is therefore a function of a per-code decode `n` that depends only on the byte code:
```
x_hat = vmin + vdiff * n
```
This structure lets us factor `vmin` and `vdiff` out of the per-database-vector inner loop entirely, instead of recomputing the transform on every code on every distance evaluation.
**L2 distance.**
```
||q - x_hat||^2 = ||q - (vmin + vdiff * n)||^2
= vdiff^2 * ||(q - vmin) / vdiff - n||^2
```
We pre-adjust the query once in `set_query()` to `q_adj = (q - vmin) / vdiff` and precompute `scale = vdiff^2`. The hot loop then compares codes directly against `q_adj` in the codec's native decode space, applying `scale` exactly once at the end.
**Inner product.**
```
<q, x_hat> = sum_i q[i] * (vmin + vdiff * n[i])
= vmin * sum_i q[i] + vdiff * sum_i q[i] * n[i]
= bias + scale * <q, n>
```
We compute `bias = vmin * sum_i q[i]` and `scale = vdiff` once in `set_query()`. The hot loop accumulates the dot product against the raw decode `n` and applies `bias + scale` once at the end.
In both cases this removes one FMA (the `vmin + vdiff * n` denormalization) from the inner loop per 8 components per database vector. Beyond the raw FLOP reduction, shortening the dependency chain lets SIMD pipelines better overlap the decode of the next lane with the accumulator update of the previous lane.
**The Change**
```
Current This PR
-------- ---------
set_query(q): store q q_adj = (q - vmin) / vdiff [once]
per code (×N): raw = decode(bytes) raw = decode(bytes)
x = vmin + raw * vdiff ← gone diff = q_adj - raw
diff = q - x accu += diff^2
accu += diff^2
```
The optimization is gated on a C++20 `requires` check for a new `decode_8_raw()` method, defined only on the uniform `QuantizerTemplate` specializations. All other quantizer types fall through to the original `compute_distance` path unchanged.
**Speedup**
Below are the results from running `benchs/bench_scalar_quantizer.py` for a `dd` build on SPR, compared to the existing implementation. Similar results were observed for `avx-2` as well.
```
| | QT_4bit_uniform | QT_8bit_uniform |
|--------------|-----------------|-----------------|
| RS_minmax | 0.99x | 1.05x |
| RS_minmax | 1.07x | 1.03x |
| RS_minmax | 0.83x | 1.03x |
| RS_minmax | 0.96x | 1.05x |
| RS_minmax | 0.89x | 1.03x |
| RS_minmax | 1.14x | 1.03x |
| RS_minmax | 0.99x | 1.05x |
| RS_meanstd | 1.28x | 1.09x |
| RS_meanstd | 1.10x | 1.01x |
| RS_meanstd | 1.14x | 1.06x |
| RS_meanstd | 1.18x | 1.08x |
| RS_meanstd | 1.11x | 1.05x |
| RS_meanstd | 1.18x | 1.06x |
| RS_meanstd | 1.16x | 1.08x |
| RS_quantiles | 1.39x | 1.07x |
| RS_quantiles | 1.08x | 1.01x |
| RS_quantiles | 1.21x | 0.99x |
| RS_quantiles | 1.25x | 1.10x |
| RS_optim | 1.03x | 1.03x |
```
The raw performance results are available here: https://gist.github.com/mulugetam/7db50f89279bb270a1fe336206730d60
Pull Request resolved: #5166
Test Plan: Ran `python benchs/bench_scalar_quantizer.py` on SPR to validate performance results (see summary table). Ran `pytest tests/test_index_accuracy.py` to verify correctness is preserved for uniform SQ types (`QT_8bit_uniform`, `QT_4bit_uniform`).
Reviewed By: mdouze
Differential Revision: D106148760
Pulled By: mnorris11
fbshipit-source-id: 65769cfe9cf8d2d1ef5de05b6bdecf9dbee96d5c1 parent 34eb989 commit 1cb7601
4 files changed
Lines changed: 242 additions & 8 deletions
File tree
- benchs
- faiss/impl/scalar_quantizer
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
17 | 17 | | |
18 | 18 | | |
19 | 19 | | |
20 | | - | |
| 20 | + | |
21 | 21 | | |
22 | 22 | | |
23 | 23 | | |
| |||
67 | 67 | | |
68 | 68 | | |
69 | 69 | | |
70 | | - | |
| 70 | + | |
71 | 71 | | |
72 | 72 | | |
73 | 73 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
196 | 196 | | |
197 | 197 | | |
198 | 198 | | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
199 | 205 | | |
200 | 206 | | |
201 | 207 | | |
| |||
399 | 405 | | |
400 | 406 | | |
401 | 407 | | |
| 408 | + | |
| 409 | + | |
| 410 | + | |
| 411 | + | |
| 412 | + | |
| 413 | + | |
| 414 | + | |
| 415 | + | |
| 416 | + | |
| 417 | + | |
| 418 | + | |
| 419 | + | |
| 420 | + | |
| 421 | + | |
| 422 | + | |
| 423 | + | |
402 | 424 | | |
403 | 425 | | |
404 | 426 | | |
| |||
442 | 464 | | |
443 | 465 | | |
444 | 466 | | |
| 467 | + | |
| 468 | + | |
| 469 | + | |
| 470 | + | |
| 471 | + | |
| 472 | + | |
| 473 | + | |
| 474 | + | |
| 475 | + | |
| 476 | + | |
| 477 | + | |
| 478 | + | |
| 479 | + | |
| 480 | + | |
| 481 | + | |
| 482 | + | |
| 483 | + | |
445 | 484 | | |
446 | 485 | | |
447 | 486 | | |
| |||
454 | 493 | | |
455 | 494 | | |
456 | 495 | | |
| 496 | + | |
| 497 | + | |
| 498 | + | |
| 499 | + | |
| 500 | + | |
| 501 | + | |
| 502 | + | |
| 503 | + | |
| 504 | + | |
| 505 | + | |
| 506 | + | |
457 | 507 | | |
458 | | - | |
| 508 | + | |
| 509 | + | |
| 510 | + | |
| 511 | + | |
| 512 | + | |
459 | 513 | | |
460 | 514 | | |
461 | 515 | | |
| |||
484 | 538 | | |
485 | 539 | | |
486 | 540 | | |
| 541 | + | |
| 542 | + | |
| 543 | + | |
| 544 | + | |
| 545 | + | |
| 546 | + | |
| 547 | + | |
| 548 | + | |
| 549 | + | |
| 550 | + | |
| 551 | + | |
| 552 | + | |
| 553 | + | |
| 554 | + | |
| 555 | + | |
| 556 | + | |
| 557 | + | |
| 558 | + | |
| 559 | + | |
| 560 | + | |
487 | 561 | | |
488 | 562 | | |
489 | 563 | | |
| |||
492 | 566 | | |
493 | 567 | | |
494 | 568 | | |
495 | | - | |
| 569 | + | |
| 570 | + | |
| 571 | + | |
| 572 | + | |
| 573 | + | |
496 | 574 | | |
497 | 575 | | |
498 | 576 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
214 | 214 | | |
215 | 215 | | |
216 | 216 | | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
217 | 223 | | |
218 | 224 | | |
219 | 225 | | |
| |||
411 | 417 | | |
412 | 418 | | |
413 | 419 | | |
| 420 | + | |
| 421 | + | |
| 422 | + | |
| 423 | + | |
| 424 | + | |
| 425 | + | |
| 426 | + | |
| 427 | + | |
| 428 | + | |
| 429 | + | |
| 430 | + | |
| 431 | + | |
| 432 | + | |
| 433 | + | |
| 434 | + | |
| 435 | + | |
414 | 436 | | |
415 | 437 | | |
416 | 438 | | |
| |||
445 | 467 | | |
446 | 468 | | |
447 | 469 | | |
| 470 | + | |
| 471 | + | |
| 472 | + | |
| 473 | + | |
| 474 | + | |
| 475 | + | |
| 476 | + | |
| 477 | + | |
| 478 | + | |
| 479 | + | |
| 480 | + | |
| 481 | + | |
| 482 | + | |
| 483 | + | |
| 484 | + | |
| 485 | + | |
| 486 | + | |
448 | 487 | | |
449 | 488 | | |
450 | 489 | | |
| |||
458 | 497 | | |
459 | 498 | | |
460 | 499 | | |
| 500 | + | |
| 501 | + | |
| 502 | + | |
| 503 | + | |
| 504 | + | |
| 505 | + | |
| 506 | + | |
| 507 | + | |
| 508 | + | |
| 509 | + | |
| 510 | + | |
461 | 511 | | |
462 | | - | |
| 512 | + | |
| 513 | + | |
| 514 | + | |
| 515 | + | |
| 516 | + | |
463 | 517 | | |
464 | 518 | | |
465 | 519 | | |
| |||
485 | 539 | | |
486 | 540 | | |
487 | 541 | | |
| 542 | + | |
| 543 | + | |
| 544 | + | |
| 545 | + | |
| 546 | + | |
| 547 | + | |
| 548 | + | |
| 549 | + | |
| 550 | + | |
| 551 | + | |
| 552 | + | |
| 553 | + | |
| 554 | + | |
| 555 | + | |
| 556 | + | |
| 557 | + | |
| 558 | + | |
| 559 | + | |
| 560 | + | |
| 561 | + | |
488 | 562 | | |
489 | 563 | | |
490 | 564 | | |
| |||
493 | 567 | | |
494 | 568 | | |
495 | 569 | | |
496 | | - | |
| 570 | + | |
| 571 | + | |
| 572 | + | |
| 573 | + | |
| 574 | + | |
497 | 575 | | |
498 | 576 | | |
499 | 577 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
180 | 180 | | |
181 | 181 | | |
182 | 182 | | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
183 | 189 | | |
184 | 190 | | |
185 | 191 | | |
| |||
397 | 403 | | |
398 | 404 | | |
399 | 405 | | |
| 406 | + | |
| 407 | + | |
| 408 | + | |
| 409 | + | |
| 410 | + | |
| 411 | + | |
| 412 | + | |
| 413 | + | |
| 414 | + | |
| 415 | + | |
| 416 | + | |
| 417 | + | |
| 418 | + | |
| 419 | + | |
| 420 | + | |
| 421 | + | |
400 | 422 | | |
401 | 423 | | |
402 | 424 | | |
| |||
431 | 453 | | |
432 | 454 | | |
433 | 455 | | |
| 456 | + | |
| 457 | + | |
| 458 | + | |
| 459 | + | |
| 460 | + | |
| 461 | + | |
| 462 | + | |
| 463 | + | |
| 464 | + | |
| 465 | + | |
| 466 | + | |
| 467 | + | |
| 468 | + | |
| 469 | + | |
| 470 | + | |
| 471 | + | |
| 472 | + | |
434 | 473 | | |
435 | 474 | | |
436 | 475 | | |
| |||
444 | 483 | | |
445 | 484 | | |
446 | 485 | | |
| 486 | + | |
| 487 | + | |
| 488 | + | |
| 489 | + | |
| 490 | + | |
| 491 | + | |
| 492 | + | |
| 493 | + | |
| 494 | + | |
| 495 | + | |
| 496 | + | |
447 | 497 | | |
448 | | - | |
| 498 | + | |
| 499 | + | |
| 500 | + | |
| 501 | + | |
| 502 | + | |
449 | 503 | | |
450 | 504 | | |
451 | 505 | | |
| |||
471 | 525 | | |
472 | 526 | | |
473 | 527 | | |
| 528 | + | |
| 529 | + | |
| 530 | + | |
| 531 | + | |
| 532 | + | |
| 533 | + | |
| 534 | + | |
| 535 | + | |
| 536 | + | |
| 537 | + | |
| 538 | + | |
| 539 | + | |
| 540 | + | |
| 541 | + | |
| 542 | + | |
| 543 | + | |
| 544 | + | |
| 545 | + | |
| 546 | + | |
| 547 | + | |
474 | 548 | | |
475 | 549 | | |
476 | 550 | | |
| |||
479 | 553 | | |
480 | 554 | | |
481 | 555 | | |
482 | | - | |
| 556 | + | |
| 557 | + | |
| 558 | + | |
| 559 | + | |
| 560 | + | |
483 | 561 | | |
484 | 562 | | |
485 | 563 | | |
| |||
0 commit comments