Use mixed mode AD for Jac evaluation#2605
Conversation
|
Just popping by to say that the bug you found is indeed easy to fix, but that bidirectional coloring is still fairly new and not widely used. In particular, the number of colors it returns for each side is fairly unpredictable, as you have noticed. I'll be tinkering with ways to weight forward and reverse mode differently, in order to favor forward colors when possible |
|
Hi @SouthEndMusic, Detailsusing StableRNGs, SparseArrays
using SparseMatrixColorings # branch https://github.com/gdalle/SparseMatrixColorings.jl/tree/am/postprocessing_v2
J = sprand(StableRNG(0), Bool, 1000, 2000, 0.001) # replace with your Jacobian sparsity pattern
problem = ColoringProblem(; structure = :nonsymmetric, partition = :bidirectional)
order = RandomOrder(StableRNG(0), 0)
algo_r = GreedyColoringAlgorithm(
order; postprocessing = true, postprocessing_minimizes = :row_colors
)
algo_c = GreedyColoringAlgorithm(
order; postprocessing = true, postprocessing_minimizes = :column_colors
)
algo_a = GreedyColoringAlgorithm(
order; postprocessing = true, postprocessing_minimizes = :all_colors
)
result_r = coloring(J, problem, algo_r);
result_c = coloring(J, problem, algo_c);
result_a = coloring(J, problem, algo_a);
count_colors(result) = length(row_groups(result)), length(column_groups(result))
count_colors(result_r)
count_colors(result_a)
count_colors(result_c) |
Fixes #2176 (?)
I haven't really looked into scaling yet, but I found an avenue for performance improvement: evaluating the Jacobian with mixed mode AD where the reverse mode is
AutoMooncake(). Some notes:CArrayscompatible withMooncake.jl, so for now the state is aVector{Float64}and wrapped in aCArraywhere needed@threadedcode but it should be fine for our application, an issue came out of my discussions with a developer: Add support for reverse mode AD of multi-threadedmap!operation chalk-lab/Mooncake.jl#791. This is probably not going to be solved before merging this PR. There is a workaround with@mooncake_overlay.get_diff_evalwhich tries different colorings to obtain the cheapest way to evaluate the JacobianDifferentiationInterfacecode for sparse matrix coloring for mixed mode AD which makes certain small models fail, I've been told this is easy to fix so it will probably be fixed shortly: Sparse Jacobian preparation with mixed mode AD andGreedyColoringAlgorithm(; postprocessing=true)fails JuliaDiff/DifferentiationInterface.jl#857