Skip to content

Leave zero vectors unchanged in span normalization instead of producing NaN - #1484

Merged
martindevans merged 1 commit into
SciSharp:masterfrom
ewerton336:fix/zero-vector-normalization
Nov 8, 2026
Merged

martindevans merged 1 commit into
SciSharp:masterfrom
ewerton336:fix/zero-vector-normalization

Conversation

@ewerton336

Copy link
Copy Markdown
Contributor

MaxAbsoluteNormalization, TaxicabNormalization, EuclideanNormalization and PNormalization divide by the norm of the vector. For an all zero vector the norm is 0, so every element becomes NaN.

llama.cpp handles this case in common_embd_normalize (common/common.cpp):

const float norm = sum > 0.0 ? 1.0 / sum : 0.0f;

so a zero vector stays all zeros. This change does the same: each method returns the vector unchanged when the norm is 0 (and the ReadOnlySpan overload returns a zero filled array).

Added tests in SpanNormalizationExtensionsTests that run every mode on new float[3] through the array and span overloads and expect [0, 0, 0]. They failed before the fix ([NaN, NaN, NaN]) and pass now. No model needed.

@martindevans martindevans left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thanks for fixing this

@martindevans
martindevans merged commit d99eb2c into SciSharp:master Nov 8, 2026
8 checks passed
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.

2 participants