Repository navigation
[Testcase Refactoring] Device-agnostic move_to_device_pass tests via instantiate + device - #191079
SHIHAIFENG15 wants to merge 9 commits into
Conversation
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/191079
Note: Links to docs will display an error until the docs builds have been completed. ✅ No FailuresAs of commit c31d94a with merge base ff30875 ( This comment was automatically generated by Dr. CI and updates every 15 minutes. |
63746ec to
04a237e
Compare
|
@pytorchbot label "module: tests/graph" |
can-gaa-hou
left a comment
There was a problem hiding this comment.
Thanks for the PR! Overall lgtm minor some small nits. Please take a look.
| @unittest.skipIf(not TEST_CUDA, "requires cuda") | ||
| def test_move_to_device_pass(self): | ||
| def test_move_device_example_inputs(self, device): | ||
| device_type = str(device).split(":", 1)[0] |
There was a problem hiding this comment.
| device_type = str(device).split(":", 1)[0] | |
| device_type = torch.device(device).type |
| self.assertEqual(ep_moved.example_inputs[1]["z"].device, torch.device(device0)) | ||
|
|
||
| def test_move_to_device_pass(self, device): | ||
| device_type = str(device).split(":", 1)[0] |
| if device_type not in ("cuda", "xpu"): | ||
| self.skipTest("move_to_device_pass RNN is supported only on cuda/xpu") |
There was a problem hiding this comment.
If this test is not device-generic, please add another device-specific class (e.g. TestPassesCUDA or something?) and move to it. Try your best not to explicitly skip a specific device in the test unless you have to.
There was a problem hiding this comment.
Thanks for the review. Addressed the nits:
- use
torch.device(device).typeinstead of string split - move the RNN case into
TestPassesRNNwithonly_for=("cuda", "xpu")(no in-method device skip)
| self.assertEqual(ep_cuda.example_inputs[0][1].device, torch.device("cuda:0")) | ||
| self.assertEqual(ep_cuda.example_inputs[1]["z"].device, torch.device("cuda:0")) | ||
| instantiate_device_type_tests(TestPassesDevice, globals(), except_for="cpu") | ||
| instantiate_device_type_tests(TestPassesRNN, globals(), except_for="cpu") |
There was a problem hiding this comment.
Why is it necessary to split the TestPassesRNN test case when the accelerator types of the test cases are the same?
There was a problem hiding this comment.
Thanks — merged TestPassesRNN into TestPassesDevice since both used the same ACCELERATOR classification and except_for="cpu" instantiate settings.
fffrog
left a comment
There was a problem hiding this comment.
Overall look ok to me ,thank you for your contributions.
| add = torch.ops.aten.add.Tensor(to, to); to = None | ||
| return (add,) | ||
| """, | ||
| ) | ||
|
|
||
| @unittest.skipIf(not TEST_CUDA, "requires cuda") | ||
| def test_move_to_device_pass(self): |
There was a problem hiding this comment.
Why swap the positions of test_move_to_device_pass and test_move_device_example_inputs? This would increase the difficulty of review.
There was a problem hiding this comment.
The method order follows the original TestPasses class in main (move_device_example_inputs → move_device_submod → move_device_to → move_to_device_pass).
|
Updated the PR to preserve the complete device injected by |
8033836 to
c9ed1b5
Compare
|
@claude review this pr with pr review skill |
|
Claude finished @can-gaa-hou's task in 14m 17s —— View job Reviewed with the Full PR review (click to expand)PR Review: #191079SummaryConverts the four Testing1. self.assertExpectedInline(
ep.graph_module.code.strip("\n"),
f"""\
def forward(self, x):
_assert_tensor_metadata_default = ... device = '{device_type}' ...expecttest's accept path rewrites the source literal in place. Two problems follow:
These are 30-line multi-line graph goldens; The established pattern is a static golden plus post-hoc normalization — see expected = """{
'input_nodes':[
"[[10,22],[22,1],torch.float32,device(type='cuda',index=0),0]", ...expected = expected.replace("cuda", GPU_TYPE)
self.assertExpectedInline(remove_white_space(cache_key), remove_white_space(expected))Applying that here keeps the literal static (so 2.
python test/export/test_passes.py -v -k TestPassesDeviceXPU
python test/export/test_passes.py -v -k TestPassesDeviceCUDA3. The GRU test is known-failing on the exact backend this PR exists to enable, with no skip and no tracking issue. The PR body states 4. On a CPU-only host these four tests now vanish rather than skip.
Code Quality5.
Note for reviewers: 6.
Verified as correct (no action needed)Recorded only because they were plausible failure modes I checked and ruled out, so they need not be re-investigated:
RecommendationRequest Changes Finding 1 is a concrete regression in test maintainability with a cheap, established fix already used elsewhere in the repo. Finding 2 asks only for evidence that the device coverage this PR advertises actually passes — given two goldens and an on-device GRU, an NPU-only test run is not enough to land XPU and CUDA enablement. Finding 3 should be resolved by a tracking-issue reference rather than by weakening the test. |
…vice
Migrate CUDA-hardcoded / CPU-only device checks in test_passes.py to a
single device-agnostic class `TestPassesDevice` using
instantiate_device_type_tests with an injected `device`, so accelerators
including privateuse1 (NPU) are covered for the move_to_device_pass suite.
The move_to_device tests are split out of the TestExportPasses suite into
`TestPassesDevice` (except_for="cpu"). Golden strings are kept as
assertExpectedInline but parameterized by `device_type` (f-string), so the
expected graph code follows the injected device rather than a literal
"cuda".
The RNN case is scoped to cuda/xpu only via a per-method runtime skip
(`device_type not in ("cuda","xpu")`): NPU fp32 GRU fails after
move_to_device_pass via DynamicGRUV2, so it is not whitelisted. A single
class is used instead of two because instantiate_device_type_tests
applies one device filter per class; the RNN exclusion is therefore done
per-method. Note that on NPU the injected `device` strings as "npu:0"
(torch_npu renames privateuse1), so the skip keys on the device string
rather than the "privateuse1" dispatch name.
The class retains `@skipIfTorchDynamo("recursively running dynamo on
export is unlikely")` (in addition to `@unittest.skipIf(not
is_dynamo_supported(), ...)`) carried over from the original TestPasses
class on main; dropping it would let the tests run under dynamo
unintendedly.
No HAS_GPU/GPU_TYPE used as the NPU entry path; device entry is
instantiate + injected `device`.
Test Plan:
```
# CPU (except_for="cpu" -> 0 tests, confirms no import/syntax regression)
source /home/y00839695/npu-pytorch-setup/venv_cpu/bin/activate
python test/export/test_passes.py -v -k TestPassesDevice
# NPU (after sourcing CANN env + venv_npu)
export PYTORCH_TESTING_DEVICE_ONLY_FOR=npu
python test/export/test_passes.py -v -k TestPassesDevice
# 3 move_device tests ok, test_move_to_device_pass skipped
# (RNN supported only on cuda/xpu)
```
Tag TestPasses as GENERIC and TestPassesDevice (instantiate) as ACCELERATOR for --hw-classification filtering. Test Plan: ``` python test/export/test_passes.py --hw-classification ACCELERATOR -v -k TestPassesDevice python test/export/test_passes.py --hw-classification GENERIC -v -k TestPasses ```
Use torch.device(device).type instead of string split. Move the RNN
move_to_device_pass case into TestPassesRNN with only_for=("cuda", "xpu")
instead of an in-method device skipTest.
Test Plan:
```
python test/export/test_passes.py -v -k 'TestPassesDevice or TestPassesRNN'
```
Replace only_for=("cuda", "xpu") with except_for="cpu" so non-CUDA
accelerators are covered by instantiate, matching other device-agnostic
move_to_device_pass coverage.
Test Plan:
```
python test/export/test_passes.py -v -k 'TestPassesDevice or TestPassesRNN'
```
TestPassesRNN used the same ACCELERATOR hw_classification and except_for="cpu" instantiate settings as TestPassesDevice, so keep a single class and move test_move_to_device_pass onto TestPassesDevice. Test Plan: ``` python test/export/test_passes.py -v -k TestPassesDevice ```
The device-agnostic conversion reconstructed every target as device zero, which ignored the primary device selected by the test framework. Use the complete injected device for tensor placement, remapping, and assertions while retaining device-type-only values where the API requires them, and opt the class into XPU instantiation. Test Plan: ``` git diff --check ``` `lintrunner -a` was attempted but could not run because `uv` is unavailable in the local environment. Authored with AI assistant.
The original tests were CUDA-only, so do not instantiate XPU. Replace f-string goldens with a static cuda string plus replace() so EXPECTTEST_ACCEPT can still rewrite the literal. Test Plan: ``` python test/export/test_passes.py -v -k TestPassesDevice ```
|
@pytorchbot started a rebase job onto refs/remotes/origin/viable/strict. Check the current status here |
|
Rebase failed due to Command |
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". ```
|
@pytorchbot merge |
Merge failedReason: Approvers from one of the following sets are needed:
|
fffrog
left a comment
There was a problem hiding this comment.
SGTM, thank you for fixing this one
There was a problem hiding this comment.
Hi @SHIHAIFENG15 , this one should be CUDA, right? Could you mind fixing it while you are at it?
There was a problem hiding this comment.
Sure, fixed it.
TestMoveToDevicePassCuda hardcodes cuda:0 and skips unless CUDA is available, so its hardware classification is CUDA rather than the accelerator-generic label. Test Plan: ``` .venv/bin/lintrunner -a test/export/test_passes.py ``` lintrunner reported no lint issues. This machine has no CUDA, so the tests themselves still skip; loading the module shows TestMoveToDevicePassCuda.hw_classification is HardwareClassification.CUDA. Authored with an AI assistant.
|
|
|
@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 |
Merge failedReason: 3 mandatory check(s) failed. The first few are: Dig deeper by viewing the failures on hud |
|
Could you approve the workflows so CI can run? @KarhouTam |
|
@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 |
…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]>
Summary
Make
move_to_device_passtests intest_passes.pydevice-agnostic viainstantiate_device_type_testsand injecteddevice, so accelerators including privateuse1 (NPU) are covered.Changes
move_to_devicecases intoTestPassesDevicewithhw_classification = ACCELERATORandexcept_for="cpu"."cuda"/@requires_gpuwith the injecteddevice.assertExpectedInlinegoldens, parameterized bydevice_type.TestPassesDeviceclass (no separateTestPassesRNN).Test Plan
Local NPU:
python test/export/test_passes.py -v -k TestPassesDevice
(with PYTORCH_TESTING_DEVICE_ONLY_FOR=privateuse1, import torch_npu before run)
Result: Ran 4 tests — 3 passed, 1 error
PASS: test_move_device_to_npu, test_move_device_submod_npu, test_move_device_example_inputs_npu
ERROR: test_move_to_device_pass_npu — DynamicGRUV2 unsupported on NPU (float32 GRU); torch_npu backend gap, not a pass regression
This is a torch_npu backend gap (DynamicGRUV2), not a move_to_device_pass bug. The case stays on the accelerator class (no per-device skip). Tracking: https://gitcode.com/Ascend/pytorch/issues/3886
CPU: not run (TestPassesDevice uses except_for="cpu").
@wjlFlyer @pjyFight @FuDdd @fffrog @lyriexs666 @jishuangfeng @JamesD18 @JiasenTian