Skip to content
Merged
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
53 changes: 52 additions & 1 deletion GNNlib/ext/GNNlibMooncakeExt.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module GNNlibMooncakeExt

using GNNlib: GNNlib, propagate, copy_xj, e_mul_xj
using GNNlib: GNNlib, propagate, copy_xj, e_mul_xj, w_mul_xj
using GNNGraphs: GNNGraph, adjacency_matrix, edge_index, set_edge_weight
using LinearAlgebra: adjoint
using Base: IEEEFloat
Expand Down Expand Up @@ -85,4 +85,55 @@ function Mooncake.rrule!!(
return res, propagate_e_mul_xj_add_pullback!!
end

# Reverse rule for the weighted fast path `propagate(w_mul_xj, g, +, xj) == xj * A(w)`.
# `w` are the graph's own weights, so `dw` accumulates into the COO tangent `g.graph[3]`.

@is_primitive DefaultCtx Tuple{
typeof(propagate),
typeof(w_mul_xj),
GNNGraph{<:Tuple},
typeof(+),
Nothing,
AbstractMatrix{P},
Nothing,
} where {P <: IEEEFloat}

# Edge-weight tangent accumulator (COO `graph[3]`), or `nothing` for a constant graph.
_w_mul_xj_dw(::Mooncake.NoFData) = nothing
function _w_mul_xj_dw(dg::Mooncake.FData)
gt = dg.data.graph
gt isa Tuple || return nothing
dw = gt[3]
return dw isa AbstractVector{<:IEEEFloat} ? dw : nothing
end

function Mooncake.rrule!!(
::CoDual{typeof(propagate)},
::CoDual{typeof(w_mul_xj)},
g::CoDual{<:GNNGraph{<:Tuple}},
::CoDual{typeof(+)},
::CoDual{Nothing},
xj::CoDual{<:AbstractMatrix{P}},
::CoDual{Nothing},
) where {P <: IEEEFloat}
pg = Mooncake.primal(g)
pxj = Mooncake.primal(xj)
s, t = edge_index(pg)
A = adjacency_matrix(pg, P; weighted = true)
y = pxj * A
res = Mooncake.zero_fcodual(y)
dw = _w_mul_xj_dw(Mooncake.tangent(g))
function propagate_w_mul_xj_add_pullback!!(::NoRData)
dy = Mooncake.tangent(res)
dxj = Mooncake.tangent(xj)
dxj .+= dy * adjoint(A)
if dw !== nothing
dw .+= vec(sum(view(pxj, :, s) .* view(dy, :, t); dims = 1))
end
return NoRData(), NoRData(), Mooncake.zero_rdata(pg),
NoRData(), NoRData(), NoRData(), NoRData()
end
return res, propagate_w_mul_xj_add_pullback!!
end

end # module
Loading