-
Notifications
You must be signed in to change notification settings - Fork 410
Fix complex rewrite device mismatch #4513
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
jloftin-nv
wants to merge
1
commit into
pytorch:main
Choose a base branch
from
jloftin-nv:dev-jloftin-complex-mismatch
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 |
|---|---|---|
|
|
@@ -38,6 +38,7 @@ | |
| from torch_tensorrt.dynamo.lowering.passes.complex_graph_rewrite import ( | ||
| complex_graph_detection, | ||
| ) | ||
| from torch_tensorrt.dynamo.lowering.passes.constant_folding import constant_fold | ||
|
|
||
| # --------------------------------------------------------------------------- | ||
| # 1. Infrastructure | ||
|
|
@@ -1251,7 +1252,9 @@ def forward(self, z, opt): | |
|
|
||
|
|
||
| @pytest.mark.unit | ||
| @pytest.mark.parametrize("scale", [torch.tensor(2.0), 2, True], ids=["tensor", "int", "bool"]) | ||
| @pytest.mark.parametrize( | ||
| "scale", [torch.tensor(2.0), 2, True], ids=["tensor", "int", "bool"] | ||
| ) | ||
| def test_non_tensor_scalar_placeholder(scale): | ||
| class RotaryComplex(nn.Module): | ||
| def forward(self, xq, freqs_cis, scale): | ||
|
|
@@ -1261,4 +1264,93 @@ def forward(self, xq, freqs_cis, scale): | |
|
|
||
| xq = torch.randn(1, 2, 4, 8) | ||
| freqs = torch.polar(torch.ones(1, 2, 4, 4), torch.randn(1, 2, 4, 4)) | ||
| _export_and_lower(RotaryComplex(), (xq, freqs, scale)) | ||
| gm = _export_and_lower(RotaryComplex(), (xq, freqs, scale)) | ||
|
|
||
| placeholder_vals = [ | ||
| node.meta["val"] | ||
| for node in gm.graph.nodes | ||
| if node.op == "placeholder" and "val" in node.meta | ||
| ] | ||
| if isinstance(scale, torch.Tensor): | ||
| # A 0-dim tensor stays a tensor: smoke test only, not guard coverage | ||
| assert placeholder_vals, "export produced no placeholder metadata" | ||
| else: | ||
| # The guard only runs for non-tensor vals; fail if export specialized it away | ||
| kinds = [type(val).__name__ for val in placeholder_vals] | ||
| assert any(not isinstance(val, torch.Tensor) for val in placeholder_vals), ( | ||
| f"scale={scale!r} did not survive export as a non-tensor " | ||
| f"placeholder; placeholder vals were {kinds}" | ||
| ) | ||
|
|
||
|
|
||
| @pytest.mark.unit | ||
| @pytest.mark.skipif(not torch.cuda.is_available(), reason="CUDA required") | ||
| def test_cpu_offloaded_frozen_param_is_aligned(): | ||
| """A CPU-offloaded folded constant keeping cuda metadata must be realigned.""" | ||
|
|
||
| class M(nn.Module): | ||
| def __init__(self) -> None: | ||
| super().__init__() | ||
| # Non-scalar, so scalar promotion cannot excuse a device mismatch | ||
| self.register_buffer("w", torch.randn(3, 4)) | ||
|
|
||
| def forward(self, z): | ||
| return (z * z) * (self.w * 2) | ||
|
|
||
| model = M().cuda().eval() | ||
| z = torch.randn(3, 4, dtype=torch.complex64, device="cuda") | ||
| with torch.no_grad(): | ||
| exp = torch.export.export(model, (z,)) | ||
| gm = exp.module() | ||
|
|
||
| # Offloading stores the constant on CPU; its inherited meta still says cuda | ||
| gm = constant_fold(gm, CompilationSettings(offload_module_to_cpu=True)) | ||
|
|
||
| offloaded = [ | ||
| node | ||
| for node in gm.graph.nodes | ||
| if node.op == "get_attr" | ||
| and str(node.target).startswith("_frozen_param") | ||
| and isinstance(node.meta.get("val"), torch.Tensor) | ||
| and node.meta["val"].device.type == "cuda" | ||
| and getattr(gm, str(node.target)).device.type == "cpu" | ||
| ] | ||
| assert offloaded, "expected a CPU-offloaded _frozen_param with cuda metadata" | ||
|
|
||
| complex_graph_detection(gm, CompilationSettings()) | ||
|
|
||
| for node in offloaded: | ||
| assert ( | ||
| getattr(gm, str(node.target)).device.type == "cpu" | ||
| ), f"{node.target}: propagation must not move the module's parameters" | ||
|
|
||
|
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 test would pass even without the fix. 0 dim would pass in eager mode and fake tensor mode,but then if you do torch.tensor([2.0]) triggers a mismatch too early FakeTensorDeviceMismatchError—in eager/export—rather than specifically during post-rewrite metadata propagation. |
||
|
|
||
| @pytest.mark.unit | ||
| @pytest.mark.skipif(not torch.cuda.is_available(), reason="CUDA required") | ||
| def test_independent_cpu_branch_stays_on_cpu(): | ||
| """An unrelated CPU attribute must stay on CPU despite a cuda placeholder.""" | ||
|
|
||
| class M(nn.Module): | ||
| def __init__(self) -> None: | ||
| super().__init__() | ||
| self.register_buffer("cpu_table", torch.arange(4)) | ||
|
|
||
| def forward(self, z): | ||
| return z * z, self.cpu_table + 1 | ||
|
|
||
| # Module stays on CPU so the table is genuinely CPU, not offloaded | ||
| z = torch.randn(3, 4, dtype=torch.complex64, device="cuda") | ||
| gm = _export_and_lower(M().eval(), (z,)) | ||
|
|
||
| cpu_branch = [ | ||
| node | ||
| for node in gm.graph.nodes | ||
| if isinstance(node.meta.get("val"), torch.Tensor) | ||
| and node.meta["val"].dtype == torch.int64 | ||
| and tuple(node.meta["val"].shape) == (4,) | ||
| ] | ||
| assert cpu_branch, "the independent CPU branch disappeared from the graph" | ||
| for node in cpu_branch: | ||
| assert ( | ||
| node.meta["val"].device.type == "cpu" | ||
| ), f"{node.name}: CPU branch moved to {node.meta['val'].device}" | ||
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.
Every tensor attribute is moved to the first CUDA input’s device.
The new fetch_attr changes all tensor attributes whenever any CUDA placeholder exists. That includes intentionally CPU attributes or an independent CPU branch, producing incorrect device metadata. Something like below
Maybe we should add