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

**URL:** <https://dev-discuss.pytorch.org/t/python-c-api-rules-for-device-generic-apis/2511>\
**Category:** frontend API\
**Created:** [October 3, 2024, 3:06pm UTC](https://dev-discuss.pytorch.org/t/python-c-api-rules-for-device-generic-apis/2511 "2024-10-03T15:06:40Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![albanD](https://yyz2.discourse-cdn.com/flex036/user_avatar/dev-discuss.pytorch.org/alband/32/9_2.png) [@albanD](https://dev-discuss.pytorch.org/u/albanD)\
**Post date:** [October 3, 2024, 3:06pm UTC](https://dev-discuss.pytorch.org/t/python-c-api-rules-for-device-generic-apis/2511/1 "2024-10-03T15:06:40Z")

</div>

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](https://pytorch.org/docs/main/torch.html#accelerators)) 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](https://github.com/pytorch/pytorch/issues/128403) , 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](https://github.com/pytorch/pytorch/issues/134978), etc) and ao ([Enable Intel GPU by dbyoung18 · Pull Request #753 · pytorch/ao · GitHub](https://github.com/pytorch/ao/pull/753)) 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](https://github.com/pytorch/pytorch/pull/129119), Stream/Event API, [Update pin memory related APIs to not pass 'device' argument by wizzniu · Pull Request #131858 · pytorch/pytorch · GitHub](https://github.com/pytorch/pytorch/pull/131858), etc).

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

---

<div class="post-metadata">

**Author:** ![EikanWang](https://yyz2.discourse-cdn.com/flex036/user_avatar/dev-discuss.pytorch.org/eikanwang/32/992_2.png) [@EikanWang](https://dev-discuss.pytorch.org/u/EikanWang)\
**Post date:** [October 8, 2024, 1:18pm UTC](https://dev-discuss.pytorch.org/t/python-c-api-rules-for-device-generic-apis/2511/2 "2024-10-08T13:18:30Z")

</div>

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 ✌

---

<div class="post-metadata">

**Author:** ![shink](https://yyz2.discourse-cdn.com/flex036/user_avatar/dev-discuss.pytorch.org/shink/32/1970_2.png) [@shink](https://dev-discuss.pytorch.org/u/shink)\
**Post date:** [January 23, 2025, 8:51am UTC](https://dev-discuss.pytorch.org/t/python-c-api-rules-for-device-generic-apis/2511/3 "2025-01-23T08:51:09Z")

</div>

Hey thanks for the rules! I noticed these PRs are almost landed so is there still some work to do? If so, I’d love to do it!
