Skip to content

Commit

Permalink
symm rrule implementation (#219)
Browse files Browse the repository at this point in the history
* Fix typo

* symm rrule implementation

* Bump patch

* Use BLAS for multithreading

* Optimise for common edge case

* Improve error message further
  • Loading branch information
willtebbutt authored Aug 8, 2024
1 parent 55cb4cf commit 0b55e25
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "Tapir"
uuid = "07d77754-e150-4737-8c94-cd238a1fb45b"
authors = ["Will Tebbutt, Hong Ge, and contributors"]
version = "0.2.31"
version = "0.2.32"

[deps]
ADTypes = "47edcb42-4c32-4615-8424-f2b9edc5f35b"
Expand Down
6 changes: 6 additions & 0 deletions src/interpreter/s2s_reverse_mode_ad.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1254,6 +1254,12 @@ function (rule::LazyDerivedRule{T, Trule})(args::Vararg{Any, N}) where {N, T, Tr
rule.rule = derived_rule
else
@warn "Unable to put rule in rule field. Rule should error."
println("MethodInstance is")
display(rule.mi)
println()
println("with signature")
display(rule.mi.specTypes)
println()
println("derived_rule is of type")
display(typeof(derived_rule))
println()
Expand Down
106 changes: 105 additions & 1 deletion src/rrules/blas.jl
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,93 @@ for (gemm, elty) in ((:dgemm_, :Float64), (:sgemm_, :Float32))
end
end

@is_primitive(
MinimalCtx,
Tuple{
typeof(BLAS.symm!),
Char,
Char,
T,
MatrixOrView{T},
MatrixOrView{T},
T,
Matrix{T},
} where {T<:Union{Float32, Float64}},
)

function rrule!!(
::CoDual{typeof(BLAS.symm!)},
side::CoDual{Char},
uplo::CoDual{Char},
alpha::CoDual{T},
A_dA::CoDual{<:MatrixOrView{T}},
B_dB::CoDual{<:MatrixOrView{T}},
beta::CoDual{T},
C_dC::CoDual{Matrix{T}},
) where {T<:Union{Float32, Float64}}

# Extract primals.
s = primal(side)
ul = primal(uplo)
α = primal(alpha)
β = primal(beta)
A, dA = viewify(A_dA)
B, dB = viewify(B_dB)
C, dC = viewify(C_dC)

# In this rule we optimise carefully for the special case a == 1 && b == 0, which
# corresponds to simply multiplying symm(A) and B together, and writing the result to C.
# This is an extremely common edge case, so it's important to do well for it.
C_copy = copy(C)
tmp_ref = Ref{Matrix{T}}()
if== 1 && β == 0)
BLAS.symm!(s, ul, α, A, B, β, C)
else
tmp = BLAS.symm(s, ul, one(T), A, B)
tmp_ref[] = tmp
BLAS.axpby!(α, tmp, β, C)
end

function symm!_adjoint(::NoRData)

if== 1 && β == 0)
= dot(dC, C)
BLAS.copyto!(C, C_copy)
else
# Reset C.
BLAS.copyto!(C, C_copy)

# gradient w.r.t. α. Safe to write into memory for copy of C.
BLAS.symm!(s, ul, one(T), A, B, zero(T), C_copy)
= dot(dC, C_copy)
end

# gradient w.r.t. A.
dA_tmp = s == 'L' ? dC * B' : B' * dC
if ul == 'L'
dA .+= α .* LowerTriangular(dA_tmp)
dA .+= α .* UpperTriangular(dA_tmp)'
else
dA .+= α .* LowerTriangular(dA_tmp)'
dA .+= α .* UpperTriangular(dA_tmp)
end
@inbounds for n in diagind(dA)
dA[n] -= α * dA_tmp[n]
end

# gradient w.r.t. B.
BLAS.symm!(s, ul, α, A, dC, one(T), dB)

# gradient w.r.t. beta.
= dot(dC, C)

# gradient w.r.t. C.
BLAS.scal!(β, dC)

return NoRData(), NoRData(), NoRData(), dα, NoRData(), NoRData(), dβ, NoRData()
end
return C_dC, symm!_adjoint
end

for (syrk, elty) in ((:dsyrk_, :Float64), (:ssyrk_, :Float32))
@eval function rrule!!(
Expand Down Expand Up @@ -600,7 +687,7 @@ end

function generate_hand_written_rrule!!_test_cases(rng_ctor, ::Val{:blas})
t_flags = ['N', 'T', 'C']
alphas = [0.0, -0.25]
alphas = [1.0, -0.25]
betas = [0.0, 0.33]

test_cases = vcat(
Expand All @@ -626,6 +713,23 @@ function generate_hand_written_rrule!!_test_cases(rng_ctor, ::Val{:blas})
end
end),
)),

# symm!
vec(reduce(
vcat,
vec(map(product(['L', 'R'], ['L', 'U'], alphas, betas)) do (side, uplo, α, β)
nA = side == 'L' ? 5 : 7
A = randn(nA, nA)
vA = view(randn(15, 15), 1:nA, 1:nA)
B = randn(5, 7)
vB = view(randn(15, 15), 1:5, 1:7)
C = randn(5, 7)
return Any[
(false, :stability, nothing, BLAS.symm!, side, uplo, α, A, B, β, C),
(false, :stability, nothing, BLAS.symm!, side, uplo, α, vA, vB, β, C),
]
end)
)),
)

memory = Any[]
Expand Down

2 comments on commit 0b55e25

@willtebbutt
Copy link
Member Author

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

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

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.2.32 -m "<description of version>" 0b55e253c9f9543ac5bb789df4a81875d76e69fb
git push origin v0.2.32

Please sign in to comment.