Repository navigation
Conversation
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/185105
Note: Links to docs will display an error until the docs builds have been completed. ✅ No FailuresAs of commit df67355 with merge base ef7af40 ( This comment was automatically generated by Dr. CI and updates every 15 minutes. |
|
@claude review these changes |
|
Claude finished @jansel's task in 1m 11s —— View job Code Review: PR #185105
Summary: This is a well-motivated fix addressing two real bugs in FindingsThe recompile fix ( Parameter identity preservation ( Shared parameter deduplication (lines 63–69) — Using Grad handling (lines 52–59) — Correctly applies the same compatible-shallow-copy pattern to gradients. Minor observations
VerdictLGTM. The fix is minimal, well-tested, and correctly addresses both reported issues. The parameter-identity semantics align with how |
| with torch.no_grad(): | ||
| param_applied = _move_tensor(param) | ||
| if torch._has_compatible_shallow_copy_type(param, param_applied): | ||
| param.data = param_applied |
There was a problem hiding this comment.
For freshly exported programs, ep.state_dict contains the source module's actual Parameter objects. Assigning param.data therefore moves the source module's parameters, while buffers below are only replaced in the EP dictionary, leaving a retained source module mixed-device and unusable. Either detach EP state before this mutation or establish consistent, documented alias semantics for all state, with a source-module reuse test.
There was a problem hiding this comment.
My agent says
Addressed by removing the in-place parameter-data mutation and retaining the existing out-of-place state replacement. The new regression verifies that both parameters and buffers in the retained source module stay on CPU and remain usable after the exported program moves to CUDA.
| def _move_parameter(param: torch.nn.Parameter) -> torch.nn.Parameter: | ||
| with torch.no_grad(): | ||
| param_applied = _move_tensor(param) | ||
| if torch._has_compatible_shallow_copy_type(param, param_applied): |
There was a problem hiding this comment.
This .data path is unsafe for traceable wrapper-subclass parameters. Export supports parameters such as TwoTensor, whose child tensors live in Python attributes; .data updates only the outer TensorImpl, leaving those children on the original device. Mirror Module._apply's is_traceable_wrapper_subclass/swap_tensors path and add a subclass-parameter regression test.
There was a problem hiding this comment.
My agent says
Addressed by removing the unsafe in-place path. Wrapper-subclass parameters now use the existing out-of-place Tensor.to() path, which moves both the outer wrapper and its child tensors. The new TwoTensor regression verifies that the moved exported state is CUDA-valid while the retained source state remains CPU-valid.
| else v, | ||
| node.meta.get("val"), | ||
| ) | ||
| m.recompile() |
There was a problem hiding this comment.
ExportedProgram deliberately catches SyntaxError during GraphModule code generation and retains the graph for interpreter execution when custom objects cannot be emitted as Python. This unconditional recompile() retries that unsupported generation without a catch, so moving such a valid interpreter-only EP now raises. Preserve the fallback and test an EP containing an in-memory custom-object constant.
There was a problem hiding this comment.
My agent says
Addressed by catching only SyntaxError from GraphModule.recompile(), matching the existing ExportedProgram interpreter-only fallback. The new regression injects an in-memory custom object, rewrites device-bearing IR from CPU to meta, and verifies execution through torch.fx.Interpreter.
[ghstack-poisoned]
`move_to_device_pass` rewrites device-bearing FX nodes and tensor metadata, but it did not recompile the affected GraphModules. Generated Python could therefore retain stale `_assert_tensor_metadata` device arguments and fail when the moved graph ran directly through `ep.graph_module`. Recompile every GraphModule touched by the pass so its callable code matches the rewritten graph. Export also supports interpreter-only graphs whose in-memory custom objects cannot be emitted as Python, so keep that existing fallback by ignoring only the same `SyntaxError` raised during code generation. An earlier version tried to preserve parameter identity with `.data`. Freshly exported state aliases the source module, so that moved source parameters while replacing buffers and left the source module mixed-device. It also corrupted traceable wrapper subclasses by moving only the outer TensorImpl. Retain the existing out-of-place state movement instead, with regressions covering source-module reuse and wrapper-subclass children. I considered overriding `GraphModule.to()` so it rewrites graph IR, but exported graphs are device-specific and `move_to_device_pass` is the supported graph-casting path. Keeping the fix in that pass avoids changing general FX `GraphModule.to()` semantics. Fixes #163323 Generated by my agent Test Plan: ```bash python test/export/test_passes.py TestPasses.test_move_device_to TestPasses.test_move_device_submod TestPasses.test_move_to_device_pass_recompiles_graph_module TestPasses.test_move_to_device_pass_does_not_mutate_source_module TestPasses.test_move_to_device_pass_wrapper_subclass_parameter TestPasses.test_move_to_device_pass_interpreter_only_graph TestPasses.test_move_to_device_pass TestPasses.test_move_device_example_inputs ``` ```bash python test/export/test_export.py TestOneOffModelExportResult.test_assert_tensor_metadata_device_index ``` ```bash lintrunner -a ``` ghstack-source-id: 5ccc2ad Pull-Request: #185105
`move_to_device_pass` rewrites device-bearing FX nodes and tensor metadata, but it did not recompile the affected GraphModules. Generated Python could therefore retain stale `_assert_tensor_metadata` device arguments and fail when the moved graph ran directly through `ep.graph_module`. Recompile every GraphModule touched by the pass so its callable code matches the rewritten graph. Export also supports interpreter-only graphs whose in-memory custom objects cannot be emitted as Python, so keep that existing fallback by ignoring only the same `SyntaxError` raised during code generation. An earlier version tried to preserve parameter identity with `.data`. Freshly exported state aliases the source module, so that moved source parameters while replacing buffers and left the source module mixed-device. It also corrupted traceable wrapper subclasses by moving only the outer TensorImpl. Retain the existing out-of-place state movement instead, with regressions covering source-module reuse and wrapper-subclass children. I considered overriding `GraphModule.to()` so it rewrites graph IR, but exported graphs are device-specific and `move_to_device_pass` is the supported graph-casting path. Keeping the fix in that pass avoids changing general FX `GraphModule.to()` semantics. Fixes #163323 Generated by my agent Test Plan: ```bash python test/export/test_passes.py TestPasses.test_move_device_to TestPasses.test_move_device_submod TestPasses.test_move_to_device_pass_recompiles_graph_module TestPasses.test_move_to_device_pass_does_not_mutate_source_module TestPasses.test_move_to_device_pass_wrapper_subclass_parameter TestPasses.test_move_to_device_pass_interpreter_only_graph TestPasses.test_move_to_device_pass TestPasses.test_move_device_example_inputs ``` ```bash python test/export/test_export.py TestOneOffModelExportResult.test_assert_tensor_metadata_device_index ``` ```bash lintrunner -a ``` ghstack-source-id: 5ccc2ad Pull-Request: #185105
[ghstack-poisoned]
`move_to_device_pass` rewrites device-bearing FX nodes and tensor metadata, but it did not recompile the affected GraphModules. Generated Python could therefore retain stale `_assert_tensor_metadata` device arguments and fail when the moved graph ran directly through `ep.graph_module`. Recompile every GraphModule touched by the pass so its callable code matches the rewritten graph. Export also supports interpreter-only graphs whose in-memory custom objects cannot be emitted as Python, so keep that existing fallback by ignoring only the same `SyntaxError` raised during code generation. An earlier version tried to preserve parameter identity with `.data`. Freshly exported state aliases the source module, so that moved source parameters while replacing buffers and left the source module mixed-device. It also corrupted traceable wrapper subclasses by moving only the outer TensorImpl. Retain the existing out-of-place state movement instead, with regressions covering source-module reuse and wrapper-subclass children. I considered overriding `GraphModule.to()` so it rewrites graph IR, but exported graphs are device-specific and `move_to_device_pass` is the supported graph-casting path. Keeping the fix in that pass avoids changing general FX `GraphModule.to()` semantics. Fixes #163323 Generated by my agent Test Plan: ```bash python test/export/test_passes.py TestPasses.test_move_device_to TestPasses.test_move_device_submod TestPasses.test_move_to_device_pass_recompiles_graph_module TestPasses.test_move_to_device_pass_does_not_mutate_source_module TestPasses.test_move_to_device_pass_wrapper_subclass_parameter TestPasses.test_move_to_device_pass_interpreter_only_graph TestPasses.test_move_to_device_pass TestPasses.test_move_device_example_inputs ``` ```bash python test/export/test_export.py TestOneOffModelExportResult.test_assert_tensor_metadata_device_index ``` ```bash lintrunner -a ``` ghstack-source-id: 834edda Pull-Request: #185105
|
@pytorchbot merge |
Merge startedYour change will be merged once all checks pass (ETA 0-4 Hours). Learn more about merging in the wiki. Questions? Feedback? Please reach out to the PyTorch DevX Team |
pytorch#196974 already moved these tests onto instantiate_device_type_tests for CUDA and XPU, and pytorch#185105 added the CUDA recompile regressions. Keep that code, and open the device class to other non-CPU backends with except_for="cpu" while retaining allow_xpu=True. Test Plan: ``` python -m pytest test/export/test_passes.py --collect-only -q # 28 tests collected on CPU. TestMoveToDevicePassDevice is not generated without an accelerator, which matches except_for="cpu". ```
…rators Four move_to_device_pass tests sat in TestMoveToDevicePassCuda behind skipIf(not TEST_CUDA) with "cuda:0" hard-coded, so XPU and other accelerators skipped them. Nothing in them is CUDA-specific. They check that the moved GraphModule and its nested torch.cond submodules are recompiled, that the source module's parameters and buffers are not mutated, and that wrapper-subclass parameters move with their inner tensors. Root cause: pytorch#185105 added the tests behind a TEST_CUDA gate. pytorch#196974 moved the four older move_to_device_pass tests into the device-generic TestMoveToDevicePassDevice but left these behind, and pytorch#191079 then tagged their class CUDA to match its gate. Move them into TestMoveToDevicePassDevice with a device parameter, drop the gates and replace each "cuda:0" with device. The instantiated primary device always carries an index (cuda:N, xpu:N, <privateuse1>:N), so the code and device-equality assertions behave as before on CUDA. After the previous commit the CUDA class is empty, so delete it and the now-unused TEST_CUDA import. XPU, PrivateUse1 and HPU gain the four tests. CUDA keeps them as TestMoveToDevicePassDeviceCUDA::<name>_cuda. On a machine without an accelerator they are no longer collected instead of being reported as skipped, the same trade-off pytorch#196974 made. Widening the gate to TEST_CUDA or TEST_XPU was the alternative; it keeps the hard-coded device and leaves PrivateUse1 out. See intel/torch-xpu-ops#5994. Test Plan: The same AST comparison as the previous commit: the four methods match the originals once device maps back to 'cuda:0' and the device parameter and TEST_CUDA skip are dropped. The other 40 methods are unchanged. On an Intel Data Center GPU Max 1550: ``` python test/export/test_passes.py -v -k move_device -k move_to_device python test/export/test_passes.py ``` All 8 TestMoveToDevicePassDeviceXPU tests pass, 4 of them new. The whole file goes from OK (skipped=4, expected failures=2) to OK (expected failures=2) with the same 39 tests. The only ID changes are TestMoveToDevicePassCuda::<name> -> TestMoveToDevicePassDeviceXPU::<name>_xpu. CUDA was not run locally. Authored with an AI assistant (Claude Code). Co-Authored-By: Claude Opus 5.5 <[email protected]>
…rators Four move_to_device_pass tests sat in TestMoveToDevicePassCuda behind skipIf(not TEST_CUDA) with "cuda:0" hard-coded, so XPU and other accelerators skipped them. Nothing in them is CUDA-specific. They check that the moved GraphModule and its nested torch.cond submodules are recompiled, that the source module's parameters and buffers are not mutated, and that wrapper-subclass parameters move with their inner tensors. Root cause: pytorch#185105 added the tests behind a TEST_CUDA gate. pytorch#196974 moved the four older move_to_device_pass tests into the device-generic TestMoveToDevicePassDevice but left these behind, and pytorch#191079 then tagged their class CUDA to match its gate. Move them into TestMoveToDevicePassDevice with a device parameter, drop the gates and replace each "cuda:0" with device. The instantiated primary device always carries an index (cuda:N, xpu:N, <privateuse1>:N), so the code and device-equality assertions behave as before on CUDA. After the previous commit the CUDA class is empty, so delete it and the now-unused TEST_CUDA import. XPU, PrivateUse1 and HPU gain the four tests. CUDA keeps them as TestMoveToDevicePassDeviceCUDA::<name>_cuda. On a machine without an accelerator they are no longer collected instead of being reported as skipped, the same trade-off pytorch#196974 made. Widening the gate to TEST_CUDA or TEST_XPU was the alternative; it keeps the hard-coded device and leaves PrivateUse1 out. See intel/torch-xpu-ops#5994. Test Plan: The same AST comparison as the previous commit: the four methods match the originals once device maps back to 'cuda:0' and the device parameter and TEST_CUDA skip are dropped. The other 40 methods are unchanged. On an Intel Data Center GPU Max 1550: ``` python test/export/test_passes.py -v -k move_device -k move_to_device python test/export/test_passes.py ``` All 8 TestMoveToDevicePassDeviceXPU tests pass, 4 of them new. The whole file goes from OK (skipped=4, expected failures=2) to OK (expected failures=2) with the same 39 tests. The only ID changes are TestMoveToDevicePassCuda::<name> -> TestMoveToDevicePassDeviceXPU::<name>_xpu. CUDA was not run locally. Authored with an AI assistant (Claude Code). Co-Authored-By: Claude Opus 5.5 <[email protected]>
…rators Four move_to_device_pass tests sat in TestMoveToDevicePassCuda behind skipIf(not TEST_CUDA) with "cuda:0" hard-coded, so XPU and other accelerators skipped them. Nothing in them is CUDA-specific. They check that the moved GraphModule and its nested torch.cond submodules are recompiled, that the source module's parameters and buffers are not mutated, and that wrapper-subclass parameters move with their inner tensors. Root cause: pytorch#185105 added the tests behind a TEST_CUDA gate. pytorch#196974 moved the four older move_to_device_pass tests into the device-generic TestMoveToDevicePassDevice but left these behind, and pytorch#191079 then tagged their class CUDA to match its gate. Move them into TestMoveToDevicePassDevice with a device parameter, drop the gates and replace each "cuda:0" with device. The instantiated primary device always carries an index (cuda:N, xpu:N, <privateuse1>:N), so the code and device-equality assertions behave as before on CUDA. After the previous commit the CUDA class is empty, so delete it and the now-unused TEST_CUDA import. XPU, PrivateUse1 and HPU gain the four tests. CUDA keeps them as TestMoveToDevicePassDeviceCUDA::<name>_cuda. On a machine without an accelerator they are no longer collected instead of being reported as skipped, the same trade-off pytorch#196974 made. Widening the gate to TEST_CUDA or TEST_XPU was the alternative; it keeps the hard-coded device and leaves PrivateUse1 out. See intel/torch-xpu-ops#5994. Test Plan: The same AST comparison as the previous commit: the four methods match the originals once device maps back to 'cuda:0' and the device parameter and TEST_CUDA skip are dropped. The other 40 methods are unchanged. On an Intel Data Center GPU Max 1550: ``` python test/export/test_passes.py -v -k move_device -k move_to_device python test/export/test_passes.py ``` All 8 TestMoveToDevicePassDeviceXPU tests pass, 4 of them new. The whole file goes from OK (skipped=4, expected failures=2) to OK (expected failures=2) with the same 39 tests. The only ID changes are TestMoveToDevicePassCuda::<name> -> TestMoveToDevicePassDeviceXPU::<name>_xpu. CUDA was not run locally. Authored with an AI assistant (Claude Code). Co-Authored-By: Claude Opus 5.5 <[email protected]>
Stack from ghstack (oldest at bottom):
move_to_device_passrewrites device-bearing FX nodes and tensor metadata, but it did not recompile the affected GraphModules. Generated Python could therefore retain stale_assert_tensor_metadatadevice arguments and fail when the moved graph ran directly throughep.graph_module.Recompile every GraphModule touched by the pass so its callable code matches the rewritten graph. Export also supports interpreter-only graphs whose in-memory custom objects cannot be emitted as Python, so keep that existing fallback by ignoring only the same
SyntaxErrorraised during code generation.An earlier version tried to preserve parameter identity with
.data. Freshly exported state aliases the source module, so that moved source parameters while replacing buffers and left the source module mixed-device. It also corrupted traceable wrapper subclasses by moving only the outer TensorImpl. Retain the existing out-of-place state movement instead, with regressions covering source-module reuse and wrapper-subclass children.I considered overriding
GraphModule.to()so it rewrites graph IR, but exported graphs are device-specific andmove_to_device_passis the supported graph-casting path. Keeping the fix in that pass avoids changing general FXGraphModule.to()semantics.Fixes #163323
Generated by my agent
Test Plan: