Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
11 changes: 11 additions & 0 deletions GNNGraphs/ext/GNNGraphsCUDAExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ using CUDA
using Random, Statistics, LinearAlgebra
using GNNGraphs
using GNNGraphs: COO_T, ADJMAT_T, SPARSE_T
using SparseArrays

const CUMAT_T = Union{CUDA.AnyCuMatrix, CUDA.CUSPARSE.CuSparseMatrix}

Expand All @@ -20,6 +21,16 @@ GNNGraphs.dense_zeros_like(a::CUMAT_T, T::Type, sz = size(a)) = CUDA.zeros(T, sz

GNNGraphs.iscuarray(x::AnyCuArray) = true

function GNNGraphs.binarize(Mat::CUSPARSE.CuSparseMatrixCSC)
bin_vals = fill!(similar(nonzeros(Mat), Bool), true)
return CUSPARSE.CuSparseMatrixCSC(Mat.colPtr, rowvals(Mat), bin_vals, size(Mat))
end
function GNNGraphs.binarize(Mat::CUSPARSE.CuSparseMatrixCSC, T::DataType)
bin_vals = fill!(similar(nonzeros(Mat)), one(T))
# Binarize a CuSparseMatrixCSC by setting all nonzero values to one(T)
return CUSPARSE.CuSparseMatrixCSC(Mat.colPtr, rowvals(Mat), bin_vals, size(Mat))
end


function sort_edge_index(u::AnyCuArray, v::AnyCuArray)
dev = get_device(u)
Expand Down
4 changes: 2 additions & 2 deletions GNNGraphs/src/query.jl
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ function Graphs.adjacency_matrix(g::GNNGraph{<:ADJMAT_T}, T::DataType = eltype(g
@assert dir ∈ [:in, :out]
A = g.graph
if !weighted
A = binarize(A)
A = binarize(A, T)
end
A = T != eltype(A) ? T.(A) : A
return dir == :out ? A : A'
Expand Down Expand Up @@ -377,7 +377,7 @@ end

function _degree(A::AbstractMatrix, T::Type, dir::Symbol, edge_weight::Bool, num_nodes::Int)
if edge_weight === false
A = binarize(A)
A = binarize(A, T)
end
A = eltype(A) != T ? T.(A) : A
return dir == :out ? vec(sum(A, dims = 2)) :
Expand Down
2 changes: 2 additions & 0 deletions GNNGraphs/src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,8 @@ function _rand_edges(rng, (n1, n2), m)
end

binarize(x) = map(>(0), x)
# here just to allow CUDA extension to overload this function with correct type casting
binarize(x, T::DataType) = binarize(x)

CRC.@non_differentiable binarize(x...)
CRC.@non_differentiable edge_encoding(x...)
Expand Down
Loading