-
Notifications
You must be signed in to change notification settings - Fork 751
Implement python deserialize for flat_tensor #11779
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 |
|---|---|---|
|
|
@@ -6,17 +6,19 @@ | |
|
|
||
| # pyre-unsafe | ||
|
|
||
| import dataclasses | ||
| import math | ||
| import unittest | ||
|
|
||
| from typing import List, Optional | ||
|
|
||
| from executorch.exir._serialize._cord import Cord | ||
|
|
||
| from executorch.exir._serialize.data_serializer import ( | ||
| DataEntry, | ||
| DataPayload, | ||
| DataSerializer, | ||
| ) | ||
|
|
||
| from executorch.exir._serialize.padding import aligned_size | ||
|
|
||
| from executorch.exir.schema import ScalarType | ||
|
|
@@ -223,3 +225,39 @@ def test_serialize(self) -> None: | |
| ) | ||
|
|
||
| self.assertEqual(segments[2].offset + segments[2].size, len(segment_data)) | ||
|
|
||
| def test_round_trip(self) -> None: | ||
| # Serialize and then deserialize the test payload. Make sure it's reconstructed | ||
| # properly. | ||
| config = FlatTensorConfig() | ||
| serializer: DataSerializer = FlatTensorSerializer(config) | ||
|
|
||
| # Round trip the data. | ||
| serialized_data = bytes(serializer.serialize(TEST_DATA_PAYLOAD)) | ||
| deserialized_payload = serializer.deserialize(Cord(serialized_data)) | ||
|
|
||
| # Validate the deserialized payload. Since alignment isn't serialized, we need to | ||
| # do this somewhat manually. | ||
|
Contributor
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. I don't think there's currently a need to serialize the alignment(?) |
||
| for i in range(len(deserialized_payload.buffers)): | ||
| self.assertEqual( | ||
| TEST_DATA_PAYLOAD.buffers[i], | ||
| deserialized_payload.buffers[i], | ||
| f"Buffer at index {i} does not match.", | ||
| ) | ||
|
|
||
| self.assertEqual( | ||
| TEST_DATA_PAYLOAD.named_data.keys(), deserialized_payload.named_data.keys() | ||
| ) | ||
|
|
||
| SKIP_FIELDS = {"alignment"} # Fields to ignore in comparison. | ||
| for key in TEST_DATA_PAYLOAD.named_data.keys(): | ||
| reference = TEST_DATA_PAYLOAD.named_data[key] | ||
| actual = deserialized_payload.named_data[key] | ||
|
|
||
| for field in dataclasses.fields(reference): | ||
| if field.name not in SKIP_FIELDS: | ||
| self.assertEqual( | ||
| getattr(reference, field.name), | ||
| getattr(actual, field.name), | ||
|
Contributor
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. this is nice! |
||
| f"Named data record {key}.{field.name} does not match.", | ||
| ) | ||
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.
Note to reviewers: I considered adding a deserialize test in the same vein as the serialize test, but it's a bit awkward to do, in that we'd need to construct a binary input without using the existing serialization logic, Could also include a pre-serialized artifact, but I think this coverage is sufficient in conjunction with the serialize test. Feel free to let me know if you want to see more coverage.