Skip to content
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

Fix potential edge case scoring in context search #474

Merged
merged 4 commits into from
Feb 8, 2024
Merged
Changes from 1 commit
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
15 changes: 6 additions & 9 deletions qdrant_client/local/distances.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from typing import List, Optional, Union

import numpy as np
from numpy.lib.ufunclike import isneginf

from qdrant_client.conversions import common_types as types
from qdrant_client.http import models
Expand Down Expand Up @@ -148,19 +149,15 @@ def calculate_distance_core(


def fast_sigmoid(x: np.float32) -> np.float32:
if not np.isfinite(x):
# To avoid NaNs, which gets: RuntimeWarning: invalid value encountered in scalar divide
return x
if np.isnan(x):
# To avoid divisions on NaNs, which gets: RuntimeWarning: invalid value encountered in scalar divide
return x # NaN

Copy link
Member

Choose a reason for hiding this comment

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

do we want to just hide the warning? :D

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Rust implementation also returns NaN when dividing by NaN, so I'd say it is safe to return it here too

return x / (1.0 + abs(x))
return x / np.add(1.0, abs(x))


def scaled_fast_sigmoid(x: np.float32) -> np.float32:
if not np.isfinite(x):
# To avoid NaNs, which gets: RuntimeWarning: invalid value encountered in scalar divide
return x

return 0.5 * (x / (1.0 + abs(x)) + 1.0)
return 0.5 * (np.add(fast_sigmoid(x), 1.0))


def calculate_recommend_best_scores(
Expand Down
Loading