Repository navigation
hnsw: int16 kernels for cosine and negative inner product - #36
Open
tomas-villagesql wants to merge 1 commit into
Open
tomas-villagesql wants to merge 1 commit into
tomas-villagesql wants to merge 1 commit into
Conversation
|
Please add a kind label with /kind. |
|
Please add an area label with /area. |
|
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: Needs approval from an approver in each of these files:Approvers can indicate their approval by writing |
tomas-villagesql
changed the base branch from
main
to
tomas/vector-cache-fill-once
November 5, 2026 20:31
tomas-villagesql
force-pushed
the
tomas/int16-cosine-ip
branch
from
November 7, 2026 15:36
149eb9b to
5c68566
Compare
tomas-villagesql
force-pushed
the
tomas/vector-cache-fill-once
branch
from
November 7, 2026 15:36
360ed50 to
1bae0cc
Compare
tomas-villagesql
force-pushed
the
tomas/vector-cache-fill-once
branch
from
November 15, 2026 10:39
1bae0cc to
76fff30
Compare
tomas-villagesql
force-pushed
the
tomas/int16-cosine-ip
branch
from
November 15, 2026 10:39
5c68566 to
35857ae
Compare
The vector cache holds vectors in int16 form, so it attached only to an index whose metric had an int16 kernel: squared L2. Cosine and inner-product indexes always ran in f32 from pages. quantize.h gains dist_cosine_q and dist_negative_inner_product_q. Each is still one int16 dot product plus a constant-time combine: cosine takes its norms from the precomputed abs2 = 0.5 * ||v||^2, so ||a|| * ||b|| = 2 * sqrt(abs2a * abs2b), and returns 1 for a zero or overflowed norm as native::dist_cosine does. distance_registry.h maps a metric name to its int16 kernel next to the f32 map, and has_quantized_kernel() is true for every metric but L1, which has no per-vector reduction. DistanceEvaluator resolves the metric once and picks the kernel with a switch, so squared L2 stays inlined; an int16 comparison on a metric without a kernel is an error. distance_evaluator_test checks each int16 kernel against f32 within quantization error through both the both-quantized and mixed paths, cosine against a zero vector, and that L1 has no kernel. hnsw_vector_cache_metrics runs cosine and inner-product KNN with the cache on: both are served from the cache and match brute force.
tomas-villagesql
force-pushed
the
tomas/int16-cosine-ip
branch
from
November 17, 2026 20:21
35857ae to
97bf11f
Compare
tomas-villagesql
force-pushed
the
tomas/vector-cache-fill-once
branch
from
November 17, 2026 20:21
76fff30 to
4c5acff
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Stacked on #37 (fill-once vector cache); review only
149eb9b. Retarget tomainonce #37 merges.The vector cache attached only to squared-L2 indexes, since it holds vectors in int16 form and only L2 had an int16 kernel. Cosine and inner-product indexes always ran in f32 from pages.
Change
quantize.h:dist_cosine_q(norms fromabs2,||a||*||b|| = 2*sqrt(abs2a*abs2b), zero/overflowed norm gives 1 as in f32) anddist_negative_inner_product_q(-sa*sb*qdot). Each is one int16 dot product.distance_registry.h:quantized_metric_for_name()next to the f32 map;has_quantized_kernel()is true for every metric but L1, which has no per-vector reduction.DistanceEvaluatorresolves the metric once and switches on it, so squared L2 stays inlined; an int16 comparison on a metric without a kernel is an error.Tests
distance_evaluator_test: each int16 kernel against f32 within quantization error (both-quantized and mixed paths), cosine against a zero vector, L1 has no kernel.hnsw_vector_cache_metrics: cosine and inner-product KNN with the cache on are served from the cache (cache_hitsrises) and match brute force.make test6/6 pass;hnsw_vector_cache_metricsrecorded and passing.🤖 Generated with Claude Code