Legacy grobner (smt.arith.solver=2) does not charge rlimit for coefficient size: rlimit=40000000 never binds (runs > 30 min)
Summary. With smt.arith.solver=2, theory_arith's grobner basis computation can run for
at least 30 minutes under rlimit=40000000, while timeout and Z3_interrupt still stop it within
0.1 s. The coefficients in grobner::equation grow from ~230 bits to ~25,000 bits inside one check. The
rlimit charge is one unit per monomial (merge_monomials: limit().inc(sz); mul_append charges
nothing). Each unit therefore stands for ~5,000x more work by the end. A 23-line patch that charges by
coefficient size turns this into unknown (canceled) at the budget. On 3,186 of our arith.solver=2
benchmarks it changed no verdicts. Only 34 of them run grobner, so 34 is the real denominator.
Reproduce (5.1.0 release and master b83dca781e45, macOS arm64):
cc repro.c -I<z3>/src/api -o repro -lz3
./repro repro.smt2repro.c parses the file once and runs Z3_solver_check twice, each time on a fresh Z3_mk_solver
in the same context, with smt.random_seed=0 rlimit=40000000 smt.arith.solver=2 smt.mbqi=false.
| build | check 1 | check 2 (3 runs, 1,800 s wall) |
|---|---|---|
| 5.1.0 release | unsat, 0.04 s | still running at 1,800 s (3 of 3) |
master b83dca781e45 |
unsat, 0.04 s | still running at 1,800 s (3 of 3) |
| master + patch below | unsat, 0.04 s | unknown (canceled) at 34.5-35.1 s (3 of 3) |
The first check on a context never shows it (0 of 5), and the CLI cannot express the second check. We
tried (reset), (push)/(pop) and (reset-assertions) copies of the same file, and all of them
return in under 10 s. A second fresh solver on the same context is what our tool does in production.
The file is sensitive to symbol names. 12 of 48 renamings were still running at 30 s; at a 400 s
wall, 7 of those 12 still were, and the other 5 finished in 37-213 s. Our guess is that it comes from
grobner::var_lt's tie-break on get_id(); we have not verified this.
Where the time goes. Native sample of check 2 at 90 s, master: 3,750 of 3,750 samples in
theory_arith<mi_ext>::compute_grobner → grobner::compute_basis_step → simplify_to_process. Of those,
2,978 are in merge_monomials (mpq_manager::rat_add → mpz gcd) and 769 in mul_append.
Instrumented master, printing inside compute_basis_loop:
| time into check 2 | largest coefficient | one compute_basis_step |
rlimit count |
|---|---|---|---|
| < 1 s | 233 bits | ≤ 7 ms | 11.1M |
| ~5 s | 9,500 bits | 0.2-1.2 s | 15.6M |
| ~30 s | 15,130 bits | 2-10 s | 16.5M |
| ~60 s | 25,190 bits | 30 s | 16.6M |
(That table is from the unreduced 28-assertion original of repro.smt2. The reduced file shows the
same frames.)
timeout works, so this is not #4192. Z3_interrupt at 5, 30, 120 and 300 s returned in 0.01,
0.02, 0.07 and 0.08 s. timeout=5000 returned at 5.00 s. The loops already poll through
m_manager.inc() and ctx.get_cancel_flag(). The defect is that the deterministic budget measures the
wrong quantity. For a tool that uses rlimit for reproducibility, a bound that never binds is not a
bound.
Suggested fix (src/math/grobner/grobner.cpp, +23/−0):
diff --git a/src/math/grobner/grobner.cpp b/src/math/grobner/grobner.cpp
index e83a88059..93b4b40ec 100644
--- a/src/math/grobner/grobner.cpp
+++ b/src/math/grobner/grobner.cpp
@@ -421,6 +421,25 @@ bool grobner::is_eq_monomial_body(monomial const * m1, monomial const * m2) {
return true;
}
+/**
+ \brief Resource cost of one arithmetic operation on coefficients a and b.
+
+ The loops below charge the resource limit one unit per monomial. The
+ coefficients are arbitrary-precision rationals, and an operation on two
+ n-digit coefficients costs O(n^2) (the gcd that normalizes the result).
+ Coefficients can grow by orders of magnitude during a basis computation, so
+ a flat per-monomial charge lets the computation run for hours under a
+ resource limit it would otherwise exhaust in seconds. Charge the product of
+ the digit counts, scaled so that coefficients below ~256 bits cost nothing
+ extra.
+*/
+static unsigned coeff_cost(rational const & a, rational const & b) {
+ uint64_t da = a.bitsize() / 32;
+ uint64_t db = b.bitsize() / 32;
+ uint64_t c = (da * db) >> 6;
+ return c > UINT_MAX ? UINT_MAX : static_cast<unsigned>(c);
+}
+
/**
\brief Merge monomials (* c1 m) (* c2 m).
@@ -440,6 +459,8 @@ void grobner::merge_monomials(ptr_vector<monomial> & monomials) {
monomial * m1 = monomials[j];
monomial * m2 = monomials[i];
if (is_eq_monomial_body(m1, m2)) {
+ if (unsigned c = coeff_cost(m1->m_coeff, m2->m_coeff))
+ m_manager.limit().inc(c);
m1->m_coeff += m2->m_coeff;
to_delete.push_back(m2);
}
@@ -577,6 +598,8 @@ void grobner::mul_append(unsigned start_idx, equation const * source, rational c
for (unsigned i = start_idx; i < sz; ++i) {
monomial const * m = source->get_monomial(i);
monomial * new_m = alloc(monomial);
+ if (unsigned c = coeff_cost(m->m_coeff, coeff))
+ m_manager.limit().inc(c);
new_m->m_coeff = m->m_coeff;
new_m->m_coeff *= coeff;
new_m->m_vars.append(m->m_vars.size(), m->m_vars.data());
Why this shape:
- It charges and does not poll. The existing
inc()checks insimplifysee the raised count at the next pass. No loop gains an exit, so no half-updated equation can be observed. - Ordinary runs keep their counts. The charge is zero while both coefficients are below ~256 bits. We ran 3,186 benchmarks × 2 checks, patched against unpatched master, comparing verdict and rlimit to the unit. 34 of the 3,186 run grobner at all; the charge cannot act on the rest. Results: 0 verdict changes and 3,173 bit-identical. 4 of the 12 that differ are nondeterministic run-to-run on unpatched master anyway. The other 8 moved by +3 units to +2.5 %.
- The scale is a choice we leave to you. With
>> 6the budget is spent in 28-80 s on our file, against ~4 s for 40M units of ordinary work, so it still undercharges ~7-20x at 25k bits.>> 4would tighten it at the cost of moving more ordinary counts. - The rejected alternative was a cap on coefficient size (
normalize_coeffalready skips anything over 1,000 bits). A cap changes which bases grobner computes. The charge changes only when a budget is judged spent.
What we cannot see from outside: whether other rational-heavy loops in theory_arith_nl.h have the same
gap. is_inconsistent2 is O(n³) in monomials and charges nothing. It did not appear in our samples.
We can build and test any variant you prefer.
Related: #10417 (a different uncancellable phase, lp::dioph_eq, fixed in 5.1.0), #6398 (rlimit not
charged, arith.solver=6).
repro.smt2 (5,166 bytes)(set-option :smt.random_seed 0)
(set-option :rlimit 40000000)
(set-option :smt.arith.solver 2)
(set-option :smt.mbqi false)
(set-info :status unknown)
(declare-fun s18 ((_ FloatingPoint 8 24)) Real)
(declare-fun s13 () (_ FloatingPoint 8 24))
(declare-fun s8 () Real)
(declare-fun s2 () Real)
(declare-fun s4 () Real)
(declare-fun s3 () Real)
(declare-fun s1 () (_ FloatingPoint 8 24))
(declare-fun s0 (RoundingMode (_ FloatingPoint 8 24) (_ FloatingPoint 8 24)) (_ FloatingPoint 8 24))
(declare-fun s11 () (_ FloatingPoint 8 24))
(declare-fun s17 () (_ FloatingPoint 8 24))
(declare-fun s7 (RoundingMode (_ FloatingPoint 8 24) (_ FloatingPoint 8 24)) (_ FloatingPoint 8 24))
(declare-fun s9 () (_ FloatingPoint 8 24))
(declare-fun s15 (RoundingMode (_ FloatingPoint 8 24) (_ FloatingPoint 8 24)) (_ FloatingPoint 8 24))
(declare-fun s12 () (_ FloatingPoint 8 24))
(declare-fun s6 () (_ FloatingPoint 8 24))
(declare-fun s16 (RoundingMode (_ FloatingPoint 8 24) (_ FloatingPoint 8 24)) (_ FloatingPoint 8 24))
(declare-fun s14 () (_ FloatingPoint 8 24))
(declare-fun s5 () (_ FloatingPoint 8 24))
(declare-fun s10 () Real)
(assert
(let ((?x33 (/ 3458764513820541.0 1152921504606846976.0)))
(let ((?x30 (s18 s13)))
(>= ?x30 ?x33))))
(assert
(let ((?x33 (/ 3458764513820541.0 1152921504606846976.0)))
(>= s8 ?x33)))
(assert
(let ((?x30 (s18 s13)))
(let ((?x46 (- ?x30 s8)))
(<= (ite (>= ?x46 0.0) ?x46 (- ?x46)) s2))))
(assert
(let ((?x54 (s18 s1)))
(let ((?x61 (- ?x54 s3)))
(<= (ite (>= ?x61 0.0) ?x61 (- ?x61)) s4))))
(assert
(= s17 (s0 roundNearestTiesToEven s1 s11)))
(assert
(= s9 (s7 roundNearestTiesToEven s17 s13)))
(assert
(= s6 (s15 roundNearestTiesToEven s9 s12)))
(assert
(= s5 (s16 roundNearestTiesToEven s6 s14)))
(assert
(let ((?x33 (/ 3458764513820541.0 1152921504606846976.0)))
(let ((?x89 (/ 6427752177035961.0 6427752177035961102167848369364650410088811975131171341205504.0)))
(let ((?x90 (+ (* (/ 1.0 16777216.0) s4) ?x89)))
(let ((?x95 (+ (* (/ 1.0 16777216.0) (/ (+ s4 ?x90) ?x33)) ?x89 (/ ?x90 ?x33))))
(let ((?x30 (s18 s13)))
(let ((?x37 (s18 s11)))
(let ((?x54 (s18 s1)))
(let ((?x55 (- ?x54 ?x37)))
(let ((?x81 (/ ?x55 ?x30)))
(let ((?x82 (- (s18 s9) ?x81)))
(let ((?x102 (ite (>= ?x82 0.0) ?x82 (- ?x82))))
(let (($x99 (not (or (fp.isNaN s5) (fp.isInfinite s5)))))
(=> $x99 (<= ?x102 ?x95)))))))))))))))
(assert
(let ((?x30 (s18 s13)))
(let ((?x37 (s18 s11)))
(let ((?x54 (s18 s1)))
(let ((?x55 (- ?x54 ?x37)))
(let ((?x81 (/ ?x55 ?x30)))
(let ((?x107 (ite (>= ?x81 0.0) ?x81 (- ?x81))))
(let ((?x82 (- (s18 s9) ?x81)))
(let ((?x102 (ite (>= ?x82 0.0) ?x82 (- ?x82))))
(<= (ite (>= (+ ?x82 ?x81) 0.0) (+ ?x82 ?x81) (- (+ ?x82 ?x81))) (+ ?x102 ?x107)))))))))))
(assert
(let ((?x33 (/ 3458764513820541.0 1152921504606846976.0)))
(let ((?x89 (/ 6427752177035961.0 6427752177035961102167848369364650410088811975131171341205504.0)))
(let ((?x90 (+ (* (/ 1.0 16777216.0) s4) ?x89)))
(let ((?x95 (+ (* (/ 1.0 16777216.0) (/ (+ s4 ?x90) ?x33)) ?x89 (/ ?x90 ?x33))))
(let ((?x118 (s18 s12)))
(let ((?x122 (ite (>= ?x118 0.0) ?x118 (- ?x118))))
(let ((?x134 (+ (* (/ 1.0 16777216.0) (* (+ (/ s4 ?x33) ?x95) ?x122)) ?x89)))
(let ((?x124 (s18 s14)))
(let ((?x132 (* (+ (/ s4 ?x33) ?x95) ?x122)))
(let ((?x85 (/ 1.0 16777216.0)))
(let ((?x141 (+ (* ?x85 (+ ?x132 ?x134 (ite (>= ?x124 0.0) ?x124 (- ?x124)))) ?x89 ?x134 (* ?x122 ?x95))))
(let ((?x30 (s18 s13)))
(let ((?x37 (s18 s11)))
(let ((?x54 (s18 s1)))
(let ((?x55 (- ?x54 ?x37)))
(let ((?x81 (/ ?x55 ?x30)))
(let ((?x119 (* ?x81 ?x118)))
(let ((?x125 (+ ?x119 ?x124)))
(let ((?x126 (- (s18 s5) ?x125)))
(let (($x99 (not (or (fp.isNaN s5) (fp.isInfinite s5)))))
(=> $x99 (<= (ite (>= ?x126 0.0) ?x126 (- ?x126)) ?x141)))))))))))))))))))))))
(assert
(let ((?x148 (* s4 (/ s2 (* (/ 3458764513820541.0 1152921504606846976.0) (/ 3458764513820541.0 1152921504606846976.0))))))
(let ((?x150 (+ ?x148 (/ s10 (/ 3458764513820541.0 1152921504606846976.0)))))
(let ((?x118 (s18 s12)))
(let ((?x122 (ite (>= ?x118 0.0) ?x118 (- ?x118))))
(let ((?x164 (* ?x122 ?x150)))
(let ((?x54 (s18 s1)))
(let ((?x61 (- ?x54 s3)))
(let ((?x144 (/ ?x61 s8)))
(let ((?x30 (s18 s13)))
(let ((?x37 (s18 s11)))
(let ((?x55 (- ?x54 ?x37)))
(let ((?x81 (/ ?x55 ?x30)))
(let ((?x145 (- ?x81 ?x144)))
(let ((?x153 (ite (>= ?x145 0.0) ?x145 (- ?x145))))
(let ((?x162 (* ?x122 ?x153)))
(<= ?x162 ?x164)))))))))))))))))
(assert
(let ((?x148 (* s4 (/ s2 (* (/ 3458764513820541.0 1152921504606846976.0) (/ 3458764513820541.0 1152921504606846976.0))))))
(let ((?x150 (+ ?x148 (/ s10 (/ 3458764513820541.0 1152921504606846976.0)))))
(let ((?x118 (s18 s12)))
(let ((?x122 (ite (>= ?x118 0.0) ?x118 (- ?x118))))
(let ((?x164 (* ?x122 ?x150)))
(let ((?x124 (s18 s14)))
(let ((?x54 (s18 s1)))
(let ((?x61 (- ?x54 s3)))
(let ((?x144 (/ ?x61 s8)))
(let ((?x155 (* ?x144 ?x118)))
(let ((?x30 (s18 s13)))
(let ((?x37 (s18 s11)))
(let ((?x55 (- ?x54 ?x37)))
(let ((?x81 (/ ?x55 ?x30)))
(let ((?x119 (* ?x81 ?x118)))
(let ((?x125 (+ ?x119 ?x124)))
(let ((?x167 (- ?x125 (+ ?x155 ?x124))))
(not (<= (ite (>= ?x167 0.0) ?x167 (- ?x167)) ?x164))))))))))))))))))))
(check-sat)
repro.c// repro.c: check the same formula twice, each time with a fresh solver on ONE context.
// The first check answers in ~0.02 s. The second runs in the legacy grobner (smt.arith.solver=2)
// and does not stop at rlimit=40000000.
//
// cc repro.c -I<z3>/src/api -o repro <libz3> && ./repro repro.smt2
#include <z3.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
static double now(void) { struct timespec t; clock_gettime(CLOCK_MONOTONIC, &t); return t.tv_sec + t.tv_nsec / 1e9; }
int main(int argc, char** argv) {
if (argc < 2) { fprintf(stderr, "usage: repro file.smt2 [checks=2]\n"); return 2; }
int n = argc > 2 ? atoi(argv[2]) : 2;
Z3_config cfg = Z3_mk_config();
Z3_context c = Z3_mk_context(cfg);
Z3_del_config(cfg);
Z3_ast_vector fs = Z3_parse_smtlib2_file(c, argv[1], 0, 0, 0, 0, 0, 0);
Z3_ast_vector_inc_ref(c, fs);
Z3_params p = Z3_mk_params(c);
Z3_params_inc_ref(c, p);
Z3_params_set_uint(c, p, Z3_mk_string_symbol(c, "smt.random_seed"), 0);
Z3_params_set_uint(c, p, Z3_mk_string_symbol(c, "rlimit"), 40000000);
Z3_params_set_uint(c, p, Z3_mk_string_symbol(c, "smt.arith.solver"), 2);
Z3_params_set_bool(c, p, Z3_mk_string_symbol(c, "smt.mbqi"), false);
for (int i = 1; i <= n; i++) {
Z3_solver s = Z3_mk_solver(c);
Z3_solver_inc_ref(c, s);
Z3_solver_set_params(c, s, p);
for (unsigned j = 0; j < Z3_ast_vector_size(c, fs); j++) Z3_solver_assert(c, s, Z3_ast_vector_get(c, fs, j));
double t0 = now();
Z3_lbool r = Z3_solver_check(c, s);
printf("check %d: %s in %.2f s%s%s\n", i, r == Z3_L_TRUE ? "sat" : r == Z3_L_FALSE ? "unsat" : "unknown", now() - t0,
r == Z3_L_UNDEF ? ", reason: " : "", r == Z3_L_UNDEF ? Z3_solver_get_reason_unknown(c, s) : "");
fflush(stdout);
Z3_solver_dec_ref(c, s);
}
return 0;
}Source: Z3Prover/z3