Skip to content

Commit

Permalink
Fix potential edge case scoring in context search (#474)
Browse files Browse the repository at this point in the history
* apply fast_sigmoid fn to context pair score

* remove redundant else statement

* better NaN and float32 handling

* remove unused import
  • Loading branch information
coszio authored and joein committed Mar 4, 2024
1 parent eda201a commit 3123236
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions qdrant_client/local/distances.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,12 +147,16 @@ def calculate_distance_core(
return calculate_distance(query, vectors, distance_type)


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

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


def scaled_fast_sigmoid(x: np.float32) -> np.float32:
if np.isfinite(x):
return 0.5 * (x / (1.0 + abs(x)) + 1.0)
else:
# To avoid NaNs, which gets: RuntimeWarning: invalid value encountered in scalar divide
return x
return 0.5 * (np.add(fast_sigmoid(x), 1.0))


def calculate_recommend_best_scores(
Expand Down Expand Up @@ -234,7 +238,9 @@ def calculate_context_scores(
neg = calculate_distance_core(pair.negative, vectors, distance_type)

difference = pos - neg - EPSILON
pair_scores = np.minimum(difference, 0.0)
pair_scores = np.fromiter(
(fast_sigmoid(xi) for xi in np.minimum(difference, 0.0)), np.float32
)
overall_scores += pair_scores

return overall_scores
Expand Down

0 comments on commit 3123236

Please sign in to comment.