-
Notifications
You must be signed in to change notification settings - Fork 4
feat: Reactant compatibility #135
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -32,6 +32,7 @@ jobs: | |
| - 'MOI' | ||
| - 'Perf' | ||
| - 'OpenCL' | ||
| - 'Reactant' | ||
| exclude: | ||
| - version: 'lts' | ||
| group: 'Perf' | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| module CoolPDLPReactantExt | ||
|
|
||
| using CoolPDLP: | ||
| CoolPDLP, | ||
| ConvergenceStats, | ||
| KKTErrors, | ||
| MILP, | ||
| PDHGState, | ||
| PrimalDualSolution, | ||
| Scratch, | ||
| StepSizes, | ||
| custom_to_rarray | ||
| using Reactant: ConcreteRArray, to_rarray | ||
|
|
||
| function CoolPDLP.custom_to_rarray(milp::MILP; kwargs...) | ||
| return to_rarray(milp; kwargs...) | ||
| end | ||
|
|
||
| function CoolPDLP.custom_to_rarray(sol::PrimalDualSolution; kwargs...) | ||
| return to_rarray(sol; kwargs...) | ||
| end | ||
|
|
||
| function CoolPDLP.custom_to_rarray(scratch::Scratch; kwargs...) | ||
| (; x, y, z, b1, b2) = scratch | ||
| xr = to_rarray(x; kwargs...) | ||
| yr = to_rarray(y; kwargs...) | ||
| zr = to_rarray(z; kwargs...) | ||
| b1r = to_rarray(b1; kwargs...) | ||
| b2r = to_rarray(b2; kwargs...) | ||
| return Scratch(; | ||
| x = xr, y = yr, z = zr, b1 = b1r, b2 = b2r | ||
| ) | ||
| end | ||
|
|
||
| function CoolPDLP.custom_to_rarray(stats::ConvergenceStats; kwargs...) | ||
| (; err, starting_time, time_elapsed, kkt_passes, termination_status, error_history) = stats | ||
| return ConvergenceStats( | ||
| to_rarray(err; kwargs...); | ||
| starting_time, | ||
| time_elapsed, | ||
| kkt_passes, | ||
| termination_status, | ||
| error_history = to_rarray(error_history; kwargs...) | ||
| ) | ||
| end | ||
|
|
||
| function CoolPDLP.custom_to_rarray(state::PDHGState; kwargs...) | ||
| (; sol, sol_last, step_sizes, scratch, stats) = state | ||
| sol_r = to_rarray(sol; kwargs...) | ||
| sol_last_r = to_rarray(sol_last; kwargs...) | ||
| step_sizes_r = to_rarray(step_sizes; kwargs...) | ||
| scratch_r = custom_to_rarray(scratch; kwargs...) | ||
| stats_r = custom_to_rarray(stats; kwargs...) | ||
| return PDHGState(; | ||
| sol = sol_r, | ||
| sol_last = sol_last_r, | ||
| step_sizes = step_sizes_r, | ||
| scratch = scratch_r, | ||
| stats = stats_r, | ||
| ) | ||
| end | ||
|
|
||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| """ | ||
| custom_to_rarray | ||
|
|
||
| Alternative to `Reactant.to_rarray` designed to trace structs inside the CoolPDLP package. | ||
|
|
||
| # See also | ||
|
|
||
| - https://github.com/EnzymeAD/Reactant.jl/issues/3184 | ||
| """ | ||
| function custom_to_rarray end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -34,7 +34,7 @@ struct MILP{ | |
| Dg2 <: Diagonal{T}, | ||
| M <: AbstractMatrix{T}, | ||
| Mt <: AbstractMatrix{T}, | ||
| Vb <: DenseVector{Bool}, | ||
| Vb <: DenseVector, | ||
| } | ||
| "objective vector" | ||
| c::Vo | ||
|
|
@@ -197,14 +197,14 @@ nbvar(milp::MILP) = size(milp.c, 1) | |
|
|
||
| Return the number of integer variables in `milp`. | ||
| """ | ||
| nbvar_int(milp::MILP) = sum(milp.int_var) | ||
| nbvar_int(milp::MILP) = sum(identity, milp.int_var) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why? Is this a Reactant bug?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this errors inside Reactant because the integers are traced and so they have no concrete value. It's only used inside printing routines though so not essential |
||
|
|
||
| """ | ||
| nbvar_cont(milp) | ||
|
|
||
| Return the number of continuous variables in `milp`. | ||
| """ | ||
| nbvar_cont(milp::MILP) = nbvar(milp) - nbvar_int(milp) | ||
| nbvar_cont(milp::MILP) = sum(!, milp.int_var) | ||
|
|
||
| """ | ||
| nbcons(milp) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| using CoolPDLP | ||
| using MathOptBenchmarkInstances | ||
| using Reactant | ||
| using Test | ||
|
|
||
| dataset = Netlib | ||
| list = list_instances(dataset); | ||
| name = list[4] | ||
| qps, path = read_instance(dataset, name); | ||
|
|
||
| milp0 = MILP(qps; dataset, name, path); | ||
| sol0 = PrimalDualSolution(milp0); | ||
|
|
||
| algo = PDHG( | ||
| Float32, | ||
| Int32, | ||
| Matrix; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, you're using a dense matrix! I have been taking a bit with the Reactant people how we could maybe have some basic sparse support. There is https://mlir.llvm.org/docs/Dialects/SparseTensorOps/, but we would need to lower it into cuSPARSE calls ourselves
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Put up EnzymeAD/Reactant.jl#3198, which should enable spmv and spmm support in Reactant |
||
| termination_reltol = 1.0f-6, | ||
| time_limit = 10.0, | ||
| record_error_history = false, | ||
| show_progress = false | ||
| ); | ||
|
|
||
| milp, sol = preprocess(milp0, sol0, algo); | ||
| state = initialize(milp, sol, algo; starting_time = time()); | ||
|
|
||
| milp_r = CoolPDLP.custom_to_rarray(milp); | ||
| state_r = CoolPDLP.custom_to_rarray(state; track_numbers = true); | ||
|
|
||
| compile_options = CompileOptions(; donated_args = :none) | ||
| @test_nowarn compiled_step! = @compile compile_options = compile_options CoolPDLP.step!(state_r, milp_r) | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unrelated? I personally don't like
var = if ...that much, but maybe that's just meThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, unrelated. But when I tried to trace the whole loop instead of just the
step!, I had issues withif/elsestatements like this one which return a non-traceable struct