-
Notifications
You must be signed in to change notification settings - Fork 371
Tentatively eliminate graph break overhead #3741
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
Draft
cehongwang
wants to merge
5
commits into
main
Choose a base branch
from
graph-break
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
d0ae590
Tentatively eliminate graph break overhead
cehongwang 56a8949
Added stream manipulation and output tensor reusage
cehongwang 5fb0beb
Closed the graph break overhead in python
cehongwang 0046f66
fixed a bug in dynamic shape
cehongwang 7259443
Added some comments and an edge case
cehongwang File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,6 @@ | |
|
||
import logging | ||
from contextlib import nullcontext | ||
from tempfile import tempdir | ||
from typing import Any, Dict, List, Optional, Sequence, Tuple | ||
|
||
import tensorrt as trt | ||
|
@@ -218,7 +217,8 @@ def __init__( | |
self.requires_output_allocator = requires_output_allocator | ||
self.output_allocator: Optional[DynamicOutputAllocator] = None | ||
self.use_output_allocator_outputs = False | ||
|
||
self.device = torch.cuda.current_device() | ||
self.cudagraphs_enabled = torch_tensorrt.runtime.get_cudagraphs_mode() | ||
if self.serialized_engine is not None and not self.settings.lazy_engine_init: | ||
self.setup_engine() | ||
|
||
|
@@ -263,7 +263,12 @@ def setup_engine(self) -> None: | |
assert ( | ||
self.target_platform == Platform.current_platform() | ||
), f"TensorRT engine was not built to target current platform (target: {self.target_platform}, current: {Platform.current_platform()})" | ||
|
||
self._caller_stream = torch.cuda.current_stream() | ||
if ( | ||
self._engine_stream == torch.cuda.default_stream() | ||
or self._engine_stream is None | ||
): | ||
self._engine_stream = torch.cuda.Stream() | ||
self.initialized = True | ||
runtime = trt.Runtime(TRT_LOGGER) | ||
self.engine = runtime.deserialize_cuda_engine(self.serialized_engine) | ||
|
@@ -286,10 +291,14 @@ def setup_engine(self) -> None: | |
for output_name in self.output_names | ||
] | ||
self.output_shapes = [ | ||
self.engine.get_tensor_shape(output_name) | ||
tuple(self.context.get_tensor_shape(output_name)) | ||
for output_name in self.output_names | ||
] | ||
|
||
self.shape_key = "".join( | ||
str(tuple(t)).replace(" ", "") for t in self.input_shapes | ||
) | ||
|
||
if self.requires_output_allocator: | ||
self.create_output_allocator() | ||
|
||
|
@@ -370,9 +379,9 @@ def setup_input_tensors( | |
+ contiguous_inputs[i + 1 :] | ||
) | ||
|
||
assert ( | ||
contiguous_inputs[i].dtype == self.input_dtypes[i] | ||
), f"Dtype mismatch for {i}th input({input_name}). Expect {self.input_dtypes[i]}, got {contiguous_inputs[i].dtype}." | ||
# assert ( | ||
# contiguous_inputs[i].dtype == self.input_dtypes[i] | ||
# ), f"Dtype mismatch for {i}th input({input_name}). Expect {self.input_dtypes[i]}, got {contiguous_inputs[i].dtype}." | ||
Comment on lines
+397
to
+399
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. why is this commented ? |
||
|
||
if need_cudagraphs_record: | ||
# If cudagraphs is enabled, this memory is reserved for future cudagraph runs | ||
|
@@ -409,7 +418,7 @@ def create_output_tensors(self) -> List[torch.Tensor]: | |
output = torch.empty( | ||
size=self.output_shapes[o], | ||
dtype=self.output_dtypes[o], | ||
device=torch.cuda.current_device(), | ||
device=self.device, | ||
) | ||
outputs.append(output) | ||
return outputs | ||
|
@@ -480,10 +489,10 @@ def run_standard_execution() -> torch.Tensor | Tuple[torch.Tensor, ...]: | |
if can_use_pre_allocated_outputs: | ||
outputs = self.pre_allocated_outputs | ||
else: | ||
self.output_shapes = [ | ||
tuple(self.context.get_tensor_shape(output_name)) | ||
for output_name in self.output_names | ||
] | ||
# self.output_shapes = [ | ||
# tuple(self.context.get_tensor_shape(output_name)) | ||
# for output_name in self.output_names | ||
# ] | ||
if DYNAMIC_DIM in self.output_shapes: | ||
raise ValueError( | ||
"Encountered dynamic output shapes during runtime. This could mean the network has data-dependent output shapes which is not currently supported." | ||
|
@@ -510,42 +519,36 @@ def run_standard_execution() -> torch.Tensor | Tuple[torch.Tensor, ...]: | |
if self.profiling_enabled | ||
else nullcontext() | ||
): | ||
self._caller_stream = torch.cuda.current_stream() | ||
if ( | ||
self._engine_stream == torch.cuda.default_stream() | ||
or self._engine_stream is None | ||
): | ||
self._engine_stream = torch.cuda.Stream() | ||
|
||
self._engine_stream.wait_stream(self._caller_stream) | ||
|
||
with torch.cuda.stream(self._engine_stream): | ||
if self.cudagraphs_enabled: | ||
if need_cudagraphs_record: | ||
self.cudagraph = torch.cuda.CUDAGraph() | ||
# with torch.cuda.stream(self._engine_stream): | ||
# if self.cudagraphs_enabled: | ||
# if need_cudagraphs_record: | ||
# self.cudagraph = torch.cuda.CUDAGraph() | ||
|
||
if self.profiling_enabled: | ||
self.cudagraph.enable_debug_mode() | ||
# if self.profiling_enabled: | ||
# self.cudagraph.enable_debug_mode() | ||
|
||
with torch.cuda.graph( | ||
self.cudagraph, stream=self._engine_stream | ||
): | ||
self.context.execute_async_v3( | ||
self._engine_stream.cuda_stream | ||
) | ||
# with torch.cuda.graph( | ||
# self.cudagraph, stream=self._engine_stream | ||
# ): | ||
# self.context.execute_async_v3( | ||
# self._engine_stream.cuda_stream | ||
# ) | ||
|
||
if self.profiling_enabled: | ||
import tempfile | ||
# if self.profiling_enabled: | ||
# import tempfile | ||
|
||
with tempfile.TemporaryDirectory() as tmpdir: | ||
self.cudagraph.debug_dump( | ||
f"{tempdir}/{self.name}_cudagraph.dot" | ||
) | ||
# with tempfile.TemporaryDirectory() as tmpdir: | ||
# self.cudagraph.debug_dump( | ||
# f"{tempdir}/{self.name}_cudagraph.dot" | ||
# ) | ||
|
||
self.cudagraph.replay() # type: ignore | ||
# self.cudagraph.replay() # type: ignore | ||
|
||
else: | ||
self.context.execute_async_v3(self._engine_stream.cuda_stream) | ||
# else: | ||
self.context.execute_async_v3(self._engine_stream.cuda_stream) | ||
|
||
self._caller_stream.wait_stream(self._engine_stream) | ||
|
||
|
@@ -646,8 +649,6 @@ def run_output_allocator() -> torch.Tensor | Tuple[torch.Tensor, ...]: | |
|
||
return outputs | ||
|
||
self.cudagraphs_enabled = torch_tensorrt.runtime.get_cudagraphs_mode() | ||
|
||
# Run forward function | ||
contiguous_inputs: List[torch.Tensor] = [ | ||
(i.contiguous() if isinstance(i, torch.Tensor) else torch.tensor(i).cuda()) | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.