fix(cluster): reject non-finite and zero-norm embeddings in Scorer.Route - #845
Open
sanidhyasin wants to merge 1 commit into
Open
fix(cluster): reject non-finite and zero-norm embeddings in Scorer.Route#845sanidhyasin wants to merge 1 commit into
sanidhyasin wants to merge 1 commit into
Conversation
Route validated only the embedding dimension, so a dimensionally valid zero, NaN or Inf vector reached topPNearest and produced a routing decision from similarities that carry no cosine ordering. Validate finiteness and a non-zero norm at the same trust boundary and map failures to ErrClusterUnavailable, with the rejection category on the error log. Signed-off-by: Sanidhya Singh <singhsanidhya741@gmail.com>
|
PR author is not in the allowed authors list. |
Contributor
|
Thanks a lot for this — really nice contribution, and it lands squarely inside the repo's conventions, so no changes needed from our side. What I checked and liked:
Also appreciated the honesty about production incidence being unproven and the explicit scope note leaving the |
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.
Fixes #780.
Problem
After a successful
Embed,Scorer.Routechecked only the embeddingdimension. A dimensionally valid zero, NaN or Inf vector went
straight into
topPNearestand produced a routing decision.Neither case has meaningful cosine ordering:
sort.SliceStable's index tie-break decides the top-p. Centroid orderin the artifact becomes the routing feature.
so the selected centroids aren't semantically valid. Go doesn't panic
here; routing just succeeds with a wrong answer.
Either way the request is served by a model chosen on artifact layout
rather than on the prompt, and nothing upstream reports it. That's the
fail-open shape the package rules out: every other failure mode in
Route(embed timeout, embed error, dim mismatch, alpha-vector length,empty argmax) returns
ErrClusterUnavailable→ 503.The embedder can't be relied on to catch this. hugot's
WithNormalizationfloors the denominator at
1e-12, so a zero vector stays zero and NaNsstay NaN — the ONNX path can return a numerically corrupted vector with
no error, and
onnxEmbedder.Embedvalidates only count and dim.Production incidence is unproven — I have no telemetry or ONNX
reproduction, only the code-level gap. Treating this as a correctness
fix, not an active incident.
Changes
internal/router/cluster/scorer.go:validateEmbedding(vec, wantDim) (reason, error), called at thedim check it replaces.
Scorer.Routeis the only place aruntime-produced vector enters the package (
distribution.go'stopPNearestcall feeds it centroid rows, which are trusted artifactdata), so validating there covers the surface — and it sits on
Scorerrather than in the ONNX adapter, sinceScorercan be handedany
Embedderimplementation.ErrClusterUnavailable. The dim-mismatch error stringis unchanged, so its 503 mapping and message are byte-identical.
reason— one ofwrong_dimension,nan,positive_inf,negative_inf,zero_norm— plusembedder, bothdims and
requested_model. That's the countable signal the issue asksfor, in the package's existing structured-log idiom rather than a new
metrics dependency. No vector values or prompt text are logged. The
dim-mismatch log message becomes the shared "invalid embedding" line;
reason=wrong_dimensionkeeps that case distinguishable.The norm test is exact zero, not an epsilon. A tiny but finite vector
still orders centroids meaningfully, so rejecting it would turn working
requests into 503s; zero and non-finite categorically don't. No
norm ≈ 1check either — that needs real Jina/Qwen output norms first.Package
CLAUDE.md/AGENTS.mdfail-open list updated (mirrored, per themirror notice).
Tests
TestScorer_ReturnsErrOnInvalidEmbedding— table-driven over all-zero,NaN at beginning/middle/end, +Inf and -Inf. Each asserts
RoutereturnsErrClusterUnavailableand that the rejection is categorized correctly.Every case routes successfully on
main.TestScorer_RoutesTinyNonZeroEmbedding— a1e-20vector must stillroute, pinning the exact-zero boundary so a later epsilon can't creep in
unnoticed.
TestScorer_ReturnsErrOnDimMismatchis unchanged and still passesthrough the new helper.
Validation
go test -tags no_onnx ./internal/router/...— all packages pass.go build -tags no_onnx ./...andgofmt -lclean.I don't have the ONNX assets or
libonnxruntimelocally, so theonnx_integrationparity test wasn't run; this change doesn't touch theembedder implementations.
Not in this PR
Per the issue's scope note, the
AlphaFloor/ dial-calibration work(#779) is left alone.