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/writing_good_rules.md
+13-13Lines changed: 13 additions & 13 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -44,39 +44,39 @@ Instead you probably want `transpose(a)`, unless you've already restricted `a` t
44
44
45
45
## Code Style
46
46
47
-
Use named local functions for the `pushforward`/`pullback`:
47
+
Use named local functions for the `pullback` in an `rrule`.
48
48
49
49
```julia
50
50
# good:
51
-
functionfrule(::typeof(foo), x)
51
+
functionrrule(::typeof(foo), x)
52
52
Y =foo(x)
53
-
functionfoo_pushforward(_, ẋ)
54
-
returnbar(ẋ)
53
+
functionfoo_pullback(x̄)
54
+
returnNO_FIELDS, bar(x̄)
55
55
end
56
-
return Y, foo_pushforward
56
+
return Y, foo_pullback
57
57
end
58
58
#== output
59
-
julia> frule(foo, 2)
60
-
(4, var"#foo_pushforward#11"())
59
+
julia> rrule(foo, 2)
60
+
(4, var"#foo_pullback#11"())
61
61
==#
62
62
63
63
# bad:
64
-
functionfrule(::typeof(foo), x)
65
-
returnfoo(x), (_, ẋ) ->bar(ẋ)
64
+
functionrrule(::typeof(foo), x)
65
+
returnfoo(x), x̄ ->(NO_FIELDS, bar(x̄))
66
66
end
67
67
#== output:
68
-
julia> frule(foo, 2)
68
+
julia> rrule(foo, 2)
69
69
(4, var"##9#10"())
70
70
==#
71
71
```
72
72
73
-
While this is more verbose, it ensures that if an error is thrown during the `pullback`/`pushforward` the [`gensym`](https://docs.julialang.org/en/v1/base/base/#Base.gensym) name of the local function will include the name you gave it.
73
+
While this is more verbose, it ensures that if an error is thrown during the `pullback` the [`gensym`](https://docs.julialang.org/en/v1/base/base/#Base.gensym) name of the local function will include the name you gave it.
74
74
This makes it a lot simpler to debug from the stacktrace.
75
75
76
76
## Write tests
77
77
78
-
There are fairly decent tools for writing tests based on [FiniteDifferences.jl](https://github.com/JuliaDiff/FiniteDifferences.jl).
79
-
They are in [ChainRulesTestUtils.jl](https://github.com/JuliaDiff/ChainRulesTestUtils.jl).
78
+
In [ChainRulesTestUtils.jl](https://github.com/JuliaDiff/ChainRulesTestUtils.jl)m
79
+
there are fairly decent tools for writing tests based on [FiniteDifferences.jl](https://github.com/JuliaDiff/FiniteDifferences.jl).
80
80
Take a look at existing [ChainRules.jl](https://github.com/JuliaDiff/ChainRules.jl) tests and you should see how to do stuff.
0 commit comments