-
Notifications
You must be signed in to change notification settings - Fork 410
complex _reshape_copy / _to_copy #4516
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
base: main
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,7 +12,7 @@ | |
| from torch_tensorrt.dynamo.lowering.passes.pass_utils import ( | ||
| clean_up_graph_after_modifications, | ||
| ) | ||
| from torch_tensorrt.dynamo.utils import COMPLEX_DTYPES | ||
| from torch_tensorrt.dynamo.utils import COMPLEX_DTYPES, COMPLEX_TO_REAL_DTYPE | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
@@ -1282,6 +1282,27 @@ def _rewrite_scalar_tensor(self, node: Node) -> bool: | |
| self.gm.graph.erase_node(node) | ||
| return True | ||
|
|
||
| @_complex_unpacker(torch.ops.aten._to_copy.default) | ||
| def _rewrite_to_copy(self, node: Node) -> bool: | ||
| # complex target: remap dtype, [..., 2] layout unchanged | ||
| # real target: the cast discards the imaginary part, so select re | ||
| kwargs = dict(node.kwargs) | ||
| dtype = kwargs.get("dtype") | ||
| inp = node.args[0] | ||
| to_real = dtype is not None and dtype not in COMPLEX_DTYPES | ||
| if dtype is not None and not to_real: | ||
| kwargs["dtype"] = COMPLEX_TO_REAL_DTYPE[dtype] | ||
| with SubgraphBuilder(self.gm.graph, node) as b: | ||
| if to_real: | ||
| inp = b(torch.ops.aten.select.int, inp, -1, 0) | ||
| out = b(torch.ops.aten._to_copy.default, inp) | ||
|
Collaborator
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. This always selects only the real component before casting. For boolean conversion eg: 0+1j, it selects 0, which becomes False while PyTorch returns True because the imaginary component is nonzero. |
||
| out.kwargs = kwargs | ||
| if not to_real: | ||
| out.meta["is_complex_layout"] = True | ||
| node.replace_all_uses_with(out) | ||
| self.gm.graph.erase_node(node) | ||
| return True | ||
|
|
||
|
Collaborator
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. this seems incorrect.
|
||
| # ------------------------------------------------------------------ | ||
| # Shape-manipulation handlers | ||
| # | ||
|
|
@@ -1297,6 +1318,7 @@ def _rewrite_scalar_tensor(self, node: Node) -> bool: | |
| torch.ops.aten.reshape.default, | ||
| torch.ops.aten.view.default, | ||
| torch.ops.aten._unsafe_view.default, | ||
| torch.ops.aten._reshape_copy.default, | ||
| ) | ||
| def _rewrite_reshape_view(self, node: Node) -> bool: | ||
| # Append 2 to the target shape so the trailing real/imag dim is | ||
|
|
||
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.
the rewrite assumes that any tensor being converted to a complex dtype is already stored in the internal [..., 2] real/imaginary layout. For a real tensor converted to complex, it does not create the required zero imaginary component before marking the result as complex. For example, [1, 2] should become [[1, 0], [2, 0]], but the rewrite leaves it as [1, 2]. so real to complex would fail