Repository navigation
fix(embedding): normalize subnormal vectors without losing direction #724
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
rng1995
merged 15 commits into
NVIDIA:main
from
Afloat16:fix/subnormal-embedding-normalization
Nov 23, 2026
Merged
Changes from 5 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
e9a0533
fix(embedding): normalize subnormal vectors without losing direction
Afloat16 0f13248
Merge branch 'main' into fix/subnormal-embedding-normalization
chrisknvidia e94a6c1
docs: simplify the tiny-vector release note
Afloat16 a0f4dee
Merge main to resolve release note conflict in PR #724
Afloat16 be2289c
Merge latest main to resolve release note conflict in PR #724
Afloat16 e430e5a
docs: move pending change notes into Unreleased
Afloat16 08dc089
Merge upstream main into fix/subnormal-embedding-normalization
Afloat16 2c478d3
Merge main into fix/subnormal-embedding-normalization
Afloat16 cd75ad2
chore: integrate main and preserve unreleased fixes
Afloat16 4c58813
chore: integrate latest SkillSpector diagnostics from main
Afloat16 4b8e02d
chore: reconcile release notes with current main
Afloat16 37f3bec
chore: merge main and preserve the PR release note
Afloat16 326cdf6
Merge main into fix/subnormal-embedding-normalization
rng1995 c6ab7d5
Merge main into afloat/pr-724
rng1995 410e42a
Merge main into afloat/pr-724
rng1995 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| # SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| """Cosine similarity must preserve direction when accepted vectors are tiny.""" | ||
|
|
||
| import math | ||
| from unittest.mock import MagicMock | ||
|
|
||
| import pytest | ||
|
|
||
| from skillevaluator.embedding.client import EmbeddingClient, normalize_embedding_vector | ||
| from skillevaluator.embedding.registry import EmbeddingRegistry, RegistryEntry | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("scale", [math.ulp(0.0), 1e-320, 1e-310]) | ||
| @pytest.mark.parametrize("direction", [[1.0, 1.0], [-1.0, 1.0], [1.0, 2.0, -3.0]]) | ||
| def test_subnormal_vector_normalization_preserves_direction(scale: float, direction: list[float]) -> None: | ||
| vector = [scale * component for component in direction] | ||
| original = list(vector) | ||
| expected = normalize_embedding_vector(direction) | ||
|
|
||
| normalized = normalize_embedding_vector(vector) | ||
|
|
||
| assert normalized == pytest.approx(expected, abs=1e-14) | ||
| assert math.hypot(*normalized) == pytest.approx(1.0, abs=1e-14) | ||
| assert vector == original | ||
| axis = [1.0] + [0.0] * (len(vector) - 1) | ||
| assert EmbeddingClient.cosine_similarity(vector, axis) == pytest.approx(expected[0], abs=1e-14) | ||
|
|
||
|
|
||
| def test_tiny_vector_does_not_create_a_false_high_similarity_match() -> None: | ||
| client = MagicMock(spec=EmbeddingClient) | ||
| client.embed_single.return_value = [math.ulp(0.0), math.ulp(0.0)] | ||
| registry = EmbeddingRegistry(client) | ||
| registry._entries["axis"] = RegistryEntry( | ||
| name="axis", | ||
| description="A distinct direction", | ||
| path="axis", | ||
| content_type="skill", | ||
| embedding=[1.0, 0.0], | ||
| ) | ||
|
|
||
| assert registry.query("query", threshold=0.9) == [] |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.