Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,7 @@ All notable changes to SkillEvaluator are documented in this file.

### Fixed

- Similarity scores stay correct for extremely small embedding vectors.
Comment thread
Afloat16 marked this conversation as resolved.
Resolved
- GitLab JUnit collection includes nested skill folders in catalog reports.
- API caller now percent-encodes scalar OpenAPI path values as individual segments and rejects `.` and `..` values with a JSON input error.
- Direct Tier 1 validates the target, selected checks and LLM configuration before
Expand Down
6 changes: 6 additions & 0 deletions src/skillevaluator/embedding/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]


Expand Down
43 changes: 43 additions & 0 deletions tests/embedding/test_subnormal_vectors.py
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) == []
Loading