Skip to content

[Testcase Refactoring][FSDP] Run backend-independent GradScaler tests - #191178

Draft
danieldale2026 wants to merge 5 commits into
pytorch:mainfrom
danieldale2026:test/fsdp-sharded-grad-scaler-cpu-tests
Draft

danieldale2026 wants to merge 5 commits into
pytorch:mainfrom
danieldale2026:test/fsdp-sharded-grad-scaler-cpu-tests

Conversation

@danieldale2026

@danieldale2026 danieldale2026 commented Jul 27, 2026 •

Copy link
Copy Markdown
Contributor

I reviewed the code at the current head, the review threads, and the validation evidence, and take responsibility for this submission and the statements in the quoted body. The quoted body was prepared with agent assistance; it is relevant because it summarizes the exact current-head change and its validation so reviewers can verify both directly. The title is unchanged.

Issue

Part of #185590. This references the tracking issue only and does not close it.

Summary

Run backend-independent ShardedGradScaler unit tests on CPU while admitting the accelerator FSDP/DDP parity tests through the device-type harness with open admission.

Changes

  • Label TestShardGradScaler as HardwareClassification.GENERIC and remove accelerator-availability skips from its three tests, which use CPU tensors, a CPU scaler (device="cpu"), and DummyProcessGroup.
  • Label TestShardedGradScalerParityWithDDP as HardwareClassification.ACCELERATOR, keeping its @requires_capabilities(distributed.backend, distributed.fsdp) and skip_if_lt_x_gpu(2) gates.
  • Instantiate the parity class with except_for=("cpu",) and retain allow_xpu=True for the reviewed XPU opt-in, admitting supported accelerators (CUDA, XPU, HPU, PrivateUse1) through the harness.
  • Remove imports that become unused after the classification change.

Motivation

The three GENERIC tests validate scaling, sparse-gradient unscaling, and optimizer-step skipping with CPU tensors; they do not require an accelerator. The fixed device="cpu" is intentional: ShardedGradScaler uses its configured device when synchronizing found_inf tensors, so using the ambient accelerator type would reintroduce accelerator coupling into otherwise CPU-only tests.

The parity tests do require an accelerator. The original code admitted HPU through the explicit TEST_HPU branch in skip_if_lt_x_gpu and the DEVICE_TYPE="hpu:0" / DISTRIBUTED_BACKEND="hccl" path in common_fsdp.py; except_for=("cpu",) restores that admission. Execution on an admitted accelerator remains gated by the distributed backend/FSDP capability checks and the two-device requirement.

Test Plan

python test/distributed/fsdp/test_fsdp_sharded_grad_scaler.py TestShardGradScaler -v
python -m py_compile test/distributed/fsdp/test_fsdp_sharded_grad_scaler.py
spin quicklint -- --skip PYREFLY test/distributed/fsdp/test_fsdp_sharded_grad_scaler.py
git diff --check

Results, from a candidate-local CPU-only build (MAX_JOBS=8, CUDA not built):

  • GENERIC CPU tests: 3 executed, 3 passed; 0 failed, 0 errors, 0 skipped, 0 timed out, with the executed test source identical to the committed head.
  • Admission AST check for except_for == ("cpu",) with allow_xpu is True: pass.
  • py_compile, scoped quicklint, and git diff --check: pass.
  • CUDA runtime: NOT_RUN (CPU-only build). HPU runtime: NOT_RUN (no HPU runtime available); the HPU admission conclusion is source-based.

Notes

  • Topic-level consumer: test/distributed/fsdp/test_fsdp_sharded_grad_scaler.py.
  • Base: main@3b4bcd4f227686bcaa0c5f8aa887ca55f387ce07.
  • Head: 228c40add9f36846f233b1653e4f9f0d3a593bbf.
  • Remote view: 5 commits, 6 files, +436/-124, including inherited framework/helper ancestry.
  • HPU is re-admitted conditionally: gated by the distributed backend/FSDP capabilities and the two-device requirement; no public API changes are introduced.

Checklist

  • Scoped quicklint passed for the changed test file.
  • Added/updated tests.
  • Documentation update not applicable; this is internal test infrastructure.
  • Benchmark results not applicable; no performance behavior changes.

BC-breaking?

No.

@pytorch-bot

pytorch-bot Bot commented Jul 27, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

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

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

❗ 1 Active SEVs

There are 1 currently active SEVs. If your PR is affected, please view them below:

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 Jul 27, 2026
@danieldale2026 danieldale2026 changed the title [FSDP] Run backend-independent GradScaler tests [Testcase Refactoring][FSDP] Run backend-independent GradScaler tests Jul 28, 2026
@danieldale2026

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 Jul 28, 2026
@danieldale2026
danieldale2026 marked this pull request as ready for review July 28, 2026 03:46
@drisspg drisspg added the triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module label Jul 29, 2026
@drisspg
drisspg requested a review from fffrog July 29, 2026 03:57
@fffrog fffrog moved this to Backlog in PyTorch Test Refactoring Jul 29, 2026
…device capability

Builds on pytorch#190846 (now in main) to add:
- distributed.backend/dtensor/fsdp, memory.non_blocking_copy, stream.generic,
  lib.safetensors capability namespaces with _check_capabilities preflight
  in DeviceTypeTestBase.setUp (cooperative super() for MultiProcessTestCase)
- dtype.fp64: CPU/CUDA=True, XPU=dynamic via has_fp64, HPU=False
- HPU distributed backend+fsdp declarations (nested setdefault to preserve
  inherited capabilities) so HPU reaches FP64 unsupported skip instead of
  missing-capability assertion
- OpenReg capability regression: supported executes, unsupported SkipTest,
  missing AssertionError; CPU/CUDA/HPU FP64 declarations and XPU dynamic

Test Plan:
- py_compile, FLAKE8, RUFF, git diff --check pass
- OpenReg: 21 passed, 6 skipped, 2 xfailed (capability three-state verified)
- NPU (69231fe + v2 overlay): 23 distributed test files, 18 PASS / 1 FAIL
  (CANN version) / 2 PARTIAL (NPU FP64 + torchdistX) / 2 SKIP
@danieldale2026
danieldale2026 force-pushed the test/fsdp-sharded-grad-scaler-cpu-tests branch from 6e691d5 to 8b6783c Compare August 20, 2026 08:05
@danieldale2026
danieldale2026 marked this pull request as draft August 20, 2026 08:08
Temporary dependency equivalent to pytorch#187650 for this provisional consumer topic.
This commit was authored with AI assistance and reviewed by the contributor.

Test Plan:

```
PYTHONPYCACHEPREFIX=/tmp/pr-split-23-pycache /home/daniel/workspace/pytorch-dev/pytorch/.venv/bin/python -m py_compile test/distributed/fsdp/test_fsdp_sharded_grad_scaler.py
uvx --python 3.10 [email protected] -a --skip PYREFLY test/distributed/fsdp/test_fsdp_sharded_grad_scaler.py
git diff --cached --check
```
@danieldale2026
danieldale2026 force-pushed the test/fsdp-sharded-grad-scaler-cpu-tests branch from 8b6783c to 15fcbef Compare August 20, 2026 09:12
kerer-ai pushed a commit to kerer-ai/pytorch-nv that referenced this pull request Aug 26, 2026
pg = DummyProcessGroup(0, 1)
scaler = ShardedGradScaler(
device=device_type, init_scale=2.0, process_group=pg, enabled=True
device="cpu", init_scale=2.0, process_group=pg, enabled=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.

should device be fixed to 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.

I reviewed and verified the current implementation. This response was prepared with Codex assistance.

device="cpu" is intentional. These three tests use only CPU tensors and DummyProcessGroup, so keeping the scaler on CPU makes them genuinely backend-independent and avoids reintroducing accelerator coupling.

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.

I rechecked the implementation and data flow at the current head and take responsibility for the statements below, which were prepared with agent assistance. The quoted reply is relevant because it directly answers whether pinning the scaler to CPU is intentional.

device="cpu" is intentional here. TestShardGradScaler is a GENERIC single-process test using DummyProcessGroup; all tensors, gradients, optimizer parameters, and losses are on CPU. ShardedGradScaler also uses its configured device when synchronizing CPU found_inf tensors, so using the ambient accelerator type would reintroduce an unnecessary accelerator dependency. Keeping the scaler on CPU is therefore consistent with the behavior this class tests.

instantiate_device_type_tests(
TestShardedGradScalerParityWithDDP,
globals(),
only_for=("cuda", "xpu", "privateuse1"),

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

We could only use except_for, not only_for when current class is tagged accelerator.

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.

I reviewed and verified the current implementation. This response was prepared with Codex assistance.

Updated the accelerator test instantiation to use except_for=("cpu", "hpu") while retaining allow_xpu=True. This keeps the requested HPU exclusion and allows supported accelerator and PrivateUse1 implementations to participate through the device-type harness.

Keep the explicit HPU exclusion while allowing supported accelerator and PrivateUse1 test bases through except_for. The CPU-only scaler tests remain fixed to CPU.

Test Plan:

```
PYTHONPYCACHEPREFIX=/tmp/pr191178-review-fix-20260904-pycache /home/daniel/workspace/pytorch-dev/pytorch/.venv/bin/python test/distributed/fsdp/test_fsdp_sharded_grad_scaler.py TestShardGradScaler -v
PYTHONPYCACHEPREFIX=/tmp/pr191178-review-fix-20260904-pycache /home/daniel/workspace/pytorch-dev/pytorch/.venv/bin/python test/distributed/fsdp/test_fsdp_sharded_grad_scaler.py TestShardedGradScalerParityWithDDPCUDA -v
python3 -m py_compile test/distributed/fsdp/test_fsdp_sharded_grad_scaler.py
git diff --check
/home/daniel/workspace/pytorch-dev/pytorch/.venv/bin/spin fixlint
```

The CPU tests passed 3/3. The CUDA parity collection ran 17 tests, all skipped because this host has one GPU. spin fixlint was attempted but could not bootstrap because uvx is unavailable.

Authored with Codex assistance and reviewed by the contributor.
instantiate_device_type_tests(
TestShardedGradScalerParityWithDDP,
globals(),
except_for=("cpu", "hpu"),

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

why exclude hpu here? I didn't find related logic in original code.

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.

I rechecked the pre-change implementation and the current head and take responsibility for the statements below, which were prepared with agent assistance. The quoted reply is relevant because it directly answers why HPU should not be excluded and which runtime remains unvalidated.

You're right. I found no source-based reason to exclude HPU: the original parity tests admitted HPU through the explicit TEST_HPU branch in skip_if_lt_x_gpu and the DEVICE_TYPE="hpu:0" / DISTRIBUTED_BACKEND="hccl" path in common_fsdp.py. The exclusion was carried over when the earlier only_for whitelist was converted to except_for; it did not preserve the original admission behavior.

I changed the instantiation to except_for=("cpu",) while retaining allow_xpu=True, and the fix is pushed at the current head. HPU execution is still gated by its distributed backend/FSDP capabilities and the two-device requirement. I do not have HPU runtime validation, so this conclusion is based on source and admission-path reconciliation rather than an HPU runtime pass.

The original FSDP parity tests admit HPU through the existing backend/FSDP capabilities and multi-device gate. Remove the consumer-level HPU exclusion while retaining CPU exclusion and allow_xpu=True.

HPU runtime was not available locally; validation used source-consistent CPU tests and static admission checks.

This branch has not been deployed

No deployments
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 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: Backlog

Development

Successfully merging this pull request may close these issues.

4 participants