Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

relation summary #911

Open
wants to merge 1 commit into
base: ohad/relationeval
Choose a base branch
from
Open
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
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(())
}
}
Loading