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

Partially Revert Optimize trace relocation #906 #1492

Merged
merged 16 commits into from
Nov 24, 2023
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,17 @@

#### Upcoming Changes

* BREAKING: Partially Revert `Optimize trace relocation #906` [#1492](https://github.com/lambdaclass/cairo-vm/pull/1492)

* Remove methods `VirtualMachine::get_relocated_trace`& `VirtualMachine::relocate_trace`.
* Add `relocated_trace` field & `relocate_trace` method to `CairoRunner`.
* Swap `TraceEntry` for `RelocatedTraceEntry` type in `write_encoded_trace` & `PublicInput::new` signatures.
* Now takes into account the program counter's segment index when building the execution trace instead of assuming it to be 0.

* feat: add debugging capabilities behind `print` feature flag. [#1476](https://github.com/lambdaclass/cairo-vm/pull/1476)

#### [0.9.1] - 2023-11-16


* chore: bump `cairo-lang-` dependencies to 2.3.1 [#1482](https://github.com/lambdaclass/cairo-vm/pull/1482), [#1483](https://github.com/lambdaclass/cairo-vm/pull/1483)

* feat: Make PublicInput fields public [#1474](https://github.com/lambdaclass/cairo-vm/pull/1474)
Expand Down
5 changes: 4 additions & 1 deletion cairo-vm-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,10 @@ fn run(args: impl Iterator<Item = String>) -> Result<(), Error> {
}

if let Some(trace_path) = args.trace_file {
let relocated_trace = vm.get_relocated_trace()?;
let relocated_trace = cairo_runner
.relocated_trace
.as_ref()
.ok_or(Error::Trace(TraceError::TraceNotRelocated))?;

let trace_file = std::fs::File::create(trace_path)?;
let mut trace_writer =
Expand Down
6 changes: 4 additions & 2 deletions cairo1-run/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,12 +294,14 @@ fn run(args: impl Iterator<Item = String>) -> Result<Vec<MaybeRelocatable>, Erro
runner.relocate(&mut vm, true)?;

if let Some(trace_path) = args.trace_file {
let relocated_trace = vm.get_relocated_trace()?;
let relocated_trace = runner
.relocated_trace
.ok_or(Error::Trace(TraceError::TraceNotRelocated))?;
let trace_file = std::fs::File::create(trace_path)?;
let mut trace_writer =
FileWriter::new(io::BufWriter::with_capacity(3 * 1024 * 1024, trace_file));

cairo_run::write_encoded_trace(relocated_trace, &mut trace_writer)?;
cairo_run::write_encoded_trace(&relocated_trace, &mut trace_writer)?;
trace_writer.flush()?;
}
if let Some(memory_path) = args.memory_file {
Expand Down
4 changes: 2 additions & 2 deletions vm/src/air_public_input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::{
types::layout::CairoLayout,
vm::{
errors::{trace_errors::TraceError, vm_errors::VirtualMachineError},
trace::trace_entry::TraceEntry,
trace::trace_entry::RelocatedTraceEntry,
},
};

Expand Down Expand Up @@ -73,7 +73,7 @@ impl<'a> PublicInput<'a> {
dyn_layout_params: Option<&'a CairoLayout>,
public_memory_addresses: &[(usize, usize)],
memory_segment_addresses: HashMap<&'static str, (usize, usize)>,
trace: &[TraceEntry],
trace: &[RelocatedTraceEntry],
Oppen marked this conversation as resolved.
Show resolved Hide resolved
rc_limits: (isize, isize),
) -> Result<Self, PublicInputError> {
let memory_entry =
Expand Down
8 changes: 4 additions & 4 deletions vm/src/cairo_run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ pub struct EncodeTraceError(usize, bincode::error::EncodeError);
/// Bincode encodes to little endian by default and each trace entry is composed of
/// 3 usize values that are padded to always reach 64 bit size.
pub fn write_encoded_trace(
relocated_trace: &[crate::vm::trace::trace_entry::TraceEntry],
relocated_trace: &[crate::vm::trace::trace_entry::RelocatedTraceEntry],
Oppen marked this conversation as resolved.
Show resolved Hide resolved
dest: &mut impl Writer,
) -> Result<(), EncodeTraceError> {
for (i, entry) in relocated_trace.iter().enumerate() {
Expand Down Expand Up @@ -318,11 +318,11 @@ mod tests {
// relocate memory so we can dump it to file
assert!(cairo_runner.relocate(&mut vm, false).is_ok());

let trace_entries = vm.get_relocated_trace().unwrap();
let trace_entries = cairo_runner.relocated_trace.unwrap();
let mut buffer = [0; 24];
let mut buff_writer = SliceWriter::new(&mut buffer);
// write cairo_rs vm trace file
write_encoded_trace(trace_entries, &mut buff_writer).unwrap();
write_encoded_trace(&trace_entries, &mut buff_writer).unwrap();

// compare that the original cairo vm trace file and cairo_rs vm trace files are equal
assert_eq!(buffer, *expected_encoded_trace);
Expand Down Expand Up @@ -369,6 +369,6 @@ mod tests {
.run_until_pc(end, &mut vm, &mut hint_processor)
.is_ok());
assert!(cairo_runner.relocate(&mut vm, false).is_ok());
assert!(vm.get_relocated_trace().is_err());
assert!(cairo_runner.relocated_trace.is_none());
}
}
6 changes: 3 additions & 3 deletions vm/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
use crate::vm::errors::cairo_run_errors::CairoRunError;
#[cfg(feature = "cairo-1-hints")]
use crate::vm::runners::cairo_runner::RunResources;
use crate::vm::trace::trace_entry::RelocatedTraceEntry;
#[cfg(feature = "cairo-1-hints")]
use crate::{
hint_processor::cairo_1_hint_processor::hint_processor::Cairo1HintProcessor,
Expand All @@ -22,7 +23,6 @@ use crate::stdlib::prelude::*;
use crate::{
cairo_run::{cairo_run, CairoRunConfig},
hint_processor::builtin_hint_processor::builtin_hint_processor_definition::BuiltinHintProcessor,
vm::trace::trace_entry::TraceEntry,
};

#[cfg(target_arch = "wasm32")]
Expand Down Expand Up @@ -93,9 +93,9 @@ pub(self) fn run_program(
let expected_trace: Vec<_> = trace
.iter()
.copied()
.map(|(pc, ap, fp)| TraceEntry { pc, ap, fp })
.map(|(pc, ap, fp)| RelocatedTraceEntry { pc, ap, fp })
.collect();
let trace = vm.get_relocated_trace().unwrap();
let trace = runner.relocated_trace.as_ref().unwrap();
assert_eq!(trace.len(), expected_trace.len());
for (entry, expected) in trace.iter().zip(expected_trace.iter()) {
assert_eq!(entry, expected);
Expand Down
20 changes: 15 additions & 5 deletions vm/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,10 @@ pub mod test_utils {
pub(crate) use non_continuous_ids_data;

#[track_caller]
pub(crate) fn trace_check(actual: &[TraceEntry], expected: &[(usize, usize, usize)]) {
pub(crate) fn trace_check(
actual: &[TraceEntry],
expected: &[(crate::utils::Relocatable, usize, usize)],
) {
assert_eq!(actual.len(), expected.len());
for (entry, expected) in actual.iter().zip(expected.iter()) {
assert_eq!(&(entry.pc, entry.ap, entry.fp), expected);
Expand Down Expand Up @@ -704,22 +707,29 @@ mod test {
fn assert_trace() {
let trace = vec![
TraceEntry {
pc: 2,
pc: (0, 2).into(),
ap: 7,
fp: 1,
},
TraceEntry {
pc: 5,
pc: (0, 5).into(),
ap: 1,
fp: 0,
},
TraceEntry {
pc: 9,
pc: (0, 9).into(),
ap: 5,
fp: 7,
},
];
trace_check(&trace, &[(2, 7, 1), (5, 1, 0), (9, 5, 7)]);
trace_check(
&trace,
&[
((0, 2).into(), 7, 1),
((0, 5).into(), 1, 0),
((0, 9).into(), 5, 7),
],
);
}

#[test]
Expand Down
Loading
Loading