Skip to content

TorchTensorRTModule Serialization Fix #3572

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open

Conversation

cehongwang
Copy link
Collaborator

Description

Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. List any dependencies that are required for this change.

Fixes # (issue)

Type of change

Please delete options that are not relevant and/or add your own.

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

Checklist:

  • My code follows the style guidelines of this project (You can use the linters)
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas and hacks
  • I have made corresponding changes to the documentation
  • I have added tests to verify my fix or my feature
  • New and existing unit tests pass locally with my changes
  • I have added the relevant labels to my PR in so that relevant reviewers are notified

@github-actions github-actions bot added component: api [Python] Issues re: Python API component: runtime component: dynamo Issues relating to the `torch.compile` or `torch._dynamo.export` paths labels Jun 13, 2025
@github-actions github-actions bot requested a review from peri044 June 13, 2025 19:19
Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are some changes that do not conform to Python style guidelines:

--- /home/runner/work/TensorRT/TensorRT/py/torch_tensorrt/dynamo/runtime/_TorchTensorRTModule.py	2025-06-13 19:19:30.680941+00:00
+++ /home/runner/work/TensorRT/TensorRT/py/torch_tensorrt/dynamo/runtime/_TorchTensorRTModule.py	2025-06-13 19:19:58.614974+00:00
@@ -225,20 +225,28 @@
            return
        self.engine = torch.classes.tensorrt.Engine(self._pack_engine_info())

    def encode_metadata(self, metadata: Any) -> str:
        metadata = copy.deepcopy(metadata)
-        metadata["settings"].torch_executed_ops = TorchTensorRTModule.serialize_aten_ops(metadata["settings"].torch_executed_ops)
+        metadata["settings"].torch_executed_ops = (
+            TorchTensorRTModule.serialize_aten_ops(
+                metadata["settings"].torch_executed_ops
+            )
+        )
        dumped_metadata = pickle.dumps(metadata)
        encoded_metadata = base64.b64encode(dumped_metadata).decode("utf-8")
        return encoded_metadata

    @staticmethod
    def decode_metadata(encoded_metadata: bytes) -> Any:
        dumped_metadata = base64.b64decode(encoded_metadata.encode("utf-8"))
        metadata = pickle.loads(dumped_metadata)
-        metadata["settings"].torch_executed_ops = TorchTensorRTModule.deserialize_aten_ops(metadata["settings"].torch_executed_ops)
+        metadata["settings"].torch_executed_ops = (
+            TorchTensorRTModule.deserialize_aten_ops(
+                metadata["settings"].torch_executed_ops
+            )
+        )
        return metadata

    @staticmethod
    def serialize_aten_ops(aten_ops: Set[torch._ops.OpOverload]) -> Set[str]:
        return {str(op) for op in aten_ops}

return metadata

@staticmethod
def serialize_aten_ops(aten_ops: Set[torch._ops.OpOverload]) -> Set[str]:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

call this _serialize_torch_ops

@cehongwang cehongwang force-pushed the cehongwang-patch-1 branch from 6bf3127 to f9235ed Compare June 13, 2025 19:22
Copy link
Collaborator

@narendasan narendasan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we associate this function with CompileSettings not the TorchTensorRTModule? as something like getstate /setstate

@@ -143,6 +144,16 @@ class CompilationSettings:
use_distributed_mode_trace: bool = USE_DISTRIBUTED_MODE_TRACE
offload_module_to_cpu: bool = OFFLOAD_MODULE_TO_CPU

def __getstate__(self) -> dict[str, Any]:
state = self.__dict__.copy()
state["torch_executed_ops"] = {str(op) for op in state["torch_executed_ops"]}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

consider using ConverterRegistry.qualified_name_or_str(target)

@cehongwang cehongwang force-pushed the cehongwang-patch-1 branch 2 times, most recently from 1f50da4 to 0b3edfe Compare June 16, 2025 20:46
Copy link
Collaborator

@narendasan narendasan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM can you add a testcase for the serialization? Just save and load a module that uses torch_executed_ops?

@cehongwang cehongwang force-pushed the cehongwang-patch-1 branch from 0b3edfe to 2d7c87f Compare June 16, 2025 20:57
@github-actions github-actions bot added the component: tests Issues re: Tests label Jun 16, 2025
@@ -84,6 +84,41 @@ def test_resnet18_cpu_offload(ir):
torch._dynamo.reset()


@pytest.mark.unit
def test_resnet18_torch_exec_ops(ir):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should also test that you can serialize and deserialize this model right?

@@ -372,6 +372,44 @@ def test_resnet18_dynamic(ir):
)


@pytest.mark.unit
def test_resnet18_dynamic_torch_exec_ops(ir):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this test about dynamic inputs or serialization?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla signed component: api [Python] Issues re: Python API component: dynamo Issues relating to the `torch.compile` or `torch._dynamo.export` paths component: tests Issues re: Tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

🐛 [Bug] Using torch_executed_ops causes the metadata packing to fail
4 participants