-
Notifications
You must be signed in to change notification settings - Fork 371
Dynamic memory allocation #3727
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
cehongwang
wants to merge
2
commits into
main
Choose a base branch
from
dynamic-allocation
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.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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
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
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
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 |
---|---|---|
|
@@ -38,13 +38,17 @@ typedef enum { | |
SERIALIZED_METADATA_IDX, | ||
TARGET_PLATFORM_IDX, | ||
REQUIRES_OUTPUT_ALLOCATOR_IDX, | ||
RESOURCE_ALLOCATION_STRATEGY_IDX, | ||
SERIALIZATION_LEN, // NEVER USED FOR DATA, USED TO DETERMINE LENGTH OF SERIALIZED INFO | ||
} SerializedInfoIndex; | ||
|
||
std::string base64_encode(const std::string& in); | ||
std::string base64_decode(const std::string& in); | ||
std::string serialize_bindings(const std::vector<std::string>& bindings); | ||
|
||
std::string resource_allocation_strategy_to_string(TRTEngine::ResourceAllocationStrategy strategy); | ||
TRTEngine::ResourceAllocationStrategy resource_allocation_strategy_from_string(const std::string& str); | ||
Comment on lines
+49
to
+50
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. Forgot to delete these |
||
|
||
c10::optional<RTDevice> get_most_compatible_device( | ||
const RTDevice& target_device, | ||
const RTDevice& curr_device = RTDevice(), | ||
|
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 |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# %% | ||
import numpy as np | ||
import torch | ||
import torch_tensorrt as torch_trt | ||
import torchvision.models as models | ||
import time | ||
import gc | ||
|
||
np.random.seed(5) | ||
torch.manual_seed(5) | ||
inputs = [torch.rand((100, 3, 224, 224)).to("cuda")] | ||
|
||
settings = { | ||
"ir": "dynamo", | ||
"use_python_runtime": False, | ||
"enabled_precisions": {torch.float32}, | ||
"immutable_weights": False, | ||
"lazy_engine_init": True, | ||
"dynamically_allocate_resources": True | ||
|
||
} | ||
|
||
model = models.resnet152(pretrained=True).eval().to("cuda") | ||
compiled_module = torch_trt.compile(model, inputs=inputs, **settings) | ||
print((torch.cuda.mem_get_info()[1] - torch.cuda.mem_get_info()[0]) / 1024**3) | ||
compiled_module(*inputs) | ||
|
||
time.sleep(30) | ||
with torch_trt.dynamo.runtime.ResourceAllocationStrategy(compiled_module, dynamically_allocate_resources=False): | ||
print( | ||
"Memory used (GB):", | ||
(torch.cuda.mem_get_info()[1] - torch.cuda.mem_get_info()[0]) / 1024**3, | ||
) | ||
compiled_module(*inputs) | ||
gc.collect() | ||
torch.cuda.empty_cache() | ||
time.sleep(30) | ||
print( | ||
"Memory used (GB):", | ||
(torch.cuda.mem_get_info()[1] - torch.cuda.mem_get_info()[0]) / 1024**3, | ||
) | ||
compiled_module(*inputs) |
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
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
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
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 |
---|---|---|
@@ -0,0 +1,32 @@ | ||
from typing import Any | ||
|
||
import torch | ||
|
||
|
||
class ResourceAllocationStrategy(torch.nn.Module): # type: ignore[misc] | ||
""" | ||
ResourceAllocationStrategy is a context manager module that temporarily enables dynamic resource allocation | ||
for all TRT submodules of the given compiled_module. When entering the context, | ||
it sets these submodules to use dynamically allocated resources. Upon exiting, it restores them to their | ||
original (static) resource allocation mode. | ||
""" | ||
|
||
def __init__( | ||
self, | ||
compiled_module: torch.nn.Module, | ||
dynamically_allocate_resources: bool = True | ||
) -> None: | ||
super(ResourceAllocationStrategy, self).__init__() | ||
self.compiled_module = compiled_module | ||
self.dynamically_allocate_resources = dynamically_allocate_resources | ||
|
||
def __enter__(self) -> None: | ||
print("Entering resource allocator context") | ||
for name, submodule in self.compiled_module.named_modules(): | ||
if "_run_on_acc" in name: | ||
submodule.use_dynamically_allocated_resources(dynamically_allocate_resources=self.dynamically_allocate_resources) | ||
|
||
def __exit__(self, exc_type: Any, exc_value: Any, exc_tb: Any) -> None: | ||
for name, submodule in self.compiled_module.named_modules(): | ||
if "_run_on_acc" in name: | ||
submodule.use_dynamically_allocated_resources(dynamically_allocate_resources=self.dynamically_allocate_resources) |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Make sure to bump the ABI version