Skip to content

Commit

Permalink
add subtract rule
Browse files Browse the repository at this point in the history
  • Loading branch information
nmheim committed Feb 9, 2024
1 parent b3f9f9b commit 2c3e48d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/rulesets/Base/arraymath.jl
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,16 @@ function rrule(::typeof(-), x::AbstractArray)
return -x, negation_pullback
end

#####
##### Subtraction
#####

frule((_, Δx, Δy), ::typeof(-), x::AbstractArray, y::AbstractArray) = x-y, Δx-Δy

function rrule(::typeof(-), x::AbstractArray, y::AbstractArray)
subtract_pullback(dy) = (NoTangent(), dy, -dy)
return x-y, subtract_pullback
end

#####
##### Addition (Multiarg `+`)
Expand Down
8 changes: 8 additions & 0 deletions test/rulesets/Base/arraymath.jl
Original file line number Diff line number Diff line change
Expand Up @@ -217,4 +217,12 @@
@gpu test_rrule(+, randn(4, 4), randn(4, 4), randn(4, 4))
@gpu test_rrule(+, randn(3), randn(3,1), randn(3,1,1))
end

@testset "subtraction" begin
# fwd
@gpu test_frule(-, randn(2), randn(2))
# rev
@gpu test_rrule(-, randn(4, 4), randn(4, 4))
@gpu test_rrule(-, randn(3), randn(3,1))
end
end

0 comments on commit 2c3e48d

Please sign in to comment.