Skip to content
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

add prod #385

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/rulesets/Base/mapreduce.jl
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,17 @@ function rrule(
end
return y, sum_abs2_pullback
end

####
#### prod
####

function rrule(::typeof(prod), x::AbstractArray{T}; dims=:) where {T<:CommutativeMulNumber}
Copy link
Member

@willtebbutt willtebbutt Mar 9, 2021

Choose a reason for hiding this comment

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

Would you consider restricting the type of x to a subtype of AbstractArrays?

Copy link
Member Author

@oxinabox oxinabox Mar 9, 2021

Choose a reason for hiding this comment

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

I just matched sum
I am happy to restrict to whatever

Copy link
Member

Choose a reason for hiding this comment

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

Given that we've not tied this down, I propose StridedArray.

y = prod(x; dims=dims)
function prod_pullback(ȳ)
# broadcasting the two works out the size no-matter `dims`
x̄ = y .* ȳ ./ x
return (NO_FIELDS, x̄)
end
return y, prod_pullback
end
7 changes: 7 additions & 0 deletions test/rulesets/Base/mapreduce.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,11 @@
end
end
end # sum abs2

@testset "prod" begin
test_rrule(prod, randn(5))
test_rrule(prod, randn(5, 6))
test_rrule(prod, randn(5, 6); fkwargs=(;dims=2))
test_rrule(prod, randn(5, 6); fkwargs=(;dims=1))
Comment on lines +25 to +28
Copy link
Member

Choose a reason for hiding this comment

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

Needs complex tests as well

end
end