Skip to content

Commit 8a36207

Browse files
authored
PR: Transform device support md to rst (#379)
* Transform device support md to rst * Update url
1 parent d23febb commit 8a36207

File tree

2 files changed

+111
-116
lines changed

2 files changed

+111
-116
lines changed

spec/design_topics/device_support.md

-116
This file was deleted.

spec/design_topics/device_support.rst

+111
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
.. _device-support:
2+
3+
Device support
4+
==============
5+
6+
For libraries that support execution on more than a single hardware device - e.g. CPU and GPU, or multiple GPUs - it is important to be able to control on which device newly created arrays get placed and where execution happens. Attempting to be fully implicit doesn't always scale well to situations with multiple GPUs.
7+
8+
Existing libraries employ one or more of these three methods to exert such control over data placement:
9+
10+
1. A global default device, which may be fixed or user-switchable.
11+
2. A context manager to control device assignment within its scope.
12+
3. Local control for data allocation target device via explicit keywords, and a method to transfer arrays to another device.
13+
14+
Libraries differ in how execution is controlled, via a context manager or with the convention that execution takes place on the same device where all argument arrays are allocated. And they may or may not allow mixing arrays on different devices via implicit data transfers.
15+
16+
This standard chooses to add support for method 3 (local control), with the convention that execution takes place on the same device where all argument arrays are allocated. The rationale for choosing method 3 is because it's the most explicit and granular, with its only downside being verbosity. A context manager may be added in the future - see :ref:`device-out-of-scope` for details.
17+
18+
Intended usage
19+
--------------
20+
21+
The intended usage for the device support in the current version of the
22+
standard is *device handling in library code*. The assumed pattern is that
23+
users create arrays (for which they can use all the relevant device syntax
24+
that the library they use provides), and that they then pass those arrays
25+
into library code which may have to do the following:
26+
27+
- Create new arrays on the same device as an array that's passed in.
28+
- Determine whether two input arrays are present on the same device or not.
29+
- Move an array from one device to another.
30+
- Create output arrays on the same device as the input arrays.
31+
- Pass on a specified device to other library code.
32+
33+
.. note::
34+
Given that there is not much that's currently common in terms of
35+
device-related syntax between different array libraries, the syntax included
36+
in the standard is kept as minimal as possible while enabling the
37+
above-listed use cases.
38+
39+
Syntax for device assignment
40+
----------------------------
41+
42+
The array API will offer the following syntax for device assignment and
43+
cross-device data transfer:
44+
45+
1. A ``.device`` property on the array object, which returns a ``Device`` object
46+
representing the device the data in the array is stored on, and supports
47+
comparing devices for equality with ``==`` and ``!=`` within the same library
48+
(e.g., by implementing ``__eq__``); comparing device objects from different
49+
libraries is out of scope).
50+
2. A ``device=None`` keyword for array creation functions, which takes an
51+
instance of a ``Device`` object.
52+
3. A ``.to_device`` method on the array object to copy an array to a different device.
53+
54+
.. note::
55+
In the current API standard, the only way to obtain a ``Device`` object is from the
56+
``.device`` property on the array object. The standard does **not** include a universal
57+
``Device`` object recognized by all compliant libraries. Accordingly, the standard does
58+
not provide a means of instantiating a ``Device`` object to point to a specific physical or
59+
logical device.
60+
61+
The choice to not include a standardized ``Device`` object may be revisited in a future revision of this standard.
62+
63+
For array libraries which concern themselves with multi-device support, including CPU and GPU,
64+
they are free to expose a library-specific device object (e.g., for creating an
65+
array on a particular device). While a library-specific device object can be used as input to
66+
``to_device``, beware that this will mean non-portability as code will be specific to
67+
that library.
68+
69+
Semantics
70+
---------
71+
72+
Handling devices is complex, and some frameworks have elaborate policies for
73+
handling device placement. Therefore this section only gives recommendations,
74+
rather than hard requirements:
75+
76+
- Respect explicit device assignment (i.e. if the input to the ``device=`` keyword is not ``None``, guarantee that the array is created on the given device, and raise an exception otherwise).
77+
- Preserve device assignment as much as possible (e.g. output arrays from a function are expected to be on the same device as input arrays to the function).
78+
- Raise an exception if an operation involves arrays on different devices (i.e. avoid implicit data transfer between devices).
79+
- Use a default for ``device=None`` which is consistent between functions within the same library.
80+
- If a library has multiple ways of controlling device placement, the most explicit method should have the highest priority. For example:
81+
82+
1. If ``device=`` keyword is specified, that always takes precedence
83+
84+
2. If ``device=None``, then use the setting from a context manager, if set.
85+
86+
3. If no context manager was used, then use the global default device/strategy
87+
88+
.. _device-out-of-scope:
89+
90+
Out of scope for device support
91+
-------------------------------
92+
93+
Individual libraries may offers APIs for one or more of the following topics,
94+
however those are out of scope for this standard:
95+
96+
- Identifying a specific physical or logical device across libraries
97+
- Setting a default device globally
98+
- Stream/queue control
99+
- Distributed allocation
100+
- Memory pinning
101+
- A context manager for device control
102+
103+
.. note::
104+
A context manager for controlling the default device is present in most existing array
105+
libraries (NumPy being the exception). There are concerns with using a
106+
context manager however. A context manager can be tricky to use at a high
107+
level, since it may affect library code below function calls (non-local
108+
effects). See, e.g., `this PyTorch issue <https://github.com/pytorch/pytorch/issues/27878>`_
109+
for a discussion on a good context manager API.
110+
111+
Adding a context manager may be considered in a future version of this API standard.

0 commit comments

Comments
 (0)