-
Notifications
You must be signed in to change notification settings - Fork 382
fix: expand rank mismatch on symbolic shapes; add regression test #4018
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
lordaarush
wants to merge
1
commit into
pytorch:main
Choose a base branch
from
lordaarush:fix/dynamo-expand-rank-repeat
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| import pytest | ||
| import torch | ||
| import torch.nn as nn | ||
|
|
||
| try: | ||
| import torch_tensorrt | ||
| except Exception: | ||
| torch_tensorrt = None | ||
|
|
||
| REQUIRES_TRT = torch.cuda.is_available() and (torch_tensorrt is not None) | ||
|
|
||
| pytestmark = pytest.mark.skipif(not REQUIRES_TRT, reason="requires CUDA + Torch-TensorRT runtime") | ||
|
|
||
| class CosmosLearnablePositionalEmbed(nn.Module): | ||
| def __init__(self, hidden_size, max_size, patch_size): | ||
| super().__init__() | ||
| self.patch_size = patch_size | ||
| self.pos_emb_t = nn.Parameter(torch.zeros(max_size[0] // patch_size[0], hidden_size)) | ||
| self.pos_emb_h = nn.Parameter(torch.zeros(max_size[1] // patch_size[1], hidden_size)) | ||
| self.pos_emb_w = nn.Parameter(torch.zeros(max_size[2] // patch_size[2], hidden_size)) | ||
|
|
||
| def forward(self, hidden_states): | ||
| batch_size, _, num_frames, height, width = hidden_states.shape | ||
| pe_size = [num_frames // self.patch_size[0], height // self.patch_size[1], width // self.patch_size[2]] | ||
| emb_t = self.pos_emb_t[:pe_size[0]][None, :, None, None, :].repeat(batch_size, 1, pe_size[1], pe_size[2], 1) | ||
| emb_h = self.pos_emb_h[:pe_size[1]][None, None, :, None, :].repeat(batch_size, pe_size[0], 1, pe_size[2], 1) | ||
| emb_w = self.pos_emb_w[:pe_size[2]][None, None, None, :, :].repeat(batch_size, pe_size[0], pe_size[1], 1, 1) | ||
| emb = emb_t + emb_h + emb_w | ||
| emb = emb.flatten(1, 3) | ||
| return emb | ||
|
|
||
| def test_repeat_expand_lowering_repro(): | ||
| device = torch.device("cuda") | ||
| hidden_size = 4096 | ||
| model = CosmosLearnablePositionalEmbed(hidden_size=hidden_size, max_size=(128,240,240), patch_size=(1,2,2)).to(device).eval() | ||
| hidden_states = torch.randn(1, 17, 16, 88, 160, dtype=torch.bfloat16, device=device) | ||
|
|
||
| with torch.no_grad(): | ||
| pyt_out = model(hidden_states) | ||
|
|
||
| ep = torch.export.export(model, args=(hidden_states,), strict=False) | ||
| trt_mod = torch_tensorrt.dynamo.compile(ep, inputs=[hidden_states], enabled_precisions={torch.bfloat16}, use_python_runtime=True) | ||
| trt_out = trt_mod(hidden_states) | ||
|
|
||
| assert pyt_out.shape == trt_out.shape | ||
| maxdiff = (pyt_out.float() - trt_out.float()).abs().max().item() | ||
| assert maxdiff < 1e-2 |
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.
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.
Thanks for the PR. I don't think the above fix addresses the issue. Since dynamic shapes are already handled in prepend_ones. Also current_rank should now be the shape it is expanded to.
In the below test case there will be 0 computational nodes that will depend on runtime input, since the shape values will be constant. You could make them dynamic to invoke the converter.
I went through the above issue and looks like the root issue is dims being 10 here which is not permitted in TRT. It can handle till 8 max dims- https://docs.nvidia.com/deeplearning/tensorrt/latest/_static/c-api/classnvinfer1_1_1_dims64.html
Pytorch decomposes repeat to unsqueeze-> expand -> permute -> reshape
But for 5D tensor
layer.reshape_dims = new_shapefails here, since DIMS can't be 10 here, and it fails in the dynamic case too inlayer.set_input(1, reshape_dim_layer.get_output(0)). Hence input_tensor.shape would come invalid.The original example would work with only 4 dimensions. Instead of
emb_t = self.pos_emb_t[: pe_size[0]][None, :, None, None, :].repeat(batch_size, 1, pe_size[1], pe_size[2], 1)5 dims here.WAR would be to replace repeat with expand without broadcasting the dimension or using tile operation. I need to look into this more.
Ideally for tests below we would want the test case to be in
https://github.com/pytorch/TensorRT/tree/main/tests/py/dynamo/conversionif it is a converter fix, so below location would not work.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.
Thanks for reviewing this, looks like I misunderstood what was actually failing and ended up fixing the wrong thing. I see the issue much more clearly now. I’ll take another pass at it with the correct context and follow up if I find something useful.