Python/C++ API rules for device-generic APIs

We want to follow the following rule for any new API being added (and are going to migrate existing API to follow this as well):

  1. Any API that related to device property should be done as a Device-agnostic API (all devices if possible, else accelerator) unless there is a clear reason on why it does not apply to other devices.
  2. Device-agnostic APIs that apply to all devices should be placed under the torch namespace and accept a device_type optional argument (with default value being the current accelerator or cpu).
  3. Device-agnostic APIs for accelerators style device (torch — PyTorch main documentation) should be placed under the torch.acc namespace and not accept a device_type argument (they are always applied on the current accelerator).
  4. Device-specific APIs should be placed under device-specific submodules (torch.cuda, torch.xpu, torch.mps, etc).

Please comment here if there is any concern/feedback about this new rule.

For more details on the discussions that lead to this proposal, see [RFC] A device-agnostic Python runtime API design for stream-based accelerators · Issue #128403 · pytorch/pytorch · GitHub , partial list of related issues in both core ([RFC] A device-agnostic Python device memory related API design for stream-based accelerators · Issue #134978 · pytorch/pytorch · GitHub, etc) and ao (Enable Intel GPU by dbyoung18 · Pull Request #753 · pytorch/ao · GitHub) as well as early investigative implementation (Refine the logic of device construction when only device index is given by guangyey · Pull Request #129119 · pytorch/pytorch · GitHub, Stream/Event API, Update pin memory related APIs to not pass 'device' argument by wizzniu · Pull Request #131858 · pytorch/pytorch · GitHub, etc).

The credit for driving this discussion goes to @guangyey and other people involved on the issues listed above.

6 Likes

Thanks for the rules to ensure we are on the same page. It will help us move fast to implement more device-agnostic API PRs :v:

1 Like