Skip to content

rqe-optimizer: check accuracy at the query's merge count - #154

Merged
zzylol merged 3 commits into
mainfrom
rqe-cost-merge-accuracy
Oct 6, 2026
Merged

zzylol merged 3 commits into
mainfrom
rqe-cost-merge-accuracy

Conversation

@zzylol

@zzylol zzylol commented Oct 6, 2026 •

Copy link
Copy Markdown
Collaborator

Part of #147 ("Accuracy at several merge counts").

Why

A query over lookback L served by windows of size x folds L/x window sketches. Before this PR the optimizer checked a candidate's accuracy against the single-instance score, measured on one sketch fed the whole stream. For sketches whose merge is lossy (KLL, the CMS/CountSketch top-k heaps, UnivMon), the folded result can be worse, so a candidate could be judged feasible when the query it serves is not. This decides feasibility, not just cost, so it matters most of the #147 items.

What

  • Export: scripts/export_rqe_optimizer_costs.sh also measures each row's accuracy after folding m shards, for m = 4, 16, 64, 256, 1024.
  • Table: AtomicCostEntry gains merge_accuracy: {m: scores}, using the same comparator keys as query_accuracy.
  • Optimizer: a candidate is eligible for a query only if the accuracy SLA holds at both measured counts bracketing the query's L/x.

How

  • Measurement. For each row and each m, the script runs one --operations merge --metrics accuracy --merge-shards m pass with --runs 1 --warmup-runs 0 on the same data, seed and dtype as the cost pass. Each run is written as a --flat row to rqe_merge_accuracy_grid.jsonl. These rows stay out of the raw file because flatten keeps one merge slot per cell, so different counts would collide.

  • Fixed total. One stream is split m ways, so a larger m means smaller shards. That matches the optimizer's choice: for a fixed query L, picking x splits the query's total over L/x windows.

  • Join. atomic-costs --merge-accuracy FILE attaches each merge row to its cell with same_cell (sketch, impl, config, workload; the identity flatten groups by).

    • A merge row that matches no cell is an error.
    • Two rows at one count skip the cell.
    • A cell missing any count present in the file is skipped, so the export's "0 skipped" check catches a partial grid.
  • Feasibility. accuracy_at(config, L/x) returns two score maps:

    • the largest measured count ≤ L/x, with count 1 = query_accuracy;
    • the smallest measured count ≥ L/x, or the largest measured count beyond the grid.

    accuracy_ok_for requires both to clear the SLA in the metric's own direction, because merge error is not monotone in m (see Evidence). is_eligible passes L/x.

  • Compatibility. merge_accuracy is #[serde(default)] and omitted when empty, so older tables still load and ASAPQuery's export, which doesn't measure it, is unchanged.

Before this PR

  • The table row had only query_accuracy, measured on one instance.
  • is_eligible checked that value whatever the query's L/x. With L = 1 h and x = 15 s, the query folds 240 windows, yet a KLL candidate was judged on its unmerged rank error.

After this PR

Each row carries, for example:

"query_accuracy": {"mean_rank_err": 0.00110, ...},
"merge_accuracy": {"4": {"mean_rank_err": 0.00186, ...}, "16": {"mean_rank_err": 0.00364, ...},
                   "64": {...}, "256": {...}, "1024": {...}}

The optimizer checks L/x = 1 against query_accuracy, L/x = 8 against counts 4 and 16, L/x = 240 against 64 and 256, and anything above 1024 against 1024.

Evidence

Reduced end-to-end run of this branch (--size 20000, runs 1; not the full export): 10 rows, 0 skipped, every row with all five counts. Score at each merge count:

Row Metric m=1 4 16 64 256 1024
kll-percall k=200 mean_rank_err 0.00110 0.00186 0.00364 0.00104 0.00108 0.00189
hydra-kll 3×128 mean_rank_err 0.00375 0.00401 0.00385 0.00378 0.00370 0.00381
univmon-topk 3×1024 precision_at_k 0.969 0.969 1.0 1.0 1.0 1.0
univmon-cardinality 3×1024 relative_error 0.426 0.438 0.424 0.432 0.427 0.429
dd α=0.01, hll lg_k=12 (own) identical at every m (lossless merge)
exact-sum, exact-increase relative_error_mean 0 at every m
cms-heap-topk 3×2048 precision_at_k 1.0 at every m

KLL is worst at m=16 and better again at 64. That is why one bracket alone isn't conservative and both are checked. At 20k items the shards are tiny, so the full export will give the real values.

Verification

  • Unit tests:
    • merge_runs_attach_to_their_cell_by_merge_count: rows join by cell and count; other cells are ignored.
    • two_merge_runs_at_one_count_skip_the_row: a duplicated export is refused.
    • a_row_missing_a_grid_merge_count_is_skipped: a partial grid fails the "0 skipped" check.
    • merge_accuracy_round_trips_and_is_omitted_when_empty: serde round trip; older tables and ASAPQuery's export are unchanged.
    • accuracy_at_reads_both_measured_merge_counts_bracketing_the_query: brackets at 0, 1, 2, exact counts, between counts and beyond the grid.
    • both_brackets_must_clear_a_higher_is_better_metric: precision at k with a bad count between two good ones.
    • eligibility_checks_accuracy_at_the_queries_merge_count: is_eligible uses L/x.
  • End-to-end tests: the reduced export above (exact, KLL, DD, HLL, CMS-heap top-k, UnivMon top-k and cardinality, HydraKLL).
  • Other checks: cargo test --workspace (514 passed), cargo clippy --workspace --all-targets -- -D warnings.

Architectural decisions

  • Fixed total, not fixed shard size. It matches choosing x for a fixed query. How large the real total λ·L/card(G) is belongs to the Tracking: make the rqe-optimizer cost export fit the per-phase cost model #147 size sweep.
  • Check both brackets, not the nearest one or the one above. Merge error is not monotone in m.
  • Above 1024, read 1024. Rejected alternative: making those candidates ineligible, which would also drop families whose merges lose nothing (HLL, DDSketch, exact accumulators).
  • Separate file joined at reduce time. Rejected alternative: changing flatten to keep several merge slots per cell.

Limitations and follow-up

  • The stream size is the benchmark's 1M (200k for spec rows), not the deployment's λ·L/card(G); the size sweep in Tracking: make the rqe-optimizer cost export fit the per-phase cost model #147 covers this.
  • On the 200k-row specs, 1024 shards of ceil(200000/1024) rows come out as 1021.
  • same_cell has no comparator. A cell scored by two comparators would be skipped loudly; no current row does that.
  • The export takes about 15 minutes longer; it needs one rerun after merge.

Human review — do not complete with an agent

  • The MVP boundary is correct.
  • New conceptual layers or public interfaces are necessary.
  • The before/after description matches the intended product behavior.
  • Human reviewer:
  • Decision and rationale:

🤖 Generated with Claude Code

@zzylol

zzylol commented Oct 6, 2026

Copy link
Copy Markdown
Collaborator Author

These commits cover the four review answers you approved:

  • Q1: fixed-total split. Unchanged: each merge count splits the same stream m ways. export_rqe_optimizer_costs.sh now has a comment saying why. For a fixed query L, choosing x spreads the query's total over L/x windows.
  • Q2: more merge counts. MERGE_ACCURACY_SHARDS=(4 16 64 256 1024). Above 1024, the optimizer still reads the largest measured count.
  • Q3: check both measured counts around L/x. accuracy_at now returns two score sets: the largest measured count ≤ L/x (count 1 means query_accuracy) and the smallest measured count ≥ L/x (or the largest if none is that big). accuracy_ok_for requires both to pass. In the eligibility test, count 16 is bad while 4 and 64 pass. L/x = 12, 20 and 60 are rejected because they sit next to 16. L/x = 2, 4 and 120 are accepted.
  • Q4: rows missing a count are skipped. With --merge-accuracy, a row that lacks any merge count found in the file is skipped as SkipReason::MissingField("merge_accuracy"), so the export's "0 skipped" check catches it. There is a new test, a_row_missing_a_grid_merge_count_is_skipped.

cargo test --workspace: 507 passed, 0 failed. cargo clippy --workspace --all-targets -- -D warnings is clean.

🤖 Generated with Claude Code

zzylol and others added 3 commits October 6, 2026 20:47
Export measures merge accuracy at 4, 16 and 64 shards as one --flat row per
count; atomic-costs joins them by cell (--merge-accuracy) into a new
AtomicCostEntry.merge_accuracy map, omitted when empty. Eligibility checks
accuracy at L/x merged windows: the smallest measured count >= L/x, else the
largest; one window or no measurements reads query_accuracy.

Part of #147

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Eligibility now requires the metric to pass at both measured merge counts
bracketing L/x (count 1 is query_accuracy), since error is not monotone in
the count. Export measures 4..1024 shards and documents the fixed-total
split. atomic-costs skips a row missing any merge count in the grid.

Part of #147

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…better bracket test

- docs: eligibility rule 3 checks both bracketing merge counts; replace the
  single-instance accuracy TODO.
- export: merge-accuracy passes score once, so --runs 1 --warmup-runs 0;
  note 1024 shards on 200k-row specs come out as 1021.
- same_cell: note it has no comparator.
- test: both brackets must clear a higher-is-better metric.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@zzylol
zzylol force-pushed the rqe-cost-merge-accuracy branch from 2b63cc7 to 3234bd4 Compare October 6, 2026 20:49
@zzylol zzylol changed the title rqe-optimizer: accuracy at the query's merge count rqe-optimizer: check accuracy at the query's merge count Oct 6, 2026
@zzylol
zzylol merged commit 373ea6b into main Oct 6, 2026
2 checks passed
zzylol added a commit that referenced this pull request Oct 6, 2026
Runner for ASAPQuery#777's comparison on main's planner: one metric per
stream of the evaluation table, ASAP by milp::minimize, PerQuery by
minimize per RQE, AutoSketch by autosketch::plan, all scored by
analytical_cost_model::score (#145) at #777's weights (CPU only; Fargate
$/vCPU-h and $/GB-h) and SLA grid {0.01, 0.1, 1, 10} ms and none. Each
RQE's accuracy rides in the cost entries: acc@<id> (with merged values in
merge_accuracy, #154) for ASAP, as@<id> for AutoSketch.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant