1- (copyview -mutability)=
1+ .. _ copyview -mutability:
22
3- # Copy-view behaviour and mutability
3+ Copy-view behaviour and mutability
4+ ==================================
45
56Strided array implementations (e.g. NumPy, PyTorch, CuPy, MXNet) typically
67have the concept of a "view", meaning an array containing data in memory that
78belongs to another array (i.e. a different "view" on the original data).
89Views are useful for performance reasons - not copying data to a new location
910saves 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.
1112This 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
2223of views and mutability is fundamentally problematic here if the goal is to
2324be able to write code with unambiguous semantics.
2425
@@ -30,14 +31,14 @@ specify this - libraries can do either.
3031There are several types of operations that do in-place mutation of data
3132contained 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
3839Libraries 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
4243A potential solution could be to make views read-only, or use copy-on-write
4344semantics. 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
4748does not attempt to go down this route.
4849
4950Both 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
5253assignment are very widely used in both library and end user code, this
5354standard 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
5657easier to avoid. It's also not an optimal API, because it mixes an
5758"efficiency of implementation" consideration ("you're allowed to do this
5859inplace") with the semantics of a function ("the output _must_ be placed into
5960this array). There are libraries that do some form of tracing or abstract
6061interpretation 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
6263views may even be impossible to do. There's alternatives, for example the
6364donated arguments in JAX or working buffers in LAPACK, that allow the user to
6465express "you _may_ overwrite this data, do whatever is fastest". Given that
6566those 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
6768of reusing arrays that are no longer needed as buffers.
6869
6970This leaves the problem of the initial example - with this API standard it
7071remains possible to write code that will not work the same for all array
7172libraries. 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