Skip to content

perf(stage3,pass2): solve the selection MILP's independent blocks apart (exact); frontier dead-candidate elimination - #1564

Draft
zzylol wants to merge 6 commits into
feat/sil-intentfrom
feat/stage3-block-solve
Draft

zzylol wants to merge 6 commits into
feat/sil-intentfrom
feat/stage3-block-solve

Conversation

@zzylol

@zzylol zzylol commented Dec 4, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Problem: Stage 3 solves the whole selection MILP as one HiGHS problem. On paper workloads that problem is made of independent blocks (one per source: PromQL data, SQL http, SQL flows). HiGHS's root work on the hardest block holds back all the others.
  • Change: after the model is built, find its independent blocks (no shared variable, no shared row). Solve each block as its own HiGHS problem, up to 4 blocks in parallel threads, and put the solution back together in block order. Pass 2's dead-candidate elimination now rechecks only the candidates next to newly dead ones, instead of every candidate each round.
  • Effect: same MILP optimum. Planning wall time: paper n100 25.1 → 11.4 s, mixed-8users 37.3 → 13.8 s, backend n100 4.2 → 4.4 s (within noise; its solve drops 0.76 → 0.24 s).

Review guide

  • Base: feat/sil-intent (feat(sil): plan SIL intents: parse/check, lowering, hole guard, planning modes, group latency (L_G) #1553). This draft is stacked on it.
  • Read first: crates/stage3-plan-selection/src/milp/model/independent_blocks.rs (independent_blocks, solve_by_blocks, solve_block). Then read the wiring in solve_selection_reporting_lp_bound in milp/model/mod.rs.
  • Needs careful review:
    • error and status mapping in solve_selection_reporting_lp_bound;
    • the round invariant in dead_candidates (stage1-pass2-reuse-candidates/src/dead_candidates.rs).
  • Mechanical: evaluation_grids.rs now puts a group on the queue at most once; there is also a cargo fmt commit.
  • Commits, in order:
    1. tests with todo!() stubs;
    2. block detection and the solve;
    3. Stage 3 wiring;
    4. Pass 2 reference check;
    5. Pass 2 frontier;
    6. fmt.

On the 11-26 "no DAG split" decision: this is not a split of the workload or the DAG.

  • Stages 0–2 still see the whole workload, and the MILP is built whole, with every sharing variable and row.
  • The split happens only inside Stage 3, after the model is built. It follows connected components of the variable–row graph. Two blocks share no variable and no row, so no shared plan can be lost, and the optimum is the sum of the block optima.

Behavior: before → after

asap_planner --workload paper.n100.promql.json --workload paper.n100.sql.json --sql-catalog-table <http> --sql-catalog-table <flows> --run-until stage3 --format text --verbose (S1 arguments). Load average 19–55 on the 56-core local machine.

before (b9c7fdf) after
selection MILP: line 12954 variables, 17219 constraints, 118 ms building, 23443 ms solving, objective 6.732615e5/s 12954 variables, 17219 constraints, 120 ms building, 9582 ms solving, objective 6.732615e5/s
planning wall / CPU 25.1 s / 24.9 s 11.4 s / 13.4 s

This serves the eval: S1 planning time on paper workloads, where the MILP is 90–96% of planning CPU.

Interfaces

None public. All new items are pub(super) inside stage3-plan-selection/src/milp/model/.

crates/stage3-plan-selection/src/milp/model/independent_blocks.rs

+/// A minimization MILP as HiGHS takes it (columns: cost, bounds, integrality; rows: bounds, sorted entries).
+pub(super) struct Milp { pub(super) columns: Vec<MilpColumn>, pub(super) rows: Vec<MilpRow> }
+impl Milp {
+    /// The MILP good_lp would hand HiGHS: a column per variable, a row per constraint, in order.
+    pub(super) fn of(problem: &ProblemVariables, objective: &Expression, constraints: &[Constraint]) -> Self;
+}
+/// One independent block: its columns and rows, ascending.
+pub(super) struct MilpBlock { pub(super) columns: Vec<usize>, pub(super) rows: Vec<usize> }
+/// The blocks (ordered by first column), plus the columns no row reads and the rows that read no column.
+pub(super) struct MilpBlocks { pub(super) blocks: Vec<MilpBlock>, pub(super) loose_columns: Vec<usize>, pub(super) rows_without_columns: Vec<usize> }
+/// Union-find over the columns each row reads.
+pub(super) fn independent_blocks(milp: &Milp) -> MilpBlocks;
+/// Feasibility tolerance override (the accuracy re-solve) and LP relaxation (the LP-bound measurement).
+pub(super) struct HighsSettings { pub(super) feasibility_tolerance: Option<f64>, pub(super) relaxation: bool }
+/// Every column's value and the summed objective.
+pub(super) struct SolvedMilp { pub(super) columns: Vec<f64>, pub(super) objective: f64 }
+/// HiGHS status of the first block (in block order) without a solution, or a HiGHS failure.
+pub(super) enum MilpSolveError { Status(HighsModelStatus), Solver(String) }
+/// The most threads that solve blocks at once (each HiGHS solve stays single-threaded).
+pub(super) const MOST_BLOCK_SOLVER_THREADS: usize = 4;
+/// Solves each block at gap 0 on up to `threads` threads; the result is assembled in block order.
+pub(super) fn solve_by_blocks(milp: &Milp, blocks: &MilpBlocks, settings: HighsSettings, threads: usize) -> Result<SolvedMilp, MilpSolveError>;

crates/stage3-plan-selection/src/milp/model/mod.rs

-fn lp_relaxation_objective(mut model: highs::Model) -> Option<f64>;

lp_relaxation_objective is removed because the LP bound is now solve_by_blocks with relaxation: true.

Usage (end to end, as above): the planner CLI reaches select_plan, then solve_selection_reporting_lp_bound.

  • It builds the good_lp model as before, then calls Milp::of(&problem, &objective, &constraints): 12954 columns and 17219 rows on n100.
  • independent_blocks finds 939 blocks in 1 ms. Three have more than one column: 6334, 4934 and 750 columns. The rest are single columns.
  • solve_by_blocks(.., HighsSettings::default(), 4) solves them. The largest blocks start first. Wall time is about the hardest block's time, since the RangeHeavy PromQL block takes about 7.7 s alone.
  • The columns go back through column_of to the selection, exactly as before. The CLI prints the plan and the selection MILP: line shown above.

Algorithm changes

Stage 3: solve independent blocks apart

  • Problem: n100's MILP is 3 large independent blocks. Solved whole, HiGHS took 23.4 s. Most of that is the RangeHeavy block's root cuts, sub-MIP heuristics and restarts, which also hold back the easy blocks.
  • Before: one HiGHS model (gap 0, one thread). One solve, plus a re-solve at tolerance 1e-10 if the exact accuracy check fails. The LP bound was read from the same model.
  • After:
    1. Run union-find over the columns of each row.
    2. Treat each component that has rows as a block.
    3. Give each loose column its cheapest bound; check each row without columns against 0 (tolerance 1e-7).
    4. Solve each block as its own RowProblem with the same options, on a scoped thread pool (an atomic work index, largest block first).
    5. Write the values back by block, and sum the objectives in block order.
    6. Run the accuracy re-solve and the LP bound on the same blocks.
  • Correctness: blocks share no variable and no row, so the feasible set is the product of the block feasible sets and the objective separates. The optimum is therefore the sum of the block optima. Each HiGHS solve is single-threaded and the result is assembled by block index, so the plan does not depend on the thread count. Tests:
    • generated MILPs, split solve vs whole solve, with equal columns and objective at 1 and 4 threads;
    • the paper models dumped as MPS: objective equal to well within 1e-9 relative (n100 8786.347614241902 vs 8786.34761298455; m8u 10537.19448644036 vs 10537.194486636117; b100 905542.7244542161 vs 905542.724455967).
  • Complexity:
    • Block detection costs O(nonzeros · α); 1 ms on m8u.
    • Solve time goes from that of the whole model to roughly the slowest block. Solve wall: n100 23.4 → 9.6 s, m8u 35.1 → 11.2 s, b100 0.76 → 0.24 s.
    • CPU drops too: n100 24.9 → 13.4 s user.
    • Memory is about the same (n100 RSS 230 → 245 MB; m8u 314 → 278 MB).
  • Assumptions and limits:
    • Applies to cost model 1's solve_selection only. The cost model 3 cluster solve (cluster/solve.rs) is unchanged; its budget rows would couple the blocks anyway.
    • The speed-up is capped by the hardest block.
    • The re-solve at 1e-10 no longer passes the first selection to HiGHS as a MIP start. HiGHS dropped that start whenever it missed at the tighter tolerance, so it never changed the result.
    • good_lp normalizes a >= row to <= with negated coefficients. The rows are passed that way, which can change HiGHS's search path, though not the optimum.

Pass 2: frontier dead-candidate elimination

  • Problem: dead_candidates rescanned every candidate in each round. cut_edges_of_dominated_thetas calls it once per round of its own loop.
  • After: the first round checks every candidate. Each later round checks only the producers and consumers (via EdgeGraph::neighbours) of candidates that died in the previous round. Each round still judges against the dead set from before that round.
  • Correctness: a candidate's verdict depends only on which of its neighbours are dead. If no neighbour died since its last check, its verdict is unchanged. So the rounds, the dead set and every reason are the same as with rescanning. A #[cfg(test)] assertion compares against the kept rescanning reference on every memo in the crate's tests.
  • Evaluation grids: evaluation_period_ms_per_query now puts each group on the queue at most once. The result is the same least fixpoint of gcd merges, whatever the order.
    • The report counted this table as "computed twice", but the second call runs on the memo with the chain groups added, so it is needed.
  • Complexity: rounds × candidates becomes candidates plus the edges around the newly dead. Measured Pass 2 time on b100 and m8u is unchanged within noise (b100 user 1.98–2.61 s for both builds, load about 65). The report's estimate (≤ 0.1 s) is below the noise on this machine.

Decisions and open questions

  • Needs reviewer decision:
    • Ties on the large workloads: the MILP optimum is identical, but on n100, m8u and b100 the split solve breaks ties between equal-objective plans differently, so the plan JSON differs.
      • On n100 this shows as a different recomputed cost_per_second: before 6.528505e5, after 6.732607e5, with an identical MILP objective of 6.732615e5.
      • So the base's tied plan happened to cost less under plan_cost than the MILP charges it. That is a gap between the MILP and plan_cost that exists already, not something this PR adds. m8u and b100 report equal cost_per_second.
      • Should the eval accept this, or should a follow-up close the MILP/plan_cost gap?
    • Thread cap: fixed at 4 (MOST_BLOCK_SOLVER_THREADS), and also capped by available_parallelism. Make it configurable?
  • Decided: 2026-12-03, the user approved exact block solving and the Pass 2 fixes from the planner-speed report.

Verification

scope result notes
cargo test -p asap-stage3-plan-selection --lib 67 passed, 0 failed, 1 ignored the ignored test is the MPS check below
cargo test -p asap-stage1-pass2-reuse-candidates --lib 40 passed each one also asserts frontier = rescanning
MPS check dumped_planner_milps_have_the_whole_models_optimum (release, ASAP_STAGE3_MPS_DIR) passed n100: whole 23.1 s vs blocks 8.0 s; m8u: 30.9 s vs 14.4 s; b100: 0.73 s vs 0.22 s
fmt; clippy -D warnings --all-targets on both crates clean
Pass 2 output, base vs head, --run-until stage1_pass2 --format json 60 of 60 identical 14 o11y corpus queries, 42 sketch-snapshot workloads, n100, m8u, b100, rh8
Stage 3 plan, base vs head, --run-until stage3 --format json (timings removed) 57 of 60 identical all corpus and sketch workloads identical; n100, m8u, b100 break ties differently with the same MILP objective (see above)

The new tests:

  • columns_linked_through_rows_are_one_block, a_row_over_every_column_makes_one_block: block detection, including loose columns and rows without columns.
  • a_good_lp_model_becomes_highs_columns_and_rows: the good_lp model becomes the right HiGHS rows, bounds and integrality.
  • solving_block_by_block_finds_the_whole_models_optimum, linked_blocks_are_solved_as_one, the_lp_relaxation_block_by_block_is_the_whole_relaxation: the split solve equals the whole solve.
  • a_loose_column_takes_its_cheapest_bound, an_infeasible_block_makes_the_milp_infeasible, a_row_without_columns_holds_only_when_its_bounds_contain_zero, an_empty_milp_is_solved_with_objective_zero: edge statuses.

The first commit holds the tests with todo!() stubs. It was not built on its own; its tests fail by construction.

Planning time, release binaries, --format text, local 56-core machine, load average 19–64 (other agents' builds):

workload base wall / user head wall / user MILP solve base → head
paper n100 25.1 / 24.9 s, 26.8 / 26.5 s 11.4 / 13.4 s, 11.6 / 13.9 s 23.4 → 9.6 s
mixed-8users 37.3 / 36.9 s, 37.9 / 37.2 s 13.8 / 18.4 s, 13.9 / 19.3 s 35.1 → 11.2 s
backend n100 5.2 / 4.0 s, 4.2 / 4.0 s 4.5 / 4.0 s, 4.4 / 4.6 s 0.76 → 0.24 s

Follow-ups

Deferred:

  • block solving for the cost model 3 cluster solve;
  • a report field with the block count;
  • JSON-free memo hash-consing (report item 2);
  • one-plan SIL IntentFull (item 3);
  • the MILP/plan_cost gap shown by the n100 tie.

🤖 Generated with Claude Code

https://claude.ai/code/session_01K1M6boFpwkpMtRc1FnZuYi

zzylol and others added 6 commits December 3, 2026 22:29
…vs whole (failing)

Block detection over rows, the good_lp model as HiGHS columns/rows, and
the optimum solved block by block against the whole model on generated
MILPs; an ignored check on MPS dumps of paper workloads (n100, mixed-8users,
backend n100). The functions are stubs (todo!), so the tests fail.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01K1M6boFpwkpMtRc1FnZuYi
Union-find over the columns each row reads; each block is its own HiGHS
problem (gap 0, one thread), up to MOST_BLOCK_SOLVER_THREADS blocks at
once, assembled in block order. Loose columns take their cheapest bound;
rows without columns are checked against 0.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01K1M6boFpwkpMtRc1FnZuYi
Blocks share no variable and no row, so the optimum is the whole model's;
the split is after the model is built, so no sharing is lost. The
accuracy re-solve and the LP bound run on the same blocks.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01K1M6boFpwkpMtRc1FnZuYi
…ery round

Keeps the rescanning elimination as a test-only reference and asserts the
elimination agrees with it on every memo the crate's tests build.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01K1M6boFpwkpMtRc1FnZuYi
dead_candidates checks every candidate in the first round and then only
the neighbours of the newly dead, with the same rounds and reasons as
rescanning; threshold_theta's loop calls it each round, so it gains too.
evaluation_period_ms_per_query queues a group at most once (same least
fixpoint of gcd merges in any order).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01K1M6boFpwkpMtRc1FnZuYi
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01K1M6boFpwkpMtRc1FnZuYi
@zzylol zzylol added enhancement New feature or request optimizer Workload optimization and plan selection labels Dec 4, 2026
@zzylol

zzylol commented Dec 4, 2026

Copy link
Copy Markdown
Contributor Author

Follow-up issue for the objective vs plan_cost gap that this PR's tie-breaking exposed (n100: same objective, plan_cost +3.1%): #1566. No change requested on this PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request optimizer Workload optimization and plan selection

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant