Skip to content

[Testcase Refactoring] Include a loaded PrivateUse1 backend in the fake backend's device list - #202363

Closed
twthighd wants to merge 1 commit into
pytorch:mainfrom
twthighd:test/fake-pg-privateuse1-registration
Closed

twthighd wants to merge 1 commit into
pytorch:mainfrom
twthighd:test/fake-pg-privateuse1-registration

Conversation

@twthighd

@twthighd twthighd commented Oct 28, 2026 •

Copy link
Copy Markdown
Contributor

Summary

This PR makes the fake backend register itself for a PrivateUse1-based out-of-tree backend when that backend's module is loaded, so device-type-instantiated fake-PG tests can generate and run PrivateUse1 variants instead of failing with "No backend type associated with device type PrivateUse1".

Changes

  • torch/distributed/_backends/fake.py:
    • _register_fake_backend() now builds the device list at invocation time: the built-in four ("cpu", "cuda", "hpu", "xpu") plus the renamed PrivateUse1 backend name when its module is present on torch (an hasattr gate, no hardware query), and passes it to register_backend instead of the fixed list.
    • Without an out-of-tree plugin the list is unchanged, so CPU-only environments keep the original registration behavior.
    • The registrar is invoked through lazy backend loading (the torch.distributed.backends entry point, or the built-in fallback) or through the compatibility shim torch/testing/_internal/distributed/fake_pg.py. The PrivateUse1 module must be loaded before registration runs; loading it later does not automatically refresh an existing fake registration.

Motivation

#194716 instantiates the fake-PG device tests with instantiate_device_type_tests, which auto-generates a PrivateUse1 variant when a PrivateUse1 backend is available. On such hosts init_process_group(backend="fake", device_id=<plugin device>) fails at the device-qualified backend query because fake only registered the built-in devices. The precedence rules in Backend.register_backend keep this safe for real backends: fake does not overwrite the default device map entry of an already-registered real backend, and a real backend registered later replaces fake in the default device map. This PR only extends the device list so out-of-tree backends are covered by the same registration.

Test Plan

Static: py_compile clean; targeted lintrunner shows no new findings (the one local Pyrefly missing-attribute on torch._C._get_privateuse1_backend_name is a local stub-generation lag — the API is declared in torch/_C/__init__.pyi.in, and torch/_utils.py produces the same local finding on upstream code).

Registration mechanics on a pre-move nightly CPU wheel (torch 2.16.0.dev20261029+cpu, whose Backend.register_backend/default_device_backend_map match current main), executing the shipped registrar source verbatim (AST-extracted from this PR's file) with plugin visibility simulated by a rename plus module attribute:

  • Capability append: fake's device capability becomes the built-in four plus the simulated plugin device.
  • Default map, both orders: a real backend registered before fake is not shadowed (the built-in cpu/cuda/xpu entries held by gloo/nccl/xccl are untouched; only the unclaimed hpu is added), and a real backend registered after fake replaces it for that device.
  • No-plugin regression: the capability stays exactly the built-in four; the un-renamed default name has no torch attribute, so nothing is appended.

Predicate on a real out-of-tree backend (8-device verify host): without the plugin the name stays at its default with no module attribute; after the plugin import the name flips and the module attribute appears; with default device-backend autoload the plugin is visible already after import torch, i.e. before any fake registration trigger.

Notes

@pytorch-bot

pytorch-bot Bot commented Oct 28, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

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

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

⚠️ 4 Awaiting Approval

As of commit ba58d8a with merge base 7559537 (image):

AWAITING APPROVAL - The following workflows need approval before CI can run:

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

@pytorch-bot

pytorch-bot Bot commented Oct 28, 2026

Copy link
Copy Markdown

This PR needs a release notes: label

If your changes are user facing and intended to be a part of release notes, please use a label starting with release notes:.

If not, please add the topic: not user facing label.

To add a label, you can comment to pytorchbot, for example
@pytorchbot label "topic: not user facing"

For more information, see
https://github.com/pytorch/pytorch/wiki/PyTorch-AutoLabel-Bot#why-categorize-for-release-notes-and-how-does-it-work.

@twthighd

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 Oct 28, 2026
@twthighd
twthighd force-pushed the test/fake-pg-privateuse1-registration branch from a912582 to 06a12f0 Compare October 28, 2026 07:47
@fffrog fffrog moved this to Backlog in PyTorch Test Refactoring Oct 28, 2026
twthighd added a commit to twthighd/pytorch that referenced this pull request Oct 30, 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.
…ice list

Summary:
- _register_fake_backend() registers the fake backend with a fixed device
  list ("cpu", "cuda", "hpu", "xpu"), so a fake world cannot serve an
  out-of-tree backend loaded through PrivateUse1: a device-qualified
  backend query after init_process_group(backend="fake") raises
  "No backend type associated with device type PrivateUse1". Build the
  device list when the registrar is invoked and append the renamed
  PrivateUse1 backend when its module is loaded, gated on module presence
  with no hardware query.
- The registrar is invoked through lazy backend loading or the
  compatibility shim in torch/testing/_internal/distributed/fake_pg.py;
  the PrivateUse1 module must be loaded before registration, and loading
  it later does not automatically refresh an existing fake registration.
- register_backend's precedence rules keep real backends safe: fake does
  not overwrite the default device map entry of an already-registered
  real backend, and a real backend registered later replaces fake in the
  default device map.

This allows device-type-instantiated fake-PG tests to generate and run
out-of-tree accelerator variants instead of failing at the device-qualified
backend query, while CPU-only environments keep the original registration
behavior.

This is part of the test case refactoring initiative tracked in pytorch#185590.
@twthighd
twthighd force-pushed the test/fake-pg-privateuse1-registration branch from 06a12f0 to ba58d8a Compare October 31, 2026 00:45
@pytorch-bot pytorch-bot Bot added the ciflow/torchtitan Run TorchTitan integration tests label Oct 31, 2026
@pytorch-bot

pytorch-bot Bot commented Oct 31, 2026

Copy link
Copy Markdown

The following ciflow label(s) have been added but CI has not been triggered yet because the workflows are awaiting approval:

  • ciflow/torchtitan

Once a maintainer approves the workflows (scroll to the bottom of the PR page), the corresponding CI jobs will be triggered automatically. Please ping one of the reviewers if you do not have access to approve and run workflows.

@twthighd twthighd changed the title [Testcase Refactoring] Register fake PG for a loaded PrivateUse1 backend [c10d] Include a loaded PrivateUse1 backend in the fake backend's device list Oct 31, 2026
@twthighd twthighd changed the title [c10d] Include a loaded PrivateUse1 backend in the fake backend's device list [Testcase Refactoring] Include a loaded PrivateUse1 backend in the fake backend's device list Oct 31, 2026
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 twthighd closed this Nov 23, 2026
@github-project-automation github-project-automation Bot moved this from Backlog to Done in PyTorch Test Refactoring Nov 23, 2026
twthighd added a commit to twthighd/pytorch that referenced this pull request Dec 3, 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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/torchtitan Run TorchTitan integration tests module: tests/distributed This label covers all test cases within the Distributed sheet. open source

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants