-
Notifications
You must be signed in to change notification settings - Fork 7
Use mixed mode AD for Jac evaluation #2605
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
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 |
|
@gdalle In this PR I loop over the orderings for the mixed mode coloring and check the Jacobian evaluation time: Lines 80 to 104 in 33f082c
As an (opt-in) feature you could do something similar internally where you time a forward and reverse pass to use as weights in the coloring algorithm |
|
Hi @SouthEndMusic, using 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