Skip to content

Conversation

iwknow
Copy link

@iwknow iwknow commented Aug 24, 2025

Currently, the implementation of torch.Generator only support "cpu" and "cuda" device type. https://github.com/pytorch/pytorch/blob/main/torch/csrc/Generator.cpp#L55-L61

This change enables torch.Generator to support more device type by allowing any device backend to register their own generator factory through a Generator Registry. This is similar to what "DeviceGuardImpl registry" does today.

Key Changes:

New registry API:

  • Added GeneratorRegistry.h and GeneratorRegistry.cpp in c10/core/impl.
  • API supports registerGenerator(DeviceType, GeneratorFactory), unregisterGenerator(DeviceType), and getGeneratorFactory(DeviceType).
  • Uses c10::DeviceType as the key and stores a factory function returning c10::intrusive_ptrc10::GeneratorImpl.

Python/C++ integration:

  • The registry is consulted in the torch.Generator constructor path for non-CPU/CUDA devices.
  • If a factory is registered for the requested device, it constructs the appropriate generator; otherwise, raises an error.

Backend extensibility:

  • Out-of-tree backends (e.g., torch_xla, torch-directml, torch_npu) can now register their custom generator implementation at module load via a static registrar object.
    Example usage:
C++
namespace {
  struct Registrar {
    Registrar() {
      at::detail::registerGenerator(c10::DeviceType::XLA, &CreateXlaGenerator);
    }
  } registrar_instance;
}

This allows torch.Generator(device='xla') to return an XlaGeneratorImpl when the torch_xla extension is imported.

Copy link

pytorch-bot bot commented Aug 24, 2025

🔗 Helpful Links

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

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:

✅ No Failures

As of commit d838990 with merge base 76f81b5 (image):
💚 Looks good so far! There are no failures yet. 💚

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

Copy link

linux-foundation-easycla bot commented Aug 24, 2025

CLA Signed

The committers listed above are authorized under a signed CLA.

@iwknow
Copy link
Author

iwknow commented Aug 24, 2025

related issue: pytorch/xla#9159

Copy link
Collaborator

@EikanWang EikanWang left a comment

Choose a reason for hiding this comment

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

Regarding out-of-tree accelerators, I think PrivateUse1 based mechanism should be the recommended option - https://github.com/pytorch/pytorch/blob/main/aten/src/ATen/detail/PrivateUse1HooksInterface.h#L76-L77

@iwknow
Copy link
Author

iwknow commented Aug 25, 2025

hi @EikanWang Thanks for reviewing!
i don't think PrivateUse1 is viable for XLA use case if my understanding is correct.

  1. for backends like XLA, it is a first‑class backend in PyTorch with its own DispatchKey::XLA and DeviceType::XLA. It is "off-tree" simply because it is developed and maintained in a different package. This is fundamentally different from the PrivateUse1 use case where the entire backend is registered under PrivateUse1, including use "DispatchKey::PrivateUse1".
  2. Or, if you are suggesting to keep XLA and only make RNG kernels explicitly consume a PrivateUse1 generator. That wouldn't work neither. torch.device('xla') is already bound to DeviceType::XLA. You can’t rebind the name “xla” to PrivateUse1 in the same process/build. Registering a PrivateUse1 backend under the “xla” name would conflict with the existing device.

@FFFrog
Copy link
Collaborator

FFFrog commented Aug 26, 2025

@EikanWang Thanks for the mention.

@iwknow PyTorch supports registering generators for out-of-tree backends by inheriting from the AcceleratorHooksInterface class. You can refer to the below link for more information.

Therefore, it seems to me that you might need to add new files named XLAHooksInterface.h and XLAHooksInterface.cpp to internally implement XLAHooksInterface in PyTorch Repo, and then create new XLAHooks.h and XLAHooks.cpp files in the XLA Repo that internally inherit from XLAHooksInterface implements the XLAHooks custom class and registers it in PyTorch.

Comment on lines 62 to 66
} else if (c10::impl::hasGenerator(device_type)) {
self->cdata = at::Generator(c10::impl::makeGenerator(device));
} else {
throw std::runtime_error("No generator available for device type: " +
c10::toString(device_type));
Copy link
Collaborator

Choose a reason for hiding this comment

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

So, wo do not need to modify this one, all the accelerators except CPU will follow the same AcceleratorHooksInterface logic.

@soulitzer soulitzer added the triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module label Aug 26, 2025
@iwknow
Copy link
Author

iwknow commented Aug 26, 2025

@EikanWang Thanks for the mention.

@iwknow PyTorch supports registering generators for out-of-tree backends by inheriting from the AcceleratorHooksInterface class. You can refer to the below link for more information.

Therefore, it seems to me that you might need to add new files named XLAHooksInterface.h and XLAHooksInterface.cpp to internally implement XLAHooksInterface in PyTorch Repo, and then create new XLAHooks.h and XLAHooks.cpp files in the XLA Repo that internally inherit from XLAHooksInterface implements the XLAHooks custom class and registers it in PyTorch.

This is actually a brilliant idea, i will take a closer look.

@iwknow
Copy link
Author

iwknow commented Aug 30, 2025

Hi @FFFrog
I've updated the PR based on your suggestion. please review. thank you very much!

Comment on lines 47 to 49
virtual DeviceIndex getNumDevices() const {
return 0;
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why do we need this one? Can deviceCount from base class AcceleratorHooksInterface help?

Copy link
Author

Choose a reason for hiding this comment

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

good catch. i guess it is copy/paste from getNumGPUs XPUHooksInterface.h. removed to use DeviceIndex deviceCount from AcceleratorHooksInterface

@FFFrog
Copy link
Collaborator

FFFrog commented Aug 30, 2025

It would be better if we change the code below into return detail::getXLAHooks().hasXLA(); as well.

static bool hasXLA() {
return c10::impl::hasDeviceGuardImpl(c10::DeviceType::XLA);

@iwknow
Copy link
Author

iwknow commented Aug 31, 2025

@FFFrog Thanks for reviewing. PR updated.

@FFFrog FFFrog added the topic: not user facing topic category label Sep 1, 2025
Copy link
Collaborator

@FFFrog FFFrog left a comment

Choose a reason for hiding this comment

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

Thanks, LGTM, let's trigger CI first.

@iwknow, I don't have valid approval permissions, so we'll have to get approval from @albanD.

Hey, @albanD, please take a look at this, thanks.

Also, it's necessary to find a better way to implement Hooks registration, otherwise every time we add a new backend to PyTorch, we'll need to add a Hooks interface for it.

@iwknow
Copy link
Author

iwknow commented Sep 3, 2025

@albanD, a kindly ping.

@iwknow
Copy link
Author

iwknow commented Sep 9, 2025

Hi @FFFrog, can you please nominate another approver as it seems that albanD is unresponsive.

@FFFrog
Copy link
Collaborator

FFFrog commented Sep 10, 2025

Hi @FFFrog, can you please nominate another approver as it seems that albanD is unresponsive.

Sure, Hey @albanD @ezyang @malfet, could you please help to take a look at this one? Thank you.

Copy link
Collaborator

@albanD albanD left a comment

Choose a reason for hiding this comment

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

That sounds ok to add to make pytorch/XLA work. Do you have a sister PR on the xla side to make it work we should look at at the same time?
Also I would like one of the pytorch/XLA maintainer to comment here to make sure this is good on their end.

@FFFrog xla is a bit of a weird backend for historical reason. We don't plan on adding any new other backend that is not going through PrivateUse1 going forward indeed.

@iwknow
Copy link
Author

iwknow commented Sep 11, 2025

That sounds ok to add to make pytorch/XLA work. Do you have a sister PR on the xla side to make it work we should look at at the same time? Also I would like one of the pytorch/XLA maintainer to comment here to make sure this is good on their end.

@FFFrog xla is a bit of a weird backend for historical reason. We don't plan on adding any new other backend that is not going through PrivateUse1 going forward indeed.

Sister PR for xla: yes, i do have the code change for XLA. it's basically a concrete implementation of the interface. but it will be in pytorch/XLA package. would you like to review?

qihqi has been reviewing the generator change. I think he can take a look at this change as well. qihqi, please leave a comment if this change looks good to you and is what torch/XLA team want.

@FFFrog
Copy link
Collaborator

FFFrog commented Sep 12, 2025

@FFFrog xla is a bit of a weird backend for historical reason. We don't plan on adding any new other backend that is not going through PrivateUse1 going forward indeed.

Thank you, I got it.

@albanD albanD changed the title Enable torch.Generator to support off-tree generator implementation Enable torch.Generator to support pytorch/xla generator implementation Sep 12, 2025
@iwknow
Copy link
Author

iwknow commented Sep 16, 2025

@zhanyong-wan can you please take a look or nominate someone from XLA side to see if this change is good from the XLA side. thanks!

@iwknow
Copy link
Author

iwknow commented Sep 25, 2025

@qihqi ping again.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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
None yet
Development

Successfully merging this pull request may close these issues.

6 participants