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

feat: recursion optimizations #1781

Open
wants to merge 55 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
55 commits
Select commit Hold shift + click to select a range
2b6bd90
Merge pull request #8 from succinctlabs/tamir/merge-upstream
tamirhemo Sep 5, 2024
f17c080
Merge remote-tracking branch 'upstream/dev' into dev
tamirhemo Sep 10, 2024
4b7b72e
Merge remote-tracking branch 'upstream/dev' into tqn/merge-upstream
tqn Sep 13, 2024
7ec9cd6
Merge pull request #15 from succinctlabs/tqn/merge-upstream
tqn Sep 13, 2024
35c1ee4
Merge remote-tracking branch 'origin/dev' into kevjue/merge_upstream
kevjue Sep 20, 2024
1728714
Merge pull request #18 from succinctlabs/kevjue/merge_upstream
kevjue Sep 20, 2024
35b8d99
Merge remote-tracking branch 'upstream/dev' into tqn/merge-upstream
tqn Oct 17, 2024
544d59b
Merge pull request #30 from succinctlabs/tqn/merge-upstream
tqn Oct 17, 2024
369e1c0
Merge remote-tracking branch 'upstream/dev' into tamir/dev-10-18
tamirhemo Oct 18, 2024
e0a7dec
Merge pull request #34 from succinctlabs/tamir/dev-10-18
tamirhemo Oct 18, 2024
05fc72f
Merge remote-tracking branch 'upstream/dev' into tamir/update-10-22
tamirhemo Oct 22, 2024
a8af928
Merge pull request #36 from succinctlabs/tamir/update-10-22
tamirhemo Oct 22, 2024
f41d750
Merge remote-tracking branch 'upstream/dev' into dev
jtguibas Oct 28, 2024
4870b74
Merge remote-tracking branch 'upstream/dev' into dev
jtguibas Oct 29, 2024
e6e8bce
Merge remote-tracking branch 'upstream/dev' into erabinov/merge-upstream
erabinov Nov 3, 2024
474bbe2
Merge pull request #54 from succinctlabs/erabinov/merge-upstream
erabinov Nov 4, 2024
41f7fe1
Merge remote-tracking branch 'upstream/dev' into tamir/update-nov-5
tamirhemo Nov 5, 2024
5253c03
Merge pull request #58 from succinctlabs/tamir/update-nov-5
tamirhemo Nov 5, 2024
8a696d2
Merge remote-tracking branch 'upstream/dev' into dev
tamirhemo Nov 5, 2024
6af5b1d
Merge remote-tracking branch 'upstream/dev' into dev
tamirhemo Nov 5, 2024
31147b4
perf: remove proving single proofs (#60)
erabinov Nov 7, 2024
fbceea5
Merge remote-tracking branch 'upstream/dev' into tamir/update
tamirhemo Nov 7, 2024
d8ca89c
Merge remote-tracking branch 'upstream/dev' into dev
tamirhemo Nov 8, 2024
c9662c4
perf: drop record in different task (#65)
tamirhemo Nov 8, 2024
2614e21
feat: execute mode on `perf` (#62)
tamirhemo Nov 8, 2024
adcda87
perf: two shapes + shape script (#63)
erabinov Nov 11, 2024
fb7e7c6
optional gpu image
jtguibas Nov 11, 2024
8c489f2
optional image
jtguibas Nov 11, 2024
55b10a2
update image
jtguibas Nov 11, 2024
1780b80
small adjustments
jtguibas Nov 12, 2024
a573f77
opts
jtguibas Nov 12, 2024
9905bcb
boom
jtguibas Nov 12, 2024
cba87a2
changes
jtguibas Nov 12, 2024
f037e81
lots of changes
jtguibas Nov 13, 2024
35691ba
hm
jtguibas Nov 13, 2024
bf227d7
Wtf
jtguibas Nov 13, 2024
20b8bb5
hm
jtguibas Nov 13, 2024
eec6afa
more testing
jtguibas Nov 13, 2024
54d8d85
more testing
jtguibas Nov 13, 2024
08d6ac0
hm
jtguibas Nov 13, 2024
de80d06
boom
jtguibas Nov 13, 2024
b1950cc
cleanup
jtguibas Nov 13, 2024
98ca9c6
boom
jtguibas Nov 13, 2024
b16af57
earlier prove
jtguibas Nov 13, 2024
16a0990
done
jtguibas Nov 13, 2024
2643ccf
hm
jtguibas Nov 13, 2024
7238b83
sort stuff
jtguibas Nov 13, 2024
1acba88
more
jtguibas Nov 13, 2024
6837800
hm
jtguibas Nov 13, 2024
5763858
match ordering
jtguibas Nov 13, 2024
e78a6e4
changes
jtguibas Nov 13, 2024
7316572
wtf
jtguibas Nov 13, 2024
5fb67cd
wtf
jtguibas Nov 13, 2024
585f28e
log
jtguibas Nov 13, 2024
ed16360
hm
jtguibas Nov 13, 2024
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
3 changes: 3 additions & 0 deletions .github/workflows/suite.yml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ jobs:
args: --release -p sp1-perf -- --program workdir/program.bin --stdin workdir/stdin.bin --mode cpu
env:
RUST_LOG: info
VERIFY_VK: false
RUSTFLAGS: -Copt-level=3 -Ctarget-cpu=native
RUST_BACKTRACE: 1

Expand Down Expand Up @@ -120,6 +121,7 @@ jobs:
args: --release -p sp1-perf -- --program workdir/program.bin --stdin workdir/stdin.bin --mode cuda
env:
RUST_LOG: debug
VERIFY_VK: false
RUSTFLAGS: -Copt-level=3 -Ctarget-cpu=native
RUST_BACKTRACE: 1
SP1_PROVER: cuda
Expand Down Expand Up @@ -172,6 +174,7 @@ jobs:
args: --release -p sp1-perf --features "native-gnark,network-v2" -- --program workdir/program.bin --stdin workdir/stdin.bin --mode network
env:
RUST_LOG: info
VERIFY_VK: false
RUSTFLAGS: -Copt-level=3 -Ctarget-cpu=native
RUST_BACKTRACE: 1
SP1_PROVER: network
Expand Down
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion crates/cli/src/commands/build_toolchain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ impl BuildToolchainCmd {
std::fs::write(&config_file, config_toml)
.with_context(|| format!("while writing configuration to {:?}", config_file))?;

// Work around target sanity check added in rust-lang/rust@09c076810cb7649e5817f316215010d49e78e8d7.
// Work around target sanity check added in
// rust-lang/rust@09c076810cb7649e5817f316215010d49e78e8d7.
let temp_dir = std::env::temp_dir().join("rustc-targets");
if !temp_dir.exists() {
std::fs::create_dir_all(&temp_dir)?;
Expand Down
11 changes: 1 addition & 10 deletions crates/cli/src/commands/prove.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ use anstyle::*;
use anyhow::Result;
use clap::Parser;
use sp1_build::{execute_build_program, BuildArgs};
use sp1_core_machine::{
io::SP1Stdin,
utils::{setup_logger, setup_tracer},
};
use sp1_core_machine::{io::SP1Stdin, utils::setup_logger};
use sp1_sdk::ProverClient;
use std::{env, fs::File, io::Read, path::PathBuf, str::FromStr, time::Instant};

Expand Down Expand Up @@ -78,12 +75,6 @@ impl ProveCmd {
Err(_) => env::set_var("RUST_LOG", "info"),
}
setup_logger();
} else {
match env::var("RUST_TRACER") {
Ok(_) => {}
Err(_) => env::set_var("RUST_TRACER", "info"),
}
setup_tracer();
}

// The command predates multi-target build support. This allows the command to continue to
Expand Down
4 changes: 2 additions & 2 deletions crates/core/executor/src/events/syscall.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use super::LookupId;

/// Syscall Event.
///
/// This object encapsulated the information needed to prove a syscall invocation from the CPU table.
/// This includes its shard, clk, syscall id, arguments, other relevant information.
/// This object encapsulated the information needed to prove a syscall invocation from the CPU
/// table. This includes its shard, clk, syscall id, arguments, other relevant information.
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
pub struct SyscallEvent {
/// The shard number.
Expand Down
3 changes: 1 addition & 2 deletions crates/core/executor/src/events/utils.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use serde::Deserialize;
use serde::Serialize;
use serde::{Deserialize, Serialize};
use std::{
fmt::Display,
iter::{Map, Peekable},
Expand Down
5 changes: 3 additions & 2 deletions crates/core/executor/src/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,9 @@ pub struct Executor<'a> {
/// checkpoints.
pub memory_checkpoint: PagedMemory<Option<MemoryRecord>>,

/// Memory addresses that were initialized in this batch of shards. Used to minimize the size of
/// checkpoints. The value stored is whether or not it had a value at the beginning of the batch.
/// Memory addresses that were initialized in this batch of shards. Used to minimize the size
/// of checkpoints. The value stored is whether or not it had a value at the beginning of
/// the batch.
pub uninitialized_memory_checkpoint: PagedMemory<bool>,

/// The memory accesses for the current cycle.
Expand Down
6 changes: 4 additions & 2 deletions crates/core/executor/src/hook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ use core::fmt::Debug;
use std::sync::{Arc, RwLock, RwLockWriteGuard};

use hashbrown::HashMap;
use sp1_curves::k256::{Invert, RecoveryId, Signature, VerifyingKey};
use sp1_curves::p256::Signature as p256Signature;
use sp1_curves::{
k256::{Invert, RecoveryId, Signature, VerifyingKey},
p256::Signature as p256Signature,
};

use crate::Executor;

Expand Down
3 changes: 2 additions & 1 deletion crates/core/executor/src/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,8 @@ impl<'a, V: Copy> Entry<'a, V> {
}
}

/// Provides in-place mutable access to an occupied entry before any potential inserts into the map.
/// Provides in-place mutable access to an occupied entry before any potential inserts into the
/// map.
pub fn and_modify<F: FnOnce(&mut V)>(mut self, f: F) -> Self {
match &mut self {
Entry::Vacant(_) => {}
Expand Down
3 changes: 2 additions & 1 deletion crates/core/executor/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ pub struct ExecutionState {
/// + timestamp that each memory address was accessed.
pub memory: PagedMemory<MemoryRecord>,

/// The global clock keeps track of how many instructions have been executed through all shards.
/// The global clock keeps track of how many instructions have been executed through all
/// shards.
pub global_clk: u64,

/// The clock increments by 4 (possibly more in syscalls) for each instruction that has been
Expand Down
6 changes: 3 additions & 3 deletions crates/core/executor/src/syscalls/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,9 @@ impl<'a, 'b> SyscallContext<'a, 'b> {
let mut syscall_local_mem_events = Vec::new();

if !self.rt.unconstrained && self.rt.executor_mode == ExecutorMode::Trace {
// Will need to transfer the existing memory local events in the executor to it's record,
// and return all the syscall memory local events. This is similar to what
// `bump_record` does.
// Will need to transfer the existing memory local events in the executor to it's
// record, and return all the syscall memory local events. This is similar
// to what `bump_record` does.
for (addr, event) in self.local_memory_access.drain() {
let local_mem_access = self.rt.local_memory_access.remove(&addr);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ impl<P: FpOpField> Syscall for Fp2AddSubSyscall<P> {
local_mem_access: rt.postprocess(),
};
match P::FIELD_TYPE {
// All the fp2 add and sub events for a given curve are coalesced to the curve's fp2 add operation. Only check for
// that operation.
// All the fp2 add and sub events for a given curve are coalesced to the curve's fp2 add
// operation. Only check for that operation.
// TODO: Fix this.
FieldType::Bn254 => {
let syscall_code_key = match syscall_code {
Expand Down
2 changes: 2 additions & 0 deletions crates/core/machine/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ p3-util = { workspace = true }
sp1-derive = { workspace = true }
sp1-primitives = { workspace = true }

rayon = "1.10.0"

amcl = { package = "snowbridge-amcl", version = "1.0.2", default-features = false, features = [
"bls381",
] }
Expand Down
66 changes: 41 additions & 25 deletions crates/core/machine/src/riscv/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -571,26 +571,26 @@ pub mod tests {
use crate::{
io::SP1Stdin,
riscv::RiscvAir,
utils,
utils::{prove, run_test, setup_logger},
utils::{self, prove_core, run_test, setup_logger},
};

use sp1_core_executor::{
programs::tests::{
fibonacci_program, simple_memory_program, simple_program, ssz_withdrawals_program,
},
Instruction, Opcode, Program,
Instruction, Opcode, Program, SP1Context,
};
use sp1_stark::{
baby_bear_poseidon2::BabyBearPoseidon2, CpuProver, SP1CoreOpts, StarkProvingKey,
StarkVerifyingKey,
baby_bear_poseidon2::BabyBearPoseidon2, CpuProver, MachineProver, SP1CoreOpts,
StarkProvingKey, StarkVerifyingKey,
};

#[test]
fn test_simple_prove() {
utils::setup_logger();
let program = simple_program();
run_test::<CpuProver<_, _>>(program).unwrap();
let stdin = SP1Stdin::new();
run_test::<CpuProver<_, _>>(program, stdin).unwrap();
}

#[test]
Expand All @@ -607,7 +607,8 @@ pub mod tests {
Instruction::new(*shift_op, 31, 29, 3, false, false),
];
let program = Program::new(instructions, 0, 0);
run_test::<CpuProver<_, _>>(program).unwrap();
let stdin = SP1Stdin::new();
run_test::<CpuProver<_, _>>(program, stdin).unwrap();
}
}
}
Expand All @@ -621,7 +622,8 @@ pub mod tests {
Instruction::new(Opcode::SUB, 31, 30, 29, false, false),
];
let program = Program::new(instructions, 0, 0);
run_test::<CpuProver<_, _>>(program).unwrap();
let stdin = SP1Stdin::new();
run_test::<CpuProver<_, _>>(program, stdin).unwrap();
}

#[test]
Expand All @@ -633,7 +635,8 @@ pub mod tests {
Instruction::new(Opcode::ADD, 31, 30, 29, false, false),
];
let program = Program::new(instructions, 0, 0);
run_test::<CpuProver<_, _>>(program).unwrap();
let stdin = SP1Stdin::new();
run_test::<CpuProver<_, _>>(program, stdin).unwrap();
}

#[test]
Expand All @@ -650,7 +653,8 @@ pub mod tests {
Instruction::new(*mul_op, 31, 30, 29, false, false),
];
let program = Program::new(instructions, 0, 0);
run_test::<CpuProver<_, _>>(program).unwrap();
let stdin = SP1Stdin::new();
run_test::<CpuProver<_, _>>(program, stdin).unwrap();
}
}
}
Expand All @@ -666,7 +670,8 @@ pub mod tests {
Instruction::new(*lt_op, 31, 30, 29, false, false),
];
let program = Program::new(instructions, 0, 0);
run_test::<CpuProver<_, _>>(program).unwrap();
let stdin = SP1Stdin::new();
run_test::<CpuProver<_, _>>(program, stdin).unwrap();
}
}

Expand All @@ -682,7 +687,8 @@ pub mod tests {
Instruction::new(*bitwise_op, 31, 30, 29, false, false),
];
let program = Program::new(instructions, 0, 0);
run_test::<CpuProver<_, _>>(program).unwrap();
let stdin = SP1Stdin::new();
run_test::<CpuProver<_, _>>(program, stdin).unwrap();
}
}

Expand All @@ -705,7 +711,8 @@ pub mod tests {
Instruction::new(*div_rem_op, 31, 29, 30, false, false),
];
let program = Program::new(instructions, 0, 0);
run_test::<CpuProver<_, _>>(program).unwrap();
let stdin = SP1Stdin::new();
run_test::<CpuProver<_, _>>(program, stdin).unwrap();
}
}
}
Expand All @@ -714,7 +721,8 @@ pub mod tests {
fn test_fibonacci_prove_simple() {
setup_logger();
let program = fibonacci_program();
run_test::<CpuProver<_, _>>(program).unwrap();
let stdin = SP1Stdin::new();
run_test::<CpuProver<_, _>>(program, stdin).unwrap();
}

#[test]
Expand All @@ -726,36 +734,44 @@ pub mod tests {
let mut opts = SP1CoreOpts::default();
opts.shard_size = 1024;
opts.shard_batch_size = 2;
prove::<_, CpuProver<_, _>>(program, &stdin, BabyBearPoseidon2::new(), opts, None).unwrap();

let config = BabyBearPoseidon2::new();
let machine = RiscvAir::machine(config);
let prover = CpuProver::new(machine);
let (pk, _) = prover.setup(&program);
prove_core::<_, _>(&prover, &pk, program, &stdin, opts, SP1Context::default(), None)
.unwrap();
}

#[test]
fn test_fibonacci_prove_batch() {
setup_logger();
let program = fibonacci_program();
let stdin = SP1Stdin::new();
prove::<_, CpuProver<_, _>>(
program,
&stdin,
BabyBearPoseidon2::new(),
SP1CoreOpts::default(),
None,
)
.unwrap();

let opts = SP1CoreOpts::default();
let config = BabyBearPoseidon2::new();
let machine = RiscvAir::machine(config);
let prover = CpuProver::new(machine);
let (pk, _) = prover.setup(&program);
prove_core::<_, _>(&prover, &pk, program, &stdin, opts, SP1Context::default(), None)
.unwrap();
}

#[test]
fn test_simple_memory_program_prove() {
setup_logger();
let program = simple_memory_program();
run_test::<CpuProver<_, _>>(program).unwrap();
let stdin = SP1Stdin::new();
run_test::<CpuProver<_, _>>(program, stdin).unwrap();
}

#[test]
fn test_ssz_withdrawal() {
setup_logger();
let program = ssz_withdrawals_program();
run_test::<CpuProver<_, _>>(program).unwrap();
let stdin = SP1Stdin::new();
run_test::<CpuProver<_, _>>(program, stdin).unwrap();
}

#[test]
Expand Down
24 changes: 22 additions & 2 deletions crates/core/machine/src/riscv/shape.rs
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,27 @@ impl<F: PrimeField32> CoreShapeConfig<F> {

max_core_shapes.collect()
}
pub fn maximal_core_plus_precompile_shapes(&self) -> Vec<CoreShape> {
let max_preprocessed = self
.allowed_preprocessed_log_heights
.iter()
.map(|(air, allowed_heights)| (air.name(), allowed_heights.last().unwrap().unwrap()));

let precompile_only_shapes = self.precompile_allowed_log_heights.iter().flat_map(
move |(air, (mem_events_per_row, allowed_log_heights))| {
self.get_precompile_shapes(
air,
*mem_events_per_row,
*allowed_log_heights.last().unwrap(),
)
},
);

let precompile_shapes = precompile_only_shapes
.map(|x| max_preprocessed.clone().chain(x).collect::<CoreShape>());

self.maximal_core_shapes().into_iter().chain(precompile_shapes).collect()
}
}

impl<F: PrimeField32> Default for CoreShapeConfig<F> {
Expand Down Expand Up @@ -769,8 +790,7 @@ pub mod tests {
fn test_dummy_record() {
use crate::utils::setup_logger;
use p3_baby_bear::BabyBear;
use sp1_stark::baby_bear_poseidon2::BabyBearPoseidon2;
use sp1_stark::CpuProver;
use sp1_stark::{baby_bear_poseidon2::BabyBearPoseidon2, CpuProver};

type SC = BabyBearPoseidon2;
type A = RiscvAir<BabyBear>;
Expand Down
Loading