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

Add a benchmark/profiler to count cycles in demo-prover #577

Merged
merged 51 commits into from
Aug 16, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
51 commits
Select commit Hold shift + click to select a range
7a7f7de
extract data_gen into its own function
Jul 7, 2023
9a76147
Merge branch 'main' into dub/prover_bench
Jul 10, 2023
cec4f57
temp push
Jul 11, 2023
d5ee9d4
add prover cycles
Jul 11, 2023
0d26271
include multiple transactions
Jul 11, 2023
bfcf224
reduce txns
Jul 11, 2023
bfea7de
temp switch
Jul 13, 2023
4b24b1b
cycle tracker macro
Jul 20, 2023
e80fc78
another temp commit
Jul 26, 2023
8d3cac5
Merge branch 'main' into dub/prover_bench
Jul 26, 2023
d1e0008
Merge branch 'nightly' into dub/prover_bench
Jul 26, 2023
b9bd4d6
merge main and nightly
Jul 27, 2023
4bf1528
use run_without_prover for bench/profile
Jul 27, 2023
2fe0041
working metrics
Jul 28, 2023
f6193e4
add zk-cycle-utils. missed it
Jul 28, 2023
d2e080d
temporary commit
Jul 31, 2023
994912b
pass features to risc0 vm
Aug 1, 2023
aba3917
feature gate all the bench things
Aug 1, 2023
3ce4624
cleanup and testing
Aug 1, 2023
12ecf07
check in readme
Aug 1, 2023
b70e58e
checking some cargo locks in prep for merge
Aug 2, 2023
367d93d
Merge branch 'nightly' into dub/prover_bench
Aug 3, 2023
e5627d0
some fixes
Aug 3, 2023
2a9c7b1
revert rollup config toml
Aug 3, 2023
1818c58
working merge
Aug 3, 2023
0b4dbf7
cleanup and documentation
Aug 3, 2023
1011680
Merge branch 'nightly' into dub/prover_bench
Aug 3, 2023
26aa682
simplify the macro for generating the wrapped function
Aug 3, 2023
4c6ec0e
format fixes
Aug 4, 2023
7190a9a
README changes
Aug 4, 2023
92dc766
simplify code as per lint
Aug 4, 2023
d2bdff5
lint fix
Aug 4, 2023
2d9c6d5
add profiler to analyze trace
Aug 10, 2023
9ef3cc7
Merge branch 'nightly' into dub/prover_bench
Aug 10, 2023
cb641ef
fix cargo lock
Aug 10, 2023
3c54d90
really fix Cargo.lock
Aug 10, 2023
0eacdae
trace options
Aug 11, 2023
768eb77
cargo fmt
Aug 11, 2023
a07cd15
fix log error
Aug 11, 2023
f4fad22
Merge branch 'nightly' into dub/prover_bench
Aug 14, 2023
e8aae75
Merge branch 'nightly' into dub/prover_bench
Aug 14, 2023
87681d2
fix prover bench
Aug 14, 2023
4e94af8
fix nightly merge conflict
Aug 14, 2023
e31f995
add docs and address comments
Aug 14, 2023
993851e
prover bench readme
Aug 16, 2023
b11f60c
add a skip proving option to demo-prover
Aug 16, 2023
f708bb6
add tests for macros
Aug 16, 2023
c708fcf
demo prover format
Aug 16, 2023
817aaf5
Merge branch 'nightly' into dub/prover_bench
Aug 16, 2023
6d4b7be
remove commented code
Aug 16, 2023
2a3369f
formatting
Aug 16, 2023
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
24 changes: 15 additions & 9 deletions adapters/risc0/src/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,25 @@ pub struct Risc0Host<'a> {
}

impl<'a> Risc0Host<'a> {
#[cfg(not(feature = "bench"))]
pub fn new(elf: &'a [u8]) -> Self {
#[cfg(not(feature = "bench"))]
let default_env = ExecutorEnvBuilder::default();
#[cfg(feature = "bench")]
let mut default_env = ExecutorEnvBuilder::default();
#[cfg(feature = "bench")]
{
let metrics_syscall_name = get_syscall_name();
default_env.io_callback(metrics_syscall_name, metrics_callback);

let cycles_syscall_name = get_syscall_name_cycles();
default_env.io_callback(cycles_syscall_name, cycle_count_callback);
Self {
env: RefCell::new(default_env),
elf,
}
}

#[cfg(feature = "bench")]
pub fn new(elf: &'a [u8]) -> Self {
let mut default_env = ExecutorEnvBuilder::default();

let metrics_syscall_name = get_syscall_name();
default_env.io_callback(metrics_syscall_name, metrics_callback);

let cycles_syscall_name = get_syscall_name_cycles();
default_env.io_callback(cycles_syscall_name, cycle_count_callback);

Self {
env: RefCell::new(default_env),
Expand Down
23 changes: 8 additions & 15 deletions examples/demo-prover/host/benches/prover_bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use anyhow::Context;
use const_rollup_config::{ROLLUP_NAMESPACE_RAW, SEQUENCER_DA_ADDRESS};
use demo_stf::app::{App, DefaultPrivateKey};
use demo_stf::genesis_config::create_demo_genesis_config;
use jupiter::da_service::{CelestiaService, DaServiceConfig};
use jupiter::da_service::CelestiaService;
use jupiter::types::{FilteredCelestiaBlock, NamespaceId};
use jupiter::verifier::address::CelestiaAddress;
use jupiter::verifier::{ChainValidityCondition, RollupParams};
Expand All @@ -18,13 +18,12 @@ use log4rs::config::{Appender, Config, Root};
use methods::ROLLUP_ELF;
use regex::Regex;
use risc0_adapter::host::Risc0Host;
use serde::Deserialize;
use sov_modules_api::PrivateKey;
use sov_rollup_interface::services::da::DaService;
use sov_rollup_interface::stf::StateTransitionFunction;
use sov_rollup_interface::zk::ZkvmHost;
use sov_state::storage::Storage;
use sov_stf_runner::{from_toml_path, Config as RunnerConfig};
use sov_stf_runner::{from_toml_path, RollupConfig};
use tempfile::TempDir;

#[derive(Debug)]
Expand All @@ -33,12 +32,6 @@ struct RegexAppender {
file: Arc<Mutex<File>>,
}

#[derive(Debug, serde::Serialize, serde::Deserialize)]
struct HexKey {
hex_priv_key: String,
address: String,
}

impl RegexAppender {
fn new(pattern: &str, file_path: &str) -> Self {
if Path::new(file_path).exists() {
Expand Down Expand Up @@ -96,12 +89,12 @@ use std::collections::HashMap;
#[cfg(feature = "bench")]
use risc0_adapter::metrics::GLOBAL_HASHMAP;

#[derive(Debug, Clone, PartialEq, Deserialize)]
pub struct RollupConfig {
pub start_height: u64,
pub da: DaServiceConfig,
pub runner: RunnerConfig,
}
// #[derive(Debug, Clone, PartialEq, Deserialize)]
dubbelosix marked this conversation as resolved.
Show resolved Hide resolved
// pub struct RollupConfig {
// pub start_height: u64,
// pub da: DaServiceConfig,
// pub runner: RunnerConfig,
// }

// The rollup stores its data in the namespace b"sov-test" on Celestia
const ROLLUP_NAMESPACE: NamespaceId = NamespaceId(ROLLUP_NAMESPACE_RAW);
Expand Down
17 changes: 12 additions & 5 deletions examples/demo-prover/methods/build.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
use std::collections::HashMap;

fn main() {
#[cfg(not(feature = "bench"))]
let guest_pkg_to_options = HashMap::new();
#[cfg(feature = "bench")]
let guest_pkg_to_options = get_guest_options();
risc0_build::embed_methods_with_options(guest_pkg_to_options);
}

#[cfg(not(feature = "bench"))]
fn get_guest_options() -> HashMap<&'static str, risc0_build::GuestOptions> {
HashMap::new()
}

#[cfg(feature = "bench")]
fn get_guest_options() -> HashMap<&'static str, risc0_build::GuestOptions> {
let mut guest_pkg_to_options = HashMap::new();
#[cfg(feature = "bench")]
guest_pkg_to_options.insert(
"sov-demo-prover-guest",
risc0_build::GuestOptions {
features: vec!["bench".to_string()],
std: true,
},
);
risc0_build::embed_methods_with_options(guest_pkg_to_options);
guest_pkg_to_options
}
14 changes: 14 additions & 0 deletions utils/zk-cycle-macros/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,23 @@
#![deny(missing_docs)]
#![doc = include_str!("../README.md")]
extern crate proc_macro;

use proc_macro::TokenStream;
use quote::quote;
use syn::{parse_macro_input, ItemFn};

/// This macro is used to annotate functions that we want to track the number of riscV cycles being
/// generated inside the VM. The purpose of the this macro is to measure how many cycles a rust
/// function takes because prover time is directly proportional to the number of riscv cycles
/// generated. It does this by making use of a risc0 provided function
/// ```
/// risc0_zkvm::guest::env::get_cycle_count
/// ```
/// The macro essentially generates new function with the same name by wrapping the body with a get_cycle_count
/// at the beginning and end of the function, subtracting it and then emitting it out using the
/// a custom syscall that is generated when the prover is run with the `bench` feature.
/// `send_recv_slice` is used to communicate and pass a slice to the syscall that we defined.
/// The handler for the syscall can be seen in adapters/risc0/src/host.rs and adapters/risc0/src/metrics.rs
#[proc_macro_attribute]
pub fn cycle_tracker(_attr: TokenStream, item: TokenStream) -> TokenStream {
let input = parse_macro_input!(item as ItemFn);
Expand Down