-
Notifications
You must be signed in to change notification settings - Fork 336
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
Add cargo risczero benchmark
command
#1302
Open
Cardosaum
wants to merge
29
commits into
main
Choose a base branch
from
cardosaum/add-cargo-risczero-benchmark
base: main
Could not load branches
Branch not found: {{ refName }}
Could not load tags
Nothing to show
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+306
−44
Open
Changes from all commits
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
3cf5675
feat: Add `cargo risczero benchmark` command
Cardosaum 4d9460b
Merge branch 'main' into cardosaum/add-cargo-risczero-benchmark
Cardosaum 81515a7
chore: format and sort dependencies
Cardosaum 5e90ce1
Refactor benchmark command to handle iterations
Cardosaum 11c9cbf
Add risc-zkvm-methods crate to workspace
Cardosaum 6baaca7
feat(cargo-build): Add ability to compile specified binaries.
Cardosaum f37a441
Add tracing, sha2, and serde_json dependencies
Cardosaum e13ac03
Add logic to download specified ELF binary from GitHub
Cardosaum 80a8ba8
Add words to cSpell dictionary
Cardosaum 852500c
Merge branch 'main' into cardosaum/add-cargo-risczero-benchmark
Cardosaum e652c76
chore: sort dependencies
Cardosaum e1a1bf0
create `risc0-zkvm-methods-core`
Cardosaum 25079ef
Remove unused code and logging initialization
Cardosaum 2856c23
Fix commit_slice argument in multi_test.rs
Cardosaum 945f73d
Merge branch 'main' into cardosaum/add-cargo-risczero-benchmark
Cardosaum cff8bcd
Revert "feat(cargo-build): Add ability to compile specified binaries."
Cardosaum 153b412
Add xtask to compile benchmark elf
Cardosaum fb37fcb
Remove unused dependencies in Cargo.toml
Cardosaum 9351187
Add empty line between imports and struct definition
Cardosaum 1ac3c65
cargo fmt
Cardosaum fb76458
cargo sort
Cardosaum 671a6bf
Add logic to generate `benchmark_elf.rs` during build time
Cardosaum 3f8833c
update cspell
Cardosaum 4572b4e
fix import for risc0-zkvm-methods-core in multiple crates
Cardosaum 09a25f0
cargo sort
Cardosaum 3f38719
Merge branch 'main' into cardosaum/add-cargo-risczero-benchmark
Cardosaum 735a6e6
fix path for risc0-zkvm-methods-core
Cardosaum 20ff703
Fix extra bracket in executor.rs example
Cardosaum 9486165
Update import statement in guest_run.rs
Cardosaum File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
src/benchmark_elf.rs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
// Copyright 2024 RISC Zero, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
use std::time::{Duration, Instant}; | ||
|
||
use anyhow::Result; | ||
use clap::Parser; | ||
use risc0_zkvm::{ | ||
get_prover_server, | ||
recursion::{join, lift}, | ||
ExecutorEnv, ExecutorImpl, ProverOpts, VerifierContext, | ||
}; | ||
use risc0_zkvm_methods_core::bench::{BenchmarkSpec, SpecWithIters}; | ||
|
||
/// `cargo risczero benchmark` | ||
#[derive(Parser)] | ||
pub struct BenchmarkCommand { | ||
/// Number of iterations. | ||
#[arg(short, long)] | ||
pub iterations: Option<u64>, | ||
|
||
/// Which hash function to use. | ||
#[arg(short = 'f', long, default_value_t = String::from("poseidon"), value_parser = ["poseidon", "sha-256"])] | ||
pub hashfn: String, | ||
|
||
/// Specify the segment po2. | ||
#[arg(short, long, default_value_t = 20)] | ||
po2: u32, | ||
} | ||
|
||
impl BenchmarkCommand { | ||
/// Execute this command. | ||
pub fn run(&self) -> Result<()> { | ||
if let Some(i) = self.iterations { | ||
return self.run_with_iterations(i); | ||
} else { | ||
let iterations = [ | ||
0, // warm-up | ||
1, // 16, 64K | ||
4 * 1024, // 17, 128K | ||
16 * 1024, // 18, 256K | ||
32 * 1024, // 19, 512K | ||
64 * 1024, // 20, 1M | ||
200 * 1024, // 21, 2M | ||
400 * 1024, // 22, 4M | ||
// 900 * 1024, // 23, 8M | ||
// 1400 * 1024, // 24, 16M | ||
]; | ||
|
||
for i in iterations.into_iter() { | ||
self.run_with_iterations(i)?; | ||
} | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
fn run_with_iterations(&self, iterations: u64) -> Result<()> { | ||
let bench_elf = include_bytes!("../benchmark_elf.rs"); | ||
let iterations = SpecWithIters(BenchmarkSpec::SimpleLoop, iterations); | ||
let env = ExecutorEnv::builder() | ||
.write(&iterations)? | ||
.segment_limit_po2(self.po2) | ||
.build()?; | ||
let mut exec = ExecutorImpl::from_elf(env, bench_elf)?; | ||
|
||
// Execute | ||
let (session, exec_duration) = with_duration(|| exec.run())?; | ||
|
||
let cycles = session.get_cycles()?; | ||
let segments = session.resolve()?; | ||
|
||
let opts = ProverOpts::default(); | ||
let ctx = VerifierContext::default(); | ||
let prover = get_prover_server(&opts)?; | ||
|
||
let mut lifts = vec![]; | ||
let mut prove_durations = vec![]; | ||
let mut lift_durations = vec![]; | ||
|
||
// Prove and Lift | ||
for segment in segments.iter() { | ||
let (receipt, receipt_duration) = | ||
with_duration(|| prover.prove_segment(&ctx, segment))?; | ||
prove_durations.push(receipt_duration); | ||
|
||
let (lift, lift_duration) = with_duration(|| lift(&receipt))?; | ||
lifts.push(lift); | ||
lift_durations.push(lift_duration); | ||
} | ||
|
||
let mut join_durations = vec![]; | ||
// Optional Join | ||
if segments.len() > 1 { | ||
let (_final, duration) = with_duration(|| join(&lifts[0], &lifts[1]))?; | ||
join_durations.push(duration); | ||
} | ||
|
||
println!("\nSTATS:"); | ||
println!("cycles: {}", cycles.1); | ||
println!("segments: {}", segments.len()); | ||
println!("exec: {exec_duration:?}"); | ||
println!("prove: {prove_durations:?}"); | ||
println!("lift: {lift_durations:?}"); | ||
println!("prove+lift: {:?}", prove_durations[0] + lift_durations[0]); | ||
println!("join: {join_durations:?}"); | ||
|
||
Ok(()) | ||
} | ||
} | ||
|
||
fn with_duration<T, F: FnOnce() -> Result<T>>(f: F) -> Result<(T, Duration)> { | ||
let start = Instant::now(); | ||
let result = f()?; | ||
let duration = start.elapsed(); | ||
Ok((result, duration)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
[package] | ||
name = "risc0-zkvm-methods-core" | ||
version = { workspace = true } | ||
edition = { workspace = true } | ||
|
||
[build-dependencies] | ||
risc0-build = { workspace = true } | ||
tracing-subscriber = { version = "0.3", features = ["env-filter"] } | ||
|
||
[dependencies] | ||
risc0-zkvm = { workspace = true, default-features = false } | ||
risc0-zkvm-platform = { workspace = true } | ||
serde = { version = "1.0", default-features = false, features = ["derive"] } |
File renamed without changes.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I might have found a way to compile it automatically during build time! 😄
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Giving credit where credit is due, I found it already in our code base :)