-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Port to orjson
from ujson
#8584
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
Changes from all commits
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 |
---|---|---|
|
@@ -2,7 +2,7 @@ | |
from enum import Enum | ||
from typing import Any, Literal, TypedDict | ||
|
||
import ujson | ||
import orjson | ||
|
||
import dspy | ||
from dspy.adapters.base import Adapter | ||
|
@@ -60,7 +60,7 @@ def get_finetune_directory() -> str: | |
def write_lines(file_path, data): | ||
with open(file_path, "w") as f: | ||
for item in data: | ||
f.write(ujson.dumps(item) + "\n") | ||
f.write(orjson.dumps(item).decode() + "\n") | ||
Comment on lines
61
to
+63
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. As above, I recommend eliminating the calls to with open(file_path, "wb") as f:
for item in data:
f.write(orjson.dumps(item) + b"\n") This suggestion applies to the other change in this file. |
||
|
||
|
||
def save_data( | ||
|
@@ -77,7 +77,7 @@ def save_data( | |
file_path = os.path.abspath(file_path) | ||
with open(file_path, "w") as f: | ||
for item in data: | ||
f.write(ujson.dumps(item) + "\n") | ||
f.write(orjson.dumps(item).decode() + "\n") | ||
return file_path | ||
|
||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,7 @@ | |
from pathlib import Path | ||
|
||
import cloudpickle | ||
import ujson | ||
import orjson | ||
|
||
from dspy.utils.saving import get_dependency_versions | ||
|
||
|
@@ -216,7 +216,7 @@ def save(self, path, save_program=False, modules_to_serialize=None): | |
"or consider using state-only saving by setting `save_program=False`." | ||
) | ||
with open(path / "metadata.json", "w", encoding="utf-8") as f: | ||
ujson.dump(metadata, f, indent=2, ensure_ascii=False) | ||
f.write(orjson.dumps(metadata, option=orjson.OPT_INDENT_2 | orjson.OPT_APPEND_NEWLINE).decode('utf-8')) | ||
|
||
return | ||
|
||
|
@@ -225,7 +225,7 @@ def save(self, path, save_program=False, modules_to_serialize=None): | |
if path.suffix == ".json": | ||
try: | ||
with open(path, "w", encoding="utf-8") as f: | ||
f.write(ujson.dumps(state, indent=2 , ensure_ascii=False)) | ||
f.write(orjson.dumps(state, option=orjson.OPT_INDENT_2 | orjson.OPT_APPEND_NEWLINE).decode('utf-8')) | ||
except Exception as e: | ||
raise RuntimeError( | ||
f"Failed to save state to {path} with error: {e}. Your DSPy program may contain non " | ||
|
@@ -249,7 +249,7 @@ def load(self, path): | |
|
||
if path.suffix == ".json": | ||
with open(path, encoding="utf-8") as f: | ||
state = ujson.loads(f.read()) | ||
state = orjson.loads(f.read().encode('utf-8')) | ||
Comment on lines
251
to
+252
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. Last place I'll leave feedback, but in general the suggestion is "don't decode content only to re-encode it immediately: with open(path, "rb") as f:
state = orjson.loads(f.read()) Also, these are pathlib objects so this is more ideal: state = orjson.loads(path.read_bytes()) |
||
elif path.suffix == ".pkl": | ||
with open(path, "rb") as f: | ||
state = cloudpickle.load(f) | ||
|
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.
I recommend changing the
open(file_path, "w")
, above, to "wb" mode to eliminate the decodes happening here.The end result will be:
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.
decode()
is beneficial here - one nice benefit of saving state as json is the file is human readable, so that users can copy/paste the demos/instructions to other parts of the pipeline. I have seen many users only use DSPy as the prompt optimization tool, and export the optimized prompt (instruction + demos).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.
.decode()
is not doing the beneficial work that you think it's doing.It's creating this pipeline:
Opening the file in text mode means that Python must internally encode the file when it actually writes the file. Python does NOT use UTF-8 for this! It defaults to the system-specific encoding. On Windows in the U.S., for example, this is usually something like ISO-8859-1.
This produces a bytes object -- perfect for writing to disk!
This decodes the bytes to UTF-8, and is a performance problem because now Python must re-encode the content before it can be written to disk. What a waste.
Another performance problem -- the entire JSON string must now be copied in memory just so a newline can be added to the end in memory.
Here's the full pipeline in this one line of code:
The
.decode()
is not beneficial. It's a waste of cycles.The most performant option that I'm aware of, which eliminates the decode-with-UTF-8/re-encode-with-??? roundtrip, and eliminates the construct-a-new-string-in-memory is:
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.
Yes I am aware of the performance issue. I was worrying about the displayed text when opening the json file is not readable, but it's not really the case since most text editors decode by utf-8 by default.