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

Added refitting acceleration #2983

Merged
merged 9 commits into from
Aug 13, 2024
Merged

Added refitting acceleration #2983

merged 9 commits into from
Aug 13, 2024

Conversation

cehongwang
Copy link
Collaborator

@cehongwang cehongwang commented Jul 3, 2024

Added refit acceleration to existing refit pipeline.

During the first time of compilation, the interpreter will cache the weight name mapping between weights in TRT engine and weights in state_dict. The compiler then will do a tentative refit to test whether fast refit is success or not. If not, the caching will be removed. Later on, during refitting, if this mapping cache is detected, the re-interpretation of the module is skipped.

If the fast refit fails, the refitter falls back to the regular refit, which re-interprets the module and does refitting accordingly.

Checklist:

  • [ x] My code follows the style guidelines of this project (You can use the linters)
  • [ x] I have performed a self-review of my own code
  • [x ] 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
  • [ x] I have added the relevant labels to my PR in so that relevant reviewers are notified

@github-actions github-actions bot added component: conversion Issues re: Conversion stage component: api [Python] Issues re: Python API component: runtime component: dynamo Issues relating to the `torch.compile` or `torch._dynamo.export` paths component: tests Issues re: Tests labels Jul 3, 2024
@narendasan narendasan requested a review from zewenli98 July 8, 2024 17:14
Copy link
Collaborator

@zewenli98 zewenli98 left a comment

Choose a reason for hiding this comment

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

LGTM

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/conversion/_conversion.py	2024-08-08 20:53:00.452273+00:00
+++ /home/runner/work/TensorRT/TensorRT/py/torch_tensorrt/dynamo/conversion/_conversion.py	2024-08-08 20:54:40.434855+00:00
@@ -167,7 +167,7 @@
        serialized_engine=interpreter_result.serialized_engine,
        input_binding_names=list(interpreter_result.input_names),
        output_binding_names=list(interpreter_result.output_names),
        name=name,
        settings=settings,
-        weight_name_map = weight_name_map
+        weight_name_map=weight_name_map,
    )
--- /home/runner/work/TensorRT/TensorRT/py/torch_tensorrt/dynamo/conversion/_TRTInterpreter.py	2024-08-08 20:53:00.452273+00:00
+++ /home/runner/work/TensorRT/TensorRT/py/torch_tensorrt/dynamo/conversion/_TRTInterpreter.py	2024-08-08 20:54:40.911400+00:00
@@ -502,11 +502,13 @@

        with io.BytesIO() as engine_bytes:
            engine_bytes.write(serialized_engine)
            engine_str = engine_bytes.getvalue()

-        return TRTInterpreterResult(engine_str, self._input_names, self._output_names, self.weight_name_map)
+        return TRTInterpreterResult(
+            engine_str, self._input_names, self._output_names, self.weight_name_map
+        )

    def run_node(self, n: torch.fx.Node) -> torch.fx.Node:
        self._cur_node_name = get_node_name(n)
        self._cur_node = n
        # add "_itensor_to_tensor_meta"
--- /home/runner/work/TensorRT/TensorRT/py/torch_tensorrt/dynamo/runtime/_TorchTensorRTModule.py	2024-08-08 20:53:00.456273+00:00
+++ /home/runner/work/TensorRT/TensorRT/py/torch_tensorrt/dynamo/runtime/_TorchTensorRTModule.py	2024-08-08 20:54:41.476969+00:00
@@ -143,12 +143,11 @@
                TorchTensorRTModule._pack_binding_names(self.output_binding_names),
                str(int(self.hardware_compatible)),
                self.encode_metadata(metadata),
            ]
        )
-        
-        
+
    def encode_metadata(self, metadata: Any) -> str:
        metadata = copy.deepcopy(metadata)
        metadata["settings"].torch_executed_ops = {
            f"torch.ops.{op.__str__()}"
            for op in metadata["settings"].torch_executed_ops

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/conversion/_TRTInterpreter.py	2024-08-08 20:59:52.444408+00:00
+++ /home/runner/work/TensorRT/TensorRT/py/torch_tensorrt/dynamo/conversion/_TRTInterpreter.py	2024-08-08 21:01:37.564015+00:00
@@ -502,11 +502,13 @@

        with io.BytesIO() as engine_bytes:
            engine_bytes.write(serialized_engine)
            engine_str = engine_bytes.getvalue()

-        return TRTInterpreterResult(engine_str, self._input_names, self._output_names, self.weight_name_map)
+        return TRTInterpreterResult(
+            engine_str, self._input_names, self._output_names, self.weight_name_map
+        )

    def run_node(self, n: torch.fx.Node) -> torch.fx.Node:
        self._cur_node_name = get_node_name(n)
        self._cur_node = n
        # add "_itensor_to_tensor_meta"
--- /home/runner/work/TensorRT/TensorRT/py/torch_tensorrt/dynamo/runtime/_TorchTensorRTModule.py	2024-08-08 20:59:52.452408+00:00
+++ /home/runner/work/TensorRT/TensorRT/py/torch_tensorrt/dynamo/runtime/_TorchTensorRTModule.py	2024-08-08 21:01:38.143764+00:00
@@ -143,12 +143,11 @@
                TorchTensorRTModule._pack_binding_names(self.output_binding_names),
                str(int(self.hardware_compatible)),
                self.encode_metadata(metadata),
            ]
        )
-        
-        
+
    def encode_metadata(self, metadata: Any) -> str:
        metadata = copy.deepcopy(metadata)
        metadata["settings"].torch_executed_ops = {
            f"torch.ops.{op.__str__()}"
            for op in metadata["settings"].torch_executed_ops

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/conversion/_TRTInterpreter.py	2024-08-08 21:05:58.675792+00:00
+++ /home/runner/work/TensorRT/TensorRT/py/torch_tensorrt/dynamo/conversion/_TRTInterpreter.py	2024-08-08 21:09:37.161892+00:00
@@ -502,11 +502,13 @@

        with io.BytesIO() as engine_bytes:
            engine_bytes.write(serialized_engine)
            engine_str = engine_bytes.getvalue()

-        return TRTInterpreterResult(engine_str, self._input_names, self._output_names, self.weight_name_map)
+        return TRTInterpreterResult(
+            engine_str, self._input_names, self._output_names, self.weight_name_map
+        )

    def run_node(self, n: torch.fx.Node) -> torch.fx.Node:
        self._cur_node_name = get_node_name(n)
        self._cur_node = n
        # add "_itensor_to_tensor_meta"
--- /home/runner/work/TensorRT/TensorRT/py/torch_tensorrt/dynamo/runtime/_TorchTensorRTModule.py	2024-08-08 21:05:58.679792+00:00
+++ /home/runner/work/TensorRT/TensorRT/py/torch_tensorrt/dynamo/runtime/_TorchTensorRTModule.py	2024-08-08 21:09:37.741137+00:00
@@ -143,12 +143,11 @@
                TorchTensorRTModule._pack_binding_names(self.output_binding_names),
                str(int(self.hardware_compatible)),
                self.encode_metadata(metadata),
            ]
        )
-        
-        
+
    def encode_metadata(self, metadata: Any) -> str:
        metadata = copy.deepcopy(metadata)
        metadata["settings"].torch_executed_ops = {
            f"torch.ops.{op.__str__()}"
            for op in metadata["settings"].torch_executed_ops

@cehongwang cehongwang force-pushed the refitting-accelerate branch 2 times, most recently from 66c99a4 to 6588edb Compare August 8, 2024 21:26
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

@cehongwang cehongwang merged commit 3ee2d81 into main Aug 13, 2024
34 of 63 checks passed
weight_name_map=interpreter_result.weight_name_map,
)
except AssertionError:
logger.warning("Fast refit test failed. Removing the weight map caching.")
Copy link
Collaborator

Choose a reason for hiding this comment

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

Where's the operation that you remove the weight map caching?

"""

def find_weight(
weight_name: str, np_map: dict[str, Any], sd: dict[str, Any]
Copy link
Collaborator

Choose a reason for hiding this comment

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

What does np_map mean?

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: conversion Issues re: Conversion stage component: dynamo Issues relating to the `torch.compile` or `torch._dynamo.export` paths component: runtime component: tests Issues re: Tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants