Skip to content

rrule for broadcasted cast of sparse array #586

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
wants to merge 2 commits 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
4 changes: 2 additions & 2 deletions 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.27.0"
version = "1.28.0"

[deps]
ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"
Expand All @@ -16,7 +16,7 @@ Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
ChainRulesCore = "1.12"
ChainRulesTestUtils = "1.5"
Compat = "3.35"
FiniteDifferences = "0.12.20"
FiniteDifferences = "0.12.23"
IrrationalConstants = "0.1.1"
JuliaInterpreter = "0.8" # latest is "0.9.1"
RealDot = "0.1"
Expand Down
10 changes: 10 additions & 0 deletions src/rulesets/SparseArrays/sparsematrix.jl
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,13 @@ function rrule(::typeof(findnz), v::AbstractSparseVector)

return (I, V), findnz_pullback
end

function rrule(::typeof(Broadcast.broadcasted), T::Type{<:Number}, x::AbstractSparseArray)
proj = ProjectTo(x)

function broadcasted_cast_sparse(Δ)
return NoTangent(), NoTangent(), proj(Δ)
end

return T.(x), broadcasted_cast_sparse
end
5 changes: 5 additions & 0 deletions test/rulesets/SparseArrays/sparsematrix.jl
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,8 @@ end
V̄ = rand!(similar(V))
test_rrule(findnz, v ⊢ dv, output_tangent=(zeros(length(I)), V̄))
end

@testset "broadcasted cast SparseMatrixCSC" begin
A = sprand(5, 5, 0.5)
test_rrule(_broadcast, Float32, A, rtol=1e-4)
end
22 changes: 22 additions & 0 deletions test/test_helpers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,28 @@ function ChainRulesCore.rrule(::typeof(make_two_vec), x)
return make_two_vec(x), make_two_vec_pullback
end

"""
Helper function for testing rrule(::typeof(Broadcast.broadcasted), ...)

# Examples

```julia
using SparseArrays, ChainRulesTestUtils

A = sprand(5, 5, 0.5)
test_rrule(_broadcast, Float32, A, rtol=1e-5)
```
"""
_broadcast(f::F, x...) where F = broadcast(f, x...)

# we need this due to https://github.com/JuliaDiff/ChainRulesTestUtils.jl/issues/234
function ChainRulesCore.rrule(::typeof(_broadcast), f::F, args...) where F
rr = rrule(Broadcast.broadcasted, f, args...)
rr === nothing && return nothing
y, pb = rr
Broadcast.materialize(Broadcast.instantiate(y)), pb
end

@testset "test_helpers.jl" begin

@testset "Multiplier" begin
Expand Down