Skip to content

Commit

Permalink
Merge pull request #628 from JuliaDiff/mz/repeat
Browse files Browse the repository at this point in the history
Fix corner case for repeat
  • Loading branch information
mzgubic authored Jun 9, 2022
2 parents e4029df + a71bb62 commit a0d86fe
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "ChainRules"
uuid = "082447d4-558c-5d27-93f4-14fc19e9eca2"
version = "1.35.1"
version = "1.35.2"

[deps]
ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"
Expand Down
7 changes: 4 additions & 3 deletions src/rulesets/Base/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -166,18 +166,19 @@ function frule((_, ẋs), ::typeof(repeat), xs::AbstractArray, cnt...; kw...)
return repeat(xs, cnt...; kw...), repeat(ẋs, cnt...; kw...)
end

function rrule(::typeof(repeat), xs::AbstractArray; inner=ntuple(Returns(1), ndims(xs)), outer=ntuple(Returns(1), ndims(xs)))
function rrule(::typeof(repeat), xs::AbstractArray; inner=nothing, outer=nothing)

project_Xs = ProjectTo(xs)
S = size(xs)
inner_size = inner === nothing ? ntuple(Returns(1), ndims(xs)) : inner
function repeat_pullback(ȳ)
dY = unthunk(ȳ)
Δ′ = zero(xs)
# Loop through each element of Δ, calculate source dimensions, accumulate into Δ′
for (dest_idx, val) in pairs(IndexCartesian(), dY)
# First, round dest_idx[dim] to nearest gridpoint defined by inner[dim], then
# First, round dest_idx[dim] to nearest gridpoint defined by inner_dims[dim], then
# wrap around based on original size S.
src_idx = [mod1(div(dest_idx[dim] - 1, inner[dim]) + 1, S[dim]) for dim in 1:length(S)]
src_idx = [mod1(div(dest_idx[dim] - 1, inner_size[dim]) + 1, S[dim]) for dim in 1:length(S)]
Δ′[src_idx...] += val
end
= project_Xs(Δ′)
Expand Down
1 change: 1 addition & 0 deletions test/rulesets/Base/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ end
test_rrule(repeat, rand(4, 5))
test_rrule(repeat, rand(4, 5); fkwargs = (outer=(1,2),))
test_rrule(repeat, rand(4, 5); fkwargs = (inner=(1,2), outer=(1,3)))
test_rrule(repeat, rand(4, 5); fkwargs = (outer=2,))

test_rrule(repeat, rand(4, ), 2)
test_rrule(repeat, rand(4, 5), 2)
Expand Down

2 comments on commit a0d86fe

@mzgubic
Copy link
Member Author

@mzgubic mzgubic commented on a0d86fe Jun 9, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/62018

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v1.35.2 -m "<description of version>" a0d86fea0c27d9d9ff2b1872b3f7601bf20b4999
git push origin v1.35.2

Please sign in to comment.