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

Manual Bus Witgen: Output folded columns only if they exist #2428

Merged
merged 3 commits into from
Feb 4, 2025
Merged
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
83 changes: 65 additions & 18 deletions executor/src/witgen/bus_accumulator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ use extension_field::ExtensionField;
use fp2::Fp2;
use fp4::Fp4;
use itertools::Itertools;
use powdr_ast::analyzed::{Analyzed, Identity, PhantomBusInteractionIdentity};
use powdr_ast::analyzed::{
AlgebraicExpression, Analyzed, Identity, PhantomBusInteractionIdentity, PolyID,
};
use powdr_executor_utils::{
expression_evaluator::{ExpressionEvaluator, OwnedTerminalValues},
VariablySizedColumn,
Expand Down Expand Up @@ -120,24 +122,41 @@ impl<'a, T: FieldElement, Ext: ExtensionField<T> + Sync> BusAccumulatorGenerator
}

pub fn generate(&self) -> Vec<(String, Vec<T>)> {
let accumulators = self
let mut columns = self
.bus_interactions
.par_iter()
.flat_map(|bus_interaction| self.interaction_columns(bus_interaction))
.collect::<Vec<_>>();
.flat_map(|bus_interaction| {
let (folded, acc) = self.interaction_columns(bus_interaction);
collect_folded_columns(bus_interaction, folded)
.chain(collect_acc_columns(bus_interaction, acc))
.collect::<Vec<_>>()
})
.collect::<BTreeMap<_, _>>();

self.pil
let result = self
.pil
.committed_polys_in_source_order()
.filter(|(symbol, _)| symbol.stage == Some(1))
.flat_map(|(symbol, _)| symbol.array_elements().map(|(name, _)| name))
.zip_eq(accumulators)
.collect()
.flat_map(|(symbol, _)| symbol.array_elements())
.map(|(name, poly_id)| {
let column = columns
.remove(&poly_id)
.unwrap_or_else(|| panic!("Unexpected column: {name}"));
(name, column)
})
.collect();
assert!(
columns.is_empty(),
"Some expected columns not found in the PIL."
);

result
}

fn interaction_columns(
&self,
bus_interaction: &PhantomBusInteractionIdentity<T>,
) -> Vec<Vec<T>> {
) -> (Vec<Vec<T>>, Vec<Vec<T>>) {
let intermediate_definitions = self.pil.intermediate_definitions();

let size = self.values.height();
Expand Down Expand Up @@ -171,19 +190,18 @@ impl<'a, T: FieldElement, Ext: ExtensionField<T> + Sync> BusAccumulatorGenerator
}

// Transpose from row-major to column-major & flatten.
let mut result = vec![Vec::with_capacity(size); Ext::size() * 2];
let mut folded = vec![Vec::with_capacity(size); Ext::size()];
let mut acc = vec![Vec::with_capacity(size); Ext::size()];
for row_index in 0..size {
for (col_index, x) in folded_list[row_index]
.to_vec()
.into_iter()
.chain(acc_list[row_index].to_vec())
.enumerate()
{
result[col_index].push(x);
for (col_index, x) in folded_list[row_index].to_vec().into_iter().enumerate() {
folded[col_index].push(x);
}
for (col_index, x) in acc_list[row_index].to_vec().into_iter().enumerate() {
acc[col_index].push(x);
}
}

result
(folded, acc)
}

/// Fingerprints a tuples of field elements, using the pre-computed powers of alpha.
Expand All @@ -206,3 +224,32 @@ fn powers_of_alpha<T, Ext: ExtensionField<T>>(alpha: Ext, n: usize) -> Vec<Ext>
})
.collect::<Vec<_>>()
}

fn collect_folded_columns<T>(
bus_interaction: &PhantomBusInteractionIdentity<T>,
folded: Vec<Vec<T>>,
) -> impl Iterator<Item = (PolyID, Vec<T>)> + '_ {
bus_interaction
.folded_expressions
.0
.iter()
.zip_eq(folded)
.filter_map(|(expr, column)| match expr {
AlgebraicExpression::Reference(col_reference) if col_reference.is_witness() => {
Copy link
Collaborator

@qwang98 qwang98 Feb 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From the is_witness function, it looks like there are Committed, Constant, and Intermediate enums for PolynomialType. If the folded column has been "optimized away" as you mentioned, will it become Intermediate and thus filtered away?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes!

Some((col_reference.poly_id, column))
}
// If the folded payload is not persisted as witness columns, we skip it.
_ => None,
})
}

fn collect_acc_columns<T>(
bus_interaction: &PhantomBusInteractionIdentity<T>,
acc: Vec<Vec<T>>,
) -> impl Iterator<Item = (PolyID, Vec<T>)> + '_ {
bus_interaction
.accumulator_columns
.iter()
.zip_eq(acc)
.map(|(column_reference, column)| (column_reference.poly_id, column))
}
Loading