Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit bcc0b92

Browse files
author
array-api-bot
committed
Deploy: c91c82c
1 parent d107111 commit bcc0b92

20 files changed

+171
-112
lines changed

draft/API_specification/array_object.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -727,7 +727,7 @@ <h3 id="comparison-operators">Comparison Operators<a class="headerlink" href="#c
727727
<h3 id="in-place-operators">In-place Operators<a class="headerlink" href="#in-place-operators" title="Link to this heading"></a></h3>
728728
<div class="admonition note">
729729
<p class="admonition-title">Note</p>
730-
<p>In-place operations must be supported as discussed in <a class="reference internal" href="../design_topics/copies_views_and_mutation.html#copyview-mutability"><span class="std std-ref">Copy-view behaviour and mutability</span></a>.</p>
730+
<p>In-place operations must be supported as discussed in <a class="reference internal" href="../design_topics/copies_views_and_mutation.html#copyview-mutability"><span class="std std-ref">Copy-view behavior and mutability</span></a>.</p>
731731
</div>
732732
<p>A conforming implementation of the array API standard must provide and support an array object supporting the following “in-place” Python operators.</p>
733733
<div class="admonition note">

draft/API_specification/generated/array_api.array.__dlpack__.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -527,7 +527,7 @@ <h1 id="api-specification-generated-array-api-array-dlpack--page-root">__dlpack_
527527
API standard.</p>
528528
</p></li>
529529
<li><p><strong>copy</strong> (<em>Optional</em><em>[</em><em>bool</em><em>]</em>) – <p>boolean indicating whether or not to copy the input. If <code class="docutils literal notranslate"><span class="pre">True</span></code>, the
530-
function must always copy (performed by the producer). If <code class="docutils literal notranslate"><span class="pre">False</span></code>, the
530+
function must always copy (performed by the producer; see also <a class="reference internal" href="../../design_topics/copies_views_and_mutation.html#copy-keyword-argument"><span class="std std-ref">Copy keyword argument behavior</span></a>). If <code class="docutils literal notranslate"><span class="pre">False</span></code>, the
531531
function must never copy, and raise a <code class="docutils literal notranslate"><span class="pre">BufferError</span></code> in case a copy is
532532
deemed necessary (e.g. if a cross-device data movement is requested, and
533533
it is not possible without a copy). If <code class="docutils literal notranslate"><span class="pre">None</span></code>, the function must reuse

draft/API_specification/generated/array_api.asarray.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -493,7 +493,7 @@ <h1 id="api-specification-generated-array-api-asarray--page-root">asarray<a clas
493493
</div>
494494
</p></li>
495495
<li><p><strong>device</strong> (<em>Optional</em><em>[</em><em>device</em><em>]</em>) – device on which to place the created array. If <code class="docutils literal notranslate"><span class="pre">device</span></code> is <code class="docutils literal notranslate"><span class="pre">None</span></code> and <code class="docutils literal notranslate"><span class="pre">obj</span></code> is an array, the output array device must be inferred from <code class="docutils literal notranslate"><span class="pre">obj</span></code>. Default: <code class="docutils literal notranslate"><span class="pre">None</span></code>.</p></li>
496-
<li><p><strong>copy</strong> (<em>Optional</em><em>[</em><em>bool</em><em>]</em>) – boolean indicating whether or not to copy the input. If <code class="docutils literal notranslate"><span class="pre">True</span></code>, the function must always copy. If <code class="docutils literal notranslate"><span class="pre">False</span></code>, the function must never copy for input which supports the buffer protocol and must raise a <code class="docutils literal notranslate"><span class="pre">ValueError</span></code> in case a copy would be necessary. If <code class="docutils literal notranslate"><span class="pre">None</span></code>, the function must reuse existing memory buffer if possible and copy otherwise. Default: <code class="docutils literal notranslate"><span class="pre">None</span></code>.</p></li>
496+
<li><p><strong>copy</strong> (<em>Optional</em><em>[</em><em>bool</em><em>]</em>) – boolean indicating whether or not to copy the input. If <code class="docutils literal notranslate"><span class="pre">True</span></code>, the function must always copy (see <a class="reference internal" href="../../design_topics/copies_views_and_mutation.html#copy-keyword-argument"><span class="std std-ref">Copy keyword argument behavior</span></a>). If <code class="docutils literal notranslate"><span class="pre">False</span></code>, the function must never copy for input which supports the buffer protocol and must raise a <code class="docutils literal notranslate"><span class="pre">ValueError</span></code> in case a copy would be necessary. If <code class="docutils literal notranslate"><span class="pre">None</span></code>, the function must reuse existing memory buffer if possible and copy otherwise. Default: <code class="docutils literal notranslate"><span class="pre">None</span></code>.</p></li>
497497
</ul>
498498
</dd>
499499
<dt class="field-even">Returns<span class="colon">:</span></dt>

draft/API_specification/generated/array_api.astype.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,7 @@ <h1 id="api-specification-generated-array-api-astype--page-root">astype<a class=
490490
<dd class="field-odd"><ul class="simple">
491491
<li><p><strong>x</strong> (<em>array</em>) – array to cast.</p></li>
492492
<li><p><strong>dtype</strong> (<em>dtype</em>) – desired data type.</p></li>
493-
<li><p><strong>copy</strong> (<em>bool</em>) – specifies whether to copy an array when the specified <code class="docutils literal notranslate"><span class="pre">dtype</span></code> matches the data type of the input array <code class="docutils literal notranslate"><span class="pre">x</span></code>. If <code class="docutils literal notranslate"><span class="pre">True</span></code>, a newly allocated array must always be returned. If <code class="docutils literal notranslate"><span class="pre">False</span></code> and the specified <code class="docutils literal notranslate"><span class="pre">dtype</span></code> matches the data type of the input array, the input array must be returned; otherwise, a newly allocated array must be returned. Default: <code class="docutils literal notranslate"><span class="pre">True</span></code>.</p></li>
493+
<li><p><strong>copy</strong> (<em>bool</em>) – specifies whether to copy an array when the specified <code class="docutils literal notranslate"><span class="pre">dtype</span></code> matches the data type of the input array <code class="docutils literal notranslate"><span class="pre">x</span></code>. If <code class="docutils literal notranslate"><span class="pre">True</span></code>, a newly allocated array must always be returned (see <a class="reference internal" href="../../design_topics/copies_views_and_mutation.html#copy-keyword-argument"><span class="std std-ref">Copy keyword argument behavior</span></a>). If <code class="docutils literal notranslate"><span class="pre">False</span></code> and the specified <code class="docutils literal notranslate"><span class="pre">dtype</span></code> matches the data type of the input array, the input array must be returned; otherwise, a newly allocated array must be returned. Default: <code class="docutils literal notranslate"><span class="pre">True</span></code>.</p></li>
494494
<li><p><strong>device</strong> (<em>Optional</em><em>[</em><em>device</em><em>]</em>) – device on which to place the returned array. If <code class="docutils literal notranslate"><span class="pre">device</span></code> is <code class="docutils literal notranslate"><span class="pre">None</span></code>, the output array device must be inferred from <code class="docutils literal notranslate"><span class="pre">x</span></code>. Default: <code class="docutils literal notranslate"><span class="pre">None</span></code>.</p></li>
495495
</ul>
496496
</dd>

draft/API_specification/generated/array_api.reshape.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -471,7 +471,7 @@ <h1 id="api-specification-generated-array-api-reshape--page-root">reshape<a clas
471471
<dd class="field-odd"><ul class="simple">
472472
<li><p><strong>x</strong> (<em>array</em>) – input array to reshape.</p></li>
473473
<li><p><strong>shape</strong> (<em>Tuple</em><em>[</em><em>int</em><em>, </em><em>...</em><em>]</em>) – a new shape compatible with the original shape. One shape dimension is allowed to be <code class="docutils literal notranslate"><span class="pre">-1</span></code>. When a shape dimension is <code class="docutils literal notranslate"><span class="pre">-1</span></code>, the corresponding output array shape dimension must be inferred from the length of the array and the remaining dimensions.</p></li>
474-
<li><p><strong>copy</strong> (<em>Optional</em><em>[</em><em>bool</em><em>]</em>) – whether or not to copy the input array. If <code class="docutils literal notranslate"><span class="pre">True</span></code>, the function must always copy. If <code class="docutils literal notranslate"><span class="pre">False</span></code>, the function must never copy. If <code class="docutils literal notranslate"><span class="pre">None</span></code>, the function must avoid copying, if possible, and may copy otherwise. Default: <code class="docutils literal notranslate"><span class="pre">None</span></code>.</p></li>
474+
<li><p><strong>copy</strong> (<em>Optional</em><em>[</em><em>bool</em><em>]</em>) – whether or not to copy the input array. If <code class="docutils literal notranslate"><span class="pre">True</span></code>, the function must always copy (see <a class="reference internal" href="../../design_topics/copies_views_and_mutation.html#copy-keyword-argument"><span class="std std-ref">Copy keyword argument behavior</span></a>). If <code class="docutils literal notranslate"><span class="pre">False</span></code>, the function must never copy. If <code class="docutils literal notranslate"><span class="pre">None</span></code>, the function must avoid copying, if possible, and may copy otherwise. Default: <code class="docutils literal notranslate"><span class="pre">None</span></code>.</p></li>
475475
</ul>
476476
</dd>
477477
<dt class="field-even">Returns<span class="colon">:</span></dt>
Lines changed: 67 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
.. _copyview-mutability:
22

3-
Copy-view behaviour and mutability
3+
Copy-view behavior and mutability
44
==================================
55

66
.. admonition:: Mutating views
@@ -10,68 +10,93 @@ Copy-view behaviour and mutability
1010

1111
Strided array implementations (e.g. NumPy, PyTorch, CuPy, MXNet) typically
1212
have the concept of a "view", meaning an array containing data in memory that
13-
belongs to another array (i.e. a different "view" on the original data).
14-
Views are useful for performance reasons - not copying data to a new location
15-
saves memory and is faster than copying - but can also affect the semantics
13+
belongs to another array (i.e., a different "view" on the original data).
14+
Views are useful for performance reasonsnot copying data to a new location
15+
saves memory and is faster than copyingbut can also affect the semantics
1616
of code. This happens when views are combined with *mutating* operations.
17-
This simple example illustrates that:
17+
The following example is illustrative:
1818

1919
.. code-block:: python
2020
2121
x = ones(1)
2222
y = x[:] # `y` *may* be a view on the data of `x`
2323
y -= 1 # if `y` is a view, this modifies `x`
2424
25-
Code as simple as the above example will not be portable between array
26-
libraries - for NumPy/PyTorch/CuPy/MXNet ``x`` will contain the value ``0``,
27-
while for TensorFlow/JAX/Dask it will contain the value ``1``. The combination
28-
of views and mutability is fundamentally problematic here if the goal is to
29-
be able to write code with unambiguous semantics.
25+
Code similar to the above example will not be portable between array
26+
libraries. For example, for NumPy, PyTorch, and CuPy, ``x`` will contain the value ``0``,
27+
while, for TensorFlow, JAX, and Dask, ``x`` will contain the value ``1``. In
28+
this case, the combination of views and mutability is fundamentally problematic
29+
if the goal is to be able to write code with unambiguous semantics.
3030

3131
Views are necessary for getting good performance out of the current strided
32-
array libraries. It is not always clear however when a library will return a
33-
view, and when it will return a copy. This API standard does not attempt to
34-
specify this - libraries can do either.
32+
array libraries. It is not always clear, however, when a library will return a
33+
view and when it will return a copy. This standard does not attempt to
34+
specify thislibraries may do either.
3535

36-
There are several types of operations that do in-place mutation of data
37-
contained in arrays. These include:
36+
There are several types of operations that may perform in-place mutation of
37+
array data. These include:
3838

39-
1. Inplace operators (e.g. ``*=``)
39+
1. In-place operators (e.g. ``*=``)
4040
2. Item assignment (e.g. ``x[0] = 1``)
4141
3. Slice assignment (e.g., ``x[:2, :] = 3``)
4242
4. The `out=` keyword present in some strided array libraries (e.g. ``sin(x, out=y)``)
4343

44-
Libraries like TensorFlow and JAX tend to support inplace operators, provide
44+
Libraries such as TensorFlow and JAX tend to support in-place operators by providing
4545
alternative syntax for item and slice assignment (e.g. an ``update_index``
46-
function or ``x.at[idx].set(y)``), and have no need for ``out=``.
46+
function or ``x.at[idx].set(y)``) and have no need for ``out=``.
4747

48-
A potential solution could be to make views read-only, or use copy-on-write
49-
semantics. Both are hard to implement and would present significant issues
50-
for backwards compatibility for current strided array libraries. Read-only
51-
views would also not be a full solution, given that mutating the original
52-
(base) array will also result in ambiguous semantics. Hence this API standard
53-
does not attempt to go down this route.
48+
A potential solution could be to make views read-only or implement copy-on-write
49+
semantics. Both are hard to implement and would present significant backward
50+
compatibility issues for current strided array libraries. Read-only
51+
views would also not be a full solution due to the fact that mutating the original
52+
(base) array will also result in ambiguous semantics. Accordingly, this standard
53+
does not attempt to pursue this solution.
5454

55-
Both inplace operators and item/slice assignment can be mapped onto
55+
Both in-place operators and item/slice assignment can be mapped onto
5656
equivalent functional expressions (e.g. ``x[idx] = val`` maps to
57-
``x.at[idx].set(val)``), and given that both inplace operators and item/slice
57+
``x.at[idx].set(val)``), and, given that both in-place operators and item/slice
5858
assignment are very widely used in both library and end user code, this
5959
standard chooses to include them.
6060

61-
The situation with ``out=`` is slightly different - it's less heavily used, and
62-
easier to avoid. It's also not an optimal API, because it mixes an
61+
The situation with ``out=`` is slightly differentit's less heavily used, and
62+
easier to avoid. It's also not an optimal API because it mixes an
6363
"efficiency of implementation" consideration ("you're allowed to do this
64-
inplace") with the semantics of a function ("the output _must_ be placed into
65-
this array). There are libraries that do some form of tracing or abstract
66-
interpretation over a language that does not support mutation (to make
67-
analysis easier); in those cases implementing ``out=`` with correct handling of
68-
views may even be impossible to do. There's alternatives, for example the
69-
donated arguments in JAX or working buffers in LAPACK, that allow the user to
70-
express "you _may_ overwrite this data, do whatever is fastest". Given that
71-
those alternatives aren't widely used in array libraries today, this API
72-
standard chooses to (a) leave out ``out=``, and (b) not specify another method
73-
of reusing arrays that are no longer needed as buffers.
74-
75-
This leaves the problem of the initial example - with this API standard it
76-
remains possible to write code that will not work the same for all array
77-
libraries. This is something that the user must be careful about.
64+
in-place") with the semantics of a function ("the output _must_ be placed into
65+
this array"). There are libraries that do some form of tracing or abstract
66+
interpretation over a vocabulary that does not support mutation (to make
67+
analysis easier). In those cases implementing ``out=`` with correct handling of
68+
views may even be impossible to do.
69+
70+
There are alternatives. For example, the concept of donated arguments in JAX or
71+
working buffers in LAPACK which allow the user to express "you _may_ overwrite
72+
this data; do whatever is fastest". Given that those alternatives aren't widely
73+
used in array libraries today, this standard chooses to (a) leave out ``out=``,
74+
and (b) not specify another method of reusing arrays that are no longer needed
75+
as buffers.
76+
77+
This leaves the problem of the initial example—despite the best efforts of this
78+
standard, it remains possible to write code that will not work the same for all
79+
array libraries. This is something that the users are advised to best keep in
80+
mind and to reason carefully about the potential ambiguity of implemented code.
81+
82+
83+
.. _copy-keyword-argument:
84+
85+
Copy keyword argument behavior
86+
------------------------------
87+
88+
Several APIs in this standard support a ``copy`` keyword argument (e.g.,
89+
``asarray``, ``astype``, ``reshape``, and ``__dlpack__``). Typically, when a
90+
user sets ``copy=True``, the user does so in order to ensure that they are free
91+
to mutate the returned array without side-effects—namely, without mutating other
92+
views on the original (base) array. Accordingly, when ``copy=True``, unless an
93+
array library can guarantee that an array can be mutated without side-effects,
94+
conforming libraries are recommended to always perform a physical copy of the
95+
underlying array data.
96+
97+
.. note::
98+
Typically, in order to provide such a guarantee, libraries must perform
99+
whole-program analysis.
100+
101+
Conversely, consumers of this standard should expect that, if they set
102+
``copy=True``, they are free to use in-place operations on a returned array.

draft/design_topics/C_API.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@
225225
<li class="md-nav__item">
226226

227227

228-
<a href="copies_views_and_mutation.html" class="md-nav__link">Copy-view behaviour and mutability</a>
228+
<a href="copies_views_and_mutation.html" class="md-nav__link">Copy-view behavior and mutability</a>
229229

230230

231231
</li>

draft/design_topics/accuracy.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@
225225
<li class="md-nav__item">
226226

227227

228-
<a href="copies_views_and_mutation.html" class="md-nav__link">Copy-view behaviour and mutability</a>
228+
<a href="copies_views_and_mutation.html" class="md-nav__link">Copy-view behavior and mutability</a>
229229

230230

231231
</li>

draft/design_topics/complex_numbers.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@
226226
<li class="md-nav__item">
227227

228228

229-
<a href="copies_views_and_mutation.html" class="md-nav__link">Copy-view behaviour and mutability</a>
229+
<a href="copies_views_and_mutation.html" class="md-nav__link">Copy-view behavior and mutability</a>
230230

231231

232232
</li>

0 commit comments

Comments
 (0)