Skip to content

feat: print cycles #13

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

Merged
merged 1 commit into from
Mar 10, 2025
Merged
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
4 changes: 2 additions & 2 deletions sp1/src/bin/bigmem.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use utils::benchmark;
use utils::benchmark_v2;
use sp1_script::{bench_bigmem, init_logger};

fn main() {
init_logger();

let values = [5u32];
benchmark(bench_bigmem, &values, "../benchmark_outputs/bigmem_sp1.csv", "value");
benchmark_v2(bench_bigmem, &values, "../benchmark_outputs/bigmem_sp1.csv", "value");
}
4 changes: 2 additions & 2 deletions sp1/src/bin/fibonacci.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use utils::benchmark;
use utils::benchmark_v2;
use sp1_script::{bench_fibonacci, init_logger};

fn main() {
init_logger();

let ns = [100, 1000, 10000, 50000];
benchmark(bench_fibonacci, &ns, "../benchmark_outputs/fibonacci_sp1.csv", "n");
benchmark_v2(bench_fibonacci, &ns, "../benchmark_outputs/fibonacci_sp1.csv", "n");
}
4 changes: 2 additions & 2 deletions sp1/src/bin/sha2.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use utils::benchmark;
use utils::benchmark_v2;
use sp1_script::{benchmark_sha2, init_logger};

fn main() {
init_logger();

let lengths = [32, 256, 512, 1024, 2048];
benchmark(benchmark_sha2, &lengths, "../benchmark_outputs/sha2_sp1.csv", "byte length");
benchmark_v2(benchmark_sha2, &lengths, "../benchmark_outputs/sha2_sp1.csv", "byte length");
}
4 changes: 2 additions & 2 deletions sp1/src/bin/sha3.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use utils::benchmark;
use utils::benchmark_v2;
use sp1_script::{benchmark_sha3, init_logger};

fn main() {
init_logger();

let lengths = [32, 256, 512, 1024, 2048];
benchmark(benchmark_sha3, &lengths, "../benchmark_outputs/sha3_sp1.csv", "byte length");
benchmark_v2(benchmark_sha3, &lengths, "../benchmark_outputs/sha3_sp1.csv", "byte length");
}
4 changes: 2 additions & 2 deletions sp1/src/bin/sha3_chain.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use utils::benchmark;
use utils::benchmark_v2;
use sp1_script::{benchmark_sha3_chain, init_logger};

fn main() {
init_logger();

let iters = [230, 460, /* 920, 1840, 3680 */];
benchmark(benchmark_sha3_chain, &iters, "../benchmark_outputs/sha3_chain_sp1.csv", "iters");
benchmark_v2(benchmark_sha3_chain, &iters, "../benchmark_outputs/sha3_chain_sp1.csv", "iters");
}
58 changes: 41 additions & 17 deletions sp1/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,23 @@ pub fn init_logger() {
sp1_core_machine::utils::setup_logger();
}

pub fn benchmark_with_shard_size(func: fn(u32) -> (Duration, usize), iters: &[u32], shard_sizes: &[usize], file_name: &str, input_name: &str) {
pub fn benchmark_with_shard_size(func: fn(u32) -> (Duration, usize, u64), iters: &[u32], shard_sizes: &[usize], file_name: &str, input_name: &str) {
assert_eq!(iters.len(), shard_sizes.len());
let mut info = Vec::new();
for bench_i in 0..iters.len() {
println!("benchmark_with_shard_size start, bench_i: {}, shard_size: {}", bench_i, shard_sizes[bench_i]);
std::env::set_var("SHARD_SIZE", format!("{}", shard_sizes[bench_i]));
let duration_and_size = func(iters[bench_i]);
info.push(duration_and_size);
let duration_and_size_and_cycles = func(iters[bench_i]);
info.push(duration_and_size_and_cycles);
println!(
"benchmark_with_shard_size end, duration: {:?}, shard_size: {}",
duration_and_size.0.as_secs_f64(), duration_and_size.1,
duration_and_size_and_cycles.0.as_secs_f64(), duration_and_size_and_cycles.1,
);
}
utils::write_csv(file_name, input_name, iters, &info);
utils::write_csv_v2(file_name, input_name, iters, &info);
}

pub fn benchmark_sha2_chain(iters: u32) -> (Duration, usize) {
pub fn benchmark_sha2_chain(iters: u32) -> (Duration, usize, u64) {
let client = ProverClient::builder().cpu().build();
let (pk, vk) = client.setup(SHA2_CHAIN_ELF);

Expand All @@ -50,10 +50,14 @@ pub fn benchmark_sha2_chain(iters: u32) -> (Duration, usize) {

client.verify(&proof, &vk).expect("verification failed");

(duration, size(&proof))
// Execute the program using the `ProverClient.execute` method, without generating a proof.
let (_, report) = client.execute(SHA2_CHAIN_ELF, &stdin).run().unwrap();
println!("executed program with {} cycles", report.total_instruction_count());

(duration, size(&proof), report.total_instruction_count())
}

pub fn benchmark_sha3_chain(iters: u32) -> (Duration, usize) {
pub fn benchmark_sha3_chain(iters: u32) -> (Duration, usize, u64) {
let client = ProverClient::builder().cpu().build();
let (pk, vk) = client.setup(SHA3_CHAIN_ELF);

Expand All @@ -71,10 +75,14 @@ pub fn benchmark_sha3_chain(iters: u32) -> (Duration, usize) {

client.verify(&proof, &vk).expect("verification failed");

(duration, size(&proof))
// Execute the program using the `ProverClient.execute` method, without generating a proof.
let (_, report) = client.execute(SHA3_CHAIN_ELF, &stdin).run().unwrap();
println!("executed program with {} cycles", report.total_instruction_count());

(duration, size(&proof), report.total_instruction_count())
}

pub fn benchmark_sha2(num_bytes: usize) -> (Duration, usize) {
pub fn benchmark_sha2(num_bytes: usize) -> (Duration, usize, u64) {
let client = ProverClient::builder().cpu().build();
let (pk, vk) = client.setup(SHA2_ELF);

Expand All @@ -91,10 +99,14 @@ pub fn benchmark_sha2(num_bytes: usize) -> (Duration, usize) {

client.verify(&proof, &vk).expect("verification failed");

(duration, size(&proof))
// Execute the program using the `ProverClient.execute` method, without generating a proof.
let (_, report) = client.execute(SHA2_ELF, &stdin).run().unwrap();
println!("executed program with {} cycles", report.total_instruction_count());

(duration, size(&proof), report.total_instruction_count())
}

pub fn benchmark_sha3(num_bytes: usize) -> (Duration, usize) {
pub fn benchmark_sha3(num_bytes: usize) -> (Duration, usize, u64) {
let client = ProverClient::builder().cpu().build();
let (pk, vk) = client.setup(SHA3_ELF);

Expand All @@ -111,10 +123,14 @@ pub fn benchmark_sha3(num_bytes: usize) -> (Duration, usize) {

client.verify(&proof, &vk).expect("verification failed");

(duration, size(&proof))
// Execute the program using the `ProverClient.execute` method, without generating a proof.
let (_, report) = client.execute(SHA3_ELF, &stdin).run().unwrap();
println!("executed program with {} cycles", report.total_instruction_count());

(duration, size(&proof), report.total_instruction_count())
}

pub fn bench_fibonacci(n: u32) -> (Duration, usize) {
pub fn bench_fibonacci(n: u32) -> (Duration, usize, u64) {
let client = ProverClient::builder().cpu().build();
let (pk, vk) = client.setup(FIBONACCI_ELF);

Expand All @@ -130,10 +146,14 @@ pub fn bench_fibonacci(n: u32) -> (Duration, usize) {

client.verify(&proof, &vk).expect("verification failed");

(duration, size(&proof))
// Execute the program using the `ProverClient.execute` method, without generating a proof.
let (_, report) = client.execute(FIBONACCI_ELF, &stdin).run().unwrap();
println!("executed program with {} cycles", report.total_instruction_count());

(duration, size(&proof), report.total_instruction_count())
}

pub fn bench_bigmem(value: u32) -> (Duration, usize) {
pub fn bench_bigmem(value: u32) -> (Duration, usize, u64) {
let client = ProverClient::builder().cpu().build();
let (pk, vk) = client.setup(BIGMEM_ELF);

Expand All @@ -149,5 +169,9 @@ pub fn bench_bigmem(value: u32) -> (Duration, usize) {

client.verify(&proof, &vk).expect("verification failed");

(duration, size(&proof))
// Execute the program using the `ProverClient.execute` method, without generating a proof.
let (_, report) = client.execute(BIGMEM_ELF, &stdin).run().unwrap();
println!("executed program with {} cycles", report.total_instruction_count());

(duration, size(&proof), report.total_instruction_count())
}
12 changes: 6 additions & 6 deletions sp1/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
use utils::benchmark;
use utils::benchmark_v2;
use sp1_script::*;

fn main() {
init_logger();

let iters = [230, 460, /* 920, 1840, 3680 */];
benchmark(benchmark_sha3_chain, &iters, "../benchmark_outputs/sha3_chain_sp1.csv", "iters");
benchmark_v2(benchmark_sha3_chain, &iters, "../benchmark_outputs/sha3_chain_sp1.csv", "iters");

let lengths = [32, 256, 512, 1024, 2048];
benchmark(benchmark_sha2, &lengths, "../benchmark_outputs/sha2_sp1.csv", "byte length");
benchmark(benchmark_sha3, &lengths, "../benchmark_outputs/sha3_sp1.csv", "byte length");
benchmark_v2(benchmark_sha2, &lengths, "../benchmark_outputs/sha2_sp1.csv", "byte length");
benchmark_v2(benchmark_sha3, &lengths, "../benchmark_outputs/sha3_sp1.csv", "byte length");

let ns = [100, 1000, 10000, 50000];
benchmark(bench_fibonacci, &ns, "../benchmark_outputs/fibonacci_sp1.csv", "n");
benchmark_v2(bench_fibonacci, &ns, "../benchmark_outputs/fibonacci_sp1.csv", "n");

let values = [5u32];
benchmark(bench_bigmem, &values, "../benchmark_outputs/bigmem_sp1.csv", "value");
benchmark_v2(bench_bigmem, &values, "../benchmark_outputs/bigmem_sp1.csv", "value");

// 1 Shard
let iters = [230, 460, /* 920, 1840, 3680 */];
Expand Down
18 changes: 18 additions & 0 deletions utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,24 @@ pub fn write_csv<T: Display>(file: &str, input_name: &str, inputs: &[T], results
});
}

pub fn benchmark_v2<T: Display + Clone>(func: fn(T) -> (Duration, usize, u64), inputs: &[T], file: &str, input_name: &str) {
let mut results = Vec::new();
for input in inputs {
let (duration, size, cycles) = func(input.clone());
results.push((duration, size, cycles));
}

write_csv_v2(file, input_name, inputs, &results);
}

pub fn write_csv_v2<T: Display>(file: &str, input_name: &str, inputs: &[T], results: &[(Duration, usize, u64)]) {
let mut file = File::create(file).unwrap();
file.write_all(format!("{},prover time (ms),proof size (bytes),cycles\n", input_name).as_bytes()).unwrap();
inputs.iter().zip(results).for_each(|(input, (duration, size, cycles))| {
file.write_all(format!("{},{},{},{}\n", input, duration.as_millis(), size, cycles).as_bytes()).unwrap();
});
}

pub fn size<T: Serialize>(item: &T) -> usize {
bincode::serialized_size(item).unwrap() as usize
}
Loading