Skip to content

[Testcase Refactoring] Add hw_classification and split device-dependent test in test_multi_threaded_pg - #191905

Closed
twthighd wants to merge 2 commits into
pytorch:mainfrom
twthighd:test/decouple-test_multi_threaded_pg
Closed

twthighd wants to merge 2 commits into
pytorch:mainfrom
twthighd:test/decouple-test_multi_threaded_pg

Conversation

@twthighd

@twthighd twthighd commented Aug 3, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Split the device-dependent tests out of the mixed TestCollectivesWithBaseClass into a dedicated ACCELERATOR class instantiated via instantiate_device_type_tests, and label the CPU-only collective classes GENERIC, so the threaded-collective tests are correctly classified under --hw-classification filtering and run on-device via an injected device parameter instead of a module-level device string.

Changes

  • test/distributed/test_multi_threaded_pg.py:
    • Labeled TestCollectivesWithWrapper and TestCollectivesWithBaseClass with hw_classification = HardwareClassification.GENERIC (device-agnostic threaded-collective logic on CPU tensors).
    • Moved test_bwd_sees_fwd_pg (requires an accelerator; asserts that autograd backward runs in the same thread as forward and can still see the process group on a device tensor) out of TestCollectivesWithBaseClass into a new parallel TestCollectivesWithBaseClassDevice class labeled hw_classification = HardwareClassification.ACCELERATOR.
    • During the rebase onto main, test_collectives_issued_from_side_stream (added upstream in Fix missing stream synchronization in the threaded process group #195281, reading the module-level device string this PR deletes) moved into the ACCELERATOR class as well, with the same device parameter and helpers derived from the variant's self.device_type.
    • Extracted a shared _TestCollectivesBase(MultiThreadedTestCase) fixture base holding world_size / setUp / tearDown (the TORCH_DIST_INIT_BARRIER env setup plus _spawn_threads); both TestCollectivesWithBaseClass and TestCollectivesWithBaseClassDevice inherit it. The base has no test_* methods, so unittest does not collect it and --hw-classification filtering does not scan it; no hw_classification label is needed on it.
    • Both ACCELERATOR methods take a device parameter, and TestCollectivesWithBaseClassDevice is instantiated via instantiate_device_type_tests(..., allow_xpu=True). The device is injected per-variant rather than read from a module-level device string.
    • Removed the module-level device_type = acc.type if (acc := torch.accelerator.current_accelerator()) else "cpu" string and the pre-existing @skip_if_lt_x_gpu(1) decorator on test_bwd_sees_fwd_pg (plus its skip_if_lt_x_gpu import). Both tests carry the variant-aware @onlyAccelerator gate instead: the CPU variant is generated but skips cleanly on every host (the guard reads the running variant's device_type), while accelerator variants run on-device with the injected device. On a CPU-only host the two tests are now collected and report explicit skips instead of the class being invisible. Same gating shape as the landed [Testcase Refactoring] Label test_fsdp_freezing_weights ACCELERATOR and switch to device-type instantiation #192712.
    • Rank threads are bound to the injected device: MultiThreadedTestCase runs each test in freshly spawned threads whose thread-local current device starts at the default index, while the injected device string is resolved in the main thread at class-creation time. Both tests now call torch.accelerator.set_device_index(device) up front, test_collectives_issued_from_side_stream passes the injected device to its tensors and to Stream()/current_stream() explicitly, and the type-only helpers (get_cycles_per_ms, device_sleep) keep self.device_type — their sleep kernel now lands on the bound device.

Motivation

TestCollectivesWithBaseClass was a mixed class: CPU threaded-collective methods plus accelerator-dependent methods (test_bwd_sees_fwd_pg, and later test_collectives_issued_from_side_stream). Under --hw-classification filtering such a mixed class cannot be classified unambiguously, and the accelerator method resolved its device from a module-level string (device_type = acc.type if ...) rather than the per-variant device parameter injected by instantiate_device_type_tests — so non-default device variants would use the wrong device. Splitting the class by hardware dependency and instantiating the ACCELERATOR class with an injected device parameter makes the classification unambiguous and aligns with the TEST_LINTER direction (PR #190173): instantiated, device param on every test_*, no only_for.

The gating resolves the review divergence concern without hiding the CPU variant from collection: @onlyAccelerator reads the running variant's device_type (unlike @skip_if_lt_x_gpu(1), which checked only global accelerator availability and would let a CPU variant run with device="cpu" on accelerator hosts), so the CPU variant is collected and skips everywhere while accelerator variants keep the original on-device execution.

Test Plan

Verified on an 8-device accelerator backend with the threaded reference PG:

python test/distributed/test_multi_threaded_pg.py
# Ran 27 tests — 24 ok, 2 skipped, 1 error. The two skips are the CPU-variant
# methods reporting "onlyAccelerator: doesn't run on CPU or meta devices"
# after the threaded fixture initializes; test_bwd_sees_fwd_pg passes
# on-device on the accelerator variant. The one error is
# test_collectives_issued_from_side_stream on the accelerator variant: the
# backend's _sleep entry point exists but the Sleep operator is not
# installed in its operator package (error 161001), so that test's execution
# stays with CUDA/XPU CI. After syncing the verify host's older pure-Python
# test-infra file (its snapshot predates #196786), the two TestThreadLocalWorld
# tests pass.

python test/distributed/test_multi_threaded_pg.py --hw-classification GENERIC
# OK — total=27, passed=23, unclassified=0, mismatched=4

python test/distributed/test_multi_threaded_pg.py --hw-classification ACCELERATOR
# total=27, passed=4, unclassified=0, mismatched=23 (errors=1, skipped=2:
# the environmental Sleep error and the two CPU-variant skips)

Non-default device index: with the primary device set to a non-default index before collection, the injected device carries that index, and each of the four rank threads' current device moves from the default card to the injected index (threads start on the default card; the binding lands them on the variant's device) while test_bwd_sees_fwd_pg passes on the accelerator variant. lintrunner reports no lint issues.

Notes

@pytorch-bot

pytorch-bot Bot commented Aug 3, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

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

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

✅ No Failures

As of commit 2514a6d with merge base 628d01c (image):
💚 Looks good so far! There are no failures yet. 💚

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 3, 2026
@twthighd twthighd changed the title [Testcase Refactoring] Add hw_classification and split device-depende… [Testcase Refactoring] Add hw_classification and split device-dependent test in test_multi_threaded_pg Aug 3, 2026
@twthighd

twthighd commented Aug 3, 2026

Copy link
Copy Markdown
Contributor Author

@twthighd

twthighd commented Aug 3, 2026

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 3, 2026
@soulitzer
soulitzer requested review from albanD and fffrog August 3, 2026 15:07
@soulitzer soulitzer added the triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module label Aug 3, 2026
@twthighd
twthighd force-pushed the test/decouple-test_multi_threaded_pg branch from 893af2f to 508e2c8 Compare August 4, 2026 03:01
@jishuangfeng

Copy link
Copy Markdown

The class split, GENERIC/ACCELERATOR labeling, and instantiate_device_type_tests parameterization all look correct. The @skip_if_lt_x_gpu(1) retention on test_bwd_sees_fwd_pg is appropriate given that #187650 makes that decorator hardware-agnostic, and the dependency is clearly noted in the PR description.
LGTM.

@jishuangfeng jishuangfeng left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

LGTM

fffrog
fffrog previously requested changes Aug 17, 2026

@fffrog fffrog left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

SGTM, thank you for contributing this one.



instantiate_device_type_tests(
TestCollectivesWithBaseClassDevice, globals(), except_for=("cpu",), allow_xpu=True

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Because the device_type could be CPU, right?

@twthighd twthighd Aug 19, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yes, exactly. With except_for removed, the CPU variant is generated and device is injected as "cpu" for it. Both tests are gated with the variant-aware @onlyAccelerator (it reads the running variant's device_type and skips cpu/meta), so the CPU variant skips cleanly on every host while accelerator variants run on-device; the availability-style @skip_if_lt_x_gpu(1) is dropped. Same gating shape as the landed #192712. (Edited: an earlier revision kept @skip_if_lt_x_gpu(1); the variant-aware guard supersedes it.)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Why are we generating a CPU test variant with HardwareClassification.ACCELERATOR set ?

@twthighd twthighd Oct 22, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good question — hw_classification does not participate in variant generation at all — get_device_type_test_bases() in common_device_type.py puts CPUTestBase unconditionally at the head of the base set, so an ACCELERATOR class without except_for=("cpu",) always gets a CPU variant.

The final form now adopts the #192712 shape here as well: no except_for, plus the variant-aware @onlyAccelerator on both tests, which skips the CPU variant on every host (it reads the running variant's device_type, unlike availability guards such as @skip_if_lt_x_gpu that @jialinw77 flagged upthread). The CPU variant therefore stays in collection and reports a visible skip on CPU-only machines without the GPU-server device="cpu" execution. (Edited: the earlier revision kept except_for=("cpu",), which made the class invisible on CPU-only collection.)

@fffrog fffrog moved this from Backlog to Ready for Review(Initial) in PyTorch Test Refactoring Aug 17, 2026
@twthighd
twthighd force-pushed the test/decouple-test_multi_threaded_pg branch from ce686da to 2de7874 Compare August 19, 2026 01:58
@twthighd
twthighd requested a review from fffrog August 19, 2026 02:44
kerer-ai pushed a commit to kerer-ai/pytorch-nv that referenced this pull request Aug 20, 2026
@@ -335,11 +349,14 @@ def backward(ctx, grad_output):
return grad_output * result

x = torch.tensor(
[dist.get_rank()], dtype=torch.float, device=device_type, requires_grad=True
[dist.get_rank()], dtype=torch.float, device=device, requires_grad=True

@jialinw77 jialinw77 Aug 20, 2026 •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

There is a quesiton. If you remove except_for=("cpu",) in instantiate_device_type_tests method, class TestCollectivesWithBaseClassDeviceCPU will be instantiated.

Will TestCollectivesWithBaseClassDeviceCPU be executed on gpu server? If so, the parameter device would be cpu. But for original logic, if this method is executed on gpu server, the device_type would be gpu because it is generated by device_type = acc.type if (acc := torch.accelerator.current_accelerator()) else "cpu".

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks for catching this — it's a valid concern. With except_for removed, the CPU variant runs on a GPU host with device="cpu" (since @skip_if_lt_x_gpu(1) only checks accelerator availability, not the current variant), diverging from the original single-GPU run.

Fix: restore except_for=("cpu",) (no CPU variant generated) and drop the now-redundant @skip_if_lt_x_gpu(1) (pure-CPU skip is now handled at instantiation).

This keeps original behavior + decoupling for other accelerators.

@twthighd
twthighd force-pushed the test/decouple-test_multi_threaded_pg branch 2 times, most recently from f9c0583 to d7d15b4 Compare August 21, 2026 08:04

@KarhouTam KarhouTam left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks.
cc @fffrog

twthighd added a commit to twthighd/pytorch that referenced this pull request Oct 31, 2026
…wlist

Summary:
- This PR migrates test/distributed/test_fake_pg.py to the TEST_LINTER's
  requirements (hw_classification on every test class plus the per-classification
  structure rules), and the file currently sits in test_linter_allowlist.json.
  Drop its one-line entry in the same PR: the allowlist is a regenerated snapshot
  of non-compliant files, and a migration landing without the deletion leaves a
  silently-stale exemption (canonical: pytorch#191905 needed the follow-up pytorch#202866).

This lets the linter enforce its requirements on future changes to this file
as part of the same change, instead of a separate cleanup PR later.
twthighd added a commit to twthighd/pytorch that referenced this pull request Oct 31, 2026
… TEST_LINTER allowlist

Summary:
- This PR migrates test/distributed/test_functional_differentials.py to the TEST_LINTER's
  requirements (hw_classification on every test class plus the per-classification
  structure rules), and the file currently sits in test_linter_allowlist.json.
  Drop its one-line entry in the same PR: the allowlist is a regenerated snapshot
  of non-compliant files, and a migration landing without the deletion leaves a
  silently-stale exemption (canonical: pytorch#191905 needed the follow-up pytorch#202866).

This lets the linter enforce its requirements on future changes to this file
as part of the same change, instead of a separate cleanup PR later.
twthighd added a commit to twthighd/pytorch that referenced this pull request Oct 31, 2026
…EST_LINTER allowlist

Summary:
- This PR migrates test/distributed/test_c10d_functional_native.py to the TEST_LINTER's
  requirements (hw_classification on every test class plus the per-classification
  structure rules), and the file currently sits in test_linter_allowlist.json.
  Drop its one-line entry in the same PR: the allowlist is a regenerated snapshot
  of non-compliant files, and a migration landing without the deletion leaves a
  silently-stale exemption (canonical: pytorch#191905 needed the follow-up pytorch#202866).

This lets the linter enforce its requirements on future changes to this file
as part of the same change, instead of a separate cleanup PR later.
twthighd added a commit to twthighd/pytorch that referenced this pull request Oct 31, 2026
…_LINTER allowlist

Summary:
- This PR migrates test/distributed/fsdp/test_fsdp_clip_grad_norm.py to the TEST_LINTER's
  requirements (hw_classification on every test class plus the per-classification
  structure rules), and the file currently sits in test_linter_allowlist.json.
  Drop its one-line entry in the same PR: the allowlist is a regenerated snapshot
  of non-compliant files, and a migration landing without the deletion leaves a
  silently-stale exemption (canonical: pytorch#191905 needed the follow-up pytorch#202866).

This lets the linter enforce its requirements on future changes to this file
as part of the same change, instead of a separate cleanup PR later.
twthighd added a commit to twthighd/pytorch that referenced this pull request Oct 31, 2026
…TEST_LINTER allowlist

Summary:
- This PR migrates test/distributed/test_c10d_object_collectives.py to the TEST_LINTER's
  requirements (hw_classification on every test class plus the per-classification
  structure rules), and the file currently sits in test_linter_allowlist.json.
  Drop its one-line entry in the same PR: the allowlist is a regenerated snapshot
  of non-compliant files, and a migration landing without the deletion leaves a
  silently-stale exemption (canonical: pytorch#191905 needed the follow-up pytorch#202866).

This lets the linter enforce its requirements on future changes to this file
as part of the same change, instead of a separate cleanup PR later.
twthighd added a commit to twthighd/pytorch that referenced this pull request Oct 31, 2026
… allowlist

Summary:
- This PR migrates test/distributed/test_c10d_spawn_gloo.py to the TEST_LINTER's
  requirements (hw_classification on every test class plus the per-classification
  structure rules), and the file currently sits in test_linter_allowlist.json.
  Drop its one-line entry in the same PR: the allowlist is a regenerated snapshot
  of non-compliant files, and a migration landing without the deletion leaves a
  silently-stale exemption (canonical: pytorch#191905 needed the follow-up pytorch#202866).

This lets the linter enforce its requirements on future changes to this file
as part of the same change, instead of a separate cleanup PR later.
twthighd added a commit to twthighd/pytorch that referenced this pull request Oct 31, 2026
…EST_LINTER allowlist

Summary:
- This PR migrates test/distributed/fsdp/test_fsdp_backward_prefetch.py to the TEST_LINTER's
  requirements (hw_classification on every test class plus the per-classification
  structure rules), and the file currently sits in test_linter_allowlist.json.
  Drop its one-line entry in the same PR: the allowlist is a regenerated snapshot
  of non-compliant files, and a migration landing without the deletion leaves a
  silently-stale exemption (canonical: pytorch#191905 needed the follow-up pytorch#202866).

This lets the linter enforce its requirements on future changes to this file
as part of the same change, instead of a separate cleanup PR later.
twthighd added a commit to twthighd/pytorch that referenced this pull request Nov 4, 2026
…devices and label ACCELERATOR classes

Summary:
- Add `hw_classification = HardwareClassification.ACCELERATOR` to both
  `TestFSDPCheckpoint` and `TestFSDPCheckpointSubmodule`, and import
  `HardwareClassification`.
- Switch `TestFSDPCheckpoint` from `@instantiate_parametrized_tests` to
  `instantiate_device_type_tests(..., allow_xpu=True)`, and use the same
  form for `TestFSDPCheckpointSubmodule` (its hard-coded device list was
  already removed in the preceding commit).
- Gate all three test methods with the variant-aware `@onlyAccelerator`
  instead of excluding the CPU variant: the CPU variant is now collected and
  skips cleanly on every host (the guard reads the running variant's
  `device_type`, so CPU-only machines report explicit skips instead of the
  classes being invisible), while accelerator variants run unchanged. Same
  gating form as pytorch#192712 (landed) and pytorch#191905.
- Migrate the test methods off the module-level `device_type` string: every
  method takes the injected `device` parameter, and the bodies create
  tensors, move modules, and pass the FSDP `device_id` with the per-variant
  type-only `self.device_type`; `SequentialModule.__init__` receives the
  device. Delete the module-level `device_type = torch.device(get_devtype())`
  and the `get_devtype` import.
- Replace 4 `TestFSDPCheckpoint.SequentialModule(...)` references in class-1
  test bodies with `self.SequentialModule(...)`. `instantiate_device_type_tests`
  removes the original class name from scope (replaces it with per-device
  variants like `TestFSDPCheckpointPRIVATEUSE1`), so a test body referencing the
  original class name raises `NameError` at runtime. The variants inherit from
  the original class, so `self.SequentialModule` resolves the nested class
  correctly. This was latent until the c10d backend-type lookup for PrivateUse1
  was fixed (pytorch#187650 / out-of-tree accelerator issue pytorch#3947), because workers
  previously exited at `dist.barrier(device_ids=)` before reaching this code.

This aligns both classes' mechanism with their ACCELERATOR classification and
removes the hard-coded device whitelist so any accelerator registered via
`torch.accelerator` is admitted. The device-parameter migration ensures each
per-device variant uses its own device rather than a module-level constant,
which is the correct pattern under `instantiate_device_type_tests`.

Rebase note: upstream has since moved both classes onto the
FSDPTestContinuous base; this is kept, and the type-only device idiom in
test bodies is normalized to self.device_type (equivalent to the previous
torch.device(device).type, and unambiguous under type-only injection).

Verified end to end on an 8-card accelerator backend (the verification host
runs an older torch snapshot; its common_distributed.py carries the
previously-applied verification patches - the skip_if_lt_x_gpu device-agnostic
rewrite and the device-backend registry resolution - plus the worker-side
method-level SkipTest pass-through that current main already has):

python test/distributed/fsdp/test_fsdp_checkpoint.py -v
-> 34 cases collected: 17 accelerator-variant cases pass across both
   classes, and 17 CPU-variant cases (the two generated CPU classes) are
   collected and skipped with the exact reason "onlyAccelerator: doesn't
   run on CPU or meta devices"; 0 errors.
twthighd added a commit to twthighd/pytorch that referenced this pull request Nov 4, 2026
…TER allowlist

Summary:
- This PR migrates test/distributed/fsdp/test_fsdp_checkpoint.py to the TEST_LINTER's
  requirements (hw_classification on every test class plus the per-classification
  structure rules), and the file currently sits in test_linter_allowlist.json.
  Drop its one-line entry in the same PR: the allowlist is a regenerated snapshot
  of non-compliant files, and a migration landing without the deletion leaves a
  silently-stale exemption (canonical: pytorch#191905 needed the follow-up pytorch#202866).

This lets the linter enforce its requirements on future changes to this file
as part of the same change, instead of a separate cleanup PR later.
pytorchmergebot pushed a commit that referenced this pull request Nov 4, 2026
…devices and label ACCELERATOR classes (#191419)

## Summary

This PR labels `TestFSDPCheckpoint` and `TestFSDPCheckpointSubmodule` as `HardwareClassification.ACCELERATOR`, switches both to `instantiate_device_type_tests(..., allow_xpu=True)` with the variant-aware `@onlyAccelerator` gate on every test method, and completes the device-parameter migration in the test bodies. Rebased onto upstream's migration of both classes onto the `FSDPTestContinuous` base (kept); type-only device access in test bodies uses `self.device_type` (canonical idiom, unambiguous under `DeviceTypeTestBase` injection).

## Changes

- **`test/distributed/fsdp/test_fsdp_checkpoint.py`**:
  - Added `hw_classification = HardwareClassification.ACCELERATOR` to both classes, plus the import. Both classes' `test_*` methods require an accelerator (`@skip_if_lt_x_gpu(2)`, `device` param) and exercise FSDP checkpoint behavior on the device-tensor path.
  - Switched `TestFSDPCheckpoint` from `@instantiate_parametrized_tests` to `instantiate_device_type_tests(..., allow_xpu=True)` (deleting the class decorator avoids the double-expansion trap); `TestFSDPCheckpointSubmodule`'s hard-coded `only_for=("cuda","hpu","xpu")` device list was removed in the preceding commit and it now uses the same form. ACCELERATOR classes must not use `only_for` (landed test linter #190173).
  - All three test methods carry the variant-aware `@onlyAccelerator` (stacked above `@skip_if_lt_x_gpu(2)`): the CPU variant is generated and collected everywhere but skips cleanly with the exact reason `onlyAccelerator: doesn't run on CPU or meta devices` (the guard reads the running variant's `device_type`, unlike availability-based guards), so CPU-only machines report explicit skips instead of the classes being invisible, while accelerator variants run unchanged. Same gating form as #192712 (landed) and #191905.
  - Migrated every test method to the injected `device` parameter: added `device` to both class-1 methods and to `SequentialModule.__init__`, deleted the module-level `device_type` constant and the `get_devtype` import.
  - Replaced 4 `TestFSDPCheckpoint.SequentialModule(...)` references with `self.SequentialModule(...)` — `instantiate_device_type_tests` removes the original class name from scope, so module-level references raise `NameError` at runtime (latent until #187650 fixed the backend-type lookup for PrivateUse1 / out-of-tree accelerator issue #3947, because workers previously exited at `dist.barrier(device_ids=)` first).
  - Device access in test bodies uses `self.device_type` (type-only) for FSDP `device_id` (`fsdp_kwargs`), `.to(...)`, `torch.randn(device=...)`, and `SequentialModule(device=...)`; `SequentialModule.__init__` receives the type-only device from its callers and uses it directly. Type-only `device_id` keeps `_get_device_from_device_id` resolving each rank to its own current device (no single-card degeneration).
- **`tools/linter/adapters/test_linter_allowlist.json`**: removed the single `test/distributed/fsdp/test_fsdp_checkpoint.py` entry — this PR migrates that file to the linter's requirements.

## Motivation

Both classes had no `hw_classification` (silently dropped under `--hw-classification ACCELERATOR` filtering) and used `only_for=("cuda","hpu","xpu")`, excluding PrivateUse1-based out-of-tree accelerators. The module-level `device_type` constant does not change across per-device variants, and an indexed `device` passed bare to FSDP `device_id` silently degenerates multi-rank tests to single-card.

## Test Plan

- FSDP infra routes to the real accelerator backend (not a gloo/CPU false pass): `python -c "from torch.testing._internal.common_fsdp import DEVICE_TYPE, DISTRIBUTED_BACKEND, DEVICE_COUNT; print(...)"` resolves to the verify backend's accelerator type, its native comm backend, and 8 on the verify host.
- End-to-end on an 8-card accelerator backend (the verify host runs an older torch snapshot; its `common_distributed.py` carries the previously documented verification patches plus the worker-side method-level SkipTest pass-through that current main already has):

```
python test/distributed/fsdp/test_fsdp_checkpoint.py -v
# 34 cases collected: 17 accelerator-variant cases pass across both classes;
# 17 CPU-variant cases (the two generated CPU classes) are collected and
# skipped with the exact onlyAccelerator reason; 0 errors.
```

- The earlier revision of this PR (CPU variant excluded at instantiation) verified as `Ran 17 tests, OK (0 errors)`; accelerator-variant behavior is unchanged between the two forms.
- `lintrunner` clean; landed test linter: 0 violations.
- With the entry removed, the TEST_LINTER full re-check passes for the migrated file: `python3 tools/linter/adapters/test_linter.py tools/linter/adapters/test_linter_allowlist.json` (the allowlist-as-argument form lintrunner triggers in CI) — rc=0, zero lint messages for `test/distributed/fsdp/test_fsdp_checkpoint.py`.

## Notes

- The method-level `@onlyAccelerator` form (rather than `except_for=("cpu",)`) keeps the CPU variant in collection so CPU-only hosts report explicit skips instead of the classes silently disappearing. This PR's base already translates a worker-side `unittest.SkipTest` into a skip result, and the trade-off — one extra class-level worker/PG lifecycle per generated CPU class on accelerator hosts, with no change in accelerator-variant execution — was accepted for collection visibility. Same form as #192712 (landed) and #191905.
- This is part of the test case refactoring initiative tracked in #185590.

Pull Request resolved: #191419
Approved by: https://github.com/jialinw77, https://github.com/can-gaa-hou, https://github.com/d4l3k
dominikjastrzebskix pushed a commit to dominikjastrzebskix/pytorch that referenced this pull request Nov 5, 2026
…nt test in test_multi_threaded_pg (pytorch#191905)

## Summary

Split the device-dependent tests out of the mixed TestCollectivesWithBaseClass into a dedicated ACCELERATOR class instantiated via instantiate_device_type_tests, and label the CPU-only collective classes GENERIC, so the threaded-collective tests are correctly classified under --hw-classification filtering and run on-device via an injected device parameter instead of a module-level device string.

## Changes

- **`test/distributed/test_multi_threaded_pg.py`**:
  - Labeled `TestCollectivesWithWrapper` and `TestCollectivesWithBaseClass` with `hw_classification = HardwareClassification.GENERIC` (device-agnostic threaded-collective logic on CPU tensors).
  - Moved `test_bwd_sees_fwd_pg` (requires an accelerator; asserts that autograd backward runs in the same thread as forward and can still see the process group on a device tensor) out of `TestCollectivesWithBaseClass` into a new parallel `TestCollectivesWithBaseClassDevice` class labeled `hw_classification = HardwareClassification.ACCELERATOR`.
  - During the rebase onto main, `test_collectives_issued_from_side_stream` (added upstream in pytorch#195281, reading the module-level device string this PR deletes) moved into the ACCELERATOR class as well, with the same `device` parameter and helpers derived from the variant's `self.device_type`.
  - Extracted a shared `_TestCollectivesBase(MultiThreadedTestCase)` fixture base holding `world_size` / `setUp` / `tearDown` (the `TORCH_DIST_INIT_BARRIER` env setup plus `_spawn_threads`); both `TestCollectivesWithBaseClass` and `TestCollectivesWithBaseClassDevice` inherit it. The base has no `test_*` methods, so unittest does not collect it and `--hw-classification` filtering does not scan it; no `hw_classification` label is needed on it.
  - Both ACCELERATOR methods take a `device` parameter, and `TestCollectivesWithBaseClassDevice` is instantiated via `instantiate_device_type_tests(..., allow_xpu=True)`. The `device` is injected per-variant rather than read from a module-level device string.
  - Removed the module-level `device_type = acc.type if (acc := torch.accelerator.current_accelerator()) else "cpu"` string and the pre-existing `@skip_if_lt_x_gpu(1)` decorator on `test_bwd_sees_fwd_pg` (plus its `skip_if_lt_x_gpu` import). Both tests carry the variant-aware `@onlyAccelerator` gate instead: the CPU variant is generated but skips cleanly on every host (the guard reads the running variant's `device_type`), while accelerator variants run on-device with the injected `device`. On a CPU-only host the two tests are now collected and report explicit skips instead of the class being invisible. Same gating shape as the landed pytorch#192712.
  - Rank threads are bound to the injected device: `MultiThreadedTestCase` runs each test in freshly spawned threads whose thread-local current device starts at the default index, while the injected device string is resolved in the main thread at class-creation time. Both tests now call `torch.accelerator.set_device_index(device)` up front, `test_collectives_issued_from_side_stream` passes the injected device to its tensors and to `Stream()`/`current_stream()` explicitly, and the type-only helpers (`get_cycles_per_ms`, `device_sleep`) keep `self.device_type` — their sleep kernel now lands on the bound device.

## Motivation

`TestCollectivesWithBaseClass` was a mixed class: CPU threaded-collective methods plus accelerator-dependent methods (`test_bwd_sees_fwd_pg`, and later `test_collectives_issued_from_side_stream`). Under `--hw-classification` filtering such a mixed class cannot be classified unambiguously, and the accelerator method resolved its device from a module-level string (`device_type = acc.type if ...`) rather than the per-variant `device` parameter injected by `instantiate_device_type_tests` — so non-default device variants would use the wrong device. Splitting the class by hardware dependency and instantiating the ACCELERATOR class with an injected `device` parameter makes the classification unambiguous and aligns with the TEST_LINTER direction (PR pytorch#190173): instantiated, `device` param on every `test_*`, no `only_for`.

The gating resolves the review divergence concern without hiding the CPU variant from collection: `@onlyAccelerator` reads the running variant's `device_type` (unlike `@skip_if_lt_x_gpu(1)`, which checked only global accelerator availability and would let a CPU variant run with `device="cpu"` on accelerator hosts), so the CPU variant is collected and skips everywhere while accelerator variants keep the original on-device execution.

## Test Plan

Verified on an 8-device accelerator backend with the threaded reference PG:

```bash
python test/distributed/test_multi_threaded_pg.py
# Ran 27 tests — 24 ok, 2 skipped, 1 error. The two skips are the CPU-variant
# methods reporting "onlyAccelerator: doesn't run on CPU or meta devices"
# after the threaded fixture initializes; test_bwd_sees_fwd_pg passes
# on-device on the accelerator variant. The one error is
# test_collectives_issued_from_side_stream on the accelerator variant: the
# backend's _sleep entry point exists but the Sleep operator is not
# installed in its operator package (error 161001), so that test's execution
# stays with CUDA/XPU CI. After syncing the verify host's older pure-Python
# test-infra file (its snapshot predates pytorch#196786), the two TestThreadLocalWorld
# tests pass.

python test/distributed/test_multi_threaded_pg.py --hw-classification GENERIC
# OK — total=27, passed=23, unclassified=0, mismatched=4

python test/distributed/test_multi_threaded_pg.py --hw-classification ACCELERATOR
# total=27, passed=4, unclassified=0, mismatched=23 (errors=1, skipped=2:
# the environmental Sleep error and the two CPU-variant skips)
```

Non-default device index: with the primary device set to a non-default index before collection, the injected device carries that index, and each of the four rank threads' current device moves from the default card to the injected index (threads start on the default card; the binding lands them on the variant's device) while test_bwd_sees_fwd_pg passes on the accelerator variant. `lintrunner` reports no lint issues.

## Notes

- This is part of the test case refactoring initiative tracked in pytorch#185590.

Pull Request resolved: pytorch#191905
Approved by: https://github.com/jishuangfeng, https://github.com/jialinw77, https://github.com/KarhouTam, https://github.com/can-gaa-hou, https://github.com/d4l3k
dominikjastrzebskix pushed a commit to dominikjastrzebskix/pytorch that referenced this pull request Nov 5, 2026
…devices and label ACCELERATOR classes (pytorch#191419)

## Summary

This PR labels `TestFSDPCheckpoint` and `TestFSDPCheckpointSubmodule` as `HardwareClassification.ACCELERATOR`, switches both to `instantiate_device_type_tests(..., allow_xpu=True)` with the variant-aware `@onlyAccelerator` gate on every test method, and completes the device-parameter migration in the test bodies. Rebased onto upstream's migration of both classes onto the `FSDPTestContinuous` base (kept); type-only device access in test bodies uses `self.device_type` (canonical idiom, unambiguous under `DeviceTypeTestBase` injection).

## Changes

- **`test/distributed/fsdp/test_fsdp_checkpoint.py`**:
  - Added `hw_classification = HardwareClassification.ACCELERATOR` to both classes, plus the import. Both classes' `test_*` methods require an accelerator (`@skip_if_lt_x_gpu(2)`, `device` param) and exercise FSDP checkpoint behavior on the device-tensor path.
  - Switched `TestFSDPCheckpoint` from `@instantiate_parametrized_tests` to `instantiate_device_type_tests(..., allow_xpu=True)` (deleting the class decorator avoids the double-expansion trap); `TestFSDPCheckpointSubmodule`'s hard-coded `only_for=("cuda","hpu","xpu")` device list was removed in the preceding commit and it now uses the same form. ACCELERATOR classes must not use `only_for` (landed test linter pytorch#190173).
  - All three test methods carry the variant-aware `@onlyAccelerator` (stacked above `@skip_if_lt_x_gpu(2)`): the CPU variant is generated and collected everywhere but skips cleanly with the exact reason `onlyAccelerator: doesn't run on CPU or meta devices` (the guard reads the running variant's `device_type`, unlike availability-based guards), so CPU-only machines report explicit skips instead of the classes being invisible, while accelerator variants run unchanged. Same gating form as pytorch#192712 (landed) and pytorch#191905.
  - Migrated every test method to the injected `device` parameter: added `device` to both class-1 methods and to `SequentialModule.__init__`, deleted the module-level `device_type` constant and the `get_devtype` import.
  - Replaced 4 `TestFSDPCheckpoint.SequentialModule(...)` references with `self.SequentialModule(...)` — `instantiate_device_type_tests` removes the original class name from scope, so module-level references raise `NameError` at runtime (latent until pytorch#187650 fixed the backend-type lookup for PrivateUse1 / out-of-tree accelerator issue pytorch#3947, because workers previously exited at `dist.barrier(device_ids=)` first).
  - Device access in test bodies uses `self.device_type` (type-only) for FSDP `device_id` (`fsdp_kwargs`), `.to(...)`, `torch.randn(device=...)`, and `SequentialModule(device=...)`; `SequentialModule.__init__` receives the type-only device from its callers and uses it directly. Type-only `device_id` keeps `_get_device_from_device_id` resolving each rank to its own current device (no single-card degeneration).
- **`tools/linter/adapters/test_linter_allowlist.json`**: removed the single `test/distributed/fsdp/test_fsdp_checkpoint.py` entry — this PR migrates that file to the linter's requirements.

## Motivation

Both classes had no `hw_classification` (silently dropped under `--hw-classification ACCELERATOR` filtering) and used `only_for=("cuda","hpu","xpu")`, excluding PrivateUse1-based out-of-tree accelerators. The module-level `device_type` constant does not change across per-device variants, and an indexed `device` passed bare to FSDP `device_id` silently degenerates multi-rank tests to single-card.

## Test Plan

- FSDP infra routes to the real accelerator backend (not a gloo/CPU false pass): `python -c "from torch.testing._internal.common_fsdp import DEVICE_TYPE, DISTRIBUTED_BACKEND, DEVICE_COUNT; print(...)"` resolves to the verify backend's accelerator type, its native comm backend, and 8 on the verify host.
- End-to-end on an 8-card accelerator backend (the verify host runs an older torch snapshot; its `common_distributed.py` carries the previously documented verification patches plus the worker-side method-level SkipTest pass-through that current main already has):

```
python test/distributed/fsdp/test_fsdp_checkpoint.py -v
# 34 cases collected: 17 accelerator-variant cases pass across both classes;
# 17 CPU-variant cases (the two generated CPU classes) are collected and
# skipped with the exact onlyAccelerator reason; 0 errors.
```

- The earlier revision of this PR (CPU variant excluded at instantiation) verified as `Ran 17 tests, OK (0 errors)`; accelerator-variant behavior is unchanged between the two forms.
- `lintrunner` clean; landed test linter: 0 violations.
- With the entry removed, the TEST_LINTER full re-check passes for the migrated file: `python3 tools/linter/adapters/test_linter.py tools/linter/adapters/test_linter_allowlist.json` (the allowlist-as-argument form lintrunner triggers in CI) — rc=0, zero lint messages for `test/distributed/fsdp/test_fsdp_checkpoint.py`.

## Notes

- The method-level `@onlyAccelerator` form (rather than `except_for=("cpu",)`) keeps the CPU variant in collection so CPU-only hosts report explicit skips instead of the classes silently disappearing. This PR's base already translates a worker-side `unittest.SkipTest` into a skip result, and the trade-off — one extra class-level worker/PG lifecycle per generated CPU class on accelerator hosts, with no change in accelerator-variant execution — was accepted for collection visibility. Same form as pytorch#192712 (landed) and pytorch#191905.
- This is part of the test case refactoring initiative tracked in pytorch#185590.

Pull Request resolved: pytorch#191419
Approved by: https://github.com/jialinw77, https://github.com/can-gaa-hou, https://github.com/d4l3k
twthighd added a commit to twthighd/pytorch that referenced this pull request Nov 9, 2026
…EST_LINTER allowlist

Summary:
- This PR migrates test/distributed/fsdp/test_fsdp_backward_prefetch.py to the TEST_LINTER's
  requirements (hw_classification on every test class plus the per-classification
  structure rules), and the file currently sits in test_linter_allowlist.json.
  Drop its one-line entry in the same PR: the allowlist is a regenerated snapshot
  of non-compliant files, and a migration landing without the deletion leaves a
  silently-stale exemption (canonical: pytorch#191905 needed the follow-up pytorch#202866).

This lets the linter enforce its requirements on future changes to this file
as part of the same change, instead of a separate cleanup PR later.
twthighd added a commit to twthighd/pytorch that referenced this pull request Nov 9, 2026
…_LINTER allowlist

Summary:
- This PR migrates test/distributed/fsdp/test_fsdp_clip_grad_norm.py to the TEST_LINTER's
  requirements (hw_classification on every test class plus the per-classification
  structure rules), and the file currently sits in test_linter_allowlist.json.
  Drop its one-line entry in the same PR: the allowlist is a regenerated snapshot
  of non-compliant files, and a migration landing without the deletion leaves a
  silently-stale exemption (canonical: pytorch#191905 needed the follow-up pytorch#202866).

This lets the linter enforce its requirements on future changes to this file
as part of the same change, instead of a separate cleanup PR later.
twthighd added a commit to twthighd/pytorch that referenced this pull request Nov 9, 2026
… allowlist

Summary:
- This PR migrates test/distributed/test_shmem_triton.py to the TEST_LINTER's
  requirements (hw_classification on every test class plus the per-classification
  structure rules), and the file currently sits in test_linter_allowlist.json.
  Drop its one-line entry in the same PR: the allowlist is a regenerated snapshot
  of non-compliant files, and a migration landing without the deletion leaves a
  silently-stale exemption (canonical: pytorch#191905 needed the follow-up pytorch#202866).

This lets the linter enforce its requirements on future changes to this file
as part of the same change, instead of a separate cleanup PR later.
dominikjastrzebskix pushed a commit to dominikjastrzebskix/pytorch that referenced this pull request Nov 9, 2026
…nt test in test_multi_threaded_pg (pytorch#191905)

## Summary

Split the device-dependent tests out of the mixed TestCollectivesWithBaseClass into a dedicated ACCELERATOR class instantiated via instantiate_device_type_tests, and label the CPU-only collective classes GENERIC, so the threaded-collective tests are correctly classified under --hw-classification filtering and run on-device via an injected device parameter instead of a module-level device string.

## Changes

- **`test/distributed/test_multi_threaded_pg.py`**:
  - Labeled `TestCollectivesWithWrapper` and `TestCollectivesWithBaseClass` with `hw_classification = HardwareClassification.GENERIC` (device-agnostic threaded-collective logic on CPU tensors).
  - Moved `test_bwd_sees_fwd_pg` (requires an accelerator; asserts that autograd backward runs in the same thread as forward and can still see the process group on a device tensor) out of `TestCollectivesWithBaseClass` into a new parallel `TestCollectivesWithBaseClassDevice` class labeled `hw_classification = HardwareClassification.ACCELERATOR`.
  - During the rebase onto main, `test_collectives_issued_from_side_stream` (added upstream in pytorch#195281, reading the module-level device string this PR deletes) moved into the ACCELERATOR class as well, with the same `device` parameter and helpers derived from the variant's `self.device_type`.
  - Extracted a shared `_TestCollectivesBase(MultiThreadedTestCase)` fixture base holding `world_size` / `setUp` / `tearDown` (the `TORCH_DIST_INIT_BARRIER` env setup plus `_spawn_threads`); both `TestCollectivesWithBaseClass` and `TestCollectivesWithBaseClassDevice` inherit it. The base has no `test_*` methods, so unittest does not collect it and `--hw-classification` filtering does not scan it; no `hw_classification` label is needed on it.
  - Both ACCELERATOR methods take a `device` parameter, and `TestCollectivesWithBaseClassDevice` is instantiated via `instantiate_device_type_tests(..., allow_xpu=True)`. The `device` is injected per-variant rather than read from a module-level device string.
  - Removed the module-level `device_type = acc.type if (acc := torch.accelerator.current_accelerator()) else "cpu"` string and the pre-existing `@skip_if_lt_x_gpu(1)` decorator on `test_bwd_sees_fwd_pg` (plus its `skip_if_lt_x_gpu` import). Both tests carry the variant-aware `@onlyAccelerator` gate instead: the CPU variant is generated but skips cleanly on every host (the guard reads the running variant's `device_type`), while accelerator variants run on-device with the injected `device`. On a CPU-only host the two tests are now collected and report explicit skips instead of the class being invisible. Same gating shape as the landed pytorch#192712.
  - Rank threads are bound to the injected device: `MultiThreadedTestCase` runs each test in freshly spawned threads whose thread-local current device starts at the default index, while the injected device string is resolved in the main thread at class-creation time. Both tests now call `torch.accelerator.set_device_index(device)` up front, `test_collectives_issued_from_side_stream` passes the injected device to its tensors and to `Stream()`/`current_stream()` explicitly, and the type-only helpers (`get_cycles_per_ms`, `device_sleep`) keep `self.device_type` — their sleep kernel now lands on the bound device.

## Motivation

`TestCollectivesWithBaseClass` was a mixed class: CPU threaded-collective methods plus accelerator-dependent methods (`test_bwd_sees_fwd_pg`, and later `test_collectives_issued_from_side_stream`). Under `--hw-classification` filtering such a mixed class cannot be classified unambiguously, and the accelerator method resolved its device from a module-level string (`device_type = acc.type if ...`) rather than the per-variant `device` parameter injected by `instantiate_device_type_tests` — so non-default device variants would use the wrong device. Splitting the class by hardware dependency and instantiating the ACCELERATOR class with an injected `device` parameter makes the classification unambiguous and aligns with the TEST_LINTER direction (PR pytorch#190173): instantiated, `device` param on every `test_*`, no `only_for`.

The gating resolves the review divergence concern without hiding the CPU variant from collection: `@onlyAccelerator` reads the running variant's `device_type` (unlike `@skip_if_lt_x_gpu(1)`, which checked only global accelerator availability and would let a CPU variant run with `device="cpu"` on accelerator hosts), so the CPU variant is collected and skips everywhere while accelerator variants keep the original on-device execution.

## Test Plan

Verified on an 8-device accelerator backend with the threaded reference PG:

```bash
python test/distributed/test_multi_threaded_pg.py
# Ran 27 tests — 24 ok, 2 skipped, 1 error. The two skips are the CPU-variant
# methods reporting "onlyAccelerator: doesn't run on CPU or meta devices"
# after the threaded fixture initializes; test_bwd_sees_fwd_pg passes
# on-device on the accelerator variant. The one error is
# test_collectives_issued_from_side_stream on the accelerator variant: the
# backend's _sleep entry point exists but the Sleep operator is not
# installed in its operator package (error 161001), so that test's execution
# stays with CUDA/XPU CI. After syncing the verify host's older pure-Python
# test-infra file (its snapshot predates pytorch#196786), the two TestThreadLocalWorld
# tests pass.

python test/distributed/test_multi_threaded_pg.py --hw-classification GENERIC
# OK — total=27, passed=23, unclassified=0, mismatched=4

python test/distributed/test_multi_threaded_pg.py --hw-classification ACCELERATOR
# total=27, passed=4, unclassified=0, mismatched=23 (errors=1, skipped=2:
# the environmental Sleep error and the two CPU-variant skips)
```

Non-default device index: with the primary device set to a non-default index before collection, the injected device carries that index, and each of the four rank threads' current device moves from the default card to the injected index (threads start on the default card; the binding lands them on the variant's device) while test_bwd_sees_fwd_pg passes on the accelerator variant. `lintrunner` reports no lint issues.

## Notes

- This is part of the test case refactoring initiative tracked in pytorch#185590.

Pull Request resolved: pytorch#191905
Approved by: https://github.com/jishuangfeng, https://github.com/jialinw77, https://github.com/KarhouTam, https://github.com/can-gaa-hou, https://github.com/d4l3k
dominikjastrzebskix pushed a commit to dominikjastrzebskix/pytorch that referenced this pull request Nov 9, 2026
…devices and label ACCELERATOR classes (pytorch#191419)

## Summary

This PR labels `TestFSDPCheckpoint` and `TestFSDPCheckpointSubmodule` as `HardwareClassification.ACCELERATOR`, switches both to `instantiate_device_type_tests(..., allow_xpu=True)` with the variant-aware `@onlyAccelerator` gate on every test method, and completes the device-parameter migration in the test bodies. Rebased onto upstream's migration of both classes onto the `FSDPTestContinuous` base (kept); type-only device access in test bodies uses `self.device_type` (canonical idiom, unambiguous under `DeviceTypeTestBase` injection).

## Changes

- **`test/distributed/fsdp/test_fsdp_checkpoint.py`**:
  - Added `hw_classification = HardwareClassification.ACCELERATOR` to both classes, plus the import. Both classes' `test_*` methods require an accelerator (`@skip_if_lt_x_gpu(2)`, `device` param) and exercise FSDP checkpoint behavior on the device-tensor path.
  - Switched `TestFSDPCheckpoint` from `@instantiate_parametrized_tests` to `instantiate_device_type_tests(..., allow_xpu=True)` (deleting the class decorator avoids the double-expansion trap); `TestFSDPCheckpointSubmodule`'s hard-coded `only_for=("cuda","hpu","xpu")` device list was removed in the preceding commit and it now uses the same form. ACCELERATOR classes must not use `only_for` (landed test linter pytorch#190173).
  - All three test methods carry the variant-aware `@onlyAccelerator` (stacked above `@skip_if_lt_x_gpu(2)`): the CPU variant is generated and collected everywhere but skips cleanly with the exact reason `onlyAccelerator: doesn't run on CPU or meta devices` (the guard reads the running variant's `device_type`, unlike availability-based guards), so CPU-only machines report explicit skips instead of the classes being invisible, while accelerator variants run unchanged. Same gating form as pytorch#192712 (landed) and pytorch#191905.
  - Migrated every test method to the injected `device` parameter: added `device` to both class-1 methods and to `SequentialModule.__init__`, deleted the module-level `device_type` constant and the `get_devtype` import.
  - Replaced 4 `TestFSDPCheckpoint.SequentialModule(...)` references with `self.SequentialModule(...)` — `instantiate_device_type_tests` removes the original class name from scope, so module-level references raise `NameError` at runtime (latent until pytorch#187650 fixed the backend-type lookup for PrivateUse1 / out-of-tree accelerator issue pytorch#3947, because workers previously exited at `dist.barrier(device_ids=)` first).
  - Device access in test bodies uses `self.device_type` (type-only) for FSDP `device_id` (`fsdp_kwargs`), `.to(...)`, `torch.randn(device=...)`, and `SequentialModule(device=...)`; `SequentialModule.__init__` receives the type-only device from its callers and uses it directly. Type-only `device_id` keeps `_get_device_from_device_id` resolving each rank to its own current device (no single-card degeneration).
- **`tools/linter/adapters/test_linter_allowlist.json`**: removed the single `test/distributed/fsdp/test_fsdp_checkpoint.py` entry — this PR migrates that file to the linter's requirements.

## Motivation

Both classes had no `hw_classification` (silently dropped under `--hw-classification ACCELERATOR` filtering) and used `only_for=("cuda","hpu","xpu")`, excluding PrivateUse1-based out-of-tree accelerators. The module-level `device_type` constant does not change across per-device variants, and an indexed `device` passed bare to FSDP `device_id` silently degenerates multi-rank tests to single-card.

## Test Plan

- FSDP infra routes to the real accelerator backend (not a gloo/CPU false pass): `python -c "from torch.testing._internal.common_fsdp import DEVICE_TYPE, DISTRIBUTED_BACKEND, DEVICE_COUNT; print(...)"` resolves to the verify backend's accelerator type, its native comm backend, and 8 on the verify host.
- End-to-end on an 8-card accelerator backend (the verify host runs an older torch snapshot; its `common_distributed.py` carries the previously documented verification patches plus the worker-side method-level SkipTest pass-through that current main already has):

```
python test/distributed/fsdp/test_fsdp_checkpoint.py -v
# 34 cases collected: 17 accelerator-variant cases pass across both classes;
# 17 CPU-variant cases (the two generated CPU classes) are collected and
# skipped with the exact onlyAccelerator reason; 0 errors.
```

- The earlier revision of this PR (CPU variant excluded at instantiation) verified as `Ran 17 tests, OK (0 errors)`; accelerator-variant behavior is unchanged between the two forms.
- `lintrunner` clean; landed test linter: 0 violations.
- With the entry removed, the TEST_LINTER full re-check passes for the migrated file: `python3 tools/linter/adapters/test_linter.py tools/linter/adapters/test_linter_allowlist.json` (the allowlist-as-argument form lintrunner triggers in CI) — rc=0, zero lint messages for `test/distributed/fsdp/test_fsdp_checkpoint.py`.

## Notes

- The method-level `@onlyAccelerator` form (rather than `except_for=("cpu",)`) keeps the CPU variant in collection so CPU-only hosts report explicit skips instead of the classes silently disappearing. This PR's base already translates a worker-side `unittest.SkipTest` into a skip result, and the trade-off — one extra class-level worker/PG lifecycle per generated CPU class on accelerator hosts, with no change in accelerator-variant execution — was accepted for collection visibility. Same form as pytorch#192712 (landed) and pytorch#191905.
- This is part of the test case refactoring initiative tracked in pytorch#185590.

Pull Request resolved: pytorch#191419
Approved by: https://github.com/jialinw77, https://github.com/can-gaa-hou, https://github.com/d4l3k
twthighd added a commit to twthighd/pytorch that referenced this pull request Nov 13, 2026
…_LINTER allowlist

Summary:
- This PR migrates test/distributed/fsdp/test_fsdp_clip_grad_norm.py to the TEST_LINTER's
  requirements (hw_classification on every test class plus the per-classification
  structure rules), and the file currently sits in test_linter_allowlist.json.
  Drop its one-line entry in the same PR: the allowlist is a regenerated snapshot
  of non-compliant files, and a migration landing without the deletion leaves a
  silently-stale exemption (canonical: pytorch#191905 needed the follow-up pytorch#202866).

This lets the linter enforce its requirements on future changes to this file
as part of the same change, instead of a separate cleanup PR later.
twthighd added a commit to twthighd/pytorch that referenced this pull request Nov 16, 2026
…llowlist

Summary:
- This PR migrates test/distributed/fsdp/test_fsdp_apply.py to the TEST_LINTER's
  requirements (hw_classification on every test class plus the per-classification
  structure rules), and the file currently sits in test_linter_allowlist.json.
  Drop its one-line entry in the same PR: the allowlist is a regenerated snapshot
  of non-compliant files, and a migration landing without the deletion leaves a
  silently-stale exemption (canonical: pytorch#191905 needed the follow-up pytorch#202866).

This lets the linter enforce its requirements on future changes to this file
as part of the same change, instead of a separate cleanup PR later.
twthighd added a commit to twthighd/pytorch that referenced this pull request Nov 16, 2026
… allowlist

Summary:
- This PR migrates test/distributed/test_shmem_triton.py to the TEST_LINTER's
  requirements (hw_classification on every test class plus the per-classification
  structure rules), and the file currently sits in test_linter_allowlist.json.
  Drop its one-line entry in the same PR: the allowlist is a regenerated snapshot
  of non-compliant files, and a migration landing without the deletion leaves a
  silently-stale exemption (canonical: pytorch#191905 needed the follow-up pytorch#202866).

This lets the linter enforce its requirements on future changes to this file
as part of the same change, instead of a separate cleanup PR later.
twthighd added a commit to twthighd/pytorch that referenced this pull request Nov 18, 2026
…EST_LINTER allowlist

Summary:
- This PR migrates test/distributed/test_c10d_functional_native.py to the TEST_LINTER's
  requirements (hw_classification on every test class plus the per-classification
  structure rules), and the file currently sits in test_linter_allowlist.json.
  Drop its one-line entry in the same PR: the allowlist is a regenerated snapshot
  of non-compliant files, and a migration landing without the deletion leaves a
  silently-stale exemption (canonical: pytorch#191905 needed the follow-up pytorch#202866).

This lets the linter enforce its requirements on future changes to this file
as part of the same change, instead of a separate cleanup PR later.
twthighd added a commit to twthighd/pytorch that referenced this pull request Nov 23, 2026
…label classes

Summary:
- Split the mixed TestFakePG class: the fake-PG mechanism tests
  (copy semantics, requires_grad propagation, validation errors,
  dispatch tracing) stay in TestFakePG labeled GENERIC, while the
  nine accelerator-path tests (FSDP/TP e2e, functional-collectives
  tracing, split_group) move to a new TestFakePGDevice labeled
  ACCELERATOR.
- TestFakePGDevice is instantiated via instantiate_device_type_tests
  with allow_xpu=True, and the moved methods take the injected device
  parameter instead of the module-level device_type = get_devtype().type
  (deleted).
- Gate the nine methods with the variant-aware @onlyAccelerator instead
  of excluding the CPU variant: the CPU variant is collected and skips
  cleanly on every host (the guard reads the running variant's
  device_type), so CPU-only machines report explicit skips instead of
  the class being invisible, while accelerator variants run unchanged.
  Same gating form as the landed pytorch#192712 and pytorch#191905. @skipIfHpu
  blacklist guards are kept.
- Rebase absorption: keep pytorch#202253's six compile-sync methods in the
  GENERIC TestFakePG (14 expanded cases, parametrizations and gloo
  availability guards intact; their config strings naming cuda/nccl do
  not exercise device-tensor paths) and pytorch#202141's whole-file adjustments
  (fake backend moved to a Python PyBackend, new GENERIC
  debug-wrapper/alias/split-options tests, updated assertion patterns);
  the unittest/unittest.mock imports the new tests use are kept.

This allows the accelerator-path tests to run on any out-of-tree
accelerator variant instead of being skipped wherever HAS_ACCELERATOR
is False, while the device-agnostic fake-PG mechanics stay in a
GENERIC class that runs everywhere.

Verified on an old-snapshot verify host (its C++ fake backend predates
the Python PyBackend move):
- The generated CPU class collects 15 cases, all skipped with the exact
  reason "onlyAccelerator: doesn't run on CPU or meta devices" (OK,
  0 errors).
- A whole-file run is not obtainable there: a newly absorbed GENERIC
  test crashes the old C++ fake backend.
- The PrivateUse1 variant fails there at the fake backend's
  device-type lookup (observed error).

Full-file validation on the rebased stack remains pending. GENERIC/CPU
and accelerator coverage will be checked in the corresponding CI jobs
where selected. PrivateUse1 execution additionally requires pytorch#202363 at
the new registration site, with the backend plugin loaded before fake
registration, and a compatible verification environment; ordinary
CPU/CUDA CI does not cover it.
twthighd added a commit to twthighd/pytorch that referenced this pull request Nov 23, 2026
…wlist

Summary:
- This PR migrates test/distributed/test_fake_pg.py to the TEST_LINTER's
  requirements (hw_classification on every test class plus the per-classification
  structure rules), and the file currently sits in test_linter_allowlist.json.
  Drop its one-line entry in the same PR: the allowlist is a regenerated snapshot
  of non-compliant files, and a migration landing without the deletion leaves a
  silently-stale exemption (canonical: pytorch#191905 needed the follow-up pytorch#202866).

This lets the linter enforce its requirements on future changes to this file
as part of the same change, instead of a separate cleanup PR later.
twthighd added a commit to twthighd/pytorch that referenced this pull request Nov 23, 2026
… TEST_LINTER allowlist

Summary:
- This PR migrates test/distributed/test_functional_differentials.py to the TEST_LINTER's
  requirements (hw_classification on every test class plus the per-classification
  structure rules), and the file currently sits in test_linter_allowlist.json.
  Drop its one-line entry in the same PR: the allowlist is a regenerated snapshot
  of non-compliant files, and a migration landing without the deletion leaves a
  silently-stale exemption (canonical: pytorch#191905 needed the follow-up pytorch#202866).

This lets the linter enforce its requirements on future changes to this file
as part of the same change, instead of a separate cleanup PR later.
pytorchmergebot pushed a commit to twthighd/pytorch that referenced this pull request Nov 25, 2026
…LINTER allowlist

Summary:
- This PR migrates test/distributed/fsdp/test_checkpoint_wrapper.py to the TEST_LINTER's
  requirements (hw_classification on every test class plus the per-classification
  structure rules), and the file currently sits in test_linter_allowlist.json.
  Drop its one-line entry in the same PR: the allowlist is a regenerated snapshot
  of non-compliant files, and a migration landing without the deletion leaves a
  silently-stale exemption (canonical: pytorch#191905 needed the follow-up pytorch#202866).

This lets the linter enforce its requirements on future changes to this file
as part of the same change, instead of a separate cleanup PR later.
twthighd added a commit to twthighd/pytorch that referenced this pull request Nov 26, 2026
…TEST_LINTER allowlist

Summary:
- This PR migrates test/distributed/test_c10d_object_collectives.py to the TEST_LINTER's
  requirements (hw_classification on every test class plus the per-classification
  structure rules), and the file currently sits in test_linter_allowlist.json.
  Drop its one-line entry in the same PR: the allowlist is a regenerated snapshot
  of non-compliant files, and a migration landing without the deletion leaves a
  silently-stale exemption (canonical: pytorch#191905 needed the follow-up pytorch#202866).

This lets the linter enforce its requirements on future changes to this file
as part of the same change, instead of a separate cleanup PR later.
twthighd added a commit to twthighd/pytorch that referenced this pull request Nov 27, 2026
… allowlist

Summary:
- This PR migrates test/distributed/test_shmem_triton.py to the TEST_LINTER's
  requirements (hw_classification on every test class plus the per-classification
  structure rules), and the file currently sits in test_linter_allowlist.json.
  Drop its one-line entry in the same PR: the allowlist is a regenerated snapshot
  of non-compliant files, and a migration landing without the deletion leaves a
  silently-stale exemption (canonical: pytorch#191905 needed the follow-up pytorch#202866).

This lets the linter enforce its requirements on future changes to this file
as part of the same change, instead of a separate cleanup PR later.
twthighd added a commit to twthighd/pytorch that referenced this pull request Nov 27, 2026
…lowlist

Summary:
- This PR migrates test/distributed/fsdp/test_fsdp_core.py to the TEST_LINTER's
  requirements (hw_classification on every test class plus the per-classification
  structure rules), and the file currently sits in test_linter_allowlist.json.
  Drop its one-line entry in the same PR: the allowlist is a regenerated snapshot
  of non-compliant files, and a migration landing without the deletion leaves a
  silently-stale exemption (canonical: pytorch#191905 needed the follow-up pytorch#202866).

This lets the linter enforce its requirements on future changes to this file
as part of the same change, instead of a separate cleanup PR later.
twthighd added a commit to twthighd/pytorch that referenced this pull request Nov 27, 2026
…llowlist

Summary:
- This PR migrates test/distributed/fsdp/test_fsdp_apply.py to the TEST_LINTER's
  requirements (hw_classification on every test class plus the per-classification
  structure rules), and the file currently sits in test_linter_allowlist.json.
  Drop its one-line entry in the same PR: the allowlist is a regenerated snapshot
  of non-compliant files, and a migration landing without the deletion leaves a
  silently-stale exemption (canonical: pytorch#191905 needed the follow-up pytorch#202866).

This lets the linter enforce its requirements on future changes to this file
as part of the same change, instead of a separate cleanup PR later.
twthighd added a commit to twthighd/pytorch that referenced this pull request Dec 2, 2026
…lowlist

Summary:
- This PR migrates test/distributed/fsdp/test_fsdp_core.py to the TEST_LINTER's
  requirements (hw_classification on every test class plus the per-classification
  structure rules), and the file currently sits in test_linter_allowlist.json.
  Drop its one-line entry in the same PR: the allowlist is a regenerated snapshot
  of non-compliant files, and a migration landing without the deletion leaves a
  silently-stale exemption (canonical: pytorch#191905 needed the follow-up pytorch#202866).

This lets the linter enforce its requirements on future changes to this file
as part of the same change, instead of a separate cleanup PR later.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/trunk Trigger trunk jobs on your pull request Merged module: tests/distributed This label covers all test cases within the Distributed sheet. open source 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

Status: Done

Development

Successfully merging this pull request may close these issues.

10 participants