Repository navigation
[Testcase Refactoring] Migrate test_functional_api to device-type instantiation - #202776
AbigailFernandes wants to merge 3 commits into
Conversation
PR Status: Approved 🚀Please fix all CI failures and trigger merge by commenting "@pytorchbot merge". ✅ AUTOMATED REVIEW - OUTDATED (earlier commit) - Ready for human review:
Findings (6): Reviewed commit: 🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/202776
Note: Links to docs will display an error until the docs builds have been completed. ✅ No FailuresAs of commit 0a24c06 with merge base f864236 ( This comment was automatically generated by Dr. CI and updates every 15 minutes. |
fffrog
left a comment
There was a problem hiding this comment.
@AbigailFernandes, Hello, thank you so much for your updates.
I think there might be some details we haven't fully discussed. If you have time, you can refer to this link for more information.
Also, I'll provide some examples later to help you understand what we want to achieve. Thank you for your patience.
| elif acc := torch.accelerator.current_accelerator(check_available=True): | ||
| # Any other accelerator, for example an out-of-tree backend. | ||
| devices.append(acc.type) | ||
| DEVICE = acc.type |
There was a problem hiding this comment.
Hi @AbigailFernandes, thank you for contributing this file.
As we discussed before, we hope all the global variables describing accelerator infos should be removed and turn to use instantiate_device_type_test where the device will be passed in according to the current environment.
There was a problem hiding this comment.
Updated the PR to use instantiate_device_type_test
There was a problem hiding this comment.
And the current class is instantiated via instantiate_parametrized_tests, I think it is not correct, we should add device parameter for every test function in this class, and finally use instantiate_device_type_test to instantiate it.
There was a problem hiding this comment.
Yep, removed that
There was a problem hiding this comment.
Code related to BACKEND should also be removed. BACKEND indicates that these test cases should be ACCELERATOR, therefore these test cases must include the device parameter. This way, we can obtain the corresponding BACKEND through the device parameter in each test function, which not only provides maximum flexibility but also allows us to utilize the skip or injection capabilities of instantiate_device_type_test.
fffrog
left a comment
There was a problem hiding this comment.
Hi @AbigailFernandes thank you for contributing this file.
The changes look great to me, thank you.
| @@ -99,6 +62,8 @@ def new_subgroups(group_size: int, pg_tag=None): | |||
|
|
|||
| @skipIfHpu | |||
There was a problem hiding this comment.
I believe we can safely remove this decorator, right :D
| @@ -189,6 +154,8 @@ def test_expand_device_mesh_tuple(self): | |||
|
|
|||
| @skipIfHpu | |||
There was a problem hiding this comment.
I believe we can safely remove this decorator, right :D
|
|
||
|
|
||
| @instantiate_parametrized_tests | ||
| @skipIfHpu |
There was a problem hiding this comment.
We can also delete this line, because instantiate_device_type_tests will not instantiate this test class in HPU when HPU is not available.
| @@ -435,6 +388,8 @@ def test_all_reduce(self): | |||
|
|
|||
| @skipIfHpu | |||
| if ( | ||
| BACKEND == dist.Backend.NCCL or BACKEND == dist.Backend.XCCL | ||
| ) and torch.accelerator.device_count() < self.world_size: | ||
| dist.get_default_backend_for_device(device) != dist.Backend.GLOO |
There was a problem hiding this comment.
| dist.get_default_backend_for_device(device) != dist.Backend.GLOO | |
| self.backend(device) != dist.Backend.GLOO |
There was a problem hiding this comment.
Delete this line, please. It will prevent other accelerator from running this one.
There was a problem hiding this comment.
| @skipIf(not has_triton(), "Inductor+gpu needs triton and recent GPU arch", device_type=("cuda", "xpu")) |
There are some other accelerator that implement inductor without triton.
| @@ -649,7 +601,7 @@ def allreduce(t, pg): | |||
| compiled_allreduce(torch.randn(8, device=device), self.pg) | |||
|
|
|||
| @unittest.skipIf(not HAS_GPU, "Inductor+gpu needs triton and recent GPU arch") | |||
There was a problem hiding this comment.
ditto, using skipIf from torch/testing/_internal/common_device_type.py
There was a problem hiding this comment.
ditto, using skipIf from torch/testing/_internal/common_device_type.py
There was a problem hiding this comment.
Automated review of 57d2687: nothing blocking; ready for human review.
The PR moves test/distributed/test_functional_api.py from global DEVICE/devices/BACKEND variables to instantiate_device_type_tests, gives every class an hw_classification, and removes the file from the test-linter allowlist; earlier blocking findings and fffrog's comments are addressed at HEAD. Recommendation: Approve, with only non-blocking points left. fffrog's latest review is still CHANGES_REQUESTED, so they need to re-review before this can land.
⚪ 6 non-blocking findings
Non-blocking, carried over: hw_classification = HardwareClassification.GENERIC sits above the TestAllGatherViewOptimization docstring, so the string is now a no-op and __doc__ is None. Move the attribute below the docstring. The linter's _get_hw_classification scans the whole class body, so lint still passes.
Non-blocking, carried over: the exit_if_lt_x_accelerators(self.world_size) calls here and at :658, inside @ with_comms tests, can no longer fire. with_comms (:508-512) already exits when self.backend(device) != GLOO and torch.accelerator.device_count() < self.world_size, and with except_for=("cpu",) and no allow_mps every variant maps to a non-gloo backend. Before this PR these calls still mattered on HPU, so the PR made them dead. The call at :609 (test_tracing_with_fakepg, no with_comms) is still needed.
Non-blocking, carried over: the comment at :491-493 still says to "append an elif with the conditional and appropriate device count function", but exit_if_lt_x_accelerators only uses torch.accelerator. The PR deleted the two matching "add an elif" notes (the NOTE block and the BACKEND comment) but left this one.
Non-blocking: the latest commit turns on paths no one has run. Before, @ skipIfHpu skipped TestExpand, TestPgTag, TestGradCollectives and all of TestTraceableCollectives on HPU hosts, and HAS_GPU/requires_accelerator_dist_backend skipped the three tracing tests on HPU. Now an HPU host also generates TestTraceableCollectivesHPU, and all three tracing tests run on HPU and PrivateUse1 variants (two call torch.compile). fffrog asked for this, so not a regression, but HPU/CUDA/XPU were not run and on Trainium the same TestTraceableCollectives pattern failed 7/7; someone with HPU access should confirm. Also, the PR has no ciflow/trunk label; multi-GPU CUDA runs of this file are only in trunk.yml/periodic.yml, so the changed with_comms and new TestTraceableCollectivesCUDA variants won't run before merge unless ciflow/trunk is added.
Non-blocking: the PR description no longer matches the code. It says except_for=("cpu",) means "the set of devices does not change", but the old only_for=DEVICE never matched PrivateUse1 (the three DistributedTestBase classes now get PrivateUse1 variants), and the old elif chain picked only one of CUDA/XPU (a host with both now gets both). The rename note covers only the method name; class names change too (TestTraceableCollectives.test_broadcast_device_cuda -> TestTraceableCollectivesCUDA.test_broadcast_cuda), so DISABLED issues or slow-test entries keyed on old IDs stop matching. "What changes" omits the @ skipIfHpu removal, the requires_accelerator_dist_backend removal and the HAS_GPU->device-type skipIf switch; "Their form does not change" is no longer true for TestExpand, TestPgTag, TestGradCollectives. The Trainium results predate the skip changes and don't cover tracing tests now running on PrivateUse1.
Non-blocking, for awareness: the device-type skipIf (:590-627) raises SkipTest inside the test body. DistributedTestBase.setUp has already spawned world_size processes by then, so on a CUDA/XPU host without Triton each skip starts all ranks first, and the parent reports the generic "Test skipped at subprocess level" instead of the Triton reason. The old unittest.skipIf skipped in the parent before any process started. This follows fffrog's suggestion.
This review is AI-generated and advisory. Each new automated review replaces the previous one. Report a bad review
Withdrawn: the automated review of 57d2687 found nothing blocking.
|
@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: 1 mandatory check(s) failed. The first few are: Dig deeper by viewing the failures on hud |
Pick the device for the accelerator-only tests in test_functional_api.py from torch.accelerator when no HPU, XPU or CUDA device is present. Before, the file left DEVICE as "cuda" for other accelerators, so instantiate_device_type_tests created no tests for them.
…tantiation
Label every test class in test_functional_api.py with hw_classification.
The GENERIC classes keep their form. The ACCELERATOR classes now get the
device from instantiate_device_type_tests:
- TestTraceableCollectives drops @parametrize("device", devices) and
instantiate_parametrized_tests, takes the injected device, and is
instantiated for CPU and every accelerator.
- The three DistributedTestBase classes drop only_for=DEVICE and use
except_for=("cpu",), which keeps their current coverage.
- with_comms takes the device from the injected argument, and chooses the
device-count check from the default backend of that device.
Remove the DEVICE, devices and BACKEND globals and the TEST_CUDA, TEST_HPU
and TEST_XPU imports. Remove the file from the test linter allowlist.
Part of pytorch#185590.
Remove the class-level @skipIfHpu decorators and the hard-coded
@requires_accelerator_dist_backend(["nccl", "xccl"]) lines:
instantiate_device_type_tests already skips devices that are unavailable, and
the hard-coded backend list stopped other accelerators from running the test.
Use skipIf from common_device_type with device_type=("cuda", "xpu") for the
triton-dependent inductor tests, since other accelerators run inductor without
triton. Use self.backend(device) in with_comms instead of calling
get_default_backend_for_device directly.
Test Plan:
```
lintrunner test/distributed/test_functional_api.py
=> ok No lint issues.
```
Not re-run on an accelerator after this change: the trn2 host used for the
earlier runs is unavailable. with_comms is only applied to DistributedTestBase
subclasses (checked statically), which define backend().
This change was authored with an AI assistant (Claude Code) and reviewed by
the author.
57d2687 to
0a24c06
Compare
|
|
mergedog handoffNote This PR is being shepherded by mergedog, an automation tool run by a maintainer. It watches CI and may push fixes; the PR author does not need to act unless a maintainer asks. All CI is green (or skipped). Ready for human review and Current PR head: No LLM was invoked during this run. |
|
@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 |
Part of #185590.
This PR first added a four-line device fallback. After the review, it now changes
test/distributed/test_functional_api.pyto follow the rules intest/REVIEW.md(#202723). It overlaps with #192958, which has had no update since 2026-09-14.What changes
hw_classification.GENERIC:TestExpand,TestPgTag,TestPublicAPI,TestGradCollectives,TestMetaCollectives,TestMakeFx,TestAllGatherViewOptimization,TestFunctionalAutograd. Their form does not change.ACCELERATOR:TestTraceableCollectives,TestCollectivesWithDistributedBackend,TestDistributedBackendCollectivesWithWorldSize4,TestFunctionalAutogradWithDistributedBackend.TestTraceableCollectivesno longer uses@parametrize("device", devices)andinstantiate_parametrized_tests. Each test takes thedevicethatinstantiate_device_type_testsinjects, and the class is instantiated for CPU and each accelerator. The class keeps itsMultiThreadedTestCasebase. The seven copies of the device guard are now one method,_setup_device.DistributedTestBaseclasses no longer useonly_for=DEVICE. They useexcept_for=("cpu",), so the set of devices does not change.with_commstakes the device from the injected argument. It uses the device type, becauseDeviceMeshdoes not accept an index such as"cuda:0". It checks the device count when the default backend of that device is not gloo. Before, it checked the globalBACKEND.DEVICE,devicesandBACKENDglobals, the NOTE comment and theTEST_CUDA,TEST_HPUandTEST_XPUimports are removed.test/distributed/test_functional_api.pyis removed fromtools/linter/adapters/test_linter_allowlist.json.Testing
I ran the same change on a copy of the
release/pt_214test tree of a downstream fork. I linted thismainbranch only. I did not run this branch.On an out-of-tree accelerator (Trainium trn2, torch-neuronx, 8 workers) the file gives 23 passed, 4 skipped and 7 failed. The 7 failures are the
TestTraceableCollectivestests for that device. They fail withInconsistent device IDs found in kernel inputs: expected device 0 but found device 1. That class runs each rank as a thread of one process, so it needs one process to use several devices. That backend supports one device for each process. This is a limit of that backend and not of the tests. I handle it in the downstream test registry. The accelerator-only classes pass on that backend.I did not run CUDA, XPU or HPU.
Notes
instantiate_device_type_testsnames the variants. For exampletest_broadcast_device_cudabecomestest_broadcast_cuda.An AI assistant (Claude Code) helped to write this change and this text. I reviewed both.