jlchan/FluxDiffUtils.jl

Loss of sparsity in hadamard_sum

Closed this issue · 2 comments

cgt3 commented

Matrices are no longer sparse after the broadcasted call totranspose in hadamard_sum and hadamard_sum!(rhs, A_list,...).

So this is because the output of the broadcasted transpose is just a lazy wrapper around array accesses. Since sparse matrices are Compressed Sparse Column (CSC) storage in Julia (designed for fast access along columns) row accesses in hadamard_sum are slow.

This can be fixed by forcing a conversion to sparse, but I don't think this is a good idea. First, this conversion still incurs overhead

julia> A_list = ntuple(x->sprand(10000,10000,.01),2);
julia> @btime map(A->convert(typeof(A),transpose(A)),$A_list)
  33.846 ms (30 allocations: 61.27 MiB)

Secondly, forcing a conversion would result in a slowdown for other matrix types (such as CSR format).

I suggest precomputing ATr_list = map(A->convert(typeof(A),transpose(A)),A_list) and passing that into hadamard_sum_ATr!.

Updated docs to address this in ff0adf9