Skip to content

[Testcase Refactoring] Add hw_classification in test_functional_api - #192958

Closed
songzibo wants to merge 2 commits into
pytorch:mainfrom
songzibo:test-functional-api-hw-classification
Closed

songzibo wants to merge 2 commits into
pytorch:mainfrom
songzibo:test-functional-api-hw-classification

Conversation

@songzibo

@songzibo songzibo commented Aug 11, 2026 •

Copy link
Copy Markdown
Contributor

Summary

test/distributed/test_functional_api.py contains 11 test classes, 7 of which were unclassified. This PR labels all 11 classes with hw_classification and refactors the module-level DEVICE/devices/BACKEND globals to be device-agnostic (addressing review feedback).

Changes

1. hw_classification labels

  • GENERIC (7): TestExpand, TestPgTag, TestMetaCollectives, TestGradCollectives, TestMakeFx, TestAllGatherViewOptimization, TestFunctionalAutograd. These use fake process groups, meta device, or MultiThreadedTestCase with no real accelerator dependency.
  • ACCELERATOR (4): TestTraceableCollectives (uses @parametrize("device", devices) with accelerator device), TestCollectivesWithDistributedBackend, TestDistributedBackendCollectivesWithWorldSize4, TestFunctionalAutogradWithDistributedBackend (use instantiate_device_type_tests with a device parameter).

2. Device-agnostic globals (addressing review)

Replaced the module-level DEVICE/devices/BACKEND if-elif chains with dynamic detection:

  • DEVICE/devices: Replaced if TEST_HPU/TEST_XPU/TEST_CUDA chain with torch.accelerator.is_available() + torch.accelerator.current_accelerator().type, so out-of-tree backends registered under privateuse1 (e.g. NPU) are automatically included.
  • BACKEND: Replaced hardcoded NCCL/HCCL/XCCL chain with c10d.get_default_backend_for_device(DEVICE).
  • with_comms: Updated device-count guard from BACKEND == NCCL or BACKEND == XCCL to BACKEND != GLOO.
  • Removed stale TEST_CUDA/TEST_HPU/TEST_XPU imports.

The only_for=DEVICE whitelist on the ACCELERATOR instantiate_device_type_tests calls is retained. DistributedTestBase.backend() only knows cuda/hpu/xpu and falls back to gloo for out-of-tree devices. Removing only_for would generate device variants that DistributedTestBase cannot initialize correctly.

No method bodies or decorators are changed. No hccl/ascend/npu references introduced.

Motivation

PR #190991 established the convention that every TestCase subclass must carry a hw_classification attribute. The 7 unclassified classes in this file were invisible under hardware-classification filtering.

The DEVICE/devices/BACKEND if-elif chains only covered cuda/hpu/xpu and could not generalize to out-of-tree device types. Dynamic detection via torch.accelerator and get_default_backend_for_device makes the tests automatically include any registered accelerator (e.g. NPU via privateuse1), consistent with the device-agnostic direction.

Dependencies

None.

Test Plan

  • python -m py_compile test/distributed/test_functional_api.py passes.
  • lintrunner test/distributed/test_functional_api.py passes (No lint issues).
  • Test count unchanged: 34 def test_* methods.
  • CPU mode: GENERIC 7 passed; TestTraceableCollectives (ACCELERATOR) 7 passed.
  • NPU mode: GENERIC 7 passed; TestTraceableCollectives (ACCELERATOR) 14 passed (7 CPU + 7 NPU variants — devices now includes "npu" via dynamic detection).
  • No hccl/ascend/npu/privateuse1 references (verified: 0 matches).

cc @fffrog @wjlFlyer @pjyFight @FuDdd @lyriexs666 @jishuangfeng @JamesD18 @JiasenTian

@pytorch-bot

pytorch-bot Bot commented Aug 11, 2026

Copy link
Copy Markdown

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/192958

Note: Links to docs will display an error until the docs builds have been completed.

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@pytorch-bot pytorch-bot Bot added the topic: not user facing topic category label Aug 11, 2026
@songzibo
songzibo force-pushed the test-functional-api-hw-classification branch from c775024 to 00c8a5f Compare August 11, 2026 09:54
@songzibo songzibo changed the title [Testcase Refactoring] Add hw_classification and drop only_for in test_functional_api [Testcase Refactoring] Add hw_classification in test_functional_api Aug 11, 2026
@jbschlosser jbschlosser added the triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module label Aug 11, 2026
@songzibo

Copy link
Copy Markdown
Contributor Author

@pytorchbot label "module: tests/distributed"

@pytorch-bot pytorch-bot Bot added the module: tests/distributed This label covers all test cases within the Distributed sheet. label Aug 17, 2026
@fffrog fffrog moved this to Backlog in PyTorch Test Refactoring Aug 17, 2026
Comment thread test/distributed/test_functional_api.py Outdated

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

devices only contains cpu, hpu, xpu and cuda. The default value of DEVICE is cuda. So we can't generalize it to other device type. It's necessary to be refactored.

Comment thread test/distributed/test_functional_api.py Outdated
@instantiate_parametrized_tests
@skipIfHpu
class TestTraceableCollectives(MultiThreadedTestCase):
hw_classification = HardwareClassification.GENERIC

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Accelerator is used in this test class. I think GENERIC is not appropriate.

Comment thread test/distributed/test_functional_api.py Outdated

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BACKEND is related to cuda, hpu, xpu. It can't be generalized to other device. I think it should be refactored to be device-agnostic.

@songzibo
songzibo force-pushed the test-functional-api-hw-classification branch from 00c8a5f to 1e58c38 Compare August 19, 2026 08:29
@songzibo

Copy link
Copy Markdown
Contributor Author

Thanks for the review @wjlFlyer! All three points addressed in the latest push (1e58c38):

  1. devices/DEVICE chain (line 74): Replaced the if TEST_HPU/TEST_XPU/TEST_CUDA chain with dynamic detection via torch.accelerator.is_available() + torch.accelerator.current_accelerator().type. Out-of-tree backends registered under privateuse1 (e.g. NPU) are now automatically included in devices. Verified on NPU: TestTraceableCollectives now runs 14 tests (7 CPU + 7 NPU variants) instead of 7.

  2. TestTraceableCollectives classification (line 275): Changed from GENERIC to ACCELERATOR. This class uses @parametrize("device", devices) with accelerator devices and checks torch.accelerator.device_count() — it is cross-device, not CPU-only.

  3. BACKEND chain (line 537): Replaced the hardcoded NCCL/HCCL/XCCL chain with c10d.get_default_backend_for_device(DEVICE). Updated with_comms guard from BACKEND == NCCL or BACKEND == XCCL to BACKEND != GLOO.

Stale TEST_CUDA/TEST_HPU/TEST_XPU imports removed. The only_for=DEVICE on the 3 instantiate_device_type_tests calls is retained because DistributedTestBase.backend() only knows cuda/hpu/xpu — removing it would generate device variants the base class cannot initialize.

@@ -267,6 +249,8 @@ def test_find_root_pg(self):
@instantiate_parametrized_tests
@skipIfHpu
class TestTraceableCollectives(MultiThreadedTestCase):
hw_classification = HardwareClassification.ACCELERATOR

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Current testclass tagged ACCELERATOR are not instantiated by instantiate_device_type_tests.

kerer-ai pushed a commit to kerer-ai/pytorch-nv that referenced this pull request Aug 26, 2026
kerer-ai pushed a commit to kerer-ai/pytorch-nv that referenced this pull request Aug 26, 2026
@songzibo
songzibo force-pushed the test-functional-api-hw-classification branch from 1e58c38 to 807c094 Compare August 29, 2026 12:14
Label all 11 test classes with hw_classification, refactor module-level
DEVICE/devices/BACKEND to be device-agnostic, and instantiate
TestTraceableCollectives with instantiate_device_type_tests.

Classes labeled:
- GENERIC (7): TestExpand, TestPgTag, TestMetaCollectives, TestGradCollectives,
  TestMakeFx, TestAllGatherViewOptimization, TestFunctionalAutograd.
- ACCELERATOR (4): TestTraceableCollectives, TestCollectivesWithDistributedBackend,
  TestDistributedBackendCollectivesWithWorldSize4,
  TestFunctionalAutogradWithDistributedBackend.

Refactored to be device-agnostic:
- Replaced DEVICE/devices if-elif chain (TEST_HPU/TEST_XPU/TEST_CUDA) with
  dynamic detection via torch.accelerator.
- Replaced hardcoded BACKEND chain (NCCL/HCCL/XCCL) with
  c10d.get_default_backend_for_device(DEVICE).
- Removed stale TEST_CUDA/TEST_HPU/TEST_XPU imports.

TestTraceableCollectives now uses instantiate_device_type_tests (addressing
review feedback). Replaced @instantiate_parametrized_tests +
@parametrize("device", devices) with instantiate_device_type_tests.
Removed stale parametrize/instantiate_parametrized_tests imports.

Test Plan:
- python -m py_compile test/distributed/test_functional_api.py passes.
- lintrunner test/distributed/test_functional_api.py passes (No lint issues).
- Test count unchanged: 34 def test_* methods.
@songzibo

Copy link
Copy Markdown
Contributor Author

Thanks @wjlFlyer! TestTraceableCollectives is now instantiated with instantiate_device_type_tests in the latest push (807c094):

  • Replaced @instantiate_parametrized_tests + @parametrize("device", devices) with instantiate_device_type_tests(TestTraceableCollectives, globals(), only_for=DEVICE, allow_xpu=True).
  • All 7 test methods keep their device parameter (already had it via @parametrize).
  • Removed stale parametrize and instantiate_parametrized_tests imports.

Verified: py_compile PASS, lintrunner PASS, 34 test methods unchanged.

@songzibo

Copy link
Copy Markdown
Contributor Author

Hi @wjlFlyer, all 4 review comments have been addressed in the latest push (807c094). Could you please re-review when you have a chance? Thanks!

Comment thread test/distributed/test_functional_api.py Outdated
allow_xpu=True,
)
instantiate_device_type_tests(
TestTraceableCollectives, globals(), only_for=DEVICE, allow_xpu=True

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If test class is tagged accelerator, we could only use excep_for, shouldn't use only_for.

…test_functional_api

ACCELERATOR-classified tests must run across every accelerator, so instantiate them with except_for="cpu" instead of only_for=DEVICE. Applies to all four instantiate_device_type_tests calls in the file.
pytorchmergebot pushed a commit that referenced this pull request Nov 13, 2026
…tantiation (#202776)

Part of #185590.

This PR first added a four-line device fallback. After the review, it now changes `test/distributed/test_functional_api.py` to follow the rules in `test/REVIEW.md` (#202723). It overlaps with #192958, which has had no update since 2026-09-14.

## What changes

- Every test class has one `hw_classification`.
  - `GENERIC`: `TestExpand`, `TestPgTag`, `TestPublicAPI`, `TestGradCollectives`, `TestMetaCollectives`, `TestMakeFx`, `TestAllGatherViewOptimization`, `TestFunctionalAutograd`. Their form does not change.
  - `ACCELERATOR`: `TestTraceableCollectives`, `TestCollectivesWithDistributedBackend`, `TestDistributedBackendCollectivesWithWorldSize4`, `TestFunctionalAutogradWithDistributedBackend`.
- `TestTraceableCollectives` no longer uses `@parametrize("device", devices)` and `instantiate_parametrized_tests`. Each test takes the `device` that `instantiate_device_type_tests` injects, and the class is instantiated for CPU and each accelerator. The class keeps its `MultiThreadedTestCase` base. The seven copies of the device guard are now one method, `_setup_device`.
- The three `DistributedTestBase` classes no longer use `only_for=DEVICE`. They use `except_for=("cpu",)`, so the set of devices does not change.
- `with_comms` takes the device from the injected argument. It uses the device type, because `DeviceMesh` does 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 global `BACKEND`.
- The `DEVICE`, `devices` and `BACKEND` globals, the NOTE comment and the `TEST_CUDA`, `TEST_HPU` and `TEST_XPU` imports are removed.
- `test/distributed/test_functional_api.py` is removed from `tools/linter/adapters/test_linter_allowlist.json`.

## Testing

I ran the same change on a copy of the `release/pt_214` test tree of a downstream fork. I linted this `main` branch only. I did not run this branch.

```
# Lint on the changed file (Python 3.14): no messages
python tools/linter/adapters/test_linter.py test/distributed/test_functional_api.py
=> rc 0
# The old file gives 6 hw_classification errors with the same command.

# CPU variants
PYTORCH_TESTING_DEVICE_ONLY_FOR=cpu python test/distributed/test_functional_api.py
=> 24 passed, 1 skipped (TestTraceableCollectivesCPU: 7 passed)
```

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 `TestTraceableCollectives` tests for that device. They fail with `Inconsistent 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

- The test names change, because `instantiate_device_type_tests` names the variants. For example `test_broadcast_device_cuda` becomes `test_broadcast_cuda`.
- The bot reports "missing actionable issue". I have no maintainer who agreed to this change. @fffrog, please tell me which issue to link.

An AI assistant (Claude Code) helped to write this change and this text. I reviewed both.

Pull Request resolved: #202776
Approved by: https://github.com/fffrog, https://github.com/albanD
@github-actions

Copy link
Copy Markdown
Contributor

Looks like this PR hasn't been updated in a while so we're going to go ahead and mark this as Stale.
Feel free to remove the Stale label if you feel this was a mistake.
If you are unable to remove the Stale label please contact a maintainer in order to do so.
If you want the bot to never mark this PR stale again, add the no-stale label.
Stale pull requests will automatically be closed after 30 days of inactivity.

@github-actions github-actions Bot added the Stale label Nov 13, 2026
@songzibo songzibo closed this Nov 18, 2026
@github-project-automation github-project-automation Bot moved this from Backlog to Done in PyTorch Test Refactoring Nov 18, 2026
@songzibo

Copy link
Copy Markdown
Contributor Author

Thanks for the review and feedback. The changes in this PR have been superseded by the subsequent refactoring in #202776, which covers the hw_classification updates and device-type test instantiation in test_functional_api.py.

To avoid overlapping changes and duplicate maintenance, I'll close this PR in favor of #202776.

Thanks again for your time and review!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

module: tests/distributed This label covers all test cases within the Distributed sheet. open source Stale topic: not user facing topic category triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants