Skip to content

Commit

Permalink
add rules for vector-matrix and matrix-vector product (#305)
Browse files Browse the repository at this point in the history
* add rules for vector-matrix and matrix-vector product

Fixes #276

* fix bug in test function writing

* add separate dispatch for Vector * Matrix

* fix tests for Matrix*Vector, Vector*Matrix

* fix test

* Assert about size

* bump version

Co-authored-by: Lyndon White <oxinabox@ucc.asn.au>
  • Loading branch information
gxyd and oxinabox committed Nov 17, 2020
1 parent d6f3d26 commit 65df987
Show file tree
Hide file tree
Showing 3 changed files with 45 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 = "0.7.32"
version = "0.7.33"

[deps]
ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"
Expand Down
26 changes: 24 additions & 2 deletions src/rulesets/Base/arraymath.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ end

function rrule(
::typeof(*),
A::AbstractMatrix{<:CommutativeMulNumber},
B::AbstractMatrix{<:CommutativeMulNumber},
A::AbstractVecOrMat{<:CommutativeMulNumber},
B::AbstractVecOrMat{<:CommutativeMulNumber},
)
function times_pullback(Ȳ)
return (
Expand All @@ -40,6 +40,28 @@ function rrule(
return A * B, times_pullback
end

function rrule(
::typeof(*),
A::AbstractVector{<:CommutativeMulNumber},
B::AbstractMatrix{<:CommutativeMulNumber},
)
function times_pullback(Ȳ)
@assert size(B, 1) === 1 # otherwise primal would have failed.
return (
NO_FIELDS,
InplaceableThunk(
@thunk(Ȳ * vec(B')),
-> mul!(X̄, Ȳ, vec(B'), true, true)
),
InplaceableThunk(
@thunk(A' * Ȳ),
-> mul!(X̄, A', Ȳ, true, true)
)
)
end
return A * B, times_pullback
end

function rrule(
::typeof(*), A::CommutativeMulNumber, B::AbstractArray{<:CommutativeMulNumber}
)
Expand Down
21 changes: 20 additions & 1 deletion test/rulesets/Base/arraymath.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,20 @@
rrule_test(*, (dims), (dims), ())
end

@testset "AbstractMatrix-AbstractVector n=$n, m=$m" for n in (2, 3), m in (4, 5)
@testset "Array" begin
rrule_test(*, (n), n ₂ m, (m))
end
end

@testset "AbstractVector-AbstractMatrix n=$n, m=$m" for n in (2, 3), m in (4, 5)
@testset "Array" begin
rrule_test(*, n m, (n), 1 ₂ m)
end
end

@testset "AbstractMatrix-AbstractMatrix" begin
@testset "n=$n, m=$m, p=$p" for n in (2, 5), m in (2, 4), p in (2, 3)
@testset "Matrix * Matrix n=$n, m=$m, p=$p" for n in (2, 5), m in (2, 4), p in (2, 3)
@testset "Array" begin
rrule_test(*, np, (n₂m), (m₂p))
end
Expand All @@ -46,6 +58,13 @@
end
end
end

@testset "Covector * Vector n=$n" for n in (3, 5)
@testset "$f" for f in (adjoint, transpose)
# This should be same as dot product and give a scalar
rrule_test(*, (), f.((n)), (n))
end
end
end


Expand Down

2 comments on commit 65df987

@oxinabox
Copy link
Member

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/24823

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 v0.7.33 -m "<description of version>" 65df9875bd74af5be3f09e9d8aaf576296dd4226
git push origin v0.7.33

Please sign in to comment.