diff --git a/CHANGELOG.md b/CHANGELOG.md index 4f0a144d5..fa64c3d81 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -68,6 +68,7 @@ All notable changes to SkillEvaluator are documented in this file. ### Fixed +- Similarity scores stay correct for extremely small embedding vectors. - Bedrock preflight and host inference now sign requests with IAM credentials when `AWS_BEARER_TOKEN_BEDROCK` is empty or whitespace-only, for example when a CI job exports an unset secret. Previously the Tier 3 preflight sent a blank diff --git a/src/skillevaluator/embedding/client.py b/src/skillevaluator/embedding/client.py index 22b0b9618..5ef5f2e3a 100644 --- a/src/skillevaluator/embedding/client.py +++ b/src/skillevaluator/embedding/client.py @@ -75,6 +75,12 @@ def normalize_embedding_vector( ) if norm == 0.0: return values + if norm < sys.float_info.min: + # A subnormal norm loses significant digits even when hypot avoids + # underflow. Rescale first so normalization preserves the direction. + scale = max(abs(value) for value in values) + values = [value / scale for value in values] + norm = math.hypot(*values) return [value / norm for value in values] diff --git a/tests/embedding/test_subnormal_vectors.py b/tests/embedding/test_subnormal_vectors.py new file mode 100644 index 000000000..4e6b147c1 --- /dev/null +++ b/tests/embedding/test_subnormal_vectors.py @@ -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) == []