You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/src/rule_author/superpowers/mutation_support.md
+16-7Lines changed: 16 additions & 7 deletions
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,6 @@
1
1
# Mutation Support
2
2
3
-
ChainRulesCore.jl offers experimental support for mutation, targetting use in forward mode AD.
3
+
ChainRulesCore.jl offers experimental support for mutation, targeting use in forward mode AD.
4
4
(Mutation support in reverse mode AD is more complicated and will likely require more changes to the interface)
5
5
6
6
!!! warning "Experimental"
@@ -17,18 +17,23 @@ It is required to be a structural tangent, having one tangent for each field of
17
17
Technically, not all `mutable struct`s need to use `MutableTangent` to represent their tangents.
18
18
Just like not all `struct`s need to use `Tangent`s.
19
19
Common examples away from this are natural tangent types like for arrays.
20
-
However, if one is setting up to use a custom tangent type for this it is surficiently off the beated path that we can not provide much guidance.
20
+
However, if one is setting up to use a custom tangent type for this it is sufficiently off the beaten path that we can not provide much guidance.
21
21
22
22
## `zero_tangent`
23
23
24
24
The [`zero_tangent`](@ref) function functions to give you a zero (i.e. additive identity) for any primal value.
25
25
The [`ZeroTangent`](@ref) type also does this.
26
-
The difference is that [`zero_tangent`](@ref) is (where possible) a full structural tangent mirroring the structure of the primal.
26
+
The difference is that [`zero_tangent`](@ref) is in general full structural tangent mirroring the structure of the primal.
27
+
To be technical the promise of [`zero_tangent`](@ref) is that it will be a value that supports mutation.
28
+
However, in practice[^1] this is achieved through in a structural tangent
27
29
For mutation support this is important, since it means that there is mutable memory available in the tangent to be mutated when the primal changes.
28
30
To support this you thus need to make sure your zeros are created in various places with [`zero_tangent`](@ref) rather than []`ZeroTangent`](@ref).
29
31
30
-
It is also useful for reasons of type stability, since it is always a structural tangent.
31
-
For this reason AD system implementors might chose to use this to create the tangent for all literal values they encounter, mutable or not.
32
+
33
+
34
+
It is also useful for reasons of type stability, since it forces a consistent type (generally a structural tangent) for any given primal type.
35
+
For this reason AD system implementors might chose to use this to create the tangent for all literal values they encounter, mutable or not,
36
+
and to process the output of `frule`s to convert [`ZeroTangent`](@ref) into corresponding [`zero_tangent`](@ref)s.
32
37
33
38
## Writing a frule for a mutating function
34
39
It is relatively straight forward to write a frule for a mutating function.
@@ -41,7 +46,7 @@ There are a few key points to follow:
41
46
### Example
42
47
For example, consider the primal function with:
43
48
1. takes two `Ref`s
44
-
2. doubles the first one inplace
49
+
2. doubles the first one in place
45
50
3. overwrites the second one's value with the literal 5.0
46
51
4. returns the first one
47
52
@@ -70,4 +75,8 @@ function ChainRulesCore.frule((ȧ, ḃ), ::typeof(foo!), a::Base.RefValue, b::B
70
75
end
71
76
```
72
77
73
-
Then assuming the AD system does its part to makes sure you are indeed given mutable values to mutate (i.e. those `@assert`ions are true) then all is well and this rule will make mutation correct.
78
+
Then assuming the AD system does its part to makes sure you are indeed given mutable values to mutate (i.e. those `@assert`ions are true) then all is well and this rule will make mutation correct.
79
+
80
+
[^1]:
81
+
Further, it is hard to achieve this promise of allowing mutation to be supported without returning a structural tangent.
82
+
Except in the special case of where the struct is not mutable and has no nested fields that are mutable.
0 commit comments