use std::collections::BTreeMap;
use std::path::PathBuf;
use toml::Value;
use crate::bench::result::BenchmarkResultRow;
use crate::bench::runners::predict_zero::PredictZeroRunner;
use crate::bench::runners::rbposd::RbposdRunner;
use crate::bench::runners::rilpqec::RilpqecRunner;
use crate::bench::runners::rmatching::RmatchingRunner;
#[derive(Debug)]
pub struct BenchCasePoint {
pub input_type: String,
pub code_id: Option<String>,
pub distance: Option<usize>,
pub rounds: usize,
pub p: f64,
pub seed: u64,
pub basis: Option<String>,
pub schedule: Option<String>,
pub hx_path: Option<String>,
pub hz_path: Option<String>,
pub observables_path: Option<String>,
pub max_shots: u64,
pub max_errors: u64,
pub max_wall_seconds: Option<f64>,
pub batch_size: usize,
pub decoder_params: BTreeMap<String, Value>,
}
const DEFAULT_BENCH_SEED: u64 = 12_345;
pub struct BenchRunContext {
pub benchmark_name: String,
pub runner_name: String,
pub language: String,
pub seed: u64,
pub spec_dir: PathBuf,
}
pub trait RustBenchRunner: Send + Sync {
fn name(&self) -> &'static str;
fn preflight_point(&self, _point: &BenchCasePoint) -> Result<(), String> {
Ok(())
}
fn plan_point_identity(
&self,
_point: &BenchCasePoint,
_ctx: &BenchRunContext,
) -> Result<String, String> {
Err("pre-run identity planning is unsupported for this runner".into())
}
fn run_point(
&self,
point: &BenchCasePoint,
ctx: &BenchRunContext,
) -> Result<BenchmarkResultRow, String>;
}
pub type RustRunnerRegistry = BTreeMap<String, Box<dyn RustBenchRunner>>;
struct SplitRunnerParams {
generic: BTreeMap<String, Value>,
decoder: BTreeMap<String, Value>,
}
pub fn default_rust_runner_names() -> Vec<String> {
["rmatching", "rbposd", "rilpqec", "predict-zero"]
.into_iter()
.map(|name| name.to_string())
.collect()
}
pub fn build_default_rust_runner_registry() -> RustRunnerRegistry {
let mut registry: RustRunnerRegistry = BTreeMap::new();
registry.insert("rmatching".into(), Box::new(RmatchingRunner));
registry.insert("rbposd".into(), Box::new(RbposdRunner));
registry.insert("rilpqec".into(), Box::new(RilpqecRunner));
registry.insert("predict-zero".into(), Box::new(PredictZeroRunner));
registry
}
pub fn expand_runner_points(
params: &BTreeMap<String, Value>,
) -> Result<Vec<BenchCasePoint>, String> {
expand_runner_points_for_runner("generic", params)
}
pub fn expand_runner_points_for_runner(
runner_name: &str,
params: &BTreeMap<String, Value>,
) -> Result<Vec<BenchCasePoint>, String> {
let split = split_runner_params(runner_name, params)?;
expand_generic_runner_points(&split.generic, split.decoder)
}
fn expand_generic_runner_points(
params: &BTreeMap<String, Value>,
decoder_params: BTreeMap<String, Value>,
) -> Result<Vec<BenchCasePoint>, String> {
let input_type =
optional_string(params, "input_type")?.unwrap_or_else(|| "surface_rotated_memory_x".into());
let rounds = require_array(params, "rounds")?;
let ps = require_array(params, "p")?;
let seed = optional_u64(params, "seed")?.unwrap_or(DEFAULT_BENCH_SEED);
let max_shots = require_u64(params, "max_shots")?;
let max_errors = require_u64(params, "max_errors")?;
let max_wall_seconds = optional_f64(params, "max_wall_seconds")?;
validate_max_wall_seconds(max_wall_seconds)?;
let batch_size = require_usize(params, "batch_size")?;
if rounds.is_empty() {
return Err("rounds must not be empty".into());
}
if ps.is_empty() {
return Err("p must not be empty".into());
}
if batch_size == 0 {
return Err("batch_size must be positive".into());
}
match input_type.as_str() {
"surface_rotated_memory_x" | "surface_rotated_memory_z" | "memory-x" | "memory-z" => {
expand_surface_points(
&input_type,
params,
rounds,
ps,
seed,
max_shots,
max_errors,
max_wall_seconds,
batch_size,
decoder_params,
)
}
"css" => expand_css_points(
params,
rounds,
ps,
seed,
max_shots,
max_errors,
max_wall_seconds,
batch_size,
decoder_params,
),
other => Err(format!("unknown input_type: {other}")),
}
}
fn expand_surface_points(
input_type: &str,
params: &BTreeMap<String, Value>,
rounds: &[Value],
ps: &[Value],
seed: u64,
max_shots: u64,
max_errors: u64,
max_wall_seconds: Option<f64>,
batch_size: usize,
decoder_params: BTreeMap<String, Value>,
) -> Result<Vec<BenchCasePoint>, String> {
let distances = require_array(params, "distance")?;
if distances.is_empty() {
return Err("distance must not be empty".into());
}
let mut points = Vec::new();
for distance in distances {
for round in rounds {
for p in ps {
let distance = value_as_usize(distance, "distance entry")?;
let rounds = value_as_usize(round, "round entry")?;
if distance < 2 {
return Err("distance entry must be >= 2".into());
}
if rounds < 1 {
return Err("round entry must be >= 1".into());
}
points.push(BenchCasePoint {
input_type: input_type.to_string(),
code_id: None,
distance: Some(distance),
rounds,
p: value_as_f64(p, "p entry")?,
seed,
basis: None,
schedule: None,
hx_path: None,
hz_path: None,
observables_path: None,
max_shots,
max_errors,
max_wall_seconds,
batch_size,
decoder_params: decoder_params.clone(),
});
}
}
}
Ok(points)
}
fn expand_css_points(
params: &BTreeMap<String, Value>,
rounds: &[Value],
ps: &[Value],
seed: u64,
max_shots: u64,
max_errors: u64,
max_wall_seconds: Option<f64>,
batch_size: usize,
decoder_params: BTreeMap<String, Value>,
) -> Result<Vec<BenchCasePoint>, String> {
let basis = require_string(params, "basis")?;
let schedule = optional_string(params, "schedule")?.unwrap_or_else(|| "greedy".to_string());
let hx_path = require_string(params, "hx")?;
let hz_path = require_string(params, "hz")?;
let observables_path = optional_string(params, "observables")?;
let code_id = optional_string(params, "code_id")?;
let mut points = Vec::new();
for round in rounds {
for p in ps {
let rounds = value_as_usize(round, "round entry")?;
if rounds < 1 {
return Err("round entry must be >= 1".into());
}
points.push(BenchCasePoint {
input_type: "css".into(),
code_id: code_id.clone(),
distance: None,
rounds,
p: value_as_f64(p, "p entry")?,
seed,
basis: Some(basis.clone()),
schedule: Some(schedule.clone()),
hx_path: Some(hx_path.clone()),
hz_path: Some(hz_path.clone()),
observables_path: observables_path.clone(),
max_shots,
max_errors,
max_wall_seconds,
batch_size,
decoder_params: decoder_params.clone(),
});
}
}
Ok(points)
}
fn split_runner_params(
runner_name: &str,
params: &BTreeMap<String, Value>,
) -> Result<SplitRunnerParams, String> {
let mut generic = BTreeMap::new();
let mut decoder = BTreeMap::new();
for (key, value) in params {
if is_decoder_param_entry(runner_name, key, value) {
decoder.insert(key.clone(), value.clone());
} else if is_generic_param_key(key) {
generic.insert(key.clone(), value.clone());
} else {
return Err(format!("unknown {runner_name} runner param: {key}"));
}
}
Ok(SplitRunnerParams { generic, decoder })
}
fn is_generic_param_key(key: &str) -> bool {
matches!(
key,
"input_type"
| "distance"
| "rounds"
| "p"
| "seed"
| "max_shots"
| "max_errors"
| "max_wall_seconds"
| "batch_size"
| "basis"
| "schedule"
| "hx"
| "hz"
| "observables"
| "code_id"
)
}
fn is_decoder_param_entry(runner_name: &str, key: &str, value: &Value) -> bool {
match runner_name {
"rbposd" => {
matches!(
key,
"bp_algorithm"
| "bp_method"
| "bp_iters"
| "max_bp_iterations"
| "early_stop"
| "osd_method"
| "osd_order"
| "lsd_method"
| "lsd_order"
) || is_rbposd_bp_schedule_entry(key, value)
}
"rilpqec" => matches!(
key,
"backend" | "time_limit_s" | "mip_gap" | "threads" | "verbose"
),
"rmatching" | "generic" => false,
_ => false,
}
}
fn is_rbposd_bp_schedule_entry(key: &str, value: &Value) -> bool {
key == "schedule" && !matches!(value.as_str(), Some("greedy" | "sequential"))
}
fn optional_string(params: &BTreeMap<String, Value>, key: &str) -> Result<Option<String>, String> {
match params.get(key) {
None => Ok(None),
Some(value) => value
.as_str()
.map(str::to_string)
.map(Some)
.ok_or_else(|| format!("{key} must be a string")),
}
}
fn require_string(params: &BTreeMap<String, Value>, key: &str) -> Result<String, String> {
require_param(params, key)?
.as_str()
.map(str::to_string)
.ok_or_else(|| format!("{key} must be a string"))
}
fn require_array<'a>(
params: &'a BTreeMap<String, Value>,
key: &str,
) -> Result<&'a Vec<Value>, String> {
require_param(params, key)?
.as_array()
.ok_or_else(|| format!("{key} must be an array"))
}
fn require_u64(params: &BTreeMap<String, Value>, key: &str) -> Result<u64, String> {
let value = require_param(params, key)?
.as_integer()
.ok_or_else(|| format!("{key} must be an integer"))?;
u64::try_from(value).map_err(|_| format!("{key} must be non-negative"))
}
fn optional_u64(params: &BTreeMap<String, Value>, key: &str) -> Result<Option<u64>, String> {
match params.get(key) {
None => Ok(None),
Some(value) => {
let integer = value
.as_integer()
.ok_or_else(|| format!("{key} must be an integer"))?;
u64::try_from(integer)
.map(Some)
.map_err(|_| format!("{key} must be non-negative"))
}
}
}
fn optional_f64(params: &BTreeMap<String, Value>, key: &str) -> Result<Option<f64>, String> {
match params.get(key) {
None => Ok(None),
Some(value) => value_as_f64(value, key).map(Some),
}
}
fn validate_max_wall_seconds(max_wall_seconds: Option<f64>) -> Result<(), String> {
if let Some(seconds) = max_wall_seconds {
if !seconds.is_finite() || seconds <= 0.0 {
return Err("max_wall_seconds must be positive".into());
}
}
Ok(())
}
fn require_usize(params: &BTreeMap<String, Value>, key: &str) -> Result<usize, String> {
let value = require_param(params, key)?
.as_integer()
.ok_or_else(|| format!("{key} must be an integer"))?;
usize::try_from(value).map_err(|_| format!("{key} must be non-negative"))
}
fn require_param<'a>(params: &'a BTreeMap<String, Value>, key: &str) -> Result<&'a Value, String> {
params
.get(key)
.ok_or_else(|| format!("missing runner param: {key}"))
}
fn value_as_usize(value: &Value, label: &str) -> Result<usize, String> {
let value = value
.as_integer()
.ok_or_else(|| format!("{label} must be an integer"))?;
usize::try_from(value).map_err(|_| format!("{label} must be non-negative"))
}
fn value_as_f64(value: &Value, label: &str) -> Result<f64, String> {
if let Some(value) = value.as_float() {
return Ok(value);
}
if let Some(value) = value.as_integer() {
return Ok(value as f64);
}
Err(format!("{label} must be numeric"))
}