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

[export] Add schema version to serializer/deserializer #107420

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 14 additions & 0 deletions test/export/test_serialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
ExportedProgramSerializer,
deserialize,
serialize,
SerializeError,
)
from torch._subclasses.fake_tensor import FakeTensor
from torch.fx.experimental.symbolic_shapes import is_concrete_int
Expand Down Expand Up @@ -369,6 +370,19 @@ def test_exportdb_supported(self, name: str, case: ExportCase) -> None:

instantiate_parametrized_tests(TestDeserialize)

@unittest.skipIf(not torchdynamo.is_dynamo_supported(), "dynamo doesn't support")
class TestSchemaVersioning(TestCase):
def test_error(self):
def f(x):
return x + x

ep = export(f, (torch.randn(1, 3),))

serialized_ep, serialized_state_dict = ExportedProgramSerializer().serialize(ep)
serialized_ep.schema_version = -1
with self.assertRaisesRegex(SerializeError, r"Serialized schema version -1 does not match our current"):
ExportedProgramDeserializer().deserialize(serialized_ep, serialized_state_dict)


class TestOpVersioning(TestCase):
"""Test if serializer/deserializer behaves correctly if version mismatch."""
Expand Down
5 changes: 5 additions & 0 deletions torch/_export/serde/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
from enum import IntEnum
from typing import Dict, List, Optional, Tuple


# NOTE: Please update this value if any modifications are made to the schema
SCHEMA_VERSION = 0

# TODO (zhxchen17) Move to a separate file.
class _Union:
@classmethod
Expand Down Expand Up @@ -247,3 +251,4 @@ class ExportedProgram:
opset_version: Dict[str, int]
range_constraints: Dict[str, RangeConstraint]
equality_constraints: List[Tuple[Tuple[str, int], Tuple[str, int]]]
schema_version: int
8 changes: 8 additions & 0 deletions torch/_export/serde/serialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
OptionalTensorArgument,
RangeConstraint,
ScalarType,
SCHEMA_VERSION,
SymBool,
SymBoolArgument,
SymExpr,
Expand Down Expand Up @@ -752,6 +753,7 @@ def serialize(self, exported_program: ep.ExportedProgram) -> Tuple[ExportedProgr
opset_version=self.opset_version,
range_constraints=serialized_range_constraints,
equality_constraints=serialized_equality_constraints,
schema_version=SCHEMA_VERSION,
),
serialize_state_dict(exported_program.state_dict),
)
Expand Down Expand Up @@ -1198,6 +1200,12 @@ def deserialize_range_constraints(
def deserialize(
self, serialized_exported_program: ExportedProgram, serialized_state_dict: bytes
) -> ep.ExportedProgram:
if serialized_exported_program.schema_version != SCHEMA_VERSION:
raise SerializeError(
f"Serialized schema version {serialized_exported_program.schema_version} "
f"does not match our current schema version {SCHEMA_VERSION}."
)

symbol_name_to_range = {
k: symbolic_shapes.ValueRanges(_int_to_sympy_int(v.min_val), _int_to_sympy_int(v.max_val))
for k, v in serialized_exported_program.range_constraints.items()
Expand Down