Skip to content

Commit

Permalink
relation summary
Browse files Browse the repository at this point in the history
  • Loading branch information
ohad-starkware committed Nov 28, 2024
1 parent b4fefbf commit 4816a4a
Showing 1 changed file with 44 additions and 1 deletion.
45 changes: 44 additions & 1 deletion crates/prover/src/constraint_framework/relation_tracker.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::collections::HashMap;
use std::fmt::Debug;

use itertools::Itertools;
Expand Down Expand Up @@ -74,7 +75,7 @@ impl<E: FrameworkEval> RelationTrackerComponent<E> {
}

/// Aggregates relation entries.
// TODO(Ohad): write a summarize function, test.
// TODO(Ohad): test.
pub struct RelationTrackerEvaluator<'a> {
entries: Vec<RelationTrackerEntry>,
pub trace_eval:
Expand Down Expand Up @@ -187,3 +188,45 @@ impl<'a> EvalAtRow for RelationTrackerEvaluator<'a> {
}
}
}

type RelationInfo = (String, Vec<(Vec<M31>, M31)>);
pub struct RelationSummary(Vec<RelationInfo>);
impl RelationSummary {
/// Returns the sum of every entry's yields and uses.
/// The result is a map from relation name to a list of values(M31 vectors) and their sum.
pub fn summarize_relations(entries: &[RelationTrackerEntry]) -> Self {
let mut summary = vec![];
let relations = entries.iter().group_by(|entry| entry.relation.clone());
for (relation, entries) in &relations {
let mut relation_sums: HashMap<Vec<_>, M31> = HashMap::new();
for entry in entries {
let mult = relation_sums
.entry(entry.values.clone())
.or_insert(M31::zero());
*mult += entry.mult;
}
let relation_sums = relation_sums.into_iter().collect_vec();
summary.push((relation.clone(), relation_sums));
}
Self(summary)
}

pub fn get_relation_info(&self, relation: &str) -> Option<&[(Vec<M31>, M31)]> {
self.0
.iter()
.find(|(name, _)| name == relation)
.map(|(_, entries)| entries.as_slice())
}
}
impl Debug for RelationSummary {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
for (relation, entries) in &self.0 {
writeln!(f, "{}:", relation)?;
for (vector, sum) in entries {
let vector = vector.iter().map(|v| v.0).collect_vec();
writeln!(f, " {:?} -> {}", vector, sum)?;
}
}
Ok(())
}
}

0 comments on commit 4816a4a

Please sign in to comment.