Skip to content

Commit 198eb39

Browse files
authored
fix(new-execution): replace segmentation strategy with segmentation limits (#1901)
Resolves INT-4316
1 parent a35aa57 commit 198eb39

File tree

12 files changed

+86
-182
lines changed

12 files changed

+86
-182
lines changed

benchmarks/execute/benches/execute.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -245,16 +245,10 @@ fn benchmark_execute_metered(bencher: Bencher, program: &str) {
245245
let vm_config = ExecuteConfig::default();
246246
let exe = load_program_executable(program).expect("Failed to load program executable");
247247

248-
let segmentation_strategy = &vm_config.as_ref().segmentation_strategy;
249-
250248
let (ctx, executor_idx_to_air_idx) = metering_setup();
251-
let ctx = ctx
252-
.clone()
253-
.with_max_trace_height(segmentation_strategy.max_trace_height() as u32)
254-
.with_max_cells(segmentation_strategy.max_cells());
255249
let interpreter = InterpretedInstance::new(vm_config, exe).unwrap();
256250

257-
(interpreter, vec![], ctx, executor_idx_to_air_idx)
251+
(interpreter, vec![], ctx.clone(), executor_idx_to_air_idx)
258252
})
259253
.bench_values(|(interpreter, input, ctx, executor_idx_to_air_idx)| {
260254
interpreter

benchmarks/prove/src/util.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
use std::{path::PathBuf, sync::Arc};
1+
use std::path::PathBuf;
22

33
use clap::{command, Parser};
44
use eyre::Result;
55
use openvm_benchmarks_utils::{build_elf, get_programs_dir};
66
use openvm_circuit::arch::{
7-
instructions::exe::VmExe, verify_single, DefaultSegmentationStrategy, InsExecutorE1,
8-
InsExecutorE2, InstructionExecutor, MatrixRecordArena, SystemConfig, VmBuilder, VmConfig,
9-
VmExecutionConfig,
7+
execution_mode::metered::segment_ctx::SegmentationLimits, instructions::exe::VmExe,
8+
verify_single, InsExecutorE1, InsExecutorE2, InstructionExecutor, MatrixRecordArena,
9+
SystemConfig, VmBuilder, VmConfig, VmExecutionConfig,
1010
};
1111
use openvm_native_circuit::{NativeConfig, NativeCpuBuilder};
1212
use openvm_native_compiler::conversion::CompilerOptions;
@@ -86,9 +86,9 @@ impl BenchmarkCli {
8686

8787
app_vm_config.as_mut().profiling = self.profiling;
8888
if let Some(max_segment_length) = self.max_segment_length {
89-
app_vm_config.as_mut().set_segmentation_strategy(Arc::new(
90-
DefaultSegmentationStrategy::new_with_max_segment_len(max_segment_length),
91-
));
89+
app_vm_config.as_mut().set_segmentation_limits(
90+
SegmentationLimits::default().with_max_trace_height(max_segment_length as u32),
91+
);
9292
}
9393
AppConfig {
9494
app_fri_params: FriParameters::standard_with_100_bits_conjectured_security(

crates/sdk/guest/fib/src/main.rs

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,31 @@
33

44
openvm::entry!(main);
55

6-
pub fn main() {
7-
// arbitrary n that results in more than 1 segment
8-
let n = core::hint::black_box((1 << 5) + 4);
6+
fn fibonacci(n: u32) -> (u32, u32) {
7+
if n <= 1 {
8+
return (0, n);
9+
}
910
let mut a: u32 = 0;
1011
let mut b: u32 = 1;
11-
for _ in 1..n {
12+
for _ in 2..=n {
1213
let sum = a + b;
1314
a = b;
1415
b = sum;
1516
}
17+
(a, b)
18+
}
19+
20+
pub fn main() {
21+
// arbitrary n that results in more than 1 segment
22+
let n = core::hint::black_box(1 << 5);
23+
24+
let mut a = 0;
25+
let mut b = 0;
26+
// calculate nth fibonacci number n times
27+
for _ in 0..n {
28+
(a, b) = fibonacci(n);
29+
}
30+
1631
if a == 0 {
1732
panic!();
1833
}

crates/vm/src/arch/config.rs

Lines changed: 13 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
use std::{fs::File, io::Write, path::Path, sync::Arc};
1+
use std::{fs::File, io::Write, path::Path};
22

33
use derive_new::new;
4+
use getset::{Setters, WithSetters};
45
use openvm_instructions::NATIVE_AS;
56
use openvm_poseidon2_air::Poseidon2Config;
67
use openvm_stark_backend::{
@@ -10,14 +11,11 @@ use openvm_stark_backend::{
1011
};
1112
use serde::{de::DeserializeOwned, Deserialize, Serialize};
1213

13-
use super::{
14-
segmentation_strategy::{DefaultSegmentationStrategy, SegmentationStrategy},
15-
AnyEnum, VmChipComplex, PUBLIC_VALUES_AIR_ID,
16-
};
14+
use super::{AnyEnum, VmChipComplex, PUBLIC_VALUES_AIR_ID};
1715
use crate::{
1816
arch::{
19-
AirInventory, AirInventoryError, Arena, ChipInventoryError, ExecutorInventory,
20-
ExecutorInventoryError,
17+
execution_mode::metered::segment_ctx::SegmentationLimits, AirInventory, AirInventoryError,
18+
Arena, ChipInventoryError, ExecutorInventory, ExecutorInventoryError,
2119
},
2220
system::{
2321
memory::{merkle::public_values::PUBLIC_VALUES_AS, num_memory_airs, CHUNK},
@@ -172,9 +170,10 @@ impl MemoryConfig {
172170

173171
/// System-level configuration for the virtual machine. Contains all configuration parameters that
174172
/// are managed by the architecture, including configuration for continuations support.
175-
#[derive(Debug, Clone, Serialize, Deserialize)]
173+
#[derive(Debug, Clone, Serialize, Deserialize, Setters, WithSetters)]
176174
pub struct SystemConfig {
177175
/// The maximum constraint degree any chip is allowed to use.
176+
#[getset(set_with = "pub")]
178177
pub max_constraint_degree: usize,
179178
/// True if the VM is in continuation mode. In this mode, an execution could be segmented and
180179
/// each segment is proved by a proof. Each proof commits the before and after state of the
@@ -195,15 +194,12 @@ pub struct SystemConfig {
195194
/// Whether to collect detailed profiling metrics.
196195
/// **Warning**: this slows down the runtime.
197196
pub profiling: bool,
198-
/// Segmentation strategy
197+
/// Segmentation limits
199198
/// This field is skipped in serde as it's only used in execution and
200199
/// not needed after any serialize/deserialize.
201-
#[serde(skip, default = "get_default_segmentation_strategy")]
202-
pub segmentation_strategy: Arc<dyn SegmentationStrategy>,
203-
}
204-
205-
pub fn get_default_segmentation_strategy() -> Arc<DefaultSegmentationStrategy> {
206-
Arc::new(DefaultSegmentationStrategy::default())
200+
#[serde(skip, default = "SegmentationLimits::default")]
201+
#[getset(set = "pub")]
202+
pub segmentation_limits: SegmentationLimits,
207203
}
208204

209205
impl SystemConfig {
@@ -212,7 +208,6 @@ impl SystemConfig {
212208
mut memory_config: MemoryConfig,
213209
num_public_values: usize,
214210
) -> Self {
215-
let segmentation_strategy = get_default_segmentation_strategy();
216211
assert!(
217212
memory_config.clk_max_bits <= 29,
218213
"Timestamp max bits must be <= 29 for LessThan to work in 31-bit field"
@@ -223,8 +218,8 @@ impl SystemConfig {
223218
continuation_enabled: false,
224219
memory_config,
225220
num_public_values,
226-
segmentation_strategy,
227221
profiling: false,
222+
segmentation_limits: SegmentationLimits::default(),
228223
}
229224
}
230225

@@ -236,11 +231,6 @@ impl SystemConfig {
236231
)
237232
}
238233

239-
pub fn with_max_constraint_degree(mut self, max_constraint_degree: usize) -> Self {
240-
self.max_constraint_degree = max_constraint_degree;
241-
self
242-
}
243-
244234
pub fn with_continuations(mut self) -> Self {
245235
self.continuation_enabled = true;
246236
self
@@ -258,16 +248,10 @@ impl SystemConfig {
258248
}
259249

260250
pub fn with_max_segment_len(mut self, max_segment_len: usize) -> Self {
261-
self.segmentation_strategy = Arc::new(
262-
DefaultSegmentationStrategy::new_with_max_segment_len(max_segment_len),
263-
);
251+
self.segmentation_limits.max_trace_height = max_segment_len as u32;
264252
self
265253
}
266254

267-
pub fn set_segmentation_strategy(&mut self, strategy: Arc<dyn SegmentationStrategy>) {
268-
self.segmentation_strategy = strategy;
269-
}
270-
271255
pub fn with_profiling(mut self) -> Self {
272256
self.profiling = true;
273257
self

crates/vm/src/arch/execution_mode/metered/ctx.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,16 @@ use super::{
99
};
1010
use crate::{
1111
arch::{
12-
execution_mode::{E1ExecutionCtx, E2ExecutionCtx},
12+
execution_mode::{
13+
metered::segment_ctx::SegmentationLimits, E1ExecutionCtx, E2ExecutionCtx,
14+
},
1315
VmSegmentState,
1416
},
1517
system::memory::{dimensions::MemoryDimensions, online::GuestMemory},
1618
};
1719

1820
pub const DEFAULT_PAGE_BITS: usize = 6;
19-
const DEFAULT_SEGMENT_CHECK_INSNS: u64 = 1000;
21+
pub const DEFAULT_SEGMENT_CHECK_INSNS: u64 = 1000;
2022

2123
#[derive(Clone, Debug, WithSetters)]
2224
pub struct MeteredCtx<const PAGE_BITS: usize = DEFAULT_PAGE_BITS> {
@@ -45,6 +47,7 @@ impl<const PAGE_BITS: usize> MeteredCtx<PAGE_BITS> {
4547
air_names: Vec<String>,
4648
widths: Vec<usize>,
4749
interactions: Vec<usize>,
50+
segmentation_limits: SegmentationLimits,
4851
) -> Self {
4952
let (trace_heights, is_trace_height_constant): (Vec<u32>, Vec<bool>) =
5053
constant_trace_heights
@@ -84,7 +87,8 @@ impl<const PAGE_BITS: usize> MeteredCtx<PAGE_BITS> {
8487
air_names[memory_ctx.adapter_offset]
8588
);
8689

87-
let segmentation_ctx = SegmentationCtx::new(air_names, widths, interactions);
90+
let segmentation_ctx =
91+
SegmentationCtx::new(air_names, widths, interactions, segmentation_limits);
8892

8993
let mut ctx = Self {
9094
trace_heights,

crates/vm/src/arch/execution_mode/metered/segment_ctx.rs

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use getset::WithSetters;
12
use openvm_stark_backend::p3_field::PrimeField32;
23
use p3_baby_bear::BabyBear;
34
use serde::{Deserialize, Serialize};
@@ -13,10 +14,13 @@ pub struct Segment {
1314
pub trace_heights: Vec<u32>,
1415
}
1516

16-
#[derive(Clone, Copy, Debug)]
17+
#[derive(Clone, Copy, Debug, WithSetters)]
1718
pub struct SegmentationLimits {
19+
#[getset(set_with = "pub")]
1820
pub max_trace_height: u32,
21+
#[getset(set_with = "pub")]
1922
pub max_cells: usize,
23+
#[getset(set_with = "pub")]
2024
pub max_interactions: usize,
2125
}
2226

@@ -40,7 +44,26 @@ pub struct SegmentationCtx {
4044
}
4145

4246
impl SegmentationCtx {
43-
pub fn new(air_names: Vec<String>, widths: Vec<usize>, interactions: Vec<usize>) -> Self {
47+
pub fn new(
48+
air_names: Vec<String>,
49+
widths: Vec<usize>,
50+
interactions: Vec<usize>,
51+
segmentation_limits: SegmentationLimits,
52+
) -> Self {
53+
Self {
54+
segments: Vec::new(),
55+
air_names,
56+
widths,
57+
interactions,
58+
segmentation_limits,
59+
}
60+
}
61+
62+
pub fn new_with_default_segmentation_limits(
63+
air_names: Vec<String>,
64+
widths: Vec<usize>,
65+
interactions: Vec<usize>,
66+
) -> Self {
4467
Self {
4568
segments: Vec::new(),
4669
air_names,

crates/vm/src/arch/mod.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ mod record_arena;
1414
/// Runtime execution and segmentation
1515
// TODO: rename this module
1616
pub mod segment;
17-
/// Strategy for determining when to segment VM execution
18-
pub mod segmentation_strategy;
1917
/// Top level [VmExecutor] and [VirtualMachine] constructor and API.
2018
pub mod vm;
2119

@@ -34,5 +32,4 @@ pub use integration_api::*;
3432
pub use openvm_instructions as instructions;
3533
pub use record_arena::*;
3634
pub use segment::*;
37-
pub use segmentation_strategy::*;
3835
pub use vm::*;

0 commit comments

Comments
 (0)