Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 24 additions & 24 deletions aqpbm-cli/src/atomic_costs_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ use std::io::Read;
use anyhow::{Context, Result};
use clap::Parser;

use aqpbm_core::{reduce_all, same_cell, MergedRecord};
use std::collections::BTreeMap;

use aqpbm_core::{reduce_all, MergedRecord};

#[derive(Parser, Debug)]
pub struct AtomicCostsArgs {
Expand All @@ -19,11 +21,21 @@ pub struct AtomicCostsArgs {
/// Output path for the reduced table (JSON array). Defaults to stdout.
#[arg(short, long)]
output: Option<String>,
/// `--flat` merge-accuracy rows (`--operations merge --metrics accuracy`),
/// one per cell and `--merge-shards`. Each lands in its cell's
/// `merge_accuracy`; one that matches no input row is an error.
#[arg(long)]
merge_accuracy: Option<String>,
/// `VARIANT=METRIC`, once per variant: the `query_accuracy` key that is
/// that variant's `accuracy_metric`. A row whose variant has none, or
/// whose scores lack it, is skipped.
#[arg(long = "accuracy-metric", value_name = "VARIANT=METRIC", required = true,
value_parser = parse_accuracy_metric)]
accuracy_metrics: Vec<(String, String)>,
}

fn parse_accuracy_metric(arg: &str) -> Result<(String, String), String> {
match arg.split_once('=') {
Some((variant, metric)) if !variant.is_empty() && !metric.is_empty() => {
Ok((variant.to_string(), metric.to_string()))
}
_ => Err(format!("expected VARIANT=METRIC, got {arg}")),
}
}

fn read_records(path: &str) -> Result<Vec<MergedRecord>> {
Expand All @@ -46,25 +58,13 @@ fn read_records(path: &str) -> Result<Vec<MergedRecord>> {

pub fn run(args: AtomicCostsArgs) -> Result<()> {
let records = read_records(&args.input)?;
let merge_runs = match args.merge_accuracy.as_deref() {
Some(path) => read_records(path)?,
None => Vec::new(),
};
if let Some(run) = merge_runs
.iter()
.find(|run| !records.iter().any(|record| same_cell(record, run)))
{
anyhow::bail!(
"merge-accuracy row {} {} matches no input row",
run.sketch,
run.sketch_config
.as_ref()
.map(|c| c.to_string())
.unwrap_or_default()
);
let mut accuracy_metrics = BTreeMap::new();
for (variant, metric) in args.accuracy_metrics {
if let Some(earlier) = accuracy_metrics.insert(variant.clone(), metric.clone()) {
anyhow::bail!("--accuracy-metric gives {variant} twice ({earlier}, {metric})");
}
}

let (table, skipped) = reduce_all(&records, &merge_runs);
let (table, skipped) = reduce_all(&records, &accuracy_metrics);
for (i, reason) in &skipped {
let record = &records[*i];
eprintln!(
Expand Down
46 changes: 44 additions & 2 deletions aqpbm-cli/tests/merge_accuracy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,18 @@ use std::process::Command;
/// One `--flat` record measuring both the single sketch's and the merged
/// sketch's accuracy over the same small fixed-seed stream.
fn query_and_merge_accuracy(variant: &str, config: &str, dataset: &[&str]) -> serde_json::Value {
run_merge_accuracy(variant, "lib", &["--config", config], dataset)
}

fn run_merge_accuracy(
variant: &str,
library: &str,
config: &[&str],
dataset: &[&str],
) -> serde_json::Value {
let out = Command::new(env!("CARGO_BIN_EXE_approxbench"))
.args(["sketchbench", "--variant", variant, "--library", "lib"])
.args(["--config", config])
.args(["sketchbench", "--variant", variant, "--library", library])
.args(config)
.args(["--operations", "query,merge", "--metrics", "accuracy"])
.args(["--merge-shards", "4", "--runs", "1", "--warmup-runs", "0"])
.args(["--size", "20000", "--seed", "7", "--flat"])
Expand Down Expand Up @@ -70,3 +79,36 @@ fn merged_kll_scores_finite() {
.unwrap_or_else(|| panic!("no merged mean_rank_err in {r}"));
assert!(err.is_finite(), "{err}");
}

/// Exact accumulators merge without loss (the optimizer relies on it and no
/// longer measures merged accuracy per row, #174): every merged score is the
/// single accumulator's, and the worst group's error is 0. Increase reads
/// counters, so its shard boundaries split counter runs.
#[test]
fn merged_exact_accumulators_score_exactly_like_one() {
let spec = |name: &str| format!("{}/../configs/datagen/{name}", env!("CARGO_MANIFEST_DIR"));
for (variant, comparator, columns) in [
("exact-sum", "sum-or-count", "hydra_columns.yaml"),
("exact-min", "min", "hydra_columns.yaml"),
("exact-max", "max", "hydra_columns.yaml"),
("exact-increase", "rate-or-increase", "counter_columns.yaml"),
("exact-delta-set", "key-set", "hydra_columns.yaml"),
] {
let path = spec(columns);
let r = run_merge_accuracy(
variant,
"exact",
&[],
&[
"--comparator",
comparator,
"--spec",
&path,
"--dtype",
"i64",
],
);
assert_eq!(r["merge_accuracy"], r["query_accuracy"], "{variant}: {r}");
assert_eq!(r["merge_accuracy"]["relative_error"], 0.0, "{variant}: {r}");
}
}
Loading
Loading