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] Handle out of range values in scalar quantization #2865

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all 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
7 changes: 5 additions & 2 deletions sentence_transformers/quantization.py
Original file line number Diff line number Diff line change
Expand Up @@ -423,9 +423,12 @@ def quantize_embeddings(
steps = (ranges[1, :] - ranges[0, :]) / 255

if precision == "uint8":
return ((embeddings - starts) / steps).astype(np.uint8)
q_vals = np.floor((embeddings - starts) / steps)
return np.clip(q_vals, 0, 255).astype(np.uint8)
elif precision == "int8":
return ((embeddings - starts) / steps - 128).astype(np.int8)
q_vals = np.floor((embeddings - starts) / steps)
q_vals = np.clip(q_vals, 0, 255)
return (q_vals - 128).astype(np.int8)

if precision == "binary":
return (np.packbits(embeddings > 0).reshape(embeddings.shape[0], -1) - 128).astype(np.int8)
Expand Down