Skip to content

Preserve Polygraphy cosine similarity under rescaling - #4869

Open
sylvesterkaczmarek wants to merge 1 commit into
NVIDIA:mainfrom
sylvesterkaczmarek:fix/polygraphy-cosine-scale-invariance
Open

sylvesterkaczmarek wants to merge 1 commit into
NVIDIA:mainfrom
sylvesterkaczmarek:fix/polygraphy-cosine-scale-invariance

Conversation

@sylvesterkaczmarek

Copy link
Copy Markdown

Fixes #4868.

Scale each nonzero vector by its maximum absolute component before calculating cosine products and norms. This avoids overflow/underflow changing the comparison verdict for rescaled finite outputs. Preserve the zero-vector convention and non-finite-result rejection.

The scale is obtained from the extrema without the general absolute-value helper, whose PyTorch implementation first converts to float32. Integer inputs are promoted before scaling so their minimum representable value can be handled safely.

Add NumPy/PyTorch regressions for independently scaled identical, opposite and orthogonal vectors, plus empty, zero and signed-integer controls. The controls also check that inputs are unchanged. Update Polygraphy's changelog.

Validation

From tools/Polygraphy:

PYTHONPATH=. python -m pytest -q tests/comparator/test_compare.py   tests/comparator/test_struct.py tests/comparator/test_postprocess.py

283 passed, 4 existing skips on CPU. The 24 floating-point rescaling cases give 18 failures and 6 passes on unchanged upstream. Polygraphy's Black pre-commit hook and git diff --check pass.

No TensorRT engine, CUDA execution, or model accuracy benchmark was run. The tests exercise comparison arithmetic and verdicts using controlled arrays.

Signed-off-by: Sylvester Kaczmarek <16242628+sylvesterkaczmarek@users.noreply.github.com>
@sylvesterkaczmarek
sylvesterkaczmarek requested a review from a team as a code owner October 4, 2026 20:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Polygraphy cosine comparison changes verdict under rescaling

1 participant