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

Skip to content

Commit b982b98

Browse files
authored
Transform copies & mutability md to rst (#376)
1 parent d4b7e9a commit b982b98

File tree

1 file changed

+24
-25
lines changed

1 file changed

+24
-25
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,25 @@
1-
(copyview-mutability)=
1+
.. _copyview-mutability:
22

3-
# Copy-view behaviour and mutability
3+
Copy-view behaviour and mutability
4+
==================================
45

56
Strided array implementations (e.g. NumPy, PyTorch, CuPy, MXNet) typically
67
have the concept of a "view", meaning an array containing data in memory that
78
belongs to another array (i.e. a different "view" on the original data).
89
Views are useful for performance reasons - not copying data to a new location
910
saves memory and is faster than copying - but can also affect the semantics
10-
of code. This happens when views are combined with _mutating_ operations.
11+
of code. This happens when views are combined with *mutating* operations.
1112
This simple example illustrates that:
1213

13-
```python
14-
x = ones(1)
15-
y = x[:] # `y` *may* be a view on the data of `x`
16-
y -= 1 # if `y` is a view, this modifies `x`
17-
```
14+
.. code-block:: python
15+
16+
x = ones(1)
17+
y = x[:] # `y` *may* be a view on the data of `x`
18+
y -= 1 # if `y` is a view, this modifies `x`
1819
1920
Code as simple as the above example will not be portable between array
20-
libraries - for NumPy/PyTorch/CuPy/MXNet `x` will contain the value `0`,
21-
while for TensorFlow/JAX/Dask it will contain the value `1`. The combination
21+
libraries - for NumPy/PyTorch/CuPy/MXNet ``x`` will contain the value ``0``,
22+
while for TensorFlow/JAX/Dask it will contain the value ``1``. The combination
2223
of views and mutability is fundamentally problematic here if the goal is to
2324
be able to write code with unambiguous semantics.
2425

@@ -30,14 +31,14 @@ specify this - libraries can do either.
3031
There are several types of operations that do in-place mutation of data
3132
contained in arrays. These include:
3233

33-
1. Inplace operators (e.g. `*=`)
34-
2. Item assignment (e.g. `x[0] = 1`)
35-
3. Slice assignment (e.g., `x[:2, :] = 3`)
36-
4. The `out=` keyword present in some strided array libraries (e.g. `sin(x, out=y`))
34+
1. Inplace operators (e.g. ``*=``)
35+
2. Item assignment (e.g. ``x[0] = 1``)
36+
3. Slice assignment (e.g., ``x[:2, :] = 3``)
37+
4. The `out=` keyword present in some strided array libraries (e.g. ``sin(x, out=y)``)
3738

3839
Libraries like TensorFlow and JAX tend to support inplace operators, provide
39-
alternative syntax for item and slice assignment (e.g. an `update_index`
40-
function or `x.at[idx].set(y)`), and have no need for `out=`.
40+
alternative syntax for item and slice assignment (e.g. an ``update_index``
41+
function or ``x.at[idx].set(y)``), and have no need for ``out=``.
4142

4243
A potential solution could be to make views read-only, or use copy-on-write
4344
semantics. Both are hard to implement and would present significant issues
@@ -47,30 +48,28 @@ views would also not be a full solution, given that mutating the original
4748
does not attempt to go down this route.
4849

4950
Both inplace operators and item/slice assignment can be mapped onto
50-
equivalent functional expressions (e.g. `x[idx] = val` maps to
51-
`x.at[idx].set(val)`), and given that both inplace operators and item/slice
51+
equivalent functional expressions (e.g. ``x[idx] = val`` maps to
52+
``x.at[idx].set(val)``), and given that both inplace operators and item/slice
5253
assignment are very widely used in both library and end user code, this
5354
standard chooses to include them.
5455

55-
The situation with `out=` is slightly different - it's less heavily used, and
56+
The situation with ``out=`` is slightly different - it's less heavily used, and
5657
easier to avoid. It's also not an optimal API, because it mixes an
5758
"efficiency of implementation" consideration ("you're allowed to do this
5859
inplace") with the semantics of a function ("the output _must_ be placed into
5960
this array). There are libraries that do some form of tracing or abstract
6061
interpretation over a language that does not support mutation (to make
61-
analysis easier); in those cases implementing `out=` with correct handling of
62+
analysis easier); in those cases implementing ``out=`` with correct handling of
6263
views may even be impossible to do. There's alternatives, for example the
6364
donated arguments in JAX or working buffers in LAPACK, that allow the user to
6465
express "you _may_ overwrite this data, do whatever is fastest". Given that
6566
those alternatives aren't widely used in array libraries today, this API
66-
standard chooses to (a) leave out `out=`, and (b) not specify another method
67+
standard chooses to (a) leave out ``out=``, and (b) not specify another method
6768
of reusing arrays that are no longer needed as buffers.
6869

6970
This leaves the problem of the initial example - with this API standard it
7071
remains possible to write code that will not work the same for all array
7172
libraries. This is something that the user must be careful about.
7273

73-
```{note}
74-
75-
It is recommended that users avoid any mutating operations when a view may be involved.
76-
```
74+
.. note::
75+
It is recommended that users avoid any mutating operations when a view may be involved.

0 commit comments

Comments
 (0)