From ff5b22144b347d14310af2671daba4276606d935 Mon Sep 17 00:00:00 2001 From: zz_y Date: Wed, 7 Oct 2026 21:42:38 +0000 Subject: [PATCH 1/4] rqe-optimizer: bracket the shape parameter within each K MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The cost table's shape adds one (θ = 1.1, K = 1e4) point to the curve grid (#178). bracketing_points bracketed θ over every point's θ and K over every point's K, so a θ = 1.1 shape at another K asked for a (1.1, K) point that was never measured and got no accuracy. small_problem's top_endpoints_1h (θ = 1.1, K = 1e3) was unservable. Now θ is bracketed within each K, and K among the K values that can bracket θ. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01FhcWJExZcmVqS6r6rEtjis --- rqe-optimizer/src/saturation.rs | 78 +++++++++++++++++++++++++++------ 1 file changed, 64 insertions(+), 14 deletions(-) diff --git a/rqe-optimizer/src/saturation.rs b/rqe-optimizer/src/saturation.rs index a499ec04..204ab907 100644 --- a/rqe-optimizer/src/saturation.rs +++ b/rqe-optimizer/src/saturation.rs @@ -588,27 +588,51 @@ impl SaturationCurves { .filter(|point| point.params == params) .collect(); let quantile = points.first()?.distinct_keys().is_none(); - let shape_params = if quantile { - bracket(points.iter().map(|p| p.shape_param()), shape.tail_index)? + let target = if quantile { + shape.tail_index } else { - bracket(points.iter().map(|p| p.shape_param()), shape.zipf_s)? + shape.zipf_s }; - let keys = if quantile { + // The grid needn't be a full cross: the cost table's shape adds one + // (θ, K) point. Bracket the shape parameter within each K, and K + // among the K values that can bracket it, so a θ measured at one K + // only isn't asked for at another. + let mut by_keys: BTreeMap, Vec<&GridPoint>> = BTreeMap::new(); + for point in points { + by_keys + .entry(point.distinct_keys().map(f64::to_bits)) + .or_default() + .push(point); + } + let mut params_at: BTreeMap, Vec> = BTreeMap::new(); + for (keys, at) in &by_keys { + if let Some(params) = bracket(at.iter().map(|p| p.shape_param()), target) { + params_at.insert(*keys, params); + } + } + let keys: Vec> = if quantile { vec![None] } else { - let keys = points.iter().filter_map(|p| p.distinct_keys()); - bracket(keys, shape.distinct_keys)? + let measured = params_at.keys().filter_map(|k| k.map(f64::from_bits)); + bracket(measured, shape.distinct_keys)? .into_iter() - .map(Some) + .map(|k| Some(k.to_bits())) .collect() }; - let bracketing: Vec<&GridPoint> = points - .into_iter() - .filter(|p| { - shape_params.contains(&p.shape_param()) && keys.contains(&p.distinct_keys()) - }) - .collect(); - (bracketing.len() == shape_params.len() * keys.len()).then_some(bracketing) + let mut bracketing = Vec::new(); + for key in keys { + let params = params_at.get(&key)?; + let at: Vec<&GridPoint> = by_keys[&key] + .iter() + .copied() + .filter(|p| params.contains(&p.shape_param())) + .collect(); + if at.len() != params.len() { + return None; + } + bracketing.extend(at); + } + Some(bracketing) } } @@ -810,6 +834,32 @@ mod tests { ); } + /// The cost table's shape adds one (θ = 1.1, K = 1e4) point to a grid + /// of θ ∈ {1.0, 1.2}: θ = 1.1 at K = 1e3 still brackets 1.0 and 1.2, a + /// θ = 1.0 at K between 1e3 and 1e5 skips the K that has θ = 1.1 only, + /// and θ = 1.1 at K = 1e4 reads the extra point itself. + #[test] + fn a_sparse_extra_point_brackets_only_where_measured() { + let mut curves = curves(); + let mut extra = curves.points_by_sketch[TOPK][1].clone(); + extra.shape = MeasuredShape::Zipf { + skew: 1.1, + keys: 1e4, + }; + extra.curve = vec![(1e3, 0.5, 0.0), (1e4, 0.5, 0.0), (1e5, 0.5, 0.0)]; + curves.points_by_sketch.get_mut(TOPK).unwrap().push(extra); + let r = topk_raqe(1_000_000); + let d = deployment(TOPK, 1_000_000); + let at = |theta, keys| curves.accuracy(&r, &d, &workload(10, shape(theta, keys))); + // Same as without the extra point: θ = 1.0 at 1e4 items is worse. + assert_eq!(at(1.1, 1e3), Some(0.95 - 0.05)); + // K = 5e3 brackets 1e3 and 1e5, not the 1e4 that has θ = 1.1 only; + // θ = 1.0 at K = 1e5 never saturates, but reads 0.85 at 1e4. + assert_eq!(at(1.0, 5e3), Some(0.95 - 0.1)); + // The extra point itself. + assert_eq!(at(1.1, 1e4), Some(0.5)); + } + #[test] fn a_shape_outside_the_grid_or_without_a_fit_has_no_accuracy() { let r = topk_raqe(1_000_000); From 03109d66776eb6095ceba5c4801706233d2dbed0 Mon Sep 17 00:00:00 2001 From: zz_y Date: Wed, 7 Oct 2026 21:44:58 +0000 Subject: [PATCH 2/4] rqe-optimizer: one pass per K for bracketing; equal shapes all count From review on #186: - Gather each K's bracketing points in one pass; points with equal shapes (10000 vs 1e4 in two CSVs) all count instead of failing a length check. - Quantile when every point is Pareto, not by the first point. - Doc: the K bracket falls back to neighbouring Ks that have the parameter (conservative) rather than returning None. - The test's extra point carries no merge curves of the point it copies. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01FhcWJExZcmVqS6r6rEtjis --- rqe-optimizer/src/saturation.rs | 64 +++++++++++++++------------------ 1 file changed, 29 insertions(+), 35 deletions(-) diff --git a/rqe-optimizer/src/saturation.rs b/rqe-optimizer/src/saturation.rs index 204ab907..f75ccfc5 100644 --- a/rqe-optimizer/src/saturation.rs +++ b/rqe-optimizer/src/saturation.rs @@ -572,9 +572,14 @@ impl SaturationCurves { check } - /// Every grid point of `config` bracketing `shape` (Q8 of #156), or - /// `None` when `config` isn't in the grid, `shape` lies outside it, or a - /// bracketing point is missing. + /// Every grid point of `config` bracketing `shape` (Q8 of #156): the + /// shape parameter (θ, or the Pareto `a`) bracketed within each K, and K + /// bracketed among the K values that can bracket the parameter. The grid + /// needn't be a full cross (the cost table's shape adds one (θ, K) + /// point), so a θ measured at one K only isn't asked for at another; the + /// bracket falls back to the neighbouring Ks that have it, which is + /// conservative. Points with equal shapes all count. `None` when + /// `config` isn't in the grid or `shape` lies outside it. fn bracketing_points( &self, config: &AtomicCostEntry, @@ -587,52 +592,40 @@ impl SaturationCurves { .iter() .filter(|point| point.params == params) .collect(); - let quantile = points.first()?.distinct_keys().is_none(); + let quantile = points.iter().all(|p| p.distinct_keys().is_none()); let target = if quantile { shape.tail_index } else { shape.zipf_s }; - // The grid needn't be a full cross: the cost table's shape adds one - // (θ, K) point. Bracket the shape parameter within each K, and K - // among the K values that can bracket it, so a θ measured at one K - // only isn't asked for at another. + // Per K (none for quantiles): the points bracketing the parameter. let mut by_keys: BTreeMap, Vec<&GridPoint>> = BTreeMap::new(); - for point in points { + for point in &points { by_keys .entry(point.distinct_keys().map(f64::to_bits)) .or_default() .push(point); } - let mut params_at: BTreeMap, Vec> = BTreeMap::new(); - for (keys, at) in &by_keys { - if let Some(params) = bracket(at.iter().map(|p| p.shape_param()), target) { - params_at.insert(*keys, params); + let mut bracketed: BTreeMap, Vec<&GridPoint>> = BTreeMap::new(); + for (keys, at) in by_keys { + if let Some(values) = bracket(at.iter().map(|p| p.shape_param()), target) { + let at: Vec<&GridPoint> = at + .into_iter() + .filter(|p| values.contains(&p.shape_param())) + .collect(); + bracketed.insert(keys, at); } } - let keys: Vec> = if quantile { - vec![None] - } else { - let measured = params_at.keys().filter_map(|k| k.map(f64::from_bits)); - bracket(measured, shape.distinct_keys)? - .into_iter() - .map(|k| Some(k.to_bits())) - .collect() - }; - let mut bracketing = Vec::new(); - for key in keys { - let params = params_at.get(&key)?; - let at: Vec<&GridPoint> = by_keys[&key] - .iter() - .copied() - .filter(|p| params.contains(&p.shape_param())) - .collect(); - if at.len() != params.len() { - return None; - } - bracketing.extend(at); + if quantile { + return bracketed.remove(&None); } - Some(bracketing) + let measured = bracketed.keys().filter_map(|k| k.map(f64::from_bits)); + let keys = bracket(measured, shape.distinct_keys)?; + Some( + keys.into_iter() + .flat_map(|k| bracketed.remove(&Some(k.to_bits())).unwrap_or_default()) + .collect(), + ) } } @@ -847,6 +840,7 @@ mod tests { keys: 1e4, }; extra.curve = vec![(1e3, 0.5, 0.0), (1e4, 0.5, 0.0), (1e5, 0.5, 0.0)]; + extra.merged = BTreeMap::new(); curves.points_by_sketch.get_mut(TOPK).unwrap().push(extra); let r = topk_raqe(1_000_000); let d = deployment(TOPK, 1_000_000); From da9bf0709da3315e378c64c3023483b6606f3d06 Mon Sep 17 00:00:00 2001 From: zz_y Date: Wed, 7 Oct 2026 21:49:23 +0000 Subject: [PATCH 3/4] study_saturation: the cost shape as a full grid row and column; keep the rectangular bracket MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit From the second review of #186: rather than bracketing a sparse grid, keep the grid a full cross. --cost-shape now adds θ = 1.1 and K = 1e4 to --thetas and --cardinalities (and a = 2 to --alphas), so θ = 1.1 is measured at every K and K = 1e4 at every θ. bracketing_points goes back to the rectangular bracket with its completeness check: a hole in the grid leaves the shape without accuracy instead of bridging to farther points (accuracy needn't be monotone in K). Quantile is decided from every point, not the first. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01FhcWJExZcmVqS6r6rEtjis --- docs/rqe_sketch_deployment_v1.md | 8 ++- docs/saturation_study.md | 9 +-- rqe-optimizer/src/saturation.rs | 99 +++++++++++++------------------- scripts/study_saturation.py | 17 +++--- scripts/test_study_saturation.py | 4 +- 5 files changed, 62 insertions(+), 75 deletions(-) diff --git a/docs/rqe_sketch_deployment_v1.md b/docs/rqe_sketch_deployment_v1.md index 7fb1dc66..dbb4b9d9 100644 --- a/docs/rqe_sketch_deployment_v1.md +++ b/docs/rqe_sketch_deployment_v1.md @@ -277,9 +277,11 @@ each curve: a row's `measured_at` (items, Zipf θ and population, or Pareto row whose `accuracy_metric` is not its family's or not its curve's `error_metric`, whose configuration is not in the grid, or whose accuracy disagrees with the curve at `measured_at` (beyond 3 seed standard errors plus -5%). The study runs every configuration at the cost table's shape too, so -every sketch row has a grid point; a row without one (a study run with -`--no-cost-shape`) is reported as unchecked. `small_problem` refuses to plan +5%). The study adds the cost table's shape to the grid as a full row and +column (θ = 1.1 at every K, K = 1e4 at every θ), so every sketch row has a +grid point and the grid stays a full cross. A missing bracketing point +leaves a shape without accuracy. A row without a grid point (a study run +with `--no-cost-shape`) is reported as unchecked. `small_problem` refuses to plan when any row disagrees. For lower-is-better metrics, passing means `measured <= tol_i`. For diff --git a/docs/saturation_study.md b/docs/saturation_study.md index 02a072cc..c7743d47 100644 --- a/docs/saturation_study.md +++ b/docs/saturation_study.md @@ -80,10 +80,11 @@ atomic-costs` to `rqe_atomic_costs.json` (#174). Top-k configs are measured at h and rqe-optimizer interpolates between them. The accuracy grid measures top-k at the default heap, `k = 32`. Put it under the curves' directory as `optimizer_cost/`, where `rqe-optimizer` reads it. The accuracy -grid also runs every configuration at that shape (unless `--no-cost-shape`), -so each cost-table row is a point on a curve and the synthetic evaluation, -which uses the same data, reads an exact curve rather than the worse -neighbouring grid point. +grid also covers that shape (unless `--no-cost-shape`): θ = 1.1 and K = 1e4 +join the grid as a full row and column (a = 2 joins the Pareto values), so +the grid stays a full cross, each cost-table row is a point on a curve, and +the synthetic evaluation, which uses the same data, reads an exact curve +rather than the worse neighbouring grid point. `--cost-rows 3` times only the rows=3 configs of CMS, CountSketch and CMS-heap top-k (all four cols); HLL, KLL and DDSketch are timed at every diff --git a/rqe-optimizer/src/saturation.rs b/rqe-optimizer/src/saturation.rs index f75ccfc5..8c076689 100644 --- a/rqe-optimizer/src/saturation.rs +++ b/rqe-optimizer/src/saturation.rs @@ -572,14 +572,9 @@ impl SaturationCurves { check } - /// Every grid point of `config` bracketing `shape` (Q8 of #156): the - /// shape parameter (θ, or the Pareto `a`) bracketed within each K, and K - /// bracketed among the K values that can bracket the parameter. The grid - /// needn't be a full cross (the cost table's shape adds one (θ, K) - /// point), so a θ measured at one K only isn't asked for at another; the - /// bracket falls back to the neighbouring Ks that have it, which is - /// conservative. Points with equal shapes all count. `None` when - /// `config` isn't in the grid or `shape` lies outside it. + /// Every grid point of `config` bracketing `shape` (Q8 of #156), or + /// `None` when `config` isn't in the grid, `shape` lies outside it, or a + /// bracketing point is missing. fn bracketing_points( &self, config: &AtomicCostEntry, @@ -592,40 +587,31 @@ impl SaturationCurves { .iter() .filter(|point| point.params == params) .collect(); + if points.is_empty() { + return None; + } let quantile = points.iter().all(|p| p.distinct_keys().is_none()); - let target = if quantile { - shape.tail_index + let shape_params = if quantile { + bracket(points.iter().map(|p| p.shape_param()), shape.tail_index)? } else { - shape.zipf_s + bracket(points.iter().map(|p| p.shape_param()), shape.zipf_s)? }; - // Per K (none for quantiles): the points bracketing the parameter. - let mut by_keys: BTreeMap, Vec<&GridPoint>> = BTreeMap::new(); - for point in &points { - by_keys - .entry(point.distinct_keys().map(f64::to_bits)) - .or_default() - .push(point); - } - let mut bracketed: BTreeMap, Vec<&GridPoint>> = BTreeMap::new(); - for (keys, at) in by_keys { - if let Some(values) = bracket(at.iter().map(|p| p.shape_param()), target) { - let at: Vec<&GridPoint> = at - .into_iter() - .filter(|p| values.contains(&p.shape_param())) - .collect(); - bracketed.insert(keys, at); - } - } - if quantile { - return bracketed.remove(&None); - } - let measured = bracketed.keys().filter_map(|k| k.map(f64::from_bits)); - let keys = bracket(measured, shape.distinct_keys)?; - Some( - keys.into_iter() - .flat_map(|k| bracketed.remove(&Some(k.to_bits())).unwrap_or_default()) - .collect(), - ) + let keys = if quantile { + vec![None] + } else { + let keys = points.iter().filter_map(|p| p.distinct_keys()); + bracket(keys, shape.distinct_keys)? + .into_iter() + .map(Some) + .collect() + }; + let bracketing: Vec<&GridPoint> = points + .into_iter() + .filter(|p| { + shape_params.contains(&p.shape_param()) && keys.contains(&p.distinct_keys()) + }) + .collect(); + (bracketing.len() == shape_params.len() * keys.len()).then_some(bracketing) } } @@ -827,31 +813,24 @@ mod tests { ); } - /// The cost table's shape adds one (θ = 1.1, K = 1e4) point to a grid - /// of θ ∈ {1.0, 1.2}: θ = 1.1 at K = 1e3 still brackets 1.0 and 1.2, a - /// θ = 1.0 at K between 1e3 and 1e5 skips the K that has θ = 1.1 only, - /// and θ = 1.1 at K = 1e4 reads the extra point itself. + /// A hole in the grid (a bracketing point the study didn't measure) + /// leaves the shape unknown rather than bridging to farther points: + /// accuracy needn't be monotone in K. #[test] - fn a_sparse_extra_point_brackets_only_where_measured() { + fn a_hole_in_the_grid_has_no_accuracy() { let mut curves = curves(); - let mut extra = curves.points_by_sketch[TOPK][1].clone(); - extra.shape = MeasuredShape::Zipf { - skew: 1.1, - keys: 1e4, - }; - extra.curve = vec![(1e3, 0.5, 0.0), (1e4, 0.5, 0.0), (1e5, 0.5, 0.0)]; - extra.merged = BTreeMap::new(); - curves.points_by_sketch.get_mut(TOPK).unwrap().push(extra); + // Drop θ = 1.2, K = 1e3. + curves.points_by_sketch.get_mut(TOPK).unwrap().remove(1); let r = topk_raqe(1_000_000); let d = deployment(TOPK, 1_000_000); - let at = |theta, keys| curves.accuracy(&r, &d, &workload(10, shape(theta, keys))); - // Same as without the extra point: θ = 1.0 at 1e4 items is worse. - assert_eq!(at(1.1, 1e3), Some(0.95 - 0.05)); - // K = 5e3 brackets 1e3 and 1e5, not the 1e4 that has θ = 1.1 only; - // θ = 1.0 at K = 1e5 never saturates, but reads 0.85 at 1e4. - assert_eq!(at(1.0, 5e3), Some(0.95 - 0.1)); - // The extra point itself. - assert_eq!(at(1.1, 1e4), Some(0.5)); + assert_eq!( + curves.accuracy(&r, &d, &workload(10, shape(1.1, 1e3))), + None + ); + // θ = 1.0 at K = 1e3 is still measured. + assert!(curves + .accuracy(&r, &d, &workload(10, shape(1.0, 1e3))) + .is_some()); } #[test] diff --git a/scripts/study_saturation.py b/scripts/study_saturation.py index 8727d8b8..b5fde9ed 100644 --- a/scripts/study_saturation.py +++ b/scripts/study_saturation.py @@ -23,8 +23,9 @@ accuracy pass, plus the exact accumulators, and reduces them to rqe_atomic_costs.json, the table rqe-optimizer loads; every row names its accuracy_metric. It runs no curves and ignores the grid arguments. -The grid also holds every config at the optimizer-cost shape (Zipf 1.1 over -1e4 keys, Pareto a = 2) unless --no-cost-shape. +The grid also holds every config at the optimizer-cost shape unless +--no-cost-shape: θ = 1.1 and K = 1e4 join --thetas and --cardinalities (a full +row and column, so the grid stays a full cross) and a = 2 joins --alphas. --resume keeps the complete curves of an interrupted accuracy run; --cost-rows 3 times only the rows=3 Vector2D configs. @@ -462,11 +463,13 @@ def main(): points.append((family, variant, config, comparator, metric, dataset, "pareto", alpha, "")) continue - # Plus the cost table's shape, so its rows are points on a curve. - shapes = [(t, k) for t in thetas for k in cardinalities] - if args.cost_shape and (COST_THETA, COST_KEYS) not in shapes: - shapes.append((COST_THETA, COST_KEYS)) - for theta, k in shapes: + # Plus the cost table's shape as a full row and column (θ = 1.1 at + # every K, K = 1e4 at every θ), so its rows are points on a curve + # and the grid stays a full cross for the optimizer's bracketing. + grid_thetas = thetas + [COST_THETA] * (args.cost_shape and COST_THETA not in thetas) + grid_keys = cardinalities + [COST_KEYS] * ( + args.cost_shape and COST_KEYS not in cardinalities) + for theta, k in [(t, k) for t in grid_thetas for k in grid_keys]: dataset = ["--dataset", "zipf", "--zipf-s", str(theta), "--cardinality", str(k), "--dtype", "i64"] points.append((family, variant, config, comparator, metric, diff --git a/scripts/test_study_saturation.py b/scripts/test_study_saturation.py index 6df75564..ecd3eeec 100644 --- a/scripts/test_study_saturation.py +++ b/scripts/test_study_saturation.py @@ -422,7 +422,9 @@ def test_the_grid_also_runs_the_optimizer_cost_shape(self): ], check=True, capture_output=True) with open(os.path.join(d, "saturation_curve.csv"), newline="") as f: shapes = {(r["param"], r["cardinality"]) for r in csv.DictReader(f)} - self.assertEqual(shapes, {("1.0", "1000"), ("1.1", "10000")}) + # A full row and column: θ = 1.1 at every K, K = 1e4 at every θ. + self.assertEqual(shapes, {("1.0", "1000"), ("1.0", "10000"), + ("1.1", "1000"), ("1.1", "10000")}) # Logs every call; atomic-costs reports `--rows` rows kept, 0 skipped. From 8142fa4225ce2ecfbb0a80b4f7371579a61da388 Mon Sep 17 00:00:00 2001 From: zz_y Date: Wed, 7 Oct 2026 21:52:17 +0000 Subject: [PATCH 4/4] rqe-optimizer: refuse a curve grid that isn't a full cross, naming the holes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit From the third review of #186: - SaturationCurves::load refuses curves whose Zipf grid isn't a full cross of its θ and K values, naming the missing (θ, K). A hole used to leave every shape around it silently without accuracy. - Quantile is decided by the first point again (the grids hold one distribution per config). - The hole test removes the point by shape, not position; a study test checks a grid that already holds the cost shape adds nothing. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01FhcWJExZcmVqS6r6rEtjis --- docs/rqe_sketch_deployment_v1.md | 6 ++- rqe-optimizer/src/saturation.rs | 78 ++++++++++++++++++++++++++++++-- scripts/test_study_saturation.py | 12 +++++ 3 files changed, 89 insertions(+), 7 deletions(-) diff --git a/docs/rqe_sketch_deployment_v1.md b/docs/rqe_sketch_deployment_v1.md index dbb4b9d9..63efa770 100644 --- a/docs/rqe_sketch_deployment_v1.md +++ b/docs/rqe_sketch_deployment_v1.md @@ -279,8 +279,10 @@ row whose `accuracy_metric` is not its family's or not its curve's disagrees with the curve at `measured_at` (beyond 3 seed standard errors plus 5%). The study adds the cost table's shape to the grid as a full row and column (θ = 1.1 at every K, K = 1e4 at every θ), so every sketch row has a -grid point and the grid stays a full cross. A missing bracketing point -leaves a shape without accuracy. A row without a grid point (a study run +grid point and the grid stays a full cross. `SaturationCurves::load` +refuses curves whose Zipf grid isn't a full cross of its θ and K values and +names the missing points: a hole would leave every shape around it without +accuracy (for example, curves from `--points-from` or a narrowed `--resume`). A row without a grid point (a study run with `--no-cost-shape`) is reported as unchecked. `small_problem` refuses to plan when any row disagrees. diff --git a/rqe-optimizer/src/saturation.rs b/rqe-optimizer/src/saturation.rs index 8c076689..6ac729bc 100644 --- a/rqe-optimizer/src/saturation.rs +++ b/rqe-optimizer/src/saturation.rs @@ -380,6 +380,36 @@ impl SaturationCurves { } points_by_sketch.entry(sketch).or_default().push(point); } + // The optimizer brackets a shape between grid points, so each + // config's Zipf grid must be a full cross of its θ and K values: a + // hole would quietly leave every shape around it without accuracy. + for (sketch, points) in &points_by_sketch { + let mut by_config: BTreeMap> = BTreeMap::new(); + for point in points { + if let MeasuredShape::Zipf { skew, keys } = point.shape { + by_config + .entry(format!("{:?}", point.params)) + .or_default() + .insert((skew.to_bits(), keys.to_bits())); + } + } + for (config, shapes) in by_config { + let thetas: BTreeSet = shapes.iter().map(|&(t, _)| t).collect(); + let keys: BTreeSet = shapes.iter().map(|&(_, k)| k).collect(); + let missing: Vec<(f64, f64)> = thetas + .iter() + .flat_map(|&t| keys.iter().map(move |&k| (t, k))) + .filter(|shape| !shapes.contains(shape)) + .map(|(t, k)| (f64::from_bits(t), f64::from_bits(k))) + .collect(); + if !missing.is_empty() { + return Err(invalid(format!( + "{sketch} {config}: the grid isn't a full cross of θ and K; \ + missing (θ, K) {missing:?}. Rerun the study's accuracy grid for them" + ))); + } + } + } Ok(Self { points_by_sketch }) } @@ -587,10 +617,7 @@ impl SaturationCurves { .iter() .filter(|point| point.params == params) .collect(); - if points.is_empty() { - return None; - } - let quantile = points.iter().all(|p| p.distinct_keys().is_none()); + let quantile = points.first()?.distinct_keys().is_none(); let shape_params = if quantile { bracket(points.iter().map(|p| p.shape_param()), shape.tail_index)? } else { @@ -820,7 +847,15 @@ mod tests { fn a_hole_in_the_grid_has_no_accuracy() { let mut curves = curves(); // Drop θ = 1.2, K = 1e3. - curves.points_by_sketch.get_mut(TOPK).unwrap().remove(1); + let hole = MeasuredShape::Zipf { + skew: 1.2, + keys: 1e3, + }; + curves + .points_by_sketch + .get_mut(TOPK) + .unwrap() + .retain(|p| p.shape != hole); let r = topk_raqe(1_000_000); let d = deployment(TOPK, 1_000_000); assert_eq!( @@ -1136,6 +1171,39 @@ mod tests { assert_eq!(check(&[misnamed]).mismatched.len(), 1); } + #[test] + fn load_refuses_a_grid_that_isnt_a_full_cross() { + let dir = std::env::temp_dir().join(format!("rqe-cross-{}", std::process::id())); + let header = "family,sketch,config,dist,param,cardinality"; + let points = ["1.0,1000", "1.2,1000", "1.0,100000"] + .map(|shape| format!("topk,{TOPK},rows=3 cols=1024,zipf,{shape}")); + for run in RUN_DIRS { + std::fs::create_dir_all(dir.join(run)).unwrap(); + let summary: String = points + .iter() + .map(|p| format!("{p},1000,0.9,precision_at_k\n")) + .collect(); + let curve: String = points.iter().map(|p| format!("{p},1000,0.9,0\n")).collect(); + std::fs::write( + dir.join(run).join("saturation.csv"), + format!("{header},n_sat,final_error,error_metric\n{summary}"), + ) + .unwrap(); + std::fs::write( + dir.join(run).join("saturation_curve.csv"), + format!("{header},n,seed_mean_error,seed_se\n{curve}"), + ) + .unwrap(); + } + let loaded = SaturationCurves::load(&dir); + std::fs::remove_dir_all(&dir).unwrap(); + let err = loaded.unwrap_err().to_string(); + assert!( + err.contains("full cross") && err.contains("(1.2, 100000.0)"), + "{err}" + ); + } + #[test] fn load_takes_a_point_in_both_runs_from_the_1e9_run() { let dir = std::env::temp_dir().join(format!("rqe-saturation-{}", std::process::id())); diff --git a/scripts/test_study_saturation.py b/scripts/test_study_saturation.py index ecd3eeec..a2ab0742 100644 --- a/scripts/test_study_saturation.py +++ b/scripts/test_study_saturation.py @@ -426,6 +426,18 @@ def test_the_grid_also_runs_the_optimizer_cost_shape(self): self.assertEqual(shapes, {("1.0", "1000"), ("1.0", "10000"), ("1.1", "1000"), ("1.1", "10000")}) + def test_a_grid_that_already_has_the_cost_shape_adds_nothing(self): + with tempfile.TemporaryDirectory() as d: + subprocess.run([ + sys.executable, SCRIPT, "--binary", fake_binary(d), "--out", d, + "--phase", "accuracy", "--families", "cardinality", "--one-config", + "--thetas", "1.1", "--cardinalities", "10000", "--n-max", "1e4", + "--per-decade", "1", "--seeds", "1", + ], check=True, capture_output=True) + with open(os.path.join(d, "saturation.csv"), newline="") as f: + shapes = [(r["param"], r["cardinality"]) for r in csv.DictReader(f)] + self.assertEqual(shapes, [("1.1", "10000")]) + # Logs every call; atomic-costs reports `--rows` rows kept, 0 skipped. FAKE_COST_APPROXBENCH = textwrap.dedent("""\