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

Default pipeline with backend arguments #2557

Open
wants to merge 3 commits into
base: pilopt-two-pass
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion cli-rs/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ fn execute<F: FieldElement>(
) -> Result<(), Vec<String>> {
let mut pipeline = Pipeline::<F>::default()
.from_asm_file(file_name.to_path_buf())
.with_backend(backend, None)
.with_backend_factory(backend)
.with_prover_inputs(inputs)
.with_output(output_dir.into(), true);

Expand Down
7 changes: 2 additions & 5 deletions pipeline/benches/jit_witgen_benchmark.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use ::powdr_pipeline::Pipeline;
use powdr_backend::BackendType;
use powdr_number::GoldilocksField;

use criterion::{criterion_group, criterion_main, Criterion};
Expand All @@ -11,10 +10,8 @@ fn jit_witgen_benchmark(c: &mut Criterion) {
group.sample_size(10);

// Poseidon benchmark
let mut pipeline = Pipeline::<T>::default()
.from_file("../test_data/std/poseidon_benchmark.asm".into())
.with_backend(BackendType::Mock, None);
// this `jit_witgen_benchmark` function will also require backend type
let mut pipeline =
Pipeline::<T>::default().from_file("../test_data/std/poseidon_benchmark.asm".into());
pipeline.compute_backend_tuned_pil().unwrap();
pipeline.compute_fixed_cols().unwrap();

Expand Down
49 changes: 35 additions & 14 deletions pipeline/src/pipeline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,14 @@ impl<R: io::Read> AsIoRead for Option<R> {
}

/// Optional Arguments for various stages of the pipeline.
#[derive(Default, Clone)]
#[derive(Clone)]
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why not implement default on backend and keep this?

struct Arguments<T: FieldElement> {
/// Externally computed witness values for witness generation.
external_witness_values: Vec<(String, Vec<T>)>,
/// Callback for queries for witness generation.
query_callback: Option<Arc<dyn QueryCallback<T>>>,
/// Backend to use for proving. If None, proving will fail.
backend: Option<BackendType>,
/// Backend to use for proving. Defaults to Mock Backend.
backend: BackendType,
/// Backend options
backend_options: BackendOptions,
/// Linker options
Expand All @@ -127,6 +127,29 @@ struct Arguments<T: FieldElement> {
existing_proof_file: Option<PathBuf>,
}

impl<T> Default for Arguments<T>
Copy link
Collaborator

Choose a reason for hiding this comment

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

This would then go away

where
T: FieldElement,
{
fn default() -> Self {
Arguments {
external_witness_values: vec![],
query_callback: None,
backend: BackendType::Mock, // the only non-default value
backend_options: Default::default(),
linker_params: Default::default(),
csv_render_mode: Default::default(),
export_witness_csv: false,
export_all_columns_csv: false,
setup_file: None,
pkey_file: None,
vkey_file: None,
vkey_app_file: None,
existing_proof_file: None,
}
}
}

#[derive(Clone)]
pub struct Pipeline<T: FieldElement> {
/// Stores all artifacts at the same time.
Expand Down Expand Up @@ -363,19 +386,17 @@ impl<T: FieldElement> Pipeline<T> {
self
}

pub fn with_backend(mut self, backend: BackendType, options: Option<BackendOptions>) -> Self {
self.arguments.backend = Some(backend);
self.arguments.backend_options = options.unwrap_or_default();
pub fn with_backend_factory(mut self, backend: BackendType) -> Self {
self.arguments.backend = backend;
self.artifact.backend = None;
self
}

pub fn with_backend_if_none(&mut self, backend: BackendType, options: Option<BackendOptions>) {
if self.arguments.backend.is_none() {
self.arguments.backend = Some(backend);
self.arguments.backend_options = options.unwrap_or_default();
self.artifact.backend = None;
}
pub fn with_backend(mut self, backend: BackendType, options: Option<BackendOptions>) -> Self {
self.arguments.backend = backend;
self.arguments.backend_options = options.unwrap_or_default();
self.artifact.backend = None;
self
}

pub fn with_setup_file(mut self, setup_file: Option<PathBuf>) -> Self {
Expand Down Expand Up @@ -998,7 +1019,7 @@ impl<T: FieldElement> Pipeline<T> {

self.compute_optimized_pil()?;

let backend_type = self.arguments.backend.expect("no backend selected!");
let backend_type = self.arguments.backend;

// If backend option is set, compute and cache the backend-tuned pil in artifacts and return backend-tuned pil.
let optimized_pil = self.artifact.optimized_pil.clone().unwrap();
Expand Down Expand Up @@ -1140,7 +1161,7 @@ impl<T: FieldElement> Pipeline<T> {
let pil = self.compute_backend_tuned_pil()?; // will panic if backend type is not set yet
let fixed_cols = self.compute_fixed_cols()?;

let backend = self.arguments.backend.expect("no backend selected!");
let backend = self.arguments.backend;
let factory = backend.factory::<T>();

// Opens the setup file, if set.
Expand Down
29 changes: 24 additions & 5 deletions pipeline/src/test_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -506,7 +506,7 @@ pub fn assert_proofs_fail_for_invalid_witnesses_mock(
}

#[cfg(feature = "estark-starky")]
pub fn assert_proofs_fail_for_invalid_witnesses_pilcom(
pub fn assert_proofs_fail_for_invalid_witnesses_pilcom_monolithic(
file_name: &str,
witness: &[(String, Vec<u64>)],
) {
Expand All @@ -517,22 +517,40 @@ pub fn assert_proofs_fail_for_invalid_witnesses_pilcom(
assert!(run_pilcom_with_backend_variant(
pipeline
.clone()
.with_backend(powdr_backend::BackendType::EStarkDump, None)
.with_backend_factory(powdr_backend::BackendType::EStarkDump)
.set_witness(convert_witness(witness)),
BackendVariant::Monolithic
)
.is_err());
}

#[cfg(feature = "estark-starky")]
pub fn assert_proofs_fail_for_invalid_witnesses_pilcom_composite(
file_name: &str,
witness: &[(String, Vec<u64>)],
) {
let pipeline = Pipeline::<GoldilocksField>::default()
.with_tmp_output()
.from_file(resolve_test_file(file_name));

assert!(run_pilcom_with_backend_variant(
pipeline
.with_backend(powdr_backend::BackendType::EStarkDumpComposite, None)
.with_backend_factory(powdr_backend::BackendType::EStarkDumpComposite)
.set_witness(convert_witness(witness)),
BackendVariant::Composite
)
.is_err());
}

#[cfg(not(feature = "estark-starky"))]
pub fn assert_proofs_fail_for_invalid_witnesses_pilcom(
pub fn assert_proofs_fail_for_invalid_witnesses_pilcom_monolithic(
_file_name: &str,
_witness: &[(String, Vec<u64>)],
) {
}

#[cfg(not(feature = "estark-starky"))]
pub fn assert_proofs_fail_for_invalid_witnesses_pilcom_composite(
_file_name: &str,
_witness: &[(String, Vec<u64>)],
) {
Expand Down Expand Up @@ -590,7 +608,8 @@ pub fn assert_proofs_fail_for_invalid_witnesses_halo2(

pub fn assert_proofs_fail_for_invalid_witnesses(file_name: &str, witness: &[(String, Vec<u64>)]) {
assert_proofs_fail_for_invalid_witnesses_mock(file_name, witness);
assert_proofs_fail_for_invalid_witnesses_pilcom(file_name, witness);
assert_proofs_fail_for_invalid_witnesses_pilcom_monolithic(file_name, witness);
assert_proofs_fail_for_invalid_witnesses_pilcom_composite(file_name, witness);
assert_proofs_fail_for_invalid_witnesses_estark(file_name, witness);
#[cfg(feature = "halo2")]
assert_proofs_fail_for_invalid_witnesses_halo2(file_name, witness);
Expand Down
5 changes: 2 additions & 3 deletions pipeline/tests/asm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ fn block_to_block_empty_submachine() {
.iter()
.for_each(|backend| {
let mut pipeline = make_simple_prepared_pipeline::<GoldilocksField>(f, LinkerMode::Bus)
.with_backend(*backend, None);
.with_backend_factory(*backend);
let witness = pipeline.compute_witness().unwrap();
let arith_size = witness
.iter()
Expand Down Expand Up @@ -314,7 +314,7 @@ fn dynamic_vadcop() {
// Witness generation require backend to be known
for backend in [BackendType::Mock, BackendType::Plonky3].iter() {
let mut pipeline = make_simple_prepared_pipeline::<GoldilocksField>(f, LinkerMode::Bus)
.with_backend(*backend, None);
.with_backend_factory(*backend);
let witness = pipeline.compute_witness().unwrap();
let witness_by_name = witness
.iter()
Expand Down Expand Up @@ -891,7 +891,6 @@ fn expand_fixed_jit() {
let file_name = "asm/expand_fixed.asm";

let mut pipeline = Pipeline::<GoldilocksField>::default()
.with_backend(BackendType::Mock, None)
.with_tmp_output()
.from_file(resolve_test_file(file_name));
let pil = pipeline.compute_backend_tuned_pil().unwrap();
Expand Down
1 change: 0 additions & 1 deletion pipeline/tests/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use test_log::test;

fn run_witgen_pil<T: FieldElement>(pil: &str) -> Arc<Columns<T>> {
Pipeline::default()
.with_backend(powdr_pipeline::BackendType::Mock, None)
.from_pil_string(pil.to_string())
.compute_witness()
.unwrap()
Expand Down
6 changes: 2 additions & 4 deletions pipeline/tests/mock_backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ fn fibonacci_wrong_initialization() {
// Initializes y with 2 instead of 1
// -> fails `ISLAST * (y' - 1) = 0;` in the last row
let f = "pil/fibonacci.pil";
let pipeline = make_simple_prepared_pipeline::<GoldilocksField>(f, LinkerMode::Bus)
.with_backend(powdr_backend::BackendType::Mock, None);
let pipeline = make_simple_prepared_pipeline::<GoldilocksField>(f, LinkerMode::Bus);
let pipeline = pipeline.set_witness(vec![
// This would be the correct witness:
// col("Fibonacci::x", [1, 1, 2, 3]),
Expand All @@ -36,8 +35,7 @@ fn block_to_block_wrong_connection() {
// So, if we multiply all columns with a constant, the constraint
// should still be satisfied, but the connection argument should fail.
let f = "asm/block_to_block.asm";
let mut pipeline = make_simple_prepared_pipeline::<GoldilocksField>(f, LinkerMode::Bus)
.with_backend(powdr_backend::BackendType::Mock, None);
let mut pipeline = make_simple_prepared_pipeline::<GoldilocksField>(f, LinkerMode::Bus);

pipeline.compute_witness().unwrap();

Expand Down
22 changes: 12 additions & 10 deletions pipeline/tests/pil.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ use powdr_pipeline::{
test_util::{
assert_proofs_fail_for_invalid_witnesses, assert_proofs_fail_for_invalid_witnesses_estark,
assert_proofs_fail_for_invalid_witnesses_mock,
assert_proofs_fail_for_invalid_witnesses_pilcom,
assert_proofs_fail_for_invalid_witnesses_pilcom_composite,
assert_proofs_fail_for_invalid_witnesses_pilcom_monolithic,
assert_proofs_fail_for_invalid_witnesses_stwo, make_prepared_pipeline,
make_simple_prepared_pipeline, regular_test_all_fields, regular_test_gl,
test_halo2_with_backend_variant, test_mock_backend, test_stwo, test_stwo_stage1_public,
Expand Down Expand Up @@ -44,7 +45,8 @@ fn lookup_with_selector() {
// Invalid witness: 0 is not in the set {2, 4}
let witness = vec![("main::w".to_string(), vec![0, 42, 4, 17])];
assert_proofs_fail_for_invalid_witnesses_mock(f, &witness);
assert_proofs_fail_for_invalid_witnesses_pilcom(f, &witness);
assert_proofs_fail_for_invalid_witnesses_pilcom_monolithic(f, &witness);
assert_proofs_fail_for_invalid_witnesses_pilcom_composite(f, &witness);
}

#[test]
Expand Down Expand Up @@ -84,7 +86,8 @@ fn permutation_with_selector() {
// Invalid witness: 0 is not in the set {2, 4}
let witness = vec![("main::w".to_string(), vec![0, 42, 4, 17])];
assert_proofs_fail_for_invalid_witnesses_mock(f, &witness);
assert_proofs_fail_for_invalid_witnesses_pilcom(f, &witness);
assert_proofs_fail_for_invalid_witnesses_pilcom_monolithic(f, &witness);
assert_proofs_fail_for_invalid_witnesses_pilcom_composite(f, &witness);
}

#[test]
Expand Down Expand Up @@ -113,12 +116,9 @@ fn fibonacci() {
fn fibonacci_with_public() {
// Public references are not supported by the backends yet, but we can test witness generation.
let f = "pil/fibonacci_with_public.pil";
let pipeline: Pipeline<GoldilocksField> =
let mut pipeline: Pipeline<GoldilocksField> =
make_prepared_pipeline(f, vec![], vec![], LinkerMode::Bus);
pipeline
.with_backend(powdr_backend::BackendType::Mock, None)
.compute_witness()
.unwrap();
pipeline.compute_witness().unwrap();
}

#[test]
Expand All @@ -133,7 +133,8 @@ fn fibonacci_invalid_witness() {
("Fibonacci::y".to_string(), vec![1, 2, 3, 13]),
];
assert_proofs_fail_for_invalid_witnesses_mock(f, &witness);
assert_proofs_fail_for_invalid_witnesses_pilcom(f, &witness);
assert_proofs_fail_for_invalid_witnesses_pilcom_monolithic(f, &witness);
assert_proofs_fail_for_invalid_witnesses_pilcom_composite(f, &witness);
assert_proofs_fail_for_invalid_witnesses_stwo(f, &witness);

// All constraints are valid, except the initial row.
Expand All @@ -144,7 +145,8 @@ fn fibonacci_invalid_witness() {
("Fibonacci::y".to_string(), vec![2, 3, 5, 8]),
];
assert_proofs_fail_for_invalid_witnesses_mock(f, &witness);
assert_proofs_fail_for_invalid_witnesses_pilcom(f, &witness);
assert_proofs_fail_for_invalid_witnesses_pilcom_monolithic(f, &witness);
assert_proofs_fail_for_invalid_witnesses_pilcom_composite(f, &witness);
assert_proofs_fail_for_invalid_witnesses_stwo(f, &witness);
}

Expand Down
5 changes: 1 addition & 4 deletions riscv/benches/executor_benchmark.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use ::powdr_pipeline::Pipeline;
use powdr_backend::BackendType;
use powdr_number::GoldilocksField;

use powdr_riscv::{compile_rust_crate_to_riscv, elf, CompilerOptions};
Expand All @@ -19,9 +18,7 @@ fn executor_benchmark(c: &mut Criterion) {
compile_rust_crate_to_riscv("./tests/riscv_data/keccak/Cargo.toml", &tmp_dir, None);
let options = CompilerOptions::new_gl();
let contents = elf::translate(&executable, options);
let mut pipeline = Pipeline::<T>::default()
.from_asm_string(contents, None)
.with_backend(BackendType::Mock, None);
let mut pipeline = Pipeline::<T>::default().from_asm_string(contents, None);
pipeline.compute_backend_tuned_pil().unwrap();
pipeline.compute_fixed_cols().unwrap();

Expand Down
4 changes: 0 additions & 4 deletions riscv/src/continuations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,6 @@ pub fn rust_continuations<F: FieldElement, PipelineCallback, E>(
where
PipelineCallback: Fn(&mut Pipeline<F>) -> Result<(), E>,
{
pipeline.with_backend_if_none(powdr_pipeline::BackendType::Mock, None);

let bootloader_inputs = dry_run_result.bootloader_inputs;
let num_chunks = bootloader_inputs.len();

Expand Down Expand Up @@ -340,8 +338,6 @@ pub fn rust_continuations_dry_run<F: FieldElement>(
pipeline: &mut Pipeline<F>,
profiler_opt: Option<ProfilerOptions>,
) -> DryRunResult<F> {
pipeline.with_backend_if_none(powdr_pipeline::BackendType::Mock, None);

let field = F::known_field().unwrap();

// All inputs for all chunks.
Expand Down
4 changes: 1 addition & 3 deletions riscv/tests/common/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use mktemp::Temp;
use powdr_backend::BackendType;
use powdr_number::{BabyBearField, FieldElement, GoldilocksField, KnownField, KoalaBearField};
use powdr_pipeline::{
test_util::{
Expand All @@ -26,8 +25,7 @@ pub fn verify_riscv_asm_string<T: FieldElement, S: serde::Serialize + Send + Syn
let mut pipeline = Pipeline::default()
.with_prover_inputs(inputs.to_vec())
.with_output(temp_dir.to_path_buf(), true)
.from_asm_string(contents.to_string(), Some(PathBuf::from(file_name)))
.with_backend(BackendType::Mock, None);
.from_asm_string(contents.to_string(), Some(PathBuf::from(file_name)));

if let Some(data) = data {
pipeline = pipeline.add_data_vec(data);
Expand Down
2 changes: 0 additions & 2 deletions riscv/tests/riscv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,6 @@ fn read_slice_with_options<T: FieldElement>(options: CompilerOptions) {

let mut pipeline = Pipeline::<T>::default()
.from_asm_string(powdr_asm, Some(PathBuf::from(case)))
.with_backend(powdr_backend::BackendType::Mock, None)
.with_prover_inputs(vec![answer.into()])
.with_prover_dict_inputs(d);

Expand Down Expand Up @@ -615,7 +614,6 @@ fn output_syscall_with_options<T: FieldElement>(options: CompilerOptions) {

let inputs = vec![1u32, 2, 3].into_iter().map(T::from).collect();
let mut pipeline = Pipeline::<T>::default()
.with_backend(powdr_backend::BackendType::Mock, None)
.from_asm_string(powdr_asm, Some(PathBuf::from(case)))
.with_prover_inputs(inputs);

Expand Down
Loading