Skip to content

Commit cb1aa6b

Browse files
authored
Merge pull request #640 from ablaom/docs-give-example-of-partial-self
Add illustration of `∂self` in the maths/propagators section
2 parents 26b7748 + b4664d0 commit cb1aa6b

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

docs/src/maths/propagators.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,25 @@ So every `pushforward` takes in an extra argument, which is ignored unless the o
179179
It is common to write `function foo_pushforward(_, Δargs...)` in the case when `foo` does not have fields.
180180
Similarly every `pullback` returns an extra `∂self`, which for things without fields is `NoTangent()`, indicating there are no fields within the function itself.
181181

182+
Here's an example showing how to define `∂self` in an `rrule` when the primal function has
183+
internal fields (implicit arguments):
184+
185+
```julia
186+
struct Multiplier{T}
187+
x::T
188+
end
189+
(m::Multiplier)(y) = m.x * y
190+
191+
function ChainRulesCore.rrule(m::Multiplier, y)
192+
product = m(y)
193+
function pullback(Δproduct)
194+
∂self = Tangent{Multiplier}(; x = Δproduct * y')
195+
∂y = m.x' * Δproduct
196+
return ∂self, ∂y
197+
end
198+
return product, pullback
199+
end
200+
```
182201

183202
### Pushforward / Pullback summary
184203

0 commit comments

Comments
 (0)