1
- (copyview -mutability)=
1
+ .. _ copyview -mutability:
2
2
3
- # Copy-view behaviour and mutability
3
+ Copy-view behaviour and mutability
4
+ ==================================
4
5
5
6
Strided array implementations (e.g. NumPy, PyTorch, CuPy, MXNet) typically
6
7
have the concept of a "view", meaning an array containing data in memory that
7
8
belongs to another array (i.e. a different "view" on the original data).
8
9
Views are useful for performance reasons - not copying data to a new location
9
10
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.
11
12
This simple example illustrates that:
12
13
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 `
18
19
19
20
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
22
23
of views and mutability is fundamentally problematic here if the goal is to
23
24
be able to write code with unambiguous semantics.
24
25
@@ -30,14 +31,14 @@ specify this - libraries can do either.
30
31
There are several types of operations that do in-place mutation of data
31
32
contained in arrays. These include:
32
33
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) `` )
37
38
38
39
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= ` `.
41
42
42
43
A potential solution could be to make views read-only, or use copy-on-write
43
44
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
47
48
does not attempt to go down this route.
48
49
49
50
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
52
53
assignment are very widely used in both library and end user code, this
53
54
standard chooses to include them.
54
55
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
56
57
easier to avoid. It's also not an optimal API, because it mixes an
57
58
"efficiency of implementation" consideration ("you're allowed to do this
58
59
inplace") with the semantics of a function ("the output _must_ be placed into
59
60
this array). There are libraries that do some form of tracing or abstract
60
61
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
62
63
views may even be impossible to do. There's alternatives, for example the
63
64
donated arguments in JAX or working buffers in LAPACK, that allow the user to
64
65
express "you _may_ overwrite this data, do whatever is fastest". Given that
65
66
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
67
68
of reusing arrays that are no longer needed as buffers.
68
69
69
70
This leaves the problem of the initial example - with this API standard it
70
71
remains possible to write code that will not work the same for all array
71
72
libraries. This is something that the user must be careful about.
72
73
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