-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Fixes float32->float64 precision widening a.k.a. float bloat #3743
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
base: main
Are you sure you want to change the base?
Changes from all commits
ff8ebc7
bbe6593
74607ee
dde0f48
42b61a3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -110,6 +110,29 @@ def create( | |
| if not is_given(encoding_format): | ||
| params["encoding_format"] = "base64" | ||
|
|
||
| def parser(obj: CreateEmbeddingResponse) -> CreateEmbeddingResponse: | ||
| if is_given(encoding_format): | ||
| # don't modify the response object if a user explicitly asked for a format | ||
| return obj | ||
|
|
||
| if not obj.data: | ||
| raise ValueError("No embedding data received") | ||
|
|
||
| for embedding in obj.data: | ||
| data = cast(object, embedding.embedding) | ||
| if not isinstance(data, str): | ||
| continue | ||
| if not has_numpy(): | ||
| # use array for base64 optimisation | ||
| values = array.array("f", base64.b64decode(data)).tolist() | ||
| else: | ||
| values = np.frombuffer( # type: ignore[no-untyped-call] | ||
| base64.b64decode(data), dtype="float32" | ||
| ).tolist() | ||
| embedding.embedding = [float(f"{value:.9g}") for value in values] | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
For default-format embedding requests, this now formats and reparses every coordinate in Python, adding a temporary string allocation per value. In a local check with one 3,072-dimensional vector, this conversion took roughly 1.67 ms versus 0.05 ms for the previous Useful? React with 👍 / 👎. |
||
|
|
||
| return obj | ||
|
|
||
| return self._post( | ||
| "/embeddings", | ||
| body=maybe_transform(params, embedding_create_params.EmbeddingCreateParams), | ||
|
|
@@ -212,6 +235,29 @@ async def create( | |
| if not is_given(encoding_format): | ||
| params["encoding_format"] = "base64" | ||
|
|
||
| def parser(obj: CreateEmbeddingResponse) -> CreateEmbeddingResponse: | ||
| if is_given(encoding_format): | ||
| # don't modify the response object if a user explicitly asked for a format | ||
| return obj | ||
|
|
||
| if not obj.data: | ||
| raise ValueError("No embedding data received") | ||
|
|
||
| for embedding in obj.data: | ||
| data = cast(object, embedding.embedding) | ||
| if not isinstance(data, str): | ||
| continue | ||
| if not has_numpy(): | ||
| # use array for base64 optimisation | ||
| values = array.array("f", base64.b64decode(data)).tolist() | ||
| else: | ||
| values = np.frombuffer( # type: ignore[no-untyped-call] | ||
| base64.b64decode(data), dtype="float32" | ||
| ).tolist() | ||
| embedding.embedding = [float(f"{value:.9g}") for value in values] | ||
|
|
||
| return obj | ||
|
|
||
| return await self._post( | ||
| "/embeddings", | ||
| body=maybe_transform(params, embedding_create_params.EmbeddingCreateParams), | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For default-format embedding requests, this newly defined callback is never invoked: the request still passes
partial(_parse_embedding_response, ...)as itspost_parseron line 144, so responses continue through the unchanged decoder and retain the widened representations this commit is intended to remove. The asynchronous implementation has the same disconnect; implement the normalization in the shared handwritten parsing helper and keep this generated resource delegating to it.AGENTS.md reference: AGENTS.md:L3-L8
Useful? React with 👍 / 👎.