-
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
base: main
Are you sure you want to change the base?
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: