Skip to content

Commit 8f1ef00

Browse files
committed
feat: env LOG2_PADDED_HEIGHT_METHOD for proving capability
A previous commit made calculation of log2(padded-height) about 4x slower for ProofBuilder and ProverJob because it used a different calculation method. This commit returns to the baseline method and speed, but enables runtime selection via environment variable. By default the log2(padded-height) is calculated using VmState::run(). A more accurate but slower way is to use VM::trace_execution_of_state(). This is typically about 4x slower. And the fastest method is to skip running the program entirely. But that option is only available when running unit tests. These methods can be selected at runtime: ``` LOG2_PADDED_HEIGHT_METHOD=trace neptune-core <args> LOG2_PADDED_HEIGHT_METHOD=run neptune-core <args> LOG2_PADDED_HEIGHT_METHOD=skip cargo test <args> ```
1 parent 127a696 commit 8f1ef00

File tree

1 file changed

+48
-5
lines changed

1 file changed

+48
-5
lines changed

src/models/state/vm_proving_capability.rs

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,9 @@ impl VmProvingCapability {
8989
///
9090
/// The program is executed inside spawn_blocking() so it will not block
9191
/// concurrent async tasks on the same thread.
92+
///
93+
/// see `check_if_capable` for description of LOG2_PADDED_HEIGHT_METHOD
94+
/// env var that affects this method.
9295
pub async fn check_if_capable_async(
9396
&self,
9497
program: Program,
@@ -110,6 +113,26 @@ impl VmProvingCapability {
110113
/// The program is executed in blocking fashion so it will block concurrent
111114
/// async tasks on the same thread. async callers should use
112115
/// `check_if_capable_async()` instead.
116+
///
117+
/// #### environment variable: LOG2_PADDED_HEIGHT_METHOD
118+
///
119+
/// By default the log2(padded-height) is calculated using VmState::run().
120+
///
121+
/// A more accurate but slower way is to use VM::trace_execution_of_state().
122+
/// This is typically about 4x slower.
123+
///
124+
/// And the fastest method is to skip running the program entirely. But
125+
/// that option is only available when running unit tests.
126+
///
127+
/// These methods can be selected at runtime:
128+
///
129+
/// ```text
130+
/// LOG2_PADDED_HEIGHT_METHOD=trace neptune-core <args>
131+
/// LOG2_PADDED_HEIGHT_METHOD=run neptune-core <args>
132+
///
133+
/// # only for unit tests
134+
/// LOG2_PADDED_HEIGHT_METHOD=skip cargo test <args>
135+
/// ```
113136
pub fn check_if_capable(
114137
&self,
115138
program: Program,
@@ -123,21 +146,41 @@ impl VmProvingCapability {
123146
/// executes the supplied program triple to obtain the log2(padded_height)
124147
///
125148
/// perf: this is an expensive operation; it may be under a second up to
126-
/// several seconds
149+
/// 5+ seconds depending on the program's complexity.
127150
///
128151
/// The program is executed in blocking fashion so it will block concurrent
129152
/// async tasks on the same thread. async callers should use
130-
/// `obtain_log2_padded_height_async()` instead.
153+
/// tokio's spawn_blocking() to wrap this fn.
131154
fn obtain_log2_padded_height(
132155
program: Program,
133156
claim: Claim,
134157
nondeterminism: NonDeterminism,
135158
) -> Result<u32, VmProvingCapabilityError> {
136159
crate::macros::log_scope_duration!(crate::macros::fn_name!());
137160

138-
let vmstate = VMState::new(program, claim.input.into(), nondeterminism);
139-
let (aet, _) = VM::trace_execution_of_state(vmstate)?;
140-
Ok(aet.padded_height().ilog2())
161+
let mut vmstate = VMState::new(program, claim.input.into(), nondeterminism);
162+
163+
let method =
164+
std::env::var("LOG2_PADDED_HEIGHT_METHOD").unwrap_or_else(|_| "run".to_string());
165+
166+
match method.as_str() {
167+
"trace" => {
168+
// this is about 4x slower.
169+
let (aet, _) = VM::trace_execution_of_state(vmstate)?;
170+
Ok(aet.padded_height().ilog2())
171+
}
172+
173+
// this is fastest, as it avoids running program at all.
174+
// but only supported for unit tests, as a "turbo" mode.
175+
#[cfg(test)]
176+
"skip" => Ok(0),
177+
178+
"run" | &_ => {
179+
// this is baseline
180+
vmstate.run().unwrap();
181+
Ok(vmstate.cycle_count.next_power_of_two().ilog2())
182+
}
183+
}
141184
}
142185

143186
/// automatically detect the log2_padded_height for this device.

0 commit comments

Comments
 (0)