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

Enabled python runtime saving #2999

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
15 changes: 11 additions & 4 deletions py/torch_tensorrt/dynamo/runtime/_PythonTorchTensorRTModule.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import tensorrt as trt
import torch
import torch_tensorrt
from torch.nn import Module
from torch_tensorrt._Device import Device
from torch_tensorrt._enums import dtype
Expand All @@ -18,8 +19,6 @@
from torch_tensorrt.dynamo.utils import DYNAMIC_DIM
from torch_tensorrt.logging import TRT_LOGGER

import torch_tensorrt

logger = logging.getLogger(__name__)


Expand Down Expand Up @@ -145,15 +144,23 @@ def __getstate__(self) -> Dict[str, Any]:
state = self.__dict__.copy()
state["engine"] = bytearray(self.engine.serialize())
state.pop("context", None)
state.pop("input_dtypes", None)
state.pop("input_shapes", None)
state.pop("output_dtypes", None)
state.pop("output_shapes", None)
state.pop("active_stream", None)
Copy link
Collaborator

Choose a reason for hiding this comment

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

I dont think this data should be serialized. It should be reconstructed on reload

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

These data are poped from the dict so they are not serialized

state.pop("target_device_properties", None)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Do we reuse this data on load to set up the engine properly?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes. Line 163 initialized all these data

return state

def __setstate__(self, state: Dict[str, Any]) -> None:
logger = trt.Logger()
runtime = trt.Runtime(logger)
state["engine"] = runtime.deserialize_cuda_engine(state["engine"])
self.__dict__.update(state)
self.target_device_properties = torch.cuda.get_device_properties(
Copy link
Collaborator

Choose a reason for hiding this comment

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

The deserialization should make sure that the gpu matches the gpu the engine was built on instead of just changing it

self.target_device_id
)
if self.engine:
self.context = self.engine.create_execution_context()
self._initialize()

def __deepcopy__(self, memo: Any) -> PythonTorchTensorRTModule:
cls = self.__class__
Expand Down
Loading