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

enhance(SDK): avoid obtaining all artifact content in advance #2882

Merged
merged 2 commits into from
Oct 20, 2023
Merged
Show file tree
Hide file tree
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: 3 additions & 4 deletions client/starwhale/api/_impl/evaluation/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,13 +226,12 @@ def _auto_decode_types(self, data: t.Any) -> t.Any:
Text(encoded) -> string
Binary(encoded) -> bytes
"""
if isinstance(data, BaseArtifact):
data.owner = self._resource
data.fetch_data()

if isinstance(data, Text) and data.auto_convert_to_str:
# TODO: remove the owner assignment
data.owner = self._resource
return data.content
elif isinstance(data, Binary) and data.auto_convert_to_bytes:
data.owner = self._resource
return data.to_bytes()
elif isinstance(data, dict):
return {k: self._auto_decode_types(v) for k, v in data.items()}
Expand Down
4 changes: 2 additions & 2 deletions client/starwhale/base/data_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ class Binary(BaseArtifact, SwObject):
# TODO: use the better way to calculate the min size
# Detect if the bytes is too long to encode to Binary for the datastore efficiency
# size = DIGEST_SIZE + Binary Struct size + Link Object Struct size
AUTO_ENCODE_MIN_SIZE = sys.getsizeof(DIGEST_SIZE) + 512
AUTO_ENCODE_MIN_SIZE = sys.getsizeof(DIGEST_SIZE) + 1024

def __init__(
self,
Expand Down Expand Up @@ -679,7 +679,7 @@ class Text(BaseArtifact, SwObject):
# TODO: use the better way to calculate the min size
# Detect if the str is too long to encode to Text for the datastore efficiency
# size = DIGEST_SIZE + Text Struct size + Link Object Struct size
AUTO_ENCODE_MIN_SIZE = sys.getsizeof(DIGEST_SIZE) + 512
AUTO_ENCODE_MIN_SIZE = sys.getsizeof(DIGEST_SIZE) + 1024

def __init__(
self,
Expand Down
16 changes: 8 additions & 8 deletions client/tests/sdk/test_evaluation_log.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ def test_log_for_singleton_instance(self) -> None:

_log(category=category, id=1, metrics={"a": 1, "b": 2})
_log(category=category, id=2, metrics={"a": 2, "b": 3})
_log_result(id="id-1", metrics={"text": "ttt" * 200})
_log_result(id="id-2", metrics={"binary": b"bbb" * 200})
_log_result(id="id-1", metrics={"text": "ttt" * 1000})
_log_result(id="id-2", metrics={"binary": b"bbb" * 1000})

_els = _get_log_store_from_context()
_els.flush_all(artifacts_flush=True)
Expand All @@ -79,8 +79,8 @@ def test_log_for_singleton_instance(self) -> None:

rt = list(evaluation_log_module.scan_results())
assert rt == [
{"id": "id-1", "text": "ttt" * 200},
{"id": "id-2", "binary": b"bbb" * 200},
{"id": "id-1", "text": "ttt" * 1000},
{"id": "id-2", "binary": b"bbb" * 1000},
]

def test_log_summary_for_singleton_instance(self) -> None:
Expand Down Expand Up @@ -201,8 +201,8 @@ def test_log_and_scan_for_standalone(self) -> None:
store.log_result(
id="id-1",
metrics={
"text": "aaa" * 200,
"binary": b"bbb" * 200,
"text": "aaa" * 2000,
"binary": b"bbb" * 2000,
"image": self._generate_random_image(),
},
)
Expand Down Expand Up @@ -239,8 +239,8 @@ def test_log_and_scan_for_standalone(self) -> None:
"binary": b"bbb",
"items": [1, 2, 3],
}
assert results[0]["text"] == "aaa" * 200
assert results[0]["binary"] == b"bbb" * 200
assert results[0]["text"] == "aaa" * 2000
assert results[0]["binary"] == b"bbb" * 2000
img = results[0]["image"]
assert isinstance(img, Image)
assert len(img.to_bytes()) > 0
Expand Down
Loading