-
Notifications
You must be signed in to change notification settings - Fork 351
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Error with
aten.view
across Tensor memory (#2464)
- Loading branch information
Showing
3 changed files
with
110 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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
41 changes: 41 additions & 0 deletions
41
py/torch_tensorrt/dynamo/lowering/passes/view_to_reshape.py
This file contains 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,41 @@ | ||
import logging | ||
from typing import Callable, List, Sequence, Tuple | ||
|
||
import torch | ||
from torch_tensorrt.dynamo.lowering.passes.pass_utils import ( | ||
clean_up_graph_after_modifications, | ||
) | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
def view_to_reshape( | ||
gm: torch.fx.GraphModule, sample_inputs: Sequence[torch.Tensor] | ||
) -> torch.fx.GraphModule: | ||
"""Replace aten.view with an equivalent implementation which avoids Tensor memory issues""" | ||
orig, replacement = view_replacement() | ||
|
||
if torch.fx.subgraph_rewriter.replace_pattern(gm, orig, replacement): | ||
gm = clean_up_graph_after_modifications(gm) | ||
logger.debug(f"Graph after replacing view with reshape:\n{gm.graph}") | ||
|
||
return gm | ||
|
||
|
||
def view_replacement() -> ( | ||
Tuple[ | ||
torch.fx.GraphModule, | ||
Callable[[torch.Tensor, List[torch.SymInt]], torch.Tensor], | ||
] | ||
): | ||
"""Constructs the original and replacement functions for view""" | ||
|
||
# Original graph | ||
def orig(input: torch.Tensor, shape: List[torch.SymInt]) -> torch.Tensor: | ||
return torch.ops.aten.view.default(input, shape) | ||
|
||
# Replacement graph | ||
def replacement(input: torch.Tensor, shape: List[torch.SymInt]) -> torch.Tensor: | ||
return torch.ops.aten.reshape.default(input, shape) | ||
|
||
return orig, replacement |
This file contains 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