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
65 changes: 24 additions & 41 deletions crates/asap-aware-mapping/src/accuracy/allocation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,16 @@ impl AccuracyAllocation {
}
let (eps, delta): (Vec<f64>, Vec<Option<f64>>) = inner
.iter()
.map(|t| match t {
AccuracyTarget::Exact => (0.0, Some(0.0)),
AccuracyTarget::Epsilon(e) => (*e, None),
AccuracyTarget::EpsilonDelta { epsilon, delta } => (*epsilon, Some(*delta)),
})
.map(|t| (t.error, t.failure_probability))
.unzip();
let epsilon = match shape.metric {
ErrorMetric::RelativeValue => eps.iter().map(|e| 1.0 + e).product::<f64>() - 1.0,
ErrorMetric::Relative => eps.iter().map(|e| 1.0 + e).product::<f64>() - 1.0,
_ => eps.iter().sum(),
};
Some(match delta.iter().copied().sum::<Option<f64>>() {
Some(delta) => AccuracyTarget::EpsilonDelta { epsilon, delta },
None => AccuracyTarget::Epsilon(epsilon),
Some(AccuracyTarget {
error: epsilon,
failure_probability: delta.iter().copied().sum::<Option<f64>>(),
..inner[0].clone()
})
}
}
Expand All @@ -65,7 +62,7 @@ pub trait AccuracyBudgetAllocator {
/// equal share — `ε_i = ε / n`, `δ_i = δ / n` for an additively composed
/// metric, and `ε_i = (1 + ε)^{1/n} − 1` for a multiplicatively composed
/// one — so the composed bound meets the target exactly with no slack.
/// `AccuracyTarget::Exact` yields no allocation: no approximate layer can
/// `AccuracyTarget::exact()` yields no allocation: no approximate layer can
/// meet it.
#[derive(Debug, Default, Clone, Copy)]
pub struct EqualSplitAllocator;
Expand All @@ -77,24 +74,21 @@ impl AccuracyBudgetAllocator for EqualSplitAllocator {
composition: &CompositionShape,
) -> Vec<AccuracyAllocation> {
let n = composition.approximate_layer_count.max(1);
let (epsilon, delta) = match target {
AccuracyTarget::Exact => return Vec::new(),
AccuracyTarget::Epsilon(e) => (*e, None),
AccuracyTarget::EpsilonDelta { epsilon, delta } => (*epsilon, Some(*delta)),
};
if target.is_exact() {
return Vec::new();
}
let (epsilon, delta) = (target.error, target.failure_probability);
if !(epsilon.is_finite() && epsilon > 0.0) {
return Vec::new();
}
let local_epsilon = match composition.metric {
ErrorMetric::RelativeValue => (1.0 + epsilon).powf(1.0 / n as f64) - 1.0,
ErrorMetric::Relative => (1.0 + epsilon).powf(1.0 / n as f64) - 1.0,
_ => epsilon / n as f64,
};
let layer = match delta {
Some(delta) => AccuracyTarget::EpsilonDelta {
epsilon: local_epsilon,
delta: delta / n as f64,
},
None => AccuracyTarget::Epsilon(local_epsilon),
let layer = AccuracyTarget {
error: local_epsilon,
failure_probability: delta.map(|delta| delta / n as f64),
..target.clone()
};
vec![AccuracyAllocation {
allocator: "EqualSplitAllocator",
Expand All @@ -108,12 +102,9 @@ mod tests {
use super::*;
#[test]
fn equal_split_respects_the_root_epsilon_and_delta() {
let target = AccuracyTarget::EpsilonDelta {
epsilon: 0.1,
delta: 0.02,
};
let target = AccuracyTarget::epsilon_delta(0.1, 0.02);
let shape = CompositionShape {
metric: ErrorMetric::AbsoluteValue,
metric: ErrorMetric::ABSOLUTE,
approximate_layer_count: 2,
};
let allocations = EqualSplitAllocator.allocations(&target, &shape);
Expand All @@ -122,35 +113,27 @@ mod tests {
assert_eq!(layers.len(), 2);
let (eps, deltas): (Vec<f64>, Vec<f64>) = layers
.iter()
.map(|t| match t {
AccuracyTarget::EpsilonDelta { epsilon, delta } => (*epsilon, *delta),
other => panic!("unexpected {other:?}"),
})
.map(|t| (t.error, t.failure_probability.unwrap()))
.unzip();
assert!((eps.iter().sum::<f64>() - 0.1).abs() < 1e-12);
assert!((deltas.iter().sum::<f64>() - 0.02).abs() < 1e-12);
assert_eq!(
allocations[0].inner_target(&shape),
Some(AccuracyTarget::EpsilonDelta {
epsilon: 0.05,
delta: 0.01
})
Some(AccuracyTarget::epsilon_delta(0.05, 0.01))
);

// Multiplicative composition: (1+ε_i)^2 = 1+ε, not 2ε_i = ε.
let rel_shape = CompositionShape {
metric: ErrorMetric::RelativeValue,
metric: ErrorMetric::Relative,
approximate_layer_count: 2,
};
let allocations =
EqualSplitAllocator.allocations(&AccuracyTarget::Epsilon(0.21), &rel_shape);
let AccuracyTarget::Epsilon(e) = allocations[0].layers[0] else {
panic!()
};
EqualSplitAllocator.allocations(&AccuracyTarget::epsilon(0.21), &rel_shape);
let e = allocations[0].layers[0].error;
assert!((e - 0.1).abs() < 1e-12);

assert!(EqualSplitAllocator
.allocations(&AccuracyTarget::Exact, &shape)
.allocations(&AccuracyTarget::exact(), &shape)
.is_empty());
}
}
Loading
Loading