Skip to content

Commit e4e261b

Browse files
darth-cyroynalnarutokunxian-xia
authored
Extend Poseidon2 Chip for MULTI_OBSERVE (#5)
* Establish entry * Add opcode MULTI_OBSERVE * feat: rewrite sample_ext using new IR instruction * chore(doc): update comment * feat(openvm_circuit): store span info in cycle tracker for convenient debug info * Fix annotation, address spaces and register assignments * Consolidate MultiObserve chip debugging * Preserve testing environment * Fix constraints * Fix constraints * Fix constraints * Fix constraints * Fix constraint * Fix constraints * Complete constraints * Remove debug code * Remove multi observe logic from poseidon2 chip * Resolve minor build issue * Resolve minor build issue * set log_blowup=1 * Debug constraints * Debug constraints * Debug constraints * Debug constraints * Remove standalone chip * Populate poseidon2 columns * Reduce columns * Add more constraints * Remove debug flags * Grammatic correction * Add constraints * remove unrelated log file --------- Co-authored-by: Rohit Narurkar <[email protected]> Co-authored-by: kunxian xia <[email protected]>
1 parent ca36de3 commit e4e261b

File tree

22 files changed

+842
-55
lines changed

22 files changed

+842
-55
lines changed

crates/vm/src/arch/segment.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -282,16 +282,18 @@ impl<F: PrimeField32, VC: VmConfig<F>> ExecutionSegment<F, VC> {
282282
Some(SysPhantom::CtStart) =>
283283
{
284284
#[cfg(feature = "bench-metrics")]
285-
metrics
286-
.cycle_tracker
287-
.start(dsl_instr.cloned().unwrap_or("Default".to_string()))
285+
metrics.cycle_tracker.start(
286+
dsl_instr.cloned().unwrap_or("Default".to_string()),
287+
metrics.cycle_count,
288+
)
288289
}
289290
Some(SysPhantom::CtEnd) =>
290291
{
291292
#[cfg(feature = "bench-metrics")]
292-
metrics
293-
.cycle_tracker
294-
.end(dsl_instr.cloned().unwrap_or("Default".to_string()))
293+
metrics.cycle_tracker.end(
294+
dsl_instr.cloned().unwrap_or("Default".to_string()),
295+
metrics.cycle_count,
296+
)
295297
}
296298
_ => {}
297299
}

crates/vm/src/metrics/cycle_tracker/mod.rs

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,18 @@
1+
/// Stats for a nested span in the execution segment that is tracked by the [`CycleTracker`].
2+
#[derive(Clone, Debug, Default)]
3+
pub struct SpanInfo {
4+
/// The name of the span.
5+
pub tag: String,
6+
/// The cycle count at which the span starts.
7+
pub start: usize,
8+
}
9+
110
#[derive(Clone, Debug, Default)]
211
pub struct CycleTracker {
312
/// Stack of span names, with most recent at the end
4-
stack: Vec<String>,
13+
stack: Vec<SpanInfo>,
14+
/// Depth of the stack.
15+
depth: usize,
516
}
617

718
impl CycleTracker {
@@ -10,29 +21,41 @@ impl CycleTracker {
1021
}
1122

1223
pub fn top(&self) -> Option<&String> {
13-
self.stack.last()
24+
match self.stack.last() {
25+
Some(span) => Some(&span.tag),
26+
_ => None
27+
}
1428
}
1529

1630
/// Starts a new cycle tracker span for the given name.
17-
/// If a span already exists for the given name, it ends the existing span and pushes a new one
18-
/// to the vec.
19-
pub fn start(&mut self, mut name: String) {
31+
/// If a span already exists for the given name, it ends the existing span and pushes a new one to the vec.
32+
pub fn start(&mut self, mut name: String, cycles_count: usize) {
2033
// hack to remove "CT-" prefix
2134
if name.starts_with("CT-") {
2235
name = name.split_off(3);
2336
}
24-
self.stack.push(name);
37+
self.stack.push(SpanInfo {
38+
tag: name.clone(),
39+
start: cycles_count,
40+
});
41+
let padding = "│ ".repeat(self.depth);
42+
tracing::info!("{}┌╴{}", padding, name);
43+
self.depth += 1;
2544
}
2645

2746
/// Ends the cycle tracker span for the given name.
2847
/// If no span exists for the given name, it panics.
29-
pub fn end(&mut self, mut name: String) {
48+
pub fn end(&mut self, mut name: String, cycles_count: usize) {
3049
// hack to remove "CT-" prefix
3150
if name.starts_with("CT-") {
3251
name = name.split_off(3);
3352
}
34-
let stack_top = self.stack.pop();
35-
assert_eq!(stack_top.unwrap(), name, "Stack top does not match name");
53+
let SpanInfo { tag, start } = self.stack.pop().unwrap();
54+
assert_eq!(tag, name, "Stack top does not match name");
55+
self.depth -= 1;
56+
let padding = "│ ".repeat(self.depth);
57+
let span_cycles = cycles_count - start;
58+
tracing::info!("{}└╴{} cycles", padding, span_cycles);
3659
}
3760

3861
/// Ends the current cycle tracker span.
@@ -42,7 +65,11 @@ impl CycleTracker {
4265

4366
/// Get full name of span with all parent names separated by ";" in flamegraph format
4467
pub fn get_full_name(&self) -> String {
45-
self.stack.join(";")
68+
self.stack
69+
.iter()
70+
.map(|span_info| span_info.tag.clone())
71+
.collect::<Vec<String>>()
72+
.join(";")
4673
}
4774
}
4875

crates/vm/src/metrics/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ impl VmMetrics {
117117
.map(|(_, func)| (*func).clone())
118118
.unwrap();
119119
if pc == self.current_fn.start {
120-
self.cycle_tracker.start(self.current_fn.name.clone());
120+
self.cycle_tracker.start(self.current_fn.name.clone(), 0);
121121
} else {
122122
while let Some(name) = self.cycle_tracker.top() {
123123
if name == &self.current_fn.name {

crates/vm/src/system/memory/controller/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ pub const MERKLE_AIR_OFFSET: usize = 1;
5959
pub const BOUNDARY_AIR_OFFSET: usize = 0;
6060

6161
#[repr(C)]
62-
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
62+
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
6363
pub struct RecordId(pub usize);
6464

6565
pub type MemoryImage<F> = AddressMap<F, PAGE_SIZE>;

extensions/native/circuit/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ openvm-instructions = { workspace = true }
1919
openvm-rv32im-circuit = { workspace = true }
2020
openvm-native-compiler = { workspace = true }
2121

22-
2322
strum.workspace = true
2423
itertools.workspace = true
2524
tracing.workspace = true

extensions/native/circuit/src/extension.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use air::VerifyBatchBus;
1+
use poseidon2::air::VerifyBatchBus;
22
use alu_native_adapter::AluNativeAdapterChip;
33
use branch_native_adapter::BranchNativeAdapterChip;
44
use derive_more::derive::From;
@@ -30,7 +30,7 @@ use strum::IntoEnumIterator;
3030

3131
use crate::{
3232
adapters::{convert_adapter::ConvertAdapterChip, *},
33-
chip::NativePoseidon2Chip,
33+
poseidon2::chip::NativePoseidon2Chip,
3434
phantom::*,
3535
*,
3636
};
@@ -203,6 +203,7 @@ impl<F: PrimeField32> VmExtension<F> for Native {
203203
VerifyBatchOpcode::VERIFY_BATCH.global_opcode(),
204204
Poseidon2Opcode::PERM_POS2.global_opcode(),
205205
Poseidon2Opcode::COMP_POS2.global_opcode(),
206+
Poseidon2Opcode::MULTI_OBSERVE.global_opcode(),
206207
],
207208
)?;
208209

0 commit comments

Comments
 (0)