-
Notifications
You must be signed in to change notification settings - Fork 95
[WIP] Add Zygote repeat rrules #405
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
b4e5ae8
Add repeat rrules
0778cd3
No kwargs sensitivities (don't DoesNotExist)
779b8a3
Tests, fix kwargs(?)
e97e3c6
Use fkwargs
d4ba658
Version bump
a3c4f13
Update src/rulesets/Base/array.jl
e1e2843
Update src/rulesets/Base/array.jl
db385a6
Update src/rulesets/Base/array.jl
f9f1548
Update src/rulesets/Base/array.jl
bdb1eb9
Update src/rulesets/Base/array.jl
15b758a
add frules and rule for zero-dim
09c8f2e
1 error on fill(4.0) rrule
c4d38ee
Formatting
f55670d
More formatting
eabd344
Rm frules
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -103,3 +103,40 @@ function rrule(::typeof(fill), value::Any, dims::Int...) | |
| end | ||
| return fill(value, dims), fill_pullback | ||
| end | ||
|
|
||
| ##### | ||
| ##### `repeat` | ||
| ##### | ||
|
|
||
| function rrule(::typeof(repeat), x::AbstractVector, m::Integer) | ||
| function repeat_pullback(Ȳ) | ||
| return (NO_FIELDS, dropdims(sum(reshape(Ȳ, length(x), :); dims=2); dims=2), DoesNotExist()) | ||
| end | ||
| return repeat(x, m), repeat_pullback | ||
| end | ||
|
|
||
| function rrule(::typeof(repeat), x::AbstractVecOrMat, m::Integer, n::Integer=1) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since things like What this won't handle is |
||
| function repeat_pullback(Ȳ) | ||
| Ȳ′ = reshape(Ȳ, size(x, 1), m, size(x, 2), n) | ||
| return (NO_FIELDS, reshape(sum(Ȳ′; dims=(2,4)), size(x)), DoesNotExist(), DoesNotExist()) | ||
| end | ||
| return repeat(x, m, n), repeat_pullback | ||
| end | ||
|
|
||
| function rrule(::typeof(repeat), xs::AbstractArray; inner=ntuple(_->1, ndims(xs)), outer=ntuple(_->1, ndims(xs))) | ||
| function repeat_pullback(Ȳ) | ||
| Ȳ′ = zero(xs) | ||
| S = size(xs) | ||
| for (dest_idx, val) ∈ pairs(IndexCartesian(), Ȳ) | ||
| src_idx = [mod1(div(dest_idx[dim] - 1, inner[dim]) + 1, S[dim]) for dim ∈ 1:length(S)] | ||
| Ȳ′[src_idx...] += val | ||
| end | ||
| return (NO_FIELDS, Ȳ′) | ||
| end | ||
| return repeat(xs; inner=inner, outer=outer), repeat_pullback | ||
| end | ||
|
|
||
| function rrule(::typeof(repeat), x::AbstractArray{<:Real, 0}, m::Integer) | ||
| repeat_pullback(Ȳ) = (NO_FIELDS, similar(x, eltype(Ȳ)) .= sum(Ȳ), DoesNotExist()) | ||
| return repeat(x, m), repeat_pullback | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should this close over
length(x)rather thanx(to reduce memory requirements)?And likewise with the
sizecalls in the rules below.This is a genuine question, i'm not sure what best practice here is (cc @oxinabox).
I believe this would be manually doing one thing we want an
OpaqueClosureto be able to do automatically.Also maybe this should be mentioned on the "writing good rules docs"?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I am cautious about doing that since it makes code uglier.
So I wouldn't block a PR over it (it can always be optimized later)
But it does save memory.
We should discuss this with pros and cons in "Writing God Rules"